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