direct_convolution.cuh 19.5 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
#pragma once
#include "device_tensor.cuh"

Chao Liu's avatar
Chao Liu committed
4
5
6
7
8
9
10
11
12
13
14
template <class TFloat,
          unsigned NWorkLen0,
          unsigned NWorkLen1,
          unsigned NWorkLen2,
          unsigned NWorkLen3,
          class F>
__device__ void blockwise_4d_tensor_op(const DeviceTensorDescriptor<4>& src_desc,
                                       TFloat* const __restrict__ p_src,
                                       const DeviceTensorDescriptor<4>& dst_desc,
                                       TFloat* __restrict__ p_dst,
                                       F f)
Chao Liu's avatar
Chao Liu committed
15
{
Chao Liu's avatar
Chao Liu committed
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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#if 1
    if(threadIdx.x < 100)
    {
        printf("====== blockwise_4d_tensor_op: \t"
               "threadIdx.x %u, p_src[threadIdx.x] %f, p_dst[threadIdx.x] %f\n",
                threadIdx.x, p_src[threadIdx.x], p_dst[threadIdx.x]);
    }
#endif

    constexpr unsigned NWorkStride3 = 1;
    constexpr unsigned NWorkStride2 = NWorkLen3 * NWorkStride3;
    constexpr unsigned NWorkStride1 = NWorkLen2 * NWorkStride2;
    constexpr unsigned NWorkStride0 = NWorkLen1 * NWorkStride1;

    unsigned itmp =
        threadIdx.x + threadIdx.y * blockDim.x + threadIdx.z * (blockDim.y * blockDim.x);

    const unsigned did0_begin = itmp / NWorkStride0;

    itmp -= did0_begin * NWorkStride0;

    const unsigned did1_begin = itmp / NWorkStride1;

    itmp -= did1_begin * NWorkStride1;

    const unsigned did2_begin = itmp / NWorkStride2;

    itmp -= did2_begin * NWorkStride2;

    const unsigned did3_begin = itmp / NWorkStride3;

    for(unsigned did0 = did0_begin; did0 < src_desc.GetLength(0); did0 += NWorkLen0)
    {
        for(unsigned did1 = did1_begin; did1 < src_desc.GetLength(1); did1 += NWorkLen1)
        {
            for(unsigned did2 = did2_begin; did2 < src_desc.GetLength(2); did2 += NWorkLen2)
            {
                for(unsigned did3 = did3_begin; did3 < src_desc.GetLength(3); did3 += NWorkLen3)
                {
                    const unsigned sindex =
                        src_desc.GetStride(0) * did0 + src_desc.GetStride(1) * did1 +
                        src_desc.GetStride(2) * did2 + src_desc.GetStride(3) * did3;

                    const unsigned dindex =
                        dst_desc.GetStride(0) * did0 + dst_desc.GetStride(1) * did1 +
                        dst_desc.GetStride(2) * did2 + dst_desc.GetStride(3) * did3;

                    f(p_dst[dindex], p_src[sindex]);

#if 1
                    printf("thread id %u, dindex %u, p_dst[dindex] %f, sindex %u, p_src[sindex] %f\n",
                            threadIdx.x, dindex, p_dst[dindex], sindex, p_src[sindex]);
#endif

                }
            }
        }
    }
}

template <class TFloat, class F>
__device__ void threadwise_4d_tensor_op(const DeviceTensorDescriptor<4>& src_desc,
                                        TFloat* const __restrict__ p_src,
                                        const DeviceTensorDescriptor<4>& dst_desc,
                                        TFloat* __restrict__ p_dst,
                                        F f)
{
    for(unsigned did0 = 0; did0 < src_desc.GetLength(0); ++did0)
    {
        for(unsigned did1 = 0; did1 < src_desc.GetLength(1); ++did1)
        {
            for(unsigned did2 = 0; did2 < src_desc.GetLength(2); ++did2)
            {
                for(unsigned did3 = 0; did3 < src_desc.GetLength(3); ++did3)
                {
                    const unsigned sindex =
                        src_desc.GetStride(0) * did0 + src_desc.GetStride(1) * did1 +
                        src_desc.GetStride(2) * did2 + src_desc.GetStride(3) * did3;

                    const unsigned dindex =
                        dst_desc.GetStride(0) * did0 + dst_desc.GetStride(1) * did1 +
                        dst_desc.GetStride(2) * did2 + dst_desc.GetStride(3) * did3;

                    f(p_dst[dindex], p_src[sindex]);
                }
            }
        }
    }
}

