blob: e9e56842c7ef4d1d8d7c7e0b2e98cac07dedbaa0 [file] [log] [blame]
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +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
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010026#include "ndrange.hpp"
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000027
Pablo Telloeb82fd22018-02-23 13:43:50 +000028#include <cstddef>
29
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010030namespace arm_gemm
31{
Pablo Telloeb82fd22018-02-23 13:43:50 +000032// Abstract class for the GEMM/GEMV functions.
33//
34// GEMM implementations may be "native" (never require any input
35// permutation), "pretransposed" (require permutation up-front) or require
36// working space (permute as they go along). This interface should support
37// all of them.
38
Georgios Pinitas1d480652019-01-23 11:24:50 +000039// The real GemmCommon class is templated based on the operand and return
40// type. This is an interface class which is independent of those types.
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010041class IGemmCommon
42{
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043public:
Pablo Telloeb82fd22018-02-23 13:43:50 +000044 /* Pass in the pointers to the arrays to be operated on and their
Georgios Pinitas14613832019-03-01 19:07:11 +000045 * strides. This "generic" version uses void *s, the preferred version
46 * is the one provided by templated GemmCommon (below) which takes
47 * appropriately typed pointers. If B is pretransposed (see below) then
48 * the settings for B here are ignored.
Anthony Barbier5f707732018-07-03 16:22:02 +010049 */
Georgios Pinitas14613832019-03-01 19:07:11 +000050 virtual void set_arrays_generic(const void *A, const int lda, const int A_batch_stride, const int A_multi_stride,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010051 const void *B, const int ldb, /* batches share B */ const int B_multi_stride,
52 void *C, const int ldc, const int C_batch_stride, const int C_multi_stride,
53 const void *bias, /* no row or batch stride needed */ const int bias_multi_stride) = 0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000054
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000055 /** @returns an ndrange containing ranges of the compute space which can be
56 * broken up and parallelised over
57 */
58 virtual ndrange_t get_window_size() const = 0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000059
60 /* The maximum thread count is specified when the GEMM is created. Some
61 * implementations need to know how many threads will actually run in
62 * order to work properly.
63 *
64 * In some cases, after creating the GEMM the number of threads needs to
65 * be reduced (e.g. not enough work to split across threads). This
66 * method allows the number of actual threads to be run to be set (must
67 * be equal or lower).
68 *
69 * This has an empty default implementation, as GEMMs which don't care
70 * about thread count can safely ignore this.
71 */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010072 virtual void set_nthreads(int) {};
Pablo Telloeb82fd22018-02-23 13:43:50 +000073
Georgios Pinitas1d480652019-01-23 11:24:50 +000074 /* Whether this GEMM can be dynamically scheduled or not. */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010075 virtual bool supports_dynamic_scheduling() const
76 {
77 return false;
78 }
Georgios Pinitas1d480652019-01-23 11:24:50 +000079
Aleksandr Nikolaeva084b462020-06-25 12:25:52 +010080 /** Main execute member function
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000081 * @param [in] work_range specifies the range of work we want to be computed, total range defined by get_window_size()
82 * @param [in] thread_locator where are we inside of the thread space
83 * @naram [in] threadid a unique threadid
84 */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010085 virtual void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) = 0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000086
87 /*** Working space interface (optional) ***/
88 /* Total number of bytes of temporary working space needed. If zero, it's not necessary to call set_working_space(). */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010089 virtual size_t get_working_size() const
90 {
91 return 0;
92 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000093 /* Provide working space buffer - the void * passed in must remain allocated for the duration of any execute calls. */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010094 virtual void set_working_space(void *) {};
Pablo Telloeb82fd22018-02-23 13:43:50 +000095
96 /*** "Pretransposed" interface (optional) ***/
97 /* Is this object set up for pretranspose? If so, pretranspose_array() needs to be called before execute(); */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010098 virtual bool B_is_pretransposed() const
99 {
100 return false;
101 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000102 /* Does pretranspose still need to be done? */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100103 virtual bool B_pretranspose_required() const
104 {
105 return false;
106 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000107 /* Total number of bytes of space needed for pretransposed arrays. */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100108 virtual size_t get_B_pretransposed_array_size() const
109 {
110 return 0;
111 }
Georgios Pinitas1d480652019-01-23 11:24:50 +0000112 /* Perform pretranspose - arguments are output, input, input row stride and input multi stride. */
113 /* The "real" version of this depends on the templated operand type (see below). */
Georgios Pinitas14613832019-03-01 19:07:11 +0000114 virtual void pretranspose_B_array_generic(void *, const void *, const int, const int) = 0;
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100115 /* Set pretransposed data - the void * passed in must previously have been passed to pretranspose_B_array() for the same or a similar GEMM. */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100116 virtual void set_pretransposed_B_data(void *)
117 {
118 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000119
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100120 /*** "Quantized bias" interface (optional) ***/
121 /* Set the bias vector for quantized GEMMs */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100122 virtual void set_quantized_bias(const int32_t *, size_t)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100123 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100124 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100125
Pablo Telloeb82fd22018-02-23 13:43:50 +0000126 // Destructor
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100127 virtual ~IGemmCommon()
128 {
129 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100130};
Pablo Telloeb82fd22018-02-23 13:43:50 +0000131
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000132/* "Real" GemmCommon class which is templated on the operand and return types.
Georgios Pinitas1d480652019-01-23 11:24:50 +0000133 *
134 * In addition to correctly typed versions of the functions that operate on
135 * operand and return data, this class provides a default implementation of
136 * 'set_arrays' to capture the provided arguments in protected class
137 * members, as essentially any implementation will need these.
138 */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100139template <typename To, typename Tr>
140class GemmCommon : public IGemmCommon
141{
Georgios Pinitas1d480652019-01-23 11:24:50 +0000142protected:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100143 const To *_Aptr = nullptr;
144 int _lda = 0;
145 int _A_batch_stride = 0;
146 int _A_multi_stride = 0;
147 const To *_Bptr = nullptr;
148 int _ldb = 0;
149 int _B_multi_stride = 0;
150 Tr *_Cptr = nullptr;
151 int _ldc = 0;
152 int _C_batch_stride = 0;
153 int _C_multi_stride = 0;
154 const Tr *_bias = nullptr;
155 int _bias_multi_stride = 0;
Georgios Pinitas1d480652019-01-23 11:24:50 +0000156
157public:
158 /* Pass in the pointers to the arrays to be operated on and their
159 * strides (templated version with appropriate types). */
160 virtual void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100161 const To *B, const int ldb, /* batches share B */ const int B_multi_stride,
162 Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride,
163 const Tr *bias, /* no row or batch stride needed */ const int bias_multi_stride)
164 {
165 _Aptr = A;
166 _lda = lda;
167 _A_batch_stride = A_batch_stride;
168 _A_multi_stride = A_multi_stride;
169 _Bptr = B;
170 _ldb = ldb;
171 _B_multi_stride = B_multi_stride;
172 _Cptr = C;
173 _ldc = ldc;
174 _C_batch_stride = C_batch_stride;
175 _C_multi_stride = C_multi_stride;
176 _bias = bias;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100177 _bias_multi_stride = bias_multi_stride;
Georgios Pinitas1d480652019-01-23 11:24:50 +0000178 }
179
180 /* Implementation of the void * overload which casts its arguments to the appropriate type. */
Georgios Pinitas14613832019-03-01 19:07:11 +0000181 void set_arrays_generic(const void *A, const int lda, const int A_batch_stride, const int A_multi_stride,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100182 const void *B, const int ldb, /* batches share B */ const int B_multi_stride,
183 void *C, const int ldc, const int C_batch_stride, const int C_multi_stride,
184 const void *bias, /* no row or batch stride needed */ const int bias_multi_stride) override
185 {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000186 set_arrays(static_cast<const To *>(A), lda, A_batch_stride, A_multi_stride,
187 static_cast<const To *>(B), ldb, B_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100188 static_cast<Tr *>(C), ldc, C_batch_stride, C_multi_stride,
189 static_cast<const Tr *>(bias), bias_multi_stride);
Georgios Pinitas1d480652019-01-23 11:24:50 +0000190 }
191
192 /*** "Pretransposed" interface ***/
193
194 /* Perform pretranspose - the void * passed in must remain allocated for the duration of any execute calls. */
195 /* Arguments are: output buffer pointer, source pointer, source row stride, source multi stride */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100196 virtual void pretranspose_B_array(void *, const To *, const int, const int) {};
Georgios Pinitas1d480652019-01-23 11:24:50 +0000197
198 /* Implementation of the void * overload which casts its arguments to the appropriate type. */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100199 void pretranspose_B_array_generic(void *out, const void *in, const int row_stride, const int multi_stride) override
200 {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000201 pretranspose_B_array(out, static_cast<const To *>(in), row_stride, multi_stride);
202 }
Georgios Pinitas1d480652019-01-23 11:24:50 +0000203};
204
Georgios Pinitas14613832019-03-01 19:07:11 +0000205} // namespace arm_gemm