blob: cc862645a53b5244e4a455ea639f3f4dbd8f5640 [file] [log] [blame]
Lev Proleev13fdfcd2019-08-30 11:35:34 +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 "1.0/Utils.h"
Xusong Wangcc47dff2019-10-23 10:35:07 -070020#include "1.3/Callbacks.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010021#include "GeneratedTestHarness.h"
22#include "VtsHalNeuralnetworks.h"
23
Lev Proleev26d1bc82019-08-30 11:57:18 +010024namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010025
Xusong Wangcc47dff2019-10-23 10:35:07 -070026using implementation::PreparedModelCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010027using V1_0::ErrorStatus;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010028using V1_1::ExecutionPreference;
Lev Proleev26d1bc82019-08-30 11:57:18 +010029using V1_2::SymmPerChannelQuantParams;
Lev Proleev26d1bc82019-08-30 11:57:18 +010030using HidlToken =
31 hidl_array<uint8_t, static_cast<uint32_t>(V1_2::Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010032
33///////////////////////// UTILITY FUNCTIONS /////////////////////////
34
35static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
36 const Model& model) {
Lev Proleev26d1bc82019-08-30 11:57:18 +010037 SCOPED_TRACE(message + " [getSupportedOperations_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010038
Lev Proleev26d1bc82019-08-30 11:57:18 +010039 Return<void> ret = device->getSupportedOperations_1_3(
Lev Proleev13fdfcd2019-08-30 11:35:34 +010040 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
41 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
42 });
43 EXPECT_TRUE(ret.isOk());
44}
45
46static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
47 const Model& model, ExecutionPreference preference) {
Lev Proleev26d1bc82019-08-30 11:57:18 +010048 SCOPED_TRACE(message + " [prepareModel_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010049
50 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
51 Return<ErrorStatus> prepareLaunchStatus =
Lev Proleev26d1bc82019-08-30 11:57:18 +010052 device->prepareModel_1_3(model, preference, hidl_vec<hidl_handle>(),
Lev Proleev13fdfcd2019-08-30 11:35:34 +010053 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
54 ASSERT_TRUE(prepareLaunchStatus.isOk());
55 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
56
57 preparedModelCallback->wait();
58 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
59 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wang1b3f4262019-10-25 12:07:17 -070060 sp<IPreparedModel> preparedModel = getPreparedModel_1_3(preparedModelCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010061 ASSERT_EQ(nullptr, preparedModel.get());
62}
63
64static bool validExecutionPreference(ExecutionPreference preference) {
65 return preference == ExecutionPreference::LOW_POWER ||
66 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
67 preference == ExecutionPreference::SUSTAINED_SPEED;
68}
69
70// Primary validation function. This function will take a valid model, apply a
71// mutation to it to invalidate the model, then pass it to interface calls that
72// use the model. Note that the model here is passed by value, and any mutation
73// to the model does not leave this function.
74static void validate(const sp<IDevice>& device, const std::string& message, Model model,
75 const std::function<void(Model*)>& mutation,
76 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
77 mutation(&model);
78 if (validExecutionPreference(preference)) {
79 validateGetSupportedOperations(device, message, model);
80 }
81 validatePrepareModel(device, message, model, preference);
82}
83
84static uint32_t addOperand(Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +000085 return hidl_vec_push_back(&model->main.operands,
Lev Proleev13fdfcd2019-08-30 11:35:34 +010086 {
87 .type = OperandType::INT32,
88 .dimensions = {},
89 .numberOfConsumers = 0,
90 .scale = 0.0f,
91 .zeroPoint = 0,
Slava Shklyaevf8124a82019-12-13 12:24:35 +000092 .lifetime = OperandLifeTime::SUBGRAPH_INPUT,
Lev Proleev13fdfcd2019-08-30 11:35:34 +010093 .location = {.poolIndex = 0, .offset = 0, .length = 0},
94 });
95}
96
97static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
98 uint32_t index = addOperand(model);
Slava Shklyaevf8124a82019-12-13 12:24:35 +000099 model->main.operands[index].numberOfConsumers = 1;
100 model->main.operands[index].lifetime = lifetime;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100101 return index;
102}
103
104///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
105
106static const uint32_t invalidOperandTypes[] = {
107 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
108 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
109 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
110 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
111};
112
113static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000114 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100115 for (uint32_t invalidOperandType : invalidOperandTypes) {
116 const std::string message = "mutateOperandTypeTest: operand " +
117 std::to_string(operand) + " set to value " +
118 std::to_string(invalidOperandType);
119 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000120 model->main.operands[operand].type = static_cast<OperandType>(invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100121 });
122 }
123 }
124}
125
126///////////////////////// VALIDATE OPERAND RANK /////////////////////////
127
128static uint32_t getInvalidRank(OperandType type) {
129 switch (type) {
130 case OperandType::FLOAT16:
131 case OperandType::FLOAT32:
132 case OperandType::INT32:
133 case OperandType::UINT32:
134 case OperandType::BOOL:
135 return 1;
136 case OperandType::TENSOR_BOOL8:
137 case OperandType::TENSOR_FLOAT16:
138 case OperandType::TENSOR_FLOAT32:
139 case OperandType::TENSOR_INT32:
140 case OperandType::TENSOR_QUANT8_ASYMM:
141 case OperandType::TENSOR_QUANT8_SYMM:
142 case OperandType::TENSOR_QUANT16_ASYMM:
143 case OperandType::TENSOR_QUANT16_SYMM:
144 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
145 return 0;
146 default:
147 return 0;
148 }
149}
150
151static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000152 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
153 const uint32_t invalidRank = getInvalidRank(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100154 if (invalidRank == 0) {
155 continue;
156 }
157 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
158 " has rank of " + std::to_string(invalidRank);
159 validate(device, message, model, [operand, invalidRank](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000160 model->main.operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100161 });
162 }
163}
164
165///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
166
167static float getInvalidScale(OperandType type) {
168 switch (type) {
169 case OperandType::FLOAT16:
170 case OperandType::FLOAT32:
171 case OperandType::INT32:
172 case OperandType::UINT32:
173 case OperandType::BOOL:
174 case OperandType::TENSOR_BOOL8:
175 case OperandType::TENSOR_FLOAT16:
176 case OperandType::TENSOR_FLOAT32:
177 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
178 return 1.0f;
179 case OperandType::TENSOR_INT32:
180 return -1.0f;
181 case OperandType::TENSOR_QUANT8_SYMM:
182 case OperandType::TENSOR_QUANT8_ASYMM:
183 case OperandType::TENSOR_QUANT16_ASYMM:
184 case OperandType::TENSOR_QUANT16_SYMM:
185 return 0.0f;
186 default:
187 return 0.0f;
188 }
189}
190
191static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000192 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
193 const float invalidScale = getInvalidScale(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100194 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
195 " has scale of " + std::to_string(invalidScale);
196 validate(device, message, model, [operand, invalidScale](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000197 model->main.operands[operand].scale = invalidScale;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100198 });
199 }
200}
201
202///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
203
204static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
205 switch (type) {
206 case OperandType::FLOAT16:
207 case OperandType::FLOAT32:
208 case OperandType::INT32:
209 case OperandType::UINT32:
210 case OperandType::BOOL:
211 case OperandType::TENSOR_BOOL8:
212 case OperandType::TENSOR_FLOAT16:
213 case OperandType::TENSOR_FLOAT32:
214 case OperandType::TENSOR_INT32:
215 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
216 return {1};
217 case OperandType::TENSOR_QUANT8_ASYMM:
218 return {-1, 256};
219 case OperandType::TENSOR_QUANT8_SYMM:
220 return {-129, -1, 1, 128};
221 case OperandType::TENSOR_QUANT16_ASYMM:
222 return {-1, 65536};
223 case OperandType::TENSOR_QUANT16_SYMM:
224 return {-32769, -1, 1, 32768};
225 default:
226 return {};
227 }
228}
229
230static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000231 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100232 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000233 getInvalidZeroPoints(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100234 for (int32_t invalidZeroPoint : invalidZeroPoints) {
235 const std::string message = "mutateOperandZeroPointTest: operand " +
236 std::to_string(operand) + " has zero point of " +
237 std::to_string(invalidZeroPoint);
238 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000239 model->main.operands[operand].zeroPoint = invalidZeroPoint;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100240 });
241 }
242 }
243}
244
245///////////////////////// VALIDATE EXTRA ??? /////////////////////////
246
247// TODO: Operand::lifetime
248// TODO: Operand::location
249
250///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
251
252static void mutateOperand(Operand* operand, OperandType type) {
253 Operand newOperand = *operand;
254 newOperand.type = type;
255 switch (type) {
256 case OperandType::FLOAT16:
257 case OperandType::FLOAT32:
258 case OperandType::INT32:
259 case OperandType::UINT32:
260 case OperandType::BOOL:
261 newOperand.dimensions = hidl_vec<uint32_t>();
262 newOperand.scale = 0.0f;
263 newOperand.zeroPoint = 0;
264 break;
265 case OperandType::TENSOR_BOOL8:
266 case OperandType::TENSOR_FLOAT16:
267 case OperandType::TENSOR_FLOAT32:
268 newOperand.dimensions =
269 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
270 newOperand.scale = 0.0f;
271 newOperand.zeroPoint = 0;
272 break;
273 case OperandType::TENSOR_INT32:
274 newOperand.dimensions =
275 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
276 newOperand.zeroPoint = 0;
277 break;
278 case OperandType::TENSOR_QUANT8_ASYMM:
279 case OperandType::TENSOR_QUANT8_SYMM:
280 case OperandType::TENSOR_QUANT16_ASYMM:
281 case OperandType::TENSOR_QUANT16_SYMM:
282 newOperand.dimensions =
283 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
284 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
285 break;
286 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
287 newOperand.dimensions =
288 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
289 newOperand.scale = 0.0f;
290 newOperand.zeroPoint = 0;
291
292 SymmPerChannelQuantParams channelQuant;
293 channelQuant.channelDim = 0;
294 channelQuant.scales = hidl_vec<float>(
295 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
296 : 0);
297 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
298 channelQuant.scales[i] = 1.0f;
299 }
300 newOperand.extraParams.channelQuant(std::move(channelQuant));
301 } break;
302 case OperandType::OEM:
303 case OperandType::TENSOR_OEM_BYTE:
304 default:
305 break;
306 }
307 *operand = newOperand;
308}
309
310static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
311 // Do not test OEM types
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000312 if (type == model.main.operands[operand].type || type == OperandType::OEM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100313 type == OperandType::TENSOR_OEM_BYTE) {
314 return true;
315 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000316 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100317 // Skip mutateOperationOperandTypeTest for the following operations.
318 // - LSH_PROJECTION's second argument is allowed to have any type.
319 // - ARGMIN and ARGMAX's first argument can be any of
320 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
321 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
322 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
323 // - DEQUANTIZE input can be any of
Lev Proleevae643ae2019-12-05 16:57:30 +0000324 // TENSOR_(QUANT8_ASYMM|QUANT8_ASYMM_SIGNED|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL),
325 // output can be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100326 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
327 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
328 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
329 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
330 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevda779f32020-01-02 17:49:03 +0000331 // - AXIS_ALIGNED_BBOX_TRANSFORM bounding boxes (arg 1) can be of
332 // TENSOR_QUANT8_ASYMM or TENSOR_QUANT8_ASYMM_SIGNED.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100333 switch (operation.type) {
334 case OperationType::LSH_PROJECTION: {
335 if (operand == operation.inputs[1]) {
336 return true;
337 }
338 } break;
339 case OperationType::CAST:
340 case OperationType::ARGMAX:
341 case OperationType::ARGMIN: {
342 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000343 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
344 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100345 return true;
346 }
347 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000348 case OperationType::QUANTIZE: {
349 if (operand == operation.inputs[0] &&
350 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
351 return true;
352 }
353 if (operand == operation.outputs[0] &&
354 (type == OperandType::TENSOR_QUANT8_ASYMM ||
355 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
356 return true;
357 }
358 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100359 case OperationType::RANDOM_MULTINOMIAL: {
360 if (operand == operation.inputs[0] &&
361 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
362 return true;
363 }
364 } break;
365 case OperationType::DEQUANTIZE: {
366 if (operand == operation.inputs[0] &&
367 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000368 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100369 type == OperandType::TENSOR_QUANT8_SYMM ||
370 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
371 return true;
372 }
373 if (operand == operation.outputs[0] &&
374 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
375 return true;
376 }
377 } break;
378 case OperationType::TRANSPOSE_CONV_2D:
379 case OperationType::GROUPED_CONV_2D:
380 case OperationType::DEPTHWISE_CONV_2D:
381 case OperationType::CONV_2D: {
382 if (operand == operation.inputs[1] &&
383 (type == OperandType::TENSOR_QUANT8_ASYMM ||
384 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
385 return true;
386 }
387 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000388 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
389 if (operand == operation.inputs[1] &&
390 (type == OperandType::TENSOR_QUANT8_ASYMM ||
391 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
392 return true;
393 }
394 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100395 default:
396 break;
397 }
398 }
399 return false;
400}
401
402static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000403 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100404 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
405 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
406 continue;
407 }
408 const std::string message = "mutateOperationOperandTypeTest: operand " +
409 std::to_string(operand) + " set to type " +
410 toString(invalidOperandType);
411 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000412 mutateOperand(&model->main.operands[operand], invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100413 });
414 }
415 }
416}
417
418///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
419
420static const uint32_t invalidOperationTypes[] = {
421 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
422 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
423 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
424};
425
426static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000427 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100428 for (uint32_t invalidOperationType : invalidOperationTypes) {
429 const std::string message = "mutateOperationTypeTest: operation " +
430 std::to_string(operation) + " set to value " +
431 std::to_string(invalidOperationType);
432 validate(device, message, model, [operation, invalidOperationType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000433 model->main.operations[operation].type =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100434 static_cast<OperationType>(invalidOperationType);
435 });
436 }
437 }
438}
439
440///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
441
442static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000443 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
444 const uint32_t invalidOperand = model.main.operands.size();
445 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100446 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
447 std::to_string(operation) + " input " +
448 std::to_string(input);
449 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000450 model->main.operations[operation].inputs[input] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100451 });
452 }
453 }
454}
455
456///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
457
458static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000459 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
460 const uint32_t invalidOperand = model.main.operands.size();
461 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
462 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100463 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
464 std::to_string(operation) + " output " +
465 std::to_string(output);
466 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000467 model->main.operations[operation].outputs[output] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100468 });
469 }
470 }
471}
472
473///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
474
475static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
476 if (vec) {
477 // remove elements matching "value"
478 auto last = std::remove(vec->begin(), vec->end(), value);
479 vec->resize(std::distance(vec->begin(), last));
480
481 // decrement elements exceeding "value"
482 std::transform(vec->begin(), vec->end(), vec->begin(),
483 [value](uint32_t v) { return v > value ? v-- : v; });
484 }
485}
486
487static void removeOperand(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000488 hidl_vec_removeAt(&model->main.operands, index);
489 for (Operation& operation : model->main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100490 removeValueAndDecrementGreaterValues(&operation.inputs, index);
491 removeValueAndDecrementGreaterValues(&operation.outputs, index);
492 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000493 removeValueAndDecrementGreaterValues(&model->main.inputIndexes, index);
494 removeValueAndDecrementGreaterValues(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100495}
496
497static bool removeOperandSkip(size_t operand, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000498 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100499 // Skip removeOperandTest for the following operations.
500 // - SPLIT's outputs are not checked during prepareModel.
501 if (operation.type == OperationType::SPLIT) {
502 for (const size_t outOprand : operation.outputs) {
503 if (operand == outOprand) {
504 return true;
505 }
506 }
507 }
508 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
509 // outputs depending on their mergeOutputs parameter.
510 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
511 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
512 for (const size_t outOprand : operation.outputs) {
513 if (operand == outOprand) {
514 return true;
515 }
516 }
517 }
518 }
519 return false;
520}
521
522static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000523 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100524 if (removeOperandSkip(operand, model)) {
525 continue;
526 }
527 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
528 validate(device, message, model,
529 [operand](Model* model) { removeOperand(model, operand); });
530 }
531}
532
533///////////////////////// REMOVE OPERATION /////////////////////////
534
535static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000536 for (uint32_t operand : model->main.operations[index].inputs) {
537 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100538 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000539 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100540}
541
542static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000543 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100544 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
545 validate(device, message, model,
546 [operation](Model* model) { removeOperation(model, operation); });
547 }
548}
549
550///////////////////////// REMOVE OPERATION INPUT /////////////////////////
551
552static bool removeOperationInputSkip(const Operation& op, size_t input) {
553 // Skip removeOperationInputTest for the following operations.
554 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
555 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
556 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
557 // layout parameter.
558 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
559 // parameter.
560 switch (op.type) {
561 case OperationType::CONCATENATION: {
562 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
563 return true;
564 }
565 } break;
566 case OperationType::DEPTHWISE_CONV_2D: {
567 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
568 return true;
569 }
570 } break;
571 case OperationType::CONV_2D:
572 case OperationType::AVERAGE_POOL_2D:
573 case OperationType::MAX_POOL_2D:
574 case OperationType::L2_POOL_2D: {
575 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
576 return true;
577 }
578 } break;
579 case OperationType::RESIZE_BILINEAR: {
580 if (op.inputs.size() == 4 && input == 3) {
581 return true;
582 }
583 } break;
584 case OperationType::SPACE_TO_DEPTH:
585 case OperationType::DEPTH_TO_SPACE:
586 case OperationType::BATCH_TO_SPACE_ND: {
587 if (op.inputs.size() == 3 && input == 2) {
588 return true;
589 }
590 } break;
591 case OperationType::SPACE_TO_BATCH_ND: {
592 if (op.inputs.size() == 4 && input == 3) {
593 return true;
594 }
595 } break;
596 case OperationType::L2_NORMALIZATION: {
597 if (op.inputs.size() == 2 && input == 1) {
598 return true;
599 }
600 } break;
601 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
602 if (op.inputs.size() == 6 && input == 5) {
603 return true;
604 }
605 } break;
606 case OperationType::SOFTMAX: {
607 if (op.inputs.size() == 3 && input == 2) {
608 return true;
609 }
610 } break;
611 default:
612 break;
613 }
614 return false;
615}
616
617static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000618 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
619 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
620 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100621 if (removeOperationInputSkip(op, input)) {
622 continue;
623 }
624 const std::string message = "removeOperationInputTest: operation " +
625 std::to_string(operation) + ", input " +
626 std::to_string(input);
627 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000628 uint32_t operand = model->main.operations[operation].inputs[input];
629 model->main.operands[operand].numberOfConsumers--;
630 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100631 });
632 }
633 }
634}
635
636///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
637
638static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000639 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
640 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
641 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100642 const std::string message = "removeOperationOutputTest: operation " +
643 std::to_string(operation) + ", output " +
644 std::to_string(output);
645 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000646 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100647 });
648 }
649 }
650}
651
652///////////////////////// MODEL VALIDATION /////////////////////////
653
654// TODO: remove model input
655// TODO: remove model output
656// TODO: add unused operation
657
658///////////////////////// ADD OPERATION INPUT /////////////////////////
659
660static bool addOperationInputSkip(const Operation& op) {
661 // Skip addOperationInputTest for the following operations.
662 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
663 // parameter.
664 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
665 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
666 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
667 return true;
668 }
669 return false;
670}
671
672static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000673 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
674 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100675 continue;
676 }
677 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
678 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000679 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
680 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
681 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100682 });
683 }
684}
685
686///////////////////////// ADD OPERATION OUTPUT /////////////////////////
687
688static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000689 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100690 const std::string message =
691 "addOperationOutputTest: operation " + std::to_string(operation);
692 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000693 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
694 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
695 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100696 });
697 }
698}
699
700///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
701
702static const int32_t invalidExecutionPreferences[] = {
703 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
704 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
705};
706
707static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
708 for (int32_t preference : invalidExecutionPreferences) {
709 const std::string message =
710 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
711 validate(
712 device, message, model, [](Model*) {},
713 static_cast<ExecutionPreference>(preference));
714 }
715}
716
717////////////////////////// ENTRY POINT //////////////////////////////
718
719void validateModel(const sp<IDevice>& device, 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
Lev Proleev26d1bc82019-08-30 11:57:18 +0100737} // namespace android::hardware::neuralnetworks::V1_3::vts::functional