ConstantTensorDescriptor.hip.hpp 31.1 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
template <class Lengths>
Chao Liu's avatar
Chao Liu committed
5
__host__ __device__ constexpr auto calculate_tensor_strides_default_rank_packed(Lengths)
6
{
7
    return reverse_inclusive_scan_sequence(Lengths{}.PopFront(), mod_conv::multiplies<index_t>{})
8
        .PushBack(Number<1>{});
9
10
}

11
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
12
13
__host__ __device__ constexpr auto calculate_tensor_strides_default_rank_aligned(Lengths,
                                                                                 Number<Align>)
Chao Liu's avatar
Chao Liu committed
14
{
15
16
    constexpr index_t L_back_align =
        Align * mod_conv::integer_divide_ceiler<index_t>{}(Lengths{}.Back(), Align);
Chao Liu's avatar
Chao Liu committed
17

Chao Liu's avatar
Chao Liu committed
18
    return calculate_tensor_strides_default_rank_packed(
19
        Lengths{}.Modify(Number<Lengths{}.GetSize() - 1>{}, Number<L_back_align>{}));
20
21
}

22
23
// MemoryRanks of dimensions is for conversion from offset to multi-index
template <class Lengths, class Strides, class MemoryRanks>
Chao Liu's avatar
Chao Liu committed
24
25
struct ConstantTensorDescriptor
{
Chao Liu's avatar
Chao Liu committed
26
27
    using Type = ConstantTensorDescriptor;

28
    static constexpr index_t nDim = Lengths::GetSize();
Chao Liu's avatar
Chao Liu committed
29
30
31

    __host__ __device__ constexpr ConstantTensorDescriptor()
    {
32
33
34
35
36
37
38
39
40
        static_assert(Lengths::GetSize() == Strides::GetSize() &&
                          Lengths::GetSize() == MemoryRanks::GetSize(),
                      "nDim not consistent");

#if 0 // require sequence_sort, but it's not implemented yet
        static_assert(is_same<typename sequence_sort<MemoryRanks>::SortedSeqType,
                              typename arithmetic_sequence_gen<0, nDim, 1>::SeqType>::value,
                      "wrong! invalid MemoryRanks");
#endif
Chao Liu's avatar
Chao Liu committed
41
42
    }

43
44
45
46
47
48
49
50
    __host__ __device__ static constexpr auto GetOriginalTensorDescriptor() { return Type{}; }

    template <index_t IDim>
    __host__ __device__ static constexpr auto GetContainedOriginalDimensions(Number<IDim>)
    {
        return Sequence<IDim>{};
    }

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

53
    __host__ __device__ static constexpr auto GetLengths() { return Lengths{}; }
Chao Liu's avatar
Chao Liu committed
54

55
56
57
    __host__ __device__ static constexpr auto GetStrides() { return Strides{}; }

