Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 1 | // |
David Monahan | d7fca09 | 2023-01-12 14:53:34 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 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 | } |
David Monahan | d7fca09 | 2023-01-12 14:53:34 +0000 | [diff] [blame] | 255 | TEST_CASE("GetTosaMapping_ElementwiseUnaryLayerRsqrt") |
| 256 | { |
| 257 | TensorInfo inputInfo = TensorInfo({ 2, 2 }, DataType::Float32, 0.0f, 0, true); |
| 258 | TensorInfo outputInfo = TensorInfo({ 2, 2 }, DataType::Float32, 0.0f, 0, true); |
| 259 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 2 }}; |
| 260 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 2 }}; |
| 261 | |
| 262 | ElementwiseUnaryDescriptor unaryDescriptor = ElementwiseUnaryDescriptor(UnaryOperation::Rsqrt); |
| 263 | TosaSerializationBasicBlock* basicBlock = |
| 264 | GetTosaMapping(nullptr, LayerType::ElementwiseUnary, {&inputInfo,}, {&outputInfo}, unaryDescriptor); |
| 265 | |
| 266 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 267 | inputShape, |
| 268 | outputShape, |
| 269 | tosa::Op_RSQRT, |
| 270 | tosa::Attribute_NONE, |
| 271 | unaryDescriptor, |
| 272 | LayerType::ElementwiseUnary); |
| 273 | } |
| 274 | TEST_CASE("GetTosaMappingFromLayer_ElementwiseUnaryLayerRsqrt") |
| 275 | { |
| 276 | IRuntime::CreationOptions options; |
| 277 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 278 | |
| 279 | // Builds up the structure of the network. |
| 280 | INetworkPtr net(INetwork::Create()); |
| 281 | ElementwiseUnaryDescriptor unaryDescriptor = ElementwiseUnaryDescriptor(UnaryOperation::Rsqrt); |
| 282 | IConnectableLayer* input = net->AddInputLayer(0, "input0"); |
| 283 | IConnectableLayer* unaryRsqrt = net->AddElementwiseUnaryLayer(unaryDescriptor, "rsqrt"); |
| 284 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 285 | |
| 286 | input->GetOutputSlot(0).Connect(unaryRsqrt->GetInputSlot(0)); |
| 287 | unaryRsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 288 | TensorInfo inputInfo = TensorInfo({ 2, 2 }, DataType::Float32, 0.0f, 0, true); |
| 289 | TensorInfo outputInfo = TensorInfo({ 2, 2 }, DataType::Float32, 0.0f, 0, true); |
| 290 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 291 | unaryRsqrt->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 292 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 2 }}; |
| 293 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 2 }}; |
| 294 | |
| 295 | TosaSerializationBasicBlock* basicBlock = |
| 296 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(unaryRsqrt)); |
| 297 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 298 | inputShape, |
| 299 | outputShape, |
| 300 | tosa::Op_RSQRT, |
| 301 | tosa::Attribute_NONE, |
| 302 | unaryDescriptor, |
| 303 | LayerType::ElementwiseUnary); |
| 304 | } |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 305 | |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 306 | TEST_CASE("GetTosaMapping_MultiplicationLayer") |
| 307 | { |
| 308 | |
| 309 | const TensorInfo input0Info ({ 1, 2, 4, 2 }, DataType::Float32); |
| 310 | const TensorInfo input1Info ({ 1, 2, 4, 2 }, DataType::Float32); |
| 311 | const TensorInfo outputInfo ({ 1, 2, 4, 2 }, DataType::Float32); |
| 312 | |
| 313 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 2, 4, 2 }, { 1, 2, 4, 2 }}; |
| 314 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 2, 4, 2 }}; |
| 315 | |
| 316 | TosaSerializationBasicBlock* basicBlock = |
| 317 | GetTosaMapping(nullptr, LayerType::Multiplication, {&input0Info, &input1Info}, {&outputInfo}, BaseDescriptor()); |
| 318 | AssertTosaOneToOneMappingBasicBlock( basicBlock, inputShape, outputShape, |
| 319 | tosa::Op_MUL, tosa::Attribute_MulAttribute, BaseDescriptor(), LayerType::Multiplication); |
| 320 | } |
| 321 | |
| 322 | TEST_CASE("GetTosaMappingFromLayer_MultiplicationLayer") |
| 323 | { |
| 324 | IRuntime::CreationOptions options; |
| 325 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 326 | |
| 327 | // Builds up the structure of the network. |
| 328 | INetworkPtr net(INetwork::Create()); |
| 329 | |
| 330 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 331 | IConnectableLayer* input1 = net->AddInputLayer(1, "input1"); |
| 332 | IConnectableLayer* add = net->AddMultiplicationLayer("multiplication"); |
| 333 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 334 | |
| 335 | input0->GetOutputSlot(0).Connect(add->GetInputSlot(0)); |
| 336 | input1->GetOutputSlot(0).Connect(add->GetInputSlot(1)); |
| 337 | add->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 338 | |
| 339 | TensorInfo info = TensorInfo({ 2, 2 }, DataType::Float32, 0.0f, 0, true); |
| 340 | |
| 341 | input0->GetOutputSlot(0).SetTensorInfo(info); |
| 342 | input1->GetOutputSlot(0).SetTensorInfo(info); |
| 343 | add->GetOutputSlot(0).SetTensorInfo(info); |
| 344 | |
| 345 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 2 }, { 2, 2 }}; |
| 346 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 2 }}; |
| 347 | |
| 348 | TosaSerializationBasicBlock* basicBlock = |
| 349 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(add)); |
| 350 | AssertTosaOneToOneMappingBasicBlock( basicBlock, inputShape, outputShape, |
| 351 | tosa::Op_MUL, Attribute_MulAttribute, BaseDescriptor(), LayerType::Multiplication); |
| 352 | } |
| 353 | |
Teresa Charlin | 3fbad94 | 2022-12-15 10:35:37 +0000 | [diff] [blame] | 354 | TEST_CASE("GetTosaMapping_AvgPool2DLayer") |
| 355 | { |
| 356 | Pooling2dDescriptor descriptor; |
| 357 | descriptor.m_PoolType = PoolingAlgorithm::Average; |
| 358 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 359 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 360 | descriptor.m_PadLeft = 1; |
| 361 | descriptor.m_PadRight = 1; |
| 362 | descriptor.m_PadTop = 1; |
| 363 | descriptor.m_PadBottom = 1; |
| 364 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
| 365 | |
| 366 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 367 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 368 | |
| 369 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 370 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 371 | |
| 372 | TosaSerializationBasicBlock* basicBlock = |
| 373 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
| 374 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 375 | inputShape, |
| 376 | outputShape, |
| 377 | Op_AVG_POOL2D, |
| 378 | Attribute_PoolAttribute, |
| 379 | descriptor, |
| 380 | LayerType::Pooling2d); |
| 381 | } |
| 382 | |
| 383 | TEST_CASE("GetTosaMappingFromLayer_AvgPool2DLayer") |
| 384 | { |
| 385 | IRuntime::CreationOptions options; |
| 386 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 387 | |
| 388 | // Builds up the structure of the network. |
| 389 | INetworkPtr net(INetwork::Create()); |
| 390 | |
| 391 | Pooling2dDescriptor descriptor; |
| 392 | descriptor.m_PoolType = PoolingAlgorithm::Average; |
| 393 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 394 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 395 | descriptor.m_PadLeft = 1; |
| 396 | descriptor.m_PadRight = 1; |
| 397 | descriptor.m_PadTop = 1; |
| 398 | descriptor.m_PadBottom = 1; |
| 399 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
| 400 | |
| 401 | IConnectableLayer* input0 = net->AddInputLayer(0, "input0"); |
| 402 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 403 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 404 | |
| 405 | input0->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
| 406 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 407 | |
| 408 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 409 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
| 410 | |
| 411 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 412 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 413 | |
| 414 | input0->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 415 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 416 | |
| 417 | TosaSerializationBasicBlock* basicBlock = |
| 418 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
| 419 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 420 | inputShape, |
| 421 | outputShape, |
| 422 | Op_AVG_POOL2D, |
| 423 | Attribute_PoolAttribute, |
| 424 | descriptor, |
| 425 | LayerType::Pooling2d); |
| 426 | } |
| 427 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 428 | TEST_CASE("GetTosaMapping_MaxPool2DLayer") |
| 429 | { |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 430 | Pooling2dDescriptor descriptor; |
| 431 | descriptor.m_PoolType = PoolingAlgorithm::Max; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 432 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 433 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 434 | descriptor.m_PadLeft = 1; |
| 435 | descriptor.m_PadRight = 1; |
| 436 | descriptor.m_PadTop = 1; |
| 437 | descriptor.m_PadBottom = 1; |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 438 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 439 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 440 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 441 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 442 | |
| 443 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 444 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 445 | |
| 446 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 447 | GetTosaMapping(nullptr, LayerType::Pooling2d, {&inputTensorInfo}, {&outputTensorInfo}, descriptor); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 448 | AssertTosaOneToOneMappingBasicBlock( |
| 449 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 450 | } |
| 451 | |
| 452 | TEST_CASE("GetTosaMappingFromLayer_MaxPool2DLayer") |
| 453 | { |
| 454 | IRuntime::CreationOptions options; |
| 455 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 456 | |
| 457 | // Builds up the structure of the network. |
| 458 | INetworkPtr net(INetwork::Create()); |
| 459 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 460 | Pooling2dDescriptor descriptor; |
| 461 | descriptor.m_PoolType = PoolingAlgorithm::Max; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 462 | descriptor.m_PoolWidth = descriptor.m_PoolHeight = 2; |
| 463 | descriptor.m_StrideX = descriptor.m_StrideY = 2; |
| 464 | descriptor.m_PadLeft = 1; |
| 465 | descriptor.m_PadRight = 1; |
| 466 | descriptor.m_PadTop = 1; |
| 467 | descriptor.m_PadBottom = 1; |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 468 | descriptor.m_PaddingMethod = PaddingMethod::Exclude; |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 469 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 470 | IConnectableLayer* input = net->AddInputLayer(0, "input0"); |
| 471 | IConnectableLayer* pool = net->AddPooling2dLayer(descriptor, "pool"); |
| 472 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 473 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 474 | input->GetOutputSlot(0).Connect(pool->GetInputSlot(0)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 475 | pool->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 476 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 477 | TensorInfo inputTensorInfo({ 1, 1, 4, 4 }, DataType::Float32); |
| 478 | TensorInfo outputTensorInfo({ 1, 1, 3, 3 }, DataType::Float32); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 479 | |
| 480 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 4, 4 }}; |
| 481 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 1, 3, 3 }}; |
| 482 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 483 | input->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 484 | pool->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 485 | |
| 486 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 487 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(pool)); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 488 | AssertTosaOneToOneMappingBasicBlock( |
| 489 | basicBlock, inputShape, outputShape, Op_MAX_POOL2D, Attribute_PoolAttribute, descriptor, LayerType::Pooling2d); |
| 490 | } |
| 491 | |
Cathal Corbett | b30e655 | 2022-12-07 11:50:50 +0000 | [diff] [blame] | 492 | TEST_CASE("GetTosaMapping_ReshapeLayer") |
| 493 | { |
| 494 | TensorInfo inputInfo = TensorInfo({ 2, 3 }, DataType::Float32); |
| 495 | TensorInfo outputInfo = TensorInfo({ 6 }, DataType::Float32); |
| 496 | |
| 497 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 3 }}; |
| 498 | std::vector<std::vector<int32_t>> outputShape = {{ 6 }}; |
| 499 | |
| 500 | ReshapeDescriptor descriptor; |
| 501 | descriptor.m_TargetShape = { 6 }; |
| 502 | |
| 503 | TosaSerializationBasicBlock* basicBlock = |
| 504 | GetTosaMapping(nullptr, LayerType::Reshape, {&inputInfo}, {&outputInfo}, descriptor); |
| 505 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 506 | inputShape, |
| 507 | outputShape, |
| 508 | Op_RESHAPE, |
| 509 | Attribute_ReshapeAttribute, |
| 510 | descriptor, |
| 511 | LayerType::Reshape); |
| 512 | } |
| 513 | |
| 514 | TEST_CASE("GetTosaMappingFromLayer_ReshapeLayer") |
| 515 | { |
| 516 | IRuntime::CreationOptions options; |
| 517 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 518 | |
| 519 | // Builds up the structure of the network. |
| 520 | INetworkPtr net(INetwork::Create()); |
| 521 | |
| 522 | ReshapeDescriptor descriptor; |
| 523 | descriptor.m_TargetShape = { 6 }; |
| 524 | |
| 525 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 526 | IConnectableLayer* reshape = net->AddReshapeLayer(descriptor, "reshape"); |
| 527 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 528 | |
| 529 | input->GetOutputSlot(0).Connect(reshape->GetInputSlot(0)); |
| 530 | reshape->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 531 | |
| 532 | TensorInfo inputInfo = TensorInfo({ 2, 3 }, DataType::Float32); |
| 533 | TensorInfo outputInfo = TensorInfo({ 6 }, DataType::Float32); |
| 534 | |
| 535 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 536 | reshape->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 537 | |
| 538 | std::vector<std::vector<int32_t>> inputShape = {{ 2, 3 }}; |
| 539 | std::vector<std::vector<int32_t>> outputShape = {{ 6 }}; |
| 540 | |
| 541 | TosaSerializationBasicBlock* basicBlock = |
| 542 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(reshape)); |
| 543 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 544 | inputShape, |
| 545 | outputShape, |
| 546 | Op_RESHAPE, |
| 547 | Attribute_ReshapeAttribute, |
| 548 | descriptor, |
| 549 | LayerType::Reshape); |
| 550 | } |
| 551 | |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 552 | TEST_CASE("GetTosaMapping_SliceLayer") |
| 553 | { |
| 554 | TensorInfo inputInfo = TensorInfo({ 3, 2, 3 }, DataType::Float32); |
| 555 | TensorInfo outputInfo = TensorInfo({ 2, 1, 3 }, DataType::Float32); |
| 556 | |
| 557 | std::vector<std::vector<int32_t>> inputShape = {{ 3, 2, 3 }}; |
| 558 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 1, 3 }}; |
| 559 | |
| 560 | SliceDescriptor descriptor; |
Matthew Sloyan | 67fd526 | 2022-12-07 19:28:18 +0000 | [diff] [blame] | 561 | descriptor.m_Begin = { 1, 0, 0 }; |
| 562 | descriptor.m_Size = { 2, 1, 3 }; |
Cathal Corbett | 3b9acd5 | 2022-12-09 12:17:27 +0000 | [diff] [blame] | 563 | |
| 564 | TosaSerializationBasicBlock* basicBlock = |
| 565 | GetTosaMapping(nullptr, LayerType::Slice, {&inputInfo}, {&outputInfo}, descriptor); |
| 566 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 567 | inputShape, |
| 568 | outputShape, |
| 569 | Op_SLICE, |
| 570 | Attribute_SliceAttribute, |
| 571 | descriptor, |
| 572 | LayerType::Slice); |
| 573 | } |
| 574 | |
| 575 | TEST_CASE("GetTosaMappingFromLayer_SliceLayer") |
| 576 | { |
| 577 | IRuntime::CreationOptions options; |
| 578 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 579 | |
| 580 | // Builds up the structure of the network. |
| 581 | INetworkPtr net(INetwork::Create()); |
| 582 | |
| 583 | TensorInfo inputInfo = TensorInfo({ 3, 2, 3 }, DataType::Float32); |
| 584 | TensorInfo outputInfo = TensorInfo({ 2, 1, 3 }, DataType::Float32); |
| 585 | |
| 586 | std::vector<std::vector<int32_t>> inputShape = {{ 3, 2, 3 }}; |
| 587 | std::vector<std::vector<int32_t>> outputShape = {{ 2, 1, 3 }}; |
| 588 | |
| 589 | SliceDescriptor descriptor; |
| 590 | descriptor.m_Begin = { 1, 0, 0 }; |
| 591 | descriptor.m_Size = { 2, 1, 3 }; |
| 592 | |
| 593 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 594 | IConnectableLayer* slice = net->AddSliceLayer(descriptor, "slice"); |
| 595 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 596 | |
| 597 | input->GetOutputSlot(0).Connect(slice->GetInputSlot(0)); |
| 598 | slice->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 599 | |
| 600 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 601 | slice->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 602 | |
| 603 | TosaSerializationBasicBlock* basicBlock = |
| 604 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(slice)); |
| 605 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 606 | inputShape, |
| 607 | outputShape, |
| 608 | Op_SLICE, |
| 609 | Attribute_SliceAttribute, |
| 610 | descriptor, |
| 611 | LayerType::Slice); |
| 612 | } |
| 613 | |
| 614 | |
Matthew Sloyan | fc9d5e7 | 2022-12-08 13:38:23 +0000 | [diff] [blame] | 615 | TEST_CASE("GetTosaMapping_TransposeConv2dLayer") |
| 616 | { |
| 617 | const TensorInfo inputInfo ({ 1, 7, 7, 1 }, DataType::Float32); |
| 618 | const TensorInfo outputInfo({ 1, 9, 9, 1 }, DataType::Float32); |
| 619 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 620 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 621 | |
| 622 | TransposeConvolution2dDescriptor descriptor; |
| 623 | descriptor.m_PadLeft = 1; |
| 624 | descriptor.m_PadRight = 1; |
| 625 | descriptor.m_PadTop = 1; |
| 626 | descriptor.m_PadBottom = 1; |
| 627 | descriptor.m_StrideX = 1; |
| 628 | descriptor.m_StrideY = 1; |
| 629 | descriptor.m_BiasEnabled = true; |
| 630 | descriptor.m_DataLayout = DataLayout::NHWC; |
| 631 | |
| 632 | TosaSerializationBasicBlock* basicBlock = GetTosaMapping(nullptr, |
| 633 | LayerType::TransposeConvolution2d, |
| 634 | {&inputInfo, &weightsInfo, &biasesInfo}, |
| 635 | {&outputInfo}, |
| 636 | descriptor); |
| 637 | |
| 638 | CHECK(basicBlock->GetInputs().size() == 3); |
| 639 | CHECK(basicBlock->GetOutputs().size() == 1); |
| 640 | CHECK(basicBlock->GetOperators().size() == 3); |
| 641 | CHECK(basicBlock->GetTensors().size() == 4); |
| 642 | |
| 643 | CHECK(basicBlock->GetInputs()[0].find("input0_") != std::string::npos); |
| 644 | CHECK(basicBlock->GetInputs()[1].find("constant_") != std::string::npos); |
| 645 | CHECK(basicBlock->GetInputs()[2].find("constant_") != std::string::npos); |
| 646 | CHECK(basicBlock->GetOutputs()[0].find("output0_") != std::string::npos); |
| 647 | |
| 648 | VerifyTosaAttribute(descriptor, |
| 649 | basicBlock->GetOperators().at(2)->GetAttribute(), |
| 650 | {}, |
| 651 | {}, |
| 652 | LayerType::TransposeConvolution2d); |
| 653 | } |
| 654 | |
| 655 | TEST_CASE("GetTosaMappingFromLayer_TransposeConv2dLayer") |
| 656 | { |
| 657 | IRuntime::CreationOptions options; |
| 658 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 659 | |
| 660 | // Builds up the structure of the network. |
| 661 | INetworkPtr net(INetwork::Create()); |
| 662 | |
| 663 | const TensorInfo inputInfo ({ 1, 7, 7, 1 }, DataType::Float32); |
| 664 | const TensorInfo outputInfo({ 1, 9, 9, 1 }, DataType::Float32); |
| 665 | const TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 666 | const TensorInfo biasesInfo ({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 667 | |
| 668 | std::vector<float> weightsData = GenerateRandomData<float>(weightsInfo.GetNumElements()); |
| 669 | ConstTensor weights(weightsInfo, weightsData); |
| 670 | |
| 671 | std::vector<float> biasesData = GenerateRandomData<float>(biasesInfo.GetNumElements()); |
| 672 | ConstTensor biases(biasesInfo, biasesData); |
| 673 | |
| 674 | TransposeConvolution2dDescriptor descriptor; |
| 675 | descriptor.m_PadLeft = 1; |
| 676 | descriptor.m_PadRight = 1; |
| 677 | descriptor.m_PadTop = 1; |
| 678 | descriptor.m_PadBottom = 1; |
| 679 | descriptor.m_StrideX = 1; |
| 680 | descriptor.m_StrideY = 1; |
| 681 | descriptor.m_BiasEnabled = true; |
| 682 | descriptor.m_DataLayout = DataLayout::NHWC; |
| 683 | |
| 684 | IConnectableLayer* const inputLayer = net->AddInputLayer(0); |
| 685 | IConnectableLayer* const convLayer = |
| 686 | net->AddTransposeConvolution2dLayer(descriptor, |
| 687 | weights, |
| 688 | Optional<ConstTensor>(biases), |
| 689 | "transposeConvolution2d"); |
| 690 | IConnectableLayer* const outputLayer = net->AddOutputLayer(0); |
| 691 | |
| 692 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
| 693 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 694 | |
| 695 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 696 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 697 | |
| 698 | TosaSerializationBasicBlock* basicBlock = GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(convLayer)); |
| 699 | |
| 700 | CHECK(basicBlock->GetInputs().size() == 3); |
| 701 | CHECK(basicBlock->GetOutputs().size() == 1); |
| 702 | CHECK(basicBlock->GetOperators().size() == 3); |
| 703 | CHECK(basicBlock->GetTensors().size() == 4); |
| 704 | |
| 705 | CHECK(basicBlock->GetInputs()[0].find("input0_") != std::string::npos); |
| 706 | CHECK(basicBlock->GetInputs()[1].find("constant_") != std::string::npos); |
| 707 | CHECK(basicBlock->GetInputs()[2].find("constant_") != std::string::npos); |
| 708 | CHECK(basicBlock->GetOutputs()[0].find("output0_") != std::string::npos); |
| 709 | |
| 710 | VerifyTosaAttribute(descriptor, |
| 711 | basicBlock->GetOperators().at(2)->GetAttribute(), |
| 712 | {}, |
| 713 | {}, |
| 714 | LayerType::TransposeConvolution2d); |
| 715 | } |
| 716 | |
Cathal Corbett | 0bb096d | 2022-12-22 13:09:38 +0000 | [diff] [blame] | 717 | TEST_CASE("GetTosaMapping_TransposeLayer") |
| 718 | { |
| 719 | TensorInfo inputInfo = TensorInfo({ 1, 1, 5, 3 }, DataType::Float32, 0.0f, 0, true); |
| 720 | TensorInfo outputInfo = TensorInfo({ 1, 5, 1, 3 }, DataType::Float32, 0.0f, 0, true); |
| 721 | |
| 722 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 5, 3 }}; |
| 723 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 5, 1, 3 }}; |
| 724 | |
| 725 | TransposeDescriptor transposeDescriptor = TransposeDescriptor({ 0, 2, 1 ,3 }); |
| 726 | |
| 727 | TosaSerializationBasicBlock* basicBlock = |
| 728 | GetTosaMapping(nullptr, LayerType::Transpose, {&inputInfo,}, {&outputInfo}, transposeDescriptor); |
| 729 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 730 | inputShape, |
| 731 | outputShape, |
| 732 | Op_TRANSPOSE, |
| 733 | Attribute_TransposeAttribute, |
| 734 | transposeDescriptor, |
| 735 | LayerType::Transpose); |
| 736 | } |
| 737 | |
| 738 | TEST_CASE("GetTosaMappingFromLayer_TransposeLayer") |
| 739 | { |
| 740 | IRuntime::CreationOptions options; |
| 741 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 742 | |
| 743 | // Builds up the structure of the network. |
| 744 | INetworkPtr net(INetwork::Create()); |
| 745 | |
| 746 | TransposeDescriptor transposeDescriptor = TransposeDescriptor({ 0, 2, 1 ,3 }); |
| 747 | |
| 748 | IConnectableLayer* input = net->AddInputLayer(0, "input0"); |
| 749 | IConnectableLayer* transpose = net->AddTransposeLayer(transposeDescriptor, "transpose"); |
| 750 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 751 | |
| 752 | input->GetOutputSlot(0).Connect(transpose->GetInputSlot(0)); |
| 753 | transpose->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 754 | |
| 755 | TensorInfo inputInfo = TensorInfo({ 1, 1, 5, 3 }, DataType::Float32, 0.0f, 0, true); |
| 756 | TensorInfo outputInfo = TensorInfo({ 1, 5, 1, 3 }, DataType::Float32, 0.0f, 0, true); |
| 757 | |
| 758 | input->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 759 | transpose->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 760 | |
| 761 | std::vector<std::vector<int32_t>> inputShape = {{ 1, 1, 5, 3 }}; |
| 762 | std::vector<std::vector<int32_t>> outputShape = {{ 1, 5, 1, 3 }}; |
| 763 | |
| 764 | TosaSerializationBasicBlock* basicBlock = |
| 765 | GetTosaMappingFromLayer(PolymorphicDowncast<Layer*>(transpose)); |
| 766 | AssertTosaOneToOneMappingBasicBlock(basicBlock, |
| 767 | inputShape, |
| 768 | outputShape, |
| 769 | Op_TRANSPOSE, |
| 770 | Attribute_TransposeAttribute, |
| 771 | transposeDescriptor, |
| 772 | LayerType::Transpose); |
| 773 | } |
| 774 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 775 | TEST_CASE("GetTosaMapping_Unimplemented") |
| 776 | { |
| 777 | TosaSerializationBasicBlock* basicBlock = |
Matthew Sloyan | c5fe6e7 | 2022-11-25 16:10:00 +0000 | [diff] [blame] | 778 | GetTosaMapping(nullptr, LayerType::UnidirectionalSequenceLstm, {}, {}, BaseDescriptor()); |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 779 | |
| 780 | CHECK(basicBlock->GetName() == ""); |
| 781 | CHECK(basicBlock->GetTensors().size() == 0); |
| 782 | CHECK(basicBlock->GetOperators().size() == 1); |
| 783 | CHECK(basicBlock->GetInputs().size() == 0); |
| 784 | CHECK(basicBlock->GetOutputs().size() == 0); |
| 785 | |
| 786 | TosaSerializationOperator* op = basicBlock->GetOperators()[0]; |
| 787 | CHECK(op->GetAttributeType() == Attribute_NONE); |
| 788 | CHECK(op->GetOp() == tosa::Op_UNKNOWN); |
| 789 | CHECK(op->GetInputTensorNames().size() == 0); |
| 790 | CHECK(op->GetOutputTensorNames().size() == 0); |
| 791 | } |
| 792 | } |