blob: 0a35e2d23308b01e60d69e45151a25e986086c05 [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 }
530 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
531 // outputs depending on their mergeOutputs parameter.
532 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
533 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
534 for (const size_t outOprand : operation.outputs) {
535 if (operand == outOprand) {
536 return true;
537 }
538 }
539 }
540 }
541 return false;
542}
543
544static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000545 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100546 if (removeOperandSkip(operand, model)) {
547 continue;
548 }
549 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
550 validate(device, message, model,
551 [operand](Model* model) { removeOperand(model, operand); });
552 }
553}
554
555///////////////////////// REMOVE OPERATION /////////////////////////
556
557static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000558 for (uint32_t operand : model->main.operations[index].inputs) {
559 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100560 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000561 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100562}
563
564static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000565 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100566 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
567 validate(device, message, model,
568 [operation](Model* model) { removeOperation(model, operation); });
569 }
570}
571
572///////////////////////// REMOVE OPERATION INPUT /////////////////////////
573
574static bool removeOperationInputSkip(const Operation& op, size_t input) {
575 // Skip removeOperationInputTest for the following operations.
576 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
577 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
578 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
579 // layout parameter.
580 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
581 // parameter.
582 switch (op.type) {
583 case OperationType::CONCATENATION: {
584 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
585 return true;
586 }
587 } break;
588 case OperationType::DEPTHWISE_CONV_2D: {
589 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
590 return true;
591 }
592 } break;
593 case OperationType::CONV_2D:
594 case OperationType::AVERAGE_POOL_2D:
595 case OperationType::MAX_POOL_2D:
596 case OperationType::L2_POOL_2D: {
597 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
598 return true;
599 }
600 } break;
601 case OperationType::RESIZE_BILINEAR: {
602 if (op.inputs.size() == 4 && input == 3) {
603 return true;
604 }
605 } break;
606 case OperationType::SPACE_TO_DEPTH:
607 case OperationType::DEPTH_TO_SPACE:
608 case OperationType::BATCH_TO_SPACE_ND: {
609 if (op.inputs.size() == 3 && input == 2) {
610 return true;
611 }
612 } break;
613 case OperationType::SPACE_TO_BATCH_ND: {
614 if (op.inputs.size() == 4 && input == 3) {
615 return true;
616 }
617 } break;
618 case OperationType::L2_NORMALIZATION: {
619 if (op.inputs.size() == 2 && input == 1) {
620 return true;
621 }
622 } break;
623 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
624 if (op.inputs.size() == 6 && input == 5) {
625 return true;
626 }
627 } break;
628 case OperationType::SOFTMAX: {
629 if (op.inputs.size() == 3 && input == 2) {
630 return true;
631 }
632 } break;
633 default:
634 break;
635 }
636 return false;
637}
638
639static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000640 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
641 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
642 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100643 if (removeOperationInputSkip(op, input)) {
644 continue;
645 }
646 const std::string message = "removeOperationInputTest: operation " +
647 std::to_string(operation) + ", input " +
648 std::to_string(input);
649 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000650 uint32_t operand = model->main.operations[operation].inputs[input];
651 model->main.operands[operand].numberOfConsumers--;
652 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100653 });
654 }
655 }
656}
657
658///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
659
660static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000661 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
662 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
663 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100664 const std::string message = "removeOperationOutputTest: operation " +
665 std::to_string(operation) + ", output " +
666 std::to_string(output);
667 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000668 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100669 });
670 }
671 }
672}
673
674///////////////////////// MODEL VALIDATION /////////////////////////
675
676// TODO: remove model input
677// TODO: remove model output
678// TODO: add unused operation
679
680///////////////////////// ADD OPERATION INPUT /////////////////////////
681
682static bool addOperationInputSkip(const Operation& op) {
683 // Skip addOperationInputTest for the following operations.
684 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
685 // parameter.
686 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
687 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
688 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
689 return true;
690 }
691 return false;
692}
693
694static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000695 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
696 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100697 continue;
698 }
699 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
700 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000701 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
702 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
703 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100704 });
705 }
706}
707
708///////////////////////// ADD OPERATION OUTPUT /////////////////////////
709
710static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000711 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100712 const std::string message =
713 "addOperationOutputTest: operation " + std::to_string(operation);
714 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000715 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
716 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
717 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100718 });
719 }
720}
721
722///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
723
724static const int32_t invalidExecutionPreferences[] = {
725 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
726 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
727};
728
729static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
730 for (int32_t preference : invalidExecutionPreferences) {
731 const std::string message =
732 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
733 validate(
734 device, message, model, [](Model*) {},
735 static_cast<ExecutionPreference>(preference));
736 }
737}
738
Michael Butler95899b32020-01-07 14:52:44 -0800739///////////////////////// DEADLINE /////////////////////////
740
741static void deadlineTest(const sp<IDevice>& device, const Model& model) {
742 const std::string message = "deadlineTest: deadline not supported";
743 const auto noop = [](Model*) {};
744 validate(device, message, model, noop, ExecutionPreference::FAST_SINGLE_ANSWER,
745 /*testDeadline=*/true);
746}
747
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100748////////////////////////// ENTRY POINT //////////////////////////////
749
Michael Butler95899b32020-01-07 14:52:44 -0800750void validateModel(const sp<IDevice>& device, const Model& model,
751 bool prepareModelDeadlineSupported) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100752 mutateOperandTypeTest(device, model);
753 mutateOperandRankTest(device, model);
754 mutateOperandScaleTest(device, model);
755 mutateOperandZeroPointTest(device, model);
756 mutateOperationOperandTypeTest(device, model);
757 mutateOperationTypeTest(device, model);
758 mutateOperationInputOperandIndexTest(device, model);
759 mutateOperationOutputOperandIndexTest(device, model);
760 removeOperandTest(device, model);
761 removeOperationTest(device, model);
762 removeOperationInputTest(device, model);
763 removeOperationOutputTest(device, model);
764 addOperationInputTest(device, model);
765 addOperationOutputTest(device, model);
766 mutateExecutionPreferenceTest(device, model);
Michael Butler95899b32020-01-07 14:52:44 -0800767 if (!prepareModelDeadlineSupported) {
768 deadlineTest(device, model);
769 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100770}
771
Lev Proleev26d1bc82019-08-30 11:57:18 +0100772} // namespace android::hardware::neuralnetworks::V1_3::vts::functional