blockwise_batched_gemm.hip.hpp 33.9 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
#pragma once
#include "threadwise_gemm.hip.hpp"

Chao Liu's avatar
Chao Liu committed
4
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
5
6
7
8
9
10
          class BlockMatrixA,
          class BlockMatrixB,
          class ThreadMatrixC,
          bool TransA,
          bool TransB,
          bool TransC,
Chao Liu's avatar
Chao Liu committed
11
12
13
14
15
16
          index_t BlockMatrixStrideA,
          index_t BlockMatrixStrideB,
          index_t ThreadMatrixStrideC,
          index_t BatchSize,
          index_t BatchPerThread,
          index_t KPerThreadLoop,
Chao Liu's avatar
Chao Liu committed
17
18
19
          bool DistributeThreadAlongColumnFirst>
struct Blockwise1dStridedBatchedGemmBlockABlockBThreadC
{
Chao Liu's avatar
Chao Liu committed
20
21
    index_t mMyThreadOffsetA = 0;
    index_t mMyThreadOffsetB = 0;
Chao Liu's avatar
Chao Liu committed
22
23
24

    struct MatrixIndex
    {
Chao Liu's avatar
Chao Liu committed
25
26
27
        index_t batch;
        index_t row;
        index_t col;
Chao Liu's avatar
Chao Liu committed
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
    };

    __device__ Blockwise1dStridedBatchedGemmBlockABlockBThreadC()
    {
        constexpr auto a_block_mtx = BlockMatrixA{};
        constexpr auto b_block_mtx = BlockMatrixB{};

        const auto c_thread_mtx_index = GetBeginOfThreadMatrixC(get_thread_local_1d_id());

        mMyThreadOffsetA = c_thread_mtx_index.batch * BlockMatrixStrideA +
                           ((!TransA) ? a_block_mtx.Get1dIndex(c_thread_mtx_index.row, 0)
                                      : a_block_mtx.Get1dIndex(0, c_thread_mtx_index.row));

        mMyThreadOffsetB = c_thread_mtx_index.batch * BlockMatrixStrideB +
                           ((!TransB) ? b_block_mtx.Get1dIndex(0, c_thread_mtx_index.col)
                                      : b_block_mtx.Get1dIndex(c_thread_mtx_index.col, 0));

#if 0
        if(get_thread_local_1d_id() == 0 && get_block_1d_id() == 0)
        {
            print_ConstantMatrixDescriptor(BlockMatrixA{}, "a_block_mtx: ");
            print_ConstantMatrixDescriptor(BlockMatrixB{}, "b_block_mtx: ");
            print_ConstantMatrixDescriptor(ThreadMatrixC{}, "c_thread_mtx: ");

            printf("%u %u, %u %u %u, %u %u\n",
                   get_block_1d_id(),
                   get_thread_local_1d_id(),
                   c_thread_mtx_index.batch,
                   c_thread_mtx_index.row,
                   c_thread_mtx_index.col,
                   mMyThreadOffsetA,
                   mMyThreadOffsetB);
        }
#endif
    }

Chao Liu's avatar
Chao Liu committed
64
    __device__ MatrixIndex GetBeginOfThreadMatrixC(index_t thread_id) const
Chao Liu's avatar
Chao Liu committed
65
66
67
68
69
70
71
72
73
74
    {

        if(TransA && (!TransB) && (!TransC))
        {
            constexpr auto a_block_mtx = BlockMatrixA{};
            constexpr auto b_block_mtx = BlockMatrixB{};

            static_assert(a_block_mtx.NRow() == b_block_mtx.NRow(),
                          "wrong! k dimension not consistent!");

Chao Liu's avatar
Chao Liu committed
75
76
            constexpr index_t MPerBlock = a_block_mtx.NCol();
            constexpr index_t NPerBlock = b_block_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
77
78
79
80

            constexpr auto c_thread_mtx = ThreadMatrixC{};

            // divide thread work
Chao Liu's avatar
Chao Liu committed
81
82
            constexpr index_t MPerThread = c_thread_mtx.NRow();
            constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
83
84
85
86
87

            static_assert(BatchSize % BatchPerThread == 0, "BatchSize % BatchPerThread != 0");
            static_assert(MPerBlock % MPerThread == 0, "MPerBlock % MPerThread != 0");
            static_assert(NPerBlock % NPerThread == 0, "NPerBlock % NPerThread != 0");

Chao Liu's avatar
Chao Liu committed
88
89
90
            constexpr index_t BatchThreadWork = (BatchSize + BatchPerThread - 1) / BatchPerThread;
            constexpr index_t MThreadWork     = (MPerBlock + MPerThread - 1) / MPerThread;
            constexpr index_t NThreadWork     = (NPerBlock + NPerThread - 1) / NPerThread;
Chao Liu's avatar
Chao Liu committed
91
92
93
94
95
96
97

            static_assert(BlockSize == BatchThreadWork * MThreadWork * NThreadWork,
                          "wrong! wrong BlockSize");

            if(DistributeThreadAlongColumnFirst)
            {
                // num of operations can be reduced
Chao Liu's avatar
Chao Liu committed
98
99
100
101
                const index_t b_work_id = thread_id / (MThreadWork * NThreadWork);
                index_t itmp            = thread_id - b_work_id * (MThreadWork * NThreadWork);
                const index_t m_work_id = itmp / NThreadWork;
                const index_t n_work_id = itmp - m_work_id * NThreadWork;
Chao Liu's avatar
Chao Liu committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

                return MatrixIndex{
                    b_work_id * BatchPerThread, m_work_id * MPerThread, n_work_id * NPerThread};
            }
            else
            {
                // not implemented
                assert(false);
            }
        }
        else
        {
            // not implemented
            assert(false);
        }
    }

