blob: 8f6d54f7f92de0eea85bd4f965167056cd462096 [file] [log] [blame]
Slava Shklyaevfeb87a92018-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 Shklyaevfeb87a92018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
30
31namespace vts {
32namespace functional {
33
Xusong Wang1a06e772018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
35using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaevfeb87a92018-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 Wang1a06e772018-10-31 08:43:12 -070064 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaevfeb87a92018-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. Sanders9233dbe2018-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 Shklyaevfeb87a92018-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. Sanders9233dbe2018-10-30 15:16:54 +0000139 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaevfeb87a92018-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 Wang56666262018-12-05 14:21:51 -0800154 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100155 case OperandType::FLOAT32:
156 case OperandType::INT32:
157 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100158 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100159 return 1;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100160 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100161 case OperandType::TENSOR_FLOAT32:
162 case OperandType::TENSOR_INT32:
163 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000164 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-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) {
Xusong Wang56666262018-12-05 14:21:51 -0800186 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100187 case OperandType::FLOAT32:
188 case OperandType::INT32:
189 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100190 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100191 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100192 case OperandType::TENSOR_FLOAT32:
193 return 1.0f;
194 case OperandType::TENSOR_INT32:
195 return -1.0f;
196 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000197 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100198 return 0.0f;
199 default:
200 return 0.0f;
201 }
202}
203
204static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
205 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
206 const float invalidScale = getInvalidScale(model.operands[operand].type);
207 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
208 " has scale of " + std::to_string(invalidScale);
209 validate(device, message, model, [operand, invalidScale](Model* model) {
210 model->operands[operand].scale = invalidScale;
211 });
212 }
213}
214
215///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
216
217static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
218 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800219 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100220 case OperandType::FLOAT32:
221 case OperandType::INT32:
222 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100223 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100224 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100225 case OperandType::TENSOR_FLOAT32:
226 case OperandType::TENSOR_INT32:
227 return {1};
228 case OperandType::TENSOR_QUANT8_ASYMM:
229 return {-1, 256};
Lev Proleev217c4072018-11-13 15:42:36 +0000230 case OperandType::TENSOR_QUANT16_SYMM:
231 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100232 default:
233 return {};
234 }
235}
236
237static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
238 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
239 const std::vector<int32_t> invalidZeroPoints =
240 getInvalidZeroPoints(model.operands[operand].type);
241 for (int32_t invalidZeroPoint : invalidZeroPoints) {
242 const std::string message = "mutateOperandZeroPointTest: operand " +
243 std::to_string(operand) + " has zero point of " +
244 std::to_string(invalidZeroPoint);
245 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
246 model->operands[operand].zeroPoint = invalidZeroPoint;
247 });
248 }
249 }
250}
251
252///////////////////////// VALIDATE EXTRA ??? /////////////////////////
253
254// TODO: Operand::lifetime
255// TODO: Operand::location
256
257///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
258
259static void mutateOperand(Operand* operand, OperandType type) {
260 Operand newOperand = *operand;
261 newOperand.type = type;
262 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800263 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100264 case OperandType::FLOAT32:
265 case OperandType::INT32:
266 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100267 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100268 newOperand.dimensions = hidl_vec<uint32_t>();
269 newOperand.scale = 0.0f;
270 newOperand.zeroPoint = 0;
271 break;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100272 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100273 case OperandType::TENSOR_FLOAT32:
274 newOperand.dimensions =
275 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
276 newOperand.scale = 0.0f;
277 newOperand.zeroPoint = 0;
278 break;
279 case OperandType::TENSOR_INT32:
280 newOperand.dimensions =
281 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
282 newOperand.zeroPoint = 0;
283 break;
284 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000285 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100286 newOperand.dimensions =
287 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
288 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
289 break;
290 case OperandType::OEM:
291 case OperandType::TENSOR_OEM_BYTE:
292 default:
293 break;
294 }
295 *operand = newOperand;
296}
297
Xusong Wang1a2492f2018-10-05 11:49:13 -0700298static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
299 // Do not test OEM types
300 if (type == model.operands[operand].type || type == OperandType::OEM ||
301 type == OperandType::TENSOR_OEM_BYTE) {
302 return true;
303 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100304 for (const Operation& operation : model.operations) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700305 // Skip mutateOperationOperandTypeTest for the following operations.
306 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sanders2a010122018-11-28 10:35:08 +0000307 // - ARGMIN and ARGMAX's first argument can be any of
308 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
309 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Xusong Wang1a2492f2018-10-05 11:49:13 -0700310 switch (operation.type) {
311 case OperationType::LSH_PROJECTION: {
312 if (operand == operation.inputs[1]) {
313 return true;
314 }
315 } break;
316 case OperationType::CAST:
317 case OperationType::ARGMAX:
318 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000319 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
320 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700321 return true;
322 }
323 } break;
324 default:
325 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100326 }
327 }
328 return false;
329}
330
331static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
332 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100333 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700334 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100335 continue;
336 }
337 const std::string message = "mutateOperationOperandTypeTest: operand " +
338 std::to_string(operand) + " set to type " +
339 toString(invalidOperandType);
340 validate(device, message, model, [operand, invalidOperandType](Model* model) {
341 mutateOperand(&model->operands[operand], invalidOperandType);
342 });
343 }
344 }
345}
346
347///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
348
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000349static const uint32_t invalidOperationTypes[] = {
350 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MIN) - 1,
351 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MAX) + 1,
352 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MIN) - 1,
353 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100354};
355
356static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
357 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000358 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100359 const std::string message = "mutateOperationTypeTest: operation " +
360 std::to_string(operation) + " set to value " +
361 std::to_string(invalidOperationType);
362 validate(device, message, model, [operation, invalidOperationType](Model* model) {
363 model->operations[operation].type =
364 static_cast<OperationType>(invalidOperationType);
365 });
366 }
367 }
368}
369
370///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
371
372static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
373 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
374 const uint32_t invalidOperand = model.operands.size();
375 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
376 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
377 std::to_string(operation) + " input " +
378 std::to_string(input);
379 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
380 model->operations[operation].inputs[input] = invalidOperand;
381 });
382 }
383 }
384}
385
386///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
387
388static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
389 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
390 const uint32_t invalidOperand = model.operands.size();
391 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
392 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
393 std::to_string(operation) + " output " +
394 std::to_string(output);
395 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
396 model->operations[operation].outputs[output] = invalidOperand;
397 });
398 }
399 }
400}
401
402///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
403
404static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
405 if (vec) {
406 // remove elements matching "value"
407 auto last = std::remove(vec->begin(), vec->end(), value);
408 vec->resize(std::distance(vec->begin(), last));
409
410 // decrement elements exceeding "value"
411 std::transform(vec->begin(), vec->end(), vec->begin(),
412 [value](uint32_t v) { return v > value ? v-- : v; });
413 }
414}
415
416static void removeOperand(Model* model, uint32_t index) {
417 hidl_vec_removeAt(&model->operands, index);
418 for (Operation& operation : model->operations) {
419 removeValueAndDecrementGreaterValues(&operation.inputs, index);
420 removeValueAndDecrementGreaterValues(&operation.outputs, index);
421 }
422 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
423 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
424}
425
Xusong Wang1a2492f2018-10-05 11:49:13 -0700426static bool removeOperandSkip(size_t operand, const Model& model) {
427 for (const Operation& operation : model.operations) {
428 // Skip removeOperandTest for the following operations.
429 // - SPLIT's outputs are not checked during prepareModel.
430 if (operation.type == OperationType::SPLIT) {
431 for (const size_t outOprand : operation.outputs) {
432 if (operand == outOprand) {
433 return true;
434 }
435 }
436 }
437 }
438 return false;
439}
440
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100441static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
442 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700443 if (removeOperandSkip(operand, model)) {
444 continue;
445 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100446 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
447 validate(device, message, model,
448 [operand](Model* model) { removeOperand(model, operand); });
449 }
450}
451
452///////////////////////// REMOVE OPERATION /////////////////////////
453
454static void removeOperation(Model* model, uint32_t index) {
455 for (uint32_t operand : model->operations[index].inputs) {
456 model->operands[operand].numberOfConsumers--;
457 }
458 hidl_vec_removeAt(&model->operations, index);
459}
460
461static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
462 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
463 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
464 validate(device, message, model,
465 [operation](Model* model) { removeOperation(model, operation); });
466 }
467}
468
469///////////////////////// REMOVE OPERATION INPUT /////////////////////////
470
Xusong Wang1a2492f2018-10-05 11:49:13 -0700471static bool removeOperationInputSkip(const Operation& op, size_t input) {
472 // Skip removeOperationInputTest for the following operations.
473 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
474 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
475 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
476 // layout parameter.
477 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
478 // parameter.
479 switch (op.type) {
480 case OperationType::CONCATENATION: {
481 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
482 return true;
483 }
484 } break;
485 case OperationType::DEPTHWISE_CONV_2D: {
486 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
487 return true;
488 }
489 } break;
490 case OperationType::CONV_2D:
491 case OperationType::AVERAGE_POOL_2D:
492 case OperationType::MAX_POOL_2D:
493 case OperationType::L2_POOL_2D: {
494 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
495 return true;
496 }
497 } break;
498 case OperationType::RESIZE_BILINEAR: {
499 if (op.inputs.size() == 4 && input == 3) {
500 return true;
501 }
502 } break;
503 case OperationType::SPACE_TO_DEPTH:
504 case OperationType::DEPTH_TO_SPACE:
505 case OperationType::BATCH_TO_SPACE_ND: {
506 if (op.inputs.size() == 3 && input == 2) {
507 return true;
508 }
509 } break;
510 case OperationType::SPACE_TO_BATCH_ND: {
511 if (op.inputs.size() == 4 && input == 3) {
512 return true;
513 }
514 } break;
515 case OperationType::L2_NORMALIZATION: {
516 if (op.inputs.size() == 2 && input == 1) {
517 return true;
518 }
519 } break;
520 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
521 if (op.inputs.size() == 6 && input == 5) {
522 return true;
523 }
524 } break;
525 case OperationType::SOFTMAX: {
526 if (op.inputs.size() == 3 && input == 2) {
527 return true;
528 }
529 } break;
530 default:
531 break;
532 }
533 return false;
534}
535
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100536static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
537 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
538 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
539 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700540 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100541 continue;
542 }
543 const std::string message = "removeOperationInputTest: operation " +
544 std::to_string(operation) + ", input " +
545 std::to_string(input);
546 validate(device, message, model, [operation, input](Model* model) {
547 uint32_t operand = model->operations[operation].inputs[input];
548 model->operands[operand].numberOfConsumers--;
549 hidl_vec_removeAt(&model->operations[operation].inputs, input);
550 });
551 }
552 }
553}
554
555///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
556
557static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
558 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
559 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
560 const std::string message = "removeOperationOutputTest: operation " +
561 std::to_string(operation) + ", output " +
562 std::to_string(output);
563 validate(device, message, model, [operation, output](Model* model) {
564 hidl_vec_removeAt(&model->operations[operation].outputs, output);
565 });
566 }
567 }
568}
569
570///////////////////////// MODEL VALIDATION /////////////////////////
571
572// TODO: remove model input
573// TODO: remove model output
574// TODO: add unused operation
575
576///////////////////////// ADD OPERATION INPUT /////////////////////////
577
Xusong Wang1a2492f2018-10-05 11:49:13 -0700578static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700579 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700580 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
581 // parameter.
582 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
583 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
584 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700585 return true;
586 }
587 return false;
588}
589
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100590static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
591 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700592 if (addOperationInputSkip(model.operations[operation])) {
593 continue;
594 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100595 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
596 validate(device, message, model, [operation](Model* model) {
597 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
598 hidl_vec_push_back(&model->operations[operation].inputs, index);
599 hidl_vec_push_back(&model->inputIndexes, index);
600 });
601 }
602}
603
604///////////////////////// ADD OPERATION OUTPUT /////////////////////////
605
606static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
607 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
608 const std::string message =
609 "addOperationOutputTest: operation " + std::to_string(operation);
610 validate(device, message, model, [operation](Model* model) {
611 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
612 hidl_vec_push_back(&model->operations[operation].outputs, index);
613 hidl_vec_push_back(&model->outputIndexes, index);
614 });
615 }
616}
617
618///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
619
620static const int32_t invalidExecutionPreferences[] = {
621 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
622 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
623};
624
625static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
626 for (int32_t preference : invalidExecutionPreferences) {
627 const std::string message =
628 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
629 validate(device, message, model, [](Model*) {},
630 static_cast<ExecutionPreference>(preference));
631 }
632}
633
634////////////////////////// ENTRY POINT //////////////////////////////
635
636void ValidationTest::validateModel(const Model& model) {
637 mutateOperandTypeTest(device, model);
638 mutateOperandRankTest(device, model);
639 mutateOperandScaleTest(device, model);
640 mutateOperandZeroPointTest(device, model);
641 mutateOperationOperandTypeTest(device, model);
642 mutateOperationTypeTest(device, model);
643 mutateOperationInputOperandIndexTest(device, model);
644 mutateOperationOutputOperandIndexTest(device, model);
645 removeOperandTest(device, model);
646 removeOperationTest(device, model);
647 removeOperationInputTest(device, model);
648 removeOperationOutputTest(device, model);
649 addOperationInputTest(device, model);
650 addOperationOutputTest(device, model);
651 mutateExecutionPreferenceTest(device, model);
652}
653
654} // namespace functional
655} // namespace vts
656} // namespace V1_2
657} // namespace neuralnetworks
658} // namespace hardware
659} // namespace android