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