telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 6 | #include <armnn/Descriptors.hpp> |
| 7 | #include <armnn/IRuntime.hpp> |
| 8 | #include <armnn/INetwork.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <Half.hpp> |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 10 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 11 | #include <Graph.hpp> |
| 12 | #include <Optimizer.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 13 | #include <armnn/backends/TensorHandle.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 14 | #include <armnn/utility/IgnoreUnused.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 16 | #include <doctest/doctest.h> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | #include <set> |
| 19 | |
| 20 | using namespace armnn; |
| 21 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 22 | TEST_SUITE("Fp16Support") |
| 23 | { |
| 24 | TEST_CASE("Fp16DataTypeSupport") |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | { |
| 26 | Graph graph; |
| 27 | |
| 28 | Layer* const inputLayer1 = graph.AddLayer<InputLayer>(1, "input1"); |
| 29 | Layer* const inputLayer2 = graph.AddLayer<InputLayer>(2, "input2"); |
| 30 | |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 31 | Layer* const additionLayer = graph.AddLayer<ElementwiseBinaryLayer>(BinaryOperation::Add, "addition"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 43 | CHECK(inputLayer1->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16); |
| 44 | CHECK(inputLayer2->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16); |
| 45 | CHECK(additionLayer->GetOutputSlot(0).GetTensorInfo().GetDataType() == armnn::DataType::Float16); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 48 | TEST_CASE("Fp16AdditionTest") |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | { |
| 50 | using namespace half_float::literal; |
| 51 | // Create runtime in which test will run |
| 52 | IRuntime::CreationOptions options; |
Aron Virginas-Tar | 3b278e9 | 2018-10-12 13:00:55 +0100 | [diff] [blame] | 53 | IRuntimePtr runtime(IRuntime::Create(options)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | |
| 55 | // Builds up the structure of the network. |
| 56 | INetworkPtr net(INetwork::Create()); |
| 57 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | IConnectableLayer* inputLayer1 = net->AddInputLayer(0); |
| 59 | IConnectableLayer* inputLayer2 = net->AddInputLayer(1); |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 60 | IConnectableLayer* additionLayer = net->AddElementwiseBinaryLayer(BinaryOperation::Add); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 74 | std::vector<BackendId> backends = {Compute::GpuAcc}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 76 | |
| 77 | // Loads it into the runtime. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 | |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 91 | TensorInfo inputTensorInfo = runtime->GetInputTensorInfo(netId, 0); |
| 92 | inputTensorInfo.SetConstant(true); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | InputTensors inputTensors |
| 94 | { |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 95 | {0,ConstTensor(inputTensorInfo, input1Data.data())}, |
| 96 | {1,ConstTensor(inputTensorInfo, input2Data.data())} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 109 | CHECK(outputData == std::vector<Half>({ 101.0_h, 202.0_h, 303.0_h, 404.0_h})); // Add |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 112 | } |