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