Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ConvertBf16ToFp32TestImpl.hpp" |
| 7 | |
| 8 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 9 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 10 | |
| 11 | #include <test/TensorHelpers.hpp> |
| 12 | |
| 13 | LayerTestResult<float, 4> ConvertBf16ToFp32Test( |
| 14 | armnn::IWorkloadFactory& workloadFactory, |
| 15 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 16 | { |
| 17 | IgnoreUnused(memoryManager); |
| 18 | |
| 19 | const armnn::TensorInfo inputTensorInfo({1, 3, 2, 3}, armnn::DataType::BFloat16); |
| 20 | const armnn::TensorInfo outputTensorInfo({1, 3, 2, 3}, armnn::DataType::Float32); |
| 21 | |
| 22 | std::vector<armnn::BFloat16> inputValues = armnnUtils::QuantizedVector<armnn::BFloat16>( |
| 23 | { |
| 24 | -37.5f, -15.2f, -8.76f, -2.0f, -1.5f, -1.3f, -0.5f, -0.4f, 0.0f, |
| 25 | 1.0f, 0.4f, 0.5f, 1.3f, 1.5f, 2.0f, 8.76f, 15.2f, 37.5f |
| 26 | }, |
| 27 | 1.0f, 0); |
| 28 | |
| 29 | auto input = MakeTensor<armnn::BFloat16, 4>(inputTensorInfo, std::vector<armnn::BFloat16>(inputValues)); |
| 30 | |
| 31 | LayerTestResult<float, 4> ret(outputTensorInfo); |
| 32 | ret.outputExpected = MakeTensor<float, 4>(outputTensorInfo, |
| 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 | |
| 36 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 37 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 38 | |
| 39 | armnn::ConvertBf16ToFp32QueueDescriptor data; |
| 40 | armnn::WorkloadInfo info; |
| 41 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 42 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 43 | |
| 44 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvertBf16ToFp32(data, info); |
| 45 | |
| 46 | inputHandle->Allocate(); |
| 47 | outputHandle->Allocate(); |
| 48 | |
| 49 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 50 | |
| 51 | workload->Execute(); |
| 52 | |
| 53 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 54 | |
| 55 | return ret; |
| 56 | } |