Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | c074cdb | 2023-01-13 18:44:00 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClBatchMatMulWorkload.hpp" |
| 7 | |
| 8 | #include "ClWorkloadUtils.hpp" |
| 9 | |
| 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 11 | #include <aclCommon/ArmComputeUtils.hpp> |
| 12 | |
| 13 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 14 | |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 15 | #include <backendsCommon/WorkloadUtils.hpp> |
| 16 | |
| 17 | #include <cl/ClTensorHandle.hpp> |
| 18 | |
Nikhil Raj | 038f52b | 2023-07-31 10:06:32 +0100 | [diff] [blame] | 19 | #include <arm_compute/function_info/MatMulInfo.h> |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 20 | |
| 21 | namespace armnn |
| 22 | { |
Mike Kelly | 0e3fe10 | 2023-01-23 19:32:06 +0000 | [diff] [blame] | 23 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 24 | arm_compute::Status ClBatchMatMulValidate(const TensorInfo& inputInfoX, |
| 25 | const TensorInfo& inputInfoY, |
| 26 | const TensorInfo& outputInfo, |
| 27 | const BatchMatMulDescriptor& descriptor, |
| 28 | const ActivationDescriptor* activationDescriptor) |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 29 | { |
| 30 | if (descriptor.m_AdjointX || descriptor.m_AdjointY ) |
| 31 | { |
| 32 | throw Exception("Support for adjoint not implemented."); |
| 33 | } |
| 34 | if (descriptor.m_DataLayoutX != armnn::DataLayout::NCHW || descriptor.m_DataLayoutY != armnn::DataLayout::NCHW ) |
| 35 | { |
| 36 | throw Exception("Only supported the MatMul in the last 2 dimensions"); |
| 37 | } |
| 38 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 39 | arm_compute::TensorInfo aclInputInfoX = armcomputetensorutils::BuildArmComputeTensorInfo(inputInfoX); |
| 40 | arm_compute::TensorInfo aclInputInfoY = armcomputetensorutils::BuildArmComputeTensorInfo(inputInfoY); |
| 41 | const arm_compute::TensorInfo aclOutputInfo = armcomputetensorutils::BuildArmComputeTensorInfo(outputInfo); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 42 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 43 | // GeMM dispatches kernel handles dynamic inputs differently to static so this flag needs to be set |
| 44 | aclInputInfoX.set_are_values_constant(false); |
| 45 | aclInputInfoY.set_are_values_constant(false); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 46 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 47 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 48 | activationDescriptor); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 49 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 50 | arm_compute::MatMulInfo matMulInfo; |
| 51 | matMulInfo.adj_lhs(descriptor.m_TransposeX); |
| 52 | matMulInfo.adj_rhs(descriptor.m_TransposeY); |
Mike Kelly | 0e3fe10 | 2023-01-23 19:32:06 +0000 | [diff] [blame] | 53 | |
Nikhil Raj | d29d09d | 2023-06-26 11:52:40 +0100 | [diff] [blame] | 54 | return arm_compute::CLMatMul::validate(&aclInputInfoX, &aclInputInfoY, &aclOutputInfo, matMulInfo, activationInfo); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | ClBatchMatMulWorkload::ClBatchMatMulWorkload(const BatchMatMulQueueDescriptor& descriptor, |
| 58 | const WorkloadInfo& info, |
| 59 | const arm_compute::CLCompileContext& clCompileContext) |
| 60 | : ClBaseWorkload<BatchMatMulQueueDescriptor>(descriptor, info) |
| 61 | { |
| 62 | // Report Profiling Details |
| 63 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchMatMulWorkload_Construct", |
| 64 | descriptor.m_Parameters, |
| 65 | info, |
| 66 | this->GetGuid()); |
| 67 | |
| 68 | if (descriptor.m_Parameters.m_AdjointX || descriptor.m_Parameters.m_AdjointY ) |
| 69 | { |
| 70 | throw Exception("Support for adjoint not implemented."); |
| 71 | } |
| 72 | if (descriptor.m_Parameters.m_DataLayoutX != armnn::DataLayout::NCHW || |
| 73 | descriptor.m_Parameters.m_DataLayoutY != armnn::DataLayout::NCHW ) |
| 74 | { |
| 75 | throw Exception("Only supported the MatMul in the last 2 dimensions"); |
| 76 | } |
| 77 | |
| 78 | m_Data.ValidateInputsOutputs("ClBatchMatMulWorkload", 2, 1); |
| 79 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 80 | arm_compute::ICLTensor& inputX = PolymorphicDowncast<ClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 81 | arm_compute::ICLTensor& inputY = PolymorphicDowncast<ClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
| 82 | auto outputHandle = PolymorphicDowncast<ClTensorHandle*>(m_Data.m_Outputs[0]); |
| 83 | arm_compute::ICLTensor& output = outputHandle->GetTensor(); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 84 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 85 | // GeMM dispatches kernel handles dynamic inputs differently to static so this flag needs to be set |
| 86 | inputX.info()->set_are_values_constant(false); |
| 87 | inputY.info()->set_are_values_constant(false); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 88 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 89 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 90 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 91 | arm_compute::MatMulInfo matMulInfo; |
| 92 | matMulInfo.adj_lhs(descriptor.m_Parameters.m_TransposeX); |
| 93 | matMulInfo.adj_rhs(descriptor.m_Parameters.m_TransposeY); |
Mike Kelly | 0e3fe10 | 2023-01-23 19:32:06 +0000 | [diff] [blame] | 94 | |
Nikhil Raj | d29d09d | 2023-06-26 11:52:40 +0100 | [diff] [blame] | 95 | arm_compute::GpuMatMulSettings settings; |
| 96 | |
| 97 | m_MatMulLayer.configure(clCompileContext, &inputX, &inputY, &output, matMulInfo, settings, activationInfo); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 98 | |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 99 | // Report Profiling Details |
| 100 | WorkloadInfo detailsInfo; |
| 101 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 102 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
| 103 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchMatMulWorkload_Construct", |
| 104 | descriptor.m_Parameters, |
| 105 | detailsInfo, |
| 106 | GetGuid()); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void ClBatchMatMulWorkload::Execute() const |
| 110 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 111 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchMatMulWorkload_Execute"); |
Teresa Charlin | 97a3aef | 2023-01-10 10:32:51 +0000 | [diff] [blame] | 112 | RunClFunction(m_MatMulLayer, CHECK_LOCATION()); |
Teresa Charlin | 94916a5 | 2022-10-19 08:48:07 +0100 | [diff] [blame] | 113 | } |
| 114 | } //namespace armnn |