David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <armnn/INetwork.hpp> |
| 7 | |
| 8 | #include <GraphUtils.hpp> |
| 9 | #include <TestUtils.hpp> |
| 10 | |
| 11 | #include <doctest/doctest.h> |
| 12 | |
| 13 | using namespace armnn; |
| 14 | |
| 15 | TEST_SUITE("GpuFsaOptimizedNetwork") |
| 16 | { |
| 17 | |
| 18 | TEST_CASE("SingleConv2dSupportedOptimizedNetwork") |
| 19 | { |
| 20 | IRuntime::CreationOptions options; |
| 21 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 22 | INetworkPtr network(INetwork::Create()); |
| 23 | |
| 24 | TensorInfo inputInfo({ 1, 5, 5, 1 }, DataType::Float32); |
| 25 | TensorInfo outputInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 26 | TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 27 | TensorInfo biasesInfo({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 28 | |
| 29 | Convolution2dDescriptor desc; |
| 30 | desc.m_BiasEnabled = true; |
| 31 | desc.m_DataLayout = DataLayout::NHWC; |
| 32 | |
| 33 | auto inputLayer = network->AddInputLayer(0, "input"); |
| 34 | auto weightLayer = network->AddConstantLayer(ConstTensor(weightsInfo, nullptr), "weights"); |
| 35 | auto biasLayer = network->AddConstantLayer(ConstTensor(biasesInfo, nullptr), "bias"); |
| 36 | auto convLayer = network->AddConvolution2dLayer(desc, "conv2d"); |
| 37 | auto outputLayer = network->AddOutputLayer(1, "output"); |
| 38 | |
| 39 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
| 40 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 41 | |
| 42 | weightLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(1)); |
| 43 | weightLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 44 | |
| 45 | biasLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(2)); |
| 46 | biasLayer->GetOutputSlot(0).SetTensorInfo(biasesInfo); |
| 47 | |
| 48 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 49 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 50 | |
| 51 | std::vector<BackendId> backends = { "GpuFsa" }; |
| 52 | |
| 53 | OptimizerOptionsOpaque optimizedOptions; |
| 54 | IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec(), optimizedOptions); |
| 55 | CHECK(optNet); |
| 56 | |
| 57 | Graph& graph = GetGraphForTesting(optNet.get()); |
| 58 | |
| 59 | // Check graph layer sequence to ensure that the network has been replaced with a PreCompiledLayer |
| 60 | CHECK(CheckSequence(graph.cbegin(), graph.cend(), |
| 61 | &IsLayerOfType<InputLayer>, |
| 62 | &IsLayerOfType<ConstantLayer>, |
| 63 | &IsLayerOfType<ConstantLayer>, |
| 64 | &IsLayerOfType<PreCompiledLayer>, |
| 65 | &IsLayerOfType<OutputLayer>)); |
| 66 | } |
| 67 | |
| 68 | TEST_CASE("TwoConv2dSupportedOptimizedNetwork") |
| 69 | { |
| 70 | IRuntime::CreationOptions options; |
| 71 | IRuntimePtr runtime(IRuntime::Create(options)); |
| 72 | INetworkPtr network(INetwork::Create()); |
| 73 | |
| 74 | TensorInfo inputInfo({ 1, 5, 5, 1 }, DataType::Float32); |
| 75 | TensorInfo intermediateInfo({ 1, 3, 3, 1 }, DataType::Float32); |
| 76 | TensorInfo outputInfo({ 1, 1, 1, 1 }, DataType::Float32); |
| 77 | TensorInfo weightsInfo({ 1, 3, 3, 1 }, DataType::Float32, 0.0f, 0, true); |
| 78 | TensorInfo biasesInfo({ 1 }, DataType::Float32, 0.0f, 0, true); |
| 79 | |
| 80 | Convolution2dDescriptor desc; |
| 81 | desc.m_BiasEnabled = true; |
| 82 | desc.m_DataLayout = DataLayout::NHWC; |
| 83 | |
| 84 | auto inputLayer = network->AddInputLayer(0, "input"); |
| 85 | |
| 86 | auto weightLayer1 = network->AddConstantLayer(ConstTensor(weightsInfo, nullptr), "weights"); |
| 87 | auto biasLayer1 = network->AddConstantLayer(ConstTensor(biasesInfo, nullptr), "bias"); |
| 88 | auto convLayer1 = network->AddConvolution2dLayer(desc, "conv2d"); |
| 89 | |
| 90 | auto weightLayer2 = network->AddConstantLayer(ConstTensor(weightsInfo, nullptr), "weights"); |
| 91 | auto biasLayer2 = network->AddConstantLayer(ConstTensor(biasesInfo, nullptr), "bias"); |
| 92 | auto convLayer2 = network->AddConvolution2dLayer(desc, "conv2d"); |
| 93 | |
| 94 | auto outputLayer = network->AddOutputLayer(0, "output"); |
| 95 | |
| 96 | inputLayer->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(0)); |
| 97 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 98 | |
| 99 | weightLayer1->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(1)); |
| 100 | weightLayer1->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 101 | |
| 102 | biasLayer1->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(2)); |
| 103 | biasLayer1->GetOutputSlot(0).SetTensorInfo(biasesInfo); |
| 104 | |
| 105 | convLayer1->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(0)); |
| 106 | convLayer1->GetOutputSlot(0).SetTensorInfo(intermediateInfo); |
| 107 | |
| 108 | weightLayer2->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(1)); |
| 109 | weightLayer2->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 110 | |
| 111 | biasLayer2->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(2)); |
| 112 | biasLayer2->GetOutputSlot(0).SetTensorInfo(biasesInfo); |
| 113 | |
| 114 | convLayer2->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 115 | convLayer2->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 116 | |
| 117 | std::vector<BackendId> backends = { "GpuFsa" }; |
| 118 | |
| 119 | OptimizerOptionsOpaque optimizedOptions; |
| 120 | IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec(), optimizedOptions); |
| 121 | CHECK(optNet); |
| 122 | |
| 123 | Graph& graph = GetGraphForTesting(optNet.get()); |
| 124 | |
| 125 | // Check graph layer sequence to ensure that the network has been replaced with a PreCompiledLayer |
| 126 | CHECK(CheckSequence(graph.cbegin(), graph.cend(), |
| 127 | &IsLayerOfType<InputLayer>, |
| 128 | &IsLayerOfType<ConstantLayer>, |
| 129 | &IsLayerOfType<ConstantLayer>, |
| 130 | &IsLayerOfType<ConstantLayer>, |
| 131 | &IsLayerOfType<ConstantLayer>, |
| 132 | &IsLayerOfType<PreCompiledLayer>, |
| 133 | &IsLayerOfType<PreCompiledLayer>, |
| 134 | &IsLayerOfType<OutputLayer>)); |
| 135 | } |
| 136 | |
| 137 | } |