blob: 5ff7ce58b91b143f8f766185ef392dcc8707d0e0 [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
19#include "VtsHalNeuralnetworks.h"
20
21#include "Callbacks.h"
22
23namespace 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;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010036
37///////////////////////// UTILITY FUNCTIONS /////////////////////////
38
39static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message,
40 const Model& model) {
41 SCOPED_TRACE(message + " [getSupportedOperations_1_2]");
42
43 Return<void> ret =
44 device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) {
45 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
46 });
47 EXPECT_TRUE(ret.isOk());
48}
49
50static void validatePrepareModel(const sp<IDevice>& device, const std::string& message,
51 const Model& model, ExecutionPreference preference) {
52 SCOPED_TRACE(message + " [prepareModel_1_2]");
53
54 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
55 ASSERT_NE(nullptr, preparedModelCallback.get());
56 Return<ErrorStatus> prepareLaunchStatus =
57 device->prepareModel_1_2(model, preference, preparedModelCallback);
58 ASSERT_TRUE(prepareLaunchStatus.isOk());
59 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus));
60
61 preparedModelCallback->wait();
62 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
63 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus);
Xusong Wang1a06e772018-10-31 08:43:12 -070064 sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010065 ASSERT_EQ(nullptr, preparedModel.get());
66}
67
68static bool validExecutionPreference(ExecutionPreference preference) {
69 return preference == ExecutionPreference::LOW_POWER ||
70 preference == ExecutionPreference::FAST_SINGLE_ANSWER ||
71 preference == ExecutionPreference::SUSTAINED_SPEED;
72}
73
74// Primary validation function. This function will take a valid model, apply a
75// mutation to it to invalidate the model, then pass it to interface calls that
76// use the model. Note that the model here is passed by value, and any mutation
77// to the model does not leave this function.
78static void validate(const sp<IDevice>& device, const std::string& message, Model model,
79 const std::function<void(Model*)>& mutation,
80 ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) {
81 mutation(&model);
82 if (validExecutionPreference(preference)) {
83 validateGetSupportedOperations(device, message, model);
84 }
85 validatePrepareModel(device, message, model, preference);
86}
87
88// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
89// so this is efficiently accomplished by moving the element to the end and
90// resizing the hidl_vec to one less.
91template <typename Type>
92static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
93 if (vec) {
94 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
95 vec->resize(vec->size() - 1);
96 }
97}
98
99template <typename Type>
100static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
101 // assume vec is valid
102 const uint32_t index = vec->size();
103 vec->resize(index + 1);
104 (*vec)[index] = value;
105 return index;
106}
107
108static uint32_t addOperand(Model* model) {
109 return hidl_vec_push_back(&model->operands,
110 {
111 .type = OperandType::INT32,
112 .dimensions = {},
113 .numberOfConsumers = 0,
114 .scale = 0.0f,
115 .zeroPoint = 0,
116 .lifetime = OperandLifeTime::MODEL_INPUT,
117 .location = {.poolIndex = 0, .offset = 0, .length = 0},
118 });
119}
120
121static uint32_t addOperand(Model* model, OperandLifeTime lifetime) {
122 uint32_t index = addOperand(model);
123 model->operands[index].numberOfConsumers = 1;
124 model->operands[index].lifetime = lifetime;
125 return index;
126}
127
128///////////////////////// VALIDATE MODEL OPERAND TYPE /////////////////////////
129
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000130static const uint32_t invalidOperandTypes[] = {
131 static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MIN) - 1,
132 static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MAX) + 1,
133 static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MIN) - 1,
134 static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100135};
136
137static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) {
138 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000139 for (uint32_t invalidOperandType : invalidOperandTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100140 const std::string message = "mutateOperandTypeTest: operand " +
141 std::to_string(operand) + " set to value " +
142 std::to_string(invalidOperandType);
143 validate(device, message, model, [operand, invalidOperandType](Model* model) {
144 model->operands[operand].type = static_cast<OperandType>(invalidOperandType);
145 });
146 }
147 }
148}
149
150///////////////////////// VALIDATE OPERAND RANK /////////////////////////
151
152static uint32_t getInvalidRank(OperandType type) {
153 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800154 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100155 case OperandType::FLOAT32:
156 case OperandType::INT32:
157 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100158 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100159 return 1;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100160 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100161 case OperandType::TENSOR_FLOAT32:
162 case OperandType::TENSOR_INT32:
163 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000164 case OperandType::TENSOR_QUANT16_SYMM:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000165 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100166 return 0;
167 default:
168 return 0;
169 }
170}
171
172static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) {
173 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
174 const uint32_t invalidRank = getInvalidRank(model.operands[operand].type);
175 const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) +
176 " has rank of " + std::to_string(invalidRank);
177 validate(device, message, model, [operand, invalidRank](Model* model) {
178 model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0);
179 });
180 }
181}
182
183///////////////////////// VALIDATE OPERAND SCALE /////////////////////////
184
185static float getInvalidScale(OperandType type) {
186 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800187 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100188 case OperandType::FLOAT32:
189 case OperandType::INT32:
190 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100191 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100192 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100193 case OperandType::TENSOR_FLOAT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000194 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100195 return 1.0f;
196 case OperandType::TENSOR_INT32:
197 return -1.0f;
198 case OperandType::TENSOR_QUANT8_ASYMM:
Lev Proleev217c4072018-11-13 15:42:36 +0000199 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100200 return 0.0f;
201 default:
202 return 0.0f;
203 }
204}
205
206static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) {
207 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
208 const float invalidScale = getInvalidScale(model.operands[operand].type);
209 const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) +
210 " has scale of " + std::to_string(invalidScale);
211 validate(device, message, model, [operand, invalidScale](Model* model) {
212 model->operands[operand].scale = invalidScale;
213 });
214 }
215}
216
217///////////////////////// VALIDATE OPERAND ZERO POINT /////////////////////////
218
219static std::vector<int32_t> getInvalidZeroPoints(OperandType type) {
220 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800221 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100222 case OperandType::FLOAT32:
223 case OperandType::INT32:
224 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100225 case OperandType::BOOL:
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100226 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100227 case OperandType::TENSOR_FLOAT32:
228 case OperandType::TENSOR_INT32:
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000229 case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100230 return {1};
231 case OperandType::TENSOR_QUANT8_ASYMM:
232 return {-1, 256};
Lev Proleev217c4072018-11-13 15:42:36 +0000233 case OperandType::TENSOR_QUANT16_SYMM:
234 return {-32769, -1, 1, 32768};
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100235 default:
236 return {};
237 }
238}
239
240static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) {
241 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
242 const std::vector<int32_t> invalidZeroPoints =
243 getInvalidZeroPoints(model.operands[operand].type);
244 for (int32_t invalidZeroPoint : invalidZeroPoints) {
245 const std::string message = "mutateOperandZeroPointTest: operand " +
246 std::to_string(operand) + " has zero point of " +
247 std::to_string(invalidZeroPoint);
248 validate(device, message, model, [operand, invalidZeroPoint](Model* model) {
249 model->operands[operand].zeroPoint = invalidZeroPoint;
250 });
251 }
252 }
253}
254
255///////////////////////// VALIDATE EXTRA ??? /////////////////////////
256
257// TODO: Operand::lifetime
258// TODO: Operand::location
259
260///////////////////////// VALIDATE OPERATION OPERAND TYPE /////////////////////////
261
262static void mutateOperand(Operand* operand, OperandType type) {
263 Operand newOperand = *operand;
264 newOperand.type = type;
265 switch (type) {
Xusong Wang56666262018-12-05 14:21:51 -0800266 case OperandType::FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100267 case OperandType::FLOAT32:
268 case OperandType::INT32:
269 case OperandType::UINT32:
Lev Proleev08662c62018-10-01 11:18:31 +0100270 case OperandType::BOOL:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100271 newOperand.dimensions = hidl_vec<uint32_t>();
272 newOperand.scale = 0.0f;
273 newOperand.zeroPoint = 0;
274 break;
Michael K. Sanders5dd84122018-10-12 09:10:15 +0100275 case OperandType::TENSOR_FLOAT16:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100276 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:
Lev Proleev217c4072018-11-13 15:42:36 +0000288 case OperandType::TENSOR_QUANT16_SYMM:
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100289 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;
Przemyslaw Szczepaniak4766f8b2018-11-08 15:22:17 +0000293 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]) : 0);
303 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.
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000329 // - CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000330 // - DEPTHWISE_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000331 // - GROUPED_CONV_2D filter type (arg 1) can be QUANT8_ASYMM or QUANT8_SYMM_PER_CHANNEL
Xusong Wang1a2492f2018-10-05 11:49:13 -0700332 switch (operation.type) {
333 case OperationType::LSH_PROJECTION: {
334 if (operand == operation.inputs[1]) {
335 return true;
336 }
337 } break;
338 case OperationType::CAST:
339 case OperationType::ARGMAX:
340 case OperationType::ARGMIN: {
Michael K. Sanders2a010122018-11-28 10:35:08 +0000341 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32 ||
342 type == OperandType::TENSOR_INT32 || type == OperandType::TENSOR_QUANT8_ASYMM) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700343 return true;
344 }
345 } break;
Michael K. Sanders41e67322018-12-06 12:34:07 +0000346 case OperationType::RANDOM_MULTINOMIAL: {
347 if (type == OperandType::TENSOR_FLOAT16 || type == OperandType::TENSOR_FLOAT32) {
348 return true;
349 }
350 } break;
Lev Proleevb35eb1e2019-01-15 17:53:46 +0000351 case OperationType::GROUPED_CONV_2D:
Przemyslaw Szczepaniak725f6842018-12-11 13:42:27 +0000352 case OperationType::DEPTHWISE_CONV_2D:
Przemyslaw Szczepaniak2fadc842018-11-26 14:10:06 +0000353 case OperationType::CONV_2D: {
354 if (operand == 1 && (type == OperandType::TENSOR_QUANT8_ASYMM ||
355 type == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)) {
356 return true;
357 }
358 } break;
Xusong Wang1a2492f2018-10-05 11:49:13 -0700359 default:
360 break;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100361 }
362 }
363 return false;
364}
365
366static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) {
367 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100368 for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700369 if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100370 continue;
371 }
372 const std::string message = "mutateOperationOperandTypeTest: operand " +
373 std::to_string(operand) + " set to type " +
374 toString(invalidOperandType);
375 validate(device, message, model, [operand, invalidOperandType](Model* model) {
376 mutateOperand(&model->operands[operand], invalidOperandType);
377 });
378 }
379 }
380}
381
382///////////////////////// VALIDATE MODEL OPERATION TYPE /////////////////////////
383
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000384static const uint32_t invalidOperationTypes[] = {
385 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MIN) - 1,
386 static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MAX) + 1,
387 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MIN) - 1,
388 static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MAX) + 1,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100389};
390
391static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) {
392 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Michael K. Sanders9233dbe2018-10-30 15:16:54 +0000393 for (uint32_t invalidOperationType : invalidOperationTypes) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100394 const std::string message = "mutateOperationTypeTest: operation " +
395 std::to_string(operation) + " set to value " +
396 std::to_string(invalidOperationType);
397 validate(device, message, model, [operation, invalidOperationType](Model* model) {
398 model->operations[operation].type =
399 static_cast<OperationType>(invalidOperationType);
400 });
401 }
402 }
403}
404
405///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX /////////////////////////
406
407static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
408 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
409 const uint32_t invalidOperand = model.operands.size();
410 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
411 const std::string message = "mutateOperationInputOperandIndexTest: operation " +
412 std::to_string(operation) + " input " +
413 std::to_string(input);
414 validate(device, message, model, [operation, input, invalidOperand](Model* model) {
415 model->operations[operation].inputs[input] = invalidOperand;
416 });
417 }
418 }
419}
420
421///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX /////////////////////////
422
423static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) {
424 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
425 const uint32_t invalidOperand = model.operands.size();
426 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
427 const std::string message = "mutateOperationOutputOperandIndexTest: operation " +
428 std::to_string(operation) + " output " +
429 std::to_string(output);
430 validate(device, message, model, [operation, output, invalidOperand](Model* model) {
431 model->operations[operation].outputs[output] = invalidOperand;
432 });
433 }
434 }
435}
436
437///////////////////////// REMOVE OPERAND FROM EVERYTHING /////////////////////////
438
439static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) {
440 if (vec) {
441 // remove elements matching "value"
442 auto last = std::remove(vec->begin(), vec->end(), value);
443 vec->resize(std::distance(vec->begin(), last));
444
445 // decrement elements exceeding "value"
446 std::transform(vec->begin(), vec->end(), vec->begin(),
447 [value](uint32_t v) { return v > value ? v-- : v; });
448 }
449}
450
451static void removeOperand(Model* model, uint32_t index) {
452 hidl_vec_removeAt(&model->operands, index);
453 for (Operation& operation : model->operations) {
454 removeValueAndDecrementGreaterValues(&operation.inputs, index);
455 removeValueAndDecrementGreaterValues(&operation.outputs, index);
456 }
457 removeValueAndDecrementGreaterValues(&model->inputIndexes, index);
458 removeValueAndDecrementGreaterValues(&model->outputIndexes, index);
459}
460
Xusong Wang1a2492f2018-10-05 11:49:13 -0700461static bool removeOperandSkip(size_t operand, const Model& model) {
462 for (const Operation& operation : model.operations) {
463 // Skip removeOperandTest for the following operations.
464 // - SPLIT's outputs are not checked during prepareModel.
465 if (operation.type == OperationType::SPLIT) {
466 for (const size_t outOprand : operation.outputs) {
467 if (operand == outOprand) {
468 return true;
469 }
470 }
471 }
472 }
473 return false;
474}
475
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100476static void removeOperandTest(const sp<IDevice>& device, const Model& model) {
477 for (size_t operand = 0; operand < model.operands.size(); ++operand) {
Xusong Wang1a2492f2018-10-05 11:49:13 -0700478 if (removeOperandSkip(operand, model)) {
479 continue;
480 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100481 const std::string message = "removeOperandTest: operand " + std::to_string(operand);
482 validate(device, message, model,
483 [operand](Model* model) { removeOperand(model, operand); });
484 }
485}
486
487///////////////////////// REMOVE OPERATION /////////////////////////
488
489static void removeOperation(Model* model, uint32_t index) {
490 for (uint32_t operand : model->operations[index].inputs) {
491 model->operands[operand].numberOfConsumers--;
492 }
493 hidl_vec_removeAt(&model->operations, index);
494}
495
496static void removeOperationTest(const sp<IDevice>& device, const Model& model) {
497 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
498 const std::string message = "removeOperationTest: operation " + std::to_string(operation);
499 validate(device, message, model,
500 [operation](Model* model) { removeOperation(model, operation); });
501 }
502}
503
504///////////////////////// REMOVE OPERATION INPUT /////////////////////////
505
Xusong Wang1a2492f2018-10-05 11:49:13 -0700506static bool removeOperationInputSkip(const Operation& op, size_t input) {
507 // Skip removeOperationInputTest for the following operations.
508 // - CONCATENATION has at least 2 inputs, with the last element being INT32.
509 // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR,
510 // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional
511 // layout parameter.
512 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis
513 // parameter.
514 switch (op.type) {
515 case OperationType::CONCATENATION: {
516 if (op.inputs.size() > 2 && input != op.inputs.size() - 1) {
517 return true;
518 }
519 } break;
520 case OperationType::DEPTHWISE_CONV_2D: {
521 if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) {
522 return true;
523 }
524 } break;
525 case OperationType::CONV_2D:
526 case OperationType::AVERAGE_POOL_2D:
527 case OperationType::MAX_POOL_2D:
528 case OperationType::L2_POOL_2D: {
529 if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) {
530 return true;
531 }
532 } break;
533 case OperationType::RESIZE_BILINEAR: {
534 if (op.inputs.size() == 4 && input == 3) {
535 return true;
536 }
537 } break;
538 case OperationType::SPACE_TO_DEPTH:
539 case OperationType::DEPTH_TO_SPACE:
540 case OperationType::BATCH_TO_SPACE_ND: {
541 if (op.inputs.size() == 3 && input == 2) {
542 return true;
543 }
544 } break;
545 case OperationType::SPACE_TO_BATCH_ND: {
546 if (op.inputs.size() == 4 && input == 3) {
547 return true;
548 }
549 } break;
550 case OperationType::L2_NORMALIZATION: {
551 if (op.inputs.size() == 2 && input == 1) {
552 return true;
553 }
554 } break;
555 case OperationType::LOCAL_RESPONSE_NORMALIZATION: {
556 if (op.inputs.size() == 6 && input == 5) {
557 return true;
558 }
559 } break;
560 case OperationType::SOFTMAX: {
561 if (op.inputs.size() == 3 && input == 2) {
562 return true;
563 }
564 } break;
565 default:
566 break;
567 }
568 return false;
569}
570
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100571static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) {
572 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
573 for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) {
574 const Operation& op = model.operations[operation];
Xusong Wang1a2492f2018-10-05 11:49:13 -0700575 if (removeOperationInputSkip(op, input)) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100576 continue;
577 }
578 const std::string message = "removeOperationInputTest: operation " +
579 std::to_string(operation) + ", input " +
580 std::to_string(input);
581 validate(device, message, model, [operation, input](Model* model) {
582 uint32_t operand = model->operations[operation].inputs[input];
583 model->operands[operand].numberOfConsumers--;
584 hidl_vec_removeAt(&model->operations[operation].inputs, input);
585 });
586 }
587 }
588}
589
590///////////////////////// REMOVE OPERATION OUTPUT /////////////////////////
591
592static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) {
593 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
594 for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) {
595 const std::string message = "removeOperationOutputTest: operation " +
596 std::to_string(operation) + ", output " +
597 std::to_string(output);
598 validate(device, message, model, [operation, output](Model* model) {
599 hidl_vec_removeAt(&model->operations[operation].outputs, output);
600 });
601 }
602 }
603}
604
605///////////////////////// MODEL VALIDATION /////////////////////////
606
607// TODO: remove model input
608// TODO: remove model output
609// TODO: add unused operation
610
611///////////////////////// ADD OPERATION INPUT /////////////////////////
612
Xusong Wang1a2492f2018-10-05 11:49:13 -0700613static bool addOperationInputSkip(const Operation& op) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700614 // Skip addOperationInputTest for the following operations.
Xusong Wang1a2492f2018-10-05 11:49:13 -0700615 // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis
616 // parameter.
617 if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) ||
618 (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) ||
619 (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700620 return true;
621 }
622 return false;
623}
624
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100625static void addOperationInputTest(const sp<IDevice>& device, const Model& model) {
626 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
Xusong Wanga6b1dc72018-10-22 13:49:00 -0700627 if (addOperationInputSkip(model.operations[operation])) {
628 continue;
629 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100630 const std::string message = "addOperationInputTest: operation " + std::to_string(operation);
631 validate(device, message, model, [operation](Model* model) {
632 uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT);
633 hidl_vec_push_back(&model->operations[operation].inputs, index);
634 hidl_vec_push_back(&model->inputIndexes, index);
635 });
636 }
637}
638
639///////////////////////// ADD OPERATION OUTPUT /////////////////////////
640
641static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) {
642 for (size_t operation = 0; operation < model.operations.size(); ++operation) {
643 const std::string message =
644 "addOperationOutputTest: operation " + std::to_string(operation);
645 validate(device, message, model, [operation](Model* model) {
646 uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT);
647 hidl_vec_push_back(&model->operations[operation].outputs, index);
648 hidl_vec_push_back(&model->outputIndexes, index);
649 });
650 }
651}
652
653///////////////////////// VALIDATE EXECUTION PREFERENCE /////////////////////////
654
655static const int32_t invalidExecutionPreferences[] = {
656 static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound
657 static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound
658};
659
660static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) {
661 for (int32_t preference : invalidExecutionPreferences) {
662 const std::string message =
663 "mutateExecutionPreferenceTest: preference " + std::to_string(preference);
664 validate(device, message, model, [](Model*) {},
665 static_cast<ExecutionPreference>(preference));
666 }
667}
668
669////////////////////////// ENTRY POINT //////////////////////////////
670
671void ValidationTest::validateModel(const Model& model) {
672 mutateOperandTypeTest(device, model);
673 mutateOperandRankTest(device, model);
674 mutateOperandScaleTest(device, model);
675 mutateOperandZeroPointTest(device, model);
676 mutateOperationOperandTypeTest(device, model);
677 mutateOperationTypeTest(device, model);
678 mutateOperationInputOperandIndexTest(device, model);
679 mutateOperationOutputOperandIndexTest(device, model);
680 removeOperandTest(device, model);
681 removeOperationTest(device, model);
682 removeOperationInputTest(device, model);
683 removeOperationOutputTest(device, model);
684 addOperationInputTest(device, model);
685 addOperationOutputTest(device, model);
686 mutateExecutionPreferenceTest(device, model);
687}
688
689} // namespace functional
690} // namespace vts
691} // namespace V1_2
692} // namespace neuralnetworks
693} // namespace hardware
694} // namespace android