    // this should be optimized away if input is known
    __device__ static MatrixIndex
Chao Liu's avatar
Chao Liu committed
121
    GetDistanceFromBeginOfThreadMatrixC(index_t batch_in_c, index_t m_in_c, index_t n_in_c)
Chao Liu's avatar
Chao Liu committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    {
        return MatrixIndex{batch_in_c, m_in_c, n_in_c};
    }

    template <class FloatA, class FloatB, class FloatC, class Accumulator>
    __device__ void Run(const FloatA* __restrict__ p_a_block,
                        const FloatB* __restrict__ p_b_block,
                        FloatC* __restrict__ p_c_thread,
                        Accumulator f_accum) const
    {
        if(TransA && (!TransB) && (!TransC))
        {
            constexpr auto True  = integral_constant<bool, true>{};
            constexpr auto False = integral_constant<bool, false>{};

            constexpr auto a_block_mtx  = BlockMatrixA{};
            constexpr auto b_block_mtx  = BlockMatrixB{};
            constexpr auto c_thread_mtx = ThreadMatrixC{};

Chao Liu's avatar
Chao Liu committed
141
            constexpr index_t KPerBlock = a_block_mtx.NRow(); // A is transposed
Chao Liu's avatar
Chao Liu committed
142

Chao Liu's avatar
Chao Liu committed
143
144
            constexpr index_t MPerThread = c_thread_mtx.NRow();
            constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
145
146
147
148
149
150
151
152
153
154
155
156

            // a is transposed, b is not
            constexpr auto a_thread_mtx =
                make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<MPerThread>{});

            constexpr auto b_thread_mtx =
                make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<NPerThread>{});

            FloatA p_a_thread[a_thread_mtx.GetElementSpace()];
            FloatB p_b_thread[b_thread_mtx.GetElementSpace()];

            // loop over k
Chao Liu's avatar
Chao Liu committed
157
            for(index_t k_begin = 0; k_begin < KPerBlock; k_begin += KPerThreadLoop)
Chao Liu's avatar
Chao Liu committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            {
                // read first batch of a, b
                threadwise_matrix_copy(a_block_mtx,
                                       p_a_block + mMyThreadOffsetA +
                                           k_begin * a_block_mtx.RowStride(),
                                       a_thread_mtx,
                                       p_a_thread,
                                       a_thread_mtx.GetLengths());

                threadwise_matrix_copy(b_block_mtx,
                                       p_b_block + mMyThreadOffsetB +
                                           k_begin * b_block_mtx.RowStride(),
                                       b_thread_mtx,
                                       p_b_thread,
                                       b_thread_mtx.GetLengths());

                // loop over batch
Chao Liu's avatar
Chao Liu committed
175
                for(index_t ib = 0; ib + 1 < BatchPerThread; ++ib)
Chao Liu's avatar
Chao Liu committed
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
                {
                    // do current batch of gemm
                    threadwise_gemm(a_thread_mtx,
                                    True,
                                    p_a_thread,
                                    b_thread_mtx,
                                    False,
                                    p_b_thread,
                                    c_thread_mtx,
                                    False,
                                    p_c_thread + ib * ThreadMatrixStrideC,
                                    f_accum);

                    // read next batch of a, b
                    if(BlockMatrixStrideA != 0)
                    {
                        threadwise_matrix_copy(a_block_mtx,
                                               p_a_block + mMyThreadOffsetA +
                                                   (ib + 1) * BlockMatrixStrideA +
                                                   +k_begin * a_block_mtx.RowStride(),
                                               a_thread_mtx,
                                               p_a_thread,
                                               a_thread_mtx.GetLengths());
                    }

                    if(BlockMatrixStrideB != 0)
                    {
                        threadwise_matrix_copy(b_block_mtx,
                                               p_b_block + mMyThreadOffsetB +
                                                   (ib + 1) * BlockMatrixStrideB +
                                                   k_begin * b_block_mtx.RowStride(),
                                               b_thread_mtx,
                                               p_b_thread,
                                               b_thread_mtx.GetLengths());
                    }
                }

                // do last batch of gemm
                threadwise_gemm(a_thread_mtx,
                                True,
                                p_a_thread,
                                b_thread_mtx,
                                False,
                                p_b_thread,
                                c_thread_mtx,
                                False,
                                p_c_thread + (BatchPerThread - 1) * ThreadMatrixStrideC,
                                f_accum);
            }
        }
    }
};

Chao Liu's avatar
Chao Liu committed
229
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
230
231
232
          class BlockMatrixA,
          class BlockMatrixB,
          class ThreadMatrixC,
Chao Liu's avatar
Chao Liu committed
233
234
235
236
237
238
239
240
241
242
243
244
          index_t BlockMatrixStrideA,
          index_t BlockMatrixStrideB,
          index_t ThreadMatrixStrideC,
          index_t BatchSize,
          index_t MPerThreadSubC,
          index_t NPerThreadSubC,
          index_t MLevel0Cluster,
          index_t NLevel0Cluster,
          index_t MLevel1Cluster,
          index_t NLevel1Cluster,
          index_t KPerThreadLoop,
          index_t BatchPerThread>
