ArmNN
 24.08
ClConvertFp32ToFp16Workload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2018,2020-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include <cl/ClTensorHandle.hpp>
8 
9 #include "ClWorkloadUtils.hpp"
10 
11 namespace armnn
12 {
13 using namespace armcomputetensorutils;
14 
15 static constexpr arm_compute::ConvertPolicy g_AclConvertPolicy = arm_compute::ConvertPolicy::SATURATE;
16 
18  const ConvertFp32ToFp16QueueDescriptor& descriptor,
19  const WorkloadInfo& info,
20  const arm_compute::CLCompileContext& clCompileContext) :
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 
28  // 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 
32  {
33  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvertFp32ToFp16Workload_configure");
34  m_Layer.configure(clCompileContext, m_InputProxy.get(), m_OutputProxy.get(), g_AclConvertPolicy, 0);
35  }
36 }
37 
39 {
40  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvertFp32ToFp16Workload_Execute");
41  RunClFunction(m_Layer, CHECK_LOCATION());
42 }
43 
45 {
46  if (input.GetDataType() != DataType::Float32)
47  {
48  return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, "Input should be Float32");
49  }
50  if (output.GetDataType() != DataType::Float16)
51  {
52  return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, "Output should be Float16");
53  }
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 
61  return aclStatus;
62 }
63 
65 {
66  ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
67  this->m_Data.m_Inputs[slot] = tensorHandle;
68  try
69  {
70  Reconfigure();
71  }
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
82 {
83  ITensorHandle* backupHandle = this->m_Data.m_Outputs[slot];
84  this->m_Data.m_Outputs[slot] = tensorHandle;
85  try
86  {
87  Reconfigure();
88  }
90  {
91  // Cannot reconfigure, revert the slot back and throw the exception.
92  this->m_Data.m_Outputs[slot] = backupHandle;
93  throw e;
94  }
95 }
96 
97 void ClConvertFp32ToFp16Workload::Reconfigure()
98 {
99  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);
103 }
104 
105 } //namespace armnn
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:447
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::ClConvertFp32ToFp16Workload::ClConvertFp32ToFp16Workload
ClConvertFp32ToFp16Workload(const ConvertFp32ToFp16QueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
Definition: ClConvertFp32ToFp16Workload.cpp:17
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ClConvertFp32ToFp16Workload::Execute
virtual void Execute() const override
Definition: ClConvertFp32ToFp16Workload.cpp:38
armnn::DataType::Float32
@ Float32
armnn::ITensorHandle
Definition: ITensorHandle.hpp:16
armnn::ConvertFp32ToFp16QueueDescriptor
Definition: WorkloadData.hpp:457
armnn::ClConvertFp32ToFp16Workload::ReplaceInputTensorHandle
void ReplaceInputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
Definition: ClConvertFp32ToFp16Workload.cpp:64
armnn::DataType::Float16
@ Float16
ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: ClWorkloadUtils.hpp:36
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
ClWorkloadUtils.hpp
armnn::ClConvertFp32ToFp16WorkloadValidate
arm_compute::Status ClConvertFp32ToFp16WorkloadValidate(const TensorInfo &input, const TensorInfo &output)
Definition: ClConvertFp32ToFp16Workload.cpp:44
armnn::BoostLogSeverityMapping::info
@ info
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
ClConvertFp32ToFp16Workload.hpp
armnn::IClTensorHandle
Definition: IClTensorHandle.hpp:13
armnn::Status
Status
Definition: Types.hpp:42
ClTensorHandle.hpp
armnn::BaseWorkload::m_Data
QueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::RunClFunction
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:167
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ClConvertFp32ToFp16Workload::ReplaceOutputTensorHandle
void ReplaceOutputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
Definition: ClConvertFp32ToFp16Workload.cpp:81
armnn::UnimplementedException
Definition: Exceptions.hpp:98
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26
armnn::MultiTypedWorkload
Definition: Workload.hpp:160