ConstantTensorDescriptor.hip.hpp 10.8 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#pragma once
2
#include "common.hip.hpp"
Chao Liu's avatar
Chao Liu committed
3

Chao Liu's avatar
Chao Liu committed
4
// this is ugly, only for 2d
Chao Liu's avatar
Chao Liu committed
5
template <index_t L0, index_t L1>
Chao Liu's avatar
Chao Liu committed
6
7
8
9
10
__host__ __device__ constexpr auto calculate_default_strides(Sequence<L0, L1>)
{
    return Sequence<L1, 1>{};
}

Chao Liu's avatar
Chao Liu committed
11
// this is ugly, only for 4d
Chao Liu's avatar
Chao Liu committed
12
template <index_t L0, index_t L1, index_t L2, index_t L3>
Chao Liu's avatar
Chao Liu committed
13
14
15
16
17
__host__ __device__ constexpr auto calculate_default_strides(Sequence<L0, L1, L2, L3>)
{
    return Sequence<L1 * L2 * L3, L2 * L3, L3, 1>{};
}

18
// this is ugly, only for 6d
Chao Liu's avatar
Chao Liu committed
19
template <index_t L0, index_t L1, index_t L2, index_t L3, index_t L4, index_t L5>
20
21
22
23
24
25
__host__ __device__ constexpr auto calculate_default_strides(Sequence<L0, L1, L2, L3, L4, L5>)
{
    return Sequence<L1 * L2 * L3 * L4 * L5, L2 * L3 * L4 * L5, L3 * L4 * L5, L4 * L5, L5, 1>{};
}

// this is ugly, only for 8d
Chao Liu's avatar
Chao Liu committed
26
27
28
29
30
31
32
33
template <index_t L0,
          index_t L1,
          index_t L2,
          index_t L3,
          index_t L4,
          index_t L5,
          index_t L6,
          index_t L7>
34
35
36
37
38
39
40
41
42
43
44
45
46
__host__ __device__ constexpr auto
    calculate_default_strides(Sequence<L0, L1, L2, L3, L4, L5, L6, L7>)
{
    return Sequence<L1 * L2 * L3 * L4 * L5 * L6 * L7,
                    L2 * L3 * L4 * L5 * L6 * L7,
                    L3 * L4 * L5 * L6 * L7,
                    L4 * L5 * L6 * L7,
                    L5 * L6 * L7,
                    L6 * L7,
                    L7,
                    1>{};
}

Chao Liu's avatar
Chao Liu committed
47
// this is ugly, only for 2d
Chao Liu's avatar
Chao Liu committed
48
template <index_t L0, index_t L1, index_t Align>
Chao Liu's avatar
Chao Liu committed
49
50
51
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1>,
                                                                     Number<Align>)
{
Chao Liu's avatar
Chao Liu committed
52
    constexpr index_t L1_align = Align * ((L1 + Align - 1) / Align);
Chao Liu's avatar
Chao Liu committed
53
54
55
56
    return Sequence<L1_align, 1>{};
}

// this is ugly, only for 4d
Chao Liu's avatar
Chao Liu committed
57
template <index_t L0, index_t L1, index_t L2, index_t L3, index_t Align>
Chao Liu's avatar
Chao Liu committed
58
59
60
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1, L2, L3>,
                                                                     Number<Align>)
{
Chao Liu's avatar
Chao Liu committed
61
    constexpr index_t L3_align = Align * ((L3 + Align - 1) / Align);
Chao Liu's avatar
Chao Liu committed
62
63
64
    return Sequence<L1 * L2 * L3_align, L2 * L3_align, L3_align, 1>{};
}

