"...composable_kernel.git" did not exist on "5c7cec11159d22636dd4c1119e7e430d156a8df7"
ConstantTensorDescriptor.hip.hpp 15.3 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>{};
}

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

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

25
// this is ugly, only for 6d
Chao Liu's avatar
Chao Liu committed
26
template <index_t L0, index_t L1, index_t L2, index_t L3, index_t L4, index_t L5>
27
28
29
30
31
32
__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
33
34
35
36
37
38
39
40
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>
41
42
43
44
45
46
47
48
49
50
51
52
53
__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>{};
}

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// this is ugly, only for 8d
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,
          index_t L8,
          index_t L9>
__host__ __device__ constexpr auto
    calculate_default_strides(Sequence<L0, L1, L2, L3, L4, L5, L6, L7, L8, L9>)
{
    return Sequence<L1 * L2 * L3 * L4 * L5 * L6 * L7 * L8 * L9,
                    L2 * L3 * L4 * L5 * L6 * L7 * L8 * L9,
                    L3 * L4 * L5 * L6 * L7 * L8 * L9,
                    L4 * L5 * L6 * L7 * L8 * L9,
                    L5 * L6 * L7 * L8 * L9,
                    L6 * L7 * L8 * L9,
                    L7 * L8 * L9,
                    L8 * L9,
                    L9,
                    1>{};
}

Chao Liu's avatar
Chao Liu committed
80
// this is ugly, only for 2d
Chao Liu's avatar
Chao Liu committed
81
template <index_t L0, index_t L1, index_t Align>
Chao Liu's avatar
Chao Liu committed
82
83
84
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1>,
                                                                     Number<Align>)
{
Chao Liu's avatar
Chao Liu committed
85
    constexpr index_t L1_align = Align * ((L1 + Align - 1) / Align);
Chao Liu's avatar
Chao Liu committed
86
87
88
    return Sequence<L1_align, 1>{};
}

89
90
91
92
93
94
95
96
97
// 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
98
// this is ugly, only for 4d
Chao Liu's avatar
Chao Liu committed
99
template <index_t L0, index_t L1, index_t L2, index_t L3, index_t Align>
Chao Liu's avatar
Chao Liu committed
100
101
102
__host__ __device__ constexpr auto calculate_default_strides_aligned(Sequence<L0, L1, L2, L3>,
                                                                     Number<Align>)
{
Chao Liu's avatar
Chao Liu committed
103
    constexpr index_t L3_align = Align * ((L3 + Align - 1) / Align);
Chao Liu's avatar
Chao Liu committed
104
105
106
    return Sequence<L1 * L2 * L3_align, L2 * L3_align, L3_align, 1>{};
}

Chao Liu's avatar
Chao Liu committed
107
108
109
template <class Lengths, class Strides>
struct ConstantTensorDescriptor
{
Chao Liu's avatar
Chao Liu committed
110
111
    using Type                    = ConstantTensorDescriptor<Lengths, Strides>;
    static constexpr index_t nDim = Lengths::nDim;
Chao Liu's avatar
Chao Liu committed
112
113
114
115
116
117

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

Chao Liu's avatar
Chao Liu committed
118
    __host__ __device__ constexpr index_t GetDimension() const { return nDim; }
Chao Liu's avatar
Chao Liu committed
119
120
121
122
123

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

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

Chao Liu's avatar
Chao Liu committed
124
125
    template <index_t I>
    __host__ __device__ constexpr index_t GetLength(Number<I>) const
Chao Liu's avatar
Chao Liu committed
126
    {
Chao Liu's avatar
Chao Liu committed
127
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
128
129
    }

Chao Liu's avatar
Chao Liu committed
130
131
    template <index_t I>
    __host__ __device__ constexpr index_t GetStride(Number<I>) const
Chao Liu's avatar
Chao Liu committed
132
    {
Chao Liu's avatar
Chao Liu committed
133
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
134
135
    }

136
137
    // c++14 doesn't support constexpr lambdas, has to use this trick instead
    struct GetElementSize_f
Chao Liu's avatar
Chao Liu committed
138
    {
139
        template <class IDim>
Chao Liu's avatar
Chao Liu committed
140
        __host__ __device__ constexpr index_t operator()(IDim idim) const
Chao Liu's avatar
Chao Liu committed
141
        {
142
            return Type{}.GetLength(idim);
Chao Liu's avatar
Chao Liu committed
143
        }
144
    };
Chao Liu's avatar
Chao Liu committed
145

Chao Liu's avatar
Chao Liu committed
146
    __host__ __device__ constexpr index_t GetElementSize() const
147
148
149
    {
        // c++14 doesn't support constexpr lambdas, has to use this trick instead
        struct multiply
150
        {
Chao Liu's avatar
Chao Liu committed
151
            __host__ __device__ constexpr index_t operator()(index_t a, index_t b) const
152
153
154
155
            {
                return a * b;
            }
        };
156

157
158
        return static_const_reduce_n<nDim>{}(GetElementSize_f{}, multiply{});
    }
159

160
161
162
163
    // 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
164
        __host__ __device__ constexpr index_t operator()(IDim idim) const
165
        {
166
            return (Type{}.GetLength(idim) - 1) * Type{}.GetStride(idim);
167
        }
168
    };
