constant_tensor_descriptor.cuh 7.09 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#pragma once
2
#include "common.cuh"
Chao Liu's avatar
Chao Liu committed
3
4
5
6

template <class T, T N>
struct Constant
{
Chao Liu's avatar
Chao Liu committed
7
    static const T mValue = N;
Chao Liu's avatar
Chao Liu committed
8
9
};

Chao Liu's avatar
Chao Liu committed
10
11
template <unsigned N>
using Number = Constant<unsigned, N>;
Chao Liu's avatar
Chao Liu committed
12
13
14
15
16
17
18
19
20

template <unsigned... Is>
struct Sequence
{
    static constexpr unsigned nDim = sizeof...(Is);

    const unsigned mData[nDim] = {Is...};

    template <unsigned I>
Chao Liu's avatar
Chao Liu committed
21
    __host__ __device__ constexpr unsigned Get(Number<I>) const
Chao Liu's avatar
Chao Liu committed
22
23
24
    {
        return mData[I];
    }
25
26

    template <unsigned I>
Chao Liu's avatar
Chao Liu committed
27
    __host__ __device__ constexpr auto GetConstant(Number<I>) const
28
29
30
31
32
    {
        constexpr unsigned N = Get(I);

        return Number<N>{};
    }
Chao Liu's avatar
Chao Liu committed
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

    template <unsigned I0, unsigned I1>
    __host__ __device__ constexpr auto Reorder(Number<I0>, Number<I1>) const
    {
        constexpr unsigned IR0 = Get(Number<I0>{});
        constexpr unsigned IR1 = Get(Number<I1>{});

        return Sequence<IR0, IR1>{};
    }

    template <unsigned I0, unsigned I1, unsigned I2>
    __host__ __device__ constexpr auto Reorder(Number<I0>, Number<I1>, Number<I2>) const
    {
        constexpr unsigned IR0 = Get(Number<I0>{});
        constexpr unsigned IR1 = Get(Number<I1>{});
        constexpr unsigned IR2 = Get(Number<I2>{});

        return Sequence<IR0, IR1, IR2>{};
    }

    template <unsigned I0, unsigned I1, unsigned I2, unsigned I3>
    __host__ __device__ constexpr auto Reorder(Number<I0>, Number<I1>, Number<I2>, Number<I3>) const
    {
        constexpr unsigned IR0 = Get(Number<I0>{});
        constexpr unsigned IR1 = Get(Number<I1>{});
        constexpr unsigned IR2 = Get(Number<I2>{});
        constexpr unsigned IR3 = Get(Number<I3>{});

        return Sequence<IR0, IR1, IR2, IR3>{};
    }

    template <unsigned I0, unsigned I1, unsigned I2, unsigned I3, unsigned I4>
    __host__ __device__ constexpr auto
        Reorder(Number<I0>, Number<I1>, Number<I2>, Number<I3>, Number<I4>) const
    {
        constexpr unsigned IR0 = Get(Number<I0>{});
        constexpr unsigned IR1 = Get(Number<I1>{});
        constexpr unsigned IR2 = Get(Number<I2>{});
        constexpr unsigned IR3 = Get(Number<I3>{});
        constexpr unsigned IR4 = Get(Number<I4>{});

        return Sequence<IR0, IR1, IR2, IR3, IR4>{};
    }
Chao Liu's avatar
Chao Liu committed
76
77
78
79
80
81
};

template <class Lengths, class Strides>
struct ConstantTensorDescriptor
{
    static constexpr unsigned nDim = Lengths::nDim;
Chao Liu's avatar
Chao Liu committed
82
    using NDimConstant             = Number<nDim>;
Chao Liu's avatar
Chao Liu committed
83
84
85
86
87
88
89
90
91
92
93
94
95

    __host__ __device__ constexpr ConstantTensorDescriptor()
    {
        static_assert(Lengths::nDim == Strides::nDim, "nDim not consistent");
    }

    __host__ __device__ constexpr unsigned GetDimension() const { return nDim; }

    __host__ __device__ constexpr Lengths GetLengths() const { return Lengths{}; }

    __host__ __device__ constexpr Strides GetStrides() const { return Strides{}; }

    template <unsigned I>
Chao Liu's avatar
Chao Liu committed
96
    __host__ __device__ constexpr unsigned GetLength(Number<I>) const
Chao Liu's avatar
Chao Liu committed
97
    {
Chao Liu's avatar
Chao Liu committed
98
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
99
100
101
    }

    template <unsigned I>
Chao Liu's avatar
Chao Liu committed
102
    __host__ __device__ constexpr unsigned GetStride(Number<I>) const
Chao Liu's avatar
Chao Liu committed
103
    {
Chao Liu's avatar
Chao Liu committed
104
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
105
106
    }

Chao Liu's avatar
Chao Liu committed
107
108
109
110
111
    // this is ugly, only for 4d
    __host__ __device__ constexpr unsigned GetElementSize() const
    {
        static_assert(nDim == 4, "nDim is not 4");

Chao Liu's avatar
Chao Liu committed
112
113
114
115
        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
116
117
118
119
120
121

        return GetLength(I0) * GetLength(I1) * GetLength(I2) * GetLength(I3);
    }

