blob: a44a80c9978da749da6c83c189f53f33209ac081 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +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 "ClConvertFp32ToFp16Workload.hpp"
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00007#include <cl/ClTensorHandle.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01008
Matthew Bentham14e46692018-09-20 15:35:30 +01009#include "ClWorkloadUtils.hpp"
10
telsoa01c577f2c2018-08-31 09:22:23 +010011namespace armnn
12{
13using namespace armcomputetensorutils;
14
15static constexpr arm_compute::ConvertPolicy g_AclConvertPolicy = arm_compute::ConvertPolicy::SATURATE;
16
17ClConvertFp32ToFp16Workload::ClConvertFp32ToFp16Workload(
Sadik Armagane9444752020-12-02 11:28:58 +000018 const ConvertFp32ToFp16QueueDescriptor& descriptor,
19 const WorkloadInfo& info,
20 const arm_compute::CLCompileContext& clCompileContext) :
telsoa01c577f2c2018-08-31 09:22:23 +010021 Float32ToFloat16Workload<ConvertFp32ToFp16QueueDescriptor>(descriptor, info)
22{
23 this->m_Data.ValidateInputsOutputs("ClConvertFp32ToFp16Workload", 1, 1);
24
25 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(this->m_Data.m_Inputs[0])->GetTensor();
26 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(this->m_Data.m_Outputs[0])->GetTensor();
27
Jim Flynn5e7335b2022-02-14 11:22:29 +000028 // Create Proxy tensor and set the initial tensor handle to it
29 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
30 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
31
Kevin May9f6862d2021-10-22 15:42:28 +010032 {
33 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvertFp32ToFp16Workload_configure");
Jim Flynn5e7335b2022-02-14 11:22:29 +000034 m_Layer.configure(clCompileContext, m_InputProxy.get(), m_OutputProxy.get(), g_AclConvertPolicy, 0);
Kevin May9f6862d2021-10-22 15:42:28 +010035 }
telsoa01c577f2c2018-08-31 09:22:23 +010036}
37
38void ClConvertFp32ToFp16Workload::Execute() const
39{
Keith Davisbcd860a2021-08-05 14:20:33 +010040 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvertFp32ToFp16Workload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +010041 RunClFunction(m_Layer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +010042}
43
arovir01085f0a42018-10-08 14:48:19 +010044arm_compute::Status ClConvertFp32ToFp16WorkloadValidate(const TensorInfo& input, const TensorInfo& output)
telsoa01c577f2c2018-08-31 09:22:23 +010045{
46 if (input.GetDataType() != DataType::Float32)
47 {
arovir01085f0a42018-10-08 14:48:19 +010048 return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, "Input should be Float32");
telsoa01c577f2c2018-08-31 09:22:23 +010049 }
50 if (output.GetDataType() != DataType::Float16)
51 {
arovir01085f0a42018-10-08 14:48:19 +010052 return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, "Output should be Float16");
telsoa01c577f2c2018-08-31 09:22:23 +010053 }
54
55 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
56 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
57
58 const arm_compute::Status aclStatus = arm_compute::CLDepthConvertLayer::validate(
59 &aclInputInfo, &aclOutputInfo, g_AclConvertPolicy, 0);
60
telsoa01c577f2c2018-08-31 09:22:23 +010061 return aclStatus;
62}
63
David Monahanec819992022-02-10 14:47:13 +000064void ClConvertFp32ToFp16Workload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
65{
66 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
67 this->m_Data.m_Inputs[slot] = tensorHandle;
68 try
69 {
70 Reconfigure();
71 }
72 catch(armnn::UnimplementedException& e)
73 {
74 // Cannot reconfigure, revert the slot back and throw the exception.
75 this->m_Data.m_Inputs[slot] = backupHandle;
76 throw e;
77 }
78}
79
80// Replace output tensor handle with the given TensorHandle
81void ClConvertFp32ToFp16Workload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
82{
83 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
84 this->m_Data.m_Inputs[slot] = tensorHandle;
85 try
86 {
87 Reconfigure();
88 }
89 catch(armnn::UnimplementedException& e)
90 {
91 // Cannot reconfigure, revert the slot back and throw the exception.
92 this->m_Data.m_Inputs[slot] = backupHandle;
93 throw e;
94 }
95}
96
97void ClConvertFp32ToFp16Workload::Reconfigure()
98{
Jim Flynn5e7335b2022-02-14 11:22:29 +000099 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
100 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
101 m_InputProxy->set(&input);
102 m_OutputProxy->set(&output);
David Monahanec819992022-02-10 14:47:13 +0000103}
telsoa01c577f2c2018-08-31 09:22:23 +0100104
Matthew Bentham14e46692018-09-20 15:35:30 +0100105} //namespace armnn