threadwise_direct_convolution.cuh 8.07 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
#pragma once
#include "constant_tensor_descriptor.cuh"

Chao Liu's avatar
Chao Liu committed
4
// optimized for scenario if p_in, p_wei, p_out are in register
Chao Liu's avatar
Chao Liu committed
5
template <class TFloat, class InDesc, class WeiDesc, class OutDesc>
Chao Liu's avatar
Chao Liu committed
6
7
8
9
10
11
__device__ void threadwise_direct_convolution_1(InDesc,
                                                TFloat* const __restrict__ p_in,
                                                WeiDesc,
                                                TFloat* const __restrict__ p_wei,
                                                OutDesc,
                                                TFloat* __restrict__ p_out)
Chao Liu's avatar
Chao Liu committed
12
{
Chao Liu's avatar
Chao Liu committed
13
14
15
16
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};
Chao Liu's avatar
Chao Liu committed
17
18
19
20
21
22

    constexpr auto in_desc  = InDesc{};
    constexpr auto wei_desc = WeiDesc{};
    constexpr auto out_desc = OutDesc{};

#if 0
Chao Liu's avatar
Chao Liu committed
23
    if(blockIdx.x == 0 && threadIdx.x == 0)
Chao Liu's avatar
Chao Liu committed
24
    {
Chao Liu's avatar
Chao Liu committed
25
26
27
        print_ConstantTensorDescriptor(in_desc, "threadwise_direct_convolution: in_desc: ");
        print_ConstantTensorDescriptor(wei_desc, "threadwise_direct_convolution: wei_desc: ");
        print_ConstantTensorDescriptor(out_desc, "threadwise_direct_convolution: out_desc: ");
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
    }
#endif

    for(unsigned n = 0; n < out_desc.GetLength(I0); ++n)
    {
        for(unsigned k = 0; k < out_desc.GetLength(I1); ++k)
        {
            for(unsigned ho = 0; ho < out_desc.GetLength(I2); ++ho)
            {
                for(unsigned wo = 0; wo < out_desc.GetLength(I3); ++wo)
                {
                    for(unsigned c = 0; c < wei_desc.GetLength(I1); ++c)
                    {
                        for(unsigned s = 0; s < wei_desc.GetLength(I2); ++s)
                        {
                            for(unsigned r = 0; r < wei_desc.GetLength(I3); ++r)
                            {
                                const unsigned hi = ho + s;
                                const unsigned wi = wo + r;

48
                                const unsigned in_index = in_desc.Get1dIndex(n, c, hi, wi);
Chao Liu's avatar
Chao Liu committed
49

50
                                const unsigned wei_index = wei_desc.Get1dIndex(k, c, s, r);
Chao Liu's avatar
Chao Liu committed
51

52
                                const unsigned out_index = out_desc.Get1dIndex(n, k, ho, wo);
Chao Liu's avatar
Chao Liu committed
53
54
55
56

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

#if 0
Chao Liu's avatar
Chao Liu committed
57
                                //   if(threadIdx.x == 0)
Chao Liu's avatar
Chao Liu committed
58
                                {
Chao Liu's avatar
Chao Liu committed
59
                                    printf("threadwise_direct_convolution: \t"
Chao Liu's avatar
Chao Liu committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                                           "threadIdx.x %u\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",
                                           threadIdx.x,
                                           out_index,
                                           p_out[out_index],
                                           wei_index,
                                           p_wei[wei_index],
                                           in_index,
                                           p_in[in_index]);
                                }
#endif
                            }
                        }
                    }
                }
            }
        }
    }
}
Chao Liu's avatar
Chao Liu committed
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