    // this is ugly, only for 4d
    __host__ __device__ constexpr unsigned GetElementSpace() const
Chao Liu's avatar
Chao Liu committed
122
    {
Chao Liu's avatar
Chao Liu committed
123
124
        static_assert(nDim == 4, "nDim is not 4");

Chao Liu's avatar
Chao Liu committed
125
126
127
128
        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
129
130
131

        return (GetLength(I0) - 1) * GetStride(I0) + (GetLength(I1) - 1) * GetStride(I1) +
               (GetLength(I2) - 1) * GetStride(I2) + (GetLength(I3) - 1) * GetStride(I3) + 1;
Chao Liu's avatar
Chao Liu committed
132
    }
Chao Liu's avatar
Chao Liu committed
133

Chao Liu's avatar
Chao Liu committed
134
135
136
    // this is ugly, only for 4d
    __host__ __device__ unsigned Get1dIndex(unsigned n, unsigned c, unsigned h, unsigned w) const
    {
Chao Liu's avatar
Chao Liu committed
137
138
139
140
        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
141

Chao Liu's avatar
Chao Liu committed
142
        static_assert(nDim == 4, "nDim is not 4");
Chao Liu's avatar
Chao Liu committed
143
144
        return n * GetStride(I0) + c * GetStride(I1) + h * GetStride(I2) + w * GetStride(I3);
    }
Chao Liu's avatar
Chao Liu committed
145
146
147
148
149
150
151
152
153

    template <class... Is>
    __host__ __device__ constexpr auto Reorder(Is... is) const
    {
        constexpr auto lengths = Lengths{}.Reorder(is...);
        constexpr auto strides = Strides{}.Reorder(is...);

        return ConstantTensorDescriptor<decltype(lengths), decltype(strides)>{};
    }
Chao Liu's avatar
Chao Liu committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
};

// this is ugly, only for 4d
template <unsigned N, unsigned C, unsigned H, unsigned W>
__host__ __device__ constexpr auto calculate_default_strides(Sequence<N, C, H, W>)
{
    return Sequence<C * H * W, H * W, W, 1>{};
}

template <class Lengths>
__host__ __device__ constexpr auto make_ConstantTensorDescriptor(Lengths)
{
    using Strides = decltype(calculate_default_strides(Lengths{}));
    return ConstantTensorDescriptor<Lengths, Strides>{};
}

template <class Lengths, class Strides>
__host__ __device__ constexpr auto make_ConstantTensorDescriptor(Lengths, Strides)
{
    return ConstantTensorDescriptor<Lengths, Strides>{};
}

// this is ugly, only for 4d
template <class InDesc, class WeiDesc>
178
__host__ __device__ constexpr auto get_convolution_output_4d_tensor_descriptor(InDesc, WeiDesc)
Chao Liu's avatar
Chao Liu committed
179
180
181
182
{
    constexpr auto in_desc  = InDesc{};
    constexpr auto wei_desc = WeiDesc{};

Chao Liu's avatar
Chao Liu committed
183
184
185
186
    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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

    static_assert(in_desc.GetDimension() == 4, "input nDim is not 4");
    static_assert(wei_desc.GetDimension() == 4, "weight nDim is not 4");
    static_assert(in_desc.GetLength(I1) == wei_desc.GetLength(I1),
                  "input & weight dimension not consistent");

    constexpr auto N  = in_desc.GetLength(I0);
    constexpr auto HI = in_desc.GetLength(I2);
    constexpr auto WI = in_desc.GetLength(I3);

    constexpr auto K = wei_desc.GetLength(I0);
    constexpr auto S = wei_desc.GetLength(I2);
    constexpr auto R = wei_desc.GetLength(I3);

    constexpr auto HO = HI - S + 1;
    constexpr auto WO = WI - R + 1;

    return make_ConstantTensorDescriptor(Sequence<N, K, HO, WO>{});
}

// this is ugly, only for 4d
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
209
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
210
211
212
{
    constexpr auto desc = TDesc{};

Chao Liu's avatar
Chao Liu committed
213
214
215
216
    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
217
218
219

    static_assert(desc.GetDimension() == 4, "dim is not 4");

Chao Liu's avatar
Chao Liu committed
220
221
    printf("%s dim %u, lengths {%u %u %u %u}, strides {%u %u %u %u}\n",
           s,
Chao Liu's avatar
Chao Liu committed
222
223
224
225
226
227
228
229
230
231
           desc.GetDimension(),
           desc.GetLength(I0),
           desc.GetLength(I1),
           desc.GetLength(I2),
           desc.GetLength(I3),
           desc.GetStride(I0),
           desc.GetStride(I1),
           desc.GetStride(I2),
           desc.GetStride(I3));
}