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 | |
Kevin May | 5b58e31 | 2022-12-15 10:15:21 +0000 | [diff] [blame^] | 59 | TEST_CASE("GetTosaMapping_ConcatLayer") |
| 60 | { |
| 61 | std::vector<armnn::TensorShape> inputTensorShapes = { { 2, 3, 2, 2 }, { 2, 3, 2, 2 } }; |
| 62 | armnn::TensorInfo input0Info(inputTensorShapes[0], DataType::Float32); |
| 63 | armnn::TensorInfo input1Info(inputTensorShapes[1], DataType::Float32); |
| 64 | armnn::TensorInfo outputInfo({ 2, 6, 2, 2 }, DataType::Float32); |
| 65 | |
| 66 | armnn::OriginsDescriptor descriptor; |
| 67 | unsigned int concatAxis = 1; |
| 68 | descriptor.SetConcatAxis(concatAxis); |
| 69 | descriptor = armnn::CreateDescriptorForConcatenation(inputTensorShapes.begin(), |
| 70 | inputTensorShapes.end(), |
| 71 | concatAxis); |
| 72 | |
| 73 | TosaSerializationBasicBlock* basicBlock = |
| 74 | GetTosaMapping(nullptr, LayerType::Concat, {&input0Info,&input1Info}, {&outputInfo}, descriptor); |
| 75 | |
| 76 | std::vector<std::vector<int32_t>> inputShapes = { { 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 77 | std::vector<std::vector<int32_t>> outputShape = { { 2, 6, 2, 2 } }; |
| 78 | |
| 79 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 80 | inputShapes, |
| 81 | outputShape, |
| 82 | Op_CONCAT, |
| 83 | Attribute_AxisAttribute, |
| 84 | descriptor, |
| 85 | LayerType::Concat); |
| 86 | } |
| 87 | |
| 88 | TEST_CASE("GetTosaMappingFromLayer_ConcatLayer") |
| 89 | { |
| 90 | IRuntime::CreationOptions options; |
| 91 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 92 | |
| 93 | // Builds up the structure of the network. |
| 94 | INetworkPtr net(INetwork::Create()); |
| 95 | |
| 96 | armnn::OriginsDescriptor descriptor; |
| 97 | unsigned int concatAxis = 1; |
| 98 | descriptor.SetConcatAxis(concatAxis); |
| 99 | std::vector<armnn::TensorShape> inputTensorShapes = { { 2, 3, 2, 2 }, { 2, 3, 2, 2 } }; |
| 100 | descriptor = armnn::CreateDescriptorForConcatenation(inputTensorShapes.begin(), |
| 101 | inputTensorShapes.end(), |
| 102 | concatAxis); |
| 103 | |
| 104 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 105 | IConnectableLayer* input1 = net->AddInputLayer(1, "input1"); |
| 106 | IConnectableLayer* concat = net->AddConcatLayer(descriptor, "concat"); |
| 107 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 108 | |
| 109 | input0->GetOutputSlot(0).Connect(concat->GetInputSlot(0)); |
| 110 | input1->GetOutputSlot(0).Connect(concat->GetInputSlot(1)); |
| 111 | concat->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 112 | |
| 113 | |
| 114 | TensorInfo inputInfo0 = TensorInfo(inputTensorShapes[0], DataType::Float32, 0.0f, 0, true); |
| 115 | TensorInfo inputInfo1 = TensorInfo(inputTensorShapes[1], DataType::Float32, 0.0f, 0, true); |
| 116 | armnn::TensorInfo outputInfo({ 2, 6, 2, 2 }, DataType::Float32); |
| 117 | |
| 118 | input0->GetOutputSlot(0).SetTensorInfo(inputInfo0); |
| 119 | input1->GetOutputSlot(0).SetTensorInfo(inputInfo1); |
| 120 | concat->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 121 | |
| 122 | std::vector<std::vector<int32_t>> inputShapes = { { 2, 3, 2, 2 }, { 2, 3, 2, 2 }}; |
| 123 | std::vector<std::vector<int32_t>> outputShape = { { 2, 6, 2, 2 } }; |
| 124 | |
| 125 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(concat)); |
| 126 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 127 | inputShapes, |
| 128 | outputShape, |
| 129 | Op_CONCAT, |
| 130 | Attribute_AxisAttribute, |
| 131 | descriptor, |
| 132 | LayerType::Concat); |
| 133 | } |
| 134 | |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 135 | TEST_CASE("GetTosaMapping_ConstantLayer") |
| 136 | { |
| 137 | TensorInfo outputInfo = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 138 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 139 | |
| 140 | TosaSerializationBasicBlock* basicBlock = |
| 141 | GetTosaMapping(nullptr, LayerType::Constant, {}, {&outputInfo}, BaseDescriptor()); |
| 142 | AssertTosaOneToOneMappingBasicBlock( |
| 143 | basicBlock, {}, outputShape, Op_CONST, Attribute_NONE, BaseDescriptor(), LayerType::Constant); |
| 144 | } |
| 145 | |
| 146 | TEST_CASE("GetTosaMappingFromLayer_ConstantLayer") |
| 147 | { |
| 148 | IRuntime::CreationOptions options; |
| 149 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 150 | |
| 151 | // Builds up the structure of the network. |
| 152 | INetworkPtr net(INetwork::Create()); |
| 153 | |
| 154 | TensorInfo info = TensorInfo({ 1, 2, 4, 2 }, DataType::Float32, 0.0f, 0, true); |
| 155 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 156 | |
| 157 | std::vector<float> data = GenerateRandomData<float>(info.GetNumElements()); |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 158 | ConstTensor constTensor(info, data); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 159 | |
| 160 | IConnectableLayer* constant = net->AddConstantLayer(constTensor, "constant"); |
| 161 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 162 | |
| 163 | constant->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 164 | constant->GetOutputSlot(0).SetTensorInfo(info); |
| 165 | |
| 166 | TosaSerializationBasicBlock* basicBlock = |
| 167 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(constant)); |
| 168 | AssertTosaOneToOneMappingBasicBlock( |
| 169 | basicBlock, {}, outputShape, Op_CONST, Attribute_NONE, BaseDescriptor(), LayerType::Constant); |
| 170 | } |
| 171 | |
| 172 | TEST_CASE("GetTosaMapping_Conv2dLayer") |
| 173 | { |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 174 | Convolution2dDescriptor descriptor; |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 175 | descriptor.m_PadLeft = 1; |
| 176 | descriptor.m_PadRight = 1; |
| 177 | descriptor.m_PadTop = 1; |
| 178 | descriptor.m_PadBottom = 1; |
| 179 | descriptor.m_StrideX = 2; |
| 180 | descriptor.m_StrideY = 2; |
| 181 | descriptor.m_DilationX = 2; |
| 182 | descriptor.m_DilationY = 2; |
| 183 | descriptor.m_BiasEnabled = true; |
| 184 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 185 | const TensorInfo inputInfo ({ 1, 5, 5, 1 }, DataType::Float32); |
| 186 | const TensorInfo outputInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 187 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 188 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 189 | |
| 190 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 5, 5, 1 }, { 1, 3, 3, 1 }, { 1 }}; |
| 191 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 3, 3, 1 }}; |
| 192 | |
| 193 | TosaSerializationBasicBlock* basicBlock = GetTosaMapping(nullptr, |
| 194 | LayerType::Convolution2d, |
| 195 | {&inputInfo, &weightsInfo, &biasesInfo}, |
| 196 | {&outputInfo}, |
| 197 | descriptor); |
| 198 | AssertTosaOneToOneMappingBasicBlock( |
| 199 | basicBlock, inputShape, outputShape, Op_CONV2D, Attribute_ConvAttribute, descriptor, LayerType::Convolution2d); |
| 200 | } |
| 201 | |
| 202 | TEST_CASE("GetTosaMappingFromLayer_Conv2dLayer") |
| 203 | { |
| 204 | IRuntime::CreationOptions options; |
| 205 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 206 | |
| 207 | // Builds up the structure of the network. |
| 208 | INetworkPtr net(INetwork::Create()); |
| 209 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 210 | Convolution2dDescriptor descriptor; |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 211 | descriptor.m_PadLeft = 1; |
| 212 | descriptor.m_PadRight = 1; |
| 213 | descriptor.m_PadTop = 1; |
| 214 | descriptor.m_PadBottom = 1; |
| 215 | descriptor.m_StrideX = 2; |
| 216 | descriptor.m_StrideY = 2; |
| 217 | descriptor.m_DilationX = 2; |
| 218 | descriptor.m_DilationY = 2; |
| 219 | descriptor.m_BiasEnabled = true; |
| 220 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 221 | const TensorInfo inputInfo ({ 1, 5, 5, 1 }, DataType::Float32); |
| 222 | const TensorInfo outputInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 223 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 224 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 225 | |
| 226 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 5, 5, 1 }}; |
| 227 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 3, 3, 1 }}; |
| 228 | |
| 229 | std::vector<float> weightsData = GenerateRandomData<float>(weightsInfo.GetNumElements()); |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 230 | ConstTensor weights(weightsInfo, weightsData); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 231 | |
| 232 | std::vector<float> biasesData = GenerateRandomData<float>(biasesInfo.GetNumElements()); |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 233 | ConstTensor biases(biasesInfo, biasesData); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 234 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 235 | IConnectableLayer* const inputLayer = net->AddInputLayer(0, "input0"); |
| 236 | IConnectableLayer* const weightsLayer = net->AddConstantLayer(weights, "weights"); |
| 237 | IConnectableLayer* const biasesLayer = net->AddConstantLayer(biases, "biases"); |
| 238 | IConnectableLayer* const convLayer = net->AddConvolution2dLayer(descriptor, "conv2d"); |
| 239 | IConnectableLayer* const outputLayer = net->AddOutputLayer(0); |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 240 | |
| 241 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
| 242 | weightsLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(1)); |
| 243 | biasesLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(2)); |
| 244 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 245 | |
| 246 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 247 | weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 248 | biasesLayer->GetOutputSlot(0).SetTensorInfo(biasesInfo); |
| 249 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 250 | |
| 251 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(convLayer)); |
| 252 | AssertTosaOneToOneMappingBasicBlock( |
| 253 | basicBlock, inputShape, outputShape, Op_CONV2D, Attribute_ConvAttribute, descriptor, LayerType::Convolution2d); |
| 254 | } |
| 255 | |
Teresa Charlin | 3fbad94 | 2022-12-15 10:35:37 +0000 | [diff] [blame] | 256 | TEST_CASE("GetTosaMapping_AvgPool2DLayer") |
| 257 | { |
| 258 | Pooling2dDescriptor descriptor; |
| 259 | descriptor.m_PoolType = PoolingAlgorithm::Average; |
| 260 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 261 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 262 | descriptor.m_PadLeft = 1; |
| 263 | descriptor.m_PadRight = 1; |
| 264 | descriptor.m_PadTop = 1; |
| 265 | descriptor.m_PadBottom = 1; |
| 266 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
| 267 | |
| 268 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 269 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 270 | |
| 271 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 272 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 273 | |
| 274 | TosaSerializationBasicBlock* basicBlock = |
| 275 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
| 276 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 277 | inputShape, |
| 278 | outputShape, |
| 279 | Op_AVG_POOL2D, |
| 280 | Attribute_PoolAttribute, |
| 281 | descriptor, |
| 282 | LayerType::Pooling2d); |
| 283 | } |
| 284 | |
| 285 | TEST_CASE("GetTosaMappingFromLayer_AvgPool2DLayer") |
| 286 | { |
| 287 | IRuntime::CreationOptions options; |
| 288 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 289 | |
| 290 | // Builds up the structure of the network. |
| 291 | INetworkPtr net(INetwork::Create()); |
| 292 | |
| 293 | Pooling2dDescriptor descriptor; |
| 294 | descriptor.m_PoolType = PoolingAlgorithm::Average; |
| 295 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 296 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 297 | descriptor.m_PadLeft = 1; |
| 298 | descriptor.m_PadRight = 1; |
| 299 | descriptor.m_PadTop = 1; |
| 300 | descriptor.m_PadBottom = 1; |
| 301 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
| 302 | |
| 303 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 304 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 305 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 306 | |
| 307 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 308 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 309 | |
| 310 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 311 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 312 | |
| 313 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 314 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 315 | |
| 316 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 317 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 318 | |
| 319 | TosaSerializationBasicBlock* basicBlock = |
| 320 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
| 321 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 322 | inputShape, |
| 323 | outputShape, |
| 324 | Op_AVG_POOL2D, |
| 325 | Attribute_PoolAttribute, |
| 326 | descriptor, |
| 327 | LayerType::Pooling2d); |
| 328 | } |
| 329 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 330 | TEST_CASE("GetTosaMapping_MaxPool2DLayer") |
| 331 | { |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 332 | Pooling2dDescriptor descriptor; |
| 333 | descriptor.m_PoolType = PoolingAlgorithm::Max; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 334 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 335 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 336 | descriptor.m_PadLeft = 1; |
| 337 | descriptor.m_PadRight = 1; |
| 338 | descriptor.m_PadTop = 1; |
| 339 | descriptor.m_PadBottom = 1; |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 340 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 341 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 342 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 343 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 344 | |
| 345 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 346 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 347 | |
| 348 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 349 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 350 | AssertTosaOneToOneMappingBasicBlock( |
| 351 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 352 | } |
| 353 | |
| 354 | TEST_CASE("GetTosaMappingFromLayer_MaxPool2DLayer") |
| 355 | { |
| 356 | IRuntime::CreationOptions options; |
| 357 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 358 | |
| 359 | // Builds up the structure of the network. |
| 360 | INetworkPtr net(INetwork::Create()); |
| 361 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 362 | Pooling2dDescriptor descriptor; |
| 363 | descriptor.m_PoolType = PoolingAlgorithm::Max; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 364 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 365 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 366 | descriptor.m_PadLeft = 1; |
| 367 | descriptor.m_PadRight = 1; |
| 368 | descriptor.m_PadTop = 1; |
| 369 | descriptor.m_PadBottom = 1; |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 370 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 371 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 372 | IConnectableLayer* input = net->AddInputLayer(0, "input0"); |
| 373 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 374 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 375 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 376 | input->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 377 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 378 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 379 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 380 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 381 | |
| 382 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 383 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 384 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 385 | input->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 386 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 387 | |
| 388 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 389 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 390 | AssertTosaOneToOneMappingBasicBlock( |
| 391 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 392 | } |
| 393 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 394 | TEST_CASE("GetTosaMapping_ReshapeLayer") |
| 395 | { |
| 396 | TensorInfo inputInfo = TensorInfo({ 2, 3 }, DataType::Float32); |
| 397 | TensorInfo outputInfo = TensorInfo({ 6 }, DataType::Float32); |
| 398 | |
| 399 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 3 }}; |
| 400 | std::vector<std::vector<int32_t>> outputShape = {{ 6 }}; |
| 401 | |
| 402 | ReshapeDescriptor descriptor; |
| 403 | descriptor.m_TargetShape = { 6 }; |
| 404 | |
| 405 | TosaSerializationBasicBlock* basicBlock = |
| 406 | GetTosaMapping(nullptr, LayerType::Reshape, {&inputInfo}, {&outputInfo}, descriptor); |
| 407 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 408 | inputShape, |
| 409 | outputShape, |
| 410 | Op_RESHAPE, |
| 411 | Attribute_ReshapeAttribute, |
| 412 | descriptor, |
| 413 | LayerType::Reshape); |
| 414 | } |
| 415 | |
| 416 | TEST_CASE("GetTosaMappingFromLayer_ReshapeLayer") |
| 417 | { |
| 418 | IRuntime::CreationOptions options; |
| 419 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 420 | |
| 421 | // Builds up the structure of the network. |
| 422 | INetworkPtr net(INetwork::Create()); |
| 423 | |
| 424 | ReshapeDescriptor descriptor; |
| 425 | descriptor.m_TargetShape = { 6 }; |
| 426 | |
| 427 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 428 | IConnectableLayer* reshape = net->AddReshapeLayer(descriptor, "reshape"); |
| 429 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 430 | |
| 431 | input->GetOutputSlot(0).Connect(reshape->GetInputSlot(0)); |
| 432 | reshape->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 433 | |
| 434 | TensorInfo inputInfo = TensorInfo({ 2, 3 }, DataType::Float32); |
| 435 | TensorInfo outputInfo = TensorInfo({ 6 }, DataType::Float32); |
| 436 | |
| 437 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 438 | reshape->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 439 | |
| 440 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 3 }}; |
| 441 | std::vector<std::vector<int32_t>> outputShape = {{ 6 }}; |
| 442 | |
| 443 | TosaSerializationBasicBlock* basicBlock = |
| 444 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(reshape)); |
| 445 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 446 | inputShape, |
| 447 | outputShape, |
| 448 | Op_RESHAPE, |
| 449 | Attribute_ReshapeAttribute, |
| 450 | descriptor, |
| 451 | LayerType::Reshape); |
| 452 | } |
| 453 | |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 454 | TEST_CASE("GetTosaMapping_SliceLayer") |
| 455 | { |
| 456 | TensorInfo inputInfo = TensorInfo({ 3, 2, 3 }, DataType::Float32); |
| 457 | TensorInfo outputInfo = TensorInfo({ 2, 1, 3 }, DataType::Float32); |
| 458 | |
| 459 | std::vector<std::vector<int32_t>> inputShape = {{ 3, 2, 3 }}; |
| 460 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 1, 3 }}; |
| 461 | |
| 462 | SliceDescriptor descriptor; |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 463 | descriptor.m_Begin = { 1, 0, 0 }; |
| 464 | descriptor.m_Size = { 2, 1, 3 }; |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 465 | |
| 466 | TosaSerializationBasicBlock* basicBlock = |
| 467 | GetTosaMapping(nullptr, LayerType::Slice, {&inputInfo}, {&outputInfo}, descriptor); |
| 468 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 469 | inputShape, |
| 470 | outputShape, |
| 471 | Op_SLICE, |
| 472 | Attribute_SliceAttribute, |
| 473 | descriptor, |
| 474 | LayerType::Slice); |
| 475 | } |
| 476 | |
| 477 | TEST_CASE("GetTosaMappingFromLayer_SliceLayer") |
| 478 | { |
| 479 | IRuntime::CreationOptions options; |
| 480 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 481 | |
| 482 | // Builds up the structure of the network. |
| 483 | INetworkPtr net(INetwork::Create()); |
| 484 | |
| 485 | TensorInfo inputInfo = TensorInfo({ 3, 2, 3 }, DataType::Float32); |
| 486 | TensorInfo outputInfo = TensorInfo({ 2, 1, 3 }, DataType::Float32); |
| 487 | |
| 488 | std::vector<std::vector<int32_t>> inputShape = {{ 3, 2, 3 }}; |
| 489 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 1, 3 }}; |
| 490 | |
| 491 | SliceDescriptor descriptor; |
| 492 | descriptor.m_Begin = { 1, 0, 0 }; |
| 493 | descriptor.m_Size = { 2, 1, 3 }; |
| 494 | |
| 495 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 496 | IConnectableLayer* slice = net->AddSliceLayer(descriptor, "slice"); |
| 497 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 498 | |
| 499 | input->GetOutputSlot(0).Connect(slice->GetInputSlot(0)); |
| 500 | slice->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 501 | |
| 502 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 503 | slice->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 504 | |
| 505 | TosaSerializationBasicBlock* basicBlock = |
| 506 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(slice)); |
| 507 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 508 | inputShape, |
| 509 | outputShape, |
| 510 | Op_SLICE, |
| 511 | Attribute_SliceAttribute, |
| 512 | descriptor, |
| 513 | LayerType::Slice); |
| 514 | } |
| 515 | |
| 516 | |
Matthew Sloyan | fc9d5e7 | 2022-12-08 13:38:23 +0000 | [diff] [blame] | 517 | TEST_CASE("GetTosaMapping_TransposeConv2dLayer") |
| 518 | { |
| 519 | const TensorInfo inputInfo ({ 1, 7, 7, 1 }, DataType::Float32); |
| 520 | const TensorInfo outputInfo({ 1, 9, 9, 1 }, DataType::Float32); |
| 521 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 522 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 523 | |
| 524 | TransposeConvolution2dDescriptor descriptor; |
| 525 | descriptor.m_PadLeft = 1; |
| 526 | descriptor.m_PadRight = 1; |
| 527 | descriptor.m_PadTop = 1; |
| 528 | descriptor.m_PadBottom = 1; |
| 529 | descriptor.m_StrideX = 1; |
| 530 | descriptor.m_StrideY = 1; |
| 531 | descriptor.m_BiasEnabled = true; |
| 532 | descriptor.m_DataLayout = DataLayout::NHWC; |
| 533 | |
| 534 | TosaSerializationBasicBlock* basicBlock = GetTosaMapping(nullptr, |
| 535 | LayerType::TransposeConvolution2d, |
| 536 | {&inputInfo, &weightsInfo, &biasesInfo}, |
| 537 | {&outputInfo}, |
| 538 | descriptor); |
| 539 | |
| 540 | CHECK(basicBlock->GetInputs().size() == 3); |
| 541 | CHECK(basicBlock->GetOutputs().size() == 1); |
| 542 | CHECK(basicBlock->GetOperators().size() == 3); |
| 543 | CHECK(basicBlock->GetTensors().size() == 4); |
| 544 | |
| 545 | CHECK(basicBlock->GetInputs()[0].find("input0_") != std::string::npos); |
| 546 | CHECK(basicBlock->GetInputs()[1].find("constant_") != std::string::npos); |
| 547 | CHECK(basicBlock->GetInputs()[2].find("constant_") != std::string::npos); |
| 548 | CHECK(basicBlock->GetOutputs()[0].find("output0_") != std::string::npos); |
| 549 | |
| 550 | VerifyTosaAttribute(descriptor, |
| 551 | basicBlock->GetOperators().at(2)->GetAttribute(), |
| 552 | {}, |
| 553 | {}, |
| 554 | LayerType::TransposeConvolution2d); |
| 555 | } |
| 556 | |
| 557 | TEST_CASE("GetTosaMappingFromLayer_TransposeConv2dLayer") |
| 558 | { |
| 559 | IRuntime::CreationOptions options; |
| 560 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 561 | |
| 562 | // Builds up the structure of the network. |
| 563 | INetworkPtr net(INetwork::Create()); |
| 564 | |
| 565 | const TensorInfo inputInfo ({ 1, 7, 7, 1 }, DataType::Float32); |
| 566 | const TensorInfo outputInfo({ 1, 9, 9, 1 }, DataType::Float32); |
| 567 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 568 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 569 | |
| 570 | std::vector<float> weightsData = GenerateRandomData<float>(weightsInfo.GetNumElements()); |
| 571 | ConstTensor weights(weightsInfo, weightsData); |
| 572 | |
| 573 | std::vector<float> biasesData = GenerateRandomData<float>(biasesInfo.GetNumElements()); |
| 574 | ConstTensor biases(biasesInfo, biasesData); |
| 575 | |
| 576 | TransposeConvolution2dDescriptor descriptor; |
| 577 | descriptor.m_PadLeft = 1; |
| 578 | descriptor.m_PadRight = 1; |
| 579 | descriptor.m_PadTop = 1; |
| 580 | descriptor.m_PadBottom = 1; |
| 581 | descriptor.m_StrideX = 1; |
| 582 | descriptor.m_StrideY = 1; |
| 583 | descriptor.m_BiasEnabled = true; |
| 584 | descriptor.m_DataLayout = DataLayout::NHWC; |
| 585 | |
| 586 | IConnectableLayer* const inputLayer = net->AddInputLayer(0); |
| 587 | IConnectableLayer* const convLayer = |
| 588 | net->AddTransposeConvolution2dLayer(descriptor, |
| 589 | weights, |
| 590 | Optional<ConstTensor>(biases), |
| 591 | "transposeConvolution2d"); |
| 592 | IConnectableLayer* const outputLayer = net->AddOutputLayer(0); |
| 593 | |
| 594 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
| 595 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 596 | |
| 597 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 598 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 599 | |
| 600 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(convLayer)); |
| 601 | |
| 602 | CHECK(basicBlock->GetInputs().size() == 3); |
| 603 | CHECK(basicBlock->GetOutputs().size() == 1); |
| 604 | CHECK(basicBlock->GetOperators().size() == 3); |
| 605 | CHECK(basicBlock->GetTensors().size() == 4); |
| 606 | |
| 607 | CHECK(basicBlock->GetInputs()[0].find("input0_") != std::string::npos); |
| 608 | CHECK(basicBlock->GetInputs()[1].find("constant_") != std::string::npos); |
| 609 | CHECK(basicBlock->GetInputs()[2].find("constant_") != std::string::npos); |
| 610 | CHECK(basicBlock->GetOutputs()[0].find("output0_") != std::string::npos); |
| 611 | |
| 612 | VerifyTosaAttribute(descriptor, |
| 613 | basicBlock->GetOperators().at(2)->GetAttribute(), |
| 614 | {}, |
| 615 | {}, |
| 616 | LayerType::TransposeConvolution2d); |
| 617 | } |
| 618 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 619 | TEST_CASE("GetTosaMapping_Unimplemented") |
| 620 | { |
| 621 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 622 | GetTosaMapping(nullptr, LayerType::UnidirectionalSequenceLstm, {}, {}, BaseDescriptor()); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 623 | |
| 624 | CHECK(basicBlock->GetName() == ""); |
| 625 | CHECK(basicBlock->GetTensors().size() == 0); |
| 626 | CHECK(basicBlock->GetOperators().size() == 1); |
| 627 | CHECK(basicBlock->GetInputs().size() == 0); |
| 628 | CHECK(basicBlock->GetOutputs().size() == 0); |
| 629 | |
| 630 | TosaSerializationOperator* op = basicBlock->GetOperators()[0]; |
| 631 | CHECK(op->GetAttributeType() == Attribute_NONE); |
| 632 | CHECK(op->GetOp() == tosa::Op_UNKNOWN); |
| 633 | CHECK(op->GetInputTensorNames().size() == 0); |
| 634 | CHECK(op->GetOutputTensorNames().size() == 0); |
| 635 | } |
| 636 | } |