blob: 14ab897c8c205dcd966b7a31505fb43c1c2aad7d [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
Lev Proleevae643ae2019-12-05 16:57:30 +0000326 // TENSOR_(QUANT8_ASYMM|QUANT8_ASYMM_SIGNED|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL),
327 // output can be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100328 // - 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
Lev Proleevda779f32020-01-02 17:49:03 +0000333 // - AXIS_ALIGNED_BBOX_TRANSFORM bounding boxes (arg 1) can be of
334 // TENSOR_QUANT8_ASYMM or TENSOR_QUANT8_ASYMM_SIGNED.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100335 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 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000345 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
346 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100347 return true;
348 }
349 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000350 case OperationType::QUANTIZE: {
351 if (operand == operation.inputs[0] &&
352 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
353 return true;
354 }
355 if (operand == operation.outputs[0] &&
356 (type == OperandType::TENSOR_QUANT8_ASYMM ||
357 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
358 return true;
359 }
360 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100361 case OperationType::RANDOM_MULTINOMIAL: {
362 if (operand == operation.inputs[0] &&
363 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
364 return true;
365 }
366 } break;
367 case OperationType::DEQUANTIZE: {
368 if (operand == operation.inputs[0] &&
369 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000370 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100371 type == OperandType::TENSOR_QUANT8_SYMM ||
372 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
373 return true;
374 }
375 if (operand == operation.outputs[0] &&
376 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
377 return true;
378 }
379 } break;
380 case OperationType::TRANSPOSE_CONV_2D:
381 case OperationType::GROUPED_CONV_2D:
382 case OperationType::DEPTHWISE_CONV_2D:
383 case OperationType::CONV_2D: {
384 if (operand == operation.inputs[1] &&
385 (type == OperandType::TENSOR_QUANT8_ASYMM ||
386 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
387 return true;
388 }
389 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000390 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
391 if (operand == operation.inputs[1] &&
392 (type == OperandType::TENSOR_QUANT8_ASYMM ||
393 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
394 return true;
395 }
396 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100397 default:
398 break;
399 }
400 }
401 return false;
402}
403
404static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
405 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
406 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
407 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
408 continue;
409 }
410 const std::string message = "mutateOperationOperandTypeTest: operand " +
411 std::to_string(operand) + " set to type " +
412 toString(invalidOperandType);
413 validate(device, message, model, [operand, invalidOperandType](Model* model) {
414 mutateOperand(&model->operands[operand], invalidOperandType);
415 });
416 }
417 }
418}
419
420///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
421
422static const uint32_t invalidOperationTypes[] = {
423 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
424 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
425 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
426};
427
428static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
429 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
430 for (uint32_t invalidOperationType : invalidOperationTypes) {
431 const std::string message = "mutateOperationTypeTest: operation " +
432 std::to_string(operation) + " set to value " +
433 std::to_string(invalidOperationType);
434 validate(device, message, model, [operation, invalidOperationType](Model* model) {
435 model->operations[operation].type =
436 static_cast<OperationType>(invalidOperationType);
437 });
438 }
439 }
440}
441
442///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
443
444static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
445 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
446 const uint32_t invalidOperand = model.operands.size();
447 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
448 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
449 std::to_string(operation) + " input " +
450 std::to_string(input);
451 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
452 model->operations[operation].inputs[input] = invalidOperand;
453 });
454 }
455 }
456}
457
458///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
459
460static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
461 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
462 const uint32_t invalidOperand = model.operands.size();
463 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
464 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
465 std::to_string(operation) + " output " +
466 std::to_string(output);
467 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
468 model->operations[operation].outputs[output] = invalidOperand;
469 });
470 }
471 }
472}
473
474///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
475
476static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
477 if (vec) {
478 // remove elements matching "value"
479 auto last = std::remove(vec->begin(), vec->end(), value);
480 vec->resize(std::distance(vec->begin(), last));
481
482 // decrement elements exceeding "value"
483 std::transform(vec->begin(), vec->end(), vec->begin(),
484 [value](uint32_t v) { return v > value ? v-- : v; });
485 }
486}
487
488static void removeOperand(Model* model, uint32_t index) {
489 hidl_vec_removeAt(&model->operands, index);
490 for (Operation& operation : model->operations) {
491 removeValueAndDecrementGreaterValues(&operation.inputs, index);
492 removeValueAndDecrementGreaterValues(&operation.outputs, index);
493 }
494 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
495 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
496}
497
498static bool removeOperandSkip(size_t operand, const Model& model) {
499 for (const Operation& operation : model.operations) {
500 // Skip removeOperandTest for the following operations.
501 // - SPLIT's outputs are not checked during prepareModel.
502 if (operation.type == OperationType::SPLIT) {
503 for (const size_t outOprand : operation.outputs) {
504 if (operand == outOprand) {
505 return true;
506 }
507 }
508 }
509 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
510 // outputs depending on their mergeOutputs parameter.
511 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
512 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
513 for (const size_t outOprand : operation.outputs) {
514 if (operand == outOprand) {
515 return true;
516 }
517 }
518 }
519 }
520 return false;
521}
522
523static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
524 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
525 if (removeOperandSkip(operand, model)) {
526 continue;
527 }
528 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
529 validate(device, message, model,
530 [operand](Model* model) { removeOperand(model, operand); });
531 }
532}
533
534///////////////////////// REMOVE OPERATION /////////////////////////
535
536static void removeOperation(Model* model, uint32_t index) {
537 for (uint32_t operand : model->operations[index].inputs) {
538 model->operands[operand].numberOfConsumers--;
539 }
540 hidl_vec_removeAt(&model->operations, index);
541}
542
543static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
544 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
545 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
546 validate(device, message, model,
547 [operation](Model* model) { removeOperation(model, operation); });
548 }
549}
550
551///////////////////////// REMOVE OPERATION INPUT /////////////////////////
552
553static bool removeOperationInputSkip(const Operation& op, size_t input) {
554 // Skip removeOperationInputTest for the following operations.
555 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
556 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
557 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
558 // layout parameter.
559 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
560 // parameter.
561 switch (op.type) {
562 case OperationType::CONCATENATION: {
563 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
564 return true;
565 }
566 } break;
567 case OperationType::DEPTHWISE_CONV_2D: {
568 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
569 return true;
570 }
571 } break;
572 case OperationType::CONV_2D:
573 case OperationType::AVERAGE_POOL_2D:
574 case OperationType::MAX_POOL_2D:
575 case OperationType::L2_POOL_2D: {
576 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
577 return true;
578 }
579 } break;
580 case OperationType::RESIZE_BILINEAR: {
581 if (op.inputs.size() == 4 && input == 3) {
582 return true;
583 }
584 } break;
585 case OperationType::SPACE_TO_DEPTH:
586 case OperationType::DEPTH_TO_SPACE:
587 case OperationType::BATCH_TO_SPACE_ND: {
588 if (op.inputs.size() == 3 && input == 2) {
589 return true;
590 }
591 } break;
592 case OperationType::SPACE_TO_BATCH_ND: {
593 if (op.inputs.size() == 4 && input == 3) {
594 return true;
595 }
596 } break;
597 case OperationType::L2_NORMALIZATION: {
598 if (op.inputs.size() == 2 && input == 1) {
599 return true;
600 }
601 } break;
602 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
603 if (op.inputs.size() == 6 && input == 5) {
604 return true;
605 }
606 } break;
607 case OperationType::SOFTMAX: {
608 if (op.inputs.size() == 3 && input == 2) {
609 return true;
610 }
611 } break;
612 default:
613 break;
614 }
615 return false;
616}
617
618static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
619 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
620 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
621 const Operation& op = model.operations[operation];
622 if (removeOperationInputSkip(op, input)) {
623 continue;
624 }
625 const std::string message = "removeOperationInputTest: operation " +
626 std::to_string(operation) + ", input " +
627 std::to_string(input);
628 validate(device, message, model, [operation, input](Model* model) {
629 uint32_t operand = model->operations[operation].inputs[input];
630 model->operands[operand].numberOfConsumers--;
631 hidl_vec_removeAt(&model->operations[operation].inputs, input);
632 });
633 }
634 }
635}
636
637///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
638
639static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
640 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
641 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
642 const std::string message = "removeOperationOutputTest: operation " +
643 std::to_string(operation) + ", output " +
644 std::to_string(output);
645 validate(device, message, model, [operation, output](Model* model) {
646 hidl_vec_removeAt(&model->operations[operation].outputs, output);
647 });
648 }
649 }
650}
651
652///////////////////////// MODEL VALIDATION /////////////////////////
653
654// TODO: remove model input
655// TODO: remove model output
656// TODO: add unused operation
657
658///////////////////////// ADD OPERATION INPUT /////////////////////////
659
660static bool addOperationInputSkip(const Operation& op) {
661 // Skip addOperationInputTest for the following operations.
662 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
663 // parameter.
664 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
665 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
666 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
667 return true;
668 }
669 return false;
670}
671
672static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
673 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
674 if (addOperationInputSkip(model.operations[operation])) {
675 continue;
676 }
677 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
678 validate(device, message, model, [operation](Model* model) {
679 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
680 hidl_vec_push_back(&model->operations[operation].inputs, index);
681 hidl_vec_push_back(&model->inputIndexes, index);
682 });
683 }
684}
685
686///////////////////////// ADD OPERATION OUTPUT /////////////////////////
687
688static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
689 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
690 const std::string message =
691 "addOperationOutputTest: operation " + std::to_string(operation);
692 validate(device, message, model, [operation](Model* model) {
693 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
694 hidl_vec_push_back(&model->operations[operation].outputs, index);
695 hidl_vec_push_back(&model->outputIndexes, index);
696 });
697 }
698}
699
700///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
701
702static const int32_t invalidExecutionPreferences[] = {
703 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
704 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
705};
706
707static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
708 for (int32_t preference : invalidExecutionPreferences) {
709 const std::string message =
710 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
711 validate(
712 device, message, model, [](Model*) {},
713 static_cast<ExecutionPreference>(preference));
714 }
715}
716
717////////////////////////// ENTRY POINT //////////////////////////////
718
719void validateModel(const sp<IDevice>& device, const Model& model) {
720 mutateOperandTypeTest(device, model);
721 mutateOperandRankTest(device, model);
722 mutateOperandScaleTest(device, model);
723 mutateOperandZeroPointTest(device, model);
724 mutateOperationOperandTypeTest(device, model);
725 mutateOperationTypeTest(device, model);
726 mutateOperationInputOperandIndexTest(device, model);
727 mutateOperationOutputOperandIndexTest(device, model);
728 removeOperandTest(device, model);
729 removeOperationTest(device, model);
730 removeOperationInputTest(device, model);
731 removeOperationOutputTest(device, model);
732 addOperationInputTest(device, model);
733 addOperationOutputTest(device, model);
734 mutateExecutionPreferenceTest(device, model);
735}
736
Lev Proleev26d1bc82019-08-30 11:57:18 +0100737} // namespace android::hardware::neuralnetworks::V1_3::vts::functional