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 | |
| 17 | #include <ActivationFunctor.h> |
| 18 | #include <CpuExecutor.h> |
| 19 | #include <OperationsUtils.h> |
| 20 | |
| 21 | #include <boost/assert.hpp> |
| 22 | #include <boost/core/ignore_unused.hpp> |
Aron Virginas-Tar | 0e7ab54 | 2019-04-10 15:02:31 +0100 | [diff] [blame] | 23 | #include <boost/numeric/conversion/cast.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 24 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 25 | |
| 26 | #include <log/log.h> |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 27 | #include <vector> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 28 | |
| 29 | namespace armnn_driver |
| 30 | { |
| 31 | |
| 32 | /// |
| 33 | /// Helper classes |
| 34 | /// |
| 35 | |
| 36 | struct ConversionData |
| 37 | { |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 38 | ConversionData(const std::vector<armnn::BackendId>& backends) |
| 39 | : m_Backends(backends) |
| 40 | , m_Network(nullptr, nullptr) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 41 | {} |
| 42 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 43 | const std::vector<armnn::BackendId> m_Backends; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 44 | armnn::INetworkPtr m_Network; |
| 45 | std::vector<armnn::IOutputSlot*> m_OutputSlotForOperand; |
| 46 | std::vector<android::nn::RunTimePoolInfo> m_MemPools; |
| 47 | }; |
| 48 | |
| 49 | class LayerInputHandle |
| 50 | { |
| 51 | public: |
| 52 | LayerInputHandle(); |
| 53 | LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo); |
| 54 | |
| 55 | bool IsValid() const; |
| 56 | |
| 57 | void Connect(armnn::IInputSlot& inputSlot); |
| 58 | |
| 59 | const armnn::TensorInfo& GetTensorInfo() const; |
| 60 | |
| 61 | private: |
| 62 | armnn::IOutputSlot* m_OutputSlot; |
| 63 | bool m_Valid; |
| 64 | armnn::TensorInfo m_TensorInfo; |
| 65 | }; |
| 66 | |
| 67 | class ConstTensorPin |
| 68 | { |
| 69 | public: |
| 70 | // Creates an invalid tensor pin (can be used to signal errors) |
| 71 | // The optional flag can be set to indicate the tensor values were missing, but it was otherwise valid |
| 72 | ConstTensorPin(bool optional = false); |
| 73 | |
| 74 | // @param tensorInfo TensorInfo associated with the tensor. |
| 75 | // @param valueStart Start address of tensor data. Belongs to one of the memory pools associated with |
| 76 | // the model being converted. |
| 77 | // @param numBytes Number of bytes for the tensor data. |
| 78 | ConstTensorPin(const armnn::TensorInfo& tensorInfo, const void* valueStart, uint32_t numBytes, |
| 79 | const armnn::PermutationVector& mappings); |
| 80 | |
| 81 | ConstTensorPin(const ConstTensorPin& other) = delete; |
| 82 | ConstTensorPin(ConstTensorPin&& other) = default; |
| 83 | |
| 84 | bool IsValid() const; |
| 85 | bool IsOptional() const; |
| 86 | |
| 87 | const armnn::ConstTensor& GetConstTensor() const; |
| 88 | const armnn::ConstTensor* GetConstTensorPtr() const; |
| 89 | |
| 90 | private: |
| 91 | armnn::ConstTensor m_ConstTensor; |
| 92 | |
| 93 | // Owned memory for swizzled tensor data, only required if the tensor needed |
| 94 | // swizzling. Otherwise, @ref m_ConstTensor will reference memory from one of |
| 95 | // the pools associated with the model being converted. |
| 96 | std::vector<uint8_t> m_SwizzledTensorData; |
| 97 | |
| 98 | // optional flag to indicate that an invalid tensor pin is not an error, but the optional values were not given |
| 99 | bool m_Optional; |
| 100 | }; |
| 101 | |
| 102 | } // namespace armnn_driver |
| 103 | |
| 104 | /// |
| 105 | /// Utility functions |
| 106 | /// |
| 107 | |
| 108 | namespace |
| 109 | { |
| 110 | |
| 111 | using namespace armnn_driver; |
| 112 | using namespace android::nn; |
| 113 | |
| 114 | // Convenience function to log the reason for failing to convert a model. |
| 115 | // @return Always returns false (so that it can be used by callers as a quick way to signal an error and return) |
| 116 | template<class... Args> |
| 117 | static bool Fail(const char* formatStr, Args&&... args) |
| 118 | { |
| 119 | ALOGD(formatStr, std::forward<Args>(args)...); |
| 120 | return false; |
| 121 | } |
| 122 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 123 | // Convenience macro to call an Is*Supported function and log caller name together with reason for lack of support. |
| 124 | // Called as: FORWARD_LAYER_SUPPORT_FUNC(__func__, Is*Supported, backends, a, b, c, d, e) |
| 125 | #define FORWARD_LAYER_SUPPORT_FUNC(funcName, func, backends, supported, ...) \ |
| 126 | std::string reasonIfUnsupported; \ |
| 127 | try { \ |
| 128 | for (auto&& backendId : backends) \ |
| 129 | { \ |
| 130 | auto layerSupportObject = armnn::GetILayerSupportByBackendId(backendId); \ |
| 131 | if (layerSupportObject) \ |
| 132 | { \ |
| 133 | supported = \ |
| 134 | layerSupportObject->func(__VA_ARGS__, armnn::Optional<std::string&>(reasonIfUnsupported)); \ |
| 135 | if (supported) \ |
| 136 | { \ |
| 137 | break; \ |
| 138 | } \ |
| 139 | else \ |
| 140 | { \ |
| 141 | if (reasonIfUnsupported.size() > 0) \ |
| 142 | { \ |
| 143 | ALOGD("%s: not supported by armnn: %s", funcName, reasonIfUnsupported.c_str()); \ |
| 144 | } \ |
| 145 | else \ |
| 146 | { \ |
| 147 | ALOGD("%s: not supported by armnn", funcName); \ |
| 148 | } \ |
| 149 | } \ |
| 150 | } \ |
| 151 | else \ |
| 152 | { \ |
| 153 | ALOGD("%s: backend not registered: %s", funcName, backendId.Get().c_str()); \ |
| 154 | } \ |
| 155 | } \ |
| 156 | if (!supported) \ |
| 157 | { \ |
| 158 | ALOGD("%s: not supported by any specified backend", funcName); \ |
| 159 | } \ |
| 160 | } catch (const armnn::InvalidArgumentException &e) { \ |
| 161 | throw armnn::InvalidArgumentException(e, "Failed to check layer support", CHECK_LOCATION()); \ |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 162 | } |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 163 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 164 | template<typename Operand> |
| 165 | armnn::TensorShape GetTensorShapeForOperand(const Operand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 166 | { |
| 167 | return armnn::TensorShape(operand.dimensions.size(), operand.dimensions.data()); |
| 168 | } |
| 169 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 170 | inline bool IsOperandTypeSupportedForTensors(V1_0::OperandType type) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 171 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 172 | return type == V1_0::OperandType::TENSOR_FLOAT32 || |
| 173 | type == V1_0::OperandType::TENSOR_QUANT8_ASYMM || |
| 174 | type == V1_0::OperandType::TENSOR_INT32; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 177 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 178 | |
| 179 | inline bool IsOperandTypeSupportedForTensors(V1_2::OperandType type) |
| 180 | { |
| 181 | return type == V1_2::OperandType::BOOL || |
| 182 | type == V1_2::OperandType::TENSOR_FLOAT16 || |
| 183 | type == V1_2::OperandType::TENSOR_FLOAT32 || |
| 184 | type == V1_2::OperandType::TENSOR_QUANT8_ASYMM || |
| 185 | type == V1_2::OperandType::TENSOR_QUANT16_SYMM || |
| 186 | type == V1_2::OperandType::TENSOR_INT32; |
| 187 | } |
| 188 | |
| 189 | #endif |
| 190 | |
| 191 | inline bool IsBool(V1_0::Operand) |
| 192 | { |
| 193 | return false; |
| 194 | } |
| 195 | |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 196 | inline bool Is12Operand(V1_0::Operand) |
| 197 | { |
| 198 | return false; |
| 199 | } |
| 200 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 201 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 202 | |
| 203 | inline bool IsBool(V1_2::Operand operand) |
| 204 | { |
| 205 | return operand.type == V1_2::OperandType::BOOL; |
| 206 | } |
| 207 | |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 208 | /// Checks if a operand is 1_2 Operand |
| 209 | inline bool Is12Operand(V1_2::Operand) |
| 210 | { |
| 211 | return true; |
| 212 | } |
| 213 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 214 | #endif |
| 215 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 216 | template<typename LayerHandleType> |
| 217 | armnn::IConnectableLayer& AddReshapeLayer(armnn::INetwork& network, LayerHandleType& inputLayer, |
| 218 | armnn::TensorInfo reshapeInfo) |
| 219 | { |
| 220 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 221 | reshapeDescriptor.m_TargetShape = reshapeInfo.GetShape(); |
| 222 | |
| 223 | armnn::IConnectableLayer* reshapeLayer = network.AddReshapeLayer(reshapeDescriptor); |
| 224 | BOOST_ASSERT(reshapeLayer != nullptr); |
| 225 | |
| 226 | // Attach the input layer to the reshape layer |
| 227 | inputLayer.Connect(reshapeLayer->GetInputSlot(0)); |
| 228 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapeInfo); |
| 229 | |
| 230 | return *reshapeLayer; |
| 231 | } |
| 232 | |
| 233 | void BroadcastTensor(LayerInputHandle& input0, LayerInputHandle& input1, |
| 234 | armnn::IConnectableLayer* startLayer, armnn::INetwork& network) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 235 | { |
| 236 | BOOST_ASSERT(startLayer != nullptr); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 237 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 238 | const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo(); |
| 239 | const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo(); |
| 240 | |
| 241 | unsigned int inputDimensions0 = inputInfo0.GetNumDimensions(); |
| 242 | unsigned int inputDimensions1 = inputInfo1.GetNumDimensions(); |
| 243 | |
| 244 | if (inputDimensions0 == inputDimensions1) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 245 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 246 | // The inputs have the same number of dimensions, simply connect them to the given layer as they are |
| 247 | input0.Connect(startLayer->GetInputSlot(0)); |
| 248 | input1.Connect(startLayer->GetInputSlot(1)); |
| 249 | |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Since the number of dimensions do not match then we need to add degenerate dimensions |
| 254 | // to the "smaller" tensor using a reshape, while keeping the order of the inputs. |
| 255 | |
| 256 | unsigned int maxInputDimensions = std::max(inputDimensions0, inputDimensions1); |
| 257 | unsigned int sizeDifference = std::abs(boost::numeric_cast<int>(inputDimensions0) - |
| 258 | boost::numeric_cast<int>(inputDimensions1)); |
| 259 | |
| 260 | bool input0IsSmaller = inputDimensions0 < inputDimensions1; |
| 261 | LayerInputHandle& smallInputHandle = input0IsSmaller ? input0 : input1; |
| 262 | const armnn::TensorInfo& smallInfo = smallInputHandle.GetTensorInfo(); |
| 263 | |
| 264 | const armnn::TensorShape& smallShape = smallInfo.GetShape(); |
| 265 | std::vector<unsigned int> reshapedDimensions(maxInputDimensions, 1); |
| 266 | for (unsigned int i = sizeDifference; i < maxInputDimensions; i++) |
| 267 | { |
| 268 | reshapedDimensions[i] = smallShape[i - sizeDifference]; |
| 269 | } |
| 270 | |
| 271 | armnn::TensorInfo reshapedInfo = smallInfo; |
| 272 | reshapedInfo.SetShape(armnn::TensorShape{ boost::numeric_cast<unsigned int>(reshapedDimensions.size()), |
| 273 | reshapedDimensions.data() }); |
| 274 | armnn::IConnectableLayer& reshapeLayer = AddReshapeLayer(network, smallInputHandle, reshapedInfo); |
| 275 | |
| 276 | if (input0IsSmaller) |
| 277 | { |
| 278 | // Input0 is the "smaller" tensor, connect the reshape layer as follows: |
| 279 | // |
| 280 | // Input0 Input1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 281 | // | | |
| 282 | // Reshape | |
| 283 | // \ / |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 284 | // StartLayer |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 285 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 286 | reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(0)); |
| 287 | input1.Connect(startLayer->GetInputSlot(1)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 288 | } |
| 289 | else |
| 290 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 291 | // Input1 is the "smaller" tensor, connect the reshape layer as follows: |
| 292 | // |
| 293 | // Input0 Input1 |
| 294 | // | | |
| 295 | // | Reshape |
| 296 | // \ / |
| 297 | // StartLayer |
| 298 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 299 | input0.Connect(startLayer->GetInputSlot(0)); |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 300 | reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(1)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t& outPadHead, uint32_t& outPadTail, |
| 305 | android::nn::PaddingScheme scheme) |
| 306 | { |
| 307 | int32_t padHead; |
| 308 | int32_t padTail; |
| 309 | calculateExplicitPadding(input, stride, kernel, scheme, &padHead, &padTail); |
| 310 | outPadHead = boost::numeric_cast<uint32_t>(padHead); |
| 311 | outPadTail = boost::numeric_cast<uint32_t>(padTail); |
| 312 | } |
| 313 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 314 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 315 | |
| 316 | void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t dilation, uint32_t& outPadHead, |
| 317 | uint32_t& outPadTail, android::nn::PaddingScheme scheme) |
| 318 | { |
| 319 | int32_t padHead; |
| 320 | int32_t padTail; |
| 321 | calculateExplicitPadding(input, stride, dilation, kernel, scheme, &padHead, &padTail); |
| 322 | outPadHead = boost::numeric_cast<uint32_t>(padHead); |
| 323 | outPadTail = boost::numeric_cast<uint32_t>(padTail); |
| 324 | } |
| 325 | |
| 326 | #endif |
| 327 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 328 | Shape GetOperandShape(const V1_0::Operand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 329 | { |
| 330 | Shape shape; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 331 | shape.type = OperandType(operand.type); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 332 | shape.dimensions = operand.dimensions; |
| 333 | shape.scale = operand.scale; |
| 334 | shape.offset = operand.zeroPoint; |
| 335 | return shape; |
| 336 | } |
| 337 | |
| 338 | // ArmNN requires the bias scale to be equal to the product of the weight and input scales, which is also |
| 339 | // what AndroidNN requires. However for some of the AndroidNN tests the values don't exactly match so |
| 340 | // we accept some tolerance. We don't want to ArmNN itself to accept these inconsistencies as it is up to the user |
| 341 | // (us, in this case) to ensure they match. |
| 342 | void SanitizeBiasQuantizationScale(armnn::TensorInfo& biasInfo, |
| 343 | const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& inputInfo) |
| 344 | { |
| 345 | const float expectedBiasScale = weightInfo.GetQuantizationScale() * inputInfo.GetQuantizationScale(); |
| 346 | if (biasInfo.GetQuantizationScale() != expectedBiasScale) |
| 347 | { |
| 348 | boost::math::fpc::close_at_tolerance<float> comparer(boost::math::fpc::percent_tolerance(1.0f)); |
| 349 | if (comparer(biasInfo.GetQuantizationScale(), expectedBiasScale)) |
| 350 | { |
| 351 | ALOGW("Bias quantization scale has been modified to match input*weights"); |
| 352 | biasInfo.SetQuantizationScale(expectedBiasScale); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // 4D Tensor Permutations |
| 358 | const armnn::PermutationVector IdentityPermutation4D({ 0U, 1U, 2U, 3U }); |
| 359 | const armnn::PermutationVector NHWCToArmNN({ 0U, 2U, 3U, 1U }); |
| 360 | const armnn::PermutationVector ArmNNToNHWC({ 0U, 3U, 1U, 2U }); |
| 361 | const armnn::PermutationVector SwapDim1And2({ 0U, 2U, 1U, 3U }); |
| 362 | |
| 363 | // 3D Permutation Vectors |
| 364 | const armnn::PermutationVector IdentityPermutation3D({ 0U, 1U, 2U }); |
| 365 | const armnn::PermutationVector RotateTensorLeft({ 2U, 0U, 1U }); |
| 366 | const armnn::PermutationVector RotateTensorRight({ 1U, 2U, 0U }); |
| 367 | |
| 368 | template<typename OSlot> |
| 369 | armnn::IConnectableLayer& AddPermuteLayer(armnn::INetwork& network, OSlot& input, |
| 370 | const armnn::PermutationVector& mappings) |
| 371 | { |
| 372 | // Add swizzle layer |
| 373 | armnn::IConnectableLayer* const layer = network.AddPermuteLayer(mappings); |
| 374 | |
| 375 | BOOST_ASSERT(layer != nullptr); |
| 376 | |
| 377 | // Connect input to swizzle layer |
| 378 | input.Connect(layer->GetInputSlot(0)); |
| 379 | |
| 380 | // Setup swizzled output |
| 381 | const armnn::TensorInfo outInfo = armnnUtils::Permuted(input.GetTensorInfo(), mappings); |
| 382 | layer->GetOutputSlot(0).SetTensorInfo(outInfo); |
| 383 | |
| 384 | return *layer; |
| 385 | } |
| 386 | |
| 387 | void SwizzleIn(armnn::INetwork& network, LayerInputHandle& input, armnn::IConnectableLayer& layer, unsigned int index) |
| 388 | { |
| 389 | // Add swizzle layer |
| 390 | armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, input, NHWCToArmNN); |
| 391 | // Connect swizzled input to layer |
| 392 | swizzleLayer.GetOutputSlot(0).Connect(layer.GetInputSlot(index)); |
| 393 | } |
| 394 | |
| 395 | armnn::IConnectableLayer& DeswizzleOut(armnn::INetwork& network, armnn::IConnectableLayer& layer, unsigned int index) |
| 396 | { |
| 397 | // Add deswizzle layer |
| 398 | armnn::IConnectableLayer& deswizzleLayer = AddPermuteLayer(network, layer.GetOutputSlot(index), ArmNNToNHWC); |
| 399 | return deswizzleLayer; |
| 400 | } |
| 401 | |
| 402 | // only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly |
| 403 | armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network, |
| 404 | LayerInputHandle& input, |
| 405 | armnn::IConnectableLayer& firstLayer, |
| 406 | armnn::IConnectableLayer& lastLayer) |
| 407 | { |
| 408 | SwizzleIn(network, input, firstLayer, 0); |
| 409 | return DeswizzleOut(network, lastLayer, 0); |
| 410 | } |
| 411 | |
| 412 | // only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly |
| 413 | armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network, LayerInputHandle& input, |
| 414 | armnn::IConnectableLayer& layer) |
| 415 | { |
| 416 | return SwizzleInDeswizzleOut(network, input, layer, layer); |
| 417 | } |
| 418 | |
| 419 | bool ValidateConcatOutputShape(const std::vector<armnn::TensorShape> & inputShapes, |
| 420 | const armnn::TensorShape & outputShape, |
| 421 | uint32_t concatDim) |
| 422 | { |
| 423 | // Validate the output shape is correct given the input shapes (which have just been validated) |
| 424 | unsigned int numDimensions = inputShapes[0].GetNumDimensions(); |
| 425 | if (outputShape.GetNumDimensions() != numDimensions) |
| 426 | { |
| 427 | return Fail("%s: Output shape has wrong number of dimensions", __func__); |
| 428 | } |
| 429 | |
| 430 | unsigned int outputSizeAlongConcatenatedDimension = 0; |
| 431 | for (unsigned int i = 0; i < inputShapes.size(); i++) |
| 432 | { |
| 433 | outputSizeAlongConcatenatedDimension += inputShapes[i][concatDim]; |
| 434 | } |
| 435 | |
| 436 | for (unsigned int i = 0; i < numDimensions; ++i) |
| 437 | { |
| 438 | if (i == concatDim) |
| 439 | { |
| 440 | if (outputShape[i] != outputSizeAlongConcatenatedDimension) |
| 441 | { |
| 442 | return Fail( |
| 443 | "%s: Invalid output shape for dimension %d (%d != %d)", |
| 444 | __func__, |
| 445 | i, |
| 446 | outputShape[i], |
| 447 | outputSizeAlongConcatenatedDimension); |
| 448 | } |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | if (outputShape[i] != inputShapes[0][i]) |
| 453 | { |
| 454 | return Fail("%s: Invalid output shape", __func__); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | bool RequiresReshape(armnn::TensorShape & inputShape) |
| 463 | { |
| 464 | return inputShape.GetNumDimensions() < 3; |
| 465 | } |
| 466 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 467 | void SwizzleInputs(armnn::INetwork& network, |
| 468 | std::vector<LayerInputHandle>& inputs, |
| 469 | std::vector<armnn::TensorShape>& inputShapes, |
| 470 | const armnn::PermutationVector& mapping) |
| 471 | { |
| 472 | if (!mapping.IsEqual(IdentityPermutation4D)) |
| 473 | { |
| 474 | size_t nInputs = inputs.size(); |
| 475 | for (size_t i=0; i<nInputs; ++i) |
| 476 | { |
| 477 | // add swizzle layer |
| 478 | armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, inputs[i], mapping); |
| 479 | auto& outputSlot = swizzleLayer.GetOutputSlot(0); |
| 480 | auto& outputInfo = outputSlot.GetTensorInfo(); |
| 481 | // replace inputs with the swizzled ones |
| 482 | inputs[i] = LayerInputHandle(true, &outputSlot, outputInfo); |
| 483 | inputShapes[i] = inputs[i].GetTensorInfo().GetShape(); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 488 | bool CreateConcatPermutationParameters(const unsigned int numberOfDimensions, |
| 489 | int32_t & concatDimension, |
| 490 | std::pair<armnn::PermutationVector, armnn::PermutationVector> & permutationPair) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 491 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 492 | bool needPermute = false; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 493 | BOOST_ASSERT(numberOfDimensions >= 3); |
| 494 | |
| 495 | // ArmNN uses Compute Library subtensors to perform concatenation |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 496 | // This only works when concatenating along dimension 0, 1 or 3 for a 4-D tensor, |
| 497 | // or along dimension 0 or 2 for a 3-D tensor. |
| 498 | if (numberOfDimensions == 4 && concatDimension == 2) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 499 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 500 | concatDimension = 1; |
| 501 | permutationPair = std::make_pair(SwapDim1And2, SwapDim1And2); |
| 502 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 503 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 504 | else if (numberOfDimensions == 3 && concatDimension == 1) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 505 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 506 | concatDimension = 0; |
| 507 | permutationPair = std::make_pair(RotateTensorLeft, RotateTensorRight); |
| 508 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 509 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 510 | return needPermute; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | } // anonymous namespace |
| 514 | |
| 515 | namespace armnn_driver |
| 516 | { |
| 517 | |
| 518 | //// Creates an ArmNN activation layer and connects it to the given layer, if the |
| 519 | //// passed in AndroidNN activation function requires so. |
| 520 | //// @return The end layer of the sequence of layers built for the given AndroidNN |
| 521 | //// activation function or nullptr if an error occurred (e.g. unsupported activation). |
| 522 | //// Note that the end layer matches the input layer if no activation is required |
| 523 | //// (the sequence of layers has length 1). |
| 524 | armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo, |
| 525 | ActivationFn activation, |
| 526 | armnn::IConnectableLayer* prevLayer, |
| 527 | ConversionData& data); |
| 528 | |
| 529 | } // namespace armnn_driver |
| 530 | |
| 531 | /// |
| 532 | /// Utility templates |
| 533 | /// |
| 534 | |
| 535 | namespace armnn_driver |
| 536 | { |
| 537 | |
| 538 | using namespace android::nn; |
| 539 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 540 | template<typename HalPolicy, |
| 541 | typename HalOperand = typename HalPolicy::Operand, |
| 542 | typename HalOperation = typename HalPolicy::Operation, |
| 543 | typename HalModel = typename HalPolicy::Model> |
| 544 | const HalOperand* GetInputOperand(const HalOperation& operation, |
| 545 | uint32_t inputIndex, |
| 546 | const HalModel& model, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 547 | bool failOnIndexOutOfBounds = true) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 548 | { |
| 549 | if (inputIndex >= operation.inputs.size()) |
| 550 | { |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 551 | if (failOnIndexOutOfBounds) |
| 552 | { |
| 553 | Fail("%s: invalid input index: %i out of %i", __func__, inputIndex, operation.inputs.size()); |
| 554 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 555 | return nullptr; |
| 556 | } |
| 557 | |
| 558 | BOOST_ASSERT(operation.inputs[inputIndex] < model.operands.size()); // Model should have been validated beforehand |
| 559 | return &model.operands[operation.inputs[inputIndex]]; |
| 560 | } |
| 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* GetOutputOperand(const HalOperation& operation, |
| 567 | uint32_t outputIndex, |
| 568 | const HalModel& model) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 569 | { |
| 570 | if (outputIndex >= operation.outputs.size()) |
| 571 | { |
| 572 | Fail("%s: invalid output index: %i out of %i", __func__, outputIndex, operation.outputs.size()); |
| 573 | return nullptr; |
| 574 | } |
| 575 | |
| 576 | // Model should have been validated beforehand |
| 577 | BOOST_ASSERT(operation.outputs[outputIndex] < model.operands.size()); |
| 578 | |
| 579 | return &model.operands[operation.outputs[outputIndex]]; |
| 580 | } |
| 581 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 582 | template<typename HalPolicy, |
| 583 | typename HalOperand = typename HalPolicy::Operand, |
| 584 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 585 | const void* GetOperandValueReadOnlyAddress(const HalOperand& operand, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 586 | const HalModel& model, |
| 587 | const ConversionData& data, |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 588 | bool optional = false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 589 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 590 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 591 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 592 | const void* valueStart = nullptr; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 593 | switch (operand.lifetime) |
| 594 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 595 | case HalOperandLifeTime::CONSTANT_COPY: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 596 | { |
| 597 | // Constant found in model.operandValues |
| 598 | valueStart = &model.operandValues[operand.location.offset]; |
| 599 | break; |
| 600 | } |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 601 | case HalOperandLifeTime::CONSTANT_REFERENCE: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 602 | { |
| 603 | // Constant specified via a Memory object |
| 604 | valueStart = GetMemoryFromPool(operand.location, data.m_MemPools); |
| 605 | break; |
| 606 | } |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 607 | case HalOperandLifeTime::NO_VALUE: |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 608 | { |
| 609 | // An optional input tensor with no values is not an error so should not register as a fail |
| 610 | if (optional) |
| 611 | { |
| 612 | valueStart = nullptr; |
| 613 | break; |
| 614 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 615 | [[fallthrough]]; |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 616 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 617 | default: |
| 618 | { |
| 619 | // Unsupported/invalid (e.g. can't get value of an input to the model) |
| 620 | Fail("%s: unsupported/invalid operand lifetime: %s", |
| 621 | __func__, toString(operand.lifetime).c_str()); |
| 622 | valueStart = nullptr; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return valueStart; |
| 627 | } |
| 628 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 629 | template<typename HalPolicy, |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 630 | typename HalOperation = typename HalPolicy::Operation, |
| 631 | typename HalModel = typename HalPolicy::Model, |
| 632 | typename HalOperandType = typename HalPolicy::OperandType> |
| 633 | bool GetOperandType(const HalOperation& operation, |
| 634 | uint32_t inputIndex, |
| 635 | const HalModel& model, |
| 636 | HalOperandType& type) |
| 637 | { |
| 638 | using HalOperand = typename HalPolicy::Operand; |
| 639 | |
| 640 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 641 | if (!operand) |
| 642 | { |
| 643 | return Fail("%s: invalid input operand at index %i", __func__, inputIndex); |
| 644 | } |
| 645 | |
| 646 | type = operand->type; |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 651 | typename HalOperand = typename HalPolicy::Operand, |
| 652 | typename HalModel = typename HalPolicy::Model> |
| 653 | ConstTensorPin ConvertOperandToConstTensorPin(const HalOperand& operand, |
| 654 | const HalModel& model, |
| 655 | const ConversionData& data, |
| 656 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 657 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 658 | bool optional = false) |
| 659 | { |
| 660 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 661 | |
| 662 | if (!IsOperandTypeSupportedForTensors(operand.type)) |
| 663 | { |
| 664 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand.type).c_str()); |
| 665 | return ConstTensorPin(); |
| 666 | } |
| 667 | |
| 668 | if (!optional && |
| 669 | operand.lifetime != HalOperandLifeTime::CONSTANT_COPY && |
| 670 | operand.lifetime != HalOperandLifeTime::CONSTANT_REFERENCE && |
| 671 | operand.lifetime != HalOperandLifeTime::NO_VALUE) |
| 672 | { |
| 673 | Fail("%s: invalid operand lifetime: %s", __func__, toString(operand.lifetime).c_str()); |
| 674 | return ConstTensorPin(); |
| 675 | } |
| 676 | |
| 677 | const void* const valueStart = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data, optional); |
| 678 | if (!valueStart) |
| 679 | { |
| 680 | if (optional) |
| 681 | { |
| 682 | // optional tensor with no values is not really an error; return it as invalid, but marked as optional |
| 683 | return ConstTensorPin(true); |
| 684 | } |
| 685 | // mandatory tensor with no values |
| 686 | Fail("%s: failed to get operand address", __func__); |
| 687 | return ConstTensorPin(); |
| 688 | } |
| 689 | |
| 690 | armnn::TensorInfo tensorInfo = GetTensorInfoForOperand(operand); |
| 691 | if (overrideTensorShape != nullptr) |
| 692 | { |
| 693 | tensorInfo.SetShape(*overrideTensorShape); |
| 694 | } |
| 695 | return ConstTensorPin(tensorInfo, valueStart, operand.location.length, dimensionMappings); |
| 696 | } |
| 697 | |
| 698 | template<typename HalPolicy, |
| 699 | typename HalOperation = typename HalPolicy::Operation, |
| 700 | typename HalModel = typename HalPolicy::Model> |
| 701 | ConstTensorPin ConvertOperationInputToConstTensorPin(const HalOperation& operation, |
| 702 | uint32_t inputIndex, |
| 703 | const HalModel& model, |
| 704 | const ConversionData& data, |
| 705 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 706 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 707 | bool optional = false) |
| 708 | { |
| 709 | using HalOperand = typename HalPolicy::Operand; |
| 710 | |
| 711 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
| 712 | if (!operand) |
| 713 | { |
| 714 | Fail("%s: failed to get input operand: index=%u", __func__, inputIndex); |
| 715 | return ConstTensorPin(); |
| 716 | } |
| 717 | return ConvertOperandToConstTensorPin<HalPolicy>(*operand, |
| 718 | model, |
| 719 | data, |
| 720 | dimensionMappings, |
| 721 | overrideTensorShape, |
| 722 | optional); |
| 723 | } |
| 724 | |
| 725 | template<typename HalPolicy, |
| 726 | typename OutputType, |
| 727 | typename HalOperandType = typename HalPolicy::OperandType, |
| 728 | typename HalOperation = typename HalPolicy::Operation, |
| 729 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 730 | bool GetInputScalar(const HalOperation& operation, |
| 731 | uint32_t inputIndex, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 732 | HalOperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 733 | OutputType& outValue, |
| 734 | const HalModel& model, |
| 735 | const ConversionData& data) |
| 736 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 737 | using HalOperand = typename HalPolicy::Operand; |
| 738 | |
| 739 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 740 | if (!operand) |
| 741 | { |
| 742 | return Fail("%s: invalid input operand at index %i", __func__, inputIndex); |
| 743 | } |
| 744 | |
| 745 | if (operand->type != type) |
| 746 | { |
| 747 | return Fail("%s: unexpected operand type: %s (should be %s)", |
| 748 | __func__, toString(operand->type).c_str(), toString(type).c_str()); |
| 749 | } |
| 750 | |
| 751 | if (operand->location.length != sizeof(OutputType)) |
| 752 | { |
| 753 | return Fail("%s: incorrect operand location length: %i (should be %i)", |
| 754 | __func__, operand->location.length, sizeof(OutputType)); |
| 755 | } |
| 756 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 757 | const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 758 | if (!valueAddress) |
| 759 | { |
| 760 | return Fail("%s: failed to get address for operand", __func__); |
| 761 | } |
| 762 | |
| 763 | outValue = *(static_cast<const OutputType*>(valueAddress)); |
| 764 | return true; |
| 765 | } |
| 766 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 767 | template<typename HalPolicy, |
| 768 | typename HalOperation = typename HalPolicy::Operation, |
| 769 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 770 | bool GetInputInt32(const HalOperation& operation, |
| 771 | uint32_t inputIndex, |
| 772 | int32_t& outValue, |
| 773 | const HalModel& model, |
| 774 | const ConversionData& data) |
| 775 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 776 | return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::INT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 779 | template<typename HalPolicy, |
| 780 | typename HalOperation = typename HalPolicy::Operation, |
| 781 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 782 | bool GetInputFloat32(const HalOperation& operation, |
| 783 | uint32_t inputIndex, |
| 784 | float& outValue, |
| 785 | const HalModel& model, |
| 786 | const ConversionData& data) |
| 787 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 788 | return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::FLOAT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 791 | template<typename HalPolicy, |
| 792 | typename HalOperation = typename HalPolicy::Operation, |
| 793 | typename HalOperandType = typename HalPolicy::OperandType, |
| 794 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 795 | bool GetInputActivationFunctionImpl(const HalOperation& operation, |
| 796 | uint32_t inputIndex, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 797 | HalOperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 798 | ActivationFn& outActivationFunction, |
| 799 | const HalModel& model, |
| 800 | const ConversionData& data) |
| 801 | { |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 802 | if (type != HalOperandType::INT32 && type != HalOperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 803 | { |
| 804 | return Fail("%s: unexpected operand type: %s (should be %s or %s)", |
| 805 | __func__, |
| 806 | toString(type).c_str(), |
| 807 | toString(OperandType::INT32).c_str(), |
| 808 | toString(OperandType::TENSOR_INT32).c_str()); |
| 809 | } |
| 810 | |
| 811 | int32_t activationFunctionAsInt; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 812 | if (!GetInputScalar<HalPolicy>(operation, inputIndex, type, activationFunctionAsInt, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 813 | { |
| 814 | return Fail("%s: failed to get activation input value", __func__); |
| 815 | } |
| 816 | outActivationFunction = static_cast<ActivationFn>(activationFunctionAsInt); |
| 817 | return true; |
| 818 | } |
| 819 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 820 | template<typename HalPolicy, |
| 821 | typename HalOperation = typename HalPolicy::Operation, |
| 822 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 823 | bool GetInputActivationFunction(const HalOperation& operation, |
| 824 | uint32_t inputIndex, |
| 825 | ActivationFn& outActivationFunction, |
| 826 | const HalModel& model, |
| 827 | const ConversionData& data) |
| 828 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 829 | return GetInputActivationFunctionImpl<HalPolicy>(operation, |
| 830 | inputIndex, |
| 831 | HalPolicy::OperandType::INT32, |
| 832 | outActivationFunction, |
| 833 | model, |
| 834 | data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 837 | template<typename HalPolicy, |
| 838 | typename HalOperation = typename HalPolicy::Operation, |
| 839 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 840 | bool GetInputActivationFunctionFromTensor(const HalOperation& operation, |
| 841 | uint32_t inputIndex, |
| 842 | ActivationFn& outActivationFunction, |
| 843 | const HalModel& model, |
| 844 | const ConversionData& data) |
| 845 | { |
| 846 | // This only accepts a 1-D tensor of size 1 |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 847 | return GetInputActivationFunctionImpl<HalPolicy>(operation, |
| 848 | inputIndex, |
| 849 | HalPolicy::OperandType::INT32, |
| 850 | outActivationFunction, |
| 851 | model, |
| 852 | data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 856 | template<typename HalPolicy, |
| 857 | typename HalOperation = typename HalPolicy::Operation, |
| 858 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 859 | bool GetOptionalInputActivation(const HalOperation& operation, |
| 860 | uint32_t inputIndex, |
| 861 | ActivationFn& activationFunction, |
| 862 | const HalModel& model, |
| 863 | const ConversionData& data) |
| 864 | { |
| 865 | if (operation.inputs.size() <= inputIndex) |
| 866 | { |
| 867 | activationFunction = ActivationFn::kActivationNone; |
| 868 | } |
| 869 | else |
| 870 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 871 | if (!GetInputActivationFunction<HalPolicy>(operation, inputIndex, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 872 | { |
| 873 | return Fail("%s: Operation has invalid inputs", __func__); |
| 874 | } |
| 875 | } |
| 876 | return true; |
| 877 | } |
| 878 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 879 | template<typename HalPolicy, |
| 880 | typename ConvolutionDescriptor, |
| 881 | typename HalOperation = typename HalPolicy::Operation, |
| 882 | typename HalModel = typename HalPolicy::Model> |
Aron Virginas-Tar | 07c7c9a | 2019-06-12 14:03:35 +0100 | [diff] [blame] | 883 | bool GetOptionalConvolutionDilationParams(const HalOperation& operation, |
| 884 | uint32_t dilationXIndex, |
| 885 | ConvolutionDescriptor& descriptor, |
| 886 | const HalModel& model, |
| 887 | const ConversionData& data) |
| 888 | { |
| 889 | bool success = true; |
| 890 | if (operation.inputs.size() >= dilationXIndex + 2) |
| 891 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 892 | success &= GetInputScalar<HalPolicy>(operation, |
| 893 | dilationXIndex, |
| 894 | HalPolicy::OperandType::INT32, |
| 895 | descriptor.m_DilationX, |
| 896 | model, |
| 897 | data); |
| 898 | success &= GetInputScalar<HalPolicy>(operation, |
| 899 | dilationXIndex + 1, |
| 900 | HalPolicy::OperandType::INT32, |
| 901 | descriptor.m_DilationY, |
| 902 | model, |
| 903 | data); |
Aron Virginas-Tar | 07c7c9a | 2019-06-12 14:03:35 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | return success; |
| 907 | } |
| 908 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 909 | template<typename HalPolicy, |
| 910 | typename HalOperand = typename HalPolicy::Operand, |
| 911 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 912 | bool GetTensorInt32Values(const HalOperand& operand, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 913 | std::vector<int32_t>& outValues, |
| 914 | const HalModel& model, |
| 915 | const ConversionData& data) |
| 916 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 917 | if (operand.type != HalPolicy::OperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 918 | { |
| 919 | return Fail("%s: invalid operand type: %s", __func__, toString(operand.type).c_str()); |
| 920 | } |
| 921 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 922 | const void* startAddress = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 923 | if (!startAddress) |
| 924 | { |
| 925 | return Fail("%s: failed to get operand address", __func__, operand.type); |
| 926 | } |
| 927 | |
| 928 | // Check number of bytes is sensible |
| 929 | const uint32_t numBytes = operand.location.length; |
| 930 | if (numBytes % sizeof(int32_t) != 0) |
| 931 | { |
| 932 | return Fail("%s: invalid number of bytes: %i, expected to be a multiple of %i", |
| 933 | __func__, numBytes, sizeof(int32_t)); |
| 934 | } |
| 935 | |
| 936 | outValues.resize(numBytes / sizeof(int32_t)); |
| 937 | memcpy(outValues.data(), startAddress, numBytes); |
| 938 | return true; |
| 939 | } |
| 940 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 941 | template<typename HalPolicy, |
| 942 | typename HalOperation = typename HalPolicy::Operation, |
| 943 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 944 | bool GetInputPaddingScheme(const HalOperation& operation, |
| 945 | uint32_t inputIndex, |
| 946 | PaddingScheme& outPaddingScheme, |
| 947 | const HalModel& model, |
| 948 | const ConversionData& data) |
| 949 | { |
| 950 | int32_t paddingSchemeAsInt; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 951 | if (!GetInputInt32<HalPolicy>(operation, inputIndex, paddingSchemeAsInt, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 952 | { |
| 953 | return Fail("%s: failed to get padding scheme input value", __func__); |
| 954 | } |
| 955 | |
| 956 | outPaddingScheme = static_cast<android::nn::PaddingScheme>(paddingSchemeAsInt); |
| 957 | return true; |
| 958 | } |
| 959 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 960 | template<typename HalPolicy, |
| 961 | typename HalOperation = typename HalPolicy::Operation, |
| 962 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 963 | LayerInputHandle ConvertToLayerInputHandle(const HalOperation& operation, |
| 964 | uint32_t inputIndex, |
| 965 | const HalModel& model, |
| 966 | ConversionData& data) |
| 967 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 968 | using HalOperand = typename HalPolicy::Operand; |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 969 | using HalOperandType = typename HalPolicy::OperandType; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 970 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 971 | |
| 972 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 973 | if (!operand) |
| 974 | { |
| 975 | Fail("%s: failed to get input operand %i", __func__, inputIndex); |
| 976 | return LayerInputHandle(); |
| 977 | } |
| 978 | |
| 979 | if (!IsOperandTypeSupportedForTensors(operand->type)) |
| 980 | { |
| 981 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand->type).c_str()); |
| 982 | return LayerInputHandle(); |
| 983 | } |
| 984 | |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 985 | try |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 986 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 987 | armnn::TensorInfo operandTensorInfo = GetTensorInfoForOperand(*operand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 988 | if (IsDynamicTensor(operandTensorInfo)) |
| 989 | { |
| 990 | Fail("%s: dynamic input tensors are not supported", __func__); |
| 991 | return LayerInputHandle(); |
| 992 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 993 | |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 994 | switch (operand->lifetime) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 995 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 996 | case HalOperandLifeTime::MODEL_INPUT: |
Aron Virginas-Tar | 000117b | 2019-07-25 16:24:49 +0100 | [diff] [blame] | 997 | { |
| 998 | // NOTE: We must check whether we can support the input tensor on at least one |
| 999 | // of the provided backends; otherwise we cannot convert the operation |
| 1000 | bool isInputSupported = false; |
| 1001 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1002 | IsInputSupported, |
| 1003 | data.m_Backends, |
| 1004 | isInputSupported, |
| 1005 | operandTensorInfo); |
| 1006 | |
| 1007 | if (!isInputSupported) |
| 1008 | { |
| 1009 | Fail("%s: unsupported input tensor", __func__); |
| 1010 | return LayerInputHandle(); |
| 1011 | } |
| 1012 | |
| 1013 | BOOST_FALLTHROUGH; // intentional fallthrough |
| 1014 | } |
| 1015 | case HalOperandLifeTime::TEMPORARY_VARIABLE: // intentional fallthrough |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1016 | case HalOperandLifeTime::MODEL_OUTPUT: |
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 | // The tensor is either an operand internal to the model, or a model input. |
| 1019 | // It can be associated with an ArmNN output slot for an existing layer. |
| 1020 | |
| 1021 | // m_OutputSlotForOperand[...] can be nullptr if the previous layer could not be converted |
| 1022 | const uint32_t operandIndex = operation.inputs[inputIndex]; |
| 1023 | return LayerInputHandle(true, data.m_OutputSlotForOperand[operandIndex], operandTensorInfo); |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1024 | } |
Aron Virginas-Tar | 000117b | 2019-07-25 16:24:49 +0100 | [diff] [blame] | 1025 | case HalOperandLifeTime::CONSTANT_COPY: // intentional fallthrough |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1026 | case HalOperandLifeTime::CONSTANT_REFERENCE: |
| 1027 | { |
| 1028 | // The tensor has an already known constant value, and can be converted into an ArmNN Constant layer. |
| 1029 | ConstTensorPin tensorPin = ConvertOperandToConstTensorPin<HalPolicy>(*operand, model, data); |
| 1030 | if (tensorPin.IsValid()) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1031 | { |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1032 | bool isSupported = false; |
| 1033 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1034 | IsConstantSupported, |
| 1035 | data.m_Backends, |
| 1036 | isSupported, |
| 1037 | tensorPin.GetConstTensor().GetInfo()); |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1038 | if (isSupported) |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1039 | { |
| 1040 | return LayerInputHandle(); |
| 1041 | } |
| 1042 | |
| 1043 | armnn::IConnectableLayer* constantLayer = |
| 1044 | data.m_Network->AddConstantLayer(tensorPin.GetConstTensor()); |
| 1045 | armnn::IOutputSlot& outputSlot = constantLayer->GetOutputSlot(0); |
| 1046 | outputSlot.SetTensorInfo(tensorPin.GetConstTensor().GetInfo()); |
| 1047 | |
| 1048 | return LayerInputHandle(true, &outputSlot, operandTensorInfo); |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | Fail("%s: invalid operand tensor", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1053 | return LayerInputHandle(); |
| 1054 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1055 | break; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1056 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1057 | default: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1058 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1059 | // Unsupported lifetime for an input tensor |
| 1060 | Fail("%s: unsupported lifetime for input tensor: %s", |
| 1061 | __func__, toString(operand->lifetime).c_str()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1062 | return LayerInputHandle(); |
| 1063 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1064 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 1065 | } |
| 1066 | catch (UnsupportedOperand<HalOperandType>& e) |
| 1067 | { |
| 1068 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 1069 | return LayerInputHandle(); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1073 | template<typename HalPolicy, |
| 1074 | typename HalOperation = typename HalPolicy::Operation, |
| 1075 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1076 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 1077 | uint32_t operationOutputIndex, |
| 1078 | armnn::IConnectableLayer& layer, |
| 1079 | uint32_t layerOutputIndex, |
| 1080 | const HalModel& model, |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1081 | ConversionData& data) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1082 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1083 | using HalOperand = typename HalPolicy::Operand; |
| 1084 | |
| 1085 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, operationOutputIndex, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1086 | if ((outputOperand == nullptr) || (operationOutputIndex >= layer.GetNumOutputSlots())) |
| 1087 | { |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | armnn::IOutputSlot& outputSlot = layer.GetOutputSlot(layerOutputIndex); |
| 1092 | |
| 1093 | const uint32_t operandIndex = operation.outputs[operationOutputIndex]; |
| 1094 | data.m_OutputSlotForOperand[operandIndex] = &outputSlot; |
| 1095 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1096 | outputSlot.SetTensorInfo(GetTensorInfoForOperand(*outputOperand)); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1097 | |
| 1098 | return true; |
| 1099 | } |
| 1100 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1101 | template<typename HalPolicy, |
| 1102 | typename HalOperation = typename HalPolicy::Operation, |
| 1103 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1104 | armnn::DataLayout OptionalDataLayout(const HalOperation& operation, |
| 1105 | uint32_t inputIndex, |
| 1106 | const HalModel& model, |
| 1107 | ConversionData& data) |
| 1108 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1109 | using HalOperand = typename HalPolicy::Operand; |
| 1110 | |
| 1111 | const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1112 | if (!operand) |
| 1113 | { |
| 1114 | return armnn::DataLayout::NHWC; |
| 1115 | } |
| 1116 | |
| 1117 | if (!IsBool(*operand)) |
| 1118 | { |
| 1119 | return armnn::DataLayout::NHWC; |
| 1120 | } |
| 1121 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1122 | const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1123 | if (!valueAddress) |
| 1124 | { |
| 1125 | return armnn::DataLayout::NHWC; |
| 1126 | } |
| 1127 | |
| 1128 | if (*(static_cast<const bool*>(valueAddress))) |
| 1129 | { |
| 1130 | return armnn::DataLayout::NCHW; |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | return armnn::DataLayout::NHWC; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1138 | template<typename HalPolicy, |
| 1139 | typename HalOperation = typename HalPolicy::Operation, |
| 1140 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1141 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 1142 | uint32_t outputIndex, |
| 1143 | armnn::IConnectableLayer& layer, |
| 1144 | const HalModel& model, |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1145 | ConversionData& data) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1146 | { |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 1147 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, |
| 1148 | outputIndex, |
| 1149 | layer, |
| 1150 | outputIndex, |
| 1151 | model, |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1152 | data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1155 | template<typename HalPolicy, |
| 1156 | typename HalOperation = typename HalPolicy::Operation, |
| 1157 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1158 | bool ConvertToActivation(const HalOperation& operation, |
| 1159 | const char* operationName, |
| 1160 | const armnn::ActivationDescriptor& activationDesc, |
| 1161 | const HalModel& model, |
| 1162 | ConversionData& data) |
| 1163 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1164 | using HalOperand = typename HalPolicy::Operand; |
| 1165 | |
| 1166 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1167 | if (!input.IsValid()) |
| 1168 | { |
| 1169 | return Fail("%s: Input 0 is invalid", operationName); |
| 1170 | } |
| 1171 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1172 | const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1173 | if (!outputOperand) |
| 1174 | { |
| 1175 | return false; |
| 1176 | } |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1177 | |
| 1178 | const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand); |
Sadik Armagan | 2050c23 | 2019-07-23 16:59:58 +0100 | [diff] [blame] | 1179 | if (IsDynamicTensor(outInfo)) |
| 1180 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1181 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Sadik Armagan | 2050c23 | 2019-07-23 16:59:58 +0100 | [diff] [blame] | 1182 | } |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1183 | |
| 1184 | bool isSupported = false; |
| 1185 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1186 | IsActivationSupported, |
| 1187 | data.m_Backends, |
| 1188 | isSupported, |
| 1189 | input.GetTensorInfo(), |
| 1190 | outInfo, |
| 1191 | activationDesc); |
| 1192 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1193 | { |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | armnn::IConnectableLayer* layer = data.m_Network->AddActivationLayer(activationDesc); |
| 1198 | BOOST_ASSERT(layer != nullptr); |
| 1199 | input.Connect(layer->GetInputSlot(0)); |
| 1200 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1201 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1204 | template<typename HalPolicy, |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1205 | typename HalOperation = typename HalPolicy::Operation, |
| 1206 | typename HalModel = typename HalPolicy::Model> |
| 1207 | bool ConvertReLu(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1208 | { |
| 1209 | armnn::ActivationDescriptor desc; |
| 1210 | desc.m_Function = armnn::ActivationFunction::ReLu; |
| 1211 | |
| 1212 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1213 | } |
| 1214 | |
| 1215 | template<typename HalPolicy, |
| 1216 | typename HalOperation = typename HalPolicy::Operation, |
| 1217 | typename HalModel = typename HalPolicy::Model> |
| 1218 | bool ConvertReLu1(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1219 | { |
| 1220 | armnn::ActivationDescriptor desc; |
| 1221 | desc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 1222 | desc.m_A = 1.0f; |
| 1223 | desc.m_B = -1.0f; |
| 1224 | |
| 1225 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1226 | } |
| 1227 | |
| 1228 | template<typename HalPolicy, |
| 1229 | typename HalOperation = typename HalPolicy::Operation, |
| 1230 | typename HalModel = typename HalPolicy::Model> |
| 1231 | bool ConvertReLu6(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1232 | { |
| 1233 | armnn::ActivationDescriptor desc; |
| 1234 | desc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 1235 | desc.m_A = 6.0f; |
| 1236 | |
| 1237 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1238 | } |
| 1239 | |
| 1240 | template<typename HalPolicy, |
| 1241 | typename HalOperation = typename HalPolicy::Operation, |
| 1242 | typename HalModel = typename HalPolicy::Model> |
| 1243 | bool ConvertTanH(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1244 | { |
| 1245 | armnn::ActivationDescriptor desc; |
| 1246 | desc.m_Function = armnn::ActivationFunction::TanH; |
| 1247 | desc.m_A = 1.0f; // android nn does not support tanH parameters |
| 1248 | desc.m_B = 1.0f; // set to 1.0f for unity scaling |
| 1249 | |
| 1250 | return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data); |
| 1251 | } |
| 1252 | |
| 1253 | template<typename HalPolicy, |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1254 | typename HalOperation = typename HalPolicy::Operation, |
| 1255 | typename HalModel = typename HalPolicy::Model> |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 1256 | bool ConvertPaddings(const HalOperation& operation, |
| 1257 | const HalModel& model, |
| 1258 | ConversionData& data, |
| 1259 | unsigned int rank, |
| 1260 | armnn::PadDescriptor& padDescriptor) |
| 1261 | { |
| 1262 | using HalOperand = typename HalPolicy::Operand; |
| 1263 | |
| 1264 | const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 1265 | if (!paddingsOperand) |
| 1266 | { |
| 1267 | return Fail("%s: Could not read paddings operand", __func__); |
| 1268 | } |
| 1269 | |
| 1270 | armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand); |
| 1271 | if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != rank * 2) |
| 1272 | { |
| 1273 | return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, rank); |
| 1274 | } |
| 1275 | |
| 1276 | std::vector<int32_t> paddings; |
| 1277 | GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data); |
| 1278 | |
| 1279 | // add padding for each dimension of input tensor. |
| 1280 | for (unsigned int i = 0; i < paddings.size() - 1; i += 2) |
| 1281 | { |
| 1282 | int paddingBeforeInput = paddings[i]; |
| 1283 | int paddingAfterInput = paddings[i + 1]; |
| 1284 | |
| 1285 | if (paddingBeforeInput < 0 || paddingAfterInput < 0) |
| 1286 | { |
| 1287 | return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__); |
| 1288 | } |
| 1289 | |
| 1290 | padDescriptor.m_PadList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput); |
| 1291 | } |
| 1292 | |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
| 1296 | template<typename HalPolicy, |
| 1297 | typename HalOperation = typename HalPolicy::Operation, |
| 1298 | typename HalModel = typename HalPolicy::Model> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1299 | bool ConvertPooling2d(const HalOperation& operation, |
| 1300 | const char* operationName, |
| 1301 | armnn::PoolingAlgorithm poolType, |
| 1302 | const HalModel& model, |
| 1303 | ConversionData& data) |
| 1304 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1305 | using HalOperand = typename HalPolicy::Operand; |
| 1306 | using HalOperandType = typename HalPolicy::OperandType; |
| 1307 | |
| 1308 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1309 | if (!input.IsValid()) |
| 1310 | { |
| 1311 | return Fail("%s: Could not read input 0", operationName); |
| 1312 | } |
| 1313 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1314 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1315 | if (!output) |
| 1316 | { |
| 1317 | return Fail("%s: Could not read output 0", __func__); |
| 1318 | } |
| 1319 | |
| 1320 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1321 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1322 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1323 | if (IsDynamicTensor(outputInfo)) |
| 1324 | { |
| 1325 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1326 | } |
| 1327 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1328 | armnn::Pooling2dDescriptor desc; |
| 1329 | desc.m_PoolType = poolType; |
| 1330 | desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor; |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1331 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1332 | |
| 1333 | ActivationFn activation; |
| 1334 | |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 1335 | auto inputSize = operation.inputs.size(); |
| 1336 | |
| 1337 | if (inputSize >= 10) |
| 1338 | { |
| 1339 | // one input, 9 parameters (padding l r t b, stridex, stridey, width, height, activation type) |
| 1340 | if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 1341 | !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 1342 | !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 1343 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 1344 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1345 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_StrideY, model, data) || |
| 1346 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_PoolWidth, model, data) || |
| 1347 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_PoolHeight, model, data) || |
| 1348 | !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data)) |
| 1349 | { |
| 1350 | return Fail("%s: Operation has invalid inputs", operationName); |
| 1351 | } |
| 1352 | |
| 1353 | if (Is12Operand(*output)) |
| 1354 | { |
| 1355 | desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 10, model, data); |
| 1356 | } |
| 1357 | } |
| 1358 | else |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1359 | { |
| 1360 | // one input, 6 parameters (padding, stridex, stridey, width, height, activation type) |
| 1361 | android::nn::PaddingScheme scheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1362 | if (!GetInputPaddingScheme<HalPolicy>(operation, 1, scheme, model, data) || |
| 1363 | !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1364 | !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_StrideY, model, data) || |
| 1365 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PoolWidth, model, data) || |
| 1366 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PoolHeight, model, data) || |
| 1367 | !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1368 | { |
| 1369 | return Fail("%s: Operation has invalid inputs", operationName); |
| 1370 | } |
| 1371 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1372 | const unsigned int inputWidth = inputInfo.GetShape()[2]; |
| 1373 | const unsigned int inputHeight = inputInfo.GetShape()[1]; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1374 | |
| 1375 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, scheme); |
| 1376 | 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] | 1377 | |
| 1378 | if (Is12Operand(*output)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1379 | { |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 1380 | desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 7, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1384 | bool isSupported = false; |
| 1385 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1386 | IsPooling2dSupported, |
| 1387 | data.m_Backends, |
| 1388 | isSupported, |
| 1389 | inputInfo, |
| 1390 | outputInfo, |
| 1391 | desc); |
| 1392 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1393 | { |
Éanna Ó Catháin | 3d1059c | 2018-10-11 15:53:04 +0100 | [diff] [blame] | 1394 | return false; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1395 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1396 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1397 | armnn::IConnectableLayer* pooling2dLayer = data.m_Network->AddPooling2dLayer(desc); |
| 1398 | if (!pooling2dLayer) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1399 | { |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1400 | return Fail("%s: AddPooling2dLayer failed", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1401 | } |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1402 | |
| 1403 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, pooling2dLayer, data); |
| 1404 | if (!endLayer) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1405 | { |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1406 | return Fail("%s: ProcessActivation failed", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1407 | } |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1408 | |
| 1409 | input.Connect(pooling2dLayer->GetInputSlot(0)); |
| 1410 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1411 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1414 | template<typename HalPolicy, |
| 1415 | typename HalOperation = typename HalPolicy::Operation, |
| 1416 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1417 | bool ConvertConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1418 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1419 | using HalOperand = typename HalPolicy::Operand; |
| 1420 | using HalOperandType = typename HalPolicy::OperandType; |
| 1421 | |
| 1422 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1423 | if (!input.IsValid()) |
| 1424 | { |
| 1425 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1426 | } |
| 1427 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1428 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1429 | if (!output) |
| 1430 | { |
| 1431 | return Fail("%s: Could not read output 0", __func__); |
| 1432 | } |
| 1433 | |
| 1434 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1435 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1436 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1437 | if (IsDynamicTensor(outputInfo)) |
| 1438 | { |
| 1439 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1440 | } |
| 1441 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1442 | // ArmNN does not currently support non-fixed weights or bias |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1443 | const ConstTensorPin weightsPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 1, model, data); |
| 1444 | const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1445 | |
| 1446 | if (!weightsPin.IsValid() || !biasPin.IsValid()) |
| 1447 | { |
| 1448 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1449 | } |
| 1450 | |
| 1451 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1452 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1453 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 1454 | |
| 1455 | armnn::Convolution2dDescriptor desc; |
| 1456 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1457 | ActivationFn activation; |
| 1458 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1459 | if (operation.inputs.size() == 10) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1460 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1461 | if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 1462 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 1463 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 1464 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 1465 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1466 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1467 | !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1468 | { |
| 1469 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1470 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1471 | } |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1472 | else if (operation.inputs.size() == 7) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1473 | { |
| 1474 | android::nn::PaddingScheme paddingScheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1475 | if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 1476 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1477 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1478 | !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1479 | { |
| 1480 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1481 | } |
| 1482 | |
| 1483 | const uint32_t kernelX = weights.GetShape()[2]; |
| 1484 | const uint32_t kernelY = weights.GetShape()[1]; |
| 1485 | const uint32_t inputX = inputInfo.GetShape()[2]; |
| 1486 | const uint32_t inputY = inputInfo.GetShape()[1]; |
| 1487 | |
| 1488 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 1489 | 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] | 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 1494 | } |
| 1495 | |
| 1496 | desc.m_BiasEnabled = true; |
| 1497 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 1498 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1499 | bool isSupported = false; |
| 1500 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1501 | IsConvolution2dSupported, |
| 1502 | data.m_Backends, |
| 1503 | isSupported, |
| 1504 | inputInfo, |
| 1505 | outputInfo, |
| 1506 | desc, |
| 1507 | weights.GetInfo(), |
| 1508 | biases); |
| 1509 | if (!isSupported) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1510 | { |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
| 1514 | armnn::IConnectableLayer* startLayer = |
| 1515 | data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 1516 | |
| 1517 | if (!startLayer) |
| 1518 | { |
| 1519 | return Fail("%s: AddConvolution2dLayer failed", __func__); |
| 1520 | } |
| 1521 | |
| 1522 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 1523 | |
| 1524 | if (!endLayer) |
| 1525 | { |
| 1526 | return Fail("%s: ProcessActivation failed", __func__); |
| 1527 | } |
| 1528 | |
| 1529 | input.Connect(startLayer->GetInputSlot(0)); |
| 1530 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1531 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1532 | } |
| 1533 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1534 | template<typename HalPolicy, |
| 1535 | typename HalOperation = typename HalPolicy::Operation, |
| 1536 | typename HalModel = typename HalPolicy::Model> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1537 | bool ConvertDepthwiseConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1538 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1539 | using HalOperand = typename HalPolicy::Operand; |
| 1540 | using HalOperandType = typename HalPolicy::OperandType; |
| 1541 | |
| 1542 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1543 | |
| 1544 | if (!input.IsValid()) |
| 1545 | { |
| 1546 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1547 | } |
| 1548 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1549 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1550 | |
| 1551 | if (!output) |
| 1552 | { |
| 1553 | return Fail("%s: Could not read output 0", __func__); |
| 1554 | } |
| 1555 | |
| 1556 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1557 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1558 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1559 | if (IsDynamicTensor(outputInfo)) |
| 1560 | { |
| 1561 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1562 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1563 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1564 | // ArmNN does not currently support non-fixed weights or bias |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1565 | // 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] | 1566 | const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1567 | |
| 1568 | if (weightsOperand == nullptr) |
| 1569 | { |
| 1570 | return Fail("%s: Operand is invalid", __func__); |
| 1571 | } |
| 1572 | armnn::DepthwiseConvolution2dDescriptor desc; |
| 1573 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1574 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1575 | // Reinterpret weight data as [ H, W, I, M ] |
| 1576 | armnn::TensorShape weightsShape({ weightsOperand->dimensions[1], |
| 1577 | weightsOperand->dimensions[2], |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1578 | inputInfo.GetShape()[3], |
| 1579 | weightsOperand->dimensions[3] / inputInfo.GetShape()[3] }); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1580 | |
| 1581 | // Swizzle weight data [ H, W, I, M ] -> [ M, I, H, W ] |
| 1582 | const armnn::PermutationVector HWIMToMIHW = { 2U, 3U, 1U, 0U }; |
| 1583 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1584 | const ConstTensorPin weightsPin = |
| 1585 | ConvertOperationInputToConstTensorPin<HalPolicy>(operation, |
| 1586 | 1, |
| 1587 | model, |
| 1588 | data, |
| 1589 | HWIMToMIHW, |
| 1590 | &weightsShape); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1591 | |
| 1592 | // Bias is a 1D tensor |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1593 | const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1594 | |
| 1595 | if (!weightsPin.IsValid() || !biasPin.IsValid()) |
| 1596 | { |
| 1597 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1598 | } |
| 1599 | |
| 1600 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 1601 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 1602 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 1603 | |
| 1604 | ActivationFn activation; |
| 1605 | |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1606 | if (operation.inputs.size() == 11) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1607 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1608 | if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) || |
| 1609 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) || |
| 1610 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) || |
| 1611 | !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) || |
| 1612 | !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1613 | !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1614 | !GetInputActivationFunction<HalPolicy>(operation, 10, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1615 | { |
| 1616 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1617 | } |
| 1618 | } |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1619 | else if (operation.inputs.size() == 8) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1620 | { |
| 1621 | android::nn::PaddingScheme paddingScheme; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1622 | if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 1623 | !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) || |
| 1624 | !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) || |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1625 | !GetInputActivationFunction<HalPolicy>(operation, 7, activation, model, data)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1626 | { |
| 1627 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1628 | } |
| 1629 | |
| 1630 | const uint32_t kernelX = weights.GetShape()[3]; |
| 1631 | const uint32_t kernelY = weights.GetShape()[2]; |
Aron Virginas-Tar | a5e2a45 | 2019-07-29 16:13:19 +0100 | [diff] [blame] | 1632 | const uint32_t inputX = inputInfo.GetShape()[2]; |
| 1633 | const uint32_t inputY = inputInfo.GetShape()[1]; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1634 | |
| 1635 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 1636 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
| 1637 | } |
| 1638 | else |
| 1639 | { |
| 1640 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 1641 | } |
| 1642 | |
| 1643 | desc.m_BiasEnabled = true; |
| 1644 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 1645 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1646 | bool isSupported = false; |
| 1647 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1648 | IsDepthwiseConvolutionSupported, |
| 1649 | data.m_Backends, |
| 1650 | isSupported, |
| 1651 | inputInfo, |
| 1652 | outputInfo, |
| 1653 | desc, |
| 1654 | weights.GetInfo(), |
| 1655 | biases); |
| 1656 | if (!isSupported) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1657 | { |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
| 1661 | armnn::IConnectableLayer* startLayer = |
| 1662 | data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 1663 | if (!startLayer) |
| 1664 | { |
| 1665 | return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__); |
| 1666 | } |
| 1667 | |
| 1668 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 1669 | if (!endLayer) |
| 1670 | { |
| 1671 | return Fail("%s: ProcessActivation failed", __func__); |
| 1672 | } |
| 1673 | |
| 1674 | input.Connect(startLayer->GetInputSlot(0)); |
| 1675 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1676 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 1679 | template<typename HalPolicy, |
| 1680 | typename HalOperation = typename HalPolicy::Operation, |
| 1681 | typename HalOperand = typename HalPolicy::Operand, |
| 1682 | typename HalModel = typename HalPolicy::Model> |
| 1683 | bool ConvertPad(HalOperation& operation, const HalModel& model, ConversionData& data) |
| 1684 | { |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 1685 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 1686 | if (!input.IsValid()) |
| 1687 | { |
| 1688 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1689 | } |
| 1690 | |
| 1691 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1692 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 1693 | |
| 1694 | armnn::PadDescriptor descriptor; |
| 1695 | if (!ConvertPaddings<HalPolicy>(operation, model, data, rank, descriptor)) |
| 1696 | { |
| 1697 | return Fail("%s: Could not convert paddings", __func__); |
| 1698 | } |
| 1699 | |
| 1700 | // Before Android Q, the pad value for ANEURALNETWORKS_TENSOR_QUANT8_ASYMM was undefined. Since Android Q the pad |
| 1701 | // value must be "logical zero" we set it to be equal to the QuantizationOffset so effectively it ends up as |
| 1702 | // (QuantizationOffset - QuantizationOffset) * scale = 0. |
| 1703 | if (inputInfo.GetDataType() == armnn::DataType::QuantisedAsymm8) |
| 1704 | { |
| 1705 | descriptor.m_PadValue = inputInfo.GetQuantizationOffset(); |
| 1706 | } |
| 1707 | |
| 1708 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 1709 | if (!output) |
| 1710 | { |
| 1711 | return Fail("%s: Could not read output", __func__); |
| 1712 | } |
| 1713 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1714 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 1715 | if (IsDynamicTensor(outputInfo)) |
| 1716 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1717 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | bool isSupported = false; |
| 1721 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1722 | IsPadSupported, |
| 1723 | data.m_Backends, |
| 1724 | isSupported, |
| 1725 | inputInfo, |
| 1726 | outputInfo, |
| 1727 | descriptor); |
| 1728 | if (!isSupported) |
| 1729 | { |
| 1730 | return false; |
| 1731 | } |
| 1732 | |
| 1733 | armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor); |
| 1734 | assert(layer != nullptr); |
| 1735 | input.Connect(layer->GetInputSlot(0)); |
| 1736 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1737 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1738 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 1739 | } |
| 1740 | |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 1741 | template<typename HalPolicy, |
| 1742 | typename Operation = typename HalPolicy::Operation, |
| 1743 | typename Operand = typename HalPolicy::Operand, |
| 1744 | typename Model = typename HalPolicy::Model> |
| 1745 | bool ConvertSub(const Operation& operation, const Model& model, ConversionData& data) |
| 1746 | { |
| 1747 | LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 1748 | LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data); |
| 1749 | |
| 1750 | if (!input0.IsValid() || !input1.IsValid()) |
| 1751 | { |
| 1752 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1753 | } |
| 1754 | |
| 1755 | // The FuseActivation parameter is always the input index 2 |
| 1756 | // and it should be optional |
| 1757 | ActivationFn activationFunction; |
| 1758 | if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data)) |
| 1759 | { |
| 1760 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1761 | } |
| 1762 | |
| 1763 | const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 1764 | if (!output) |
| 1765 | { |
| 1766 | return Fail("%s: Could not read output 0", __func__); |
| 1767 | } |
| 1768 | |
| 1769 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1770 | if (IsDynamicTensor(outputInfo)) |
| 1771 | { |
| 1772 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1773 | } |
| 1774 | |
| 1775 | bool isSupported = false; |
| 1776 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1777 | IsSubtractionSupported, |
| 1778 | data.m_Backends, |
| 1779 | isSupported, |
| 1780 | input0.GetTensorInfo(), |
| 1781 | input1.GetTensorInfo(), |
| 1782 | outputInfo); |
| 1783 | if (!isSupported) |
| 1784 | { |
| 1785 | return false; |
| 1786 | } |
| 1787 | |
| 1788 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddSubtractionLayer(); |
| 1789 | armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data); |
| 1790 | |
| 1791 | const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo(); |
| 1792 | const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo(); |
| 1793 | |
| 1794 | if (endLayer) |
| 1795 | { |
| 1796 | BroadcastTensor(input0, input1, startLayer, *data.m_Network); |
| 1797 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data); |
| 1798 | } |
| 1799 | |
| 1800 | return Fail("%s: ProcessActivation failed", __func__); |
| 1801 | } |
| 1802 | |
Finn Williams | 23b87b3 | 2019-07-30 11:44:05 +0100 | [diff] [blame^] | 1803 | template<typename HalPolicy, |
| 1804 | typename HalOperation = typename HalPolicy::Operation, |
| 1805 | typename HalModel = typename HalPolicy::Model> |
| 1806 | bool ConvertBatchToSpaceNd(const HalOperation& operation, |
| 1807 | const HalModel& model, |
| 1808 | ConversionData& data) |
| 1809 | { |
| 1810 | using HalOperand = typename HalPolicy::Operand; |
| 1811 | using HalOperandType = typename HalPolicy::OperandType; |
| 1812 | |
| 1813 | LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data); |
| 1814 | if (!input.IsValid()) |
| 1815 | { |
| 1816 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1817 | } |
| 1818 | |
| 1819 | const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model); |
| 1820 | if (!output) |
| 1821 | { |
| 1822 | return Fail("%s: Could not read output 0", __func__); |
| 1823 | } |
| 1824 | |
| 1825 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1826 | if (IsDynamicTensor(outputInfo)) |
| 1827 | { |
| 1828 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1829 | } |
| 1830 | |
| 1831 | const HalOperand* blockOperand = GetInputOperand<HalPolicy>(operation, 1, model); |
| 1832 | if (!blockOperand) |
| 1833 | { |
| 1834 | return Fail("%s: Could not read input 1", __func__); |
| 1835 | } |
| 1836 | |
| 1837 | // Convert the block operand to int32 |
| 1838 | std::vector<int32_t> block; |
| 1839 | if (!GetTensorInt32Values<HalPolicy>(*blockOperand, block, model, data)) |
| 1840 | { |
| 1841 | return Fail("%s: Input 1 has invalid values", __func__); |
| 1842 | } |
| 1843 | |
| 1844 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1845 | |
| 1846 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 1847 | if (rank != 4) |
| 1848 | { |
| 1849 | Fail("%s: Only inputs with rank equal to 4 are supported", __func__); |
| 1850 | } |
| 1851 | |
| 1852 | if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; })) |
| 1853 | { |
| 1854 | return Fail("%s: Block sizes for each spatial dimension of the input tensor must be" |
| 1855 | " greater than or equal to 1", __func__); |
| 1856 | } |
| 1857 | |
| 1858 | armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc; |
| 1859 | batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend()); |
| 1860 | batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1861 | |
| 1862 | if (Is12Operand(*output)) |
| 1863 | { |
| 1864 | batchToSpaceNdDesc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 3, model, data); |
| 1865 | } |
| 1866 | // Setting crops to 0,0 0,0 as it is not supported in Android NN API |
| 1867 | batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}}; |
| 1868 | |
| 1869 | bool isSupported = false; |
| 1870 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1871 | IsBatchToSpaceNdSupported, |
| 1872 | data.m_Backends, |
| 1873 | isSupported, |
| 1874 | inputInfo, |
| 1875 | outputInfo, |
| 1876 | batchToSpaceNdDesc); |
| 1877 | if (!isSupported) |
| 1878 | { |
| 1879 | return false; |
| 1880 | } |
| 1881 | |
| 1882 | armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc); |
| 1883 | assert(layer != nullptr); |
| 1884 | input.Connect(layer->GetInputSlot(0)); |
| 1885 | |
| 1886 | return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data); |
| 1887 | } |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 1888 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 1889 | } // namespace armnn_driver |