xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuGemmMatrixAdditionKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "src/cpu/kernels/CpuGemmMatrixAdditionKernel.h"
25 
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/core/Validate.h"
29 #include "src/core/CPP/Validate.h"
30 #include "src/core/NEON/NEFixedPoint.h"
31 #include "src/core/common/Registrars.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34 #include "src/cpu/kernels/gemm_matrix_add/list.h"
35 namespace arm_compute
36 {
37 namespace cpu
38 {
39 namespace kernels
40 {
41 namespace
42 {
43 static const std::vector<CpuGemmMatrixAdditionKernel::GemmMatrixAddKernel> available_kernels =
44 {
45     {
46         "neon_fp32_gemm_matrix_add",
47         [](const DataTypeISASelectorData & data)
__anon60783cda0202() 48         {
49             return (data.dt == DataType::F32);
50         },
51         REGISTER_FP32_NEON(neon_fp32_gemm_matrix_add)
52     },
53     {
54         "neon_fp16_gemm_matrix_add",
55         [](const DataTypeISASelectorData & data)
__anon60783cda0302() 56         {
57             return (data.dt == DataType::F16) && data.isa.fp16;
58         },
59         REGISTER_FP16_NEON(neon_fp16_gemm_matrix_add)
60     },
61 
62 };
63 } // namespace
64 
configure(const ITensorInfo * src,ITensorInfo * dst,float beta)65 void CpuGemmMatrixAdditionKernel::configure(const ITensorInfo *src, ITensorInfo *dst, float beta)
66 {
67     ARM_COMPUTE_UNUSED(dst);
68     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
69 
70     // Perform validation step
71     ARM_COMPUTE_ERROR_THROW_ON(CpuGemmMatrixAdditionKernel::validate(src, dst, beta));
72 
73     _beta         = beta;
74     const auto uk = CpuGemmMatrixAdditionKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
75     ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
76     _func = uk->ukernel;
77     // Configure kernel window
78     Window win = calculate_max_window(*src, Steps());
79     ICPPKernel::configure(win);
80 }
81 
validate(const ITensorInfo * src,const ITensorInfo * dst,float beta)82 Status CpuGemmMatrixAdditionKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, float beta)
83 {
84     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
85     ARM_COMPUTE_UNUSED(beta);
86 
87     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
88     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
89 
90     if(dst->total_size() > 0)
91     {
92         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
93         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
94     }
95     return Status{};
96 }
97 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)98 void CpuGemmMatrixAdditionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
99 {
100     ARM_COMPUTE_UNUSED(info);
101     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
102     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
103     ARM_COMPUTE_ERROR_ON(tensors.empty());
104 
105     const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
106     ITensor       *dst = tensors.get_tensor(TensorType::ACL_DST);
107 
108     if(_beta != 0.0f)
109     {
110         (*_func)(src, dst, window, _beta);
111     }
112 }
113 
name() const114 const char *CpuGemmMatrixAdditionKernel::name() const
115 {
116     return "CpuGemmMatrixAdditionKernel";
117 }
118 
get_available_kernels()119 const std::vector<CpuGemmMatrixAdditionKernel::GemmMatrixAddKernel> &CpuGemmMatrixAdditionKernel::get_available_kernels()
120 {
121     return available_kernels;
122 }
123 } // namespace kernels
124 } // namespace cpu
125 } // namespace arm_compute
126