blob: 8f2d4b7f193c31fa3fc89f80fb0020ddefef8caf [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"
Michael Butler79a41d72019-12-11 19:08:08 -080021#include "1.3/Utils.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010022#include "GeneratedTestHarness.h"
23#include "VtsHalNeuralnetworks.h"
24
Lev Proleev26d1bc82019-08-30 11:57:18 +010025namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010026
Xusong Wangcc47dff2019-10-23 10:35:07 -070027using implementation::PreparedModelCallback;
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,
Michael Butlerff7d6c52020-02-13 16:37:22 -080047 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();
Michael Butler79a41d72019-12-11 19:08:08 -080051 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_3(
Michael Butlerff7d6c52020-02-13 16:37:22 -080052 model, preference, kDefaultPriority, {}, hidl_vec<hidl_handle>(),
Michael Butler79a41d72019-12-11 19:08:08 -080053 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010054 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,
Michael Butlerff7d6c52020-02-13 16:37:22 -080076 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010077 mutation(&model);
Michael Butlerff7d6c52020-02-13 16:37:22 -080078 if (validExecutionPreference(preference)) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010079 validateGetSupportedOperations(device, message, model);
80 }
Michael Butlerff7d6c52020-02-13 16:37:22 -080081 validatePrepareModel(device, message, model, preference);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010082}
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:
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000178 case OperandType::SUBGRAPH:
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100179 return 1.0f;
180 case OperandType::TENSOR_INT32:
181 return -1.0f;
182 case OperandType::TENSOR_QUANT8_SYMM:
183 case OperandType::TENSOR_QUANT8_ASYMM:
184 case OperandType::TENSOR_QUANT16_ASYMM:
185 case OperandType::TENSOR_QUANT16_SYMM:
186 return 0.0f;
187 default:
188 return 0.0f;
189 }
190}
191
192static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000193 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
194 const float invalidScale = getInvalidScale(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100195 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
196 " has scale of " + std::to_string(invalidScale);
197 validate(device, message, model, [operand, invalidScale](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000198 model->main.operands[operand].scale = invalidScale;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100199 });
200 }
201}
202
203///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
204
205static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
206 switch (type) {
207 case OperandType::FLOAT16:
208 case OperandType::FLOAT32:
209 case OperandType::INT32:
210 case OperandType::UINT32:
211 case OperandType::BOOL:
212 case OperandType::TENSOR_BOOL8:
213 case OperandType::TENSOR_FLOAT16:
214 case OperandType::TENSOR_FLOAT32:
215 case OperandType::TENSOR_INT32:
216 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000217 case OperandType::SUBGRAPH:
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100218 return {1};
219 case OperandType::TENSOR_QUANT8_ASYMM:
220 return {-1, 256};
221 case OperandType::TENSOR_QUANT8_SYMM:
222 return {-129, -1, 1, 128};
223 case OperandType::TENSOR_QUANT16_ASYMM:
224 return {-1, 65536};
225 case OperandType::TENSOR_QUANT16_SYMM:
226 return {-32769, -1, 1, 32768};
227 default:
228 return {};
229 }
230}
231
232static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000233 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100234 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000235 getInvalidZeroPoints(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100236 for (int32_t invalidZeroPoint : invalidZeroPoints) {
237 const std::string message = "mutateOperandZeroPointTest: operand " +
238 std::to_string(operand) + " has zero point of " +
239 std::to_string(invalidZeroPoint);
240 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000241 model->main.operands[operand].zeroPoint = invalidZeroPoint;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100242 });
243 }
244 }
245}
246
247///////////////////////// VALIDATE EXTRA ??? /////////////////////////
248
249// TODO: Operand::lifetime
250// TODO: Operand::location
251
252///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
253
254static void mutateOperand(Operand* operand, OperandType type) {
255 Operand newOperand = *operand;
256 newOperand.type = type;
257 switch (type) {
258 case OperandType::FLOAT16:
259 case OperandType::FLOAT32:
260 case OperandType::INT32:
261 case OperandType::UINT32:
262 case OperandType::BOOL:
263 newOperand.dimensions = hidl_vec<uint32_t>();
264 newOperand.scale = 0.0f;
265 newOperand.zeroPoint = 0;
266 break;
267 case OperandType::TENSOR_BOOL8:
268 case OperandType::TENSOR_FLOAT16:
269 case OperandType::TENSOR_FLOAT32:
270 newOperand.dimensions =
271 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
272 newOperand.scale = 0.0f;
273 newOperand.zeroPoint = 0;
274 break;
275 case OperandType::TENSOR_INT32:
276 newOperand.dimensions =
277 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
278 newOperand.zeroPoint = 0;
279 break;
280 case OperandType::TENSOR_QUANT8_ASYMM:
281 case OperandType::TENSOR_QUANT8_SYMM:
282 case OperandType::TENSOR_QUANT16_ASYMM:
283 case OperandType::TENSOR_QUANT16_SYMM:
284 newOperand.dimensions =
285 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
286 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
287 break;
288 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
289 newOperand.dimensions =
290 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
291 newOperand.scale = 0.0f;
292 newOperand.zeroPoint = 0;
293
294 SymmPerChannelQuantParams channelQuant;
295 channelQuant.channelDim = 0;
296 channelQuant.scales = hidl_vec<float>(
297 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
298 : 0);
299 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
300 channelQuant.scales[i] = 1.0f;
301 }
302 newOperand.extraParams.channelQuant(std::move(channelQuant));
303 } break;
304 case OperandType::OEM:
305 case OperandType::TENSOR_OEM_BYTE:
306 default:
307 break;
308 }
309 *operand = newOperand;
310}
311
312static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
313 // Do not test OEM types
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000314 if (type == model.main.operands[operand].type || type == OperandType::OEM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100315 type == OperandType::TENSOR_OEM_BYTE) {
316 return true;
317 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000318 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100319 // Skip mutateOperationOperandTypeTest for the following operations.
320 // - LSH_PROJECTION's second argument is allowed to have any type.
321 // - ARGMIN and ARGMAX's first argument can be any of
322 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
323 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
324 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
325 // - DEQUANTIZE input can be any of
Lev Proleevae643ae2019-12-05 16:57:30 +0000326 // TENSOR_(QUANT8_ASYMM|QUANT8_ASYMM_SIGNED|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL),
327 // output can be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100328 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
329 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
330 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
331 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
332 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevda779f32020-01-02 17:49:03 +0000333 // - AXIS_ALIGNED_BBOX_TRANSFORM bounding boxes (arg 1) can be of
334 // TENSOR_QUANT8_ASYMM or TENSOR_QUANT8_ASYMM_SIGNED.
Lev Proleev53a51cb2020-01-20 18:54:46 +0000335 // - RANK's input can have any TENSOR_* type.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100336 switch (operation.type) {
337 case OperationType::LSH_PROJECTION: {
338 if (operand == operation.inputs[1]) {
339 return true;
340 }
341 } break;
342 case OperationType::CAST:
343 case OperationType::ARGMAX:
344 case OperationType::ARGMIN: {
345 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000346 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
347 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100348 return true;
349 }
350 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000351 case OperationType::QUANTIZE: {
352 if (operand == operation.inputs[0] &&
353 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
354 return true;
355 }
356 if (operand == operation.outputs[0] &&
357 (type == OperandType::TENSOR_QUANT8_ASYMM ||
358 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
359 return true;
360 }
361 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100362 case OperationType::RANDOM_MULTINOMIAL: {
363 if (operand == operation.inputs[0] &&
364 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
365 return true;
366 }
367 } break;
368 case OperationType::DEQUANTIZE: {
369 if (operand == operation.inputs[0] &&
370 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000371 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100372 type == OperandType::TENSOR_QUANT8_SYMM ||
373 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
374 return true;
375 }
376 if (operand == operation.outputs[0] &&
377 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
378 return true;
379 }
380 } break;
381 case OperationType::TRANSPOSE_CONV_2D:
382 case OperationType::GROUPED_CONV_2D:
383 case OperationType::DEPTHWISE_CONV_2D:
384 case OperationType::CONV_2D: {
385 if (operand == operation.inputs[1] &&
386 (type == OperandType::TENSOR_QUANT8_ASYMM ||
387 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
388 return true;
389 }
390 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000391 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
392 if (operand == operation.inputs[1] &&
393 (type == OperandType::TENSOR_QUANT8_ASYMM ||
394 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
395 return true;
396 }
397 } break;
Lev Proleev53a51cb2020-01-20 18:54:46 +0000398 case OperationType::RANK: {
399 if (operand == operation.inputs[0] &&
400 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
401 type == OperandType::TENSOR_INT32 ||
402 type == OperandType::TENSOR_QUANT8_ASYMM ||
403 type == OperandType::TENSOR_QUANT16_SYMM ||
404 type == OperandType::TENSOR_BOOL8 ||
405 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
406 type == OperandType::TENSOR_QUANT16_ASYMM ||
407 type == OperandType::TENSOR_QUANT8_SYMM ||
408 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
409 return true;
410 }
411 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100412 default:
413 break;
414 }
415 }
416 return false;
417}
418
419static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000420 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100421 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
422 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
423 continue;
424 }
425 const std::string message = "mutateOperationOperandTypeTest: operand " +
426 std::to_string(operand) + " set to type " +
427 toString(invalidOperandType);
428 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000429 mutateOperand(&model->main.operands[operand], invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100430 });
431 }
432 }
433}
434
435///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
436
437static const uint32_t invalidOperationTypes[] = {
438 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
439 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
440 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
441};
442
443static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000444 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100445 for (uint32_t invalidOperationType : invalidOperationTypes) {
446 const std::string message = "mutateOperationTypeTest: operation " +
447 std::to_string(operation) + " set to value " +
448 std::to_string(invalidOperationType);
449 validate(device, message, model, [operation, invalidOperationType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000450 model->main.operations[operation].type =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100451 static_cast<OperationType>(invalidOperationType);
452 });
453 }
454 }
455}
456
457///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
458
459static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000460 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
461 const uint32_t invalidOperand = model.main.operands.size();
462 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100463 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
464 std::to_string(operation) + " input " +
465 std::to_string(input);
466 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000467 model->main.operations[operation].inputs[input] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100468 });
469 }
470 }
471}
472
473///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
474
475static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000476 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
477 const uint32_t invalidOperand = model.main.operands.size();
478 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
479 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100480 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
481 std::to_string(operation) + " output " +
482 std::to_string(output);
483 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000484 model->main.operations[operation].outputs[output] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100485 });
486 }
487 }
488}
489
490///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
491
492static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
493 if (vec) {
494 // remove elements matching "value"
495 auto last = std::remove(vec->begin(), vec->end(), value);
496 vec->resize(std::distance(vec->begin(), last));
497
498 // decrement elements exceeding "value"
499 std::transform(vec->begin(), vec->end(), vec->begin(),
500 [value](uint32_t v) { return v > value ? v-- : v; });
501 }
502}
503
504static void removeOperand(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000505 hidl_vec_removeAt(&model->main.operands, index);
506 for (Operation& operation : model->main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100507 removeValueAndDecrementGreaterValues(&operation.inputs, index);
508 removeValueAndDecrementGreaterValues(&operation.outputs, index);
509 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000510 removeValueAndDecrementGreaterValues(&model->main.inputIndexes, index);
511 removeValueAndDecrementGreaterValues(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100512}
513
514static bool removeOperandSkip(size_t operand, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000515 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100516 // Skip removeOperandTest for the following operations.
517 // - SPLIT's outputs are not checked during prepareModel.
518 if (operation.type == OperationType::SPLIT) {
519 for (const size_t outOprand : operation.outputs) {
520 if (operand == outOprand) {
521 return true;
522 }
523 }
524 }
Lev Proleev17689aa2020-01-30 17:40:13 +0000525 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have
526 // either one, two, three or four outputs depending on their
527 // mergeOutputs parameter and if state outputs are provided.
528 // UNIDIRECTIONAL_SEQUENCE_LSTM and UNIDIRECTIONAL_SEQUENCE_RNN can have
529 // either one or three outputs depending on whether state outputs are
530 // provided.
531 if (operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM ||
532 operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_RNN ||
533 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100534 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
535 for (const size_t outOprand : operation.outputs) {
536 if (operand == outOprand) {
537 return true;
538 }
539 }
540 }
541 }
542 return false;
543}
544
545static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000546 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100547 if (removeOperandSkip(operand, model)) {
548 continue;
549 }
550 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
551 validate(device, message, model,
552 [operand](Model* model) { removeOperand(model, operand); });
553 }
554}
555
556///////////////////////// REMOVE OPERATION /////////////////////////
557
558static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000559 for (uint32_t operand : model->main.operations[index].inputs) {
560 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100561 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000562 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100563}
564
565static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000566 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100567 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
568 validate(device, message, model,
569 [operation](Model* model) { removeOperation(model, operation); });
570 }
571}
572
573///////////////////////// REMOVE OPERATION INPUT /////////////////////////
574
575static bool removeOperationInputSkip(const Operation& op, size_t input) {
576 // Skip removeOperationInputTest for the following operations.
577 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
578 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
579 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
580 // layout parameter.
581 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
582 // parameter.
583 switch (op.type) {
584 case OperationType::CONCATENATION: {
585 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
586 return true;
587 }
588 } break;
589 case OperationType::DEPTHWISE_CONV_2D: {
590 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
591 return true;
592 }
593 } break;
594 case OperationType::CONV_2D:
595 case OperationType::AVERAGE_POOL_2D:
596 case OperationType::MAX_POOL_2D:
597 case OperationType::L2_POOL_2D: {
598 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
599 return true;
600 }
601 } break;
602 case OperationType::RESIZE_BILINEAR: {
603 if (op.inputs.size() == 4 && input == 3) {
604 return true;
605 }
606 } break;
607 case OperationType::SPACE_TO_DEPTH:
608 case OperationType::DEPTH_TO_SPACE:
609 case OperationType::BATCH_TO_SPACE_ND: {
610 if (op.inputs.size() == 3 && input == 2) {
611 return true;
612 }
613 } break;
614 case OperationType::SPACE_TO_BATCH_ND: {
615 if (op.inputs.size() == 4 && input == 3) {
616 return true;
617 }
618 } break;
619 case OperationType::L2_NORMALIZATION: {
620 if (op.inputs.size() == 2 && input == 1) {
621 return true;
622 }
623 } break;
624 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
625 if (op.inputs.size() == 6 && input == 5) {
626 return true;
627 }
628 } break;
629 case OperationType::SOFTMAX: {
630 if (op.inputs.size() == 3 && input == 2) {
631 return true;
632 }
633 } break;
634 default:
635 break;
636 }
637 return false;
638}
639
640static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000641 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
642 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
643 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100644 if (removeOperationInputSkip(op, input)) {
645 continue;
646 }
647 const std::string message = "removeOperationInputTest: operation " +
648 std::to_string(operation) + ", input " +
649 std::to_string(input);
650 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000651 uint32_t operand = model->main.operations[operation].inputs[input];
652 model->main.operands[operand].numberOfConsumers--;
653 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100654 });
655 }
656 }
657}
658
659///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
660
661static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000662 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
663 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
664 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100665 const std::string message = "removeOperationOutputTest: operation " +
666 std::to_string(operation) + ", output " +
667 std::to_string(output);
668 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000669 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100670 });
671 }
672 }
673}
674
675///////////////////////// MODEL VALIDATION /////////////////////////
676
677// TODO: remove model input
678// TODO: remove model output
679// TODO: add unused operation
680
681///////////////////////// ADD OPERATION INPUT /////////////////////////
682
683static bool addOperationInputSkip(const Operation& op) {
684 // Skip addOperationInputTest for the following operations.
685 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
686 // parameter.
687 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
688 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
689 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
690 return true;
691 }
692 return false;
693}
694
695static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000696 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
697 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100698 continue;
699 }
700 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
701 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000702 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
703 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
704 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100705 });
706 }
707}
708
709///////////////////////// ADD OPERATION OUTPUT /////////////////////////
710
711static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000712 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100713 const std::string message =
714 "addOperationOutputTest: operation " + std::to_string(operation);
715 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000716 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
717 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
718 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100719 });
720 }
721}
722
723///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
724
725static const int32_t invalidExecutionPreferences[] = {
726 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
727 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
728};
729
730static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
731 for (int32_t preference : invalidExecutionPreferences) {
732 const std::string message =
733 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
734 validate(
735 device, message, model, [](Model*) {},
736 static_cast<ExecutionPreference>(preference));
737 }
738}
739
740////////////////////////// ENTRY POINT //////////////////////////////
741
Michael Butlerff7d6c52020-02-13 16:37:22 -0800742void validateModel(const sp<IDevice>& device, const Model& model) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100743 mutateOperandTypeTest(device, model);
744 mutateOperandRankTest(device, model);
745 mutateOperandScaleTest(device, model);
746 mutateOperandZeroPointTest(device, model);
747 mutateOperationOperandTypeTest(device, model);
748 mutateOperationTypeTest(device, model);
749 mutateOperationInputOperandIndexTest(device, model);
750 mutateOperationOutputOperandIndexTest(device, model);
751 removeOperandTest(device, model);
752 removeOperationTest(device, model);
753 removeOperationInputTest(device, model);
754 removeOperationOutputTest(device, model);
755 addOperationInputTest(device, model);
756 addOperationOutputTest(device, model);
757 mutateExecutionPreferenceTest(device, model);
758}
759
Lev Proleev26d1bc82019-08-30 11:57:18 +0100760} // namespace android::hardware::neuralnetworks::V1_3::vts::functional