    __host__ __device__ static constexpr auto GetMemoryRanks() { return MemoryRanks{}; }
Chao Liu's avatar
Chao Liu committed
58

Chao Liu's avatar
Chao Liu committed
59
    template <index_t I>
60
    __host__ __device__ static constexpr index_t GetLength(Number<I>)
Chao Liu's avatar
Chao Liu committed
61
    {
Chao Liu's avatar
Chao Liu committed
62
        return Lengths{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
63
64
    }

Chao Liu's avatar
Chao Liu committed
65
    template <index_t I>
66
    __host__ __device__ static constexpr index_t GetStride(Number<I>)
Chao Liu's avatar
Chao Liu committed
67
    {
Chao Liu's avatar
Chao Liu committed
68
        return Strides{}.Get(Number<I>{});
Chao Liu's avatar
Chao Liu committed
69
70
    }

71
72
73
74
75
76
    template <index_t I>
    __host__ __device__ static constexpr index_t GetMemoryRank(Number<I>)
    {
        return MemoryRanks{}.Get(Number<I>{});
    }

77
78
79
80
81
82
83
84
85
86
87
88
89
    __host__ __device__ static constexpr bool AreStridesNonAscending()
    {
        bool flag = true;

        static_for<0, nDim - 1, 1>{}([&](auto IDim) {
            constexpr auto IDim_p1 = Number<IDim.Get() + 1>{};

            flag = flag && (GetLength(IDim) >= GetLength(IDim_p1));
        });

        return flag;
    }

Chao Liu's avatar
Chao Liu committed
90
91
92
93
94
95
    template <class T>
    __host__ __device__ static constexpr bool ContainMultipleOriginalDimensions(T)
    {
        return false;
    }

96
    __host__ __device__ static constexpr index_t GetElementSize()
Chao Liu's avatar
Chao Liu committed
97
    {
98
        return accumulate_on_sequence(Lengths{}, mod_conv::multiplies<index_t>{}, Number<1>{});
99
    }
100

101
    // WRONG! ReorderGivenOld2New is broken
Chao Liu's avatar
Chao Liu committed
102
    template <class Align = Number<1>>
103
    __host__ __device__ static constexpr index_t GetElementSpace(Align align = Align{})
Chao Liu's avatar
Chao Liu committed
104
    {
105
106
107
108
109
#if 0
        constexpr auto lengths_in_rank = GetLengths().ReorderGivenOld2New(MemoryRank{});
        constexpr auto strides_in_rank = GetStrides().ReorderGivenOld2new(MemoryRank{});

        constexpr index_t element_space_unaligned = accumulate_on_sequence(
110
            (lengths_in_rank - Number<1>{}) * strides_in_rank, mod_conv::plus<index_t>{}, Number<1>{});
111
#else // WRONG! align shouldbe applied to the last memory rank, not the last tensor dimension
Chao Liu's avatar
Chao Liu committed
112
        constexpr index_t element_space_unaligned = accumulate_on_sequence(
113
            (GetLengths() - Number<1>{}) * GetStrides(), mod_conv::plus<index_t>{}, Number<1>{});
114
#endif
Chao Liu's avatar
Chao Liu committed
115
116

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

119
    template <index_t NSize>
120
    __host__ __device__ static index_t GetOffsetFromMultiIndex(Array<index_t, NSize> multi_id)
Chao Liu's avatar
Chao Liu committed
121
    {
122
        static_assert(NSize == nDim, "wrong! Dimension not consistent");
Chao Liu's avatar
Chao Liu committed
123

124
        index_t offset = 0;
Chao Liu's avatar
Chao Liu committed
125

126
        static_for<0, nDim, 1>{}([&](auto IDim) {
Chao Liu's avatar
Chao Liu committed
127
            constexpr index_t idim = IDim.Get();
128
            offset += multi_id[idim] * GetStride(IDim);
129
        });
Chao Liu's avatar
Chao Liu committed
130

131
        return offset;
132
133
    }

134
    template <class... Is>
135
    __host__ __device__ static index_t GetOffsetFromMultiIndex(Is... is)
136
    {
137
        return GetOffsetFromMultiIndex(Array<index_t, sizeof...(Is)>{is...});
138
139
    }

140
    template <index_t... Is>
141
    __host__ __device__ static constexpr index_t GetOffsetFromMultiIndex(Sequence<Is...>)
142
143
144
    {
        static_assert(sizeof...(Is) == nDim, "wrong! Dimension not consistent");

Chao Liu's avatar
Chao Liu committed
145
146
        constexpr auto multi_id = Sequence<Is...>{};

147
148
        return accumulate_on_sequence(
            multi_id * GetStrides(), mod_conv::plus<index_t>{}, Number<0>{});
149
150
    }

151
152
#if 0 // ReorderGivenOld2new is broken
    __host__ __device__ static Array<index_t, nDim> GetMultiIndexFromOffset(index_t offset)
Chao Liu's avatar
Chao Liu committed
153
    {
154
155
156
157
        Array<index_t, nDim> ranked_multi_id;

        constexpr auto ranked_strides =
            GetStrides().ReorderGivenOld2new(MemoryRanks{}); // check this
158

159
        // calculate index in each of the dimensions in the order of their rank (not dimension)
160
        static_for<0, nDim - 1, 1>{}([&](auto IDim) {
161
162
163
164
            constexpr index_t idim   = IDim.Get();
            constexpr index_t stride = ranked_strides.Get(Number<idim>{});
            ranked_multi_id[idim]    = offset / stride;
            offset -= ranked_multi_id[idim] * stride;
165
166
        });

167
        ranked_multi_id[nDim - 1] = offset / ranked_strides.Get(Number<nDim - 1>{});
168

169
        return reorder_array_given_new2old(ranked_multi_id, MemoryRanks{}); // check this
Chao Liu's avatar
Chao Liu committed
170
    }
171
#endif
172

173
    __host__ __device__ static Array<index_t, nDim> GetMultiIndexFrom1dIndex(index_t id)
174
    {
175
176
        Array<index_t, nDim> multi_id;

Chao Liu's avatar
Chao Liu committed
177
        constexpr auto dummy_strides = calculate_tensor_strides_default_rank_packed(GetLengths());
178
179
180
181
182
183
184
185
186
187
188
189

        // calculate index in each of the dimensions in the order of their dimension (not rank)
        static_for<0, nDim - 1, 1>{}([&](auto IDim) {
            constexpr index_t idim   = IDim.Get();
            constexpr index_t stride = dummy_strides.Get(Number<idim>{});
            multi_id[idim]           = id / stride;
            id -= multi_id[idim] * stride;
        });

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

        return multi_id;
190
    }
Chao Liu's avatar
Chao Liu committed
191

192
193
194
195
196
197
198
199
200
    __host__ __device__ static auto
    GetOriginalMultiIndexFromMultiIndex(Array<index_t, nDim> multi_id)
    {
        return multi_id;
    }

    // This function doesn't do carry check on the highest dimension, for performance reason.
    // It is the user's responsibility to make sure the result "new_mutli_id" is not out-of-bound
    // on the highest dimension
201
    template <bool PositiveDirection>
202
203
    __host__ __device__ static Array<index_t, nDim>
    UpdateMultiIndexGivenStepSizeOf1dIndex(Array<index_t, nDim> old_multi_id,
204
205
                                           index_t step_size_of_1d_index,
                                           integral_constant<bool, PositiveDirection>)
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
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
        Array<index_t, nDim> new_multi_id;

        const auto step_sizes = GetMultiIndexFrom1dIndex(step_size_of_1d_index);

        static_if<PositiveDirection>{}([&](auto) {
            new_multi_id = old_multi_id + step_sizes;

            bool carry = false;

            // do carry check in reversed order, starting from lowest dimension
            // don't check the highest dimension
            static_for<0, nDim - 1, 1>{}([&](auto IDimReverse) {
                constexpr index_t idim = nDim - 1 - IDimReverse.Get();
                constexpr auto IDim    = Number<idim>{};

                if(carry)
                {
                    ++new_multi_id[idim];
                }

                carry = false;

                if(new_multi_id[idim] >= GetLength(IDim))
                {
                    new_multi_id[idim] -= GetLength(IDim);
                    carry = true;
                }
            });
        }).Else([&](auto) {
            // shift up multi-id to avoid unsigned integer underflow during intermediate
            // calculations. After the shift, should have new_multi_id[...] >= 1
            new_multi_id = old_multi_id + (GetLengths() - step_sizes);

            bool borrow = false;

            // do borrow check in reversed order, starting from lowest dimension
            // don't check the highest dimension
            static_for<0, nDim - 1, 1>{}([&](auto IDimReverse) {
                constexpr index_t idim = nDim - 1 - IDimReverse.Get();
                constexpr auto IDim    = Number<idim>{};

                if(borrow)
                {
                    --new_multi_id[idim];
                }

                borrow = false;

                if(new_multi_id[idim] < GetLength(IDim))
                {
                    new_multi_id[idim] += GetLength(IDim);
                    borrow = true;
                }
            });

            // shift back down multi-id
            // here, should have new_multi_id[...] >= GetLengths()
            new_multi_id = new_multi_id - GetLengths();
265
266
267
268
269
        });

        return new_multi_id;
    }

270
    // WRONG! Ranks is broken
Chao Liu's avatar
Chao Liu committed
271
    template <index_t... IDims>
Chao Liu's avatar
Chao Liu committed
272
    __host__ __device__ static constexpr auto Extract(Number<IDims>... extract_dims)
Chao Liu's avatar
Chao Liu committed
273
    {
Chao Liu's avatar
Chao Liu committed
274
275
        static_assert(sizeof...(IDims) <= GetNumOfDimension(),
                      "wrong! too many number of dimensions to be extracted");
Chao Liu's avatar
Chao Liu committed
276

277
278
279
280
281
282
283
284
285
286
287
        using extract_lengths = decltype(Lengths{}.Extract(extract_dims...));
        using extract_strides = decltype(Strides{}.Extract(extract_dims...));
        using extract_ranks   = decltype(MemoryRanks{}.Extract(extract_dims...));

#if 0
        using new_ranks = typename sequence_sort<extract_ranks>::Original2SortedType;
#else // WRONG! TODO:: implement sequence_sort
        using new_ranks = typename arithmetic_sequence_gen<0, sizeof...(IDims), 1>::SeqType;
#endif

        return ConstantTensorDescriptor<extract_lengths, extract_strides, new_ranks>{};
Chao Liu's avatar
Chao Liu committed
288
289
    }

Chao Liu's avatar
Chao Liu committed
290
291
292
293
294
295
    template <index_t... IDims>
    __host__ __device__ static constexpr auto Extract(Sequence<IDims...>)
    {
        return Extract(Number<IDims>{}...);
    }

296
    template <class... Ts>
297
    __host__ __device__ static constexpr auto Embed(ConstantTensorDescriptor<Ts...>)
298
299
300
301
302
303
304
305
306
307
308
    {
        using leaf_tensor = ConstantTensorDescriptor<Ts...>;

        // memory rank is broken
        // TODO: remove memory rank info from tensor descritpor
        return ConstantTensorDescriptor<decltype(GetLengths().Append(leaf_tensor::GetLengths())),
                                        decltype(GetStrides().Append(leaf_tensor::GetStrides())),
                                        decltype(GetMemoryRanks().Append(
                                            leaf_tensor::GetMemoryRanks()))>{};
    }

Chao Liu's avatar
Chao Liu committed
309
310
311
    template <index_t IDim, index_t SliceLen>
    __host__ __device__ static constexpr auto Slice(Number<IDim>, Number<SliceLen>)
    {
312
313
314
        using slice_lengths = decltype(Lengths{}.Modify(Number<IDim>{}, Number<SliceLen>{}));

        return ConstantTensorDescriptor<slice_lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
315
316
    }

317
318
319
320
321
322
323
324
325
    template <index_t Threashold, index_t Delta>
    struct f_fold_impl
    {
        __host__ __device__ constexpr index_t operator()(index_t x) const
        {
            return x > Threashold ? x + Delta : x;
        }
    };

Chao Liu's avatar
Chao Liu committed
326
    template <index_t IDim, index_t... FoldIntervals>
Chao Liu's avatar
Chao Liu committed
327
    __host__ __device__ static constexpr auto Fold(Number<IDim>, Number<FoldIntervals>...)
Chao Liu's avatar
Chao Liu committed
328
    {
Chao Liu's avatar
Chao Liu committed
329
330
        constexpr auto fold_intervals = Sequence<FoldIntervals...>{};

Chao Liu's avatar
Chao Liu committed
331
        constexpr index_t fold_intervals_product =
332
            accumulate_on_sequence(fold_intervals, mod_conv::multiplies<index_t>{}, Number<1>{});
Chao Liu's avatar
Chao Liu committed
333
334
335

        constexpr auto unfold_length = GetLength(Number<IDim>{});
        constexpr auto unfold_stride = GetStride(Number<IDim>{});
336
        constexpr auto unfold_rank   = GetMemoryRank(Number<IDim>{});
Chao Liu's avatar
Chao Liu committed
337
338
339

        // length of the dimension to be folded needs to be dividable by fold_interval_product,
        // otherwise, folding is invalid
Chao Liu's avatar
Chao Liu committed
340
        static_assert(unfold_length % fold_intervals_product == 0,
Chao Liu's avatar
Chao Liu committed
341
342
343
344
                      "wrong! length on the dimension to be folded cannot be evenly divided!");

        // folded lengths
        constexpr auto fold_lengths =
Chao Liu's avatar
Chao Liu committed
345
            Sequence<unfold_length / fold_intervals_product>{}.Append(fold_intervals);
Chao Liu's avatar
Chao Liu committed
346
347

        // folded strides
Chao Liu's avatar
Chao Liu committed
348
349
        constexpr auto fold_strides =
            Number<unfold_stride>{} *
Chao Liu's avatar
Chao Liu committed
350
            reverse_inclusive_scan_sequence(fold_intervals.PushBack(Number<1>{}),
351
                                            mod_conv::multiplies<index_t>{});
Chao Liu's avatar
Chao Liu committed
352

353
354
355
356
357
        // folded_ranks
        constexpr auto fold_ranks =
            typename arithmetic_sequence_gen<unfold_rank,
                                             unfold_rank + fold_intervals.GetSize() + 1,
                                             1>::SeqType{};
Chao Liu's avatar
Chao Liu committed
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
        // increase the ranks that are larger than unfold_rank
        constexpr auto tmp_ranks = transform_sequences(
            f_fold_impl<unfold_rank, fold_intervals.GetSize()>{}, GetMemoryRanks());

        // left and right
        constexpr auto left = typename arithmetic_sequence_gen<0, IDim, 1>::SeqType{};
        constexpr auto right =
            typename arithmetic_sequence_gen<IDim + 1, GetNumOfDimension(), 1>::SeqType{};

        constexpr auto new_lengths =
            GetLengths().Extract(left).Append(fold_lengths).Append(GetLengths().Extract(right));
        constexpr auto new_strides =
            GetStrides().Extract(left).Append(fold_strides).Append(GetStrides().Extract(right));
        constexpr auto new_ranks =
            tmp_ranks.Extract(left).Append(fold_ranks).Append(tmp_ranks.Extract(right));

        static_assert(new_ranks.GetSize() == new_lengths.GetSize(), "wrong!");
        static_assert(fold_ranks.GetSize() == fold_lengths.GetSize(), "wrong!");

        return ConstantTensorDescriptor<decltype(new_lengths),
                                        decltype(new_strides),
                                        decltype(new_ranks)>{};
Chao Liu's avatar
Chao Liu committed
381
382
    }

383
384
385
386
387
388
389
390
391
    template <index_t Threashold, index_t Delta>
    struct f_unfold_impl
    {
        __host__ __device__ constexpr index_t operator()(index_t x) const
        {
            return x > Threashold ? x - Delta : x;
        }
    };

Chao Liu's avatar
Chao Liu committed
392
393
394
    template <index_t FirstUnfoldDim, index_t LastUnfoldDim>
    __host__ __device__ static constexpr auto Unfold(Number<FirstUnfoldDim>, Number<LastUnfoldDim>)
    {
Chao Liu's avatar
Chao Liu committed
395
396
397
398
        static_assert(FirstUnfoldDim >= 0 && LastUnfoldDim < nDim &&
                          FirstUnfoldDim <= LastUnfoldDim,
                      "wrong! should have FirstUnfoldDim <= LastUnfoldDim!");

Chao Liu's avatar
Chao Liu committed
399
#if 0 // cannot compile: compiler complain about constexpr
Chao Liu's avatar
Chao Liu committed
400
401
        // dimensions to be unfold need to be in descending order (w.r.t. strides), and need to be
        // packed in memory, otherwise, unfolding is invalid
Chao Liu's avatar
Chao Liu committed
402
403
        static_for<FirstUnfoldDim, LastUnfoldDim, 1>{}([&](auto IDim_) {
            constexpr auto IDim    = decltype(IDim_){};
404
405
406
            constexpr auto IDim_p1 = IDim + Number<1>{};

            // check stride
Chao Liu's avatar
Chao Liu committed
407
            static_assert(
408
                GetStride(IDim) >= GetStride(IDim_p1),
Chao Liu's avatar
Chao Liu committed
409
410
                "wrong! dimensions to be unfolded need to be in descending order w.r.t strides");

411
412
            // check if packed
            static_assert(GetStride(IDim_p1) * GetLength(IDim_p1) == GetStride(IDim),
Chao Liu's avatar
Chao Liu committed
413
                          "wrong! dimensions to be unfolded need to be packed");
414

Chao Liu's avatar
Chao Liu committed
415
            // check ranks
416
417
418
            static_assert(GetMemoryRank(IDim_p1) == GetMemoryRank(IDim) + 1,
                          "wrong! ranks of dimensions to be unfolded need to be in increasing and "
                          "continuous ranks");
Chao Liu's avatar
Chao Liu committed
419
        });
Chao Liu's avatar
Chao Liu committed
420
#endif
Chao Liu's avatar
Chao Liu committed
421

Chao Liu's avatar
Chao Liu committed
422
        // left and right
423
424
425
426
427
428
429
        constexpr auto left = typename arithmetic_sequence_gen<0, FirstUnfoldDim, 1>::SeqType{};
        constexpr auto middle =
            typename arithmetic_sequence_gen<FirstUnfoldDim, LastUnfoldDim + 1, 1>::SeqType{};
        constexpr auto right =
            typename arithmetic_sequence_gen<LastUnfoldDim + 1, GetNumOfDimension(), 1>::SeqType{};

        // unfolded length, stride and rank
Chao Liu's avatar
Chao Liu committed
430
        constexpr index_t unfold_length = accumulate_on_sequence(
431
            GetLengths().Extract(middle), mod_conv::multiplies<index_t>{}, Number<1>{});
Chao Liu's avatar
Chao Liu committed
432
433
434

        constexpr index_t unfold_stride = GetStride(Number<LastUnfoldDim>{});

435
436
437
438
        constexpr index_t unfold_rank = GetMemoryRank(Number<FirstUnfoldDim>{});

        // decrease the ranks that are larger than the rank of LastUnfoldDim
        constexpr auto tmp_ranks =
Chao Liu's avatar
Chao Liu committed
439
440
441
            transform_sequences(f_unfold_impl<GetMemoryRank(Number<LastUnfoldDim>{}),
                                              LastUnfoldDim - FirstUnfoldDim + 1>{},
                                GetMemoryRanks());
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

        // new lengths, strides and ranks
        constexpr auto new_lengths = GetLengths()
                                         .Extract(left)
                                         .PushBack(Number<unfold_length>{})
                                         .Append(GetLengths().Extract(right));

        constexpr auto new_strides = GetStrides()
                                         .Extract(left)
                                         .PushBack(Number<unfold_stride>{})
                                         .Append(GetStrides().Extract(right));

        constexpr auto new_ranks = tmp_ranks.Extract(left)
                                       .PushBack(Number<unfold_rank>{})
                                       .Append(tmp_ranks.Extract(right));

        return ConstantTensorDescriptor<decltype(new_lengths),
                                        decltype(new_strides),
                                        decltype(new_ranks)>{};
    }

    template <class MapNew2Old>
    __host__ __device__ static constexpr auto ReorderGivenNew2Old(MapNew2Old)
    {
        return ConstantTensorDescriptor<decltype(Lengths{}.ReorderGivenNew2Old(MapNew2Old{})),
                                        decltype(Strides{}.ReorderGivenNew2Old(MapNew2Old{})),
                                        decltype(
                                            MemoryRanks{}.ReorderGivenNew2Old(MapNew2Old{}))>{};
Chao Liu's avatar
Chao Liu committed
470
471
    }

472
473
474
#if 0 // require sequence_sort, which is not implemented yet
    template <class MapOld2New>
    __host__ __device__ static constexpr auto ReorderGivenOld2New(MapOld2New)
Chao Liu's avatar
Chao Liu committed
475
    {
476
477
478
479
        return ConstantTensorDescriptor<decltype(Lengths{}.ReorderGivenOld2New(MapOld2New{})),
                                        decltype(Strides{}.ReorderGivenOld2New(MapOld2New{})),
                                        decltype(
                                            MemoryRanks{}.ReorderGivenOld2New(MapOld2New{}))>{};
Chao Liu's avatar
Chao Liu committed
480
    }
481
#endif
Chao Liu's avatar
Chao Liu committed
482
};
Chao Liu's avatar
Chao Liu committed
483
484

template <class Lengths>
Chao Liu's avatar
Chao Liu committed
485
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank_packed(Lengths)
Chao Liu's avatar
Chao Liu committed
486
{
Chao Liu's avatar
Chao Liu committed
487
    using Strides     = decltype(calculate_tensor_strides_default_rank_packed(Lengths{}));
488
489
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
490
491
492
}

template <class Lengths, class Strides>
Chao Liu's avatar
Chao Liu committed
493
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank(Lengths, Strides)
Chao Liu's avatar
Chao Liu committed
494
{
495
496
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
497
498
}

Chao Liu's avatar
Chao Liu committed
499
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
500
501
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank_aligned(Lengths,
                                                                                      Number<Align>)
Chao Liu's avatar
Chao Liu committed
502
{
503
    using Strides =
Chao Liu's avatar
Chao Liu committed
504
        decltype(calculate_tensor_strides_default_rank_aligned(Lengths{}, Number<Align>{}));
505
506
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
507
508
}

Chao Liu's avatar
Chao Liu committed
509
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
510
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
511
{
Chao Liu's avatar
Chao Liu committed
512
    constexpr index_t ndim = TDesc::GetNumOfDimension();
Chao Liu's avatar
Chao Liu committed
513

514
515
516
517
518
519
520
521
522
523
524
525
526
527
    static_assert(ndim >= 1 && ndim <= 10, "wrong!");

    static_if<ndim == 1>{}([&](auto fwd) {
        constexpr auto I0 = Number<0>{};

        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u}, strides {%u}, ranks {%u}\n",
               s,
               desc.GetNumOfDimension(),
               desc.GetLength(I0),
               desc.GetStride(I0),
               desc.GetMemoryRank(I0));
    });