template <class TFloat>
__device__ void threadwise_direct_convolution(const DeviceTensorDescriptor<4>& in_desc,
                                              TFloat* const __restrict__ p_in,
                                              const DeviceTensorDescriptor<4>& wei_desc,
                                              TFloat* const __restrict__ p_wei,
                                              const DeviceTensorDescriptor<4>& out_desc,
                                              TFloat* __restrict__ p_out)
{
    for(unsigned n = 0; n < out_desc.GetLength(0); ++n)
    {
        for(unsigned k = 0; k < out_desc.GetLength(1); ++k)
        {
            for(unsigned ho = 0; ho < out_desc.GetLength(2); ++ho)
            {
                for(unsigned wo = 0; wo < out_desc.GetLength(3); ++wo)
                {
                    for(unsigned c = 0; c < wei_desc.GetLength(1); ++c)
                    {
                        for(unsigned s = 0; s < wei_desc.GetLength(2); ++s)
                        {
                            for(unsigned r = 0; r < wei_desc.GetLength(3); ++r)
                            {
                                const unsigned hi = ho + s;
                                const unsigned wi = wo + r;

                                const unsigned in_index =
                                    in_desc.GetStride(0) * n + in_desc.GetStride(1) * c +
                                    in_desc.GetStride(2) * hi + in_desc.GetStride(3) * wi;

                                const unsigned wei_index =
                                    wei_desc.GetStride(0) * k + wei_desc.GetStride(1) * c +
                                    wei_desc.GetStride(2) * s + in_desc.GetStride(3) * r;

                                const unsigned out_index =
                                    out_desc.GetStride(0) * n + out_desc.GetStride(1) * k +
                                    out_desc.GetStride(2) * ho + out_desc.GetStride(3) * wo;

                                p_out[out_index] += p_wei[wei_index] * p_in[in_index];

#if 1
                                if(threadIdx.x == 0 )
                                {
                                    printf("====== 5: \t"
                                           "out_index %u, p_out[out_index] %f, \t"
                                           "wei_index %u, p_wei[wei_index] %f, \t"
                                           "in_index %u, p_in[in_index] %f\n",
                                           out_index, p_out[out_index],
                                           wei_index, p_wei[wei_index],
                                           in_index, p_in[in_index]); 
                                }
#endif
                            }
                        }
                    }
                }
            }
        }
    }


}

template <class TFloat,
          unsigned S, // weight size in H-dir
          unsigned R, // weight size in W-dir
          unsigned InTileSizeH,
          unsigned InTileSizeW,
          unsigned OutTileSizeH,
          unsigned OutTileSizeW,
          unsigned NPerBlock,
          unsigned KPerBlock,
          unsigned YPerBlock,
          unsigned XPerBlock,
          unsigned CPerBlockLoop>
