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