blob: 562ac33c262bd6896502b807d7c71fde08b9d626 [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) {
155 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:
Lev Proleev48c88202018-11-13 15:42:36 +0000164 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100165 return 0;
166 default:
167 return 0;
168 }
169}
170
171static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
172 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
173 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
174 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
175 " has rank of " + std::to_string(invalidRank);
176 validate(device, message, model, [operand, invalidRank](Model* model) {
177 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
178 });
179 }
180}
181
182///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
183
184static float getInvalidScale(OperandType type) {
185 switch (type) {
186 case OperandType::FLOAT32:
187 case OperandType::INT32:
188 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100189 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100190 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100191 case OperandType::TENSOR_FLOAT32:
192 return 1.0f;
193 case OperandType::TENSOR_INT32:
194 return -1.0f;
195 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000196 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100197 return 0.0f;
198 default:
199 return 0.0f;
200 }
201}
202
203static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
204 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
205 const float invalidScale = getInvalidScale(model.operands[operand].type);
206 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
207 " has scale of " + std::to_string(invalidScale);
208 validate(device, message, model, [operand, invalidScale](Model* model) {
209 model->operands[operand].scale = invalidScale;
210 });
211 }
212}
213
214///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
215
216static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
217 switch (type) {
218 case OperandType::FLOAT32:
219 case OperandType::INT32:
220 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100221 case OperandType::BOOL:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100222 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100223 case OperandType::TENSOR_FLOAT32:
224 case OperandType::TENSOR_INT32:
225 return {1};
226 case OperandType::TENSOR_QUANT8_ASYMM:
227 return {-1, 256};
Lev Proleev48c88202018-11-13 15:42:36 +0000228 case OperandType::TENSOR_QUANT16_SYMM:
229 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100230 default:
231 return {};
232 }
233}
234
235static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
236 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
237 const std::vector<int32_t> invalidZeroPoints =
238 getInvalidZeroPoints(model.operands[operand].type);
239 for (int32_t invalidZeroPoint : invalidZeroPoints) {
240 const std::string message = "mutateOperandZeroPointTest: operand " +
241 std::to_string(operand) + " has zero point of " +
242 std::to_string(invalidZeroPoint);
243 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
244 model->operands[operand].zeroPoint = invalidZeroPoint;
245 });
246 }
247 }
248}
249
250///////////////////////// VALIDATE EXTRA ??? /////////////////////////
251
252// TODO: Operand::lifetime
253// TODO: Operand::location
254
255///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
256
257static void mutateOperand(Operand* operand, OperandType type) {
258 Operand newOperand = *operand;
259 newOperand.type = type;
260 switch (type) {
261 case OperandType::FLOAT32:
262 case OperandType::INT32:
263 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100264 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100265 newOperand.dimensions = hidl_vec<uint32_t>();
266 newOperand.scale = 0.0f;
267 newOperand.zeroPoint = 0;
268 break;
Michael K. Sanders19d63452018-10-12 09:10:15 +0100269 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100270 case OperandType::TENSOR_FLOAT32:
271 newOperand.dimensions =
272 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
273 newOperand.scale = 0.0f;
274 newOperand.zeroPoint = 0;
275 break;
276 case OperandType::TENSOR_INT32:
277 newOperand.dimensions =
278 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
279 newOperand.zeroPoint = 0;
280 break;
281 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000282 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100283 newOperand.dimensions =
284 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
285 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
286 break;
287 case OperandType::OEM:
288 case OperandType::TENSOR_OEM_BYTE:
289 default:
290 break;
291 }
292 *operand = newOperand;
293}
294
Xusong Wang5b747ae2018-10-05 11:49:13 -0700295static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
296 // Do not test OEM types
297 if (type == model.operands[operand].type || type == OperandType::OEM ||
298 type == OperandType::TENSOR_OEM_BYTE) {
299 return true;
300 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100301 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700302 // Skip mutateOperationOperandTypeTest for the following operations.
303 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000304 // - ARGMIN and ARGMAX's first argument can be any of
305 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
306 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000307 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700308 switch (operation.type) {
309 case OperationType::LSH_PROJECTION: {
310 if (operand == operation.inputs[1]) {
311 return true;
312 }
313 } break;
314 case OperationType::CAST:
315 case OperationType::ARGMAX:
316 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000317 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
318 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700319 return true;
320 }
321 } break;
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000322 case OperationType::RANDOM_MULTINOMIAL: {
323 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
324 return true;
325 }
326 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700327 default:
328 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100329 }
330 }
331 return false;
332}
333
334static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
335 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100336 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700337 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100338 continue;
339 }
340 const std::string message = "mutateOperationOperandTypeTest: operand " +
341 std::to_string(operand) + " set to type " +
342 toString(invalidOperandType);
343 validate(device, message, model, [operand, invalidOperandType](Model* model) {
344 mutateOperand(&model->operands[operand], invalidOperandType);
345 });
346 }
347 }
348}
349
350///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
351
Michael K. Sandersc785d462018-10-30 15:16:54 +0000352static const uint32_t invalidOperationTypes[] = {
353 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MIN) - 1,
354 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MAX) + 1,
355 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MIN) - 1,
356 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100357};
358
359static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
360 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000361 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100362 const std::string message = "mutateOperationTypeTest: operation " +
363 std::to_string(operation) + " set to value " +
364 std::to_string(invalidOperationType);
365 validate(device, message, model, [operation, invalidOperationType](Model* model) {
366 model->operations[operation].type =
367 static_cast<OperationType>(invalidOperationType);
368 });
369 }
370 }
371}
372
373///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
374
375static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
376 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
377 const uint32_t invalidOperand = model.operands.size();
378 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
379 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
380 std::to_string(operation) + " input " +
381 std::to_string(input);
382 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
383 model->operations[operation].inputs[input] = invalidOperand;
384 });
385 }
386 }
387}
388
389///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
390
391static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
392 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
393 const uint32_t invalidOperand = model.operands.size();
394 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
395 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
396 std::to_string(operation) + " output " +
397 std::to_string(output);
398 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
399 model->operations[operation].outputs[output] = invalidOperand;
400 });
401 }
402 }
403}
404
405///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
406
407static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
408 if (vec) {
409 // remove elements matching "value"
410 auto last = std::remove(vec->begin(), vec->end(), value);
411 vec->resize(std::distance(vec->begin(), last));
412
413 // decrement elements exceeding "value"
414 std::transform(vec->begin(), vec->end(), vec->begin(),
415 [value](uint32_t v) { return v > value ? v-- : v; });
416 }
417}
418
419static void removeOperand(Model* model, uint32_t index) {
420 hidl_vec_removeAt(&model->operands, index);
421 for (Operation& operation : model->operations) {
422 removeValueAndDecrementGreaterValues(&operation.inputs, index);
423 removeValueAndDecrementGreaterValues(&operation.outputs, index);
424 }
425 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
426 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
427}
428
Xusong Wang5b747ae2018-10-05 11:49:13 -0700429static bool removeOperandSkip(size_t operand, const Model& model) {
430 for (const Operation& operation : model.operations) {
431 // Skip removeOperandTest for the following operations.
432 // - SPLIT's outputs are not checked during prepareModel.
433 if (operation.type == OperationType::SPLIT) {
434 for (const size_t outOprand : operation.outputs) {
435 if (operand == outOprand) {
436 return true;
437 }
438 }
439 }
440 }
441 return false;
442}
443
Slava Shklyaev871be942018-09-12 14:52:02 +0100444static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
445 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700446 if (removeOperandSkip(operand, model)) {
447 continue;
448 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100449 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
450 validate(device, message, model,
451 [operand](Model* model) { removeOperand(model, operand); });
452 }
453}
454
455///////////////////////// REMOVE OPERATION /////////////////////////
456
457static void removeOperation(Model* model, uint32_t index) {
458 for (uint32_t operand : model->operations[index].inputs) {
459 model->operands[operand].numberOfConsumers--;
460 }
461 hidl_vec_removeAt(&model->operations, index);
462}
463
464static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
465 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
466 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
467 validate(device, message, model,
468 [operation](Model* model) { removeOperation(model, operation); });
469 }
470}
471
472///////////////////////// REMOVE OPERATION INPUT /////////////////////////
473
Xusong Wang5b747ae2018-10-05 11:49:13 -0700474static bool removeOperationInputSkip(const Operation& op, size_t input) {
475 // Skip removeOperationInputTest for the following operations.
476 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
477 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
478 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
479 // layout parameter.
480 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
481 // parameter.
482 switch (op.type) {
483 case OperationType::CONCATENATION: {
484 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
485 return true;
486 }
487 } break;
488 case OperationType::DEPTHWISE_CONV_2D: {
489 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
490 return true;
491 }
492 } break;
493 case OperationType::CONV_2D:
494 case OperationType::AVERAGE_POOL_2D:
495 case OperationType::MAX_POOL_2D:
496 case OperationType::L2_POOL_2D: {
497 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
498 return true;
499 }
500 } break;
501 case OperationType::RESIZE_BILINEAR: {
502 if (op.inputs.size() == 4 && input == 3) {
503 return true;
504 }
505 } break;
506 case OperationType::SPACE_TO_DEPTH:
507 case OperationType::DEPTH_TO_SPACE:
508 case OperationType::BATCH_TO_SPACE_ND: {
509 if (op.inputs.size() == 3 && input == 2) {
510 return true;
511 }
512 } break;
513 case OperationType::SPACE_TO_BATCH_ND: {
514 if (op.inputs.size() == 4 && input == 3) {
515 return true;
516 }
517 } break;
518 case OperationType::L2_NORMALIZATION: {
519 if (op.inputs.size() == 2 && input == 1) {
520 return true;
521 }
522 } break;
523 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
524 if (op.inputs.size() == 6 && input == 5) {
525 return true;
526 }
527 } break;
528 case OperationType::SOFTMAX: {
529 if (op.inputs.size() == 3 && input == 2) {
530 return true;
531 }
532 } break;
533 default:
534 break;
535 }
536 return false;
537}
538
Slava Shklyaev871be942018-09-12 14:52:02 +0100539static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
540 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
541 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
542 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700543 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100544 continue;
545 }
546 const std::string message = "removeOperationInputTest: operation " +
547 std::to_string(operation) + ", input " +
548 std::to_string(input);
549 validate(device, message, model, [operation, input](Model* model) {
550 uint32_t operand = model->operations[operation].inputs[input];
551 model->operands[operand].numberOfConsumers--;
552 hidl_vec_removeAt(&model->operations[operation].inputs, input);
553 });
554 }
555 }
556}
557
558///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
559
560static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
561 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
562 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
563 const std::string message = "removeOperationOutputTest: operation " +
564 std::to_string(operation) + ", output " +
565 std::to_string(output);
566 validate(device, message, model, [operation, output](Model* model) {
567 hidl_vec_removeAt(&model->operations[operation].outputs, output);
568 });
569 }
570 }
571}
572
573///////////////////////// MODEL VALIDATION /////////////////////////
574
575// TODO: remove model input
576// TODO: remove model output
577// TODO: add unused operation
578
579///////////////////////// ADD OPERATION INPUT /////////////////////////
580
Xusong Wang5b747ae2018-10-05 11:49:13 -0700581static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700582 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700583 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
584 // parameter.
585 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
586 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
587 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700588 return true;
589 }
590 return false;
591}
592
Slava Shklyaev871be942018-09-12 14:52:02 +0100593static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
594 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700595 if (addOperationInputSkip(model.operations[operation])) {
596 continue;
597 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100598 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
599 validate(device, message, model, [operation](Model* model) {
600 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
601 hidl_vec_push_back(&model->operations[operation].inputs, index);
602 hidl_vec_push_back(&model->inputIndexes, index);
603 });
604 }
605}
606
607///////////////////////// ADD OPERATION OUTPUT /////////////////////////
608
609static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
610 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
611 const std::string message =
612 "addOperationOutputTest: operation " + std::to_string(operation);
613 validate(device, message, model, [operation](Model* model) {
614 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
615 hidl_vec_push_back(&model->operations[operation].outputs, index);
616 hidl_vec_push_back(&model->outputIndexes, index);
617 });
618 }
619}
620
621///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
622
623static const int32_t invalidExecutionPreferences[] = {
624 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
625 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
626};
627
628static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
629 for (int32_t preference : invalidExecutionPreferences) {
630 const std::string message =
631 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
632 validate(device, message, model, [](Model*) {},
633 static_cast<ExecutionPreference>(preference));
634 }
635}
636
637////////////////////////// ENTRY POINT //////////////////////////////
638
639void ValidationTest::validateModel(const Model& model) {
640 mutateOperandTypeTest(device, model);
641 mutateOperandRankTest(device, model);
642 mutateOperandScaleTest(device, model);
643 mutateOperandZeroPointTest(device, model);
644 mutateOperationOperandTypeTest(device, model);
645 mutateOperationTypeTest(device, model);
646 mutateOperationInputOperandIndexTest(device, model);
647 mutateOperationOutputOperandIndexTest(device, model);
648 removeOperandTest(device, model);
649 removeOperationTest(device, model);
650 removeOperationInputTest(device, model);
651 removeOperationOutputTest(device, model);
652 addOperationInputTest(device, model);
653 addOperationOutputTest(device, model);
654 mutateExecutionPreferenceTest(device, model);
655}
656
657} // namespace functional
658} // namespace vts
659} // namespace V1_2
660} // namespace neuralnetworks
661} // namespace hardware
662} // namespace android