Chao Liu's avatar
Chao Liu committed
245
246
struct BlockwiseBatchGemmBlockABlockBThreadCTransANormalBNormalC_V2
{
Chao Liu's avatar
Chao Liu committed
247
248
    index_t mMyThreadOffsetA = 0;
    index_t mMyThreadOffsetB = 0;
Chao Liu's avatar
Chao Liu committed
249
250
251

    struct MatrixIndex
    {
Chao Liu's avatar
Chao Liu committed
252
253
254
        index_t batch;
        index_t row;
        index_t col;
Chao Liu's avatar
Chao Liu committed
255
256
257
258
259
260
261
    };

    __device__ BlockwiseBatchGemmBlockABlockBThreadCTransANormalBNormalC_V2()
    {
        static_assert(BatchSize % BatchPerThread == 0,
                      "wrong! BatchSize is not dividable by BatchPerThread");

Chao Liu's avatar
Chao Liu committed
262
        constexpr index_t BatchThreadWork = BatchSize / BatchPerThread;
Chao Liu's avatar
Chao Liu committed
263

Chao Liu's avatar
Chao Liu committed
264
        constexpr index_t ThreadPerLevel1Cluster =
Chao Liu's avatar
Chao Liu committed
265
266
267
268
269
270
271
272
273
274
275
276
            MLevel0Cluster * NLevel0Cluster * MLevel1Cluster * NLevel1Cluster;

        static_assert(BlockSize == BatchThreadWork * ThreadPerLevel1Cluster,
                      "wrong! wrong blocksize\n");

        constexpr auto a_block_mtx  = BlockMatrixA{};
        constexpr auto b_block_mtx  = BlockMatrixB{};
        constexpr auto c_thread_mtx = ThreadMatrixC{};

        static_assert(a_block_mtx.NRow() == b_block_mtx.NRow(),
                      "wrong! K dimension not consistent\n");

Chao Liu's avatar
Chao Liu committed
277
278
279
        constexpr index_t M = a_block_mtx.NCol(); // A is transposed
        constexpr index_t N = b_block_mtx.NCol();
        constexpr index_t K = a_block_mtx.NRow();
Chao Liu's avatar
Chao Liu committed
280

Chao Liu's avatar
Chao Liu committed
281
282
        constexpr index_t MPerThread = c_thread_mtx.NRow();
        constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
283
284
285
286

        static_assert((MPerThread % MPerThreadSubC == 0) && (NPerThread % NPerThreadSubC == 0),
                      "wrong! Cannot evenly divide thread work among repeat \n");

Chao Liu's avatar
Chao Liu committed
287
288
        constexpr index_t MRepeat = MPerThread / MPerThreadSubC;
        constexpr index_t NRepeat = NPerThread / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
289
290
291
292

        static_assert((M % MRepeat == 0) && (N % NRepeat == 0),
                      "wrong! Cannot evenly divide work among repeat\n");

Chao Liu's avatar
Chao Liu committed
293
294
        constexpr index_t MPerLevel1Cluster = M / MRepeat;
        constexpr index_t NPerLevel1Cluster = N / NRepeat;
Chao Liu's avatar
Chao Liu committed
295
296
297
298
299

        static_assert((MPerLevel1Cluster % MLevel1Cluster == 0) &&
                          (NPerLevel1Cluster % NLevel1Cluster == 0),
                      "wrong! Cannot evenly divide work among Level1Cluster\n");

Chao Liu's avatar
Chao Liu committed
300
301
        constexpr index_t MPerLevel0Cluster = MPerLevel1Cluster / MLevel1Cluster;
        constexpr index_t NPerLevel0Cluster = NPerLevel1Cluster / NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

        static_assert((MPerLevel0Cluster % MLevel0Cluster == 0) &&
                          (NPerLevel0Cluster % NLevel0Cluster == 0),
                      "wrong! Cannot evenly divide work among Level0Cluster\n");

        static_assert((MPerThreadSubC == MPerLevel0Cluster / MLevel0Cluster) &&
                          (NPerThreadSubC == NPerLevel0Cluster / NLevel0Cluster),
                      "wrong! thread work size is wrong\n");

        const auto c_thread_mtx_index = GetBeginOfThreadMatrixC(get_thread_local_1d_id());

        mMyThreadOffsetA = c_thread_mtx_index.batch * BlockMatrixStrideA +
                           a_block_mtx.Get1dIndex(0, c_thread_mtx_index.row);

        mMyThreadOffsetB = c_thread_mtx_index.batch * BlockMatrixStrideB +
                           b_block_mtx.Get1dIndex(0, c_thread_mtx_index.col);

#if 0
        if(get_thread_local_1d_id() == 0 && get_block_1d_id() == 0)
        {
            print_ConstantMatrixDescriptor(BlockMatrixA{}, "a_block_mtx: ");
            print_ConstantMatrixDescriptor(BlockMatrixB{}, "b_block_mtx: ");
            print_ConstantMatrixDescriptor(ThreadMatrixC{}, "c_thread_mtx: ");

            printf("%u %u, %u %u %u, %u %u\n",
                   get_block_1d_id(),
                   get_thread_local_1d_id(),
                   c_thread_mtx_index.batch,
                   c_thread_mtx_index.row,
                   c_thread_mtx_index.col,
                   mMyThreadOffsetA,
                   mMyThreadOffsetB);
        }
#endif
    }

Chao Liu's avatar
Chao Liu committed
338
    __device__ MatrixIndex GetBeginOfThreadMatrixC(index_t thread_id) const
Chao Liu's avatar
Chao Liu committed
339
    {
Chao Liu's avatar
Chao Liu committed
340
        constexpr index_t BatchThreadWork = BatchSize / BatchPerThread;
Chao Liu's avatar
Chao Liu committed
341

Chao Liu's avatar
Chao Liu committed
342
        constexpr index_t ThreadPerLevel1Cluster =
Chao Liu's avatar
Chao Liu committed
343
344
            MLevel0Cluster * NLevel0Cluster * MLevel1Cluster * NLevel1Cluster;

Chao Liu's avatar
Chao Liu committed
345
        constexpr index_t ThreadPerLevel0Cluster = MLevel0Cluster * NLevel0Cluster;
Chao Liu's avatar
Chao Liu committed
346

Chao Liu's avatar
Chao Liu committed
347
348
        index_t batch_work_id = thread_id / ThreadPerLevel1Cluster;
        index_t cluster_id    = thread_id - batch_work_id * ThreadPerLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
349

Chao Liu's avatar
Chao Liu committed
350
351
352
        index_t level1_id   = cluster_id / ThreadPerLevel0Cluster;
        index_t level1_m_id = level1_id / NLevel1Cluster;
        index_t level1_n_id = level1_id % NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
353

Chao Liu's avatar
Chao Liu committed
354
355
356
        index_t level0_id   = cluster_id % ThreadPerLevel0Cluster;
        index_t level0_m_id = level0_id / NLevel0Cluster;
        index_t level0_n_id = level0_id % NLevel0Cluster;
Chao Liu's avatar
Chao Liu committed
357

Chao Liu's avatar
Chao Liu committed
358
359
        constexpr index_t MPerLevel0Cluster = MPerThreadSubC * MLevel0Cluster;
        constexpr index_t NPerLevel0Cluster = NPerThreadSubC * NLevel0Cluster;
Chao Liu's avatar
Chao Liu committed
360
361
362
363
364
365
366
367

        return MatrixIndex{batch_work_id * BatchPerThread,
                           level1_m_id * MPerLevel0Cluster + level0_m_id * MPerThreadSubC,
                           level1_n_id * NPerLevel0Cluster + level0_n_id * NPerThreadSubC};
    }

