ArmNN
 22.08
ConvertConstantsFloatToHalfTests.cpp File Reference
#include <TestUtils.hpp>
#include <Optimizer.hpp>
#include <Half.hpp>
#include <doctest/doctest.h>

Go to the source code of this file.

Functions

 TEST_SUITE ("Optimizer")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "Optimizer"  )

Definition at line 15 of file ConvertConstantsFloatToHalfTests.cpp.

References Graph::AddLayer(), OutputSlot::Connect(), armnn::Float16, armnn::Float32, Layer::GetDataType(), Layer::GetInputSlot(), Layer::GetOutputSlot(), armnn::info, FullyConnectedDescriptor::m_BiasEnabled, ConstantLayer::m_LayerOutput, FullyConnectedLayer::m_Weight, armnn::MakeOptimizations(), Optimizer::Pass(), and OutputSlot::SetTensorInfo().

16 {
17 using namespace armnn::optimizations;
18 
19 TEST_CASE("ConvertConstantsFloatToHalfTest")
20 {
21  armnn::Graph graph;
22 
23  const armnn::TensorInfo info({ 1, 1, 1, 2 }, armnn::DataType::Float16);
24 
25  // Create const tensor from fp32 data
26  unsigned int dims[] = { 4, 1, 1, 1 };
27  std::vector<float> floatWeights{ 1.0f, 2.0f, 3.0f, 4.0f };
28  armnn::ConstTensor weights(armnn::TensorInfo(4, dims, armnn::DataType::Float32, 0.0f, 0, true), floatWeights);
29 
30  // Create simple test network
31  auto input = graph.AddLayer<armnn::InputLayer>(0, "input");
32  input->GetOutputSlot().SetTensorInfo(info);
33 
35  fc->m_Weight = std::make_unique<armnn::ScopedTensorHandle>(weights);
36  fc->GetOutputSlot().SetTensorInfo(info);
37 
38  auto output = graph.AddLayer<armnn::OutputLayer>(1, "output");
39 
40  // Connect up the layers
41  input->GetOutputSlot().Connect(fc->GetInputSlot(0));
42  fc->GetOutputSlot().Connect(output->GetInputSlot(0));
43 
44  // Check tensor data type before conversion
45  CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::Float32);
46 
47  // Run the optimizer
49 
50  // Check tensor data type after conversion
51  CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::Float16);
52 
53  // Check whether data matches expected fp16 data
54  const Half* data = fc->m_Weight->GetConstTensor<Half>();
55  CHECK(data[0] == Half(1.0f));
56  CHECK(data[1] == Half(2.0f));
57  CHECK(data[2] == Half(3.0f));
58  CHECK(data[3] == Half(4.0f));
59 }
60 
61 
62 TEST_CASE("ConvertConstantsFloatToHalfTest_constant")
63 {
64  armnn::Graph graph;
65 
66  // Create the simple test network with Weights and Biases as inputs to a FullyConnected layer.
67  auto input = graph.AddLayer<armnn::InputLayer>(0, "Input");
68  auto weights = graph.AddLayer<armnn::ConstantLayer>("Weights");
69  auto biases = graph.AddLayer<armnn::ConstantLayer>("Biases");
70 
72  desc.m_BiasEnabled = true;
73  desc.m_ConstantWeights = true;
74  auto fcLayer = graph.AddLayer<armnn::FullyConnectedLayer>(desc, "FullyConnected");
75  auto output = graph.AddLayer<armnn::OutputLayer>(1, "Output");
76 
77  float expectedWeightsData[] = { 1.0f, 2.0f, 3.0f, 4.0f };
78  float expectedBiasesData[] = { 2.0f, 2.0f };
79 
80  const armnn::TensorInfo inputInfo ({ 1, 2, 2, 3 }, armnn::DataType::Float16);
81  const armnn::TensorInfo outputInfo ({ 1, 2, 2, 3 }, armnn::DataType::Float16);
82  const armnn::TensorInfo weightsInfo({ 4 }, armnn::DataType::Float32, 0.0f, 0, true);
83  const armnn::TensorInfo biasesInfo ({ 2 }, armnn::DataType::Float32, 0.0f, 0, true);
84 
85  // Set the m_LayerOutput for the optimizer to point to.
86  armnn::ConstTensor weightsTensor(weightsInfo, &expectedWeightsData);
87  armnn::ConstTensor biasesTensor(biasesInfo, &expectedBiasesData);
88  weights->m_LayerOutput = std::make_unique<armnn::ScopedTensorHandle>(weightsTensor);
89  biases->m_LayerOutput = std::make_unique<armnn::ScopedTensorHandle>(biasesTensor);
90 
91  input->GetOutputSlot().SetTensorInfo(inputInfo);
92  weights->GetOutputSlot().SetTensorInfo(weightsInfo);
93  biases->GetOutputSlot().SetTensorInfo(biasesInfo);
94  fcLayer->GetOutputSlot().SetTensorInfo(outputInfo);
95 
96  // Connect up the layers
97  input->GetOutputSlot(0).Connect(fcLayer->GetInputSlot(0));
98  weights->GetOutputSlot(0).Connect(fcLayer->GetInputSlot(1));
99  biases->GetOutputSlot(0).Connect(fcLayer->GetInputSlot(2));
100  fcLayer->GetOutputSlot(0).Connect(output->GetInputSlot(0));
101 
102  // Check tensor data type before conversion
103  CHECK(weights->m_LayerOutput->GetTensorInfo().GetDataType() == armnn::DataType::Float32);
104 
105  // Run the optimizer
107 
108  // Check tensor data type after conversion
109  CHECK(weights->m_LayerOutput->GetTensorInfo().GetDataType() == armnn::DataType::Float16);
110 
111  // Check whether weights data matches expected fp16 data
112  const Half* data = weights->m_LayerOutput->GetConstTensor<Half>();
113  CHECK(data[0] == Half(1.0f));
114  CHECK(data[1] == Half(2.0f));
115  CHECK(data[2] == Half(3.0f));
116  CHECK(data[3] == Half(4.0f));
117 
118  // Check whether bias data matches expected fp16 data
119  const Half* biasData = biases->m_LayerOutput->GetConstTensor<Half>();
120  CHECK(biasData[0] == Half(2.0f));
121  CHECK(biasData[1] == Half(2.0f));
122 }
123 
124 
125 }
A layer that the constant data can be bound to.
Optimizer::Optimizations MakeOptimizations(Args &&... args)
Definition: Optimizer.hpp:43
std::shared_ptr< ConstTensorHandle > m_LayerOutput
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:456
int Connect(InputSlot &destination)
Definition: Layer.cpp:112
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:324
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
ConvertConstants< Float32ToFloat16, IsFloat16Layer > ConvertConstantsFloatToHalf
This layer represents a fully connected operation.
std::shared_ptr< ConstTensorHandle > m_Weight
A unique pointer to store Weight values.
A FullyConnectedDescriptor for the FullyConnectedLayer.
bool m_BiasEnabled
Enable/disable bias.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
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:87
DataType GetDataType() const
Definition: Layer.cpp:313
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:326
half_float::half Half
Definition: Half.hpp:18