blob: a14b86bcf1f965c157937153d6218b7e74a916c5 [file] [log] [blame]
Slava Shklyaev871be942018-09-12 14:52:02 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "neuralnetworks_hidl_hal_test"
18
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010019#include "1.0/Utils.h"
20#include "1.2/Callbacks.h"
Xusong Wangbcaa7822019-08-23 16:10:54 -070021#include "GeneratedTestHarness.h"
Slava Shklyaev871be942018-09-12 14:52:02 +010022#include "VtsHalNeuralnetworks.h"
23
Michael Butler62749b92019-08-26 23:55:47 -070024namespace android::hardware::neuralnetworks::V1_2::vts::functional {
Slava Shklyaev871be942018-09-12 14:52:02 +010025
Michael Butler62749b92019-08-26 23:55:47 -070026using implementation::PreparedModelCallback;
27using V1_0::ErrorStatus;
Slava Shklyaev871be942018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
Xusong Wangb61ba1e2019-02-25 16:58:58 -080030using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Slava Shklyaev871be942018-09-12 14:52:02 +010031
32///////////////////////// UTILITY FUNCTIONS /////////////////////////
33
34static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
35 const Model& model) {
36 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
37
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010038 Return<void> ret = device->getSupportedOperations_1_2(
39 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
40 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
41 });
Slava Shklyaev871be942018-09-12 14:52:02 +010042 EXPECT_TRUE(ret.isOk());
43}
44
45static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
46 const Model& model, ExecutionPreference preference) {
47 SCOPED_TRACE(message + " [prepareModel_1_2]");
48
49 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
Slava Shklyaev871be942018-09-12 14:52:02 +010050 Return<ErrorStatus> prepareLaunchStatus =
Xusong Wangb61ba1e2019-02-25 16:58:58 -080051 device->prepareModel_1_2(model, preference, hidl_vec<hidl_handle>(),
52 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010053 ASSERT_TRUE(prepareLaunchStatus.isOk());
54 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
55
56 preparedModelCallback->wait();
57 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
58 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070059 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010060 ASSERT_EQ(nullptr, preparedModel.get());
61}
62
63static bool validExecutionPreference(ExecutionPreference preference) {
64 return preference == ExecutionPreference::LOW_POWER ||
65 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
66 preference == ExecutionPreference::SUSTAINED_SPEED;
67}
68
69// Primary validation function. This function will take a valid model, apply a
70// mutation to it to invalidate the model, then pass it to interface calls that
71// use the model. Note that the model here is passed by value, and any mutation
72// to the model does not leave this function.
73static void validate(const sp<IDevice>& device, const std::string& message, Model model,
74 const std::function<void(Model*)>& mutation,
75 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
76 mutation(&model);
77 if (validExecutionPreference(preference)) {
78 validateGetSupportedOperations(device, message, model);
79 }
80 validatePrepareModel(device, message, model, preference);
81}
82
Slava Shklyaev871be942018-09-12 14:52:02 +010083static uint32_t addOperand(Model* model) {
84 return hidl_vec_push_back(&model->operands,
85 {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010086 .type = OperandType::INT32,
87 .dimensions = {},
88 .numberOfConsumers = 0,
89 .scale = 0.0f,
90 .zeroPoint = 0,
91 .lifetime = OperandLifeTime::MODEL_INPUT,
92 .location = {.poolIndex = 0, .offset = 0, .length = 0},
Slava Shklyaev871be942018-09-12 14:52:02 +010093 });
94}
95
96static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
97 uint32_t index = addOperand(model);
98 model->operands[index].numberOfConsumers = 1;
99 model->operands[index].lifetime = lifetime;
100 return index;
101}
102
103///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
104
Michael K. Sandersc785d462018-10-30 15:16:54 +0000105static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000106 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
107 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
108 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
109 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100110};
111
112static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
113 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000114 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100115 const std::string message = "mutateOperandTypeTest: operand " +
116 std::to_string(operand) + " set to value " +
117 std::to_string(invalidOperandType);
118 validate(device, message, model, [operand, invalidOperandType](Model* model) {
119 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
120 });
121 }
122 }
123}
124
125///////////////////////// VALIDATE OPERAND RANK /////////////////////////
126
127static uint32_t getInvalidRank(OperandType type) {
128 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800129 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100130 case OperandType::FLOAT32:
131 case OperandType::INT32:
132 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100133 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100134 return 1;
Lev Proleev923b8c52019-01-30 17:14:40 +0000135 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100136 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100137 case OperandType::TENSOR_FLOAT32:
138 case OperandType::TENSOR_INT32:
139 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800140 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800141 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000142 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000143 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100144 return 0;
145 default:
146 return 0;
147 }
148}
149
150static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
151 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
152 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wanga3165812018-11-19 18:26:08 -0800153 if (invalidRank == 0) {
154 continue;
155 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100156 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
157 " has rank of " + std::to_string(invalidRank);
158 validate(device, message, model, [operand, invalidRank](Model* model) {
159 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
160 });
161 }
162}
163
164///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
165
166static float getInvalidScale(OperandType type) {
167 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800168 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100169 case OperandType::FLOAT32:
170 case OperandType::INT32:
171 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100172 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000173 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100174 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100175 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000176 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100177 return 1.0f;
178 case OperandType::TENSOR_INT32:
179 return -1.0f;
Hervé Guihotbae91692019-01-23 19:18:59 -0800180 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100181 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800182 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000183 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100184 return 0.0f;
185 default:
186 return 0.0f;
187 }
188}
189
190static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
191 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
192 const float invalidScale = getInvalidScale(model.operands[operand].type);
193 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
194 " has scale of " + std::to_string(invalidScale);
195 validate(device, message, model, [operand, invalidScale](Model* model) {
196 model->operands[operand].scale = invalidScale;
197 });
198 }
199}
200
201///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
202
203static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
204 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800205 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100206 case OperandType::FLOAT32:
207 case OperandType::INT32:
208 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100209 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000210 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100211 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100212 case OperandType::TENSOR_FLOAT32:
213 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000214 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100215 return {1};
216 case OperandType::TENSOR_QUANT8_ASYMM:
217 return {-1, 256};
Hervé Guihotbae91692019-01-23 19:18:59 -0800218 case OperandType::TENSOR_QUANT8_SYMM:
219 return {-129, -1, 1, 128};
Xusong Wangd49f6652019-01-16 18:32:24 -0800220 case OperandType::TENSOR_QUANT16_ASYMM:
221 return {-1, 65536};
Lev Proleev48c88202018-11-13 15:42:36 +0000222 case OperandType::TENSOR_QUANT16_SYMM:
223 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100224 default:
225 return {};
226 }
227}
228
229static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
230 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
231 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100232 getInvalidZeroPoints(model.operands[operand].type);
Slava Shklyaev871be942018-09-12 14:52:02 +0100233 for (int32_t invalidZeroPoint : invalidZeroPoints) {
234 const std::string message = "mutateOperandZeroPointTest: operand " +
235 std::to_string(operand) + " has zero point of " +
236 std::to_string(invalidZeroPoint);
237 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
238 model->operands[operand].zeroPoint = invalidZeroPoint;
239 });
240 }
241 }
242}
243
244///////////////////////// VALIDATE EXTRA ??? /////////////////////////
245
246// TODO: Operand::lifetime
247// TODO: Operand::location
248
249///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
250
251static void mutateOperand(Operand* operand, OperandType type) {
252 Operand newOperand = *operand;
253 newOperand.type = type;
254 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800255 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100256 case OperandType::FLOAT32:
257 case OperandType::INT32:
258 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100259 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100260 newOperand.dimensions = hidl_vec<uint32_t>();
261 newOperand.scale = 0.0f;
262 newOperand.zeroPoint = 0;
263 break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000264 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100265 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100266 case OperandType::TENSOR_FLOAT32:
267 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100268 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100269 newOperand.scale = 0.0f;
270 newOperand.zeroPoint = 0;
271 break;
272 case OperandType::TENSOR_INT32:
273 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100274 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100275 newOperand.zeroPoint = 0;
276 break;
277 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800278 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800279 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000280 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100281 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100282 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100283 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
284 break;
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000285 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
286 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100287 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000288 newOperand.scale = 0.0f;
289 newOperand.zeroPoint = 0;
290
291 SymmPerChannelQuantParams channelQuant;
292 channelQuant.channelDim = 0;
293 channelQuant.scales = hidl_vec<float>(
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100294 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
295 : 0);
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000296 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
297 channelQuant.scales[i] = 1.0f;
298 }
299 newOperand.extraParams.channelQuant(std::move(channelQuant));
300 } break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100301 case OperandType::OEM:
302 case OperandType::TENSOR_OEM_BYTE:
303 default:
304 break;
305 }
306 *operand = newOperand;
307}
308
Xusong Wang5b747ae2018-10-05 11:49:13 -0700309static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
310 // Do not test OEM types
311 if (type == model.operands[operand].type || type == OperandType::OEM ||
312 type == OperandType::TENSOR_OEM_BYTE) {
313 return true;
314 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100315 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700316 // Skip mutateOperationOperandTypeTest for the following operations.
317 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000318 // - ARGMIN and ARGMAX's first argument can be any of
319 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
320 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000321 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev923b8c52019-01-30 17:14:40 +0000322 // - DEQUANTIZE input can be any of
323 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
324 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
325 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000326 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000327 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb0762cc2019-01-15 17:53:46 +0000328 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev1509a262019-01-15 17:49:24 +0000329 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang5b747ae2018-10-05 11:49:13 -0700330 switch (operation.type) {
331 case OperationType::LSH_PROJECTION: {
332 if (operand == operation.inputs[1]) {
333 return true;
334 }
335 } break;
336 case OperationType::CAST:
337 case OperationType::ARGMAX:
338 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000339 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
340 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700341 return true;
342 }
343 } break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000344 case OperationType::QUANTIZE:
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000345 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleev923b8c52019-01-30 17:14:40 +0000346 if (operand == operation.inputs[0] &&
347 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
348 return true;
349 }
350 } break;
351 case OperationType::DEQUANTIZE: {
352 if (operand == operation.inputs[0] &&
353 (type == OperandType::TENSOR_QUANT8_ASYMM ||
354 type == OperandType::TENSOR_QUANT8_SYMM ||
355 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
356 return true;
357 }
358 if (operand == operation.outputs[0] &&
359 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000360 return true;
361 }
362 } break;
Lev Proleev1509a262019-01-15 17:49:24 +0000363 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb0762cc2019-01-15 17:53:46 +0000364 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000365 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000366 case OperationType::CONV_2D: {
Xusong Wang88044232019-03-13 16:24:34 -0700367 if (operand == operation.inputs[1] &&
368 (type == OperandType::TENSOR_QUANT8_ASYMM ||
369 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000370 return true;
371 }
372 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700373 default:
374 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100375 }
376 }
377 return false;
378}
379
380static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
381 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100382 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700383 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100384 continue;
385 }
386 const std::string message = "mutateOperationOperandTypeTest: operand " +
387 std::to_string(operand) + " set to type " +
388 toString(invalidOperandType);
389 validate(device, message, model, [operand, invalidOperandType](Model* model) {
390 mutateOperand(&model->operands[operand], invalidOperandType);
391 });
392 }
393 }
394}
395
396///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
397
Michael K. Sandersc785d462018-10-30 15:16:54 +0000398static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000399 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
400 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
401 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100402};
403
404static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
405 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000406 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100407 const std::string message = "mutateOperationTypeTest: operation " +
408 std::to_string(operation) + " set to value " +
409 std::to_string(invalidOperationType);
410 validate(device, message, model, [operation, invalidOperationType](Model* model) {
411 model->operations[operation].type =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100412 static_cast<OperationType>(invalidOperationType);
Slava Shklyaev871be942018-09-12 14:52:02 +0100413 });
414 }
415 }
416}
417
418///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
419
420static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
421 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
422 const uint32_t invalidOperand = model.operands.size();
423 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
424 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
425 std::to_string(operation) + " input " +
426 std::to_string(input);
427 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
428 model->operations[operation].inputs[input] = invalidOperand;
429 });
430 }
431 }
432}
433
434///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
435
436static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
437 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
438 const uint32_t invalidOperand = model.operands.size();
439 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
440 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
441 std::to_string(operation) + " output " +
442 std::to_string(output);
443 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
444 model->operations[operation].outputs[output] = invalidOperand;
445 });
446 }
447 }
448}
449
450///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
451
452static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
453 if (vec) {
454 // remove elements matching "value"
455 auto last = std::remove(vec->begin(), vec->end(), value);
456 vec->resize(std::distance(vec->begin(), last));
457
458 // decrement elements exceeding "value"
459 std::transform(vec->begin(), vec->end(), vec->begin(),
460 [value](uint32_t v) { return v > value ? v-- : v; });
461 }
462}
463
464static void removeOperand(Model* model, uint32_t index) {
465 hidl_vec_removeAt(&model->operands, index);
466 for (Operation& operation : model->operations) {
467 removeValueAndDecrementGreaterValues(&operation.inputs, index);
468 removeValueAndDecrementGreaterValues(&operation.outputs, index);
469 }
470 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
471 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
472}
473
Xusong Wang5b747ae2018-10-05 11:49:13 -0700474static bool removeOperandSkip(size_t operand, const Model& model) {
475 for (const Operation& operation : model.operations) {
476 // Skip removeOperandTest for the following operations.
477 // - SPLIT's outputs are not checked during prepareModel.
478 if (operation.type == OperationType::SPLIT) {
479 for (const size_t outOprand : operation.outputs) {
480 if (operand == outOprand) {
481 return true;
482 }
483 }
484 }
Viet Dang723ff2d2019-03-28 17:22:56 +0000485 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have
486 // either one or two outputs depending on their mergeOutputs parameter.
487 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
488 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
489 for (const size_t outOprand : operation.outputs) {
490 if (operand == outOprand) {
491 return true;
Lev Proleev923b8c52019-01-30 17:14:40 +0000492 }
Viet Dang723ff2d2019-03-28 17:22:56 +0000493 }
Lev Proleev923b8c52019-01-30 17:14:40 +0000494 }
Xusong Wang5b747ae2018-10-05 11:49:13 -0700495 }
496 return false;
497}
498
Slava Shklyaev871be942018-09-12 14:52:02 +0100499static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
500 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700501 if (removeOperandSkip(operand, model)) {
502 continue;
503 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100504 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
505 validate(device, message, model,
506 [operand](Model* model) { removeOperand(model, operand); });
507 }
508}
509
510///////////////////////// REMOVE OPERATION /////////////////////////
511
512static void removeOperation(Model* model, uint32_t index) {
513 for (uint32_t operand : model->operations[index].inputs) {
514 model->operands[operand].numberOfConsumers--;
515 }
516 hidl_vec_removeAt(&model->operations, index);
517}
518
519static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
520 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
521 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
522 validate(device, message, model,
523 [operation](Model* model) { removeOperation(model, operation); });
524 }
525}
526
527///////////////////////// REMOVE OPERATION INPUT /////////////////////////
528
Xusong Wang5b747ae2018-10-05 11:49:13 -0700529static bool removeOperationInputSkip(const Operation& op, size_t input) {
530 // Skip removeOperationInputTest for the following operations.
531 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
532 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
533 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
534 // layout parameter.
535 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
536 // parameter.
537 switch (op.type) {
538 case OperationType::CONCATENATION: {
539 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
540 return true;
541 }
542 } break;
543 case OperationType::DEPTHWISE_CONV_2D: {
544 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
545 return true;
546 }
547 } break;
548 case OperationType::CONV_2D:
549 case OperationType::AVERAGE_POOL_2D:
550 case OperationType::MAX_POOL_2D:
551 case OperationType::L2_POOL_2D: {
552 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
553 return true;
554 }
555 } break;
556 case OperationType::RESIZE_BILINEAR: {
557 if (op.inputs.size() == 4 && input == 3) {
558 return true;
559 }
560 } break;
561 case OperationType::SPACE_TO_DEPTH:
562 case OperationType::DEPTH_TO_SPACE:
563 case OperationType::BATCH_TO_SPACE_ND: {
564 if (op.inputs.size() == 3 && input == 2) {
565 return true;
566 }
567 } break;
568 case OperationType::SPACE_TO_BATCH_ND: {
569 if (op.inputs.size() == 4 && input == 3) {
570 return true;
571 }
572 } break;
573 case OperationType::L2_NORMALIZATION: {
574 if (op.inputs.size() == 2 && input == 1) {
575 return true;
576 }
577 } break;
578 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
579 if (op.inputs.size() == 6 && input == 5) {
580 return true;
581 }
582 } break;
583 case OperationType::SOFTMAX: {
584 if (op.inputs.size() == 3 && input == 2) {
585 return true;
586 }
587 } break;
588 default:
589 break;
590 }
591 return false;
592}
593
Slava Shklyaev871be942018-09-12 14:52:02 +0100594static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
595 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
596 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
597 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700598 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100599 continue;
600 }
601 const std::string message = "removeOperationInputTest: operation " +
602 std::to_string(operation) + ", input " +
603 std::to_string(input);
604 validate(device, message, model, [operation, input](Model* model) {
605 uint32_t operand = model->operations[operation].inputs[input];
606 model->operands[operand].numberOfConsumers--;
607 hidl_vec_removeAt(&model->operations[operation].inputs, input);
608 });
609 }
610 }
611}
612
613///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
614
615static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
616 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
617 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
618 const std::string message = "removeOperationOutputTest: operation " +
619 std::to_string(operation) + ", output " +
620 std::to_string(output);
621 validate(device, message, model, [operation, output](Model* model) {
622 hidl_vec_removeAt(&model->operations[operation].outputs, output);
623 });
624 }
625 }
626}
627
628///////////////////////// MODEL VALIDATION /////////////////////////
629
630// TODO: remove model input
631// TODO: remove model output
632// TODO: add unused operation
633
634///////////////////////// ADD OPERATION INPUT /////////////////////////
635
Xusong Wang5b747ae2018-10-05 11:49:13 -0700636static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700637 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700638 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
639 // parameter.
640 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
641 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
642 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700643 return true;
644 }
645 return false;
646}
647
Slava Shklyaev871be942018-09-12 14:52:02 +0100648static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
649 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700650 if (addOperationInputSkip(model.operations[operation])) {
651 continue;
652 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100653 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
654 validate(device, message, model, [operation](Model* model) {
655 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
656 hidl_vec_push_back(&model->operations[operation].inputs, index);
657 hidl_vec_push_back(&model->inputIndexes, index);
658 });
659 }
660}
661
662///////////////////////// ADD OPERATION OUTPUT /////////////////////////
663
664static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
665 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
666 const std::string message =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100667 "addOperationOutputTest: operation " + std::to_string(operation);
Slava Shklyaev871be942018-09-12 14:52:02 +0100668 validate(device, message, model, [operation](Model* model) {
669 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
670 hidl_vec_push_back(&model->operations[operation].outputs, index);
671 hidl_vec_push_back(&model->outputIndexes, index);
672 });
673 }
674}
675
676///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
677
678static const int32_t invalidExecutionPreferences[] = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100679 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
680 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
Slava Shklyaev871be942018-09-12 14:52:02 +0100681};
682
683static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
684 for (int32_t preference : invalidExecutionPreferences) {
685 const std::string message =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100686 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
Michael Butler62749b92019-08-26 23:55:47 -0700687 validate(
688 device, message, model, [](Model*) {},
689 static_cast<ExecutionPreference>(preference));
Slava Shklyaev871be942018-09-12 14:52:02 +0100690 }
691}
692
693////////////////////////// ENTRY POINT //////////////////////////////
694
Michael Butler13b05162019-08-29 22:17:24 -0700695void validateModel(const sp<IDevice>& device, const Model& model) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100696 mutateOperandTypeTest(device, model);
697 mutateOperandRankTest(device, model);
698 mutateOperandScaleTest(device, model);
699 mutateOperandZeroPointTest(device, model);
700 mutateOperationOperandTypeTest(device, model);
701 mutateOperationTypeTest(device, model);
702 mutateOperationInputOperandIndexTest(device, model);
703 mutateOperationOutputOperandIndexTest(device, model);
704 removeOperandTest(device, model);
705 removeOperationTest(device, model);
706 removeOperationInputTest(device, model);
707 removeOperationOutputTest(device, model);
708 addOperationInputTest(device, model);
709 addOperationOutputTest(device, model);
710 mutateExecutionPreferenceTest(device, model);
711}
712
Michael Butler62749b92019-08-26 23:55:47 -0700713} // namespace android::hardware::neuralnetworks::V1_2::vts::functional