| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #pragma once |
|
|
| #include <cute/config.hpp> |
| #include <cute/layout.hpp> |
| #include <cute/layout_composed.hpp> |
| #include <cute/swizzle.hpp> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| namespace cute |
| { |
|
|
| |
| |
| |
| template <int B, int M, int S, class Offset, class LayoutB> |
| struct get_swizzle<ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB>> { using type = Swizzle<B,M,S>; }; |
|
|
| |
| |
| |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_layout(Swizzle<B,M,S> const& sxor) |
| { |
| return composition(sxor, Layout<Int<M+B+abs(S)>,Int<1>>{}); |
| } |
|
|
| namespace detail { |
|
|
| template <int B, int M, int S, class OldShape, class OldStride, class NewShape, class NewStride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| transfer_swizzle(Layout<OldShape,OldStride> const& old_layout, |
| Layout<NewShape,NewStride> const& new_layout) |
| { |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| auto swizzle_only_zy = make_layout(make_shape (Int<(1 << M)>{}, Int<(1 << B)>{}, Int<(1 << (abs(S)-B))>{}, Int<(1 << B )>{}, Int<1>{}), |
| make_stride( Int<0>{}, Int<(1 << M)>{}, Int<0>{}, Int<(1 << (M+abs(S)))>{}, Int<0>{})); |
|
|
| |
| auto layout_only_zy = composition(swizzle_only_zy, old_layout); |
| |
| auto swizzle_active_bits = layout_only_zy(size(layout_only_zy)-Int<1>{}); |
|
|
| |
| auto zzz_msk = typename Swizzle<B,M,S>::zzz_msk{}; |
| auto yyy_msk = typename Swizzle<B,M,S>::yyy_msk{}; |
| auto msk_sft = typename Swizzle<B,M,S>::msk_sft{}; |
| auto active_Z = swizzle_active_bits & shiftr(swizzle_active_bits, msk_sft) & zzz_msk; |
| auto active_Y = swizzle_active_bits & shiftr(swizzle_active_bits, -msk_sft) & yyy_msk; |
|
|
| |
| auto new_active_Z = new_layout(old_layout.get_1d_coord(active_Z)); |
| auto new_active_Y = new_layout(old_layout.get_1d_coord(active_Y)); |
|
|
| |
| |
| return composition(make_swizzle<new_active_Y,new_active_Z>(), new_layout); |
| } |
|
|
| } |
|
|
| template <int B, int M, int S, class Offset, class Layout> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_fragment_like(ComposedLayout<Swizzle<B,M,S>,Offset,Layout> const& layout) |
| { |
| return make_fragment_like(layout.layout_b()); |
| } |
|
|
| |
| |
| |
|
|
| |
| template <int B, int M, int S, class Offset, class LayoutB> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| get_swizzle_portion(ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB>) |
| { |
| return Swizzle<B,M,S>{}; |
| } |
|
|
| |
| template <class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| get_swizzle_portion(Layout<Shape,Stride>) |
| { |
| return Swizzle<0,4,3>{}; |
| } |
|
|
| |
| |
| template <int B, int M, int S, class Offset, class LayoutB> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| get_nonswizzle_portion(ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& slayout) |
| { |
| return slayout.layout_b(); |
| } |
|
|
| |
| template <class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| get_nonswizzle_portion(Layout<Shape,Stride> const& slayout) |
| { |
| return slayout; |
| } |
|
|
| |
| |
| |
|
|
| namespace detail { |
|
|
| template <class IntZ, class IntY, class Offset, int... I> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_swizzle_strides(true_type, |
| IntZ const& Z, |
| IntY const& Y, |
| Offset const& offset, |
| int_sequence<I...>) |
| { |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| return cute::make_tuple(conditional_return((offset & (Y << Int<I>{})) == Int<0>{}, Z * Int<(1 << I)>{}, -Z * Int<(1 << I)>{})...); |
| } |
|
|
| template <class IntZ, class IntY, class Offset, int... I> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_swizzle_strides(false_type, |
| IntZ const& Z, |
| IntY const& Y, |
| Offset const& offset, |
| int_sequence<I...>) |
| { |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| return cute::make_tuple(conditional_return((offset & (Z << Int<I>{})) == Int<0>{}, (Y+Z) * Int<(1 << I)>{}, (Y-Z) * Int<(1 << I)>{})...); |
| } |
|
|
| } |
|
|
| template <class Coord, int B, int M, int S, class Offset, class Layout> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| slice_and_offset(Coord const& coord, ComposedLayout<Swizzle<B,M,S>,Offset,Layout> const& layout) |
| { |
| if constexpr (all_underscore<Coord>::value) { |
| |
| return cute::make_tuple(composition(layout.layout_a(), layout.offset(), slice(coord, layout.layout_b())), Int<0>{}); |
| } else { |
|
|
| |
| auto sw = make_layout(make_shape(Int<(1 << M)>{}, Int<(1 << B)>{}, Int<(1 << (abs(S)-B))>{}, Int<(1 << B)>{}, Int<1>{})); |
|
|
| auto swizzle_anti_zy = make_layout(shape(sw), |
| make_stride(stride<0>(sw), Int<0>{}, stride<2>(sw), Int<0>{}, size(sw))); |
| auto swizzle_only_zy = make_layout(shape(sw), |
| make_stride( Int<0>{}, stride<1>(sw), Int<0>{}, stride<3>(sw), Int<0>{})); |
|
|
| |
| auto sliced_layout = slice(coord, layout.layout_b()); |
|
|
| |
| auto diced_layout = dice(coord, layout.layout_b()); |
| auto diced_coord = dice(coord, coord); |
|
|
| auto diced_layout_anti_zy = composition(swizzle_anti_zy, diced_layout); |
| auto diced_layout_only_zy = composition(swizzle_only_zy, diced_layout); |
|
|
| |
| auto swizzle = layout.layout_a(); |
| |
| |
| auto offset_only_zy = layout.offset() ^ to_mixed_bits(diced_layout_only_zy, diced_coord); |
| |
| auto offset_anti_zy = diced_layout_anti_zy(diced_coord); |
|
|
| |
| |
| |
|
|
| |
|
|
| |
| |
| auto sliced_layout_only_zy = composition(swizzle_only_zy, sliced_layout); |
| |
| [[maybe_unused]] auto swizzle_active_bits = sliced_layout_only_zy(size(sliced_layout_only_zy)-Int<1>{}); |
|
|
| |
| if constexpr (is_constant<0, decltype(not (swizzle_active_bits & ~swizzle(swizzle_active_bits)))>::value) |
| { |
| return cute::make_tuple(composition(swizzle, offset_only_zy, sliced_layout), offset_anti_zy); |
| } else |
| { |
|
|
| |
| auto Z = typename Swizzle<B,M,S>::zzz_msk{} & -typename Swizzle<B,M,S>::zzz_msk{}; |
| auto Y = typename Swizzle<B,M,S>::yyy_msk{} & -typename Swizzle<B,M,S>::yyy_msk{}; |
| auto stride_lo = detail::make_swizzle_strides(Z < Y, Z, Y, offset_only_zy, make_int_sequence<B>{}); |
| auto stride_hi = detail::make_swizzle_strides(Z > Y, Z, Y, offset_only_zy, make_int_sequence<B>{}); |
|
|
| |
| auto swizzle_layout = make_layout(make_shape (Int<(1 << M)>{}, repeat<B>(Int<2>{}), Int<(1 << (abs(S)-B))>{}, repeat<B>(Int<2>{}), Int< 1>{}), |
| make_stride(Int< 1>{}, stride_lo, Int<(1 << (M+B))>{}, stride_hi , Int<(1 << (M+B+abs(S)))>{})); |
|
|
| |
| return cute::make_tuple(composition(swizzle_layout, sliced_layout), |
| swizzle(offset_only_zy) + offset_anti_zy); |
| } |
| } |
|
|
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| |
| |
| |
|
|
| |
| template <int M, int S, |
| class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| composition(Swizzle<0,M,S> const&, |
| Int<0> const&, |
| Layout<Shape,Stride> const& layout) |
| { |
| return layout; |
| } |
|
|
| template <int B, int M, int S, |
| class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| composition(Swizzle<B,M,S> const& sxor, |
| Layout<Shape,Stride> const& layout) |
| { |
| return composition(sxor, Int<0>{}, layout); |
| } |
|
|
| template <class ShapeA, class StrideA, |
| int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| composition(Layout<ShapeA,StrideA> const& a, |
| Swizzle<B,M,S> const& b) |
| { |
| |
| auto active_Y = a(typename Swizzle<B,M,S>::yyy_msk{}); |
| auto active_Z = a(typename Swizzle<B,M,S>::zzz_msk{}); |
|
|
| |
|
|
| return composition(make_swizzle<active_Y,active_Z>(), a); |
| } |
|
|
| |
| |
| |
|
|
| |
| template <int B, int M, int S, class Offset, class Layout> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| right_inverse(ComposedLayout<Swizzle<B,M,S>,Offset,Layout> const& layout) |
| { |
| if constexpr (is_constant<0, Offset>::value) { |
| return composition(right_inverse(layout.layout_b()), layout.layout_a()); |
| } else { |
| return composition(right_inverse(layout.layout_b()), right_inverse(layout.offset()), right_inverse(layout.layout_a())); |
| } |
| } |
|
|
| |
| template <int B, int M, int S, class Offset, class Layout> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| left_inverse(ComposedLayout<Swizzle<B,M,S>,Offset,Layout> const& layout) |
| { |
| if constexpr (is_constant<0, Offset>::value) { |
| return composition(left_inverse(layout.layout_b()), layout.layout_a()); |
| } else { |
| return composition(left_inverse(layout.layout_b()), left_inverse(layout.offset()), left_inverse(layout.layout_a())); |
| } |
| } |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| Swizzle<B,M,S> |
| right_inverse(Swizzle<B,M,S> const& sw) |
| { |
| return sw; |
| } |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| Swizzle<B,M,S> |
| left_inverse(Swizzle<B,M,S> const& sw) |
| { |
| return sw; |
| } |
|
|
| |
| template <class T, __CUTE_REQUIRES(is_integral<T>::value)> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| right_inverse(T const& t) |
| { |
| return -t; |
| } |
|
|
| |
| template <class T, __CUTE_REQUIRES(is_integral<T>::value)> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| left_inverse(T const& t) |
| { |
| return -t; |
| } |
|
|
| |
| |
| |
|
|
| template <int N, int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| upcast(Swizzle<B,M,S> const& swizzle) |
| { |
| static_assert(has_single_bit(N), "N must be a power of two"); |
| constexpr int log2_n = bit_width(uint32_t(N)) - 1; |
| constexpr int NewM = M - log2_n; |
| if constexpr (NewM >= 0) { |
| return Swizzle<B,NewM,S>{}; |
| } else { |
| return Swizzle<cute::max(B+NewM,0), 0, S>{}; |
| } |
|
|
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| template <int N, int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| downcast(Swizzle<B,M,S> const& swizzle) |
| { |
| static_assert(has_single_bit(N), "N must be a power of two"); |
| constexpr int log2_n = bit_width(uint32_t(N)) - 1; |
| return Swizzle<B,(M + log2_n),S>{}; |
| } |
|
|
| template <class OldType, class NewType, |
| int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| recast_layout(Swizzle<B,M,S> const& swizzle) |
| { |
| using scale = decltype(trait_ratio(sizeof_bits<NewType>{}, sizeof_bits<OldType>{})); |
| if constexpr (scale::num == 1 && scale::den == 1) { |
| return swizzle; |
| } |
| else if constexpr (scale::num == 1) { |
| return downcast<scale::den>(swizzle); |
| } |
| else if constexpr (scale::den == 1) { |
| return upcast<scale::num>(swizzle); |
| } |
| else { |
| return downcast<scale::den>(upcast<scale::num>(layout)); |
| } |
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_alignment(Swizzle<B,M,S> const&) |
| { |
| return Int<(1 << M)>{}; |
| } |
|
|
| template <int B, int M, int S, class Offset, class LayoutB> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_alignment(ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& layout) |
| { |
| return gcd(max_alignment(layout.layout_a()), |
| max_alignment(layout.offset()), |
| max_alignment(layout.layout_b())); |
| } |
|
|
| |
| |
| |
|
|
| template <int B, int M, int S, class Offset, class LayoutB, class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_common_layout(ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& a, |
| Layout<Shape,Stride> const& b) |
| { |
| auto common = max_common_layout(a.layout_b(), b); |
| auto base = Int<(1 << M)>{}; |
| if constexpr (base < size(common)) { |
| return common.compose(base); |
| } else { |
| return common; |
| } |
| } |
|
|
| template <class Shape, class Stride, int B, int M, int S, class Offset, class LayoutB> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_common_layout(Layout<Shape,Stride> const& a, |
| ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& b) |
| { |
| return max_common_layout(b, a); |
| } |
|
|
| template <int B, int M, int S, class Offset, class LayoutB, class Shape, class Stride> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_common_vector(ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& a, |
| Layout<Shape,Stride> const& b) |
| { |
| |
| return cute::min(max_common_vector(a.layout_b(), b), Int<(1 << M)>{}); |
| } |
|
|
| template <class Shape, class Stride, int B, int M, int S, class Offset, class LayoutB> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_common_vector(Layout<Shape,Stride> const& a, |
| ComposedLayout<Swizzle<B,M,S>,Offset,LayoutB> const& b) |
| { |
| return max_common_vector(b, a); |
| } |
|
|
| template <int B0, int M0, int S0, class Offset0, class LayoutB0, |
| int B1, int M1, int S1, class Offset1, class LayoutB1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_common_vector(ComposedLayout<Swizzle<B0,M0,S0>,Offset0,LayoutB0> const& a, |
| ComposedLayout<Swizzle<B1,M1,S1>,Offset1,LayoutB1> const& b) |
| { |
| |
| |
| auto vec = max_common_vector(a.layout_b(), b.layout_b()); |
|
|
| |
| if constexpr (Swizzle<B0,M0,S0>{} == Swizzle<B1,M1,S1>{}) { |
| return vec; |
| } else { |
| return cute::min(vec, Int<(1 << M0)>{}, Int<(1 << M1)>{}); |
| } |
|
|
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| |
| |
|
|
| template <class Shape, class Stride, |
| int B, int M, int S, class Offset, class LayoutT> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| logical_product(Layout<Shape,Stride> const& layout, |
| ComposedLayout<Swizzle<B,M,S>,Offset,LayoutT> const& tiler) |
| { |
| CUTE_STATIC_ASSERT_V(tiler.offset() == Int<0>{}, "Require Swizzle offset == 0."); |
| |
| |
| auto new_layout = logical_product(layout, tiler.layout_b()); |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| auto swizzle_only_zy = make_layout(make_shape (Int<(1 << M)>{}, Int<(1 << B)>{}, Int<(1 << (abs(S)-B))>{}, Int<(1 << B )>{}, Int<1>{}), |
| make_stride( Int<0>{}, Int<(1 << M)>{}, Int<0>{}, Int<(1 << (M+abs(S)))>{}, Int<0>{})); |
|
|
| |
| auto layout_only_zy = composition(swizzle_only_zy, tiler.layout_b()); |
| |
| auto swizzle_active_bits = layout_only_zy(size(layout_only_zy)-Int<1>{}); |
| |
| auto active_Z = swizzle_active_bits & typename Swizzle<B,M,S>::zzz_msk{}; |
| auto active_Y = swizzle_active_bits & typename Swizzle<B,M,S>::yyy_msk{}; |
|
|
| |
| auto new_active_Z = new_layout(Int<0>{}, tiler.layout_b()(active_Z)); |
| auto new_active_Y = new_layout(Int<0>{}, tiler.layout_b()(active_Y)); |
|
|
| |
| |
| return composition(make_swizzle<new_active_Y,new_active_Z>(), new_layout); |
| } |
|
|
| } |
|
|