aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/kernels/assembly/gemm_common.hpp
blob: 45d1e43274aa0eb5b7223ae09c74c080a6548525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2017-2021,2023-2024 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef ACL_SRC_CPU_KERNELS_ASSEMBLY_GEMM_COMMON_HPP
#define ACL_SRC_CPU_KERNELS_ASSEMBLY_GEMM_COMMON_HPP

#pragma once

#include "convolution_parameters.hpp"
#include "ndrange.hpp"
#include <cstddef>

namespace arm_gemm
{
// Avoid circular dependency with arm_gemm.hpp
struct GemmConfig;

// Abstract class for the GEMM/GEMV functions.
//
// GEMM implementations may be "native" (never require any input
// permutation), "pretransposed" (require permutation up-front) or require
// working space (permute as they go along).  This interface should support
// all of them.

// The real GemmCommon class is templated based on the operand and return
// type.  This is an interface class which is independent of those types.
class IGemmCommon
{
public:
    /* Pass in the pointers to the arrays to be operated on and their
     * strides.  This "generic" version uses void *s, the preferred version
     * is the one provided by templated GemmCommon (below) which takes
     * appropriately typed pointers.  If B is pretransposed (see below) then
     * the settings for B here are ignored.
     */
    virtual void set_arrays_generic(const void                                   *A,
                                    const int                                     lda,
                                    const int                                     A_batch_stride,
                                    const int                                     A_multi_stride,
                                    const void                                   *B,
                                    const int                                     ldb,
                                    /* batches share B */ const int               B_multi_stride,
                                    void                                         *C,
                                    const int                                     ldc,
                                    const int                                     C_batch_stride,
                                    const int                                     C_multi_stride,
                                    const void                                   *bias,
                                    /* no row or batch stride needed */ const int bias_multi_stride) = 0;

    /** @returns an ndrange containing ranges of the compute space which can be
     * broken up and parallelised over
     */
    virtual ndrange_t get_window_size() const = 0;

    /* The maximum thread count is specified when the GEMM is created.  Some
     * implementations need to know how many threads will actually run in
     * order to work properly.
     *
     * In some cases, after creating the GEMM the number of threads needs to
     * be reduced (e.g. not enough work to split across threads).  This
     * method allows the number of actual threads to be run to be set (must
     * be equal or lower).
     *
     * This has an empty default implementation, as GEMMs which don't care
     * about thread count can safely ignore this.
     */
    virtual void set_nthreads(int){};

    /* Whether this GEMM can be dynamically scheduled or not. */
    virtual bool supports_dynamic_scheduling() const
    {
        return false;
    }

    /** Main execute member fucntion
     * @param [in] work_range     specifies the range of work we want to be computed, total range defined by get_window_size()
     * @param [in] thread_locator where are we inside of the thread space
     * @param [in] threadid       a unique threadid
     */
    virtual void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) = 0;

    /*** Working space interface (optional) ***/
    /* Total number of bytes of temporary working space needed.  If zero, it's not necessary to call set_working_space(). */
    virtual size_t get_working_size() const
    {
        return 0;
    }
    /* Provide working space buffer - the void * passed in must remain allocated for the duration of any execute calls. */
    virtual void set_working_space(void *){};

    /*** "Pretransposed" interface (optional) ***/
    /* Is this object set up for pretranspose?  If so, pretranspose_array() needs to be called before execute(); */
    virtual bool B_is_pretransposed() const
    {
        return false;
    }
    /* Does pretranspose still need to be done? */
    virtual bool B_pretranspose_required() const
    {
        return false;
    }
    /* Does pretranspose accept the transposed flag? */
    virtual bool B_pretranspose_supports_transpose() const
    {
        return false;
    }
    /* Total number of bytes of space needed for pretransposed arrays. */
    virtual size_t get_B_pretransposed_array_size() const
    {
        return 0;
    }
    /* Amount of work for the threaded cases */
    virtual size_t get_B_pretranspose_window_size() const
    {
        return 1;
    }
    /* Perform pretranspose - arguments are output, input, input row stride and input multi stride. */
    /* The "real" version of this depends on the templated operand type (see below).  */
    virtual void pretranspose_B_array_generic(void *, const void *, const int, const int, bool) = 0;
    /* Threaded version with window start/end parameters */
    virtual void
    pretranspose_B_array_part_generic(void *, const void *, const int, const int, bool, const size_t, const size_t) = 0;

    /* Set pretransposed data - the void * passed in must previously have been passed to pretranspose_B_array() for the same or a similar GEMM. */
    virtual void set_pretransposed_B_data(void *)
    {
    }

    /*** "Quantized bias" interface (optional) ***/
    /* Set the bias vector for quantized GEMMs */
    virtual void set_quantized_bias(const int32_t *, size_t)
    {
    }

    /*** Indirect interface (optional) ***/
    /* Set the indirect table.  This comprises a number of values per kernel point, and a densely packed array of pointers,
     * multis * batches * kernel_points */
    virtual void set_indirect_parameters_generic(size_t, const void *const *const *)
    {
    }

    /*** Convolution interface (optional) ***/
    /* Set the convolution parameters. */
    virtual void set_convolution_parameters(ConvolutionParameters)
    {
    }

