xref: /aosp_15_r20/external/armnn/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Pooling2DOperator.hpp"
7 
ConvertPooling2DToTosaOperator(const Layer * layer,const std::vector<const TensorInfo * > & inputs,const std::vector<const TensorInfo * > & outputs,const Pooling2dDescriptor * poolDescriptor)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                                            {op}, // operators
70                                            tensors, // tensors
71                                            {input0Name}, // inputs
72                                            {outputName}); // outputs
73 }