ArmNN
 20.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>
9 
10 namespace armnn
11 {
12 
14  const PermutationVector& permutationVector, void* permuteBuffer)
15 {
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 
36 void 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] });
48  weightInfo.SetShape({ 1,
49  weightShape[0] * weightShape[1],
50  weightShape[2],
51  weightShape[3] });
52  break;
53  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;
58  }
59 }
60 
61 template <typename DataType>
62 ConstTensor 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 
87  std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
88  unsigned int destinationWeightsChannel;
89  unsigned int totalChannels = inputChannels * multiplier;
90  unsigned int channelSize = height * width;
91  unsigned int inputChannel = 0;
92 
93  for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
94  {
95  inputChannel = originWeightsChannel % inputChannels;
96  destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
97 
98  for (unsigned int i = 0; i < channelSize; i++)
99  {
100  weightAclOrder[i + destinationWeightsChannel * channelSize] =
101  weight[i + originWeightsChannel * channelSize];
102  }
103  }
104 
105  ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
106  return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
107 }
108 
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 
133  DataLayout dataLayout,
134  void* permuteBuffer)
135 {
136  BOOST_ASSERT_MSG(weightTensor, "Invalid input tensor");
137  BOOST_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
138 
139  auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
140  auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
141 
142  // 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 
157  // Shuffle the weights data to obtain the channel order needed used by Acl
158  if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
159  {
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;
169  case DataType::QAsymmS8:
170  case DataType::QAsymmU8:
171  weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
172  break;
176  case DataType::QSymmS8:
177  weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
178  break;
180  default:
181  break;
182  }
183  }
184 
185  // 2. Reshape the weights
186  ReshapeWeightsForAcl(weightPermuted.GetInfo(), dataLayout);
187 
188  // 3. Return both the tensor and the allocated storage to ensure that the data stays alive
189  return weightPermuted;
190 }
191 
192 int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
193 {
194  int32_t reversedMask = 0;
195  for (unsigned int i = 0; i < boost::numeric_cast<unsigned int>(numDim); ++i)
196  {
197  // Check if bit set in mask for each dimension
198  int32_t bit = (mask & 1 << i) != 0;
199  // Increment the new mask with the bits reversed
200  reversedMask += (bit << std::max(numDim-(boost::numeric_cast<int>(i)+1), 0));
201  }
202 
203  return reversedMask;
204 }
205 
206 } // namespace armnn
DataLayout
Definition: Types.hpp:49
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
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:169
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
const T * GetConstTensor() const
Copyright (c) 2020 ARM Limited.
SizeType GetSize() const
Definition: Types.hpp:202
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:90
DataType
Definition: Types.hpp:32
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
DataType GetDataType() const
Definition: Tensor.hpp:95
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
#define ARMNN_FALLTHROUGH
Definition: Utils.hpp:35
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:167
int32_t ConvertMaskToACLFormat(int32_t mask, int32_t numDim)
DataType GetDataType() const
Definition: Tensor.hpp:172
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:115