blob: 2988211e5a4d246b0e687f3c76e48a5cae4e98a3 [file] [log] [blame]
Slava Shklyaev871be942018-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 Shklyaev871be942018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
30
31namespace vts {
32namespace functional {
33
Xusong Wangb5cb8f72018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
35using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Xusong Wangb61ba1e2019-02-25 16:58:58 -080036using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Slava Shklyaev871be942018-09-12 14:52:02 +010037
38///////////////////////// UTILITY FUNCTIONS /////////////////////////
39
40static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
41 const Model& model) {
42 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
43
44 Return<void> ret =
45 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
46 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
47 });
48 EXPECT_TRUE(ret.isOk());
49}
50
51static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
52 const Model& model, ExecutionPreference preference) {
53 SCOPED_TRACE(message + " [prepareModel_1_2]");
54
55 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
56 ASSERT_NE(nullptr, preparedModelCallback.get());
57 Return<ErrorStatus> prepareLaunchStatus =
Xusong Wangb61ba1e2019-02-25 16:58:58 -080058 device->prepareModel_1_2(model, preference, hidl_vec<hidl_handle>(),
59 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010060 ASSERT_TRUE(prepareLaunchStatus.isOk());
61 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
62
63 preparedModelCallback->wait();
64 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
65 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070066 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010067 ASSERT_EQ(nullptr, preparedModel.get());
68}
69
70static bool validExecutionPreference(ExecutionPreference preference) {
71 return preference == ExecutionPreference::LOW_POWER ||
72 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
73 preference == ExecutionPreference::SUSTAINED_SPEED;
74}
75
76// Primary validation function. This function will take a valid model, apply a
77// mutation to it to invalidate the model, then pass it to interface calls that
78// use the model. Note that the model here is passed by value, and any mutation
79// to the model does not leave this function.
80static void validate(const sp<IDevice>& device, const std::string& message, Model model,
81 const std::function<void(Model*)>& mutation,
82 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
83 mutation(&model);
84 if (validExecutionPreference(preference)) {
85 validateGetSupportedOperations(device, message, model);
86 }
87 validatePrepareModel(device, message, model, preference);
88}
89
90// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
91// so this is efficiently accomplished by moving the element to the end and
92// resizing the hidl_vec to one less.
93template <typename Type>
94static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
95 if (vec) {
96 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
97 vec->resize(vec->size() - 1);
98 }
99}
100
101template <typename Type>
102static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
103 // assume vec is valid
104 const uint32_t index = vec->size();
105 vec->resize(index + 1);
106 (*vec)[index] = value;
107 return index;
108}
109
110static uint32_t addOperand(Model* model) {
111 return hidl_vec_push_back(&model->operands,
112 {
113 .type = OperandType::INT32,
114 .dimensions = {},
115 .numberOfConsumers = 0,
116 .scale = 0.0f,
117 .zeroPoint = 0,
118 .lifetime = OperandLifeTime::MODEL_INPUT,
119 .location = {.poolIndex = 0, .offset = 0, .length = 0},
120 });
121}
122
123static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
124 uint32_t index = addOperand(model);
125 model->operands[index].numberOfConsumers = 1;
126 model->operands[index].lifetime = lifetime;
127 return index;
128}
129
130///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
131
Michael K. Sandersc785d462018-10-30 15:16:54 +0000132static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000133 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
134 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
135 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
136 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100137};
138
139static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
140 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000141 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100142 const std::string message = "mutateOperandTypeTest: operand " +
143 std::to_string(operand) + " set to value " +
144 std::to_string(invalidOperandType);
145 validate(device, message, model, [operand, invalidOperandType](Model* model) {
146 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
147 });
148 }
149 }
150}
151
152///////////////////////// VALIDATE OPERAND RANK /////////////////////////
153
154static uint32_t getInvalidRank(OperandType type) {
155 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800156 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100157 case OperandType::FLOAT32:
158 case OperandType::INT32:
159 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100160 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100161 return 1;
Lev Proleev923b8c52019-01-30 17:14:40 +0000162 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100163 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100164 case OperandType::TENSOR_FLOAT32:
165 case OperandType::TENSOR_INT32:
166 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800167 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800168 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000169 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000170 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100171 return 0;
172 default:
173 return 0;
174 }
175}
176
177static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
178 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
179 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wanga3165812018-11-19 18:26:08 -0800180 if (invalidRank == 0) {
181 continue;
182 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100183 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
184 " has rank of " + std::to_string(invalidRank);
185 validate(device, message, model, [operand, invalidRank](Model* model) {
186 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
187 });
188 }
189}
190
191///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
192
193static float getInvalidScale(OperandType type) {
194 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800195 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100196 case OperandType::FLOAT32:
197 case OperandType::INT32:
198 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100199 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000200 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100201 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100202 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000203 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100204 return 1.0f;
205 case OperandType::TENSOR_INT32:
206 return -1.0f;
Hervé Guihotbae91692019-01-23 19:18:59 -0800207 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100208 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800209 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000210 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100211 return 0.0f;
212 default:
213 return 0.0f;
214 }
215}
216
217static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
218 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
219 const float invalidScale = getInvalidScale(model.operands[operand].type);
220 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
221 " has scale of " + std::to_string(invalidScale);
222 validate(device, message, model, [operand, invalidScale](Model* model) {
223 model->operands[operand].scale = invalidScale;
224 });
225 }
226}
227
228///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
229
230static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
231 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800232 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100233 case OperandType::FLOAT32:
234 case OperandType::INT32:
235 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100236 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000237 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100238 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100239 case OperandType::TENSOR_FLOAT32:
240 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000241 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100242 return {1};
243 case OperandType::TENSOR_QUANT8_ASYMM:
244 return {-1, 256};
Hervé Guihotbae91692019-01-23 19:18:59 -0800245 case OperandType::TENSOR_QUANT8_SYMM:
246 return {-129, -1, 1, 128};
Xusong Wangd49f6652019-01-16 18:32:24 -0800247 case OperandType::TENSOR_QUANT16_ASYMM:
248 return {-1, 65536};
Lev Proleev48c88202018-11-13 15:42:36 +0000249 case OperandType::TENSOR_QUANT16_SYMM:
250 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100251 default:
252 return {};
253 }
254}
255
256static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
257 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
258 const std::vector<int32_t> invalidZeroPoints =
259 getInvalidZeroPoints(model.operands[operand].type);
260 for (int32_t invalidZeroPoint : invalidZeroPoints) {
261 const std::string message = "mutateOperandZeroPointTest: operand " +
262 std::to_string(operand) + " has zero point of " +
263 std::to_string(invalidZeroPoint);
264 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
265 model->operands[operand].zeroPoint = invalidZeroPoint;
266 });
267 }
268 }
269}
270
271///////////////////////// VALIDATE EXTRA ??? /////////////////////////
272
273// TODO: Operand::lifetime
274// TODO: Operand::location
275
276///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
277
278static void mutateOperand(Operand* operand, OperandType type) {
279 Operand newOperand = *operand;
280 newOperand.type = type;
281 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800282 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100283 case OperandType::FLOAT32:
284 case OperandType::INT32:
285 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100286 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100287 newOperand.dimensions = hidl_vec<uint32_t>();
288 newOperand.scale = 0.0f;
289 newOperand.zeroPoint = 0;
290 break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000291 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100292 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100293 case OperandType::TENSOR_FLOAT32:
294 newOperand.dimensions =
295 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
296 newOperand.scale = 0.0f;
297 newOperand.zeroPoint = 0;
298 break;
299 case OperandType::TENSOR_INT32:
300 newOperand.dimensions =
301 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
302 newOperand.zeroPoint = 0;
303 break;
304 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800305 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800306 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000307 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100308 newOperand.dimensions =
309 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
310 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
311 break;
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000312 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
313 newOperand.dimensions =
314 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
315 newOperand.scale = 0.0f;
316 newOperand.zeroPoint = 0;
317
318 SymmPerChannelQuantParams channelQuant;
319 channelQuant.channelDim = 0;
320 channelQuant.scales = hidl_vec<float>(
321 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0]) : 0);
322 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
323 channelQuant.scales[i] = 1.0f;
324 }
325 newOperand.extraParams.channelQuant(std::move(channelQuant));
326 } break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100327 case OperandType::OEM:
328 case OperandType::TENSOR_OEM_BYTE:
329 default:
330 break;
331 }
332 *operand = newOperand;
333}
334
Xusong Wang5b747ae2018-10-05 11:49:13 -0700335static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
336 // Do not test OEM types
337 if (type == model.operands[operand].type || type == OperandType::OEM ||
338 type == OperandType::TENSOR_OEM_BYTE) {
339 return true;
340 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100341 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700342 // Skip mutateOperationOperandTypeTest for the following operations.
343 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000344 // - ARGMIN and ARGMAX's first argument can be any of
345 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
346 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000347 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev923b8c52019-01-30 17:14:40 +0000348 // - DEQUANTIZE input can be any of
349 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
350 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
351 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000352 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000353 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb0762cc2019-01-15 17:53:46 +0000354 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev1509a262019-01-15 17:49:24 +0000355 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang5b747ae2018-10-05 11:49:13 -0700356 switch (operation.type) {
357 case OperationType::LSH_PROJECTION: {
358 if (operand == operation.inputs[1]) {
359 return true;
360 }
361 } break;
362 case OperationType::CAST:
363 case OperationType::ARGMAX:
364 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000365 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
366 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700367 return true;
368 }
369 } break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000370 case OperationType::QUANTIZE:
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000371 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleev923b8c52019-01-30 17:14:40 +0000372 if (operand == operation.inputs[0] &&
373 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
374 return true;
375 }
376 } break;
377 case OperationType::DEQUANTIZE: {
378 if (operand == operation.inputs[0] &&
379 (type == OperandType::TENSOR_QUANT8_ASYMM ||
380 type == OperandType::TENSOR_QUANT8_SYMM ||
381 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
382 return true;
383 }
384 if (operand == operation.outputs[0] &&
385 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000386 return true;
387 }
388 } break;
Lev Proleev1509a262019-01-15 17:49:24 +0000389 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb0762cc2019-01-15 17:53:46 +0000390 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000391 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000392 case OperationType::CONV_2D: {
Xusong Wang88044232019-03-13 16:24:34 -0700393 if (operand == operation.inputs[1] &&
394 (type == OperandType::TENSOR_QUANT8_ASYMM ||
395 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000396 return true;
397 }
398 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700399 default:
400 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100401 }
402 }
403 return false;
404}
405
406static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
407 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100408 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700409 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100410 continue;
411 }
412 const std::string message = "mutateOperationOperandTypeTest: operand " +
413 std::to_string(operand) + " set to type " +
414 toString(invalidOperandType);
415 validate(device, message, model, [operand, invalidOperandType](Model* model) {
416 mutateOperand(&model->operands[operand], invalidOperandType);
417 });
418 }
419 }
420}
421
422///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
423
Michael K. Sandersc785d462018-10-30 15:16:54 +0000424static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000425 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
426 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
427 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100428};
429
430static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
431 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000432 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100433 const std::string message = "mutateOperationTypeTest: operation " +
434 std::to_string(operation) + " set to value " +
435 std::to_string(invalidOperationType);
436 validate(device, message, model, [operation, invalidOperationType](Model* model) {
437 model->operations[operation].type =
438 static_cast<OperationType>(invalidOperationType);
439 });
440 }
441 }
442}
443
444///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
445
446static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
447 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
448 const uint32_t invalidOperand = model.operands.size();
449 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
450 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
451 std::to_string(operation) + " input " +
452 std::to_string(input);
453 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
454 model->operations[operation].inputs[input] = invalidOperand;
455 });
456 }
457 }
458}
459
460///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
461
462static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
463 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
464 const uint32_t invalidOperand = model.operands.size();
465 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
466 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
467 std::to_string(operation) + " output " +
468 std::to_string(output);
469 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
470 model->operations[operation].outputs[output] = invalidOperand;
471 });
472 }
473 }
474}
475
476///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
477
478static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
479 if (vec) {
480 // remove elements matching "value"
481 auto last = std::remove(vec->begin(), vec->end(), value);
482 vec->resize(std::distance(vec->begin(), last));
483
484 // decrement elements exceeding "value"
485 std::transform(vec->begin(), vec->end(), vec->begin(),
486 [value](uint32_t v) { return v > value ? v-- : v; });
487 }
488}
489
490static void removeOperand(Model* model, uint32_t index) {
491 hidl_vec_removeAt(&model->operands, index);
492 for (Operation& operation : model->operations) {
493 removeValueAndDecrementGreaterValues(&operation.inputs, index);
494 removeValueAndDecrementGreaterValues(&operation.outputs, index);
495 }
496 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
497 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
498}
499
Xusong Wang5b747ae2018-10-05 11:49:13 -0700500static bool removeOperandSkip(size_t operand, const Model& model) {
501 for (const Operation& operation : model.operations) {
502 // Skip removeOperandTest for the following operations.
503 // - SPLIT's outputs are not checked during prepareModel.
504 if (operation.type == OperationType::SPLIT) {
505 for (const size_t outOprand : operation.outputs) {
506 if (operand == outOprand) {
507 return true;
508 }
509 }
510 }
Lev Proleev923b8c52019-01-30 17:14:40 +0000511 // BIDIRECTIONAL_SEQUENCE_RNN can have either on or two outputs
512 // depending on a mergeOutputs parameter
513 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
514 for (const size_t outOprand : operation.outputs) {
515 if (operand == outOprand) {
516 return true;
517 }
518 }
519 }
Xusong Wang5b747ae2018-10-05 11:49:13 -0700520 }
521 return false;
522}
523
Slava Shklyaev871be942018-09-12 14:52:02 +0100524static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
525 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700526 if (removeOperandSkip(operand, model)) {
527 continue;
528 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100529 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
530 validate(device, message, model,
531 [operand](Model* model) { removeOperand(model, operand); });
532 }
533}
534
535///////////////////////// REMOVE OPERATION /////////////////////////
536
537static void removeOperation(Model* model, uint32_t index) {
538 for (uint32_t operand : model->operations[index].inputs) {
539 model->operands[operand].numberOfConsumers--;
540 }
541 hidl_vec_removeAt(&model->operations, index);
542}
543
544static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
545 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
546 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
547 validate(device, message, model,
548 [operation](Model* model) { removeOperation(model, operation); });
549 }
550}
551
552///////////////////////// REMOVE OPERATION INPUT /////////////////////////
553
Xusong Wang5b747ae2018-10-05 11:49:13 -0700554static bool removeOperationInputSkip(const Operation& op, size_t input) {
555 // Skip removeOperationInputTest for the following operations.
556 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
557 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
558 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
559 // layout parameter.
560 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
561 // parameter.
562 switch (op.type) {
563 case OperationType::CONCATENATION: {
564 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
565 return true;
566 }
567 } break;
568 case OperationType::DEPTHWISE_CONV_2D: {
569 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
570 return true;
571 }
572 } break;
573 case OperationType::CONV_2D:
574 case OperationType::AVERAGE_POOL_2D:
575 case OperationType::MAX_POOL_2D:
576 case OperationType::L2_POOL_2D: {
577 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
578 return true;
579 }
580 } break;
581 case OperationType::RESIZE_BILINEAR: {
582 if (op.inputs.size() == 4 && input == 3) {
583 return true;
584 }
585 } break;
586 case OperationType::SPACE_TO_DEPTH:
587 case OperationType::DEPTH_TO_SPACE:
588 case OperationType::BATCH_TO_SPACE_ND: {
589 if (op.inputs.size() == 3 && input == 2) {
590 return true;
591 }
592 } break;
593 case OperationType::SPACE_TO_BATCH_ND: {
594 if (op.inputs.size() == 4 && input == 3) {
595 return true;
596 }
597 } break;
598 case OperationType::L2_NORMALIZATION: {
599 if (op.inputs.size() == 2 && input == 1) {
600 return true;
601 }
602 } break;
603 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
604 if (op.inputs.size() == 6 && input == 5) {
605 return true;
606 }
607 } break;
608 case OperationType::SOFTMAX: {
609 if (op.inputs.size() == 3 && input == 2) {
610 return true;
611 }
612 } break;
613 default:
614 break;
615 }
616 return false;
617}
618
Slava Shklyaev871be942018-09-12 14:52:02 +0100619static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
620 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
621 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
622 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700623 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100624 continue;
625 }
626 const std::string message = "removeOperationInputTest: operation " +
627 std::to_string(operation) + ", input " +
628 std::to_string(input);
629 validate(device, message, model, [operation, input](Model* model) {
630 uint32_t operand = model->operations[operation].inputs[input];
631 model->operands[operand].numberOfConsumers--;
632 hidl_vec_removeAt(&model->operations[operation].inputs, input);
633 });
634 }
635 }
636}
637
638///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
639
640static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
641 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
642 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
643 const std::string message = "removeOperationOutputTest: operation " +
644 std::to_string(operation) + ", output " +
645 std::to_string(output);
646 validate(device, message, model, [operation, output](Model* model) {
647 hidl_vec_removeAt(&model->operations[operation].outputs, output);
648 });
649 }
650 }
651}
652
653///////////////////////// MODEL VALIDATION /////////////////////////
654
655// TODO: remove model input
656// TODO: remove model output
657// TODO: add unused operation
658
659///////////////////////// ADD OPERATION INPUT /////////////////////////
660
Xusong Wang5b747ae2018-10-05 11:49:13 -0700661static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700662 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700663 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
664 // parameter.
665 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
666 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
667 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700668 return true;
669 }
670 return false;
671}
672
Slava Shklyaev871be942018-09-12 14:52:02 +0100673static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
674 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700675 if (addOperationInputSkip(model.operations[operation])) {
676 continue;
677 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100678 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
679 validate(device, message, model, [operation](Model* model) {
680 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
681 hidl_vec_push_back(&model->operations[operation].inputs, index);
682 hidl_vec_push_back(&model->inputIndexes, index);
683 });
684 }
685}
686
687///////////////////////// ADD OPERATION OUTPUT /////////////////////////
688
689static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
690 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
691 const std::string message =
692 "addOperationOutputTest: operation " + std::to_string(operation);
693 validate(device, message, model, [operation](Model* model) {
694 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
695 hidl_vec_push_back(&model->operations[operation].outputs, index);
696 hidl_vec_push_back(&model->outputIndexes, index);
697 });
698 }
699}
700
701///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
702
703static const int32_t invalidExecutionPreferences[] = {
704 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
705 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
706};
707
708static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
709 for (int32_t preference : invalidExecutionPreferences) {
710 const std::string message =
711 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
712 validate(device, message, model, [](Model*) {},
713 static_cast<ExecutionPreference>(preference));
714 }
715}
716
717////////////////////////// ENTRY POINT //////////////////////////////
718
719void ValidationTest::validateModel(const Model& model) {
720 mutateOperandTypeTest(device, model);
721 mutateOperandRankTest(device, model);
722 mutateOperandScaleTest(device, model);
723 mutateOperandZeroPointTest(device, model);
724 mutateOperationOperandTypeTest(device, model);
725 mutateOperationTypeTest(device, model);
726 mutateOperationInputOperandIndexTest(device, model);
727 mutateOperationOutputOperandIndexTest(device, model);
728 removeOperandTest(device, model);
729 removeOperationTest(device, model);
730 removeOperationInputTest(device, model);
731 removeOperationOutputTest(device, model);
732 addOperationInputTest(device, model);
733 addOperationOutputTest(device, model);
734 mutateExecutionPreferenceTest(device, model);
735}
736
737} // namespace functional
738} // namespace vts
739} // namespace V1_2
740} // namespace neuralnetworks
741} // namespace hardware
742} // namespace android