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