blob: ad992c5c3238d0ec48c3a047c49a2b109db173ea [file] [log] [blame]
Slava Shklyaevfeb87a92018-09-12 14:52:02 +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
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010019#include "1.0/Utils.h"
20#include "1.2/Callbacks.h"
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010021#include "VtsHalNeuralnetworks.h"
22
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010023namespace android {
24namespace hardware {
25namespace neuralnetworks {
26namespace V1_2 {
27
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010028using V1_0::OperandLifeTime;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010029using V1_1::ExecutionPreference;
30
31namespace vts {
32namespace functional {
33
Xusong Wang1a06e772018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
35using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Xusong Wanged0822b2019-02-25 16:58:58 -080036using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010037
38///////////////////////// UTILITY FUNCTIONS /////////////////////////
39
40static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
41 const Model& model) {
42 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
43
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010044 Return<void> ret = device->getSupportedOperations_1_2(
45 model, [&](ErrorStatus status, const hidl_vec<bool>&) {
46 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
47 });
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010048 EXPECT_TRUE(ret.isOk());
49}
50
51static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
52 const Model& model, ExecutionPreference preference) {
53 SCOPED_TRACE(message + " [prepareModel_1_2]");
54
55 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
56 ASSERT_NE(nullptr, preparedModelCallback.get());
57 Return<ErrorStatus> prepareLaunchStatus =
Xusong Wanged0822b2019-02-25 16:58:58 -080058 device->prepareModel_1_2(model, preference, hidl_vec<hidl_handle>(),
59 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +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 Wang1a06e772018-10-31 08:43:12 -070066 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +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,
82 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
83 mutation(&model);
84 if (validExecutionPreference(preference)) {
85 validateGetSupportedOperations(device, message, model);
86 }
87 validatePrepareModel(device, message, model, preference);
88}
89
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010090static uint32_t addOperand(Model* model) {
91 return hidl_vec_push_back(&model->operands,
92 {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010093 .type = OperandType::INT32,
94 .dimensions = {},
95 .numberOfConsumers = 0,
96 .scale = 0.0f,
97 .zeroPoint = 0,
98 .lifetime = OperandLifeTime::MODEL_INPUT,
99 .location = {.poolIndex = 0, .offset = 0, .length = 0},
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100100 });
101}
102
103static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
104 uint32_t index = addOperand(model);
105 model->operands[index].numberOfConsumers = 1;
106 model->operands[index].lifetime = lifetime;
107 return index;
108}
109
110///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
111
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000112static const uint32_t invalidOperandTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000113 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MIN) - 1,
114 static_cast<uint32_t>(OperandTypeRange::FUNDAMENTAL_MAX) + 1,
115 static_cast<uint32_t>(OperandTypeRange::OEM_MIN) - 1,
116 static_cast<uint32_t>(OperandTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100117};
118
119static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
120 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000121 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100122 const std::string message = "mutateOperandTypeTest: operand " +
123 std::to_string(operand) + " set to value " +
124 std::to_string(invalidOperandType);
125 validate(device, message, model, [operand, invalidOperandType](Model* model) {
126 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
127 });
128 }
129 }
130}
131
132///////////////////////// VALIDATE OPERAND RANK /////////////////////////
133
134static uint32_t getInvalidRank(OperandType type) {
135 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800136 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100137 case OperandType::FLOAT32:
138 case OperandType::INT32:
139 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100140 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100141 return 1;
Lev Proleevdce38f12019-01-30 17:14:40 +0000142 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100143 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100144 case OperandType::TENSOR_FLOAT32:
145 case OperandType::TENSOR_INT32:
146 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800147 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800148 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000149 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000150 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100151 return 0;
152 default:
153 return 0;
154 }
155}
156
157static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
158 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
159 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
Xusong Wangd22c5232018-11-19 18:26:08 -0800160 if (invalidRank == 0) {
161 continue;
162 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100163 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
164 " has rank of " + std::to_string(invalidRank);
165 validate(device, message, model, [operand, invalidRank](Model* model) {
166 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
167 });
168 }
169}
170
171///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
172
173static float getInvalidScale(OperandType type) {
174 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800175 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100176 case OperandType::FLOAT32:
177 case OperandType::INT32:
178 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100179 case OperandType::BOOL:
Lev Proleevdce38f12019-01-30 17:14:40 +0000180 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100181 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100182 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000183 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100184 return 1.0f;
185 case OperandType::TENSOR_INT32:
186 return -1.0f;
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800187 case OperandType::TENSOR_QUANT8_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100188 case OperandType::TENSOR_QUANT8_ASYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800189 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000190 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100191 return 0.0f;
192 default:
193 return 0.0f;
194 }
195}
196
197static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
198 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
199 const float invalidScale = getInvalidScale(model.operands[operand].type);
200 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
201 " has scale of " + std::to_string(invalidScale);
202 validate(device, message, model, [operand, invalidScale](Model* model) {
203 model->operands[operand].scale = invalidScale;
204 });
205 }
206}
207
208///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
209
210static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
211 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800212 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100213 case OperandType::FLOAT32:
214 case OperandType::INT32:
215 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100216 case OperandType::BOOL:
Lev Proleevdce38f12019-01-30 17:14:40 +0000217 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100218 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100219 case OperandType::TENSOR_FLOAT32:
220 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000221 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100222 return {1};
223 case OperandType::TENSOR_QUANT8_ASYMM:
224 return {-1, 256};
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800225 case OperandType::TENSOR_QUANT8_SYMM:
226 return {-129, -1, 1, 128};
Xusong Wangcf6a9112019-01-16 18:32:24 -0800227 case OperandType::TENSOR_QUANT16_ASYMM:
228 return {-1, 65536};
Lev Proleev217c4072018-11-13 15:42:36 +0000229 case OperandType::TENSOR_QUANT16_SYMM:
230 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100231 default:
232 return {};
233 }
234}
235
236static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
237 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
238 const std::vector<int32_t> invalidZeroPoints =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100239 getInvalidZeroPoints(model.operands[operand].type);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100240 for (int32_t invalidZeroPoint : invalidZeroPoints) {
241 const std::string message = "mutateOperandZeroPointTest: operand " +
242 std::to_string(operand) + " has zero point of " +
243 std::to_string(invalidZeroPoint);
244 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
245 model->operands[operand].zeroPoint = invalidZeroPoint;
246 });
247 }
248 }
249}
250
251///////////////////////// VALIDATE EXTRA ??? /////////////////////////
252
253// TODO: Operand::lifetime
254// TODO: Operand::location
255
256///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
257
258static void mutateOperand(Operand* operand, OperandType type) {
259 Operand newOperand = *operand;
260 newOperand.type = type;
261 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800262 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100263 case OperandType::FLOAT32:
264 case OperandType::INT32:
265 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100266 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100267 newOperand.dimensions = hidl_vec<uint32_t>();
268 newOperand.scale = 0.0f;
269 newOperand.zeroPoint = 0;
270 break;
Lev Proleevdce38f12019-01-30 17:14:40 +0000271 case OperandType::TENSOR_BOOL8:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100272 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100273 case OperandType::TENSOR_FLOAT32:
274 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100275 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100276 newOperand.scale = 0.0f;
277 newOperand.zeroPoint = 0;
278 break;
279 case OperandType::TENSOR_INT32:
280 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100281 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100282 newOperand.zeroPoint = 0;
283 break;
284 case OperandType::TENSOR_QUANT8_ASYMM:
Hervé Guihot86a9fa02019-01-23 19:18:59 -0800285 case OperandType::TENSOR_QUANT8_SYMM:
Xusong Wangcf6a9112019-01-16 18:32:24 -0800286 case OperandType::TENSOR_QUANT16_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000287 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100288 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100289 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100290 newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f;
291 break;
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000292 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
293 newOperand.dimensions =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100294 operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1});
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000295 newOperand.scale = 0.0f;
296 newOperand.zeroPoint = 0;
297
298 SymmPerChannelQuantParams channelQuant;
299 channelQuant.channelDim = 0;
300 channelQuant.scales = hidl_vec<float>(
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100301 operand->dimensions.size() > 0 ? static_cast<size_t>(operand->dimensions[0])
302 : 0);
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000303 for (size_t i = 0; i < channelQuant.scales.size(); ++i) {
304 channelQuant.scales[i] = 1.0f;
305 }
306 newOperand.extraParams.channelQuant(std::move(channelQuant));
307 } break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100308 case OperandType::OEM:
309 case OperandType::TENSOR_OEM_BYTE:
310 default:
311 break;
312 }
313 *operand = newOperand;
314}
315
Xusong Wang1a2492f2018-10-05 11:49:13 -0700316static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) {
317 // Do not test OEM types
318 if (type == model.operands[operand].type || type == OperandType::OEM ||
319 type == OperandType::TENSOR_OEM_BYTE) {
320 return true;
321 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100322 for (const Operation& operation : model.operations) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700323 // Skip mutateOperationOperandTypeTest for the following operations.
324 // - LSH_PROJECTION's second argument is allowed to have any type.
Michael K. Sanders2a010122018-11-28 10:35:08 +0000325 // - ARGMIN and ARGMAX's first argument can be any of
326 // TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
327 // - CAST's argument can be any of TENSOR_(FLOAT16|FLOAT32|INT32|QUANT8_ASYMM).
Michael K. Sanders41e67322018-12-06 12:34:07 +0000328 // - RANDOM_MULTINOMIAL's argument can be either TENSOR_FLOAT16 or TENSOR_FLOAT32.
Lev Proleevdce38f12019-01-30 17:14:40 +0000329 // - DEQUANTIZE input can be any of
330 // TENSOR_(QUANT8_ASYMM|QUANT8_SYMM|QUANT8_SYMM_PER_CHANNEL), output can
331 // be of either TENSOR_FLOAT16 or TENSOR_FLOAT32.
332 // - QUANTIZE input can be either TENSOR_FLOAT16 or TENSOR_FLOAT32
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000333 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000334 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000335 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleev2d982642019-01-15 17:49:24 +0000336 // - TRANSPOSE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang1a2492f2018-10-05 11:49:13 -0700337 switch (operation.type) {
338 case OperationType::LSH_PROJECTION: {
339 if (operand == operation.inputs[1]) {
340 return true;
341 }
342 } break;
343 case OperationType::CAST:
344 case OperationType::ARGMAX:
345 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000346 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
347 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700348 return true;
349 }
350 } break;
Lev Proleevdce38f12019-01-30 17:14:40 +0000351 case OperationType::QUANTIZE:
Michael K. Sanders41e67322018-12-06 12:34:07 +0000352 case OperationType::RANDOM_MULTINOMIAL: {
Lev Proleevdce38f12019-01-30 17:14:40 +0000353 if (operand == operation.inputs[0] &&
354 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
355 return true;
356 }
357 } break;
358 case OperationType::DEQUANTIZE: {
359 if (operand == operation.inputs[0] &&
360 (type == OperandType::TENSOR_QUANT8_ASYMM ||
361 type == OperandType::TENSOR_QUANT8_SYMM ||
362 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
363 return true;
364 }
365 if (operand == operation.outputs[0] &&
366 (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32)) {
Michael K. Sanders41e67322018-12-06 12:34:07 +0000367 return true;
368 }
369 } break;
Lev Proleev2d982642019-01-15 17:49:24 +0000370 case OperationType::TRANSPOSE_CONV_2D:
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000371 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000372 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000373 case OperationType::CONV_2D: {
Xusong Wangf80e3e72019-03-13 16:24:34 -0700374 if (operand == operation.inputs[1] &&
375 (type == OperandType::TENSOR_QUANT8_ASYMM ||
376 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000377 return true;
378 }
379 } break;
Xusong Wang1a2492f2018-10-05 11:49:13 -0700380 default:
381 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100382 }
383 }
384 return false;
385}
386
387static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
388 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100389 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700390 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100391 continue;
392 }
393 const std::string message = "mutateOperationOperandTypeTest: operand " +
394 std::to_string(operand) + " set to type " +
395 toString(invalidOperandType);
396 validate(device, message, model, [operand, invalidOperandType](Model* model) {
397 mutateOperand(&model->operands[operand], invalidOperandType);
398 });
399 }
400 }
401}
402
403///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
404
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000405static const uint32_t invalidOperationTypes[] = {
Slava Shklyaev2739b2e2019-01-17 15:37:05 +0000406 static_cast<uint32_t>(OperationTypeRange::FUNDAMENTAL_MAX) + 1,
407 static_cast<uint32_t>(OperationTypeRange::OEM_MIN) - 1,
408 static_cast<uint32_t>(OperationTypeRange::OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100409};
410
411static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
412 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000413 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100414 const std::string message = "mutateOperationTypeTest: operation " +
415 std::to_string(operation) + " set to value " +
416 std::to_string(invalidOperationType);
417 validate(device, message, model, [operation, invalidOperationType](Model* model) {
418 model->operations[operation].type =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100419 static_cast<OperationType>(invalidOperationType);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100420 });
421 }
422 }
423}
424
425///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
426
427static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
428 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
429 const uint32_t invalidOperand = model.operands.size();
430 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
431 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
432 std::to_string(operation) + " input " +
433 std::to_string(input);
434 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
435 model->operations[operation].inputs[input] = invalidOperand;
436 });
437 }
438 }
439}
440
441///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
442
443static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
444 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
445 const uint32_t invalidOperand = model.operands.size();
446 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
447 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
448 std::to_string(operation) + " output " +
449 std::to_string(output);
450 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
451 model->operations[operation].outputs[output] = invalidOperand;
452 });
453 }
454 }
455}
456
457///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
458
459static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
460 if (vec) {
461 // remove elements matching "value"
462 auto last = std::remove(vec->begin(), vec->end(), value);
463 vec->resize(std::distance(vec->begin(), last));
464
465 // decrement elements exceeding "value"
466 std::transform(vec->begin(), vec->end(), vec->begin(),
467 [value](uint32_t v) { return v > value ? v-- : v; });
468 }
469}
470
471static void removeOperand(Model* model, uint32_t index) {
472 hidl_vec_removeAt(&model->operands, index);
473 for (Operation& operation : model->operations) {
474 removeValueAndDecrementGreaterValues(&operation.inputs, index);
475 removeValueAndDecrementGreaterValues(&operation.outputs, index);
476 }
477 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
478 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
479}
480
Xusong Wang1a2492f2018-10-05 11:49:13 -0700481static bool removeOperandSkip(size_t operand, const Model& model) {
482 for (const Operation& operation : model.operations) {
483 // Skip removeOperandTest for the following operations.
484 // - SPLIT's outputs are not checked during prepareModel.
485 if (operation.type == OperationType::SPLIT) {
486 for (const size_t outOprand : operation.outputs) {
487 if (operand == outOprand) {
488 return true;
489 }
490 }
491 }
Viet Dang723ff2d2019-03-28 17:22:56 +0000492 // BIDIRECTIONAL_SEQUENCE_LSTM and BIDIRECTIONAL_SEQUENCE_RNN can have
493 // either one or two outputs depending on their mergeOutputs parameter.
494 if (operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_LSTM ||
495 operation.type == OperationType::BIDIRECTIONAL_SEQUENCE_RNN) {
496 for (const size_t outOprand : operation.outputs) {
497 if (operand == outOprand) {
498 return true;
Lev Proleevdce38f12019-01-30 17:14:40 +0000499 }
Viet Dang723ff2d2019-03-28 17:22:56 +0000500 }
Lev Proleevdce38f12019-01-30 17:14:40 +0000501 }
Xusong Wang1a2492f2018-10-05 11:49:13 -0700502 }
503 return false;
504}
505
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100506static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
507 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700508 if (removeOperandSkip(operand, model)) {
509 continue;
510 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100511 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
512 validate(device, message, model,
513 [operand](Model* model) { removeOperand(model, operand); });
514 }
515}
516
517///////////////////////// REMOVE OPERATION /////////////////////////
518
519static void removeOperation(Model* model, uint32_t index) {
520 for (uint32_t operand : model->operations[index].inputs) {
521 model->operands[operand].numberOfConsumers--;
522 }
523 hidl_vec_removeAt(&model->operations, index);
524}
525
526static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
527 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
528 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
529 validate(device, message, model,
530 [operation](Model* model) { removeOperation(model, operation); });
531 }
532}
533
534///////////////////////// REMOVE OPERATION INPUT /////////////////////////
535
Xusong Wang1a2492f2018-10-05 11:49:13 -0700536static bool removeOperationInputSkip(const Operation& op, size_t input) {
537 // Skip removeOperationInputTest for the following operations.
538 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
539 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
540 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
541 // layout parameter.
542 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
543 // parameter.
544 switch (op.type) {
545 case OperationType::CONCATENATION: {
546 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
547 return true;
548 }
549 } break;
550 case OperationType::DEPTHWISE_CONV_2D: {
551 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
552 return true;
553 }
554 } break;
555 case OperationType::CONV_2D:
556 case OperationType::AVERAGE_POOL_2D:
557 case OperationType::MAX_POOL_2D:
558 case OperationType::L2_POOL_2D: {
559 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
560 return true;
561 }
562 } break;
563 case OperationType::RESIZE_BILINEAR: {
564 if (op.inputs.size() == 4 && input == 3) {
565 return true;
566 }
567 } break;
568 case OperationType::SPACE_TO_DEPTH:
569 case OperationType::DEPTH_TO_SPACE:
570 case OperationType::BATCH_TO_SPACE_ND: {
571 if (op.inputs.size() == 3 && input == 2) {
572 return true;
573 }
574 } break;
575 case OperationType::SPACE_TO_BATCH_ND: {
576 if (op.inputs.size() == 4 && input == 3) {
577 return true;
578 }
579 } break;
580 case OperationType::L2_NORMALIZATION: {
581 if (op.inputs.size() == 2 && input == 1) {
582 return true;
583 }
584 } break;
585 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
586 if (op.inputs.size() == 6 && input == 5) {
587 return true;
588 }
589 } break;
590 case OperationType::SOFTMAX: {
591 if (op.inputs.size() == 3 && input == 2) {
592 return true;
593 }
594 } break;
595 default:
596 break;
597 }
598 return false;
599}
600
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100601static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
602 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
603 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
604 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700605 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100606 continue;
607 }
608 const std::string message = "removeOperationInputTest: operation " +
609 std::to_string(operation) + ", input " +
610 std::to_string(input);
611 validate(device, message, model, [operation, input](Model* model) {
612 uint32_t operand = model->operations[operation].inputs[input];
613 model->operands[operand].numberOfConsumers--;
614 hidl_vec_removeAt(&model->operations[operation].inputs, input);
615 });
616 }
617 }
618}
619
620///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
621
622static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
623 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
624 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
625 const std::string message = "removeOperationOutputTest: operation " +
626 std::to_string(operation) + ", output " +
627 std::to_string(output);
628 validate(device, message, model, [operation, output](Model* model) {
629 hidl_vec_removeAt(&model->operations[operation].outputs, output);
630 });
631 }
632 }
633}
634
635///////////////////////// MODEL VALIDATION /////////////////////////
636
637// TODO: remove model input
638// TODO: remove model output
639// TODO: add unused operation
640
641///////////////////////// ADD OPERATION INPUT /////////////////////////
642
Xusong Wang1a2492f2018-10-05 11:49:13 -0700643static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700644 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700645 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
646 // parameter.
647 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
648 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
649 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700650 return true;
651 }
652 return false;
653}
654
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100655static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
656 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700657 if (addOperationInputSkip(model.operations[operation])) {
658 continue;
659 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100660 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
661 validate(device, message, model, [operation](Model* model) {
662 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
663 hidl_vec_push_back(&model->operations[operation].inputs, index);
664 hidl_vec_push_back(&model->inputIndexes, index);
665 });
666 }
667}
668
669///////////////////////// ADD OPERATION OUTPUT /////////////////////////
670
671static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
672 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
673 const std::string message =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100674 "addOperationOutputTest: operation " + std::to_string(operation);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100675 validate(device, message, model, [operation](Model* model) {
676 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
677 hidl_vec_push_back(&model->operations[operation].outputs, index);
678 hidl_vec_push_back(&model->outputIndexes, index);
679 });
680 }
681}
682
683///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
684
685static const int32_t invalidExecutionPreferences[] = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100686 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
687 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100688};
689
690static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
691 for (int32_t preference : invalidExecutionPreferences) {
692 const std::string message =
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100693 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100694 validate(device, message, model, [](Model*) {},
695 static_cast<ExecutionPreference>(preference));
696 }
697}
698
699////////////////////////// ENTRY POINT //////////////////////////////
700
701void ValidationTest::validateModel(const Model& model) {
702 mutateOperandTypeTest(device, model);
703 mutateOperandRankTest(device, model);
704 mutateOperandScaleTest(device, model);
705 mutateOperandZeroPointTest(device, model);
706 mutateOperationOperandTypeTest(device, model);
707 mutateOperationTypeTest(device, model);
708 mutateOperationInputOperandIndexTest(device, model);
709 mutateOperationOutputOperandIndexTest(device, model);
710 removeOperandTest(device, model);
711 removeOperationTest(device, model);
712 removeOperationInputTest(device, model);
713 removeOperationOutputTest(device, model);
714 addOperationInputTest(device, model);
715 addOperationOutputTest(device, model);
716 mutateExecutionPreferenceTest(device, model);
717}
718
719} // namespace functional
720} // namespace vts
721} // namespace V1_2
722} // namespace neuralnetworks
723} // namespace hardware
724} // namespace android