    // this should be optimized away if input is known
    __device__ static MatrixIndex
Chao Liu's avatar
Chao Liu committed
368
    GetDistanceFromBeginOfThreadMatrixC(index_t batch_in_c, index_t m_in_c, index_t n_in_c)
Chao Liu's avatar
Chao Liu committed
369
370
371
    {
        constexpr auto c_thread_mtx = ThreadMatrixC{};

Chao Liu's avatar
Chao Liu committed
372
373
        constexpr index_t MPerThread = c_thread_mtx.NRow();
        constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
374

Chao Liu's avatar
Chao Liu committed
375
376
        constexpr index_t MRepeat = MPerThread / MPerThreadSubC;
        constexpr index_t NRepeat = NPerThread / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
377

Chao Liu's avatar
Chao Liu committed
378
379
        constexpr index_t MPerLevel1Cluster = MPerThreadSubC * MLevel0Cluster * MLevel1Cluster;
        constexpr index_t NPerLevel1Cluster = NPerThreadSubC * NLevel0Cluster * NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
380

Chao Liu's avatar
Chao Liu committed
381
382
        index_t m_repeat = m_in_c / MPerThreadSubC;
        index_t n_repeat = n_in_c / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
383

Chao Liu's avatar
Chao Liu committed
384
385
        index_t m_in_sub_c = m_in_c % MPerThreadSubC;
        index_t n_in_sub_c = n_in_c % NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

        return MatrixIndex{batch_in_c,
                           m_repeat * MPerLevel1Cluster + m_in_sub_c,
                           n_repeat * NPerLevel1Cluster + n_in_sub_c};
    }

    template <class FloatA, class FloatB, class FloatC, class Accumulator>
    __device__ void Run(const FloatA* __restrict__ p_a_block,
                        const FloatB* __restrict__ p_b_block,
                        FloatC* __restrict__ p_c_thread,
                        Accumulator f_accum) const
    {
        constexpr auto True  = integral_constant<bool, true>{};
        constexpr auto False = integral_constant<bool, false>{};

        constexpr auto a_block_mtx  = BlockMatrixA{};
        constexpr auto b_block_mtx  = BlockMatrixB{};
        constexpr auto c_thread_mtx = ThreadMatrixC{};

Chao Liu's avatar
Chao Liu committed
405
        constexpr index_t KPerBlock = a_block_mtx.NRow(); // A is transposed
Chao Liu's avatar
Chao Liu committed
406

Chao Liu's avatar
Chao Liu committed
407
408
        constexpr index_t MPerThread = c_thread_mtx.NRow();
        constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

        // thread A, B for GEMM
        //   A is transposed, b is not
        constexpr auto a_thread_mtx =
            make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<MPerThread>{});

