Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ConvertFp32ToBf16TestImpl.hpp" |
| 7 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 8 | #include <armnnTestUtils/TensorCopyUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 9 | #include <armnnTestUtils/WorkloadTestUtils.hpp> |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 10 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 11 | #include <TensorHelpers.hpp> |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 12 | |
| 13 | LayerTestResult<armnn::BFloat16, 4> ConvertFp32ToBf16Test( |
| 14 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | f500d6c | 2020-08-31 08:32:55 +0100 | [diff] [blame] | 15 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 16 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 17 | { |
| 18 | IgnoreUnused(memoryManager); |
| 19 | |
| 20 | const armnn::TensorInfo inputTensorInfo({1, 2, 4, 3}, armnn::DataType::Float32); |
| 21 | const armnn::TensorInfo outputTensorInfo({1, 2, 4, 3}, armnn::DataType::BFloat16); |
| 22 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 23 | std::vector<float> input = |
| 24 | { |
| 25 | -37.5f, -15.2f, -8.76f, |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 26 | -2.0f, -1.5f, -1.3f, |
| 27 | -0.5f, -0.4f, 0.0f, |
| 28 | 1.0f, 0.4f, 0.5f, |
| 29 | 1.3f, 1.5f, 2.0f, |
| 30 | 8.76f, 15.2f, 37.5f, |
| 31 | 3.8f, // 0x40733333 Round down |
| 32 | 3.1055E+29f, // 0x707ADC3C Round up |
| 33 | 9.149516E-10f, // 0x307B7FFF Round down |
| 34 | -3.8f, // 0xC0733333 Round down |
| 35 | -3.1055E+29f, // 0xF07ADC3C Round up |
| 36 | -9.149516E-10f // 0xB07B7FFF Round down |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 37 | }; |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 38 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 39 | std::vector<armnn::BFloat16> expectedOutput = armnnUtils::QuantizedVector<armnn::BFloat16>( |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 40 | { |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 41 | -37.5f, -15.2f, -8.76f, |
| 42 | -2.0f, -1.5f, -1.3f, |
| 43 | -0.5f, -0.4f, 0.0f, |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 44 | 1.0f, 0.4f, 0.5f, |
| 45 | 1.3f, 1.5f, 2.0f, |
| 46 | 8.76f, 15.2f, 37.5f, |
| 47 | 3.796875f, // 0x4073 |
| 48 | 3.1072295E29f, // 0x707B |
| 49 | 9.131327E-10f, // 0x307B |
| 50 | -3.796875f, // 0xC073 |
| 51 | -3.1072295E29f, // 0xF07B |
| 52 | -9.131327E-10f // 0xB07B |
| 53 | }, |
| 54 | 1.0f, 0); |
| 55 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 56 | std::vector<armnn::BFloat16> actualOutput(outputTensorInfo.GetNumElements()); |
Keith Davis | f500d6c | 2020-08-31 08:32:55 +0100 | [diff] [blame] | 57 | |
| 58 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 59 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
| 60 | |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 61 | armnn::ConvertFp32ToBf16QueueDescriptor data; |
| 62 | armnn::WorkloadInfo info; |
| 63 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 64 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 65 | |
| 66 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvertFp32ToBf16(data, info); |
| 67 | |
| 68 | inputHandle->Allocate(); |
| 69 | outputHandle->Allocate(); |
| 70 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 71 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 72 | |
| 73 | workload->Execute(); |
| 74 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 75 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 76 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 77 | return LayerTestResult<armnn::BFloat16, 4>(actualOutput, |
| 78 | expectedOutput, |
| 79 | outputHandle->GetShape(), |
| 80 | outputTensorInfo.GetShape()); |
| 81 | |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 82 | } |