ArmNN
 24.08
Pooling2DOperator.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* ConvertPooling2DToTosaOperator(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 poolType = (poolDescriptor->m_PoolType == PoolingAlgorithm::Max) ? "Op_MAX" : "Op_AVG";
14  Op opcode = (poolDescriptor->m_PoolType == PoolingAlgorithm::Max) ? Op_MAX_POOL2D : Op_AVG_POOL2D;
15 
16  std::string input0Name = std::string("input_");
17  std::string outputName = std::string("output0_");
18  std::string blockName = std::string("Op_") + poolType + std::string("_POOL2D_block_") + GetUniqueTosaMappingID();
19 
20  // If a layer is present then the block will be used for execution, so input and output names need to be determined
21  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
22  if(layer != nullptr)
23  {
24  input0Name = GenerateUniqueInputName(layer->GetInputSlot(0));
25  outputName = GenerateUniqueOutputName(*layer);
26  }
27 
28  std::vector<int> pad = {static_cast<int>(poolDescriptor->m_PadTop),
29  static_cast<int>(poolDescriptor->m_PadBottom),
30  static_cast<int>(poolDescriptor->m_PadLeft),
31  static_cast<int>(poolDescriptor->m_PadRight)};
32  std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight),
33  static_cast<int>(poolDescriptor->m_PoolWidth)};
34  std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY),
35  static_cast<int>(poolDescriptor->m_StrideX)};
36  TosaPoolAttribute attribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType()));
37 
38  auto* op = new TosaSerializationOperator(opcode,
39  Attribute_PoolAttribute,
40  &attribute,
41  {input0Name},
42  {outputName});
43 
44  std::vector<TosaSerializationTensor*> tensors;
45 
46  // Only add input tensors if connected layer is an input layer.
47  // As intermediate or constant tensors will be created separately.
48  // There also can't be duplicate tensor.
49  if(input0Name.find("input_") != std::string::npos)
50  {
51  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
52  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
53 
54  tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
55  }
56 
57  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
58  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
59 
60  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
61 
62  // operatorInputNames/operatorOutputNames ends up being the same as
63  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
64  return new TosaSerializationBasicBlock(blockName, // name
65  mainName, // region name
66  {op}, // operators
67  tensors, // tensors
68  {input0Name}, // inputs
69  {outputName}); // outputs
70 }
armnn::Pooling2dDescriptor::m_PoolHeight
uint32_t m_PoolHeight
Pooling height value.
Definition: Descriptors.hpp:417
armnn::Pooling2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:421
ConvertPooling2DToTosaOperator
TosaSerializationBasicBlock * ConvertPooling2DToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Pooling2dDescriptor *poolDescriptor)
Definition: Pooling2DOperator.cpp:8
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
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
armnn::Pooling2dDescriptor::m_PoolType
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
Definition: Descriptors.hpp:405
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138