xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/assembly/CpuGemmAssemblyWrapperKernel.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_ASSEMBLY_GEMM_KERNEL_WRAPPER_KERNEL_H
25*c217d954SCole Faust #define ARM_COMPUTE_ASSEMBLY_GEMM_KERNEL_WRAPPER_KERNEL_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/Utils.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "src/core/NEON/INEKernel.h"
30*c217d954SCole Faust #include "src/cpu/kernels/assembly/arm_gemm_compute_iface.hpp"
31*c217d954SCole Faust 
32*c217d954SCole Faust #include "gemm_common.hpp"
33*c217d954SCole Faust 
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust class ITensor;
37*c217d954SCole Faust 
38*c217d954SCole Faust namespace cpu
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace kernel
41*c217d954SCole Faust {
42*c217d954SCole Faust /** This class is a wrapper for the assembly kernels.
43*c217d954SCole Faust   *
44*c217d954SCole Faust   * Some kernels were written in assembly and highly optimised for specific CPUs like A53 or A55.
45*c217d954SCole Faust   * This class works as a wrapper for these assembly kernels. The arm compute library creates an instance
46*c217d954SCole Faust   * of CpuGemmAssemblyWrapperKernel and other auxiliary data structures to execute a single assembly kernel
47*c217d954SCole Faust   * in the context of an NEFunctions.
48*c217d954SCole Faust   *
49*c217d954SCole Faust   * The type T is the type of the actual kernel implemented in assembly which is of type
50*c217d954SCole Faust   *         template<typename To, typename Tr> class GemmCommon
51*c217d954SCole Faust   *
52*c217d954SCole Faust   *
53*c217d954SCole Faust   */
54*c217d954SCole Faust template <typename TypeInput, typename TypeOutput>
55*c217d954SCole Faust class CpuGemmAssemblyWrapperKernel final : public INEKernel
56*c217d954SCole Faust {
57*c217d954SCole Faust public:
58*c217d954SCole Faust     /** Constructor
59*c217d954SCole Faust      */
CpuGemmAssemblyWrapperKernel()60*c217d954SCole Faust     CpuGemmAssemblyWrapperKernel()
61*c217d954SCole Faust         : _kernel(nullptr), _name("CpuGemmAssemblyWrapperKernel")
62*c217d954SCole Faust     {
63*c217d954SCole Faust     }
64*c217d954SCole Faust 
65*c217d954SCole Faust     CpuGemmAssemblyWrapperKernel(CpuGemmAssemblyWrapperKernel &)  = delete;
66*c217d954SCole Faust     CpuGemmAssemblyWrapperKernel(CpuGemmAssemblyWrapperKernel &&) = default;
67*c217d954SCole Faust     CpuGemmAssemblyWrapperKernel &operator=(CpuGemmAssemblyWrapperKernel &) = delete;
68*c217d954SCole Faust 
name()69*c217d954SCole Faust     const char *name() const override
70*c217d954SCole Faust     {
71*c217d954SCole Faust         return _name.c_str();
72*c217d954SCole Faust     }
73*c217d954SCole Faust 
run(const Window & window,const ThreadInfo & info)74*c217d954SCole Faust     void run(const Window &window, const ThreadInfo &info) override
75*c217d954SCole Faust     {
76*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_NULLPTR((reinterpret_cast<void *>(_kernel)));
77*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
78*c217d954SCole Faust 
79*c217d954SCole Faust         auto win = arm_gemm::to_ndcoord(window);
80*c217d954SCole Faust 
81*c217d954SCole Faust         arm_gemm::ndcoord_t thread_locator{};
82*c217d954SCole Faust 
83*c217d954SCole Faust         _kernel->execute(win, thread_locator, info.thread_id);
84*c217d954SCole Faust     }
85*c217d954SCole Faust 
86*c217d954SCole Faust     // Inherited methods overridden:
run_nd(const Window & window,const ThreadInfo & info,const Window & thread_locator)87*c217d954SCole Faust     void run_nd(const Window &window, const ThreadInfo &info, const Window &thread_locator) override
88*c217d954SCole Faust     {
89*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_NULLPTR((reinterpret_cast<void *>(_kernel)));
90*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
91*c217d954SCole Faust 
92*c217d954SCole Faust         //convert between arm_compute and arm_gemm types
93*c217d954SCole Faust         auto ndc_win = arm_gemm::to_ndcoord(window);
94*c217d954SCole Faust         auto ndc_tlc = arm_gemm::to_ndcoord(thread_locator);
95*c217d954SCole Faust 
96*c217d954SCole Faust         _kernel->execute(ndc_win, ndc_tlc, info.thread_id);
97*c217d954SCole Faust     }
98*c217d954SCole Faust 
99*c217d954SCole Faust     /** Initialise the kernel's input and output.
100*c217d954SCole Faust      *
101*c217d954SCole Faust      * @param[in] kernel          Pointer to an assembly kernel implementation.
102*c217d954SCole Faust      * @param[in] kernel_name_tag Tag to be attacehd to the kernel's name.
103*c217d954SCole Faust      */
configure(arm_gemm::GemmCommon<TypeInput,TypeOutput> * kernel,std::string kernel_name_tag)104*c217d954SCole Faust     void configure(arm_gemm::GemmCommon<TypeInput, TypeOutput> *kernel, std::string kernel_name_tag)
105*c217d954SCole Faust     {
106*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_NULLPTR((reinterpret_cast<void *>(kernel)));
107*c217d954SCole Faust         _kernel = kernel;
108*c217d954SCole Faust 
109*c217d954SCole Faust         Window win = to_window(kernel->get_window_size());
110*c217d954SCole Faust 
111*c217d954SCole Faust         INEKernel::configure(win);
112*c217d954SCole Faust 
113*c217d954SCole Faust         if(!kernel_name_tag.empty())
114*c217d954SCole Faust         {
115*c217d954SCole Faust             _name += "/" + kernel_name_tag;
116*c217d954SCole Faust         }
117*c217d954SCole Faust     }
118*c217d954SCole Faust     /** Return minimum workload size of the relevant kernel
119*c217d954SCole Faust      *
120*c217d954SCole Faust      * @param[in] platform     The CPU platform used to create the context.
121*c217d954SCole Faust      * @param[in] thread_count Number of threads in the execution.
122*c217d954SCole Faust      *
123*c217d954SCole Faust      * @return[out] small_network_mws         Minimum workload size for requested configuration.
124*c217d954SCole Faust      */
get_mws(const CPUInfo & platform,size_t thread_count)125*c217d954SCole Faust     size_t get_mws(const CPUInfo &platform, size_t thread_count) const override
126*c217d954SCole Faust     {
127*c217d954SCole Faust         ARM_COMPUTE_UNUSED(thread_count);
128*c217d954SCole Faust         ARM_COMPUTE_UNUSED(platform);
129*c217d954SCole Faust 
130*c217d954SCole Faust         return ICPPKernel::default_mws;
131*c217d954SCole Faust     }
132*c217d954SCole Faust 
133*c217d954SCole Faust private:
134*c217d954SCole Faust     arm_gemm::GemmCommon<TypeInput, TypeOutput> *_kernel;
135*c217d954SCole Faust     std::string _name;
136*c217d954SCole Faust };
137*c217d954SCole Faust } // namespace kernel
138*c217d954SCole Faust } // namespace cpu
139*c217d954SCole Faust } // namespace arm_compute
140*c217d954SCole Faust #endif /* ARM_COMPUTE_ASSEMBLY_GEMM_KERNEL_WRAPPER_KERNEL_H */
141