xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/arm_gemm/gemv_batched.hpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 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 #pragma once
25 
26 #include "arm_gemm.hpp"
27 
28 namespace arm_gemm {
29 
30 /* "Batched GEMV" (where M=1 and nbatches>1) can be executed much more
31  * efficiently as a GEMM (with M'=nbatches and nbatches'=1).  This wrapper
32  * implements this.  */
33 template<typename To, typename Tr>
34 class GemvBatched : public GemmCommon<To, Tr> {
35 private:
36     UniqueGemmCommon<To, Tr> _subgemm = nullptr;
37 
38 public:
GemvBatched(const GemmArgs & args)39     GemvBatched(const GemmArgs &args) {
40         /* Just create a subgemm with batches->M */
41         GemmArgs newargs = args;
42         newargs._Msize = args._nbatches;
43         newargs._nbatches = 1;
44         newargs._cfg = nullptr;
45         _subgemm = gemm<To,Tr>(newargs);
46     }
47 
set_arrays(const To * A,const int,const int A_batch_stride,const int A_multi_stride,const To * B,const int ldb,const int B_multi_stride,Tr * C,const int,const int C_batch_stride,const int C_multi_stride,const Tr * bias,const int bias_multi_stride)48     void set_arrays(const To *A, const int, const int A_batch_stride, const int A_multi_stride,
49                     const To *B, const int ldb, const int B_multi_stride,
50                           Tr *C, const int, const int C_batch_stride, const int C_multi_stride,
51                     const Tr *bias, const int bias_multi_stride) override {
52         /* A and C's batch stride becomes their new row stride.  New batch stride is 0 as nbatches for subgemm is always 1. */
53         _subgemm->set_arrays(A, A_batch_stride, 0, A_multi_stride,
54                              B, ldb, B_multi_stride,
55                              C, C_batch_stride, 0, C_multi_stride,
56                              bias, bias_multi_stride);
57     }
58 
get_window_size() const59     ndrange_t get_window_size() const override {
60         return _subgemm->get_window_size();
61     }
62 
set_nthreads(int nthreads)63     void set_nthreads(int nthreads) override {
64         _subgemm->set_nthreads(nthreads);
65     }
66 
execute(const ndcoord_t & work_range,const ndcoord_t & thread_locator,int threadid)67     void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) override {
68         _subgemm->execute(work_range, thread_locator, threadid);
69     }
70 
get_working_size() const71     size_t get_working_size() const override {
72         return _subgemm->get_working_size();
73     }
74 
set_working_space(void * space)75     void set_working_space(void *space) override {
76         _subgemm->set_working_space(space);
77     }
78 
B_is_pretransposed() const79     bool B_is_pretransposed() const override {
80         return _subgemm->B_is_pretransposed();
81     }
82 
B_pretranspose_required() const83     bool B_pretranspose_required() const override {
84         return _subgemm->B_pretranspose_required();
85     }
86 
get_B_pretransposed_array_size() const87     size_t get_B_pretransposed_array_size() const override {
88         return _subgemm->get_B_pretransposed_array_size();
89     }
90 
pretranspose_B_array(void * buffer,const To * B,const int ldb,const int B_multi_stride)91     void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
92         _subgemm->pretranspose_B_array(buffer, B, ldb, B_multi_stride);
93     }
94 
set_pretransposed_B_data(void * buffer)95     void set_pretransposed_B_data(void *buffer) override {
96         _subgemm->set_pretransposed_B_data(buffer);
97     }
98 
get_config()99     GemmConfig get_config() override {
100         GemmConfig c = _subgemm->get_config();
101 
102         std::string new_filter = "gemv_batched[";
103         new_filter.append(c.filter);
104         new_filter.append("]");
105 
106         c.filter = new_filter;
107 
108         return c;
109     }
110 };
111 
112 } // namespace arm_gemm
113