Chao Liu's avatar
Chao Liu committed
528

Chao Liu's avatar
Chao Liu committed
529
    static_if<ndim == 2>{}([&](auto fwd) {
Chao Liu's avatar
Chao Liu committed
530
531
532
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};

Chao Liu's avatar
Chao Liu committed
533
534
535
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u}, strides {%u %u}, ranks {%u %u}\n",
Chao Liu's avatar
Chao Liu committed
536
               s,
Chao Liu's avatar
Chao Liu committed
537
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
538
539
540
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetStride(I0),
Chao Liu's avatar
Chao Liu committed
541
542
543
544
545
546
               desc.GetStride(I1),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1));
    });

    static_if<ndim == 3>{}([&](auto fwd) {
547
548
549
550
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};

Chao Liu's avatar
Chao Liu committed
551
552
553
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u}, strides {%u %u %u}, ranks {%u %u %u}\n",
554
               s,
Chao Liu's avatar
Chao Liu committed
555
               desc.GetNumOfDimension(),
556
557
558
559
560
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetStride(I0),
               desc.GetStride(I1),
Chao Liu's avatar
Chao Liu committed
561
562
563
564
565
566
567
               desc.GetStride(I2),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2));
    });

    static_if<ndim == 4>{}([&](auto fwd) {
Chao Liu's avatar
Chao Liu committed
568
569
570
571
572
        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
573
574
575
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u %u}, strides {%u %u %u %u}, ranks {%u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
576
               s,
Chao Liu's avatar
Chao Liu committed
577
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
578
579
580
581
582
583
584
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetLength(I3),
               desc.GetStride(I0),
               desc.GetStride(I1),
               desc.GetStride(I2),
Chao Liu's avatar
Chao Liu committed
585
586
587
588
589
590
591
592
               desc.GetStride(I3),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3));
    });

    static_if<ndim == 5>{}([&](auto fwd) {
593
594
595
596
597
598
        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>{};

Chao Liu's avatar
Chao Liu committed
599
600
601
602
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u %u %u}, strides {%u %u %u %u %u}, ranks {%u %u %u %u "
               "%u}\n",
603
               s,
Chao Liu's avatar
Chao Liu committed
604
               desc.GetNumOfDimension(),
605
606
607
608
609
610
611
612
613
               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),
Chao Liu's avatar
Chao Liu committed
614
615
616
617
618
619
620
621
622
               desc.GetStride(I4),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4));
    });

    static_if<ndim == 6>{}([&](auto fwd) {
623
624
625
626
627
628
629
        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>{};

Chao Liu's avatar
Chao Liu committed
630
631
632
633
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u %u %u %u}, strides {%u %u %u %u %u %u}, ranks {%u %u "
               "%u %u %u %u}\n",
634
               s,
Chao Liu's avatar
Chao Liu committed
635
               desc.GetNumOfDimension(),
636
637
638
639
640
641
642
643
644
645
646
               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),
Chao Liu's avatar
Chao Liu committed
647
648
649
650
651
652
653
654
655
656
               desc.GetStride(I5),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4),
               desc.GetMemoryRank(I5));
    });

    static_if<ndim == 7>{}([&](auto fwd) {
657
658
659
660
661
662
663
664
        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>{};

Chao Liu's avatar
Chao Liu committed
665
666
667
668
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u}, ranks "
               "{%u %u %u %u %u %u %u}\n",
669
               s,
Chao Liu's avatar
Chao Liu committed
670
               desc.GetNumOfDimension(),
671
672
673
674
675
676
677
678
679
680
681
682
683
               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),
