ArmNN
 20.08
ClOptimizedNetworkTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <Network.hpp>
9 
10 #include <test/GraphUtils.hpp>
11 
12 #include <cl/ClWorkloadFactory.hpp>
13 
14 #include <boost/test/unit_test.hpp>
15 
16 BOOST_AUTO_TEST_SUITE(ClOptimizedNetwork)
17 
18 BOOST_AUTO_TEST_CASE(OptimizeValidateGpuDeviceSupportLayerNoFallback)
19 {
20  // build up the structure of the network
22 
23  armnn::IConnectableLayer* input = net->AddInputLayer(0);
24  armnn::IConnectableLayer* output = net->AddOutputLayer(0);
25 
26  input->GetOutputSlot(0).Connect(output->GetInputSlot(0));
28 
31 
32  std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
33  armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec());
34  BOOST_CHECK(optNet);
35  // validate workloads
37  ClWorkloadFactoryHelper::GetFactory(ClWorkloadFactoryHelper::GetMemoryManager());
38  for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph())
39  {
40  BOOST_CHECK(layer->GetBackendId() == armnn::Compute::GpuAcc);
41  BOOST_CHECK_NO_THROW(
42  layer->CreateWorkload(fact));
43  }
44 }
45 
46 BOOST_AUTO_TEST_CASE(FP16TurboModeTestOnGpuAcc)
47 {
48  // Test to check when Fp16 Turbo mode set
49  // it converts the Fp32 network to Fp16 Network
50  // add Fp32ToFp16 conversion layer after the InputLayer
51  // add Fp16ToFp32 conversion layer after the OutputLayer
52  // checks the other layers if they are supported in Fp16
53  // if they are not put the conversion layers before and after
54  // if they are not supported in Fp16 use Fp32 instead
55  // if there are inverse conversion layers remove them with optimization
56  // at the moment FloorLayer is not supported in Fp16 so it rolls back to Fp32
57  // and inverse conversion layers are removed by the optimizer
58  armnn::Network net;
59 
60  // Defines layers.
61  auto input = net.AddInputLayer(0, "input layer");
62  // ReLu1
63  armnn::ActivationDescriptor activation1Descriptor;
64  activation1Descriptor.m_Function = armnn::ActivationFunction::BoundedReLu;
65  activation1Descriptor.m_A = 1.f;
66  activation1Descriptor.m_B = -1.f;
67  auto activation = net.AddActivationLayer(activation1Descriptor, "activation layer");
68  auto output = net.AddOutputLayer(0, "output layer");
69 
70  // Connects layers.
71  input->GetOutputSlot(0).Connect(activation->GetInputSlot(0));
72  activation->GetOutputSlot(0).Connect(output->GetInputSlot(0));
73 
74  armnn::TensorShape shape({4});
76  input->GetOutputSlot(0).SetTensorInfo(info);
77  activation->GetOutputSlot(0).SetTensorInfo(info);
78 
81 
82  std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc};
83 
84  armnn::OptimizerOptions optimizerOptions;
85  optimizerOptions.m_ReduceFp32ToFp16 = true;
86 
88  net, backends, runtime->GetDeviceSpec(), optimizerOptions);
89 
90  const armnn::Graph& graph = static_cast<armnn::OptimizedNetwork*>(optimizedNet.get())->GetGraph();
91 
92  // Tests that all layers are present in the graph.
93  BOOST_TEST(graph.GetNumLayers() == 5);
94 
95  // Tests that the vertices exist and have correct names.
96  BOOST_TEST(GraphHasNamedLayer(graph, "input layer"));
97  BOOST_TEST(GraphHasNamedLayer(graph, "convert_fp32_to_fp16-0-input layer"));
98  BOOST_TEST(GraphHasNamedLayer(graph, "activation layer"));
99  BOOST_TEST(GraphHasNamedLayer(graph, "convert_fp16_to_fp32-0-output layer"));
100  BOOST_TEST(GraphHasNamedLayer(graph, "output layer"));
101 }
102 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:32
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
IConnectableLayer * AddOutputLayer(LayerBindingId id, const char *name=nullptr) override
Adds an output layer to the network.
Definition: Network.cpp:1461
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:25
bool GraphHasNamedLayer(const armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:10
IConnectableLayer * AddInputLayer(LayerBindingId id, const char *name=nullptr) override
Adds an input layer to the network.
Definition: Network.cpp:1186
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1014
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:593
IConnectableLayer * AddActivationLayer(const ActivationDescriptor &activationDescriptor, const char *name=nullptr) override
Adds an activation layer to the network.
Definition: Network.cpp:1394
GPU Execution: OpenCL: ArmCompute.
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:20
min(a, max(b, input)) ReLu1 & ReLu6.
Private implementation of INetwork.
Definition: Network.hpp:28
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH, Elu).
Definition: Descriptors.hpp:45
BOOST_AUTO_TEST_SUITE_END()
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:101
virtual int Connect(IInputSlot &destination)=0
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:50
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
Definition: Descriptors.hpp:47
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square, Elu).
Definition: Descriptors.hpp:43
BOOST_AUTO_TEST_CASE(OptimizeValidateGpuDeviceSupportLayerNoFallback)