blob: ad504f266472d555bc02e6cbeb76246bde6e31aa [file] [log] [blame]
Michalis Spyroue7e96e02018-04-13 13:44:10 +01001/*
Gunes Bayiref637392024-02-12 21:32:51 +00002 * Copyright (c) 2017-2021, 2024 Arm Limited.
Michalis Spyroue7e96e02018-04-13 13:44:10 +01003 *
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
Anthony Barbier5f707732018-07-03 16:22:02 +010028namespace arm_gemm {
David Mansellce8f6052018-05-17 18:51:26 +010029
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. */
Anthony Barbier5f707732018-07-03 16:22:02 +010033template<typename To, typename Tr>
34class GemvBatched : public GemmCommon<To, Tr> {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010035private:
36 UniqueGemmCommon<To, Tr> _subgemm = nullptr;
37
38public:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010039 GemvBatched(const GemmArgs &args) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010040 /* Just create a subgemm with batches->M */
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010041 GemmArgs newargs = args;
David Manselle39334c2018-07-06 17:53:35 +010042 newargs._Msize = args._nbatches;
43 newargs._nbatches = 1;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010044 newargs._cfg = nullptr;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000045 _subgemm = gemm<To,Tr>(newargs);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010046 }
47
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010048 void set_arrays(const To *A, const int, const int A_batch_stride, const int A_multi_stride,
Michalis Spyroue7e96e02018-04-13 13:44:10 +010049 const To *B, const int ldb, const int B_multi_stride,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010050 Tr *C, const int, const int C_batch_stride, const int C_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010051 const Tr *bias, const int bias_multi_stride) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010052 /* 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,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010055 C, C_batch_stride, 0, C_multi_stride,
56 bias, bias_multi_stride);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010057 }
58
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000059 ndrange_t get_window_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010060 return _subgemm->get_window_size();
61 }
62
Anthony Barbier5f707732018-07-03 16:22:02 +010063 void set_nthreads(int nthreads) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010064 _subgemm->set_nthreads(nthreads);
65 }
66
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010067 void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) override {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000068 _subgemm->execute(work_range, thread_locator, threadid);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010069 }
70
Anthony Barbier5f707732018-07-03 16:22:02 +010071 size_t get_working_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010072 return _subgemm->get_working_size();
73 }
74
Anthony Barbier5f707732018-07-03 16:22:02 +010075 void set_working_space(void *space) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010076 _subgemm->set_working_space(space);
77 }
78
Anthony Barbier5f707732018-07-03 16:22:02 +010079 bool B_is_pretransposed() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010080 return _subgemm->B_is_pretransposed();
81 }
82
Anthony Barbier5f707732018-07-03 16:22:02 +010083 bool B_pretranspose_required() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010084 return _subgemm->B_pretranspose_required();
85 }
86
Anthony Barbier5f707732018-07-03 16:22:02 +010087 size_t get_B_pretransposed_array_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010088 return _subgemm->get_B_pretransposed_array_size();
89 }
90
Gunes Bayiref637392024-02-12 21:32:51 +000091 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride, bool transposed) override {
92 _subgemm->pretranspose_B_array(buffer, B, ldb, B_multi_stride, transposed);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010093 }
94
Anthony Barbier5f707732018-07-03 16:22:02 +010095 void set_pretransposed_B_data(void *buffer) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010096 _subgemm->set_pretransposed_B_data(buffer);
97 }
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010098
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 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110};
111
112} // namespace arm_gemm