ArmNN
 21.02
WorkloadUtils.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <armnn/Utils.hpp>
10 
11 namespace armnn
12 {
13 
15  const PermutationVector& permutationVector, void* permuteBuffer)
16 {
17  ARMNN_ASSERT_MSG(tensor, "Invalid input tensor");
18  ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
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] });
49  weightInfo.SetShape({ 1,
50  weightShape[0] * weightShape[1],
51  weightShape[2],
52  weightShape[3] });
53  break;
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;
59  }
60 }
61 
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 
88  std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
89  unsigned int destinationWeightsChannel;
90  unsigned int totalChannels = inputChannels * multiplier;
91  unsigned int channelSize = height * width;
92  unsigned int inputChannel = 0;
93 
94  for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
95  {
96  inputChannel = originWeightsChannel % inputChannels;
97  destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
98 
99  for (unsigned int i = 0; i < channelSize; i++)
100  {
101  weightAclOrder[i + destinationWeightsChannel * channelSize] =
102  weight[i + originWeightsChannel * channelSize];
103  }
104  }
105 
106  ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
107  return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
108 }
109 
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 
134  DataLayout dataLayout,
135  void* permuteBuffer)
136 {
137  ARMNN_ASSERT_MSG(weightTensor, "Invalid input tensor");
138  ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
139 
140  auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
141  auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
142 
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 
158  // Shuffle the weights data to obtain the channel order needed used by Acl
159  if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
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;
170  case DataType::QAsymmS8:
171  case DataType::QAsymmU8:
172  weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
173  break;
177  case DataType::QSymmS8:
178  weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
179  break;
181  default:
182  break;
183  }
184  }
185 
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 
193 int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
194 {
195  int32_t reversedMask = 0;
196  for (unsigned int i = 0; i < armnn::numeric_cast<unsigned int>(numDim); ++i)
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
201  reversedMask += (bit << std::max(numDim-(armnn::numeric_cast<int>(i)+1), 0));
202  }
203 
204  return reversedMask;
205 }
206 
207 } // namespace armnn
DataLayout
Definition: Types.hpp:50
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle *weightTensor, DataLayout dataLayout, void *permuteBuffer)
TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo &weightInfo, DataLayout dataLayout)
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
const TensorShape & GetShape() const
Definition: Tensor.hpp:284
unsigned int GetNumBytes() const
Definition: Tensor.cpp:418
const T * GetConstTensor() const
Copyright (c) 2021 ARM Limited and Contributors.
SizeType GetSize() const
Definition: Types.hpp:241
armnn::ConstTensor PermuteTensor(const ConstCpuTensorHandle *tensor, const PermutationVector &permutationVector, void *permuteBuffer)
ConstTensor ReorderWeightChannelsForAcl(const ConstTensor &weightHandle, DataLayout dataLayout, void *permuteBuffer)
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:189
DataType
Definition: Types.hpp:32
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
DataType GetDataType() const
Definition: Tensor.hpp:194
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
#define ARMNN_FALLTHROUGH
Definition: Utils.hpp:36
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:282
int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
DataType GetDataType() const
Definition: Tensor.hpp:287
void ReshapeWeightsForAcl(TensorInfo &weightInfo, DataLayout dataLayout)
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98
const TensorInfo & GetTensorInfo() const
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:126