arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 8 | #include "Utils.hpp" |
| 9 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 10 | #include <armnn/ArmNN.hpp> |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 11 | #include <armnn/BackendHelper.hpp> |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 0b7a419 | 2020-03-09 18:20:42 +0000 | [diff] [blame] | 13 | #include <armnn/utility/IgnoreUnused.hpp> |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 14 | #include <armnn/utility/NumericCast.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 15 | |
Matteo Martincigh | 00d6ed1 | 2019-11-28 17:13:24 +0000 | [diff] [blame] | 16 | #include <armnnUtils/DataLayoutIndexed.hpp> |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 17 | #include <armnnUtils/Transpose.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 18 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 19 | #include "1.0/FullyConnected.hpp" |
| 20 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 21 | #include <ActivationFunctor.h> |
| 22 | #include <CpuExecutor.h> |
| 23 | #include <OperationsUtils.h> |
| 24 | |
James Ward | 4e22f60 | 2020-10-20 15:50:33 +0100 | [diff] [blame] | 25 | #include <armnnUtils/FloatingPointComparison.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 26 | |
| 27 | #include <log/log.h> |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 28 | #include <vector> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 29 | |
| 30 | namespace armnn_driver |
| 31 | { |
| 32 | |
| 33 | /// |
| 34 | /// Helper classes |
| 35 | /// |
| 36 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 37 | #ifdef ARMNN_ANDROID_R |
| 38 | using OperandType = android::nn::hal::OperandType; |
| 39 | #endif |
| 40 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 41 | #ifdef ARMNN_ANDROID_S |
| 42 | #include <nnapi/Types.h> |
| 43 | #endif |
| 44 | |
| 45 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 46 | struct ConversionData |
| 47 | { |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 48 | ConversionData(const std::vector<armnn::BackendId>& backends) |
| 49 | : m_Backends(backends) |
| 50 | , m_Network(nullptr, nullptr) |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 51 | , m_DynamicInputsEncountered(false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 52 | {} |
| 53 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 54 | const std::vector<armnn::BackendId> m_Backends; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 55 | armnn::INetworkPtr m_Network; |
| 56 | std::vector<armnn::IOutputSlot*> m_OutputSlotForOperand; |
| 57 | std::vector<android::nn::RunTimePoolInfo> m_MemPools; |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 58 | bool m_DynamicInputsEncountered; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class LayerInputHandle |
| 62 | { |
| 63 | public: |
| 64 | LayerInputHandle(); |
| 65 | LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo); |
| 66 | |
| 67 | bool IsValid() const; |
| 68 | |
| 69 | void Connect(armnn::IInputSlot& inputSlot); |
| 70 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 71 | void Disconnect(armnn::IInputSlot& inputSlot); |
| 72 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 73 | const armnn::TensorInfo& GetTensorInfo() const; |
| 74 | |
| 75 | private: |
| 76 | armnn::IOutputSlot* m_OutputSlot; |
| 77 | bool m_Valid; |
| 78 | armnn::TensorInfo m_TensorInfo; |
| 79 | }; |
| 80 | |
| 81 | class ConstTensorPin |
| 82 | { |
| 83 | public: |
| 84 | // Creates an invalid tensor pin (can be used to signal errors) |
| 85 | // The optional flag can be set to indicate the tensor values were missing, but it was otherwise valid |
| 86 | ConstTensorPin(bool optional = false); |
| 87 | |
| 88 | // @param tensorInfo TensorInfo associated with the tensor. |
| 89 | // @param valueStart Start address of tensor data. Belongs to one of the memory pools associated with |
| 90 | // the model being converted. |
| 91 | // @param numBytes Number of bytes for the tensor data. |
Jan Eilers | a71c063 | 2021-04-12 13:12:19 +0100 | [diff] [blame] | 92 | ConstTensorPin(armnn::TensorInfo& tensorInfo, const void* valueStart, uint32_t numBytes, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 93 | const armnn::PermutationVector& mappings); |
| 94 | |
| 95 | ConstTensorPin(const ConstTensorPin& other) = delete; |
| 96 | ConstTensorPin(ConstTensorPin&& other) = default; |
| 97 | |
| 98 | bool IsValid() const; |
| 99 | bool IsOptional() const; |
| 100 | |
| 101 | const armnn::ConstTensor& GetConstTensor() const; |
| 102 | const armnn::ConstTensor* GetConstTensorPtr() const; |
| 103 | |
| 104 | private: |
| 105 | armnn::ConstTensor m_ConstTensor; |
| 106 | |
| 107 | // Owned memory for swizzled tensor data, only required if the tensor needed |
| 108 | // swizzling. Otherwise, @ref m_ConstTensor will reference memory from one of |
| 109 | // the pools associated with the model being converted. |
| 110 | std::vector<uint8_t> m_SwizzledTensorData; |
| 111 | |
| 112 | // optional flag to indicate that an invalid tensor pin is not an error, but the optional values were not given |
| 113 | bool m_Optional; |
| 114 | }; |
| 115 | |
| 116 | } // namespace armnn_driver |
| 117 | |
| 118 | /// |
| 119 | /// Utility functions |
| 120 | /// |
| 121 | |
| 122 | namespace |
| 123 | { |
| 124 | |
| 125 | using namespace armnn_driver; |
| 126 | using namespace android::nn; |
| 127 | |
| 128 | // Convenience function to log the reason for failing to convert a model. |
| 129 | // @return Always returns false (so that it can be used by callers as a quick way to signal an error and return) |
| 130 | template<class... Args> |
| 131 | static bool Fail(const char* formatStr, Args&&... args) |
| 132 | { |
| 133 | ALOGD(formatStr, std::forward<Args>(args)...); |
| 134 | return false; |
| 135 | } |
| 136 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 137 | // Convenience macro to call an Is*Supported function and log caller name together with reason for lack of support. |
| 138 | // Called as: FORWARD_LAYER_SUPPORT_FUNC(__func__, Is*Supported, backends, a, b, c, d, e) |
| 139 | #define FORWARD_LAYER_SUPPORT_FUNC(funcName, func, backends, supported, ...) \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 140 | try \ |
| 141 | { \ |
| 142 | for (auto&& backendId : backends) \ |
| 143 | { \ |
| 144 | auto layerSupportObject = armnn::GetILayerSupportByBackendId(backendId); \ |
Francis Murtagh | 0182473 | 2021-01-28 14:26:27 +0000 | [diff] [blame] | 145 | if (layerSupportObject.IsBackendRegistered()) \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 146 | { \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 147 | std::string reasonIfUnsupported; \ |
| 148 | supported = \ |
Francis Murtagh | 0182473 | 2021-01-28 14:26:27 +0000 | [diff] [blame] | 149 | layerSupportObject.func(__VA_ARGS__, armnn::Optional<std::string&>(reasonIfUnsupported)); \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 150 | if (supported) \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 151 | { \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 152 | break; \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 153 | } \ |
| 154 | else \ |
| 155 | { \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 156 | if (reasonIfUnsupported.size() > 0) \ |
| 157 | { \ |
| 158 | ALOGD("%s: not supported by armnn: %s", funcName, reasonIfUnsupported.c_str()); \ |
| 159 | } \ |
| 160 | else \ |
| 161 | { \ |
| 162 | ALOGD("%s: not supported by armnn", funcName); \ |
| 163 | } \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 164 | } \ |
| 165 | } \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 166 | else \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 167 | { \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 168 | ALOGD("%s: backend not registered: %s", funcName, backendId.Get().c_str()); \ |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 169 | } \ |
Teresa Charlin | 8f6429d | 2019-10-01 13:10:15 +0100 | [diff] [blame] | 170 | } \ |
| 171 | if (!supported) \ |
| 172 | { \ |
| 173 | ALOGD("%s: not supported by any specified backend", funcName); \ |
| 174 | } \ |
| 175 | } \ |
| 176 | catch (const armnn::InvalidArgumentException &e) \ |
| 177 | { \ |
| 178 | throw armnn::InvalidArgumentException(e, "Failed to check layer support", CHECK_LOCATION()); \ |
| 179 | } |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 180 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 181 | template<typename HalOperand> |
| 182 | armnn::TensorShape GetTensorShapeForOperand(const HalOperand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 183 | { |
| 184 | return armnn::TensorShape(operand.dimensions.size(), operand.dimensions.data()); |
| 185 | } |
| 186 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 187 | inline bool IsOperandTypeSupportedForTensors(V1_0::OperandType type) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 188 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 189 | return type == V1_0::OperandType::TENSOR_FLOAT32 || |
| 190 | type == V1_0::OperandType::TENSOR_QUANT8_ASYMM || |
| 191 | type == V1_0::OperandType::TENSOR_INT32; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 194 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 195 | |
Keith Davis | 7100649 | 2020-01-06 17:44:16 +0000 | [diff] [blame] | 196 | // Support within the 1.2 driver for specific tensor data types |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 197 | inline bool IsOperandTypeSupportedForTensors(V1_2::OperandType type) |
| 198 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 199 | return type == V1_2::OperandType::BOOL || |
Sadik Armagan | 793a70c | 2020-03-19 13:54:04 +0000 | [diff] [blame] | 200 | type == V1_2::OperandType::TENSOR_BOOL8 || |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 201 | type == V1_2::OperandType::TENSOR_FLOAT16 || |
| 202 | type == V1_2::OperandType::TENSOR_FLOAT32 || |
| 203 | type == V1_2::OperandType::TENSOR_QUANT8_ASYMM || |
Keith Davis | 7100649 | 2020-01-06 17:44:16 +0000 | [diff] [blame] | 204 | type == V1_2::OperandType::TENSOR_QUANT8_SYMM || |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 205 | type == V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL || |
| 206 | type == V1_2::OperandType::TENSOR_QUANT16_SYMM || |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 207 | type == V1_2::OperandType::TENSOR_INT32; |
| 208 | } |
| 209 | |
| 210 | #endif |
| 211 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 212 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 213 | |
| 214 | // Support within the 1.3 driver for specific tensor data types |
| 215 | inline bool IsOperandTypeSupportedForTensors(V1_3::OperandType type) |
| 216 | { |
| 217 | return type == V1_3::OperandType::BOOL || |
Sadik Armagan | 51ba2c6 | 2020-03-31 15:36:25 +0100 | [diff] [blame] | 218 | type == V1_3::OperandType::TENSOR_BOOL8 || |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 219 | type == V1_3::OperandType::TENSOR_FLOAT16 || |
| 220 | type == V1_3::OperandType::TENSOR_FLOAT32 || |
| 221 | type == V1_3::OperandType::TENSOR_QUANT8_ASYMM || |
Sadik Armagan | 51ba2c6 | 2020-03-31 15:36:25 +0100 | [diff] [blame] | 222 | type == V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED || |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 223 | type == V1_3::OperandType::TENSOR_QUANT8_SYMM || |
| 224 | type == V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL || |
| 225 | type == V1_3::OperandType::TENSOR_QUANT16_SYMM || |
| 226 | type == V1_3::OperandType::TENSOR_INT32; |
| 227 | } |
| 228 | |
| 229 | #endif |
| 230 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 231 | inline bool IsBool(V1_0::Operand) |
| 232 | { |
| 233 | return false; |
| 234 | } |
| 235 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 236 | inline bool Is12OrLaterOperand(V1_0::Operand) |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 237 | { |
| 238 | return false; |
| 239 | } |
| 240 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 241 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 242 | |
| 243 | inline bool IsBool(V1_2::Operand operand) |
| 244 | { |
| 245 | return operand.type == V1_2::OperandType::BOOL; |
| 246 | } |
| 247 | |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 248 | /// Checks if a operand is 1_2 Operand |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 249 | inline bool Is12OrLaterOperand(V1_2::Operand) |
| 250 | { |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | #endif |
| 255 | |
| 256 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 257 | |
| 258 | inline bool IsBool(V1_3::Operand operand) |
| 259 | { |
| 260 | return operand.type == V1_3::OperandType::BOOL; |
| 261 | } |
| 262 | |
| 263 | /// Checks if a operand is 1_2 Operand |
| 264 | inline bool Is12OrLaterOperand(V1_3::Operand) |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 265 | { |
| 266 | return true; |
| 267 | } |
| 268 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 269 | #endif |
| 270 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 271 | template<typename LayerHandleType> |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 272 | armnn::IConnectableLayer& AddReshapeLayer(armnn::INetwork& network, |
| 273 | LayerHandleType& inputLayer, |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 274 | armnn::TensorInfo reshapeInfo) |
| 275 | { |
| 276 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 277 | reshapeDescriptor.m_TargetShape = reshapeInfo.GetShape(); |
| 278 | |
| 279 | armnn::IConnectableLayer* reshapeLayer = network.AddReshapeLayer(reshapeDescriptor); |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 280 | ARMNN_ASSERT(reshapeLayer != nullptr); |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 281 | |
| 282 | // Attach the input layer to the reshape layer |
| 283 | inputLayer.Connect(reshapeLayer->GetInputSlot(0)); |
| 284 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapeInfo); |
| 285 | |
| 286 | return *reshapeLayer; |
| 287 | } |
| 288 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 289 | bool BroadcastTensor(LayerInputHandle& input0, |
| 290 | LayerInputHandle& input1, |
| 291 | armnn::IConnectableLayer* startLayer, |
| 292 | ConversionData& data) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 293 | { |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 294 | ARMNN_ASSERT(startLayer != nullptr); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 295 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 296 | const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo(); |
| 297 | const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo(); |
| 298 | |
| 299 | unsigned int inputDimensions0 = inputInfo0.GetNumDimensions(); |
| 300 | unsigned int inputDimensions1 = inputInfo1.GetNumDimensions(); |
| 301 | |
| 302 | if (inputDimensions0 == inputDimensions1) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 303 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 304 | // The inputs have the same number of dimensions, simply connect them to the given layer as they are |
| 305 | input0.Connect(startLayer->GetInputSlot(0)); |
| 306 | input1.Connect(startLayer->GetInputSlot(1)); |
| 307 | |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 308 | return true; |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Since the number of dimensions do not match then we need to add degenerate dimensions |
| 312 | // to the "smaller" tensor using a reshape, while keeping the order of the inputs. |
| 313 | |
| 314 | unsigned int maxInputDimensions = std::max(inputDimensions0, inputDimensions1); |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 315 | unsigned int sizeDifference = std::abs(armnn::numeric_cast<int>(inputDimensions0) - |
| 316 | armnn::numeric_cast<int>(inputDimensions1)); |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 317 | |
| 318 | bool input0IsSmaller = inputDimensions0 < inputDimensions1; |
| 319 | LayerInputHandle& smallInputHandle = input0IsSmaller ? input0 : input1; |
| 320 | const armnn::TensorInfo& smallInfo = smallInputHandle.GetTensorInfo(); |
| 321 | |
| 322 | const armnn::TensorShape& smallShape = smallInfo.GetShape(); |
| 323 | std::vector<unsigned int> reshapedDimensions(maxInputDimensions, 1); |
| 324 | for (unsigned int i = sizeDifference; i < maxInputDimensions; i++) |
| 325 | { |
| 326 | reshapedDimensions[i] = smallShape[i - sizeDifference]; |
| 327 | } |
| 328 | |
| 329 | armnn::TensorInfo reshapedInfo = smallInfo; |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 330 | reshapedInfo.SetShape(armnn::TensorShape{ armnn::numeric_cast<unsigned int>(reshapedDimensions.size()), |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 331 | reshapedDimensions.data() }); |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 332 | |
| 333 | // RehsapeDescriptor that is ignored in the IsReshapeSupported function |
| 334 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 335 | |
| 336 | bool isSupported = false; |
| 337 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 338 | IsReshapeSupported, |
| 339 | data.m_Backends, |
| 340 | isSupported, |
Derek Lamberti | 6fd4ceb | 2019-12-19 15:45:35 +0000 | [diff] [blame] | 341 | smallInfo, |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 342 | reshapedInfo, |
| 343 | reshapeDescriptor); |
| 344 | if (!isSupported) |
| 345 | { |
| 346 | return false; |
| 347 | } |
| 348 | |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 349 | ARMNN_ASSERT(data.m_Network != nullptr); |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 350 | armnn::IConnectableLayer& reshapeLayer = AddReshapeLayer(*data.m_Network, smallInputHandle, reshapedInfo); |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 351 | |
| 352 | if (input0IsSmaller) |
| 353 | { |
| 354 | // Input0 is the "smaller" tensor, connect the reshape layer as follows: |
| 355 | // |
| 356 | // Input0 Input1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 357 | // | | |
| 358 | // Reshape | |
| 359 | // \ / |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 360 | // StartLayer |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 361 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 362 | reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(0)); |
| 363 | input1.Connect(startLayer->GetInputSlot(1)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 364 | } |
| 365 | else |
| 366 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 367 | // Input1 is the "smaller" tensor, connect the reshape layer as follows: |
| 368 | // |
| 369 | // Input0 Input1 |
| 370 | // | | |
| 371 | // | Reshape |
| 372 | // \ / |
| 373 | // StartLayer |
| 374 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 375 | input0.Connect(startLayer->GetInputSlot(0)); |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 376 | reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(1)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 377 | } |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 378 | |
| 379 | return true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 382 | void CalcPadding(uint32_t input, |
| 383 | uint32_t kernel, |
| 384 | uint32_t stride, |
| 385 | uint32_t& outPadHead, |
| 386 | uint32_t& outPadTail, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 387 | android::nn::PaddingScheme scheme) |
| 388 | { |
| 389 | int32_t padHead; |
| 390 | int32_t padTail; |
| 391 | calculateExplicitPadding(input, stride, kernel, scheme, &padHead, &padTail); |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 392 | outPadHead = armnn::numeric_cast<uint32_t>(padHead); |
| 393 | outPadTail = armnn::numeric_cast<uint32_t>(padTail); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 396 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 397 | |
| 398 | void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t dilation, uint32_t& outPadHead, |
| 399 | uint32_t& outPadTail, android::nn::PaddingScheme scheme) |
| 400 | { |
| 401 | int32_t padHead; |
| 402 | int32_t padTail; |
| 403 | calculateExplicitPadding(input, stride, dilation, kernel, scheme, &padHead, &padTail); |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 404 | outPadHead = armnn::numeric_cast<uint32_t>(padHead); |
| 405 | outPadTail = armnn::numeric_cast<uint32_t>(padTail); |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Mike Kelly | 26123db | 2020-01-15 10:02:33 +0000 | [diff] [blame] | 408 | void CalcPaddingTransposeConv(uint32_t output, uint32_t kernel, int32_t stride, int32_t& outPadHead, |
Narumol Prangnawarat | c8bdb39 | 2019-08-01 15:51:44 +0100 | [diff] [blame] | 409 | int32_t& outPadTail, android::nn::PaddingScheme scheme) |
| 410 | { |
| 411 | calculateExplicitPaddingTransposeConv(output, stride, kernel, scheme, &outPadHead, &outPadTail); |
| 412 | } |
| 413 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 414 | #endif |
| 415 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 416 | Shape GetOperandShape(const V1_0::Operand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 417 | { |
| 418 | Shape shape; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 419 | shape.type = OperandType(operand.type); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 420 | shape.dimensions = operand.dimensions; |
| 421 | shape.scale = operand.scale; |
| 422 | shape.offset = operand.zeroPoint; |
| 423 | return shape; |
| 424 | } |
| 425 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 426 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 427 | |
| 428 | Shape GetOperandShape(const V1_2::Operand& operand) |
| 429 | { |
| 430 | Shape shape; |
| 431 | shape.type = OperandType(operand.type); |
| 432 | shape.dimensions = operand.dimensions; |
| 433 | shape.scale = operand.scale; |
| 434 | shape.offset = operand.zeroPoint; |
| 435 | return shape; |
| 436 | } |
| 437 | |
| 438 | #endif |
| 439 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 440 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 441 | |
| 442 | Shape GetOperandShape(const V1_3::Operand& operand) |
| 443 | { |
| 444 | Shape shape; |
| 445 | shape.type = OperandType(operand.type); |
| 446 | shape.dimensions = operand.dimensions; |
| 447 | shape.scale = operand.scale; |
| 448 | shape.offset = operand.zeroPoint; |
| 449 | return shape; |
| 450 | } |
| 451 | |
| 452 | #endif |
| 453 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 454 | // ArmNN requires the bias scale to be equal to the product of the weight and input scales, which is also |
| 455 | // what AndroidNN requires. However for some of the AndroidNN tests the values don't exactly match so |
Aron Virginas-Tar | a0baa17 | 2019-08-01 11:24:08 +0100 | [diff] [blame] | 456 | // we accept some tolerance. We don't want ArmNN itself to accept these inconsistencies as it is up to the |
| 457 | // user (us, in this case) to ensure they match. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 458 | void SanitizeBiasQuantizationScale(armnn::TensorInfo& biasInfo, |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 459 | const armnn::TensorInfo& weightInfo, |
| 460 | const armnn::TensorInfo& inputInfo) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 461 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 462 | if (weightInfo.HasPerAxisQuantization()) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 463 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 464 | // NOTE: Bias scale is always set to 0 for per-axis quantization and |
| 465 | // it needs to be calculated: scale[i] = input_scale * weight_scale[i] |
| 466 | auto UpdateBiasScaleValue = [&inputInfo](float biasScale) -> float |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 467 | { |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 468 | return biasScale * inputInfo.GetQuantizationScale(); |
| 469 | }; |
| 470 | |
| 471 | std::vector<float> biasScales(weightInfo.GetQuantizationScales()); |
| 472 | std::transform(biasScales.begin(), biasScales.end(), biasScales.begin(), UpdateBiasScaleValue); |
| 473 | |
| 474 | biasInfo.SetQuantizationScales(biasScales); |
Jan Eilers | a20d2b8 | 2021-04-27 09:21:08 +0100 | [diff] [blame] | 475 | // bias is expected to be a 1d tensor, set qdim=0 |
| 476 | biasInfo.SetQuantizationDim(0); |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 477 | |
| 478 | ALOGV("Bias quantization params have been updated for per-axis quantization"); |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | const float expectedBiasScale = weightInfo.GetQuantizationScale() * inputInfo.GetQuantizationScale(); |
| 483 | if (biasInfo.GetQuantizationScale() != expectedBiasScale) |
| 484 | { |
James Ward | 4e22f60 | 2020-10-20 15:50:33 +0100 | [diff] [blame] | 485 | if (armnnUtils::within_percentage_tolerance(biasInfo.GetQuantizationScale(), expectedBiasScale, 1.0f)) |
Aron Virginas-Tar | 9f0693b | 2019-11-06 14:32:30 +0000 | [diff] [blame] | 486 | { |
| 487 | ALOGW("Bias quantization scale has been modified to match input * weights"); |
| 488 | biasInfo.SetQuantizationScale(expectedBiasScale); |
| 489 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // 4D Tensor Permutations |
| 495 | const armnn::PermutationVector IdentityPermutation4D({ 0U, 1U, 2U, 3U }); |
David Monahan | 7f492ac | 2020-10-16 10:36:29 +0100 | [diff] [blame] | 496 | const armnn::PermutationVector IdentityPermutation3D({ 0U, 1U, 2U }); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 497 | const armnn::PermutationVector SwapDim1And2({ 0U, 2U, 1U, 3U }); |
| 498 | |
| 499 | // 3D Permutation Vectors |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 500 | const armnn::PermutationVector RotateTensorLeft({ 1U, 2U, 0U }); |
| 501 | const armnn::PermutationVector RotateTensorRight({ 2U, 0U, 1U }); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 502 | |
| 503 | template<typename OSlot> |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 504 | armnn::IConnectableLayer& AddTransposeLayer(armnn::INetwork& network, OSlot& input, |
| 505 | const armnn::PermutationVector& mappings) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 506 | { |
| 507 | // Add swizzle layer |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 508 | armnn::IConnectableLayer* const layer = network.AddTransposeLayer(mappings); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 509 | |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 510 | ARMNN_ASSERT(layer != nullptr); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 511 | |
| 512 | // Connect input to swizzle layer |
| 513 | input.Connect(layer->GetInputSlot(0)); |
| 514 | |
| 515 | // Setup swizzled output |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 516 | const armnn::TensorInfo outInfo = armnnUtils::TransposeTensorShape(input.GetTensorInfo(), mappings); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 517 | layer->GetOutputSlot(0).SetTensorInfo(outInfo); |
| 518 | |
| 519 | return *layer; |
| 520 | } |
| 521 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 522 | bool ValidateConcatOutputShape(const std::vector<armnn::TensorShape> & inputShapes, |
| 523 | const armnn::TensorShape & outputShape, |
| 524 | uint32_t concatDim) |
| 525 | { |
| 526 | // Validate the output shape is correct given the input shapes (which have just been validated) |
| 527 | unsigned int numDimensions = inputShapes[0].GetNumDimensions(); |
| 528 | if (outputShape.GetNumDimensions() != numDimensions) |
| 529 | { |
| 530 | return Fail("%s: Output shape has wrong number of dimensions", __func__); |
| 531 | } |
| 532 | |
| 533 | unsigned int outputSizeAlongConcatenatedDimension = 0; |
| 534 | for (unsigned int i = 0; i < inputShapes.size(); i++) |
| 535 | { |
| 536 | outputSizeAlongConcatenatedDimension += inputShapes[i][concatDim]; |
| 537 | } |
| 538 | |
| 539 | for (unsigned int i = 0; i < numDimensions; ++i) |
| 540 | { |
| 541 | if (i == concatDim) |
| 542 | { |
| 543 | if (outputShape[i] != outputSizeAlongConcatenatedDimension) |
| 544 | { |
| 545 | return Fail( |
| 546 | "%s: Invalid output shape for dimension %d (%d != %d)", |
| 547 | __func__, |
| 548 | i, |
| 549 | outputShape[i], |
| 550 | outputSizeAlongConcatenatedDimension); |
| 551 | } |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | if (outputShape[i] != inputShapes[0][i]) |
| 556 | { |
| 557 | return Fail("%s: Invalid output shape", __func__); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | bool RequiresReshape(armnn::TensorShape & inputShape) |
| 566 | { |
| 567 | return inputShape.GetNumDimensions() < 3; |
| 568 | } |
| 569 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 570 | void SwizzleInputs(armnn::INetwork& network, |
| 571 | std::vector<LayerInputHandle>& inputs, |
| 572 | std::vector<armnn::TensorShape>& inputShapes, |
| 573 | const armnn::PermutationVector& mapping) |
| 574 | { |
| 575 | if (!mapping.IsEqual(IdentityPermutation4D)) |
| 576 | { |
| 577 | size_t nInputs = inputs.size(); |
| 578 | for (size_t i=0; i<nInputs; ++i) |
| 579 | { |
| 580 | // add swizzle layer |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 581 | armnn::IConnectableLayer& swizzleLayer = AddTransposeLayer(network, inputs[i], mapping); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 582 | auto& outputSlot = swizzleLayer.GetOutputSlot(0); |
| 583 | auto& outputInfo = outputSlot.GetTensorInfo(); |
| 584 | // replace inputs with the swizzled ones |
| 585 | inputs[i] = LayerInputHandle(true, &outputSlot, outputInfo); |
| 586 | inputShapes[i] = inputs[i].GetTensorInfo().GetShape(); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 591 | bool TransposeInputTensors(ConversionData& data, |
| 592 | std::vector<LayerInputHandle>& inputs, |
| 593 | std::vector<armnn::TensorShape>& inputShapes, |
| 594 | const armnn::PermutationVector& mapping) |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 595 | { |
David Monahan | 7f492ac | 2020-10-16 10:36:29 +0100 | [diff] [blame] | 596 | // If we have a IdentityPermutation4D or IdentityPermutation3D then we are not permuting |
| 597 | if (!mapping.IsEqual(IdentityPermutation4D) && !mapping.IsEqual(IdentityPermutation3D)) |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 598 | { |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 599 | armnn::TensorInfo outputTransposeInfo; |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 600 | size_t nInputs = inputs.size(); |
| 601 | for (size_t i=0; i<nInputs; ++i) |
| 602 | { |
| 603 | // check permute layer |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 604 | armnn::TransposeDescriptor transposeDesc; |
| 605 | transposeDesc.m_DimMappings = mapping; |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 606 | outputTransposeInfo = armnnUtils::TransposeTensorShape(inputs[i].GetTensorInfo(), mapping); |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 607 | |
| 608 | bool isSupported = false; |
| 609 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 610 | IsTransposeSupported, |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 611 | data.m_Backends, |
| 612 | isSupported, |
| 613 | inputs[i].GetTensorInfo(), |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 614 | outputTransposeInfo, |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 615 | transposeDesc); |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 616 | if (!isSupported) |
| 617 | { |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | } |
| 622 | SwizzleInputs(*data.m_Network, inputs, inputShapes, mapping); |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 628 | bool CreateConcatPermutationParameters(const unsigned int numberOfDimensions, |
| 629 | int32_t & concatDimension, |
| 630 | std::pair<armnn::PermutationVector, armnn::PermutationVector> & permutationPair) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 631 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 632 | bool needPermute = false; |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 633 | ARMNN_ASSERT(numberOfDimensions >= 3); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 634 | |
| 635 | // ArmNN uses Compute Library subtensors to perform concatenation |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 636 | // This only works when concatenating along dimension 0, 1 or 3 for a 4-D tensor, |
| 637 | // or along dimension 0 or 2 for a 3-D tensor. |
| 638 | if (numberOfDimensions == 4 && concatDimension == 2) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 639 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 640 | concatDimension = 1; |
| 641 | permutationPair = std::make_pair(SwapDim1And2, SwapDim1And2); |
| 642 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 643 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 644 | else if (numberOfDimensions == 3 && concatDimension == 1) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 645 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 646 | concatDimension = 0; |
| 647 | permutationPair = std::make_pair(RotateTensorLeft, RotateTensorRight); |
| 648 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 649 | } |
David Monahan | 7f492ac | 2020-10-16 10:36:29 +0100 | [diff] [blame] | 650 | // If the tensor is 3-D and the concat dimension is 2 then we don't need to permute but we do need to change the |
| 651 | // permutation identity to only have 3 dimensions |
| 652 | else if (numberOfDimensions == 3 && concatDimension == 2) |
| 653 | { |
| 654 | permutationPair = std::make_pair(IdentityPermutation3D, IdentityPermutation3D); |
| 655 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 656 | return needPermute; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | } // anonymous namespace |
| 660 | |
| 661 | namespace armnn_driver |
| 662 | { |
| 663 | |
| 664 | //// Creates an ArmNN activation layer and connects it to the given layer, if the |
| 665 | //// passed in AndroidNN activation function requires so. |
| 666 | //// @return The end layer of the sequence of layers built for the given AndroidNN |
| 667 | //// activation function or nullptr if an error occurred (e.g. unsupported activation). |
| 668 | //// Note that the end layer matches the input layer if no activation is required |
| 669 | //// (the sequence of layers has length 1). |
| 670 | armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo, |
| 671 | ActivationFn activation, |
| 672 | armnn::IConnectableLayer* prevLayer, |
| 673 | ConversionData& data); |
| 674 | |
| 675 | } // namespace armnn_driver |
| 676 | |
| 677 | /// |
| 678 | /// Utility templates |
| 679 | /// |
| 680 | |
| 681 | namespace armnn_driver |
| 682 | { |
| 683 | |
| 684 | using namespace android::nn; |
| 685 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 686 | template<typename HalPolicy, |
| 687 | typename HalOperand = typename HalPolicy::Operand, |
| 688 | typename HalOperation = typename HalPolicy::Operation, |
| 689 | typename HalModel = typename HalPolicy::Model> |
| 690 | const HalOperand* GetInputOperand(const HalOperation& operation, |
| 691 | uint32_t inputIndex, |
| 692 | const HalModel& model, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 693 | bool failOnIndexOutOfBounds = true) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 694 | { |
| 695 | if (inputIndex >= operation.inputs.size()) |
| 696 | { |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 697 | if (failOnIndexOutOfBounds) |
| 698 | { |
| 699 | Fail("%s: invalid input index: %i out of %i", __func__, inputIndex, operation.inputs.size()); |
| 700 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 701 | return nullptr; |
| 702 | } |
| 703 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 704 | // Model should have been validated beforehand |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 705 | ARMNN_ASSERT(operation.inputs[inputIndex] < getMainModel(model).operands.size()); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 706 | return &getMainModel(model).operands[operation.inputs[inputIndex]]; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 709 | template<typename HalPolicy, |
| 710 | typename HalOperand = typename HalPolicy::Operand, |
| 711 | typename HalOperation = typename HalPolicy::Operation, |
| 712 | typename HalModel = typename HalPolicy::Model> |
| 713 | const HalOperand* GetOutputOperand(const HalOperation& operation, |
| 714 | uint32_t outputIndex, |
| 715 | const HalModel& model) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 716 | { |
| 717 | if (outputIndex >= operation.outputs.size()) |
| 718 | { |
| 719 | Fail("%s: invalid output index: %i out of %i", __func__, outputIndex, operation.outputs.size()); |
| 720 | return nullptr; |
| 721 | } |
| 722 | |
| 723 | // Model should have been validated beforehand |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 724 | ARMNN_ASSERT(operation.outputs[outputIndex] < getMainModel(model).operands.size()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 725 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 726 | return &getMainModel(model).operands[operation.outputs[outputIndex]]; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 729 | template<typename HalPolicy, |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 730 | typename HalOperand = typename HalPolicy::Operand, |
| 731 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 732 | const void* GetOperandValueReadOnlyAddress(const HalOperand& operand, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 733 | const HalModel& model, |
| 734 | const ConversionData& data, |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 735 | bool optional = false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 736 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 737 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 738 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 739 | const void* valueStart = nullptr; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 740 | switch (operand.lifetime) |
| 741 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 742 | case HalOperandLifeTime::CONSTANT_COPY: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 743 | { |
| 744 | // Constant found in model.operandValues |
| 745 | valueStart = &model.operandValues[operand.location.offset]; |
| 746 | break; |
| 747 | } |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 748 | case HalOperandLifeTime::CONSTANT_REFERENCE: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 749 | { |
| 750 | // Constant specified via a Memory object |
| 751 | valueStart = GetMemoryFromPool(operand.location, data.m_MemPools); |
| 752 | break; |
| 753 | } |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 754 | case HalOperandLifeTime::NO_VALUE: |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 755 | { |
| 756 | // An optional input tensor with no values is not an error so should not register as a fail |
| 757 | if (optional) |
| 758 | { |
| 759 | valueStart = nullptr; |
| 760 | break; |
| 761 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 762 | [[fallthrough]]; |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 763 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 764 | default: |
| 765 | { |
| 766 | // Unsupported/invalid (e.g. can't get value of an input to the model) |
| 767 | Fail("%s: unsupported/invalid operand lifetime: %s", |
| 768 | __func__, toString(operand.lifetime).c_str()); |
| 769 | valueStart = nullptr; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return valueStart; |
| 774 | } |
| 775 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 776 | template<typename HalPolicy, |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 777 | typename HalOperation = typename HalPolicy::Operation, |
| 778 | typename HalModel = typename HalPolicy::Model, |
| 779 | typename HalOperandType = typename HalPolicy::OperandType> |
| 780 | bool GetOperandType(const HalOperation& operation, |
| 781 | uint32_t inputIndex, |
| 782 | const HalModel& model, |
| 783 | HalOperandType& type) |
| 784 | { |
| 785 | using HalOperand = typename HalPolicy::Operand; |
| 786 | |
| 787 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 788 | if (!operand) |
| 789 | { |
| 790 | return Fail("%s: invalid input operand at index %i", __func__, inputIndex); |
| 791 | } |
| 792 | |
| 793 | type = operand->type; |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | template<typename HalPolicy, |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 798 | typename HalOperand = typename HalPolicy::Operand> |
| 799 | bool IsOperandConstant(const HalOperand& operand) |
| 800 | { |
| 801 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 802 | |
| 803 | HalOperandLifeTime lifetime = operand.lifetime; |
| 804 | |
| 805 | return lifetime == HalOperandLifeTime::CONSTANT_COPY || |
| 806 | lifetime == HalOperandLifeTime::CONSTANT_REFERENCE || |
| 807 | lifetime == HalOperandLifeTime::NO_VALUE; |
| 808 | } |
| 809 | |
| 810 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 811 | typename HalOperand = typename HalPolicy::Operand, |
| 812 | typename HalModel = typename HalPolicy::Model> |
| 813 | ConstTensorPin ConvertOperandToConstTensorPin(const HalOperand& operand, |
| 814 | const HalModel& model, |
| 815 | const ConversionData& data, |
| 816 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 817 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 818 | bool optional = false) |
| 819 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 820 | if (!IsOperandTypeSupportedForTensors(operand.type)) |
| 821 | { |
| 822 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand.type).c_str()); |
| 823 | return ConstTensorPin(); |
| 824 | } |
| 825 | |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 826 | if (!optional && !IsOperandConstant<HalPolicy>(operand)) |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 827 | { |
| 828 | Fail("%s: invalid operand lifetime: %s", __func__, toString(operand.lifetime).c_str()); |
| 829 | return ConstTensorPin(); |
| 830 | } |
| 831 | |
| 832 | const void* const valueStart = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data, optional); |
| 833 | if (!valueStart) |
| 834 | { |
| 835 | if (optional) |
| 836 | { |
| 837 | // optional tensor with no values is not really an error; return it as invalid, but marked as optional |
| 838 | return ConstTensorPin(true); |
| 839 | } |
| 840 | // mandatory tensor with no values |
| 841 | Fail("%s: failed to get operand address", __func__); |
| 842 | return ConstTensorPin(); |
| 843 | } |
| 844 | |
| 845 | armnn::TensorInfo tensorInfo = GetTensorInfoForOperand(operand); |
Teresa Charlin | 02dce09 | 2019-11-11 17:06:23 +0000 | [diff] [blame] | 846 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 847 | // Make sure isConstant flag is set. |
| 848 | tensorInfo.SetConstant(); |
| 849 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 850 | if (overrideTensorShape != nullptr) |
| 851 | { |
| 852 | tensorInfo.SetShape(*overrideTensorShape); |
| 853 | } |
| 854 | return ConstTensorPin(tensorInfo, valueStart, operand.location.length, dimensionMappings); |
| 855 | } |
| 856 | |
| 857 | template<typename HalPolicy, |
| 858 | typename HalOperation = typename HalPolicy::Operation, |
| 859 | typename HalModel = typename HalPolicy::Model> |
| 860 | ConstTensorPin ConvertOperationInputToConstTensorPin(const HalOperation& operation, |
| 861 | uint32_t inputIndex, |
| 862 | const HalModel& model, |
| 863 | const ConversionData& data, |
| 864 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 865 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 866 | bool optional = false) |
| 867 | { |
| 868 | using HalOperand = typename HalPolicy::Operand; |
| 869 | |
| 870 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 871 | if (!operand) |
| 872 | { |
| 873 | Fail("%s: failed to get input operand: index=%u", __func__, inputIndex); |
| 874 | return ConstTensorPin(); |
| 875 | } |
| 876 | return ConvertOperandToConstTensorPin<HalPolicy>(*operand, |
| 877 | model, |
| 878 | data, |
| 879 | dimensionMappings, |
| 880 | overrideTensorShape, |
| 881 | optional); |
| 882 | } |
| 883 | |
| 884 | template<typename HalPolicy, |
| 885 | typename OutputType, |
| 886 | typename HalOperandType = typename HalPolicy::OperandType, |
| 887 | typename HalOperation = typename HalPolicy::Operation, |
| 888 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 889 | bool GetInputScalar(const HalOperation& operation, |
| 890 | uint32_t inputIndex, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 891 | HalOperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 892 | OutputType& outValue, |
| 893 | const HalModel& model, |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 894 | const ConversionData& data, |
| 895 | bool optional = false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 896 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 897 | using HalOperand = typename HalPolicy::Operand; |
| 898 | |
| 899 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 900 | if (!optional && !operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 901 | { |
| 902 | return Fail("%s: invalid input operand at index %i", __func__, inputIndex); |
| 903 | } |
| 904 | |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 905 | if (!optional && operand->type != type) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 906 | { |
| 907 | return Fail("%s: unexpected operand type: %s (should be %s)", |
| 908 | __func__, toString(operand->type).c_str(), toString(type).c_str()); |
| 909 | } |
| 910 | |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 911 | if (!optional && operand->location.length != sizeof(OutputType)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 912 | { |
| 913 | return Fail("%s: incorrect operand location length: %i (should be %i)", |
| 914 | __func__, operand->location.length, sizeof(OutputType)); |
| 915 | } |
| 916 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 917 | const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 918 | if (!optional && !valueAddress) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 919 | { |
| 920 | return Fail("%s: failed to get address for operand", __func__); |
| 921 | } |
| 922 | |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 923 | if(!optional) |
| 924 | { |
| 925 | outValue = *(static_cast<const OutputType*>(valueAddress)); |
| 926 | } |
| 927 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 928 | return true; |
| 929 | } |
| 930 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 931 | template<typename HalPolicy, |
| 932 | typename HalOperation = typename HalPolicy::Operation, |
| 933 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 934 | bool GetInputInt32(const HalOperation& operation, |
| 935 | uint32_t inputIndex, |
| 936 | int32_t& outValue, |
| 937 | const HalModel& model, |
| 938 | const ConversionData& data) |
| 939 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 940 | return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::INT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 943 | template<typename HalPolicy, |
| 944 | typename HalOperation = typename HalPolicy::Operation, |
| 945 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 946 | bool GetInputFloat32(const HalOperation& operation, |
| 947 | uint32_t inputIndex, |
| 948 | float& outValue, |
| 949 | const HalModel& model, |
| 950 | const ConversionData& data) |
| 951 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 952 | return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::FLOAT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 953 | } |
| 954 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 955 | template<typename HalPolicy, |
| 956 | typename HalOperation = typename HalPolicy::Operation, |
| 957 | typename HalOperandType = typename HalPolicy::OperandType, |
| 958 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 959 | bool GetInputActivationFunctionImpl(const HalOperation& operation, |
| 960 | uint32_t inputIndex, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 961 | HalOperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 962 | ActivationFn& outActivationFunction, |
| 963 | const HalModel& model, |
| 964 | const ConversionData& data) |
| 965 | { |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 966 | if (type != HalOperandType::INT32 && type != HalOperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 967 | { |
| 968 | return Fail("%s: unexpected operand type: %s (should be %s or %s)", |
| 969 | __func__, |
| 970 | toString(type).c_str(), |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 971 | toString(HalOperandType::INT32).c_str(), |
| 972 | toString(HalOperandType::TENSOR_INT32).c_str()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | int32_t activationFunctionAsInt; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 976 | if (!GetInputScalar<HalPolicy>(operation, inputIndex, type, activationFunctionAsInt, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 977 | { |
| 978 | return Fail("%s: failed to get activation input value", __func__); |
| 979 | } |
| 980 | outActivationFunction = static_cast<ActivationFn>(activationFunctionAsInt); |
| 981 | return true; |
| 982 | } |
| 983 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 984 | template<typename HalPolicy, |
| 985 | typename HalOperation = typename HalPolicy::Operation, |
| 986 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 987 | bool GetInputActivationFunction(const HalOperation& operation, |
| 988 | uint32_t inputIndex, |
| 989 | ActivationFn& outActivationFunction, |
| 990 | const HalModel& model, |
| 991 | const ConversionData& data) |
| 992 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 993 | return GetInputActivationFunctionImpl<HalPolicy>(operation, |
| 994 | inputIndex, |
| 995 | HalPolicy::OperandType::INT32, |
| 996 | outActivationFunction, |
| 997 | model, |
| 998 | data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1001 | template<typename HalPolicy, |
| 1002 | typename HalOperation = typename HalPolicy::Operation, |
| 1003 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1004 | bool GetInputActivationFunctionFromTensor(const HalOperation& operation, |
| 1005 | uint32_t inputIndex, |
| 1006 | ActivationFn& outActivationFunction, |
| 1007 | const HalModel& model, |
| 1008 | const ConversionData& data) |
| 1009 | { |
| 1010 | // This only accepts a 1-D tensor of size 1 |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1011 | return GetInputActivationFunctionImpl<HalPolicy>(operation, |
| 1012 | inputIndex, |
| 1013 | HalPolicy::OperandType::INT32, |
| 1014 | outActivationFunction, |
| 1015 | model, |
| 1016 | data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1020 | template<typename HalPolicy, |
| 1021 | typename HalOperation = typename HalPolicy::Operation, |
| 1022 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1023 | bool GetOptionalInputActivation(const HalOperation& operation, |
| 1024 | uint32_t inputIndex, |
| 1025 | ActivationFn& activationFunction, |
| 1026 | const HalModel& model, |
| 1027 | const ConversionData& data) |
| 1028 | { |
| 1029 | if (operation.inputs.size() <= inputIndex) |
| 1030 | { |
| 1031 | activationFunction = ActivationFn::kActivationNone; |
| 1032 | } |
| 1033 | else |
| 1034 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1035 | if (!GetInputActivationFunction<HalPolicy>(operation, inputIndex, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1036 | { |
| 1037 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1038 | } |
| 1039 | } |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1043 | template<typename HalPolicy, |
| 1044 | typename ConvolutionDescriptor, |
| 1045 | typename HalOperation = typename HalPolicy::Operation, |
| 1046 | typename HalModel = typename HalPolicy::Model> |
Aron Virginas-Tar | 07c7c9a | 2019-06-12 14:03:35 +0100 | [diff] [blame] | 1047 | bool GetOptionalConvolutionDilationParams(const HalOperation& operation, |
| 1048 | uint32_t dilationXIndex, |
| 1049 | ConvolutionDescriptor& descriptor, |
| 1050 | const HalModel& model, |
| 1051 | const ConversionData& data) |
| 1052 | { |
| 1053 | bool success = true; |
| 1054 | if (operation.inputs.size() >= dilationXIndex + 2) |
| 1055 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1056 | success &= GetInputScalar<HalPolicy>(operation, |
| 1057 | dilationXIndex, |
| 1058 | HalPolicy::OperandType::INT32, |
| 1059 | descriptor.m_DilationX, |
| 1060 | model, |
| 1061 | data); |
| 1062 | success &= GetInputScalar<HalPolicy>(operation, |
| 1063 | dilationXIndex + 1, |
| 1064 | HalPolicy::OperandType::INT32, |
| 1065 | descriptor.m_DilationY, |
| 1066 | model, |
| 1067 | data); |
Aron Virginas-Tar | 07c7c9a | 2019-06-12 14:03:35 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | return success; |
| 1071 | } |
| 1072 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1073 | template<typename HalPolicy, |
David Monahan | 51e0b13 | 2020-04-20 16:12:06 +0100 | [diff] [blame] | 1074 | typename HalOperation = typename HalPolicy::Operation, |
| 1075 | typename HalModel = typename HalPolicy::Model> |
| 1076 | bool GetOptionalBool(const HalOperation& operation, |
| 1077 | uint32_t inputIndex, |
| 1078 | const HalModel& model, |
| 1079 | const ConversionData& data) |
| 1080 | { |
| 1081 | using HalOperand = typename HalPolicy::Operand; |
| 1082 | |
| 1083 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 1084 | if (!operand) |
| 1085 | { |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | if (!IsBool(*operand)) |
| 1090 | { |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
| 1095 | if (!valueAddress) |
| 1096 | { |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
| 1100 | if (*(static_cast<const bool*>(valueAddress))) |
| 1101 | { |
| 1102 | return true; |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | return false; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1111 | typename HalOperand = typename HalPolicy::Operand, |
| 1112 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1113 | bool GetTensorInt32Values(const HalOperand& operand, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1114 | std::vector<int32_t>& outValues, |
| 1115 | const HalModel& model, |
| 1116 | const ConversionData& data) |
| 1117 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1118 | if (operand.type != HalPolicy::OperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1119 | { |
| 1120 | return Fail("%s: invalid operand type: %s", __func__, toString(operand.type).c_str()); |
| 1121 | } |
| 1122 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1123 | const void* startAddress = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1124 | if (!startAddress) |
| 1125 | { |
| 1126 | return Fail("%s: failed to get operand address", __func__, operand.type); |
| 1127 | } |
| 1128 | |
| 1129 | // Check number of bytes is sensible |
| 1130 | const uint32_t numBytes = operand.location.length; |
| 1131 | if (numBytes % sizeof(int32_t) != 0) |
| 1132 | { |
| 1133 | return Fail("%s: invalid number of bytes: %i, expected to be a multiple of %i", |
| 1134 | __func__, numBytes, sizeof(int32_t)); |
| 1135 | } |
| 1136 | |
| 1137 | outValues.resize(numBytes / sizeof(int32_t)); |
| 1138 | memcpy(outValues.data(), startAddress, numBytes); |
| 1139 | return true; |
| 1140 | } |
| 1141 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1142 | template<typename HalPolicy, |
| 1143 | typename HalOperation = typename HalPolicy::Operation, |
| 1144 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1145 | bool GetInputPaddingScheme(const HalOperation& operation, |
| 1146 | uint32_t inputIndex, |
| 1147 | PaddingScheme& outPaddingScheme, |
| 1148 | const HalModel& model, |
| 1149 | const ConversionData& data) |
| 1150 | { |
| 1151 | int32_t paddingSchemeAsInt; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1152 | if (!GetInputInt32<HalPolicy>(operation, inputIndex, paddingSchemeAsInt, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1153 | { |
| 1154 | return Fail("%s: failed to get padding scheme input value", __func__); |
| 1155 | } |
| 1156 | |
| 1157 | outPaddingScheme = static_cast<android::nn::PaddingScheme>(paddingSchemeAsInt); |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1161 | template<typename HalPolicy, |
| 1162 | typename HalOperation = typename HalPolicy::Operation, |
| 1163 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1164 | LayerInputHandle ConvertToLayerInputHandle(const HalOperation& operation, |
| 1165 | uint32_t inputIndex, |
| 1166 | const HalModel& model, |
| 1167 | ConversionData& data) |
| 1168 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1169 | using HalOperand = typename HalPolicy::Operand; |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1170 | using HalOperandType = typename HalPolicy::OperandType; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1171 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 1172 | |
| 1173 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1174 | if (!operand) |
| 1175 | { |
| 1176 | Fail("%s: failed to get input operand %i", __func__, inputIndex); |
| 1177 | return LayerInputHandle(); |
| 1178 | } |
| 1179 | |
| 1180 | if (!IsOperandTypeSupportedForTensors(operand->type)) |
| 1181 | { |
| 1182 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand->type).c_str()); |
| 1183 | return LayerInputHandle(); |
| 1184 | } |
| 1185 | |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1186 | try |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1187 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1188 | armnn::TensorInfo operandTensorInfo = GetTensorInfoForOperand(*operand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 1189 | if (IsDynamicTensor(operandTensorInfo)) |
| 1190 | { |
| 1191 | Fail("%s: dynamic input tensors are not supported", __func__); |
| 1192 | return LayerInputHandle(); |
| 1193 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1194 | |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1195 | switch (operand->lifetime) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1196 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1197 | case HalOperandLifeTime::MODEL_INPUT: |
Aron Virginas-Tar | 000117b | 2019-07-25 16:24:49 +0100 | [diff] [blame] | 1198 | { |
| 1199 | // NOTE: We must check whether we can support the input tensor on at least one |
| 1200 | // of the provided backends; otherwise we cannot convert the operation |
| 1201 | bool isInputSupported = false; |
| 1202 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1203 | IsInputSupported, |
| 1204 | data.m_Backends, |
| 1205 | isInputSupported, |
| 1206 | operandTensorInfo); |
| 1207 | |
| 1208 | if (!isInputSupported) |
| 1209 | { |
| 1210 | Fail("%s: unsupported input tensor", __func__); |
| 1211 | return LayerInputHandle(); |
| 1212 | } |
| 1213 | |
James Ward | 4e22f60 | 2020-10-20 15:50:33 +0100 | [diff] [blame] | 1214 | [[clang::fallthrough]]; // intentional fallthrough |
Aron Virginas-Tar | 000117b | 2019-07-25 16:24:49 +0100 | [diff] [blame] | 1215 | } |
| 1216 | case HalOperandLifeTime::TEMPORARY_VARIABLE: // intentional fallthrough |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1217 | case HalOperandLifeTime::MODEL_OUTPUT: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1218 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1219 | // The tensor is either an operand internal to the model, or a model input. |
| 1220 | // It can be associated with an ArmNN output slot for an existing layer. |
| 1221 | |
| 1222 | // m_OutputSlotForOperand[...] can be nullptr if the previous layer could not be converted |
| 1223 | const uint32_t operandIndex = operation.inputs[inputIndex]; |
| 1224 | return LayerInputHandle(true, data.m_OutputSlotForOperand[operandIndex], operandTensorInfo); |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1225 | } |
Aron Virginas-Tar | 000117b | 2019-07-25 16:24:49 +0100 | [diff] [blame] | 1226 | case HalOperandLifeTime::CONSTANT_COPY: // intentional fallthrough |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1227 | case HalOperandLifeTime::CONSTANT_REFERENCE: |
| 1228 | { |
| 1229 | // The tensor has an already known constant value, and can be converted into an ArmNN Constant layer. |
| 1230 | ConstTensorPin tensorPin = ConvertOperandToConstTensorPin<HalPolicy>(*operand, model, data); |
| 1231 | if (tensorPin.IsValid()) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1232 | { |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1233 | bool isSupported = false; |
| 1234 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1235 | IsConstantSupported, |
| 1236 | data.m_Backends, |
| 1237 | isSupported, |
| 1238 | tensorPin.GetConstTensor().GetInfo()); |
Mike Kelly | 28e3d9f | 2019-08-07 14:55:04 +0100 | [diff] [blame] | 1239 | if (!isSupported) |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1240 | { |
| 1241 | return LayerInputHandle(); |
| 1242 | } |
| 1243 | |
| 1244 | armnn::IConnectableLayer* constantLayer = |
| 1245 | data.m_Network->AddConstantLayer(tensorPin.GetConstTensor()); |
| 1246 | armnn::IOutputSlot& outputSlot = constantLayer->GetOutputSlot(0); |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 1247 | armnn::TensorInfo constantTensorInfo = tensorPin.GetConstTensor().GetInfo(); |
| 1248 | outputSlot.SetTensorInfo(constantTensorInfo); |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1249 | |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 1250 | return LayerInputHandle(true, &outputSlot, constantTensorInfo); |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1251 | } |
| 1252 | else |
| 1253 | { |
| 1254 | Fail("%s: invalid operand tensor", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1255 | return LayerInputHandle(); |
| 1256 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1257 | break; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1258 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1259 | default: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1260 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1261 | // Unsupported lifetime for an input tensor |
| 1262 | Fail("%s: unsupported lifetime for input tensor: %s", |
| 1263 | __func__, toString(operand->lifetime).c_str()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1264 | return LayerInputHandle(); |
| 1265 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1266 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1267 | } |
| 1268 | catch (UnsupportedOperand<HalOperandType>& e) |
| 1269 | { |
| 1270 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 1271 | return LayerInputHandle(); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1275 | |
| 1276 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 1277 | template<typename HalPolicy> |
| 1278 | LayerInputHandle ConvertToLayerInputHandle(const ::android::hardware::neuralnetworks::V1_3::Operation& operation, |
| 1279 | uint32_t inputIndex, |
| 1280 | const::android::hardware::neuralnetworks::V1_3::Model& model, |
| 1281 | ConversionData& data) |
| 1282 | { |
| 1283 | using HalOperand = typename HalPolicy::Operand; |
| 1284 | using HalOperandType = typename HalPolicy::OperandType; |
| 1285 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 1286 | |
| 1287 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 1288 | if (!operand) |
| 1289 | { |
| 1290 | Fail("%s: failed to get input operand %i", __func__, inputIndex); |
| 1291 | return LayerInputHandle(); |
| 1292 | } |
| 1293 | |
| 1294 | if (!IsOperandTypeSupportedForTensors(operand->type)) |
| 1295 | { |
| 1296 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand->type).c_str()); |
| 1297 | return LayerInputHandle(); |
| 1298 | } |
| 1299 | |
| 1300 | try |
| 1301 | { |
| 1302 | armnn::TensorInfo operandTensorInfo = GetTensorInfoForOperand(*operand); |
Finn Williams | 9a04441 | 2020-08-17 19:08:35 +0100 | [diff] [blame] | 1303 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1304 | if (IsDynamicTensor(operandTensorInfo)) |
| 1305 | { |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 1306 | data.m_DynamicInputsEncountered = true; |
| 1307 | |
Finn Williams | 9a04441 | 2020-08-17 19:08:35 +0100 | [diff] [blame] | 1308 | const uint32_t operandIndex = operation.inputs[inputIndex]; |
| 1309 | |
| 1310 | // Check if the dynamic input tensors have been inferred by one of the previous layers |
| 1311 | // If not we can't support them |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 1312 | if (data.m_OutputSlotForOperand.size() >= operandIndex && data.m_OutputSlotForOperand[operandIndex]) |
Finn Williams | 9a04441 | 2020-08-17 19:08:35 +0100 | [diff] [blame] | 1313 | { |
| 1314 | operandTensorInfo = data.m_OutputSlotForOperand[operandIndex]->GetTensorInfo(); |
| 1315 | } |
| 1316 | else |
| 1317 | { |
| 1318 | Fail("%s: Type 2 dynamic input tensors are not supported", __func__); |
| 1319 | return LayerInputHandle(); |
| 1320 | } |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | switch (operand->lifetime) |
| 1324 | { |
| 1325 | case HalOperandLifeTime::SUBGRAPH_INPUT: |
| 1326 | { |
| 1327 | // NOTE: We must check whether we can support the input tensor on at least one |
| 1328 | // of the provided backends; otherwise we cannot convert the operation |
| 1329 | bool isInputSupported = false; |
| 1330 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1331 | IsInputSupported, |
| 1332 | data.m_Backends, |
| 1333 | isInputSupported, |
| 1334 | operandTensorInfo); |
| 1335 | |
| 1336 | if (!isInputSupported) |
| 1337 | { |
| 1338 | Fail("%s: unsupported input tensor", __func__); |
| 1339 | return LayerInputHandle(); |
| 1340 | } |
| 1341 | |
James Ward | 4e22f60 | 2020-10-20 15:50:33 +0100 | [diff] [blame] | 1342 | [[clang::fallthrough]]; // intentional fallthrough |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1343 | } |
| 1344 | case HalOperandLifeTime::TEMPORARY_VARIABLE: // intentional fallthrough |
| 1345 | case HalOperandLifeTime::SUBGRAPH_OUTPUT: |
| 1346 | { |
| 1347 | // The tensor is either an operand internal to the model, or a model input. |
| 1348 | // It can be associated with an ArmNN output slot for an existing layer. |
| 1349 | |
| 1350 | // m_OutputSlotForOperand[...] can be nullptr if the previous layer could not be converted |
| 1351 | const uint32_t operandIndex = operation.inputs[inputIndex]; |
| 1352 | return LayerInputHandle(true, data.m_OutputSlotForOperand[operandIndex], operandTensorInfo); |
| 1353 | } |
| 1354 | case HalOperandLifeTime::CONSTANT_COPY: // intentional fallthrough |
| 1355 | case HalOperandLifeTime::CONSTANT_REFERENCE: |
| 1356 | { |
| 1357 | // The tensor has an already known constant value, and can be converted into an ArmNN Constant layer. |
| 1358 | ConstTensorPin tensorPin = ConvertOperandToConstTensorPin<HalPolicy>(*operand, model, data); |
| 1359 | if (tensorPin.IsValid()) |
| 1360 | { |
| 1361 | bool isSupported = false; |
| 1362 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1363 | IsConstantSupported, |
| 1364 | data.m_Backends, |
| 1365 | isSupported, |
| 1366 | tensorPin.GetConstTensor().GetInfo()); |
| 1367 | if (!isSupported) |
| 1368 | { |
| 1369 | return LayerInputHandle(); |
| 1370 | } |
| 1371 | |
| 1372 | armnn::IConnectableLayer* constantLayer = |
| 1373 | data.m_Network->AddConstantLayer(tensorPin.GetConstTensor()); |
| 1374 | armnn::IOutputSlot& outputSlot = constantLayer->GetOutputSlot(0); |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 1375 | armnn::TensorInfo constantTensorInfo = tensorPin.GetConstTensor().GetInfo(); |
| 1376 | outputSlot.SetTensorInfo(constantTensorInfo); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1377 | |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 1378 | return LayerInputHandle(true, &outputSlot, constantTensorInfo); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1379 | } |
| 1380 | else |
| 1381 | { |
| 1382 | Fail("%s: invalid operand tensor", __func__); |
| 1383 | return LayerInputHandle(); |
| 1384 | } |
| 1385 | break; |
| 1386 | } |
| 1387 | default: |
| 1388 | { |
| 1389 | // Unsupported lifetime for an input tensor |
| 1390 | Fail("%s: unsupported lifetime for input tensor: %s", |
| 1391 | __func__, toString(operand->lifetime).c_str()); |
| 1392 | return LayerInputHandle(); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | catch (UnsupportedOperand<HalOperandType>& e) |
| 1397 | { |
| 1398 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 1399 | return LayerInputHandle(); |
| 1400 | } |
| 1401 | } |
| 1402 | #endif |
| 1403 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1404 | template<typename HalPolicy, |
| 1405 | typename HalOperation = typename HalPolicy::Operation, |
| 1406 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1407 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 1408 | uint32_t operationOutputIndex, |
| 1409 | armnn::IConnectableLayer& layer, |
| 1410 | uint32_t layerOutputIndex, |
| 1411 | const HalModel& model, |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 1412 | ConversionData& data, |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1413 | const armnn::TensorInfo* overrideOutputInfo = nullptr, |
Sadik Armagan | dbda4b7 | 2020-09-03 11:33:07 +0100 | [diff] [blame] | 1414 | const std::function <void (const armnn::TensorInfo&, bool&)>& validateFunc = nullptr, |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1415 | const ActivationFn& activationFunction = ActivationFn::kActivationNone, |
Sadik Armagan | dbda4b7 | 2020-09-03 11:33:07 +0100 | [diff] [blame] | 1416 | bool inferOutputShapes = false) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1417 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1418 | using HalOperand = typename HalPolicy::Operand; |
| 1419 | |
| 1420 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, operationOutputIndex, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1421 | if ((outputOperand == nullptr) || (operationOutputIndex >= layer.GetNumOutputSlots())) |
| 1422 | { |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | armnn::IOutputSlot& outputSlot = layer.GetOutputSlot(layerOutputIndex); |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 1427 | if (overrideOutputInfo == nullptr) |
| 1428 | { |
| 1429 | outputSlot.SetTensorInfo(GetTensorInfoForOperand(*outputOperand)); |
| 1430 | } |
| 1431 | else |
| 1432 | { |
| 1433 | outputSlot.SetTensorInfo(*overrideOutputInfo); |
| 1434 | } |
| 1435 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1436 | bool isSupported = false; |
Sadik Armagan | dbda4b7 | 2020-09-03 11:33:07 +0100 | [diff] [blame] | 1437 | if (validateFunc && (IsDynamicTensor(outputSlot.GetTensorInfo()) || inferOutputShapes)) |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 1438 | { |
Sadik Armagan | dbda4b7 | 2020-09-03 11:33:07 +0100 | [diff] [blame] | 1439 | // Type one dynamic tensors require the previous layer's output shape for inference |
| 1440 | for (unsigned int inputSlotIndex = 0; inputSlotIndex < layer.GetNumInputSlots(); ++inputSlotIndex) |
| 1441 | { |
| 1442 | if(!layer.GetInputSlot(inputSlotIndex).GetConnection()) |
| 1443 | { |
| 1444 | return false; |
| 1445 | } |
| 1446 | } |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 1447 | // IsTensorInfoSet will infer the dynamic output shape |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1448 | outputSlot.IsTensorInfoSet(); |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 1449 | // Once the shape is inferred we can validate it |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1450 | validateFunc(outputSlot.GetTensorInfo(), isSupported); |
| 1451 | |
Sadik Armagan | dbda4b7 | 2020-09-03 11:33:07 +0100 | [diff] [blame] | 1452 | if(!isSupported) |
| 1453 | { |
| 1454 | for (unsigned int inputSlotIndex = 0; inputSlotIndex < layer.GetNumInputSlots(); ++inputSlotIndex) |
| 1455 | { |
| 1456 | layer.GetInputSlot(inputSlotIndex).GetConnection()->Disconnect(layer.GetInputSlot(inputSlotIndex)); |
| 1457 | } |
| 1458 | return false; |
| 1459 | } |
Sadik Armagan | 813f230 | 2020-05-19 14:10:30 +0100 | [diff] [blame] | 1460 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1461 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1462 | const uint32_t operandIndex = operation.outputs[operationOutputIndex]; |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1463 | |
| 1464 | if (activationFunction != ActivationFn::kActivationNone) |
| 1465 | { |
| 1466 | const armnn::TensorInfo& activationOutputInfo = outputSlot.GetTensorInfo(); |
| 1467 | armnn::IConnectableLayer* const endLayer = ProcessActivation(activationOutputInfo, activationFunction, |
| 1468 | &layer, data); |
| 1469 | |
| 1470 | if (!endLayer) |
| 1471 | { |
| 1472 | return Fail("%s: ProcessActivation failed", __func__); |
| 1473 | } |
| 1474 | |
| 1475 | armnn::IOutputSlot& activationOutputSlot = endLayer->GetOutputSlot(layerOutputIndex); |
| 1476 | data.m_OutputSlotForOperand[operandIndex] = &activationOutputSlot; |
| 1477 | } |
| 1478 | else |
| 1479 | { |
| 1480 | data.m_OutputSlotForOperand[operandIndex] = &outputSlot; |
| 1481 | } |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1482 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1483 | return true; |
| 1484 | } |
| 1485 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1486 | template<typename HalPolicy, |
| 1487 | typename HalOperation = typename HalPolicy::Operation, |
| 1488 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1489 | armnn::DataLayout OptionalDataLayout(const HalOperation& operation, |
| 1490 | uint32_t inputIndex, |
| 1491 | const HalModel& model, |
| 1492 | ConversionData& data) |
| 1493 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1494 | using HalOperand = typename HalPolicy::Operand; |
| 1495 | |
| 1496 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1497 | if (!operand) |
| 1498 | { |
| 1499 | return armnn::DataLayout::NHWC; |
| 1500 | } |
| 1501 | |
| 1502 | if (!IsBool(*operand)) |
| 1503 | { |
| 1504 | return armnn::DataLayout::NHWC; |
| 1505 | } |
| 1506 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1507 | const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1508 | if (!valueAddress) |
| 1509 | { |
| 1510 | return armnn::DataLayout::NHWC; |
| 1511 | } |
| 1512 | |
| 1513 | if (*(static_cast<const bool*>(valueAddress))) |
| 1514 | { |
| 1515 | return armnn::DataLayout::NCHW; |
| 1516 | } |
| 1517 | else |
| 1518 | { |
| 1519 | return armnn::DataLayout::NHWC; |
| 1520 | } |
| 1521 | } |
| 1522 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1523 | template<typename HalPolicy, |
| 1524 | typename HalOperation = typename HalPolicy::Operation, |
| 1525 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1526 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 1527 | uint32_t outputIndex, |
| 1528 | armnn::IConnectableLayer& layer, |
| 1529 | const HalModel& model, |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 1530 | ConversionData& data, |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1531 | const armnn::TensorInfo* overrideOutputInfo = nullptr, |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1532 | const std::function <void (const armnn::TensorInfo&, bool&)>& validateFunc = nullptr, |
| 1533 | const ActivationFn& activationFunction = ActivationFn::kActivationNone) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1534 | { |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 1535 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, |
| 1536 | outputIndex, |
| 1537 | layer, |
| 1538 | outputIndex, |
| 1539 | model, |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 1540 | data, |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1541 | overrideOutputInfo, |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1542 | validateFunc, |
| 1543 | activationFunction); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1546 | template<typename HalPolicy, |
| 1547 | typename HalOperation = typename HalPolicy::Operation, |
| 1548 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1549 | bool ConvertToActivation(const HalOperation& operation, |
| 1550 | const char* operationName, |
| 1551 | const armnn::ActivationDescriptor& activationDesc, |
| 1552 | const HalModel& model, |
| 1553 | ConversionData& data) |
| 1554 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1555 | using HalOperand = typename HalPolicy::Operand; |
| 1556 | |
| 1557 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1558 | if (!input.IsValid()) |
| 1559 | { |
| 1560 | return Fail("%s: Input 0 is invalid", operationName); |
| 1561 | } |
| 1562 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1563 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1564 | if (!outputOperand) |
| 1565 | { |
| 1566 | return false; |
| 1567 | } |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1568 | |
| 1569 | const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1570 | |
| 1571 | bool isSupported = false; |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1572 | |
| 1573 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 1574 | { |
| 1575 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1576 | IsActivationSupported, |
| 1577 | data.m_Backends, |
| 1578 | isSupported, |
| 1579 | input.GetTensorInfo(), |
| 1580 | outInfo, |
| 1581 | activationDesc); |
| 1582 | }; |
| 1583 | |
| 1584 | if(IsDynamicTensor(outInfo)) |
| 1585 | { |
| 1586 | isSupported = AreDynamicTensorsSupported(); |
| 1587 | } |
| 1588 | else |
| 1589 | { |
| 1590 | validateFunc(outInfo, isSupported); |
| 1591 | } |
| 1592 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1593 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1594 | { |
| 1595 | return false; |
| 1596 | } |
| 1597 | |
| 1598 | armnn::IConnectableLayer* layer = data.m_Network->AddActivationLayer(activationDesc); |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 1599 | ARMNN_ASSERT(layer != nullptr); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1600 | input.Connect(layer->GetInputSlot(0)); |
| 1601 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1602 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1605 | template<typename HalPolicy, |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1606 | typename HalOperation = typename HalPolicy::Operation, |
| 1607 | typename HalModel = typename HalPolicy::Model> |
| 1608 | bool ConvertReLu(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1609 | { |
| 1610 | armnn::ActivationDescriptor desc; |
| 1611 | desc.m_Function = armnn::ActivationFunction::ReLu; |
| 1612 | |
| 1613 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1614 | } |
| 1615 | |
| 1616 | template<typename HalPolicy, |
| 1617 | typename HalOperation = typename HalPolicy::Operation, |
| 1618 | typename HalModel = typename HalPolicy::Model> |
| 1619 | bool ConvertReLu1(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1620 | { |
| 1621 | armnn::ActivationDescriptor desc; |
| 1622 | desc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 1623 | desc.m_A = 1.0f; |
| 1624 | desc.m_B = -1.0f; |
| 1625 | |
| 1626 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1627 | } |
| 1628 | |
| 1629 | template<typename HalPolicy, |
| 1630 | typename HalOperation = typename HalPolicy::Operation, |
| 1631 | typename HalModel = typename HalPolicy::Model> |
| 1632 | bool ConvertReLu6(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1633 | { |
| 1634 | armnn::ActivationDescriptor desc; |
| 1635 | desc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 1636 | desc.m_A = 6.0f; |
| 1637 | |
| 1638 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1639 | } |
| 1640 | |
| 1641 | template<typename HalPolicy, |
| 1642 | typename HalOperation = typename HalPolicy::Operation, |
| 1643 | typename HalModel = typename HalPolicy::Model> |
| 1644 | bool ConvertTanH(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1645 | { |
| 1646 | armnn::ActivationDescriptor desc; |
| 1647 | desc.m_Function = armnn::ActivationFunction::TanH; |
| 1648 | desc.m_A = 1.0f; // android nn does not support tanH parameters |
| 1649 | desc.m_B = 1.0f; // set to 1.0f for unity scaling |
| 1650 | |
| 1651 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1652 | } |
| 1653 | |
| 1654 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1655 | typename HalOperation = typename HalPolicy::Operation, |
| 1656 | typename HalModel = typename HalPolicy::Model> |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 1657 | bool ConvertPaddings(const HalOperation& operation, |
| 1658 | const HalModel& model, |
| 1659 | ConversionData& data, |
| 1660 | unsigned int rank, |
| 1661 | armnn::PadDescriptor& padDescriptor) |
| 1662 | { |
| 1663 | using HalOperand = typename HalPolicy::Operand; |
| 1664 | |
| 1665 | const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 1666 | if (!paddingsOperand) |
| 1667 | { |
| 1668 | return Fail("%s: Could not read paddings operand", __func__); |
| 1669 | } |
| 1670 | |
| 1671 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
| 1672 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != rank * 2) |
| 1673 | { |
| 1674 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, rank); |
| 1675 | } |
| 1676 | |
| 1677 | std::vector<int32_t> paddings; |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 1678 | if (!GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data)) |
| 1679 | { |
| 1680 | return Fail("%s: Operation has invalid or unsupported paddings operand", __func__); |
| 1681 | } |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 1682 | |
| 1683 | // add padding for each dimension of input tensor. |
| 1684 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 1685 | { |
| 1686 | int paddingBeforeInput = paddings[i]; |
| 1687 | int paddingAfterInput = paddings[i + 1]; |
| 1688 | |
| 1689 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 1690 | { |
| 1691 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 1692 | } |
| 1693 | |
| 1694 | padDescriptor.m_PadList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
| 1695 | } |
| 1696 | |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
| 1700 | template<typename HalPolicy, |
| 1701 | typename HalOperation = typename HalPolicy::Operation, |
| 1702 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1703 | bool ConvertPooling2d(const HalOperation& operation, |
| 1704 | const char* operationName, |
| 1705 | armnn::PoolingAlgorithm poolType, |
| 1706 | const HalModel& model, |
| 1707 | ConversionData& data) |
| 1708 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1709 | using HalOperand = typename HalPolicy::Operand; |
| 1710 | using HalOperandType = typename HalPolicy::OperandType; |
| 1711 | |
| 1712 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1713 | if (!input.IsValid()) |
| 1714 | { |
FinnWilliamsArm | 493e9b7 | 2019-11-25 16:02:07 +0000 | [diff] [blame] | 1715 | return Fail("%s: Operation Could not read input 0", operationName); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1716 | } |
| 1717 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1718 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1719 | if (!output) |
| 1720 | { |
| 1721 | return Fail("%s: Could not read output 0", __func__); |
| 1722 | } |
| 1723 | |
| 1724 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1725 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1726 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1727 | armnn::Pooling2dDescriptor desc; |
| 1728 | desc.m_PoolType = poolType; |
| 1729 | desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor; |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1730 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1731 | |
| 1732 | ActivationFn activation; |
| 1733 | |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 1734 | auto inputSize = operation.inputs.size(); |
| 1735 | |
| 1736 | if (inputSize >= 10) |
| 1737 | { |
| 1738 | // one input, 9 parameters (padding l r t b, stridex, stridey, width, height, activation type) |
| 1739 | if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 1740 | !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 1741 | !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 1742 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 1743 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1744 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_StrideY, model, data) || |
| 1745 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_PoolWidth, model, data) || |
| 1746 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_PoolHeight, model, data) || |
| 1747 | !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data)) |
| 1748 | { |
| 1749 | return Fail("%s: Operation has invalid inputs", operationName); |
| 1750 | } |
| 1751 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1752 | if (Is12OrLaterOperand(*output)) |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 1753 | { |
| 1754 | desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 10, model, data); |
| 1755 | } |
| 1756 | } |
| 1757 | else |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1758 | { |
| 1759 | // one input, 6 parameters (padding, stridex, stridey, width, height, activation type) |
| 1760 | android::nn::PaddingScheme scheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1761 | if (!GetInputPaddingScheme<HalPolicy>(operation, 1, scheme, model, data) || |
| 1762 | !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1763 | !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_StrideY, model, data) || |
| 1764 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PoolWidth, model, data) || |
| 1765 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PoolHeight, model, data) || |
| 1766 | !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1767 | { |
| 1768 | return Fail("%s: Operation has invalid inputs", operationName); |
| 1769 | } |
| 1770 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1771 | if (Is12OrLaterOperand(*output)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1772 | { |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 1773 | desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 7, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1774 | } |
FinnWilliamsArm | 493e9b7 | 2019-11-25 16:02:07 +0000 | [diff] [blame] | 1775 | |
| 1776 | const armnnUtils::DataLayoutIndexed dataLayout(desc.m_DataLayout); |
| 1777 | const unsigned int inputWidth = inputInfo.GetShape()[dataLayout.GetWidthIndex()]; |
| 1778 | const unsigned int inputHeight = inputInfo.GetShape()[dataLayout.GetHeightIndex()]; |
| 1779 | |
| 1780 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, scheme); |
| 1781 | CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, scheme); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1784 | bool isSupported = false; |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1785 | |
| 1786 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 1787 | { |
| 1788 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1789 | IsPooling2dSupported, |
| 1790 | data.m_Backends, |
| 1791 | isSupported, |
| 1792 | inputInfo, |
| 1793 | outputInfo, |
| 1794 | desc); |
| 1795 | |
| 1796 | }; |
| 1797 | |
| 1798 | if(IsDynamicTensor(outputInfo)) |
| 1799 | { |
| 1800 | isSupported = AreDynamicTensorsSupported(); |
| 1801 | } |
| 1802 | else |
| 1803 | { |
| 1804 | validateFunc(outputInfo, isSupported); |
| 1805 | } |
| 1806 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1807 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1808 | { |
Éanna Ó Catháin | 3d1059c | 2018-10-11 15:53:04 +0100 | [diff] [blame] | 1809 | return false; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1810 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1811 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1812 | armnn::IConnectableLayer* pooling2dLayer = data.m_Network->AddPooling2dLayer(desc); |
| 1813 | if (!pooling2dLayer) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1814 | { |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1815 | return Fail("%s: AddPooling2dLayer failed", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1816 | } |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1817 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1818 | input.Connect(pooling2dLayer->GetInputSlot(0)); |
| 1819 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1820 | if (!isSupported) |
| 1821 | { |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1825 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *pooling2dLayer, model, |
| 1826 | data, nullptr, validateFunc, activation); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1829 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1830 | typename HalOperation = typename HalPolicy::Operation, |
| 1831 | typename HalModel = typename HalPolicy::Model> |
| 1832 | bool ConvertAdd(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1833 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1834 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1835 | |
| 1836 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 1837 | LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 1838 | |
| 1839 | if (!input0.IsValid() || !input1.IsValid()) |
| 1840 | { |
| 1841 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1842 | } |
| 1843 | |
| 1844 | // The FuseActivation parameter is always the input index 2 |
| 1845 | // and it should be optional |
| 1846 | ActivationFn activationFunction; |
| 1847 | if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data)) |
| 1848 | { |
| 1849 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1850 | } |
| 1851 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1852 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1853 | if (!outputOperand) |
| 1854 | { |
| 1855 | return false; |
| 1856 | } |
| 1857 | |
| 1858 | const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo(); |
| 1859 | const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo(); |
| 1860 | |
| 1861 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1862 | |
| 1863 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 1864 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 1865 | { |
| 1866 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1867 | IsAdditionSupported, |
| 1868 | data.m_Backends, |
| 1869 | isSupported, |
| 1870 | inputInfo0, |
| 1871 | inputInfo1, |
| 1872 | outputInfo); |
| 1873 | }; |
| 1874 | |
| 1875 | if(!IsDynamicTensor(outputInfo)) |
| 1876 | { |
| 1877 | validateFunc(outputInfo, isSupported); |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | isSupported = AreDynamicTensorsSupported(); |
| 1882 | } |
| 1883 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1884 | if (!isSupported) |
| 1885 | { |
| 1886 | return false; |
| 1887 | } |
| 1888 | |
| 1889 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddAdditionLayer(); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1890 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1891 | bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data); |
| 1892 | if (!isReshapeSupported) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1893 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1894 | return false; |
| 1895 | } |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 1896 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 1897 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 1898 | data, nullptr, validateFunc, activationFunction); |
| 1899 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1903 | typename HalOperation = typename HalPolicy::Operation, |
| 1904 | typename HalModel = typename HalPolicy::Model> |
| 1905 | bool ConvertArgMinMax(const HalOperation& operation, |
| 1906 | const HalModel& model, |
Francis Murtagh | 19fa0cc | 2019-11-19 12:06:47 +0000 | [diff] [blame] | 1907 | ConversionData& data, |
| 1908 | armnn::ArgMinMaxFunction argMinMaxFunction) |
| 1909 | { |
| 1910 | ALOGV("argMinMaxFunction = %s", GetArgMinMaxFunctionAsCString(argMinMaxFunction)); |
| 1911 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1912 | using HalOperand = typename HalPolicy::Operand; |
Francis Murtagh | 19fa0cc | 2019-11-19 12:06:47 +0000 | [diff] [blame] | 1913 | using HalOperandType = typename HalPolicy::OperandType; |
| 1914 | |
| 1915 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 1916 | |
| 1917 | if (!input0.IsValid()) |
| 1918 | { |
| 1919 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1920 | } |
| 1921 | |
| 1922 | int32_t axis; |
| 1923 | if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, axis, model, data)) |
| 1924 | { |
| 1925 | return Fail("%s: Operation has invalid inputs. Failed to read axis.", __func__); |
| 1926 | } |
| 1927 | |
| 1928 | const armnn::TensorInfo& inputInfo = input0.GetTensorInfo(); |
| 1929 | int rank = static_cast<int>(inputInfo.GetNumDimensions()); |
| 1930 | |
| 1931 | if (((axis < -rank) && (axis < 0)) || ((axis >= rank) && (axis > 0))) |
| 1932 | { |
| 1933 | // Square bracket denotes inclusive n while parenthesis denotes exclusive n |
| 1934 | // E.g. Rank 4 tensor can have axis in range [-4, 3) |
| 1935 | // -1 == 3, -2 == 2, -3 == 1, -4 == 0 |
| 1936 | return Fail("%s: Axis must be in range [-n, n)", __func__); |
| 1937 | } |
| 1938 | |
| 1939 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 1940 | if (!output) |
| 1941 | { |
| 1942 | return Fail("%s: Could not read output 0", __func__); |
| 1943 | } |
| 1944 | |
| 1945 | const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo(); |
| 1946 | |
| 1947 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Francis Murtagh | 19fa0cc | 2019-11-19 12:06:47 +0000 | [diff] [blame] | 1948 | |
| 1949 | armnn::ArgMinMaxDescriptor descriptor; |
| 1950 | descriptor.m_Function = argMinMaxFunction; |
| 1951 | descriptor.m_Axis = axis; |
| 1952 | |
| 1953 | bool isSupported = false; |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1954 | |
| 1955 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 1956 | { |
| 1957 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1958 | IsArgMinMaxSupported, |
| 1959 | data.m_Backends, |
| 1960 | isSupported, |
| 1961 | inputInfo0, |
| 1962 | outputInfo, |
| 1963 | descriptor); |
| 1964 | }; |
| 1965 | |
| 1966 | if(IsDynamicTensor(outputInfo)) |
| 1967 | { |
| 1968 | isSupported = AreDynamicTensorsSupported(); |
| 1969 | } |
| 1970 | else |
| 1971 | { |
| 1972 | validateFunc(outputInfo, isSupported); |
| 1973 | } |
| 1974 | |
Francis Murtagh | 19fa0cc | 2019-11-19 12:06:47 +0000 | [diff] [blame] | 1975 | if (!isSupported) |
| 1976 | { |
| 1977 | return false; |
| 1978 | } |
| 1979 | |
| 1980 | armnn::IConnectableLayer* layer = data.m_Network->AddArgMinMaxLayer(descriptor); |
| 1981 | assert(layer != nullptr); |
| 1982 | |
| 1983 | input0.Connect(layer->GetInputSlot(0)); |
| 1984 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 1985 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Francis Murtagh | 19fa0cc | 2019-11-19 12:06:47 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 1989 | typename HalOperation = typename HalPolicy::Operation, |
| 1990 | typename HalModel = typename HalPolicy::Model> |
| 1991 | bool ConvertConcatenation(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 1992 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 1993 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 1994 | using HalOperandType = typename HalPolicy::OperandType; |
| 1995 | |
| 1996 | // The first N (0..N-1) inputs are tensors. The Nth input is the concatenation axis. |
| 1997 | if (operation.inputs.size() <= 1) |
| 1998 | { |
| 1999 | return Fail("%s: Operation has insufficient arguments", __func__); |
| 2000 | } |
| 2001 | |
| 2002 | // Get inputs and outputs |
| 2003 | const std::size_t numInputTensors = operation.inputs.size() - 1; |
| 2004 | |
| 2005 | int32_t concatDim; |
| 2006 | if (!GetInputScalar<HalPolicy>(operation, numInputTensors, HalOperandType::INT32, concatDim, model, data)) |
| 2007 | { |
| 2008 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2009 | } |
| 2010 | |
| 2011 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 2012 | if (!outputOperand) |
| 2013 | { |
| 2014 | return Fail("%s: Operation has no outputs", __func__); |
| 2015 | } |
| 2016 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2017 | armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 2018 | armnn::TensorShape outputShape = outputInfo.GetShape(); |
| 2019 | const bool isDynamicTensor = IsDynamicTensor(outputInfo); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2020 | // |
| 2021 | // handle negative concat dims along the lines of tensorflow as described here: |
| 2022 | // https://www.tensorflow.org/api_docs/python/tf/concat |
| 2023 | // "negative axis refers to axis + rank(values)-th dimension" |
| 2024 | // |
| 2025 | if (concatDim < 0) |
| 2026 | { |
| 2027 | concatDim += outputShape.GetNumDimensions(); |
| 2028 | } |
| 2029 | |
| 2030 | if (concatDim >= static_cast<int32_t>(outputShape.GetNumDimensions()) || concatDim < 0) |
| 2031 | { |
| 2032 | return Fail("%s: Operation has invalid concat axis: %d", __func__, concatDim); |
| 2033 | } |
| 2034 | |
| 2035 | std::vector<LayerInputHandle> inputHandles; |
| 2036 | std::vector<armnn::TensorShape> inputShapes; |
| 2037 | |
| 2038 | inputHandles.reserve(numInputTensors); |
| 2039 | inputShapes.reserve(numInputTensors); |
| 2040 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2041 | bool inputsHaveBeenReshaped = false; |
| 2042 | unsigned int tensorDimensionsAdded = 0; |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2043 | for (uint32_t i = 0; i < numInputTensors; ++i) |
| 2044 | { |
| 2045 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, i, model); |
| 2046 | if (!operand) |
| 2047 | { |
| 2048 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2049 | } |
| 2050 | |
Teresa Charlin | 3b95960 | 2019-10-31 17:05:47 +0000 | [diff] [blame] | 2051 | LayerInputHandle operandInputHandle = ConvertToLayerInputHandle<HalPolicy>(operation, i, model, data); |
| 2052 | if (!operandInputHandle.IsValid()) |
| 2053 | { |
| 2054 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2055 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2056 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2057 | armnn::TensorShape operandShape = GetTensorShapeForOperand(*operand); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2058 | if (operandShape.GetNumDimensions() == 0) |
| 2059 | { |
| 2060 | return Fail("%s: Operands with rank 0 are not supported", __func__); |
| 2061 | } |
| 2062 | |
| 2063 | if (RequiresReshape(operandShape)) |
| 2064 | { |
| 2065 | inputsHaveBeenReshaped = true; |
| 2066 | |
| 2067 | armnn::TensorInfo reshapeInfo = operandInputHandle.GetTensorInfo(); |
| 2068 | |
| 2069 | // Expand the tensor to three dimensions |
| 2070 | if (operandShape.GetNumDimensions() == 2) |
| 2071 | { |
| 2072 | reshapeInfo.SetShape(armnn::TensorShape({1, operandShape[0], operandShape[1]})); |
| 2073 | tensorDimensionsAdded = 1; |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | reshapeInfo.SetShape(armnn::TensorShape({1, 1, operandShape[0]})); |
| 2078 | tensorDimensionsAdded = 2; |
| 2079 | } |
| 2080 | |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2081 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 2082 | reshapeDescriptor.m_TargetShape = reshapeInfo.GetShape(); |
| 2083 | |
| 2084 | bool isSupported = false; |
| 2085 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2086 | IsReshapeSupported, |
| 2087 | data.m_Backends, |
| 2088 | isSupported, |
| 2089 | operandInputHandle.GetTensorInfo(), |
| 2090 | reshapeInfo, |
| 2091 | reshapeDescriptor); |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2092 | |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2093 | if (!isSupported) |
| 2094 | { |
| 2095 | return false; |
| 2096 | } |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2097 | armnn::IConnectableLayer& newReshape = AddReshapeLayer(*data.m_Network, operandInputHandle, reshapeInfo); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2098 | |
| 2099 | // Point to the reshape operation rather then the input operation |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2100 | operandShape = reshapeInfo.GetShape(); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2101 | operandInputHandle = LayerInputHandle(true, &newReshape.GetOutputSlot(0), reshapeInfo); |
| 2102 | } |
| 2103 | |
| 2104 | inputShapes.emplace_back(operandShape); |
| 2105 | inputHandles.emplace_back(operandInputHandle); |
| 2106 | |
| 2107 | if (!inputHandles.back().IsValid()) |
| 2108 | { |
| 2109 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2110 | } |
| 2111 | } |
| 2112 | |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 2113 | ARMNN_ASSERT(inputShapes.size() == inputHandles.size()); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2114 | |
| 2115 | if (inputsHaveBeenReshaped) |
| 2116 | { |
| 2117 | // Adjust the concatenation dimension by the amount of dimensions added (if any) |
| 2118 | concatDim += tensorDimensionsAdded; |
| 2119 | |
| 2120 | // Add extra dimensions to the output shape to reflect the addition of the reshape layers |
| 2121 | if (tensorDimensionsAdded == 1) |
| 2122 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2123 | if (IsDynamicTensor(outputInfo)) |
| 2124 | { |
| 2125 | outputShape = armnn::TensorShape({1, 0, 0}, {true, false, false}); |
| 2126 | } |
| 2127 | else |
| 2128 | { |
| 2129 | outputShape = armnn::TensorShape({1, outputShape[0], outputShape[1]}); |
| 2130 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2131 | } |
| 2132 | else if (tensorDimensionsAdded == 2) |
| 2133 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2134 | if (IsDynamicTensor(outputInfo)) |
| 2135 | { |
| 2136 | outputShape = armnn::TensorShape({1, 1, 0}, {true, true, false}); |
| 2137 | } |
| 2138 | else |
| 2139 | { |
| 2140 | outputShape = armnn::TensorShape({1, 1, outputShape[0]}); |
| 2141 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | // Check if permutations is required and get the pair of permutations required for the concatenation. |
| 2146 | // Permutation is required when the concat dimension is 2 for a 4D tensor or 1 for a 3D tensor. |
| 2147 | std::pair<armnn::PermutationVector, armnn::PermutationVector> permutationPair = |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2148 | std::make_pair(IdentityPermutation4D, IdentityPermutation4D); |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2149 | bool needPermute = CreateConcatPermutationParameters(inputShapes[0].GetNumDimensions(), |
| 2150 | concatDim, |
| 2151 | permutationPair); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2152 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2153 | // Only relevant to static tensors as dynamic output tensors will be transposed as a result of inferring from input |
| 2154 | if (!isDynamicTensor) |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2155 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2156 | if (needPermute) |
| 2157 | { |
| 2158 | outputShape = armnnUtils::TransposeTensorShape(outputShape, permutationPair.first); |
| 2159 | } |
| 2160 | |
| 2161 | outputInfo.SetShape(outputShape); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2162 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2163 | // this is no-op for identity swizzles, otherwise it replaces both |
| 2164 | // the handles and shapes with the swizzled layer output handles and shapes |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 2165 | if (!TransposeInputTensors(data, inputHandles, inputShapes, permutationPair.first)) |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2166 | { |
| 2167 | return false; |
| 2168 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2169 | |
| 2170 | // Create an armnn concat layer descriptor - this will also perform validation on the input shapes |
| 2171 | armnn::OriginsDescriptor concatDescriptor; |
| 2172 | |
| 2173 | try |
| 2174 | { |
| 2175 | // The concat descriptor is always created across the only supported concat dimension |
| 2176 | // which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor. |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2177 | concatDescriptor = armnn::CreateDescriptorForConcatenation(inputShapes.begin(), |
| 2178 | inputShapes.end(), |
| 2179 | concatDim); |
| 2180 | } catch (std::exception& error) |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2181 | { |
| 2182 | return Fail("%s: Error preparing concat descriptor. %s", __func__, error.what()); |
| 2183 | } |
| 2184 | |
| 2185 | // Validate the output shape is correct given the input shapes based on the |
| 2186 | // only valid concat dimension which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor. |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2187 | if (!isDynamicTensor) |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2188 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2189 | if (!ValidateConcatOutputShape(inputShapes, outputShape, concatDim)) |
| 2190 | { |
| 2191 | return Fail("%s: Error validating the output shape for concat", __func__); |
| 2192 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | std::vector<const armnn::TensorInfo*> inputTensorInfos; |
| 2196 | std::transform(inputHandles.begin(), inputHandles.end(), std::back_inserter(inputTensorInfos), |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2197 | [](const LayerInputHandle& h)->const armnn::TensorInfo*{ return &h.GetTensorInfo(); }); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2198 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2199 | bool isSupported = false; |
| 2200 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported){ |
| 2201 | FORWARD_LAYER_SUPPORT_FUNC(__func__, IsConcatSupported, data.m_Backends, isSupported, inputTensorInfos, |
| 2202 | outputInfo, concatDescriptor); |
| 2203 | }; |
| 2204 | |
| 2205 | if (!isDynamicTensor) |
| 2206 | { |
| 2207 | validateFunc(outputInfo, isSupported); |
| 2208 | } |
| 2209 | else |
| 2210 | { |
| 2211 | isSupported = AreDynamicTensorsSupported(); |
| 2212 | } |
| 2213 | |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2214 | if (!isSupported) |
| 2215 | { |
| 2216 | return false; |
| 2217 | } |
| 2218 | |
| 2219 | armnn::IConnectableLayer* layer = data.m_Network->AddConcatLayer(concatDescriptor); |
| 2220 | assert(layer != nullptr); |
| 2221 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2222 | // Connect inputs to the layer |
| 2223 | const int numInputSlots = layer->GetNumInputSlots(); |
| 2224 | assert(static_cast<std::size_t>(numInputSlots) == inputHandles.size()); |
| 2225 | for (int i = 0; i < numInputSlots; ++i) |
| 2226 | { |
| 2227 | // connect the input directly to the merge (concat) layer |
| 2228 | inputHandles[static_cast<unsigned int>(i)].Connect(layer->GetInputSlot(i)); |
| 2229 | } |
| 2230 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2231 | // Transpose the output shape |
| 2232 | auto transposeOutputShape = [&](){ |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 2233 | armnn::TransposeDescriptor transposeDesc; |
| 2234 | transposeDesc.m_DimMappings = permutationPair.second; |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 2235 | armnn::TensorInfo inputTransposeInfo = layer->GetOutputSlot(0).GetTensorInfo(); |
| 2236 | armnn::TensorInfo outputTransposeInfo = armnnUtils::TransposeTensorShape(inputTransposeInfo, |
| 2237 | permutationPair.second); |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2238 | isSupported = false; |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2239 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 2240 | IsTransposeSupported, |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2241 | data.m_Backends, |
| 2242 | isSupported, |
Teresa Charlin | 185f588 | 2020-04-06 21:59:18 +0100 | [diff] [blame] | 2243 | inputTransposeInfo, |
| 2244 | outputTransposeInfo, |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 2245 | transposeDesc); |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2246 | if (!isSupported) |
| 2247 | { |
| 2248 | return false; |
| 2249 | } |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2250 | // Add permutation layer and connect the output to it, the permutation becomes the output layer |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2251 | armnn::IConnectableLayer& deswizzleLayer = AddTransposeLayer(*data.m_Network, layer->GetOutputSlot(0), |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 2252 | permutationPair.second); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2253 | layer = &deswizzleLayer; |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2254 | |
| 2255 | return true; |
| 2256 | }; |
| 2257 | |
| 2258 | if (needPermute && !isDynamicTensor) |
| 2259 | { |
| 2260 | transposeOutputShape(); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | if (inputsHaveBeenReshaped) |
| 2264 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2265 | if (isDynamicTensor) |
| 2266 | { |
| 2267 | // Infer the output shapes of concat if outputs are type 1 dynamic |
David Monahan | 7f492ac | 2020-10-16 10:36:29 +0100 | [diff] [blame] | 2268 | ARMNN_ASSERT(layer->GetOutputSlot(0).IsTensorInfoSet()); |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2269 | if (!ValidateConcatOutputShape(inputShapes, |
| 2270 | layer->GetOutputSlot(0).GetTensorInfo().GetShape(), |
| 2271 | concatDim)) |
| 2272 | { |
| 2273 | return Fail("%s: Error validating the output shape for concat", __func__); |
| 2274 | } |
| 2275 | transposeOutputShape(); |
| 2276 | } |
| 2277 | |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2278 | armnn::TensorInfo afterConcatInfo = layer->GetOutputSlot(0).GetTensorInfo(); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2279 | // Undo the reshape knowing the amount of dimensions added |
| 2280 | if (tensorDimensionsAdded == 1) |
| 2281 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2282 | afterConcatInfo.SetShape( |
| 2283 | armnn::TensorShape({afterConcatInfo.GetShape()[1], afterConcatInfo.GetShape()[2]})); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2284 | } |
| 2285 | else if (tensorDimensionsAdded == 2) |
| 2286 | { |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2287 | afterConcatInfo.SetShape(armnn::TensorShape({afterConcatInfo.GetShape()[2]})); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2290 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 2291 | reshapeDescriptor.m_TargetShape = afterConcatInfo.GetShape(); |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2292 | armnn::TensorInfo concatInfo = layer->GetOutputSlot(0).GetTensorInfo(); |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2293 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2294 | isSupported = false; |
| 2295 | auto validateReshapeFunc = [&](const armnn::TensorInfo& afterConcatInfo, bool& isSupported){ |
| 2296 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2297 | IsReshapeSupported, |
| 2298 | data.m_Backends, |
| 2299 | isSupported, |
| 2300 | concatInfo, |
| 2301 | afterConcatInfo, |
| 2302 | reshapeDescriptor); |
| 2303 | }; |
| 2304 | |
| 2305 | if (!IsDynamicTensor(afterConcatInfo)) |
| 2306 | { |
| 2307 | validateReshapeFunc(afterConcatInfo, isSupported); |
| 2308 | } |
| 2309 | else |
| 2310 | { |
| 2311 | isSupported = AreDynamicTensorsSupported(); |
| 2312 | } |
| 2313 | |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 2314 | if (!isSupported) |
| 2315 | { |
| 2316 | return false; |
| 2317 | } |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2318 | layer = &AddReshapeLayer(*data.m_Network, layer->GetOutputSlot(0), afterConcatInfo); |
| 2319 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, |
| 2320 | 0, |
| 2321 | *layer, |
| 2322 | model, |
| 2323 | data, |
| 2324 | nullptr, |
| 2325 | validateReshapeFunc); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
Keith Davis | 6e4081f | 2020-09-03 13:17:21 +0100 | [diff] [blame] | 2328 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | b880520 | 2019-07-31 17:25:43 +0100 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2332 | typename HalOperation = typename HalPolicy::Operation, |
| 2333 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2334 | bool ConvertConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 2335 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2336 | using HalOperand = typename HalPolicy::Operand; |
| 2337 | using HalOperandType = typename HalPolicy::OperandType; |
| 2338 | |
| 2339 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2340 | if (!input.IsValid()) |
| 2341 | { |
| 2342 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2343 | } |
| 2344 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2345 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2346 | if (!output) |
| 2347 | { |
| 2348 | return Fail("%s: Could not read output 0", __func__); |
| 2349 | } |
| 2350 | |
| 2351 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 2352 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2353 | |
| 2354 | // ArmNN does not currently support non-fixed weights or bias |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2355 | const ConstTensorPin weightsPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 1, model, data); |
| 2356 | const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2357 | |
| 2358 | if (!weightsPin.IsValid() || !biasPin.IsValid()) |
| 2359 | { |
| 2360 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2361 | } |
| 2362 | |
| 2363 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2364 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2365 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 2366 | |
| 2367 | armnn::Convolution2dDescriptor desc; |
| 2368 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 2369 | ActivationFn activation; |
| 2370 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2371 | if (operation.inputs.size() == 10) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2372 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2373 | if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 2374 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 2375 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 2376 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 2377 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 2378 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2379 | !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2380 | { |
| 2381 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2382 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2383 | } |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2384 | else if (operation.inputs.size() == 7) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2385 | { |
| 2386 | android::nn::PaddingScheme paddingScheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2387 | if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 2388 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 2389 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2390 | !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2391 | { |
| 2392 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2393 | } |
| 2394 | |
| 2395 | const uint32_t kernelX = weights.GetShape()[2]; |
| 2396 | const uint32_t kernelY = weights.GetShape()[1]; |
| 2397 | const uint32_t inputX = inputInfo.GetShape()[2]; |
| 2398 | const uint32_t inputY = inputInfo.GetShape()[1]; |
| 2399 | |
| 2400 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 2401 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2402 | } |
| 2403 | else |
| 2404 | { |
| 2405 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 2406 | } |
| 2407 | |
| 2408 | desc.m_BiasEnabled = true; |
| 2409 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 2410 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 2411 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2412 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2413 | { |
| 2414 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2415 | IsConvolution2dSupported, |
| 2416 | data.m_Backends, |
| 2417 | isSupported, |
| 2418 | inputInfo, |
| 2419 | outputInfo, |
| 2420 | desc, |
| 2421 | weights.GetInfo(), |
| 2422 | biases); |
| 2423 | }; |
| 2424 | |
| 2425 | if(!IsDynamicTensor(outputInfo)) |
| 2426 | { |
| 2427 | validateFunc(outputInfo, isSupported); |
| 2428 | } |
| 2429 | else |
| 2430 | { |
| 2431 | isSupported = AreDynamicTensorsSupported(); |
| 2432 | } |
| 2433 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 2434 | if (!isSupported) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2435 | { |
| 2436 | return false; |
| 2437 | } |
| 2438 | |
| 2439 | armnn::IConnectableLayer* startLayer = |
| 2440 | data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 2441 | |
| 2442 | if (!startLayer) |
| 2443 | { |
| 2444 | return Fail("%s: AddConvolution2dLayer failed", __func__); |
| 2445 | } |
| 2446 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2447 | input.Connect(startLayer->GetInputSlot(0)); |
| 2448 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 2449 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 2450 | data, nullptr, validateFunc, activation); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2453 | template<typename HalPolicy, |
| 2454 | typename HalOperation = typename HalPolicy::Operation, |
| 2455 | typename HalModel = typename HalPolicy::Model> |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 2456 | bool ConvertDepthToSpace(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 2457 | { |
| 2458 | using HalOperand = typename HalPolicy::Operand; |
| 2459 | using HalOperandType = typename HalPolicy::OperandType; |
| 2460 | |
| 2461 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 2462 | if (!input.IsValid() ) |
| 2463 | { |
| 2464 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2465 | } |
| 2466 | |
| 2467 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 2468 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 2469 | if (rank != 4) |
| 2470 | { |
| 2471 | return Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 2472 | } |
| 2473 | |
| 2474 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 2475 | if (!output) |
| 2476 | { |
| 2477 | return Fail("%s: Could not read output 0", __func__); |
| 2478 | } |
| 2479 | |
| 2480 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 2481 | |
| 2482 | armnn::DepthToSpaceDescriptor descriptor; |
| 2483 | |
| 2484 | GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, descriptor.m_BlockSize, model, data); |
| 2485 | if (descriptor.m_BlockSize <= 1) |
| 2486 | { |
| 2487 | return Fail("%s: Block size must be at least 1 in all dimensions"); |
| 2488 | } |
| 2489 | |
| 2490 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 2491 | if (Is12OrLaterOperand(*output)) |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 2492 | { |
| 2493 | descriptor.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 2, model, data); |
| 2494 | } |
| 2495 | |
| 2496 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2497 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2498 | { |
| 2499 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2500 | IsDepthToSpaceSupported, |
| 2501 | data.m_Backends, |
| 2502 | isSupported, |
| 2503 | inputInfo, |
| 2504 | outputInfo, |
| 2505 | descriptor); |
| 2506 | }; |
| 2507 | |
| 2508 | if(!IsDynamicTensor(outputInfo)) |
| 2509 | { |
| 2510 | validateFunc(outputInfo, isSupported); |
| 2511 | } |
| 2512 | else |
| 2513 | { |
| 2514 | isSupported = AreDynamicTensorsSupported(); |
| 2515 | } |
| 2516 | |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 2517 | if (!isSupported) |
| 2518 | { |
| 2519 | return false; |
| 2520 | } |
| 2521 | |
| 2522 | armnn::IConnectableLayer* const layer = data.m_Network->AddDepthToSpaceLayer(descriptor); |
| 2523 | assert(layer != nullptr); |
| 2524 | input.Connect(layer->GetInputSlot(0)); |
| 2525 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2526 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Aron Virginas-Tar | 8edb16d | 2019-10-01 13:34:59 +0100 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | template<typename HalPolicy, |
| 2530 | typename HalOperation = typename HalPolicy::Operation, |
| 2531 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2532 | bool ConvertDepthwiseConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 2533 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2534 | using HalOperand = typename HalPolicy::Operand; |
| 2535 | using HalOperandType = typename HalPolicy::OperandType; |
| 2536 | |
| 2537 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2538 | |
| 2539 | if (!input.IsValid()) |
| 2540 | { |
| 2541 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2542 | } |
| 2543 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2544 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2545 | |
| 2546 | if (!output) |
| 2547 | { |
| 2548 | return Fail("%s: Could not read output 0", __func__); |
| 2549 | } |
| 2550 | |
| 2551 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 2552 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2553 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2554 | // ArmNN does not currently support non-fixed weights or bias |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2555 | // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ] |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2556 | const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2557 | |
| 2558 | if (weightsOperand == nullptr) |
| 2559 | { |
| 2560 | return Fail("%s: Operand is invalid", __func__); |
| 2561 | } |
Colm Donelan | ccfeb5e | 2021-03-30 15:30:13 +0100 | [diff] [blame] | 2562 | // Basic sanity check on the weights shape. |
| 2563 | // ANEURALNETWORKS_DEPTHWISE_CONV_2D specifies a 4-D tensor, of shape |
| 2564 | // [1, filter_height, filter_width, depth_out] |
| 2565 | if (weightsOperand->dimensions[0] != 1) |
| 2566 | { |
| 2567 | return Fail("%s: Filter operand dimension 0 is invalid, should be 1", __func__); |
| 2568 | } |
| 2569 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2570 | armnn::DepthwiseConvolution2dDescriptor desc; |
| 2571 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 2572 | |
Jan Eilers | a20d2b8 | 2021-04-27 09:21:08 +0100 | [diff] [blame] | 2573 | // The layout for weights in depthwise is [ 1, H, W, O] and it's the same in ArmNN. No need to permute anything. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2574 | const ConstTensorPin weightsPin = |
| 2575 | ConvertOperationInputToConstTensorPin<HalPolicy>(operation, |
| 2576 | 1, |
| 2577 | model, |
Jan Eilers | a20d2b8 | 2021-04-27 09:21:08 +0100 | [diff] [blame] | 2578 | data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2579 | |
| 2580 | // Bias is a 1D tensor |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2581 | const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2582 | |
| 2583 | if (!weightsPin.IsValid() || !biasPin.IsValid()) |
| 2584 | { |
| 2585 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2586 | } |
| 2587 | |
| 2588 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 2589 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 2590 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 2591 | |
| 2592 | ActivationFn activation; |
| 2593 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2594 | if (operation.inputs.size() == 11) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2595 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2596 | if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 2597 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 2598 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 2599 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 2600 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 2601 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2602 | !GetInputActivationFunction<HalPolicy>(operation, 10, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2603 | { |
| 2604 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2605 | } |
| 2606 | } |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2607 | else if (operation.inputs.size() == 8) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2608 | { |
| 2609 | android::nn::PaddingScheme paddingScheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 2610 | if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 2611 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 2612 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2613 | !GetInputActivationFunction<HalPolicy>(operation, 7, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2614 | { |
| 2615 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2616 | } |
| 2617 | |
Jan Eilers | a20d2b8 | 2021-04-27 09:21:08 +0100 | [diff] [blame] | 2618 | const uint32_t kernelX = weights.GetShape()[2]; |
| 2619 | const uint32_t kernelY = weights.GetShape()[1]; |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 2620 | const uint32_t inputX = inputInfo.GetShape()[2]; |
| 2621 | const uint32_t inputY = inputInfo.GetShape()[1]; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2622 | |
| 2623 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 2624 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 2629 | } |
| 2630 | |
| 2631 | desc.m_BiasEnabled = true; |
| 2632 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 2633 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 2634 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2635 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2636 | { |
| 2637 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2638 | IsDepthwiseConvolutionSupported, |
| 2639 | data.m_Backends, |
| 2640 | isSupported, |
| 2641 | inputInfo, |
| 2642 | outputInfo, |
| 2643 | desc, |
| 2644 | weights.GetInfo(), |
| 2645 | biases); |
| 2646 | }; |
| 2647 | |
| 2648 | if(!IsDynamicTensor(outputInfo)) |
| 2649 | { |
| 2650 | validateFunc(outputInfo, isSupported); |
| 2651 | } |
| 2652 | else |
| 2653 | { |
| 2654 | isSupported = AreDynamicTensorsSupported(); |
| 2655 | } |
| 2656 | |
| 2657 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 2658 | if (!isSupported) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2659 | { |
| 2660 | return false; |
| 2661 | } |
| 2662 | |
| 2663 | armnn::IConnectableLayer* startLayer = |
| 2664 | data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 2665 | if (!startLayer) |
| 2666 | { |
| 2667 | return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__); |
| 2668 | } |
| 2669 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 2670 | input.Connect(startLayer->GetInputSlot(0)); |
| 2671 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 2672 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 2673 | data, nullptr, validateFunc, activation); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 2676 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2677 | typename HalOperation = typename HalPolicy::Operation, |
| 2678 | typename HalModel = typename HalPolicy::Model> |
| 2679 | bool ConvertDequantize(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 2680 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2681 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2682 | |
| 2683 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 2684 | if (!input.IsValid()) |
| 2685 | { |
| 2686 | return Fail("%s: Operation has invalid input", __func__); |
| 2687 | } |
| 2688 | |
Sadik Armagan | 98c0f66 | 2019-11-21 15:54:36 +0000 | [diff] [blame] | 2689 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 2690 | const armnn::Optional<unsigned int>& quantizationDim = inputInfo.GetQuantizationDim(); |
| 2691 | if (quantizationDim.has_value() && quantizationDim.value() != 0) |
| 2692 | { |
| 2693 | return Fail("%s: Operation has quantization dimension different than 0", __func__); |
| 2694 | } |
| 2695 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2696 | const HalOperand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2697 | if (!outputOperand) |
| 2698 | { |
| 2699 | return Fail("%s: Operation has invalid outputs", __func__); |
| 2700 | } |
| 2701 | |
| 2702 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2703 | |
| 2704 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2705 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2706 | { |
| 2707 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2708 | IsDequantizeSupported, |
| 2709 | data.m_Backends, |
| 2710 | isSupported, |
| 2711 | inputInfo, |
| 2712 | outputInfo); |
| 2713 | }; |
| 2714 | |
| 2715 | if(IsDynamicTensor(outputInfo)) |
| 2716 | { |
| 2717 | isSupported = AreDynamicTensorsSupported(); |
| 2718 | } |
| 2719 | else |
| 2720 | { |
| 2721 | validateFunc(outputInfo, isSupported); |
| 2722 | } |
| 2723 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2724 | if (!isSupported) |
| 2725 | { |
| 2726 | return false; |
| 2727 | } |
| 2728 | |
| 2729 | armnn::IConnectableLayer* const layer = data.m_Network->AddDequantizeLayer(); |
| 2730 | assert(layer != nullptr); |
| 2731 | input.Connect(layer->GetInputSlot(0)); |
| 2732 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2733 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2737 | typename HalOperation = typename HalPolicy::Operation, |
| 2738 | typename HalModel = typename HalPolicy::Model> |
| 2739 | bool ConvertDiv(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2740 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2741 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2742 | |
| 2743 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 2744 | LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 2745 | |
| 2746 | if (!input0.IsValid() || !input1.IsValid()) |
| 2747 | { |
| 2748 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2749 | } |
| 2750 | |
| 2751 | // The FuseActivation parameter is always the input index 2 |
| 2752 | // and it should be optional |
| 2753 | ActivationFn activationFunction; |
| 2754 | if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data)) |
| 2755 | { |
| 2756 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2757 | } |
| 2758 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2759 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2760 | if (!output) |
| 2761 | { |
| 2762 | return Fail("%s: Could not read output 0", __func__); |
| 2763 | } |
| 2764 | |
| 2765 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2766 | |
| 2767 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2768 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2769 | { |
| 2770 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2771 | IsDivisionSupported, |
| 2772 | data.m_Backends, |
| 2773 | isSupported, |
| 2774 | input0.GetTensorInfo(), |
| 2775 | input1.GetTensorInfo(), |
| 2776 | outputInfo); |
| 2777 | }; |
| 2778 | |
| 2779 | if(!IsDynamicTensor(outputInfo)) |
| 2780 | { |
| 2781 | validateFunc(outputInfo, isSupported); |
| 2782 | } |
| 2783 | else |
| 2784 | { |
| 2785 | isSupported = AreDynamicTensorsSupported(); |
| 2786 | } |
| 2787 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2788 | if (!isSupported) |
| 2789 | { |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer(); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2794 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 2795 | bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data); |
| 2796 | if (!isReshapeSupported) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2797 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 2798 | return false; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2799 | } |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 2800 | |
| 2801 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 2802 | data, nullptr, validateFunc, activationFunction); |
| 2803 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2807 | typename HalOperation = typename HalPolicy::Operation, |
| 2808 | typename HalModel = typename HalPolicy::Model> |
| 2809 | bool ConvertFloor(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2810 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2811 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2812 | |
| 2813 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 2814 | if (!input.IsValid()) |
| 2815 | { |
| 2816 | return Fail("%s: Operation has invalid inputs", __func__); |
| 2817 | } |
| 2818 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2819 | const HalOperand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2820 | if (!outputOperand) |
| 2821 | { |
| 2822 | return Fail("%s: Operation has invalid outputs", __func__); |
| 2823 | } |
| 2824 | |
| 2825 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2826 | |
| 2827 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2828 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 2829 | { |
| 2830 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 2831 | IsFloorSupported, |
| 2832 | data.m_Backends, |
| 2833 | isSupported, |
| 2834 | input.GetTensorInfo(), |
| 2835 | outputInfo); |
| 2836 | }; |
| 2837 | |
| 2838 | if(!IsDynamicTensor(outputInfo)) |
| 2839 | { |
| 2840 | validateFunc(outputInfo, isSupported); |
| 2841 | } |
| 2842 | else |
| 2843 | { |
| 2844 | isSupported = AreDynamicTensorsSupported(); |
| 2845 | } |
| 2846 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2847 | if (!isSupported) |
| 2848 | { |
| 2849 | return false; |
| 2850 | } |
| 2851 | |
| 2852 | armnn::IConnectableLayer* layer = data.m_Network->AddFloorLayer(); |
| 2853 | assert(layer != nullptr); |
| 2854 | input.Connect(layer->GetInputSlot(0)); |
| 2855 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 2856 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 2857 | } |
| 2858 | |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2859 | inline bool IsQSymm8(const V1_0::Operand&) |
| 2860 | { |
| 2861 | return false; |
| 2862 | } |
| 2863 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 2864 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2865 | |
| 2866 | inline bool IsQSymm8(const V1_2::Operand& operand) |
| 2867 | { |
| 2868 | return operand.type == V1_2::OperandType::TENSOR_QUANT8_SYMM; |
| 2869 | } |
| 2870 | |
| 2871 | #endif |
| 2872 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 2873 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 2874 | |
| 2875 | inline bool IsQSymm8(const V1_3::Operand& operand) |
| 2876 | { |
| 2877 | return operand.type == V1_3::OperandType::TENSOR_QUANT8_SYMM; |
| 2878 | } |
| 2879 | |
| 2880 | #endif |
| 2881 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2882 | enum class DequantizeStatus |
| 2883 | { |
| 2884 | SUCCESS, |
| 2885 | NOT_REQUIRED, |
| 2886 | INVALID_OPERAND |
| 2887 | }; |
| 2888 | |
| 2889 | using DequantizeResult = std::tuple<std::unique_ptr<float[]>, size_t, armnn::TensorInfo, DequantizeStatus>; |
| 2890 | |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2891 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2892 | typename HalOperation = typename HalPolicy::Operation, |
| 2893 | typename HalModel = typename HalPolicy::Model> |
| 2894 | DequantizeResult DequantizeIfRequired(size_t operand_index, |
| 2895 | const HalOperation& operation, |
| 2896 | const HalModel& model, |
| 2897 | const ConversionData& data) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2898 | { |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2899 | using HalOperand = typename HalPolicy::Operand; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2900 | |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2901 | const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, operand_index, model); |
Sadik Armagan | d081194 | 2019-11-18 17:11:21 +0000 | [diff] [blame] | 2902 | if (!weightsOperand) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2903 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2904 | return { nullptr, 0, armnn::TensorInfo(), DequantizeStatus::INVALID_OPERAND }; |
Sadik Armagan | d081194 | 2019-11-18 17:11:21 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | if (IsOperandConstant<HalPolicy>(*weightsOperand)) |
| 2908 | { |
| 2909 | // Weights are already constant |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2910 | return { nullptr, 0, armnn::TensorInfo(), DequantizeStatus::NOT_REQUIRED }; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | const size_t weightsInputIndex = operation.inputs[operand_index]; |
| 2914 | |
| 2915 | // The weights are a non const tensor, this indicates they might be the output of a dequantize op. |
| 2916 | // Iterate over the nodes and find the previous operation which should be DEQUANTIZE |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 2917 | for (uint32_t operationIdx = 0; operationIdx < getMainModel(model).operations.size(); ++operationIdx) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2918 | { |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2919 | // Search for the DEQUANTIZE op which has the operand with index equal to operandIndex |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 2920 | const auto& operationIt = getMainModel(model).operations[operationIdx]; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2921 | if (operationIt.type != HalPolicy::OperationType::DEQUANTIZE) |
| 2922 | { |
| 2923 | continue; |
| 2924 | } |
| 2925 | |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2926 | size_t outOpIndex = weightsInputIndex + 1; |
| 2927 | for (size_t i = 0; outOpIndex != weightsInputIndex && i < operationIt.outputs.size(); ++i) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2928 | { |
| 2929 | outOpIndex = operationIt.outputs[i]; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | if (outOpIndex != weightsInputIndex) |
| 2933 | { |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2934 | continue; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2935 | } |
| 2936 | |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2937 | const HalOperand* operand = GetInputOperand<HalPolicy>(operationIt, 0, model); |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 2938 | ARMNN_ASSERT(operand); |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2939 | |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2940 | if (!IsQSymm8(*operand)) |
| 2941 | { |
| 2942 | // Only supporting dequantize from QSYMM8 to FLOAT |
| 2943 | break; |
| 2944 | } |
| 2945 | |
| 2946 | // Allocate a new buffer for the dequantized data and manually dequantize |
| 2947 | const void* startValue = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
| 2948 | if (!startValue) |
| 2949 | { |
| 2950 | // Failed to get the operand address |
| 2951 | break; |
| 2952 | } |
| 2953 | |
| 2954 | const uint8_t* quantizedBuffer = reinterpret_cast<const uint8_t*>(startValue); |
| 2955 | size_t dequantizedBufferLength = operand->location.length; |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2956 | const float quantizationScale = operand->scale; |
| 2957 | |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2958 | auto dequantizedBuffer = std::make_unique<float[]>(dequantizedBufferLength + 1); |
| 2959 | for (size_t i = 0; i < dequantizedBufferLength; ++i) |
| 2960 | { |
| 2961 | float* dstPtr = dequantizedBuffer.get(); |
Narumol Prangnawarat | 4d07e5e | 2020-04-06 16:46:21 +0100 | [diff] [blame] | 2962 | ARMNN_ASSERT(dstPtr); |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2963 | *dstPtr++ = quantizedBuffer[i] * quantizationScale; |
| 2964 | } |
| 2965 | |
Aron Virginas-Tar | 65a1b1d | 2019-11-15 15:59:51 +0000 | [diff] [blame] | 2966 | // Construct tensor info for dequantized ConstTensor |
| 2967 | armnn::TensorInfo tensorInfo(operand->dimensions.size(), |
| 2968 | operand->dimensions.data(), |
| 2969 | armnn::DataType::Float32); |
| 2970 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2971 | return { std::move(dequantizedBuffer), dequantizedBufferLength * sizeof(float), |
| 2972 | std::move(tensorInfo), |
| 2973 | DequantizeStatus::SUCCESS }; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2976 | return { nullptr, 0, armnn::TensorInfo() , DequantizeStatus::NOT_REQUIRED}; |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2980 | typename HalOperation = typename HalPolicy::Operation, |
| 2981 | typename HalModel = typename HalPolicy::Model> |
| 2982 | ConstTensorPin DequantizeAndMakeConstTensorPin(const HalOperation& operation, |
| 2983 | const HalModel& model, |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2984 | const ConversionData& data, |
| 2985 | size_t operandIndex, |
| 2986 | bool optional = false) |
| 2987 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2988 | DequantizeResult dequantized = DequantizeIfRequired<HalPolicy>(operandIndex,operation, model, data); |
| 2989 | |
| 2990 | DequantizeStatus status = std::get<3>(dequantized); |
| 2991 | switch (status) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 2992 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 2993 | case DequantizeStatus::INVALID_OPERAND: |
| 2994 | { |
| 2995 | // return invalid const tensor pin |
| 2996 | return ConstTensorPin(); |
| 2997 | } |
| 2998 | case DequantizeStatus::NOT_REQUIRED: |
| 2999 | { |
| 3000 | return ConvertOperationInputToConstTensorPin<HalPolicy>( |
| 3001 | operation, operandIndex, model, data, g_DontPermute, nullptr, optional); |
| 3002 | } |
| 3003 | case DequantizeStatus::SUCCESS: |
| 3004 | default: |
| 3005 | { |
| 3006 | return ConstTensorPin( |
| 3007 | std::get<2>(dequantized), std::get<0>(dequantized).get(), std::get<1>(dequantized), g_DontPermute); |
| 3008 | } |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 3009 | } |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3013 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3014 | typename HalOperation = typename HalPolicy::Operation, |
| 3015 | typename HalModel = typename HalPolicy::Model> |
| 3016 | bool ConvertFullyConnected(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3017 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3018 | using HalOperand = typename HalPolicy::Operand; |
| 3019 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3020 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3021 | if (!input.IsValid()) |
| 3022 | { |
| 3023 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3024 | } |
| 3025 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3026 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3027 | if (!output) |
| 3028 | { |
| 3029 | return Fail("%s: Could not read output 0", __func__); |
| 3030 | } |
| 3031 | |
| 3032 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3033 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 3034 | |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3035 | LayerInputHandle weightsInput = LayerInputHandle(); |
| 3036 | const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 3037 | if (!weightsOperand) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3038 | { |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3039 | return Fail("%s: Could not read weights", __func__); |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 3040 | } |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3041 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3042 | // If weights are constant a separate constant layer will be created to store data. |
| 3043 | // Otherwise handle non const weights as inputs. |
| 3044 | weightsInput = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 3045 | if (!weightsInput.IsValid()) |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 3046 | { |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3047 | return Fail("%s: Operation has invalid inputs", __func__); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3050 | LayerInputHandle biasInput = LayerInputHandle(); |
| 3051 | const HalOperand* biasOperand = GetInputOperand<HalPolicy>(operation, 2, model); |
| 3052 | if (!biasOperand) |
| 3053 | { |
| 3054 | return Fail("%s: Could not read bias", __func__); |
| 3055 | } |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3056 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3057 | // If bias are constant a separate constant layer will be created to store data. |
| 3058 | // Otherwise handle non const bias as inputs. |
| 3059 | biasInput = ConvertToLayerInputHandle<HalPolicy>(operation, 2, model, data); // 1D |
| 3060 | if (!biasInput.IsValid()) |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3061 | { |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3062 | return Fail("%s: Operation has invalid inputs", __func__); |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 3065 | armnn::TensorInfo weightsInfo = weightsInput.GetTensorInfo(); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3066 | armnn::TensorInfo reshapedInfo = inputInfo; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3067 | try |
| 3068 | { |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3069 | reshapedInfo.SetShape(FlattenFullyConnectedInput(inputInfo.GetShape(), weightsInfo.GetShape())); |
Pablo Tello | fb45e2f | 2019-10-18 16:51:57 +0100 | [diff] [blame] | 3070 | } |
| 3071 | catch (const std::exception& e) |
| 3072 | { |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3073 | return Fail("%s: %s", __func__, e.what()); |
| 3074 | } |
| 3075 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3076 | // Ensuring that the bias value is within 1% of the weights input (small float differences can exist) |
Matthew Sloyan | 56c249c | 2021-08-09 12:49:23 +0100 | [diff] [blame] | 3077 | armnn::TensorInfo biasInfo = biasInput.GetTensorInfo(); |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3078 | SanitizeBiasQuantizationScale(biasInfo, weightsInfo, reshapedInfo); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3079 | |
| 3080 | ActivationFn activationFunction; |
| 3081 | if (!GetInputActivationFunction<HalPolicy>(operation, 3, activationFunction, model, data)) |
| 3082 | { |
| 3083 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3084 | } |
| 3085 | |
| 3086 | armnn::FullyConnectedDescriptor desc; |
| 3087 | desc.m_TransposeWeightMatrix = true; |
| 3088 | desc.m_BiasEnabled = true; |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3089 | desc.m_ConstantWeights = IsOperandConstant<HalPolicy>(*weightsOperand); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3090 | |
| 3091 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3092 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3093 | { |
Finn Williams | 4918446 | 2020-10-02 13:28:34 +0100 | [diff] [blame] | 3094 | if (!VerifyFullyConnectedShapes(reshapedInfo.GetShape(), |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3095 | weightsInfo.GetShape(), |
Finn Williams | 4918446 | 2020-10-02 13:28:34 +0100 | [diff] [blame] | 3096 | outputInfo.GetShape(), |
| 3097 | desc.m_TransposeWeightMatrix)) |
| 3098 | { |
| 3099 | isSupported = false; |
| 3100 | Fail("%s: Expected outputShape does not match actual outputShape", __func__); |
| 3101 | return; |
| 3102 | } |
| 3103 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3104 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3105 | IsFullyConnectedSupported, |
| 3106 | data.m_Backends, |
| 3107 | isSupported, |
| 3108 | reshapedInfo, |
| 3109 | outputInfo, |
| 3110 | weightsInfo, |
| 3111 | biasInfo, |
| 3112 | desc); |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3113 | }; |
| 3114 | |
| 3115 | if(!IsDynamicTensor(outputInfo)) |
| 3116 | { |
| 3117 | validateFunc(outputInfo, isSupported); |
| 3118 | } |
| 3119 | else |
| 3120 | { |
| 3121 | isSupported = AreDynamicTensorsSupported(); |
| 3122 | } |
| 3123 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3124 | if (!isSupported) |
| 3125 | { |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3129 | // Add FullyConnected layer. Weights and bias will be connected as constant layers or non const inputs. |
| 3130 | armnn::IConnectableLayer* startLayer = data.m_Network->AddFullyConnectedLayer(desc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3131 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3132 | if (inputInfo.GetNumDimensions() > 2U) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3133 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3134 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 3135 | reshapeDescriptor.m_TargetShape = reshapedInfo.GetShape(); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3136 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3137 | armnn::IConnectableLayer* reshapeLayer = data.m_Network->AddReshapeLayer(reshapeDescriptor); |
| 3138 | assert(reshapeLayer != nullptr); |
| 3139 | input.Connect(reshapeLayer->GetInputSlot(0)); |
| 3140 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo); |
| 3141 | reshapeLayer->GetOutputSlot(0).Connect(startLayer->GetInputSlot(0)); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3142 | } |
| 3143 | else |
| 3144 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3145 | input.Connect(startLayer->GetInputSlot(0)); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3146 | } |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3147 | |
Matthew Sloyan | 29cc961 | 2021-07-16 10:21:12 +0100 | [diff] [blame] | 3148 | // Connect weights and bias inputs |
| 3149 | weightsInput.Connect(startLayer->GetInputSlot(1)); |
| 3150 | biasInput.Connect(startLayer->GetInputSlot(2)); |
Sadik Armagan | 2e4a24a | 2021-03-18 13:59:40 +0000 | [diff] [blame] | 3151 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3152 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 3153 | data, nullptr, validateFunc, activationFunction); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3157 | typename HalOperation = typename HalPolicy::Operation, |
| 3158 | typename HalModel = typename HalPolicy::Model> |
| 3159 | bool ConvertL2Normalization(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3160 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3161 | using HalOperand = typename HalPolicy::Operand; |
| 3162 | |
Mike Kelly | 999e209 | 2019-08-15 10:46:46 +0100 | [diff] [blame] | 3163 | if (operation.inputs.size() != 1) |
| 3164 | { |
| 3165 | return Fail("%s: Optional inputs are not supported", __func__); |
| 3166 | } |
| 3167 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3168 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3169 | if (!input.IsValid()) |
| 3170 | { |
| 3171 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3172 | } |
| 3173 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3174 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3175 | if (!output) |
| 3176 | { |
| 3177 | return Fail("%s: Could not read output 0", __func__); |
| 3178 | } |
| 3179 | |
| 3180 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3181 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 3182 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3183 | if (outputInfo.GetNumDimensions() != 4u) |
| 3184 | { |
| 3185 | return Fail("%s: Tensor Rank other than 4 is not supported", __func__); |
| 3186 | } |
| 3187 | |
| 3188 | armnn::L2NormalizationDescriptor desc; |
| 3189 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 3190 | |
| 3191 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3192 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3193 | { |
| 3194 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3195 | IsL2NormalizationSupported, |
| 3196 | data.m_Backends, |
| 3197 | isSupported, |
| 3198 | inputInfo, |
| 3199 | outputInfo, |
| 3200 | desc); |
| 3201 | }; |
| 3202 | |
| 3203 | if(!IsDynamicTensor(outputInfo)) |
| 3204 | { |
| 3205 | validateFunc(outputInfo, isSupported); |
| 3206 | } |
| 3207 | else |
| 3208 | { |
| 3209 | isSupported = AreDynamicTensorsSupported(); |
| 3210 | } |
| 3211 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3212 | if (!isSupported) |
| 3213 | { |
| 3214 | return false; |
| 3215 | } |
| 3216 | |
| 3217 | armnn::IConnectableLayer* layer = data.m_Network->AddL2NormalizationLayer(desc); |
| 3218 | assert(layer != nullptr); |
| 3219 | input.Connect(layer->GetInputSlot(0)); |
| 3220 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3221 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3225 | typename HalOperation = typename HalPolicy::Operation, |
| 3226 | typename HalModel = typename HalPolicy::Model> |
| 3227 | bool ConvertLocalResponseNormalization(const HalOperation& operation, |
| 3228 | const HalModel& model, |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3229 | ConversionData& data) |
| 3230 | { |
Mike Kelly | 999e209 | 2019-08-15 10:46:46 +0100 | [diff] [blame] | 3231 | if (operation.inputs.size() != 5) |
| 3232 | { |
| 3233 | return Fail("%s: Optional inputs are not supported", __func__); |
| 3234 | } |
| 3235 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3236 | using HalOperand = typename HalPolicy::Operand; |
| 3237 | using HalOperandType = typename HalPolicy::OperandType; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3238 | |
| 3239 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3240 | if (!input.IsValid()) |
| 3241 | { |
| 3242 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3243 | } |
| 3244 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3245 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3246 | if (!output) |
| 3247 | { |
| 3248 | return Fail("%s: Could not read output 0", __func__); |
| 3249 | } |
| 3250 | |
| 3251 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3252 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 3253 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3254 | if (outputInfo.GetNumDimensions() != 4u) |
| 3255 | { |
| 3256 | return Fail("%s: Tensor Rank other than 4 is not supported", __func__); |
| 3257 | } |
| 3258 | |
| 3259 | armnn::NormalizationDescriptor descriptor; |
| 3260 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 3261 | descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across; |
| 3262 | descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness; |
| 3263 | |
| 3264 | if (!input.IsValid() || |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3265 | !GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, descriptor.m_NormSize, model, data) || |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3266 | !GetInputFloat32<HalPolicy>(operation, 2, descriptor.m_K, model, data) || |
| 3267 | !GetInputFloat32<HalPolicy>(operation, 3, descriptor.m_Alpha, model, data) || |
| 3268 | !GetInputFloat32<HalPolicy>(operation, 4, descriptor.m_Beta, model, data)) |
| 3269 | { |
| 3270 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3271 | } |
| 3272 | |
| 3273 | // ArmNN expects normSize to be the full size of the normalization |
| 3274 | // window rather than the radius as in AndroidNN. |
| 3275 | descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize); |
| 3276 | |
| 3277 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3278 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3279 | { |
| 3280 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3281 | IsNormalizationSupported, |
| 3282 | data.m_Backends, |
| 3283 | isSupported, |
| 3284 | inputInfo, |
| 3285 | outputInfo, |
| 3286 | descriptor); |
| 3287 | }; |
| 3288 | |
| 3289 | if(!IsDynamicTensor(outputInfo)) |
| 3290 | { |
| 3291 | validateFunc(outputInfo, isSupported); |
| 3292 | } |
| 3293 | else |
| 3294 | { |
| 3295 | isSupported = AreDynamicTensorsSupported(); |
| 3296 | } |
| 3297 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3298 | if (!isSupported) |
| 3299 | { |
| 3300 | return false; |
| 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | armnn::IConnectableLayer* layer = data.m_Network->AddNormalizationLayer(descriptor); |
| 3305 | assert(layer != nullptr); |
| 3306 | input.Connect(layer->GetInputSlot(0)); |
| 3307 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3308 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3312 | typename HalOperation = typename HalPolicy::Operation, |
| 3313 | typename HalModel = typename HalPolicy::Model> |
| 3314 | bool ConvertLogistic(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3315 | { |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3316 | armnn::ActivationDescriptor desc; |
| 3317 | desc.m_Function = armnn::ActivationFunction::Sigmoid; |
| 3318 | |
| 3319 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 3320 | } |
| 3321 | |
| 3322 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3323 | typename HalOperation = typename HalPolicy::Operation, |
| 3324 | typename HalModel = typename HalPolicy::Model> |
| 3325 | bool ConvertMean(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3326 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3327 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3328 | |
| 3329 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3330 | if (!input.IsValid()) |
| 3331 | { |
| 3332 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3333 | } |
| 3334 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3335 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3336 | if (!output) |
| 3337 | { |
| 3338 | return Fail("%s: Could not read output 0", __func__); |
| 3339 | } |
| 3340 | |
| 3341 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3342 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3343 | const HalOperand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3344 | if (!axisOperand) |
| 3345 | { |
| 3346 | return Fail("%s: Could not read input 1", __func__); |
| 3347 | } |
| 3348 | |
| 3349 | std::vector<int32_t> axis; |
| 3350 | if (!GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data)) |
| 3351 | { |
| 3352 | return Fail("%s: Input 1 has invalid values", __func__); |
| 3353 | } |
| 3354 | |
| 3355 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3356 | |
| 3357 | // Convert the axis to unsigned int and remove duplicates. |
| 3358 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 3359 | std::set<unsigned int> uniqueAxis; |
| 3360 | std::transform(axis.begin(), axis.end(), |
| 3361 | std::inserter(uniqueAxis, uniqueAxis.begin()), |
| 3362 | [rank](int i) -> unsigned int { return (i + rank) % rank; }); |
| 3363 | |
| 3364 | // Get the "keep dims" flag. |
| 3365 | int32_t keepDims = 0; |
| 3366 | if (!GetInputInt32<HalPolicy>(operation, 2, keepDims, model, data)) |
| 3367 | { |
| 3368 | return Fail("%s: Could not read input 2", __func__); |
| 3369 | } |
| 3370 | |
| 3371 | armnn::MeanDescriptor descriptor; |
| 3372 | descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end()); |
| 3373 | descriptor.m_KeepDims = keepDims > 0; |
| 3374 | |
| 3375 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3376 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3377 | { |
| 3378 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3379 | IsMeanSupported, |
| 3380 | data.m_Backends, |
| 3381 | isSupported, |
| 3382 | inputInfo, |
| 3383 | outputInfo, |
| 3384 | descriptor); |
| 3385 | }; |
| 3386 | |
| 3387 | if(!IsDynamicTensor(outputInfo)) |
| 3388 | { |
| 3389 | validateFunc(outputInfo, isSupported); |
| 3390 | } |
| 3391 | else |
| 3392 | { |
| 3393 | isSupported = AreDynamicTensorsSupported(); |
| 3394 | } |
| 3395 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3396 | if (!isSupported) |
| 3397 | { |
| 3398 | return false; |
| 3399 | } |
| 3400 | |
| 3401 | armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor); |
| 3402 | assert(layer != nullptr); |
| 3403 | input.Connect(layer->GetInputSlot(0)); |
| 3404 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3405 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3409 | typename HalOperation = typename HalPolicy::Operation, |
| 3410 | typename HalModel = typename HalPolicy::Model> |
| 3411 | bool ConvertMul(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3412 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3413 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3414 | |
| 3415 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3416 | LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 3417 | |
| 3418 | if (!input0.IsValid() || !input1.IsValid()) |
| 3419 | { |
| 3420 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3421 | } |
| 3422 | |
| 3423 | // The FuseActivation parameter is always the input index 2 |
| 3424 | // and it should be optional |
| 3425 | ActivationFn activationFunction; |
| 3426 | if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data)) |
| 3427 | { |
| 3428 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3429 | } |
| 3430 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3431 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3432 | |
| 3433 | if (outputOperand == nullptr) |
| 3434 | { |
| 3435 | return false; |
| 3436 | } |
| 3437 | |
| 3438 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3439 | |
| 3440 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3441 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3442 | { |
| 3443 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3444 | IsMultiplicationSupported, |
| 3445 | data.m_Backends, |
| 3446 | isSupported, |
| 3447 | input0.GetTensorInfo(), |
| 3448 | input1.GetTensorInfo(), |
| 3449 | outputInfo); |
| 3450 | }; |
| 3451 | |
| 3452 | if(!IsDynamicTensor(outputInfo)) |
| 3453 | { |
| 3454 | validateFunc(outputInfo, isSupported); |
| 3455 | } |
| 3456 | else |
| 3457 | { |
| 3458 | isSupported = AreDynamicTensorsSupported(); |
| 3459 | } |
| 3460 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3461 | if (!isSupported) |
| 3462 | { |
| 3463 | return false; |
| 3464 | } |
| 3465 | |
| 3466 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddMultiplicationLayer(); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3467 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3468 | bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data); |
| 3469 | if (!isReshapeSupported) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3470 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3471 | return false; |
| 3472 | } |
Sadik Armagan | 64b19b5 | 2019-08-19 09:49:58 +0100 | [diff] [blame] | 3473 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3474 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 3475 | data, nullptr, validateFunc, activationFunction); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3479 | typename HalOperation = typename HalPolicy::Operation, |
| 3480 | typename HalModel = typename HalPolicy::Model> |
| 3481 | bool ConvertPad(HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3482 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3483 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3484 | |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3485 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3486 | if (!input.IsValid()) |
| 3487 | { |
| 3488 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3489 | } |
| 3490 | |
| 3491 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3492 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 3493 | |
| 3494 | armnn::PadDescriptor descriptor; |
| 3495 | if (!ConvertPaddings<HalPolicy>(operation, model, data, rank, descriptor)) |
| 3496 | { |
| 3497 | return Fail("%s: Could not convert paddings", __func__); |
| 3498 | } |
| 3499 | |
Sadik Armagan | 7b9ce8d | 2020-04-21 10:39:28 +0100 | [diff] [blame] | 3500 | // For a ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED tensor, |
| 3501 | // the scale and zeroPoint must be the same as input0 |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3502 | // Before Android Q, the pad value for ANEURALNETWORKS_TENSOR_QUANT8_ASYMM was undefined. Since Android Q the pad |
| 3503 | // value must be "logical zero" we set it to be equal to the QuantizationOffset so effectively it ends up as |
| 3504 | // (QuantizationOffset - QuantizationOffset) * scale = 0. |
Sadik Armagan | 7b9ce8d | 2020-04-21 10:39:28 +0100 | [diff] [blame] | 3505 | if (inputInfo.GetDataType() == armnn::DataType::QAsymmU8 || inputInfo.GetDataType() == armnn::DataType::QAsymmS8) |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3506 | { |
| 3507 | descriptor.m_PadValue = inputInfo.GetQuantizationOffset(); |
| 3508 | } |
| 3509 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3510 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3511 | if (!output) |
| 3512 | { |
| 3513 | return Fail("%s: Could not read output", __func__); |
| 3514 | } |
| 3515 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 3516 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3517 | |
| 3518 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3519 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3520 | { |
| 3521 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3522 | IsPadSupported, |
| 3523 | data.m_Backends, |
| 3524 | isSupported, |
| 3525 | inputInfo, |
| 3526 | outputInfo, |
| 3527 | descriptor); |
| 3528 | }; |
| 3529 | |
| 3530 | if(!IsDynamicTensor(outputInfo)) |
| 3531 | { |
| 3532 | validateFunc(outputInfo, isSupported); |
| 3533 | } |
| 3534 | else |
| 3535 | { |
| 3536 | isSupported = AreDynamicTensorsSupported(); |
| 3537 | } |
| 3538 | |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3539 | if (!isSupported) |
| 3540 | { |
| 3541 | return false; |
| 3542 | } |
| 3543 | |
| 3544 | armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor); |
| 3545 | assert(layer != nullptr); |
| 3546 | input.Connect(layer->GetInputSlot(0)); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3547 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3548 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 3549 | } |
| 3550 | |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3551 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3552 | typename HalOperation = typename HalPolicy::Operation, |
| 3553 | typename HalModel = typename HalPolicy::Model> |
| 3554 | bool ConvertReshape(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3555 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3556 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3557 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3558 | const HalOperand* inputOperand = GetInputOperand<HalPolicy>(operation, 0, model); |
| 3559 | const HalOperand* requestedShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 3560 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3561 | |
| 3562 | if (inputOperand == nullptr |
| 3563 | || requestedShapeOperand == nullptr |
| 3564 | || outputOperand == nullptr) |
| 3565 | { |
| 3566 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3567 | } |
| 3568 | |
| 3569 | if (requestedShapeOperand->dimensions.size() != 1) |
| 3570 | { |
| 3571 | return Fail("%s: Input 1 expected to be one-dimensional (found %i dimensions)", |
| 3572 | __func__, requestedShapeOperand->dimensions.size()); |
| 3573 | } |
| 3574 | |
| 3575 | std::vector<int32_t> targetDimensions; |
| 3576 | if (!GetTensorInt32Values<HalPolicy>(*requestedShapeOperand, targetDimensions, model, data)) |
| 3577 | { |
| 3578 | return Fail("%s: Could not read values of input 1", __func__); |
| 3579 | } |
| 3580 | |
| 3581 | const Shape inputOperandShape = GetOperandShape(*inputOperand); |
| 3582 | |
| 3583 | Shape requestedShape; |
| 3584 | // targetDimensions may contain special values (e.g. -1). reshapePrepare() is an AndroidNN provided utility |
| 3585 | // function that resolves these values into a fully specified tensor shape. |
| 3586 | if (!reshapePrepare(inputOperandShape, targetDimensions.data(), targetDimensions.size(), &requestedShape)) |
| 3587 | { |
| 3588 | return Fail("%s: Failed to resolve the requested shape", __func__); |
| 3589 | } |
| 3590 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3591 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3592 | if (!input.IsValid()) |
| 3593 | { |
| 3594 | return Fail("%s: Could not read input 0", __func__); |
| 3595 | } |
| 3596 | |
| 3597 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 3598 | reshapeDescriptor.m_TargetShape = armnn::TensorShape(requestedShape.dimensions.size(), |
| 3599 | requestedShape.dimensions.data()); |
| 3600 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3601 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 3602 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3603 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3604 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3605 | { |
| 3606 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3607 | IsReshapeSupported, |
| 3608 | data.m_Backends, |
| 3609 | isSupported, |
| 3610 | input.GetTensorInfo(), |
| 3611 | outputInfo, |
| 3612 | reshapeDescriptor); |
| 3613 | }; |
| 3614 | |
| 3615 | if(!IsDynamicTensor(outputInfo)) |
| 3616 | { |
| 3617 | validateFunc(outputInfo, isSupported); |
| 3618 | } |
| 3619 | else |
| 3620 | { |
| 3621 | isSupported = AreDynamicTensorsSupported(); |
| 3622 | } |
| 3623 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3624 | if (!isSupported) |
| 3625 | { |
| 3626 | return false; |
| 3627 | } |
| 3628 | |
| 3629 | armnn::IConnectableLayer* layer = data.m_Network->AddReshapeLayer(reshapeDescriptor); |
| 3630 | assert(layer != nullptr); |
| 3631 | input.Connect(layer->GetInputSlot(0)); |
| 3632 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3633 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3637 | typename HalOperation = typename HalPolicy::Operation, |
| 3638 | typename HalModel = typename HalPolicy::Model> |
| 3639 | bool ConvertSub(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3640 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3641 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3642 | |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3643 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3644 | LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 3645 | |
| 3646 | if (!input0.IsValid() || !input1.IsValid()) |
| 3647 | { |
| 3648 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3649 | } |
| 3650 | |
| 3651 | // The FuseActivation parameter is always the input index 2 |
| 3652 | // and it should be optional |
| 3653 | ActivationFn activationFunction; |
| 3654 | if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data)) |
| 3655 | { |
| 3656 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3657 | } |
| 3658 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3659 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3660 | if (!output) |
| 3661 | { |
| 3662 | return Fail("%s: Could not read output 0", __func__); |
| 3663 | } |
| 3664 | |
| 3665 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3666 | |
| 3667 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3668 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3669 | { |
| 3670 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3671 | IsSubtractionSupported, |
| 3672 | data.m_Backends, |
| 3673 | isSupported, |
| 3674 | input0.GetTensorInfo(), |
| 3675 | input1.GetTensorInfo(), |
| 3676 | outputInfo); |
| 3677 | }; |
| 3678 | |
| 3679 | if(IsDynamicTensor(outputInfo)) |
| 3680 | { |
| 3681 | isSupported = AreDynamicTensorsSupported(); |
| 3682 | } |
| 3683 | else |
| 3684 | { |
| 3685 | validateFunc(outputInfo, isSupported); |
| 3686 | } |
| 3687 | |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3688 | if (!isSupported) |
| 3689 | { |
| 3690 | return false; |
| 3691 | } |
| 3692 | |
| 3693 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddSubtractionLayer(); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3694 | |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3695 | bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data); |
| 3696 | if (!isReshapeSupported) |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3697 | { |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3698 | return false; |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3699 | } |
Kevin May | fcf2a15 | 2020-09-08 16:06:32 +0100 | [diff] [blame] | 3700 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *startLayer, model, |
| 3701 | data, nullptr, validateFunc, activationFunction); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 3704 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3705 | typename HalOperation = typename HalPolicy::Operation, |
| 3706 | typename HalModel = typename HalPolicy::Model> |
| 3707 | bool ConvertSqueeze(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3708 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3709 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3710 | |
| 3711 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3712 | if (!input.IsValid()) |
| 3713 | { |
| 3714 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3715 | } |
| 3716 | |
| 3717 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3718 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 3719 | if (rank > 4) |
| 3720 | { |
| 3721 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 3722 | } |
| 3723 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3724 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3725 | if (!output) |
| 3726 | { |
| 3727 | return Fail("%s: Could not read output 0", __func__); |
| 3728 | } |
Sadik Armagan | 346e811 | 2020-09-02 09:55:14 +0100 | [diff] [blame] | 3729 | |
| 3730 | if (IsDynamicTensor(GetTensorInfoForOperand(*output)) && !(AreDynamicTensorsSupported())) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3731 | { |
| 3732 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 3733 | } |
| 3734 | |
| 3735 | // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure |
| 3736 | // if the operand index is out of bounds. |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3737 | const HalOperand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model, false); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3738 | |
| 3739 | const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 3740 | |
| 3741 | std::vector<int32_t> axis; |
| 3742 | if (!axisOperand) |
| 3743 | { |
| 3744 | axis.assign(dimensionSequence, |
| 3745 | dimensionSequence + rank); |
| 3746 | } |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 3747 | else if (!GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data)) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3748 | { |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 3749 | return Fail("%s: Operation has an invalid or unsupported axis operand", __func__); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | std::vector<uint32_t> outputDims; |
| 3753 | for (unsigned int i = 0; i < rank; i++) |
| 3754 | { |
| 3755 | bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end()); |
| 3756 | auto currentDimension = inputInfo.GetShape()[i]; |
| 3757 | if (skipSqueeze || currentDimension != 1) |
| 3758 | { |
| 3759 | outputDims.push_back(currentDimension); |
| 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data()); |
| 3764 | |
| 3765 | armnn::TensorInfo outputInfo = inputInfo; |
| 3766 | outputInfo.SetShape(outShape); |
| 3767 | |
| 3768 | armnn::ReshapeDescriptor reshapeDesc; |
| 3769 | reshapeDesc.m_TargetShape = outputInfo.GetShape(); |
| 3770 | |
| 3771 | bool isSupported = false; |
| 3772 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3773 | IsReshapeSupported, |
| 3774 | data.m_Backends, |
| 3775 | isSupported, |
| 3776 | inputInfo, |
Kevin May | aed08ac | 2019-12-12 16:33:31 +0000 | [diff] [blame] | 3777 | outputInfo, |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3778 | reshapeDesc); |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3779 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3780 | if (!isSupported) |
| 3781 | { |
| 3782 | return false; |
| 3783 | } |
| 3784 | |
| 3785 | armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc); |
| 3786 | assert(layer != nullptr); |
| 3787 | input.Connect(layer->GetInputSlot(0)); |
| 3788 | |
| 3789 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data); |
| 3790 | } |
| 3791 | |
| 3792 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3793 | typename HalOperation = typename HalPolicy::Operation, |
| 3794 | typename HalModel = typename HalPolicy::Model> |
| 3795 | bool ConvertStridedSlice(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3796 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3797 | using HalOperand = typename HalPolicy::Operand; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3798 | |
| 3799 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3800 | if (!input.IsValid()) |
| 3801 | { |
| 3802 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3803 | } |
| 3804 | |
| 3805 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3806 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 3807 | if (rank > 4) |
| 3808 | { |
| 3809 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 3810 | } |
| 3811 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3812 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3813 | if (!output) |
| 3814 | { |
| 3815 | return Fail("%s: Could not read output 0", __func__); |
| 3816 | } |
| 3817 | |
| 3818 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3819 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3820 | const HalOperand* beginOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 3821 | const HalOperand* endOperand = GetInputOperand<HalPolicy>(operation, 2, model); |
| 3822 | const HalOperand* stridesOperand = GetInputOperand<HalPolicy>(operation, 3, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3823 | |
| 3824 | std::vector<int32_t> beginValues; |
| 3825 | std::vector<int32_t> endValues; |
| 3826 | std::vector<int32_t> stridesValues; |
| 3827 | |
| 3828 | // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input) |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3829 | auto ValidateInputOperands = [&] (const HalOperand& operand, std::vector<int32_t>& operandValues) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3830 | { |
| 3831 | if (!GetTensorInt32Values<HalPolicy>(operand, operandValues, model, data)) |
| 3832 | { |
| 3833 | return false; |
| 3834 | } |
| 3835 | |
| 3836 | if (operandValues.size() != rank) |
| 3837 | { |
| 3838 | return false; |
| 3839 | } |
| 3840 | |
| 3841 | return true; |
| 3842 | }; |
| 3843 | |
| 3844 | if (!ValidateInputOperands(*beginOperand, beginValues) |
| 3845 | || !ValidateInputOperands(*endOperand, endValues) |
| 3846 | || !ValidateInputOperands(*stridesOperand, stridesValues)) |
| 3847 | { |
| 3848 | return Fail("%s: Operation has invalid input operand", __func__); |
| 3849 | } |
| 3850 | |
| 3851 | // Stride cannot have value '0' |
| 3852 | if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; })) |
| 3853 | { |
| 3854 | return Fail("%s: Stride must be non-zero value.", __func__); |
| 3855 | } |
| 3856 | |
| 3857 | armnn::StridedSliceDescriptor descriptor; |
| 3858 | descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend()); |
| 3859 | descriptor.m_End.assign(endValues.cbegin(), endValues.cend()); |
| 3860 | descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend()); |
| 3861 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 3862 | |
| 3863 | // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags |
| 3864 | if (!GetInputInt32<HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) || |
| 3865 | !GetInputInt32<HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) || |
| 3866 | !GetInputInt32<HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data)) |
| 3867 | { |
| 3868 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3869 | } |
| 3870 | |
| 3871 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3872 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3873 | { |
| 3874 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3875 | IsStridedSliceSupported, |
| 3876 | data.m_Backends, |
| 3877 | isSupported, |
| 3878 | inputInfo, |
| 3879 | outputInfo, |
| 3880 | descriptor); |
| 3881 | }; |
| 3882 | |
| 3883 | if(IsDynamicTensor(outputInfo)) |
| 3884 | { |
| 3885 | isSupported = AreDynamicTensorsSupported(); |
| 3886 | } |
| 3887 | else |
| 3888 | { |
| 3889 | validateFunc(outputInfo, isSupported); |
| 3890 | } |
| 3891 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3892 | if (!isSupported) |
| 3893 | { |
| 3894 | return false; |
| 3895 | } |
| 3896 | |
Sadik Armagan | be6b3c2 | 2020-05-14 11:51:33 +0100 | [diff] [blame] | 3897 | // Check if slice can fit in a inferred output |
| 3898 | armnn::TensorShape inputShape = inputInfo.GetShape(); |
| 3899 | for (unsigned int i = 0; i < inputShape.GetNumDimensions(); i++) |
| 3900 | { |
| 3901 | int stride = descriptor.m_Stride[i]; |
Sadik Armagan | be6b3c2 | 2020-05-14 11:51:33 +0100 | [diff] [blame] | 3902 | |
| 3903 | if (descriptor.m_ShrinkAxisMask & (1 << i)) |
| 3904 | { |
| 3905 | // If the difference between the start point and the end point of the slice on an axis being shrunk |
| 3906 | // is greater than 1 then throw an error as the output will not be large enough to hold the slice |
| 3907 | if (((descriptor.m_Begin[i] - descriptor.m_End[i]) > 1) |
| 3908 | || ((descriptor.m_Begin[i] - descriptor.m_End[i]) < -1)) |
| 3909 | { |
| 3910 | return Fail("%s: StridedSlice: Output will not be large enough to hold the slice", __func__); |
| 3911 | } |
Ryan OShea | 00b586b | 2020-07-03 11:31:20 +0100 | [diff] [blame] | 3912 | |
| 3913 | if(stride < 0) |
| 3914 | { |
| 3915 | return Fail("%s: StridedSlice: Stride can not be negative while ShrinkAxisMask is set.", __func__); |
| 3916 | } |
Sadik Armagan | be6b3c2 | 2020-05-14 11:51:33 +0100 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3920 | armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor); |
| 3921 | assert(layer != nullptr); |
| 3922 | input.Connect(layer->GetInputSlot(0)); |
| 3923 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3924 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | template<typename HalPolicy, |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3928 | typename HalOperation = typename HalPolicy::Operation, |
| 3929 | typename HalModel = typename HalPolicy::Model> |
| 3930 | bool ConvertTranspose(const HalOperation& operation, const HalModel& model, ConversionData& data) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3931 | { |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3932 | using HalOperand = typename HalPolicy::Operand; |
Kevin May | 81f27fd | 2020-08-20 10:22:53 +0100 | [diff] [blame] | 3933 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3934 | |
| 3935 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 3936 | if (!input.IsValid()) |
| 3937 | { |
| 3938 | return Fail("%s: Operation has invalid inputs", __func__); |
| 3939 | } |
| 3940 | |
| 3941 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 3942 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 3943 | if (rank > 4) |
| 3944 | { |
| 3945 | Fail("%s: Inputs with rank greater than 4 are not supported", __func__); |
| 3946 | } |
| 3947 | |
| 3948 | // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure |
| 3949 | // if the operand index is out of bounds. |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3950 | const HalOperand* permOperand = GetInputOperand<HalPolicy>(operation, 1, model, false); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3951 | |
| 3952 | std::vector<int32_t> perm(rank); |
Kevin May | 81f27fd | 2020-08-20 10:22:53 +0100 | [diff] [blame] | 3953 | if (!permOperand || (permOperand->lifetime == HalOperandLifeTime::NO_VALUE)) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3954 | { |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3955 | for (unsigned int i = rank; i > 0; i--) |
| 3956 | { |
Matthew Sloyan | 9b088d9 | 2020-09-14 15:12:55 +0100 | [diff] [blame] | 3957 | perm[rank - i] = armnn::numeric_cast<int> (i - 1); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3958 | } |
| 3959 | } |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 3960 | else if (!GetTensorInt32Values<HalPolicy>(*permOperand, perm, model, data)) |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3961 | { |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 3962 | return Fail("%s: Operation has an invalid or unsupported permutation operand", __func__); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank); |
| 3966 | |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 3967 | armnn::TransposeDescriptor transposeDesc; |
| 3968 | transposeDesc.m_DimMappings = armnn::PermutationVector(outputDims.data(), outputDims.size()); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3969 | |
Aron Virginas-Tar | aa5df2d | 2019-11-19 12:49:55 +0000 | [diff] [blame] | 3970 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3971 | if (!output) |
| 3972 | { |
| 3973 | return Fail("%s: Could not read output 0", __func__); |
| 3974 | } |
| 3975 | |
| 3976 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 3977 | |
| 3978 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 3979 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 3980 | { |
| 3981 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 3982 | IsTransposeSupported, |
| 3983 | data.m_Backends, |
| 3984 | isSupported, |
| 3985 | inputInfo, |
| 3986 | outputInfo, |
| 3987 | transposeDesc); |
| 3988 | }; |
| 3989 | |
| 3990 | if(IsDynamicTensor(outputInfo)) |
| 3991 | { |
| 3992 | isSupported = AreDynamicTensorsSupported(); |
| 3993 | } |
| 3994 | else |
| 3995 | { |
| 3996 | validateFunc(outputInfo, isSupported); |
| 3997 | } |
| 3998 | |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 3999 | if (!isSupported) |
| 4000 | { |
| 4001 | return false; |
| 4002 | } |
| 4003 | |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 4004 | armnn::IConnectableLayer* const layer = data.m_Network->AddTransposeLayer(transposeDesc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 4005 | assert(layer != nullptr); |
| 4006 | input.Connect(layer->GetInputSlot(0)); |
| 4007 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 4008 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Mike Kelly | 4627280 | 2019-08-14 17:00:48 +0100 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | template<typename HalPolicy, |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4012 | typename HalOperation = typename HalPolicy::Operation, |
Finn Williams | 0e4e439 | 2019-07-31 10:56:27 +0100 | [diff] [blame] | 4013 | typename HalOperand = typename HalPolicy::Operand, |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4014 | typename HalModel = typename HalPolicy::Model> |
| 4015 | bool ConvertBatchToSpaceNd(const HalOperation& operation, |
| 4016 | const HalModel& model, |
| 4017 | ConversionData& data) |
| 4018 | { |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4019 | |
| 4020 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 4021 | if (!input.IsValid()) |
| 4022 | { |
| 4023 | return Fail("%s: Operation has invalid inputs", __func__); |
| 4024 | } |
| 4025 | |
| 4026 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 4027 | if (!output) |
| 4028 | { |
| 4029 | return Fail("%s: Could not read output 0", __func__); |
| 4030 | } |
| 4031 | |
| 4032 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4033 | |
| 4034 | const HalOperand* blockOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 4035 | if (!blockOperand) |
| 4036 | { |
| 4037 | return Fail("%s: Could not read input 1", __func__); |
| 4038 | } |
| 4039 | |
| 4040 | // Convert the block operand to int32 |
| 4041 | std::vector<int32_t> block; |
| 4042 | if (!GetTensorInt32Values<HalPolicy>(*blockOperand, block, model, data)) |
| 4043 | { |
| 4044 | return Fail("%s: Input 1 has invalid values", __func__); |
| 4045 | } |
| 4046 | |
| 4047 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 4048 | |
| 4049 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 4050 | if (rank != 4) |
| 4051 | { |
| 4052 | Fail("%s: Only inputs with rank equal to 4 are supported", __func__); |
| 4053 | } |
| 4054 | |
| 4055 | if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; })) |
| 4056 | { |
| 4057 | return Fail("%s: Block sizes for each spatial dimension of the input tensor must be" |
| 4058 | " greater than or equal to 1", __func__); |
| 4059 | } |
| 4060 | |
| 4061 | armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc; |
| 4062 | batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend()); |
| 4063 | batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC; |
| 4064 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 4065 | if (Is12OrLaterOperand(*output)) |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4066 | { |
Finn Williams | 0e4e439 | 2019-07-31 10:56:27 +0100 | [diff] [blame] | 4067 | batchToSpaceNdDesc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 2, model, data); |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4068 | } |
| 4069 | // Setting crops to 0,0 0,0 as it is not supported in Android NN API |
| 4070 | batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}}; |
| 4071 | |
| 4072 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 4073 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 4074 | { |
| 4075 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 4076 | IsBatchToSpaceNdSupported, |
| 4077 | data.m_Backends, |
| 4078 | isSupported, |
| 4079 | inputInfo, |
| 4080 | outputInfo, |
| 4081 | batchToSpaceNdDesc); |
| 4082 | }; |
| 4083 | |
| 4084 | if(!IsDynamicTensor(outputInfo)) |
| 4085 | { |
| 4086 | validateFunc(outputInfo, isSupported); |
| 4087 | } |
| 4088 | else |
| 4089 | { |
| 4090 | isSupported = AreDynamicTensorsSupported(); |
| 4091 | } |
| 4092 | |
| 4093 | |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4094 | if (!isSupported) |
| 4095 | { |
| 4096 | return false; |
| 4097 | } |
| 4098 | |
| 4099 | armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc); |
| 4100 | assert(layer != nullptr); |
| 4101 | input.Connect(layer->GetInputSlot(0)); |
| 4102 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 4103 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame] | 4104 | } |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 4105 | |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4106 | template<typename HalPolicy, |
| 4107 | typename HalOperation = typename HalPolicy::Operation, |
| 4108 | typename HalOperand = typename HalPolicy::Operand, |
| 4109 | typename HalModel = typename HalPolicy::Model> |
| 4110 | bool ConvertSpaceToBatchNd(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 4111 | { |
| 4112 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 4113 | if (!input.IsValid()) |
| 4114 | { |
| 4115 | return Fail("%s: Operation has invalid inputs", __func__); |
| 4116 | } |
| 4117 | |
| 4118 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 4119 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 4120 | unsigned int spatialDim = rank - 2; |
| 4121 | |
| 4122 | if (rank != 4) |
| 4123 | { |
| 4124 | Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 4125 | } |
| 4126 | |
| 4127 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 4128 | if (!output) |
| 4129 | { |
| 4130 | return Fail("%s: Could not read output 0", __func__); |
| 4131 | } |
| 4132 | |
| 4133 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4134 | |
| 4135 | const HalOperand* blockShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 4136 | const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 2, model); |
| 4137 | |
| 4138 | armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand); |
| 4139 | if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim) |
| 4140 | { |
| 4141 | return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim); |
| 4142 | } |
| 4143 | |
| 4144 | std::vector<int32_t> blockShape; |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 4145 | if (!GetTensorInt32Values<HalPolicy>(*blockShapeOperand, blockShape, model, data)) |
| 4146 | { |
| 4147 | return Fail("%s: Operation has an invalid or unsupported block size operand", __func__); |
| 4148 | } |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4149 | if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; })) |
| 4150 | { |
| 4151 | return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__); |
| 4152 | } |
| 4153 | |
| 4154 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
| 4155 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim) |
| 4156 | { |
| 4157 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim); |
| 4158 | } |
| 4159 | |
| 4160 | std::vector<std::pair<unsigned int, unsigned int>> paddingList; |
| 4161 | std::vector<int32_t> paddings; |
Mike Kelly | eec836e | 2020-02-18 10:03:30 +0000 | [diff] [blame] | 4162 | if (!GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data)) |
| 4163 | { |
| 4164 | return Fail("%s: Operation has an invalid or unsupported paddings operand", __func__); |
| 4165 | } |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4166 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 4167 | { |
| 4168 | int paddingBeforeInput = paddings[i]; |
| 4169 | int paddingAfterInput = paddings[i + 1]; |
| 4170 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 4171 | { |
| 4172 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 4173 | } |
| 4174 | |
| 4175 | paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
| 4176 | } |
| 4177 | |
| 4178 | armnn::SpaceToBatchNdDescriptor descriptor; |
| 4179 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 4180 | descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend()); |
| 4181 | descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend()); |
| 4182 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 4183 | if (Is12OrLaterOperand(*output)) |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4184 | { |
| 4185 | descriptor.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 3, model, data); |
| 4186 | } |
| 4187 | |
| 4188 | bool isSupported = false; |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 4189 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 4190 | { |
| 4191 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 4192 | IsSpaceToBatchNdSupported, |
| 4193 | data.m_Backends, |
| 4194 | isSupported, |
| 4195 | inputInfo, |
| 4196 | outputInfo, |
| 4197 | descriptor); |
| 4198 | }; |
| 4199 | |
| 4200 | if(IsDynamicTensor(outputInfo)) |
| 4201 | { |
| 4202 | isSupported = AreDynamicTensorsSupported(); |
| 4203 | } |
| 4204 | else |
| 4205 | { |
| 4206 | validateFunc(outputInfo, isSupported); |
| 4207 | } |
| 4208 | |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4209 | if (!isSupported) |
| 4210 | { |
| 4211 | return false; |
| 4212 | } |
| 4213 | |
| 4214 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor); |
| 4215 | assert(layer != nullptr); |
| 4216 | input.Connect(layer->GetInputSlot(0)); |
| 4217 | |
Teresa Charlin | 4bd9a74 | 2020-08-12 12:58:50 +0100 | [diff] [blame] | 4218 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data, nullptr, validateFunc); |
Finn Williams | d74c505 | 2019-07-30 17:06:00 +0100 | [diff] [blame] | 4219 | } |
| 4220 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 4221 | } // namespace armnn_driver |