__device__ void blockwise_convolution(const DeviceTensorDescriptor<4>& in_desc,
                                      TFloat* const __restrict__ p_in,
                                      const DeviceTensorDescriptor<4>& wei_desc,
                                      TFloat* const __restrict__ p_wei,
                                      const DeviceTensorDescriptor<4>& out_desc,
                                      TFloat* __restrict__ p_out)
{
    // for now, one thread do 1 N and 1 K
    DeviceTensorDescriptor<4> wei_thread_desc;
    wei_thread_desc.mpLengths[0] = 1;
    wei_thread_desc.mpLengths[1] = CPerBlockLoop;
    wei_thread_desc.mpLengths[2] = S;
    wei_thread_desc.mpLengths[3] = R;
    wei_thread_desc.mpStrides[3] = 1;
    wei_thread_desc.mpStrides[2] = wei_thread_desc.GetLength(3) * wei_thread_desc.GetStride(3);
    wei_thread_desc.mpStrides[1] = wei_thread_desc.GetLength(2) * wei_thread_desc.GetStride(2);
    wei_thread_desc.mpStrides[0] = wei_thread_desc.GetLength(1) * wei_thread_desc.GetStride(1);

    DeviceTensorDescriptor<4> out_thread_desc;
    out_thread_desc.mpLengths[0] = 1;
    out_thread_desc.mpLengths[1] = 1;
    out_thread_desc.mpLengths[2] = OutTileSizeH;
    out_thread_desc.mpLengths[3] = OutTileSizeW;
    out_thread_desc.mpStrides[3] = 1;
    out_thread_desc.mpStrides[2] = out_thread_desc.GetLength(3) * out_thread_desc.GetStride(3);
    out_thread_desc.mpStrides[1] = out_thread_desc.GetLength(2) * out_thread_desc.GetStride(2);
    out_thread_desc.mpStrides[0] = out_thread_desc.GetLength(1) * out_thread_desc.GetStride(1);

    DeviceTensorDescriptor<4> in_thread_desc;
    in_thread_desc.mpLengths[0] = 1;
    in_thread_desc.mpLengths[1] = CPerBlockLoop;
    in_thread_desc.mpLengths[2] = OutTileSizeH + S - 1;
    in_thread_desc.mpLengths[3] = OutTileSizeW + R - 1;
    in_thread_desc.mpStrides[3] = 1;
    in_thread_desc.mpStrides[2] = in_thread_desc.GetLength(3) * in_thread_desc.GetStride(3);
    in_thread_desc.mpStrides[1] = in_thread_desc.GetLength(2) * in_thread_desc.GetStride(2);
    in_thread_desc.mpStrides[0] = in_thread_desc.GetLength(1) * in_thread_desc.GetStride(1);

    const unsigned thread_sz = blockDim.x * blockDim.y * blockDim.z;

    const unsigned thread_id =
        threadIdx.x + threadIdx.y * blockDim.x + threadIdx.z * (blockDim.y * blockDim.x);

    for(unsigned thread_work_id = thread_id;
        thread_work_id < NPerBlock * KPerBlock * YPerBlock * XPerBlock;
        thread_work_id += thread_sz)
    {
        unsigned itmp             = thread_work_id;
        unsigned n_thread_work_id = itmp / (KPerBlock * YPerBlock * XPerBlock);
        itmp -= n_thread_work_id * (KPerBlock * YPerBlock * XPerBlock);
        unsigned k_thread_work_id = itmp / (YPerBlock * XPerBlock);
        itmp -= k_thread_work_id * (YPerBlock * XPerBlock);
        unsigned y_thread_work_id = itmp / XPerBlock;
        unsigned x_thread_work_id = itmp - y_thread_work_id * XPerBlock;

        unsigned n_thread_work_begin  = n_thread_work_id * 1;
        unsigned k_thread_work_begin  = k_thread_work_id * 1;
        unsigned ho_thread_work_begin = y_thread_work_id * OutTileSizeH;
        unsigned wo_thread_work_begin = x_thread_work_id * OutTileSizeW;

        unsigned hi_thread_work_begin = ho_thread_work_begin; // minus padding
        unsigned wi_thread_work_begin = wo_thread_work_begin; // minus padding

        TFloat p_in_thread[1 * CPerBlockLoop * InTileSizeH * InTileSizeW];
        TFloat p_wei_thread[1 * CPerBlockLoop * S * R];
        TFloat p_out_thread[1 * 1 * OutTileSizeH * OutTileSizeW];

        auto f_copy = [](const TFloat& src, TFloat& dst) { dst = src; };

        // copy input tensor into register
        threadwise_4d_tensor_op<TFloat, decltype(f_copy)>(
            in_desc,
            p_in + in_desc.Get1dIndex(
                       n_thread_work_begin, 0, hi_thread_work_begin, wi_thread_work_begin),
            in_thread_desc,
            p_in_thread,
            f_copy);

        // copy weight tensor into register
        threadwise_4d_tensor_op<TFloat, decltype(f_copy)>(
            wei_desc,
            p_wei + wei_thread_desc.Get1dIndex(k_thread_work_begin, 0, 0, 0),
            wei_thread_desc,
            p_wei_thread,
            f_copy);

        // copy output tensor into register
        threadwise_4d_tensor_op<TFloat, decltype(f_copy)>(
            out_desc,
            p_out + out_desc.Get1dIndex(n_thread_work_begin,
                                        k_thread_work_begin,
                                        ho_thread_work_begin,
                                        wo_thread_work_begin),
            out_thread_desc,
            p_out_thread,
            f_copy);

        // threadwise convolution
        threadwise_direct_convolution(in_thread_desc,
                                      p_in_thread,
                                      wei_thread_desc,
                                      p_wei_thread,
                                      out_thread_desc,
                                      p_out_thread);

        // accumulate output tensor into device mem
        threadwise_4d_tensor_op<TFloat, decltype(f_copy)>(
            out_thread_desc,
            p_out_thread,
            out_desc,
            p_out + out_desc.Get1dIndex(n_thread_work_begin,
                                        k_thread_work_begin,
                                        ho_thread_work_begin,
                                        wo_thread_work_begin),
            f_copy);
    }
}