Chao Liu's avatar
Chao Liu committed
65
66
67
template <class Lengths, class Strides>
struct ConstantTensorDescriptor
{
Chao Liu's avatar
Chao Liu committed
68
69
    using Type                    = ConstantTensorDescriptor<Lengths, Strides>;
    static constexpr index_t nDim = Lengths::nDim;
Chao Liu's avatar
Chao Liu committed
70
71
72
73
74
75

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

Chao Liu's avatar
Chao Liu committed
76
    __host__ __device__ constexpr index_t GetDimension() const { return nDim; }
Chao Liu's avatar
Chao Liu committed
77
78
79
80
81

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

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

Chao Liu's avatar
Chao Liu committed
82
83
    template <index_t I>
    __host__ __device__ constexpr index_t GetLength(Number<I>) const
Chao Liu's avatar
Chao Liu committed
84
    {
Chao Liu's avatar
Chao Liu committed
85
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
86
87
    }

Chao Liu's avatar
Chao Liu committed
88
89
    template <index_t I>
    __host__ __device__ constexpr index_t GetStride(Number<I>) const
Chao Liu's avatar
Chao Liu committed
90
    {
Chao Liu's avatar
Chao Liu committed
91
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
92
93
    }

94
95
    // c++14 doesn't support constexpr lambdas, has to use this trick instead
    struct GetElementSize_f
Chao Liu's avatar
Chao Liu committed
96
    {
97
        template <class IDim>
Chao Liu's avatar
Chao Liu committed
98
        __host__ __device__ constexpr index_t operator()(IDim idim) const
Chao Liu's avatar
Chao Liu committed
99
        {
100
            return Type{}.GetLength(idim);
Chao Liu's avatar
Chao Liu committed
101
        }
102
    };
Chao Liu's avatar
Chao Liu committed
103

Chao Liu's avatar
Chao Liu committed
104
    __host__ __device__ constexpr index_t GetElementSize() const
105
106
107
    {
        // c++14 doesn't support constexpr lambdas, has to use this trick instead
        struct multiply
108
        {
Chao Liu's avatar
Chao Liu committed
109
            __host__ __device__ constexpr index_t operator()(index_t a, index_t b) const
110
111
112
113
            {
                return a * b;
            }
        };
114

115
116
        return static_const_reduce_n<nDim>{}(GetElementSize_f{}, multiply{});
    }
117

118
119
120
121
    // c++14 doesn't support constexpr lambdas, has to use this trick instead
    struct GetElementSpace_f
    {
        template <class IDim>
Chao Liu's avatar
Chao Liu committed
122
        __host__ __device__ constexpr index_t operator()(IDim idim) const
123
        {
124
            return (Type{}.GetLength(idim) - 1) * Type{}.GetStride(idim);
125
        }
126
    };
Chao Liu's avatar
Chao Liu committed
127

Chao Liu's avatar
Chao Liu committed
128
    template <class Align = Number<1>>
Chao Liu's avatar
Chao Liu committed
129
    __host__ __device__ constexpr index_t GetElementSpace(Align align = Align{}) const
Chao Liu's avatar
Chao Liu committed
130
    {
131
132
        // c++14 doesn't support constexpr lambdas, has to use this trick instead
        struct add
Chao Liu's avatar
Chao Liu committed
133
        {
Chao Liu's avatar
Chao Liu committed
134
            __host__ __device__ constexpr index_t operator()(index_t a, index_t b) const
135
136
137
138
            {
                return a + b;
            }
        };
Chao Liu's avatar
Chao Liu committed
139

140
        return static_const_reduce_n<nDim>{}(GetElementSpace_f{}, add{}) + align.Get();
Chao Liu's avatar
Chao Liu committed
141
    }
Chao Liu's avatar
Chao Liu committed
142

143
    template <class... Is>
Chao Liu's avatar
Chao Liu committed
144
    __host__ __device__ index_t Get1dIndex(Is... is) const
Chao Liu's avatar
Chao Liu committed
145
    {
146
        static_assert(sizeof...(Is) == nDim, "number of multi-index is wrong");
Chao Liu's avatar
Chao Liu committed
147

Chao Liu's avatar
Chao Liu committed
148
        const auto multi_id = Array<index_t, nDim>(is...);
Chao Liu's avatar
Chao Liu committed
149

Chao Liu's avatar
Chao Liu committed
150
        index_t id = 0;
Chao Liu's avatar
Chao Liu committed
151

152
        static_loop_n<nDim>{}([&](auto IDim) {
Chao Liu's avatar
Chao Liu committed
153
154
155
156
            constexpr index_t idim = IDim.Get();
#if DEVICE_BACKEND_HIP
            id += __mul24(multi_id[idim], GetStride(IDim));
#else
157
            id += multi_id[idim] * GetStride(IDim);
Chao Liu's avatar
Chao Liu committed
158
#endif
159
        });
Chao Liu's avatar
Chao Liu committed
160

161
        return id;
162
163
    }

Chao Liu's avatar
Chao Liu committed
164
165
166
167
168
    __host__ __device__ constexpr auto Condense() const
    {
        constexpr auto default_strides = calculate_default_strides(Lengths{});
        return ConstantTensorDescriptor<Lengths, decltype(default_strides)>{};
    }
169

Chao Liu's avatar
Chao Liu committed
170
    template <index_t IDim, index_t NVector>
171
172
173
174
    __host__ __device__ constexpr auto Vectorize(Number<IDim>, Number<NVector>) const
    {
        assert(false); // not implemented
    }
Chao Liu's avatar
Chao Liu committed
175
};
Chao Liu's avatar
Chao Liu committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189

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

