ArmNN
 24.05
AvgPool2DIgnoreValueOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Pooling2DOperator.hpp"
7 
8 TosaSerializationBasicBlock* ConvertAvgPool2DIgnoreValueToTosaOperator(const Layer* layer,
9  const std::vector<const TensorInfo*>& inputs,
10  const std::vector<const TensorInfo*>& outputs,
11  const Pooling2dDescriptor* poolDescriptor)
12 {
13  std::string padInputName = std::string("input_");
14  std::string padOutputName = std::string("intermediate0_") + GetUniqueTosaMappingID();
15  std::string poolOutputName = std::string("output0_");
16  std::string blockName = std::string("Op_AVG_POOL2D_block_") + GetUniqueTosaMappingID();
17 
18  // If a layer is present then the block will be used for execution, so input and output names need to be determined
19  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
20  if(layer != nullptr)
21  {
22  padInputName = GenerateUniqueInputName(layer->GetInputSlot(0));
23  poolOutputName = GenerateUniqueOutputName(*layer);
24  }
25 
26  std::vector<int> paddings;
27  if (poolDescriptor->m_DataLayout == DataLayout::NHWC)
28  {
29  paddings = {0,
30  0,
31  static_cast<int>(poolDescriptor->m_PadTop),
32  static_cast<int>(poolDescriptor->m_PadBottom),
33  static_cast<int>(poolDescriptor->m_PadLeft),
34  static_cast<int>(poolDescriptor->m_PadRight),
35  0,
36  0
37  };
38  }
39  else
40  {
41  paddings = {0,
42  0,
43  0,
44  0,
45  static_cast<int>(poolDescriptor->m_PadTop),
46  static_cast<int>(poolDescriptor->m_PadBottom),
47  static_cast<int>(poolDescriptor->m_PadLeft),
48  static_cast<int>(poolDescriptor->m_PadRight)
49  };
50  }
51 
52  TosaPadAttribute padAttribute(paddings, 0, 0.0f);
53  auto* opPad = new TosaSerializationOperator(Op_PAD,
54  Attribute_PadAttribute,
55  &padAttribute,
56  {padInputName},
57  {padOutputName});
58 
59  std::vector<int> pad = {0, 0, 0, 0};
60  std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight),
61  static_cast<int>(poolDescriptor->m_PoolWidth)};
62  std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY),
63  static_cast<int>(poolDescriptor->m_StrideX)};
64  TosaPoolAttribute poolAttribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType()));
65 
66  auto* opPool = new TosaSerializationOperator(Op_AVG_POOL2D,
67  Attribute_PoolAttribute,
68  &poolAttribute,
69  {padOutputName},
70  {poolOutputName});
71 
72  std::vector<TosaSerializationTensor*> tensors;
73 
74  std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
75  DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
76 
77  // Only add input tensors if connected layer is an input layer.
78  // As intermediate or constant tensors will be created separately.
79  // There also can't be duplicate tensor.
80  if(padInputName.find("input_") != std::string::npos)
81  {
82  tensors.push_back(new TosaSerializationTensor(padInputName, inputShape, inputDType, {}));
83  }
84 
85  std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
86  DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
87 
88  std::vector<int32_t> intermediateShape;
89  if (poolDescriptor->m_DataLayout == DataLayout::NHWC)
90  {
91  intermediateShape = {inputShape[0],
92  inputShape[1] + paddings[2] + paddings[3],
93  inputShape[2] + paddings[4] + paddings[5],
94  inputShape[3]};
95  }
96  else
97  {
98  intermediateShape = {inputShape[0],
99  inputShape[1],
100  inputShape[2] + paddings[4] + paddings[5],
101  inputShape[3] + paddings[6] + paddings[7]};
102  }
103 
104  tensors.push_back(new TosaSerializationTensor(padOutputName, intermediateShape, inputDType, {}));
105  tensors.push_back(new TosaSerializationTensor(poolOutputName, outputShape, outputDType, {}));
106 
107  // operatorInputNames/operatorOutputNames ends up being the same as
108  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
109  return new TosaSerializationBasicBlock(blockName, // name
110  mainName, // region name
111  {opPad, opPool}, // operators
112  tensors, // tensors
113  {padInputName}, // inputs
114  {poolOutputName}); // outputs
115 }
armnn::Pooling2dDescriptor::m_PoolHeight
uint32_t m_PoolHeight
Pooling height value.
Definition: Descriptors.hpp:417
ConvertAvgPool2DIgnoreValueToTosaOperator
TosaSerializationBasicBlock * ConvertAvgPool2DIgnoreValueToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Pooling2dDescriptor *poolDescriptor)
Definition: AvgPool2DIgnoreValueOperator.cpp:8
armnn::Pooling2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:421
armnn::Pooling2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:411
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::Pooling2dDescriptor::m_PoolWidth
uint32_t m_PoolWidth
Pooling width value.
Definition: Descriptors.hpp:415
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::Pooling2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:427
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::Pooling2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:413
armnn::Pooling2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:409
Pooling2DOperator.hpp
armnn::Pooling2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:407
armnn::Pooling2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:419
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
armnn::Pooling2dDescriptor
A Pooling2dDescriptor for the Pooling2dLayer.
Definition: Descriptors.hpp:371
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138