blob: 065546d927b784433d2266344d58e9da29fd8d52 [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;
Lev Proleevdce38f12019-01-30 17:14:40 +0000160 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100161 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100162 case OperandType::TENSOR_FLOAT32:
163 case OperandType::TENSOR_INT32:
164 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800165 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800166 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000167 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000168 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100169 return 0;
170 default:
171 return 0;
172 }
173}
174
175static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
176 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
177 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wangd22c5232018-11-19 18:26:08 -0800178 if (invalidRank == 0) {
179 continue;
180 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100181 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
182 " has rank of " + std::to_string(invalidRank);
183 validate(device, message, model, [operand, invalidRank](Model* model) {
184 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
185 });
186 }
187}
188
189///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
190
191static float getInvalidScale(OperandType type) {
192 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800193 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100194 case OperandType::FLOAT32:
195 case OperandType::INT32:
196 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100197 case OperandType::BOOL:
Lev Proleevdce38f12019-01-30 17:14:40 +0000198 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100199 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100200 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000201 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100202 return 1.0f;
203 case OperandType::TENSOR_INT32:
204 return -1.0f;
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800205 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100206 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800207 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000208 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100209 return 0.0f;
210 default:
211 return 0.0f;
212 }
213}
214
215static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
216 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
217 const float invalidScale = getInvalidScale(model.operands[operand].type);
218 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
219 " has scale of " + std::to_string(invalidScale);
220 validate(device, message, model, [operand, invalidScale](Model* model) {
221 model->operands[operand].scale = invalidScale;
222 });
223 }
224}
225
226///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
227
228static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
229 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800230 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100231 case OperandType::FLOAT32:
232 case OperandType::INT32:
233 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100234 case OperandType::BOOL:
Lev Proleevdce38f12019-01-30 17:14:40 +0000235 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100236 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100237 case OperandType::TENSOR_FLOAT32:
238 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000239 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100240 return {1};
241 case OperandType::TENSOR_QUANT8_ASYMM:
242 return {-1, 256};
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800243 case OperandType::TENSOR_QUANT8_SYMM:
244 return {-129, -1, 1, 128};
Xusong Wangcf6a9112019-01-16 18:32:24 -0800245 case OperandType::TENSOR_QUANT16_ASYMM:
246 return {-1, 65536};
Lev Proleev217c4072018-11-13 15:42:36 +0000247 case OperandType::TENSOR_QUANT16_SYMM:
248 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100249 default:
250 return {};
251 }
252}
253
254static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
255 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
256 const std::vector<int32_t> invalidZeroPoints =
257 getInvalidZeroPoints(model.operands[operand].type);
258 for (int32_t invalidZeroPoint : invalidZeroPoints) {
259 const std::string message = "mutateOperandZeroPointTest: operand " +
260 std::to_string(operand) + " has zero point of " +
261 std::to_string(invalidZeroPoint);
262 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
263 model->operands[operand].zeroPoint = invalidZeroPoint;
264 });
265 }
266 }
267}
268
269///////////////////////// VALIDATE EXTRA ??? /////////////////////////
270
271// TODO: Operand::lifetime
272// TODO: Operand::location
273
274///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
275
276static void mutateOperand(Operand* operand, OperandType type) {
277 Operand newOperand = *operand;
278 newOperand.type = type;
279 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800280 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100281 case OperandType::FLOAT32:
282 case OperandType::INT32:
283 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100284 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100285 newOperand.dimensions = hidl_vec<uint32_t>();
286 newOperand.scale = 0.0f;
287 newOperand.zeroPoint = 0;
288 break;
Lev Proleevdce38f12019-01-30 17:14:40 +0000289 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100290 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100291 case OperandType::TENSOR_FLOAT32:
292 newOperand.dimensions =
293 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
294 newOperand.scale = 0.0f;
295 newOperand.zeroPoint = 0;
296 break;
297 case OperandType::TENSOR_INT32:
298 newOperand.dimensions =
299 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
300 newOperand.zeroPoint = 0;
301 break;
302 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800303 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800304 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000305 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100306 newOperand.dimensions =
307 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
308 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
309 break;
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000310 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
311 newOperand.dimensions =
312 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
313 newOperand.scale = 0.0f;
314 newOperand.zeroPoint = 0;
315
316 SymmPerChannelQuantParams channelQuant;
317 channelQuant.channelDim = 0;
318 channelQuant.scales = hidl_vec<float>(
319 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0]) : 0);
320 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
321 channelQuant.scales[i] = 1.0f;
322 }
323 newOperand.extraParams.channelQuant(std::move(channelQuant));
324 } break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100325 case OperandType::OEM:
326 case OperandType::TENSOR_OEM_BYTE:
327 default:
328 break;
329 }
330 *operand = newOperand;
331}
332
Xusong Wang1a2492f2018-10-05 11:49:13 -0700333static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
334 // Do not test OEM types
335 if (type == model.operands[operand].type || type == OperandType::OEM ||
336 type == OperandType::TENSOR_OEM_BYTE) {
337 return true;
338 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100339 for (const Operation& operation : model.operations) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700340 // Skip mutateOperationOperandTypeTest for the following operations.
341 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sanders2a010122018-11-28 10:35:08 +0000342 // - ARGMIN and ARGMAX's first argument can be any of
343 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
344 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders41e67322018-12-06 12:34:07 +0000345 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleevdce38f12019-01-30 17:14:40 +0000346 // - DEQUANTIZE input can be any of
347 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
348 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
349 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000350 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000351 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000352 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev2d982642019-01-15 17:49:24 +0000353 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang1a2492f2018-10-05 11:49:13 -0700354 switch (operation.type) {
355 case OperationType::LSH_PROJECTION: {
356 if (operand == operation.inputs[1]) {
357 return true;
358 }
359 } break;
360 case OperationType::CAST:
361 case OperationType::ARGMAX:
362 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000363 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
364 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700365 return true;
366 }
367 } break;
Lev Proleevdce38f12019-01-30 17:14:40 +0000368 case OperationType::QUANTIZE:
Michael K. Sanders41e67322018-12-06 12:34:07 +0000369 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleevdce38f12019-01-30 17:14:40 +0000370 if (operand == operation.inputs[0] &&
371 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
372 return true;
373 }
374 } break;
375 case OperationType::DEQUANTIZE: {
376 if (operand == operation.inputs[0] &&
377 (type == OperandType::TENSOR_QUANT8_ASYMM ||
378 type == OperandType::TENSOR_QUANT8_SYMM ||
379 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
380 return true;
381 }
382 if (operand == operation.outputs[0] &&
383 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders41e67322018-12-06 12:34:07 +0000384 return true;
385 }
386 } break;
Lev Proleev2d982642019-01-15 17:49:24 +0000387 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000388 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000389 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000390 case OperationType::CONV_2D: {
Xusong Wangf80e3e72019-03-13 16:24:34 -0700391 if (operand == operation.inputs[1] &&
392 (type == OperandType::TENSOR_QUANT8_ASYMM ||
393 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000394 return true;
395 }
396 } break;
Xusong Wang1a2492f2018-10-05 11:49:13 -0700397 default:
398 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100399 }
400 }
401 return false;
402}
403
404static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
405 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100406 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700407 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100408 continue;
409 }
410 const std::string message = "mutateOperationOperandTypeTest: operand " +
411 std::to_string(operand) + " set to type " +
412 toString(invalidOperandType);
413 validate(device, message, model, [operand, invalidOperandType](Model* model) {
414 mutateOperand(&model->operands[operand], invalidOperandType);
415 });
416 }
417 }
418}
419
420///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
421
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000422static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000423 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
424 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
425 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100426};
427
428static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
429 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000430 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100431 const std::string message = "mutateOperationTypeTest: operation " +
432 std::to_string(operation) + " set to value " +
433 std::to_string(invalidOperationType);
434 validate(device, message, model, [operation, invalidOperationType](Model* model) {
435 model->operations[operation].type =
436 static_cast<OperationType>(invalidOperationType);
437 });
438 }
439 }
440}
441
442///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
443
444static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
445 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
446 const uint32_t invalidOperand = model.operands.size();
447 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
448 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
449 std::to_string(operation) + " input " +
450 std::to_string(input);
451 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
452 model->operations[operation].inputs[input] = invalidOperand;
453 });
454 }
455 }
456}
457
458///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
459
460static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
461 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
462 const uint32_t invalidOperand = model.operands.size();
463 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
464 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
465 std::to_string(operation) + " output " +
466 std::to_string(output);
467 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
468 model->operations[operation].outputs[output] = invalidOperand;
469 });
470 }
471 }
472}
473
474///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
475
476static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
477 if (vec) {
478 // remove elements matching "value"
479 auto last = std::remove(vec->begin(), vec->end(), value);
480 vec->resize(std::distance(vec->begin(), last));
481
482 // decrement elements exceeding "value"
483 std::transform(vec->begin(), vec->end(), vec->begin(),
484 [value](uint32_t v) { return v > value ? v-- : v; });
485 }
486}
487
488static void removeOperand(Model* model, uint32_t index) {
489 hidl_vec_removeAt(&model->operands, index);
490 for (Operation& operation : model->operations) {
491 removeValueAndDecrementGreaterValues(&operation.inputs, index);
492 removeValueAndDecrementGreaterValues(&operation.outputs, index);
493 }
494 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
495 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
496}
497
Xusong Wang1a2492f2018-10-05 11:49:13 -0700498static bool removeOperandSkip(size_t operand, const Model& model) {
499 for (const Operation& operation : model.operations) {
500 // Skip removeOperandTest for the following operations.
501 // - SPLIT's outputs are not checked during prepareModel.
502 if (operation.type == OperationType::SPLIT) {
503 for (const size_t outOprand : operation.outputs) {
504 if (operand == outOprand) {
505 return true;
506 }
507 }
508 }
Lev Proleevdce38f12019-01-30 17:14:40 +0000509 // BIDIRECTIONAL_SEQUENCE_RNN can have either on or two outputs
510 // depending on a mergeOutputs parameter
511 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
512 for (const size_t outOprand : operation.outputs) {
513 if (operand == outOprand) {
514 return true;
515 }
516 }
517 }
Xusong Wang1a2492f2018-10-05 11:49:13 -0700518 }
519 return false;
520}
521
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100522static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
523 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700524 if (removeOperandSkip(operand, model)) {
525 continue;
526 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100527 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
528 validate(device, message, model,
529 [operand](Model* model) { removeOperand(model, operand); });
530 }
531}
532
533///////////////////////// REMOVE OPERATION /////////////////////////
534
535static void removeOperation(Model* model, uint32_t index) {
536 for (uint32_t operand : model->operations[index].inputs) {
537 model->operands[operand].numberOfConsumers--;
538 }
539 hidl_vec_removeAt(&model->operations, index);
540}
541
542static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
543 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
544 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
545 validate(device, message, model,
546 [operation](Model* model) { removeOperation(model, operation); });
547 }
548}
549
550///////////////////////// REMOVE OPERATION INPUT /////////////////////////
551
Xusong Wang1a2492f2018-10-05 11:49:13 -0700552static bool removeOperationInputSkip(const Operation& op, size_t input) {
553 // Skip removeOperationInputTest for the following operations.
554 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
555 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
556 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
557 // layout parameter.
558 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
559 // parameter.
560 switch (op.type) {
561 case OperationType::CONCATENATION: {
562 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
563 return true;
564 }
565 } break;
566 case OperationType::DEPTHWISE_CONV_2D: {
567 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
568 return true;
569 }
570 } break;
571 case OperationType::CONV_2D:
572 case OperationType::AVERAGE_POOL_2D:
573 case OperationType::MAX_POOL_2D:
574 case OperationType::L2_POOL_2D: {
575 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
576 return true;
577 }
578 } break;
579 case OperationType::RESIZE_BILINEAR: {
580 if (op.inputs.size() == 4 && input == 3) {
581 return true;
582 }
583 } break;
584 case OperationType::SPACE_TO_DEPTH:
585 case OperationType::DEPTH_TO_SPACE:
586 case OperationType::BATCH_TO_SPACE_ND: {
587 if (op.inputs.size() == 3 && input == 2) {
588 return true;
589 }
590 } break;
591 case OperationType::SPACE_TO_BATCH_ND: {
592 if (op.inputs.size() == 4 && input == 3) {
593 return true;
594 }
595 } break;
596 case OperationType::L2_NORMALIZATION: {
597 if (op.inputs.size() == 2 && input == 1) {
598 return true;
599 }
600 } break;
601 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
602 if (op.inputs.size() == 6 && input == 5) {
603 return true;
604 }
605 } break;
606 case OperationType::SOFTMAX: {
607 if (op.inputs.size() == 3 && input == 2) {
608 return true;
609 }
610 } break;
611 default:
612 break;
613 }
614 return false;
615}
616
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100617static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
618 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
619 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
620 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700621 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100622 continue;
623 }
624 const std::string message = "removeOperationInputTest: operation " +
625 std::to_string(operation) + ", input " +
626 std::to_string(input);
627 validate(device, message, model, [operation, input](Model* model) {
628 uint32_t operand = model->operations[operation].inputs[input];
629 model->operands[operand].numberOfConsumers--;
630 hidl_vec_removeAt(&model->operations[operation].inputs, input);
631 });
632 }
633 }
634}
635
636///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
637
638static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
639 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
640 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
641 const std::string message = "removeOperationOutputTest: operation " +
642 std::to_string(operation) + ", output " +
643 std::to_string(output);
644 validate(device, message, model, [operation, output](Model* model) {
645 hidl_vec_removeAt(&model->operations[operation].outputs, output);
646 });
647 }
648 }
649}
650
651///////////////////////// MODEL VALIDATION /////////////////////////
652
653// TODO: remove model input
654// TODO: remove model output
655// TODO: add unused operation
656
657///////////////////////// ADD OPERATION INPUT /////////////////////////
658
Xusong Wang1a2492f2018-10-05 11:49:13 -0700659static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700660 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700661 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
662 // parameter.
663 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
664 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
665 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700666 return true;
667 }
668 return false;
669}
670
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100671static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
672 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700673 if (addOperationInputSkip(model.operations[operation])) {
674 continue;
675 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100676 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
677 validate(device, message, model, [operation](Model* model) {
678 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
679 hidl_vec_push_back(&model->operations[operation].inputs, index);
680 hidl_vec_push_back(&model->inputIndexes, index);
681 });
682 }
683}
684
685///////////////////////// ADD OPERATION OUTPUT /////////////////////////
686
687static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
688 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
689 const std::string message =
690 "addOperationOutputTest: operation " + std::to_string(operation);
691 validate(device, message, model, [operation](Model* model) {
692 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
693 hidl_vec_push_back(&model->operations[operation].outputs, index);
694 hidl_vec_push_back(&model->outputIndexes, index);
695 });
696 }
697}
698
699///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
700
701static const int32_t invalidExecutionPreferences[] = {
702 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
703 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
704};
705
706static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
707 for (int32_t preference : invalidExecutionPreferences) {
708 const std::string message =
709 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
710 validate(device, message, model, [](Model*) {},
711 static_cast<ExecutionPreference>(preference));
712 }
713}
714
715////////////////////////// ENTRY POINT //////////////////////////////
716
717void ValidationTest::validateModel(const Model& model) {
718 mutateOperandTypeTest(device, model);
719 mutateOperandRankTest(device, model);
720 mutateOperandScaleTest(device, model);
721 mutateOperandZeroPointTest(device, model);
722 mutateOperationOperandTypeTest(device, model);
723 mutateOperationTypeTest(device, model);
724 mutateOperationInputOperandIndexTest(device, model);
725 mutateOperationOutputOperandIndexTest(device, model);
726 removeOperandTest(device, model);
727 removeOperationTest(device, model);
728 removeOperationInputTest(device, model);
729 removeOperationOutputTest(device, model);
730 addOperationInputTest(device, model);
731 addOperationOutputTest(device, model);
732 mutateExecutionPreferenceTest(device, model);
733}
734
735} // namespace functional
736} // namespace vts
737} // namespace V1_2
738} // namespace neuralnetworks
739} // namespace hardware
740} // namespace android