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