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