blob: 7ec6ff183e3de3d64125411e9b9e2569aa353d99 [file] [log] [blame]
Slava Shklyaevfeb87a92018-09-12 14:52:02 +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 "VtsHalNeuralnetworks.h"
20
21#include "Callbacks.h"
22
23namespace android {
24namespace hardware {
25namespace neuralnetworks {
26namespace V1_2 {
27
28using V1_0::IPreparedModel;
29using V1_0::Operand;
30using V1_0::OperandLifeTime;
31using V1_0::OperandType;
32using V1_1::ExecutionPreference;
33
34namespace vts {
35namespace functional {
36
37using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
38using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
39
40///////////////////////// UTILITY FUNCTIONS /////////////////////////
41
42static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
43 const Model& model) {
44 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
45
46 Return<void> ret =
47 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
48 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
49 });
50 EXPECT_TRUE(ret.isOk());
51}
52
53static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
54 const Model& model, ExecutionPreference preference) {
55 SCOPED_TRACE(message + " [prepareModel_1_2]");
56
57 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
58 ASSERT_NE(nullptr, preparedModelCallback.get());
59 Return<ErrorStatus> prepareLaunchStatus =
60 device->prepareModel_1_2(model, preference, preparedModelCallback);
61 ASSERT_TRUE(prepareLaunchStatus.isOk());
62 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
63
64 preparedModelCallback->wait();
65 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
66 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
67 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
68 ASSERT_EQ(nullptr, preparedModel.get());
69}
70
71static bool validExecutionPreference(ExecutionPreference preference) {
72 return preference == ExecutionPreference::LOW_POWER ||
73 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
74 preference == ExecutionPreference::SUSTAINED_SPEED;
75}
76
77// Primary validation function. This function will take a valid model, apply a
78// mutation to it to invalidate the model, then pass it to interface calls that
79// use the model. Note that the model here is passed by value, and any mutation
80// to the model does not leave this function.
81static void validate(const sp<IDevice>& device, const std::string& message, Model model,
82 const std::function<void(Model*)>& mutation,
83 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
84 mutation(&model);
85 if (validExecutionPreference(preference)) {
86 validateGetSupportedOperations(device, message, model);
87 }
88 validatePrepareModel(device, message, model, preference);
89}
90
91// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
92// so this is efficiently accomplished by moving the element to the end and
93// resizing the hidl_vec to one less.
94template <typename Type>
95static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
96 if (vec) {
97 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
98 vec->resize(vec->size() - 1);
99 }
100}
101
102template <typename Type>
103static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
104 // assume vec is valid
105 const uint32_t index = vec->size();
106 vec->resize(index + 1);
107 (*vec)[index] = value;
108 return index;
109}
110
111static uint32_t addOperand(Model* model) {
112 return hidl_vec_push_back(&model->operands,
113 {
114 .type = OperandType::INT32,
115 .dimensions = {},
116 .numberOfConsumers = 0,
117 .scale = 0.0f,
118 .zeroPoint = 0,
119 .lifetime = OperandLifeTime::MODEL_INPUT,
120 .location = {.poolIndex = 0, .offset = 0, .length = 0},
121 });
122}
123
124static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
125 uint32_t index = addOperand(model);
126 model->operands[index].numberOfConsumers = 1;
127 model->operands[index].lifetime = lifetime;
128 return index;
129}
130
131///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
132
133static const int32_t invalidOperandTypes[] = {
134 static_cast<int32_t>(OperandType::FLOAT32) - 1, // lower bound fundamental
135 static_cast<int32_t>(OperandType::TENSOR_QUANT8_ASYMM) + 1, // upper bound fundamental
136 static_cast<int32_t>(OperandType::OEM) - 1, // lower bound OEM
137 static_cast<int32_t>(OperandType::TENSOR_OEM_BYTE) + 1, // upper bound OEM
138};
139
140static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
141 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
142 for (int32_t invalidOperandType : invalidOperandTypes) {
143 const std::string message = "mutateOperandTypeTest: operand " +
144 std::to_string(operand) + " set to value " +
145 std::to_string(invalidOperandType);
146 validate(device, message, model, [operand, invalidOperandType](Model* model) {
147 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
148 });
149 }
150 }
151}
152
153///////////////////////// VALIDATE OPERAND RANK /////////////////////////
154
155static uint32_t getInvalidRank(OperandType type) {
156 switch (type) {
157 case OperandType::FLOAT32:
158 case OperandType::INT32:
159 case OperandType::UINT32:
160 return 1;
161 case OperandType::TENSOR_FLOAT32:
162 case OperandType::TENSOR_INT32:
163 case OperandType::TENSOR_QUANT8_ASYMM:
164 return 0;
165 default:
166 return 0;
167 }
168}
169
170static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
171 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
172 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
173 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
174 " has rank of " + std::to_string(invalidRank);
175 validate(device, message, model, [operand, invalidRank](Model* model) {
176 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
177 });
178 }
179}
180
181///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
182
183static float getInvalidScale(OperandType type) {
184 switch (type) {
185 case OperandType::FLOAT32:
186 case OperandType::INT32:
187 case OperandType::UINT32:
188 case OperandType::TENSOR_FLOAT32:
189 return 1.0f;
190 case OperandType::TENSOR_INT32:
191 return -1.0f;
192 case OperandType::TENSOR_QUANT8_ASYMM:
193 return 0.0f;
194 default:
195 return 0.0f;
196 }
197}
198
199static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
200 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
201 const float invalidScale = getInvalidScale(model.operands[operand].type);
202 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
203 " has scale of " + std::to_string(invalidScale);
204 validate(device, message, model, [operand, invalidScale](Model* model) {
205 model->operands[operand].scale = invalidScale;
206 });
207 }
208}
209
210///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
211
212static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
213 switch (type) {
214 case OperandType::FLOAT32:
215 case OperandType::INT32:
216 case OperandType::UINT32:
217 case OperandType::TENSOR_FLOAT32:
218 case OperandType::TENSOR_INT32:
219 return {1};
220 case OperandType::TENSOR_QUANT8_ASYMM:
221 return {-1, 256};
222 default:
223 return {};
224 }
225}
226
227static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
228 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
229 const std::vector<int32_t> invalidZeroPoints =
230 getInvalidZeroPoints(model.operands[operand].type);
231 for (int32_t invalidZeroPoint : invalidZeroPoints) {
232 const std::string message = "mutateOperandZeroPointTest: operand " +
233 std::to_string(operand) + " has zero point of " +
234 std::to_string(invalidZeroPoint);
235 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
236 model->operands[operand].zeroPoint = invalidZeroPoint;
237 });
238 }
239 }
240}
241
242///////////////////////// VALIDATE EXTRA ??? /////////////////////////
243
244// TODO: Operand::lifetime
245// TODO: Operand::location
246
247///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
248
249static void mutateOperand(Operand* operand, OperandType type) {
250 Operand newOperand = *operand;
251 newOperand.type = type;
252 switch (type) {
253 case OperandType::FLOAT32:
254 case OperandType::INT32:
255 case OperandType::UINT32:
256 newOperand.dimensions = hidl_vec<uint32_t>();
257 newOperand.scale = 0.0f;
258 newOperand.zeroPoint = 0;
259 break;
260 case OperandType::TENSOR_FLOAT32:
261 newOperand.dimensions =
262 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
263 newOperand.scale = 0.0f;
264 newOperand.zeroPoint = 0;
265 break;
266 case OperandType::TENSOR_INT32:
267 newOperand.dimensions =
268 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
269 newOperand.zeroPoint = 0;
270 break;
271 case OperandType::TENSOR_QUANT8_ASYMM:
272 newOperand.dimensions =
273 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
274 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
275 break;
276 case OperandType::OEM:
277 case OperandType::TENSOR_OEM_BYTE:
278 default:
279 break;
280 }
281 *operand = newOperand;
282}
283
284static bool mutateOperationOperandTypeSkip(size_t operand, const Model& model) {
285 // LSH_PROJECTION's second argument is allowed to have any type. This is the
286 // only operation that currently has a type that can be anything independent
287 // from any other type. Changing the operand type to any other type will
288 // result in a valid model for LSH_PROJECTION. If this is the case, skip the
289 // test.
290 for (const Operation& operation : model.operations) {
291 if (operation.type == OperationType::LSH_PROJECTION && operand == operation.inputs[1]) {
292 return true;
293 }
294 }
295 return false;
296}
297
298static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
299 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
300 if (mutateOperationOperandTypeSkip(operand, model)) {
301 continue;
302 }
303 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
304 // Do not test OEM types
305 if (invalidOperandType == model.operands[operand].type ||
306 invalidOperandType == OperandType::OEM ||
307 invalidOperandType == OperandType::TENSOR_OEM_BYTE) {
308 continue;
309 }
310 const std::string message = "mutateOperationOperandTypeTest: operand " +
311 std::to_string(operand) + " set to type " +
312 toString(invalidOperandType);
313 validate(device, message, model, [operand, invalidOperandType](Model* model) {
314 mutateOperand(&model->operands[operand], invalidOperandType);
315 });
316 }
317 }
318}
319
320///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
321
322static const int32_t invalidOperationTypes[] = {
323 static_cast<int32_t>(OperationType::ADD) - 1, // lower bound fundamental
324 static_cast<int32_t>(OperationType::TRANSPOSE) + 1, // upper bound fundamental
325 static_cast<int32_t>(OperationType::OEM_OPERATION) - 1, // lower bound OEM
326 static_cast<int32_t>(OperationType::OEM_OPERATION) + 1, // upper bound OEM
327};
328
329static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
330 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
331 for (int32_t invalidOperationType : invalidOperationTypes) {
332 const std::string message = "mutateOperationTypeTest: operation " +
333 std::to_string(operation) + " set to value " +
334 std::to_string(invalidOperationType);
335 validate(device, message, model, [operation, invalidOperationType](Model* model) {
336 model->operations[operation].type =
337 static_cast<OperationType>(invalidOperationType);
338 });
339 }
340 }
341}
342
343///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
344
345static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
346 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
347 const uint32_t invalidOperand = model.operands.size();
348 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
349 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
350 std::to_string(operation) + " input " +
351 std::to_string(input);
352 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
353 model->operations[operation].inputs[input] = invalidOperand;
354 });
355 }
356 }
357}
358
359///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
360
361static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
362 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
363 const uint32_t invalidOperand = model.operands.size();
364 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
365 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
366 std::to_string(operation) + " output " +
367 std::to_string(output);
368 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
369 model->operations[operation].outputs[output] = invalidOperand;
370 });
371 }
372 }
373}
374
375///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
376
377static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
378 if (vec) {
379 // remove elements matching "value"
380 auto last = std::remove(vec->begin(), vec->end(), value);
381 vec->resize(std::distance(vec->begin(), last));
382
383 // decrement elements exceeding "value"
384 std::transform(vec->begin(), vec->end(), vec->begin(),
385 [value](uint32_t v) { return v > value ? v-- : v; });
386 }
387}
388
389static void removeOperand(Model* model, uint32_t index) {
390 hidl_vec_removeAt(&model->operands, index);
391 for (Operation& operation : model->operations) {
392 removeValueAndDecrementGreaterValues(&operation.inputs, index);
393 removeValueAndDecrementGreaterValues(&operation.outputs, index);
394 }
395 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
396 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
397}
398
399static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
400 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
401 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
402 validate(device, message, model,
403 [operand](Model* model) { removeOperand(model, operand); });
404 }
405}
406
407///////////////////////// REMOVE OPERATION /////////////////////////
408
409static void removeOperation(Model* model, uint32_t index) {
410 for (uint32_t operand : model->operations[index].inputs) {
411 model->operands[operand].numberOfConsumers--;
412 }
413 hidl_vec_removeAt(&model->operations, index);
414}
415
416static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
417 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
418 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
419 validate(device, message, model,
420 [operation](Model* model) { removeOperation(model, operation); });
421 }
422}
423
424///////////////////////// REMOVE OPERATION INPUT /////////////////////////
425
426static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
427 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
428 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
429 const Operation& op = model.operations[operation];
430 // CONCATENATION has at least 2 inputs, with the last element being
431 // INT32. Skip this test if removing one of CONCATENATION's
432 // inputs still produces a valid model.
433 if (op.type == OperationType::CONCATENATION && op.inputs.size() > 2 &&
434 input != op.inputs.size() - 1) {
435 continue;
436 }
437 const std::string message = "removeOperationInputTest: operation " +
438 std::to_string(operation) + ", input " +
439 std::to_string(input);
440 validate(device, message, model, [operation, input](Model* model) {
441 uint32_t operand = model->operations[operation].inputs[input];
442 model->operands[operand].numberOfConsumers--;
443 hidl_vec_removeAt(&model->operations[operation].inputs, input);
444 });
445 }
446 }
447}
448
449///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
450
451static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
452 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
453 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
454 const std::string message = "removeOperationOutputTest: operation " +
455 std::to_string(operation) + ", output " +
456 std::to_string(output);
457 validate(device, message, model, [operation, output](Model* model) {
458 hidl_vec_removeAt(&model->operations[operation].outputs, output);
459 });
460 }
461 }
462}
463
464///////////////////////// MODEL VALIDATION /////////////////////////
465
466// TODO: remove model input
467// TODO: remove model output
468// TODO: add unused operation
469
470///////////////////////// ADD OPERATION INPUT /////////////////////////
471
472static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
473 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
474 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
475 validate(device, message, model, [operation](Model* model) {
476 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
477 hidl_vec_push_back(&model->operations[operation].inputs, index);
478 hidl_vec_push_back(&model->inputIndexes, index);
479 });
480 }
481}
482
483///////////////////////// ADD OPERATION OUTPUT /////////////////////////
484
485static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
486 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
487 const std::string message =
488 "addOperationOutputTest: operation " + std::to_string(operation);
489 validate(device, message, model, [operation](Model* model) {
490 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
491 hidl_vec_push_back(&model->operations[operation].outputs, index);
492 hidl_vec_push_back(&model->outputIndexes, index);
493 });
494 }
495}
496
497///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
498
499static const int32_t invalidExecutionPreferences[] = {
500 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
501 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
502};
503
504static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
505 for (int32_t preference : invalidExecutionPreferences) {
506 const std::string message =
507 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
508 validate(device, message, model, [](Model*) {},
509 static_cast<ExecutionPreference>(preference));
510 }
511}
512
513////////////////////////// ENTRY POINT //////////////////////////////
514
515void ValidationTest::validateModel(const Model& model) {
516 mutateOperandTypeTest(device, model);
517 mutateOperandRankTest(device, model);
518 mutateOperandScaleTest(device, model);
519 mutateOperandZeroPointTest(device, model);
520 mutateOperationOperandTypeTest(device, model);
521 mutateOperationTypeTest(device, model);
522 mutateOperationInputOperandIndexTest(device, model);
523 mutateOperationOutputOperandIndexTest(device, model);
524 removeOperandTest(device, model);
525 removeOperationTest(device, model);
526 removeOperationInputTest(device, model);
527 removeOperationOutputTest(device, model);
528 addOperationInputTest(device, model);
529 addOperationOutputTest(device, model);
530 mutateExecutionPreferenceTest(device, model);
531}
532
533} // namespace functional
534} // namespace vts
535} // namespace V1_2
536} // namespace neuralnetworks
537} // namespace hardware
538} // namespace android