blob: 7dfcff2d4216da6ea51a7d72fcdd059691d1fbb5 [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
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.0/Utils.h"
20#include "1.2/Callbacks.h"
Xusong Wang9e2b97b2019-08-23 16:10:54 -070021#include "GeneratedTestHarness.h"
Slava Shklyaev871be942018-09-12 14:52:02 +010022#include "VtsHalNeuralnetworks.h"
23
Slava Shklyaev871be942018-09-12 14:52:02 +010024namespace android {
25namespace hardware {
26namespace neuralnetworks {
27namespace V1_2 {
28
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010030using V1_1::ExecutionPreference;
31
32namespace vts {
33namespace functional {
34
Xusong Wangb5cb8f72018-10-31 08:43:12 -070035using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
36using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Xusong Wangb61ba1e2019-02-25 16:58:58 -080037using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Slava Shklyaev871be942018-09-12 14:52:02 +010038
39///////////////////////// UTILITY FUNCTIONS /////////////////////////
40
41static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
42 const Model& model) {
43 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
44
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010045 Return<void> ret = device->getSupportedOperations_1_2(
46 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
47 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
48 });
Slava Shklyaev871be942018-09-12 14:52:02 +010049 EXPECT_TRUE(ret.isOk());
50}
51
52static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
53 const Model& model, ExecutionPreference preference) {
54 SCOPED_TRACE(message + " [prepareModel_1_2]");
55
56 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
57 ASSERT_NE(nullptr, preparedModelCallback.get());
58 Return<ErrorStatus> prepareLaunchStatus =
Xusong Wangb61ba1e2019-02-25 16:58:58 -080059 device->prepareModel_1_2(model, preference, hidl_vec<hidl_handle>(),
60 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010061 ASSERT_TRUE(prepareLaunchStatus.isOk());
62 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
63
64 preparedModelCallback->wait();
65 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
66 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070067 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010068 ASSERT_EQ(nullptr, preparedModel.get());
69}
70
71static bool validExecutionPreference(ExecutionPreference preference) {
72 return preference == ExecutionPreference::LOW_POWER ||
73 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
74 preference == ExecutionPreference::SUSTAINED_SPEED;
75}
76
77// Primary validation function. This function will take a valid model, apply a
78// mutation to it to invalidate the model, then pass it to interface calls that
79// use the model. Note that the model here is passed by value, and any mutation
80// to the model does not leave this function.
81static void validate(const sp<IDevice>& device, const std::string& message, Model model,
82 const std::function<void(Model*)>& mutation,
83 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
84 mutation(&model);
85 if (validExecutionPreference(preference)) {
86 validateGetSupportedOperations(device, message, model);
87 }
88 validatePrepareModel(device, message, model, preference);
89}
90
Slava Shklyaev871be942018-09-12 14:52:02 +010091static uint32_t addOperand(Model* model) {
92 return hidl_vec_push_back(&model->operands,
93 {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010094 .type = OperandType::INT32,
95 .dimensions = {},
96 .numberOfConsumers = 0,
97 .scale = 0.0f,
98 .zeroPoint = 0,
99 .lifetime = OperandLifeTime::MODEL_INPUT,
100 .location = {.poolIndex = 0, .offset = 0, .length = 0},
Slava Shklyaev871be942018-09-12 14:52:02 +0100101 });
102}
103
104static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
105 uint32_t index = addOperand(model);
106 model->operands[index].numberOfConsumers = 1;
107 model->operands[index].lifetime = lifetime;
108 return index;
109}
110
111///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
112
Michael K. Sandersc785d462018-10-30 15:16:54 +0000113static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000114 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
115 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
116 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
117 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100118};
119
120static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
121 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000122 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100123 const std::string message = "mutateOperandTypeTest: operand " +
124 std::to_string(operand) + " set to value " +
125 std::to_string(invalidOperandType);
126 validate(device, message, model, [operand, invalidOperandType](Model* model) {
127 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
128 });
129 }
130 }
131}
132
133///////////////////////// VALIDATE OPERAND RANK /////////////////////////
134
135static uint32_t getInvalidRank(OperandType type) {
136 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800137 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100138 case OperandType::FLOAT32:
139 case OperandType::INT32:
140 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100141 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100142 return 1;
Lev Proleev923b8c52019-01-30 17:14:40 +0000143 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100144 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100145 case OperandType::TENSOR_FLOAT32:
146 case OperandType::TENSOR_INT32:
147 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800148 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800149 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000150 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000151 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100152 return 0;
153 default:
154 return 0;
155 }
156}
157
158static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
159 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
160 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wanga3165812018-11-19 18:26:08 -0800161 if (invalidRank == 0) {
162 continue;
163 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100164 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
165 " has rank of " + std::to_string(invalidRank);
166 validate(device, message, model, [operand, invalidRank](Model* model) {
167 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
168 });
169 }
170}
171
172///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
173
174static float getInvalidScale(OperandType type) {
175 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800176 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100177 case OperandType::FLOAT32:
178 case OperandType::INT32:
179 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100180 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000181 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100182 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100183 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000184 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100185 return 1.0f;
186 case OperandType::TENSOR_INT32:
187 return -1.0f;
Hervé Guihotbae91692019-01-23 19:18:59 -0800188 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100189 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800190 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000191 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100192 return 0.0f;
193 default:
194 return 0.0f;
195 }
196}
197
198static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
199 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
200 const float invalidScale = getInvalidScale(model.operands[operand].type);
201 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
202 " has scale of " + std::to_string(invalidScale);
203 validate(device, message, model, [operand, invalidScale](Model* model) {
204 model->operands[operand].scale = invalidScale;
205 });
206 }
207}
208
209///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
210
211static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
212 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800213 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100214 case OperandType::FLOAT32:
215 case OperandType::INT32:
216 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100217 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000218 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100219 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100220 case OperandType::TENSOR_FLOAT32:
221 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000222 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100223 return {1};
224 case OperandType::TENSOR_QUANT8_ASYMM:
225 return {-1, 256};
Hervé Guihotbae91692019-01-23 19:18:59 -0800226 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100227 return {-129, -1, 1, 128};
Xusong Wangd49f6652019-01-16 18:32:24 -0800228 case OperandType::TENSOR_QUANT16_ASYMM:
229 return {-1, 65536};
Lev Proleev48c88202018-11-13 15:42:36 +0000230 case OperandType::TENSOR_QUANT16_SYMM:
231 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100232 default:
233 return {};
234 }
235}
236
237static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
238 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
239 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100240 getInvalidZeroPoints(model.operands[operand].type);
Slava Shklyaev871be942018-09-12 14:52:02 +0100241 for (int32_t invalidZeroPoint : invalidZeroPoints) {
242 const std::string message = "mutateOperandZeroPointTest: operand " +
243 std::to_string(operand) + " has zero point of " +
244 std::to_string(invalidZeroPoint);
245 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
246 model->operands[operand].zeroPoint = invalidZeroPoint;
247 });
248 }
249 }
250}
251
252///////////////////////// VALIDATE EXTRA ??? /////////////////////////
253
254// TODO: Operand::lifetime
255// TODO: Operand::location
256
257///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
258
259static void mutateOperand(Operand* operand, OperandType type) {
260 Operand newOperand = *operand;
261 newOperand.type = type;
262 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800263 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100264 case OperandType::FLOAT32:
265 case OperandType::INT32:
266 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100267 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100268 newOperand.dimensions = hidl_vec<uint32_t>();
269 newOperand.scale = 0.0f;
270 newOperand.zeroPoint = 0;
271 break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000272 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100273 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100274 case OperandType::TENSOR_FLOAT32:
275 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100276 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100277 newOperand.scale = 0.0f;
278 newOperand.zeroPoint = 0;
279 break;
280 case OperandType::TENSOR_INT32:
281 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100282 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100283 newOperand.zeroPoint = 0;
284 break;
285 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800286 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800287 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000288 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100289 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100290 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100291 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
292 break;
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000293 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
294 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100295 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000296 newOperand.scale = 0.0f;
297 newOperand.zeroPoint = 0;
298
299 SymmPerChannelQuantParams channelQuant;
300 channelQuant.channelDim = 0;
301 channelQuant.scales = hidl_vec<float>(
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100302 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
303 : 0);
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000304 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
305 channelQuant.scales[i] = 1.0f;
306 }
307 newOperand.extraParams.channelQuant(std::move(channelQuant));
308 } break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100309 case OperandType::OEM:
310 case OperandType::TENSOR_OEM_BYTE:
311 default:
312 break;
313 }
314 *operand = newOperand;
315}
316
Xusong Wang5b747ae2018-10-05 11:49:13 -0700317static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
318 // Do not test OEM types
319 if (type == model.operands[operand].type || type == OperandType::OEM ||
320 type == OperandType::TENSOR_OEM_BYTE) {
321 return true;
322 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100323 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700324 // Skip mutateOperationOperandTypeTest for the following operations.
325 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000326 // - ARGMIN and ARGMAX's first argument can be any of
327 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
328 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000329 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev923b8c52019-01-30 17:14:40 +0000330 // - DEQUANTIZE input can be any of
331 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
332 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
333 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000334 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000335 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb0762cc2019-01-15 17:53:46 +0000336 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev1509a262019-01-15 17:49:24 +0000337 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang5b747ae2018-10-05 11:49:13 -0700338 switch (operation.type) {
339 case OperationType::LSH_PROJECTION: {
340 if (operand == operation.inputs[1]) {
341 return true;
342 }
343 } break;
344 case OperationType::CAST:
345 case OperationType::ARGMAX:
346 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000347 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
348 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700349 return true;
350 }
351 } break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000352 case OperationType::QUANTIZE:
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000353 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleev923b8c52019-01-30 17:14:40 +0000354 if (operand == operation.inputs[0] &&
355 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
356 return true;
357 }
358 } break;
359 case OperationType::DEQUANTIZE: {
360 if (operand == operation.inputs[0] &&
361 (type == OperandType::TENSOR_QUANT8_ASYMM ||
362 type == OperandType::TENSOR_QUANT8_SYMM ||
363 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
364 return true;
365 }
366 if (operand == operation.outputs[0] &&
367 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000368 return true;
369 }
370 } break;
Lev Proleev1509a262019-01-15 17:49:24 +0000371 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb0762cc2019-01-15 17:53:46 +0000372 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000373 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000374 case OperationType::CONV_2D: {
Xusong Wang88044232019-03-13 16:24:34 -0700375 if (operand == operation.inputs[1] &&
376 (type == OperandType::TENSOR_QUANT8_ASYMM ||
377 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000378 return true;
379 }
380 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700381 default:
382 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100383 }
384 }
385 return false;
386}
387
388static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
389 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100390 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700391 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100392 continue;
393 }
394 const std::string message = "mutateOperationOperandTypeTest: operand " +
395 std::to_string(operand) + " set to type " +
396 toString(invalidOperandType);
397 validate(device, message, model, [operand, invalidOperandType](Model* model) {
398 mutateOperand(&model->operands[operand], invalidOperandType);
399 });
400 }
401 }
402}
403
404///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
405
Michael K. Sandersc785d462018-10-30 15:16:54 +0000406static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000407 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
408 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
409 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100410};
411
412static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
413 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000414 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100415 const std::string message = "mutateOperationTypeTest: operation " +
416 std::to_string(operation) + " set to value " +
417 std::to_string(invalidOperationType);
418 validate(device, message, model, [operation, invalidOperationType](Model* model) {
419 model->operations[operation].type =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100420 static_cast<OperationType>(invalidOperationType);
Slava Shklyaev871be942018-09-12 14:52:02 +0100421 });
422 }
423 }
424}
425
426///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
427
428static void mutateOperationInputOperandIndexTest(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 input = 0; input < model.operations[operation].inputs.size(); ++input) {
432 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
433 std::to_string(operation) + " input " +
434 std::to_string(input);
435 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
436 model->operations[operation].inputs[input] = invalidOperand;
437 });
438 }
439 }
440}
441
442///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
443
444static void mutateOperationOutputOperandIndexTest(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 output = 0; output < model.operations[operation].outputs.size(); ++output) {
448 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
449 std::to_string(operation) + " output " +
450 std::to_string(output);
451 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
452 model->operations[operation].outputs[output] = invalidOperand;
453 });
454 }
455 }
456}
457
458///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
459
460static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
461 if (vec) {
462 // remove elements matching "value"
463 auto last = std::remove(vec->begin(), vec->end(), value);
464 vec->resize(std::distance(vec->begin(), last));
465
466 // decrement elements exceeding "value"
467 std::transform(vec->begin(), vec->end(), vec->begin(),
468 [value](uint32_t v) { return v > value ? v-- : v; });
469 }
470}
471
472static void removeOperand(Model* model, uint32_t index) {
473 hidl_vec_removeAt(&model->operands, index);
474 for (Operation& operation : model->operations) {
475 removeValueAndDecrementGreaterValues(&operation.inputs, index);
476 removeValueAndDecrementGreaterValues(&operation.outputs, index);
477 }
478 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
479 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
480}
481
Xusong Wang5b747ae2018-10-05 11:49:13 -0700482static bool removeOperandSkip(size_t operand, const Model& model) {
483 for (const Operation& operation : model.operations) {
484 // Skip removeOperandTest for the following operations.
485 // - SPLIT's outputs are not checked during prepareModel.
486 if (operation.type == OperationType::SPLIT) {
487 for (const size_t outOprand : operation.outputs) {
488 if (operand == outOprand) {
489 return true;
490 }
491 }
492 }
Viet Danga8f33f72019-03-28 17:22:56 +0000493 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
494 // outputs depending on their mergeOutputs parameter.
495 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
496 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
Lev Proleev923b8c52019-01-30 17:14:40 +0000497 for (const size_t outOprand : operation.outputs) {
498 if (operand == outOprand) {
499 return true;
500 }
501 }
502 }
Xusong Wang5b747ae2018-10-05 11:49:13 -0700503 }
504 return false;
505}
506
Slava Shklyaev871be942018-09-12 14:52:02 +0100507static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
508 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700509 if (removeOperandSkip(operand, model)) {
510 continue;
511 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100512 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
513 validate(device, message, model,
514 [operand](Model* model) { removeOperand(model, operand); });
515 }
516}
517
518///////////////////////// REMOVE OPERATION /////////////////////////
519
520static void removeOperation(Model* model, uint32_t index) {
521 for (uint32_t operand : model->operations[index].inputs) {
522 model->operands[operand].numberOfConsumers--;
523 }
524 hidl_vec_removeAt(&model->operations, index);
525}
526
527static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
528 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
529 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
530 validate(device, message, model,
531 [operation](Model* model) { removeOperation(model, operation); });
532 }
533}
534
535///////////////////////// REMOVE OPERATION INPUT /////////////////////////
536
Xusong Wang5b747ae2018-10-05 11:49:13 -0700537static bool removeOperationInputSkip(const Operation& op, size_t input) {
538 // Skip removeOperationInputTest for the following operations.
539 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
540 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
541 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
542 // layout parameter.
543 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
544 // parameter.
545 switch (op.type) {
546 case OperationType::CONCATENATION: {
547 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
548 return true;
549 }
550 } break;
551 case OperationType::DEPTHWISE_CONV_2D: {
552 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
553 return true;
554 }
555 } break;
556 case OperationType::CONV_2D:
557 case OperationType::AVERAGE_POOL_2D:
558 case OperationType::MAX_POOL_2D:
559 case OperationType::L2_POOL_2D: {
560 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
561 return true;
562 }
563 } break;
564 case OperationType::RESIZE_BILINEAR: {
565 if (op.inputs.size() == 4 && input == 3) {
566 return true;
567 }
568 } break;
569 case OperationType::SPACE_TO_DEPTH:
570 case OperationType::DEPTH_TO_SPACE:
571 case OperationType::BATCH_TO_SPACE_ND: {
572 if (op.inputs.size() == 3 && input == 2) {
573 return true;
574 }
575 } break;
576 case OperationType::SPACE_TO_BATCH_ND: {
577 if (op.inputs.size() == 4 && input == 3) {
578 return true;
579 }
580 } break;
581 case OperationType::L2_NORMALIZATION: {
582 if (op.inputs.size() == 2 && input == 1) {
583 return true;
584 }
585 } break;
586 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
587 if (op.inputs.size() == 6 && input == 5) {
588 return true;
589 }
590 } break;
591 case OperationType::SOFTMAX: {
592 if (op.inputs.size() == 3 && input == 2) {
593 return true;
594 }
595 } break;
596 default:
597 break;
598 }
599 return false;
600}
601
Slava Shklyaev871be942018-09-12 14:52:02 +0100602static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
603 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
604 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
605 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700606 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100607 continue;
608 }
609 const std::string message = "removeOperationInputTest: operation " +
610 std::to_string(operation) + ", input " +
611 std::to_string(input);
612 validate(device, message, model, [operation, input](Model* model) {
613 uint32_t operand = model->operations[operation].inputs[input];
614 model->operands[operand].numberOfConsumers--;
615 hidl_vec_removeAt(&model->operations[operation].inputs, input);
616 });
617 }
618 }
619}
620
621///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
622
623static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
624 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
625 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
626 const std::string message = "removeOperationOutputTest: operation " +
627 std::to_string(operation) + ", output " +
628 std::to_string(output);
629 validate(device, message, model, [operation, output](Model* model) {
630 hidl_vec_removeAt(&model->operations[operation].outputs, output);
631 });
632 }
633 }
634}
635
636///////////////////////// MODEL VALIDATION /////////////////////////
637
638// TODO: remove model input
639// TODO: remove model output
640// TODO: add unused operation
641
642///////////////////////// ADD OPERATION INPUT /////////////////////////
643
Xusong Wang5b747ae2018-10-05 11:49:13 -0700644static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700645 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700646 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
647 // parameter.
648 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
649 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
650 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700651 return true;
652 }
653 return false;
654}
655
Slava Shklyaev871be942018-09-12 14:52:02 +0100656static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
657 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700658 if (addOperationInputSkip(model.operations[operation])) {
659 continue;
660 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100661 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
662 validate(device, message, model, [operation](Model* model) {
663 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
664 hidl_vec_push_back(&model->operations[operation].inputs, index);
665 hidl_vec_push_back(&model->inputIndexes, index);
666 });
667 }
668}
669
670///////////////////////// ADD OPERATION OUTPUT /////////////////////////
671
672static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
673 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
674 const std::string message =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100675 "addOperationOutputTest: operation " + std::to_string(operation);
Slava Shklyaev871be942018-09-12 14:52:02 +0100676 validate(device, message, model, [operation](Model* model) {
677 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
678 hidl_vec_push_back(&model->operations[operation].outputs, index);
679 hidl_vec_push_back(&model->outputIndexes, index);
680 });
681 }
682}
683
684///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
685
686static const int32_t invalidExecutionPreferences[] = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100687 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
688 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
Slava Shklyaev871be942018-09-12 14:52:02 +0100689};
690
691static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
692 for (int32_t preference : invalidExecutionPreferences) {
693 const std::string message =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100694 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
Slava Shklyaev871be942018-09-12 14:52:02 +0100695 validate(device, message, model, [](Model*) {},
696 static_cast<ExecutionPreference>(preference));
697 }
698}
699
700////////////////////////// ENTRY POINT //////////////////////////////
701
702void ValidationTest::validateModel(const Model& model) {
703 mutateOperandTypeTest(device, model);
704 mutateOperandRankTest(device, model);
705 mutateOperandScaleTest(device, model);
706 mutateOperandZeroPointTest(device, model);
707 mutateOperationOperandTypeTest(device, model);
708 mutateOperationTypeTest(device, model);
709 mutateOperationInputOperandIndexTest(device, model);
710 mutateOperationOutputOperandIndexTest(device, model);
711 removeOperandTest(device, model);
712 removeOperationTest(device, model);
713 removeOperationInputTest(device, model);
714 removeOperationOutputTest(device, model);
715 addOperationInputTest(device, model);
716 addOperationOutputTest(device, model);
717 mutateExecutionPreferenceTest(device, model);
718}
719
720} // namespace functional
721} // namespace vts
722} // namespace V1_2
723} // namespace neuralnetworks
724} // namespace hardware
725} // namespace android