blob: 7451f095bf96894b7022debde6632795e6fd5291 [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
Michael Butlerbbe5dad2019-08-26 23:55:47 -070024namespace android::hardware::neuralnetworks::V1_2::vts::functional {
Slava Shklyaev871be942018-09-12 14:52:02 +010025
Michael Butlerbbe5dad2019-08-26 23:55:47 -070026using implementation::PreparedModelCallback;
27using V1_0::ErrorStatus;
Slava Shklyaev871be942018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaev871be942018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
Xusong Wangb61ba1e2019-02-25 16:58:58 -080030using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Slava Shklyaev871be942018-09-12 14:52:02 +010031
Michael Butlerda1a6922020-03-11 18:45:45 -070032using PrepareModelMutation = std::function<void(Model*, ExecutionPreference*)>;
33
Slava Shklyaev871be942018-09-12 14:52:02 +010034///////////////////////// UTILITY FUNCTIONS /////////////////////////
35
36static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
37 const Model& model) {
38 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
39
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010040 Return<void> ret = device->getSupportedOperations_1_2(
41 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
42 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
43 });
Slava Shklyaev871be942018-09-12 14:52:02 +010044 EXPECT_TRUE(ret.isOk());
45}
46
47static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
48 const Model& model, ExecutionPreference preference) {
49 SCOPED_TRACE(message + " [prepareModel_1_2]");
50
51 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
Slava Shklyaev871be942018-09-12 14:52:02 +010052 Return<ErrorStatus> prepareLaunchStatus =
Xusong Wangb61ba1e2019-02-25 16:58:58 -080053 device->prepareModel_1_2(model, preference, hidl_vec<hidl_handle>(),
54 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010055 ASSERT_TRUE(prepareLaunchStatus.isOk());
56 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
57
58 preparedModelCallback->wait();
59 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
60 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070061 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010062 ASSERT_EQ(nullptr, preparedModel.get());
63}
64
65static bool validExecutionPreference(ExecutionPreference preference) {
66 return preference == ExecutionPreference::LOW_POWER ||
67 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
68 preference == ExecutionPreference::SUSTAINED_SPEED;
69}
70
71// Primary validation function. This function will take a valid model, apply a
Michael Butlerda1a6922020-03-11 18:45:45 -070072// mutation to invalidate either the model or the execution preference, then
73// pass these to supportedOperations and/or prepareModel if that method is
74// called with an invalid argument.
75static void validate(const sp<IDevice>& device, const std::string& message,
76 const Model& originalModel, const PrepareModelMutation& mutate) {
77 Model model = originalModel;
78 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER;
79 mutate(&model, &preference);
80
Slava Shklyaev871be942018-09-12 14:52:02 +010081 if (validExecutionPreference(preference)) {
82 validateGetSupportedOperations(device, message, model);
83 }
Michael Butlerda1a6922020-03-11 18:45:45 -070084
Slava Shklyaev871be942018-09-12 14:52:02 +010085 validatePrepareModel(device, message, model, preference);
86}
87
Slava Shklyaev871be942018-09-12 14:52:02 +010088static uint32_t addOperand(Model* model) {
89 return hidl_vec_push_back(&model->operands,
90 {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010091 .type = OperandType::INT32,
92 .dimensions = {},
93 .numberOfConsumers = 0,
94 .scale = 0.0f,
95 .zeroPoint = 0,
96 .lifetime = OperandLifeTime::MODEL_INPUT,
97 .location = {.poolIndex = 0, .offset = 0, .length = 0},
Slava Shklyaev871be942018-09-12 14:52:02 +010098 });
99}
100
101static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
102 uint32_t index = addOperand(model);
103 model->operands[index].numberOfConsumers = 1;
104 model->operands[index].lifetime = lifetime;
105 return index;
106}
107
108///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
109
Michael K. Sandersc785d462018-10-30 15:16:54 +0000110static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000111 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
112 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
113 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
114 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100115};
116
117static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
118 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000119 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100120 const std::string message = "mutateOperandTypeTest: operand " +
121 std::to_string(operand) + " set to value " +
122 std::to_string(invalidOperandType);
Michael Butlerda1a6922020-03-11 18:45:45 -0700123 validate(device, message, model,
124 [operand, invalidOperandType](Model* model, ExecutionPreference*) {
125 model->operands[operand].type =
126 static_cast<OperandType>(invalidOperandType);
127 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100128 }
129 }
130}
131
132///////////////////////// VALIDATE OPERAND RANK /////////////////////////
133
134static uint32_t getInvalidRank(OperandType type) {
135 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800136 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100137 case OperandType::FLOAT32:
138 case OperandType::INT32:
139 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100140 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100141 return 1;
Lev Proleev923b8c52019-01-30 17:14:40 +0000142 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100143 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100144 case OperandType::TENSOR_FLOAT32:
145 case OperandType::TENSOR_INT32:
146 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800147 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800148 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000149 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000150 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100151 return 0;
152 default:
153 return 0;
154 }
155}
156
157static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
158 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
159 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wanga3165812018-11-19 18:26:08 -0800160 if (invalidRank == 0) {
161 continue;
162 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100163 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
164 " has rank of " + std::to_string(invalidRank);
Michael Butlerda1a6922020-03-11 18:45:45 -0700165 validate(device, message, model,
166 [operand, invalidRank](Model* model, ExecutionPreference*) {
167 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
168 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100169 }
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);
Michael Butlerda1a6922020-03-11 18:45:45 -0700203 validate(device, message, model,
204 [operand, invalidScale](Model* model, ExecutionPreference*) {
205 model->operands[operand].scale = invalidScale;
206 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100207 }
208}
209
210///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
211
212static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
213 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800214 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100215 case OperandType::FLOAT32:
216 case OperandType::INT32:
217 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100218 case OperandType::BOOL:
Lev Proleev923b8c52019-01-30 17:14:40 +0000219 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100220 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100221 case OperandType::TENSOR_FLOAT32:
222 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000223 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100224 return {1};
225 case OperandType::TENSOR_QUANT8_ASYMM:
226 return {-1, 256};
Hervé Guihotbae91692019-01-23 19:18:59 -0800227 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100228 return {-129, -1, 1, 128};
Xusong Wangd49f6652019-01-16 18:32:24 -0800229 case OperandType::TENSOR_QUANT16_ASYMM:
230 return {-1, 65536};
Lev Proleev48c88202018-11-13 15:42:36 +0000231 case OperandType::TENSOR_QUANT16_SYMM:
232 return {-32769, -1, 1, 32768};
Slava Shklyaev871be942018-09-12 14:52:02 +0100233 default:
234 return {};
235 }
236}
237
238static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
239 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
240 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100241 getInvalidZeroPoints(model.operands[operand].type);
Slava Shklyaev871be942018-09-12 14:52:02 +0100242 for (int32_t invalidZeroPoint : invalidZeroPoints) {
243 const std::string message = "mutateOperandZeroPointTest: operand " +
244 std::to_string(operand) + " has zero point of " +
245 std::to_string(invalidZeroPoint);
Michael Butlerda1a6922020-03-11 18:45:45 -0700246 validate(device, message, model,
247 [operand, invalidZeroPoint](Model* model, ExecutionPreference*) {
248 model->operands[operand].zeroPoint = invalidZeroPoint;
249 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100250 }
251 }
252}
253
254///////////////////////// VALIDATE EXTRA ??? /////////////////////////
255
256// TODO: Operand::lifetime
257// TODO: Operand::location
258
259///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
260
261static void mutateOperand(Operand* operand, OperandType type) {
262 Operand newOperand = *operand;
263 newOperand.type = type;
264 switch (type) {
Xusong Wang7bca34b2018-12-05 14:21:51 -0800265 case OperandType::FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100266 case OperandType::FLOAT32:
267 case OperandType::INT32:
268 case OperandType::UINT32:
Lev Proleevabad9ea2018-10-01 11:18:31 +0100269 case OperandType::BOOL:
Slava Shklyaev871be942018-09-12 14:52:02 +0100270 newOperand.dimensions = hidl_vec<uint32_t>();
271 newOperand.scale = 0.0f;
272 newOperand.zeroPoint = 0;
273 break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000274 case OperandType::TENSOR_BOOL8:
Michael K. Sanders19d63452018-10-12 09:10:15 +0100275 case OperandType::TENSOR_FLOAT16:
Slava Shklyaev871be942018-09-12 14:52:02 +0100276 case OperandType::TENSOR_FLOAT32:
277 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100278 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100279 newOperand.scale = 0.0f;
280 newOperand.zeroPoint = 0;
281 break;
282 case OperandType::TENSOR_INT32:
283 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100284 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100285 newOperand.zeroPoint = 0;
286 break;
287 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihotbae91692019-01-23 19:18:59 -0800288 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangd49f6652019-01-16 18:32:24 -0800289 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev48c88202018-11-13 15:42:36 +0000290 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaev871be942018-09-12 14:52:02 +0100291 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100292 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaev871be942018-09-12 14:52:02 +0100293 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
294 break;
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000295 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
296 newOperand.dimensions =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100297 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000298 newOperand.scale = 0.0f;
299 newOperand.zeroPoint = 0;
300
301 SymmPerChannelQuantParams channelQuant;
302 channelQuant.channelDim = 0;
303 channelQuant.scales = hidl_vec<float>(
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100304 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
305 : 0);
Przemyslaw Szczepaniakfaa59b82018-11-08 15:22:17 +0000306 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
307 channelQuant.scales[i] = 1.0f;
308 }
309 newOperand.extraParams.channelQuant(std::move(channelQuant));
310 } break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100311 case OperandType::OEM:
312 case OperandType::TENSOR_OEM_BYTE:
313 default:
314 break;
315 }
316 *operand = newOperand;
317}
318
Xusong Wang5b747ae2018-10-05 11:49:13 -0700319static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
320 // Do not test OEM types
321 if (type == model.operands[operand].type || type == OperandType::OEM ||
322 type == OperandType::TENSOR_OEM_BYTE) {
323 return true;
324 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100325 for (const Operation& operation : model.operations) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700326 // Skip mutateOperationOperandTypeTest for the following operations.
327 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000328 // - ARGMIN and ARGMAX's first argument can be any of
329 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
330 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000331 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev923b8c52019-01-30 17:14:40 +0000332 // - DEQUANTIZE input can be any of
333 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
334 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
335 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000336 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000337 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb0762cc2019-01-15 17:53:46 +0000338 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev1509a262019-01-15 17:49:24 +0000339 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang5b747ae2018-10-05 11:49:13 -0700340 switch (operation.type) {
341 case OperationType::LSH_PROJECTION: {
342 if (operand == operation.inputs[1]) {
343 return true;
344 }
345 } break;
346 case OperationType::CAST:
347 case OperationType::ARGMAX:
348 case OperationType::ARGMIN: {
Michael K. Sandersbbdab2f2018-11-28 10:35:08 +0000349 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
350 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700351 return true;
352 }
353 } break;
Lev Proleev923b8c52019-01-30 17:14:40 +0000354 case OperationType::QUANTIZE:
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000355 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleev923b8c52019-01-30 17:14:40 +0000356 if (operand == operation.inputs[0] &&
357 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
358 return true;
359 }
360 } break;
361 case OperationType::DEQUANTIZE: {
362 if (operand == operation.inputs[0] &&
363 (type == OperandType::TENSOR_QUANT8_ASYMM ||
364 type == OperandType::TENSOR_QUANT8_SYMM ||
365 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
366 return true;
367 }
368 if (operand == operation.outputs[0] &&
369 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders5b2615b2018-12-06 12:34:07 +0000370 return true;
371 }
372 } break;
Lev Proleev1509a262019-01-15 17:49:24 +0000373 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb0762cc2019-01-15 17:53:46 +0000374 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak47b91412018-12-11 13:42:27 +0000375 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000376 case OperationType::CONV_2D: {
Xusong Wang88044232019-03-13 16:24:34 -0700377 if (operand == operation.inputs[1] &&
378 (type == OperandType::TENSOR_QUANT8_ASYMM ||
379 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniakf54f1262018-11-26 14:10:06 +0000380 return true;
381 }
382 } break;
Xusong Wang5b747ae2018-10-05 11:49:13 -0700383 default:
384 break;
Slava Shklyaev871be942018-09-12 14:52:02 +0100385 }
386 }
387 return false;
388}
389
390static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
391 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100392 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700393 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100394 continue;
395 }
396 const std::string message = "mutateOperationOperandTypeTest: operand " +
397 std::to_string(operand) + " set to type " +
398 toString(invalidOperandType);
Michael Butlerda1a6922020-03-11 18:45:45 -0700399 validate(device, message, model,
400 [operand, invalidOperandType](Model* model, ExecutionPreference*) {
401 mutateOperand(&model->operands[operand], invalidOperandType);
402 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100403 }
404 }
405}
406
407///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
408
Michael K. Sandersc785d462018-10-30 15:16:54 +0000409static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev794703d2019-01-17 15:37:05 +0000410 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
411 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
412 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaev871be942018-09-12 14:52:02 +0100413};
414
415static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
416 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sandersc785d462018-10-30 15:16:54 +0000417 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100418 const std::string message = "mutateOperationTypeTest: operation " +
419 std::to_string(operation) + " set to value " +
420 std::to_string(invalidOperationType);
Michael Butlerda1a6922020-03-11 18:45:45 -0700421 validate(device, message, model,
422 [operation, invalidOperationType](Model* model, ExecutionPreference*) {
423 model->operations[operation].type =
424 static_cast<OperationType>(invalidOperationType);
425 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100426 }
427 }
428}
429
430///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
431
432static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
433 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
434 const uint32_t invalidOperand = model.operands.size();
435 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
436 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
437 std::to_string(operation) + " input " +
438 std::to_string(input);
Michael Butlerda1a6922020-03-11 18:45:45 -0700439 validate(device, message, model,
440 [operation, input, invalidOperand](Model* model, ExecutionPreference*) {
441 model->operations[operation].inputs[input] = invalidOperand;
442 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100443 }
444 }
445}
446
447///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
448
449static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
450 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
451 const uint32_t invalidOperand = model.operands.size();
452 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
453 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
454 std::to_string(operation) + " output " +
455 std::to_string(output);
Michael Butlerda1a6922020-03-11 18:45:45 -0700456 validate(device, message, model,
457 [operation, output, invalidOperand](Model* model, ExecutionPreference*) {
458 model->operations[operation].outputs[output] = invalidOperand;
459 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100460 }
461 }
462}
463
464///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
465
466static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
467 if (vec) {
468 // remove elements matching "value"
469 auto last = std::remove(vec->begin(), vec->end(), value);
470 vec->resize(std::distance(vec->begin(), last));
471
472 // decrement elements exceeding "value"
473 std::transform(vec->begin(), vec->end(), vec->begin(),
474 [value](uint32_t v) { return v > value ? v-- : v; });
475 }
476}
477
478static void removeOperand(Model* model, uint32_t index) {
479 hidl_vec_removeAt(&model->operands, index);
480 for (Operation& operation : model->operations) {
481 removeValueAndDecrementGreaterValues(&operation.inputs, index);
482 removeValueAndDecrementGreaterValues(&operation.outputs, index);
483 }
484 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
485 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
486}
487
Xusong Wang5b747ae2018-10-05 11:49:13 -0700488static bool removeOperandSkip(size_t operand, const Model& model) {
489 for (const Operation& operation : model.operations) {
490 // Skip removeOperandTest for the following operations.
491 // - SPLIT's outputs are not checked during prepareModel.
492 if (operation.type == OperationType::SPLIT) {
493 for (const size_t outOprand : operation.outputs) {
494 if (operand == outOprand) {
495 return true;
496 }
497 }
498 }
Viet Danga8f33f72019-03-28 17:22:56 +0000499 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
500 // outputs depending on their mergeOutputs parameter.
501 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
502 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
Lev Proleev923b8c52019-01-30 17:14:40 +0000503 for (const size_t outOprand : operation.outputs) {
504 if (operand == outOprand) {
505 return true;
506 }
507 }
508 }
Xusong Wang5b747ae2018-10-05 11:49:13 -0700509 }
510 return false;
511}
512
Slava Shklyaev871be942018-09-12 14:52:02 +0100513static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
514 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang5b747ae2018-10-05 11:49:13 -0700515 if (removeOperandSkip(operand, model)) {
516 continue;
517 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100518 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
519 validate(device, message, model,
Michael Butlerda1a6922020-03-11 18:45:45 -0700520 [operand](Model* model, ExecutionPreference*) { removeOperand(model, operand); });
Slava Shklyaev871be942018-09-12 14:52:02 +0100521 }
522}
523
524///////////////////////// REMOVE OPERATION /////////////////////////
525
526static void removeOperation(Model* model, uint32_t index) {
527 for (uint32_t operand : model->operations[index].inputs) {
528 model->operands[operand].numberOfConsumers--;
529 }
530 hidl_vec_removeAt(&model->operations, index);
531}
532
533static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
534 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
535 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
Michael Butlerda1a6922020-03-11 18:45:45 -0700536 validate(device, message, model, [operation](Model* model, ExecutionPreference*) {
537 removeOperation(model, operation);
538 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100539 }
540}
541
542///////////////////////// REMOVE OPERATION INPUT /////////////////////////
543
Xusong Wang5b747ae2018-10-05 11:49:13 -0700544static bool removeOperationInputSkip(const Operation& op, size_t input) {
545 // Skip removeOperationInputTest for the following operations.
546 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
547 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
548 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
549 // layout parameter.
550 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
551 // parameter.
552 switch (op.type) {
553 case OperationType::CONCATENATION: {
554 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
555 return true;
556 }
557 } break;
558 case OperationType::DEPTHWISE_CONV_2D: {
559 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
560 return true;
561 }
562 } break;
563 case OperationType::CONV_2D:
564 case OperationType::AVERAGE_POOL_2D:
565 case OperationType::MAX_POOL_2D:
566 case OperationType::L2_POOL_2D: {
567 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
568 return true;
569 }
570 } break;
571 case OperationType::RESIZE_BILINEAR: {
572 if (op.inputs.size() == 4 && input == 3) {
573 return true;
574 }
575 } break;
576 case OperationType::SPACE_TO_DEPTH:
577 case OperationType::DEPTH_TO_SPACE:
578 case OperationType::BATCH_TO_SPACE_ND: {
579 if (op.inputs.size() == 3 && input == 2) {
580 return true;
581 }
582 } break;
583 case OperationType::SPACE_TO_BATCH_ND: {
584 if (op.inputs.size() == 4 && input == 3) {
585 return true;
586 }
587 } break;
588 case OperationType::L2_NORMALIZATION: {
589 if (op.inputs.size() == 2 && input == 1) {
590 return true;
591 }
592 } break;
593 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
594 if (op.inputs.size() == 6 && input == 5) {
595 return true;
596 }
597 } break;
598 case OperationType::SOFTMAX: {
599 if (op.inputs.size() == 3 && input == 2) {
600 return true;
601 }
602 } break;
603 default:
604 break;
605 }
606 return false;
607}
608
Slava Shklyaev871be942018-09-12 14:52:02 +0100609static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
610 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
611 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
612 const Operation& op = model.operations[operation];
Xusong Wang5b747ae2018-10-05 11:49:13 -0700613 if (removeOperationInputSkip(op, input)) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100614 continue;
615 }
616 const std::string message = "removeOperationInputTest: operation " +
617 std::to_string(operation) + ", input " +
618 std::to_string(input);
Michael Butlerda1a6922020-03-11 18:45:45 -0700619 validate(device, message, model,
620 [operation, input](Model* model, ExecutionPreference*) {
621 uint32_t operand = model->operations[operation].inputs[input];
622 model->operands[operand].numberOfConsumers--;
623 hidl_vec_removeAt(&model->operations[operation].inputs, input);
624 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100625 }
626 }
627}
628
629///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
630
631static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
632 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
633 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
634 const std::string message = "removeOperationOutputTest: operation " +
635 std::to_string(operation) + ", output " +
636 std::to_string(output);
Michael Butlerda1a6922020-03-11 18:45:45 -0700637 validate(device, message, model,
638 [operation, output](Model* model, ExecutionPreference*) {
639 hidl_vec_removeAt(&model->operations[operation].outputs, output);
640 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100641 }
642 }
643}
644
645///////////////////////// MODEL VALIDATION /////////////////////////
646
647// TODO: remove model input
648// TODO: remove model output
649// TODO: add unused operation
650
651///////////////////////// ADD OPERATION INPUT /////////////////////////
652
Xusong Wang5b747ae2018-10-05 11:49:13 -0700653static bool addOperationInputSkip(const Operation& op) {
Xusong Wang64337282018-10-22 13:49:00 -0700654 // Skip addOperationInputTest for the following operations.
Xusong Wang5b747ae2018-10-05 11:49:13 -0700655 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
656 // parameter.
657 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
658 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
659 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wang64337282018-10-22 13:49:00 -0700660 return true;
661 }
662 return false;
663}
664
Slava Shklyaev871be942018-09-12 14:52:02 +0100665static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
666 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wang64337282018-10-22 13:49:00 -0700667 if (addOperationInputSkip(model.operations[operation])) {
668 continue;
669 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100670 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
Michael Butlerda1a6922020-03-11 18:45:45 -0700671 validate(device, message, model, [operation](Model* model, ExecutionPreference*) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100672 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
673 hidl_vec_push_back(&model->operations[operation].inputs, index);
674 hidl_vec_push_back(&model->inputIndexes, index);
675 });
676 }
677}
678
679///////////////////////// ADD OPERATION OUTPUT /////////////////////////
680
681static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
682 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
683 const std::string message =
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100684 "addOperationOutputTest: operation " + std::to_string(operation);
Michael Butlerda1a6922020-03-11 18:45:45 -0700685 validate(device, message, model, [operation](Model* model, ExecutionPreference*) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100686 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
687 hidl_vec_push_back(&model->operations[operation].outputs, index);
688 hidl_vec_push_back(&model->outputIndexes, index);
689 });
690 }
691}
692
693///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
694
695static const int32_t invalidExecutionPreferences[] = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100696 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
697 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
Slava Shklyaev871be942018-09-12 14:52:02 +0100698};
699
700static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
Michael Butlerda1a6922020-03-11 18:45:45 -0700701 for (int32_t invalidPreference : invalidExecutionPreferences) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100702 const std::string message =
Michael Butlerda1a6922020-03-11 18:45:45 -0700703 "mutateExecutionPreferenceTest: preference " + std::to_string(invalidPreference);
704 validate(device, message, model,
705 [invalidPreference](Model*, ExecutionPreference* preference) {
706 *preference = static_cast<ExecutionPreference>(invalidPreference);
707 });
Slava Shklyaev871be942018-09-12 14:52:02 +0100708 }
709}
710
711////////////////////////// ENTRY POINT //////////////////////////////
712
Michael Butlere16af0a2019-08-29 22:17:24 -0700713void validateModel(const sp<IDevice>& device, const Model& model) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100714 mutateOperandTypeTest(device, model);
715 mutateOperandRankTest(device, model);
716 mutateOperandScaleTest(device, model);
717 mutateOperandZeroPointTest(device, model);
718 mutateOperationOperandTypeTest(device, model);
719 mutateOperationTypeTest(device, model);
720 mutateOperationInputOperandIndexTest(device, model);
721 mutateOperationOutputOperandIndexTest(device, model);
722 removeOperandTest(device, model);
723 removeOperationTest(device, model);
724 removeOperationInputTest(device, model);
725 removeOperationOutputTest(device, model);
726 addOperationInputTest(device, model);
727 addOperationOutputTest(device, model);
728 mutateExecutionPreferenceTest(device, model);
729}
730
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700731} // namespace android::hardware::neuralnetworks::V1_2::vts::functional