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