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