    /*** Dequanize scale interface (optional) ***/
    /* Set the dequantize scale for GEMMs when converting from int to float (float out = scale * float(int out) ) */
    virtual void set_dequantize_scale(const float)
    {
    }

    /*** Introspection interface ***/
    /* Get the configuration of this GEMM */
    virtual GemmConfig get_config() = 0;

    // Destructor
    virtual ~IGemmCommon()
    {
    }
};

/* "Real" GemmCommon class which is templated on the operand and return types.
 *
 * In addition to correctly typed versions of the functions that operate on
 * operand and return data, this class provides a default implementation of
 * 'set_arrays' to capture the provided arguments in protected class
 * members, as essentially any implementation will need these.
 */
template <typename To, typename Tr>
class GemmCommon : public IGemmCommon
{
protected:
    const To *_Aptr              = nullptr;
    int       _lda               = 0;
    int       _A_batch_stride    = 0;
    int       _A_multi_stride    = 0;
    const To *_Bptr              = nullptr;
    int       _ldb               = 0;
    int       _B_multi_stride    = 0;
    Tr       *_Cptr              = nullptr;
    int       _ldc               = 0;
    int       _C_batch_stride    = 0;
    int       _C_multi_stride    = 0;
    const Tr *_bias              = nullptr;
    int       _bias_multi_stride = 0;

public:
    /* Pass in the pointers to the arrays to be operated on and their
     * strides (templated version with appropriate types). */
    virtual void set_arrays(const To                                     *A,
                            const int                                     lda,
                            const int                                     A_batch_stride,
                            const int                                     A_multi_stride,
                            const To                                     *B,
                            const int                                     ldb,
                            /* batches share B */ const int               B_multi_stride,
                            Tr                                           *C,
                            const int                                     ldc,
                            const int                                     C_batch_stride,
                            const int                                     C_multi_stride,
                            const Tr                                     *bias,
                            /* no row or batch stride needed */ const int bias_multi_stride)
    {
        _Aptr              = A;
        _lda               = lda;
        _A_batch_stride    = A_batch_stride;
        _A_multi_stride    = A_multi_stride;
        _Bptr              = B;
        _ldb               = ldb;
        _B_multi_stride    = B_multi_stride;
        _Cptr              = C;
        _ldc               = ldc;
        _C_batch_stride    = C_batch_stride;
        _C_multi_stride    = C_multi_stride;
        _bias              = bias;
        _bias_multi_stride = bias_multi_stride;
    }

    /* Implementation of the void * overload which casts its arguments to the appropriate type. */
    void set_arrays_generic(const void                                   *A,
                            const int                                     lda,
                            const int                                     A_batch_stride,
                            const int                                     A_multi_stride,
                            const void                                   *B,
                            const int                                     ldb,
                            /* batches share B */ const int               B_multi_stride,
                            void                                         *C,
                            const int                                     ldc,
                            const int                                     C_batch_stride,
                            const int                                     C_multi_stride,
                            const void                                   *bias,
                            /* no row or batch stride needed */ const int bias_multi_stride) override
    {
        set_arrays(static_cast<const To *>(A), lda, A_batch_stride, A_multi_stride, static_cast<const To *>(B), ldb,
                   B_multi_stride, static_cast<Tr *>(C), ldc, C_batch_stride, C_multi_stride,
                   static_cast<const Tr *>(bias), bias_multi_stride);
    }

    /*** "Pretransposed" interface ***/

    /* Compute col sums over all columns */
    virtual void requantize_bias(void *, const To *, const int, const int){};

    /* Perform pretranspose - the void * passed in must remain allocated for the duration of any execute calls. */
    /* Arguments are: output buffer pointer, source pointer, source row stride, source multi stride */
    virtual void pretranspose_B_array(void *, const To *, const int, const int, bool){};

    /* Implementation of the void * overload which casts its arguments to the appropriate type. */
    void pretranspose_B_array_generic(
        void *out, const void *in, const int row_stride, const int multi_stride, bool transposed) override
    {
        pretranspose_B_array(out, static_cast<const To *>(in), row_stride, multi_stride, transposed);
    }

    /* Threaded versions of the above.
     * The fallback/backwards compatible version of the threaded interface exposes a window size of 1 and
     * just calls the non-threaded functions to do the work.  This is valid as with window size of 1 the only
     * legal values for start and end are 0 and 1 respectively. */
    virtual void pretranspose_B_array_part(
        void *out, const To *in, const int row_stride, const int multi_stride, bool transposed, size_t, size_t)
    {
        pretranspose_B_array(out, in, row_stride, multi_stride, transposed);
    };

    void pretranspose_B_array_part_generic(void       *out,
                                           const void *in,
                                           const int   row_stride,
                                           const int   multi_stride,
                                           bool        transposed,
                                           size_t      start,
                                           size_t      end) override
    {
        pretranspose_B_array_part(out, static_cast<const To *>(in), row_stride, multi_stride, transposed, start, end);
    }

    /*** Indirect interface ***/
    virtual void set_indirect_parameters(size_t, const To *const *const *)
    {
    }

    void set_indirect_parameters_generic(size_t sz, const void *const *const *ptr) override
    {
        set_indirect_parameters(sz, reinterpret_cast<const To *const *const *>(ptr));
    }
};

} // namespace arm_gemm

#endif // ACL_SRC_CPU_KERNELS_ASSEMBLY_GEMM_COMMON_HPP