blob: b9ea430694ee514e2f205ae6269332af8b15d50b [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 Butler95899b32020-01-07 14:52:44 -080047 const Model& model, ExecutionPreference preference,
48 bool testDeadline) {
Lev Proleev26d1bc82019-08-30 11:57:18 +010049 SCOPED_TRACE(message + " [prepareModel_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010050
Michael Butler95899b32020-01-07 14:52:44 -080051 OptionalTimePoint deadline;
52 if (testDeadline) {
Michael Butlerc3e1a292020-02-04 16:15:04 -080053 deadline.nanosecondsSinceEpoch(std::numeric_limits<uint64_t>::max());
Michael Butler95899b32020-01-07 14:52:44 -080054 }
55
Lev Proleev13fdfcd2019-08-30 11:35:34 +010056 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
Michael Butler79a41d72019-12-11 19:08:08 -080057 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_3(
Michael Butler95899b32020-01-07 14:52:44 -080058 model, preference, kDefaultPriority, deadline, hidl_vec<hidl_handle>(),
Michael Butler79a41d72019-12-11 19:08:08 -080059 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010060 ASSERT_TRUE(prepareLaunchStatus.isOk());
61 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
62
63 preparedModelCallback->wait();
64 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
65 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wang1b3f4262019-10-25 12:07:17 -070066 sp<IPreparedModel> preparedModel = getPreparedModel_1_3(preparedModelCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010067 ASSERT_EQ(nullptr, preparedModel.get());
68}
69
70static bool validExecutionPreference(ExecutionPreference preference) {
71 return preference == ExecutionPreference::LOW_POWER ||
72 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
73 preference == ExecutionPreference::SUSTAINED_SPEED;
74}
75
76// Primary validation function. This function will take a valid model, apply a
77// mutation to it to invalidate the model, then pass it to interface calls that
78// use the model. Note that the model here is passed by value, and any mutation
79// to the model does not leave this function.
80static void validate(const sp<IDevice>& device, const std::string& message, Model model,
81 const std::function<void(Model*)>& mutation,
Michael Butler95899b32020-01-07 14:52:44 -080082 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER,
83 bool testDeadline = false) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010084 mutation(&model);
Michael Butler95899b32020-01-07 14:52:44 -080085 if (validExecutionPreference(preference) && !testDeadline) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010086 validateGetSupportedOperations(device, message, model);
87 }
Michael Butler95899b32020-01-07 14:52:44 -080088 validatePrepareModel(device, message, model, preference, testDeadline);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010089}
90
91static uint32_t addOperand(Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +000092 return hidl_vec_push_back(&model->main.operands,
Lev Proleev13fdfcd2019-08-30 11:35:34 +010093 {
94 .type = OperandType::INT32,
95 .dimensions = {},
96 .numberOfConsumers = 0,
97 .scale = 0.0f,
98 .zeroPoint = 0,
Slava Shklyaevf8124a82019-12-13 12:24:35 +000099 .lifetime = OperandLifeTime::SUBGRAPH_INPUT,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100100 .location = {.poolIndex = 0, .offset = 0, .length = 0},
101 });
102}
103
104static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
105 uint32_t index = addOperand(model);
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000106 model->main.operands[index].numberOfConsumers = 1;
107 model->main.operands[index].lifetime = lifetime;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100108 return index;
109}
110
111///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
112
113static const uint32_t invalidOperandTypes[] = {
114 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
115 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
116 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
117 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
118};
119
120static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000121 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100122 for (uint32_t invalidOperandType : invalidOperandTypes) {
123 const std::string message = "mutateOperandTypeTest: operand " +
124 std::to_string(operand) + " set to value " +
125 std::to_string(invalidOperandType);
126 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000127 model->main.operands[operand].type = static_cast<OperandType>(invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100128 });
129 }
130 }
131}
132
133///////////////////////// VALIDATE OPERAND RANK /////////////////////////
134
135static uint32_t getInvalidRank(OperandType type) {
136 switch (type) {
137 case OperandType::FLOAT16:
138 case OperandType::FLOAT32:
139 case OperandType::INT32:
140 case OperandType::UINT32:
141 case OperandType::BOOL:
142 return 1;
143 case OperandType::TENSOR_BOOL8:
144 case OperandType::TENSOR_FLOAT16:
145 case OperandType::TENSOR_FLOAT32:
146 case OperandType::TENSOR_INT32:
147 case OperandType::TENSOR_QUANT8_ASYMM:
148 case OperandType::TENSOR_QUANT8_SYMM:
149 case OperandType::TENSOR_QUANT16_ASYMM:
150 case OperandType::TENSOR_QUANT16_SYMM:
151 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
152 return 0;
153 default:
154 return 0;
155 }
156}
157
158static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000159 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
160 const uint32_t invalidRank = getInvalidRank(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100161 if (invalidRank == 0) {
162 continue;
163 }
164 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
165 " has rank of " + std::to_string(invalidRank);
166 validate(device, message, model, [operand, invalidRank](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000167 model->main.operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100168 });
169 }
170}
171
172///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
173
174static float getInvalidScale(OperandType type) {
175 switch (type) {
176 case OperandType::FLOAT16:
177 case OperandType::FLOAT32:
178 case OperandType::INT32:
179 case OperandType::UINT32:
180 case OperandType::BOOL:
181 case OperandType::TENSOR_BOOL8:
182 case OperandType::TENSOR_FLOAT16:
183 case OperandType::TENSOR_FLOAT32:
184 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
185 return 1.0f;
186 case OperandType::TENSOR_INT32:
187 return -1.0f;
188 case OperandType::TENSOR_QUANT8_SYMM:
189 case OperandType::TENSOR_QUANT8_ASYMM:
190 case OperandType::TENSOR_QUANT16_ASYMM:
191 case OperandType::TENSOR_QUANT16_SYMM:
192 return 0.0f;
193 default:
194 return 0.0f;
195 }
196}
197
198static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000199 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
200 const float invalidScale = getInvalidScale(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100201 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
202 " has scale of " + std::to_string(invalidScale);
203 validate(device, message, model, [operand, invalidScale](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000204 model->main.operands[operand].scale = invalidScale;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100205 });
206 }
207}
208
209///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
210
211static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
212 switch (type) {
213 case OperandType::FLOAT16:
214 case OperandType::FLOAT32:
215 case OperandType::INT32:
216 case OperandType::UINT32:
217 case OperandType::BOOL:
218 case OperandType::TENSOR_BOOL8:
219 case OperandType::TENSOR_FLOAT16:
220 case OperandType::TENSOR_FLOAT32:
221 case OperandType::TENSOR_INT32:
222 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
223 return {1};
224 case OperandType::TENSOR_QUANT8_ASYMM:
225 return {-1, 256};
226 case OperandType::TENSOR_QUANT8_SYMM:
227 return {-129, -1, 1, 128};
228 case OperandType::TENSOR_QUANT16_ASYMM:
229 return {-1, 65536};
230 case OperandType::TENSOR_QUANT16_SYMM:
231 return {-32769, -1, 1, 32768};
232 default:
233 return {};
234 }
235}
236
237static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000238 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100239 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000240 getInvalidZeroPoints(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100241 for (int32_t invalidZeroPoint : invalidZeroPoints) {
242 const std::string message = "mutateOperandZeroPointTest: operand " +
243 std::to_string(operand) + " has zero point of " +
244 std::to_string(invalidZeroPoint);
245 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000246 model->main.operands[operand].zeroPoint = invalidZeroPoint;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100247 });
248 }
249 }
250}
251
252///////////////////////// VALIDATE EXTRA ??? /////////////////////////
253
254// TODO: Operand::lifetime
255// TODO: Operand::location
256
257///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
258
259static void mutateOperand(Operand* operand, OperandType type) {
260 Operand newOperand = *operand;
261 newOperand.type = type;
262 switch (type) {
263 case OperandType::FLOAT16:
264 case OperandType::FLOAT32:
265 case OperandType::INT32:
266 case OperandType::UINT32:
267 case OperandType::BOOL:
268 newOperand.dimensions = hidl_vec<uint32_t>();
269 newOperand.scale = 0.0f;
270 newOperand.zeroPoint = 0;
271 break;
272 case OperandType::TENSOR_BOOL8:
273 case OperandType::TENSOR_FLOAT16:
274 case OperandType::TENSOR_FLOAT32:
275 newOperand.dimensions =
276 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
277 newOperand.scale = 0.0f;
278 newOperand.zeroPoint = 0;
279 break;
280 case OperandType::TENSOR_INT32:
281 newOperand.dimensions =
282 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
283 newOperand.zeroPoint = 0;
284 break;
285 case OperandType::TENSOR_QUANT8_ASYMM:
286 case OperandType::TENSOR_QUANT8_SYMM:
287 case OperandType::TENSOR_QUANT16_ASYMM:
288 case OperandType::TENSOR_QUANT16_SYMM:
289 newOperand.dimensions =
290 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
291 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
292 break;
293 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
294 newOperand.dimensions =
295 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
296 newOperand.scale = 0.0f;
297 newOperand.zeroPoint = 0;
298
299 SymmPerChannelQuantParams channelQuant;
300 channelQuant.channelDim = 0;
301 channelQuant.scales = hidl_vec<float>(
302 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
303 : 0);
304 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
305 channelQuant.scales[i] = 1.0f;
306 }
307 newOperand.extraParams.channelQuant(std::move(channelQuant));
308 } break;
309 case OperandType::OEM:
310 case OperandType::TENSOR_OEM_BYTE:
311 default:
312 break;
313 }
314 *operand = newOperand;
315}
316
317static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
318 // Do not test OEM types
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000319 if (type == model.main.operands[operand].type || type == OperandType::OEM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100320 type == OperandType::TENSOR_OEM_BYTE) {
321 return true;
322 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000323 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100324 // Skip mutateOperationOperandTypeTest for the following operations.
325 // - LSH_PROJECTION's second argument is allowed to have any type.
326 // - ARGMIN and ARGMAX's first argument can be any of
327 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
328 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
329 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
330 // - DEQUANTIZE input can be any of
Lev Proleevae643ae2019-12-05 16:57:30 +0000331 // TENSOR_(QUANT8_ASYMM|QUANT8_ASYMM_SIGNED|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL),
332 // output can be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100333 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
334 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
335 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
336 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
337 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevda779f32020-01-02 17:49:03 +0000338 // - AXIS_ALIGNED_BBOX_TRANSFORM bounding boxes (arg 1) can be of
339 // TENSOR_QUANT8_ASYMM or TENSOR_QUANT8_ASYMM_SIGNED.
Lev Proleev53a51cb2020-01-20 18:54:46 +0000340 // - RANK's input can have any TENSOR_* type.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100341 switch (operation.type) {
342 case OperationType::LSH_PROJECTION: {
343 if (operand == operation.inputs[1]) {
344 return true;
345 }
346 } break;
347 case OperationType::CAST:
348 case OperationType::ARGMAX:
349 case OperationType::ARGMIN: {
350 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000351 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
352 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100353 return true;
354 }
355 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000356 case OperationType::QUANTIZE: {
357 if (operand == operation.inputs[0] &&
358 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
359 return true;
360 }
361 if (operand == operation.outputs[0] &&
362 (type == OperandType::TENSOR_QUANT8_ASYMM ||
363 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
364 return true;
365 }
366 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100367 case OperationType::RANDOM_MULTINOMIAL: {
368 if (operand == operation.inputs[0] &&
369 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
370 return true;
371 }
372 } break;
373 case OperationType::DEQUANTIZE: {
374 if (operand == operation.inputs[0] &&
375 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000376 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100377 type == OperandType::TENSOR_QUANT8_SYMM ||
378 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
379 return true;
380 }
381 if (operand == operation.outputs[0] &&
382 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
383 return true;
384 }
385 } break;
386 case OperationType::TRANSPOSE_CONV_2D:
387 case OperationType::GROUPED_CONV_2D:
388 case OperationType::DEPTHWISE_CONV_2D:
389 case OperationType::CONV_2D: {
390 if (operand == operation.inputs[1] &&
391 (type == OperandType::TENSOR_QUANT8_ASYMM ||
392 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
393 return true;
394 }
395 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000396 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
397 if (operand == operation.inputs[1] &&
398 (type == OperandType::TENSOR_QUANT8_ASYMM ||
399 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
400 return true;
401 }
402 } break;
Lev Proleev53a51cb2020-01-20 18:54:46 +0000403 case OperationType::RANK: {
404 if (operand == operation.inputs[0] &&
405 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
406 type == OperandType::TENSOR_INT32 ||
407 type == OperandType::TENSOR_QUANT8_ASYMM ||
408 type == OperandType::TENSOR_QUANT16_SYMM ||
409 type == OperandType::TENSOR_BOOL8 ||
410 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
411 type == OperandType::TENSOR_QUANT16_ASYMM ||
412 type == OperandType::TENSOR_QUANT8_SYMM ||
413 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
414 return true;
415 }
416 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100417 default:
418 break;
419 }
420 }
421 return false;
422}
423
424static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000425 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100426 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
427 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
428 continue;
429 }
430 const std::string message = "mutateOperationOperandTypeTest: operand " +
431 std::to_string(operand) + " set to type " +
432 toString(invalidOperandType);
433 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000434 mutateOperand(&model->main.operands[operand], invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100435 });
436 }
437 }
438}
439
440///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
441
442static const uint32_t invalidOperationTypes[] = {
443 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
444 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
445 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
446};
447
448static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000449 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100450 for (uint32_t invalidOperationType : invalidOperationTypes) {
451 const std::string message = "mutateOperationTypeTest: operation " +
452 std::to_string(operation) + " set to value " +
453 std::to_string(invalidOperationType);
454 validate(device, message, model, [operation, invalidOperationType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000455 model->main.operations[operation].type =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100456 static_cast<OperationType>(invalidOperationType);
457 });
458 }
459 }
460}
461
462///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
463
464static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000465 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
466 const uint32_t invalidOperand = model.main.operands.size();
467 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100468 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
469 std::to_string(operation) + " input " +
470 std::to_string(input);
471 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000472 model->main.operations[operation].inputs[input] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100473 });
474 }
475 }
476}
477
478///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
479
480static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000481 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
482 const uint32_t invalidOperand = model.main.operands.size();
483 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
484 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100485 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
486 std::to_string(operation) + " output " +
487 std::to_string(output);
488 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000489 model->main.operations[operation].outputs[output] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100490 });
491 }
492 }
493}
494
495///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
496
497static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
498 if (vec) {
499 // remove elements matching "value"
500 auto last = std::remove(vec->begin(), vec->end(), value);
501 vec->resize(std::distance(vec->begin(), last));
502
503 // decrement elements exceeding "value"
504 std::transform(vec->begin(), vec->end(), vec->begin(),
505 [value](uint32_t v) { return v > value ? v-- : v; });
506 }
507}
508
509static void removeOperand(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000510 hidl_vec_removeAt(&model->main.operands, index);
511 for (Operation& operation : model->main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100512 removeValueAndDecrementGreaterValues(&operation.inputs, index);
513 removeValueAndDecrementGreaterValues(&operation.outputs, index);
514 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000515 removeValueAndDecrementGreaterValues(&model->main.inputIndexes, index);
516 removeValueAndDecrementGreaterValues(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100517}
518
519static bool removeOperandSkip(size_t operand, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000520 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100521 // Skip removeOperandTest for the following operations.
522 // - SPLIT's outputs are not checked during prepareModel.
523 if (operation.type == OperationType::SPLIT) {
524 for (const size_t outOprand : operation.outputs) {
525 if (operand == outOprand) {
526 return true;
527 }
528 }
529 }
Lev Proleev17689aa2020-01-30 17:40:13 +0000530 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have
531 // either one, two, three or four outputs depending on their
532 // mergeOutputs parameter and if state outputs are provided.
533 // UNIDIRECTIONAL_SEQUENCE_LSTM and UNIDIRECTIONAL_SEQUENCE_RNN can have
534 // either one or three outputs depending on whether state outputs are
535 // provided.
536 if (operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM ||
537 operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_RNN ||
538 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100539 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
540 for (const size_t outOprand : operation.outputs) {
541 if (operand == outOprand) {
542 return true;
543 }
544 }
545 }
546 }
547 return false;
548}
549
550static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000551 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100552 if (removeOperandSkip(operand, model)) {
553 continue;
554 }
555 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
556 validate(device, message, model,
557 [operand](Model* model) { removeOperand(model, operand); });
558 }
559}
560
561///////////////////////// REMOVE OPERATION /////////////////////////
562
563static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000564 for (uint32_t operand : model->main.operations[index].inputs) {
565 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100566 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000567 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100568}
569
570static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000571 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100572 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
573 validate(device, message, model,
574 [operation](Model* model) { removeOperation(model, operation); });
575 }
576}
577
578///////////////////////// REMOVE OPERATION INPUT /////////////////////////
579
580static bool removeOperationInputSkip(const Operation& op, size_t input) {
581 // Skip removeOperationInputTest for the following operations.
582 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
583 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
584 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
585 // layout parameter.
586 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
587 // parameter.
588 switch (op.type) {
589 case OperationType::CONCATENATION: {
590 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
591 return true;
592 }
593 } break;
594 case OperationType::DEPTHWISE_CONV_2D: {
595 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
596 return true;
597 }
598 } break;
599 case OperationType::CONV_2D:
600 case OperationType::AVERAGE_POOL_2D:
601 case OperationType::MAX_POOL_2D:
602 case OperationType::L2_POOL_2D: {
603 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
604 return true;
605 }
606 } break;
607 case OperationType::RESIZE_BILINEAR: {
608 if (op.inputs.size() == 4 && input == 3) {
609 return true;
610 }
611 } break;
612 case OperationType::SPACE_TO_DEPTH:
613 case OperationType::DEPTH_TO_SPACE:
614 case OperationType::BATCH_TO_SPACE_ND: {
615 if (op.inputs.size() == 3 && input == 2) {
616 return true;
617 }
618 } break;
619 case OperationType::SPACE_TO_BATCH_ND: {
620 if (op.inputs.size() == 4 && input == 3) {
621 return true;
622 }
623 } break;
624 case OperationType::L2_NORMALIZATION: {
625 if (op.inputs.size() == 2 && input == 1) {
626 return true;
627 }
628 } break;
629 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
630 if (op.inputs.size() == 6 && input == 5) {
631 return true;
632 }
633 } break;
634 case OperationType::SOFTMAX: {
635 if (op.inputs.size() == 3 && input == 2) {
636 return true;
637 }
638 } break;
639 default:
640 break;
641 }
642 return false;
643}
644
645static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000646 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
647 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
648 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100649 if (removeOperationInputSkip(op, input)) {
650 continue;
651 }
652 const std::string message = "removeOperationInputTest: operation " +
653 std::to_string(operation) + ", input " +
654 std::to_string(input);
655 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000656 uint32_t operand = model->main.operations[operation].inputs[input];
657 model->main.operands[operand].numberOfConsumers--;
658 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100659 });
660 }
661 }
662}
663
664///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
665
666static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000667 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
668 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
669 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100670 const std::string message = "removeOperationOutputTest: operation " +
671 std::to_string(operation) + ", output " +
672 std::to_string(output);
673 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000674 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100675 });
676 }
677 }
678}
679
680///////////////////////// MODEL VALIDATION /////////////////////////
681
682// TODO: remove model input
683// TODO: remove model output
684// TODO: add unused operation
685
686///////////////////////// ADD OPERATION INPUT /////////////////////////
687
688static bool addOperationInputSkip(const Operation& op) {
689 // Skip addOperationInputTest for the following operations.
690 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
691 // parameter.
692 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
693 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
694 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
695 return true;
696 }
697 return false;
698}
699
700static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000701 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
702 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100703 continue;
704 }
705 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
706 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000707 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
708 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
709 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100710 });
711 }
712}
713
714///////////////////////// ADD OPERATION OUTPUT /////////////////////////
715
716static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000717 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100718 const std::string message =
719 "addOperationOutputTest: operation " + std::to_string(operation);
720 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000721 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
722 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
723 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100724 });
725 }
726}
727
728///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
729
730static const int32_t invalidExecutionPreferences[] = {
731 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
732 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
733};
734
735static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
736 for (int32_t preference : invalidExecutionPreferences) {
737 const std::string message =
738 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
739 validate(
740 device, message, model, [](Model*) {},
741 static_cast<ExecutionPreference>(preference));
742 }
743}
744
Michael Butler95899b32020-01-07 14:52:44 -0800745///////////////////////// DEADLINE /////////////////////////
746
747static void deadlineTest(const sp<IDevice>& device, const Model& model) {
748 const std::string message = "deadlineTest: deadline not supported";
749 const auto noop = [](Model*) {};
750 validate(device, message, model, noop, ExecutionPreference::FAST_SINGLE_ANSWER,
751 /*testDeadline=*/true);
752}
753
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100754////////////////////////// ENTRY POINT //////////////////////////////
755
Michael Butler95899b32020-01-07 14:52:44 -0800756void validateModel(const sp<IDevice>& device, const Model& model,
757 bool prepareModelDeadlineSupported) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100758 mutateOperandTypeTest(device, model);
759 mutateOperandRankTest(device, model);
760 mutateOperandScaleTest(device, model);
761 mutateOperandZeroPointTest(device, model);
762 mutateOperationOperandTypeTest(device, model);
763 mutateOperationTypeTest(device, model);
764 mutateOperationInputOperandIndexTest(device, model);
765 mutateOperationOutputOperandIndexTest(device, model);
766 removeOperandTest(device, model);
767 removeOperationTest(device, model);
768 removeOperationInputTest(device, model);
769 removeOperationOutputTest(device, model);
770 addOperationInputTest(device, model);
771 addOperationOutputTest(device, model);
772 mutateExecutionPreferenceTest(device, model);
Michael Butler95899b32020-01-07 14:52:44 -0800773 if (!prepareModelDeadlineSupported) {
774 deadlineTest(device, model);
775 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100776}
777
Lev Proleev26d1bc82019-08-30 11:57:18 +0100778} // namespace android::hardware::neuralnetworks::V1_3::vts::functional