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