telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 7 | #include <armnn/Descriptors.hpp> |
Aron Virginas-Tar | 5c3e923 | 2018-11-16 11:00:48 +0000 | [diff] [blame] | 8 | #include <armnn/Tensor.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | |
| 10 | #include <arm_compute/core/Types.h> |
| 11 | |
Narumol Prangnawarat | 15eb583 | 2019-05-20 15:31:05 +0100 | [diff] [blame] | 12 | #include <boost/assert.hpp> |
| 13 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | inline arm_compute::NormalizationLayerInfo |
Matteo Martincigh | 539b44d | 2018-10-01 09:26:39 +0100 | [diff] [blame] | 18 | CreateAclNormalizationLayerInfoForL2Normalization(const armnn::TensorInfo& tensorInfo, |
| 19 | armnn::DataLayout dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | { |
Matteo Martincigh | 539b44d | 2018-10-01 09:26:39 +0100 | [diff] [blame] | 21 | unsigned int depthDimension = dataLayout == armnn::DataLayout::NCHW ? 1 : 3; |
| 22 | const unsigned int depth = tensorInfo.GetShape()[depthDimension]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | |
| 24 | // At the time of writing, {CL|Neon}L2Normalization performs the reduction only along dimension 0. This version of |
| 25 | // L2 Normalization always performs the reduction along the depth axis, though. Thus, we repurpose |
| 26 | // {CL|Neon}NormalizationLayers to act as depthwise L2 normalizations by carefully chosing the normalization |
| 27 | // parameters. |
| 28 | // |
| 29 | // Please refer to both the reference implementation of the normalization layer and the implementation of |
| 30 | // {CL|Neon}NormalizationLayer when checking the derivations for the parameter values below. |
| 31 | |
| 32 | // Make sure normalization covers the entire depth range. ACL requires the normalization size to be odd. |
| 33 | // CL: This does not result in extra kernel threads not doing any work: See usage of the RADIUS parameter in |
| 34 | // ACL's normalization_layer_cross_map() CL function. |
| 35 | const uint32_t normSize = depth * 2u + 1u; |
| 36 | |
| 37 | // See ACL's NormalizationLayerInfo::scale_coeff() definition. |
| 38 | // For the reference implementation, to make alpha_ become 1, we'd have to use alpha = normSize instead. |
| 39 | const float alpha = 1.0f; |
| 40 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | // Don't offset the reduction. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | const float kappa = 0.0f; |
| 43 | |
| 44 | // pow(reduction, -0.5) = 1 / sqrt(reduction) |
| 45 | const float beta = 0.5f; |
| 46 | |
| 47 | return arm_compute::NormalizationLayerInfo(arm_compute::NormType::CROSS_MAP, normSize, alpha, beta, kappa, false); |
| 48 | } |
| 49 | |
| 50 | inline arm_compute::ActivationLayerInfo::ActivationFunction |
| 51 | ConvertActivationFunctionToAclActivationFunction(ActivationFunction armnnFunction) |
| 52 | { |
| 53 | using AclActivationFunction = arm_compute::ActivationLayerInfo::ActivationFunction; |
| 54 | |
| 55 | switch (armnnFunction) |
| 56 | { |
| 57 | case ActivationFunction::Linear: return AclActivationFunction::LINEAR; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | // Arm compute's 'logistic' function is non-parameterized, so it is exactly a sigmoid function. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | case ActivationFunction::Sigmoid: return AclActivationFunction::LOGISTIC; |
| 60 | case ActivationFunction::ReLu: return AclActivationFunction::RELU; |
| 61 | case ActivationFunction::BoundedReLu: return AclActivationFunction::LU_BOUNDED_RELU; |
| 62 | case ActivationFunction::SoftReLu: return AclActivationFunction::SOFT_RELU; |
| 63 | case ActivationFunction::LeakyReLu: return AclActivationFunction::LEAKY_RELU; |
| 64 | case ActivationFunction::Abs: return AclActivationFunction::ABS; |
| 65 | case ActivationFunction::Sqrt: return AclActivationFunction::SQRT; |
| 66 | case ActivationFunction::Square: return AclActivationFunction::SQUARE; |
| 67 | case ActivationFunction::TanH: return AclActivationFunction::TANH; |
| 68 | default: throw InvalidArgumentException("Unsupported activation function"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | inline arm_compute::ActivationLayerInfo |
| 73 | ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor& actDesc) |
| 74 | { |
| 75 | return arm_compute::ActivationLayerInfo(ConvertActivationFunctionToAclActivationFunction(actDesc.m_Function), |
| 76 | actDesc.m_A, actDesc.m_B); |
| 77 | } |
| 78 | |
| 79 | inline arm_compute::PoolingType ConvertPoolingAlgorithmToAclPoolingType(PoolingAlgorithm poolingAlgorithm) |
| 80 | { |
| 81 | using arm_compute::PoolingType; |
| 82 | |
| 83 | switch (poolingAlgorithm) |
| 84 | { |
| 85 | case PoolingAlgorithm::Max: return PoolingType::MAX; |
| 86 | case PoolingAlgorithm::Average: return PoolingType::AVG; |
| 87 | case PoolingAlgorithm::L2: return PoolingType::L2; |
| 88 | default: throw InvalidArgumentException("Unsupported pooling algorithm"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | inline arm_compute::DimensionRoundingType ConvertOutputShapeRoundingToAclDimensionRoundingType(OutputShapeRounding |
| 93 | rounding) |
| 94 | { |
| 95 | using arm_compute::DimensionRoundingType; |
| 96 | |
| 97 | switch (rounding) |
| 98 | { |
| 99 | case OutputShapeRounding::Ceiling: return DimensionRoundingType::CEIL; |
| 100 | case OutputShapeRounding::Floor: return DimensionRoundingType::FLOOR; |
| 101 | default: throw InvalidArgumentException("Unsupported Output Shape Rounding type"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | inline arm_compute::NormType |
| 106 | ConvertNormalizationAlgorithmChannelToAclNormType(NormalizationAlgorithmChannel channelType) |
| 107 | { |
| 108 | using arm_compute::NormType; |
| 109 | switch (channelType) |
| 110 | { |
| 111 | case NormalizationAlgorithmChannel::Across: return NormType::CROSS_MAP; |
| 112 | case NormalizationAlgorithmChannel::Within: return NormType::IN_MAP_2D; |
| 113 | default: throw InvalidArgumentException("Unsupported normalization algorithm channel type"); |
| 114 | } |
| 115 | } |
| 116 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | inline arm_compute::FullyConnectedLayerInfo |
| 118 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(const FullyConnectedDescriptor& fullyConnectedDesc) |
| 119 | { |
| 120 | arm_compute::FullyConnectedLayerInfo fc_info; |
| 121 | fc_info.transpose_weights = fullyConnectedDesc.m_TransposeWeightMatrix; |
| 122 | return fc_info; |
| 123 | } |
| 124 | |
Aron Virginas-Tar | cc0cefb | 2019-07-02 17:25:47 +0100 | [diff] [blame] | 125 | inline arm_compute::InterpolationPolicy ConvertResizeMethodToAclInterpolationPolicy(ResizeMethod resizeMethod) |
| 126 | { |
| 127 | switch (resizeMethod) |
| 128 | { |
| 129 | case ResizeMethod::Bilinear: |
| 130 | return arm_compute::InterpolationPolicy::BILINEAR; |
| 131 | case ResizeMethod::NearestNeighbor: |
| 132 | return arm_compute::InterpolationPolicy::NEAREST_NEIGHBOR; |
| 133 | default: |
| 134 | throw InvalidArgumentException("Unsupported resize method"); |
| 135 | } |
| 136 | } |
| 137 | |
Colm Donelan | c3c5fc2 | 2019-08-15 16:03:17 +0100 | [diff] [blame] | 138 | inline unsigned int ComputeSoftmaxAclAxis(const SoftmaxDescriptor& softmaxDesc, const armnn::TensorInfo& tensor) |
Narumol Prangnawarat | 65d3096 | 2019-03-14 11:55:03 +0000 | [diff] [blame] | 139 | { |
Colm Donelan | c3c5fc2 | 2019-08-15 16:03:17 +0100 | [diff] [blame] | 140 | // Detect the Android default value of -1 and return the ACL default value of 1. |
| 141 | if (softmaxDesc.m_Axis == -1) |
| 142 | { |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | unsigned int dim = tensor.GetNumDimensions(); |
Narumol Prangnawarat | 65d3096 | 2019-03-14 11:55:03 +0000 | [diff] [blame] | 147 | |
| 148 | BOOST_ASSERT(dim != 0); |
| 149 | |
| 150 | // Currently ArmNN support axis 1. |
| 151 | return dim - 1; |
| 152 | } |
| 153 | |
Narumol Prangnawarat | 15eb583 | 2019-05-20 15:31:05 +0100 | [diff] [blame] | 154 | inline std::set<unsigned int> ComputeSplitAxis(const armnn::SplitterDescriptor& desc, const TensorShape& input) |
| 155 | { |
| 156 | unsigned int numSplit = desc.GetNumViews(); |
| 157 | unsigned int numDimensions = desc.GetNumDimensions(); |
| 158 | std::set<unsigned int> splitAxis; |
| 159 | |
| 160 | for (unsigned int i = 0; i < numSplit; ++i) |
| 161 | { |
| 162 | for (unsigned int dimIdx = 0; dimIdx < numDimensions; ++dimIdx) |
| 163 | { |
| 164 | if (desc.GetViewSizes(i)[dimIdx] != input[dimIdx]) |
| 165 | { |
| 166 | splitAxis.insert(dimIdx); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | return splitAxis; |
| 171 | } |
| 172 | |
Aron Virginas-Tar | 5c3e923 | 2018-11-16 11:00:48 +0000 | [diff] [blame] | 173 | } // namespace armnn |