blob: f117c92e496cdc3b9b9e467069ef6c0994f78559 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
David Beckac42efd2018-09-26 17:41:13 +01006#include <armnn/ArmNN.hpp>
7#include <armnn/Descriptors.hpp>
8#include <armnn/IRuntime.hpp>
9#include <armnn/INetwork.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#include <Half.hpp>
arovir01616e7752018-10-01 17:08:59 +010011
David Beckac42efd2018-09-26 17:41:13 +010012#include <Graph.hpp>
13#include <Optimizer.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000014#include <backendsCommon/CpuTensorHandle.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010015
16#include <boost/core/ignore_unused.hpp>
17#include <boost/test/unit_test.hpp>
18
telsoa01c577f2c2018-08-31 09:22:23 +010019#include <set>
20
21using namespace armnn;
22
23BOOST_AUTO_TEST_SUITE(Fp16Support)
24
25BOOST_AUTO_TEST_CASE(Fp16DataTypeSupport)
26{
27 Graph graph;
28
29 Layer* const inputLayer1 = graph.AddLayer<InputLayer>(1, "input1");
30 Layer* const inputLayer2 = graph.AddLayer<InputLayer>(2, "input2");
31
32 Layer* const additionLayer = graph.AddLayer<AdditionLayer>("addition");
33 Layer* const outputLayer = graph.AddLayer<armnn::OutputLayer>(0, "output");
34
35 TensorInfo fp16TensorInfo({1, 2, 3, 5}, armnn::DataType::Float16);
36 inputLayer1->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(0));
37 inputLayer2->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(1));
38 additionLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
39
40 inputLayer1->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
41 inputLayer2->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
42 additionLayer->GetOutputSlot().SetTensorInfo(fp16TensorInfo);
43
44 BOOST_CHECK(inputLayer1->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16);
45 BOOST_CHECK(inputLayer2->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16);
46 BOOST_CHECK(additionLayer->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16);
telsoa01c577f2c2018-08-31 09:22:23 +010047}
48
49BOOST_AUTO_TEST_CASE(Fp16AdditionTest)
50{
51 using namespace half_float::literal;
52 // Create runtime in which test will run
53 IRuntime::CreationOptions options;
Aron Virginas-Tar3b278e92018-10-12 13:00:55 +010054 IRuntimePtr runtime(IRuntime::Create(options));
telsoa01c577f2c2018-08-31 09:22:23 +010055
56 // Builds up the structure of the network.
57 INetworkPtr net(INetwork::Create());
58
telsoa01c577f2c2018-08-31 09:22:23 +010059 IConnectableLayer* inputLayer1 = net->AddInputLayer(0);
60 IConnectableLayer* inputLayer2 = net->AddInputLayer(1);
61 IConnectableLayer* additionLayer = net->AddAdditionLayer();
62 IConnectableLayer* outputLayer = net->AddOutputLayer(0);
63
64 inputLayer1->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(0));
65 inputLayer2->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(1));
66 additionLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
67
68 //change to float16
69 TensorInfo fp16TensorInfo(TensorShape({4}), DataType::Float16);
70 inputLayer1->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
71 inputLayer2->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
72 additionLayer->GetOutputSlot(0).SetTensorInfo(fp16TensorInfo);
73
74 // optimize the network
David Beckf0b48452018-10-19 15:20:56 +010075 std::vector<BackendId> backends = {Compute::GpuAcc};
telsoa01c577f2c2018-08-31 09:22:23 +010076 IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());
77
78 // Loads it into the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010079 NetworkId netId;
80 runtime->LoadNetwork(netId, std::move(optNet));
81
82 std::vector<Half> input1Data
83 {
84 1.0_h, 2.0_h, 3.0_h, 4.0_h
85 };
86
87 std::vector<Half> input2Data
88 {
89 100.0_h, 200.0_h, 300.0_h, 400.0_h
90 };
91
92 InputTensors inputTensors
93 {
94 {0,ConstTensor(runtime->GetInputTensorInfo(netId, 0), input1Data.data())},
95 {1,ConstTensor(runtime->GetInputTensorInfo(netId, 0), input2Data.data())}
96 };
97
98 std::vector<Half> outputData(input1Data.size());
99 OutputTensors outputTensors
100 {
101 {0,Tensor(runtime->GetOutputTensorInfo(netId, 0), outputData.data())}
102 };
103
104 // Does the inference.
105 runtime->EnqueueWorkload(netId, inputTensors, outputTensors);
106
107 // Checks the results.
108 BOOST_TEST(outputData == std::vector<Half>({ 101.0_h, 202.0_h, 303.0_h, 404.0_h})); // Add
109}
110
arovir01616e7752018-10-01 17:08:59 +0100111BOOST_AUTO_TEST_SUITE_END()