Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 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 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 26 | #include "convolution_parameters.hpp" |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 27 | #include "ndrange.hpp" |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 28 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 29 | #include <cstddef> |
| 30 | |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 31 | namespace arm_gemm |
| 32 | { |
Georgios Pinitas | 4ee8b15 | 2021-07-16 16:16:43 +0100 | [diff] [blame^] | 33 | // Avoid circular dependency with arm_gemm.hpp |
| 34 | struct GemmConfig; |
| 35 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 36 | // Abstract class for the GEMM/GEMV functions. |
| 37 | // |
| 38 | // GEMM implementations may be "native" (never require any input |
| 39 | // permutation), "pretransposed" (require permutation up-front) or require |
| 40 | // working space (permute as they go along). This interface should support |
| 41 | // all of them. |
| 42 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 43 | // The real GemmCommon class is templated based on the operand and return |
| 44 | // type. This is an interface class which is independent of those types. |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 45 | class IGemmCommon |
| 46 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 47 | public: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 48 | /* Pass in the pointers to the arrays to be operated on and their |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 49 | * strides. This "generic" version uses void *s, the preferred version |
| 50 | * is the one provided by templated GemmCommon (below) which takes |
| 51 | * appropriately typed pointers. If B is pretransposed (see below) then |
| 52 | * the settings for B here are ignored. |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 53 | */ |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 54 | virtual void set_arrays_generic(const void *A, const int lda, const int A_batch_stride, const int A_multi_stride, |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 55 | const void *B, const int ldb, /* batches share B */ const int B_multi_stride, |
| 56 | void *C, const int ldc, const int C_batch_stride, const int C_multi_stride, |
| 57 | const void *bias, /* no row or batch stride needed */ const int bias_multi_stride) = 0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 58 | |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 59 | /** @returns an ndrange containing ranges of the compute space which can be |
| 60 | * broken up and parallelised over |
| 61 | */ |
| 62 | virtual ndrange_t get_window_size() const = 0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 63 | |
| 64 | /* The maximum thread count is specified when the GEMM is created. Some |
| 65 | * implementations need to know how many threads will actually run in |
| 66 | * order to work properly. |
| 67 | * |
| 68 | * In some cases, after creating the GEMM the number of threads needs to |
| 69 | * be reduced (e.g. not enough work to split across threads). This |
| 70 | * method allows the number of actual threads to be run to be set (must |
| 71 | * be equal or lower). |
| 72 | * |
| 73 | * This has an empty default implementation, as GEMMs which don't care |
| 74 | * about thread count can safely ignore this. |
| 75 | */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 76 | virtual void set_nthreads(int) {}; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 77 | |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 78 | /* Whether this GEMM can be dynamically scheduled or not. */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 79 | virtual bool supports_dynamic_scheduling() const |
| 80 | { |
| 81 | return false; |
| 82 | } |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 83 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 84 | /** Main execute member fucntion |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 85 | * @param [in] work_range specifies the range of work we want to be computed, total range defined by get_window_size() |
| 86 | * @param [in] thread_locator where are we inside of the thread space |
Sang-Hoon Park | 4f7693d | 2021-05-12 13:59:10 +0100 | [diff] [blame] | 87 | * @param [in] threadid a unique threadid |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 88 | */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 89 | virtual void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) = 0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 90 | |
| 91 | /*** Working space interface (optional) ***/ |
| 92 | /* Total number of bytes of temporary working space needed. If zero, it's not necessary to call set_working_space(). */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 93 | virtual size_t get_working_size() const |
| 94 | { |
| 95 | return 0; |
| 96 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 97 | /* Provide working space buffer - the void * passed in must remain allocated for the duration of any execute calls. */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 98 | virtual void set_working_space(void *) {}; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 99 | |
| 100 | /*** "Pretransposed" interface (optional) ***/ |
| 101 | /* Is this object set up for pretranspose? If so, pretranspose_array() needs to be called before execute(); */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 102 | virtual bool B_is_pretransposed() const |
| 103 | { |
| 104 | return false; |
| 105 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 106 | /* Does pretranspose still need to be done? */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 107 | virtual bool B_pretranspose_required() const |
| 108 | { |
| 109 | return false; |
| 110 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 111 | /* Total number of bytes of space needed for pretransposed arrays. */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 112 | virtual size_t get_B_pretransposed_array_size() const |
| 113 | { |
| 114 | return 0; |
| 115 | } |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 116 | /* Perform pretranspose - arguments are output, input, input row stride and input multi stride. */ |
| 117 | /* The "real" version of this depends on the templated operand type (see below). */ |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 118 | virtual void pretranspose_B_array_generic(void *, const void *, const int, const int) = 0; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 119 | /* Set pretransposed data - the void * passed in must previously have been passed to pretranspose_B_array() for the same or a similar GEMM. */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 120 | virtual void set_pretransposed_B_data(void *) |
| 121 | { |
| 122 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 123 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 124 | /*** "Quantized bias" interface (optional) ***/ |
| 125 | /* Set the bias vector for quantized GEMMs */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 126 | virtual void set_quantized_bias(const int32_t *, size_t) |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 127 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 128 | } |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 129 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 130 | /*** Indirect interface (optional) ***/ |
| 131 | /* Set the indirect table. This comprises a number of values per kernel point, and a densely packed array of pointers, |
| 132 | * multis * batches * kernel_points */ |
| 133 | virtual void set_indirect_parameters_generic(size_t, const void *const *const *) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | /*** Convolution interface (optional) ***/ |
| 138 | /* Set the convolution parameters. */ |
| 139 | virtual void set_convolution_parameters(ConvolutionParameters) |
| 140 | { |
| 141 | } |
| 142 | |
Georgios Pinitas | 4ee8b15 | 2021-07-16 16:16:43 +0100 | [diff] [blame^] | 143 | /*** Introspection interface ***/ |
| 144 | /* Get the configuration of this GEMM */ |
| 145 | virtual GemmConfig get_config() = 0; |
| 146 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 147 | // Destructor |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 148 | virtual ~IGemmCommon() |
| 149 | { |
| 150 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 151 | }; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 152 | |
Joseph Dobson | 6f8b17d | 2020-02-11 19:32:11 +0000 | [diff] [blame] | 153 | /* "Real" GemmCommon class which is templated on the operand and return types. |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 154 | * |
| 155 | * In addition to correctly typed versions of the functions that operate on |
| 156 | * operand and return data, this class provides a default implementation of |
| 157 | * 'set_arrays' to capture the provided arguments in protected class |
| 158 | * members, as essentially any implementation will need these. |
| 159 | */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 160 | template <typename To, typename Tr> |
| 161 | class GemmCommon : public IGemmCommon |
| 162 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 163 | protected: |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 164 | const To *_Aptr = nullptr; |
| 165 | int _lda = 0; |
| 166 | int _A_batch_stride = 0; |
| 167 | int _A_multi_stride = 0; |
| 168 | const To *_Bptr = nullptr; |
| 169 | int _ldb = 0; |
| 170 | int _B_multi_stride = 0; |
| 171 | Tr *_Cptr = nullptr; |
| 172 | int _ldc = 0; |
| 173 | int _C_batch_stride = 0; |
| 174 | int _C_multi_stride = 0; |
| 175 | const Tr *_bias = nullptr; |
| 176 | int _bias_multi_stride = 0; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 177 | |
| 178 | public: |
| 179 | /* Pass in the pointers to the arrays to be operated on and their |
| 180 | * strides (templated version with appropriate types). */ |
| 181 | virtual void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride, |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 182 | const To *B, const int ldb, /* batches share B */ const int B_multi_stride, |
| 183 | Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride, |
| 184 | const Tr *bias, /* no row or batch stride needed */ const int bias_multi_stride) |
| 185 | { |
| 186 | _Aptr = A; |
| 187 | _lda = lda; |
| 188 | _A_batch_stride = A_batch_stride; |
| 189 | _A_multi_stride = A_multi_stride; |
| 190 | _Bptr = B; |
| 191 | _ldb = ldb; |
| 192 | _B_multi_stride = B_multi_stride; |
| 193 | _Cptr = C; |
| 194 | _ldc = ldc; |
| 195 | _C_batch_stride = C_batch_stride; |
| 196 | _C_multi_stride = C_multi_stride; |
| 197 | _bias = bias; |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 198 | _bias_multi_stride = bias_multi_stride; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* Implementation of the void * overload which casts its arguments to the appropriate type. */ |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 202 | void set_arrays_generic(const void *A, const int lda, const int A_batch_stride, const int A_multi_stride, |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 203 | const void *B, const int ldb, /* batches share B */ const int B_multi_stride, |
| 204 | void *C, const int ldc, const int C_batch_stride, const int C_multi_stride, |
| 205 | const void *bias, /* no row or batch stride needed */ const int bias_multi_stride) override |
| 206 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 207 | set_arrays(static_cast<const To *>(A), lda, A_batch_stride, A_multi_stride, |
| 208 | static_cast<const To *>(B), ldb, B_multi_stride, |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 209 | static_cast<Tr *>(C), ldc, C_batch_stride, C_multi_stride, |
| 210 | static_cast<const Tr *>(bias), bias_multi_stride); |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /*** "Pretransposed" interface ***/ |
| 214 | |
| 215 | /* Perform pretranspose - the void * passed in must remain allocated for the duration of any execute calls. */ |
| 216 | /* Arguments are: output buffer pointer, source pointer, source row stride, source multi stride */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 217 | virtual void pretranspose_B_array(void *, const To *, const int, const int) {}; |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 218 | |
| 219 | /* Implementation of the void * overload which casts its arguments to the appropriate type. */ |
Georgios Pinitas | 5aa1a0b | 2020-07-02 20:02:20 +0100 | [diff] [blame] | 220 | void pretranspose_B_array_generic(void *out, const void *in, const int row_stride, const int multi_stride) override |
| 221 | { |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 222 | pretranspose_B_array(out, static_cast<const To *>(in), row_stride, multi_stride); |
| 223 | } |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 224 | |
| 225 | /*** Indirect interface ***/ |
| 226 | virtual void set_indirect_parameters(size_t, const To *const *const *) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | void set_indirect_parameters_generic(size_t sz, const void *const *const *ptr) override |
| 231 | { |
| 232 | set_indirect_parameters(sz, reinterpret_cast<const To *const *const *>(ptr)); |
| 233 | } |
Georgios Pinitas | 1d48065 | 2019-01-23 11:24:50 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
Georgios Pinitas | 1461383 | 2019-03-01 19:07:11 +0000 | [diff] [blame] | 236 | } // namespace arm_gemm |