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