ArmNN
 22.02
Fp16SupportTest.cpp File Reference
#include <armnn/Descriptors.hpp>
#include <armnn/IRuntime.hpp>
#include <armnn/INetwork.hpp>
#include <Half.hpp>
#include <Graph.hpp>
#include <Optimizer.hpp>
#include <armnn/backends/TensorHandle.hpp>
#include <armnn/utility/IgnoreUnused.hpp>
#include <doctest/doctest.h>
#include <set>

Go to the source code of this file.

Functions

 TEST_SUITE ("Fp16Support")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "Fp16Support"  )

Definition at line 22 of file Fp16SupportTest.cpp.

References Graph::AddLayer(), IOutputSlot::Connect(), OutputSlot::Connect(), IRuntime::Create(), INetwork::Create(), armnn::Float16, TensorInfo::GetDataType(), IConnectableLayer::GetInputSlot(), IConnectableLayer::GetOutputSlot(), Layer::GetOutputSlot(), OutputSlot::GetTensorInfo(), armnn::GpuAcc, armnn::Optimize(), TensorInfo::SetConstant(), IOutputSlot::SetTensorInfo(), and OutputSlot::SetTensorInfo().

23 {
24 TEST_CASE("Fp16DataTypeSupport")
25 {
26  Graph graph;
27 
28  Layer* const inputLayer1 = graph.AddLayer<InputLayer>(1, "input1");
29  Layer* const inputLayer2 = graph.AddLayer<InputLayer>(2, "input2");
30 
31  Layer* const additionLayer = graph.AddLayer<AdditionLayer>("addition");
32  Layer* const outputLayer = graph.AddLayer<armnn::OutputLayer>(0, "output");
33 
34  TensorInfo fp16TensorInfo({1, 2, 3, 5}, armnn::DataType::Float16);
35  inputLayer1->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(0));
36  inputLayer2->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(1));
37  additionLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
38 
39  inputLayer1->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
40  inputLayer2->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
41  additionLayer->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
42 
44  CHECK(inputLayer2->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16);
45  CHECK(additionLayer->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16);
46 }
47 
48 TEST_CASE("Fp16AdditionTest")
49 {
50  using namespace half_float::literal;
51  // Create runtime in which test will run
53  IRuntimePtr runtime(IRuntime::Create(options));
54 
55  // Builds up the structure of the network.
56  INetworkPtr net(INetwork::Create());
57 
58  IConnectableLayer* inputLayer1 = net->AddInputLayer(0);
59  IConnectableLayer* inputLayer2 = net->AddInputLayer(1);
60  IConnectableLayer* additionLayer = net->AddAdditionLayer();
61  IConnectableLayer* outputLayer = net->AddOutputLayer(0);
62 
63  inputLayer1->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(0));
64  inputLayer2->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(1));
65  additionLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
66 
67  //change to float16
68  TensorInfo fp16TensorInfo(TensorShape({4}), DataType::Float16);
69  inputLayer1->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
70  inputLayer2->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
71  additionLayer->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
72 
73  // optimize the network
74  std::vector<BackendId> backends = {Compute::GpuAcc};
75  IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());
76 
77  // Loads it into the runtime.
78  NetworkId netId;
79  runtime->LoadNetwork(netId, std::move(optNet));
80 
81  std::vector<Half> input1Data
82  {
83  1.0_h, 2.0_h, 3.0_h, 4.0_h
84  };
85 
86  std::vector<Half> input2Data
87  {
88  100.0_h, 200.0_h, 300.0_h, 400.0_h
89  };
90 
91  TensorInfo inputTensorInfo = runtime->GetInputTensorInfo(netId, 0);
92  inputTensorInfo.SetConstant(true);
93  InputTensors inputTensors
94  {
95  {0,ConstTensor(inputTensorInfo, input1Data.data())},
96  {1,ConstTensor(inputTensorInfo, input2Data.data())}
97  };
98 
99  std::vector<Half> outputData(input1Data.size());
100  OutputTensors outputTensors
101  {
102  {0,Tensor(runtime->GetOutputTensorInfo(netId, 0), outputData.data())}
103  };
104 
105  // Does the inference.
106  runtime->EnqueueWorkload(netId, inputTensors, outputTensors);
107 
108  // Checks the results.
109  CHECK(outputData == std::vector<Half>({ 101.0_h, 202.0_h, 303.0_h, 404.0_h})); // Add
110 }
111 
112 }
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:66
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:420
int Connect(InputSlot &destination)
Definition: Layer.cpp:86
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:31
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
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:1680
DataType GetDataType() const
Definition: Tensor.hpp:198
int NetworkId
Definition: IRuntime.hpp:25
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:242
This layer represents an addition operation.
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
void SetTensorInfo(const TensorInfo &tensorInfo) override
Definition: Layer.cpp:61
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:516
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:323
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:241
virtual int Connect(IInputSlot &destination)=0
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:66