ArmNN
 21.02
ConvertConstantsBFloatTests.cpp File Reference
#include "../TestUtils.hpp"
#include <BFloat16.hpp>
#include <Optimizer.hpp>
#include <boost/test/unit_test.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (ConvertConstantsFloatToBFloatTest)
 
 BOOST_AUTO_TEST_CASE (ConvertConstantsBFloatToFloatTest)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( ConvertConstantsFloatToBFloatTest  )

Definition at line 18 of file ConvertConstantsBFloatTests.cpp.

References Graph::AddLayer(), armnn::BFloat16, OutputSlot::Connect(), armnn::Float32, Layer::GetOutputSlot(), armnn::info, FullyConnectedLayer::m_Weight, armnn::MakeOptimizations(), Optimizer::Pass(), and OutputSlot::SetTensorInfo().

19 {
20  armnn::Graph graph;
21 
22  const armnn::TensorInfo info({ 1, 1, 1, 2 }, armnn::DataType::BFloat16);
23 
24  // Create const tensor from fp32 data
25  unsigned int dims[] = { 4, 2, 1, 1 };
26  std::vector<float> floatWeights{ 0.0f, -1.0f,
27  3.8f, // 0x40733333 Round down
28  3.1055E+29f, // 0x707ADC3C Round up
29  9.149516E-10f, // 0x307B7FFF Round down
30  -3.8f, // 0xC0733333 Round down
31  -3.1055E+29f, // 0xF07ADC3C Round up
32  -9.149516E-10f // 0xB07B7FFF Round down
33  };
34  armnn::ConstTensor weights(armnn::TensorInfo(4, dims, armnn::DataType::Float32), floatWeights);
35 
36  // Create simple test network
37  auto input = graph.AddLayer<armnn::InputLayer>(0, "input");
38  input->GetOutputSlot().SetTensorInfo(info);
39 
41  fc->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(weights);
42  fc->GetOutputSlot().SetTensorInfo(info);
43 
44  auto output = graph.AddLayer<armnn::OutputLayer>(1, "output");
45 
46  // Connect up the layers
47  input->GetOutputSlot().Connect(fc->GetInputSlot(0));
48  fc->GetOutputSlot().Connect(output->GetInputSlot(0));
49 
50  // Check tensor data type before conversion
51  BOOST_CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::Float32);
52 
53  // Run the optimizer
55 
56  // Check tensor data type after conversion
57  BOOST_CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::BFloat16);
58 
59  // Check whether data matches expected Bf16 data
60  BFloat16* data = fc->m_Weight->GetTensor<BFloat16>();
61  BOOST_CHECK(data[0] == BFloat16(0.0f));
62  BOOST_CHECK(data[1] == BFloat16(-1.0f));
63  BOOST_CHECK(data[2] == BFloat16(3.796875f)); // 0x4073
64  BOOST_CHECK(data[3] == BFloat16(3.1072295E29f)); // 0x707B
65  BOOST_CHECK(data[4] == BFloat16(9.131327E-10f)); // 0x307B
66  BOOST_CHECK(data[5] == BFloat16(-3.796875f)); // 0xC073
67  BOOST_CHECK(data[6] == BFloat16(-3.1072295E29f)); // 0xF07B
68  BOOST_CHECK(data[7] == BFloat16(-9.131327E-10f)); // 0xB07B
69 }
Optimizer::Optimizations MakeOptimizations(Args &&... args)
Definition: Optimizer.hpp:43
std::unique_ptr< ScopedCpuTensorHandle > m_Weight
A unique pointer to store Weight values.
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:402
int Connect(InputSlot &destination)
Definition: Layer.cpp:83
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
ConvertConstants< Float32ToBFloat16, IsBFloat16Layer > ConvertConstantsFloatToBFloat
This layer represents a fully connected operation.
A FullyConnectedDescriptor for the FullyConnectedLayer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
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:58
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( ConvertConstantsBFloatToFloatTest  )

Definition at line 71 of file ConvertConstantsBFloatTests.cpp.

References Graph::AddLayer(), armnn::BFloat16, BOOST_AUTO_TEST_SUITE_END(), OutputSlot::Connect(), FloatingPointConverter::ConvertFloat32ToBFloat16(), armnn::Float32, Layer::GetOutputSlot(), armnn::info, FullyConnectedLayer::m_Weight, armnn::MakeOptimizations(), Optimizer::Pass(), and OutputSlot::SetTensorInfo().

72 {
73  armnn::Graph graph;
74 
75  const armnn::TensorInfo info({ 1, 1, 1, 2 }, armnn::DataType::Float32);
76 
77  // Create the BFloat16 precision input data
78  unsigned int dims[] = { 4, 2, 1, 1 };
79  std::vector<float> convWeightsData{ 0.f, -1.f,
80  3.796875f, // 0x4073
81  3.1072295E29f, // 0x707B
82  9.131327E-10f, // 0x307B
83  -3.796875f, // 0xC073
84  -3.1072295E29f, // 0xF07B
85  -9.131327E-10f // 0xB07B
86  };
87  std::vector<uint16_t> bfWeights(8);
88  armnnUtils::FloatingPointConverter::ConvertFloat32ToBFloat16(convWeightsData.data(), convWeightsData.size(),
89  bfWeights.data());
91 
92  //Create the simple test network
93  auto input = graph.AddLayer<armnn::InputLayer>(0, "input");
94  input->GetOutputSlot().SetTensorInfo(info);
95 
97  fc->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(weights);
98  fc->GetOutputSlot().SetTensorInfo(info);
99 
100  auto output = graph.AddLayer<armnn::OutputLayer>(1, "output");
101 
102  //Connect up the layers
103  input->GetOutputSlot().Connect(fc->GetInputSlot(0));
104  fc->GetOutputSlot().Connect(output->GetInputSlot(0));
105 
106  //Test the tensor info is correct.
107  BOOST_CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::BFloat16);
108 
109  // Run the optimizer
111 
112  //Test the tensor info is correct.
113  BOOST_CHECK(fc->m_Weight->GetTensorInfo().GetDataType() == armnn::DataType::Float32);
114 
115  // Now test the data matches float32 data
116  float* data = fc->m_Weight->GetTensor<float>();
117  BOOST_CHECK(data[0] == 0.0f);
118  BOOST_CHECK(data[1] == -1.0f);
119  BOOST_CHECK(data[2] == 3.796875f);
120  BOOST_CHECK(data[3] == 3.1072295E29f);
121  BOOST_CHECK(data[4] == 9.131327E-10f);
122  BOOST_CHECK(data[5] == -3.796875f);
123  BOOST_CHECK(data[6] == -3.1072295E29f);
124  BOOST_CHECK(data[7] == -9.131327E-10f);
125 }
Optimizer::Optimizations MakeOptimizations(Args &&... args)
Definition: Optimizer.hpp:43
std::unique_ptr< ScopedCpuTensorHandle > m_Weight
A unique pointer to store Weight values.
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:402
int Connect(InputSlot &destination)
Definition: Layer.cpp:83
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
This layer represents a fully connected operation.
ConvertConstants< BFloat16ToFloat32, IsFloat32Layer > ConvertConstantsBFloatToFloat
A FullyConnectedDescriptor for the FullyConnectedLayer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
static void ConvertFloat32ToBFloat16(const float *srcFloat32Buffer, size_t numElements, void *dstBFloat16Buffer)
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:58
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318