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 | |
| 6 | #pragma once |
| 7 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 8 | #include <Half.hpp> |
| 9 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | #include <armnn/ArmNN.hpp> |
| 11 | #include <armnn/Tensor.hpp> |
| 12 | #include <armnn/TypesUtils.hpp> |
| 13 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 14 | #include <backendsCommon/CpuTensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
| 16 | #include <test/TensorHelpers.hpp> |
| 17 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | |
| 19 | LayerTestResult<armnn::Half, 4> SimpleConvertFp32ToFp16Test(armnn::IWorkloadFactory& workloadFactory) |
| 20 | { |
| 21 | using namespace half_float::literal; |
| 22 | |
| 23 | const armnn::TensorInfo inputTensorInfo({1, 3, 2, 3}, armnn::DataType::Float32); |
| 24 | const armnn::TensorInfo outputTensorInfo({1, 3, 2, 3}, armnn::DataType::Float16); |
| 25 | |
| 26 | auto input = MakeTensor<float, 4>(inputTensorInfo, |
| 27 | { -37.5f, -15.2f, -8.76f, -2.0f, -1.5f, -1.3f, -0.5f, -0.4f, 0.0f, |
| 28 | 1.0f, 0.4f, 0.5f, 1.3f, 1.5f, 2.0f, 8.76f, 15.2f, 37.5f }); |
| 29 | |
| 30 | LayerTestResult<armnn::Half, 4> ret(outputTensorInfo); |
| 31 | ret.outputExpected = MakeTensor<armnn::Half, 4>(outputTensorInfo, |
| 32 | { -37.5_h, -15.2_h, -8.76_h, -2.0_h, -1.5_h, -1.3_h, -0.5_h, -0.4_h, 0.0_h, |
| 33 | 1.0_h, 0.4_h, 0.5_h, 1.3_h, 1.5_h, 2.0_h, 8.76_h, 15.2_h, 37.5_h }); |
| 34 | |
| 35 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 36 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 37 | |
| 38 | armnn::ConvertFp32ToFp16QueueDescriptor data; |
| 39 | armnn::WorkloadInfo info; |
| 40 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 41 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 42 | |
| 43 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvertFp32ToFp16(data, info); |
| 44 | |
| 45 | inputHandle->Allocate(); |
| 46 | outputHandle->Allocate(); |
| 47 | |
| 48 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 49 | |
| 50 | workload->Execute(); |
| 51 | |
| 52 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 53 | |
| 54 | return ret; |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 55 | } |