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