ArmNN
 20.05
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 #include <boost/numeric/conversion/cast.hpp>
11 
12 namespace armnn
13 {
14 
16  const PermutationVector& permutationVector, void* permuteBuffer)
17 {
18  ARMNN_ASSERT_MSG(tensor, "Invalid input tensor");
19  ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
20 
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 
38 void 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] });
50  weightInfo.SetShape({ 1,
51  weightShape[0] * weightShape[1],
52  weightShape[2],
53  weightShape[3] });
54  break;
55  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;
60  }
61 }
62 
63 template <typename DataType>
64 ConstTensor 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 
89  std::vector<DataType> weightAclOrder(height*width*inputChannels*multiplier);
90  unsigned int destinationWeightsChannel;
91  unsigned int totalChannels = inputChannels * multiplier;
92  unsigned int channelSize = height * width;
93  unsigned int inputChannel = 0;
94 
95  for (unsigned int originWeightsChannel = 0; originWeightsChannel < totalChannels; originWeightsChannel++)
96  {
97  inputChannel = originWeightsChannel % inputChannels;
98  destinationWeightsChannel = (originWeightsChannel - inputChannel) / inputChannels + multiplier * inputChannel;
99 
100  for (unsigned int i = 0; i < channelSize; i++)
101  {
102  weightAclOrder[i + destinationWeightsChannel * channelSize] =
103  weight[i + originWeightsChannel * channelSize];
104  }
105  }
106 
107  ::memcpy(permuteBuffer, weightAclOrder.data(), weightHandle.GetInfo().GetNumBytes());
108  return ConstTensor(weightHandle.GetInfo(), permuteBuffer);
109 }
110 
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 
135  DataLayout dataLayout,
136  void* permuteBuffer)
137 {
138  ARMNN_ASSERT_MSG(weightTensor, "Invalid input tensor");
139  ARMNN_ASSERT_MSG(permuteBuffer, "Invalid permute buffer");
140 
141  auto multiplier = weightTensor->GetTensorInfo().GetShape()[0];
142  auto inputChannels = weightTensor->GetTensorInfo().GetShape()[1];
143 
144  // 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 
159  // Shuffle the weights data to obtain the channel order needed used by Acl
160  if (multiplier > 1 && inputChannels > 1 && dataLayout == DataLayout::NCHW)
161  {
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;
171  case DataType::QAsymmS8:
172  case DataType::QAsymmU8:
173  weightPermuted = ReorderWeightChannelsForAcl<uint8_t>(weightPermuted, dataLayout, permuteBuffer);
174  break;
178  case DataType::QSymmS8:
179  weightPermuted = ReorderWeightChannelsForAcl<int8_t>(weightPermuted, dataLayout, permuteBuffer);
180  break;
182  default:
183  break;
184  }
185  }
186 
187  // 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 
194 int32_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 
208 } // 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:214
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
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
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