Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 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 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 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(); |
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 | { |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 24 | // Get the layers connected to the input slots and determine unique layer names. |
| 25 | Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer(); |
| 26 | input0Name = GenerateUniqueName(connectedInputLayer, 0); |
| 27 | |
| 28 | // Get the layer connected to the output slot and determine unique layer name. |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 29 | outputName = GenerateUniqueOutputName(*layer, 0); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 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 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 42 | auto* op = new TosaSerializationOperator(opcode, |
| 43 | Attribute_PoolAttribute, |
| 44 | &attribute, |
| 45 | {input0Name}, |
| 46 | {outputName}); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 47 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 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 | } |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 60 | |
| 61 | std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); |
| 62 | DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); |
| 63 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 64 | tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {})); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 65 | |
| 66 | // operatorInputNames/operatorOutputNames ends up being the same as |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 67 | // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 68 | return new TosaSerializationBasicBlock(blockName, // name |
| 69 | {op}, // operators |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 70 | tensors, // tensors |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 71 | {input0Name}, // inputs |
| 72 | {outputName}); // outputs |
| 73 | } |