ConstantTensorDescriptor.hip.hpp 29.2 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
8
    return reverse_inclusive_scan_sequence(Lengths{}.PopFront(), std::multiplies<index_t>{})
        .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
    {
Chao Liu's avatar
Chao Liu committed
98
        return accumulate_on_sequence(Lengths{}, std::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
110
111
#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(
            (lengths_in_rank - Number<1>{}) * strides_in_rank, std::plus<index_t>{}, Number<1>{});
#else // WRONG! align shouldbe applied to the last memory rank, not the last tensor dimension
Chao Liu's avatar
Chao Liu committed
112
113
        constexpr index_t element_space_unaligned = accumulate_on_sequence(
            (GetLengths() - Number<1>{}) * GetStrides(), std::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...>{};

Chao Liu's avatar
Chao Liu committed
147
        return accumulate_on_sequence(multi_id * GetStrides(), std::plus<index_t>{}, Number<0>{});
148
149
    }

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

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

158
        // calculate index in each of the dimensions in the order of their rank (not dimension)
159
        static_for<0, nDim - 1, 1>{}([&](auto IDim) {
160
161
162
163
            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;
164
165
        });

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

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

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

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

        // 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;
189
    }
Chao Liu's avatar
Chao Liu committed
190

191
192
193
194
195
196
197
198
199
200
201
202
203
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
    __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
    __host__ __device__ static Array<index_t, nDim>
    UpdateMultiIndexGivenStepSizeOf1dIndex(Array<index_t, nDim> old_multi_id,
                                           index_t step_size_of_1d_index)
    {
        auto new_multi_id = old_multi_id + GetMultiIndexFrom1dIndex(step_size_of_1d_index);

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

        return new_multi_id;
    }

231
    // WRONG! Ranks is broken
Chao Liu's avatar
Chao Liu committed
232
    template <index_t... IDims>
Chao Liu's avatar
Chao Liu committed
233
    __host__ __device__ static constexpr auto Extract(Number<IDims>... extract_dims)
Chao Liu's avatar
Chao Liu committed
234
    {
Chao Liu's avatar
Chao Liu committed
235
236
        static_assert(sizeof...(IDims) <= GetNumOfDimension(),
                      "wrong! too many number of dimensions to be extracted");
Chao Liu's avatar
Chao Liu committed
237

238
239
240
241
242
243
244
245
246
247
248
        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
249
250
    }

Chao Liu's avatar
Chao Liu committed
251
252
253
254
255
256
    template <index_t... IDims>
    __host__ __device__ static constexpr auto Extract(Sequence<IDims...>)
    {
        return Extract(Number<IDims>{}...);
    }

