blob: 1bbb20392a9c3321bee60ff44d21deb275b234b2 [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
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
30
31namespace vts {
32namespace functional {
33
Xusong Wang1a06e772018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
35using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010036
37///////////////////////// UTILITY FUNCTIONS /////////////////////////
38
39static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
40 const Model& model) {
41 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
42
43 Return<void> ret =
44 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
45 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
46 });
47 EXPECT_TRUE(ret.isOk());
48}
49
50static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
51 const Model& model, ExecutionPreference preference) {
52 SCOPED_TRACE(message + " [prepareModel_1_2]");
53
54 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
55 ASSERT_NE(nullptr, preparedModelCallback.get());
56 Return<ErrorStatus> prepareLaunchStatus =
57 device->prepareModel_1_2(model, preference, preparedModelCallback);
58 ASSERT_TRUE(prepareLaunchStatus.isOk());
59 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
60
61 preparedModelCallback->wait();
62 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
63 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wang1a06e772018-10-31 08:43:12 -070064 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010065 ASSERT_EQ(nullptr, preparedModel.get());
66}
67
68static bool validExecutionPreference(ExecutionPreference preference) {
69 return preference == ExecutionPreference::LOW_POWER ||
70 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
71 preference == ExecutionPreference::SUSTAINED_SPEED;
72}
73
74// Primary validation function. This function will take a valid model, apply a
75// mutation to it to invalidate the model, then pass it to interface calls that
76// use the model. Note that the model here is passed by value, and any mutation
77// to the model does not leave this function.
78static void validate(const sp<IDevice>& device, const std::string& message, Model model,
79 const std::function<void(Model*)>& mutation,
80 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
81 mutation(&model);
82 if (validExecutionPreference(preference)) {
83 validateGetSupportedOperations(device, message, model);
84 }
85 validatePrepareModel(device, message, model, preference);
86}
87
88// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
89// so this is efficiently accomplished by moving the element to the end and
90// resizing the hidl_vec to one less.
91template <typename Type>
92static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
93 if (vec) {
94 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
95 vec->resize(vec->size() - 1);
96 }
97}
98
99template <typename Type>
100static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
101 // assume vec is valid
102 const uint32_t index = vec->size();
103 vec->resize(index + 1);
104 (*vec)[index] = value;
105 return index;
106}
107
108static uint32_t addOperand(Model* model) {
109 return hidl_vec_push_back(&model->operands,
110 {
111 .type = OperandType::INT32,
112 .dimensions = {},
113 .numberOfConsumers = 0,
114 .scale = 0.0f,
115 .zeroPoint = 0,
116 .lifetime = OperandLifeTime::MODEL_INPUT,
117 .location = {.poolIndex = 0, .offset = 0, .length = 0},
118 });
119}
120
121static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
122 uint32_t index = addOperand(model);
123 model->operands[index].numberOfConsumers = 1;
124 model->operands[index].lifetime = lifetime;
125 return index;
126}
127
128///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
129
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000130static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000131 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
132 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
133 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
134 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100135};
136
137static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
138 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000139 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100140 const std::string message = "mutateOperandTypeTest: operand " +
141 std::to_string(operand) + " set to value " +
142 std::to_string(invalidOperandType);
143 validate(device, message, model, [operand, invalidOperandType](Model* model) {
144 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
145 });
146 }
147 }
148}
149
150///////////////////////// VALIDATE OPERAND RANK /////////////////////////
151
152static uint32_t getInvalidRank(OperandType type) {
153 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800154 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100155 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;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100160 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100161 case OperandType::TENSOR_FLOAT32:
162 case OperandType::TENSOR_INT32:
163 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800164 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000165 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000166 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100167 return 0;
168 default:
169 return 0;
170 }
171}
172
173static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
174 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
175 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wangd22c5232018-11-19 18:26:08 -0800176 if (invalidRank == 0) {
177 continue;
178 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100179 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
180 " has rank of " + std::to_string(invalidRank);
181 validate(device, message, model, [operand, invalidRank](Model* model) {
182 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
183 });
184 }
185}
186
187///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
188
189static float getInvalidScale(OperandType type) {
190 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800191 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100192 case OperandType::FLOAT32:
193 case OperandType::INT32:
194 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100195 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100196 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100197 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000198 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100199 return 1.0f;
200 case OperandType::TENSOR_INT32:
201 return -1.0f;
202 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800203 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000204 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100205 return 0.0f;
206 default:
207 return 0.0f;
208 }
209}
210
211static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
212 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
213 const float invalidScale = getInvalidScale(model.operands[operand].type);
214 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
215 " has scale of " + std::to_string(invalidScale);
216 validate(device, message, model, [operand, invalidScale](Model* model) {
217 model->operands[operand].scale = invalidScale;
218 });
219 }
220}
221
222///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
223
224static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
225 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800226 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100227 case OperandType::FLOAT32:
228 case OperandType::INT32:
229 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100230 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100231 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100232 case OperandType::TENSOR_FLOAT32:
233 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000234 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100235 return {1};
236 case OperandType::TENSOR_QUANT8_ASYMM:
237 return {-1, 256};
Xusong Wangcf6a9112019-01-16 18:32:24 -0800238 case OperandType::TENSOR_QUANT16_ASYMM:
239 return {-1, 65536};
Lev Proleev217c4072018-11-13 15:42:36 +0000240 case OperandType::TENSOR_QUANT16_SYMM:
241 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100242 default:
243 return {};
244 }
245}
246
247static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
248 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
249 const std::vector<int32_t> invalidZeroPoints =
250 getInvalidZeroPoints(model.operands[operand].type);
251 for (int32_t invalidZeroPoint : invalidZeroPoints) {
252 const std::string message = "mutateOperandZeroPointTest: operand " +
253 std::to_string(operand) + " has zero point of " +
254 std::to_string(invalidZeroPoint);
255 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
256 model->operands[operand].zeroPoint = invalidZeroPoint;
257 });
258 }
259 }
260}
261
262///////////////////////// VALIDATE EXTRA ??? /////////////////////////
263
264// TODO: Operand::lifetime
265// TODO: Operand::location
266
267///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
268
269static void mutateOperand(Operand* operand, OperandType type) {
270 Operand newOperand = *operand;
271 newOperand.type = type;
272 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800273 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100274 case OperandType::FLOAT32:
275 case OperandType::INT32:
276 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100277 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100278 newOperand.dimensions = hidl_vec<uint32_t>();
279 newOperand.scale = 0.0f;
280 newOperand.zeroPoint = 0;
281 break;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100282 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100283 case OperandType::TENSOR_FLOAT32:
284 newOperand.dimensions =
285 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
286 newOperand.scale = 0.0f;
287 newOperand.zeroPoint = 0;
288 break;
289 case OperandType::TENSOR_INT32:
290 newOperand.dimensions =
291 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
292 newOperand.zeroPoint = 0;
293 break;
294 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800295 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000296 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100297 newOperand.dimensions =
298 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
299 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
300 break;
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000301 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
302 newOperand.dimensions =
303 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
304 newOperand.scale = 0.0f;
305 newOperand.zeroPoint = 0;
306
307 SymmPerChannelQuantParams channelQuant;
308 channelQuant.channelDim = 0;
309 channelQuant.scales = hidl_vec<float>(
310 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0]) : 0);
311 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
312 channelQuant.scales[i] = 1.0f;
313 }
314 newOperand.extraParams.channelQuant(std::move(channelQuant));
315 } break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100316 case OperandType::OEM:
317 case OperandType::TENSOR_OEM_BYTE:
318 default:
319 break;
320 }
321 *operand = newOperand;
322}
323
Xusong Wang1a2492f2018-10-05 11:49:13 -0700324static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
325 // Do not test OEM types
326 if (type == model.operands[operand].type || type == OperandType::OEM ||
327 type == OperandType::TENSOR_OEM_BYTE) {
328 return true;
329 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100330 for (const Operation& operation : model.operations) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700331 // Skip mutateOperationOperandTypeTest for the following operations.
332 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sanders2a010122018-11-28 10:35:08 +0000333 // - ARGMIN and ARGMAX's first argument can be any of
334 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
335 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders41e67322018-12-06 12:34:07 +0000336 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000337 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000338 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000339 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev2d982642019-01-15 17:49:24 +0000340 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang1a2492f2018-10-05 11:49:13 -0700341 switch (operation.type) {
342 case OperationType::LSH_PROJECTION: {
343 if (operand == operation.inputs[1]) {
344 return true;
345 }
346 } break;
347 case OperationType::CAST:
348 case OperationType::ARGMAX:
349 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000350 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
351 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700352 return true;
353 }
354 } break;
Michael K. Sanders41e67322018-12-06 12:34:07 +0000355 case OperationType::RANDOM_MULTINOMIAL: {
356 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
357 return true;
358 }
359 } break;
Lev Proleev2d982642019-01-15 17:49:24 +0000360 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000361 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000362 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000363 case OperationType::CONV_2D: {
364 if (operand == 1 && (type == OperandType::TENSOR_QUANT8_ASYMM ||
365 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
366 return true;
367 }
368 } break;
Xusong Wang1a2492f2018-10-05 11:49:13 -0700369 default:
370 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100371 }
372 }
373 return false;
374}
375
376static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
377 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100378 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700379 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100380 continue;
381 }
382 const std::string message = "mutateOperationOperandTypeTest: operand " +
383 std::to_string(operand) + " set to type " +
384 toString(invalidOperandType);
385 validate(device, message, model, [operand, invalidOperandType](Model* model) {
386 mutateOperand(&model->operands[operand], invalidOperandType);
387 });
388 }
389 }
390}
391
392///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
393
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000394static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000395 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MIN) - 1,
396 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
397 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
398 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100399};
400
401static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
402 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000403 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100404 const std::string message = "mutateOperationTypeTest: operation " +
405 std::to_string(operation) + " set to value " +
406 std::to_string(invalidOperationType);
407 validate(device, message, model, [operation, invalidOperationType](Model* model) {
408 model->operations[operation].type =
409 static_cast<OperationType>(invalidOperationType);
410 });
411 }
412 }
413}
414
415///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
416
417static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
418 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
419 const uint32_t invalidOperand = model.operands.size();
420 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
421 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
422 std::to_string(operation) + " input " +
423 std::to_string(input);
424 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
425 model->operations[operation].inputs[input] = invalidOperand;
426 });
427 }
428 }
429}
430
431///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
432
433static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
434 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
435 const uint32_t invalidOperand = model.operands.size();
436 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
437 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
438 std::to_string(operation) + " output " +
439 std::to_string(output);
440 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
441 model->operations[operation].outputs[output] = invalidOperand;
442 });
443 }
444 }
445}
446
447///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
448
449static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
450 if (vec) {
451 // remove elements matching "value"
452 auto last = std::remove(vec->begin(), vec->end(), value);
453 vec->resize(std::distance(vec->begin(), last));
454
455 // decrement elements exceeding "value"
456 std::transform(vec->begin(), vec->end(), vec->begin(),
457 [value](uint32_t v) { return v > value ? v-- : v; });
458 }
459}
460
461static void removeOperand(Model* model, uint32_t index) {
462 hidl_vec_removeAt(&model->operands, index);
463 for (Operation& operation : model->operations) {
464 removeValueAndDecrementGreaterValues(&operation.inputs, index);
465 removeValueAndDecrementGreaterValues(&operation.outputs, index);
466 }
467 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
468 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
469}
470
Xusong Wang1a2492f2018-10-05 11:49:13 -0700471static bool removeOperandSkip(size_t operand, const Model& model) {
472 for (const Operation& operation : model.operations) {
473 // Skip removeOperandTest for the following operations.
474 // - SPLIT's outputs are not checked during prepareModel.
475 if (operation.type == OperationType::SPLIT) {
476 for (const size_t outOprand : operation.outputs) {
477 if (operand == outOprand) {
478 return true;
479 }
480 }
481 }
482 }
483 return false;
484}
485
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100486static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
487 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700488 if (removeOperandSkip(operand, model)) {
489 continue;
490 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100491 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
492 validate(device, message, model,
493 [operand](Model* model) { removeOperand(model, operand); });
494 }
495}
496
497///////////////////////// REMOVE OPERATION /////////////////////////
498
499static void removeOperation(Model* model, uint32_t index) {
500 for (uint32_t operand : model->operations[index].inputs) {
501 model->operands[operand].numberOfConsumers--;
502 }
503 hidl_vec_removeAt(&model->operations, index);
504}
505
506static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
507 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
508 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
509 validate(device, message, model,
510 [operation](Model* model) { removeOperation(model, operation); });
511 }
512}
513
514///////////////////////// REMOVE OPERATION INPUT /////////////////////////
515
Xusong Wang1a2492f2018-10-05 11:49:13 -0700516static bool removeOperationInputSkip(const Operation& op, size_t input) {
517 // Skip removeOperationInputTest for the following operations.
518 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
519 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
520 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
521 // layout parameter.
522 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
523 // parameter.
524 switch (op.type) {
525 case OperationType::CONCATENATION: {
526 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
527 return true;
528 }
529 } break;
530 case OperationType::DEPTHWISE_CONV_2D: {
531 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
532 return true;
533 }
534 } break;
535 case OperationType::CONV_2D:
536 case OperationType::AVERAGE_POOL_2D:
537 case OperationType::MAX_POOL_2D:
538 case OperationType::L2_POOL_2D: {
539 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
540 return true;
541 }
542 } break;
543 case OperationType::RESIZE_BILINEAR: {
544 if (op.inputs.size() == 4 && input == 3) {
545 return true;
546 }
547 } break;
548 case OperationType::SPACE_TO_DEPTH:
549 case OperationType::DEPTH_TO_SPACE:
550 case OperationType::BATCH_TO_SPACE_ND: {
551 if (op.inputs.size() == 3 && input == 2) {
552 return true;
553 }
554 } break;
555 case OperationType::SPACE_TO_BATCH_ND: {
556 if (op.inputs.size() == 4 && input == 3) {
557 return true;
558 }
559 } break;
560 case OperationType::L2_NORMALIZATION: {
561 if (op.inputs.size() == 2 && input == 1) {
562 return true;
563 }
564 } break;
565 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
566 if (op.inputs.size() == 6 && input == 5) {
567 return true;
568 }
569 } break;
570 case OperationType::SOFTMAX: {
571 if (op.inputs.size() == 3 && input == 2) {
572 return true;
573 }
574 } break;
575 default:
576 break;
577 }
578 return false;
579}
580
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100581static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
582 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
583 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
584 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700585 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100586 continue;
587 }
588 const std::string message = "removeOperationInputTest: operation " +
589 std::to_string(operation) + ", input " +
590 std::to_string(input);
591 validate(device, message, model, [operation, input](Model* model) {
592 uint32_t operand = model->operations[operation].inputs[input];
593 model->operands[operand].numberOfConsumers--;
594 hidl_vec_removeAt(&model->operations[operation].inputs, input);
595 });
596 }
597 }
598}
599
600///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
601
602static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
603 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
604 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
605 const std::string message = "removeOperationOutputTest: operation " +
606 std::to_string(operation) + ", output " +
607 std::to_string(output);
608 validate(device, message, model, [operation, output](Model* model) {
609 hidl_vec_removeAt(&model->operations[operation].outputs, output);
610 });
611 }
612 }
613}
614
615///////////////////////// MODEL VALIDATION /////////////////////////
616
617// TODO: remove model input
618// TODO: remove model output
619// TODO: add unused operation
620
621///////////////////////// ADD OPERATION INPUT /////////////////////////
622
Xusong Wang1a2492f2018-10-05 11:49:13 -0700623static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700624 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700625 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
626 // parameter.
627 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
628 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
629 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700630 return true;
631 }
632 return false;
633}
634
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100635static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
636 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700637 if (addOperationInputSkip(model.operations[operation])) {
638 continue;
639 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100640 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
641 validate(device, message, model, [operation](Model* model) {
642 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
643 hidl_vec_push_back(&model->operations[operation].inputs, index);
644 hidl_vec_push_back(&model->inputIndexes, index);
645 });
646 }
647}
648
649///////////////////////// ADD OPERATION OUTPUT /////////////////////////
650
651static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
652 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
653 const std::string message =
654 "addOperationOutputTest: operation " + std::to_string(operation);
655 validate(device, message, model, [operation](Model* model) {
656 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
657 hidl_vec_push_back(&model->operations[operation].outputs, index);
658 hidl_vec_push_back(&model->outputIndexes, index);
659 });
660 }
661}
662
663///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
664
665static const int32_t invalidExecutionPreferences[] = {
666 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
667 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
668};
669
670static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
671 for (int32_t preference : invalidExecutionPreferences) {
672 const std::string message =
673 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
674 validate(device, message, model, [](Model*) {},
675 static_cast<ExecutionPreference>(preference));
676 }
677}
678
679////////////////////////// ENTRY POINT //////////////////////////////
680
681void ValidationTest::validateModel(const Model& model) {
682 mutateOperandTypeTest(device, model);
683 mutateOperandRankTest(device, model);
684 mutateOperandScaleTest(device, model);
685 mutateOperandZeroPointTest(device, model);
686 mutateOperationOperandTypeTest(device, model);
687 mutateOperationTypeTest(device, model);
688 mutateOperationInputOperandIndexTest(device, model);
689 mutateOperationOutputOperandIndexTest(device, model);
690 removeOperandTest(device, model);
691 removeOperationTest(device, model);
692 removeOperationInputTest(device, model);
693 removeOperationOutputTest(device, model);
694 addOperationInputTest(device, model);
695 addOperationOutputTest(device, model);
696 mutateExecutionPreferenceTest(device, model);
697}
698
699} // namespace functional
700} // namespace vts
701} // namespace V1_2
702} // namespace neuralnetworks
703} // namespace hardware
704} // namespace android