John Mcloughlin | 34c1c38 | 2023-05-17 15:08:36 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClElementwiseBinaryWorkload.hpp" |
| 7 | |
| 8 | #include <cl/ClTensorHandle.hpp> |
| 9 | #include <armnn/backends/TensorHandle.hpp> |
| 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 11 | #include <aclCommon/ArmComputeUtils.hpp> |
| 12 | |
| 13 | #include "ClWorkloadUtils.hpp" |
| 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | using namespace armcomputetensorutils; |
| 18 | |
| 19 | ClElementwiseBinaryWorkload::ClElementwiseBinaryWorkload(const ElementwiseBinaryQueueDescriptor& descriptor, |
| 20 | const WorkloadInfo& info, |
| 21 | const arm_compute::CLCompileContext& clCompileContext) |
| 22 | : ClBaseWorkload<ElementwiseBinaryQueueDescriptor>(descriptor, info) |
| 23 | { |
| 24 | this->m_Data.ValidateInputsOutputs("ClElementwiseBinaryWorkload", 2, 1); |
| 25 | |
| 26 | arm_compute::ICLTensor &input0 = static_cast<IClTensorHandle *>(this->m_Data.m_Inputs[0])->GetTensor(); |
| 27 | arm_compute::ICLTensor &input1 = static_cast<IClTensorHandle *>(this->m_Data.m_Inputs[1])->GetTensor(); |
| 28 | arm_compute::ICLTensor &output = static_cast<IClTensorHandle *>(this->m_Data.m_Outputs[0])->GetTensor(); |
| 29 | |
| 30 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 31 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 32 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClElementwiseBinaryWorkload_configure"); |
John Mcloughlin | 34c1c38 | 2023-05-17 15:08:36 +0100 | [diff] [blame] | 33 | |
| 34 | switch (descriptor.m_Parameters.m_Operation) |
| 35 | { |
| 36 | case armnn::BinaryOperation::Power: |
| 37 | { |
| 38 | auto powerLayer = std::make_unique<arm_compute::CLElementwisePower>(); |
| 39 | powerLayer->configure(clCompileContext, &input0, &input1, &output, activationInfo); |
| 40 | m_ElementwiseBinaryLayer.reset(powerLayer.release()); |
| 41 | break; |
| 42 | } |
| 43 | case armnn::BinaryOperation::SqDiff: |
| 44 | { |
| 45 | auto SqDiffLayer = std::make_unique<arm_compute::CLElementwiseSquaredDiff>(); |
| 46 | SqDiffLayer->configure(clCompileContext, &input0, &input1, &output, activationInfo); |
| 47 | m_ElementwiseBinaryLayer.reset(SqDiffLayer.release()); |
| 48 | break; |
| 49 | } |
| 50 | default: |
| 51 | throw InvalidArgumentException("Unknown binary operator", CHECK_LOCATION()); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | void ClElementwiseBinaryWorkload::Execute() const |
| 56 | { |
| 57 | if (m_ElementwiseBinaryLayer) |
| 58 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 59 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClElementwiseBinaryWorkload_Execute"); |
John Mcloughlin | 34c1c38 | 2023-05-17 15:08:36 +0100 | [diff] [blame] | 60 | m_ElementwiseBinaryLayer->run(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | arm_compute::Status ClElementwiseBinaryValidate(const TensorInfo& input0, |
| 65 | const TensorInfo& input1, |
| 66 | const TensorInfo& output, |
| 67 | const ElementwiseBinaryDescriptor& descriptor, |
| 68 | const ActivationDescriptor* activationDescriptor) |
| 69 | { |
| 70 | const arm_compute::TensorInfo aclInput0Info = BuildArmComputeTensorInfo(input0); |
| 71 | const arm_compute::TensorInfo aclInput1Info = BuildArmComputeTensorInfo(input1); |
| 72 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output); |
| 73 | |
| 74 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 75 | activationDescriptor); |
| 76 | |
| 77 | switch (descriptor.m_Operation) |
| 78 | { |
| 79 | case armnn::BinaryOperation::Power: |
| 80 | return arm_compute::CLElementwisePower::validate(&aclInput0Info, |
| 81 | &aclInput1Info, |
| 82 | &aclOutputInfo, |
| 83 | activationInfo); |
| 84 | case armnn::BinaryOperation::SqDiff: |
| 85 | return arm_compute::CLElementwiseSquaredDiff::validate(&aclInput0Info, |
| 86 | &aclInput1Info, |
| 87 | &aclOutputInfo, |
| 88 | activationInfo); |
| 89 | default: |
| 90 | throw InvalidArgumentException("Unknown binary operator", CHECK_LOCATION()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } //namespace armnn |