ConstantTensorDescriptor.hip.hpp 15.9 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

4
5
template <class PreviousStrides, class RemainLengths>
__host__ __device__ constexpr auto calculate_default_strides_impl(PreviousStrides, RemainLengths)
Chao Liu's avatar
Chao Liu committed
6
{
7
8
9
    constexpr index_t previous_stride = PreviousStrides{}.Front();
    constexpr index_t current_length  = RemainLengths{}.Back();
    constexpr index_t current_stride  = current_length * previous_stride;
Chao Liu's avatar
Chao Liu committed
10

11
12
    return calculate_default_strides_impl(PreviousStrides{}.PushFront(Number<current_stride>{}),
                                          RemainLengths{}.PopBack());
Chao Liu's avatar
Chao Liu committed
13
14
}

15
16
template <class PreviousStrides, index_t L0, index_t L1>
__host__ __device__ constexpr auto calculate_default_strides_impl(PreviousStrides, Sequence<L0, L1>)
17
{
18
19
    constexpr index_t previous_stride = PreviousStrides{}.Front();
    constexpr index_t current_stride  = L1 * previous_stride;
20

21
    return PreviousStrides{}.PushFront(Number<current_stride>{});
22
23
}

24
25
template <class Lengths>
__host__ __device__ constexpr auto calculate_default_strides(Lengths)
26
{
27
    return calculate_default_strides_impl(Sequence<1>{}, Lengths{});
28
29
}

Chao Liu's avatar
Chao Liu committed
30
// this is ugly, only for 2d
Chao Liu's avatar
Chao Liu committed
31
template <index_t L0, index_t L1, index_t Align>
Chao Liu's avatar
Chao Liu committed
32
33
34
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1>,
                                                                     Number<Align>)
{
Chao Liu's avatar
Chao Liu committed
35
    constexpr index_t L1_align = Align * ((L1 + Align - 1) / Align);
Chao Liu's avatar
Chao Liu committed
36
37
38
    return Sequence<L1_align, 1>{};
}

39
40
41
42
43
44
45
46
47
// this is ugly, only for 3d
template <index_t L0, index_t L1, index_t L2, index_t Align>
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1, L2>,
                                                                     Number<Align>)
{
    constexpr index_t L2_align = Align * ((L2 + Align - 1) / Align);
    return Sequence<L1 * L2_align, L2_align, 1>{};
}

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

Chao Liu's avatar
Chao Liu committed
57
58
59
template <class Lengths, class Strides>
struct ConstantTensorDescriptor
{
Chao Liu's avatar
Chao Liu committed
60
    using Type                    = ConstantTensorDescriptor<Lengths, Strides>;
61
    static constexpr index_t nDim = Lengths::GetSize();
Chao Liu's avatar
Chao Liu committed
62
63
64

    __host__ __device__ constexpr ConstantTensorDescriptor()
    {
65
        static_assert(Lengths::GetSize() == Strides::GetSize(), "nDim not consistent");
Chao Liu's avatar
Chao Liu committed
66
67
    }

Chao Liu's avatar
Chao Liu committed
68
    __host__ __device__ static constexpr index_t GetNumOfDimension() { return nDim; }
Chao Liu's avatar
Chao Liu committed
69

70
    __host__ __device__ static constexpr Lengths GetLengths() { return Lengths{}; }
Chao Liu's avatar
Chao Liu committed
71

72
    __host__ __device__ static constexpr Strides GetStrides() { return Strides{}; }
Chao Liu's avatar
Chao Liu committed
73

Chao Liu's avatar
Chao Liu committed
74
    template <index_t I>
75
    __host__ __device__ static constexpr index_t GetLength(Number<I>)
Chao Liu's avatar
Chao Liu committed
76
    {
Chao Liu's avatar
Chao Liu committed
77
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
78
79
    }

Chao Liu's avatar
Chao Liu committed
80
    template <index_t I>
81
    __host__ __device__ static constexpr index_t GetStride(Number<I>)
Chao Liu's avatar
Chao Liu committed
82
    {
Chao Liu's avatar
Chao Liu committed
83
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
84
85
    }

86
    __host__ __device__ static constexpr index_t GetElementSize()
Chao Liu's avatar
Chao Liu committed
87
    {
88
        return accumulate_on_sequence(Lengths{}, mod_conv::multiplies<index_t>{}, Number<1>{});
89
    }
90

91
92
93
94
    // 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
95
        __host__ __device__ constexpr index_t operator()(IDim idim) const
96
        {
97
            return (Type{}.GetLength(idim) - 1) * Type{}.GetStride(idim);
98
        }
99
    };
