blob: fcdad3e21bf649d72d3e157d9cb7b0009a320ea3 [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>
Matthew Sloyan171214c2020-09-09 09:07:37 +01009#include <armnn/utility/NumericCast.hpp>
Jan Eilers53ef7952021-06-02 12:01:25 +010010#include <armnnUtils/DataLayoutIndexed.hpp>
11
12#include <fmt/format.h>
Jan Eilersbb446e52020-04-02 13:56:54 +010013
Matteo Martincigh747ef822018-12-18 09:26:39 +000014namespace armnn
15{
16
James Conroy1f58f032021-04-27 17:13:27 +010017armnn::ConstTensor PermuteTensor(const ConstTensorHandle* tensor,
Kevin May665a964a2019-08-21 16:53:50 +010018 const PermutationVector& permutationVector, void* permuteBuffer)
Matteo Martincigh747ef822018-12-18 09:26:39 +000019{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010020 ARMNN_ASSERT_MSG(tensor, "Invalid input tensor");
21 ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
Matteo Martincigh747ef822018-12-18 09:26:39 +000022
23 TensorInfo tensorInfo = tensor->GetTensorInfo();
24
25 if (permutationVector.GetSize() > 0)
26 {
27 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector);
28 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector,
29 tensor->GetConstTensor<void>(), permuteBuffer,
30 GetDataTypeSize(tensorInfo.GetDataType()));
31 }
32 else
33 {
34 ::memcpy(permuteBuffer, tensor->GetConstTensor<void>(), tensorInfo.GetNumBytes());
35 }
Cathal Corbett5b8093c2021-10-22 11:12:07 +010036 tensorInfo.SetConstant(true);
Matteo Martincigh747ef822018-12-18 09:26:39 +000037 return ConstTensor(tensorInfo, permuteBuffer);
38}
39
40void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout)
41{
42 // Reshape the weights in-place
43 const TensorShape& weightShape = weightInfo.GetShape();
44 switch (dataLayout)
45 {
46 case DataLayout::NHWC:
47 // The data layout is NHWC, reshape from [ H, W, I, M ] to [ 1, H, W, I * M ]
48 weightInfo.SetShape({ 1,
49 weightShape[0],
50 weightShape[1],
51 weightShape[2] * weightShape[3] });
Matteo Martincigh747ef822018-12-18 09:26:39 +000052 weightInfo.SetShape({ 1,
53 weightShape[0] * weightShape[1],
54 weightShape[2],
55 weightShape[3] });
56 break;
Kevin May665a964a2019-08-21 16:53:50 +010057 case DataLayout::NCHW:
58 default:
59 // The data layout is NCHW, reshape from [ M, I, H, W ] to [ 1, I * M, H, W, ]
60 weightInfo.SetShape({ 1, weightShape[0] * weightShape[1], weightShape[2], weightShape[3] });
61 break;
Matteo Martincigh747ef822018-12-18 09:26:39 +000062 }
63}
64
Kevin May665a964a2019-08-21 16:53:50 +010065template <typename DataType>
66ConstTensor ReorderWeightChannelsForAcl(const ConstTensor& weightHandle, DataLayout dataLayout, void* permuteBuffer)
67{
68 DataType* weight = static_cast<DataType*>(permuteBuffer);
69 const TensorShape& weightShape = weightHandle.GetShape();
70 unsigned int multiplier;
71 unsigned int height;
72 unsigned int width;
73 unsigned int inputChannels;
74 switch (dataLayout)
75 {
76 case DataLayout::NHWC: //It actually is [ H, W, I, M ]
77 height = weightShape[0];
78 width = weightShape[1];
79 inputChannels = weightShape[2];
80 multiplier = weightShape[3];
81 break;
82 case DataLayout::NCHW: //It actually is [ M, I, H, W ]
83 default:
84 height = weightShape[2];
85 width = weightShape[3];
86 inputChannels = weightShape[1];
87 multiplier = weightShape[0];
88 break;
89 }
90
Rob Hughes93667b12019-09-23 16:24:05 +010091 std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
Kevin May665a964a2019-08-21 16:53:50 +010092 unsigned int destinationWeightsChannel;
93 unsigned int totalChannels = inputChannels * multiplier;
94 unsigned int channelSize = height * width;
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000095 unsigned int inputChannel = 0;
Kevin May665a964a2019-08-21 16:53:50 +010096
97 for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
98 {
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000099 inputChannel = originWeightsChannel % inputChannels;
100 destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
Kevin May665a964a2019-08-21 16:53:50 +0100101
102 for (unsigned int i = 0; i < channelSize; i++)
103 {
104 weightAclOrder[i + destinationWeightsChannel * channelSize] =
105 weight[i + originWeightsChannel * channelSize];
106 }
107 }
108
Rob Hughes93667b12019-09-23 16:24:05 +0100109 ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
Kevin May665a964a2019-08-21 16:53:50 +0100110 return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
111}
112
Jan Eilers53ef7952021-06-02 12:01:25 +0100113
Matteo Martincigh747ef822018-12-18 09:26:39 +0000114TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout)
115{
116 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
117 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
118
119 // 1. Permute the weights if necessary
120 // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done
121 // starting from the current shape of [ M, I, H, W ]
122 TensorInfo weightPermutedInfo(weightInfo);
123 if (dataLayout == DataLayout::NHWC)
124 {
125 // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ]
126 PermutationVector permutationVector{ 3, 2, 0, 1 };
127 weightPermutedInfo = armnnUtils::Permuted(weightInfo, permutationVector);
128 }
129
130 // 2. Reshape the weights
131 ReshapeWeightsForAcl(weightPermutedInfo, dataLayout);
132
133 // 3. Return the permuted weight info
134 return weightPermutedInfo;
135}
136
Jan Eilers53ef7952021-06-02 12:01:25 +0100137
138std::tuple<ConstTensor, unsigned int> Convert1HWOTensorToAcl(const ConstTensorHandle* weightTensor,
139 const TensorInfo& inputInfo,
140 const DataLayout dataLayout,
141 void* permuteBuffer)
142{
143 TensorInfo weightsInfo = weightTensor->GetTensorInfo();
144 unsigned int depthMultiplier = 1;
145 PermutationVector permutationVector{};
146 if (dataLayout == armnn::DataLayout::NHWC)
147 {
148 // No permutation required. Data layouts are the same.
149
150 depthMultiplier = weightsInfo.GetShape()[3] / inputInfo.GetShape()[3];
151 }
152 else if (dataLayout == armnn::DataLayout::NCHW)
153 {
154 // [ 1, H, W, I*M] --> [ 1, I * M, H, W ]
155 depthMultiplier = weightsInfo.GetShape()[3] / inputInfo.GetShape()[1];
156 permutationVector = { 0, 2, 3, 1 };
157 }
158 else
159 {
160 throw InvalidArgumentException(fmt::format("Unknown data layout for tensor conversion: {}",
161 GetDataLayoutName(dataLayout)));
162 }
163
164 ConstTensor weightsPermuted = PermuteTensor(weightTensor, permutationVector, permuteBuffer);
165
166 return std::make_tuple(weightsPermuted, depthMultiplier);
167}
168
169std::tuple<TensorInfo, unsigned int> Convert1HWOTensorInfoToAcl(const TensorInfo& weightInfo,
170 const TensorInfo& inputInfo,
171 const DataLayout dataLayout)
172{
173 unsigned int aclDepthMultiplier = 1;
174 TensorInfo weightsPermuted;
175 if (dataLayout == armnn::DataLayout::NHWC)
176 {
177 // No permutation required. Data layouts are the same.
178 aclDepthMultiplier = weightInfo.GetShape()[3] / inputInfo.GetShape()[3];
179 weightsPermuted = weightInfo;
180 }
181 else if (dataLayout == armnn::DataLayout::NCHW)
182 {
183 // [ 1, H, W, I*M] --> [ 1, I * M, H, W ]
184 aclDepthMultiplier = weightInfo.GetShape()[3] / inputInfo.GetShape()[1];
185 PermutationVector permutationVector{ 0, 2, 3, 1 };
186 weightsPermuted = armnnUtils::Permuted(weightInfo, permutationVector);
187 }
188 else
189 {
190 throw InvalidArgumentException(fmt::format("Unknown data layout for tensor info conversion: {}",
191 GetDataLayoutName(dataLayout)));
192 }
193
194 return std::make_tuple(weightsPermuted, aclDepthMultiplier);
195}
196
197
198std::tuple<ConstTensor, unsigned int> Convert1HWOtoMIHW(const ConstTensorHandle* weightTensor,
199 const TensorInfo& inputInfo,
200 const DataLayout& dataLayout,
201 void* permuteBuffer)
202{
203 TensorInfo weightsInfo = weightTensor->GetTensorInfo();
204
205 if (weightsInfo.HasPerAxisQuantization())
206 {
207 throw InvalidArgumentException("Can't convert tensor from [1,H,W,Cout] to [M,Cin,H,W] when per channel "
208 "quantization is applied.");
209 }
210
211 // Reshape weights [ 1, H, W, I*M ] --> [ H, W, I, M ]
212 auto weightsShape = weightsInfo.GetShape();
213 auto channelIndex = armnnUtils::DataLayoutIndexed(dataLayout).GetChannelsIndex();
214 unsigned int depthMultiplier = weightsShape[3] / inputInfo.GetShape()[channelIndex];
215 weightsInfo.SetShape({ weightsShape[1],
216 weightsShape[2],
217 inputInfo.GetShape()[channelIndex],
218 depthMultiplier});
219
220 // Permute [ H, W, I, M ] --> [ M, I, H, W ]
221 PermutationVector permutationVector = { 2, 3, 1, 0 };
222 ConstTensor weightsPermuted = PermuteTensor(weightTensor, permutationVector, permuteBuffer);
223
224 return std::make_tuple(weightsPermuted, depthMultiplier);
225}
226
James Conroy1f58f032021-04-27 17:13:27 +0100227armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstTensorHandle* weightTensor,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000228 DataLayout dataLayout,
229 void* permuteBuffer)
230{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100231 ARMNN_ASSERT_MSG(weightTensor, "Invalid input tensor");
232 ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
Matteo Martincigh747ef822018-12-18 09:26:39 +0000233
Kevin May665a964a2019-08-21 16:53:50 +0100234 auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
235 auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
236
Matteo Martincigh747ef822018-12-18 09:26:39 +0000237 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
238 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
239
240 // 1. Permute the weights if necessary
241 // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done
242 // starting from the current shape of [ M, I, H, W ]
243 // If no permutation is necessary, leave the permutation vector empty
244 PermutationVector permutationVector{};
245 if (dataLayout == DataLayout::NHWC)
246 {
247 // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ]
248 permutationVector = { 3, 2, 0, 1 };
249 }
250 ConstTensor weightPermuted = PermuteTensor(weightTensor, permutationVector, permuteBuffer);
251
Kevin May665a964a2019-08-21 16:53:50 +0100252 // Shuffle the weights data to obtain the channel order needed used by Acl
Rob Hughes93667b12019-09-23 16:24:05 +0100253 if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
Kevin May665a964a2019-08-21 16:53:50 +0100254 {
255 switch (weightPermuted.GetDataType())
256 {
257 case DataType::Float32:
258 weightPermuted = ReorderWeightChannelsForAcl<float>(weightPermuted, dataLayout, permuteBuffer);
259 break;
260 case DataType::Float16:
261 weightPermuted =
262 ReorderWeightChannelsForAcl<half_float::half>(weightPermuted, dataLayout, permuteBuffer);
263 break;
Keith Davisa8565012020-02-14 12:22:40 +0000264 case DataType::QAsymmS8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000265 case DataType::QAsymmU8:
Kevin May665a964a2019-08-21 16:53:50 +0100266 weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
267 break;
Derek Lambertid466a542020-01-22 15:37:29 +0000268 case DataType::QSymmS8:
Teresa Charlina68d8532019-11-29 13:59:18 +0000269 weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
270 break;
Kevin May665a964a2019-08-21 16:53:50 +0100271 default:
272 break;
273 }
274 }
275
Matteo Martincigh747ef822018-12-18 09:26:39 +0000276 // 2. Reshape the weights
277 ReshapeWeightsForAcl(weightPermuted.GetInfo(), dataLayout);
278
279 // 3. Return both the tensor and the allocated storage to ensure that the data stays alive
280 return weightPermuted;
281}
282
Francis Murtaghec33a912019-11-05 14:26:23 +0000283int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
284{
285 int32_t reversedMask = 0;
Matthew Sloyan171214c2020-09-09 09:07:37 +0100286 for (unsigned int i = 0; i < armnn::numeric_cast<unsigned int>(numDim); ++i)
Francis Murtaghec33a912019-11-05 14:26:23 +0000287 {
288 // Check if bit set in mask for each dimension
289 int32_t bit = (mask & 1 << i) != 0;
290 // Increment the new mask with the bits reversed
Matthew Sloyan171214c2020-09-09 09:07:37 +0100291 reversedMask += (bit << std::max(numDim-(armnn::numeric_cast<int>(i)+1), 0));
Francis Murtaghec33a912019-11-05 14:26:23 +0000292 }
293
294 return reversedMask;
295}
296
Matteo Martincigh747ef822018-12-18 09:26:39 +0000297} // namespace armnn