        constexpr auto b_thread_mtx =
            make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<NPerThread>{});

        // thread A-sub, B-sub for copy
        constexpr auto a_thread_sub_mtx = make_ConstantMatrixDescriptor(
            Number<KPerThreadLoop>{}, Number<MPerThreadSubC>{}, Number<MPerThread>{});

        constexpr auto b_thread_sub_mtx = make_ConstantMatrixDescriptor(
            Number<KPerThreadLoop>{}, Number<NPerThreadSubC>{}, Number<NPerThread>{});

        FloatA p_a_thread[a_thread_mtx.GetElementSpace()];
        FloatB p_b_thread[b_thread_mtx.GetElementSpace()];

Chao Liu's avatar
Chao Liu committed
428
429
        constexpr index_t MPerLevel1Cluster = MPerThreadSubC * MLevel0Cluster * MLevel1Cluster;
        constexpr index_t NPerLevel1Cluster = NPerThreadSubC * NLevel0Cluster * NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
430

Chao Liu's avatar
Chao Liu committed
431
432
        constexpr index_t MRepeat = MPerThread / MPerThreadSubC;
        constexpr index_t NRepeat = NPerThread / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
433
434
435

// loop over k
#pragma unroll
Chao Liu's avatar
Chao Liu committed
436
        for(index_t k_begin = 0; k_begin < KPerBlock; k_begin += KPerThreadLoop)
Chao Liu's avatar
Chao Liu committed
437
438
439
440
        {
// read first batch of A, B
//   copy A-sub to form A
#pragma unroll
Chao Liu's avatar
Chao Liu committed
441
            for(index_t m_repeat = 0; m_repeat < MRepeat; ++m_repeat)
Chao Liu's avatar
Chao Liu committed
442
443
444
445
446
447
448
449
450
451
452
453
            {
                threadwise_matrix_copy(
                    a_block_mtx,
                    p_a_block + a_block_mtx.Get1dIndex(k_begin, m_repeat * MPerLevel1Cluster) +
                        mMyThreadOffsetA,
                    a_thread_mtx,
                    p_a_thread + a_thread_mtx.Get1dIndex(0, m_repeat * MPerThreadSubC),
                    a_thread_sub_mtx.GetLengths());
            }

//   copy B-sub to form B
#pragma unroll
Chao Liu's avatar
Chao Liu committed
454
            for(index_t n_repeat = 0; n_repeat < NRepeat; ++n_repeat)
Chao Liu's avatar
Chao Liu committed
455
456
457
458
459
460
461
462
463
464
465
466
            {
                threadwise_matrix_copy(
                    b_block_mtx,
                    p_b_block + b_block_mtx.Get1dIndex(k_begin, n_repeat * NPerLevel1Cluster) +
                        mMyThreadOffsetB,
                    b_thread_mtx,
                    p_b_thread + b_thread_mtx.Get1dIndex(0, n_repeat * NPerThreadSubC),
                    b_thread_sub_mtx.GetLengths());
            }

// loop over batch
#pragma unroll
Chao Liu's avatar
Chao Liu committed
467
            for(index_t ib = 0; ib + 1 < BatchPerThread; ++ib)
Chao Liu's avatar
Chao Liu committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
            {
                // do current batch of gemm
                threadwise_gemm(a_thread_mtx,
                                True,
                                p_a_thread,
                                b_thread_mtx,
                                False,
                                p_b_thread,
                                c_thread_mtx,
                                False,
                                p_c_thread + ib * ThreadMatrixStrideC,
                                f_accum);

                // read next batch of a, b
                if(BlockMatrixStrideA != 0)
                {
#pragma unroll
Chao Liu's avatar
Chao Liu committed
485
                    for(index_t m_repeat = 0; m_repeat < MRepeat; ++m_repeat)
Chao Liu's avatar
Chao Liu committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
                    {
                        threadwise_matrix_copy(
                            a_block_mtx,
                            p_a_block +
                                a_block_mtx.Get1dIndex(k_begin, m_repeat * MPerLevel1Cluster) +
                                (ib + 1) * BlockMatrixStrideA + mMyThreadOffsetA,
                            a_thread_mtx,
                            p_a_thread + a_thread_mtx.Get1dIndex(0, m_repeat * MPerThreadSubC),
                            a_thread_sub_mtx.GetLengths());
                    }
                }

                if(BlockMatrixStrideB != 0)
                {
#pragma unroll
Chao Liu's avatar
Chao Liu committed
501
                    for(index_t n_repeat = 0; n_repeat < NRepeat; ++n_repeat)
Chao Liu's avatar
Chao Liu committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
                    {
                        threadwise_matrix_copy(
                            b_block_mtx,
                            p_b_block +
                                b_block_mtx.Get1dIndex(k_begin, n_repeat * NPerLevel1Cluster) +
                                (ib + 1) * BlockMatrixStrideB + mMyThreadOffsetB,
                            b_thread_mtx,
                            p_b_thread + b_thread_mtx.Get1dIndex(0, n_repeat * NPerThreadSubC),
                            b_thread_sub_mtx.GetLengths());
                    }
                }
            }

            // do last batch of gemm
            threadwise_gemm(a_thread_mtx,
                            True,
                            p_a_thread,
                            b_thread_mtx,
                            False,
                            p_b_thread,
                            c_thread_mtx,
                            False,
                            p_c_thread + (BatchPerThread - 1) * ThreadMatrixStrideC,
                            f_accum);
        }
    }

    template <class FloatA, class FloatB, class FloatC, class Accumulator>
    __device__ void Run_v3(const FloatA* __restrict__ p_a_block,
                           const FloatB* __restrict__ p_b_block,
                           FloatC* __restrict__ p_c_thread,
                           Accumulator f_accum) const
    {
        constexpr auto True  = integral_constant<bool, true>{};
        constexpr auto False = integral_constant<bool, false>{};

        constexpr auto a_block_mtx  = BlockMatrixA{};
        constexpr auto b_block_mtx  = BlockMatrixB{};
        constexpr auto c_thread_mtx = ThreadMatrixC{};

Chao Liu's avatar
Chao Liu committed
542
        constexpr index_t KPerBlock = a_block_mtx.NRow(); // A is transposed
Chao Liu's avatar
Chao Liu committed
543

Chao Liu's avatar
Chao Liu committed
544
545
        constexpr index_t MPerThread = c_thread_mtx.NRow();
        constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

        // thread A, B for GEMM
        //   A is transposed, b is not
        constexpr auto a_thread_mtx =
            make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<MPerThread>{});

        constexpr auto b_thread_mtx =
            make_ConstantMatrixDescriptor(Number<KPerThreadLoop>{}, Number<NPerThread>{});

        // thread A-sub, B-sub for copy
        constexpr auto a_thread_sub_mtx = make_ConstantMatrixDescriptor(
            Number<KPerThreadLoop>{}, Number<MPerThreadSubC>{}, Number<MPerThread>{});

        constexpr auto b_thread_sub_mtx = make_ConstantMatrixDescriptor(
            Number<KPerThreadLoop>{}, Number<NPerThreadSubC>{}, Number<NPerThread>{});

        FloatA p_a_thread[a_thread_mtx.GetElementSpace()];
        FloatB p_b_thread[b_thread_mtx.GetElementSpace()];

