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