blob: 37915cfc4d610a87b780df713f2a75c8dfdbf55e [file] [log] [blame]
Matteo Martincigh747ef822018-12-18 09:26:39 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighe5b8eb92019-11-28 15:45:42 +00006#include <backendsCommon/WorkloadUtils.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +00007
Derek Lambertid466a542020-01-22 15:37:29 +00008#include <armnn/Utils.hpp>
9
Jan Eilersbb446e52020-04-02 13:56:54 +010010#include <boost/numeric/conversion/cast.hpp>
11
Matteo Martincigh747ef822018-12-18 09:26:39 +000012namespace armnn
13{
14
15armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor,
Kevin May665a964a2019-08-21 16:53:50 +010016 const PermutationVector& permutationVector, void* permuteBuffer)
Matteo Martincigh747ef822018-12-18 09:26:39 +000017{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010018 ARMNN_ASSERT_MSG(tensor, "Invalid input tensor");
19 ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
Matteo Martincigh747ef822018-12-18 09:26:39 +000020
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
38void 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 Martincigh747ef822018-12-18 09:26:39 +000050 weightInfo.SetShape({ 1,
51 weightShape[0] * weightShape[1],
52 weightShape[2],
53 weightShape[3] });
54 break;
Kevin May665a964a2019-08-21 16:53:50 +010055 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 Martincigh747ef822018-12-18 09:26:39 +000060 }
61}
62
Kevin May665a964a2019-08-21 16:53:50 +010063template <typename DataType>
64ConstTensor 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 Hughes93667b12019-09-23 16:24:05 +010089 std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
Kevin May665a964a2019-08-21 16:53:50 +010090 unsigned int destinationWeightsChannel;
91 unsigned int totalChannels = inputChannels * multiplier;
92 unsigned int channelSize = height * width;
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000093 unsigned int inputChannel = 0;
Kevin May665a964a2019-08-21 16:53:50 +010094
95 for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
96 {
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000097 inputChannel = originWeightsChannel % inputChannels;
98 destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
Kevin May665a964a2019-08-21 16:53:50 +010099
100 for (unsigned int i = 0; i < channelSize; i++)
101 {
102 weightAclOrder[i + destinationWeightsChannel * channelSize] =
103 weight[i + originWeightsChannel * channelSize];
104 }
105 }
106
Rob Hughes93667b12019-09-23 16:24:05 +0100107 ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
Kevin May665a964a2019-08-21 16:53:50 +0100108 return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
109}
110
Matteo Martincigh747ef822018-12-18 09:26:39 +0000111TensorInfo 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
134armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor,
135 DataLayout dataLayout,
136 void* permuteBuffer)
137{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100138 ARMNN_ASSERT_MSG(weightTensor, "Invalid input tensor");
139 ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
Matteo Martincigh747ef822018-12-18 09:26:39 +0000140
Kevin May665a964a2019-08-21 16:53:50 +0100141 auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
142 auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
143
Matteo Martincigh747ef822018-12-18 09:26:39 +0000144 // 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 May665a964a2019-08-21 16:53:50 +0100159 // Shuffle the weights data to obtain the channel order needed used by Acl
Rob Hughes93667b12019-09-23 16:24:05 +0100160 if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
Kevin May665a964a2019-08-21 16:53:50 +0100161 {
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 Davisa8565012020-02-14 12:22:40 +0000171 case DataType::QAsymmS8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000172 case DataType::QAsymmU8:
Kevin May665a964a2019-08-21 16:53:50 +0100173 weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
174 break;
Derek Lambertid466a542020-01-22 15:37:29 +0000175 ARMNN_NO_DEPRECATE_WARN_BEGIN
Teresa Charlina68d8532019-11-29 13:59:18 +0000176 case DataType::QuantizedSymm8PerAxis:
Derek Lambertid466a542020-01-22 15:37:29 +0000177 ARMNN_FALLTHROUGH;
178 case DataType::QSymmS8:
Teresa Charlina68d8532019-11-29 13:59:18 +0000179 weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
180 break;
Derek Lambertid466a542020-01-22 15:37:29 +0000181 ARMNN_NO_DEPRECATE_WARN_END
Kevin May665a964a2019-08-21 16:53:50 +0100182 default:
183 break;
184 }
185 }
186
Matteo Martincigh747ef822018-12-18 09:26:39 +0000187 // 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 Murtaghec33a912019-11-05 14:26:23 +0000194int32_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 Martincigh747ef822018-12-18 09:26:39 +0000208} // namespace armnn