blob: a21142880e84273c8b88dc171630ba6606c6ce49 [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) {
53 deadline.nanoseconds(std::numeric_limits<uint64_t>::max());
54 }
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 Proleev13fdfcd2019-08-30 11:35:34 +0100340 switch (operation.type) {
341 case OperationType::LSH_PROJECTION: {
342 if (operand == operation.inputs[1]) {
343 return true;
344 }
345 } break;
346 case OperationType::CAST:
347 case OperationType::ARGMAX:
348 case OperationType::ARGMIN: {
349 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
Przemyslaw Szczepaniak2326dd12019-11-29 09:49:17 +0000350 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM ||
351 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100352 return true;
353 }
354 } break;
Przemyslaw Szczepaniak90fc2cc2019-11-25 11:04:19 +0000355 case OperationType::QUANTIZE: {
356 if (operand == operation.inputs[0] &&
357 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
358 return true;
359 }
360 if (operand == operation.outputs[0] &&
361 (type == OperandType::TENSOR_QUANT8_ASYMM ||
362 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
363 return true;
364 }
365 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100366 case OperationType::RANDOM_MULTINOMIAL: {
367 if (operand == operation.inputs[0] &&
368 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
369 return true;
370 }
371 } break;
372 case OperationType::DEQUANTIZE: {
373 if (operand == operation.inputs[0] &&
374 (type == OperandType::TENSOR_QUANT8_ASYMM ||
Lev Proleevae643ae2019-12-05 16:57:30 +0000375 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100376 type == OperandType::TENSOR_QUANT8_SYMM ||
377 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
378 return true;
379 }
380 if (operand == operation.outputs[0] &&
381 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
382 return true;
383 }
384 } break;
385 case OperationType::TRANSPOSE_CONV_2D:
386 case OperationType::GROUPED_CONV_2D:
387 case OperationType::DEPTHWISE_CONV_2D:
388 case OperationType::CONV_2D: {
389 if (operand == operation.inputs[1] &&
390 (type == OperandType::TENSOR_QUANT8_ASYMM ||
391 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
392 return true;
393 }
394 } break;
Lev Proleevda779f32020-01-02 17:49:03 +0000395 case OperationType::AXIS_ALIGNED_BBOX_TRANSFORM: {
396 if (operand == operation.inputs[1] &&
397 (type == OperandType::TENSOR_QUANT8_ASYMM ||
398 type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)) {
399 return true;
400 }
401 } break;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100402 default:
403 break;
404 }
405 }
406 return false;
407}
408
409static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000410 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100411 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
412 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
413 continue;
414 }
415 const std::string message = "mutateOperationOperandTypeTest: operand " +
416 std::to_string(operand) + " set to type " +
417 toString(invalidOperandType);
418 validate(device, message, model, [operand, invalidOperandType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000419 mutateOperand(&model->main.operands[operand], invalidOperandType);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100420 });
421 }
422 }
423}
424
425///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
426
427static const uint32_t invalidOperationTypes[] = {
428 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
429 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
430 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
431};
432
433static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000434 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100435 for (uint32_t invalidOperationType : invalidOperationTypes) {
436 const std::string message = "mutateOperationTypeTest: operation " +
437 std::to_string(operation) + " set to value " +
438 std::to_string(invalidOperationType);
439 validate(device, message, model, [operation, invalidOperationType](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000440 model->main.operations[operation].type =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100441 static_cast<OperationType>(invalidOperationType);
442 });
443 }
444 }
445}
446
447///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
448
449static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000450 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
451 const uint32_t invalidOperand = model.main.operands.size();
452 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100453 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
454 std::to_string(operation) + " input " +
455 std::to_string(input);
456 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000457 model->main.operations[operation].inputs[input] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100458 });
459 }
460 }
461}
462
463///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
464
465static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000466 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
467 const uint32_t invalidOperand = model.main.operands.size();
468 for (size_t output = 0; output < model.main.operations[operation].outputs.size();
469 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100470 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
471 std::to_string(operation) + " output " +
472 std::to_string(output);
473 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000474 model->main.operations[operation].outputs[output] = invalidOperand;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100475 });
476 }
477 }
478}
479
480///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
481
482static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
483 if (vec) {
484 // remove elements matching "value"
485 auto last = std::remove(vec->begin(), vec->end(), value);
486 vec->resize(std::distance(vec->begin(), last));
487
488 // decrement elements exceeding "value"
489 std::transform(vec->begin(), vec->end(), vec->begin(),
490 [value](uint32_t v) { return v > value ? v-- : v; });
491 }
492}
493
494static void removeOperand(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000495 hidl_vec_removeAt(&model->main.operands, index);
496 for (Operation& operation : model->main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100497 removeValueAndDecrementGreaterValues(&operation.inputs, index);
498 removeValueAndDecrementGreaterValues(&operation.outputs, index);
499 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000500 removeValueAndDecrementGreaterValues(&model->main.inputIndexes, index);
501 removeValueAndDecrementGreaterValues(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100502}
503
504static bool removeOperandSkip(size_t operand, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000505 for (const Operation& operation : model.main.operations) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100506 // Skip removeOperandTest for the following operations.
507 // - SPLIT's outputs are not checked during prepareModel.
508 if (operation.type == OperationType::SPLIT) {
509 for (const size_t outOprand : operation.outputs) {
510 if (operand == outOprand) {
511 return true;
512 }
513 }
514 }
515 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have either one or two
516 // outputs depending on their mergeOutputs parameter.
517 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
518 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
519 for (const size_t outOprand : operation.outputs) {
520 if (operand == outOprand) {
521 return true;
522 }
523 }
524 }
525 }
526 return false;
527}
528
529static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000530 for (size_t operand = 0; operand < model.main.operands.size(); ++operand) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100531 if (removeOperandSkip(operand, model)) {
532 continue;
533 }
534 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
535 validate(device, message, model,
536 [operand](Model* model) { removeOperand(model, operand); });
537 }
538}
539
540///////////////////////// REMOVE OPERATION /////////////////////////
541
542static void removeOperation(Model* model, uint32_t index) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000543 for (uint32_t operand : model->main.operations[index].inputs) {
544 model->main.operands[operand].numberOfConsumers--;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100545 }
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000546 hidl_vec_removeAt(&model->main.operations, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100547}
548
549static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000550 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100551 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
552 validate(device, message, model,
553 [operation](Model* model) { removeOperation(model, operation); });
554 }
555}
556
557///////////////////////// REMOVE OPERATION INPUT /////////////////////////
558
559static bool removeOperationInputSkip(const Operation& op, size_t input) {
560 // Skip removeOperationInputTest for the following operations.
561 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
562 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
563 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
564 // layout parameter.
565 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
566 // parameter.
567 switch (op.type) {
568 case OperationType::CONCATENATION: {
569 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
570 return true;
571 }
572 } break;
573 case OperationType::DEPTHWISE_CONV_2D: {
574 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
575 return true;
576 }
577 } break;
578 case OperationType::CONV_2D:
579 case OperationType::AVERAGE_POOL_2D:
580 case OperationType::MAX_POOL_2D:
581 case OperationType::L2_POOL_2D: {
582 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
583 return true;
584 }
585 } break;
586 case OperationType::RESIZE_BILINEAR: {
587 if (op.inputs.size() == 4 && input == 3) {
588 return true;
589 }
590 } break;
591 case OperationType::SPACE_TO_DEPTH:
592 case OperationType::DEPTH_TO_SPACE:
593 case OperationType::BATCH_TO_SPACE_ND: {
594 if (op.inputs.size() == 3 && input == 2) {
595 return true;
596 }
597 } break;
598 case OperationType::SPACE_TO_BATCH_ND: {
599 if (op.inputs.size() == 4 && input == 3) {
600 return true;
601 }
602 } break;
603 case OperationType::L2_NORMALIZATION: {
604 if (op.inputs.size() == 2 && input == 1) {
605 return true;
606 }
607 } break;
608 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
609 if (op.inputs.size() == 6 && input == 5) {
610 return true;
611 }
612 } break;
613 case OperationType::SOFTMAX: {
614 if (op.inputs.size() == 3 && input == 2) {
615 return true;
616 }
617 } break;
618 default:
619 break;
620 }
621 return false;
622}
623
624static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000625 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
626 for (size_t input = 0; input < model.main.operations[operation].inputs.size(); ++input) {
627 const Operation& op = model.main.operations[operation];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100628 if (removeOperationInputSkip(op, input)) {
629 continue;
630 }
631 const std::string message = "removeOperationInputTest: operation " +
632 std::to_string(operation) + ", input " +
633 std::to_string(input);
634 validate(device, message, model, [operation, input](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000635 uint32_t operand = model->main.operations[operation].inputs[input];
636 model->main.operands[operand].numberOfConsumers--;
637 hidl_vec_removeAt(&model->main.operations[operation].inputs, input);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100638 });
639 }
640 }
641}
642
643///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
644
645static void removeOperationOutputTest(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 output = 0; output < model.main.operations[operation].outputs.size();
648 ++output) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100649 const std::string message = "removeOperationOutputTest: operation " +
650 std::to_string(operation) + ", output " +
651 std::to_string(output);
652 validate(device, message, model, [operation, output](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000653 hidl_vec_removeAt(&model->main.operations[operation].outputs, output);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100654 });
655 }
656 }
657}
658
659///////////////////////// MODEL VALIDATION /////////////////////////
660
661// TODO: remove model input
662// TODO: remove model output
663// TODO: add unused operation
664
665///////////////////////// ADD OPERATION INPUT /////////////////////////
666
667static bool addOperationInputSkip(const Operation& op) {
668 // Skip addOperationInputTest for the following operations.
669 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
670 // parameter.
671 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
672 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
673 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
674 return true;
675 }
676 return false;
677}
678
679static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000680 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
681 if (addOperationInputSkip(model.main.operations[operation])) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100682 continue;
683 }
684 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
685 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000686 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_INPUT);
687 hidl_vec_push_back(&model->main.operations[operation].inputs, index);
688 hidl_vec_push_back(&model->main.inputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100689 });
690 }
691}
692
693///////////////////////// ADD OPERATION OUTPUT /////////////////////////
694
695static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000696 for (size_t operation = 0; operation < model.main.operations.size(); ++operation) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100697 const std::string message =
698 "addOperationOutputTest: operation " + std::to_string(operation);
699 validate(device, message, model, [operation](Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000700 uint32_t index = addOperand(model, OperandLifeTime::SUBGRAPH_OUTPUT);
701 hidl_vec_push_back(&model->main.operations[operation].outputs, index);
702 hidl_vec_push_back(&model->main.outputIndexes, index);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100703 });
704 }
705}
706
707///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
708
709static const int32_t invalidExecutionPreferences[] = {
710 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
711 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
712};
713
714static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
715 for (int32_t preference : invalidExecutionPreferences) {
716 const std::string message =
717 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
718 validate(
719 device, message, model, [](Model*) {},
720 static_cast<ExecutionPreference>(preference));
721 }
722}
723
Michael Butler95899b32020-01-07 14:52:44 -0800724///////////////////////// DEADLINE /////////////////////////
725
726static void deadlineTest(const sp<IDevice>& device, const Model& model) {
727 const std::string message = "deadlineTest: deadline not supported";
728 const auto noop = [](Model*) {};
729 validate(device, message, model, noop, ExecutionPreference::FAST_SINGLE_ANSWER,
730 /*testDeadline=*/true);
731}
732
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100733////////////////////////// ENTRY POINT //////////////////////////////
734
Michael Butler95899b32020-01-07 14:52:44 -0800735void validateModel(const sp<IDevice>& device, const Model& model,
736 bool prepareModelDeadlineSupported) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100737 mutateOperandTypeTest(device, model);
738 mutateOperandRankTest(device, model);
739 mutateOperandScaleTest(device, model);
740 mutateOperandZeroPointTest(device, model);
741 mutateOperationOperandTypeTest(device, model);
742 mutateOperationTypeTest(device, model);
743 mutateOperationInputOperandIndexTest(device, model);
744 mutateOperationOutputOperandIndexTest(device, model);
745 removeOperandTest(device, model);
746 removeOperationTest(device, model);
747 removeOperationInputTest(device, model);
748 removeOperationOutputTest(device, model);
749 addOperationInputTest(device, model);
750 addOperationOutputTest(device, model);
751 mutateExecutionPreferenceTest(device, model);
Michael Butler95899b32020-01-07 14:52:44 -0800752 if (!prepareModelDeadlineSupported) {
753 deadlineTest(device, model);
754 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100755}
756
Lev Proleev26d1bc82019-08-30 11:57:18 +0100757} // namespace android::hardware::neuralnetworks::V1_3::vts::functional