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