ArmNN
 24.08
ResizeOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 // Copyright © 2020, 2023 The TensorFlow Authors. All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 //
8 
9 #include <numeric>
10 #include "ResizeOperator.hpp"
11 
12 // This function is paraphrased from:
13 // tensorflow/compiler/mlir/tosa/transforms/legalize_common.cc from function convertResizeOp
14 // tensorflow/lite/kernels/internal/reference/resize_utils.h
15 TosaSerializationBasicBlock* ConvertResizeToTosaOperator(const Layer* layer,
16  const std::vector<const TensorInfo*>& inputs,
17  const std::vector<const TensorInfo*>& outputs,
18  const ResizeDescriptor* resizeDescriptor)
19 {
20  ARMNN_THROW_INVALIDARG_MSG_IF_FALSE( inputs.size() == 1,
21  "ConvertResizeToTosaOperator: Resize must have only one input." );
22  ARMNN_THROW_INVALIDARG_MSG_IF_FALSE( resizeDescriptor->m_DataLayout == DataLayout::NHWC,
23  "ConvertResizeToTosaOperator: NCHW not supported.");
24 
25  ResizeMode mode;
26  if (resizeDescriptor->m_Method == ResizeMethod::NearestNeighbor)
27  {
28  mode = tosa::ResizeMode_NEAREST;
29  }
30  else if (resizeDescriptor->m_Method == ResizeMethod::Bilinear)
31  {
32  mode = tosa::ResizeMode_BILINEAR;
33  throw armnn::InvalidArgumentException("ConvertResizeToTosaOperator: Unimplemented Resize method.");
34  }
35  else
36  {
37  throw armnn::InvalidArgumentException("ConvertResizeToTosaOperator: Unsupported Resize method.");
38  }
39 
40  std::string inputName = std::string("input_");
41  std::string outputName = std::string("output0_");
42  std::string blockName = std::string("Op_RESIZE_block_") + GetUniqueTosaMappingID();
43 
44  // If a layer is present then the block will be used for execution, so input and output names need to be determined
45  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
46  if(layer != nullptr)
47  {
48  inputName = GenerateUniqueInputName(layer->GetInputSlot(0));
49  outputName = GenerateUniqueOutputName(*layer);
50  }
51 
52  int32_t inputHeight = static_cast<int32_t>(inputs[0]->GetShape()[1]);
53  int32_t inputWidth = static_cast<int32_t>(inputs[0]->GetShape()[2]);
54 
55  int32_t outputHeight = static_cast<int32_t>(resizeDescriptor->m_TargetHeight);
56  int32_t outputWidth = static_cast<int32_t>(resizeDescriptor->m_TargetWidth);
57  bool alignCorners = resizeDescriptor->m_AlignCorners;
58  bool halfPixel = resizeDescriptor->m_HalfPixelCenters;
59 
60  // Go from ArmNN parameters (outputShape, halfPixel and alignedCorners)
61  // to TOSA parameters (scale, offset and border)
62  // Align corners sets the scaling ratio to (O - 1)/(I - 1) rather than O / I.
63  auto preprocessResizeParameters = [&](int inputSize, int outputSize, int& scale_n, int& scale_d, int& offset)
64  {
65  // Dimension is length 1, we are just sampling from one value.
66  if (inputSize == 1)
67  {
68  scale_n = outputSize;
69  scale_d = 1;
70  offset = 0;
71  return;
72  }
73 
74  // Apply if aligned and capable to be aligned.
75  // Align corners sets the scaling ratio to (OH - 1)/(IH - 1) rather than OH / IH. Same for width.
76  bool applyAligned = alignCorners && (outputSize > 1);
77  scale_n = applyAligned ? (outputSize - 1) : outputSize;
78  scale_d = applyAligned ? (inputSize - 1) : inputSize;
79 
80  // Simplify the scales, make sure they are even values.
81  int gcd = std::gcd(scale_n, scale_d);
82  scale_n = 2 * scale_n / gcd;
83  scale_d = 2 * scale_d / gcd;
84 
85  // If half pixel centers then input and output sampling positions are offset by 1/2 pixel.
86  offset = halfPixel ? (scale_d / 2 - scale_n / 2) : 0;
87 
88  // Reduce the scaling ratio if possible, we know scale_n and scale_d are even
89  if ((offset & 1) == 0)
90  {
91  scale_n /= 2;
92  scale_d /= 2;
93  offset /= 2;
94  }
95  };
96 
97  int scale_y_n, scale_y_d, offset_y;
98  int scale_x_n, scale_x_d, offset_x;
99  preprocessResizeParameters(inputHeight, outputHeight, scale_y_n, scale_y_d, offset_y);
100  preprocessResizeParameters(inputWidth, outputWidth, scale_x_n, scale_x_d, offset_x);
101 
102  int border_y = scale_y_d * (outputHeight - 1) - scale_y_n * (inputHeight - 1) + offset_y;
103  int border_x = scale_x_d * (outputWidth - 1) - scale_x_n * (inputWidth - 1) + offset_x;
104 
105  // [scale_y_n, scale_y_d, scale_x_n, scale_x_d]
106  std::vector<int16_t> scale = { static_cast<int16_t>(scale_y_n),
107  static_cast<int16_t>(scale_y_d),
108  static_cast<int16_t>(scale_x_n),
109  static_cast<int16_t>(scale_x_d) };
110 
111  // [offset_y, offset_x]
112  std::vector<int16_t> offset = { static_cast<int16_t>(offset_y),
113  static_cast<int16_t>(offset_x) };
114  // [border_y, border_x]
115  std::vector<int16_t> border = { static_cast<int16_t>(border_y),
116  static_cast<int16_t>(border_x) };
117 
118  auto isInt16Range = [](int x)
119  {
120  return (x <= std::numeric_limits<int16_t>::max()) && (x >= std::numeric_limits<int16_t>::min());
121  };
122 
123  if (inputs[0]->IsQuantized())
124  {
125  // It isn't commonly seen these numbers aren't fit within 16 bits, and won't match TFLite reference.
126  if (!isInt16Range(scale_y_n) || !isInt16Range(scale_y_d) ||
127  !isInt16Range(scale_x_n) || !isInt16Range(scale_x_d) ||
128  !isInt16Range(offset_y) || !isInt16Range(offset_x) ||
129  !isInt16Range(border_y) || !isInt16Range(border_x))
130  {
131  throw armnn::Exception("ConvertResizeToTosaOperator: stride or offset out of 16 bit range");
132  }
133  }
134 
135  TosaResizeAttribute resizeAttribute(scale, offset, border, mode);
136 
137  auto* op = new TosaSerializationOperator(Op_RESIZE,
138  Attribute_ResizeAttribute,
139  &resizeAttribute,
140  {inputName},
141  {outputName});
142 
143  std::vector<TosaSerializationTensor*> tensors;
144 
145  // Only add input tensors if connected layer is an input layer.
146  // As intermediate or constant tensors will be created separately.
147  // There also can't be duplicate tensor.
148  if(inputName.find("input_") != std::string::npos)
149  {
150  std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
151  DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
152 
153  tensors.push_back(new TosaSerializationTensor(inputName, inputShape, inputDType, {}));
154  }
155 
156  std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
157  DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
158 
159  tensors.push_back(new TosaSerializationTensor(outputName, outputShape, outputDType, {}));
160 
161  // operatorInputNames/operatorOutputNames ends up being the same as
162  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
163  return new TosaSerializationBasicBlock(blockName, // name
164  mainName, // region name
165  {op}, // operators
166  tensors, // tensors
167  {inputName}, // inputs
168  {outputName}); // outputs
169 }
armnn::ResizeDescriptor::m_HalfPixelCenters
bool m_HalfPixelCenters
Half Pixel Centers.
Definition: Descriptors.hpp:1018
armnn::ResizeDescriptor::m_TargetHeight
uint32_t m_TargetHeight
Target height value.
Definition: Descriptors.hpp:1009
armnn::ResizeDescriptor
A ResizeDescriptor for the ResizeLayer.
Definition: Descriptors.hpp:985
armnn::ResizeDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1014
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::Layer
Definition: Layer.hpp:230
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
armnn::ResizeDescriptor::m_Method
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
Definition: Descriptors.hpp:1012
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
ResizeOperator.hpp
armnn::ResizeDescriptor::m_TargetWidth
uint32_t m_TargetWidth
Target width value.
Definition: Descriptors.hpp:1007
armnn::ResizeDescriptor::m_AlignCorners
bool m_AlignCorners
Aligned corners.
Definition: Descriptors.hpp:1016
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
ConvertResizeToTosaOperator
TosaSerializationBasicBlock * ConvertResizeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ResizeDescriptor *resizeDescriptor)
Definition: ResizeOperator.cpp:15
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210