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 | |
| 8 | #include <armnn/ArmNN.hpp> |
| 9 | |
| 10 | #include "armnn/src/armnnUtils/Permute.hpp" |
| 11 | #include "Utils.hpp" |
| 12 | |
| 13 | #include <ActivationFunctor.h> |
| 14 | #include <CpuExecutor.h> |
| 15 | #include <OperationsUtils.h> |
| 16 | |
| 17 | #include <boost/assert.hpp> |
| 18 | #include <boost/core/ignore_unused.hpp> |
Aron Virginas-Tar | 0e7ab54 | 2019-04-10 15:02:31 +0100 | [diff] [blame] | 19 | #include <boost/numeric/conversion/cast.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 20 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 21 | |
| 22 | #include <log/log.h> |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 23 | #include <vector> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 24 | |
| 25 | namespace armnn_driver |
| 26 | { |
| 27 | |
| 28 | /// |
| 29 | /// Helper classes |
| 30 | /// |
| 31 | |
| 32 | struct ConversionData |
| 33 | { |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 34 | ConversionData(const std::vector<armnn::BackendId>& backends) |
| 35 | : m_Backends(backends) |
| 36 | , m_Network(nullptr, nullptr) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 37 | {} |
| 38 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 39 | const std::vector<armnn::BackendId> m_Backends; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 40 | armnn::INetworkPtr m_Network; |
| 41 | std::vector<armnn::IOutputSlot*> m_OutputSlotForOperand; |
| 42 | std::vector<android::nn::RunTimePoolInfo> m_MemPools; |
| 43 | }; |
| 44 | |
| 45 | class LayerInputHandle |
| 46 | { |
| 47 | public: |
| 48 | LayerInputHandle(); |
| 49 | LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo); |
| 50 | |
| 51 | bool IsValid() const; |
| 52 | |
| 53 | void Connect(armnn::IInputSlot& inputSlot); |
| 54 | |
| 55 | const armnn::TensorInfo& GetTensorInfo() const; |
| 56 | |
| 57 | private: |
| 58 | armnn::IOutputSlot* m_OutputSlot; |
| 59 | bool m_Valid; |
| 60 | armnn::TensorInfo m_TensorInfo; |
| 61 | }; |
| 62 | |
| 63 | class ConstTensorPin |
| 64 | { |
| 65 | public: |
| 66 | // Creates an invalid tensor pin (can be used to signal errors) |
| 67 | // The optional flag can be set to indicate the tensor values were missing, but it was otherwise valid |
| 68 | ConstTensorPin(bool optional = false); |
| 69 | |
| 70 | // @param tensorInfo TensorInfo associated with the tensor. |
| 71 | // @param valueStart Start address of tensor data. Belongs to one of the memory pools associated with |
| 72 | // the model being converted. |
| 73 | // @param numBytes Number of bytes for the tensor data. |
| 74 | ConstTensorPin(const armnn::TensorInfo& tensorInfo, const void* valueStart, uint32_t numBytes, |
| 75 | const armnn::PermutationVector& mappings); |
| 76 | |
| 77 | ConstTensorPin(const ConstTensorPin& other) = delete; |
| 78 | ConstTensorPin(ConstTensorPin&& other) = default; |
| 79 | |
| 80 | bool IsValid() const; |
| 81 | bool IsOptional() const; |
| 82 | |
| 83 | const armnn::ConstTensor& GetConstTensor() const; |
| 84 | const armnn::ConstTensor* GetConstTensorPtr() const; |
| 85 | |
| 86 | private: |
| 87 | armnn::ConstTensor m_ConstTensor; |
| 88 | |
| 89 | // Owned memory for swizzled tensor data, only required if the tensor needed |
| 90 | // swizzling. Otherwise, @ref m_ConstTensor will reference memory from one of |
| 91 | // the pools associated with the model being converted. |
| 92 | std::vector<uint8_t> m_SwizzledTensorData; |
| 93 | |
| 94 | // optional flag to indicate that an invalid tensor pin is not an error, but the optional values were not given |
| 95 | bool m_Optional; |
| 96 | }; |
| 97 | |
| 98 | } // namespace armnn_driver |
| 99 | |
| 100 | /// |
| 101 | /// Utility functions |
| 102 | /// |
| 103 | |
| 104 | namespace |
| 105 | { |
| 106 | |
| 107 | using namespace armnn_driver; |
| 108 | using namespace android::nn; |
| 109 | |
| 110 | // Convenience function to log the reason for failing to convert a model. |
| 111 | // @return Always returns false (so that it can be used by callers as a quick way to signal an error and return) |
| 112 | template<class... Args> |
| 113 | static bool Fail(const char* formatStr, Args&&... args) |
| 114 | { |
| 115 | ALOGD(formatStr, std::forward<Args>(args)...); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Convenience function to call an Is*Supported function and log caller name together with reason for lack of support. |
| 120 | // Called as: IsLayerSupported(__func__, Is*Supported, a, b, c, d, e) |
| 121 | template<typename IsLayerSupportedFunc, typename ... Args> |
| 122 | bool IsLayerSupported(const char* funcName, IsLayerSupportedFunc f, Args&&... args) |
| 123 | { |
| 124 | std::vector<char> unsupportedReason(1024+1); |
| 125 | bool isSupported = f(std::forward<Args>(args)..., unsupportedReason.data(), unsupportedReason.size()-1); |
| 126 | if(isSupported) |
| 127 | { |
| 128 | return true; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | std::string sUnsupportedReason(unsupportedReason.data()); |
| 133 | if (sUnsupportedReason.size() > 0) |
| 134 | { |
| 135 | ALOGD("%s: not supported by armnn: %s", funcName, sUnsupportedReason.c_str()); |
| 136 | } else |
| 137 | { |
| 138 | ALOGD("%s: not supported by armnn", funcName); |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 144 | template<typename IsLayerSupportedFunc, typename ... Args> |
| 145 | bool IsLayerSupportedForAnyBackend(const char* funcName, |
| 146 | IsLayerSupportedFunc f, |
| 147 | const std::vector<armnn::BackendId>& backends, |
| 148 | Args&&... args) |
| 149 | { |
| 150 | for (auto&& backend : backends) |
| 151 | { |
| 152 | if (IsLayerSupported(funcName, f, backend, std::forward<Args>(args)...)) |
| 153 | { |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | ALOGD("%s: not supported by any specified backend", funcName); |
| 159 | return false; |
| 160 | } |
| 161 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 162 | armnn::TensorShape GetTensorShapeForOperand(const V1_0::Operand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 163 | { |
| 164 | return armnn::TensorShape(operand.dimensions.size(), operand.dimensions.data()); |
| 165 | } |
| 166 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 167 | inline bool IsOperandTypeSupportedForTensors(V1_0::OperandType type) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 168 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 169 | return type == V1_0::OperandType::TENSOR_FLOAT32 || |
| 170 | type == V1_0::OperandType::TENSOR_QUANT8_ASYMM || |
| 171 | type == V1_0::OperandType::TENSOR_INT32; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void BroadcastTensor(LayerInputHandle& input0, LayerInputHandle& input1, armnn::IConnectableLayer* startLayer, |
| 175 | armnn::INetwork& network) |
| 176 | { |
| 177 | BOOST_ASSERT(startLayer != nullptr); |
| 178 | const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo(); |
| 179 | const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo(); |
| 180 | |
| 181 | if (inputTensorInfo0.GetNumDimensions() != inputTensorInfo1.GetNumDimensions()) |
| 182 | { |
| 183 | // If the number of dimensions do not match then we need to add degenerate dimensions |
| 184 | // to the "smaller" tensor using a reshape: |
| 185 | // Small Big |
| 186 | // | | |
| 187 | // Reshape | |
| 188 | // \ / |
| 189 | // Add |
| 190 | bool input0IsBigger = inputTensorInfo0.GetNumDimensions() > inputTensorInfo1.GetNumDimensions(); |
| 191 | |
| 192 | LayerInputHandle& smallTensorHandle = input0IsBigger ? input1 : input0; |
| 193 | const armnn::TensorInfo& smallTensorDims = smallTensorHandle.GetTensorInfo(); |
| 194 | |
| 195 | LayerInputHandle& bigTensorHandle = input0IsBigger ? input0 : input1; |
| 196 | const armnn::TensorInfo& bigTensorDims = bigTensorHandle.GetTensorInfo(); |
| 197 | |
| 198 | const unsigned int bigTensorDimsNumber = bigTensorDims.GetNumDimensions(); |
| 199 | std::vector<unsigned int> reshapedDims(bigTensorDimsNumber, 1); |
| 200 | unsigned int sizeDifference = bigTensorDimsNumber - smallTensorDims.GetNumDimensions(); |
| 201 | for (unsigned i = sizeDifference; i < bigTensorDimsNumber; ++i) |
| 202 | { |
| 203 | reshapedDims[i] = smallTensorDims.GetShape()[i-sizeDifference]; |
| 204 | } |
| 205 | armnn::TensorInfo reshapedInfo = smallTensorDims; |
| 206 | reshapedInfo.SetShape(armnn::TensorShape{ static_cast<unsigned int>(reshapedDims.size()), |
| 207 | reshapedDims.data() }); |
| 208 | |
| 209 | armnn::ReshapeDescriptor reshapeDesc; |
| 210 | reshapeDesc.m_TargetShape = reshapedInfo.GetShape(); |
| 211 | armnn::IConnectableLayer* const reshapeLayer = network.AddReshapeLayer(reshapeDesc); |
| 212 | smallTensorHandle.Connect(reshapeLayer->GetInputSlot(0)); |
| 213 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo); |
| 214 | |
| 215 | // Connect the outputs from new reshape and original input layer |
| 216 | reshapeLayer->GetOutputSlot(0).Connect(startLayer->GetInputSlot(0)); |
| 217 | bigTensorHandle.Connect(startLayer->GetInputSlot(1)); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | input0.Connect(startLayer->GetInputSlot(0)); |
| 222 | input1.Connect(startLayer->GetInputSlot(1)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t& outPadHead, uint32_t& outPadTail, |
| 227 | android::nn::PaddingScheme scheme) |
| 228 | { |
| 229 | int32_t padHead; |
| 230 | int32_t padTail; |
| 231 | calculateExplicitPadding(input, stride, kernel, scheme, &padHead, &padTail); |
| 232 | outPadHead = boost::numeric_cast<uint32_t>(padHead); |
| 233 | outPadTail = boost::numeric_cast<uint32_t>(padTail); |
| 234 | } |
| 235 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 236 | Shape GetOperandShape(const V1_0::Operand& operand) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 237 | { |
| 238 | Shape shape; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 239 | shape.type = OperandType(operand.type); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 240 | shape.dimensions = operand.dimensions; |
| 241 | shape.scale = operand.scale; |
| 242 | shape.offset = operand.zeroPoint; |
| 243 | return shape; |
| 244 | } |
| 245 | |
| 246 | // ArmNN requires the bias scale to be equal to the product of the weight and input scales, which is also |
| 247 | // what AndroidNN requires. However for some of the AndroidNN tests the values don't exactly match so |
| 248 | // we accept some tolerance. We don't want to ArmNN itself to accept these inconsistencies as it is up to the user |
| 249 | // (us, in this case) to ensure they match. |
| 250 | void SanitizeBiasQuantizationScale(armnn::TensorInfo& biasInfo, |
| 251 | const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& inputInfo) |
| 252 | { |
| 253 | const float expectedBiasScale = weightInfo.GetQuantizationScale() * inputInfo.GetQuantizationScale(); |
| 254 | if (biasInfo.GetQuantizationScale() != expectedBiasScale) |
| 255 | { |
| 256 | boost::math::fpc::close_at_tolerance<float> comparer(boost::math::fpc::percent_tolerance(1.0f)); |
| 257 | if (comparer(biasInfo.GetQuantizationScale(), expectedBiasScale)) |
| 258 | { |
| 259 | ALOGW("Bias quantization scale has been modified to match input*weights"); |
| 260 | biasInfo.SetQuantizationScale(expectedBiasScale); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // 4D Tensor Permutations |
| 266 | const armnn::PermutationVector IdentityPermutation4D({ 0U, 1U, 2U, 3U }); |
| 267 | const armnn::PermutationVector NHWCToArmNN({ 0U, 2U, 3U, 1U }); |
| 268 | const armnn::PermutationVector ArmNNToNHWC({ 0U, 3U, 1U, 2U }); |
| 269 | const armnn::PermutationVector SwapDim1And2({ 0U, 2U, 1U, 3U }); |
| 270 | |
| 271 | // 3D Permutation Vectors |
| 272 | const armnn::PermutationVector IdentityPermutation3D({ 0U, 1U, 2U }); |
| 273 | const armnn::PermutationVector RotateTensorLeft({ 2U, 0U, 1U }); |
| 274 | const armnn::PermutationVector RotateTensorRight({ 1U, 2U, 0U }); |
| 275 | |
| 276 | template<typename OSlot> |
| 277 | armnn::IConnectableLayer& AddPermuteLayer(armnn::INetwork& network, OSlot& input, |
| 278 | const armnn::PermutationVector& mappings) |
| 279 | { |
| 280 | // Add swizzle layer |
| 281 | armnn::IConnectableLayer* const layer = network.AddPermuteLayer(mappings); |
| 282 | |
| 283 | BOOST_ASSERT(layer != nullptr); |
| 284 | |
| 285 | // Connect input to swizzle layer |
| 286 | input.Connect(layer->GetInputSlot(0)); |
| 287 | |
| 288 | // Setup swizzled output |
| 289 | const armnn::TensorInfo outInfo = armnnUtils::Permuted(input.GetTensorInfo(), mappings); |
| 290 | layer->GetOutputSlot(0).SetTensorInfo(outInfo); |
| 291 | |
| 292 | return *layer; |
| 293 | } |
| 294 | |
| 295 | void SwizzleIn(armnn::INetwork& network, LayerInputHandle& input, armnn::IConnectableLayer& layer, unsigned int index) |
| 296 | { |
| 297 | // Add swizzle layer |
| 298 | armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, input, NHWCToArmNN); |
| 299 | // Connect swizzled input to layer |
| 300 | swizzleLayer.GetOutputSlot(0).Connect(layer.GetInputSlot(index)); |
| 301 | } |
| 302 | |
| 303 | armnn::IConnectableLayer& DeswizzleOut(armnn::INetwork& network, armnn::IConnectableLayer& layer, unsigned int index) |
| 304 | { |
| 305 | // Add deswizzle layer |
| 306 | armnn::IConnectableLayer& deswizzleLayer = AddPermuteLayer(network, layer.GetOutputSlot(index), ArmNNToNHWC); |
| 307 | return deswizzleLayer; |
| 308 | } |
| 309 | |
| 310 | // only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly |
| 311 | armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network, |
| 312 | LayerInputHandle& input, |
| 313 | armnn::IConnectableLayer& firstLayer, |
| 314 | armnn::IConnectableLayer& lastLayer) |
| 315 | { |
| 316 | SwizzleIn(network, input, firstLayer, 0); |
| 317 | return DeswizzleOut(network, lastLayer, 0); |
| 318 | } |
| 319 | |
| 320 | // only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly |
| 321 | armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network, LayerInputHandle& input, |
| 322 | armnn::IConnectableLayer& layer) |
| 323 | { |
| 324 | return SwizzleInDeswizzleOut(network, input, layer, layer); |
| 325 | } |
| 326 | |
| 327 | bool ValidateConcatOutputShape(const std::vector<armnn::TensorShape> & inputShapes, |
| 328 | const armnn::TensorShape & outputShape, |
| 329 | uint32_t concatDim) |
| 330 | { |
| 331 | // Validate the output shape is correct given the input shapes (which have just been validated) |
| 332 | unsigned int numDimensions = inputShapes[0].GetNumDimensions(); |
| 333 | if (outputShape.GetNumDimensions() != numDimensions) |
| 334 | { |
| 335 | return Fail("%s: Output shape has wrong number of dimensions", __func__); |
| 336 | } |
| 337 | |
| 338 | unsigned int outputSizeAlongConcatenatedDimension = 0; |
| 339 | for (unsigned int i = 0; i < inputShapes.size(); i++) |
| 340 | { |
| 341 | outputSizeAlongConcatenatedDimension += inputShapes[i][concatDim]; |
| 342 | } |
| 343 | |
| 344 | for (unsigned int i = 0; i < numDimensions; ++i) |
| 345 | { |
| 346 | if (i == concatDim) |
| 347 | { |
| 348 | if (outputShape[i] != outputSizeAlongConcatenatedDimension) |
| 349 | { |
| 350 | return Fail( |
| 351 | "%s: Invalid output shape for dimension %d (%d != %d)", |
| 352 | __func__, |
| 353 | i, |
| 354 | outputShape[i], |
| 355 | outputSizeAlongConcatenatedDimension); |
| 356 | } |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | if (outputShape[i] != inputShapes[0][i]) |
| 361 | { |
| 362 | return Fail("%s: Invalid output shape", __func__); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | bool RequiresReshape(armnn::TensorShape & inputShape) |
| 371 | { |
| 372 | return inputShape.GetNumDimensions() < 3; |
| 373 | } |
| 374 | |
| 375 | template<typename OSlot> |
| 376 | armnn::IConnectableLayer& AddReshapeLayer(armnn::INetwork& network, OSlot& inputLayer, |
| 377 | armnn::TensorInfo reshapeInfo) |
| 378 | { |
| 379 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 380 | reshapeDescriptor.m_TargetShape = reshapeInfo.GetShape(); |
| 381 | |
| 382 | armnn::IConnectableLayer* reshapeLayer = network.AddReshapeLayer(reshapeDescriptor); |
| 383 | BOOST_ASSERT(reshapeLayer != nullptr); |
| 384 | |
| 385 | // Attach the input layer to the reshape layer |
| 386 | inputLayer.Connect(reshapeLayer->GetInputSlot(0)); |
| 387 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapeInfo); |
| 388 | |
| 389 | return *reshapeLayer; |
| 390 | } |
| 391 | |
| 392 | void SwizzleInputs(armnn::INetwork& network, |
| 393 | std::vector<LayerInputHandle>& inputs, |
| 394 | std::vector<armnn::TensorShape>& inputShapes, |
| 395 | const armnn::PermutationVector& mapping) |
| 396 | { |
| 397 | if (!mapping.IsEqual(IdentityPermutation4D)) |
| 398 | { |
| 399 | size_t nInputs = inputs.size(); |
| 400 | for (size_t i=0; i<nInputs; ++i) |
| 401 | { |
| 402 | // add swizzle layer |
| 403 | armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, inputs[i], mapping); |
| 404 | auto& outputSlot = swizzleLayer.GetOutputSlot(0); |
| 405 | auto& outputInfo = outputSlot.GetTensorInfo(); |
| 406 | // replace inputs with the swizzled ones |
| 407 | inputs[i] = LayerInputHandle(true, &outputSlot, outputInfo); |
| 408 | inputShapes[i] = inputs[i].GetTensorInfo().GetShape(); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 413 | bool CreateConcatPermutationParameters(const unsigned int numberOfDimensions, |
| 414 | int32_t & concatDimension, |
| 415 | std::pair<armnn::PermutationVector, armnn::PermutationVector> & permutationPair) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 416 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 417 | bool needPermute = false; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 418 | BOOST_ASSERT(numberOfDimensions >= 3); |
| 419 | |
| 420 | // ArmNN uses Compute Library subtensors to perform concatenation |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 421 | // This only works when concatenating along dimension 0, 1 or 3 for a 4-D tensor, |
| 422 | // or along dimension 0 or 2 for a 3-D tensor. |
| 423 | if (numberOfDimensions == 4 && concatDimension == 2) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 424 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 425 | concatDimension = 1; |
| 426 | permutationPair = std::make_pair(SwapDim1And2, SwapDim1And2); |
| 427 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 428 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 429 | else if (numberOfDimensions == 3 && concatDimension == 1) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 430 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 431 | concatDimension = 0; |
| 432 | permutationPair = std::make_pair(RotateTensorLeft, RotateTensorRight); |
| 433 | needPermute = true; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 434 | } |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 435 | return needPermute; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | } // anonymous namespace |
| 439 | |
| 440 | namespace armnn_driver |
| 441 | { |
| 442 | |
| 443 | //// Creates an ArmNN activation layer and connects it to the given layer, if the |
| 444 | //// passed in AndroidNN activation function requires so. |
| 445 | //// @return The end layer of the sequence of layers built for the given AndroidNN |
| 446 | //// activation function or nullptr if an error occurred (e.g. unsupported activation). |
| 447 | //// Note that the end layer matches the input layer if no activation is required |
| 448 | //// (the sequence of layers has length 1). |
| 449 | armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo, |
| 450 | ActivationFn activation, |
| 451 | armnn::IConnectableLayer* prevLayer, |
| 452 | ConversionData& data); |
| 453 | |
| 454 | } // namespace armnn_driver |
| 455 | |
| 456 | /// |
| 457 | /// Utility templates |
| 458 | /// |
| 459 | |
| 460 | namespace armnn_driver |
| 461 | { |
| 462 | |
| 463 | using namespace android::nn; |
| 464 | |
| 465 | template<typename HalOperation, typename HalModel> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 466 | const V1_0::Operand* GetInputOperand(const HalOperation& operation, uint32_t inputIndex, const HalModel& model, |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 467 | bool failOnIndexOutOfBounds = true) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 468 | { |
| 469 | if (inputIndex >= operation.inputs.size()) |
| 470 | { |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 471 | if (failOnIndexOutOfBounds) |
| 472 | { |
| 473 | Fail("%s: invalid input index: %i out of %i", __func__, inputIndex, operation.inputs.size()); |
| 474 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | BOOST_ASSERT(operation.inputs[inputIndex] < model.operands.size()); // Model should have been validated beforehand |
| 479 | return &model.operands[operation.inputs[inputIndex]]; |
| 480 | } |
| 481 | |
| 482 | template<typename HalOperation, typename HalModel> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 483 | const V1_0::Operand* GetOutputOperand(const HalOperation& operation, uint32_t outputIndex, const HalModel& model) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 484 | { |
| 485 | if (outputIndex >= operation.outputs.size()) |
| 486 | { |
| 487 | Fail("%s: invalid output index: %i out of %i", __func__, outputIndex, operation.outputs.size()); |
| 488 | return nullptr; |
| 489 | } |
| 490 | |
| 491 | // Model should have been validated beforehand |
| 492 | BOOST_ASSERT(operation.outputs[outputIndex] < model.operands.size()); |
| 493 | |
| 494 | return &model.operands[operation.outputs[outputIndex]]; |
| 495 | } |
| 496 | |
| 497 | template<typename HalModel> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 498 | ConstTensorPin ConvertOperandToConstTensorPin(const V1_0::Operand& operand, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 499 | const HalModel& model, |
| 500 | const ConversionData& data, |
| 501 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 502 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 503 | bool optional = false) |
| 504 | { |
| 505 | if (!IsOperandTypeSupportedForTensors(operand.type)) |
| 506 | { |
| 507 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand.type).c_str()); |
| 508 | return ConstTensorPin(); |
| 509 | } |
| 510 | |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 511 | if (!optional && |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 512 | operand.lifetime !=V1_0::OperandLifeTime::CONSTANT_COPY && |
| 513 | operand.lifetime !=V1_0::OperandLifeTime::CONSTANT_REFERENCE && |
| 514 | operand.lifetime !=V1_0::OperandLifeTime::NO_VALUE) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 515 | { |
| 516 | Fail("%s: invalid operand lifetime: %s", __func__, toString(operand.lifetime).c_str()); |
| 517 | return ConstTensorPin(); |
| 518 | } |
| 519 | |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 520 | const void* const valueStart = GetOperandValueReadOnlyAddress(operand, model, data, optional); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 521 | if (!valueStart) |
| 522 | { |
| 523 | if (optional) |
| 524 | { |
| 525 | // optional tensor with no values is not really an error; return it as invalid, but marked as optional |
| 526 | return ConstTensorPin(true); |
| 527 | } |
| 528 | // mandatory tensor with no values |
| 529 | Fail("%s: failed to get operand address", __func__); |
| 530 | return ConstTensorPin(); |
| 531 | } |
| 532 | |
| 533 | armnn::TensorInfo tensorInfo = GetTensorInfoForOperand(operand); |
| 534 | if (overrideTensorShape != nullptr) |
| 535 | { |
| 536 | tensorInfo.SetShape(*overrideTensorShape); |
| 537 | } |
| 538 | return ConstTensorPin(tensorInfo, valueStart, operand.location.length, dimensionMappings); |
| 539 | } |
| 540 | |
| 541 | template<typename HalOperation, typename HalModel> |
| 542 | ConstTensorPin ConvertOperationInputToConstTensorPin(const HalOperation& operation, |
| 543 | uint32_t inputIndex, |
| 544 | const HalModel& model, |
| 545 | const ConversionData& data, |
| 546 | const armnn::PermutationVector& dimensionMappings = g_DontPermute, |
| 547 | const armnn::TensorShape* overrideTensorShape = nullptr, |
| 548 | bool optional = false) |
| 549 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 550 | const V1_0::Operand* operand = GetInputOperand(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 551 | if (!operand) |
| 552 | { |
| 553 | Fail("%s: failed to get input operand: index=%u", __func__, inputIndex); |
| 554 | return ConstTensorPin(); |
| 555 | } |
| 556 | return ConvertOperandToConstTensorPin(*operand, |
| 557 | model, |
| 558 | data, |
| 559 | dimensionMappings, |
| 560 | overrideTensorShape, |
| 561 | optional); |
| 562 | } |
| 563 | |
| 564 | template<typename HalModel> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 565 | const void* GetOperandValueReadOnlyAddress(const V1_0::Operand& operand, |
| 566 | const HalModel& model, |
| 567 | const ConversionData& data, |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 568 | bool optional = false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 569 | { |
| 570 | const void* valueStart = nullptr; |
| 571 | |
| 572 | switch (operand.lifetime) |
| 573 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 574 | case V1_0::OperandLifeTime::CONSTANT_COPY: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 575 | { |
| 576 | // Constant found in model.operandValues |
| 577 | valueStart = &model.operandValues[operand.location.offset]; |
| 578 | break; |
| 579 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 580 | case V1_0::OperandLifeTime::CONSTANT_REFERENCE: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 581 | { |
| 582 | // Constant specified via a Memory object |
| 583 | valueStart = GetMemoryFromPool(operand.location, data.m_MemPools); |
| 584 | break; |
| 585 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 586 | case V1_0::OperandLifeTime::NO_VALUE: |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 587 | { |
| 588 | // An optional input tensor with no values is not an error so should not register as a fail |
| 589 | if (optional) |
| 590 | { |
| 591 | valueStart = nullptr; |
| 592 | break; |
| 593 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 594 | [[fallthrough]]; |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 595 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 596 | default: |
| 597 | { |
| 598 | // Unsupported/invalid (e.g. can't get value of an input to the model) |
| 599 | Fail("%s: unsupported/invalid operand lifetime: %s", |
| 600 | __func__, toString(operand.lifetime).c_str()); |
| 601 | valueStart = nullptr; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return valueStart; |
| 606 | } |
| 607 | |
| 608 | template<typename HalOperation, typename HalModel, typename OutputType> |
| 609 | bool GetInputScalar(const HalOperation& operation, |
| 610 | uint32_t inputIndex, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 611 | V1_0::OperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 612 | OutputType& outValue, |
| 613 | const HalModel& model, |
| 614 | const ConversionData& data) |
| 615 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 616 | const V1_0::Operand* operand = GetInputOperand(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 617 | if (!operand) |
| 618 | { |
| 619 | return Fail("%s: invalid input operand at index %i", __func__, inputIndex); |
| 620 | } |
| 621 | |
| 622 | if (operand->type != type) |
| 623 | { |
| 624 | return Fail("%s: unexpected operand type: %s (should be %s)", |
| 625 | __func__, toString(operand->type).c_str(), toString(type).c_str()); |
| 626 | } |
| 627 | |
| 628 | if (operand->location.length != sizeof(OutputType)) |
| 629 | { |
| 630 | return Fail("%s: incorrect operand location length: %i (should be %i)", |
| 631 | __func__, operand->location.length, sizeof(OutputType)); |
| 632 | } |
| 633 | |
| 634 | const void* valueAddress = GetOperandValueReadOnlyAddress(*operand, model, data); |
| 635 | if (!valueAddress) |
| 636 | { |
| 637 | return Fail("%s: failed to get address for operand", __func__); |
| 638 | } |
| 639 | |
| 640 | outValue = *(static_cast<const OutputType*>(valueAddress)); |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | template<typename HalOperation, typename HalModel> |
| 645 | bool GetInputInt32(const HalOperation& operation, |
| 646 | uint32_t inputIndex, |
| 647 | int32_t& outValue, |
| 648 | const HalModel& model, |
| 649 | const ConversionData& data) |
| 650 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 651 | return GetInputScalar(operation, inputIndex,V1_0::OperandType::INT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | template<typename HalOperation, typename HalModel> |
| 656 | bool GetInputFloat32(const HalOperation& operation, |
| 657 | uint32_t inputIndex, |
| 658 | float& outValue, |
| 659 | const HalModel& model, |
| 660 | const ConversionData& data) |
| 661 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 662 | return GetInputScalar(operation, inputIndex,V1_0::OperandType::FLOAT32, outValue, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
| 666 | template<typename HalOperation, typename HalModel> |
| 667 | bool GetInputActivationFunctionImpl(const HalOperation& operation, |
| 668 | uint32_t inputIndex, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 669 | V1_0::OperandType type, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 670 | ActivationFn& outActivationFunction, |
| 671 | const HalModel& model, |
| 672 | const ConversionData& data) |
| 673 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 674 | if (type !=V1_0::OperandType::INT32 && type !=V1_0::OperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 675 | { |
| 676 | return Fail("%s: unexpected operand type: %s (should be %s or %s)", |
| 677 | __func__, |
| 678 | toString(type).c_str(), |
| 679 | toString(OperandType::INT32).c_str(), |
| 680 | toString(OperandType::TENSOR_INT32).c_str()); |
| 681 | } |
| 682 | |
| 683 | int32_t activationFunctionAsInt; |
| 684 | if (!GetInputScalar(operation, inputIndex, type, activationFunctionAsInt, model, data)) |
| 685 | { |
| 686 | return Fail("%s: failed to get activation input value", __func__); |
| 687 | } |
| 688 | outActivationFunction = static_cast<ActivationFn>(activationFunctionAsInt); |
| 689 | return true; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | template<typename HalOperation, typename HalModel> |
| 694 | bool GetInputActivationFunction(const HalOperation& operation, |
| 695 | uint32_t inputIndex, |
| 696 | ActivationFn& outActivationFunction, |
| 697 | const HalModel& model, |
| 698 | const ConversionData& data) |
| 699 | { |
| 700 | return GetInputActivationFunctionImpl(operation, |
| 701 | inputIndex, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 702 | V1_0::OperandType::INT32, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 703 | outActivationFunction, |
| 704 | model, |
| 705 | data); |
| 706 | } |
| 707 | |
| 708 | template<typename HalOperation, typename HalModel> |
| 709 | bool GetInputActivationFunctionFromTensor(const HalOperation& operation, |
| 710 | uint32_t inputIndex, |
| 711 | ActivationFn& outActivationFunction, |
| 712 | const HalModel& model, |
| 713 | const ConversionData& data) |
| 714 | { |
| 715 | // This only accepts a 1-D tensor of size 1 |
| 716 | return GetInputActivationFunctionImpl(operation, |
| 717 | inputIndex, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 718 | V1_0::OperandType::INT32, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 719 | outActivationFunction, |
| 720 | model, |
| 721 | data); |
| 722 | } |
| 723 | |
| 724 | |
| 725 | template<typename HalOperation, typename HalModel> |
| 726 | bool GetOptionalInputActivation(const HalOperation& operation, |
| 727 | uint32_t inputIndex, |
| 728 | ActivationFn& activationFunction, |
| 729 | const HalModel& model, |
| 730 | const ConversionData& data) |
| 731 | { |
| 732 | if (operation.inputs.size() <= inputIndex) |
| 733 | { |
| 734 | activationFunction = ActivationFn::kActivationNone; |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | if (!GetInputActivationFunction(operation, inputIndex, activationFunction, model, data)) |
| 739 | { |
| 740 | return Fail("%s: Operation has invalid inputs", __func__); |
| 741 | } |
| 742 | } |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | template<typename HalModel> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 747 | bool GetTensorInt32Values(const V1_0::Operand& operand, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 748 | std::vector<int32_t>& outValues, |
| 749 | const HalModel& model, |
| 750 | const ConversionData& data) |
| 751 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 752 | if (operand.type !=V1_0::OperandType::TENSOR_INT32) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 753 | { |
| 754 | return Fail("%s: invalid operand type: %s", __func__, toString(operand.type).c_str()); |
| 755 | } |
| 756 | |
| 757 | const void* startAddress = GetOperandValueReadOnlyAddress(operand, model, data); |
| 758 | if (!startAddress) |
| 759 | { |
| 760 | return Fail("%s: failed to get operand address", __func__, operand.type); |
| 761 | } |
| 762 | |
| 763 | // Check number of bytes is sensible |
| 764 | const uint32_t numBytes = operand.location.length; |
| 765 | if (numBytes % sizeof(int32_t) != 0) |
| 766 | { |
| 767 | return Fail("%s: invalid number of bytes: %i, expected to be a multiple of %i", |
| 768 | __func__, numBytes, sizeof(int32_t)); |
| 769 | } |
| 770 | |
| 771 | outValues.resize(numBytes / sizeof(int32_t)); |
| 772 | memcpy(outValues.data(), startAddress, numBytes); |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | template<typename HalOperation, typename HalModel> |
| 777 | bool GetInputPaddingScheme(const HalOperation& operation, |
| 778 | uint32_t inputIndex, |
| 779 | PaddingScheme& outPaddingScheme, |
| 780 | const HalModel& model, |
| 781 | const ConversionData& data) |
| 782 | { |
| 783 | int32_t paddingSchemeAsInt; |
| 784 | if (!GetInputInt32(operation, inputIndex, paddingSchemeAsInt, model, data)) |
| 785 | { |
| 786 | return Fail("%s: failed to get padding scheme input value", __func__); |
| 787 | } |
| 788 | |
| 789 | outPaddingScheme = static_cast<android::nn::PaddingScheme>(paddingSchemeAsInt); |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | template<typename HalOperation, typename HalModel> |
| 794 | LayerInputHandle ConvertToLayerInputHandle(const HalOperation& operation, |
| 795 | uint32_t inputIndex, |
| 796 | const HalModel& model, |
| 797 | ConversionData& data) |
| 798 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 799 | const V1_0::Operand* operand = GetInputOperand(operation, inputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 800 | if (!operand) |
| 801 | { |
| 802 | Fail("%s: failed to get input operand %i", __func__, inputIndex); |
| 803 | return LayerInputHandle(); |
| 804 | } |
| 805 | |
| 806 | if (!IsOperandTypeSupportedForTensors(operand->type)) |
| 807 | { |
| 808 | Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand->type).c_str()); |
| 809 | return LayerInputHandle(); |
| 810 | } |
| 811 | |
| 812 | armnn::TensorInfo operandTensorInfo = GetTensorInfoForOperand(*operand); |
| 813 | |
| 814 | switch (operand->lifetime) |
| 815 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 816 | case V1_0::OperandLifeTime::TEMPORARY_VARIABLE: // intentional fallthrough |
| 817 | case V1_0::OperandLifeTime::MODEL_INPUT: |
| 818 | case V1_0::OperandLifeTime::MODEL_OUTPUT: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 819 | { |
| 820 | // The tensor is either an operand internal to the model, or a model input. |
| 821 | // It can be associated with an ArmNN output slot for an existing layer. |
| 822 | |
| 823 | // m_OutputSlotForOperand[...] can be nullptr if the previous layer could not be converted |
| 824 | const uint32_t operandIndex = operation.inputs[inputIndex]; |
| 825 | return LayerInputHandle(true, data.m_OutputSlotForOperand[operandIndex], operandTensorInfo); |
| 826 | break; |
| 827 | } |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 828 | case V1_0::OperandLifeTime::CONSTANT_COPY: |
| 829 | case V1_0::OperandLifeTime::CONSTANT_REFERENCE: |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 830 | { |
| 831 | // The tensor has an already known constant value, and can be converted into an ArmNN Constant layer. |
| 832 | ConstTensorPin tensorPin = ConvertOperandToConstTensorPin(*operand, model, data); |
| 833 | if (tensorPin.IsValid()) |
| 834 | { |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 835 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 836 | armnn::IsConstantSupported, |
| 837 | data.m_Backends, |
| 838 | tensorPin.GetConstTensor().GetInfo())) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 839 | { |
| 840 | return LayerInputHandle(); |
| 841 | } |
| 842 | |
| 843 | armnn::IConnectableLayer* constantLayer = data.m_Network->AddConstantLayer(tensorPin.GetConstTensor()); |
| 844 | armnn::IOutputSlot& outputSlot = constantLayer->GetOutputSlot(0); |
| 845 | outputSlot.SetTensorInfo(tensorPin.GetConstTensor().GetInfo()); |
| 846 | |
| 847 | return LayerInputHandle(true, &outputSlot, operandTensorInfo); |
| 848 | } |
| 849 | else |
| 850 | { |
| 851 | Fail("%s: invalid operand tensor", __func__); |
| 852 | return LayerInputHandle(); |
| 853 | } |
| 854 | break; |
| 855 | } |
| 856 | default: |
| 857 | { |
| 858 | // Unsupported lifetime for an input tensor |
| 859 | Fail("%s: unsupported lifetime for input tensor: %s", |
| 860 | __func__, toString(operand->lifetime).c_str()); |
| 861 | return LayerInputHandle(); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | template<typename HalOperation, typename HalModel> |
| 867 | bool ConvertToActivation(const HalOperation& operation, |
| 868 | const char* operationName, |
| 869 | const armnn::ActivationDescriptor& activationDesc, |
| 870 | const HalModel& model, |
| 871 | ConversionData& data) |
| 872 | { |
| 873 | LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data); |
| 874 | if (!input.IsValid()) |
| 875 | { |
| 876 | return Fail("%s: Input 0 is invalid", operationName); |
| 877 | } |
| 878 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 879 | const V1_0::Operand* outputOperand = GetOutputOperand(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 880 | if (!outputOperand) |
| 881 | { |
| 882 | return false; |
| 883 | } |
| 884 | const armnn::TensorInfo outInfo = GetTensorInfoForOperand(*outputOperand); |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 885 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 886 | armnn::IsActivationSupported, |
| 887 | data.m_Backends, |
| 888 | input.GetTensorInfo(), |
| 889 | outInfo, |
| 890 | activationDesc)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 891 | { |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | armnn::IConnectableLayer* layer = data.m_Network->AddActivationLayer(activationDesc); |
| 896 | BOOST_ASSERT(layer != nullptr); |
| 897 | input.Connect(layer->GetInputSlot(0)); |
| 898 | |
| 899 | return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data); |
| 900 | } |
| 901 | |
| 902 | template<typename HalOperation, typename HalModel> |
| 903 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 904 | uint32_t operationOutputIndex, |
| 905 | armnn::IConnectableLayer& layer, |
| 906 | uint32_t layerOutputIndex, |
| 907 | const HalModel& model, |
| 908 | ConversionData& data) |
| 909 | { |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 910 | const V1_0::Operand* outputOperand = GetOutputOperand(operation, operationOutputIndex, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 911 | if ((outputOperand == nullptr) || (operationOutputIndex >= layer.GetNumOutputSlots())) |
| 912 | { |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | armnn::IOutputSlot& outputSlot = layer.GetOutputSlot(layerOutputIndex); |
| 917 | |
| 918 | const uint32_t operandIndex = operation.outputs[operationOutputIndex]; |
| 919 | data.m_OutputSlotForOperand[operandIndex] = &outputSlot; |
| 920 | |
| 921 | outputSlot.SetTensorInfo(GetTensorInfoForOperand(*outputOperand)); |
| 922 | |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | template<typename HalOperation, typename HalModel> |
| 927 | bool SetupAndTrackLayerOutputSlot(const HalOperation& operation, |
| 928 | uint32_t outputIndex, |
| 929 | armnn::IConnectableLayer& layer, |
| 930 | const HalModel& model, |
| 931 | ConversionData& data) |
| 932 | { |
| 933 | return SetupAndTrackLayerOutputSlot(operation, outputIndex, layer, outputIndex, model, data); |
| 934 | } |
| 935 | |
| 936 | template<typename HalOperation, typename HalModel> |
| 937 | bool ConvertPooling2d(const HalOperation& operation, |
| 938 | const char* operationName, |
| 939 | armnn::PoolingAlgorithm poolType, |
| 940 | const HalModel& model, |
| 941 | ConversionData& data) |
| 942 | { |
| 943 | LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data); |
| 944 | if (!input.IsValid()) |
| 945 | { |
| 946 | return Fail("%s: Could not read input 0", operationName); |
| 947 | } |
| 948 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 949 | const V1_0::Operand* output = GetOutputOperand(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 950 | if (!output) |
| 951 | { |
| 952 | return Fail("%s: Could not read output 0", __func__); |
| 953 | } |
| 954 | |
| 955 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 956 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 957 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 958 | armnn::Pooling2dDescriptor desc; |
| 959 | desc.m_PoolType = poolType; |
| 960 | desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor; |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 961 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 962 | |
| 963 | ActivationFn activation; |
| 964 | |
| 965 | if (operation.inputs.size() == 7) |
| 966 | { |
| 967 | // one input, 6 parameters (padding, stridex, stridey, width, height, activation type) |
| 968 | android::nn::PaddingScheme scheme; |
| 969 | if (!GetInputPaddingScheme(operation, 1, scheme, model, data) |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 970 | || !GetInputScalar(operation, 2,V1_0::OperandType::INT32, desc.m_StrideX, model, data) |
| 971 | || !GetInputScalar(operation, 3,V1_0::OperandType::INT32, desc.m_StrideY, model, data) |
| 972 | || !GetInputScalar(operation, 4,V1_0::OperandType::INT32, desc.m_PoolWidth, model, data) |
| 973 | || !GetInputScalar(operation, 5,V1_0::OperandType::INT32, desc.m_PoolHeight, model, data) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 974 | || !GetInputActivationFunction(operation, 6, activation, model, data)) |
| 975 | { |
| 976 | return Fail("%s: Operation has invalid inputs", operationName); |
| 977 | } |
| 978 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 979 | const unsigned int inputWidth = inputInfo.GetShape()[2]; |
| 980 | const unsigned int inputHeight = inputInfo.GetShape()[1]; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 981 | |
| 982 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, scheme); |
| 983 | CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, scheme); |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | // one input, 9 parameters (padding l r t b, stridex, stridey, width, height, activation type) |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame^] | 988 | if (!GetInputScalar(operation, 1,V1_0::OperandType::INT32, desc.m_PadLeft, model, data) |
| 989 | || !GetInputScalar(operation, 2,V1_0::OperandType::INT32, desc.m_PadRight, model, data) |
| 990 | || !GetInputScalar(operation, 3,V1_0::OperandType::INT32, desc.m_PadTop, model, data) |
| 991 | || !GetInputScalar(operation, 4,V1_0::OperandType::INT32, desc.m_PadBottom, model, data) |
| 992 | || !GetInputScalar(operation, 5,V1_0::OperandType::INT32, desc.m_StrideX, model, data) |
| 993 | || !GetInputScalar(operation, 6,V1_0::OperandType::INT32, desc.m_StrideY, model, data) |
| 994 | || !GetInputScalar(operation, 7,V1_0::OperandType::INT32, desc.m_PoolWidth, model, data) |
| 995 | || !GetInputScalar(operation, 8,V1_0::OperandType::INT32, desc.m_PoolHeight, model, data) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 996 | || !GetInputActivationFunction(operation, 9, activation, model, data)) |
| 997 | { |
| 998 | return Fail("%s: Operation has invalid inputs", operationName); |
| 999 | } |
| 1000 | } |
| 1001 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 1002 | if (!IsLayerSupportedForAnyBackend(__func__, |
| 1003 | armnn::IsPooling2dSupported, |
| 1004 | data.m_Backends, |
| 1005 | inputInfo, |
| 1006 | outputInfo, |
| 1007 | desc)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1008 | { |
Éanna Ó Catháin | 3d1059c | 2018-10-11 15:53:04 +0100 | [diff] [blame] | 1009 | return false; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1010 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1011 | |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1012 | armnn::IConnectableLayer* pooling2dLayer = data.m_Network->AddPooling2dLayer(desc); |
| 1013 | if (!pooling2dLayer) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1014 | { |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1015 | return Fail("%s: AddPooling2dLayer failed", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1016 | } |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1017 | |
| 1018 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, pooling2dLayer, data); |
| 1019 | if (!endLayer) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1020 | { |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1021 | return Fail("%s: ProcessActivation failed", __func__); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1022 | } |
Matteo Martincigh | 39fc547 | 2018-10-26 16:39:28 +0100 | [diff] [blame] | 1023 | |
| 1024 | input.Connect(pooling2dLayer->GetInputSlot(0)); |
| 1025 | |
| 1026 | return SetupAndTrackLayerOutputSlot(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
saoste01 | b847148 | 2018-10-10 09:44:51 +0100 | [diff] [blame] | 1029 | } // namespace armnn_driver |