Chao Liu's avatar
Chao Liu committed
684
685
686
687
688
689
690
691
692
693
694
               desc.GetStride(I6),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4),
               desc.GetMemoryRank(I5),
               desc.GetMemoryRank(I6));
    });

    static_if<ndim == 8>{}([&](auto fwd) {
695
696
697
698
699
700
701
702
703
        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>{};

Chao Liu's avatar
Chao Liu committed
704
705
706
707
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u %u}, "
               "ranks {%u %u %u %u %u %u %u %u}\n",
708
               s,
Chao Liu's avatar
Chao Liu committed
709
               desc.GetNumOfDimension(),
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
               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),
Chao Liu's avatar
Chao Liu committed
725
726
727
728
729
730
731
732
733
734
735
736
               desc.GetStride(I7),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4),
               desc.GetMemoryRank(I5),
               desc.GetMemoryRank(I6),
               desc.GetMemoryRank(I7));
    });

    static_if<ndim == 9>{}([&](auto fwd) {
Chao Liu's avatar
Chao Liu committed
737
738
739
740
741
742
743
744
745
746
        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
Chao Liu committed
747
748
        constexpr auto desc = fwd(TDesc{});

Chao Liu's avatar
tidy yp  
Chao Liu committed
749
        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u %u "
Chao Liu's avatar
Chao Liu committed
750
               "%u}, ranks {%u %u %u %u %u %u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
751
               s,
Chao Liu's avatar
Chao Liu committed
752
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
               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),
Chao Liu's avatar
Chao Liu committed
770
771
772
773
774
775
776
777
778
779
780
781
782
               desc.GetStride(I8),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4),
               desc.GetMemoryRank(I5),
               desc.GetMemoryRank(I6),
               desc.GetMemoryRank(I7),
               desc.GetMemoryRank(I8));
    });

    static_if<ndim == 10>{}([&](auto fwd) {
Chao Liu's avatar
Chao Liu committed
783
784
785
786
787
788
789
790
791
792
793
        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
Chao Liu committed
794
795
        constexpr auto desc = fwd(TDesc{});

Chao Liu's avatar
tidy yp  
Chao Liu committed
796
        printf("%s dim %u, lengths {%u %u %u %u %u %u %u %u %u %u}, strides {%u %u %u %u %u %u %u "
Chao Liu's avatar
Chao Liu committed
797
               "%u %u %u}, ranks {%u %u %u %u %u %u %u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
798
               s,
Chao Liu's avatar
Chao Liu committed
799
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
               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),
Chao Liu's avatar
Chao Liu committed
819
820
821
822
823
824
825
826
827
828
829
830
               desc.GetStride(I9),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4),
               desc.GetMemoryRank(I5),
               desc.GetMemoryRank(I6),
               desc.GetMemoryRank(I7),
               desc.GetMemoryRank(I8),
               desc.GetMemoryRank(I9));
    });
Chao Liu's avatar
Chao Liu committed
831
}