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