blob: a0f11ebbaf6f98d917b6663ca3c6259d049053b9 [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
19#include "VtsHalNeuralnetworks.h"
20
21#include "Callbacks.h"
22
23namespace android {
24namespace hardware {
25namespace neuralnetworks {
26namespace V1_2 {
27
Slava Shklyaev871be942018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
30
31namespace vts {
32namespace functional {
33
Xusong Wangb5cb8f72018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
35using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaev871be942018-09-12 14:52:02 +010036
37///////////////////////// UTILITY FUNCTIONS /////////////////////////
38
39static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
40 const Model& model) {
41 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
42
43 Return<void> ret =
44 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
45 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
46 });
47 EXPECT_TRUE(ret.isOk());
48}
49
50static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
51 const Model& model, ExecutionPreference preference) {
52 SCOPED_TRACE(message + " [prepareModel_1_2]");
53
54 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
55 ASSERT_NE(nullptr, preparedModelCallback.get());
56 Return<ErrorStatus> prepareLaunchStatus =
57 device->prepareModel_1_2(model, preference, preparedModelCallback);
58 ASSERT_TRUE(prepareLaunchStatus.isOk());
59 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
60
61 preparedModelCallback->wait();
62 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
63 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070064 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010065 ASSERT_EQ(nullptr, preparedModel.get());
66}
67
68static bool validExecutionPreference(ExecutionPreference preference) {
69 return preference == ExecutionPreference::LOW_POWER ||
70 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
71 preference == ExecutionPreference::SUSTAINED_SPEED;
72}
73
74// Primary validation function. This function will take a valid model, apply a
75// mutation to it to invalidate the model, then pass it to interface calls that
76// use the model. Note that the model here is passed by value, and any mutation
77// to the model does not leave this function.
78static void validate(const sp<IDevice>& device, const std::string& message, Model model,
79 const std::function<void(Model*)>& mutation,
80 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
81 mutation(&model);
82 if (validExecutionPreference(preference)) {
83 validateGetSupportedOperations(device, message, model);
84 }
85 validatePrepareModel(device, message, model, preference);
86}
87
88// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
89// so this is efficiently accomplished by moving the element to the end and
90// resizing the hidl_vec to one less.
91template <typename Type>
92static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
93 if (vec) {
94 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
95 vec->resize(vec->size() - 1);
96 }
97}
98
99template <typename Type>
100static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
101 // assume vec is valid
102 const uint32_t index = vec->size();
103 vec->resize(index + 1);
104 (*vec)[index] = value;
105 return index;
106}
107
108static uint32_t addOperand(Model* model) {
109 return hidl_vec_push_back(&model->operands,
110 {
111 .type = OperandType::INT32,
112 .dimensions = {},
113 .numberOfConsumers = 0,
114 .scale = 0.0f,
115 .zeroPoint = 0,
116 .lifetime = OperandLifeTime::MODEL_INPUT,
117 .location = {.poolIndex = 0, .offset = 0, .length = 0},
118 });
119}
120
121static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
122 uint32_t index = addOperand(model);
123 model->operands[index].numberOfConsumers = 1;
124 model->operands[index].lifetime = lifetime;
125 return index;
126}
127
128///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
129
Michael K. Sandersc785d462018-10-30 15:16:54 +0000130static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000131 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
132 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
133 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
134 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100135};
136
137static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
138 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000139 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100140 const std::string message = "mutateOperandTypeTest: operand " +
141 std::to_string(operand) + " set to value " +
142 std::to_string(invalidOperandType);
143 validate(device, message, model, [operand, invalidOperandType](Model* model) {
144 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
145 });
146 }
147 }
148}
149
150///////////////////////// VALIDATE OPERAND RANK /////////////////////////
151
152static uint32_t getInvalidRank(OperandType type) {
153 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800154 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100155 case OperandType::FLOAT32:
156 case OperandType::INT32:
157 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100158 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100159 return 1;
Michael K. Sanders19d63452018-10-12 09:10:15 +0100160 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100161 case OperandType::TENSOR_FLOAT32:
162 case OperandType::TENSOR_INT32:
163 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800164 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800165 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000166 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000167 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100168 return 0;
169 default:
170 return 0;
171 }
172}
173
174static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
175 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
176 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wanga3165812018-11-19 18:26:08 -0800177 if (invalidRank == 0) {
178 continue;
179 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100180 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
181 " has rank of " + std::to_string(invalidRank);
182 validate(device, message, model, [operand, invalidRank](Model* model) {
183 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
184 });
185 }
186}
187
188///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
189
190static float getInvalidScale(OperandType type) {
191 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800192 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100193 case OperandType::FLOAT32:
194 case OperandType::INT32:
195 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100196 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100197 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100198 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000199 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100200 return 1.0f;
201 case OperandType::TENSOR_INT32:
202 return -1.0f;
Hervé Guihotbae91692019-01-23 19:18:59 -0800203 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100204 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800205 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000206 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100207 return 0.0f;
208 default:
209 return 0.0f;
210 }
211}
212
213static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
214 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
215 const float invalidScale = getInvalidScale(model.operands[operand].type);
216 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
217 " has scale of " + std::to_string(invalidScale);
218 validate(device, message, model, [operand, invalidScale](Model* model) {
219 model->operands[operand].scale = invalidScale;
220 });
221 }
222}
223
224///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
225
226static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
227 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800228 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100229 case OperandType::FLOAT32:
230 case OperandType::INT32:
231 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100232 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100233 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100234 case OperandType::TENSOR_FLOAT32:
235 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000236 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100237 return {1};
238 case OperandType::TENSOR_QUANT8_ASYMM:
239 return {-1, 256};
Hervé Guihotbae91692019-01-23 19:18:59 -0800240 case OperandType::TENSOR_QUANT8_SYMM:
241 return {-129, -1, 1, 128};
Xusong Wangd49f6652019-01-16 18:32:24 -0800242 case OperandType::TENSOR_QUANT16_ASYMM:
243 return {-1, 65536};
Lev Proleev48c88202018-11-13 15:42:36 +0000244 case OperandType::TENSOR_QUANT16_SYMM:
245 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100246 default:
247 return {};
248 }
249}
250
251static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
252 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
253 const std::vector<int32_t> invalidZeroPoints =
254 getInvalidZeroPoints(model.operands[operand].type);
255 for (int32_t invalidZeroPoint : invalidZeroPoints) {
256 const std::string message = "mutateOperandZeroPointTest: operand " +
257 std::to_string(operand) + " has zero point of " +
258 std::to_string(invalidZeroPoint);
259 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
260 model->operands[operand].zeroPoint = invalidZeroPoint;
261 });
262 }
263 }
264}
265
266///////////////////////// VALIDATE EXTRA ??? /////////////////////////
267
268// TODO: Operand::lifetime
269// TODO: Operand::location
270
271///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
272
273static void mutateOperand(Operand* operand, OperandType type) {
274 Operand newOperand = *operand;
275 newOperand.type = type;
276 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800277 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100278 case OperandType::FLOAT32:
279 case OperandType::INT32:
280 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100281 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100282 newOperand.dimensions = hidl_vec<uint32_t>();
283 newOperand.scale = 0.0f;
284 newOperand.zeroPoint = 0;
285 break;
Michael K. Sanders19d63452018-10-12 09:10:15 +0100286 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100287 case OperandType::TENSOR_FLOAT32:
288 newOperand.dimensions =
289 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
290 newOperand.scale = 0.0f;
291 newOperand.zeroPoint = 0;
292 break;
293 case OperandType::TENSOR_INT32:
294 newOperand.dimensions =
295 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
296 newOperand.zeroPoint = 0;
297 break;
298 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800299 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800300 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000301 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100302 newOperand.dimensions =
303 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
304 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
305 break;
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000306 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
307 newOperand.dimensions =
308 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
309 newOperand.scale = 0.0f;
310 newOperand.zeroPoint = 0;
311
312 SymmPerChannelQuantParams channelQuant;
313 channelQuant.channelDim = 0;
314 channelQuant.scales = hidl_vec<float>(
315 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0]) : 0);
316 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
317 channelQuant.scales[i] = 1.0f;
318 }
319 newOperand.extraParams.channelQuant(std::move(channelQuant));
320 } break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100321 case OperandType::OEM:
322 case OperandType::TENSOR_OEM_BYTE:
323 default:
324 break;
325 }
326 *operand = newOperand;
327}
328
Xusong Wang5b747ae2018-10-05 11:49:13 -0700329static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
330 // Do not test OEM types
331 if (type == model.operands[operand].type || type == OperandType::OEM ||
332 type == OperandType::TENSOR_OEM_BYTE) {
333 return true;
334 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100335 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700336 // Skip mutateOperationOperandTypeTest for the following operations.
337 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000338 // - ARGMIN and ARGMAX's first argument can be any of
339 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
340 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000341 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000342 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000343 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb0762cc2019-01-15 17:53:46 +0000344 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev1509a262019-01-15 17:49:24 +0000345 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang5b747ae2018-10-05 11:49:13 -0700346 switch (operation.type) {
347 case OperationType::LSH_PROJECTION: {
348 if (operand == operation.inputs[1]) {
349 return true;
350 }
351 } break;
352 case OperationType::CAST:
353 case OperationType::ARGMAX:
354 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000355 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
356 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700357 return true;
358 }
359 } break;
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000360 case OperationType::RANDOM_MULTINOMIAL: {
361 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
362 return true;
363 }
364 } break;
Lev Proleev1509a262019-01-15 17:49:24 +0000365 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb0762cc2019-01-15 17:53:46 +0000366 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000367 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000368 case OperationType::CONV_2D: {
369 if (operand == 1 && (type == OperandType::TENSOR_QUANT8_ASYMM ||
370 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
371 return true;
372 }
373 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700374 default:
375 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100376 }
377 }
378 return false;
379}
380
381static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
382 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100383 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700384 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100385 continue;
386 }
387 const std::string message = "mutateOperationOperandTypeTest: operand " +
388 std::to_string(operand) + " set to type " +
389 toString(invalidOperandType);
390 validate(device, message, model, [operand, invalidOperandType](Model* model) {
391 mutateOperand(&model->operands[operand], invalidOperandType);
392 });
393 }
394 }
395}
396
397///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
398
Michael K. Sandersc785d462018-10-30 15:16:54 +0000399static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000400 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MIN) - 1,
401 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
402 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
403 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100404};
405
406static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
407 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000408 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100409 const std::string message = "mutateOperationTypeTest: operation " +
410 std::to_string(operation) + " set to value " +
411 std::to_string(invalidOperationType);
412 validate(device, message, model, [operation, invalidOperationType](Model* model) {
413 model->operations[operation].type =
414 static_cast<OperationType>(invalidOperationType);
415 });
416 }
417 }
418}
419
420///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
421
422static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
423 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
424 const uint32_t invalidOperand = model.operands.size();
425 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
426 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
427 std::to_string(operation) + " input " +
428 std::to_string(input);
429 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
430 model->operations[operation].inputs[input] = invalidOperand;
431 });
432 }
433 }
434}
435
436///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
437
438static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
439 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
440 const uint32_t invalidOperand = model.operands.size();
441 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
442 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
443 std::to_string(operation) + " output " +
444 std::to_string(output);
445 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
446 model->operations[operation].outputs[output] = invalidOperand;
447 });
448 }
449 }
450}
451
452///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
453
454static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
455 if (vec) {
456 // remove elements matching "value"
457 auto last = std::remove(vec->begin(), vec->end(), value);
458 vec->resize(std::distance(vec->begin(), last));
459
460 // decrement elements exceeding "value"
461 std::transform(vec->begin(), vec->end(), vec->begin(),
462 [value](uint32_t v) { return v > value ? v-- : v; });
463 }
464}
465
466static void removeOperand(Model* model, uint32_t index) {
467 hidl_vec_removeAt(&model->operands, index);
468 for (Operation& operation : model->operations) {
469 removeValueAndDecrementGreaterValues(&operation.inputs, index);
470 removeValueAndDecrementGreaterValues(&operation.outputs, index);
471 }
472 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
473 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
474}
475
Xusong Wang5b747ae2018-10-05 11:49:13 -0700476static bool removeOperandSkip(size_t operand, const Model& model) {
477 for (const Operation& operation : model.operations) {
478 // Skip removeOperandTest for the following operations.
479 // - SPLIT's outputs are not checked during prepareModel.
480 if (operation.type == OperationType::SPLIT) {
481 for (const size_t outOprand : operation.outputs) {
482 if (operand == outOprand) {
483 return true;
484 }
485 }
486 }
487 }
488 return false;
489}
490
Slava Shklyaev871be942018-09-12 14:52:02 +0100491static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
492 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700493 if (removeOperandSkip(operand, model)) {
494 continue;
495 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100496 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
497 validate(device, message, model,
498 [operand](Model* model) { removeOperand(model, operand); });
499 }
500}
501
502///////////////////////// REMOVE OPERATION /////////////////////////
503
504static void removeOperation(Model* model, uint32_t index) {
505 for (uint32_t operand : model->operations[index].inputs) {
506 model->operands[operand].numberOfConsumers--;
507 }
508 hidl_vec_removeAt(&model->operations, index);
509}
510
511static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
512 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
513 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
514 validate(device, message, model,
515 [operation](Model* model) { removeOperation(model, operation); });
516 }
517}
518
519///////////////////////// REMOVE OPERATION INPUT /////////////////////////
520
Xusong Wang5b747ae2018-10-05 11:49:13 -0700521static bool removeOperationInputSkip(const Operation& op, size_t input) {
522 // Skip removeOperationInputTest for the following operations.
523 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
524 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
525 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
526 // layout parameter.
527 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
528 // parameter.
529 switch (op.type) {
530 case OperationType::CONCATENATION: {
531 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
532 return true;
533 }
534 } break;
535 case OperationType::DEPTHWISE_CONV_2D: {
536 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
537 return true;
538 }
539 } break;
540 case OperationType::CONV_2D:
541 case OperationType::AVERAGE_POOL_2D:
542 case OperationType::MAX_POOL_2D:
543 case OperationType::L2_POOL_2D: {
544 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
545 return true;
546 }
547 } break;
548 case OperationType::RESIZE_BILINEAR: {
549 if (op.inputs.size() == 4 && input == 3) {
550 return true;
551 }
552 } break;
553 case OperationType::SPACE_TO_DEPTH:
554 case OperationType::DEPTH_TO_SPACE:
555 case OperationType::BATCH_TO_SPACE_ND: {
556 if (op.inputs.size() == 3 && input == 2) {
557 return true;
558 }
559 } break;
560 case OperationType::SPACE_TO_BATCH_ND: {
561 if (op.inputs.size() == 4 && input == 3) {
562 return true;
563 }
564 } break;
565 case OperationType::L2_NORMALIZATION: {
566 if (op.inputs.size() == 2 && input == 1) {
567 return true;
568 }
569 } break;
570 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
571 if (op.inputs.size() == 6 && input == 5) {
572 return true;
573 }
574 } break;
575 case OperationType::SOFTMAX: {
576 if (op.inputs.size() == 3 && input == 2) {
577 return true;
578 }
579 } break;
580 default:
581 break;
582 }
583 return false;
584}
585
Slava Shklyaev871be942018-09-12 14:52:02 +0100586static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
587 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
588 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
589 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700590 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100591 continue;
592 }
593 const std::string message = "removeOperationInputTest: operation " +
594 std::to_string(operation) + ", input " +
595 std::to_string(input);
596 validate(device, message, model, [operation, input](Model* model) {
597 uint32_t operand = model->operations[operation].inputs[input];
598 model->operands[operand].numberOfConsumers--;
599 hidl_vec_removeAt(&model->operations[operation].inputs, input);
600 });
601 }
602 }
603}
604
605///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
606
607static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
608 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
609 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
610 const std::string message = "removeOperationOutputTest: operation " +
611 std::to_string(operation) + ", output " +
612 std::to_string(output);
613 validate(device, message, model, [operation, output](Model* model) {
614 hidl_vec_removeAt(&model->operations[operation].outputs, output);
615 });
616 }
617 }
618}
619
620///////////////////////// MODEL VALIDATION /////////////////////////
621
622// TODO: remove model input
623// TODO: remove model output
624// TODO: add unused operation
625
626///////////////////////// ADD OPERATION INPUT /////////////////////////
627
Xusong Wang5b747ae2018-10-05 11:49:13 -0700628static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700629 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700630 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
631 // parameter.
632 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
633 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
634 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700635 return true;
636 }
637 return false;
638}
639
Slava Shklyaev871be942018-09-12 14:52:02 +0100640static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
641 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700642 if (addOperationInputSkip(model.operations[operation])) {
643 continue;
644 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100645 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
646 validate(device, message, model, [operation](Model* model) {
647 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
648 hidl_vec_push_back(&model->operations[operation].inputs, index);
649 hidl_vec_push_back(&model->inputIndexes, index);
650 });
651 }
652}
653
654///////////////////////// ADD OPERATION OUTPUT /////////////////////////
655
656static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
657 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
658 const std::string message =
659 "addOperationOutputTest: operation " + std::to_string(operation);
660 validate(device, message, model, [operation](Model* model) {
661 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
662 hidl_vec_push_back(&model->operations[operation].outputs, index);
663 hidl_vec_push_back(&model->outputIndexes, index);
664 });
665 }
666}
667
668///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
669
670static const int32_t invalidExecutionPreferences[] = {
671 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
672 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
673};
674
675static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
676 for (int32_t preference : invalidExecutionPreferences) {
677 const std::string message =
678 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
679 validate(device, message, model, [](Model*) {},
680 static_cast<ExecutionPreference>(preference));
681 }
682}
683
684////////////////////////// ENTRY POINT //////////////////////////////
685
686void ValidationTest::validateModel(const Model& model) {
687 mutateOperandTypeTest(device, model);
688 mutateOperandRankTest(device, model);
689 mutateOperandScaleTest(device, model);
690 mutateOperandZeroPointTest(device, model);
691 mutateOperationOperandTypeTest(device, model);
692 mutateOperationTypeTest(device, model);
693 mutateOperationInputOperandIndexTest(device, model);
694 mutateOperationOutputOperandIndexTest(device, model);
695 removeOperandTest(device, model);
696 removeOperationTest(device, model);
697 removeOperationInputTest(device, model);
698 removeOperationOutputTest(device, model);
699 addOperationInputTest(device, model);
700 addOperationOutputTest(device, model);
701 mutateExecutionPreferenceTest(device, model);
702}
703
704} // namespace functional
705} // namespace vts
706} // namespace V1_2
707} // namespace neuralnetworks
708} // namespace hardware
709} // namespace android