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 "TosaTestUtils.hpp" |
| 7 | |
| 8 | using namespace armnn; |
| 9 | using namespace tosa; |
| 10 | |
| 11 | TEST_SUITE("TosaOperatorMappingOneToOneTests") |
| 12 | { |
| 13 | TEST_CASE("GetTosaMapping_AdditionLayer") |
| 14 | { |
| 15 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 16 | |
| 17 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 2, 4, 2 }, { 1, 2, 4, 2 }}; |
| 18 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 19 | |
| 20 | TosaSerializationBasicBlock* basicBlock = |
| 21 | GetTosaMapping(LayerType::Addition, {&info, &info}, {&info}, BaseDescriptor(), false); |
| 22 | AssertTosaOneToOneMappingBasicBlock( |
| 23 | basicBlock, inputShape, outputShape, Op_ADD, Attribute_NONE, BaseDescriptor(), LayerType::Addition); |
| 24 | } |
| 25 | |
| 26 | TEST_CASE("GetTosaMappingFromLayer_AdditionLayer") |
| 27 | { |
| 28 | IRuntime::CreationOptions options; |
| 29 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 30 | |
| 31 | // Builds up the structure of the network. |
| 32 | INetworkPtr net(INetwork::Create()); |
| 33 | |
| 34 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 35 | IConnectableLayer* input1 = net->AddInputLayer(1, "input1"); |
| 36 | IConnectableLayer* add = net->AddAdditionLayer("add"); |
| 37 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 38 | |
| 39 | input0->GetOutputSlot(0).Connect(add->GetInputSlot(0)); |
| 40 | input1->GetOutputSlot(0).Connect(add->GetInputSlot(1)); |
| 41 | add->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 42 | |
| 43 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 44 | |
| 45 | input0->GetOutputSlot(0).SetTensorInfo(info); |
| 46 | input1->GetOutputSlot(0).SetTensorInfo(info); |
| 47 | add->GetOutputSlot(0).SetTensorInfo(info); |
| 48 | |
| 49 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 2, 4, 2 }, { 1, 2, 4, 2 }}; |
| 50 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 51 | |
| 52 | TosaSerializationBasicBlock* basicBlock = |
| 53 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(add), false); |
| 54 | AssertTosaOneToOneMappingBasicBlock( |
| 55 | basicBlock, inputShape, outputShape, Op_ADD, Attribute_NONE, BaseDescriptor(), LayerType::Addition); |
| 56 | } |
| 57 | |
| 58 | TEST_CASE("GetTosaMapping_MaxPool2DLayer") |
| 59 | { |
| 60 | armnn::Pooling2dDescriptor descriptor; |
| 61 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Max; |
| 62 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 63 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 64 | descriptor.m_PadLeft = 1; |
| 65 | descriptor.m_PadRight = 1; |
| 66 | descriptor.m_PadTop = 1; |
| 67 | descriptor.m_PadBottom = 1; |
| 68 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 69 | |
| 70 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 71 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 72 | |
| 73 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 74 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 75 | |
| 76 | TosaSerializationBasicBlock* basicBlock = |
| 77 | GetTosaMapping(LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor, false); |
| 78 | AssertTosaOneToOneMappingBasicBlock( |
| 79 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 80 | } |
| 81 | |
| 82 | TEST_CASE("GetTosaMappingFromLayer_MaxPool2DLayer") |
| 83 | { |
| 84 | IRuntime::CreationOptions options; |
| 85 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 86 | |
| 87 | // Builds up the structure of the network. |
| 88 | INetworkPtr net(INetwork::Create()); |
| 89 | |
| 90 | armnn::Pooling2dDescriptor descriptor; |
| 91 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Max; |
| 92 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 93 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 94 | descriptor.m_PadLeft = 1; |
| 95 | descriptor.m_PadRight = 1; |
| 96 | descriptor.m_PadTop = 1; |
| 97 | descriptor.m_PadBottom = 1; |
| 98 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 99 | |
| 100 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 101 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 102 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 103 | |
| 104 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 105 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 106 | |
| 107 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 108 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 109 | |
| 110 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 111 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 112 | |
| 113 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 114 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 115 | |
| 116 | TosaSerializationBasicBlock* basicBlock = |
| 117 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool), false); |
| 118 | AssertTosaOneToOneMappingBasicBlock( |
| 119 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 120 | } |
| 121 | |
| 122 | TEST_CASE("GetTosaMapping_AvgPool2DLayer") |
| 123 | { |
| 124 | armnn::Pooling2dDescriptor descriptor; |
| 125 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 126 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 127 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 128 | descriptor.m_PadLeft = 1; |
| 129 | descriptor.m_PadRight = 1; |
| 130 | descriptor.m_PadTop = 1; |
| 131 | descriptor.m_PadBottom = 1; |
| 132 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 133 | |
| 134 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 135 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 136 | |
| 137 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 138 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 139 | |
| 140 | TosaSerializationBasicBlock* basicBlock = |
| 141 | GetTosaMapping(LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor, false); |
| 142 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 143 | inputShape, |
| 144 | outputShape, |
| 145 | Op_AVG_POOL2D, |
| 146 | Attribute_PoolAttribute, |
| 147 | descriptor, |
| 148 | LayerType::Pooling2d); |
| 149 | } |
| 150 | |
| 151 | TEST_CASE("GetTosaMappingFromLayer_AvgPool2DLayer") |
| 152 | { |
| 153 | IRuntime::CreationOptions options; |
| 154 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 155 | |
| 156 | // Builds up the structure of the network. |
| 157 | INetworkPtr net(INetwork::Create()); |
| 158 | |
| 159 | armnn::Pooling2dDescriptor descriptor; |
| 160 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 161 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 162 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 163 | descriptor.m_PadLeft = 1; |
| 164 | descriptor.m_PadRight = 1; |
| 165 | descriptor.m_PadTop = 1; |
| 166 | descriptor.m_PadBottom = 1; |
| 167 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 168 | |
| 169 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 170 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 171 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 172 | |
| 173 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 174 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 175 | |
| 176 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 177 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 178 | |
| 179 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 180 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 181 | |
| 182 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 183 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 184 | |
| 185 | TosaSerializationBasicBlock* basicBlock = |
| 186 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool), false); |
| 187 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 188 | inputShape, |
| 189 | outputShape, |
| 190 | Op_AVG_POOL2D, |
| 191 | Attribute_PoolAttribute, |
| 192 | descriptor, |
| 193 | LayerType::Pooling2d); |
| 194 | } |
| 195 | |
| 196 | TEST_CASE("GetTosaMapping_Unimplemented") |
| 197 | { |
| 198 | TosaSerializationBasicBlock* basicBlock = |
| 199 | GetTosaMapping(LayerType::UnidirectionalSequenceLstm, {}, {}, BaseDescriptor(), false); |
| 200 | |
| 201 | CHECK(basicBlock->GetName() == ""); |
| 202 | CHECK(basicBlock->GetTensors().size() == 0); |
| 203 | CHECK(basicBlock->GetOperators().size() == 1); |
| 204 | CHECK(basicBlock->GetInputs().size() == 0); |
| 205 | CHECK(basicBlock->GetOutputs().size() == 0); |
| 206 | |
| 207 | TosaSerializationOperator* op = basicBlock->GetOperators()[0]; |
| 208 | CHECK(op->GetAttributeType() == Attribute_NONE); |
| 209 | CHECK(op->GetOp() == tosa::Op_UNKNOWN); |
| 210 | CHECK(op->GetInputTensorNames().size() == 0); |
| 211 | CHECK(op->GetOutputTensorNames().size() == 0); |
| 212 | } |
| 213 | } |