Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 8cfd059 | 2024-04-23 16:22:47 +0100 | [diff] [blame^] | 2 | // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved. |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Pooling2DOperator.hpp" |
| 7 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 8 | TosaSerializationBasicBlock* ConvertPooling2DToTosaOperator(const Layer* layer, |
| 9 | const std::vector<const TensorInfo*>& inputs, |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 10 | const std::vector<const TensorInfo*>& outputs, |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 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 | |
Teresa Charlin | 8cfd059 | 2024-04-23 16:22:47 +0100 | [diff] [blame^] | 16 | std::string input0Name = std::string("input_"); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 17 | std::string outputName = std::string("output0_"); |
| 18 | std::string blockName = std::string("Op_") + poolType + std::string("_POOL2D_block_") + GetUniqueTosaMappingID(); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 19 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 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) |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 23 | { |
Teresa Charlin | 8cfd059 | 2024-04-23 16:22:47 +0100 | [diff] [blame^] | 24 | input0Name = GenerateUniqueInputName(layer->GetInputSlot(0)); |
| 25 | outputName = GenerateUniqueOutputName(*layer); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 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 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 38 | auto* op = new TosaSerializationOperator(opcode, |
| 39 | Attribute_PoolAttribute, |
| 40 | &attribute, |
| 41 | {input0Name}, |
| 42 | {outputName}); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 43 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 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. |
Teresa Charlin | 8cfd059 | 2024-04-23 16:22:47 +0100 | [diff] [blame^] | 49 | if(input0Name.find("input_") != std::string::npos) |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 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 | } |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 56 | |
| 57 | std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); |
| 58 | DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); |
| 59 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 60 | tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {})); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 61 | |
| 62 | // operatorInputNames/operatorOutputNames ends up being the same as |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 63 | // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 64 | return new TosaSerializationBasicBlock(blockName, // name |
Narumol Prangnawarat | ad323af | 2023-09-29 17:00:38 +0100 | [diff] [blame] | 65 | mainName, // region name |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 66 | {op}, // operators |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame] | 67 | tensors, // tensors |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 68 | {input0Name}, // inputs |
| 69 | {outputName}); // outputs |
| 70 | } |