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