Slava Shklyaev | feb87a9 | 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 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 28 | using V1_0::OperandLifeTime; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 29 | using V1_1::ExecutionPreference; |
| 30 | |
| 31 | namespace vts { |
| 32 | namespace functional { |
| 33 | |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame^] | 34 | using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback; |
| 35 | using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 36 | |
| 37 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 38 | |
| 39 | static void validateGetSupportedOperations(const sp<IDevice>& device, const std::string& message, |
| 40 | const Model& model) { |
| 41 | SCOPED_TRACE(message + " [getSupportedOperations_1_2]"); |
| 42 | |
| 43 | Return<void> ret = |
| 44 | device->getSupportedOperations_1_2(model, [&](ErrorStatus status, const hidl_vec<bool>&) { |
| 45 | EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status); |
| 46 | }); |
| 47 | EXPECT_TRUE(ret.isOk()); |
| 48 | } |
| 49 | |
| 50 | static void validatePrepareModel(const sp<IDevice>& device, const std::string& message, |
| 51 | const Model& model, ExecutionPreference preference) { |
| 52 | SCOPED_TRACE(message + " [prepareModel_1_2]"); |
| 53 | |
| 54 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 55 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
| 56 | Return<ErrorStatus> prepareLaunchStatus = |
| 57 | device->prepareModel_1_2(model, preference, preparedModelCallback); |
| 58 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 59 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(prepareLaunchStatus)); |
| 60 | |
| 61 | preparedModelCallback->wait(); |
| 62 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
| 63 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, prepareReturnStatus); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame^] | 64 | sp<IPreparedModel> preparedModel = getPreparedModel_1_2(preparedModelCallback); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 65 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 66 | } |
| 67 | |
| 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 | |
| 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, Model model, |
| 79 | const std::function<void(Model*)>& mutation, |
| 80 | ExecutionPreference preference = ExecutionPreference::FAST_SINGLE_ANSWER) { |
| 81 | mutation(&model); |
| 82 | if (validExecutionPreference(preference)) { |
| 83 | validateGetSupportedOperations(device, message, model); |
| 84 | } |
| 85 | validatePrepareModel(device, message, model, preference); |
| 86 | } |
| 87 | |
| 88 | // Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation, |
| 89 | // so this is efficiently accomplished by moving the element to the end and |
| 90 | // resizing the hidl_vec to one less. |
| 91 | template <typename Type> |
| 92 | static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) { |
| 93 | if (vec) { |
| 94 | std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end()); |
| 95 | vec->resize(vec->size() - 1); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | template <typename Type> |
| 100 | static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) { |
| 101 | // assume vec is valid |
| 102 | const uint32_t index = vec->size(); |
| 103 | vec->resize(index + 1); |
| 104 | (*vec)[index] = value; |
| 105 | return index; |
| 106 | } |
| 107 | |
| 108 | static uint32_t addOperand(Model* model) { |
| 109 | return hidl_vec_push_back(&model->operands, |
| 110 | { |
| 111 | .type = OperandType::INT32, |
| 112 | .dimensions = {}, |
| 113 | .numberOfConsumers = 0, |
| 114 | .scale = 0.0f, |
| 115 | .zeroPoint = 0, |
| 116 | .lifetime = OperandLifeTime::MODEL_INPUT, |
| 117 | .location = {.poolIndex = 0, .offset = 0, .length = 0}, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | static uint32_t addOperand(Model* model, OperandLifeTime lifetime) { |
| 122 | uint32_t index = addOperand(model); |
| 123 | model->operands[index].numberOfConsumers = 1; |
| 124 | model->operands[index].lifetime = lifetime; |
| 125 | return index; |
| 126 | } |
| 127 | |
| 128 | ///////////////////////// VALIDATE MODEL OPERAND TYPE ///////////////////////// |
| 129 | |
Michael K. Sanders | 9233dbe | 2018-10-30 15:16:54 +0000 | [diff] [blame] | 130 | static const uint32_t invalidOperandTypes[] = { |
| 131 | static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MIN) - 1, |
| 132 | static_cast<uint32_t>(OperandTypeRange::OPERAND_FUNDAMENTAL_MAX) + 1, |
| 133 | static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MIN) - 1, |
| 134 | static_cast<uint32_t>(OperandTypeRange::OPERAND_OEM_MAX) + 1, |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | static void mutateOperandTypeTest(const sp<IDevice>& device, const Model& model) { |
| 138 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
Michael K. Sanders | 9233dbe | 2018-10-30 15:16:54 +0000 | [diff] [blame] | 139 | for (uint32_t invalidOperandType : invalidOperandTypes) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 140 | const std::string message = "mutateOperandTypeTest: operand " + |
| 141 | std::to_string(operand) + " set to value " + |
| 142 | std::to_string(invalidOperandType); |
| 143 | validate(device, message, model, [operand, invalidOperandType](Model* model) { |
| 144 | model->operands[operand].type = static_cast<OperandType>(invalidOperandType); |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ///////////////////////// VALIDATE OPERAND RANK ///////////////////////// |
| 151 | |
| 152 | static uint32_t getInvalidRank(OperandType type) { |
| 153 | switch (type) { |
| 154 | case OperandType::FLOAT32: |
| 155 | case OperandType::INT32: |
| 156 | case OperandType::UINT32: |
Lev Proleev | 08662c6 | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 157 | case OperandType::BOOL: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 158 | return 1; |
Michael K. Sanders | 5dd8412 | 2018-10-12 09:10:15 +0100 | [diff] [blame] | 159 | case OperandType::TENSOR_FLOAT16: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 160 | case OperandType::TENSOR_FLOAT32: |
| 161 | case OperandType::TENSOR_INT32: |
| 162 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 217c407 | 2018-11-13 15:42:36 +0000 | [diff] [blame] | 163 | case OperandType::TENSOR_QUANT16_SYMM: |
Slava Shklyaev | feb87a9 | 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 | 08662c6 | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 188 | case OperandType::BOOL: |
Michael K. Sanders | 5dd8412 | 2018-10-12 09:10:15 +0100 | [diff] [blame] | 189 | case OperandType::TENSOR_FLOAT16: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 190 | case OperandType::TENSOR_FLOAT32: |
| 191 | return 1.0f; |
| 192 | case OperandType::TENSOR_INT32: |
| 193 | return -1.0f; |
| 194 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 217c407 | 2018-11-13 15:42:36 +0000 | [diff] [blame] | 195 | case OperandType::TENSOR_QUANT16_SYMM: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 196 | return 0.0f; |
| 197 | default: |
| 198 | return 0.0f; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static void mutateOperandScaleTest(const sp<IDevice>& device, const Model& model) { |
| 203 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 204 | const float invalidScale = getInvalidScale(model.operands[operand].type); |
| 205 | const std::string message = "mutateOperandScaleTest: operand " + std::to_string(operand) + |
| 206 | " has scale of " + std::to_string(invalidScale); |
| 207 | validate(device, message, model, [operand, invalidScale](Model* model) { |
| 208 | model->operands[operand].scale = invalidScale; |
| 209 | }); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ///////////////////////// VALIDATE OPERAND ZERO POINT ///////////////////////// |
| 214 | |
| 215 | static std::vector<int32_t> getInvalidZeroPoints(OperandType type) { |
| 216 | switch (type) { |
| 217 | case OperandType::FLOAT32: |
| 218 | case OperandType::INT32: |
| 219 | case OperandType::UINT32: |
Lev Proleev | 08662c6 | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 220 | case OperandType::BOOL: |
Michael K. Sanders | 5dd8412 | 2018-10-12 09:10:15 +0100 | [diff] [blame] | 221 | case OperandType::TENSOR_FLOAT16: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 222 | case OperandType::TENSOR_FLOAT32: |
| 223 | case OperandType::TENSOR_INT32: |
| 224 | return {1}; |
| 225 | case OperandType::TENSOR_QUANT8_ASYMM: |
| 226 | return {-1, 256}; |
Lev Proleev | 217c407 | 2018-11-13 15:42:36 +0000 | [diff] [blame] | 227 | case OperandType::TENSOR_QUANT16_SYMM: |
| 228 | return {-32769, -1, 1, 32768}; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 229 | default: |
| 230 | return {}; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void mutateOperandZeroPointTest(const sp<IDevice>& device, const Model& model) { |
| 235 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
| 236 | const std::vector<int32_t> invalidZeroPoints = |
| 237 | getInvalidZeroPoints(model.operands[operand].type); |
| 238 | for (int32_t invalidZeroPoint : invalidZeroPoints) { |
| 239 | const std::string message = "mutateOperandZeroPointTest: operand " + |
| 240 | std::to_string(operand) + " has zero point of " + |
| 241 | std::to_string(invalidZeroPoint); |
| 242 | validate(device, message, model, [operand, invalidZeroPoint](Model* model) { |
| 243 | model->operands[operand].zeroPoint = invalidZeroPoint; |
| 244 | }); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | ///////////////////////// VALIDATE EXTRA ??? ///////////////////////// |
| 250 | |
| 251 | // TODO: Operand::lifetime |
| 252 | // TODO: Operand::location |
| 253 | |
| 254 | ///////////////////////// VALIDATE OPERATION OPERAND TYPE ///////////////////////// |
| 255 | |
| 256 | static void mutateOperand(Operand* operand, OperandType type) { |
| 257 | Operand newOperand = *operand; |
| 258 | newOperand.type = type; |
| 259 | switch (type) { |
| 260 | case OperandType::FLOAT32: |
| 261 | case OperandType::INT32: |
| 262 | case OperandType::UINT32: |
Lev Proleev | 08662c6 | 2018-10-01 11:18:31 +0100 | [diff] [blame] | 263 | case OperandType::BOOL: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 264 | newOperand.dimensions = hidl_vec<uint32_t>(); |
| 265 | newOperand.scale = 0.0f; |
| 266 | newOperand.zeroPoint = 0; |
| 267 | break; |
Michael K. Sanders | 5dd8412 | 2018-10-12 09:10:15 +0100 | [diff] [blame] | 268 | case OperandType::TENSOR_FLOAT16: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 269 | case OperandType::TENSOR_FLOAT32: |
| 270 | newOperand.dimensions = |
| 271 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 272 | newOperand.scale = 0.0f; |
| 273 | newOperand.zeroPoint = 0; |
| 274 | break; |
| 275 | case OperandType::TENSOR_INT32: |
| 276 | newOperand.dimensions = |
| 277 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 278 | newOperand.zeroPoint = 0; |
| 279 | break; |
| 280 | case OperandType::TENSOR_QUANT8_ASYMM: |
Lev Proleev | 217c407 | 2018-11-13 15:42:36 +0000 | [diff] [blame] | 281 | case OperandType::TENSOR_QUANT16_SYMM: |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 282 | newOperand.dimensions = |
| 283 | operand->dimensions.size() > 0 ? operand->dimensions : hidl_vec<uint32_t>({1}); |
| 284 | newOperand.scale = operand->scale != 0.0f ? operand->scale : 1.0f; |
| 285 | break; |
| 286 | case OperandType::OEM: |
| 287 | case OperandType::TENSOR_OEM_BYTE: |
| 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | *operand = newOperand; |
| 292 | } |
| 293 | |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 294 | static bool mutateOperationOperandTypeSkip(size_t operand, OperandType type, const Model& model) { |
| 295 | // Do not test OEM types |
| 296 | if (type == model.operands[operand].type || type == OperandType::OEM || |
| 297 | type == OperandType::TENSOR_OEM_BYTE) { |
| 298 | return true; |
| 299 | } |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 300 | for (const Operation& operation : model.operations) { |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 301 | // Skip mutateOperationOperandTypeTest for the following operations. |
| 302 | // - LSH_PROJECTION's second argument is allowed to have any type. |
| 303 | // - ARGMIN and ARGMAX's first argument can be any of TENSOR_(FLOAT32|INT32|QUANT8_ASYMM). |
| 304 | // - CAST's argument can be any of TENSOR_(FLOAT32|INT32|QUANT8_ASYMM). |
| 305 | switch (operation.type) { |
| 306 | case OperationType::LSH_PROJECTION: { |
| 307 | if (operand == operation.inputs[1]) { |
| 308 | return true; |
| 309 | } |
| 310 | } break; |
| 311 | case OperationType::CAST: |
| 312 | case OperationType::ARGMAX: |
| 313 | case OperationType::ARGMIN: { |
| 314 | if (type == OperandType::TENSOR_FLOAT32 || type == OperandType::TENSOR_INT32 || |
| 315 | type == OperandType::TENSOR_QUANT8_ASYMM) { |
| 316 | return true; |
| 317 | } |
| 318 | } break; |
| 319 | default: |
| 320 | break; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | static void mutateOperationOperandTypeTest(const sp<IDevice>& device, const Model& model) { |
| 327 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 328 | for (OperandType invalidOperandType : hidl_enum_range<OperandType>{}) { |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 329 | if (mutateOperationOperandTypeSkip(operand, invalidOperandType, model)) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 330 | continue; |
| 331 | } |
| 332 | const std::string message = "mutateOperationOperandTypeTest: operand " + |
| 333 | std::to_string(operand) + " set to type " + |
| 334 | toString(invalidOperandType); |
| 335 | validate(device, message, model, [operand, invalidOperandType](Model* model) { |
| 336 | mutateOperand(&model->operands[operand], invalidOperandType); |
| 337 | }); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | ///////////////////////// VALIDATE MODEL OPERATION TYPE ///////////////////////// |
| 343 | |
Michael K. Sanders | 9233dbe | 2018-10-30 15:16:54 +0000 | [diff] [blame] | 344 | static const uint32_t invalidOperationTypes[] = { |
| 345 | static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MIN) - 1, |
| 346 | static_cast<uint32_t>(OperationTypeRange::OPERATION_FUNDAMENTAL_MAX) + 1, |
| 347 | static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MIN) - 1, |
| 348 | static_cast<uint32_t>(OperationTypeRange::OPERATION_OEM_MAX) + 1, |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | static void mutateOperationTypeTest(const sp<IDevice>& device, const Model& model) { |
| 352 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
Michael K. Sanders | 9233dbe | 2018-10-30 15:16:54 +0000 | [diff] [blame] | 353 | for (uint32_t invalidOperationType : invalidOperationTypes) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 354 | const std::string message = "mutateOperationTypeTest: operation " + |
| 355 | std::to_string(operation) + " set to value " + |
| 356 | std::to_string(invalidOperationType); |
| 357 | validate(device, message, model, [operation, invalidOperationType](Model* model) { |
| 358 | model->operations[operation].type = |
| 359 | static_cast<OperationType>(invalidOperationType); |
| 360 | }); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | ///////////////////////// VALIDATE MODEL OPERATION INPUT OPERAND INDEX ///////////////////////// |
| 366 | |
| 367 | static void mutateOperationInputOperandIndexTest(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 input = 0; input < model.operations[operation].inputs.size(); ++input) { |
| 371 | const std::string message = "mutateOperationInputOperandIndexTest: operation " + |
| 372 | std::to_string(operation) + " input " + |
| 373 | std::to_string(input); |
| 374 | validate(device, message, model, [operation, input, invalidOperand](Model* model) { |
| 375 | model->operations[operation].inputs[input] = invalidOperand; |
| 376 | }); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | ///////////////////////// VALIDATE MODEL OPERATION OUTPUT OPERAND INDEX ///////////////////////// |
| 382 | |
| 383 | static void mutateOperationOutputOperandIndexTest(const sp<IDevice>& device, const Model& model) { |
| 384 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 385 | const uint32_t invalidOperand = model.operands.size(); |
| 386 | for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) { |
| 387 | const std::string message = "mutateOperationOutputOperandIndexTest: operation " + |
| 388 | std::to_string(operation) + " output " + |
| 389 | std::to_string(output); |
| 390 | validate(device, message, model, [operation, output, invalidOperand](Model* model) { |
| 391 | model->operations[operation].outputs[output] = invalidOperand; |
| 392 | }); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | ///////////////////////// REMOVE OPERAND FROM EVERYTHING ///////////////////////// |
| 398 | |
| 399 | static void removeValueAndDecrementGreaterValues(hidl_vec<uint32_t>* vec, uint32_t value) { |
| 400 | if (vec) { |
| 401 | // remove elements matching "value" |
| 402 | auto last = std::remove(vec->begin(), vec->end(), value); |
| 403 | vec->resize(std::distance(vec->begin(), last)); |
| 404 | |
| 405 | // decrement elements exceeding "value" |
| 406 | std::transform(vec->begin(), vec->end(), vec->begin(), |
| 407 | [value](uint32_t v) { return v > value ? v-- : v; }); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static void removeOperand(Model* model, uint32_t index) { |
| 412 | hidl_vec_removeAt(&model->operands, index); |
| 413 | for (Operation& operation : model->operations) { |
| 414 | removeValueAndDecrementGreaterValues(&operation.inputs, index); |
| 415 | removeValueAndDecrementGreaterValues(&operation.outputs, index); |
| 416 | } |
| 417 | removeValueAndDecrementGreaterValues(&model->inputIndexes, index); |
| 418 | removeValueAndDecrementGreaterValues(&model->outputIndexes, index); |
| 419 | } |
| 420 | |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 421 | static bool removeOperandSkip(size_t operand, const Model& model) { |
| 422 | for (const Operation& operation : model.operations) { |
| 423 | // Skip removeOperandTest for the following operations. |
| 424 | // - SPLIT's outputs are not checked during prepareModel. |
| 425 | if (operation.type == OperationType::SPLIT) { |
| 426 | for (const size_t outOprand : operation.outputs) { |
| 427 | if (operand == outOprand) { |
| 428 | return true; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 436 | static void removeOperandTest(const sp<IDevice>& device, const Model& model) { |
| 437 | for (size_t operand = 0; operand < model.operands.size(); ++operand) { |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 438 | if (removeOperandSkip(operand, model)) { |
| 439 | continue; |
| 440 | } |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 441 | const std::string message = "removeOperandTest: operand " + std::to_string(operand); |
| 442 | validate(device, message, model, |
| 443 | [operand](Model* model) { removeOperand(model, operand); }); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | ///////////////////////// REMOVE OPERATION ///////////////////////// |
| 448 | |
| 449 | static void removeOperation(Model* model, uint32_t index) { |
| 450 | for (uint32_t operand : model->operations[index].inputs) { |
| 451 | model->operands[operand].numberOfConsumers--; |
| 452 | } |
| 453 | hidl_vec_removeAt(&model->operations, index); |
| 454 | } |
| 455 | |
| 456 | static void removeOperationTest(const sp<IDevice>& device, const Model& model) { |
| 457 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 458 | const std::string message = "removeOperationTest: operation " + std::to_string(operation); |
| 459 | validate(device, message, model, |
| 460 | [operation](Model* model) { removeOperation(model, operation); }); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | ///////////////////////// REMOVE OPERATION INPUT ///////////////////////// |
| 465 | |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 466 | static bool removeOperationInputSkip(const Operation& op, size_t input) { |
| 467 | // Skip removeOperationInputTest for the following operations. |
| 468 | // - CONCATENATION has at least 2 inputs, with the last element being INT32. |
| 469 | // - CONV_2D, DEPTHWISE_CONV_2D, MAX_POOL_2D, AVERAGE_POOL_2D, L2_POOL_2D, RESIZE_BILINEAR, |
| 470 | // SPACE_TO_DEPTH, SPACE_TO_DEPTH, SPACE_TO_BATCH_ND, BATCH_TO_SPACE_ND can have an optional |
| 471 | // layout parameter. |
| 472 | // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional axis |
| 473 | // parameter. |
| 474 | switch (op.type) { |
| 475 | case OperationType::CONCATENATION: { |
| 476 | if (op.inputs.size() > 2 && input != op.inputs.size() - 1) { |
| 477 | return true; |
| 478 | } |
| 479 | } break; |
| 480 | case OperationType::DEPTHWISE_CONV_2D: { |
| 481 | if ((op.inputs.size() == 12 && input == 11) || (op.inputs.size() == 9 && input == 8)) { |
| 482 | return true; |
| 483 | } |
| 484 | } break; |
| 485 | case OperationType::CONV_2D: |
| 486 | case OperationType::AVERAGE_POOL_2D: |
| 487 | case OperationType::MAX_POOL_2D: |
| 488 | case OperationType::L2_POOL_2D: { |
| 489 | if ((op.inputs.size() == 11 && input == 10) || (op.inputs.size() == 8 && input == 7)) { |
| 490 | return true; |
| 491 | } |
| 492 | } break; |
| 493 | case OperationType::RESIZE_BILINEAR: { |
| 494 | if (op.inputs.size() == 4 && input == 3) { |
| 495 | return true; |
| 496 | } |
| 497 | } break; |
| 498 | case OperationType::SPACE_TO_DEPTH: |
| 499 | case OperationType::DEPTH_TO_SPACE: |
| 500 | case OperationType::BATCH_TO_SPACE_ND: { |
| 501 | if (op.inputs.size() == 3 && input == 2) { |
| 502 | return true; |
| 503 | } |
| 504 | } break; |
| 505 | case OperationType::SPACE_TO_BATCH_ND: { |
| 506 | if (op.inputs.size() == 4 && input == 3) { |
| 507 | return true; |
| 508 | } |
| 509 | } break; |
| 510 | case OperationType::L2_NORMALIZATION: { |
| 511 | if (op.inputs.size() == 2 && input == 1) { |
| 512 | return true; |
| 513 | } |
| 514 | } break; |
| 515 | case OperationType::LOCAL_RESPONSE_NORMALIZATION: { |
| 516 | if (op.inputs.size() == 6 && input == 5) { |
| 517 | return true; |
| 518 | } |
| 519 | } break; |
| 520 | case OperationType::SOFTMAX: { |
| 521 | if (op.inputs.size() == 3 && input == 2) { |
| 522 | return true; |
| 523 | } |
| 524 | } break; |
| 525 | default: |
| 526 | break; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 531 | static void removeOperationInputTest(const sp<IDevice>& device, const Model& model) { |
| 532 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 533 | for (size_t input = 0; input < model.operations[operation].inputs.size(); ++input) { |
| 534 | const Operation& op = model.operations[operation]; |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 535 | if (removeOperationInputSkip(op, input)) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 536 | continue; |
| 537 | } |
| 538 | const std::string message = "removeOperationInputTest: operation " + |
| 539 | std::to_string(operation) + ", input " + |
| 540 | std::to_string(input); |
| 541 | validate(device, message, model, [operation, input](Model* model) { |
| 542 | uint32_t operand = model->operations[operation].inputs[input]; |
| 543 | model->operands[operand].numberOfConsumers--; |
| 544 | hidl_vec_removeAt(&model->operations[operation].inputs, input); |
| 545 | }); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | ///////////////////////// REMOVE OPERATION OUTPUT ///////////////////////// |
| 551 | |
| 552 | static void removeOperationOutputTest(const sp<IDevice>& device, const Model& model) { |
| 553 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 554 | for (size_t output = 0; output < model.operations[operation].outputs.size(); ++output) { |
| 555 | const std::string message = "removeOperationOutputTest: operation " + |
| 556 | std::to_string(operation) + ", output " + |
| 557 | std::to_string(output); |
| 558 | validate(device, message, model, [operation, output](Model* model) { |
| 559 | hidl_vec_removeAt(&model->operations[operation].outputs, output); |
| 560 | }); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | ///////////////////////// MODEL VALIDATION ///////////////////////// |
| 566 | |
| 567 | // TODO: remove model input |
| 568 | // TODO: remove model output |
| 569 | // TODO: add unused operation |
| 570 | |
| 571 | ///////////////////////// ADD OPERATION INPUT ///////////////////////// |
| 572 | |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 573 | static bool addOperationInputSkip(const Operation& op) { |
Xusong Wang | a6b1dc7 | 2018-10-22 13:49:00 -0700 | [diff] [blame] | 574 | // Skip addOperationInputTest for the following operations. |
Xusong Wang | 1a2492f | 2018-10-05 11:49:13 -0700 | [diff] [blame] | 575 | // - L2_NORMALIZATION, LOCAL_RESPONSE_NORMALIZATION, SOFTMAX can have an optional INT32 axis |
| 576 | // parameter. |
| 577 | if ((op.type == OperationType::L2_NORMALIZATION && op.inputs.size() == 1) || |
| 578 | (op.type == OperationType::LOCAL_RESPONSE_NORMALIZATION && op.inputs.size() == 5) || |
| 579 | (op.type == OperationType::SOFTMAX && op.inputs.size() == 2)) { |
Xusong Wang | a6b1dc7 | 2018-10-22 13:49:00 -0700 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 585 | static void addOperationInputTest(const sp<IDevice>& device, const Model& model) { |
| 586 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
Xusong Wang | a6b1dc7 | 2018-10-22 13:49:00 -0700 | [diff] [blame] | 587 | if (addOperationInputSkip(model.operations[operation])) { |
| 588 | continue; |
| 589 | } |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 590 | const std::string message = "addOperationInputTest: operation " + std::to_string(operation); |
| 591 | validate(device, message, model, [operation](Model* model) { |
| 592 | uint32_t index = addOperand(model, OperandLifeTime::MODEL_INPUT); |
| 593 | hidl_vec_push_back(&model->operations[operation].inputs, index); |
| 594 | hidl_vec_push_back(&model->inputIndexes, index); |
| 595 | }); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | ///////////////////////// ADD OPERATION OUTPUT ///////////////////////// |
| 600 | |
| 601 | static void addOperationOutputTest(const sp<IDevice>& device, const Model& model) { |
| 602 | for (size_t operation = 0; operation < model.operations.size(); ++operation) { |
| 603 | const std::string message = |
| 604 | "addOperationOutputTest: operation " + std::to_string(operation); |
| 605 | validate(device, message, model, [operation](Model* model) { |
| 606 | uint32_t index = addOperand(model, OperandLifeTime::MODEL_OUTPUT); |
| 607 | hidl_vec_push_back(&model->operations[operation].outputs, index); |
| 608 | hidl_vec_push_back(&model->outputIndexes, index); |
| 609 | }); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | ///////////////////////// VALIDATE EXECUTION PREFERENCE ///////////////////////// |
| 614 | |
| 615 | static const int32_t invalidExecutionPreferences[] = { |
| 616 | static_cast<int32_t>(ExecutionPreference::LOW_POWER) - 1, // lower bound |
| 617 | static_cast<int32_t>(ExecutionPreference::SUSTAINED_SPEED) + 1, // upper bound |
| 618 | }; |
| 619 | |
| 620 | static void mutateExecutionPreferenceTest(const sp<IDevice>& device, const Model& model) { |
| 621 | for (int32_t preference : invalidExecutionPreferences) { |
| 622 | const std::string message = |
| 623 | "mutateExecutionPreferenceTest: preference " + std::to_string(preference); |
| 624 | validate(device, message, model, [](Model*) {}, |
| 625 | static_cast<ExecutionPreference>(preference)); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | ////////////////////////// ENTRY POINT ////////////////////////////// |
| 630 | |
| 631 | void ValidationTest::validateModel(const Model& model) { |
| 632 | mutateOperandTypeTest(device, model); |
| 633 | mutateOperandRankTest(device, model); |
| 634 | mutateOperandScaleTest(device, model); |
| 635 | mutateOperandZeroPointTest(device, model); |
| 636 | mutateOperationOperandTypeTest(device, model); |
| 637 | mutateOperationTypeTest(device, model); |
| 638 | mutateOperationInputOperandIndexTest(device, model); |
| 639 | mutateOperationOutputOperandIndexTest(device, model); |
| 640 | removeOperandTest(device, model); |
| 641 | removeOperationTest(device, model); |
| 642 | removeOperationInputTest(device, model); |
| 643 | removeOperationOutputTest(device, model); |
| 644 | addOperationInputTest(device, model); |
| 645 | addOperationOutputTest(device, model); |
| 646 | mutateExecutionPreferenceTest(device, model); |
| 647 | } |
| 648 | |
| 649 | } // namespace functional |
| 650 | } // namespace vts |
| 651 | } // namespace V1_2 |
| 652 | } // namespace neuralnetworks |
| 653 | } // namespace hardware |
| 654 | } // namespace android |