Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 6 | #include <backendsCommon/WorkloadUtils.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 7 | |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 8 | #include <armnn/Utils.hpp> |
| 9 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 10 | #include <boost/numeric/conversion/cast.hpp> |
| 11 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 12 | namespace armnn |
| 13 | { |
| 14 | |
| 15 | armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor, |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 16 | const PermutationVector& permutationVector, void* permuteBuffer) |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 17 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 18 | ARMNN_ASSERT_MSG(tensor, "Invalid input tensor"); |
| 19 | ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer"); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 20 | |
| 21 | TensorInfo tensorInfo = tensor->GetTensorInfo(); |
| 22 | |
| 23 | if (permutationVector.GetSize() > 0) |
| 24 | { |
| 25 | tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector); |
| 26 | armnnUtils::Permute(tensorInfo.GetShape(), permutationVector, |
| 27 | tensor->GetConstTensor<void>(), permuteBuffer, |
| 28 | GetDataTypeSize(tensorInfo.GetDataType())); |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | ::memcpy(permuteBuffer, tensor->GetConstTensor<void>(), tensorInfo.GetNumBytes()); |
| 33 | } |
| 34 | |
| 35 | return ConstTensor(tensorInfo, permuteBuffer); |
| 36 | } |
| 37 | |
| 38 | void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout) |
| 39 | { |
| 40 | // Reshape the weights in-place |
| 41 | const TensorShape& weightShape = weightInfo.GetShape(); |
| 42 | switch (dataLayout) |
| 43 | { |
| 44 | case DataLayout::NHWC: |
| 45 | // The data layout is NHWC, reshape from [ H, W, I, M ] to [ 1, H, W, I * M ] |
| 46 | weightInfo.SetShape({ 1, |
| 47 | weightShape[0], |
| 48 | weightShape[1], |
| 49 | weightShape[2] * weightShape[3] }); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 50 | weightInfo.SetShape({ 1, |
| 51 | weightShape[0] * weightShape[1], |
| 52 | weightShape[2], |
| 53 | weightShape[3] }); |
| 54 | break; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 55 | case DataLayout::NCHW: |
| 56 | default: |
| 57 | // The data layout is NCHW, reshape from [ M, I, H, W ] to [ 1, I * M, H, W, ] |
| 58 | weightInfo.SetShape({ 1, weightShape[0] * weightShape[1], weightShape[2], weightShape[3] }); |
| 59 | break; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 63 | template <typename DataType> |
| 64 | ConstTensor ReorderWeightChannelsForAcl(const ConstTensor& weightHandle, DataLayout dataLayout, void* permuteBuffer) |
| 65 | { |
| 66 | DataType* weight = static_cast<DataType*>(permuteBuffer); |
| 67 | const TensorShape& weightShape = weightHandle.GetShape(); |
| 68 | unsigned int multiplier; |
| 69 | unsigned int height; |
| 70 | unsigned int width; |
| 71 | unsigned int inputChannels; |
| 72 | switch (dataLayout) |
| 73 | { |
| 74 | case DataLayout::NHWC: //It actually is [ H, W, I, M ] |
| 75 | height = weightShape[0]; |
| 76 | width = weightShape[1]; |
| 77 | inputChannels = weightShape[2]; |
| 78 | multiplier = weightShape[3]; |
| 79 | break; |
| 80 | case DataLayout::NCHW: //It actually is [ M, I, H, W ] |
| 81 | default: |
| 82 | height = weightShape[2]; |
| 83 | width = weightShape[3]; |
| 84 | inputChannels = weightShape[1]; |
| 85 | multiplier = weightShape[0]; |
| 86 | break; |
| 87 | } |
| 88 | |
Rob Hughes | 93667b1 | 2019-09-23 16:24:05 +0100 | [diff] [blame] | 89 | std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 90 | unsigned int destinationWeightsChannel; |
| 91 | unsigned int totalChannels = inputChannels * multiplier; |
| 92 | unsigned int channelSize = height * width; |
Teresa Charlin | 93cbbcc | 2019-12-18 22:10:47 +0000 | [diff] [blame] | 93 | unsigned int inputChannel = 0; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 94 | |
| 95 | for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++) |
| 96 | { |
Teresa Charlin | 93cbbcc | 2019-12-18 22:10:47 +0000 | [diff] [blame] | 97 | inputChannel = originWeightsChannel % inputChannels; |
| 98 | destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel; |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 99 | |
| 100 | for (unsigned int i = 0; i < channelSize; i++) |
| 101 | { |
| 102 | weightAclOrder[i + destinationWeightsChannel * channelSize] = |
| 103 | weight[i + originWeightsChannel * channelSize]; |
| 104 | } |
| 105 | } |
| 106 | |
Rob Hughes | 93667b1 | 2019-09-23 16:24:05 +0100 | [diff] [blame] | 107 | ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes()); |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 108 | return ConstTensor(weightHandle.GetInfo(), permuteBuffer); |
| 109 | } |
| 110 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 111 | TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout) |
| 112 | { |
| 113 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 114 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 115 | |
| 116 | // 1. Permute the weights if necessary |
| 117 | // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done |
| 118 | // starting from the current shape of [ M, I, H, W ] |
| 119 | TensorInfo weightPermutedInfo(weightInfo); |
| 120 | if (dataLayout == DataLayout::NHWC) |
| 121 | { |
| 122 | // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ] |
| 123 | PermutationVector permutationVector{ 3, 2, 0, 1 }; |
| 124 | weightPermutedInfo = armnnUtils::Permuted(weightInfo, permutationVector); |
| 125 | } |
| 126 | |
| 127 | // 2. Reshape the weights |
| 128 | ReshapeWeightsForAcl(weightPermutedInfo, dataLayout); |
| 129 | |
| 130 | // 3. Return the permuted weight info |
| 131 | return weightPermutedInfo; |
| 132 | } |
| 133 | |
| 134 | armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor, |
| 135 | DataLayout dataLayout, |
| 136 | void* permuteBuffer) |
| 137 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 138 | ARMNN_ASSERT_MSG(weightTensor, "Invalid input tensor"); |
| 139 | ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer"); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 140 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 141 | auto multiplier = weightTensor->GetTensorInfo().GetShape()[0]; |
| 142 | auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1]; |
| 143 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 144 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 145 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 146 | |
| 147 | // 1. Permute the weights if necessary |
| 148 | // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done |
| 149 | // starting from the current shape of [ M, I, H, W ] |
| 150 | // If no permutation is necessary, leave the permutation vector empty |
| 151 | PermutationVector permutationVector{}; |
| 152 | if (dataLayout == DataLayout::NHWC) |
| 153 | { |
| 154 | // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ] |
| 155 | permutationVector = { 3, 2, 0, 1 }; |
| 156 | } |
| 157 | ConstTensor weightPermuted = PermuteTensor(weightTensor, permutationVector, permuteBuffer); |
| 158 | |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 159 | // Shuffle the weights data to obtain the channel order needed used by Acl |
Rob Hughes | 93667b1 | 2019-09-23 16:24:05 +0100 | [diff] [blame] | 160 | if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW) |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 161 | { |
| 162 | switch (weightPermuted.GetDataType()) |
| 163 | { |
| 164 | case DataType::Float32: |
| 165 | weightPermuted = ReorderWeightChannelsForAcl<float>(weightPermuted, dataLayout, permuteBuffer); |
| 166 | break; |
| 167 | case DataType::Float16: |
| 168 | weightPermuted = |
| 169 | ReorderWeightChannelsForAcl<half_float::half>(weightPermuted, dataLayout, permuteBuffer); |
| 170 | break; |
Keith Davis | a856501 | 2020-02-14 12:22:40 +0000 | [diff] [blame] | 171 | case DataType::QAsymmS8: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 172 | case DataType::QAsymmU8: |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 173 | weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer); |
| 174 | break; |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 175 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Teresa Charlin | a68d853 | 2019-11-29 13:59:18 +0000 | [diff] [blame] | 176 | case DataType::QuantizedSymm8PerAxis: |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 177 | ARMNN_FALLTHROUGH; |
| 178 | case DataType::QSymmS8: |
Teresa Charlin | a68d853 | 2019-11-29 13:59:18 +0000 | [diff] [blame] | 179 | weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer); |
| 180 | break; |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 181 | ARMNN_NO_DEPRECATE_WARN_END |
Kevin May | 665a964a | 2019-08-21 16:53:50 +0100 | [diff] [blame] | 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 187 | // 2. Reshape the weights |
| 188 | ReshapeWeightsForAcl(weightPermuted.GetInfo(), dataLayout); |
| 189 | |
| 190 | // 3. Return both the tensor and the allocated storage to ensure that the data stays alive |
| 191 | return weightPermuted; |
| 192 | } |
| 193 | |
Francis Murtagh | ec33a91 | 2019-11-05 14:26:23 +0000 | [diff] [blame] | 194 | int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim) |
| 195 | { |
| 196 | int32_t reversedMask = 0; |
| 197 | for (unsigned int i = 0; i < boost::numeric_cast<unsigned int>(numDim); ++i) |
| 198 | { |
| 199 | // Check if bit set in mask for each dimension |
| 200 | int32_t bit = (mask & 1 << i) != 0; |
| 201 | // Increment the new mask with the bits reversed |
| 202 | reversedMask += (bit << std::max(numDim-(boost::numeric_cast<int>(i)+1), 0)); |
| 203 | } |
| 204 | |
| 205 | return reversedMask; |
| 206 | } |
| 207 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 208 | } // namespace armnn |