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