"test/vscode:/vscode.git/clone" did not exist on "ea93079b3038dd156e6168cee1f3bf2defb37f51"
constant_tensor_descriptor.cuh 6 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
27
28
29
30
31
32

    template <unsigned I>
    __host__ __device__ constexpr auto GetNumber(Number<I>) const
    {
        constexpr unsigned N = Get(I);

        return Number<N>{};
    }
Chao Liu's avatar
Chao Liu committed
33
34
35
36
37
38
};

template <class Lengths, class Strides>
struct ConstantTensorDescriptor
{
    static constexpr unsigned nDim = Lengths::nDim;
Chao Liu's avatar
Chao Liu committed
39
    using NDimConstant             = Number<nDim>;
Chao Liu's avatar
Chao Liu committed
40
41
42
43
44
45
46
47
48
49
50
51
52

    __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
53
    __host__ __device__ constexpr unsigned GetLength(Number<I>) const
Chao Liu's avatar
Chao Liu committed
54
    {
Chao Liu's avatar
Chao Liu committed
55
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
56
57
58
    }

    template <unsigned I>
Chao Liu's avatar
Chao Liu committed
59
    __host__ __device__ constexpr unsigned GetStride(Number<I>) const
Chao Liu's avatar
Chao Liu committed
60
    {
Chao Liu's avatar
Chao Liu committed
61
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
62
63
    }

Chao Liu's avatar
Chao Liu committed
64
65
66
67
68
    // 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
69
70
71
72
        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
73
74
75
76
77
78

        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
79
    {
Chao Liu's avatar
Chao Liu committed
80
81
        static_assert(nDim == 4, "nDim is not 4");

Chao Liu's avatar
Chao Liu committed
82
83
84
85
        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
86
87
88

        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
89
    }
Chao Liu's avatar
Chao Liu committed
90

Chao Liu's avatar
Chao Liu committed
91
92
93
    // 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
94
95
96
97
        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
98

Chao Liu's avatar
Chao Liu committed
99
        static_assert(nDim == 4, "nDim is not 4");
Chao Liu's avatar
Chao Liu committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        return n * GetStride(I0) + c * GetStride(I1) + h * GetStride(I2) + w * GetStride(I3);
    }
};

// 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>{};
}

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// this is ugly, only for 4d
template <class Desc, class Reorder>
__host__ __device__ constexpr auto get_reordered_4d_tensor_descriptor(Desc, Reorder)
{
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};

    constexpr auto IT0 = Reorder{}.GetNumber(I0);
    constexpr auto IT1 = Reorder{}.GetNumber(I1);
    constexpr auto IT2 = Reorder{}.GetNumber(I2);
    constexpr auto IT3 = Reorder{}.GetNumber(I3);

    constexpr unsigned L0 = Desc{}.GetLength(IT0);
    constexpr unsigned L1 = Desc{}.GetLength(IT1);
    constexpr unsigned L2 = Desc{}.GetLength(IT2);
    constexpr unsigned L3 = Desc{}.GetLength(IT3);

    return make_ConstantTensorDescriptor(Sequence<L0, L1, L2, L3>{});
}

Chao Liu's avatar
Chao Liu committed
146
147
// this is ugly, only for 4d
template <class InDesc, class WeiDesc>
148
__host__ __device__ constexpr auto get_convolution_output_4d_tensor_descriptor(InDesc, WeiDesc)
Chao Liu's avatar
Chao Liu committed
149
150
151
152
{
    constexpr auto in_desc  = InDesc{};
    constexpr auto wei_desc = WeiDesc{};

Chao Liu's avatar
Chao Liu committed
153
154
155
156
    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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

    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
179
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
180
181
182
{
    constexpr auto desc = TDesc{};

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

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

Chao Liu's avatar
Chao Liu committed
190
191
    printf("%s dim %u, lengths {%u %u %u %u}, strides {%u %u %u %u}\n",
           s,
Chao Liu's avatar
Chao Liu committed
192
193
194
195
196
197
198
199
200
201
           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));
}