Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace neuralnetworks { |
| 26 | namespace V1_2 { |
| 27 | |
| 28 | using V1_0::IPreparedModel; |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 29 | using V1_0::OperandLifeTime; |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 30 | using V1_1::ExecutionPreference; |
| 31 | |
| 32 | namespace vts { |
| 33 | namespace functional { |
| 34 | |
| 35 | using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback; |
| 36 | using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback; |
| 37 | |
| 38 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 39 | |
| 40 | static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message, |
| 41 | const Model& model) { |
| 42 | SCOPED_TRACE(message + " [getSupportedOperations_1_2]"); |
| 43 | |
| 44 | Return<void> ret = |
| 45 | device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) { |
| 46 | EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status); |
| 47 | }); |
| 48 | EXPECT_TRUE(ret.isOk()); |
| 49 | } |
| 50 | |
| 51 | static 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 = |
| 58 | device->prepareModel_1_2(model, preference, preparedModelCallback); |
| 59 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 60 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus)); |
| 61 | |
| 62 | preparedModelCallback->wait(); |
| 63 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
| 64 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus); |
| 65 | sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel(); |
| 66 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 67 | } |
| 68 | |
| 69 | static bool validExecutionPreference(ExecutionPreference preference) { |
| 70 | return preference == ExecutionPreference::LOW_POWER || |
| 71 | preference == ExecutionPreference::FAST_SINGLE_ANSWER || |
| 72 | preference == ExecutionPreference::SUSTAINED_SPEED; |
| 73 | } |
| 74 | |
| 75 | // Primary validation function. This function will take a valid model, apply a |
| 76 | // mutation to it to invalidate the model, then pass it to interface calls that |
| 77 | // use the model. Note that the model here is passed by value, and any mutation |
| 78 | // to the model does not leave this function. |
| 79 | static void validate(const sp<IDevice>& device, const std::string& message, Model model, |
| 80 | const std::function<void(Model*)>& mutation, |
| 81 | ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) { |
| 82 | mutation(&model); |
| 83 | if (validExecutionPreference(preference)) { |
| 84 | validateGetSupportedOperations(device, message, model); |
| 85 | } |
| 86 | validatePrepareModel(device, message, model, preference); |
| 87 | } |
| 88 | |
| 89 | // Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation, |
| 90 | // so this is efficiently accomplished by moving the element to the end and |
| 91 | // resizing the hidl_vec to one less. |
| 92 | template <typename Type> |
| 93 | static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) { |
| 94 | if (vec) { |
| 95 | std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end()); |
| 96 | vec->resize(vec->size() - 1); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | template <typename Type> |
| 101 | static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) { |
| 102 | // assume vec is valid |
| 103 | const uint32_t index = vec->size(); |
| 104 | vec->resize(index + 1); |
| 105 | (*vec)[index] = value; |
| 106 | return index; |
| 107 | } |
| 108 | |
| 109 | static uint32_t addOperand(Model* model) { |
| 110 | return hidl_vec_push_back(&model->operands, |
| 111 | { |
| 112 | .type = OperandType::INT32, |
| 113 | .dimensions = {}, |
| 114 | .numberOfConsumers = 0, |
| 115 | .scale = 0.0f, |
| 116 | .zeroPoint = 0, |
| 117 | .lifetime = OperandLifeTime::MODEL_INPUT, |
| 118 | .location = {.poolIndex = 0, .offset = 0, .length = 0}, |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | static uint32_t addOperand(Model* model, OperandLifeTime lifetime) { |
| 123 | uint32_t index = addOperand(model); |
| 124 | model->operands[index].numberOfConsumers = 1; |
| 125 | model->operands[index].lifetime = lifetime; |
| 126 | return index; |
| 127 | } |
| 128 | |
| 129 | ///////////////////////// VALIDATE MODEL OPERAND TYPE ///////////////////////// |
| 130 | |
| 131 | static const int32_t invalidOperandTypes[] = { |
Lev Proleev | 5d7c995 | 2018-10-01 11:18:31 +0100 | [diff] [blame^] | 132 | static_cast<int32_t>(OperandType::FLOAT32) - 1, // lower bound fundamental |
| 133 | static_cast<int32_t>(OperandType::TENSOR_QUANT16_ASYMM) + 1, // upper bound fundamental |
| 134 | static_cast<int32_t>(OperandType::OEM) - 1, // lower bound OEM |
| 135 | static_cast<int32_t>(OperandType::TENSOR_OEM_BYTE) + 1, // upper bound OEM |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) { |
| 139 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 140 | for (int32_t invalidOperandType : invalidOperandTypes) { |
| 141 | const std::string message = "mutateOperandTypeTest: operand " + |
| 142 | std::to_string(operand) + " set to value " + |
| 143 | std::to_string(invalidOperandType); |
| 144 | validate(device, message, model, [operand, invalidOperandType](Model* model) { |
| 145 | model->operands[operand].type = static_cast<OperandType>(invalidOperandType); |
| 146 | }); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | ///////////////////////// VALIDATE OPERAND RANK ///////////////////////// |
| 152 | |
| 153 | static uint32_t getInvalidRank(OperandType type) { |
| 154 | switch (type) { |
| 155 | case OperandType::FLOAT32: |
| 156 | case OperandType::INT32: |
| 157 | case OperandType::UINT32: |
Lev Proleev | abad9ea | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 158 | case OperandType::BOOL: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 159 | return 1; |
| 160 | case OperandType::TENSOR_FLOAT32: |
| 161 | case OperandType::TENSOR_INT32: |
| 162 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 5d7c995 | 2018-10-01 11:18:31 +0100 | [diff] [blame^] | 163 | case OperandType::TENSOR_QUANT16_ASYMM: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 164 | return 0; |
| 165 | default: |
| 166 | return 0; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static void mutateOperandRankTest(const sp<IDevice>& device, const Model& model) { |
| 171 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 172 | const uint32_t invalidRank = getInvalidRank(model.operands[operand].type); |
| 173 | const std::string message = "mutateOperandRankTest: operand " + std::to_string(operand) + |
| 174 | " has rank of " + std::to_string(invalidRank); |
| 175 | validate(device, message, model, [operand, invalidRank](Model* model) { |
| 176 | model->operands[operand].dimensions = std::vector<uint32_t>(invalidRank, 0); |
| 177 | }); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ///////////////////////// VALIDATE OPERAND SCALE ///////////////////////// |
| 182 | |
| 183 | static float getInvalidScale(OperandType type) { |
| 184 | switch (type) { |
| 185 | case OperandType::FLOAT32: |
| 186 | case OperandType::INT32: |
| 187 | case OperandType::UINT32: |
Lev Proleev | abad9ea | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 188 | case OperandType::BOOL: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 189 | case OperandType::TENSOR_FLOAT32: |
| 190 | return 1.0f; |
| 191 | case OperandType::TENSOR_INT32: |
| 192 | return -1.0f; |
| 193 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 5d7c995 | 2018-10-01 11:18:31 +0100 | [diff] [blame^] | 194 | case OperandType::TENSOR_QUANT16_ASYMM: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 195 | return 0.0f; |
| 196 | default: |
| 197 | return 0.0f; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) { |
| 202 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 203 | const float invalidScale = getInvalidScale(model.operands[operand].type); |
| 204 | const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) + |
| 205 | " has scale of " + std::to_string(invalidScale); |
| 206 | validate(device, message, model, [operand, invalidScale](Model* model) { |
| 207 | model->operands[operand].scale = invalidScale; |
| 208 | }); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | ///////////////////////// VALIDATE OPERAND ZERO POINT ///////////////////////// |
| 213 | |
| 214 | static std::vector<int32_t> getInvalidZeroPoints(OperandType type) { |
| 215 | switch (type) { |
| 216 | case OperandType::FLOAT32: |
| 217 | case OperandType::INT32: |
| 218 | case OperandType::UINT32: |
Lev Proleev | abad9ea | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 219 | case OperandType::BOOL: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 220 | case OperandType::TENSOR_FLOAT32: |
| 221 | case OperandType::TENSOR_INT32: |
| 222 | return {1}; |
| 223 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 5d7c995 | 2018-10-01 11:18:31 +0100 | [diff] [blame^] | 224 | case OperandType::TENSOR_QUANT16_ASYMM: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 225 | return {-1, 256}; |
| 226 | default: |
| 227 | return {}; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) { |
| 232 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 233 | const std::vector<int32_t> invalidZeroPoints = |
| 234 | getInvalidZeroPoints(model.operands[operand].type); |
| 235 | for (int32_t invalidZeroPoint : invalidZeroPoints) { |
| 236 | const std::string message = "mutateOperandZeroPointTest: operand " + |
| 237 | std::to_string(operand) + " has zero point of " + |
| 238 | std::to_string(invalidZeroPoint); |
| 239 | validate(device, message, model, [operand, invalidZeroPoint](Model* model) { |
| 240 | model->operands[operand].zeroPoint = invalidZeroPoint; |
| 241 | }); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | ///////////////////////// VALIDATE EXTRA ??? ///////////////////////// |
| 247 | |
| 248 | // TODO: Operand::lifetime |
| 249 | // TODO: Operand::location |
| 250 | |
| 251 | ///////////////////////// VALIDATE OPERATION OPERAND TYPE ///////////////////////// |
| 252 | |
| 253 | static void mutateOperand(Operand* operand, OperandType type) { |
| 254 | Operand newOperand = *operand; |
| 255 | newOperand.type = type; |
| 256 | switch (type) { |
| 257 | case OperandType::FLOAT32: |
| 258 | case OperandType::INT32: |
| 259 | case OperandType::UINT32: |
Lev Proleev | abad9ea | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 260 | case OperandType::BOOL: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 261 | newOperand.dimensions = hidl_vec<uint32_t>(); |
| 262 | newOperand.scale = 0.0f; |
| 263 | newOperand.zeroPoint = 0; |
| 264 | break; |
| 265 | case OperandType::TENSOR_FLOAT32: |
| 266 | newOperand.dimensions = |
| 267 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 268 | newOperand.scale = 0.0f; |
| 269 | newOperand.zeroPoint = 0; |
| 270 | break; |
| 271 | case OperandType::TENSOR_INT32: |
| 272 | newOperand.dimensions = |
| 273 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 274 | newOperand.zeroPoint = 0; |
| 275 | break; |
| 276 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 5d7c995 | 2018-10-01 11:18:31 +0100 | [diff] [blame^] | 277 | case OperandType::TENSOR_QUANT16_ASYMM: |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 278 | newOperand.dimensions = |
| 279 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 280 | newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f; |
| 281 | break; |
| 282 | case OperandType::OEM: |
| 283 | case OperandType::TENSOR_OEM_BYTE: |
| 284 | default: |
| 285 | break; |
| 286 | } |
| 287 | *operand = newOperand; |
| 288 | } |
| 289 | |
| 290 | static bool mutateOperationOperandTypeSkip(size_t operand, const Model& model) { |
| 291 | // LSH_PROJECTION's second argument is allowed to have any type. This is the |
| 292 | // only operation that currently has a type that can be anything independent |
| 293 | // from any other type. Changing the operand type to any other type will |
| 294 | // result in a valid model for LSH_PROJECTION. If this is the case, skip the |
| 295 | // test. |
| 296 | for (const Operation& operation : model.operations) { |
| 297 | if (operation.type == OperationType::LSH_PROJECTION && operand == operation.inputs[1]) { |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) { |
| 305 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 306 | if (mutateOperationOperandTypeSkip(operand, model)) { |
| 307 | continue; |
| 308 | } |
| 309 | for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) { |
| 310 | // Do not test OEM types |
| 311 | if (invalidOperandType == model.operands[operand].type || |
| 312 | invalidOperandType == OperandType::OEM || |
| 313 | invalidOperandType == OperandType::TENSOR_OEM_BYTE) { |
| 314 | continue; |
| 315 | } |
| 316 | const std::string message = "mutateOperationOperandTypeTest: operand " + |
| 317 | std::to_string(operand) + " set to type " + |
| 318 | toString(invalidOperandType); |
| 319 | validate(device, message, model, [operand, invalidOperandType](Model* model) { |
| 320 | mutateOperand(&model->operands[operand], invalidOperandType); |
| 321 | }); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | ///////////////////////// VALIDATE MODEL OPERATION TYPE ///////////////////////// |
| 327 | |
| 328 | static const int32_t invalidOperationTypes[] = { |
| 329 | static_cast<int32_t>(OperationType::ADD) - 1, // lower bound fundamental |
| 330 | static_cast<int32_t>(OperationType::TRANSPOSE) + 1, // upper bound fundamental |
| 331 | static_cast<int32_t>(OperationType::OEM_OPERATION) - 1, // lower bound OEM |
| 332 | static_cast<int32_t>(OperationType::OEM_OPERATION) + 1, // upper bound OEM |
| 333 | }; |
| 334 | |
| 335 | static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) { |
| 336 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 337 | for (int32_t invalidOperationType : invalidOperationTypes) { |
| 338 | const std::string message = "mutateOperationTypeTest: operation " + |
| 339 | std::to_string(operation) + " set to value " + |
| 340 | std::to_string(invalidOperationType); |
| 341 | validate(device, message, model, [operation, invalidOperationType](Model* model) { |
| 342 | model->operations[operation].type = |
| 343 | static_cast<OperationType>(invalidOperationType); |
| 344 | }); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | ///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX ///////////////////////// |
| 350 | |
| 351 | static void mutateOperationInputOperandIndexTest(const sp<IDevice>& device, const Model& model) { |
| 352 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 353 | const uint32_t invalidOperand = model.operands.size(); |
| 354 | for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) { |
| 355 | const std::string message = "mutateOperationInputOperandIndexTest: operation " + |
| 356 | std::to_string(operation) + " input " + |
| 357 | std::to_string(input); |
| 358 | validate(device, message, model, [operation, input, invalidOperand](Model* model) { |
| 359 | model->operations[operation].inputs[input] = invalidOperand; |
| 360 | }); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | ///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX ///////////////////////// |
| 366 | |
| 367 | static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) { |
| 368 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 369 | const uint32_t invalidOperand = model.operands.size(); |
| 370 | for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) { |
| 371 | const std::string message = "mutateOperationOutputOperandIndexTest: operation " + |
| 372 | std::to_string(operation) + " output " + |
| 373 | std::to_string(output); |
| 374 | validate(device, message, model, [operation, output, invalidOperand](Model* model) { |
| 375 | model->operations[operation].outputs[output] = invalidOperand; |
| 376 | }); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | ///////////////////////// REMOVE OPERAND FROM EVERYTHING ///////////////////////// |
| 382 | |
| 383 | static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) { |
| 384 | if (vec) { |
| 385 | // remove elements matching "value" |
| 386 | auto last = std::remove(vec->begin(), vec->end(), value); |
| 387 | vec->resize(std::distance(vec->begin(), last)); |
| 388 | |
| 389 | // decrement elements exceeding "value" |
| 390 | std::transform(vec->begin(), vec->end(), vec->begin(), |
| 391 | [value](uint32_t v) { return v > value ? v-- : v; }); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static void removeOperand(Model* model, uint32_t index) { |
| 396 | hidl_vec_removeAt(&model->operands, index); |
| 397 | for (Operation& operation : model->operations) { |
| 398 | removeValueAndDecrementGreaterValues(&operation.inputs, index); |
| 399 | removeValueAndDecrementGreaterValues(&operation.outputs, index); |
| 400 | } |
| 401 | removeValueAndDecrementGreaterValues(&model->inputIndexes, index); |
| 402 | removeValueAndDecrementGreaterValues(&model->outputIndexes, index); |
| 403 | } |
| 404 | |
| 405 | static void removeOperandTest(const sp<IDevice>& device, const Model& model) { |
| 406 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 407 | const std::string message = "removeOperandTest: operand " + std::to_string(operand); |
| 408 | validate(device, message, model, |
| 409 | [operand](Model* model) { removeOperand(model, operand); }); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | ///////////////////////// REMOVE OPERATION ///////////////////////// |
| 414 | |
| 415 | static void removeOperation(Model* model, uint32_t index) { |
| 416 | for (uint32_t operand : model->operations[index].inputs) { |
| 417 | model->operands[operand].numberOfConsumers--; |
| 418 | } |
| 419 | hidl_vec_removeAt(&model->operations, index); |
| 420 | } |
| 421 | |
| 422 | static void removeOperationTest(const sp<IDevice>& device, const Model& model) { |
| 423 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 424 | const std::string message = "removeOperationTest: operation " + std::to_string(operation); |
| 425 | validate(device, message, model, |
| 426 | [operation](Model* model) { removeOperation(model, operation); }); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | ///////////////////////// REMOVE OPERATION INPUT ///////////////////////// |
| 431 | |
| 432 | static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) { |
| 433 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 434 | for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) { |
| 435 | const Operation& op = model.operations[operation]; |
| 436 | // CONCATENATION has at least 2 inputs, with the last element being |
| 437 | // INT32. Skip this test if removing one of CONCATENATION's |
| 438 | // inputs still produces a valid model. |
| 439 | if (op.type == OperationType::CONCATENATION && op.inputs.size() > 2 && |
| 440 | input != op.inputs.size() - 1) { |
| 441 | continue; |
| 442 | } |
| 443 | const std::string message = "removeOperationInputTest: operation " + |
| 444 | std::to_string(operation) + ", input " + |
| 445 | std::to_string(input); |
| 446 | validate(device, message, model, [operation, input](Model* model) { |
| 447 | uint32_t operand = model->operations[operation].inputs[input]; |
| 448 | model->operands[operand].numberOfConsumers--; |
| 449 | hidl_vec_removeAt(&model->operations[operation].inputs, input); |
| 450 | }); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | ///////////////////////// REMOVE OPERATION OUTPUT ///////////////////////// |
| 456 | |
| 457 | static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) { |
| 458 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 459 | for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) { |
| 460 | const std::string message = "removeOperationOutputTest: operation " + |
| 461 | std::to_string(operation) + ", output " + |
| 462 | std::to_string(output); |
| 463 | validate(device, message, model, [operation, output](Model* model) { |
| 464 | hidl_vec_removeAt(&model->operations[operation].outputs, output); |
| 465 | }); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | ///////////////////////// MODEL VALIDATION ///////////////////////// |
| 471 | |
| 472 | // TODO: remove model input |
| 473 | // TODO: remove model output |
| 474 | // TODO: add unused operation |
| 475 | |
| 476 | ///////////////////////// ADD OPERATION INPUT ///////////////////////// |
| 477 | |
| 478 | static void addOperationInputTest(const sp<IDevice>& device, const Model& model) { |
| 479 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 480 | const std::string message = "addOperationInputTest: operation " + std::to_string(operation); |
| 481 | validate(device, message, model, [operation](Model* model) { |
| 482 | uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT); |
| 483 | hidl_vec_push_back(&model->operations[operation].inputs, index); |
| 484 | hidl_vec_push_back(&model->inputIndexes, index); |
| 485 | }); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | ///////////////////////// ADD OPERATION OUTPUT ///////////////////////// |
| 490 | |
| 491 | static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) { |
| 492 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 493 | const std::string message = |
| 494 | "addOperationOutputTest: operation " + std::to_string(operation); |
| 495 | validate(device, message, model, [operation](Model* model) { |
| 496 | uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT); |
| 497 | hidl_vec_push_back(&model->operations[operation].outputs, index); |
| 498 | hidl_vec_push_back(&model->outputIndexes, index); |
| 499 | }); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | ///////////////////////// VALIDATE EXECUTION PREFERENCE ///////////////////////// |
| 504 | |
| 505 | static const int32_t invalidExecutionPreferences[] = { |
| 506 | static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound |
| 507 | static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound |
| 508 | }; |
| 509 | |
| 510 | static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) { |
| 511 | for (int32_t preference : invalidExecutionPreferences) { |
| 512 | const std::string message = |
| 513 | "mutateExecutionPreferenceTest: preference " + std::to_string(preference); |
| 514 | validate(device, message, model, [](Model*) {}, |
| 515 | static_cast<ExecutionPreference>(preference)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | ////////////////////////// ENTRY POINT ////////////////////////////// |
| 520 | |
| 521 | void ValidationTest::validateModel(const Model& model) { |
| 522 | mutateOperandTypeTest(device, model); |
| 523 | mutateOperandRankTest(device, model); |
| 524 | mutateOperandScaleTest(device, model); |
| 525 | mutateOperandZeroPointTest(device, model); |
| 526 | mutateOperationOperandTypeTest(device, model); |
| 527 | mutateOperationTypeTest(device, model); |
| 528 | mutateOperationInputOperandIndexTest(device, model); |
| 529 | mutateOperationOutputOperandIndexTest(device, model); |
| 530 | removeOperandTest(device, model); |
| 531 | removeOperationTest(device, model); |
| 532 | removeOperationInputTest(device, model); |
| 533 | removeOperationOutputTest(device, model); |
| 534 | addOperationInputTest(device, model); |
| 535 | addOperationOutputTest(device, model); |
| 536 | mutateExecutionPreferenceTest(device, model); |
| 537 | } |
| 538 | |
| 539 | } // namespace functional |
| 540 | } // namespace vts |
| 541 | } // namespace V1_2 |
| 542 | } // namespace neuralnetworks |
| 543 | } // namespace hardware |
| 544 | } // namespace android |