ArmNN
 24.02
Pooling2DOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 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("input0_");
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  // Get the layers connected to the input slots and determine unique tensor names.
25  Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
26  input0Name = GenerateUniqueName(connectedInputLayer, 0);
27 
28  // Determine unique output tensor name.
29  outputName = GenerateUniqueOutputName(*layer, 0);
30  }
31 
32  std::vector<int> pad = {static_cast<int>(poolDescriptor->m_PadTop),
33  static_cast<int>(poolDescriptor->m_PadBottom),
34  static_cast<int>(poolDescriptor->m_PadLeft),
35  static_cast<int>(poolDescriptor->m_PadRight)};
36  std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight),
37  static_cast<int>(poolDescriptor->m_PoolWidth)};
38  std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY),
39  static_cast<int>(poolDescriptor->m_StrideX)};
40  TosaPoolAttribute attribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType()));
41 
42  auto* op = new TosaSerializationOperator(opcode,
43  Attribute_PoolAttribute,
44  &attribute,
45  {input0Name},
46  {outputName});
47 
48  std::vector<TosaSerializationTensor*> tensors;
49 
50  // Only add input tensors if connected layer is an input layer.
51  // As intermediate or constant tensors will be created separately.
52  // There also can't be duplicate tensor.
53  if(input0Name.find("input0_") != std::string::npos)
54  {
55  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
56  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
57 
58  tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
59  }
60 
61  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
62  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
63 
64  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
65 
66  // operatorInputNames/operatorOutputNames ends up being the same as
67  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
68  return new TosaSerializationBasicBlock(blockName, // name
69  mainName, // region name
70  {op}, // operators
71  tensors, // tensors
72  {input0Name}, // inputs
73  {outputName}); // outputs
74 }
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
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::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
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
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:82
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
GenerateUniqueName
std::string GenerateUniqueName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:63
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:52
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
armnn::Pooling2dDescriptor
A Pooling2dDescriptor for the Pooling2dLayer.
Definition: Descriptors.hpp:371
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:100