Chao Liu's avatar
Chao Liu committed
190
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
191
192
193
194
195
196
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_aligned(Lengths, Number<Align>)
{
    using Strides = decltype(calculate_default_strides_aligned(Lengths{}, Number<Align>{}));
    return ConstantTensorDescriptor<Lengths, Strides>{};
}

Chao Liu's avatar
Chao Liu committed
197
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
198
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
199
{
Chao Liu's avatar
Chao Liu committed
200
201
    constexpr auto desc    = TDesc{};
    constexpr index_t ndim = desc.GetDimension();
Chao Liu's avatar
Chao Liu committed
202

203
    static_assert(ndim >= 2 && ndim <= 8, "wrong!");
Chao Liu's avatar
Chao Liu committed
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

    if(ndim == 2)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};

        printf("%s dim %u, lengths {%u %u}, strides {%u %u}\n",
               s,
               desc.GetDimension(),
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetStride(I0),
               desc.GetStride(I1));
    }
    else if(ndim == 4)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        printf("%s dim %u, lengths {%u %u %u %u}, strides {%u %u %u %u}\n",
               s,
               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));
    }
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
    else if(ndim == 5)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};
        constexpr auto I4 = Number<4>{};

        printf("%s dim %u, lengths {%u %u %u %u %u}, strides {%u %u %u %u %u}\n",
               s,
               desc.GetDimension(),
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetLength(I4),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4));
    }
    else if(ndim == 6)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};
        constexpr auto I4 = Number<4>{};
        constexpr auto I5 = Number<5>{};

        printf("%s dim %u, lengths {%u %u %u %u %u %u}, strides {%u %u %u %u %u %u}\n",
               s,
               desc.GetDimension(),
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetLength(I4),
               desc.GetLength(I5),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4),
               desc.GetStride(I5));
    }
    else if(ndim == 7)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};
        constexpr auto I4 = Number<4>{};
        constexpr auto I5 = Number<5>{};
        constexpr auto I6 = Number<6>{};

        printf("%s dim %u, lengths {%u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u}\n",
               s,
               desc.GetDimension(),
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetLength(I4),
               desc.GetLength(I5),
               desc.GetLength(I6),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4),
               desc.GetStride(I5),
               desc.GetStride(I6));
    }
    else if(ndim == 8)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};
        constexpr auto I4 = Number<4>{};
        constexpr auto I5 = Number<5>{};
        constexpr auto I6 = Number<6>{};
        constexpr auto I7 = Number<7>{};

        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u %u}\n",
               s,
               desc.GetDimension(),
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetLength(I4),
               desc.GetLength(I5),
               desc.GetLength(I6),
               desc.GetLength(I7),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4),
               desc.GetStride(I5),
               desc.GetStride(I6),
               desc.GetStride(I7));
    }
Chao Liu's avatar
Chao Liu committed
343
}