blob: 69a62914e5c9ddd2a09351c8d2a97e362e3c32b7 [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
Matteo Martincigh747ef822018-12-18 09:26:39 +000010namespace armnn
11{
12
13armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle* tensor,
Kevin May665a964a2019-08-21 16:53:50 +010014 const PermutationVector& permutationVector, void* permuteBuffer)
Matteo Martincigh747ef822018-12-18 09:26:39 +000015{
16 BOOST_ASSERT_MSG(tensor, "Invalid input tensor");
17 BOOST_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
18
19 TensorInfo tensorInfo = tensor->GetTensorInfo();
20
21 if (permutationVector.GetSize() > 0)
22 {
23 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector);
24 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector,
25 tensor->GetConstTensor<void>(), permuteBuffer,
26 GetDataTypeSize(tensorInfo.GetDataType()));
27 }
28 else
29 {
30 ::memcpy(permuteBuffer, tensor->GetConstTensor<void>(), tensorInfo.GetNumBytes());
31 }
32
33 return ConstTensor(tensorInfo, permuteBuffer);
34}
35
36void ReshapeWeightsForAcl(TensorInfo& weightInfo, DataLayout dataLayout)
37{
38 // Reshape the weights in-place
39 const TensorShape& weightShape = weightInfo.GetShape();
40 switch (dataLayout)
41 {
42 case DataLayout::NHWC:
43 // The data layout is NHWC, reshape from [ H, W, I, M ] to [ 1, H, W, I * M ]
44 weightInfo.SetShape({ 1,
45 weightShape[0],
46 weightShape[1],
47 weightShape[2] * weightShape[3] });
Matteo Martincigh747ef822018-12-18 09:26:39 +000048 weightInfo.SetShape({ 1,
49 weightShape[0] * weightShape[1],
50 weightShape[2],
51 weightShape[3] });
52 break;
Kevin May665a964a2019-08-21 16:53:50 +010053 case DataLayout::NCHW:
54 default:
55 // The data layout is NCHW, reshape from [ M, I, H, W ] to [ 1, I * M, H, W, ]
56 weightInfo.SetShape({ 1, weightShape[0] * weightShape[1], weightShape[2], weightShape[3] });
57 break;
Matteo Martincigh747ef822018-12-18 09:26:39 +000058 }
59}
60
Kevin May665a964a2019-08-21 16:53:50 +010061template <typename DataType>
62ConstTensor ReorderWeightChannelsForAcl(const ConstTensor& weightHandle, DataLayout dataLayout, void* permuteBuffer)
63{
64 DataType* weight = static_cast<DataType*>(permuteBuffer);
65 const TensorShape& weightShape = weightHandle.GetShape();
66 unsigned int multiplier;
67 unsigned int height;
68 unsigned int width;
69 unsigned int inputChannels;
70 switch (dataLayout)
71 {
72 case DataLayout::NHWC: //It actually is [ H, W, I, M ]
73 height = weightShape[0];
74 width = weightShape[1];
75 inputChannels = weightShape[2];
76 multiplier = weightShape[3];
77 break;
78 case DataLayout::NCHW: //It actually is [ M, I, H, W ]
79 default:
80 height = weightShape[2];
81 width = weightShape[3];
82 inputChannels = weightShape[1];
83 multiplier = weightShape[0];
84 break;
85 }
86
Rob Hughes93667b12019-09-23 16:24:05 +010087 std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
Kevin May665a964a2019-08-21 16:53:50 +010088 unsigned int destinationWeightsChannel;
89 unsigned int totalChannels = inputChannels * multiplier;
90 unsigned int channelSize = height * width;
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000091 unsigned int inputChannel = 0;
Kevin May665a964a2019-08-21 16:53:50 +010092
93 for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
94 {
Teresa Charlin93cbbcc2019-12-18 22:10:47 +000095 inputChannel = originWeightsChannel % inputChannels;
96 destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
Kevin May665a964a2019-08-21 16:53:50 +010097
98 for (unsigned int i = 0; i < channelSize; i++)
99 {
100 weightAclOrder[i + destinationWeightsChannel * channelSize] =
101 weight[i + originWeightsChannel * channelSize];
102 }
103 }
104
Rob Hughes93667b12019-09-23 16:24:05 +0100105 ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
Kevin May665a964a2019-08-21 16:53:50 +0100106 return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
107}
108
Matteo Martincigh747ef822018-12-18 09:26:39 +0000109TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo& weightInfo, DataLayout dataLayout)
110{
111 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
112 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
113
114 // 1. Permute the weights if necessary
115 // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done
116 // starting from the current shape of [ M, I, H, W ]
117 TensorInfo weightPermutedInfo(weightInfo);
118 if (dataLayout == DataLayout::NHWC)
119 {
120 // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ]
121 PermutationVector permutationVector{ 3, 2, 0, 1 };
122 weightPermutedInfo = armnnUtils::Permuted(weightInfo, permutationVector);
123 }
124
125 // 2. Reshape the weights
126 ReshapeWeightsForAcl(weightPermutedInfo, dataLayout);
127
128 // 3. Return the permuted weight info
129 return weightPermutedInfo;
130}
131
132armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle* weightTensor,
133 DataLayout dataLayout,
134 void* permuteBuffer)
135{
136 BOOST_ASSERT_MSG(weightTensor, "Invalid input tensor");
137 BOOST_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
138
Kevin May665a964a2019-08-21 16:53:50 +0100139 auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
140 auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
141
Matteo Martincigh747ef822018-12-18 09:26:39 +0000142 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
143 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
144
145 // 1. Permute the weights if necessary
146 // If the data layout is NCHW no permutation is necessary, as a reshape to [ 1, I * M, H, W ] can be better done
147 // starting from the current shape of [ M, I, H, W ]
148 // If no permutation is necessary, leave the permutation vector empty
149 PermutationVector permutationVector{};
150 if (dataLayout == DataLayout::NHWC)
151 {
152 // The data layout is NHWC, then permute the weights from [ M, I, H, W ] to [ H, W, I, M ]
153 permutationVector = { 3, 2, 0, 1 };
154 }
155 ConstTensor weightPermuted = PermuteTensor(weightTensor, permutationVector, permuteBuffer);
156
Kevin May665a964a2019-08-21 16:53:50 +0100157 // Shuffle the weights data to obtain the channel order needed used by Acl
Rob Hughes93667b12019-09-23 16:24:05 +0100158 if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
Kevin May665a964a2019-08-21 16:53:50 +0100159 {
160 switch (weightPermuted.GetDataType())
161 {
162 case DataType::Float32:
163 weightPermuted = ReorderWeightChannelsForAcl<float>(weightPermuted, dataLayout, permuteBuffer);
164 break;
165 case DataType::Float16:
166 weightPermuted =
167 ReorderWeightChannelsForAcl<half_float::half>(weightPermuted, dataLayout, permuteBuffer);
168 break;
Derek Lambertif90c56d2020-01-10 17:14:08 +0000169 case DataType::QAsymmU8:
Kevin May665a964a2019-08-21 16:53:50 +0100170 weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
171 break;
Derek Lambertid466a542020-01-22 15:37:29 +0000172 ARMNN_NO_DEPRECATE_WARN_BEGIN
Teresa Charlina68d8532019-11-29 13:59:18 +0000173 case DataType::QuantizedSymm8PerAxis:
Derek Lambertid466a542020-01-22 15:37:29 +0000174 ARMNN_FALLTHROUGH;
175 case DataType::QSymmS8:
Teresa Charlina68d8532019-11-29 13:59:18 +0000176 weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
177 break;
Derek Lambertid466a542020-01-22 15:37:29 +0000178 ARMNN_NO_DEPRECATE_WARN_END
Kevin May665a964a2019-08-21 16:53:50 +0100179 default:
180 break;
181 }
182 }
183
Matteo Martincigh747ef822018-12-18 09:26:39 +0000184 // 2. Reshape the weights
185 ReshapeWeightsForAcl(weightPermuted.GetInfo(), dataLayout);
186
187 // 3. Return both the tensor and the allocated storage to ensure that the data stays alive
188 return weightPermuted;
189}
190
Francis Murtaghec33a912019-11-05 14:26:23 +0000191int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
192{
193 int32_t reversedMask = 0;
194 for (unsigned int i = 0; i < boost::numeric_cast<unsigned int>(numDim); ++i)
195 {
196 // Check if bit set in mask for each dimension
197 int32_t bit = (mask & 1 << i) != 0;
198 // Increment the new mask with the bits reversed
199 reversedMask += (bit << std::max(numDim-(boost::numeric_cast<int>(i)+1), 0));
200 }
201
202 return reversedMask;
203}
204
Matteo Martincigh747ef822018-12-18 09:26:39 +0000205} // namespace armnn