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 | |
| 6 | #include <armnn/ArmNN.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 7 | #include <Graph.hpp> |
| 8 | #include <Network.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <reference/RefWorkloadFactory.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 11 | |
| 12 | #include <boost/test/unit_test.hpp> |
keidav01 | 738c2e6 | 2018-12-11 16:14:20 +0000 | [diff] [blame] | 13 | #include <test/GraphUtils.hpp> |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(RefOptimizedNetwork) |
| 16 | |
| 17 | BOOST_AUTO_TEST_CASE(OptimizeValidateCpuRefWorkloads) |
| 18 | { |
| 19 | const armnn::TensorInfo desc({3, 5}, armnn::DataType::Float32); |
| 20 | |
| 21 | armnn::Network net; |
| 22 | |
| 23 | armnn::NormalizationDescriptor nmDesc; |
| 24 | armnn::ActivationDescriptor acDesc; |
| 25 | |
| 26 | // in |
| 27 | // | |
| 28 | // nm |
| 29 | // / | |
| 30 | // ac | |
| 31 | // \ | |
| 32 | // ml |
| 33 | // | |
| 34 | // sm |
| 35 | // | |
| 36 | // ot |
| 37 | armnn::IConnectableLayer* layer = net.AddInputLayer(0, "in"); |
| 38 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 39 | |
| 40 | armnn::IConnectableLayer* const normLayer = net.AddNormalizationLayer(nmDesc, "nm"); |
| 41 | |
| 42 | layer->GetOutputSlot(0).Connect(normLayer->GetInputSlot(0)); |
| 43 | normLayer->GetOutputSlot(0).SetTensorInfo(desc); |
| 44 | |
| 45 | layer = net.AddActivationLayer(acDesc, "ac"); |
| 46 | |
| 47 | normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 48 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 49 | |
| 50 | armnn::IConnectableLayer* prevLayer = layer; |
| 51 | layer = net.AddMultiplicationLayer("ml"); |
| 52 | |
| 53 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 54 | normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1)); |
| 55 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 56 | |
| 57 | prevLayer = layer; |
| 58 | armnn::SoftmaxDescriptor softmaxDescriptor; |
| 59 | layer = net.AddSoftmaxLayer(softmaxDescriptor, "sm"); |
| 60 | |
| 61 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 62 | layer->GetOutputSlot(0).SetTensorInfo(desc); |
| 63 | |
| 64 | prevLayer = layer; |
| 65 | layer = net.AddOutputLayer(0, "ot"); |
| 66 | |
| 67 | prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 68 | |
| 69 | armnn::IRuntime::CreationOptions options; |
| 70 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 71 | |
| 72 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 73 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(net, backends, runtime->GetDeviceSpec()); |
| 74 | static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph().AllocateDynamicBuffers(); |
| 75 | BOOST_CHECK(optNet); |
| 76 | |
| 77 | // Validates workloads. |
| 78 | armnn::RefWorkloadFactory fact; |
| 79 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 80 | { |
| 81 | BOOST_CHECK_NO_THROW( |
| 82 | layer->CreateWorkload(static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph(), fact)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(OptimizeValidateWorkloadsCpuRefPermuteLayer) |
| 87 | { |
| 88 | // Create runtime in which test will run |
| 89 | armnn::IRuntime::CreationOptions options; |
| 90 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 91 | |
| 92 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 93 | |
| 94 | // build up the structure of the network |
| 95 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 96 | |
| 97 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 98 | |
| 99 | armnn::PermuteDescriptor descriptor({0, 2, 3, 1}); |
| 100 | armnn::IConnectableLayer* permute = net->AddPermuteLayer(descriptor); |
| 101 | |
| 102 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 103 | |
| 104 | input->GetOutputSlot(0).Connect(permute->GetInputSlot(0)); |
| 105 | permute->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 106 | |
| 107 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 1, 4, 4 }, armnn::DataType::Float32)); |
| 108 | permute->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 4, 1, 4 }, armnn::DataType::Float32)); |
| 109 | |
| 110 | // optimize the network |
| 111 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 112 | |
| 113 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 114 | { |
| 115 | BOOST_CHECK(layer->GetBackendId() == armnn::Compute::CpuRef); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(OptimizeValidateWorkloadsCpuRefMeanLayer) |
| 120 | { |
| 121 | // Create runtime in which test will run |
| 122 | armnn::IRuntime::CreationOptions options; |
| 123 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 124 | |
| 125 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 126 | |
| 127 | // build up the structure of the network |
| 128 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 129 | |
| 130 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 131 | |
| 132 | armnn::MeanDescriptor descriptor({ 0, 1 }, false); |
| 133 | armnn::IConnectableLayer* meanLayer = net->AddMeanLayer(descriptor); |
| 134 | |
| 135 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 136 | |
| 137 | input->GetOutputSlot(0).Connect(meanLayer->GetInputSlot(0)); |
| 138 | meanLayer->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 139 | |
| 140 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 4, 3, 2 }, armnn::DataType::Float32)); |
| 141 | meanLayer->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 2 }, armnn::DataType::Float32)); |
| 142 | |
| 143 | // optimize the network |
| 144 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 145 | |
| 146 | for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph()) |
| 147 | { |
| 148 | BOOST_CHECK(layer->GetBackendId() == armnn::Compute::CpuRef); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(FP16TurboModeTestOnCpuRef) |
| 153 | { |
| 154 | // Test to check when FP16 Turbo mode set |
| 155 | // it converts the FP32 network to FP16 Network |
| 156 | // add FP32ToFP16 conversion layer after the InputLayer |
| 157 | // add FP16ToFP32 conversion layer after the OutputLayer |
| 158 | // checks the other layers if they are supported in FP16 |
| 159 | // if they are not put the conversion layers before and after |
| 160 | // if they are not supported in FP16 use FP32 instead |
| 161 | // if there are inverse conversion layers remove them with optimization |
| 162 | // at the moment FloorLayer is not supported in FP16 so it rolls back to FP32 |
| 163 | // and inverse conversion layers are removed by the optimizer |
| 164 | armnn::Network net; |
| 165 | |
| 166 | // Defines layers. |
| 167 | auto input = net.AddInputLayer(0); |
| 168 | auto floor = net.AddFloorLayer(); |
| 169 | auto output = net.AddOutputLayer(0); |
| 170 | |
| 171 | // Connects layers. |
| 172 | input->GetOutputSlot(0).Connect(floor->GetInputSlot(0)); |
| 173 | floor->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 174 | |
| 175 | armnn::TensorShape shape({4}); |
| 176 | armnn::TensorInfo info(shape, armnn::DataType::Float32); |
| 177 | input->GetOutputSlot(0).SetTensorInfo(info); |
| 178 | floor->GetOutputSlot(0).SetTensorInfo(info); |
| 179 | |
| 180 | armnn::IRuntime::CreationOptions options; |
| 181 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 182 | |
| 183 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 184 | |
| 185 | armnn::OptimizerOptions optimizerOptions; |
| 186 | optimizerOptions.m_ReduceFp32ToFp16 = true; |
| 187 | |
| 188 | armnn::IOptimizedNetworkPtr optimizedNet = armnn::Optimize(net, backends, runtime->GetDeviceSpec(), |
| 189 | optimizerOptions); |
| 190 | |
| 191 | std::ostringstream ss; |
| 192 | optimizedNet->SerializeToDot(ss); |
| 193 | |
| 194 | auto inputId = input->GetGuid(); |
| 195 | auto floorId = floor->GetGuid(); |
| 196 | auto outputId = output->GetGuid(); |
| 197 | |
| 198 | std::stringstream expected; |
| 199 | expected << |
| 200 | "digraph Optimized {\n" |
| 201 | " node [shape=\"record\"];\n" |
| 202 | " edge [fontsize=8 fontcolor=\"blue\" fontname=\"arial-bold\"];\n" |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame^] | 203 | " " << inputId << " [label=\"{Input|LayerType : Input\\lBackendID : CpuRef\\l}\"];\n" |
| 204 | " " << floorId << " [label=\"{Floor|LayerType : Floor\\lBackendID : CpuRef\\l}\"];\n" |
| 205 | " " << outputId << " [label=\"{Output|LayerType : Output\\lBackendID : CpuRef\\l}\"];\n" |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 206 | " " << inputId << " -> " << floorId << " [label=< [4] >];\n" |
| 207 | " " << floorId << " -> " << outputId << " [label=< [4] >];\n" |
| 208 | "}\n"; |
| 209 | |
| 210 | BOOST_TEST(ss.str() == expected.str()); |
| 211 | } |
| 212 | |
keidav01 | 738c2e6 | 2018-12-11 16:14:20 +0000 | [diff] [blame] | 213 | BOOST_AUTO_TEST_CASE(DebugTestOnCpuRef) |
| 214 | { |
| 215 | armnn::Network net; |
| 216 | |
| 217 | armnn::ActivationDescriptor activation1Descriptor; |
| 218 | activation1Descriptor.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 219 | activation1Descriptor.m_A = 1.f; |
| 220 | activation1Descriptor.m_B = -1.f; |
| 221 | |
| 222 | // Defines layers. |
| 223 | auto input = net.AddInputLayer(0, "InputLayer"); |
| 224 | auto activation = net.AddActivationLayer(activation1Descriptor, "ActivationLayer"); |
| 225 | auto output = net.AddOutputLayer(0, "OutputLayer"); |
| 226 | |
| 227 | // Connects layers. |
| 228 | input->GetOutputSlot(0).Connect(activation->GetInputSlot(0)); |
| 229 | activation->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 230 | |
| 231 | armnn::TensorShape shape({4}); |
| 232 | armnn::TensorInfo info(shape, armnn::DataType::Float32); |
| 233 | input->GetOutputSlot(0).SetTensorInfo(info); |
| 234 | activation->GetOutputSlot(0).SetTensorInfo(info); |
| 235 | |
| 236 | armnn::IRuntime::CreationOptions options; |
| 237 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 238 | |
| 239 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
| 240 | |
| 241 | armnn::OptimizerOptions optimizerOptions; |
| 242 | optimizerOptions.m_Debug = true; |
| 243 | |
| 244 | armnn::IOptimizedNetworkPtr optimizedNet = armnn::Optimize(net, backends, runtime->GetDeviceSpec(), |
| 245 | optimizerOptions); |
| 246 | |
| 247 | const armnn::Graph& graph = static_cast<armnn::OptimizedNetwork*>(optimizedNet.get())->GetGraph(); |
| 248 | // Tests that all layers are present in the graph. |
| 249 | BOOST_TEST(graph.GetNumLayers() == 5); |
| 250 | |
| 251 | // Tests that the vertices exist and have correct names. |
| 252 | BOOST_TEST(GraphHasNamedLayer(graph, "InputLayer")); |
| 253 | BOOST_TEST(GraphHasNamedLayer(graph, "DebugLayerAfterInputLayer")); |
| 254 | BOOST_TEST(GraphHasNamedLayer(graph, "ActivationLayer")); |
| 255 | BOOST_TEST(GraphHasNamedLayer(graph, "DebugLayerAfterActivationLayer")); |
| 256 | BOOST_TEST(GraphHasNamedLayer(graph, "OutputLayer")); |
| 257 | } |
| 258 | |
Aron Virginas-Tar | 7010400 | 2018-10-24 15:33:28 +0100 | [diff] [blame] | 259 | BOOST_AUTO_TEST_SUITE_END() |