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" |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 7 | #include "CommonTestUtils.hpp" |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 8 | |
| 9 | using namespace armnn; |
| 10 | using namespace tosa; |
| 11 | |
| 12 | TEST_SUITE("TosaOperatorMappingOneToOneTests") |
| 13 | { |
| 14 | TEST_CASE("GetTosaMapping_AdditionLayer") |
| 15 | { |
| 16 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 17 | |
| 18 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 2, 4, 2 }, { 1, 2, 4, 2 }}; |
| 19 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 20 | |
| 21 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 22 | GetTosaMapping(nullptr, LayerType::Addition, {&info, &info}, {&info}, BaseDescriptor()); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 23 | AssertTosaOneToOneMappingBasicBlock( |
| 24 | basicBlock, inputShape, outputShape, Op_ADD, Attribute_NONE, BaseDescriptor(), LayerType::Addition); |
| 25 | } |
| 26 | |
| 27 | TEST_CASE("GetTosaMappingFromLayer_AdditionLayer") |
| 28 | { |
| 29 | IRuntime::CreationOptions options; |
| 30 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 31 | |
| 32 | // Builds up the structure of the network. |
| 33 | INetworkPtr net(INetwork::Create()); |
| 34 | |
| 35 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 36 | IConnectableLayer* input1 = net->AddInputLayer(1, "input1"); |
| 37 | IConnectableLayer* add = net->AddAdditionLayer("add"); |
| 38 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 39 | |
| 40 | input0->GetOutputSlot(0).Connect(add->GetInputSlot(0)); |
| 41 | input1->GetOutputSlot(0).Connect(add->GetInputSlot(1)); |
| 42 | add->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 43 | |
| 44 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 45 | |
| 46 | input0->GetOutputSlot(0).SetTensorInfo(info); |
| 47 | input1->GetOutputSlot(0).SetTensorInfo(info); |
| 48 | add->GetOutputSlot(0).SetTensorInfo(info); |
| 49 | |
| 50 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 2, 4, 2 }, { 1, 2, 4, 2 }}; |
| 51 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 52 | |
| 53 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 54 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(add)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 55 | AssertTosaOneToOneMappingBasicBlock( |
| 56 | basicBlock, inputShape, outputShape, Op_ADD, Attribute_NONE, BaseDescriptor(), LayerType::Addition); |
| 57 | } |
| 58 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 59 | TEST_CASE("GetTosaMapping_ConstantLayer") |
| 60 | { |
| 61 | TensorInfo outputInfo = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 62 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 63 | |
| 64 | TosaSerializationBasicBlock* basicBlock = |
| 65 | GetTosaMapping(nullptr, LayerType::Constant, {}, {&outputInfo}, BaseDescriptor()); |
| 66 | AssertTosaOneToOneMappingBasicBlock( |
| 67 | basicBlock, {}, outputShape, Op_CONST, Attribute_NONE, BaseDescriptor(), LayerType::Constant); |
| 68 | } |
| 69 | |
| 70 | TEST_CASE("GetTosaMappingFromLayer_ConstantLayer") |
| 71 | { |
| 72 | IRuntime::CreationOptions options; |
| 73 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 74 | |
| 75 | // Builds up the structure of the network. |
| 76 | INetworkPtr net(INetwork::Create()); |
| 77 | |
| 78 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 79 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 80 | |
| 81 | std::vector<float> data = GenerateRandomData<float>(info.GetNumElements()); |
| 82 | armnn::ConstTensor constTensor(info, data); |
| 83 | |
| 84 | IConnectableLayer* constant = net->AddConstantLayer(constTensor, "constant"); |
| 85 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 86 | |
| 87 | constant->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 88 | constant->GetOutputSlot(0).SetTensorInfo(info); |
| 89 | |
| 90 | TosaSerializationBasicBlock* basicBlock = |
| 91 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(constant)); |
| 92 | AssertTosaOneToOneMappingBasicBlock( |
| 93 | basicBlock, {}, outputShape, Op_CONST, Attribute_NONE, BaseDescriptor(), LayerType::Constant); |
| 94 | } |
| 95 | |
| 96 | TEST_CASE("GetTosaMapping_Conv2dLayer") |
| 97 | { |
| 98 | armnn::Convolution2dDescriptor descriptor; |
| 99 | descriptor.m_PadLeft = 1; |
| 100 | descriptor.m_PadRight = 1; |
| 101 | descriptor.m_PadTop = 1; |
| 102 | descriptor.m_PadBottom = 1; |
| 103 | descriptor.m_StrideX = 2; |
| 104 | descriptor.m_StrideY = 2; |
| 105 | descriptor.m_DilationX = 2; |
| 106 | descriptor.m_DilationY = 2; |
| 107 | descriptor.m_BiasEnabled = true; |
| 108 | |
| 109 | const armnn::TensorInfo inputInfo ({ 1, 5, 5, 1 }, armnn::DataType::Float32); |
| 110 | const armnn::TensorInfo outputInfo({ 1, 3, 3, 1 }, armnn::DataType::Float32); |
| 111 | const armnn::TensorInfo weightsInfo({ 1, 3, 3, 1 }, armnn::DataType::Float32, 0.0f, 0, true); |
| 112 | const armnn::TensorInfo biasesInfo ({ 1 }, armnn::DataType::Float32, 0.0f, 0, true); |
| 113 | |
| 114 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 5, 5, 1 }, { 1, 3, 3, 1 }, { 1 }}; |
| 115 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 3, 3, 1 }}; |
| 116 | |
| 117 | TosaSerializationBasicBlock* basicBlock = GetTosaMapping(nullptr, |
| 118 | LayerType::Convolution2d, |
| 119 | {&inputInfo, &weightsInfo, &biasesInfo}, |
| 120 | {&outputInfo}, |
| 121 | descriptor); |
| 122 | AssertTosaOneToOneMappingBasicBlock( |
| 123 | basicBlock, inputShape, outputShape, Op_CONV2D, Attribute_ConvAttribute, descriptor, LayerType::Convolution2d); |
| 124 | } |
| 125 | |
| 126 | TEST_CASE("GetTosaMappingFromLayer_Conv2dLayer") |
| 127 | { |
| 128 | IRuntime::CreationOptions options; |
| 129 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 130 | |
| 131 | // Builds up the structure of the network. |
| 132 | INetworkPtr net(INetwork::Create()); |
| 133 | |
| 134 | armnn::Convolution2dDescriptor descriptor; |
| 135 | descriptor.m_PadLeft = 1; |
| 136 | descriptor.m_PadRight = 1; |
| 137 | descriptor.m_PadTop = 1; |
| 138 | descriptor.m_PadBottom = 1; |
| 139 | descriptor.m_StrideX = 2; |
| 140 | descriptor.m_StrideY = 2; |
| 141 | descriptor.m_DilationX = 2; |
| 142 | descriptor.m_DilationY = 2; |
| 143 | descriptor.m_BiasEnabled = true; |
| 144 | |
| 145 | const armnn::TensorInfo inputInfo ({ 1, 5, 5, 1 }, armnn::DataType::Float32); |
| 146 | const armnn::TensorInfo outputInfo({ 1, 3, 3, 1 }, armnn::DataType::Float32); |
| 147 | const armnn::TensorInfo weightsInfo({ 1, 3, 3, 1 }, armnn::DataType::Float32, 0.0f, 0, true); |
| 148 | const armnn::TensorInfo biasesInfo ({ 1 }, armnn::DataType::Float32, 0.0f, 0, true); |
| 149 | |
| 150 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 5, 5, 1 }}; |
| 151 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 3, 3, 1 }}; |
| 152 | |
| 153 | std::vector<float> weightsData = GenerateRandomData<float>(weightsInfo.GetNumElements()); |
| 154 | armnn::ConstTensor weights(weightsInfo, weightsData); |
| 155 | |
| 156 | std::vector<float> biasesData = GenerateRandomData<float>(biasesInfo.GetNumElements()); |
| 157 | armnn::ConstTensor biases(biasesInfo, biasesData); |
| 158 | |
| 159 | armnn::IConnectableLayer* const inputLayer = net->AddInputLayer(0, "input0"); |
| 160 | armnn::IConnectableLayer* const weightsLayer = net->AddConstantLayer(weights, "weights"); |
| 161 | armnn::IConnectableLayer* const biasesLayer = net->AddConstantLayer(biases, "biases"); |
| 162 | armnn::IConnectableLayer* const convLayer = net->AddConvolution2dLayer(descriptor, "conv2d"); |
| 163 | armnn::IConnectableLayer* const outputLayer = net->AddOutputLayer(0); |
| 164 | |
| 165 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
| 166 | weightsLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(1)); |
| 167 | biasesLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(2)); |
| 168 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 169 | |
| 170 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 171 | weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 172 | biasesLayer->GetOutputSlot(0).SetTensorInfo(biasesInfo); |
| 173 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 174 | |
| 175 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(convLayer)); |
| 176 | AssertTosaOneToOneMappingBasicBlock( |
| 177 | basicBlock, inputShape, outputShape, Op_CONV2D, Attribute_ConvAttribute, descriptor, LayerType::Convolution2d); |
| 178 | } |
| 179 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 180 | TEST_CASE("GetTosaMapping_MaxPool2DLayer") |
| 181 | { |
| 182 | armnn::Pooling2dDescriptor descriptor; |
| 183 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Max; |
| 184 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 185 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 186 | descriptor.m_PadLeft = 1; |
| 187 | descriptor.m_PadRight = 1; |
| 188 | descriptor.m_PadTop = 1; |
| 189 | descriptor.m_PadBottom = 1; |
| 190 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 191 | |
| 192 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 193 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 194 | |
| 195 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 196 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 197 | |
| 198 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 199 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 200 | AssertTosaOneToOneMappingBasicBlock( |
| 201 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 202 | } |
| 203 | |
| 204 | TEST_CASE("GetTosaMappingFromLayer_MaxPool2DLayer") |
| 205 | { |
| 206 | IRuntime::CreationOptions options; |
| 207 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 208 | |
| 209 | // Builds up the structure of the network. |
| 210 | INetworkPtr net(INetwork::Create()); |
| 211 | |
| 212 | armnn::Pooling2dDescriptor descriptor; |
| 213 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Max; |
| 214 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 215 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 216 | descriptor.m_PadLeft = 1; |
| 217 | descriptor.m_PadRight = 1; |
| 218 | descriptor.m_PadTop = 1; |
| 219 | descriptor.m_PadBottom = 1; |
| 220 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 221 | |
| 222 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 223 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 224 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 225 | |
| 226 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 227 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 228 | |
| 229 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 230 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 231 | |
| 232 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 233 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 234 | |
| 235 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 236 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 237 | |
| 238 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 239 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 240 | AssertTosaOneToOneMappingBasicBlock( |
| 241 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 242 | } |
| 243 | |
| 244 | TEST_CASE("GetTosaMapping_AvgPool2DLayer") |
| 245 | { |
| 246 | armnn::Pooling2dDescriptor descriptor; |
| 247 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 248 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 249 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 250 | descriptor.m_PadLeft = 1; |
| 251 | descriptor.m_PadRight = 1; |
| 252 | descriptor.m_PadTop = 1; |
| 253 | descriptor.m_PadBottom = 1; |
| 254 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 255 | |
| 256 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 257 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 258 | |
| 259 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 260 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 261 | |
| 262 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 263 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 264 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 265 | inputShape, |
| 266 | outputShape, |
| 267 | Op_AVG_POOL2D, |
| 268 | Attribute_PoolAttribute, |
| 269 | descriptor, |
| 270 | LayerType::Pooling2d); |
| 271 | } |
| 272 | |
| 273 | TEST_CASE("GetTosaMappingFromLayer_AvgPool2DLayer") |
| 274 | { |
| 275 | IRuntime::CreationOptions options; |
| 276 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 277 | |
| 278 | // Builds up the structure of the network. |
| 279 | INetworkPtr net(INetwork::Create()); |
| 280 | |
| 281 | armnn::Pooling2dDescriptor descriptor; |
| 282 | descriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 283 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 284 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 285 | descriptor.m_PadLeft = 1; |
| 286 | descriptor.m_PadRight = 1; |
| 287 | descriptor.m_PadTop = 1; |
| 288 | descriptor.m_PadBottom = 1; |
| 289 | descriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 290 | |
| 291 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 292 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 293 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 294 | |
| 295 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 296 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 297 | |
| 298 | armnn::TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 299 | armnn::TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 300 | |
| 301 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 302 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 303 | |
| 304 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 305 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 306 | |
| 307 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 308 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 309 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 310 | inputShape, |
| 311 | outputShape, |
| 312 | Op_AVG_POOL2D, |
| 313 | Attribute_PoolAttribute, |
| 314 | descriptor, |
| 315 | LayerType::Pooling2d); |
| 316 | } |
| 317 | |
| 318 | TEST_CASE("GetTosaMapping_Unimplemented") |
| 319 | { |
| 320 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame^] | 321 | GetTosaMapping(nullptr, LayerType::UnidirectionalSequenceLstm, {}, {}, BaseDescriptor()); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 322 | |
| 323 | CHECK(basicBlock->GetName() == ""); |
| 324 | CHECK(basicBlock->GetTensors().size() == 0); |
| 325 | CHECK(basicBlock->GetOperators().size() == 1); |
| 326 | CHECK(basicBlock->GetInputs().size() == 0); |
| 327 | CHECK(basicBlock->GetOutputs().size() == 0); |
| 328 | |
| 329 | TosaSerializationOperator* op = basicBlock->GetOperators()[0]; |
| 330 | CHECK(op->GetAttributeType() == Attribute_NONE); |
| 331 | CHECK(op->GetOp() == tosa::Op_UNKNOWN); |
| 332 | CHECK(op->GetInputTensorNames().size() == 0); |
| 333 | CHECK(op->GetOutputTensorNames().size() == 0); |
| 334 | } |
| 335 | } |