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* ConvertAvgPool2DIgnoreValueToTosaOperator(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 | { |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 13 | std::string padInputName = std::string("input0_"); |
| 14 | std::string padOutputName = std::string("intermediate0_") + GetUniqueTosaMappingID(); |
| 15 | std::string poolOutputName = std::string("output0_"); |
| 16 | std::string blockName = std::string("Op_AVG_POOL2D_block_") + GetUniqueTosaMappingID(); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 17 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 18 | // If a layer is present then the block will be used for execution, so input and output names need to be determined |
| 19 | // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter. |
| 20 | if(layer != nullptr) |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 21 | { |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 22 | // Get the layers connected to the input slots and determine unique layer names. |
| 23 | Layer& connectedInputLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer(); |
| 24 | padInputName = GenerateUniqueName(connectedInputLayer, 0); |
| 25 | |
| 26 | // 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^] | 27 | poolOutputName = GenerateUniqueOutputName(*layer, 0); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | std::vector<int> paddings; |
| 31 | if (poolDescriptor->m_DataLayout == DataLayout::NHWC) |
| 32 | { |
| 33 | paddings = {0, |
| 34 | 0, |
| 35 | static_cast<int>(poolDescriptor->m_PadTop), |
| 36 | static_cast<int>(poolDescriptor->m_PadBottom), |
| 37 | static_cast<int>(poolDescriptor->m_PadLeft), |
| 38 | static_cast<int>(poolDescriptor->m_PadRight), |
| 39 | 0, |
| 40 | 0 |
| 41 | }; |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | paddings = {0, |
| 46 | 0, |
| 47 | 0, |
| 48 | 0, |
| 49 | static_cast<int>(poolDescriptor->m_PadTop), |
| 50 | static_cast<int>(poolDescriptor->m_PadBottom), |
| 51 | static_cast<int>(poolDescriptor->m_PadLeft), |
| 52 | static_cast<int>(poolDescriptor->m_PadRight) |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | TosaPadAttribute padAttribute(paddings, 0, 0.0f); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 57 | auto* opPad = new TosaSerializationOperator(Op_PAD, |
| 58 | Attribute_PadAttribute, |
| 59 | &padAttribute, |
| 60 | {padInputName}, |
| 61 | {padOutputName}); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 62 | |
| 63 | std::vector<int> pad = {0, 0, 0, 0}; |
| 64 | std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight), |
| 65 | static_cast<int>(poolDescriptor->m_PoolWidth)}; |
| 66 | std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY), |
| 67 | static_cast<int>(poolDescriptor->m_StrideX)}; |
| 68 | TosaPoolAttribute poolAttribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType())); |
| 69 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 70 | auto* opPool = new TosaSerializationOperator(Op_AVG_POOL2D, |
| 71 | Attribute_PoolAttribute, |
| 72 | &poolAttribute, |
| 73 | {padOutputName}, |
| 74 | {poolOutputName}); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 75 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 76 | std::vector<TosaSerializationTensor*> tensors; |
| 77 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 78 | std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape()); |
| 79 | DType inputDType = ArmNNToDType(inputs[0]->GetDataType()); |
| 80 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 81 | // Only add input tensors if connected layer is an input layer. |
| 82 | // As intermediate or constant tensors will be created separately. |
| 83 | // There also can't be duplicate tensor. |
| 84 | if(padInputName.find("input0_") != std::string::npos) |
| 85 | { |
| 86 | tensors.push_back(new TosaSerializationTensor(padInputName, inputShape, inputDType, {})); |
| 87 | } |
| 88 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 89 | std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape()); |
| 90 | DType outputDType = ArmNNToDType(outputs[0]->GetDataType()); |
| 91 | |
| 92 | std::vector<int32_t> intermediateShape; |
| 93 | if (poolDescriptor->m_DataLayout == DataLayout::NHWC) |
| 94 | { |
| 95 | intermediateShape = {inputShape[0], |
| 96 | inputShape[1] + paddings[2] + paddings[3], |
| 97 | inputShape[2] + paddings[4] + paddings[5], |
| 98 | inputShape[3]}; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | intermediateShape = {inputShape[0], |
| 103 | inputShape[1], |
| 104 | inputShape[2] + paddings[4] + paddings[5], |
| 105 | inputShape[3] + paddings[6] + paddings[7]}; |
| 106 | } |
| 107 | |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 108 | tensors.push_back(new TosaSerializationTensor(padOutputName, intermediateShape, inputDType, {})); |
| 109 | tensors.push_back(new TosaSerializationTensor(poolOutputName, outputShape, outputDType, {})); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 110 | |
| 111 | // operatorInputNames/operatorOutputNames ends up being the same as |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 112 | // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 113 | return new TosaSerializationBasicBlock(blockName, // name |
| 114 | {opPad, opPool}, // operators |
Matthew Sloyan | da6bf9e | 2022-12-14 10:16:27 +0000 | [diff] [blame^] | 115 | tensors, // tensors |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 116 | {padInputName}, // inputs |
| 117 | {poolOutputName}); // outputs |
| 118 | } |