Chao Liu's avatar
Chao Liu committed
565
566
        constexpr index_t MPerLevel1Cluster = MPerThreadSubC * MLevel0Cluster * MLevel1Cluster;
        constexpr index_t NPerLevel1Cluster = NPerThreadSubC * NLevel0Cluster * NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
567

Chao Liu's avatar
Chao Liu committed
568
569
        constexpr index_t MRepeat = MPerThread / MPerThreadSubC;
        constexpr index_t NRepeat = NPerThread / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
570
571
572

        // loop over k
        //#pragma unroll
Chao Liu's avatar
Chao Liu committed
573
        for(index_t k_begin = 0; k_begin < KPerBlock; k_begin += KPerThreadLoop)
Chao Liu's avatar
Chao Liu committed
574
575
576
577
        {
            // read first batch of A, B
            //   copy A-sub to form A
            //#pragma unroll
Chao Liu's avatar
Chao Liu committed
578
            for(index_t m_repeat = 0; m_repeat < MRepeat; ++m_repeat)
Chao Liu's avatar
Chao Liu committed
579
            {
Chao Liu's avatar
Chao Liu committed
580
                for(index_t i = 0; i < a_thread_sub_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
581
582
                {
#if 1
Chao Liu's avatar
Chao Liu committed
583
                    for(index_t j = 0; j < a_thread_sub_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
                    {
                        p_a_thread[a_thread_mtx.Get1dIndex(i, m_repeat * MPerThreadSubC + j)] =
                            p_a_block[a_block_mtx.Get1dIndex(k_begin + i,
                                                             m_repeat * MPerLevel1Cluster + j) +
                                      mMyThreadOffsetA];
                    }
#else
                    static_assert(a_thread_sub_mtx.NCol() == 4, "asm only read 4xfp32");

#endif
                }
            }

            //   copy B-sub to form B
            //#pragma unroll
Chao Liu's avatar
Chao Liu committed
599
            for(index_t n_repeat = 0; n_repeat < NRepeat; ++n_repeat)
Chao Liu's avatar
Chao Liu committed
600
            {
Chao Liu's avatar
Chao Liu committed
601
                for(index_t i = 0; i < b_thread_sub_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
602
                {
Chao Liu's avatar
Chao Liu committed
603
                    for(index_t j = 0; j < b_thread_sub_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
604
605
606
607
608
609
610
611
612
613
614
                    {
                        p_b_thread[b_thread_mtx.Get1dIndex(i, n_repeat * NPerThreadSubC + j)] =
                            p_b_block[b_block_mtx.Get1dIndex(k_begin + i,
                                                             n_repeat * MPerLevel1Cluster + j) +
                                      mMyThreadOffsetB];
                    }
                }
            }

            // loop over batch
            //#pragma unroll
Chao Liu's avatar
Chao Liu committed
615
            for(index_t ib = 0; ib + 1 < BatchPerThread; ++ib)
Chao Liu's avatar
Chao Liu committed
616
617
            {
                // do current batch of gemm
Chao Liu's avatar
Chao Liu committed
618
                for(index_t k = 0; k < a_thread_mtx.NRow(); ++k)
Chao Liu's avatar
Chao Liu committed
619
620
                {
#if 0
Chao Liu's avatar
Chao Liu committed
621
                    for(index_t i = 0; i < c_thread_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
622
                    {
Chao Liu's avatar
Chao Liu committed
623
                        for(index_t j = 0; j < c_thread_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
624
                        {
Chao Liu's avatar
Chao Liu committed
625
                            const index_t aindex =
Chao Liu's avatar
Chao Liu committed
626
                                a_thread_mtx.Get1dIndex(k, i); // A is transposed
Chao Liu's avatar
Chao Liu committed
627
628
                            const index_t bindex = b_thread_mtx.Get1dIndex(k, j);
                            const index_t cindex =
Chao Liu's avatar
Chao Liu committed
629
630
631
632
633
634
635
636
637
                                c_thread_mtx.Get1dIndex(i, j) + ib * ThreadMatrixStrideC;

                            f_accum(p_c_thread[cindex], p_a_thread[aindex] * p_b_thread[bindex]);
                        }
                    }
#elif 1
                    static_assert(c_thread_mtx.NRow() == 16 && c_thread_mtx.NCol() == 4,
                                  "asm is only for 16x4");

Chao Liu's avatar
Chao Liu committed
638
639
                    const index_t bindex = b_thread_mtx.Get1dIndex(k, 0);
                    for(index_t i = 0; i < c_thread_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
640
                    {
Chao Liu's avatar
Chao Liu committed
641
642
                        const index_t aindex = a_thread_mtx.Get1dIndex(k, i); // A is transposed
                        const index_t cindex = c_thread_mtx.Get1dIndex(i, 0);
Chao Liu's avatar
Chao Liu committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670

                        asm volatile("\n \
                            v_mac_f32 %0, %4, %5 \n \
                            v_mac_f32 %1, %4, %6 \n \
                            v_mac_f32 %2, %4, %7 \n \
                            v_mac_f32 %3, %4, %8 \n \
                            "
                                     : "=v"(p_c_thread[cindex + 0]),
                                       "=v"(p_c_thread[cindex + 1]),
                                       "=v"(p_c_thread[cindex + 2]),
                                       "=v"(p_c_thread[cindex + 3])
                                     : "v"(p_a_thread[aindex]),
                                       "v"(p_b_thread[bindex + 0]),
                                       "v"(p_b_thread[bindex + 1]),
                                       "v"(p_b_thread[bindex + 2]),
                                       "v"(p_b_thread[bindex + 3]),
                                       "0"(p_c_thread[cindex + 0]),
                                       "1"(p_c_thread[cindex + 1]),
                                       "2"(p_c_thread[cindex + 2]),
                                       "3"(p_c_thread[cindex + 3]));
                    }
#endif
                }

                // read next batch of a, b
                if(BlockMatrixStrideA != 0)
                {
                    //#pragma unroll
Chao Liu's avatar
Chao Liu committed
671
                    for(index_t m_repeat = 0; m_repeat < MRepeat; ++m_repeat)
Chao Liu's avatar
Chao Liu committed
672
                    {
Chao Liu's avatar
Chao Liu committed
673
                        for(index_t i = 0; i < a_thread_sub_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
674
                        {
Chao Liu's avatar
Chao Liu committed
675
                            for(index_t j = 0; j < a_thread_sub_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
                            {
                                p_a_thread[a_thread_mtx.Get1dIndex(i,
                                                                   m_repeat * MPerThreadSubC + j)] =
                                    p_a_block[a_block_mtx.Get1dIndex(
                                                  k_begin + i, m_repeat * MPerLevel1Cluster + j) +
                                              (ib + 1) * BlockMatrixStrideA + mMyThreadOffsetA];
                            }
                        }
                    }
                }

                if(BlockMatrixStrideB != 0)
                {
                    //#pragma unroll
Chao Liu's avatar
Chao Liu committed
690
                    for(index_t n_repeat = 0; n_repeat < NRepeat; ++n_repeat)
Chao Liu's avatar
Chao Liu committed
691
                    {
Chao Liu's avatar
Chao Liu committed
692
                        for(index_t i = 0; i < b_thread_sub_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
693
                        {
Chao Liu's avatar
Chao Liu committed
694
                            for(index_t j = 0; j < b_thread_sub_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
695
696
697
698
699
700
701
702
703
704
705
706
707
                            {
                                p_b_thread[b_thread_mtx.Get1dIndex(i,
                                                                   n_repeat * NPerThreadSubC + j)] =
                                    p_b_block[b_block_mtx.Get1dIndex(
                                                  k_begin + i, n_repeat * MPerLevel1Cluster + j) +
                                              (ib + 1) * BlockMatrixStrideB + mMyThreadOffsetB];
                            }
                        }
                    }
                }
            }

            // do last batch of gemm
Chao Liu's avatar
Chao Liu committed
708
            for(index_t k = 0; k < a_thread_mtx.NRow(); ++k)
Chao Liu's avatar
Chao Liu committed
709
710
            {
#if 0
Chao Liu's avatar
Chao Liu committed
711
                for(index_t i = 0; i < c_thread_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
712
                {
Chao Liu's avatar
Chao Liu committed
713
                    for(index_t j = 0; j < c_thread_mtx.NCol(); ++j)
Chao Liu's avatar
Chao Liu committed
714
                    {
Chao Liu's avatar
Chao Liu committed
715
716
717
                        const index_t aindex = a_thread_mtx.Get1dIndex(k, i); // A is transposed
                        const index_t bindex = b_thread_mtx.Get1dIndex(k, j);
                        const index_t cindex = c_thread_mtx.Get1dIndex(i, j) +
Chao Liu's avatar
Chao Liu committed
718
719
720
721
722
723
724
725
726
                                                (BatchPerThread - 1) * ThreadMatrixStrideC;

                        f_accum(p_c_thread[cindex], p_a_thread[aindex] * p_b_thread[bindex]);
                    }
                }
#elif 1
                static_assert(c_thread_mtx.NRow() == 16 && c_thread_mtx.NCol() == 4,
                              "asm is only for 16x4");

Chao Liu's avatar
Chao Liu committed
727
728
                const index_t bindex = b_thread_mtx.Get1dIndex(k, 0);
                for(index_t i = 0; i < c_thread_mtx.NRow(); ++i)
Chao Liu's avatar
Chao Liu committed
729
                {
Chao Liu's avatar
Chao Liu committed
730
731
                    const index_t aindex = a_thread_mtx.Get1dIndex(k, i); // A is transposed
                    const index_t cindex =
Chao Liu's avatar
Chao Liu committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
                        c_thread_mtx.Get1dIndex(i, 0) + (BatchPerThread - 1) * ThreadMatrixStrideC;

                    asm volatile("\n \
                            v_mac_f32 %0, %4, %5 \n \
                            v_mac_f32 %1, %4, %6 \n \
                            v_mac_f32 %2, %4, %7 \n \
                            v_mac_f32 %3, %4, %8 \n \
                            "
                                 : "=v"(p_c_thread[cindex + 0]),
                                   "=v"(p_c_thread[cindex + 1]),
                                   "=v"(p_c_thread[cindex + 2]),
                                   "=v"(p_c_thread[cindex + 3])
                                 : "v"(p_a_thread[aindex]),
                                   "v"(p_b_thread[bindex + 0]),
                                   "v"(p_b_thread[bindex + 1]),
                                   "v"(p_b_thread[bindex + 2]),
                                   "v"(p_b_thread[bindex + 3]),
                                   "0"(p_c_thread[cindex + 0]),
                                   "1"(p_c_thread[cindex + 1]),
                                   "2"(p_c_thread[cindex + 2]),
                                   "3"(p_c_thread[cindex + 3]));
                }
#endif
            }
        }
    }

Chao Liu's avatar
Chao Liu committed
759
    template <class BlockMatrixC, index_t BlockMatrixStrideC, class FloatC>
Chao Liu's avatar
Chao Liu committed
760
761
762
763
764
765
    __device__ void CopyThreadMatrixCToBlockMatrixC(const FloatC* __restrict__ p_c_thread,
                                                    FloatC* __restrict__ p_c_block) const
    {
        constexpr auto c_block_mtx  = BlockMatrixC{};
        constexpr auto c_thread_mtx = ThreadMatrixC{};

Chao Liu's avatar
Chao Liu committed
766
767
        constexpr index_t MPerThread = c_thread_mtx.NRow();
        constexpr index_t NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
768
769
770
771

        constexpr auto c_thread_sub_mtx = make_ConstantMatrixDescriptor(
            Number<MPerThreadSubC>{}, Number<NPerThreadSubC>{}, Number<NPerThread>{});

Chao Liu's avatar
Chao Liu committed
772
773
        constexpr index_t MPerLevel1Cluster = MPerThreadSubC * MLevel0Cluster * MLevel1Cluster;
        constexpr index_t NPerLevel1Cluster = NPerThreadSubC * NLevel0Cluster * NLevel1Cluster;
Chao Liu's avatar
Chao Liu committed
774

Chao Liu's avatar
Chao Liu committed
775
776
        constexpr index_t MRepeat = MPerThread / MPerThreadSubC;
        constexpr index_t NRepeat = NPerThread / NPerThreadSubC;
Chao Liu's avatar
Chao Liu committed
777
778
779

        const auto c_thread_mtx_begin = GetBeginOfThreadMatrixC(get_thread_local_1d_id());

Chao Liu's avatar
Chao Liu committed
780
        const index_t c_thread_offset =
Chao Liu's avatar
Chao Liu committed
781
782
783
            c_thread_mtx_begin.batch * BlockMatrixStrideC +
            c_block_mtx.Get1dIndex(c_thread_mtx_begin.row, c_thread_mtx_begin.col);

Chao Liu's avatar
Chao Liu committed
784
        for(index_t m_repeat = 0; m_repeat < MRepeat; ++m_repeat)
Chao Liu's avatar
Chao Liu committed
785
        {
Chao Liu's avatar
Chao Liu committed
786
            for(index_t n_repeat = 0; n_repeat < NRepeat; ++n_repeat)
Chao Liu's avatar
Chao Liu committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
            {
                threadwise_matrix_copy(
                    c_thread_sub_mtx,
                    p_c_thread +
                        c_thread_sub_mtx.Get1dIndex(m_repeat * MPerLevel1Cluster,
                                                    n_repeat * NPerLevel1Cluster),
                    c_block_mtx,
                    p_c_block +
                        c_block_mtx.Get1dIndex(m_repeat * MPerLevel1Cluster,
                                               n_repeat * NPerLevel1Cluster) +
                        c_thread_offset,
                    c_thread_sub_mtx.GetLengths());
            }
        }
    }
};