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