Chao Liu's avatar
Chao Liu committed
100

Chao Liu's avatar
Chao Liu committed
101
    template <class Align = Number<1>>
102
    __host__ __device__ static constexpr index_t GetElementSpace(Align align = Align{})
Chao Liu's avatar
Chao Liu committed
103
    {
Chao Liu's avatar
Chao Liu committed
104
        index_t element_space_unaligned =
105
            static_const_reduce_n<nDim>{}(GetElementSpace_f{}, mod_conv::plus<index_t>{}) + 1;
Chao Liu's avatar
Chao Liu committed
106
107

        return align.Get() * ((element_space_unaligned + align.Get() - 1) / align.Get());
Chao Liu's avatar
Chao Liu committed
108
    }
Chao Liu's avatar
Chao Liu committed
109

110
111
    template <index_t NSize>
    __host__ __device__ static index_t Get1dIndex(Array<index_t, NSize> multi_id)
Chao Liu's avatar
Chao Liu committed
112
    {
113
        static_assert(NSize == nDim, "wrong! Dimension not consistent");
Chao Liu's avatar
Chao Liu committed
114

Chao Liu's avatar
Chao Liu committed
115
        index_t id = 0;
Chao Liu's avatar
Chao Liu committed
116

117
        static_for<0, nDim, 1>{}([&](auto IDim) {
Chao Liu's avatar
Chao Liu committed
118
            constexpr index_t idim = IDim.Get();
119
120
            id += multi_id[idim] * GetStride(IDim);
        });
Chao Liu's avatar
Chao Liu committed
121

122
        return id;
123
124
    }

125
126
127
128
129
130
131
132
133
134
    template <class... Is>
    __host__ __device__ static index_t Get1dIndex(Is... is)
    {
        static_assert(sizeof...(Is) == nDim, "number of multi-index is wrong");

        const auto multi_id = Array<index_t, nDim>(is...);

        return Get1dIndex(multi_id);
    }

135
    template <index_t... Is>
Chao Liu's avatar
Chao Liu committed
136
    __host__ __device__ static constexpr index_t Get1dIndex(Sequence<Is...> /*multi_id*/)
137
138
139
    {
        static_assert(sizeof...(Is) == nDim, "wrong! Dimension not consistent");

Chao Liu's avatar
Chao Liu committed
140
141
142
143
144
145
        constexpr auto multi_id = Sequence<Is...>{};

        constexpr auto seq_tmp =
            transform_sequences(mod_conv::multiplies<index_t>{}, multi_id, GetStrides());

        return accumulate_on_sequence(seq_tmp, mod_conv::plus<index_t>{}, Number<0>{});
146
147
    }

148
    __host__ __device__ static Array<index_t, nDim> GetMultiIndex(index_t id)
Chao Liu's avatar
Chao Liu committed
149
    {
150
151
152
153
154
155
156
157
158
159
160
        Array<index_t, nDim> multi_id;

        static_for<0, nDim - 1, 1>{}([&](auto IDim) {
            constexpr index_t idim = IDim.Get();
            multi_id[idim]         = id / GetStride(IDim);
            id -= multi_id[idim] * GetStride(IDim);
        });

        multi_id[nDim - 1] = id / GetStride(Number<nDim - 1>{});

        return multi_id;
Chao Liu's avatar
Chao Liu committed
161
    }
162

Chao Liu's avatar
Chao Liu committed
163
    __host__ __device__ static constexpr auto Pack()
164
    {
165
166
        constexpr auto default_strides = calculate_default_strides(Lengths{});
        return ConstantTensorDescriptor<Lengths, decltype(default_strides)>{};
167
    }
