Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +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 | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ConvertBf16ToFp32TestImpl.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 | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 10 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 11 | #include <TensorHelpers.hpp> |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 12 | |
| 13 | LayerTestResult<float, 4> ConvertBf16ToFp32Test( |
| 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 | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 17 | { |
| 18 | IgnoreUnused(memoryManager); |
| 19 | |
| 20 | const armnn::TensorInfo inputTensorInfo({1, 3, 2, 3}, armnn::DataType::BFloat16); |
| 21 | const armnn::TensorInfo outputTensorInfo({1, 3, 2, 3}, armnn::DataType::Float32); |
| 22 | |
| 23 | std::vector<armnn::BFloat16> inputValues = armnnUtils::QuantizedVector<armnn::BFloat16>( |
| 24 | { |
| 25 | -37.5f, -15.2f, -8.76f, -2.0f, -1.5f, -1.3f, -0.5f, -0.4f, 0.0f, |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 26 | 1.0f, 0.4f, 0.5f, 1.3f, 1.5f, 2.0f, 8.76f, 15.2f, 37.5f |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 27 | }, |
| 28 | 1.0f, 0); |
| 29 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 30 | std::vector<float> actualOutput(outputTensorInfo.GetNumElements()); |
| 31 | std::vector<float> expectedOutput = |
| 32 | { |
| 33 | -37.5f, -15.2f, -8.76f, -2.0f, -1.5f, -1.3f, -0.5f, -0.4f, 0.0f, |
| 34 | 1.0f, 0.4f, 0.5f, 1.3f, 1.5f, 2.0f, 8.76f, 15.2f, 37.5f |
| 35 | }; |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 36 | |
Keith Davis | f500d6c | 2020-08-31 08:32:55 +0100 | [diff] [blame] | 37 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 38 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 39 | |
| 40 | armnn::ConvertBf16ToFp32QueueDescriptor data; |
| 41 | armnn::WorkloadInfo info; |
| 42 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 43 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 44 | |
| 45 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvertBf16ToFp32(data, info); |
| 46 | |
| 47 | inputHandle->Allocate(); |
| 48 | outputHandle->Allocate(); |
| 49 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 50 | CopyDataToITensorHandle(inputHandle.get(), inputValues.data()); |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 51 | |
| 52 | workload->Execute(); |
| 53 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 54 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 55 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 56 | return LayerTestResult<float, 4>(actualOutput, |
| 57 | expectedOutput, |
| 58 | outputHandle->GetShape(), |
| 59 | outputTensorInfo.GetShape()); |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 60 | } |