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