blob: 017ed9867e6e28edb97abaee58626aa50b30e5a0 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#include "NeonConvertFp32ToFp16Workload.hpp"
7
Matthew Bentham34336f92023-04-27 12:13:50 +00008#include <arm_compute/runtime/NEON/functions/NECast.h>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include <Half.hpp>
David Beckac42efd2018-09-26 17:41:13 +010010#include <Profiling.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000011
12#include <armnnUtils/FloatingPointConverter.hpp>
13
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000014#include <backendsCommon/WorkloadUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010015
Matthew Bentham34336f92023-04-27 12:13:50 +000016static constexpr arm_compute::ConvertPolicy g_AclConvertPolicy = arm_compute::ConvertPolicy::SATURATE;
17
telsoa01c577f2c2018-08-31 09:22:23 +010018namespace armnn
19{
20
Matthew Bentham34336f92023-04-27 12:13:50 +000021arm_compute::Status NeonConvertFp32ToFp16WorkloadValidate(const TensorInfo& input, const TensorInfo& output)
22{
23 // Fallback to portable software implementation if Compute Library NECast won't work, so
24 // this method always returns success
25
26 armnn::IgnoreUnused(input);
27 armnn::IgnoreUnused(output);
28 return arm_compute::Status();
29}
30
telsoa01c577f2c2018-08-31 09:22:23 +010031NeonConvertFp32ToFp16Workload::NeonConvertFp32ToFp16Workload(const ConvertFp32ToFp16QueueDescriptor& descriptor,
32 const WorkloadInfo& info)
33 : Float32ToFloat16Workload<ConvertFp32ToFp16QueueDescriptor>(descriptor, info)
34{
35 this->m_Data.ValidateInputsOutputs("NeonConvertFp32ToFp16Workload", 1, 1);
Matthew Bentham34336f92023-04-27 12:13:50 +000036
37 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
38 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
39
40 if (arm_compute::NECast::validate(input.info(), output.info(), g_AclConvertPolicy))
41 {
42 // Use NECast if supported (needs hardware support for FP16)
43 m_Cast.reset(new arm_compute::NECast);
44 m_Cast->configure(&input, &output, g_AclConvertPolicy);
45 }
46 else
47 {
48 // Else use software implementation from Half.hpp
49 GatherTensorHandlePairs(descriptor, m_TensorHandlePairs);
50 }
telsoa01c577f2c2018-08-31 09:22:23 +010051}
52
53void NeonConvertFp32ToFp16Workload::Execute() const
54{
Keith Davis2d0679f2021-08-05 11:35:00 +010055 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonConvertFp32ToFp16Workload_Execute", this->GetGuid());
telsoa01c577f2c2018-08-31 09:22:23 +010056
Matthew Bentham34336f92023-04-27 12:13:50 +000057 if (m_Cast)
telsoa01c577f2c2018-08-31 09:22:23 +010058 {
Matthew Bentham34336f92023-04-27 12:13:50 +000059 // Use NECast if supported and initialised
60 m_Cast->run();
61 }
62 else
63 {
64 // Else use softwre implementabion using Half.hpp
65 auto convertFunc = [](uint8_t* dst, const uint8_t* src, size_t size)
66 {
67 auto input = reinterpret_cast<const float*>(src);
68 auto output = reinterpret_cast<Half*>(dst);
69 size_t numElements = size/2; // 2 bytes per fp16
70 armnnUtils::FloatingPointConverter::ConvertFloat32To16(input, numElements, output);
71 };
72
73 for (const auto& pair : m_TensorHandlePairs)
74 {
75 CopyTensorContentsGeneric(pair.first, pair.second, convertFunc);
76 }
telsoa01c577f2c2018-08-31 09:22:23 +010077 }
78}
79
David Monahanec819992022-02-10 14:47:13 +000080void NeonConvertFp32ToFp16Workload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
81{
82 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
83 this->m_Data.m_Inputs[slot] = tensorHandle;
84 try
85 {
86 Reconfigure();
87 }
88 catch(armnn::UnimplementedException& e)
89 {
90 // Cannot reconfigure, revert the slot back and throw the exception.
91 this->m_Data.m_Inputs[slot] = backupHandle;
92 throw e;
93 }
94}
95
96// Replace output tensor handle with the given TensorHandle
97void NeonConvertFp32ToFp16Workload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
98{
99 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
100 this->m_Data.m_Inputs[slot] = tensorHandle;
101 try
102 {
103 Reconfigure();
104 }
105 catch(armnn::UnimplementedException& e)
106 {
107 // Cannot reconfigure, revert the slot back and throw the exception.
108 this->m_Data.m_Inputs[slot] = backupHandle;
109 throw e;
110 }
111}
112
113void NeonConvertFp32ToFp16Workload::Reconfigure()
114{
115 throw armnn::UnimplementedException("Reconfigure not implemented for this workload");
116}
117
telsoa01c577f2c2018-08-31 09:22:23 +0100118} //namespace armnn