template <class TFloat,
          unsigned S, // weight size in H-dir
          unsigned R, // weight size in W-dir
          unsigned InTileSizeH,
          unsigned InTileSizeW,
          unsigned OutTileSizeH,
          unsigned OutTileSizeW,
          unsigned NPerBlock,
          unsigned KPerBlock,
          unsigned YPerBlock,
          unsigned XPerBlock,
          unsigned CPerBlockLoop>
__global__ void gridwise_convolution(const DeviceTensorDescriptor<4> in_desc,
                                     TFloat* const __restrict__ p_in,
                                     const DeviceTensorDescriptor<4> wei_desc,
                                     TFloat* const __restrict__ p_wei,
                                     const DeviceTensorDescriptor<4> out_desc,
                                     TFloat* __restrict__ p_out)
{
#if 1
    if(threadIdx.x < 100)
    {
        printf("====== 0: \t"
               "threadIdx.x %u, p_in[threadIdx.x] %f, p_wei[threadIdx.x] %f, p_out[threadIdx.x] %f\n",
                threadIdx.x, p_in[threadIdx.x], p_wei[threadIdx.x], p_out[threadIdx.x]); 
    }
#endif

    const unsigned NBlockWork = (in_desc.GetLength(0) + NPerBlock - 1) / NPerBlock;
    const unsigned YBlockWork = (in_desc.GetLength(2) + YPerBlock - 1) / YPerBlock;
    const unsigned XBlockWork = (in_desc.GetLength(3) + XPerBlock - 1) / XPerBlock;

    const unsigned KBlockWork = (wei_desc.GetLength(1) + KPerBlock - 1) / KPerBlock;

    const unsigned block_id =
        blockIdx.x + blockIdx.y * gridDim.x + blockIdx.z * (gridDim.y * gridDim.x);

    // this is ugly
    DeviceTensorDescriptor<4> wei_block_desc;
    wei_block_desc.mpLengths[0] = KPerBlock;
    wei_block_desc.mpLengths[1] = CPerBlockLoop;
    wei_block_desc.mpLengths[2] = S;
    wei_block_desc.mpLengths[3] = R;
    wei_block_desc.mpStrides[3] = 1;
    wei_block_desc.mpStrides[2] = wei_block_desc.GetLength(3) * wei_block_desc.GetStride(3);
    wei_block_desc.mpStrides[1] = wei_block_desc.GetLength(2) * wei_block_desc.GetStride(2);
    wei_block_desc.mpStrides[0] = wei_block_desc.GetLength(1) * wei_block_desc.GetStride(1);

    DeviceTensorDescriptor<4> out_block_desc;
    out_block_desc.mpLengths[0] = NPerBlock;
    out_block_desc.mpLengths[1] = KPerBlock;
    out_block_desc.mpLengths[2] = YPerBlock * OutTileSizeH;
    out_block_desc.mpLengths[3] = XPerBlock * OutTileSizeW;
    out_block_desc.mpStrides[3] = 1;
    out_block_desc.mpStrides[2] = out_block_desc.GetLength(3) * out_block_desc.GetStride(3);
    out_block_desc.mpStrides[1] = out_block_desc.GetLength(2) * out_block_desc.GetStride(2);
    out_block_desc.mpStrides[0] = out_block_desc.GetLength(1) * out_block_desc.GetStride(1);

    DeviceTensorDescriptor<4> in_block_desc;
    in_block_desc.mpLengths[0] = NPerBlock;
    in_block_desc.mpLengths[1] = CPerBlockLoop;
    in_block_desc.mpLengths[2] = YPerBlock * OutTileSizeH + S - 1;
    in_block_desc.mpLengths[3] = XPerBlock * OutTileSizeW + R - 1;
    in_block_desc.mpStrides[3] = 1;
    in_block_desc.mpStrides[2] = in_block_desc.GetLength(3) * in_block_desc.GetStride(3);
    in_block_desc.mpStrides[1] = in_block_desc.GetLength(2) * in_block_desc.GetStride(2);
    in_block_desc.mpStrides[0] = in_block_desc.GetLength(1) * in_block_desc.GetStride(1);

    __shared__ TFloat p_in_block[NPerBlock * CPerBlockLoop * S * R];
    __shared__ TFloat p_wei_block[KPerBlock * CPerBlockLoop * (YPerBlock * OutTileSizeH + S - 1) *
                                  (XPerBlock * OutTileSizeW + R - 1)];
    __shared__ TFloat p_out_block[NPerBlock * KPerBlock * (YPerBlock * OutTileSizeH) *
                                  (XPerBlock * OutTileSizeW)];

    unsigned itmp            = block_id;
    unsigned n_block_work_id = itmp / (KBlockWork * YBlockWork * XBlockWork);
    itmp -= n_block_work_id * (KBlockWork * YBlockWork * XBlockWork);
    unsigned k_block_work_id = itmp / (YBlockWork * XBlockWork);
    itmp -= k_block_work_id * (YBlockWork * XBlockWork);
    unsigned y_block_work_id = itmp / XBlockWork;
    unsigned x_block_work_id = itmp - y_block_work_id * XBlockWork;

    unsigned n_block_work_begin = n_block_work_id * NPerBlock;
    unsigned k_block_work_begin = k_block_work_id * KPerBlock;
    unsigned y_block_work_begin = y_block_work_id * YPerBlock;
    unsigned x_block_work_begin = x_block_work_id * XPerBlock;

    unsigned ho_block_work_begin = y_block_work_begin * OutTileSizeH;
    unsigned wo_block_work_begin = x_block_work_begin * OutTileSizeW;

    unsigned hi_block_work_begin = ho_block_work_begin; // minus padding
    unsigned wi_block_work_begin = wo_block_work_begin; // minus padding

    if(threadIdx.x == 0)
        printf("====== 1:\n"); 

    for(unsigned c_block_work_begin = 0; c_block_work_begin < in_desc.GetLength(1);
        c_block_work_begin += CPerBlockLoop)
    {
        auto f_copy = [](const TFloat& src, TFloat& dst) { dst = src; };

        // copy input tensor to LDS
        blockwise_4d_tensor_op<TFloat, 1, 1, 1, 64, decltype(f_copy)>(
            in_desc,
            p_in + in_desc.Get1dIndex(n_block_work_begin,
                                      c_block_work_begin,
                                      hi_block_work_begin,
                                      wi_block_work_begin),
            in_block_desc,
            p_in_block,
            f_copy);

        // copy weight tensor to LDS
        blockwise_4d_tensor_op<TFloat, 1, 1, 1, 64, decltype(f_copy)>(
            wei_desc,
            p_wei + wei_desc.Get1dIndex(k_block_work_begin, c_block_work_begin, 0, 0),
            wei_block_desc,
            p_wei_block,
            f_copy);

        // copy output tensor to LDS
        blockwise_4d_tensor_op<TFloat, 1, 1, 1, 64, decltype(f_copy)>(
            out_desc,
            p_out + out_desc.Get1dIndex(n_block_work_begin,
                                        k_block_work_begin,
                                        ho_block_work_begin,
                                        wo_block_work_begin),
            out_block_desc,
            p_out_block,
            f_copy);

        // blockwise convolution
        blockwise_convolution<TFloat,
                              S,
                              R,
                              InTileSizeH,
                              InTileSizeW,
                              OutTileSizeH,
                              OutTileSizeW,
                              NPerBlock,
                              KPerBlock,
                              YPerBlock,
                              XPerBlock,
                              CPerBlockLoop>(
            in_block_desc, p_in_block, wei_block_desc, p_wei_block, out_block_desc, p_out_block);

        if(threadIdx.x == 0 )
            printf("====== 3:\n"); 

        // accum output tensor from LDS to device mem
        blockwise_4d_tensor_op<TFloat, 1, 1, 1, 64, decltype(f_copy)>(
            out_block_desc,
            p_out_block,
            out_desc,
            p_out + out_desc.Get1dIndex(n_block_work_begin,
                                        k_block_work_begin,
                                        ho_block_work_begin,
                                        wo_block_work_begin),
            f_copy);
    }
Chao Liu's avatar
Chao Liu committed
458
}