blob: 09e9922a23d39fbb572a3e33679fa0866f170f18 [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:
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000185 case OperandType::SUBGRAPH:
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100186 return 1.0f;
187 case OperandType::TENSOR_INT32:
188 return -1.0f;
189 case OperandType::TENSOR_QUANT8_SYMM:
190 case OperandType::TENSOR_QUANT8_ASYMM:
191 case OperandType::TENSOR_QUANT16_ASYMM:
192 case OperandType::TENSOR_QUANT16_SYMM:
193 return 0.0f;
194 default:
195 return 0.0f;
196 }
197}
198
199static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000200 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
201 const float invalidScale = getInvalidScale(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100202 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
203 " has scale of " + std::to_string(invalidScale);
204 validate(device, message, model, [operand, invalidScale](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000205 model->main.operands[operand].scale = invalidScale;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100206 });
207 }
208}
209
210///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
211
212static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
213 switch (type) {
214 case OperandType::FLOAT16:
215 case OperandType::FLOAT32:
216 case OperandType::INT32:
217 case OperandType::UINT32:
218 case OperandType::BOOL:
219 case OperandType::TENSOR_BOOL8:
220 case OperandType::TENSOR_FLOAT16:
221 case OperandType::TENSOR_FLOAT32:
222 case OperandType::TENSOR_INT32:
223 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000224 case OperandType::SUBGRAPH:
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100225 return {1};
226 case OperandType::TENSOR_QUANT8_ASYMM:
227 return {-1, 256};
228 case OperandType::TENSOR_QUANT8_SYMM:
229 return {-129, -1, 1, 128};
230 case OperandType::TENSOR_QUANT16_ASYMM:
231 return {-1, 65536};
232 case OperandType::TENSOR_QUANT16_SYMM:
233 return {-32769, -1, 1, 32768};
234 default:
235 return {};
236 }
237}
238
239static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000240 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100241 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000242 getInvalidZeroPoints(model.main.operands[operand].type);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100243 for (int32_t invalidZeroPoint : invalidZeroPoints) {
244 const std::string message = "mutateOperandZeroPointTest: operand " +
245 std::to_string(operand) + " has zero point of " +
246 std::to_string(invalidZeroPoint);
247 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000248 model->main.operands[operand].zeroPoint = invalidZeroPoint;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100249 });
250 }
251 }
252}
253
254///////////////////////// VALIDATE EXTRA ??? /////////////////////////
255
256// TODO: Operand::lifetime
257// TODO: Operand::location
258
259///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
260
261static void mutateOperand(Operand* operand, OperandType type) {
262 Operand newOperand = *operand;
263 newOperand.type = type;
264 switch (type) {
265 case OperandType::FLOAT16:
266 case OperandType::FLOAT32:
267 case OperandType::INT32:
268 case OperandType::UINT32:
269 case OperandType::BOOL:
270 newOperand.dimensions = hidl_vec<uint32_t>();
271 newOperand.scale = 0.0f;
272 newOperand.zeroPoint = 0;
273 break;
274 case OperandType::TENSOR_BOOL8:
275 case OperandType::TENSOR_FLOAT16:
276 case OperandType::TENSOR_FLOAT32:
277 newOperand.dimensions =
278 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
279 newOperand.scale = 0.0f;
280 newOperand.zeroPoint = 0;
281 break;
282 case OperandType::TENSOR_INT32:
283 newOperand.dimensions =
284 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
285 newOperand.zeroPoint = 0;
286 break;
287 case OperandType::TENSOR_QUANT8_ASYMM:
288 case OperandType::TENSOR_QUANT8_SYMM:
289 case OperandType::TENSOR_QUANT16_ASYMM:
290 case OperandType::TENSOR_QUANT16_SYMM:
291 newOperand.dimensions =
292 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
293 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
294 break;
295 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
296 newOperand.dimensions =
297 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
298 newOperand.scale = 0.0f;
299 newOperand.zeroPoint = 0;
300
301 SymmPerChannelQuantParams channelQuant;
302 channelQuant.channelDim = 0;
303 channelQuant.scales = hidl_vec<float>(
304 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
305 : 0);
306 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
307 channelQuant.scales[i] = 1.0f;
308 }
309 newOperand.extraParams.channelQuant(std::move(channelQuant));
310 } break;
311 case OperandType::OEM:
312 case OperandType::TENSOR_OEM_BYTE:
313 default:
314 break;
315 }
316 *operand = newOperand;
317}
318
319static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
320 // Do not test OEM types
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000321 if (type == model.main.operands[operand].type || type == OperandType::OEM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100322 type == OperandType::TENSOR_OEM_BYTE) {
323 return true;
324 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000325 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100326 // Skip mutateOperationOperandTypeTest for the following operations.
327 // - LSH_PROJECTION's second argument is allowed to have any type.
328 // - ARGMIN and ARGMAX's first argument can be any of
329 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
330 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
331 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
332 // - DEQUANTIZE input can be any of
Lev Proleevae643ae2019-12-05 16:57:30 +0000333 // TENSOR_(QUANT8_ASYMM|QUANT8_ASYMM_SIGNED|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL),
334 // output can be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100335 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
336 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
337 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
338 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
339 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevda779f32020-01-02 17:49:03 +0000340 // - AXIS_ALIGNED_BBOX_TRANSFORM bounding boxes (arg 1) can be of
341 // TENSOR_QUANT8_ASYMM or TENSOR_QUANT8_ASYMM_SIGNED.
Lev Proleev53a51cb2020-01-20 18:54:46 +0000342 // - RANK's input can have any TENSOR_* type.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100343 switch (operation.type) {
344 case OperationType::LSH_PROJECTION: {
345 if (operand == operation.inputs[1]) {
346 return true;
347 }
348 } break;
349 case OperationType::CAST:
350 case OperationType::ARGMAX:
351 case OperationType::ARGMIN: {
352 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000353 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
354 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100355 return true;
356 }
357 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000358 case OperationType::QUANTIZE: {
359 if (operand == operation.inputs[0] &&
360 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
361 return true;
362 }
363 if (operand == operation.outputs[0] &&
364 (type == OperandType::TENSOR_QUANT8_ASYMM ||
365 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
366 return true;
367 }
368 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100369 case OperationType::RANDOM_MULTINOMIAL: {
370 if (operand == operation.inputs[0] &&
371 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
372 return true;
373 }
374 } break;
375 case OperationType::DEQUANTIZE: {
376 if (operand == operation.inputs[0] &&
377 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000378 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100379 type == OperandType::TENSOR_QUANT8_SYMM ||
380 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
381 return true;
382 }
383 if (operand == operation.outputs[0] &&
384 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
385 return true;
386 }
387 } break;
388 case OperationType::TRANSPOSE_CONV_2D:
389 case OperationType::GROUPED_CONV_2D:
390 case OperationType::DEPTHWISE_CONV_2D:
391 case OperationType::CONV_2D: {
392 if (operand == operation.inputs[1] &&
393 (type == OperandType::TENSOR_QUANT8_ASYMM ||
394 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
395 return true;
396 }
397 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000398 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
399 if (operand == operation.inputs[1] &&
400 (type == OperandType::TENSOR_QUANT8_ASYMM ||
401 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
402 return true;
403 }
404 } break;
Lev Proleev53a51cb2020-01-20 18:54:46 +0000405 case OperationType::RANK: {
406 if (operand == operation.inputs[0] &&
407 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
408 type == OperandType::TENSOR_INT32 ||
409 type == OperandType::TENSOR_QUANT8_ASYMM ||
410 type == OperandType::TENSOR_QUANT16_SYMM ||
411 type == OperandType::TENSOR_BOOL8 ||
412 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
413 type == OperandType::TENSOR_QUANT16_ASYMM ||
414 type == OperandType::TENSOR_QUANT8_SYMM ||
415 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
416 return true;
417 }
418 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100419 default:
420 break;
421 }
422 }
423 return false;
424}
425
426static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000427 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100428 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
429 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
430 continue;
431 }
432 const std::string message = "mutateOperationOperandTypeTest: operand " +
433 std::to_string(operand) + " set to type " +
434 toString(invalidOperandType);
435 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000436 mutateOperand(&model->main.operands[operand], invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100437 });
438 }
439 }
440}
441
442///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
443
444static const uint32_t invalidOperationTypes[] = {
445 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
446 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
447 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
448};
449
450static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000451 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100452 for (uint32_t invalidOperationType : invalidOperationTypes) {
453 const std::string message = "mutateOperationTypeTest: operation " +
454 std::to_string(operation) + " set to value " +
455 std::to_string(invalidOperationType);
456 validate(device, message, model, [operation, invalidOperationType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000457 model->main.operations[operation].type =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100458 static_cast<OperationType>(invalidOperationType);
459 });
460 }
461 }
462}
463
464///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
465
466static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000467 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
468 const uint32_t invalidOperand = model.main.operands.size();
469 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100470 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
471 std::to_string(operation) + " input " +
472 std::to_string(input);
473 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000474 model->main.operations[operation].inputs[input] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100475 });
476 }
477 }
478}
479
480///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
481
482static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000483 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
484 const uint32_t invalidOperand = model.main.operands.size();
485 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
486 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100487 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
488 std::to_string(operation) + " output " +
489 std::to_string(output);
490 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000491 model->main.operations[operation].outputs[output] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100492 });
493 }
494 }
495}
496
497///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
498
499static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
500 if (vec) {
501 // remove elements matching "value"
502 auto last = std::remove(vec->begin(), vec->end(), value);
503 vec->resize(std::distance(vec->begin(), last));
504
505 // decrement elements exceeding "value"
506 std::transform(vec->begin(), vec->end(), vec->begin(),
507 [value](uint32_t v) { return v > value ? v-- : v; });
508 }
509}
510
511static void removeOperand(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000512 hidl_vec_removeAt(&model->main.operands, index);
513 for (Operation& operation : model->main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100514 removeValueAndDecrementGreaterValues(&operation.inputs, index);
515 removeValueAndDecrementGreaterValues(&operation.outputs, index);
516 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000517 removeValueAndDecrementGreaterValues(&model->main.inputIndexes, index);
518 removeValueAndDecrementGreaterValues(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100519}
520
521static bool removeOperandSkip(size_t operand, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000522 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100523 // Skip removeOperandTest for the following operations.
524 // - SPLIT's outputs are not checked during prepareModel.
525 if (operation.type == OperationType::SPLIT) {
526 for (const size_t outOprand : operation.outputs) {
527 if (operand == outOprand) {
528 return true;
529 }
530 }
531 }
Lev Proleev17689aa2020-01-30 17:40:13 +0000532 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have
533 // either one, two, three or four outputs depending on their
534 // mergeOutputs parameter and if state outputs are provided.
535 // UNIDIRECTIONAL_SEQUENCE_LSTM and UNIDIRECTIONAL_SEQUENCE_RNN can have
536 // either one or three outputs depending on whether state outputs are
537 // provided.
538 if (operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM ||
539 operation.type == OperationType::UNIDIRECTIONAL_SEQUENCE_RNN ||
540 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100541 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
542 for (const size_t outOprand : operation.outputs) {
543 if (operand == outOprand) {
544 return true;
545 }
546 }
547 }
548 }
549 return false;
550}
551
552static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000553 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100554 if (removeOperandSkip(operand, model)) {
555 continue;
556 }
557 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
558 validate(device, message, model,
559 [operand](Model* model) { removeOperand(model, operand); });
560 }
561}
562
563///////////////////////// REMOVE OPERATION /////////////////////////
564
565static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000566 for (uint32_t operand : model->main.operations[index].inputs) {
567 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100568 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000569 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100570}
571
572static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000573 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100574 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
575 validate(device, message, model,
576 [operation](Model* model) { removeOperation(model, operation); });
577 }
578}
579
580///////////////////////// REMOVE OPERATION INPUT /////////////////////////
581
582static bool removeOperationInputSkip(const Operation& op, size_t input) {
583 // Skip removeOperationInputTest for the following operations.
584 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
585 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
586 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
587 // layout parameter.
588 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
589 // parameter.
590 switch (op.type) {
591 case OperationType::CONCATENATION: {
592 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
593 return true;
594 }
595 } break;
596 case OperationType::DEPTHWISE_CONV_2D: {
597 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
598 return true;
599 }
600 } break;
601 case OperationType::CONV_2D:
602 case OperationType::AVERAGE_POOL_2D:
603 case OperationType::MAX_POOL_2D:
604 case OperationType::L2_POOL_2D: {
605 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
606 return true;
607 }
608 } break;
609 case OperationType::RESIZE_BILINEAR: {
610 if (op.inputs.size() == 4 && input == 3) {
611 return true;
612 }
613 } break;
614 case OperationType::SPACE_TO_DEPTH:
615 case OperationType::DEPTH_TO_SPACE:
616 case OperationType::BATCH_TO_SPACE_ND: {
617 if (op.inputs.size() == 3 && input == 2) {
618 return true;
619 }
620 } break;
621 case OperationType::SPACE_TO_BATCH_ND: {
622 if (op.inputs.size() == 4 && input == 3) {
623 return true;
624 }
625 } break;
626 case OperationType::L2_NORMALIZATION: {
627 if (op.inputs.size() == 2 && input == 1) {
628 return true;
629 }
630 } break;
631 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
632 if (op.inputs.size() == 6 && input == 5) {
633 return true;
634 }
635 } break;
636 case OperationType::SOFTMAX: {
637 if (op.inputs.size() == 3 && input == 2) {
638 return true;
639 }
640 } break;
641 default:
642 break;
643 }
644 return false;
645}
646
647static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000648 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
649 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
650 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100651 if (removeOperationInputSkip(op, input)) {
652 continue;
653 }
654 const std::string message = "removeOperationInputTest: operation " +
655 std::to_string(operation) + ", input " +
656 std::to_string(input);
657 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000658 uint32_t operand = model->main.operations[operation].inputs[input];
659 model->main.operands[operand].numberOfConsumers--;
660 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100661 });
662 }
663 }
664}
665
666///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
667
668static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000669 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
670 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
671 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100672 const std::string message = "removeOperationOutputTest: operation " +
673 std::to_string(operation) + ", output " +
674 std::to_string(output);
675 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000676 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100677 });
678 }
679 }
680}
681
682///////////////////////// MODEL VALIDATION /////////////////////////
683
684// TODO: remove model input
685// TODO: remove model output
686// TODO: add unused operation
687
688///////////////////////// ADD OPERATION INPUT /////////////////////////
689
690static bool addOperationInputSkip(const Operation& op) {
691 // Skip addOperationInputTest for the following operations.
692 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
693 // parameter.
694 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
695 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
696 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
697 return true;
698 }
699 return false;
700}
701
702static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000703 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
704 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100705 continue;
706 }
707 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
708 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000709 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
710 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
711 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100712 });
713 }
714}
715
716///////////////////////// ADD OPERATION OUTPUT /////////////////////////
717
718static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000719 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100720 const std::string message =
721 "addOperationOutputTest: operation " + std::to_string(operation);
722 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000723 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
724 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
725 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100726 });
727 }
728}
729
730///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
731
732static const int32_t invalidExecutionPreferences[] = {
733 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
734 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
735};
736
737static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
738 for (int32_t preference : invalidExecutionPreferences) {
739 const std::string message =
740 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
741 validate(
742 device, message, model, [](Model*) {},
743 static_cast<ExecutionPreference>(preference));
744 }
745}
746
Michael Butler95899b32020-01-07 14:52:44 -0800747///////////////////////// DEADLINE /////////////////////////
748
749static void deadlineTest(const sp<IDevice>& device, const Model& model) {
750 const std::string message = "deadlineTest: deadline not supported";
751 const auto noop = [](Model*) {};
752 validate(device, message, model, noop, ExecutionPreference::FAST_SINGLE_ANSWER,
753 /*testDeadline=*/true);
754}
755
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100756////////////////////////// ENTRY POINT //////////////////////////////
757
Michael Butler95899b32020-01-07 14:52:44 -0800758void validateModel(const sp<IDevice>& device, const Model& model,
759 bool prepareModelDeadlineSupported) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100760 mutateOperandTypeTest(device, model);
761 mutateOperandRankTest(device, model);
762 mutateOperandScaleTest(device, model);
763 mutateOperandZeroPointTest(device, model);
764 mutateOperationOperandTypeTest(device, model);
765 mutateOperationTypeTest(device, model);
766 mutateOperationInputOperandIndexTest(device, model);
767 mutateOperationOutputOperandIndexTest(device, model);
768 removeOperandTest(device, model);
769 removeOperationTest(device, model);
770 removeOperationInputTest(device, model);
771 removeOperationOutputTest(device, model);
772 addOperationInputTest(device, model);
773 addOperationOutputTest(device, model);
774 mutateExecutionPreferenceTest(device, model);
Michael Butler95899b32020-01-07 14:52:44 -0800775 if (!prepareModelDeadlineSupported) {
776 deadlineTest(device, model);
777 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100778}
779
Lev Proleev26d1bc82019-08-30 11:57:18 +0100780} // namespace android::hardware::neuralnetworks::V1_3::vts::functional