xref: /aosp_15_r20/external/armnn/src/backends/neon/workloads/NeonConvertFp16ToFp32Workload.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "NeonConvertFp16ToFp32Workload.hpp"
7 
8 #include <armnnUtils/FloatingPointConverter.hpp>
9 
10 #include <Half.hpp>
11 
12 #include <backendsCommon/WorkloadUtils.hpp>
13 
14 namespace armnn
15 {
16 
NeonConvertFp16ToFp32Workload(const ConvertFp16ToFp32QueueDescriptor & descriptor,const WorkloadInfo & info)17 NeonConvertFp16ToFp32Workload::NeonConvertFp16ToFp32Workload(const ConvertFp16ToFp32QueueDescriptor& descriptor,
18                                                              const WorkloadInfo& info)
19      : Float16ToFloat32Workload<ConvertFp16ToFp32QueueDescriptor>(descriptor, info)
20 {
21     this->m_Data.ValidateInputsOutputs("NeonConvertFp16ToFp32Workload", 1, 1);
22     GatherTensorHandlePairs(descriptor, m_TensorHandlePairs);
23 }
24 
Execute() const25 void NeonConvertFp16ToFp32Workload::Execute() const
26 {
27     ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonConvertFp16ToFp32Workload_Execute", this->GetGuid());
28 
29     auto convertFunc = [](uint8_t* dst, const uint8_t* src, size_t size)
30         {
31             auto input = reinterpret_cast<const Half*>(src);
32             auto output = reinterpret_cast<float*>(dst);
33             size_t numElements = size/2; // 2 bytes per fp16
34             armnnUtils::FloatingPointConverter::ConvertFloat16To32(input, numElements, output);
35         };
36 
37     for (const auto& pair : m_TensorHandlePairs)
38     {
39         CopyTensorContentsGeneric(pair.first, pair.second, convertFunc);
40     }
41 }
42 
ReplaceInputTensorHandle(ITensorHandle * tensorHandle,unsigned int slot)43 void NeonConvertFp16ToFp32Workload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
44 {
45     ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
46     this->m_Data.m_Inputs[slot] = tensorHandle;
47     try
48     {
49         Reconfigure();
50     }
51     catch(armnn::UnimplementedException& e)
52     {
53         // Cannot reconfigure, revert the slot back and throw the exception.
54         this->m_Data.m_Inputs[slot] = backupHandle;
55         throw e;
56     }
57 }
58 
59 // Replace output tensor handle with the given TensorHandle
ReplaceOutputTensorHandle(ITensorHandle * tensorHandle,unsigned int slot)60 void NeonConvertFp16ToFp32Workload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
61 {
62     ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
63     this->m_Data.m_Inputs[slot] = tensorHandle;
64     try
65     {
66         Reconfigure();
67     }
68     catch(armnn::UnimplementedException& e)
69     {
70         // Cannot reconfigure, revert the slot back and throw the exception.
71         this->m_Data.m_Inputs[slot] = backupHandle;
72         throw e;
73     }
74 }
75 
Reconfigure()76 void NeonConvertFp16ToFp32Workload::Reconfigure()
77 {
78     throw armnn::UnimplementedException("Reconfigure not implemented for this workload");
79 }
80 
81 } //namespace armnn
82