blob: 6fd54071f9e35c19519414f76a7640024667c513 [file] [log] [blame]
Lev Proleev13fdfcd2019-08-30 11:35:34 +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 "1.0/Utils.h"
Xusong Wangcc47dff2019-10-23 10:35:07 -070020#include "1.3/Callbacks.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010021#include "GeneratedTestHarness.h"
22#include "VtsHalNeuralnetworks.h"
23
Lev Proleev26d1bc82019-08-30 11:57:18 +010024namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010025
Xusong Wangcc47dff2019-10-23 10:35:07 -070026using implementation::PreparedModelCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010027using V1_0::ErrorStatus;
28using V1_0::OperandLifeTime;
29using V1_1::ExecutionPreference;
Lev Proleev26d1bc82019-08-30 11:57:18 +010030using V1_2::OperationTypeRange;
31using V1_2::SymmPerChannelQuantParams;
Lev Proleev26d1bc82019-08-30 11:57:18 +010032using HidlToken =
33 hidl_array<uint8_t, static_cast<uint32_t>(V1_2::Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010034
35///////////////////////// UTILITY FUNCTIONS /////////////////////////
36
37static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
38 const Model& model) {
Lev Proleev26d1bc82019-08-30 11:57:18 +010039 SCOPED_TRACE(message + " [getSupportedOperations_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010040
Lev Proleev26d1bc82019-08-30 11:57:18 +010041 Return<void> ret = device->getSupportedOperations_1_3(
Lev Proleev13fdfcd2019-08-30 11:35:34 +010042 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
43 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
44 });
45 EXPECT_TRUE(ret.isOk());
46}
47
48static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
49 const Model& model, ExecutionPreference preference) {
Lev Proleev26d1bc82019-08-30 11:57:18 +010050 SCOPED_TRACE(message + " [prepareModel_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010051
52 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
53 Return<ErrorStatus> prepareLaunchStatus =
Lev Proleev26d1bc82019-08-30 11:57:18 +010054 device->prepareModel_1_3(model, preference, hidl_vec<hidl_handle>(),
Lev Proleev13fdfcd2019-08-30 11:35:34 +010055 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
56 ASSERT_TRUE(prepareLaunchStatus.isOk());
57 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
58
59 preparedModelCallback->wait();
60 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
61 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wang1b3f4262019-10-25 12:07:17 -070062 sp<IPreparedModel> preparedModel = getPreparedModel_1_3(preparedModelCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010063 ASSERT_EQ(nullptr, preparedModel.get());
64}
65
66static bool validExecutionPreference(ExecutionPreference preference) {
67 return preference == ExecutionPreference::LOW_POWER ||
68 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
69 preference == ExecutionPreference::SUSTAINED_SPEED;
70}
71
72// Primary validation function. This function will take a valid model, apply a
73// mutation to it to invalidate the model, then pass it to interface calls that
74// use the model. Note that the model here is passed by value, and any mutation
75// to the model does not leave this function.
76static void validate(const sp<IDevice>& device, const std::string& message, Model model,
77 const std::function<void(Model*)>& mutation,
78 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
79 mutation(&model);
80 if (validExecutionPreference(preference)) {
81 validateGetSupportedOperations(device, message, model);
82 }
83 validatePrepareModel(device, message, model, preference);
84}
85
86static uint32_t addOperand(Model* model) {
87 return hidl_vec_push_back(&model->operands,
88 {
89 .type = OperandType::INT32,
90 .dimensions = {},
91 .numberOfConsumers = 0,
92 .scale = 0.0f,
93 .zeroPoint = 0,
94 .lifetime = OperandLifeTime::MODEL_INPUT,
95 .location = {.poolIndex = 0, .offset = 0, .length = 0},
96 });
97}
98
99static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
100 uint32_t index = addOperand(model);
101 model->operands[index].numberOfConsumers = 1;
102 model->operands[index].lifetime = lifetime;
103 return index;
104}
105
106///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
107
108static const uint32_t invalidOperandTypes[] = {
109 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
110 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
111 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
112 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
113};
114
115static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
116 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
117 for (uint32_t invalidOperandType : invalidOperandTypes) {
118 const std::string message = "mutateOperandTypeTest: operand " +
119 std::to_string(operand) + " set to value " +
120 std::to_string(invalidOperandType);
121 validate(device, message, model, [operand, invalidOperandType](Model* model) {
122 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
123 });
124 }
125 }
126}
127
128///////////////////////// VALIDATE OPERAND RANK /////////////////////////
129
130static uint32_t getInvalidRank(OperandType type) {
131 switch (type) {
132 case OperandType::FLOAT16:
133 case OperandType::FLOAT32:
134 case OperandType::INT32:
135 case OperandType::UINT32:
136 case OperandType::BOOL:
137 return 1;
138 case OperandType::TENSOR_BOOL8:
139 case OperandType::TENSOR_FLOAT16:
140 case OperandType::TENSOR_FLOAT32:
141 case OperandType::TENSOR_INT32:
142 case OperandType::TENSOR_QUANT8_ASYMM:
143 case OperandType::TENSOR_QUANT8_SYMM:
144 case OperandType::TENSOR_QUANT16_ASYMM:
145 case OperandType::TENSOR_QUANT16_SYMM:
146 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
147 return 0;
148 default:
149 return 0;
150 }
151}
152
153static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
154 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
155 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
156 if (invalidRank == 0) {
157 continue;
158 }
159 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
160 " has rank of " + std::to_string(invalidRank);
161 validate(device, message, model, [operand, invalidRank](Model* model) {
162 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
163 });
164 }
165}
166
167///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
168
169static float getInvalidScale(OperandType type) {
170 switch (type) {
171 case OperandType::FLOAT16:
172 case OperandType::FLOAT32:
173 case OperandType::INT32:
174 case OperandType::UINT32:
175 case OperandType::BOOL:
176 case OperandType::TENSOR_BOOL8:
177 case OperandType::TENSOR_FLOAT16:
178 case OperandType::TENSOR_FLOAT32:
179 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
180 return 1.0f;
181 case OperandType::TENSOR_INT32:
182 return -1.0f;
183 case OperandType::TENSOR_QUANT8_SYMM:
184 case OperandType::TENSOR_QUANT8_ASYMM:
185 case OperandType::TENSOR_QUANT16_ASYMM:
186 case OperandType::TENSOR_QUANT16_SYMM:
187 return 0.0f;
188 default:
189 return 0.0f;
190 }
191}
192
193static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
194 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
195 const float invalidScale = getInvalidScale(model.operands[operand].type);
196 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
197 " has scale of " + std::to_string(invalidScale);
198 validate(device, message, model, [operand, invalidScale](Model* model) {
199 model->operands[operand].scale = invalidScale;
200 });
201 }
202}
203
204///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
205
206static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
207 switch (type) {
208 case OperandType::FLOAT16:
209 case OperandType::FLOAT32:
210 case OperandType::INT32:
211 case OperandType::UINT32:
212 case OperandType::BOOL:
213 case OperandType::TENSOR_BOOL8:
214 case OperandType::TENSOR_FLOAT16:
215 case OperandType::TENSOR_FLOAT32:
216 case OperandType::TENSOR_INT32:
217 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
218 return {1};
219 case OperandType::TENSOR_QUANT8_ASYMM:
220 return {-1, 256};
221 case OperandType::TENSOR_QUANT8_SYMM:
222 return {-129, -1, 1, 128};
223 case OperandType::TENSOR_QUANT16_ASYMM:
224 return {-1, 65536};
225 case OperandType::TENSOR_QUANT16_SYMM:
226 return {-32769, -1, 1, 32768};
227 default:
228 return {};
229 }
230}
231
232static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
233 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
234 const std::vector<int32_t> invalidZeroPoints =
235 getInvalidZeroPoints(model.operands[operand].type);
236 for (int32_t invalidZeroPoint : invalidZeroPoints) {
237 const std::string message = "mutateOperandZeroPointTest: operand " +
238 std::to_string(operand) + " has zero point of " +
239 std::to_string(invalidZeroPoint);
240 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
241 model->operands[operand].zeroPoint = invalidZeroPoint;
242 });
243 }
244 }
245}
246
247///////////////////////// VALIDATE EXTRA ??? /////////////////////////
248
249// TODO: Operand::lifetime
250// TODO: Operand::location
251
252///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
253
254static void mutateOperand(Operand* operand, OperandType type) {
255 Operand newOperand = *operand;
256 newOperand.type = type;
257 switch (type) {
258 case OperandType::FLOAT16:
259 case OperandType::FLOAT32:
260 case OperandType::INT32:
261 case OperandType::UINT32:
262 case OperandType::BOOL:
263 newOperand.dimensions = hidl_vec<uint32_t>();
264 newOperand.scale = 0.0f;
265 newOperand.zeroPoint = 0;
266 break;
267 case OperandType::TENSOR_BOOL8:
268 case OperandType::TENSOR_FLOAT16:
269 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:
281 case OperandType::TENSOR_QUANT8_SYMM:
282 case OperandType::TENSOR_QUANT16_ASYMM:
283 case OperandType::TENSOR_QUANT16_SYMM:
284 newOperand.dimensions =
285 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
286 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
287 break;
288 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
289 newOperand.dimensions =
290 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
291 newOperand.scale = 0.0f;
292 newOperand.zeroPoint = 0;
293
294 SymmPerChannelQuantParams channelQuant;
295 channelQuant.channelDim = 0;
296 channelQuant.scales = hidl_vec<float>(
297 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
298 : 0);
299 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
300 channelQuant.scales[i] = 1.0f;
301 }
302 newOperand.extraParams.channelQuant(std::move(channelQuant));
303 } break;
304 case OperandType::OEM:
305 case OperandType::TENSOR_OEM_BYTE:
306 default:
307 break;
308 }
309 *operand = newOperand;
310}
311
312static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
313 // Do not test OEM types
314 if (type == model.operands[operand].type || type == OperandType::OEM ||
315 type == OperandType::TENSOR_OEM_BYTE) {
316 return true;
317 }
318 for (const Operation& operation : model.operations) {
319 // Skip mutateOperationOperandTypeTest for the following operations.
320 // - LSH_PROJECTION's second argument is allowed to have any type.
321 // - ARGMIN and ARGMAX's first argument can be any of
322 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
323 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
324 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
325 // - DEQUANTIZE input can be any of
326 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
327 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
328 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
329 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
330 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
331 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
332 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
333 switch (operation.type) {
334 case OperationType::LSH_PROJECTION: {
335 if (operand == operation.inputs[1]) {
336 return true;
337 }
338 } break;
339 case OperationType::CAST:
340 case OperationType::ARGMAX:
341 case OperationType::ARGMIN: {
342 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000343 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
344 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100345 return true;
346 }
347 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000348 case OperationType::QUANTIZE: {
349 if (operand == operation.inputs[0] &&
350 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
351 return true;
352 }
353 if (operand == operation.outputs[0] &&
354 (type == OperandType::TENSOR_QUANT8_ASYMM ||
355 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
356 return true;
357 }
358 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100359 case OperationType::RANDOM_MULTINOMIAL: {
360 if (operand == operation.inputs[0] &&
361 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
362 return true;
363 }
364 } break;
365 case OperationType::DEQUANTIZE: {
366 if (operand == operation.inputs[0] &&
367 (type == OperandType::TENSOR_QUANT8_ASYMM ||
368 type == OperandType::TENSOR_QUANT8_SYMM ||
369 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
370 return true;
371 }
372 if (operand == operation.outputs[0] &&
373 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
374 return true;
375 }
376 } break;
377 case OperationType::TRANSPOSE_CONV_2D:
378 case OperationType::GROUPED_CONV_2D:
379 case OperationType::DEPTHWISE_CONV_2D:
380 case OperationType::CONV_2D: {
381 if (operand == operation.inputs[1] &&
382 (type == OperandType::TENSOR_QUANT8_ASYMM ||
383 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
384 return true;
385 }
386 } break;
387 default:
388 break;
389 }
390 }
391 return false;
392}
393
394static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
395 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
396 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
397 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
398 continue;
399 }
400 const std::string message = "mutateOperationOperandTypeTest: operand " +
401 std::to_string(operand) + " set to type " +
402 toString(invalidOperandType);
403 validate(device, message, model, [operand, invalidOperandType](Model* model) {
404 mutateOperand(&model->operands[operand], invalidOperandType);
405 });
406 }
407 }
408}
409
410///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
411
412static const uint32_t invalidOperationTypes[] = {
413 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
414 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
415 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
416};
417
418static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
419 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
420 for (uint32_t invalidOperationType : invalidOperationTypes) {
421 const std::string message = "mutateOperationTypeTest: operation " +
422 std::to_string(operation) + " set to value " +
423 std::to_string(invalidOperationType);
424 validate(device, message, model, [operation, invalidOperationType](Model* model) {
425 model->operations[operation].type =
426 static_cast<OperationType>(invalidOperationType);
427 });
428 }
429 }
430}
431
432///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
433
434static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
435 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
436 const uint32_t invalidOperand = model.operands.size();
437 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
438 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
439 std::to_string(operation) + " input " +
440 std::to_string(input);
441 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
442 model->operations[operation].inputs[input] = invalidOperand;
443 });
444 }
445 }
446}
447
448///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
449
450static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
451 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
452 const uint32_t invalidOperand = model.operands.size();
453 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
454 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
455 std::to_string(operation) + " output " +
456 std::to_string(output);
457 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
458 model->operations[operation].outputs[output] = invalidOperand;
459 });
460 }
461 }
462}
463
464///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
465
466static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
467 if (vec) {
468 // remove elements matching "value"
469 auto last = std::remove(vec->begin(), vec->end(), value);
470 vec->resize(std::distance(vec->begin(), last));
471
472 // decrement elements exceeding "value"
473 std::transform(vec->begin(), vec->end(), vec->begin(),
474 [value](uint32_t v) { return v > value ? v-- : v; });
475 }
476}
477
478static void removeOperand(Model* model, uint32_t index) {
479 hidl_vec_removeAt(&model->operands, index);
480 for (Operation& operation : model->operations) {
481 removeValueAndDecrementGreaterValues(&operation.inputs, index);
482 removeValueAndDecrementGreaterValues(&operation.outputs, index);
483 }
484 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
485 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
486}
487
488static bool removeOperandSkip(size_t operand, const Model& model) {
489 for (const Operation& operation : model.operations) {
490 // Skip removeOperandTest for the following operations.
491 // - SPLIT's outputs are not checked during prepareModel.
492 if (operation.type == OperationType::SPLIT) {
493 for (const size_t outOprand : operation.outputs) {
494 if (operand == outOprand) {
495 return true;
496 }
497 }
498 }
499 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
500 // outputs depending on their mergeOutputs parameter.
501 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
502 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
503 for (const size_t outOprand : operation.outputs) {
504 if (operand == outOprand) {
505 return true;
506 }
507 }
508 }
509 }
510 return false;
511}
512
513static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
514 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
515 if (removeOperandSkip(operand, model)) {
516 continue;
517 }
518 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
519 validate(device, message, model,
520 [operand](Model* model) { removeOperand(model, operand); });
521 }
522}
523
524///////////////////////// REMOVE OPERATION /////////////////////////
525
526static void removeOperation(Model* model, uint32_t index) {
527 for (uint32_t operand : model->operations[index].inputs) {
528 model->operands[operand].numberOfConsumers--;
529 }
530 hidl_vec_removeAt(&model->operations, index);
531}
532
533static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
534 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
535 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
536 validate(device, message, model,
537 [operation](Model* model) { removeOperation(model, operation); });
538 }
539}
540
541///////////////////////// REMOVE OPERATION INPUT /////////////////////////
542
543static bool removeOperationInputSkip(const Operation& op, size_t input) {
544 // Skip removeOperationInputTest for the following operations.
545 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
546 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
547 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
548 // layout parameter.
549 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
550 // parameter.
551 switch (op.type) {
552 case OperationType::CONCATENATION: {
553 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
554 return true;
555 }
556 } break;
557 case OperationType::DEPTHWISE_CONV_2D: {
558 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
559 return true;
560 }
561 } break;
562 case OperationType::CONV_2D:
563 case OperationType::AVERAGE_POOL_2D:
564 case OperationType::MAX_POOL_2D:
565 case OperationType::L2_POOL_2D: {
566 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
567 return true;
568 }
569 } break;
570 case OperationType::RESIZE_BILINEAR: {
571 if (op.inputs.size() == 4 && input == 3) {
572 return true;
573 }
574 } break;
575 case OperationType::SPACE_TO_DEPTH:
576 case OperationType::DEPTH_TO_SPACE:
577 case OperationType::BATCH_TO_SPACE_ND: {
578 if (op.inputs.size() == 3 && input == 2) {
579 return true;
580 }
581 } break;
582 case OperationType::SPACE_TO_BATCH_ND: {
583 if (op.inputs.size() == 4 && input == 3) {
584 return true;
585 }
586 } break;
587 case OperationType::L2_NORMALIZATION: {
588 if (op.inputs.size() == 2 && input == 1) {
589 return true;
590 }
591 } break;
592 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
593 if (op.inputs.size() == 6 && input == 5) {
594 return true;
595 }
596 } break;
597 case OperationType::SOFTMAX: {
598 if (op.inputs.size() == 3 && input == 2) {
599 return true;
600 }
601 } break;
602 default:
603 break;
604 }
605 return false;
606}
607
608static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
609 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
610 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
611 const Operation& op = model.operations[operation];
612 if (removeOperationInputSkip(op, input)) {
613 continue;
614 }
615 const std::string message = "removeOperationInputTest: operation " +
616 std::to_string(operation) + ", input " +
617 std::to_string(input);
618 validate(device, message, model, [operation, input](Model* model) {
619 uint32_t operand = model->operations[operation].inputs[input];
620 model->operands[operand].numberOfConsumers--;
621 hidl_vec_removeAt(&model->operations[operation].inputs, input);
622 });
623 }
624 }
625}
626
627///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
628
629static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
630 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
631 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
632 const std::string message = "removeOperationOutputTest: operation " +
633 std::to_string(operation) + ", output " +
634 std::to_string(output);
635 validate(device, message, model, [operation, output](Model* model) {
636 hidl_vec_removeAt(&model->operations[operation].outputs, output);
637 });
638 }
639 }
640}
641
642///////////////////////// MODEL VALIDATION /////////////////////////
643
644// TODO: remove model input
645// TODO: remove model output
646// TODO: add unused operation
647
648///////////////////////// ADD OPERATION INPUT /////////////////////////
649
650static bool addOperationInputSkip(const Operation& op) {
651 // Skip addOperationInputTest for the following operations.
652 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
653 // parameter.
654 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
655 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
656 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
657 return true;
658 }
659 return false;
660}
661
662static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
663 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
664 if (addOperationInputSkip(model.operations[operation])) {
665 continue;
666 }
667 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
668 validate(device, message, model, [operation](Model* model) {
669 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
670 hidl_vec_push_back(&model->operations[operation].inputs, index);
671 hidl_vec_push_back(&model->inputIndexes, index);
672 });
673 }
674}
675
676///////////////////////// ADD OPERATION OUTPUT /////////////////////////
677
678static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
679 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
680 const std::string message =
681 "addOperationOutputTest: operation " + std::to_string(operation);
682 validate(device, message, model, [operation](Model* model) {
683 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
684 hidl_vec_push_back(&model->operations[operation].outputs, index);
685 hidl_vec_push_back(&model->outputIndexes, index);
686 });
687 }
688}
689
690///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
691
692static const int32_t invalidExecutionPreferences[] = {
693 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
694 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
695};
696
697static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
698 for (int32_t preference : invalidExecutionPreferences) {
699 const std::string message =
700 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
701 validate(
702 device, message, model, [](Model*) {},
703 static_cast<ExecutionPreference>(preference));
704 }
705}
706
707////////////////////////// ENTRY POINT //////////////////////////////
708
709void validateModel(const sp<IDevice>& device, const Model& model) {
710 mutateOperandTypeTest(device, model);
711 mutateOperandRankTest(device, model);
712 mutateOperandScaleTest(device, model);
713 mutateOperandZeroPointTest(device, model);
714 mutateOperationOperandTypeTest(device, model);
715 mutateOperationTypeTest(device, model);
716 mutateOperationInputOperandIndexTest(device, model);
717 mutateOperationOutputOperandIndexTest(device, model);
718 removeOperandTest(device, model);
719 removeOperationTest(device, model);
720 removeOperationInputTest(device, model);
721 removeOperationOutputTest(device, model);
722 addOperationInputTest(device, model);
723 addOperationOutputTest(device, model);
724 mutateExecutionPreferenceTest(device, model);
725}
726
Lev Proleev26d1bc82019-08-30 11:57:18 +0100727} // namespace android::hardware::neuralnetworks::V1_3::vts::functional