blob: ac23f41fe3b185c37e2117a0fa8f038089ec07bb [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
28using V1_0::IPreparedModel;
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010030using V1_1::ExecutionPreference;
31
32namespace vts {
33namespace functional {
34
35using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
36using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
37
38///////////////////////// UTILITY FUNCTIONS /////////////////////////
39
40static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
41 const Model& model) {
42 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
43
44 Return<void> ret =
45 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
46 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
47 });
48 EXPECT_TRUE(ret.isOk());
49}
50
51static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
52 const Model& model, ExecutionPreference preference) {
53 SCOPED_TRACE(message + " [prepareModel_1_2]");
54
55 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
56 ASSERT_NE(nullptr, preparedModelCallback.get());
57 Return<ErrorStatus> prepareLaunchStatus =
58 device->prepareModel_1_2(model, preference, preparedModelCallback);
59 ASSERT_TRUE(prepareLaunchStatus.isOk());
60 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
61
62 preparedModelCallback->wait();
63 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
64 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
65 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
66 ASSERT_EQ(nullptr, preparedModel.get());
67}
68
69static bool validExecutionPreference(ExecutionPreference preference) {
70 return preference == ExecutionPreference::LOW_POWER ||
71 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
72 preference == ExecutionPreference::SUSTAINED_SPEED;
73}
74
75// Primary validation function. This function will take a valid model, apply a
76// mutation to it to invalidate the model, then pass it to interface calls that
77// use the model. Note that the model here is passed by value, and any mutation
78// to the model does not leave this function.
79static void validate(const sp<IDevice>& device, const std::string& message, Model model,
80 const std::function<void(Model*)>& mutation,
81 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
82 mutation(&model);
83 if (validExecutionPreference(preference)) {
84 validateGetSupportedOperations(device, message, model);
85 }
86 validatePrepareModel(device, message, model, preference);
87}
88
89// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
90// so this is efficiently accomplished by moving the element to the end and
91// resizing the hidl_vec to one less.
92template <typename Type>
93static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
94 if (vec) {
95 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
96 vec->resize(vec->size() - 1);
97 }
98}
99
100template <typename Type>
101static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
102 // assume vec is valid
103 const uint32_t index = vec->size();
104 vec->resize(index + 1);
105 (*vec)[index] = value;
106 return index;
107}
108
109static uint32_t addOperand(Model* model) {
110 return hidl_vec_push_back(&model->operands,
111 {
112 .type = OperandType::INT32,
113 .dimensions = {},
114 .numberOfConsumers = 0,
115 .scale = 0.0f,
116 .zeroPoint = 0,
117 .lifetime = OperandLifeTime::MODEL_INPUT,
118 .location = {.poolIndex = 0, .offset = 0, .length = 0},
119 });
120}
121
122static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
123 uint32_t index = addOperand(model);
124 model->operands[index].numberOfConsumers = 1;
125 model->operands[index].lifetime = lifetime;
126 return index;
127}
128
129///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
130
Michael K. Sandersc785d462018-10-30 15:16:54 +0000131static const uint32_t invalidOperandTypes[] = {
132 static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MIN) - 1,
133 static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MAX) + 1,
134 static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MIN) - 1,
135 static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100136};
137
138static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
139 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000140 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100141 const std::string message = "mutateOperandTypeTest: operand " +
142 std::to_string(operand) + " set to value " +
143 std::to_string(invalidOperandType);
144 validate(device, message, model, [operand, invalidOperandType](Model* model) {
145 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
146 });
147 }
148 }
149}
150
151///////////////////////// VALIDATE OPERAND RANK /////////////////////////
152
153static uint32_t getInvalidRank(OperandType type) {
154 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800155 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100156 case OperandType::FLOAT32:
157 case OperandType::INT32:
158 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100159 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100160 return 1;
Michael K. Sanders19d63452018-10-12 09:10:15 +0100161 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100162 case OperandType::TENSOR_FLOAT32:
163 case OperandType::TENSOR_INT32:
164 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000165 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100166 return 0;
167 default:
168 return 0;
169 }
170}
171
172static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
173 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
174 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
175 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
176 " has rank of " + std::to_string(invalidRank);
177 validate(device, message, model, [operand, invalidRank](Model* model) {
178 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
179 });
180 }
181}
182
183///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
184
185static float getInvalidScale(OperandType type) {
186 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800187 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100188 case OperandType::FLOAT32:
189 case OperandType::INT32:
190 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100191 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100192 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100193 case OperandType::TENSOR_FLOAT32:
194 return 1.0f;
195 case OperandType::TENSOR_INT32:
196 return -1.0f;
197 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000198 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100199 return 0.0f;
200 default:
201 return 0.0f;
202 }
203}
204
205static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
206 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
207 const float invalidScale = getInvalidScale(model.operands[operand].type);
208 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
209 " has scale of " + std::to_string(invalidScale);
210 validate(device, message, model, [operand, invalidScale](Model* model) {
211 model->operands[operand].scale = invalidScale;
212 });
213 }
214}
215
216///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
217
218static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
219 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800220 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100221 case OperandType::FLOAT32:
222 case OperandType::INT32:
223 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100224 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100225 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100226 case OperandType::TENSOR_FLOAT32:
227 case OperandType::TENSOR_INT32:
228 return {1};
229 case OperandType::TENSOR_QUANT8_ASYMM:
230 return {-1, 256};
Lev Proleev48c88202018-11-13 15:42:36 +0000231 case OperandType::TENSOR_QUANT16_SYMM:
232 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100233 default:
234 return {};
235 }
236}
237
238static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
239 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
240 const std::vector<int32_t> invalidZeroPoints =
241 getInvalidZeroPoints(model.operands[operand].type);
242 for (int32_t invalidZeroPoint : invalidZeroPoints) {
243 const std::string message = "mutateOperandZeroPointTest: operand " +
244 std::to_string(operand) + " has zero point of " +
245 std::to_string(invalidZeroPoint);
246 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
247 model->operands[operand].zeroPoint = invalidZeroPoint;
248 });
249 }
250 }
251}
252
253///////////////////////// VALIDATE EXTRA ??? /////////////////////////
254
255// TODO: Operand::lifetime
256// TODO: Operand::location
257
258///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
259
260static void mutateOperand(Operand* operand, OperandType type) {
261 Operand newOperand = *operand;
262 newOperand.type = type;
263 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800264 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100265 case OperandType::FLOAT32:
266 case OperandType::INT32:
267 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100268 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100269 newOperand.dimensions = hidl_vec<uint32_t>();
270 newOperand.scale = 0.0f;
271 newOperand.zeroPoint = 0;
272 break;
Michael K. Sanders19d63452018-10-12 09:10:15 +0100273 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100274 case OperandType::TENSOR_FLOAT32:
275 newOperand.dimensions =
276 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
277 newOperand.scale = 0.0f;
278 newOperand.zeroPoint = 0;
279 break;
280 case OperandType::TENSOR_INT32:
281 newOperand.dimensions =
282 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
283 newOperand.zeroPoint = 0;
284 break;
285 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000286 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100287 newOperand.dimensions =
288 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
289 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
290 break;
291 case OperandType::OEM:
292 case OperandType::TENSOR_OEM_BYTE:
293 default:
294 break;
295 }
296 *operand = newOperand;
297}
298
Xusong Wang5b747ae2018-10-05 11:49:13 -0700299static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
300 // Do not test OEM types
301 if (type == model.operands[operand].type || type == OperandType::OEM ||
302 type == OperandType::TENSOR_OEM_BYTE) {
303 return true;
304 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100305 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700306 // Skip mutateOperationOperandTypeTest for the following operations.
307 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000308 // - ARGMIN and ARGMAX's first argument can be any of
309 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
310 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000311 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700312 switch (operation.type) {
313 case OperationType::LSH_PROJECTION: {
314 if (operand == operation.inputs[1]) {
315 return true;
316 }
317 } break;
318 case OperationType::CAST:
319 case OperationType::ARGMAX:
320 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000321 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
322 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700323 return true;
324 }
325 } break;
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000326 case OperationType::RANDOM_MULTINOMIAL: {
327 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
328 return true;
329 }
330 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700331 default:
332 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100333 }
334 }
335 return false;
336}
337
338static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
339 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100340 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700341 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100342 continue;
343 }
344 const std::string message = "mutateOperationOperandTypeTest: operand " +
345 std::to_string(operand) + " set to type " +
346 toString(invalidOperandType);
347 validate(device, message, model, [operand, invalidOperandType](Model* model) {
348 mutateOperand(&model->operands[operand], invalidOperandType);
349 });
350 }
351 }
352}
353
354///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
355
Michael K. Sandersc785d462018-10-30 15:16:54 +0000356static const uint32_t invalidOperationTypes[] = {
357 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MIN) - 1,
358 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MAX) + 1,
359 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MIN) - 1,
360 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100361};
362
363static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
364 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000365 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100366 const std::string message = "mutateOperationTypeTest: operation " +
367 std::to_string(operation) + " set to value " +
368 std::to_string(invalidOperationType);
369 validate(device, message, model, [operation, invalidOperationType](Model* model) {
370 model->operations[operation].type =
371 static_cast<OperationType>(invalidOperationType);
372 });
373 }
374 }
375}
376
377///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
378
379static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
380 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
381 const uint32_t invalidOperand = model.operands.size();
382 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
383 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
384 std::to_string(operation) + " input " +
385 std::to_string(input);
386 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
387 model->operations[operation].inputs[input] = invalidOperand;
388 });
389 }
390 }
391}
392
393///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
394
395static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
396 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
397 const uint32_t invalidOperand = model.operands.size();
398 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
399 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
400 std::to_string(operation) + " output " +
401 std::to_string(output);
402 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
403 model->operations[operation].outputs[output] = invalidOperand;
404 });
405 }
406 }
407}
408
409///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
410
411static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
412 if (vec) {
413 // remove elements matching "value"
414 auto last = std::remove(vec->begin(), vec->end(), value);
415 vec->resize(std::distance(vec->begin(), last));
416
417 // decrement elements exceeding "value"
418 std::transform(vec->begin(), vec->end(), vec->begin(),
419 [value](uint32_t v) { return v > value ? v-- : v; });
420 }
421}
422
423static void removeOperand(Model* model, uint32_t index) {
424 hidl_vec_removeAt(&model->operands, index);
425 for (Operation& operation : model->operations) {
426 removeValueAndDecrementGreaterValues(&operation.inputs, index);
427 removeValueAndDecrementGreaterValues(&operation.outputs, index);
428 }
429 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
430 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
431}
432
Xusong Wang5b747ae2018-10-05 11:49:13 -0700433static bool removeOperandSkip(size_t operand, const Model& model) {
434 for (const Operation& operation : model.operations) {
435 // Skip removeOperandTest for the following operations.
436 // - SPLIT's outputs are not checked during prepareModel.
437 if (operation.type == OperationType::SPLIT) {
438 for (const size_t outOprand : operation.outputs) {
439 if (operand == outOprand) {
440 return true;
441 }
442 }
443 }
444 }
445 return false;
446}
447
Slava Shklyaev871be942018-09-12 14:52:02 +0100448static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
449 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700450 if (removeOperandSkip(operand, model)) {
451 continue;
452 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100453 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
454 validate(device, message, model,
455 [operand](Model* model) { removeOperand(model, operand); });
456 }
457}
458
459///////////////////////// REMOVE OPERATION /////////////////////////
460
461static void removeOperation(Model* model, uint32_t index) {
462 for (uint32_t operand : model->operations[index].inputs) {
463 model->operands[operand].numberOfConsumers--;
464 }
465 hidl_vec_removeAt(&model->operations, index);
466}
467
468static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
469 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
470 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
471 validate(device, message, model,
472 [operation](Model* model) { removeOperation(model, operation); });
473 }
474}
475
476///////////////////////// REMOVE OPERATION INPUT /////////////////////////
477
Xusong Wang5b747ae2018-10-05 11:49:13 -0700478static bool removeOperationInputSkip(const Operation& op, size_t input) {
479 // Skip removeOperationInputTest for the following operations.
480 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
481 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
482 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
483 // layout parameter.
484 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
485 // parameter.
486 switch (op.type) {
487 case OperationType::CONCATENATION: {
488 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
489 return true;
490 }
491 } break;
492 case OperationType::DEPTHWISE_CONV_2D: {
493 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
494 return true;
495 }
496 } break;
497 case OperationType::CONV_2D:
498 case OperationType::AVERAGE_POOL_2D:
499 case OperationType::MAX_POOL_2D:
500 case OperationType::L2_POOL_2D: {
501 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
502 return true;
503 }
504 } break;
505 case OperationType::RESIZE_BILINEAR: {
506 if (op.inputs.size() == 4 && input == 3) {
507 return true;
508 }
509 } break;
510 case OperationType::SPACE_TO_DEPTH:
511 case OperationType::DEPTH_TO_SPACE:
512 case OperationType::BATCH_TO_SPACE_ND: {
513 if (op.inputs.size() == 3 && input == 2) {
514 return true;
515 }
516 } break;
517 case OperationType::SPACE_TO_BATCH_ND: {
518 if (op.inputs.size() == 4 && input == 3) {
519 return true;
520 }
521 } break;
522 case OperationType::L2_NORMALIZATION: {
523 if (op.inputs.size() == 2 && input == 1) {
524 return true;
525 }
526 } break;
527 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
528 if (op.inputs.size() == 6 && input == 5) {
529 return true;
530 }
531 } break;
532 case OperationType::SOFTMAX: {
533 if (op.inputs.size() == 3 && input == 2) {
534 return true;
535 }
536 } break;
537 default:
538 break;
539 }
540 return false;
541}
542
Slava Shklyaev871be942018-09-12 14:52:02 +0100543static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
544 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
545 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
546 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700547 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100548 continue;
549 }
550 const std::string message = "removeOperationInputTest: operation " +
551 std::to_string(operation) + ", input " +
552 std::to_string(input);
553 validate(device, message, model, [operation, input](Model* model) {
554 uint32_t operand = model->operations[operation].inputs[input];
555 model->operands[operand].numberOfConsumers--;
556 hidl_vec_removeAt(&model->operations[operation].inputs, input);
557 });
558 }
559 }
560}
561
562///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
563
564static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
565 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
566 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
567 const std::string message = "removeOperationOutputTest: operation " +
568 std::to_string(operation) + ", output " +
569 std::to_string(output);
570 validate(device, message, model, [operation, output](Model* model) {
571 hidl_vec_removeAt(&model->operations[operation].outputs, output);
572 });
573 }
574 }
575}
576
577///////////////////////// MODEL VALIDATION /////////////////////////
578
579// TODO: remove model input
580// TODO: remove model output
581// TODO: add unused operation
582
583///////////////////////// ADD OPERATION INPUT /////////////////////////
584
Xusong Wang5b747ae2018-10-05 11:49:13 -0700585static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700586 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700587 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
588 // parameter.
589 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
590 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
591 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700592 return true;
593 }
594 return false;
595}
596
Slava Shklyaev871be942018-09-12 14:52:02 +0100597static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
598 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700599 if (addOperationInputSkip(model.operations[operation])) {
600 continue;
601 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100602 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
603 validate(device, message, model, [operation](Model* model) {
604 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
605 hidl_vec_push_back(&model->operations[operation].inputs, index);
606 hidl_vec_push_back(&model->inputIndexes, index);
607 });
608 }
609}
610
611///////////////////////// ADD OPERATION OUTPUT /////////////////////////
612
613static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
614 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
615 const std::string message =
616 "addOperationOutputTest: operation " + std::to_string(operation);
617 validate(device, message, model, [operation](Model* model) {
618 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
619 hidl_vec_push_back(&model->operations[operation].outputs, index);
620 hidl_vec_push_back(&model->outputIndexes, index);
621 });
622 }
623}
624
625///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
626
627static const int32_t invalidExecutionPreferences[] = {
628 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
629 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
630};
631
632static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
633 for (int32_t preference : invalidExecutionPreferences) {
634 const std::string message =
635 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
636 validate(device, message, model, [](Model*) {},
637 static_cast<ExecutionPreference>(preference));
638 }
639}
640
641////////////////////////// ENTRY POINT //////////////////////////////
642
643void ValidationTest::validateModel(const Model& model) {
644 mutateOperandTypeTest(device, model);
645 mutateOperandRankTest(device, model);
646 mutateOperandScaleTest(device, model);
647 mutateOperandZeroPointTest(device, model);
648 mutateOperationOperandTypeTest(device, model);
649 mutateOperationTypeTest(device, model);
650 mutateOperationInputOperandIndexTest(device, model);
651 mutateOperationOutputOperandIndexTest(device, model);
652 removeOperandTest(device, model);
653 removeOperationTest(device, model);
654 removeOperationInputTest(device, model);
655 removeOperationOutputTest(device, model);
656 addOperationInputTest(device, model);
657 addOperationOutputTest(device, model);
658 mutateExecutionPreferenceTest(device, model);
659}
660
661} // namespace functional
662} // namespace vts
663} // namespace V1_2
664} // namespace neuralnetworks
665} // namespace hardware
666} // namespace android