Chao Liu's avatar
Chao Liu committed
169

Chao Liu's avatar
Chao Liu committed
170
    template <class Align = Number<1>>
Chao Liu's avatar
Chao Liu committed
171
    __host__ __device__ constexpr index_t GetElementSpace(Align align = Align{}) const
Chao Liu's avatar
Chao Liu committed
172
    {
173
174
        // c++14 doesn't support constexpr lambdas, has to use this trick instead
        struct add
Chao Liu's avatar
Chao Liu committed
175
        {
Chao Liu's avatar
Chao Liu committed
176
            __host__ __device__ constexpr index_t operator()(index_t a, index_t b) const
177
178
179
180
            {
                return a + b;
            }
        };
Chao Liu's avatar
Chao Liu committed
181

Chao Liu's avatar
Chao Liu committed
182
183
184
185
        index_t element_space_unaligned =
            static_const_reduce_n<nDim>{}(GetElementSpace_f{}, add{}) + 1;

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

188
    template <class... Is>
Chao Liu's avatar
Chao Liu committed
189
    __host__ __device__ index_t Get1dIndex(Is... is) const
Chao Liu's avatar
Chao Liu committed
190
    {
191
        static_assert(sizeof...(Is) == nDim, "number of multi-index is wrong");
Chao Liu's avatar
Chao Liu committed
192

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

Chao Liu's avatar
Chao Liu committed
195
        index_t id = 0;
Chao Liu's avatar
Chao Liu committed
196

197
        static_loop_n<nDim>{}([&](auto IDim) {
Chao Liu's avatar
Chao Liu committed
198
199
200
201
            constexpr index_t idim = IDim.Get();
#if DEVICE_BACKEND_HIP
            id += __mul24(multi_id[idim], GetStride(IDim));
#else
202
            id += multi_id[idim] * GetStride(IDim);
Chao Liu's avatar
Chao Liu committed
203
#endif
204
        });
Chao Liu's avatar
Chao Liu committed
205

206
        return id;
207
208
    }

Chao Liu's avatar
Chao Liu committed
209
210
211
212
213
    __host__ __device__ constexpr auto Condense() const
    {
        constexpr auto default_strides = calculate_default_strides(Lengths{});
        return ConstantTensorDescriptor<Lengths, decltype(default_strides)>{};
    }
214

Chao Liu's avatar
Chao Liu committed
215
    template <index_t IDim, index_t NVector>
216
217
218
219
    __host__ __device__ constexpr auto Vectorize(Number<IDim>, Number<NVector>) const
    {
        assert(false); // not implemented
    }
Chao Liu's avatar
Chao Liu committed
220
};
Chao Liu's avatar
Chao Liu committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234

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
235
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
236
237
238
239
240
241
__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
242
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
243
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
244
{
Chao Liu's avatar
Chao Liu committed
245
246
    constexpr auto desc    = TDesc{};
    constexpr index_t ndim = desc.GetDimension();
Chao Liu's avatar
Chao Liu committed
247

Chao Liu's avatar
Chao Liu committed
248
    static_assert(ndim >= 2 && ndim <= 10, "wrong!");
Chao Liu's avatar
Chao Liu committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262

    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));
    }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    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,
               desc.GetDimension(),
               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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    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));
    }
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
343
344
345
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    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
404
405
406
407
408
409
410
411
412
413
414
415
    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
416
417
        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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
               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.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
452
453
        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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
               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.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
477
}