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