blob: afa8e5f0dcb8a66b42cf1c0ba26f5d96efd2967f [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:
Lev Proleev217c4072018-11-13 15:42:36 +0000164 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000165 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100166 return 0;
167 default:
168 return 0;
169 }
170}
171
172static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
173 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
174 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wangd22c5232018-11-19 18:26:08 -0800175 if (invalidRank == 0) {
176 continue;
177 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100178 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
179 " has rank of " + std::to_string(invalidRank);
180 validate(device, message, model, [operand, invalidRank](Model* model) {
181 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
182 });
183 }
184}
185
186///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
187
188static float getInvalidScale(OperandType type) {
189 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800190 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100191 case OperandType::FLOAT32:
192 case OperandType::INT32:
193 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100194 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100195 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100196 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000197 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100198 return 1.0f;
199 case OperandType::TENSOR_INT32:
200 return -1.0f;
201 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000202 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100203 return 0.0f;
204 default:
205 return 0.0f;
206 }
207}
208
209static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
210 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
211 const float invalidScale = getInvalidScale(model.operands[operand].type);
212 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
213 " has scale of " + std::to_string(invalidScale);
214 validate(device, message, model, [operand, invalidScale](Model* model) {
215 model->operands[operand].scale = invalidScale;
216 });
217 }
218}
219
220///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
221
222static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
223 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800224 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100225 case OperandType::FLOAT32:
226 case OperandType::INT32:
227 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100228 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100229 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100230 case OperandType::TENSOR_FLOAT32:
231 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000232 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100233 return {1};
234 case OperandType::TENSOR_QUANT8_ASYMM:
235 return {-1, 256};
Lev Proleev217c4072018-11-13 15:42:36 +0000236 case OperandType::TENSOR_QUANT16_SYMM:
237 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100238 default:
239 return {};
240 }
241}
242
243static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
244 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
245 const std::vector<int32_t> invalidZeroPoints =
246 getInvalidZeroPoints(model.operands[operand].type);
247 for (int32_t invalidZeroPoint : invalidZeroPoints) {
248 const std::string message = "mutateOperandZeroPointTest: operand " +
249 std::to_string(operand) + " has zero point of " +
250 std::to_string(invalidZeroPoint);
251 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
252 model->operands[operand].zeroPoint = invalidZeroPoint;
253 });
254 }
255 }
256}
257
258///////////////////////// VALIDATE EXTRA ??? /////////////////////////
259
260// TODO: Operand::lifetime
261// TODO: Operand::location
262
263///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
264
265static void mutateOperand(Operand* operand, OperandType type) {
266 Operand newOperand = *operand;
267 newOperand.type = type;
268 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800269 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100270 case OperandType::FLOAT32:
271 case OperandType::INT32:
272 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100273 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100274 newOperand.dimensions = hidl_vec<uint32_t>();
275 newOperand.scale = 0.0f;
276 newOperand.zeroPoint = 0;
277 break;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100278 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100279 case OperandType::TENSOR_FLOAT32:
280 newOperand.dimensions =
281 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
282 newOperand.scale = 0.0f;
283 newOperand.zeroPoint = 0;
284 break;
285 case OperandType::TENSOR_INT32:
286 newOperand.dimensions =
287 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
288 newOperand.zeroPoint = 0;
289 break;
290 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000291 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100292 newOperand.dimensions =
293 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
294 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
295 break;
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000296 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
297 newOperand.dimensions =
298 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
299 newOperand.scale = 0.0f;
300 newOperand.zeroPoint = 0;
301
302 SymmPerChannelQuantParams channelQuant;
303 channelQuant.channelDim = 0;
304 channelQuant.scales = hidl_vec<float>(
305 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0]) : 0);
306 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
307 channelQuant.scales[i] = 1.0f;
308 }
309 newOperand.extraParams.channelQuant(std::move(channelQuant));
310 } break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100311 case OperandType::OEM:
312 case OperandType::TENSOR_OEM_BYTE:
313 default:
314 break;
315 }
316 *operand = newOperand;
317}
318
Xusong Wang1a2492f2018-10-05 11:49:13 -0700319static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
320 // Do not test OEM types
321 if (type == model.operands[operand].type || type == OperandType::OEM ||
322 type == OperandType::TENSOR_OEM_BYTE) {
323 return true;
324 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100325 for (const Operation& operation : model.operations) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700326 // Skip mutateOperationOperandTypeTest for the following operations.
327 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sanders2a010122018-11-28 10:35:08 +0000328 // - ARGMIN and ARGMAX's first argument can be any of
329 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
330 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders41e67322018-12-06 12:34:07 +0000331 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000332 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000333 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000334 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev2d982642019-01-15 17:49:24 +0000335 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang1a2492f2018-10-05 11:49:13 -0700336 switch (operation.type) {
337 case OperationType::LSH_PROJECTION: {
338 if (operand == operation.inputs[1]) {
339 return true;
340 }
341 } break;
342 case OperationType::CAST:
343 case OperationType::ARGMAX:
344 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000345 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
346 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700347 return true;
348 }
349 } break;
Michael K. Sanders41e67322018-12-06 12:34:07 +0000350 case OperationType::RANDOM_MULTINOMIAL: {
351 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
352 return true;
353 }
354 } break;
Lev Proleev2d982642019-01-15 17:49:24 +0000355 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000356 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000357 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000358 case OperationType::CONV_2D: {
359 if (operand == 1 && (type == OperandType::TENSOR_QUANT8_ASYMM ||
360 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
361 return true;
362 }
363 } break;
Xusong Wang1a2492f2018-10-05 11:49:13 -0700364 default:
365 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100366 }
367 }
368 return false;
369}
370
371static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
372 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100373 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700374 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100375 continue;
376 }
377 const std::string message = "mutateOperationOperandTypeTest: operand " +
378 std::to_string(operand) + " set to type " +
379 toString(invalidOperandType);
380 validate(device, message, model, [operand, invalidOperandType](Model* model) {
381 mutateOperand(&model->operands[operand], invalidOperandType);
382 });
383 }
384 }
385}
386
387///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
388
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000389static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000390 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MIN) - 1,
391 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
392 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
393 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100394};
395
396static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
397 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000398 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100399 const std::string message = "mutateOperationTypeTest: operation " +
400 std::to_string(operation) + " set to value " +
401 std::to_string(invalidOperationType);
402 validate(device, message, model, [operation, invalidOperationType](Model* model) {
403 model->operations[operation].type =
404 static_cast<OperationType>(invalidOperationType);
405 });
406 }
407 }
408}
409
410///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
411
412static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
413 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
414 const uint32_t invalidOperand = model.operands.size();
415 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
416 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
417 std::to_string(operation) + " input " +
418 std::to_string(input);
419 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
420 model->operations[operation].inputs[input] = invalidOperand;
421 });
422 }
423 }
424}
425
426///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
427
428static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
429 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
430 const uint32_t invalidOperand = model.operands.size();
431 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
432 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
433 std::to_string(operation) + " output " +
434 std::to_string(output);
435 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
436 model->operations[operation].outputs[output] = invalidOperand;
437 });
438 }
439 }
440}
441
442///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
443
444static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
445 if (vec) {
446 // remove elements matching "value"
447 auto last = std::remove(vec->begin(), vec->end(), value);
448 vec->resize(std::distance(vec->begin(), last));
449
450 // decrement elements exceeding "value"
451 std::transform(vec->begin(), vec->end(), vec->begin(),
452 [value](uint32_t v) { return v > value ? v-- : v; });
453 }
454}
455
456static void removeOperand(Model* model, uint32_t index) {
457 hidl_vec_removeAt(&model->operands, index);
458 for (Operation& operation : model->operations) {
459 removeValueAndDecrementGreaterValues(&operation.inputs, index);
460 removeValueAndDecrementGreaterValues(&operation.outputs, index);
461 }
462 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
463 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
464}
465
Xusong Wang1a2492f2018-10-05 11:49:13 -0700466static bool removeOperandSkip(size_t operand, const Model& model) {
467 for (const Operation& operation : model.operations) {
468 // Skip removeOperandTest for the following operations.
469 // - SPLIT's outputs are not checked during prepareModel.
470 if (operation.type == OperationType::SPLIT) {
471 for (const size_t outOprand : operation.outputs) {
472 if (operand == outOprand) {
473 return true;
474 }
475 }
476 }
477 }
478 return false;
479}
480
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100481static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
482 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700483 if (removeOperandSkip(operand, model)) {
484 continue;
485 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100486 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
487 validate(device, message, model,
488 [operand](Model* model) { removeOperand(model, operand); });
489 }
490}
491
492///////////////////////// REMOVE OPERATION /////////////////////////
493
494static void removeOperation(Model* model, uint32_t index) {
495 for (uint32_t operand : model->operations[index].inputs) {
496 model->operands[operand].numberOfConsumers--;
497 }
498 hidl_vec_removeAt(&model->operations, index);
499}
500
501static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
502 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
503 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
504 validate(device, message, model,
505 [operation](Model* model) { removeOperation(model, operation); });
506 }
507}
508
509///////////////////////// REMOVE OPERATION INPUT /////////////////////////
510
Xusong Wang1a2492f2018-10-05 11:49:13 -0700511static bool removeOperationInputSkip(const Operation& op, size_t input) {
512 // Skip removeOperationInputTest for the following operations.
513 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
514 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
515 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
516 // layout parameter.
517 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
518 // parameter.
519 switch (op.type) {
520 case OperationType::CONCATENATION: {
521 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
522 return true;
523 }
524 } break;
525 case OperationType::DEPTHWISE_CONV_2D: {
526 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
527 return true;
528 }
529 } break;
530 case OperationType::CONV_2D:
531 case OperationType::AVERAGE_POOL_2D:
532 case OperationType::MAX_POOL_2D:
533 case OperationType::L2_POOL_2D: {
534 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
535 return true;
536 }
537 } break;
538 case OperationType::RESIZE_BILINEAR: {
539 if (op.inputs.size() == 4 && input == 3) {
540 return true;
541 }
542 } break;
543 case OperationType::SPACE_TO_DEPTH:
544 case OperationType::DEPTH_TO_SPACE:
545 case OperationType::BATCH_TO_SPACE_ND: {
546 if (op.inputs.size() == 3 && input == 2) {
547 return true;
548 }
549 } break;
550 case OperationType::SPACE_TO_BATCH_ND: {
551 if (op.inputs.size() == 4 && input == 3) {
552 return true;
553 }
554 } break;
555 case OperationType::L2_NORMALIZATION: {
556 if (op.inputs.size() == 2 && input == 1) {
557 return true;
558 }
559 } break;
560 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
561 if (op.inputs.size() == 6 && input == 5) {
562 return true;
563 }
564 } break;
565 case OperationType::SOFTMAX: {
566 if (op.inputs.size() == 3 && input == 2) {
567 return true;
568 }
569 } break;
570 default:
571 break;
572 }
573 return false;
574}
575
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100576static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
577 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
578 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
579 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700580 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100581 continue;
582 }
583 const std::string message = "removeOperationInputTest: operation " +
584 std::to_string(operation) + ", input " +
585 std::to_string(input);
586 validate(device, message, model, [operation, input](Model* model) {
587 uint32_t operand = model->operations[operation].inputs[input];
588 model->operands[operand].numberOfConsumers--;
589 hidl_vec_removeAt(&model->operations[operation].inputs, input);
590 });
591 }
592 }
593}
594
595///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
596
597static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
598 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
599 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
600 const std::string message = "removeOperationOutputTest: operation " +
601 std::to_string(operation) + ", output " +
602 std::to_string(output);
603 validate(device, message, model, [operation, output](Model* model) {
604 hidl_vec_removeAt(&model->operations[operation].outputs, output);
605 });
606 }
607 }
608}
609
610///////////////////////// MODEL VALIDATION /////////////////////////
611
612// TODO: remove model input
613// TODO: remove model output
614// TODO: add unused operation
615
616///////////////////////// ADD OPERATION INPUT /////////////////////////
617
Xusong Wang1a2492f2018-10-05 11:49:13 -0700618static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700619 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700620 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
621 // parameter.
622 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
623 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
624 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700625 return true;
626 }
627 return false;
628}
629
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100630static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
631 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700632 if (addOperationInputSkip(model.operations[operation])) {
633 continue;
634 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100635 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
636 validate(device, message, model, [operation](Model* model) {
637 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
638 hidl_vec_push_back(&model->operations[operation].inputs, index);
639 hidl_vec_push_back(&model->inputIndexes, index);
640 });
641 }
642}
643
644///////////////////////// ADD OPERATION OUTPUT /////////////////////////
645
646static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
647 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
648 const std::string message =
649 "addOperationOutputTest: operation " + std::to_string(operation);
650 validate(device, message, model, [operation](Model* model) {
651 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
652 hidl_vec_push_back(&model->operations[operation].outputs, index);
653 hidl_vec_push_back(&model->outputIndexes, index);
654 });
655 }
656}
657
658///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
659
660static const int32_t invalidExecutionPreferences[] = {
661 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
662 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
663};
664
665static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
666 for (int32_t preference : invalidExecutionPreferences) {
667 const std::string message =
668 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
669 validate(device, message, model, [](Model*) {},
670 static_cast<ExecutionPreference>(preference));
671 }
672}
673
674////////////////////////// ENTRY POINT //////////////////////////////
675
676void ValidationTest::validateModel(const Model& model) {
677 mutateOperandTypeTest(device, model);
678 mutateOperandRankTest(device, model);
679 mutateOperandScaleTest(device, model);
680 mutateOperandZeroPointTest(device, model);
681 mutateOperationOperandTypeTest(device, model);
682 mutateOperationTypeTest(device, model);
683 mutateOperationInputOperandIndexTest(device, model);
684 mutateOperationOutputOperandIndexTest(device, model);
685 removeOperandTest(device, model);
686 removeOperationTest(device, model);
687 removeOperationInputTest(device, model);
688 removeOperationOutputTest(device, model);
689 addOperationInputTest(device, model);
690 addOperationOutputTest(device, model);
691 mutateExecutionPreferenceTest(device, model);
692}
693
694} // namespace functional
695} // namespace vts
696} // namespace V1_2
697} // namespace neuralnetworks
698} // namespace hardware
699} // namespace android