Chao Liu's avatar
Chao Liu committed
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

    template <index_t IDims...>
    __host__ __device__ static constexpr auto Extract(Number<IDims>... /*extracted_dims...*/)
    {
        static_assert(sizeof...(IDims) <= GetNumOfDimension(), "wrong!");

        constexpr auto extracted_lengths = Sequence<Lengths{}.Get(Number<IDims>{})...>{};
        constexpr auto extracted_strides = Sequence<Strides{}.Get(Number<IDims>{})...>{};

        return make_ConstantTensorDescriptor(extracted_lenghts, extracted_strides);
    }

    template <index_t IDim, index_t SliceLen>
    __host__ __device__ static constexpr auto Slice(Number<IDim>, Number<SliceLen>)
    {
        // not implemented
    }

    template <index_t IDim, index_t... FoldLengths>
    __host__ device__ static constexpr auto Fold(Number<IDim>, Sequence<FoldLengths...>)
    {
        // not implemented
        // need to check the Length dimension to be folded is dividable by FoldLengths
    }

    template <index_t FirstUnfoldDim, index_t LastUnfoldDim>
    __host__ __device__ static constexpr auto Unfold(Number<FirstUnfoldDim>, Number<LastUnfoldDim>)
    {
        // not implemented
        // need to check the dimensions to be unfold are packed, otherwise, Unfold is not permitted
    }

    template <index_t... IRs>
    __host__ __device__ static constexpr auto ReorderGivenNew2Old(Sequence<IRs...> /*new2old*/)
    {
        static_assert(sizeof...(IRs) == GetNumberOfDimension(), "wrong! dimension is wrong");
        constexpr auto map_new2old = Sequence<IRs...>{};
        return make_ConstantTensorDescriptor(Lengths{}.ReorderGivenNew2Old(map_new2old),
                                             Strides{}.ReorderGivenNew2Old(map_new2old));
    }
Chao Liu's avatar
Chao Liu committed
208
};
Chao Liu's avatar
Chao Liu committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222

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
223
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
224
225
226
227
228
229
__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
230
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
231
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
232
{
Chao Liu's avatar
Chao Liu committed
233
    constexpr auto desc    = TDesc{};
Chao Liu's avatar
Chao Liu committed
234
    constexpr index_t ndim = desc.GetNumOfDimension();
Chao Liu's avatar
Chao Liu committed
235

Chao Liu's avatar
Chao Liu committed
236
    static_assert(ndim >= 2 && ndim <= 10, "wrong!");
Chao Liu's avatar
Chao Liu committed
237
238
239
240
241
242
243
244

    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,
Chao Liu's avatar
Chao Liu committed
245
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
246
247
248
249
250
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetStride(I0),
               desc.GetStride(I1));
    }
251
252
253
254
255
256
257
258
    else if(ndim == 3)
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};

        printf("%s dim %u, lengths {%u %u %u}, strides {%u %u %u}\n",
               s,
Chao Liu's avatar
Chao Liu committed
259
               desc.GetNumOfDimension(),
260
261
262
263
264
265
266
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2));
    }
Chao Liu's avatar
Chao Liu committed
267
268
269
270
271
272
273
274
275
    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,
Chao Liu's avatar
Chao Liu committed
276
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
277
278
279
280
281
282
283
284
285
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3));
    }
286
287
288
289
290
291
292
293
294
295
    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,
Chao Liu's avatar
Chao Liu committed
296
               desc.GetNumOfDimension(),
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
               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,
Chao Liu's avatar
Chao Liu committed
319
               desc.GetNumOfDimension(),
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
               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,
Chao Liu's avatar
Chao Liu committed
345
               desc.GetNumOfDimension(),
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
               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,
Chao Liu's avatar
Chao Liu committed
374
               desc.GetNumOfDimension(),
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
               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
392
393
394
395
396
397
398
399
400
401
402
403
    else if(ndim == 9)
    {
        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>{};
        constexpr auto I8 = Number<8>{};

Chao Liu's avatar
tidy yp  
Chao Liu committed
404
405
        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u %u "
               "%u}\n",
Chao Liu's avatar
Chao Liu committed
406
               s,
Chao Liu's avatar
Chao Liu committed
407
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
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
               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.GetLength(I8),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4),
               desc.GetStride(I5),
               desc.GetStride(I6),
               desc.GetStride(I7),
               desc.GetStride(I8));
    }
    else if(ndim == 10)
    {
        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>{};
        constexpr auto I8 = Number<8>{};
        constexpr auto I9 = Number<9>{};

Chao Liu's avatar
tidy yp  
Chao Liu committed
440
441
        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u "
               "%u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
442
               s,
Chao Liu's avatar
Chao Liu committed
443
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
               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.GetLength(I8),
               desc.GetLength(I9),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
               desc.GetStride(I3),
               desc.GetStride(I4),
               desc.GetStride(I5),
               desc.GetStride(I6),
               desc.GetStride(I7),
               desc.GetStride(I8),
               desc.GetStride(I9));
    }
Chao Liu's avatar
Chao Liu committed
465
}