Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 6 | #include <Graph.hpp> |
| 7 | #include <Network.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 8 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <reference/RefWorkloadFactory.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 10 | |
| 11 | #include <boost/test/unit_test.hpp> |
keidav01 | 738c2e6 | 2018-12-11 16:14:20 +0000 | [diff] [blame] | 12 | #include <test/GraphUtils.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(RefOptimizedNetwork) |
| 15 | |
| 16 | BOOST_AUTO_TEST_CASE(OptimizeValidateCpuRefWorkloads) |
| 17 | { |
| 18 | const armnn::TensorInfo desc({3, 5}, armnn::DataType::Float32); |
| 19 | |
| 20 | armnn::Network net; |
| 21 | |
| 22 | armnn::NormalizationDescriptor nmDesc; |
| 23 | armnn::ActivationDescriptor acDesc; |
| 24 | |
| 25 | // in |
| 26 | // | |
| 27 | // nm |
| 28 | // / | |
| 29 | // ac | |
| 30 | // \ | |
| 31 | // ml |
| 32 | // | |
| 33 | // sm |
| 34 | // | |
| 35 | // ot |
| 36 | armnn::IConnectableLayer* layer = net.AddInputLayer(0, "in"); |
| 37 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 38 | |
| 39 | armnn::IConnectableLayer* const normLayer = net.AddNormalizationLayer(nmDesc, "nm"); |
| 40 | |
| 41 | layer->GetOutputSlot(0).Connect(normLayer->GetInputSlot(0)); |
| 42 | normLayer->GetOutputSlot(0).SetTensorInfo(desc); |
| 43 | |
| 44 | layer = net.AddActivationLayer(acDesc, "ac"); |
| 45 | |
| 46 | normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 47 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 48 | |
| 49 | armnn::IConnectableLayer* prevLayer = layer; |
| 50 | layer = net.AddMultiplicationLayer("ml"); |
| 51 | |
| 52 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 53 | normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1)); |
| 54 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 55 | |
| 56 | prevLayer = layer; |
| 57 | armnn::SoftmaxDescriptor softmaxDescriptor; |
| 58 | layer = net.AddSoftmaxLayer(softmaxDescriptor, "sm"); |
| 59 | |
| 60 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 61 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 62 | |
| 63 | prevLayer = layer; |
| 64 | layer = net.AddOutputLayer(0, "ot"); |
| 65 | |
| 66 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 67 | |
| 68 | armnn::IRuntime::CreationOptions options; |
| 69 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 70 | |
| 71 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 72 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(net, backends, runtime->GetDeviceSpec()); |
| 73 | static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph().AllocateDynamicBuffers(); |
| 74 | BOOST_CHECK(optNet); |
| 75 | |
| 76 | // Validates workloads. |
| 77 | armnn::RefWorkloadFactory fact; |
| 78 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 79 | { |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 80 | BOOST_CHECK_NO_THROW(layer->CreateWorkload(fact)); |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(OptimizeValidateWorkloadsCpuRefPermuteLayer) |
| 85 | { |
| 86 | // Create runtime in which test will run |
| 87 | armnn::IRuntime::CreationOptions options; |
| 88 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 89 | |
| 90 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 91 | |
| 92 | // build up the structure of the network |
| 93 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 94 | |
| 95 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 96 | |
| 97 | armnn::PermuteDescriptor descriptor({0, 2, 3, 1}); |
| 98 | armnn::IConnectableLayer* permute = net->AddPermuteLayer(descriptor); |
| 99 | |
| 100 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 101 | |
| 102 | input->GetOutputSlot(0).Connect(permute->GetInputSlot(0)); |
| 103 | permute->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 104 | |
| 105 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 1, 4, 4 }, armnn::DataType::Float32)); |
| 106 | permute->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 4, 1, 4 }, armnn::DataType::Float32)); |
| 107 | |
| 108 | // optimize the network |
| 109 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 110 | |
| 111 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 112 | { |
| 113 | BOOST_CHECK(layer->GetBackendId() == armnn::Compute::CpuRef); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(OptimizeValidateWorkloadsCpuRefMeanLayer) |
| 118 | { |
| 119 | // Create runtime in which test will run |
| 120 | armnn::IRuntime::CreationOptions options; |
| 121 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 122 | |
| 123 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 124 | |
| 125 | // build up the structure of the network |
| 126 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 127 | |
| 128 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 129 | |
| 130 | armnn::MeanDescriptor descriptor({ 0, 1 }, false); |
| 131 | armnn::IConnectableLayer* meanLayer = net->AddMeanLayer(descriptor); |
| 132 | |
| 133 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 134 | |
| 135 | input->GetOutputSlot(0).Connect(meanLayer->GetInputSlot(0)); |
| 136 | meanLayer->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 137 | |
| 138 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 4, 3, 2 }, armnn::DataType::Float32)); |
| 139 | meanLayer->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 2 }, armnn::DataType::Float32)); |
| 140 | |
| 141 | // optimize the network |
| 142 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 143 | |
| 144 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 145 | { |
| 146 | BOOST_CHECK(layer->GetBackendId() == armnn::Compute::CpuRef); |
| 147 | } |
| 148 | } |
| 149 | |
keidav01 | 738c2e6 | 2018-12-11 16:14:20 +0000 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(DebugTestOnCpuRef) |
| 151 | { |
| 152 | armnn::Network net; |
| 153 | |
| 154 | armnn::ActivationDescriptor activation1Descriptor; |
| 155 | activation1Descriptor.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 156 | activation1Descriptor.m_A = 1.f; |
| 157 | activation1Descriptor.m_B = -1.f; |
| 158 | |
| 159 | // Defines layers. |
| 160 | auto input = net.AddInputLayer(0, "InputLayer"); |
| 161 | auto activation = net.AddActivationLayer(activation1Descriptor, "ActivationLayer"); |
| 162 | auto output = net.AddOutputLayer(0, "OutputLayer"); |
| 163 | |
| 164 | // Connects layers. |
| 165 | input->GetOutputSlot(0).Connect(activation->GetInputSlot(0)); |
| 166 | activation->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 167 | |
| 168 | armnn::TensorShape shape({4}); |
| 169 | armnn::TensorInfo info(shape, armnn::DataType::Float32); |
| 170 | input->GetOutputSlot(0).SetTensorInfo(info); |
| 171 | activation->GetOutputSlot(0).SetTensorInfo(info); |
| 172 | |
| 173 | armnn::IRuntime::CreationOptions options; |
| 174 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 175 | |
| 176 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 177 | |
| 178 | armnn::OptimizerOptions optimizerOptions; |
| 179 | optimizerOptions.m_Debug = true; |
| 180 | |
| 181 | armnn::IOptimizedNetworkPtr optimizedNet = armnn::Optimize(net, backends, runtime->GetDeviceSpec(), |
| 182 | optimizerOptions); |
| 183 | |
| 184 | const armnn::Graph& graph = static_cast<armnn::OptimizedNetwork*>(optimizedNet.get())->GetGraph(); |
| 185 | // Tests that all layers are present in the graph. |
| 186 | BOOST_TEST(graph.GetNumLayers() == 5); |
| 187 | |
| 188 | // Tests that the vertices exist and have correct names. |
| 189 | BOOST_TEST(GraphHasNamedLayer(graph, "InputLayer")); |
| 190 | BOOST_TEST(GraphHasNamedLayer(graph, "DebugLayerAfterInputLayer")); |
| 191 | BOOST_TEST(GraphHasNamedLayer(graph, "ActivationLayer")); |
| 192 | BOOST_TEST(GraphHasNamedLayer(graph, "DebugLayerAfterActivationLayer")); |
| 193 | BOOST_TEST(GraphHasNamedLayer(graph, "OutputLayer")); |
| 194 | } |
| 195 | |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 196 | BOOST_AUTO_TEST_SUITE_END() |