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