// optimized for scenario where p_in and p_wei are in LDS, p_out is in register
// break down a non-1x1 convolution into a sequence of 1x1 convolutions,
// load 1x1 weight into register, and do 1x1 convolution in register.
template <class TFloat, class InDesc, class WeiDesc, class OutDesc>
__device__ void threadwise_direct_convolution_2(InDesc,
                                                TFloat* const __restrict__ p_in,
                                                WeiDesc,
                                                TFloat* const __restrict__ p_wei,
                                                OutDesc,
                                                TFloat* __restrict__ p_out)
{
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

    constexpr auto in_desc_lds  = InDesc{};
    constexpr auto wei_desc_lds = WeiDesc{};
    constexpr auto out_desc_reg = OutDesc{};

    constexpr auto in_desc_reg =
        make_ConstantTensorDescriptor(Sequence<in_desc_lds.GetLength(I0),
                                               in_desc_lds.GetLength(I1),
                                               out_desc_reg.GetLength(I2),
                                               out_desc_reg.GetLength(I3)>{});

    constexpr auto wei_desc_reg = make_ConstantTensorDescriptor(
        Sequence<wei_desc_lds.GetLength(I0), wei_desc_lds.GetLength(I1), 1, 1>{});

    TFloat p_in_reg[in_desc_reg.GetElementSpace()];
    TFloat p_wei_reg[wei_desc_reg.GetElementSpace()];

    constexpr unsigned in_w_new_read = 1;

    constexpr auto in_desc_reg_new_read =
        make_ConstantTensorDescriptor(Sequence<in_desc_reg.GetLength(I0),
                                               in_desc_reg.GetLength(I1),
                                               in_desc_reg.GetLength(I2),
                                               in_w_new_read>{});

    // loop over vertical direction
    for(unsigned s = 0; s < wei_desc_lds.GetLength(I2); ++s)
    {
#if 1
        // read first input
        threadwise_4d_tensor_copy(in_desc_lds,
                                  p_in + in_desc_lds.Get1dIndex(0, 0, s, 0),
                                  in_desc_reg,
                                  p_in_reg,
                                  in_desc_reg);

        // read first 1x1 weight
        threadwise_4d_tensor_copy(wei_desc_lds,
                                  p_wei + wei_desc_lds.Get1dIndex(0, 0, s, 0),
                                  wei_desc_reg,
                                  p_wei_reg,
                                  wei_desc_reg);

        // do first 1x1 conv
        threadwise_direct_convolution_1(
            in_desc_reg, p_in_reg, wei_desc_reg, p_wei_reg, out_desc_reg, p_out);

        // loop over horizontal direction
        for(unsigned r = 1; r < wei_desc_lds.GetLength(I3); ++r)
        {
            // read new weight
            threadwise_4d_tensor_copy(wei_desc_lds,
                                      p_wei + wei_desc_lds.Get1dIndex(0, 0, s, r),
                                      wei_desc_reg,
                                      p_wei_reg,
                                      wei_desc_reg);

            // shift old input to the left
            threadwise_4d_tensor_shift_down(in_desc_reg, p_in_reg, I3, Number<in_w_new_read>{});

            // read new input
            threadwise_4d_tensor_copy(
                in_desc_lds,
                p_in + in_desc_lds.Get1dIndex(0, 0, s, in_desc_reg.GetLength(I3) + r - 1),
                in_desc_reg,
                p_in_reg +
                    in_desc_reg.Get1dIndex(0, 0, 0, in_desc_reg.GetLength(I3) - in_w_new_read),
                in_desc_reg_new_read);

            // do 1x1 conv
            threadwise_direct_convolution_1(
                in_desc_reg, p_in_reg, wei_desc_reg, p_wei_reg, out_desc_reg, p_out);
        }
#elif 1
        // loop over horizontal direction
        for(unsigned r = 0; r < wei_desc_lds.GetLength(I3); ++r)
        {
            // read new weight
            threadwise_4d_tensor_copy(wei_desc_lds,
                                      p_wei + wei_desc_lds.Get1dIndex(0, 0, s, r),
                                      wei_desc_reg,
                                      p_wei_reg,
                                      wei_desc_reg);

            // read new input
            threadwise_4d_tensor_copy(in_desc_lds,
                                      p_in + in_desc_lds.Get1dIndex(0, 0, s, r),
                                      in_desc_reg,
                                      p_in_reg,
                                      in_desc_reg);

            // do 1x1 conv
            threadwise_direct_convolution_1(
                in_desc_reg, p_in_reg, wei_desc_reg, p_wei_reg, out_desc_reg, p_out);
        }
#endif
    }
}