257
258
259
260
261
262
263
264
265
266
267
268
269
    template <class... Ts>
    __host__ __device__ static constexpr auto Inject(ConstantTensorDescriptor<Ts...>)
    {
        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
270
271
272
    template <index_t IDim, index_t SliceLen>
    __host__ __device__ static constexpr auto Slice(Number<IDim>, Number<SliceLen>)
    {
273
274
275
        using slice_lengths = decltype(Lengths{}.Modify(Number<IDim>{}, Number<SliceLen>{}));

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

278
279
280
281
282
283
284
285
286
    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
287
    template <index_t IDim, index_t... FoldIntervals>
Chao Liu's avatar
Chao Liu committed
288
    __host__ __device__ static constexpr auto Fold(Number<IDim>, Number<FoldIntervals>...)
Chao Liu's avatar
Chao Liu committed
289
    {
Chao Liu's avatar
Chao Liu committed
290
291
        constexpr auto fold_intervals = Sequence<FoldIntervals...>{};

Chao Liu's avatar
Chao Liu committed
292
        constexpr index_t fold_intervals_product =
Chao Liu's avatar
Chao Liu committed
293
294
295
296
            accumulate_on_sequence(fold_intervals, std::multiplies<index_t>{}, Number<1>{});

        constexpr auto unfold_length = GetLength(Number<IDim>{});
        constexpr auto unfold_stride = GetStride(Number<IDim>{});
297
        constexpr auto unfold_rank   = GetMemoryRank(Number<IDim>{});
Chao Liu's avatar
Chao Liu committed
298
299
300

        // 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
301
        static_assert(unfold_length % fold_intervals_product == 0,
Chao Liu's avatar
Chao Liu committed
302
303
304
305
                      "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
306
            Sequence<unfold_length / fold_intervals_product>{}.Append(fold_intervals);
Chao Liu's avatar
Chao Liu committed
307
308

        // folded strides
Chao Liu's avatar
Chao Liu committed
309
310
        constexpr auto fold_strides =
            Number<unfold_stride>{} *
Chao Liu's avatar
Chao Liu committed
311
312
            reverse_inclusive_scan_sequence(fold_intervals.PushBack(Number<1>{}),
                                            std::multiplies<index_t>{});
Chao Liu's avatar
Chao Liu committed
313

314
315
316
317
318
        // 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
319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
        // 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
342
343
    }

344
345
346
347
348
349
350
351
352
    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
353
354
355
    template <index_t FirstUnfoldDim, index_t LastUnfoldDim>
    __host__ __device__ static constexpr auto Unfold(Number<FirstUnfoldDim>, Number<LastUnfoldDim>)
    {
Chao Liu's avatar
Chao Liu committed
356
357
358
359
        static_assert(FirstUnfoldDim >= 0 && LastUnfoldDim < nDim &&
                          FirstUnfoldDim <= LastUnfoldDim,
                      "wrong! should have FirstUnfoldDim <= LastUnfoldDim!");

Chao Liu's avatar
Chao Liu committed
360
#if 0 // cannot compile: compiler complain about constexpr
Chao Liu's avatar
Chao Liu committed
361
362
        // 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
363
364
        static_for<FirstUnfoldDim, LastUnfoldDim, 1>{}([&](auto IDim_) {
            constexpr auto IDim    = decltype(IDim_){};
365
366
367
            constexpr auto IDim_p1 = IDim + Number<1>{};

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

372
373
            // check if packed
            static_assert(GetStride(IDim_p1) * GetLength(IDim_p1) == GetStride(IDim),
Chao Liu's avatar
Chao Liu committed
374
                          "wrong! dimensions to be unfolded need to be packed");
375

Chao Liu's avatar
Chao Liu committed
376
            // check ranks
377
378
379
            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
380
        });
Chao Liu's avatar
Chao Liu committed
381
#endif
Chao Liu's avatar
Chao Liu committed
382

Chao Liu's avatar
Chao Liu committed
383
        // left and right
384
385
386
387
388
389
390
        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
391
392
393
394
395
        constexpr index_t unfold_length = accumulate_on_sequence(
            GetLengths().Extract(middle), std::multiplies<index_t>{}, Number<1>{});

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

396
397
398
399
        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
400
401
402
            transform_sequences(f_unfold_impl<GetMemoryRank(Number<LastUnfoldDim>{}),
                                              LastUnfoldDim - FirstUnfoldDim + 1>{},
                                GetMemoryRanks());
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

        // 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
431
432
    }

433
434
435
#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
436
    {
437
438
439
440
        return ConstantTensorDescriptor<decltype(Lengths{}.ReorderGivenOld2New(MapOld2New{})),
                                        decltype(Strides{}.ReorderGivenOld2New(MapOld2New{})),
                                        decltype(
                                            MemoryRanks{}.ReorderGivenOld2New(MapOld2New{}))>{};
Chao Liu's avatar
Chao Liu committed
441
    }
442
#endif
Chao Liu's avatar
Chao Liu committed
443
};
Chao Liu's avatar
Chao Liu committed
444
445

template <class Lengths>
Chao Liu's avatar
Chao Liu committed
446
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank_packed(Lengths)
Chao Liu's avatar
Chao Liu committed
447
{
Chao Liu's avatar
Chao Liu committed
448
    using Strides     = decltype(calculate_tensor_strides_default_rank_packed(Lengths{}));
449
450
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
451
452
453
}

template <class Lengths, class Strides>
Chao Liu's avatar
Chao Liu committed
454
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank(Lengths, Strides)
Chao Liu's avatar
Chao Liu committed
455
{
456
457
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
458
459
}

Chao Liu's avatar
Chao Liu committed
460
template <class Lengths, index_t Align>
Chao Liu's avatar
Chao Liu committed
461
462
__host__ __device__ constexpr auto make_ConstantTensorDescriptor_default_rank_aligned(Lengths,
                                                                                      Number<Align>)
Chao Liu's avatar
Chao Liu committed
463
{
464
    using Strides =
Chao Liu's avatar
Chao Liu committed
465
        decltype(calculate_tensor_strides_default_rank_aligned(Lengths{}, Number<Align>{}));
466
467
    using MemoryRanks = typename arithmetic_sequence_gen<0, Lengths::GetSize(), 1>::SeqType;
    return ConstantTensorDescriptor<Lengths, Strides, MemoryRanks>{};
Chao Liu's avatar
Chao Liu committed
468
469
}

Chao Liu's avatar
Chao Liu committed
470
template <class TDesc>
Chao Liu's avatar
Chao Liu committed
471
__host__ __device__ void print_ConstantTensorDescriptor(TDesc, const char* s)
Chao Liu's avatar
Chao Liu committed
472
{
Chao Liu's avatar
Chao Liu committed
473
    constexpr index_t ndim = TDesc::GetNumOfDimension();
Chao Liu's avatar
Chao Liu committed
474

Chao Liu's avatar
Chao Liu committed
475
    static_assert(ndim >= 2 && ndim <= 10, "wrong!");
Chao Liu's avatar
Chao Liu committed
476

Chao Liu's avatar
Chao Liu committed
477
    static_if<ndim == 2>{}([&](auto fwd) {
Chao Liu's avatar
Chao Liu committed
478
479
480
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};

Chao Liu's avatar
Chao Liu committed
481
482
483
        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
484
               s,
Chao Liu's avatar
Chao Liu committed
485
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
486
487
488
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetStride(I0),
Chao Liu's avatar
Chao Liu committed
489
490
491
492
493
494
               desc.GetStride(I1),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1));
    });

    static_if<ndim == 3>{}([&](auto fwd) {
495
496
497
498
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};

Chao Liu's avatar
Chao Liu committed
499
500
501
        constexpr auto desc = fwd(TDesc{});

        printf("%s dim %u, lengths {%u %u %u}, strides {%u %u %u}, ranks {%u %u %u}\n",
502
               s,
Chao Liu's avatar
Chao Liu committed
503
               desc.GetNumOfDimension(),
504
505
506
507
508
               desc.GetLength(I0),
               desc.GetLength(I1),
               desc.GetLength(I2),
               desc.GetStride(I0),
               desc.GetStride(I1),
Chao Liu's avatar
Chao Liu committed
509
510
511
512
513
514
515
               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
516
517
518
519
520
        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
521
522
523
        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
524
               s,
Chao Liu's avatar
Chao Liu committed
525
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
526
527
528
529
530
531
532
               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
533
534
535
536
537
538
539
540
               desc.GetStride(I3),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3));
    });

    static_if<ndim == 5>{}([&](auto fwd) {
541
542
543
544
545
546
        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
547
548
549
550
        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",
551
               s,
Chao Liu's avatar
Chao Liu committed
552
               desc.GetNumOfDimension(),
553
554
555
556
557
558
559
560
561
               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
562
563
564
565
566
567
568
569
570
               desc.GetStride(I4),
               desc.GetMemoryRank(I0),
               desc.GetMemoryRank(I1),
               desc.GetMemoryRank(I2),
               desc.GetMemoryRank(I3),
               desc.GetMemoryRank(I4));
    });

    static_if<ndim == 6>{}([&](auto fwd) {
571
572
573
574
575
576
577
        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
578
579
580
581
        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",
582
               s,
Chao Liu's avatar
Chao Liu committed
583
               desc.GetNumOfDimension(),
584
585
586
587
588
589
590
591
592
593
594
               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
595
596
597
598
599
600
601
602
603
604
               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) {
605
606
607
608
609
610
611
612
        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
613
614
615
616
        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",
617
               s,
Chao Liu's avatar
Chao Liu committed
618
               desc.GetNumOfDimension(),
619
620
621
622
623
624
625
626
627
628
629
630
631
               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
632
633
634
635
636
637
638
639
640
641
642
               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) {
643
644
645
646
647
648
649
650
651
        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
652
653
654
655
        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",
656
               s,
Chao Liu's avatar
Chao Liu committed
657
               desc.GetNumOfDimension(),
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
               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
673
674
675
676
677
678
679
680
681
682
683
684
               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
685
686
687
688
689
690
691
692
693
694
        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
695
696
        constexpr auto desc = fwd(TDesc{});

Chao Liu's avatar
tidy yp  
Chao Liu committed
697
        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
698
               "%u}, ranks {%u %u %u %u %u %u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
699
               s,
Chao Liu's avatar
Chao Liu committed
700
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
               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
718
719
720
721
722
723
724
725
726
727
728
729
730
               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
731
732
733
734
735
736
737
738
739
740
741
        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
742
743
        constexpr auto desc = fwd(TDesc{});

Chao Liu's avatar
tidy yp  
Chao Liu committed
744
        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
745
               "%u %u %u}, ranks {%u %u %u %u %u %u %u %u %u %u}\n",
Chao Liu's avatar
Chao Liu committed
746
               s,
Chao Liu's avatar
Chao Liu committed
747
               desc.GetNumOfDimension(),
Chao Liu's avatar
Chao Liu committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
               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
767
768
769
770
771
772
773
774
775
776
777
778
               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
779
}