From 953f87b61386f4acf22d307d42e3243c598f2d3a Mon Sep 17 00:00:00 2001 From: songzhl Date: Mon, 16 Nov 2026 11:28:54 +0800 Subject: [PATCH 01/18] feat: add native Hygon operators for DeepSeek V4 Flash --- cmake/NativeFlashMlaHygon.cmake | 43 + src/CMakeLists.txt | 12 + src/base/deepseek_v4_c4_index.h | 14 + src/base/deepseek_v4_cast_rz.h | 13 + src/base/deepseek_v4_compress.h | 31 + src/base/deepseek_v4_mhc_post.h | 12 + src/base/deepseek_v4_mhc_pre.h | 14 + src/base/deepseek_v4_rotary_frequencies.h | 13 + src/base/deepseek_v4_route.h | 14 + src/base/deepseek_v4_sparse_mla.h | 39 + src/base/deepseek_v4_store_mla.h | 24 + src/base/deepseek_v4_w8a8_experts.h | 39 + src/native/cuda/hygon/dynamic_int8_quant.cuh | 38 + src/native/cuda/hygon/flash_mla/bridge.hip | 167 + src/native/cuda/hygon/flash_mla/bridge.hpp | 44 + src/native/cuda/hygon/ops/cast/kernel.h | 120 + .../hygon/ops/deepseek_v4_c4_index/kernel.h | 182 + .../hygon/ops/deepseek_v4_cast_rz/kernel.h | 39 + .../hygon/ops/deepseek_v4_compress/kernel.h | 194 + .../hygon/ops/deepseek_v4_mhc_post/kernel.h | 48 + .../hygon/ops/deepseek_v4_mhc_pre/head.cuh | 45 + .../hygon/ops/deepseek_v4_mhc_pre/kernel.h | 247 + .../deepseek_v4_rotary_frequencies/kernel.h | 60 + .../cuda/hygon/ops/deepseek_v4_route/kernel.h | 110 + .../hygon/ops/deepseek_v4_sparse_mla/kernel.h | 334 + .../hygon/ops/deepseek_v4_store_mla/kernel.h | 111 + .../ops/deepseek_v4_w8a8_experts/kernel.h | 237 + src/native/cuda/hygon/ops/i8_gemm/kernel.h | 80 + .../hygon/ops/per_channel_quant_i8/kernel.h | 39 + .../flashmla_hygon/CUTLASS_LICENSE.txt | 27 + third_party/flashmla_hygon/LICENSE | 21 + third_party/flashmla_hygon/README.md | 15 + .../include/cute/algorithm/axpby_hip.hpp | 92 + .../include/cute/algorithm/clear_hip.hpp | 72 + .../include/cute/algorithm/copy_hip.hpp | 324 + .../include/cute/algorithm/fill_hip.hpp | 93 + .../include/cute/algorithm/functional_hip.hpp | 204 + .../include/cute/algorithm/gemm_hip.hpp | 746 + .../include/cute/algorithm/prefer.hpp | 46 + .../cute/algorithm/tensor_algorithms_hip.hpp | 164 + .../cute/algorithm/tuple_algorithms_hip.hpp | 1119 + .../include/cute/arch/cluster_sm90_hip.hpp | 261 + .../include/cute/arch/copy_gfx928_hip.hpp | 292 + .../include/cute/arch/copy_hip.hpp | 77 + .../include/cute/arch/copy_sm75_hip.hpp | 242 + .../include/cute/arch/copy_sm80_hip.hpp | 144 + .../include/cute/arch/copy_sm90_desc_hip.hpp | 211 + .../include/cute/arch/copy_sm90_hip.hpp | 231 + .../include/cute/arch/copy_sm90_tma_hip.hpp | 855 + .../include/cute/arch/mma_gfx906_hip.hpp | 90 + .../include/cute/arch/mma_gfx926_hip.hpp | 158 + .../include/cute/arch/mma_gfx928_hip.hpp | 2326 ++ .../include/cute/arch/mma_gfx938_hip.hpp | 1249 + .../include/cute/arch/mma_hip.hpp | 70 + .../include/cute/arch/mma_sm61_hip.hpp | 93 + .../include/cute/arch/mma_sm70_hip.hpp | 335 + .../include/cute/arch/mma_sm75_hip.hpp | 126 + .../include/cute/arch/mma_sm80_hip.hpp | 2149 ++ .../include/cute/arch/mma_sm90_desc_hip.hpp | 163 + .../include/cute/arch/mma_sm90_gmma_hip.hpp | 20645 ++++++++++++++++ .../include/cute/arch/mma_sm90_hip.hpp | 1257 + .../include/cute/arch/util_hip.hpp | 263 + .../include/cute/atom/copy_atom_hip.hpp | 787 + .../cute/atom/copy_traits_gfx928_hip.hpp | 279 + .../include/cute/atom/copy_traits_hip.hpp | 176 + .../cute/atom/copy_traits_sm75_hip.hpp | 149 + .../cute/atom/copy_traits_sm80_hip.hpp | 104 + .../cute/atom/copy_traits_sm90_hip.hpp | 138 + .../cute/atom/copy_traits_sm90_tma_hip.hpp | 1033 + .../atom/copy_traits_sm90_tma_swizzle_hip.hpp | 76 + .../include/cute/atom/mma_atom_hip.hpp | 1075 + .../cute/atom/mma_traits_gfx906_hip.hpp | 82 + .../cute/atom/mma_traits_gfx926_hip.hpp | 116 + .../cute/atom/mma_traits_gfx928_hip.hpp | 602 + .../cute/atom/mma_traits_gfx938_hip.hpp | 375 + .../include/cute/atom/mma_traits_hip.hpp | 234 + .../include/cute/atom/mma_traits_sm61_hip.hpp | 79 + .../include/cute/atom/mma_traits_sm70_hip.hpp | 204 + .../include/cute/atom/mma_traits_sm75_hip.hpp | 87 + .../include/cute/atom/mma_traits_sm80_hip.hpp | 452 + .../cute/atom/mma_traits_sm90_gmma_hip.hpp | 5929 +++++ .../include/cute/atom/mma_traits_sm90_hip.hpp | 138 + .../cutlass_3.2.1/include/cute/config_hip.hpp | 184 + .../include/cute/container/alignment_hip.hpp | 76 + .../cute/container/array_aligned_hip.hpp | 48 + .../include/cute/container/array_hip.hpp | 496 + .../cute/container/array_subbyte_hip.hpp | 577 + .../include/cute/container/bit_field_hip.hpp | 145 + .../include/cute/container/hip_types.hpp | 193 + .../include/cute/container/tuple_hip.hpp | 740 + .../include/cute/container/type_list_hip.hpp | 142 + .../include/cute/int_tuple_hip.hpp | 932 + .../include/cute/layout_composed_hip.hpp | 615 + .../cutlass_3.2.1/include/cute/layout_hip.hpp | 1826 ++ .../cute/numeric/arithmetic_tuple_hip.hpp | 552 + .../include/cute/numeric/bfloat_hip.hpp | 59 + .../include/cute/numeric/complex_hip.hpp | 79 + .../include/cute/numeric/float8_hip.hpp | 49 + .../include/cute/numeric/half_hip.hpp | 47 + .../include/cute/numeric/int_hip.hpp | 152 + .../cute/numeric/integer_sequence_hip.hpp | 157 + .../cute/numeric/integer_subbyte_hip.hpp | 239 + .../cute/numeric/integral_constant_hip.hpp | 437 + .../cute/numeric/integral_ratio_hip.hpp | 181 + .../include/cute/numeric/math_hip.hpp | 317 + .../include/cute/numeric/real_hip.hpp | 62 + .../include/cute/numeric/tfloat_hip.hpp | 59 + .../include/cute/numeric/uint128_hip.hpp | 265 + .../include/cute/pointer_hip.hpp | 399 + .../cutlass_3.2.1/include/cute/stride_hip.hpp | 458 + .../include/cute/swizzle_hip.hpp | 559 + .../include/cute/swizzle_layout_hip.hpp | 454 + .../include/cute/swizzle_ptr_hip.hpp | 309 + .../cutlass_3.2.1/include/cute/tensor_hip.hpp | 1027 + .../include/cute/tensor_predicate_hip.hpp | 69 + .../cutlass_3.2.1/include/cute/tile_hip.hpp | 64 + .../include/cute/underscore_hip.hpp | 156 + .../include/cute/util/debug_hip.hpp | 170 + .../include/cute/util/print_hip.hpp | 204 + .../include/cute/util/type_traits_hip.hpp | 247 + .../include/cutlass/arch/barrier_hip.h | 510 + .../include/cutlass/arch/memory_sm75_hip.h | 336 + .../cutlass_3.2.1/include/cutlass/array_hip.h | 2594 ++ .../include/cutlass/array_subbyte_hip.h | 579 + .../cutlass_3.2.1/include/cutlass/bfloat16.h | 539 + .../include/cutlass/complex_hip.h | 728 + .../cutlass_3.2.1/include/cutlass/coord.h | 490 + .../cutlass_3.2.1/include/cutlass/cutlass.h | 228 + .../include/cutlass/detail/helper_macros.hpp | 194 + .../include/cutlass/fast_math_hip.h | 1020 + .../cutlass_3.2.1/include/cutlass/float8.h | 1516 ++ .../include/cutlass/floating_point_nvrtc.h | 65 + .../include/cutlass/functional.h | 627 + .../cutlass_3.2.1/include/cutlass/half.h | 935 + .../include/cutlass/integer_subbyte.h | 254 + .../include/cutlass/layout/matrix_hip.h | 1365 + .../include/cutlass/matrix_coord.h | 164 + .../include/cutlass/numeric_conversion_hip.h | 2685 ++ .../include/cutlass/numeric_size.h | 93 + .../include/cutlass/numeric_types.h | 82 + .../include/cutlass/pitch_linear_coord.h | 181 + .../include/cutlass/platform/platform.h | 972 + .../cutlass_3.2.1/include/cutlass/real.h | 61 + .../cutlass_3.2.1/include/cutlass/tfloat32.h | 477 + .../cutlass/transform/thread/unary_op_hip.h | 111 + .../cutlass_3.2.1/include/cutlass/uint128.h | 270 + third_party/flashmla_hygon/csrc/defines_hip.h | 32 + .../csrc/gfx9/decode/combine/combine.hip | 328 + .../csrc/gfx9/decode/combine/combine_hip.h | 16 + .../get_decoding_sched_meta.hip | 175 + .../get_decoding_sched_meta_hip.h | 16 + .../decode/sparse_fp8/components/config_hip.h | 27 + .../sparse_fp8/components/dequant_hip.h | 133 + .../sparse_fp8/components/helpers_hip.h | 115 + .../csrc/gfx93/decode/sparse_fp8/config_hip.h | 126 + .../instantiations/model1_persistent_h64.hip | 14 + .../decode/sparse_fp8/splitkv_mla_hip.cuh | 1018 + .../gfx93/decode/sparse_fp8/splitkv_mla_hip.h | 17 + .../kerutils/include/kerutils/common/common.h | 8 + .../include/kerutils/device/common_hip.h | 76 + .../include/kerutils/device/device_hip.cuh | 19 + .../kerutils/include/kerutils/host/host_hip.h | 161 + .../include/kerutils/kerutils_hip.cuh | 10 + third_party/flashmla_hygon/csrc/params_hip.h | 208 + third_party/flashmla_hygon/csrc/softmax_hip.h | 704 + third_party/flashmla_hygon/csrc/utils_hip.h | 1674 ++ third_party/flashmla_hygon/sources.json | 548 + 167 files changed, 83884 insertions(+) create mode 100644 cmake/NativeFlashMlaHygon.cmake create mode 100644 src/base/deepseek_v4_c4_index.h create mode 100644 src/base/deepseek_v4_cast_rz.h create mode 100644 src/base/deepseek_v4_compress.h create mode 100644 src/base/deepseek_v4_mhc_post.h create mode 100644 src/base/deepseek_v4_mhc_pre.h create mode 100644 src/base/deepseek_v4_rotary_frequencies.h create mode 100644 src/base/deepseek_v4_route.h create mode 100644 src/base/deepseek_v4_sparse_mla.h create mode 100644 src/base/deepseek_v4_store_mla.h create mode 100644 src/base/deepseek_v4_w8a8_experts.h create mode 100644 src/native/cuda/hygon/dynamic_int8_quant.cuh create mode 100644 src/native/cuda/hygon/flash_mla/bridge.hip create mode 100644 src/native/cuda/hygon/flash_mla/bridge.hpp create mode 100644 src/native/cuda/hygon/ops/cast/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_c4_index/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_cast_rz/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_compress/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/head.cuh create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_rotary_frequencies/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_sparse_mla/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_store_mla/kernel.h create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h create mode 100644 src/native/cuda/hygon/ops/i8_gemm/kernel.h create mode 100644 src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h create mode 100644 third_party/flashmla_hygon/CUTLASS_LICENSE.txt create mode 100644 third_party/flashmla_hygon/LICENSE create mode 100644 third_party/flashmla_hygon/README.md create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/axpby_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/clear_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/copy_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/fill_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/functional_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/gemm_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/prefer.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tensor_algorithms_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tuple_algorithms_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/cluster_sm90_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_gfx928_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm75_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm80_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_desc_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_tma_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx906_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx926_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx928_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx938_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm61_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm70_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm75_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm80_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_desc_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_gmma_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/util_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_atom_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_gfx928_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm75_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm80_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_swizzle_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_atom_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx906_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx926_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx928_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx938_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm61_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm70_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm75_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm80_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_gmma_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/config_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/alignment_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_aligned_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_subbyte_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/bit_field_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/hip_types.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/tuple_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/type_list_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/int_tuple_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_composed_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/arithmetic_tuple_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/bfloat_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/complex_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/float8_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/half_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/int_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_sequence_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_subbyte_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_constant_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_ratio_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/math_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/real_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/tfloat_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/uint128_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/pointer_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/stride_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_layout_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_ptr_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_predicate_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tile_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/underscore_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/debug_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/print_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/type_traits_hip.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/barrier_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/memory_sm75_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_subbyte_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/bfloat16.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/complex_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/coord.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/cutlass.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/detail/helper_macros.hpp create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/fast_math_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/float8.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/floating_point_nvrtc.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/functional.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/half.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/integer_subbyte.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/layout/matrix_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/matrix_coord.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_conversion_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_size.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_types.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/pitch_linear_coord.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/platform/platform.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/real.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/tfloat32.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/transform/thread/unary_op_hip.h create mode 100644 third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/uint128.h create mode 100644 third_party/flashmla_hygon/csrc/defines_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine.hip create mode 100644 third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip create mode 100644 third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/config_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/dequant_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/helpers_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/config_hip.h create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.cuh create mode 100644 third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.h create mode 100644 third_party/flashmla_hygon/csrc/kerutils/include/kerutils/common/common.h create mode 100644 third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/common_hip.h create mode 100644 third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/device_hip.cuh create mode 100644 third_party/flashmla_hygon/csrc/kerutils/include/kerutils/host/host_hip.h create mode 100644 third_party/flashmla_hygon/csrc/kerutils/include/kerutils/kerutils_hip.cuh create mode 100644 third_party/flashmla_hygon/csrc/params_hip.h create mode 100644 third_party/flashmla_hygon/csrc/softmax_hip.h create mode 100644 third_party/flashmla_hygon/csrc/utils_hip.h create mode 100644 third_party/flashmla_hygon/sources.json diff --git a/cmake/NativeFlashMlaHygon.cmake b/cmake/NativeFlashMlaHygon.cmake new file mode 100644 index 000000000..f18b06a87 --- /dev/null +++ b/cmake/NativeFlashMlaHygon.cmake @@ -0,0 +1,43 @@ +# Build the pinned Hygon FlashMLA kernels with their HIP compiler mode. +function(infiniops_add_hygon_flashmla target) + set(_root "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/..") + set(_source "${_root}/third_party/flashmla_hygon/csrc") + set(_bridge "${_root}/src/native/cuda/hygon/flash_mla") + find_program(_flashmla_aicc NAMES aicc HINTS "${DTK_ROOT}/bin" REQUIRED) + set(_hip_sources + "${_source}/gfx9/decode/combine/combine.hip" + "${_source}/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip" + "${_source}/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip" + "${_bridge}/bridge.hip") + file(GLOB_RECURSE _headers CONFIGURE_DEPENDS "${_source}/*.h" "${_source}/*.hpp" "${_source}/*.cuh") + set(_objects) + foreach(_src IN LISTS _hip_sources) + get_filename_component(_name "${_src}" NAME_WE) + set(_object "${CMAKE_CURRENT_BINARY_DIR}/hygon-flashmla/${_name}.o") + add_custom_command(OUTPUT "${_object}" + COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/hygon-flashmla" + COMMAND "${CMAKE_COMMAND}" -E env CPLUS_INCLUDE_PATH= C_INCLUDE_PATH= CPATH= + "${_flashmla_aicc}" --rocm-path=${DTK_ROOT} --hip-path=${DTK_ROOT}/hip + -c "${_src}" -o "${_object}" + -O3 -std=c++20 -fPIC -w -D__HIP_PLATFORM_AMD__ -D__HIP_PLATFORM_HCC__ + -DUSE_ROCM -DFLASH_MLA_BF16_TYPE=1 -DHIP_ENABLE_WARP_SYNC_BUILTINS=1 -DDCU_ASM + -ffast-math -fno-finite-math-only --offload-arch=${HYGON_ARCH} + -mcode-object-version=5 -fno-gpu-rdc --gpu-max-threads-per-block=256 + -mllvm=-support-768-vgprs=true -mllvm=-disable-machine-sink -mllvm=-disable-code-sink + -mllvm=-amdgpu-enable-rewrite-partial-reg-uses=false + -mllvm=-allow-gvn-convergent-call=true -mllvm=-disallow-uniform-vmed3-combine=true + -mllvm=-hcu-pre-emit-load-store-opt=false -mllvm=-amdgpu-early-inline-all=true + -mllvm=-amdgpu-function-calls=false + "-I${_source}" "-I${_source}/kerutils/include" "-I${_source}/gfx93" + "-I${_source}/cutlass/cutlass_3.2.1/include" "-I${DTK_ROOT}/include" "-I${_bridge}" + DEPENDS "${_src}" "${_bridge}/bridge.hpp" ${_headers} + VERBATIM) + list(APPEND _objects "${_object}") + endforeach() + add_custom_target(${target} DEPENDS ${_objects}) + set_property(TARGET ${target} PROPERTY INFINIOPS_OBJECTS "${_objects}") + install(FILES "${_root}/third_party/flashmla_hygon/LICENSE" + DESTINATION share/infiniops/licenses RENAME FlashMLA-Hygon.txt) + install(FILES "${_root}/third_party/flashmla_hygon/CUTLASS_LICENSE.txt" + DESTINATION share/infiniops/licenses RENAME FlashMLA-CUTLASS.txt) +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e685b4f12..07042b7fb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -310,6 +310,18 @@ if(WITH_HYGON) CUDA_STANDARD_REQUIRED ON ) + if(HYGON_ARCH MATCHES "^gfx93[68]$") + include("${PROJECT_SOURCE_DIR}/cmake/NativeFlashMlaHygon.cmake") + infiniops_add_hygon_flashmla(infiniops_hygon_flashmla) + add_dependencies(infiniops infiniops_hygon_flashmla) + get_target_property(_flashmla_objects infiniops_hygon_flashmla INFINIOPS_OBJECTS) + set_source_files_properties(${_flashmla_objects} PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE) + target_sources(infiniops PRIVATE ${_flashmla_objects}) + target_compile_definitions(infiniops PRIVATE INFINIOPS_HAVE_HYGON_FLASHMLA=1) + target_link_directories(infiniops PRIVATE "${DTK_ROOT}/lib") + target_link_libraries(infiniops PRIVATE amdhip64) + endif() + list(APPEND DEVICE_LIST "hygon") endif() diff --git a/src/base/deepseek_v4_c4_index.h b/src/base/deepseek_v4_c4_index.h new file mode 100644 index 000000000..958140905 --- /dev/null +++ b/src/base/deepseek_v4_c4_index.h @@ -0,0 +1,14 @@ +#pragma once +#include "operator.h" + +namespace infini::ops { +class DeepseekV4C4Index : public Operator { + public: + DeepseekV4C4Index(const Tensor key, const Tensor slots, const Tensor query, + const Tensor weights, const Tensor pages, const Tensor lengths, + Tensor cache, Tensor out) {} + virtual void operator()(const Tensor key, const Tensor slots, const Tensor query, + const Tensor weights, const Tensor pages, const Tensor lengths, + Tensor cache, Tensor out) const = 0; +}; +} diff --git a/src/base/deepseek_v4_cast_rz.h b/src/base/deepseek_v4_cast_rz.h new file mode 100644 index 000000000..059d2895a --- /dev/null +++ b/src/base/deepseek_v4_cast_rz.h @@ -0,0 +1,13 @@ +#pragma once + +#include "operator.h" + +namespace infini::ops { +// Explicit HCU writeback boundary for FP32 TP sums. Other casts retain their +// normal rounding semantics. +class DeepseekV4CastRz : public Operator { + public: + DeepseekV4CastRz(const Tensor input, Tensor out) { (void)input; (void)out; } + virtual void operator()(const Tensor input, Tensor out) const = 0; +}; +} // namespace infini::ops diff --git a/src/base/deepseek_v4_compress.h b/src/base/deepseek_v4_compress.h new file mode 100644 index 000000000..daf32f83b --- /dev/null +++ b/src/base/deepseek_v4_compress.h @@ -0,0 +1,31 @@ +#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_COMPRESS_H_ +#define INFINI_OPS_BASE_DEEPSEEK_V4_COMPRESS_H_ + +#include "operator.h" + +namespace infini::ops { + +// sources encodes input rows (>=0), old state rows (-2-row), or padding (-1). +// C4 has 8 sources ordered previous group then current group. C128 has 128. +// C4 APE can be the prepared [8, dim] or raw checkpoint [4, 2 * dim]. +// An all-padding output row is zero. state_slots uses unique nonnegative +// destinations, or -1. Pooling observes the state from before the launch. +class DeepseekV4Compress : public Operator { + public: + DeepseekV4Compress(const Tensor projection, const Tensor ape, const Tensor sources, + const Tensor state_slots, Tensor state, Tensor out) { + (void)projection; + (void)ape; + (void)sources; + (void)state_slots; + (void)state; + (void)out; + } + + virtual void operator()(const Tensor projection, const Tensor ape, const Tensor sources, + const Tensor state_slots, Tensor state, Tensor out) const = 0; +}; + +} // namespace infini::ops + +#endif diff --git a/src/base/deepseek_v4_mhc_post.h b/src/base/deepseek_v4_mhc_post.h new file mode 100644 index 000000000..9b67e10a6 --- /dev/null +++ b/src/base/deepseek_v4_mhc_post.h @@ -0,0 +1,12 @@ +#pragma once +#include "operator.h" + +namespace infini::ops { +class DeepseekV4MhcPost : public Operator { + public: + DeepseekV4MhcPost(const Tensor activation,const Tensor residual,const Tensor post, + const Tensor combination,Tensor out) {} + virtual void operator()(const Tensor activation,const Tensor residual,const Tensor post, + const Tensor combination,Tensor out) const = 0; +}; +} diff --git a/src/base/deepseek_v4_mhc_pre.h b/src/base/deepseek_v4_mhc_pre.h new file mode 100644 index 000000000..63da5d9f6 --- /dev/null +++ b/src/base/deepseek_v4_mhc_pre.h @@ -0,0 +1,14 @@ +#pragma once +#include "operator.h" + +namespace infini::ops { +class DeepseekV4MhcPre : public Operator { + public: + DeepseekV4MhcPre(const Tensor residual,const Tensor projection,const Tensor scales, + const Tensor bias,double norm_eps,double mix_eps,int64_t iterations, + Tensor collapsed,Tensor post,Tensor combination) {} + virtual void operator()(const Tensor residual,const Tensor projection,const Tensor scales, + const Tensor bias,double norm_eps,double mix_eps,int64_t iterations, + Tensor collapsed,Tensor post,Tensor combination) const = 0; +}; +} diff --git a/src/base/deepseek_v4_rotary_frequencies.h b/src/base/deepseek_v4_rotary_frequencies.h new file mode 100644 index 000000000..d648a86c9 --- /dev/null +++ b/src/base/deepseek_v4_rotary_frequencies.h @@ -0,0 +1,13 @@ +#pragma once +#include "operator.h" +namespace infini::ops { +class DeepseekV4RotaryFrequencies : public Operator { + public: + DeepseekV4RotaryFrequencies(Tensor out,double theta,bool scaled,double low,double high, + double factor,bool split_cos_sin) { + (void)out;(void)theta;(void)scaled;(void)low;(void)high;(void)factor;(void)split_cos_sin; + } + virtual void operator()(Tensor out,double theta,bool scaled,double low,double high, + double factor,bool split_cos_sin) const = 0; +}; +} // namespace infini::ops diff --git a/src/base/deepseek_v4_route.h b/src/base/deepseek_v4_route.h new file mode 100644 index 000000000..d625d8b95 --- /dev/null +++ b/src/base/deepseek_v4_route.h @@ -0,0 +1,14 @@ +#pragma once +#include "operator.h" + +namespace infini::ops { +class DeepseekV4Route : public Operator { + public: + DeepseekV4Route(const Tensor logits,const std::optional bias, + const std::optional tokens,const std::optional hash, + double scale,Tensor weights,Tensor indices) {} + virtual void operator()(const Tensor logits,const std::optional bias, + const std::optional tokens,const std::optional hash, + double scale,Tensor weights,Tensor indices) const = 0; +}; +} diff --git a/src/base/deepseek_v4_sparse_mla.h b/src/base/deepseek_v4_sparse_mla.h new file mode 100644 index 000000000..fee99aee4 --- /dev/null +++ b/src/base/deepseek_v4_sparse_mla.h @@ -0,0 +1,39 @@ +#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_SPARSE_MLA_H_ +#define INFINI_OPS_BASE_DEEPSEEK_V4_SPARSE_MLA_H_ + +#include "operator.h" + +namespace infini::ops { + +// Reference FlashMLA ABI: queries [rows, heads, 512]; KV pages are packed +// FP8 NoPE plus BF16 RoPE and separate per-token exponent bytes. +class DeepseekV4SparseMla : public Operator { + public: + DeepseekV4SparseMla(const Tensor query, const Tensor swa, const Tensor indices, + const Tensor lengths, const Tensor sink, const std::optional extra, + const std::optional extra_indices, const std::optional extra_lengths, + int64_t extra_page_size, double scale, Tensor out) { + (void)query; + (void)swa; + (void)indices; + (void)lengths; + (void)sink; + (void)extra; + (void)extra_indices; + (void)extra_lengths; + (void)extra_page_size; + (void)scale; + (void)out; + } + + virtual void operator()(const Tensor query, const Tensor swa, const Tensor indices, + const Tensor lengths, const Tensor sink, + const std::optional extra, + const std::optional extra_indices, + const std::optional extra_lengths, + int64_t extra_page_size, double scale, Tensor out) const = 0; +}; + +} // namespace infini::ops + +#endif diff --git a/src/base/deepseek_v4_store_mla.h b/src/base/deepseek_v4_store_mla.h new file mode 100644 index 000000000..d6645e2c0 --- /dev/null +++ b/src/base/deepseek_v4_store_mla.h @@ -0,0 +1,24 @@ +#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_STORE_MLA_H_ +#define INFINI_OPS_BASE_DEEPSEEK_V4_STORE_MLA_H_ + +#include "operator.h" + +namespace infini::ops { + +// Store BF16 or FP32 KV rows into DeepSeek V4's packed page format. Nonnegative +// slots must be unique within a launch; -1 skips a row. +class DeepseekV4StoreMla : public Operator { + public: + DeepseekV4StoreMla(const Tensor kv, const Tensor slots, int64_t page_tokens, Tensor pages) { + (void)kv; + (void)slots; + (void)page_tokens; + (void)pages; + } + + virtual void operator()(const Tensor kv, const Tensor slots, int64_t page_tokens, + Tensor pages) const = 0; +}; +} // namespace infini::ops + +#endif diff --git a/src/base/deepseek_v4_w8a8_experts.h b/src/base/deepseek_v4_w8a8_experts.h new file mode 100644 index 000000000..eea02b47c --- /dev/null +++ b/src/base/deepseek_v4_w8a8_experts.h @@ -0,0 +1,39 @@ +#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_W8A8_EXPERTS_H_ +#define INFINI_OPS_BASE_DEEPSEEK_V4_W8A8_EXPERTS_H_ + +#include "operator.h" + +namespace infini::ops { + +// Reference W8A8 path: channelwise weights and per-row symmetric activation +// quantization and INT32 dot products. Routed experts round each weighted +// contribution to the activation dtype before FP32 reduction and scaling. +// A null routed_scale selects the shared-expert activation and epilogue. +// Weight tensors are rank-local. TP reduction is the caller's responsibility. +class DeepseekV4W8a8Experts : public Operator { + public: + DeepseekV4W8a8Experts(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, + double swiglu_limit, const std::optional routed_scale, Tensor out) { + (void)x; + (void)weights; + (void)indices; + (void)gate_up; + (void)gate_up_scale; + (void)down; + (void)down_scale; + (void)swiglu_limit; + (void)routed_scale; + (void)out; + } + + virtual void operator()(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, + double swiglu_limit, const std::optional routed_scale, Tensor out) const = 0; +}; + +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/dynamic_int8_quant.cuh b/src/native/cuda/hygon/dynamic_int8_quant.cuh new file mode 100644 index 000000000..bfa3ad07e --- /dev/null +++ b/src/native/cuda/hygon/dynamic_int8_quant.cuh @@ -0,0 +1,38 @@ +#pragma once +#include +#include "native/cuda/hygon/caster.cuh" +namespace infini::ops::hygon_dynamic_int8 { +constexpr unsigned kThreads = 128; + +template +__global__ void Quantize(const T* input, int8_t* output, float* scales, + size_t rows, size_t width) { + __shared__ float maximum[kThreads]; + const unsigned lane = threadIdx.x; + for (size_t row = blockIdx.x; row < rows; row += gridDim.x) { + const T* in = input + row * width; + auto* out = output + row * width; + float local = 0.0f; + for (size_t i = lane; i < width; i += kThreads) { + local = fmaxf(local, fabsf(Caster::template Cast(in[i]))); + } + maximum[lane] = local; + __syncthreads(); + for (unsigned step = kThreads / 2; step; step /= 2) { + if (lane < step) maximum[lane] = fmaxf(maximum[lane], maximum[lane + step]); + __syncthreads(); + } + const float absmax = fmaxf(maximum[0], 1.0e-10f); + const float scale = absmax / 127.0f; + // Dense W8A8 computes the quantization multiplier from the unrounded maximum. + const float inverse = DirectRatio ? 127.0f / absmax : 1.0f / scale; + if (lane == 0) scales[row] = scale; + for (size_t i = lane; i < width; i += kThreads) { + const int q = __float2int_rn(Caster::template Cast(in[i]) * inverse); + out[i] = static_cast(min(max(q, -127), 127)); + } + __syncthreads(); + } +} + +} // namespace infini::ops::hygon_dynamic_int8 diff --git a/src/native/cuda/hygon/flash_mla/bridge.hip b/src/native/cuda/hygon/flash_mla/bridge.hip new file mode 100644 index 000000000..160747dbf --- /dev/null +++ b/src/native/cuda/hygon/flash_mla/bridge.hip @@ -0,0 +1,167 @@ +#include "bridge.hpp" + +#include "params_hip.h" +#include "gfx9/decode/combine/combine_hip.h" +#include "gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h" +#include "gfx93/decode/sparse_fp8/splitkv_mla_hip.h" + +#include + +#include +#include + +namespace infini::ops::hygon_flash_mla { +namespace { +constexpr int kHeads = 64; +constexpr int kDimension = 512; + +void check(hipError_t status) { + if (status != hipSuccess) throw std::runtime_error(hipGetErrorString(status)); +} + +__global__ void pad_input(const uint16_t* query, const float* sink, uint16_t* padded, + float* padded_sink, int rows, int heads) { + const size_t index = blockIdx.x * blockDim.x + threadIdx.x; + const size_t count = static_cast(rows) * kHeads * kDimension; + for (size_t i = index; i < count; i += gridDim.x * blockDim.x) { + const size_t head = (i / kDimension) % kHeads; + const size_t row = i / (kHeads * kDimension); + padded[i] = head < heads ? query[(row * heads + head) * kDimension + i % kDimension] : 0; + } + if (index < kHeads) padded_sink[index] = index < heads ? sink[index] : 0.0f; +} + +__global__ void unpad_output(const uint16_t* padded, uint16_t* output, int rows, int heads) { + const size_t count = static_cast(rows) * heads * kDimension; + for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += gridDim.x * blockDim.x) { + const size_t row = i / (heads * kDimension); + output[i] = padded[row * kHeads * kDimension + i % (heads * kDimension)]; + } +} + +class Workspace { + public: + explicit Workspace(void* data) : next_(reinterpret_cast(data)) {} + + template + T* take(size_t count) { + next_ = (next_ + 255) & ~uintptr_t(255); + auto* result = reinterpret_cast(next_); + next_ += count * sizeof(T); + return result; + } + + private: + uintptr_t next_; +}; +} // namespace + +void run(const Arguments& a) { + if (!a.workspace || a.workspace_bytes < workspace_bytes(a.rows, a.partitions)) + throw std::invalid_argument("FlashMLA workspace is too small"); + Workspace work(a.workspace); + const size_t query_count = static_cast(a.rows) * kHeads; + const size_t split_count = static_cast(a.rows + a.partitions) * kHeads; + auto* query = work.take(query_count * kDimension); + auto* output = work.take(query_count * kDimension); + auto* sink = work.take(kHeads); + auto* lse = work.take(query_count); + auto* lse_accum = work.take(split_count); + auto* out_accum = work.take(split_count * kDimension); + auto* metadata = work.take(a.partitions); + auto* splits = work.take(a.rows + 1); + const auto stream = static_cast(a.stream); + const unsigned grid = std::min((query_count * kDimension + 255) / 256, 65535); + pad_input<<>>(static_cast(a.query),a.sink, + reinterpret_cast(query),sink,a.rows,a.heads); + check(hipGetLastError()); + + SparseAttnDecodeParams p{}; + p.b=a.rows; + p.s_q=1; + p.h_q=kHeads; + p.h_kv=1; + p.d_qk=kDimension; + p.d_v=kDimension; + p.sm_scale=a.scale; + p.sm_scale_div_log2=a.scale*1.4426950408889634f; + p.num_blocks=a.swa.blocks; + p.page_block_size=a.swa.page_tokens; + p.topk=a.swa.index_width; + p.model_type=ModelType::MODEL1; + p.q=query; + p.kv=reinterpret_cast(const_cast(a.swa.data)); + p.indices=const_cast(a.swa.indices); + p.topk_length=const_cast(a.swa.lengths); + p.attn_sink=sink; + p.lse=lse; + p.out=output; + p.stride_q_b=kHeads*kDimension; + p.stride_q_s_q=kHeads*kDimension; + p.stride_q_h_q=kDimension; + p.stride_kv_block=a.swa.page_stride; + p.stride_kv_row=584; + p.stride_indices_b=a.swa.index_width; + p.stride_indices_s_q=a.swa.index_width; + p.stride_lse_b=kHeads; + p.stride_lse_s_q=kHeads; + p.stride_o_b=kHeads*kDimension; + p.stride_o_s_q=kHeads*kDimension; + p.stride_o_h_q=kDimension; + p.extra_num_blocks=a.extra.blocks; + p.extra_page_block_size=a.extra.page_tokens; + p.extra_topk=a.extra.index_width; + p.extra_kv=reinterpret_cast(const_cast(a.extra.data)); + p.extra_indices=const_cast(a.extra.indices); + p.extra_topk_length=const_cast(a.extra.lengths); + p.stride_extra_kv_block=a.extra.page_stride; + p.stride_extra_kv_row=584; + p.stride_extra_indices_b=a.extra.index_width; + p.stride_extra_indices_s_q=a.extra.index_width; + p.stream=stream; + p.num_sm_parts=a.partitions; + p.tile_scheduler_metadata_ptr=metadata; + p.num_splits_ptr=splits; + p.lse_accum=lse_accum; + p.o_accum=out_accum; + p.stride_lse_accum_split=kHeads; + p.stride_lse_accum_s_q=kHeads; + p.stride_o_accum_split=kHeads*kDimension; + p.stride_o_accum_s_q=kHeads*kDimension; + p.stride_o_accum_h_q=kDimension; + GetDecodeSchedMetaParams scheduler{a.rows,1,64,5,a.swa.index_width,a.extra.index_width, + p.topk_length,p.extra_topk_length,nullptr,metadata,splits,a.partitions,stream}; + gfx9::decode::run_get_decoding_sched_meta_kernel(scheduler); + gfx93::decode::sparse_fp8::run_flash_splitkv_mla_fp8_sparse_kernel(p); + + CombineParams combine{}; + combine.b=a.rows; + combine.s_q=1; + combine.h_q=kHeads; + combine.d_v=kDimension; + combine.lse=lse; + combine.out=output; + combine.stride_lse_b=kHeads; + combine.stride_lse_s_q=kHeads; + combine.stride_o_b=kHeads*kDimension; + combine.stride_o_s_q=kHeads*kDimension; + combine.stride_o_h_q=kDimension; + combine.lse_accum=lse_accum; + combine.o_accum=out_accum; + combine.stride_lse_accum_split=kHeads; + combine.stride_lse_accum_s_q=kHeads; + combine.stride_o_accum_split=kHeads*kDimension; + combine.stride_o_accum_s_q=kHeads*kDimension; + combine.stride_o_accum_h_q=kDimension; + combine.tile_scheduler_metadata_ptr=metadata; + combine.num_splits_ptr=splits; + combine.num_sm_parts=a.partitions; + combine.attn_sink=sink; + combine.stream=stream; + gfx9::decode::run_flash_mla_combine_kernel(combine); + unpad_output<<>>(reinterpret_cast(output), + static_cast(a.output),a.rows,a.heads); + check(hipGetLastError()); +} + +} // namespace infini::ops::hygon_flash_mla diff --git a/src/native/cuda/hygon/flash_mla/bridge.hpp b/src/native/cuda/hygon/flash_mla/bridge.hpp new file mode 100644 index 000000000..51cd4bf48 --- /dev/null +++ b/src/native/cuda/hygon/flash_mla/bridge.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +namespace infini::ops::hygon_flash_mla { + +struct Pages { + const void* data; + const int32_t* indices; + const int32_t* lengths; + int blocks; + int page_tokens; + int page_stride; + int index_width; +}; + +struct Arguments { + const void* query; + const float* sink; + void* output; + void* workspace; + size_t workspace_bytes; + void* stream; + int rows; + int heads; + int partitions; + float scale; + Pages swa; + Pages extra; +}; + +inline size_t workspace_bytes(size_t rows, size_t partitions) { + constexpr size_t heads = 64, dim = 512; + const size_t queries = rows * heads; + const size_t splits = (rows + partitions) * heads; + // Each allocation in the bridge starts at a 256-byte boundary. + return 8 * 256 + queries * dim * 4 + queries * 4 + + splits * (dim + 1) * 4 + partitions * 32 + (rows + 1) * 4 + heads * 4; +} + +void run(const Arguments& args); + +} // namespace infini::ops::hygon_flash_mla diff --git a/src/native/cuda/hygon/ops/cast/kernel.h b/src/native/cuda/hygon/ops/cast/kernel.h new file mode 100644 index 000000000..757c8c5cf --- /dev/null +++ b/src/native/cuda/hygon/ops/cast/kernel.h @@ -0,0 +1,120 @@ +#ifndef INFINI_OPS_HYGON_CAST_KERNEL_H_ +#define INFINI_OPS_HYGON_CAST_KERNEL_H_ + +#include + +#include +#include +#include +#include +#include +#include + +#include "base/cast.h" +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops { +namespace hygon_cast_detail { +template +__global__ void Convert(const Src* input, Dst* output, std::size_t first, + std::size_t count) { + const auto i = static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + if (i < count) { + if constexpr (std::is_integral_v && std::is_integral_v) { + output[first + i] = static_cast(input[first + i]); + } else { + output[first + i] = + Caster::template Cast(input[first + i]); + } + } +} +} // namespace hygon_cast_detail + +template <> +class Operator : public Cast { + public: + static bool supports(const Tensor input, const Tensor out) { + const bool supported_conversion = + (FloatType(input.dtype()) && FloatType(out.dtype())) || + (input.dtype() == DataType::kInt32 && out.dtype() == DataType::kInt64); + if (!supported_conversion || input.device().type() != Device::Type::kHygon || + input.device() != out.device() || input.shape() != out.shape() || + !input.IsContiguous() || !out.IsContiguous()) + return false; + + const auto ip = reinterpret_cast(input.data()); + const auto op = reinterpret_cast(out.data()); + const auto size = input.numel(); + if (size == 0) return true; + const auto iw = Width(input.dtype()); + const auto ow = Width(out.dtype()); + const auto limit = std::numeric_limits::max(); + if (ip == 0 || op == 0 || ip % iw || op % ow || + size > (limit - ip) / iw || size > (limit - op) / ow) + return false; + return (ip == op && input.dtype() == out.dtype()) || + ip + size * iw <= op || op + size * ow <= ip; + } + + Operator(const Tensor input, Tensor out) : Cast{Checked(input, out), out} {} + bool capture_safe() const override { return true; } + + void operator()(const Tensor input, Tensor out) const override { + if (input.dtype() != input_dtype_ || out.dtype() != out_dtype_ || + input.shape() != input_shape_ || out.shape() != out_shape_ || + !supports(input, out)) + throw std::invalid_argument("Hygon Cast: unsupported call metadata"); + if (output_size_ == 0 || input.data() == out.data()) return; + + if (input.dtype() == DataType::kInt32) { + Launch(input, out); + return; + } + DispatchFunc, + List>( + {static_cast(input_dtype_), static_cast(out_dtype_)}, + [&](auto tag) { + using Src = TypeMapType(tag)>; + using Dst = TypeMapType(tag)>; + Launch(input, out); + }, "Hygon Cast"); + } + + private: + static bool FloatType(DataType dtype) { + return dtype == DataType::kFloat32 || dtype == DataType::kFloat16 || + dtype == DataType::kBFloat16; + } + + static std::size_t Width(DataType dtype) { + if (dtype == DataType::kInt64) return 8; + return dtype == DataType::kFloat32 || dtype == DataType::kInt32 ? 4 : 2; + } + + static Tensor Checked(const Tensor input, Tensor out) { + if (!supports(input, out)) + throw std::invalid_argument("Hygon Cast: unsupported dtype, layout or alias"); + return input; + } + + template + void Launch(const Tensor input, Tensor out) const { + constexpr std::size_t threads = 256; + constexpr std::size_t capacity = 65535 * threads; + const auto stream = static_cast(stream_); + for (std::size_t first = 0; first < output_size_;) { + const auto count = std::min(capacity, output_size_ - first); + const auto blocks = static_cast((count + threads - 1) / threads); + hygon_cast_detail::Convert<<>>( + static_cast(input.data()), static_cast(out.data()), + first, count); + const auto status = cudaGetLastError(); + if (status != cudaSuccess) + throw std::runtime_error(std::string("Hygon Cast: ") + cudaGetErrorString(status)); + first += count; + } + } +}; +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/deepseek_v4_c4_index/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_c4_index/kernel.h new file mode 100644 index 000000000..f6245227d --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_c4_index/kernel.h @@ -0,0 +1,182 @@ +#pragma once +#include +#include +#include +#include "base/deepseek_v4_c4_index.h" + +namespace infini::ops { +namespace v4_c4 { +constexpr size_t kWidth = 128, kPage = 64, kPageBytes = kPage*(kWidth+sizeof(float)); +constexpr size_t kTop = 512; + +static __device__ float decode(uint8_t bits) { + const int exp = (bits >> 3) & 15, mantissa = bits & 7; + const float mag = exp == 0 ? ldexpf(float(mantissa),-9) + : ldexpf(1.0f + float(mantissa)/8,exp-7); + return (bits & 128) ? -mag : mag; +} + +static __global__ void transform(const __nv_bfloat16* in, const int32_t* slots, uint8_t* dst, + float* scales, size_t count, size_t pages, size_t stride) { + __shared__ float lane_value[kWidth]; + const unsigned lane = threadIdx.x; + for (size_t row=blockIdx.x; row= pages)) continue; + float value = __bfloat162float(in[row*kWidth+lane]); + for (unsigned bit=1; bit(page+kPage*kWidth)[size_t(slot)%kPage] = scale; + else scales[row] = scale; + } + __syncthreads(); + } +} + +static __device__ int32_t physical(const int32_t* pages, size_t pos, size_t page_count) { + const auto page = pages[pos/kPage]; + return page < 0 || size_t(page) >= page_count ? -1 : page*int(kPage)+int(pos%kPage); +} + +static __global__ void score(const uint8_t* query, const float* qs, const __nv_bfloat16* weights, + const int32_t* table, const int32_t* lengths, const uint8_t* cache, + float* scores, size_t rows, size_t heads, size_t width, + size_t cache_pages, size_t stride) { + __shared__ float dot[kWidth]; + const unsigned lane=threadIdx.x; + const size_t span=width*kPage; + for (size_t flat=blockIdx.x; flat= size_t(lengths[row])) continue; + const auto id = physical(table+row*width,pos,cache_pages); + if (id < 0) { + if (lane == 0) scores[flat]=-INFINITY; + continue; + } + const auto* page = cache+(size_t(id)/kPage)*stride; + const auto offset=size_t(id)%kPage; + const float key=decode(page[offset*kWidth+lane]); + const float key_scale=reinterpret_cast(page+kPage*kWidth)[offset]; + float total=0; + for (size_t h=0;h 0 ? min(size_t(lengths[row]),span) : 0; + for (size_t k=0;kbest) { best=value; chosen=j; } + } + if (best!=-INFINITY) { + idx=physical(table+row*width,chosen,cache_pages); + scores[row*span+chosen]=-INFINITY; + } + } + out[row*kTop+k]=idx; + } + } +} +} // namespace v4_c4 + +template <> +class Operator : public DeepseekV4C4Index { + public: + static bool supports(Tensor k, Tensor slots, Tensor q, Tensor w, Tensor table, + Tensor lens, Tensor cache, Tensor out) { + using namespace v4_c4; + if (q.ndim()!=3 || q.size(2)!=kWidth || !q.size(0) || !q.size(1) || + table.ndim()!=2 || table.size(0)!=q.size(0) || !table.size(1) || + cache.ndim()!=2 || !cache.size(0) || cache.size(0)>INT32_MAX/kPage || + cache.size(1)(std::min(n,65535));}; + auto stream=static_cast(stream_); + auto* scores=static_cast(workspace_); + auto* scales=scores+rows*width*kPage; + auto* query=reinterpret_cast(scales+rows*heads); + auto* raw=static_cast(cache.data()); + const auto* lengths=static_cast(lens.data()); + const auto* page_table=static_cast(table.data()); + transform<<>>(static_cast(k.data()), + static_cast(slots.data()),raw,nullptr,rows,cache.size(0),cache.size(1)); + if (width*kPage>kTop) { + transform<<>>( + static_cast(q.data()),nullptr,query,scales,rows*heads,0,0); + score<<>>(query,scales, + static_cast(w.data()),page_table,lengths,raw,scores, + rows,heads,width,cache.size(0),cache.size(1)); + } + select<<>>(scores,page_table,lengths, + static_cast(out.data()),rows,width,cache.size(0)); + if (cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 C4 index launch failed"); + } + private: + size_t bytes_{}; +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_cast_rz/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_cast_rz/kernel.h new file mode 100644 index 000000000..f3433a794 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_cast_rz/kernel.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +#include "base/deepseek_v4_cast_rz.h" + +namespace infini::ops { +namespace v4_cast_rz { +static __global__ void convert(const float* input, __nv_bfloat16* output, size_t count) { + for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < count; + i += static_cast(gridDim.x) * blockDim.x) + output[i] = __float2bfloat16_rz(input[i]); +} +} // namespace v4_cast_rz + +template<> +class Operator : public DeepseekV4CastRz { + public: + static bool supports(const Tensor input, const Tensor out) { + return input.device().type() == Device::Type::kHygon && + out.device() == input.device() && input.dtype() == DataType::kFloat32 && + out.dtype() == DataType::kBFloat16 && input.shape() == out.shape() && + input.IsContiguous() && out.IsContiguous(); + } + Operator(const Tensor input, Tensor out) : DeepseekV4CastRz(input,out) { + if (!supports(input,out)) throw std::invalid_argument("Invalid DeepSeek V4 RZ cast"); + } + bool capture_safe() const override { return true; } + void operator()(const Tensor input, Tensor out) const override { + if (!supports(input,out)) throw std::invalid_argument("Invalid DeepSeek V4 RZ cast execution"); + if (!input.numel()) return; + const auto grid = static_cast(std::min((input.numel()-1)/256+1,65535)); + v4_cast_rz::convert<<(stream_)>>>( + static_cast(input.data()),static_cast<__nv_bfloat16*>(out.data()),input.numel()); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 RZ cast launch failed"); + } +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_compress/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_compress/kernel.h new file mode 100644 index 000000000..2b536bed2 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_compress/kernel.h @@ -0,0 +1,194 @@ +#ifndef INFINI_OPS_HYGON_DEEPSEEK_V4_COMPRESS_KERNEL_H_ +#define INFINI_OPS_HYGON_DEEPSEEK_V4_COMPRESS_KERNEL_H_ + +#include +#include + +#include "base/deepseek_v4_compress.h" +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops { +namespace v4_compress_detail { + +template +static __device__ void ReadSource(const T* projection, const float* state, + const float* ape, int32_t source_id, size_t input_rows, + size_t state_rows, size_t head_dim, size_t width, + size_t position, size_t channel, bool overlap, + bool checkpoint_ape, float& value, float& score) { + value = 0.0f; + score = -INFINITY; + const size_t base = (overlap && position >= 4 ? head_dim : 0) + channel; + if (source_id >= 0 && static_cast(source_id) < input_rows) { + using Convert = Caster; + const auto* source = projection + static_cast(source_id) * width; + value = Convert::template Cast(source[base]); + score = Convert::template Cast(source[base + width / 2]); + } else if (source_id < -1 && static_cast(-2LL - source_id) < state_rows) { + const auto* source = state + static_cast(-2LL - source_id) * width; + value = source[base]; + score = source[base + width / 2]; + } else { + return; + } + // The checkpoint stores the previous and current C4 bias halves side by side. + const size_t ape_row = checkpoint_ape ? 2 * (position % 4) + (position >= 4) : position; + score += ape[ape_row * head_dim + channel]; +} + +static __device__ float FastExp(float value) { + return __builtin_amdgcn_exp2f(value * 1.4426950408889634f); +} + +template +static __global__ void Pool(const T* projection, const float* ape, + const int32_t* sources, const float* state, T* out, + size_t input_rows, size_t state_rows, size_t output_rows, + size_t head_dim, size_t window, bool checkpoint_ape) { + using Convert = Caster; + const size_t width = (window == 8 ? 4 : 2) * head_dim; + for (size_t row = blockIdx.x; row < output_rows; row += gridDim.x) { + for (size_t channel = threadIdx.x; channel < head_dim; channel += blockDim.x) { + const auto load = [&](size_t pos, float& value, float& score) { + ReadSource(projection, state, ape, sources[row * window + pos], input_rows, + state_rows, head_dim, width, pos, channel, window == 8, + checkpoint_ape, value, score); + }; + float maximum = -INFINITY; + float sum = 0.0f; + float total = 0.0f; + if constexpr (std::is_same_v) { + // C4 uses one group. C128 merges sixteen groups of eight tokens. + float group_maxima[16], group_sums[16], group_products[16]; + const size_t groups = window / 8; + for (size_t group = 0; group < groups; ++group) { + float values[8], scores[8], group_max = -INFINITY; + for (size_t i = 0; i < 8; ++i) { + load(group * 8 + i, values[i], scores[i]); + group_max = fmaxf(group_max, scores[i]); + } + float group_sum = 0.0f, group_product = 0.0f; + if (group_max != -INFINITY) { + for (size_t i = 0; i < 8; ++i) { + if (scores[i] == -INFINITY) continue; + const float weight = FastExp(scores[i] - group_max); + group_product = fmaf(values[i], weight, group_product); + group_sum += weight; + } + } + group_maxima[group] = group_max; + group_sums[group] = group_sum; + group_products[group] = group_product; + maximum = fmaxf(maximum, group_max); + } + if (maximum != -INFINITY) { + for (size_t group = 0; group < groups; ++group) { + if (group_maxima[group] == -INFINITY) continue; + const float weight = FastExp(group_maxima[group] - maximum); + total = fmaf(group_products[group], weight, total); + sum = fmaf(group_sums[group], weight, sum); + } + } + } else { + for (size_t pos = 0; pos < window; ++pos) { + float value, score; + load(pos, value, score); + if (score == -INFINITY) continue; + const float next_max = fmaxf(maximum, score); + const float previous_weight = sum == 0.0f ? 0.0f : expf(maximum - next_max); + const float weight = expf(score - next_max); + total = total * previous_weight + weight * value; + sum = sum * previous_weight + weight; + maximum = next_max; + } + } + out[row * head_dim + channel] = + Convert::template Cast(sum == 0.0f ? 0.0f : total / sum); + } + } +} + +template +static __global__ void Update(const T* projection, const int32_t* slots, float* state, + size_t input_rows, size_t state_rows, size_t width) { + for (size_t row = blockIdx.x; row < input_rows; row += gridDim.x) { + const auto slot = slots[row]; + if (slot < 0 || static_cast(slot) >= state_rows) continue; + for (size_t col = threadIdx.x; col < width; col += blockDim.x) { + state[static_cast(slot) * width + col] = + Caster::template Cast(projection[row * width + col]); + } + } +} + +} // namespace v4_compress_detail + +template <> +class Operator : public DeepseekV4Compress { + public: + static bool supports(const Tensor projection, const Tensor ape, const Tensor sources, + const Tensor slots, const Tensor state, const Tensor out) { + if (projection.device().type() != Device::Type::kHygon || + (projection.dtype() != DataType::kBFloat16 && projection.dtype() != DataType::kFloat32) || + ape.dtype() != DataType::kFloat32 || + sources.dtype() != DataType::kInt32 || slots.dtype() != DataType::kInt32 || + state.dtype() != DataType::kFloat32 || out.dtype() != projection.dtype() || + projection.ndim() != 2 || ape.ndim() != 2 || sources.ndim() != 2 || + state.ndim() != 2 || out.ndim() != 2) return false; + const auto window = sources.size(1), dim = out.size(1); + if ((window != 8 && window != 128) || (dim != 128 && dim != 512) || + (window == 128 && dim != 512) || !projection.size(0) || !state.size(0) || + projection.size(1) != dim * (window == 8 ? 4 : 2) || + state.size(1) != projection.size(1) || !sources.size(0) || + (ape.shape() != Tensor::Shape{window, dim} && + (window != 8 || ape.shape() != Tensor::Shape{4, 2 * dim})) || + out.size(0) != sources.size(0) || + slots.shape() != Tensor::Shape{projection.size(0)}) return false; + for (const auto* t : {&projection, &ape, &sources, &slots, &state, &out}) { + if (t->device() != projection.device() || !t->IsContiguous()) + return false; + } + return true; + } + + Operator(const Tensor projection, const Tensor ape, const Tensor sources, + const Tensor slots, Tensor state, Tensor out) + : DeepseekV4Compress(projection, ape, sources, slots, state, out) { + if (!supports(projection, ape, sources, slots, state, out)) + throw std::invalid_argument("DeepSeek V4 compressor inputs are unsupported"); + } + + bool capture_safe() const override { return true; } + + void operator()(const Tensor projection, const Tensor ape, const Tensor sources, + const Tensor slots, Tensor state, Tensor out) const override { + if (!supports(projection, ape, sources, slots, state, out)) + throw std::invalid_argument("DeepSeek V4 compressor inputs changed"); + using namespace v4_compress_detail; + const auto grid = [](size_t rows) { + return static_cast(rows < 65535 ? rows : 65535); + }; + auto stream = static_cast(stream_); + auto* state_data = static_cast(state.data()); + const auto launch = [&](auto tag) { + using T = decltype(tag); + const auto* data = static_cast(projection.data()); + Pool<<>>( + data, static_cast(ape.data()), + static_cast(sources.data()), state_data, + static_cast(out.data()), projection.size(0), state.size(0), + out.size(0), out.size(1), sources.size(1), ape.size(0) == 4); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 pooling launch failed"); + Update<<>>( + data, static_cast(slots.data()), state_data, projection.size(0), + state.size(0), projection.size(1)); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 state update launch failed"); + }; + if (projection.dtype() == DataType::kFloat32) launch(float{}); + else launch(__nv_bfloat16{}); + } +}; + +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h new file mode 100644 index 000000000..4ce8fbdcf --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include +#include "base/deepseek_v4_mhc_post.h" + +namespace infini::ops { +namespace v4_mhc_post { +static __global__ void mix(const __nv_bfloat16* activation,const __nv_bfloat16* residual, + const float* post,const float* matrix,__nv_bfloat16* out, + size_t n,size_t hidden) { + for(size_t idx=blockIdx.x*blockDim.x+threadIdx.x;idx +class Operator : public DeepseekV4MhcPost { + public: + using DeepseekV4MhcPost::DeepseekV4MhcPost; + bool capture_safe() const override {return true;} + static bool supports(Tensor x,Tensor r,Tensor p,Tensor m,Tensor y) { + if(r.ndim()!=3 || r.size(1)!=4 || !r.size(0) || !r.size(2) || + r.device().type()!=Device::Type::kHygon || r.dtype()!=DataType::kBFloat16 || + x.shape()!=Tensor::Shape{r.size(0),r.size(2)} || p.shape()!=Tensor::Shape{r.size(0),4} || + m.shape()!=Tensor::Shape{r.size(0),4,4} || y.shape()!=r.shape() || + y.dtype()!=r.dtype() || x.dtype()!=r.dtype() || p.dtype()!=DataType::kFloat32 || + m.dtype()!=DataType::kFloat32) return false; + for(const auto& t:{x,r,p,m,y}) if(t.device()!=r.device() || !t.IsContiguous()) return false; + return true; + } + void operator()(Tensor x,Tensor r,Tensor p,Tensor m,Tensor y) const override { + if(!supports(x,r,p,m,y)) throw std::invalid_argument("Invalid DeepSeek V4 mHC post"); + v4_mhc_post::mix<<(std::min((y.numel()-1)/128+1,65535)),128,0, + static_cast(stream_)>>>( + static_cast(x.data()),static_cast(r.data()), + static_cast(p.data()),static_cast(m.data()), + static_cast<__nv_bfloat16*>(y.data()),y.numel(),r.size(2)); + if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC post launch failed"); + } +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/head.cuh b/src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/head.cuh new file mode 100644 index 000000000..6bff98b13 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/head.cuh @@ -0,0 +1,45 @@ +#pragma once +#include +#include +// Match the reference's 512-column head projection tiles and warp reductions. +// This is separate from the split-K decoder mHC projection. +namespace infini::ops::v4_mhc_head { +static __device__ float wave_sum(float x) { + for(unsigned offset=8;offset;offset/=2)x+=__shfl_xor(x,offset,64); + x+=__shfl_xor(x,16,64);x+=__shfl_xor(x,32,64);return x; +} +static __global__ void head(const __nv_bfloat16* x,const float* weight,const float* scale,const float* bias, + __nv_bfloat16* out,size_t hidden,float eps,float hc_eps) { + __shared__ float squares[4],pre[4]; + const size_t row=blockIdx.x,width=hidden*4; + const unsigned tid=threadIdx.x,lane=tid%64,col=tid/64; + float dot=0,sum_sq=0; + for(size_t tile=0;tile +#include +#include "base/deepseek_v4_mhc_pre.h" +#include "head.cuh" + +namespace infini::ops { +namespace v4_mhc { +constexpr unsigned kThreads=128; +constexpr unsigned kStreams=4, kWidth=24; +constexpr unsigned kSplits = 32; + +inline bool use_split_projection(size_t rows, size_t hidden, int64_t iterations) { + return iterations && rows <= 1024 && (hidden == 4096 || hidden == 7168); +} + +inline size_t workspace_floats(size_t rows, size_t hidden, size_t outputs, int64_t iterations) { + return use_split_projection(rows, hidden, iterations) ? + rows * kSplits * (outputs + 1) : rows * outputs; +} + +// Hygon FP32 MMAC assigns the K chunks to four 16-lane groups and writes +// four output columns per lane. Preserve the reduction order across K splits. +using Float2 = float __attribute__((ext_vector_type(2))); +using Float4 = float __attribute__((ext_vector_type(4))); + +template +static __global__ void project_split(const __nv_bfloat16* x, const float* w, float* dots, + float* squares, size_t rows, size_t width) { + const unsigned lane = threadIdx.x; + const unsigned group = lane / 16; + const size_t row = blockIdx.x * 16 + lane % 16; + const size_t col = blockIdx.y * 16 + lane % 16; + const size_t split_width = width / kSplits; + const size_t split_start = blockIdx.z * split_width; + Float4 dot = {}; + float square = 0.0f; + for (size_t tile = 0; tile < split_width; tile += 64) { + for (unsigned j = 0; j < 16; j += Pack8 ? 2 : 1) { + const size_t k = split_start + tile + (j / 8 * 4 + group) * 8 + j % 8; + const float a = row < rows ? __bfloat162float(x[row * width + k]) : 0.0f; + const float b = col < kWidth ? w[col * width + k] : 0.0f; + square = fmaf(a, a, square); + if constexpr (Pack8) { + const float a_next = row < rows ? __bfloat162float(x[row * width + k + 1]) : 0.0f; + const float b_next = col < kWidth ? w[col * width + k + 1] : 0.0f; + square = fmaf(a_next, a_next, square); + dot = __builtin_hcu_mmac_16x16x8_f32(Float2{a, a_next}, Float2{b, b_next}, dot); + } else { + dot = __builtin_hcu_mmac_16x16x4_f32(a, b, dot); + } + } + } + const float pair = square + __shfl_xor(square, 32, 64); + const float total = pair + __shfl_xor(pair, 16, 64); + if (row >= rows) return; + const size_t index = blockIdx.z * rows + row; + if (blockIdx.y == 0 && group == 0) squares[index] = total; + for (unsigned i = 0; i < 4; ++i) { + const size_t column = blockIdx.y * 16 + group + i * 4; + if (column < kWidth) dots[index * kWidth + column] = dot[i]; + } +} + +static __device__ float sum_splits(const float* data, size_t stride) { + float partials[4] = {}; + for (unsigned split = 0; split < kSplits; split += 4) { + for (unsigned group = 0; group < 4; ++group) { + partials[group] += data[(split + group) * stride]; + } + } + return ((partials[0] + partials[1]) + partials[2]) + partials[3]; +} + +static __device__ float sum_pairs(float a, float b, float c, float d) { + return (a + b) + (c + d); +} + +static __global__ void project(const __nv_bfloat16* x,const float* w,float* values, + size_t rows,size_t width,size_t outputs,float eps) { + __shared__ float dots[kThreads], squares[kThreads]; + const auto lane=threadIdx.x; + for(size_t idx=blockIdx.x;idx +class Operator : public DeepseekV4MhcPre { + public: + static bool supports(Tensor x,Tensor w,Tensor s,Tensor b,double e,double m,int64_t it, + Tensor y,Tensor p,Tensor c) { + using namespace v4_mhc; + if(x.ndim()!=3 || x.size(1)!=kStreams || !x.size(0) || !x.size(2) || + x.dtype()!=DataType::kBFloat16 || x.device().type()!=Device::Type::kHygon || + it<0 || it>1000 || !std::isfinite(e) || e<=0 || !std::isfinite(m) || m<=0) return false; + const auto n=x.size(0),h=x.size(2); + const size_t outputs=it ? kWidth : kStreams; + if(w.shape()!=Tensor::Shape{outputs,kStreams*h} || s.shape()!=Tensor::Shape{it ? 3u : 1u} || + b.shape()!=Tensor::Shape{outputs} || y.shape()!=Tensor::Shape{n,h} || + p.shape()!=Tensor::Shape{n,kStreams} || c.shape()!=Tensor::Shape{n,kStreams,kStreams} || + y.dtype()!=x.dtype()) return false; + for(const auto& t:{w,s,b,p,c}) if(t.dtype()!=DataType::kFloat32) return false; + for(const auto& t:{x,w,s,b,y,p,c}) if(t.device()!=x.device() || !t.IsContiguous()) return false; + return n<=SIZE_MAX/outputs/sizeof(float); + } + Operator(Tensor x,Tensor w,Tensor s,Tensor b,double e,double m,int64_t it,Tensor y,Tensor p,Tensor c) + : DeepseekV4MhcPre(x,w,s,b,e,m,it,y,p,c) { + if(!supports(x,w,s,b,e,m,it,y,p,c)) throw std::invalid_argument("Invalid DeepSeek V4 mHC pre"); + bytes_=v4_mhc::workspace_floats(x.size(0),x.size(2),w.size(0),it)*sizeof(float); + } + bool capture_safe() const override {return true;} + size_t workspace_size_in_bytes() const override {return bytes_;} + void operator()(Tensor x,Tensor w,Tensor s,Tensor b,double e,double m,int64_t it,Tensor y,Tensor p,Tensor c) const override { + using namespace v4_mhc; + if(!supports(x,w,s,b,e,m,it,y,p,c) || !workspace_ || + workspace_size_in_bytes_(std::min(n,65535));}; + auto stream=static_cast(stream_); + auto* v=static_cast(workspace_); + const auto* residual=static_cast(x.data()); + const auto rows = x.size(0); + const auto width = kStreams*x.size(2); + const auto* weight = static_cast(w.data()); + if (!it && rows <= 65535 && (x.size(2)==4096 || x.size(2)==7168)) { + v4_mhc_head::head<<>>(residual,weight, + static_cast(s.data()),static_cast(b.data()), + static_cast<__nv_bfloat16*>(y.data()),x.size(2),float(e),float(m)); + if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC head launch failed"); + return; + } + float* squares = nullptr; + if (use_split_projection(rows,x.size(2),it)) { + squares = v + rows*kSplits*kWidth; + const dim3 split_grid((rows+15)/16,2,kSplits); + if (rows > 64) project_split<<>>(residual,weight,v,squares,rows,width); + else project_split<<>>(residual,weight,v,squares,rows,width); + } else { + project<<>>(residual,weight,v,rows,width,w.size(0),float(e)); + } + if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC projection launch failed"); + mix<<>>(residual,v,static_cast(s.data()), + static_cast(b.data()),static_cast<__nv_bfloat16*>(y.data()), + static_cast(p.data()),static_cast(c.data()),x.size(0),x.size(2),float(m),it,squares,float(e)); + if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC pre launch failed"); + } + private: + size_t bytes_{}; +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_rotary_frequencies/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_rotary_frequencies/kernel.h new file mode 100644 index 000000000..f2be4f5a2 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_rotary_frequencies/kernel.h @@ -0,0 +1,60 @@ +#pragma once +#include +#include +#include +#include +#include "base/deepseek_v4_rotary_frequencies.h" +#pragma once +#include +namespace infini::ops::v4_frequency_detail { +// Each tensor operation in table construction rounds separately in the reference. +static __device__ __forceinline__ float round_f32(float x) {volatile float value=x;return value;} +static __global__ void generate(float* out,size_t positions,size_t dim,float theta, + bool scaled,float low,float high,float factor,bool split) { + for(size_t k=blockIdx.x*blockDim.x+threadIdx.x;k +class Operator : public DeepseekV4RotaryFrequencies { + public: + static bool supports(Tensor out,double theta,bool,double low,double high,double factor,bool) { + return out.device().type()==Device::Type::kHygon && out.dtype()==DataType::kFloat32 && + out.ndim()==2 && out.size(0)>0 && out.size(1)>0 && out.size(1)%2==0 && + out.IsContiguous() && std::isfinite(theta) && theta>1 && theta<=std::numeric_limits::max() && + std::isfinite(low) && std::isfinite(high) && high>=low && + std::isfinite(factor) && factor>=1 && factor<=std::numeric_limits::max(); + } + Operator(Tensor out,double theta,bool scaled,double low,double high,double factor,bool split) + : DeepseekV4RotaryFrequencies(out,theta,scaled,low,high,factor,split) { + if(!supports(out,theta,scaled,low,high,factor,split))throw std::invalid_argument("Invalid V4 rotary frequencies"); + } + bool capture_safe() const override {return true;} + void operator()(Tensor out,double theta,bool scaled,double low,double high,double factor,bool split) const override { + if(!supports(out,theta,scaled,low,high,factor,split))throw std::invalid_argument("Invalid V4 rotary frequency execution"); + const size_t pairs=out.numel()/2; + const auto grid=static_cast(std::min((pairs-1)/256+1,65535)); + v4_frequency_detail::generate<<(stream_)>>>( + static_cast(out.data()),out.size(0),out.size(1),static_cast(theta), + scaled,static_cast(low),static_cast(high),static_cast(factor),split); + if(cudaGetLastError()!=cudaSuccess)throw std::runtime_error("V4 rotary frequency launch failed"); + } +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h new file mode 100644 index 000000000..7b8f32985 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h @@ -0,0 +1,110 @@ +#pragma once +#include +#include "base/deepseek_v4_route.h" + +namespace infini::ops { +namespace v4_route { + +// Reproduce the Triton gate's stable log1p expansion and HCU math. +static __device__ float probability(float x) { + const float exponent = __builtin_amdgcn_exp2f(-fabsf(x) * 1.4426950408889634f); + volatile float shifted = 1.0f + exponent; + const float log2 = __builtin_amdgcn_logf(shifted); + const float ln2_hi = __uint_as_float(0x3f317217); + const float ln2_lo = __uint_as_float(0x3377d1cf); + const float product = __fmul_rn(log2, ln2_hi); + float correction = fmaf(log2, ln2_hi, -product); + correction = fmaf(log2, ln2_lo, correction); + volatile float natural_log = fmaf(log2, ln2_hi, correction); + volatile float numerator = exponent * natural_log; + const float log1p = shifted == 1.0f ? exponent : numerator / (shifted - 1.0f); + return __builtin_amdgcn_sqrtf(fmaxf(x, 0.0f) + log1p); +} + +// HCU's hash gate uses stable sqrt(softplus), then a six-lane warp sum. +static __device__ float hash_probability(float x) { + return sqrtf(fmaxf(x,0.f)+log1pf(expf(-fabsf(x)))); +} + +template +__global__ void route(const float* logits,const float* bias,const Token* tokens, + const int64_t* hash,float scale,float* weights,int64_t* ids, + size_t rows,size_t experts,size_t k,size_t vocab) { + for(size_t row=blockIdx.x*blockDim.x+threadIdx.x;row(tokens[row]) : -1; + float norm=0; + for(size_t i=0;i=0 && size_t(token)=0 && size_t(id)0 ? (w[i]/norm)*scale : 0.f; + } +} +} // namespace v4_route + +template<> +class Operator : public DeepseekV4Route { + public: + using Opt=std::optional; + using DeepseekV4Route::DeepseekV4Route; + bool capture_safe() const override {return true;} + static bool supports(Tensor x,Opt b,Opt t,Opt h,double s,Tensor w,Tensor i) { + if(x.ndim()!=2 || !x.size(0) || !x.size(1) || w.ndim()!=2 || w.size(0)!=x.size(0) || + !w.size(1) || w.size(1)>x.size(1) || w.shape()!=i.shape() || + x.dtype()!=DataType::kFloat32 || w.dtype()!=DataType::kFloat32 || i.dtype()!=DataType::kInt64 || + x.device().type()!=Device::Type::kHygon || !std::isfinite(s) || s<=0 || + bool(t)!=bool(h) || (t && b)) return false; + if(b && (b->shape()!=Tensor::Shape{x.size(1)} || b->dtype()!=DataType::kFloat32)) return false; + if(t && (t->shape()!=Tensor::Shape{x.size(0)} || + (t->dtype()!=DataType::kInt32 && t->dtype()!=DataType::kInt64) || + h->ndim()!=2 || !h->size(0) || h->size(1)!=w.size(1) || + h->dtype()!=DataType::kInt64)) return false; + for(const auto& a:{x,w,i}) if(a.device()!=x.device() || !a.IsContiguous()) return false; + for(const auto& a:{b,t,h}) if(a && (a->device()!=x.device() || !a->IsContiguous())) return false; + return true; + } + void operator()(Tensor x,Opt b,Opt t,Opt h,double s,Tensor w,Tensor i) const override { + if(!supports(x,b,t,h,s,w,i)) throw std::invalid_argument("Invalid DeepSeek V4 routing inputs"); + auto launch=[&](auto tag) { + using T=decltype(tag); + v4_route::route<<(std::min((x.size(0)-1)/64+1,65535)),64,0, + static_cast(stream_)>>>( + static_cast(x.data()),b ? static_cast(b->data()) : nullptr, + t ? static_cast(t->data()) : nullptr,h ? static_cast(h->data()) : nullptr, + float(s),static_cast(w.data()),static_cast(i.data()), + x.size(0),x.size(1),w.size(1),h ? h->size(0) : 0); + }; + if(t && t->dtype()==DataType::kInt32) launch(int32_t{}); + else launch(int64_t{}); + if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 routing launch failed"); + } +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_sparse_mla/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_sparse_mla/kernel.h new file mode 100644 index 000000000..d525c01e5 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_sparse_mla/kernel.h @@ -0,0 +1,334 @@ +#ifndef INFINI_OPS_HYGON_DEEPSEEK_V4_SPARSE_MLA_H_ +#define INFINI_OPS_HYGON_DEEPSEEK_V4_SPARSE_MLA_H_ + +#include +#include "native/cuda/hygon/flash_mla/bridge.hpp" + +#include +#include +#include + +#include "base/deepseek_v4_sparse_mla.h" +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops { +namespace v4_sparse_mla_detail { +constexpr size_t kDim = 512; +constexpr size_t kNope = 448; +constexpr size_t kValueBytes = 576; +constexpr unsigned kThreads = 128; + +struct Pages { + const uint8_t* data{}; + const int32_t* indices{}; + const int32_t* lengths{}; + size_t page_count{}; + size_t page_stride{}; + size_t page_tokens{}; + size_t index_width{}; +}; + +// E4M3 finite-only encoding used by DeepSeek V4's packed cache. +static __device__ float decode_fp8(uint8_t byte) { + const int exponent = (byte >> 3) & 15; + const int mantissa = byte & 7; + if (exponent == 15 && mantissa == 7) return NAN; + const float value = exponent ? ldexpf(1.0f + mantissa * 0.125f, exponent - 7) + : ldexpf(static_cast(mantissa), -9); + return byte & 128 ? -value : value; +} + +static __device__ float value_at(const uint8_t* values, const uint8_t* exponents, size_t channel) { + if (channel < kNope) { + return ldexpf(decode_fp8(values[channel]), static_cast(exponents[channel / 64]) - 127); + } + return __bfloat162float(reinterpret_cast(values + kNope)[channel - kNope]); +} + +template +__global__ void AttendUnrounded(const T* q, const float* sink, Pages first, Pages second, + T* out, size_t rows, size_t heads, float scale) { + __shared__ float reduce[kThreads]; + __shared__ float past_weight, new_weight, denominator; + const unsigned lane = threadIdx.x; + for (size_t block = blockIdx.x; block < rows * heads; block += gridDim.x) { + const auto row = block / heads, head = block % heads; + float accum[4] = {}; + float query[4]; + for (size_t i = 0; i < 4; ++i) { + query[i] = Caster::template Cast(q[block * kDim + i * kThreads + lane]); + } + const auto sink_score = sink[head]; + float maximum = sink_score; + if (lane == 0) denominator = sink_score == -INFINITY ? 0.0f : 1.0f; + for (int source = 0; source < 2; ++source) { + const Pages table = source == 0 ? first : second; + if (!table.data) continue; + const int32_t length = table.lengths[row]; + const size_t positive = length <= 0 ? 0 : static_cast(length); + const size_t count = positive < table.index_width ? positive : table.index_width; + for (size_t i = 0; i < count; ++i) { + const int32_t id = table.indices[row * table.index_width + i]; + if (id < 0 || static_cast(id) / table.page_tokens >= table.page_count) continue; + const auto page = static_cast(id) / table.page_tokens; + const auto offset = static_cast(id) % table.page_tokens; + const auto* raw = table.data + page * table.page_stride; + const auto* values = raw + offset * kValueBytes; + const auto* exponents = raw + table.page_tokens * kValueBytes + offset * 8; + float kv[4]; + float local = 0.0f; + for (size_t j = 0; j < 4; ++j) { + kv[j] = value_at(values, exponents, j * kThreads + lane); + local += query[j] * kv[j]; + } + reduce[lane] = local; + __syncthreads(); + for (unsigned width = kThreads / 2; width; width /= 2) { + if (lane < width) reduce[lane] += reduce[lane + width]; + __syncthreads(); + } + if (lane == 0) { + const float score = reduce[0] * scale; + const float next = fmaxf(score, maximum); + past_weight = maximum == -INFINITY ? 0.0f : expf(maximum - next); + new_weight = next == INFINITY ? 0.0f : expf(score - next); + if (maximum == INFINITY) past_weight = 1.0f; + maximum = next; + denominator = denominator * past_weight + new_weight; + } + __syncthreads(); + for (size_t j = 0; j < 4; ++j) { + accum[j] = accum[j] * past_weight + kv[j] * new_weight; + } + __syncthreads(); + } + } + __syncthreads(); + for (size_t j = 0; j < 4; ++j) { + out[block * kDim + j * kThreads + lane] = Caster::template Cast( + denominator ? accum[j] / denominator : 0.0f); + } + __syncthreads(); + } +} +constexpr size_t kMaxKeyTile = 128; + +static __device__ bool valid_key(const Pages& table, int32_t id) { + return id >= 0 && static_cast(id) / table.page_tokens < table.page_count; +} + +static __device__ void load_key(const Pages& table, int32_t id, unsigned lane, float (&kv)[4]) { + const size_t page = static_cast(id) / table.page_tokens; + const size_t offset = static_cast(id) % table.page_tokens; + const auto* raw = table.data + page * table.page_stride; + const auto* values = raw + offset * kValueBytes; + const auto* exponents = raw + table.page_tokens * kValueBytes + offset * 8; + for (size_t j = 0; j < 4; ++j) { + kv[j] = value_at(values, exponents, j * kThreads + lane); + } +} + +template +__global__ void Attend(const T* q, const float* sink, Pages first, Pages second, + T* out, size_t rows, size_t heads, float scale, int parallel_queries) { + using Convert = Caster; + __shared__ float reduce[kThreads]; + __shared__ float scores[kMaxKeyTile]; + __shared__ float weights[kMaxKeyTile]; + __shared__ float prior_weight, tile_weight, denominator; + const unsigned lane = threadIdx.x; + const bool online_tiles = rows >= static_cast(parallel_queries) && second.data; + const size_t keys_per_tile = rows > 1 && !second.data ? 128 : 64; + for (size_t block = blockIdx.x; block < rows * heads; block += gridDim.x) { + const auto row = block / heads, head = block % heads; + float accum[4] = {}; + float query[4]; + for (size_t j = 0; j < 4; ++j) { + query[j] = Convert::template Cast(q[block * kDim + j * kThreads + lane]); + } + float maximum = -INFINITY; + if (lane == 0) denominator = 0.0f; + for (int source = 0; source < 2; ++source) { + const Pages table = source == 0 ? first : second; + if (!table.data) continue; + const int32_t length = table.lengths[row]; + const size_t positive = length <= 0 ? 0 : static_cast(length); + const size_t count = positive < table.index_width ? positive : table.index_width; + const auto* indices = table.indices + row * table.index_width; + for (size_t start = 0; start < count; start += keys_per_tile) { + const size_t remaining = count - start; + const size_t tile_size = remaining < keys_per_tile ? remaining : keys_per_tile; + for (size_t i = 0; i < tile_size; ++i) { + const int32_t id = indices[start + i]; + if (!valid_key(table, id)) { + if (lane == 0) scores[i] = -INFINITY; + continue; + } + float kv[4], dot = 0.0f; + load_key(table, id, lane, kv); + for (size_t j = 0; j < 4; ++j) dot += query[j] * kv[j]; + reduce[lane] = dot; + __syncthreads(); + for (unsigned width = kThreads / 2; width; width /= 2) { + if (lane < width) reduce[lane] += reduce[lane + width]; + __syncthreads(); + } + if (lane == 0) scores[i] = reduce[0] * scale; + } + if (lane == 0) { + float tile_max = -INFINITY; + for (size_t i = 0; i < tile_size; ++i) tile_max = fmaxf(tile_max, scores[i]); + const float next = fmaxf(maximum, tile_max); + const float shift = online_tiles ? next : tile_max; + float mass = 0.0f; + for (size_t i = 0; i < tile_size; ++i) { + const float score = scores[i]; + const float prob = score == -INFINITY ? 0.0f : + (score == shift ? 1.0f : expf(score - shift)); + mass += prob; + weights[i] = Convert::template Cast(Convert::template Cast(prob)); + } + prior_weight = maximum == next ? 1.0f : expf(maximum - next); + tile_weight = shift == next ? 1.0f : expf(shift - next); + maximum = next; + denominator = denominator * prior_weight + mass * tile_weight; + } + __syncthreads(); + float weighted[4] = {}; + for (size_t i = 0; i < tile_size; ++i) { + const float weight = weights[i]; + if (weight == 0.0f) continue; + float kv[4]; + load_key(table, indices[start + i], lane, kv); + for (size_t j = 0; j < 4; ++j) weighted[j] += weight * kv[j]; + } + for (size_t j = 0; j < 4; ++j) { + accum[j] = accum[j] * prior_weight + weighted[j] * tile_weight; + } + __syncthreads(); + } + } + if (lane == 0 && denominator != 0.0f) { + denominator += expf(sink[head] - maximum); + } + __syncthreads(); + for (size_t j = 0; j < 4; ++j) { + out[block * kDim + j * kThreads + lane] = Convert::template Cast( + denominator ? accum[j] / denominator : 0.0f); + } + __syncthreads(); + } +} + +} // namespace v4_sparse_mla_detail + +template <> +class Operator + : public DeepseekV4SparseMla { + public: + static bool supports(const Tensor q, const Tensor swa, const Tensor indices, + const Tensor lengths, const Tensor sink, + const std::optional& extra, + const std::optional& extra_indices, + const std::optional& extra_lengths, + int64_t extra_page_size, double scale, const Tensor out) { + using namespace v4_sparse_mla_detail; + if (q.device().type() != Device::Type::kHygon || q.ndim() != 3 || q.size(2) != kDim || + !q.size(0) || !q.size(1) || q.size(0) > std::numeric_limits::max() / q.size(1) / kDim || + (q.dtype() != DataType::kBFloat16 && q.dtype() != DataType::kFloat32) || + out.dtype() != q.dtype() || out.shape() != q.shape() || + sink.dtype() != DataType::kFloat32 || sink.shape() != Tensor::Shape{q.size(1)} || + !std::isfinite(scale) || scale <= 0.0 || scale > std::numeric_limits::max() || + static_cast(extra) != static_cast(extra_indices) || + static_cast(extra) != static_cast(extra_lengths)) + return false; + const auto valid = [&](const Tensor& t) { + return t.device() == q.device() && t.IsContiguous(); + }; + const auto pages_valid = [&](const Tensor& p, const Tensor& ids, const Tensor& lens, size_t page_tokens) { + return p.dtype() == DataType::kUInt8 && p.ndim() == 2 && p.size(0) > 0 && + p.size(1) >= page_tokens * 584 && p.size(1) % 2 == 0 && + ids.dtype() == DataType::kInt32 && ids.ndim() == 2 && ids.size(0) == q.size(0) && + ids.size(1) > 0 && lens.dtype() == DataType::kInt32 && lens.shape() == Tensor::Shape{q.size(0)} && + valid(p) && valid(ids) && valid(lens); + }; + return valid(q) && valid(sink) && valid(out) && pages_valid(swa, indices, lengths, 256) && + (!extra || ((extra_page_size == 64 || extra_page_size == 2) && + pages_valid(*extra, *extra_indices, *extra_lengths, extra_page_size))); + } + + Operator(const Tensor q, const Tensor swa, const Tensor indices, + const Tensor lengths, const Tensor sink, const std::optional extra, + const std::optional extra_indices, const std::optional extra_lengths, + int64_t extra_page_size, double scale, Tensor out) + : DeepseekV4SparseMla(q, swa, indices, lengths, sink, extra, extra_indices, extra_lengths, extra_page_size, scale, out) { + if (!supports(q, swa, indices, lengths, sink, extra, extra_indices, extra_lengths, extra_page_size, scale, out)) + throw std::invalid_argument("DeepSeek V4 sparse MLA inputs are unsupported"); + if (cudaDeviceGetAttribute(¶llel_queries_, cudaDevAttrMultiProcessorCount, q.device().index()) != cudaSuccess) + throw std::runtime_error("DeepSeek V4 sparse MLA device query failed"); +#ifdef INFINIOPS_HAVE_HYGON_FLASHMLA + vendor_ = q.dtype()==DataType::kBFloat16 && q.size(1)<=64 && q.size(0)<=3072; + if (vendor_) workspace_bytes_=hygon_flash_mla::workspace_bytes(q.size(0),parallel_queries_); +#endif + } + + bool capture_safe() const override { return true; } + size_t workspace_size_in_bytes() const override { return workspace_bytes_; } + + void operator()(const Tensor q, const Tensor swa, const Tensor indices, + const Tensor lengths, const Tensor sink, const std::optional extra, + const std::optional extra_indices, const std::optional extra_lengths, + int64_t extra_page_size, double scale, Tensor out) const override { + using namespace v4_sparse_mla_detail; + if (!supports(q, swa, indices, lengths, sink, extra, extra_indices, extra_lengths, extra_page_size, scale, out)) + throw std::invalid_argument("DeepSeek V4 sparse MLA inputs changed"); + const auto page = [](const Tensor& p, const Tensor& ids, const Tensor& lens, size_t tokens) { + return Pages{static_cast(p.data()), static_cast(ids.data()), + static_cast(lens.data()), p.size(0), p.size(1), tokens, ids.size(1)}; + }; + const auto first = page(swa, indices, lengths, 256); + const auto second = extra ? page(*extra, *extra_indices, *extra_lengths, extra_page_size) : Pages{}; +#ifdef INFINIOPS_HAVE_HYGON_FLASHMLA + if (vendor_) { + if(!workspace_ || workspace_size_in_bytes_(p.page_count),static_cast(p.page_tokens), + static_cast(p.page_stride),static_cast(p.index_width)}; + }; + hygon_flash_mla::run({q.data(),static_cast(sink.data()),out.data(), + workspace_,workspace_size_in_bytes_,stream_,static_cast(q.size(0)), + static_cast(q.size(1)),parallel_queries_,static_cast(scale), + convert(first),convert(second)}); + return; + } +#endif + const size_t blocks = q.size(0) * q.size(1); + const auto launch = [&](auto tag) { + using T = decltype(tag); + if constexpr (std::is_same_v) { + AttendUnrounded<<(blocks < 65535 ? blocks : 65535), kThreads, 0, + static_cast(stream_)>>>( + static_cast(q.data()), static_cast(sink.data()), + first, second, static_cast(out.data()), q.size(0), q.size(1), static_cast(scale)); + } else { + Attend<<(blocks < 65535 ? blocks : 65535), kThreads, 0, + static_cast(stream_)>>>( + static_cast(q.data()), static_cast(sink.data()), + first, second, static_cast(out.data()), q.size(0), q.size(1), static_cast(scale), parallel_queries_); + } + }; + if (q.dtype() == DataType::kFloat32) launch(float{}); + else launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 sparse MLA launch failed"); + } + private: + int parallel_queries_{}; + bool vendor_{}; + size_t workspace_bytes_{}; +}; + +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/deepseek_v4_store_mla/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_store_mla/kernel.h new file mode 100644 index 000000000..1ee3b4fdb --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_store_mla/kernel.h @@ -0,0 +1,111 @@ +#ifndef INFINI_OPS_HYGON_DEEPSEEK_V4_STORE_MLA_H_ +#define INFINI_OPS_HYGON_DEEPSEEK_V4_STORE_MLA_H_ + +#include +#include + +#include + +#include "base/deepseek_v4_store_mla.h" +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops { +namespace v4_store_mla_detail { + +template +static __global__ void Store(const T* kv, const int32_t* slots, uint8_t* pages, + size_t rows, size_t page_count, size_t page_stride, size_t page_tokens) { + using Convert = Caster; + constexpr size_t nope_dim = 448, value_bytes = 576; + __shared__ float max_abs[7][64]; + __shared__ float inverse_scale[7]; + __shared__ uint8_t exponents[7]; + const unsigned lane = threadIdx.x; + for (size_t row = blockIdx.x; row < rows; row += gridDim.x) { + const int32_t id = slots[row]; + if (id < 0 || static_cast(id) / page_tokens >= page_count) continue; + const auto* input = kv + row * 512; + for (size_t i = lane; i < nope_dim; i += blockDim.x) { + max_abs[i / 64][i % 64] = fabsf(Convert::template Cast(input[i])); + } + __syncthreads(); + for (unsigned step = 32; step; step /= 2) { + for (size_t i = lane; i < nope_dim; i += blockDim.x) { + const auto channel = i % 64; + if (channel < step) { + auto* group = max_abs[i / 64]; + group[channel] = fmaxf(group[channel], group[channel + step]); + } + } + __syncthreads(); + } + if (lane < 7) { + // Match the HCU FlashMLA writer's conservative E4M3 scale range. + const float raw_scale = fmaxf(max_abs[lane][0], 1e-4f) / 224.0f; + const uint32_t bits = __float_as_uint(raw_scale); + const int encoded = min(int((bits >> 23) & 255) + int((bits & 0x7fffff) != 0), 254); + exponents[lane] = static_cast(encoded); + inverse_scale[lane] = exp2f(127.0f - encoded); + } + __syncthreads(); + const auto offset = static_cast(id) % page_tokens; + auto* page = pages + static_cast(id) / page_tokens * page_stride; + auto* values = page + offset * value_bytes; + for (size_t i = lane; i < nope_dim; i += blockDim.x) { + values[i] = __nv_cvt_float_to_fp8(Convert::template Cast(input[i]) * inverse_scale[i / 64], + __NV_SATFINITE, __NV_E4M3); + } + if (lane < 64) { + reinterpret_cast<__nv_bfloat16*>(values + nope_dim)[lane] = + Convert::template Cast<__nv_bfloat16>(input[nope_dim + lane]); + } + if (lane < 8) page[page_tokens * value_bytes + offset * 8 + lane] = lane < 7 ? exponents[lane] : 0; + __syncthreads(); + } +} +} // namespace v4_store_mla_detail + +template <> +class Operator : public DeepseekV4StoreMla { + public: + static bool supports(const Tensor kv, const Tensor slots, int64_t page_tokens, const Tensor pages) { + return kv.device().type() == Device::Type::kHygon && + kv.device() == slots.device() && kv.device() == pages.device() && + (kv.dtype() == DataType::kBFloat16 || kv.dtype() == DataType::kFloat32) && + slots.dtype() == DataType::kInt32 && + pages.dtype() == DataType::kUInt8 && kv.ndim() == 2 && kv.size(0) > 0 && + kv.size(1) == 512 && slots.shape() == Tensor::Shape{kv.size(0)} && + (page_tokens == 256 || page_tokens == 64 || page_tokens == 2) && + pages.ndim() == 2 && pages.size(0) > 0 && + pages.size(1) >= static_cast(page_tokens) * 584 && pages.size(1) % 2 == 0 && + kv.IsContiguous() && slots.IsContiguous() && pages.IsContiguous(); + } + + Operator(const Tensor kv, const Tensor slots, int64_t page_tokens, Tensor pages) + : DeepseekV4StoreMla(kv, slots, page_tokens, pages) { + if (!supports(kv, slots, page_tokens, pages)) + throw std::invalid_argument("DeepSeek V4 raw MLA cache store inputs are unsupported"); + } + + bool capture_safe() const override { return true; } + + void operator()(const Tensor kv, const Tensor slots, int64_t page_tokens, Tensor pages) const override { + if (!supports(kv, slots, page_tokens, pages)) + throw std::invalid_argument("DeepSeek V4 raw MLA cache store inputs changed"); + const auto rows = kv.size(0); + auto launch = [&](auto tag) { + using T = decltype(tag); + v4_store_mla_detail::Store<<(rows < 65535 ? rows : 65535), 128, 0, + static_cast(stream_)>>>( + static_cast(kv.data()), static_cast(slots.data()), + static_cast(pages.data()), rows, pages.size(0), pages.size(1), page_tokens); + }; + if (kv.dtype() == DataType::kFloat32) launch(float{}); + else launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 cache store launch failed"); + } +}; + +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h new file mode 100644 index 000000000..24a82fa06 --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h @@ -0,0 +1,237 @@ +#ifndef INFINI_OPS_HYGON_DEEPSEEK_V4_W8A8_EXPERTS_H_ +#define INFINI_OPS_HYGON_DEEPSEEK_V4_W8A8_EXPERTS_H_ + +#include + +#include +#include +#include + +#include "base/deepseek_v4_w8a8_experts.h" +#include "native/cuda/hygon/dynamic_int8_quant.cuh" + +namespace infini::ops { +namespace deepseek_v4_w8a8_detail { + +using hygon_dynamic_int8::kThreads; +using hygon_dynamic_int8::Quantize; + +// HCU shared dense projection switches to its assembly epilogue above 16 +// tokens. Routed GEMMs always use the joint-scale epilogue. +static __device__ float ScaledDot(int32_t accumulator, float token_scale, float weight_scale, + bool joint_scale) { + if (!joint_scale) + return __fmul_rn(__fmul_rn(static_cast(accumulator), token_scale), weight_scale); + const float scale = __fmul_rn(token_scale, weight_scale); + return __fmul_rn(static_cast(accumulator), scale); +} + +template +__global__ void GateUp(const int8_t* x, const float* input_scale, const int64_t* indices, + const int8_t* w, const float* scale, float* activated, size_t routes, + size_t top_k, size_t experts, size_t hidden, + size_t inner, float limit, bool routed) { + __shared__ int32_t gate_sum[kThreads]; + __shared__ int32_t up_sum[kThreads]; + const unsigned lane = threadIdx.x; + for (size_t index = blockIdx.x; index < routes * inner; index += gridDim.x) { + const size_t route = index / inner; + const auto expert = indices[route]; + if (expert < 0 || static_cast(expert) >= experts) { + if (lane == 0) activated[index] = 0.0f; + continue; + } + const size_t gate = static_cast(expert) * (2 * inner) + index % inner; + const size_t up = gate + inner; + const auto* row = x + (route / top_k) * hidden; + int32_t g = 0, u = 0; + for (size_t i = lane; i < hidden; i += kThreads) { + g += static_cast(row[i]) * w[gate * hidden + i]; + u += static_cast(row[i]) * w[up * hidden + i]; + } + gate_sum[lane] = g; + up_sum[lane] = u; + __syncthreads(); + for (unsigned width = kThreads / 2; width; width /= 2) { + if (lane < width) { + gate_sum[lane] += gate_sum[lane + width]; + up_sum[lane] += up_sum[lane + width]; + } + __syncthreads(); + } + if (lane == 0) { + using Convert = Caster; + const float token_scale = input_scale[route / top_k]; + const auto cast_gate = Convert::template Cast(ScaledDot(gate_sum[0], token_scale, scale[gate], routed || routes / top_k > 16)); + const auto cast_up = Convert::template Cast(ScaledDot(up_sum[0], token_scale, scale[up], routed || routes / top_k > 16)); + float clipped_gate = fminf(Convert::template Cast(cast_gate), limit); + if (routed) clipped_gate = fmaxf(clipped_gate, -limit); + const float clipped_up = fminf(fmaxf(Convert::template Cast(cast_up), -limit), limit); + float gate_activation = clipped_gate / (1.0f + expf(-clipped_gate)); + if (routed) gate_activation = Convert::template Cast(Convert::template Cast(gate_activation)); + const float value = gate_activation * clipped_up; + activated[index] = Convert::template Cast(Convert::template Cast(value)); + } + __syncthreads(); + } +} + +template +__global__ void Down(const int8_t* activated, const float* activation_scale, const int64_t* indices, + const float* weights, const int8_t* w, const float* scale, + T* out, size_t tokens, size_t top_k, size_t experts, + size_t hidden, size_t inner, bool routed, float routed_scale) { + __shared__ int32_t sum[kThreads]; + const unsigned lane = threadIdx.x; + for (size_t index = blockIdx.x; index < tokens * hidden; index += gridDim.x) { + const size_t token = index / hidden; + const size_t channel = index % hidden; + float output = 0.0f; + for (size_t k = 0; k < top_k; ++k) { + const size_t route = token * top_k + k; + const auto expert = indices[route]; + if (expert < 0 || static_cast(expert) >= experts) continue; + const size_t channel_idx = static_cast(expert) * hidden + channel; + int32_t local = 0; + for (size_t i = lane; i < inner; i += kThreads) { + local += static_cast(activated[route * inner + i]) * w[channel_idx * inner + i]; + } + sum[lane] = local; + __syncthreads(); + for (unsigned width = kThreads / 2; width; width /= 2) { + if (lane < width) sum[lane] += sum[lane + width]; + __syncthreads(); + } + if (lane == 0) { + using Convert = Caster; + const float token_scale = activation_scale[route], weight_scale = scale[channel_idx]; + if (routed) { + const float weighted = ScaledDot(sum[0], token_scale, weight_scale, true) * weights[route]; + output += Convert::template Cast(Convert::template Cast(weighted)); + } else { + output += ScaledDot(sum[0], token_scale, weight_scale, tokens > 16) * weights[route]; + } + } + __syncthreads(); + } + if (lane == 0) { + out[index] = Caster::template Cast(output * routed_scale); + } + } +} + +} // namespace deepseek_v4_w8a8_detail + +template <> +class Operator + : public DeepseekV4W8a8Experts { + public: + static bool supports(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, + double limit, const std::optional routed_scale, const Tensor out) { + if (x.device().type() != Device::Type::kHygon || x.ndim() != 2 || + weights.ndim() != 2 || gate_up.ndim() != 3 || down.ndim() != 3 || + out.shape() != x.shape() || out.dtype() != x.dtype() || + (x.dtype() != DataType::kBFloat16 && x.dtype() != DataType::kFloat32) || + weights.dtype() != DataType::kFloat32 || indices.dtype() != DataType::kInt64 || + weights.shape() != indices.shape() || weights.size(0) != x.size(0) || + gate_up.dtype() != DataType::kInt8 || down.dtype() != DataType::kInt8 || + gate_up_scale.dtype() != DataType::kFloat32 || down_scale.dtype() != DataType::kFloat32 || + !std::isfinite(limit) || limit <= 0.0 || limit > std::numeric_limits::max() || + (routed_scale && (!std::isfinite(*routed_scale) || *routed_scale <= 0.0 || + *routed_scale > std::numeric_limits::max()))) + return false; + + const auto experts = down.size(0), hidden = x.size(1), inner = down.size(2); + const auto size_limit = std::numeric_limits::max(); + if (!experts || !hidden || !inner || !x.size(0) || !weights.size(1) || + hidden > kMaxDotWidth || inner > kMaxDotWidth || + x.size(0) > size_limit / (hidden + sizeof(float)) / 2 || + x.size(0) > size_limit / weights.size(1) / (inner * (sizeof(float) + 1) + sizeof(float)) / 2 || + gate_up.shape() != Tensor::Shape{experts, 2 * inner, hidden} || + down.shape() != Tensor::Shape{experts, hidden, inner} || + gate_up_scale.shape() != Tensor::Shape{experts, 2 * inner, 1} || + down_scale.shape() != Tensor::Shape{experts, hidden, 1}) + return false; + for (const auto* t : {&x, &weights, &indices, &gate_up, &gate_up_scale, &down, &down_scale, &out}) { + if (t->device() != x.device() || !t->IsContiguous()) return false; + } + return true; + } + + Operator(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, double limit, const std::optional routed_scale, Tensor out) + : DeepseekV4W8a8Experts(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out) { + if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out)) + throw std::invalid_argument("DeepSeek V4 W8A8 expert input is unsupported"); + workspace_bytes_ = workspace_bytes(x, weights, down.size(2)); + } + + std::size_t workspace_size_in_bytes() const override { return workspace_bytes_; } + bool capture_safe() const override { return true; } + + void operator()(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, + double limit, const std::optional routed_scale, Tensor out) const override { + if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out) || + workspace_bytes(x, weights, down.size(2)) != workspace_bytes_ || + workspace_size_in_bytes_ < workspace_bytes_ || !workspace_) + throw std::invalid_argument("DeepSeek V4 W8A8 expert inputs or workspace changed"); + if (x.dtype() == DataType::kFloat32) { + launch(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out); + } else { + launch>( + x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out); + } + } + + private: + static constexpr size_t kMaxDotWidth = std::numeric_limits::max() / (127 * 128); + size_t workspace_bytes_{}; + + static size_t workspace_bytes(const Tensor x, const Tensor weights, size_t inner) { + const size_t tokens = x.size(0), routes = weights.numel(); + return (routes * inner + tokens + routes) * sizeof(float) + tokens * x.size(1) + routes * inner; + } + + template + void launch(const Tensor x, const Tensor weights, const Tensor indices, + const Tensor gate_up, const Tensor gate_up_scale, + const Tensor down, const Tensor down_scale, double limit, const std::optional routed_scale, Tensor out) const { + using namespace deepseek_v4_w8a8_detail; + const auto stream = static_cast(stream_); + const auto routes = weights.numel(), inner = down.size(2); + const auto* ids = static_cast(indices.data()); + const auto tokens = x.size(0), hidden = x.size(1); + auto* activated = static_cast(workspace_); + auto* input_scale = activated + routes * inner; + auto* activation_scale = input_scale + tokens; + auto* q_input = reinterpret_cast(activation_scale + routes); + auto* q_activated = q_input + tokens * hidden; + const auto grid = [](size_t rows) { return static_cast(rows < 65535 ? rows : 65535); }; + Quantize<<>>( + static_cast(x.data()), q_input, input_scale, tokens, hidden); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 input quantization launch failed"); + GateUp<<>>( + q_input, input_scale, ids, static_cast(gate_up.data()), + static_cast(gate_up_scale.data()), activated, routes, weights.size(1), + down.size(0), hidden, inner, static_cast(limit), routed_scale.has_value()); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 gate/up launch failed"); + Quantize<<>>( + activated, q_activated, activation_scale, routes, inner); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 activation quantization launch failed"); + Down<<>>( + q_activated, activation_scale, ids, static_cast(weights.data()), + static_cast(down.data()), static_cast(down_scale.data()), + static_cast(out.data()), tokens, weights.size(1), down.size(0), hidden, inner, + routed_scale.has_value(), static_cast(routed_scale.value_or(1.0))); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 down launch failed"); + } +}; + +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/i8_gemm/kernel.h b/src/native/cuda/hygon/ops/i8_gemm/kernel.h new file mode 100644 index 000000000..5cdccb661 --- /dev/null +++ b/src/native/cuda/hygon/ops/i8_gemm/kernel.h @@ -0,0 +1,80 @@ +#pragma once +#include +#include "base/i8_gemm.h" +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops { +namespace hygon_i8_gemm { + +template +__global__ void matmul(T* out, const int8_t* a, const float* as, const int8_t* b, + const float* bs, const T* bias, size_t m, size_t n, size_t k, + ptrdiff_t bias_row, ptrdiff_t bias_col) { + __shared__ int32_t partial[128]; + for (size_t idx = blockIdx.x; idx < m*n; idx += gridDim.x) { + const size_t row = idx/n, col = idx%n; + int32_t sum = 0; + for (size_t j = threadIdx.x; j < k; j += blockDim.x) + sum += int32_t(a[row*k+j]) * b[col*k+j]; + partial[threadIdx.x] = sum; + __syncthreads(); + for (unsigned step = 64; step; step /= 2) { + if (threadIdx.x < step) partial[threadIdx.x] += partial[threadIdx.x+step]; + __syncthreads(); + } + if (threadIdx.x == 0) { + using Convert = Caster; + // The W8A8 epilogue rounds the joint scale before applying it to the dot. + const float scale = __fmul_rn(as[row], bs[col]); + float value = __fmul_rn(static_cast(partial[0]), scale); + if (bias) value += Convert::template Cast(bias[row*bias_row+col*bias_col]); + out[idx] = Convert::template Cast(value); + } + __syncthreads(); + } +} + +} // namespace hygon_i8_gemm + +template <> +class Operator : public I8Gemm { + public: + using I8Gemm::I8Gemm; + bool capture_safe() const override { return true; } + static bool supports(Tensor c, Tensor a, Tensor as, Tensor b, Tensor bs, + const std::optional bias) { + if (a.ndim() != 2 || b.ndim() != 2 || c.ndim() != 2 || + a.size(1) == 0 || a.size(1) > INT32_MAX/(128*128) || b.size(0) != a.size(1) || + a.dtype() != DataType::kInt8 || b.dtype() != DataType::kInt8 || + as.dtype() != DataType::kFloat32 || bs.dtype() != DataType::kFloat32 || + (c.dtype() != DataType::kFloat32 && c.dtype() != DataType::kBFloat16 && c.dtype() != DataType::kFloat16) || + c.shape() != Tensor::Shape{a.size(0),b.size(1)} || + as.shape() != Tensor::Shape{a.size(0),1} || bs.shape() != Tensor::Shape{b.size(1),1} || + !a.IsContiguous() || !c.IsContiguous() || !as.IsContiguous() || !bs.IsContiguous() || + b.stride(0) != 1 || b.stride(1) != static_cast(b.size(0)) || + c.device().type() != Device::Type::kHygon || a.device() != c.device() || + b.device() != c.device() || as.device() != c.device() || bs.device() != c.device()) return false; + return !bias || (bias->device() == c.device() && bias->dtype() == c.dtype() && + bias->shape() == c.shape() && bias->stride(0) >= 0 && bias->stride(1) >= 0); + } + void operator()(Tensor c, Tensor a, Tensor as, Tensor b, Tensor bs, + const std::optional bias) const override { + if (!supports(c,a,as,b,bs,bias)) throw std::invalid_argument("Unsupported Hygon INT8 GEMM"); + if (!c.numel()) return; + auto launch = [&](auto tag) { + using T = decltype(tag); + hygon_i8_gemm::matmul<<(std::min(c.numel(),65535)), + 128, 0, static_cast(stream_)>>>( + static_cast(c.data()), static_cast(a.data()), + static_cast(as.data()), static_cast(b.data()), + static_cast(bs.data()), bias ? static_cast(bias->data()) : nullptr, + c.size(0),c.size(1),a.size(1),bias ? bias->stride(0) : 0,bias ? bias->stride(1) : 0); + }; + if (c.dtype() == DataType::kFloat32) launch(float{}); + else if (c.dtype() == DataType::kFloat16) launch(__half{}); + else launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("Hygon INT8 GEMM launch failed"); + } +}; + +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h b/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h new file mode 100644 index 000000000..51c47984f --- /dev/null +++ b/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h @@ -0,0 +1,39 @@ +#pragma once +#include "base/per_channel_quant_i8.h" +#include "native/cuda/hygon/dynamic_int8_quant.cuh" + +namespace infini::ops { + +template <> +class Operator : public PerChannelQuantI8 { + public: + using PerChannelQuantI8::PerChannelQuantI8; + bool capture_safe() const override { return true; } + static bool supports(Tensor x, Tensor packed, Tensor scale) { + return x.device().type() == Device::Type::kHygon && packed.device() == x.device() && + scale.device() == x.device() && x.ndim() == 2 && x.size(1) > 0 && + (x.dtype() == DataType::kBFloat16 || x.dtype() == DataType::kFloat32 || + x.dtype() == DataType::kFloat16) && packed.dtype() == DataType::kInt8 && + scale.dtype() == DataType::kFloat32 && packed.shape() == x.shape() && + scale.shape() == Tensor::Shape{x.size(0), 1} && + x.IsContiguous() && packed.IsContiguous() && scale.IsContiguous(); + } + void operator()(Tensor x, Tensor packed, Tensor scale) const override { + if (!supports(x, packed, scale)) throw std::invalid_argument("Unsupported Hygon INT8 quantization"); + if (!x.size(0)) return; + using namespace hygon_dynamic_int8; + auto launch = [&](auto tag) { + using T = decltype(tag); + Quantize<<(std::min(x.size(0), 65535)), + kThreads, 0, static_cast(stream_)>>>( + static_cast(x.data()), static_cast(packed.data()), + static_cast(scale.data()), x.size(0), x.size(1)); + }; + if (x.dtype() == DataType::kFloat32) launch(float{}); + else if (x.dtype() == DataType::kFloat16) launch(__half{}); + else launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("Hygon INT8 quantization launch failed"); + } +}; + +} // namespace infini::ops diff --git a/third_party/flashmla_hygon/CUTLASS_LICENSE.txt b/third_party/flashmla_hygon/CUTLASS_LICENSE.txt new file mode 100644 index 000000000..2913ab80f --- /dev/null +++ b/third_party/flashmla_hygon/CUTLASS_LICENSE.txt @@ -0,0 +1,27 @@ +Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +SPDX-License-Identifier: BSD-3-Clause + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/third_party/flashmla_hygon/LICENSE b/third_party/flashmla_hygon/LICENSE new file mode 100644 index 000000000..5c48bdc9f --- /dev/null +++ b/third_party/flashmla_hygon/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 DeepSeek + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/flashmla_hygon/README.md b/third_party/flashmla_hygon/README.md new file mode 100644 index 000000000..0e4fc4508 --- /dev/null +++ b/third_party/flashmla_hygon/README.md @@ -0,0 +1,15 @@ +# Hygon FlashMLA kernel subset + +This directory pins the include closure for the gfx93 MODEL1 sparse FP8 decode +instantiation and the gfx9 scheduling/combination kernels. InfiniOps builds them +with DTK AICC; no Python extension or PyTorch runtime is required. + +The original file hashes and copied file hashes are in sources.json. Sources +were imported from the existing flashmla-hygon checkout on the development host. +Only the required kernel/header closure is included. References to ATen scalar +macros are replaced by their equivalent definitions; the pinned kernel arithmetic +is unchanged. + +FlashMLA and CUTLASS licenses are preserved in LICENSE and CUTLASS_LICENSE.txt. +The InfiniOps runtime bridge lives in src/native/cuda/hygon/flash_mla relative to +the InfiniOps root. The default integration is enabled for gfx936/gfx938. diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/axpby_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/axpby_hip.hpp new file mode 100644 index 000000000..5f3a0df7f --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/axpby_hip.hpp @@ -0,0 +1,92 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +// +// Accept mutable temporaries +// +template +CUTE_HOST_DEVICE +void +axpby(Alpha const& alpha, + Tensor const& x, + Beta const& beta, + Tensor && y) +{ + return axpby(alpha, x, beta, y); +} + +// +// AXPBY +// +template +CUTE_HOST_DEVICE +void +axpby(Alpha const& alpha, + Tensor const& x, + Beta const& beta, + Tensor & y) +{ + auto isBetaZero = [&] () { + if constexpr (is_complex::value) { + return beta.real() == Int<0>{} && beta.imag() == Int<0>{}; + } + else { + return beta == Int<0>{}; + } + } (); + + CUTE_UNROLL + for (int i = 0; i < size(x); ++i) { + y(i) = (isBetaZero ? alpha * x(i) : alpha * x(i) + beta * y(i)); + } +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/clear_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/clear_hip.hpp new file mode 100644 index 000000000..0d7fedef3 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/clear_hip.hpp @@ -0,0 +1,72 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +#include + +namespace cute +{ + +// +// Accept mutable temporaries +// +template +CUTE_HOST_DEVICE +void +clear(Tensor&& tensor) +{ + return clear(tensor); +} + +// +// Set elements to zero +// +template +CUTE_HOST_DEVICE +void +clear(Tensor& tensor) +{ + using T = typename Tensor::value_type; + + fill(tensor, T{}); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/copy_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/copy_hip.hpp new file mode 100644 index 000000000..4b058b898 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/copy_hip.hpp @@ -0,0 +1,324 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +#include + +namespace cute +{ + +// +// Accept mutable temporaries +// + +template +CUTE_HOST_DEVICE +void +copy_if(PrdTensor const& pred, + Tensor const& src, + Tensor && dst) +{ + return copy_if(pred, src, dst); +} + +template +CUTE_HOST_DEVICE +void +copy_if(Copy_Atom const& copy_atom, + PrdTensor const& pred, + Tensor const& src, + Tensor && dst) +{ + return copy_if(copy_atom, pred, src, dst); +} + +template +CUTE_HOST_DEVICE +void +copy_vec(Tensor const& src, + Tensor && dst) +{ + return copy_vec(src, dst); +} + +template +CUTE_HOST_DEVICE +void +copy(Tensor const& src, + Tensor && dst) +{ + return copy(src, dst); +} + +template +CUTE_HOST_DEVICE +void +copy(Copy_Atom const& copy_atom, + Tensor const& src, + Tensor && dst) +{ + return copy(copy_atom, src, dst); +} + +// +// copy_if -- Predicated Copy +// + +template +CUTE_HOST_DEVICE +void +copy_if(PrdTensor const& pred, + Tensor const& src, + Tensor & dst) +{ + + auto copy_op = select_elementwise_copy(src, dst); + + CUTE_UNROLL + for (int i = 0; i < size(src); ++i) { + if (pred(i)) { + copy_op.copy(src(i), dst(i)); + } + } +} + +// +// copy_if -- Predicated CopyAtom +// + +template +CUTE_HOST_DEVICE +void +copy_if(Copy_Atom const& copy_atom, + PredTensor const& pred, // (Rest...) + Tensor const& src, // (V,Rest...) + Tensor & dst) // (V,Rest...) +{ + static_assert(SrcLayout::rank == DstLayout::rank, "CopyAtom rank-mismatch."); + // + if constexpr (SrcLayout::rank == 1) { // Dispatch the copy + copy_atom.call(src, dst); + } else { // Loop over all but the first mode + + constexpr int R = SrcLayout::rank; + auto src_v = group_modes<1,R>(src); + auto dst_v = group_modes<1,R>(dst); + + + CUTE_UNROLL + for (int i = 0; i < size<1>(src_v); ++i) { + if (pred(i)) { + copy_atom.call(src_v(_,i), dst_v(_,i)); + } + } + } +} + +// +// copy_vec -- attempt vectorized copy with VecType +// + +template +CUTE_HOST_DEVICE +void +copy_vec(Tensor const& src, + Tensor & dst) +{ + using SrcType = typename SrcEngine::value_type; + using DstType = typename DstEngine::value_type; + if constexpr (sizeof(SrcType) == sizeof(DstType) && sizeof(VecType) > sizeof(DstType)) + { + /* @pre is_aligned(src.data()) && + * is_aligned(dst.data()) + */ + auto src_v = recast(src); + auto dst_v = recast(dst); + +#if 0 + if (thread0()) { + print("copy_vec -- vectorizing copy from %3db to %3db\n", int(8*sizeof(SrcType)), int(8*sizeof(VecType))); + print(" "); print(layout(src)); print(" => "); print(layout(src_v)); print("\n"); + print(" "); print(layout(dst)); print(" => "); print(layout(dst_v)); print("\n"); + } +#endif + + return copy_if(TrivialPredTensor{}, src_v, dst_v); + } else { +#if 0 + if (thread0()) { + print("copy_vec -- not vectorizing, copy with %3db and %3db\n", int(8*sizeof(SrcType)), int(8*sizeof(DstType))); + print(" "); print(layout(src)); print("\n"); + print(" "); print(layout(dst)); print("\n"); + } +#endif + + return copy_if(TrivialPredTensor{}, src, dst); + } +} + +// +// copy -- auto-vectorizing copy +// + +template +CUTE_HOST_DEVICE +void +copy(Tensor const& src, + Tensor & dst) +{ + constexpr int N = decltype(max_common_vector(src, dst))::value; + +#if 0 + if (thread0()) { + print("copy -- found a max_common_vector of %d\n", N); + print(" "); print(src.data()); print(" o "); print(layout(src)); print("\n"); + print(" "); print(dst.data()); print(" o "); print(layout(dst)); print("\n"); + } +#endif + + if constexpr (N <= 1) { + + return copy_if(TrivialPredTensor{}, src, dst); + } else { + + constexpr int vec_bits = N * sizeof_bits::value; + using VecType = uint_bit_t; + return copy_vec(src, dst); + } +} + +// +// copy -- CopyAtom +// + +template +CUTE_HOST_DEVICE +void +copy(Copy_Atom const& copy_atom, + Tensor const& src, + Tensor & dst) +{ + + return copy_if(copy_atom, TrivialPredTensor{}, src, dst); +} + +template +CUTE_HOST_DEVICE +void +copy(Copy_Atom const&, + Tensor const& src, + Tensor & dst) +{ + + return copy(src, dst); +} + +////////////////////////////////////////// +// Special Auto-Vectorizing Overloads +////////////////////////////////////////// + +#if defined(CUTE_COPY_ATOM_TMA_SM90_ENABLED) +template +CUTE_HOST_DEVICE +void +copy(Copy_Atom, CA_Args...> const& atom, + Tensor const& src, + Tensor & dst) +{ + using SrcType = typename SrcEngine::value_type; + using DstType = typename DstEngine::value_type; + static_assert(sizeof_bits::value == sizeof_bits::value); + static_assert((is_gmem::value && is_smem::value) || + (is_smem::value && is_gmem::value), + "Bulk Copy only supports gmem -> smem or smem -> gmem movement."); + // Do BulkCopy dispatch + using BULK_COPY_OP = conditional_t::value, + SM90_BULK_COPY_G2S, + SM90_BULK_COPY_S2G>; + + constexpr int N = decltype(max_common_vector(src, dst))::value; + + // Construct a new concrete Atom of the vector size + using N_BITS = Int::value>; + using COPY_ATOM = Copy_Atom, SrcType>; + auto bulk_atom = apply(atom.opargs_, [&](auto const&... args) { return COPY_ATOM{args...}; }); + + // Tile the src and dst to the Atom + auto tiler = right_inverse(dst.layout()).compose(Int{}); + +#if 0 + if (thread0()) { + print("copy -- found a max_common_vector of %d\n", N); + print(" "); print(src.data()); print(" o "); print(layout(src)); print("\n"); + print(" "); print(dst.data()); print(" o "); print(layout(dst)); print("\n"); + } +#endif + + return copy(bulk_atom, logical_divide(src, tiler), logical_divide(dst, tiler)); +} +#endif // #if defined(CUTE_COPY_ATOM_TMA_SM90_ENABLED) + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/fill_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/fill_hip.hpp new file mode 100644 index 000000000..219aae85e --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/fill_hip.hpp @@ -0,0 +1,93 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +// +// Accept mutable temporaries +// +template +CUTE_HOST_DEVICE +void +fill(Tensor&& tensor, T const& value) +{ + return fill(tensor, value); +} + +namespace detail +{ + +// Prefer fill(tensor.data(), value), if possible +template +CUTE_HOST_DEVICE +auto +fill(Tensor& tensor, T const& value, prefer<1>) + -> decltype(fill(tensor.data(), value)) +{ + fill(tensor.data(), value); +} + +// Default implementation +template +CUTE_HOST_DEVICE +void +fill(Tensor& tensor, T const& value, prefer<0>) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor); ++i) { + tensor(i) = value; + } +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE +void +fill(Tensor& tensor, T const& value) +{ + return detail::fill(tensor, value, prefer<1>{}); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/functional_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/functional_hip.hpp new file mode 100644 index 000000000..cbd6298e4 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/functional_hip.hpp @@ -0,0 +1,204 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +/** C++14 extensions */ + +namespace cute { + +/**************/ +/** Identity **/ +/**************/ + +struct identity { + template + CUTE_HOST_DEVICE constexpr + decltype(auto) operator()(T&& arg) const { + return std::forward(arg); + } +}; + +template +struct constant_fn { + template + CUTE_HOST_DEVICE constexpr + decltype(auto) operator()(T&&...) const { + return r_; + } + R r_; +}; + +/***********/ +/** Unary **/ +/***********/ + +#define CUTE_LEFT_UNARY_OP(NAME,OP) \ + struct NAME { \ + template \ + CUTE_HOST_DEVICE constexpr \ + decltype(auto) operator()(T&& arg) const { \ + return OP std::forward(arg); \ + } \ + } +#define CUTE_RIGHT_UNARY_OP(NAME,OP) \ + struct NAME { \ + template \ + CUTE_HOST_DEVICE constexpr \ + decltype(auto) operator()(T&& arg) const { \ + return std::forward(arg) OP ; \ + } \ + } +#define CUTE_NAMED_UNARY_OP(NAME,OP) \ + struct NAME { \ + template \ + CUTE_HOST_DEVICE constexpr \ + decltype(auto) operator()(T&& arg) const { \ + return OP (std::forward(arg)); \ + } \ + } + +CUTE_LEFT_UNARY_OP(unary_plus, +); +CUTE_LEFT_UNARY_OP(negate, -); +CUTE_LEFT_UNARY_OP(bit_not, ~); +CUTE_LEFT_UNARY_OP(logical_not, !); +CUTE_LEFT_UNARY_OP(dereference, *); +CUTE_LEFT_UNARY_OP(address_of, &); +CUTE_LEFT_UNARY_OP(pre_increment, ++); +CUTE_LEFT_UNARY_OP(pre_decrement, --); + +CUTE_RIGHT_UNARY_OP(post_increment, ++); +CUTE_RIGHT_UNARY_OP(post_decrement, --); + +CUTE_NAMED_UNARY_OP(abs_fn, abs); +CUTE_NAMED_UNARY_OP(conjugate, cute::conj); + +#undef CUTE_LEFT_UNARY_OP +#undef CUTE_RIGHT_UNARY_OP +#undef CUTE_NAMED_UNARY_OP + +/************/ +/** Binary **/ +/************/ + +#define CUTE_BINARY_OP(NAME,OP) \ + struct NAME { \ + template \ + CUTE_HOST_DEVICE constexpr \ + decltype(auto) operator()(T&& lhs, U&& rhs) const { \ + return std::forward(lhs) OP std::forward(rhs); \ + } \ + } +#define CUTE_NAMED_BINARY_OP(NAME,OP) \ + struct NAME { \ + template \ + CUTE_HOST_DEVICE constexpr \ + decltype(auto) operator()(T&& lhs, U&& rhs) const { \ + return OP (std::forward(lhs), std::forward(rhs)); \ + } \ + } + + +CUTE_BINARY_OP(plus, +); +CUTE_BINARY_OP(minus, -); +CUTE_BINARY_OP(multiplies, *); +CUTE_BINARY_OP(divides, /); +CUTE_BINARY_OP(modulus, %); + +CUTE_BINARY_OP(plus_assign, +=); +CUTE_BINARY_OP(minus_assign, -=); +CUTE_BINARY_OP(multiplies_assign, *=); +CUTE_BINARY_OP(divides_assign, /=); +CUTE_BINARY_OP(modulus_assign, %=); + +CUTE_BINARY_OP(bit_and, &); +CUTE_BINARY_OP(bit_or, |); +CUTE_BINARY_OP(bit_xor, ^); +CUTE_BINARY_OP(left_shift, <<); +CUTE_BINARY_OP(right_shift, >>); + +CUTE_BINARY_OP(bit_and_assign, &=); +CUTE_BINARY_OP(bit_or_assign, |=); +CUTE_BINARY_OP(bit_xor_assign, ^=); +CUTE_BINARY_OP(left_shift_assign, <<=); +CUTE_BINARY_OP(right_shift_assign, >>=); + +CUTE_BINARY_OP(logical_and, &&); +CUTE_BINARY_OP(logical_or, ||); + +CUTE_BINARY_OP(equal_to, ==); +CUTE_BINARY_OP(not_equal_to, !=); +CUTE_BINARY_OP(greater, >); +CUTE_BINARY_OP(less, <); +CUTE_BINARY_OP(greater_equal, >=); +CUTE_BINARY_OP(less_equal, <=); + +CUTE_NAMED_BINARY_OP(max_fn, cute::max); +CUTE_NAMED_BINARY_OP(min_fn, cute::min); + +#undef CUTE_BINARY_OP +#undef CUTE_NAMED_BINARY_OP + +/**********/ +/** Meta **/ +/**********/ + +template +struct bound_fn { + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator()(T&& arg) { + return fn_(arg_, std::forward(arg)); + } + + Fn fn_; + Arg arg_; +}; + +template +CUTE_HOST_DEVICE constexpr +auto +bind(Fn const& fn, Arg const& arg) { + return bound_fn{fn, arg}; +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/gemm_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/gemm_hip.hpp new file mode 100644 index 000000000..08f8ca9ca --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/gemm_hip.hpp @@ -0,0 +1,746 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +#include + +#include + +/** The gemm algorithm takes four (or three) tensors and computes + * D = A * B + C + * It dispatches based on the number of modes each tensor has: + * + * 1. `(V) x (V) => (V)`. + * The element-wise product of vectors. Dispatches to FMA or MMA. + * 2. `(M) x (N) => (M,N)`. + * The outer product of vectors. Dispatches to [3] with new mode K=(1). + * 3. `(M,K) x (N,K) => (M,N)`. + * The product of matrices. Dispatches to [5] with MMA vector-mode V. + * 4. `(V,M) x (V,N) => (V,M,N)`. + * The batched outer product of vectors. Accounts for register reuse and dispatches to [1] for each (m,n). + * 5. `(V,M,K) x (V,N,K) => (V,M,N)`. + * The batched product of matrices. Dispatches to [4] for each (k). + */ + +namespace cute +{ + +// +// Three arguments to four +// + +template +CUTE_HOST_DEVICE +void +gemm(Tensor const& A, + Tensor const& B, + Tensor & C) +{ + return gemm(C, A, B, C); +} + +template +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor const& A, + Tensor const& B, + Tensor & C) +{ + return gemm(mma, C, A, B, C); +} + +// +// Accept mutable temporaries +// + +template +CUTE_HOST_DEVICE +void +gemm(Tensor const& A, + Tensor const& B, + Tensor && C) +{ + return gemm(C, A, B, C); +} + +template +CUTE_HOST_DEVICE +void +gemm(Tensor && D, + Tensor const& A, + Tensor const& B, + Tensor const& C) +{ + return gemm(D, A, B, C); +} + +template +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor const& A, + Tensor const& B, + Tensor && C) +{ + return gemm(mma, C, A, B, C); +} + +template +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor && D, + Tensor const& A, + Tensor const& B, + Tensor const& C) +{ + return gemm(mma, D, A, B, C); +} + +// +// Default MMA is UniversalFMA +// + +template +CUTE_HOST_DEVICE +void +gemm(Tensor & D, + Tensor const& A, + Tensor const& B, + Tensor const& C) +{ + using MMA = MMA_Atom::value_type, + typename Tensor::value_type, + typename Tensor::value_type, + typename Tensor::value_type>>; + + return gemm(MMA{}, D, A, B, C); +} + +// +// Thread-Local Register-Memory GEMMs +// + +// Dispatch [1]: (V) x (V) => (V) +template ::value && + ALayout::rank == 1 && is_rmem::value && + BLayout::rank == 1 && is_rmem::value && + CLayout::rank == 1 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (V) Logical data + Tensor const& A, // (V) Logical data + Tensor const& B, // (V) Logical data + Tensor const& C) // (V) Logical data +{ + // No static assertions on (V), MMA checks compatibility + mma.call(D, A, B, C); +} + +// Dispatch [2]: (M) x (N) => (M,N) +template ::value && + ALayout::rank == 1 && is_rmem::value && + BLayout::rank == 1 && is_rmem::value && + CLayout::rank == 2 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (M,N) Logical data + Tensor const& A, // (M) Logical data + Tensor const& B, // (N) Logical data + Tensor const& C) // (M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<0>(A) == size<0>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<0>(B) == size<1>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D)); + gemm(mma, + D, // (M,N) + make_tensor(A.data(), append<2>(A.layout())), // (M,1) + make_tensor(B.data(), append<2>(B.layout())), // (N,1) + C); // (M,N) +} + +// Dispatch [3]: (M,K) x (N,K) => (M,N) +template ::value && + ALayout::rank == 2 && is_rmem::value && + BLayout::rank == 2 && is_rmem::value && + CLayout::rank == 2 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (M,N) Logical data + Tensor const& A, // (M,K) Logical data + Tensor const& B, // (N,K) Logical data + Tensor const& C) // (M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<0>(A) == size<0>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<0>(B) == size<1>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<1>(A) == size<1>(B)); // AK == BK + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D)); + + // Assert this is a 1-value MMA + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutC_TV{}) == Int<1>{}); + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutA_TV{}) == Int<1>{}); + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutB_TV{}) == Int<1>{}); + + gemm(mma, + make_tensor(D.data(), prepend<3>(D.layout())), // (1,M,N) + make_tensor(A.data(), prepend<3>(A.layout())), // (1,M,K) + make_tensor(B.data(), prepend<3>(B.layout())), // (1,N,K) + make_tensor(C.data(), prepend<3>(C.layout()))); // (1,M,N) +} + +// Dispatch [4]: (V,M) x (V,N) => (V,M,N) +template ::value && + ALayout::rank == 2 && is_rmem::value && + BLayout::rank == 2 && is_rmem::value && + CLayout::rank == 3 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (V,M,N) Logical data + Tensor const& A, // (V,M) Logical data + Tensor const& B, // (V,N) Logical data + Tensor const& C) // (V,M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<1>(A) == size<1>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<1>(B) == size<2>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D) && size<2>(C) == size<2>(D)); + auto M = size<1>(A); + auto N = size<1>(B); + // REGISTER .reuse OPTIMIZATIONS + // 64-bit traversal specialization -- serpentine path + if constexpr (decltype(size<0>(A))::value * sizeof(typename TA::value_type) == 8 && + decltype(size<0>(B))::value * sizeof(typename TB::value_type) == 8) + { +#if 1 // NOTE: Row- vs Col- major could depend on the C-matrix order... (which we can test) + // Row-major serpentine iteration + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + int ns = (m & 1) ? N-1-n : n; // Serpentine coordinate + gemm(mma, D(_,m,ns), A(_,m), B(_,ns), C(_,m,ns)); + } + } +#else + // Col-major serpentine iteration + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + int ms = (n & 1) ? M-1-m : m; // Serpentine coordinate + gemm(mma, D(_,ms,n), A(_,ms), B(_,n), C(_,ms,n)); + } + } +#endif + } else + // 32-bit traversal specialization -- kinked serpentine path + if constexpr (decltype(size<0>(A))::value * sizeof(typename TA::value_type) == 4 && + decltype(size<0>(B))::value * sizeof(typename TB::value_type) == 4) + { +#if 1 // NOTE: Row- vs Col- major could depend on the C-matrix order... (which we can test) + // Row-major kinked serpentine iteration + CUTE_UNROLL + for (int m = 0; m < M; m += 2) { + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + int ns = (m & 2) ? N-1-n : n; + gemm(mma, D(_,m+0,ns), A(_,m+0), B(_,ns), C(_,m+0,ns)); + + if (m+1 < M) { + gemm(mma, D(_,m+1,ns), A(_,m+1), B(_,ns), C(_,m+1,ns)); + } + } + } +#else + // Col-major kinked serpentine iteration + CUTE_UNROLL + for (int n = 0; n < N; n += 2) { + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + // Kinked serpentine traversal for maximum register reuse + int ms = (n & 2) ? M-1-m : m; + gemm(mma, D(_,ms,n+0), A(_,ms), B(_,n+0), C(_,ms,n+0)); + + if (n+1 < N) { + gemm(mma, D(_,ms,n+1), A(_,ms), B(_,n+1), C(_,ms,n+1)); + } + } + } +#endif + } else + // 64-bit + 32-bit traversal order -- keep A (64-bit) in the outer loop and serpentine B + if constexpr (decltype(size<0>(A))::value * sizeof(typename TA::value_type) == 8 && + decltype(size<0>(B))::value * sizeof(typename TB::value_type) == 4) { + // Row-major serpentine iteration + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + int ns = (m & 1) ? N-1-n : n; // Serpentine coordinate + gemm(mma, D(_,m,ns), A(_,m), B(_,ns), C(_,m,ns)); + } + } + } else + // 32-bit + 64-bit traversal order -- keep B (64-bit) in the outer loop and serpentine A + if constexpr (decltype(size<0>(A))::value * sizeof(typename TA::value_type) == 4 && + decltype(size<0>(B))::value * sizeof(typename TB::value_type) == 8) { + // Col-major serpentine iteration + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + int ms = (n & 1) ? M-1-m : m; // Serpentine coordinate + gemm(mma, D(_,ms,n), A(_,ms), B(_,n), C(_,ms,n)); + } + } + } else + // Fallback to serpentine loop + { + // Col-major serpentine iteration + CUTE_UNROLL + for (int n = 0; n < N; ++n) { + CUTE_UNROLL + for (int m = 0; m < M; ++m) { + int ms = (n & 1) ? M-1-m : m; // Serpentine coordinate + gemm(mma, D(_,ms,n), A(_,ms), B(_,n), C(_,ms,n)); + } + } + } +} + +// Dispatch [5]: (V,M,K) x (V,N,K) => (V,M,N) +template ::value && + ALayout::rank == 3 && is_rmem::value && + BLayout::rank == 3 && is_rmem::value && + CLayout::rank == 3 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (V,M,N) Logical data + Tensor const& A, // (V,M,K) Logical data + Tensor const& B, // (V,N,K) Logical data + Tensor const& C) // (V,M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<1>(A) == size<1>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<1>(B) == size<2>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<2>(A) == size<2>(B)); // AK == BK + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D) && size<2>(C) == size<2>(D)); + auto K = size<2>(A); + + CUTE_UNROLL + for (int k = 0; k < K; ++k) { + gemm(mma, D, A(_,_,k), B(_,_,k), C); + } +} + +// +// Thread-Local Shared-Memory GEMMs +// + +// Dispatch [1]: (V) x (V) => (V) +// Dispatch [2]: (M) x (N) => (M,N) +// Dispatch [3]: (M,K) x (N,K) => (M,N) +// Dispatch [4]: (V,M) x (V,N) => (V,M,N) +// Dispatch [5]: (V,M,K) x (V,N,K) => (V,M,N) +// Dispatch [3]: (M,K) x (N,K) => (M,N) +template ::value && + ALayout::rank == 2 && is_smem::value && + BLayout::rank == 2 && is_smem::value && + CLayout::rank == 2 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (M,N) Logical data + Tensor const& A, // (M,K) Logical data + Tensor const& B, // (N,K) Logical data + Tensor const& C) // (M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<0>(A) == size<0>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<0>(B) == size<1>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<1>(A) == size<1>(B)); // AK == BK + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D)); + + // Assert this is a 1-value MMA + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutC_TV{}) == Int<1>{}); + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutA_TV{}) == Int<1>{}); + CUTE_STATIC_ASSERT_V(size<1>(typename MMA_Atom::LayoutB_TV{}) == Int<1>{}); + gemm(mma, + make_tensor(D.data(), prepend<3>(D.layout())), // (1,M,N) + make_tensor(A.data(), prepend<3>(A.layout())), // (1,M,K) + make_tensor(B.data(), prepend<3>(B.layout())), // (1,N,K) + make_tensor(C.data(), prepend<3>(C.layout()))); // (1,M,N) +} + +// Dispatch [5]: (V,M,K) x (V,N,K) => (V,M,N) +template ::value && + ALayout::rank == 3 && is_smem::value && + BLayout::rank == 3 && is_smem::value && + CLayout::rank == 3 && is_rmem::value)> +CUTE_HOST_DEVICE +void +gemm(MMA_Atom const& mma, + Tensor & D, // (V,M,N) Logical data + Tensor const& A, // (V,M,K) Logical data + Tensor const& B, // (V,N,K) Logical data + Tensor const& C) // (V,M,N) Logical data +{ + CUTE_STATIC_ASSERT_V(size<1>(A) == size<1>(C)); // AM == CM + CUTE_STATIC_ASSERT_V(size<1>(B) == size<2>(C)); // BN == CN + CUTE_STATIC_ASSERT_V(size<2>(A) == size<2>(B)); // AK == BK + CUTE_STATIC_ASSERT_V(size<0>(C) == size<0>(D) && size<1>(C) == size<1>(D) && size<2>(C) == size<2>(D)); + + auto rA = MMA_Atom::make_fragment_A(A); + auto rB = MMA_Atom::make_fragment_B(B); + + auto K = size<2>(A); + + CUTE_UNROLL + for (int k = 0; k < K; ++k) + { + copy(A(_,_,k), rA(_,_,k)); + copy(B(_,_,k), rB(_,_,k)); + // Thread-level register gemm for k + gemm(mma, D, rA(_,_,k), rB(_,_,k), C); + } +} + +// +// Collective Shared-Memory GEMMs +// + +template ::value && + BLayout::rank == 2 && is_smem::value && + CLayout::rank == 2 && is_smem::value)> +CUTE_HOST_DEVICE +void +gemm(ThrMMA const& thr_mma, + Alpha const& alpha, + Tensor sA, + Tensor sB, + Beta const& beta, + Tensor sC, + ALoadTransformOp const& sA_load_op /* transforms A values before used in GEMM */, + BLoadTransformOp const& sB_load_op /* transforms B values before used in GEMM */) +{ + CUTE_STATIC_ASSERT_V(size<0>(sA) == size<0>(sC)); // AM == CM + CUTE_STATIC_ASSERT_V(size<0>(sB) == size<1>(sC)); // BN == CN + CUTE_STATIC_ASSERT_V(size<1>(sA) == size<1>(sB)); // AK == BK + + using TypeA = typename TA::value_type; + using TypeB = typename TB::value_type; + using TypeC = typename TC::value_type; + + static_assert(is_same_v>, TypeA>, + "ALoadTransformOp functor must accept and return value of type TA::value_type"); + static_assert(is_same_v>, TypeB>, + "BLoadTransformOp functor must accept and return value of type TB::value_type"); + + // Original, static size of the problem + auto M = size<0>(sC); + auto N = size<1>(sC); + auto K = size<1>(sA); + + // Block size of the compute tile + auto BLK_M = tile_size<0>(thr_mma); + auto BLK_N = tile_size<1>(thr_mma); + auto BLK_K = tile_size<2>(thr_mma); + + // Compute the "residues" + auto m_residue = M - BLK_M * (ceil_div(M, BLK_M) - Int<1>{}); // (0,BLK_M] + auto n_residue = N - BLK_N * (ceil_div(N, BLK_N) - Int<1>{}); // (0,BLK_N] + auto k_residue = K - BLK_K * (ceil_div(K, BLK_K) ); // (-BLK_K,0] + + // Shift the origin so k_residue is zeroth tile + sA.data() = &sA(0,k_residue); + sB.data() = &sB(0,k_residue); + +#if 0 + if (thread0()) { + printf("%d in BLK_M (%d)\n", int(m_residue), int(BLK_M)); + printf("%d in BLK_N (%d)\n", int(n_residue), int(BLK_N)); + printf("%d in BLK_K (%d)\n", int(k_residue), int(BLK_K)); + } +#endif + + // + // MMA Partitioning + // + + // Round the layout extents up to BLK_X + Tensor rounded_sA = sA.compose(make_shape(ceil_div(M, BLK_M) * BLK_M, ceil_div(K, BLK_K) * BLK_K)); + Tensor rounded_sB = sB.compose(make_shape(ceil_div(N, BLK_N) * BLK_N, ceil_div(K, BLK_K) * BLK_K)); + Tensor rounded_sC = sC.compose(make_shape(ceil_div(M, BLK_M) * BLK_M, ceil_div(N, BLK_N) * BLK_N)); + +#if 0 + if (thread0()) { + print(rounded_sA.layout()); print("\n"); + print(rounded_sB.layout()); print("\n"); + print(rounded_sC.layout()); print("\n"); + } +#endif + + // Partition the sA and sB tiles across the threads for the MMA + Tensor tCsA = thr_mma.partition_A(rounded_sA); // (MMA,MMA_M,MMA_K) + Tensor tCsB = thr_mma.partition_B(rounded_sB); // (MMA,MMA_N,MMA_K) + Tensor tCsC = thr_mma.partition_C(rounded_sC); // (MMA,MMA_M,MMA_N) + // Create register tensors for the MMA to operate on + Tensor tCrA = thr_mma.make_fragment_A(tCsA); // (MMA,MMA_M,MMA_K) + Tensor tCrB = thr_mma.make_fragment_B(tCsB); // (MMA,MMA_N,MMA_K) + Tensor tCrC = thr_mma.make_fragment_C(tCsC); // (MMA,MMA_M,MMA_N) + +#if 0 + if (thread0()) { + print(tCsA.layout()); print("\n"); + print(tCsB.layout()); print("\n"); + print(tCsC.layout()); print("\n"); + print(tCrA.layout()); print("\n"); + print(tCrB.layout()); print("\n"); + print(tCrC.layout()); print("\n"); + } +#endif + + // + // PREDICATION + // + + // Allocate the preds for only the MMA-mode of tCsA and tCsB + Tensor tCpA = make_tensor(size<0>(tCsA)); + Tensor tCpB = make_tensor(size<0>(tCsB)); + + // Create coordinate tensors on a single compute block for predication + Tensor cA = make_identity_tensor(make_shape(BLK_M, BLK_K)); // (BLK_M,BLK_K) -> (blk_m,blk_k) + Tensor cB = make_identity_tensor(make_shape(BLK_N, BLK_K)); // (BLK_M,BLK_K) -> (blk_n,blk_k) + + // Repeat partitioning with thr_mma + Tensor tCcA = thr_mma.partition_A(cA); // (MMA,1,1) -> (blk_m,blk_k) + Tensor tCcB = thr_mma.partition_B(cB); // (MMA,1,1) -> (blk_n,blk_k) + + // Populate the m and n predicates + CUTE_UNROLL + for (int i = 0; i < size(tCpA); ++i) { + tCpA(i) = elem_less(get<0>(tCcA(i)), m_residue); + } + CUTE_UNROLL + for (int i = 0; i < size(tCpB); ++i) { + tCpB(i) = elem_less(get<0>(tCcB(i)), n_residue); + } + +#if 0 + printf("Thr %d: A(%d,%d):%d B(%d,%d):%d\n", + threadIdx.x, + int(get<0>(tCcA(0))), int(get<1>(tCcA(0))), int(tCpA(0)), + int(get<0>(tCcB(0))), int(get<1>(tCcB(0))), int(tCpB(0))); +#endif + + // + // PREFETCH k_block = 0 (with k-predication) + // + + CUTE_UNROLL + for (int i = 0; i < size<0>(tCsA); ++i) { // Copy MMA_I + if (k_residue == 0 || get<1>(tCcA(i)) >= -k_residue) { // k_block = 0, predicated on k + CUTE_UNROLL + for (int m = 0; m < size<1>(tCsA); ++m) { // Copy MMA_M, predicated on m + tCrA(i,m,0) = (m_residue == BLK_M || m < size<1>(tCsA)-1 || tCpA(i)) ? sA_load_op(tCsA(i,m,0)) : TypeA{}; + } + } + } + + CUTE_UNROLL + for (int i = 0; i < size<0>(tCsB); ++i) { // Copy MMA_I + if (k_residue == 0 || get<1>(tCcB(i)) >= -k_residue) { // k_block = 0, predicated on k + CUTE_UNROLL + for (int n = 0; n < size<1>(tCsB); ++n) { // Copy MMA_N, predicated on n + tCrB(i,n,0) = (n_residue == BLK_N || n < size<1>(tCsB)-1 || tCpB(i)) ? sB_load_op(tCsB(i,n,0)) : TypeB{}; + } + } + } + // + // MAINLOOP + // + + // Clear accumulators + clear(tCrC); + + constexpr int K_BLOCK_MAX = size<2>(tCrA); + + CUTE_UNROLL + for (int k_block = 0; k_block < K_BLOCK_MAX; ++k_block) + { + // static-if load the next k_block. No k-predication required on these loads. + if (k_block < K_BLOCK_MAX-1) + { + // Load the next k_block + int k_next = k_block + 1; + + CUTE_UNROLL + for (int m = 0; m < size<1>(tCsA); ++m) { // Copy MMA_M + CUTE_UNROLL + for (int i = 0; i < size<0>(tCsA); ++i) { // Copy_if MMA_I predicated on m + tCrA(i,m,k_next) = (m_residue == BLK_M || m < size<1>(tCsA)-1 || tCpA(i)) ? sA_load_op(tCsA(i,m,k_next)) : TypeA{}; + } + } + + CUTE_UNROLL + for (int n = 0; n < size<1>(tCsB); ++n) { // Copy MMA_N + CUTE_UNROLL + for (int i = 0; i < size<0>(tCsB); ++i) { // Copy MMA_I predicated on n + tCrB(i,n,k_next) = (n_residue == BLK_N || n < size<1>(tCsB)-1 || tCpB(i)) ? sB_load_op(tCsB(i,n,k_next)) : TypeB{}; + } + } + } + + // GEMM on k_block in registers + gemm(thr_mma, tCrA(_,_,k_block), tCrB(_,_,k_block), tCrC); + } + + // + // Epilogue + // + + Tensor cC = make_identity_tensor(make_shape(BLK_M, BLK_N)); // (BLK_M,BLK_N) -> (blk_m,blk_n) + Tensor tCcC = thr_mma.partition_C(cC); // (MMA, 1, 1) -> (blk_m,blk_n) + + const bool isBetaZero = (beta == Beta{}); + + // Custom axpby_if for now + CUTE_UNROLL + for (int m = 0; m < size<1>(tCsC); ++m) + { + CUTE_UNROLL + for (int n = 0; n < size<2>(tCsC); ++n) + { + CUTE_UNROLL + for (int i = 0; i < size<0>(tCsC); ++i) + { + if ((m_residue == BLK_M || m < size<1>(tCrC)-1 || get<0>(tCcC(i)) < m_residue) && + (n_residue == BLK_N || n < size<2>(tCrC)-1 || get<1>(tCcC(i)) < n_residue)) + { + tCsC(i,m,n) = isBetaZero ? alpha * tCrC(i,m,n) : alpha * tCrC(i,m,n) + beta * tCsC(i,m,n); + } + } + } + } +} + +template ::value && + BLayout::rank == 2 && is_smem::value && + CLayout::rank == 2 && is_smem::value)> +CUTE_HOST_DEVICE +void +gemm(ThrMMA const& thr_mma, + Alpha const& alpha, + Tensor sA, + Tensor sB, + Beta const& beta, + Tensor sC) +{ + gemm(thr_mma, alpha, sA, sB, beta, sC, identity() /* sA_load_op */, identity() /* sB_load_op */); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/prefer.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/prefer.hpp new file mode 100644 index 000000000..804896ceb --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/prefer.hpp @@ -0,0 +1,46 @@ +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +namespace cute +{ + +// Infinite types that inherit from each other +template +struct prefer : prefer {}; + +template <> +struct prefer<0> {}; + +// Can be used to preferencially overload implementations +// Higher N in prefer have higher priority. + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tensor_algorithms_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tensor_algorithms_hip.hpp new file mode 100644 index 000000000..affc8f1d1 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tensor_algorithms_hip.hpp @@ -0,0 +1,164 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/** Common algorithms on (hierarchical) tensors */ + +#pragma once + +#include + +#include + +namespace cute +{ + +// +// for_each +// + +template +CUTE_HOST_DEVICE constexpr +void +for_each(Tensor const& tensor, UnaryOp&& op) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor); ++i) { + static_cast(op)(tensor(i)); + } +} + +template +CUTE_HOST_DEVICE constexpr +void +for_each(Tensor& tensor, UnaryOp&& op) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor); ++i) { + static_cast(op)(tensor(i)); + } +} + +// Accept mutable temporaries +template +CUTE_HOST_DEVICE constexpr +void +for_each(Tensor&& tensor, UnaryOp&& op) +{ + return for_each(tensor, static_cast(op)); +} + +// +// transform +// + +// Similar to std::transform but does not return number of elements affected +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor& tensor, UnaryOp&& op) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor); ++i) { + tensor(i) = static_cast(op)(tensor(i)); + } +} + +// Accept mutable temporaries +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor&& tensor, UnaryOp&& op) +{ + return transform(tensor, std::forward(op)); +} + +// Similar to std::transform transforms one tensors and assigns it to another +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor& tensor_in, Tensor& tensor_out, UnaryOp&& op) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor_in); ++i) { + tensor_out(i) = static_cast(op)(tensor_in(i)); + } +} + +// Accept mutable temporaries +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor&& tensor_in, Tensor&& tensor_out, UnaryOp&& op) +{ + return transform(tensor_in, tensor_out, op); +} + +// Similar to std::transform with a binary operation +// Takes two tensors as input and one tensor as output. +// Applies the binary_op to tensor_in1 and and tensor_in2 and +// assigns it to tensor_out +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor& tensor_in1, + Tensor& tensor_in2, + Tensor& tensor_out, + BinaryOp&& op) +{ + CUTE_UNROLL + for (int i = 0; i < size(tensor_in1); ++i) { + tensor_out(i) = static_cast(op)(tensor_in1(i), tensor_in2(i)); + } +} + +// Accept mutable temporaries +template +CUTE_HOST_DEVICE constexpr +void +transform(Tensor&& tensor_in1, + Tensor&& tensor_in2, + Tensor&& tensor_out, + BinaryOp&& op) +{ + return transform(tensor_in1, tensor_in2, tensor_out, op); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tuple_algorithms_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tuple_algorithms_hip.hpp new file mode 100644 index 000000000..6efbb83e5 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tuple_algorithms_hip.hpp @@ -0,0 +1,1119 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include +#include + +/// @file tuple_algorithms.hpp +/// @brief Common algorithms on (hierarchical) tuples +/// +/// Code guidelines and style preferences: +/// +/// For perfect forwarding, don't use std::forward, because it may not +/// be defined in device code when compiling with NVRTC. Instead, use +/// `static_cast(parameter_name)`. +/// +/// CuTe generally does not bother forwarding functions, as +/// reference-qualified member functions are rare in this code base. +/// +/// Throughout CUTLASS, cute::make_tuple always needs to be called +/// namespace-qualified, EVEN If inside the cute namespace and/or in +/// scope of a "using namespace cute" declaration. Otherwise, the +/// compiler may select std::make_tuple instead of cute::make_tuple, +/// due to argument-dependent lookup. Two problems may result from +/// that. +/// +/// 1. Functions have an unexpected return type (std::tuple instead of +/// cute::tuple), so functions that take cute::tuple parameters +/// fail to compile (generally inside functions that have template +/// parameters expected to be cute::tuple). +/// +/// 2. std::tuple does not have the required __host__ __device__ +/// markings, so the CUDA compiler complains if you use it in +/// device code. +/// +/// cute::make_tuple will occur more often than std::make_tuple would +/// in modern C++ code, because cute::tuple's design deprioritizes +/// correct operation of CTAD (constructor template argument +/// deduction) in favor of implementation simplicity. + +namespace cute +{ + +// +// Apply (Unpack) +// (t, f) => f(t_0,t_1,...,t_n) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +apply(T&& t, F&& f, seq) +{ + return f(get(static_cast(t))...); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +apply(T&& t, F&& f) +{ + return detail::apply(static_cast(t), f, tuple_seq{}); +} + +// +// Transform Apply +// (t, f, g) => g(f(t_0),f(t_1),...) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +tapply(T&& t, F&& f, G&& g, seq) +{ + return g(f(get(static_cast(t)))...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tapply(T0&& t0, T1&& t1, F&& f, G&& g, seq) +{ + return g(f(get(static_cast(t0)), + get(static_cast(t1)))...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tapply(T0&& t0, T1&& t1, T2&& t2, F&& f, G&& g, seq) +{ + return g(f(get(static_cast(t0)), + get(static_cast(t1)), + get(static_cast(t2)))...); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +transform_apply(T&& t, F&& f, G&& g) +{ + if constexpr (is_tuple>::value) { + return detail::tapply(static_cast(t), f, g, tuple_seq{}); + } else { + return g(f(static_cast(t))); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_apply(T0&& t0, T1&& t1, F&& f, G&& g) +{ + if constexpr (is_tuple>::value) { + return detail::tapply(static_cast(t0), static_cast(t1), f, g, tuple_seq{}); + } else { + return g(f(static_cast(t0), static_cast(t1))); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_apply(T0&& t0, T1&& t1, T2&& t2, F&& f, G&& g) +{ + if constexpr (is_tuple>::value) { + return detail::tapply(static_cast(t0), static_cast(t1), static_cast(t2), f, g, tuple_seq{}); + } else { + return g(f(static_cast(t0), static_cast(t1), static_cast(t2))); + } +} + +// +// For Each +// (t, f) => f(t_0),f(t_1),...,f(t_n) +// + +template +CUTE_HOST_DEVICE constexpr +void +for_each(T&& t, F&& f) +{ + if constexpr (is_tuple>::value) { + return detail::apply(t, [&](auto&&... a) { (f(static_cast(a)), ...); }, tuple_seq{}); + } else { + return f(static_cast(t)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +for_each_leaf(T&& t, F&& f) +{ + if constexpr (is_tuple>::value) { + return detail::apply(static_cast(t), [&](auto&&... a){ return (for_each_leaf(static_cast(a), f), ...); }, tuple_seq{}); + } else { + return f(static_cast(t)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// For Sequence +// (s, t, f) => (f(t[s_0]),f(t[s_1]),...,f(t[s_n])) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +void +for_sequence(seq const&, F&& f) { + (f(Int{}), ...); +} + +}; // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +void +for_sequence(seq const& s, T&& t, F&& f) { + detail::for_sequence(s, [&](auto&& i){ f(get::value>(static_cast(t))); }); +} + +template +CUTE_HOST_DEVICE constexpr +void +for_sequence(T&& t, F&& f) { + for_sequence(make_seq{}, static_cast(t), static_cast(f)); +} + +// +// Transform +// (t, f) => (f(t_0),f(t_1),...,f(t_n)) +// + +template +CUTE_HOST_DEVICE constexpr +auto +transform(T const& t, F&& f) +{ + if constexpr (is_tuple::value) { + return detail::tapply(t, f, [](auto const&... a){ return cute::make_tuple(a...); }, tuple_seq{}); + } else { + return f(t); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform(T0 const& t0, T1 const& t1, F&& f) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched tuple_size"); + return detail::tapply(t0, t1, f, [](auto const&... a){ return cute::make_tuple(a...); }, tuple_seq{}); + } else { + return f(t0, t1); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform(T0 const& t0, T1 const& t1, T2 const& t2, F&& f) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched tuple_size"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched tuple_size"); + return detail::tapply(t0, t1, t2, f, [](auto const&... a){ return cute::make_tuple(a...); }, tuple_seq{}); + } else { + return f(t0, t1, t2); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_leaf(T const& t, F&& f) +{ + if constexpr (is_tuple::value) { + return transform(t, [&](auto const& a) { return transform_leaf(a, f); }); + } else { + return f(t); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_leaf(T0 const& t0, T1 const& t1, F&& f) +{ + if constexpr (is_tuple::value) { + return transform(t0, t1, [&](auto const& a, auto const& b) { return transform_leaf(a, b, f); }); + } else { + return f(t0, t1); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// find and find_if +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +find_if(T const& t, F&& f, seq) +{ + if constexpr (decltype(f(get(t)))::value) { + return cute::C{}; + } else + if constexpr (sizeof...(Is) == 0) { + return cute::C{}; + } else { + return find_if(t, f, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +find_if(T const& t, F&& f) +{ + if constexpr (is_tuple::value) { + return detail::find_if(t, f, tuple_seq{}); + } else { + return cute::C{}; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +find(T const& t, X const& x) +{ + return find_if(t, [&](auto const& v) { return v == x; }); // This should always return a static true/false +} + +template +CUTE_HOST_DEVICE constexpr +auto +any_of(T const& t, F&& f) +{ + if constexpr (is_tuple::value) { + return detail::apply(cute::transform(t, f), [&] (auto const&... a) { return (false_type{} || ... || a); }, tuple_seq{}); + } else { + return f(t); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +all_of(T const& t, F&& f) +{ + if constexpr (is_tuple::value) { + return detail::apply(t, [&] (auto const&... a) { return (true_type{} && ... && f(a)); }, tuple_seq{}); + } else { + return f(t); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +none_of(T const& t, F&& f) +{ + return not any_of(t, f); +} + +// +// Filter +// (t, f) => +// + +template +CUTE_HOST_DEVICE constexpr +auto +filter_tuple(T const& t, F&& f) +{ + return transform_apply(t, f, [](auto const&... a) { return cute::tuple_cat(a...); }); +} + +template +CUTE_HOST_DEVICE constexpr +auto +filter_tuple(T0 const& t0, T1 const& t1, F&& f) +{ + return transform_apply(t0, t1, f, [](auto const&... a) { return cute::tuple_cat(a...); }); +} + +template +CUTE_HOST_DEVICE constexpr +auto +filter_tuple(T0 const& t0, T1 const& t1, T2 const& t2, F&& f) +{ + return transform_apply(t0, t1, t2, f, [](auto const&... a) { return cute::tuple_cat(a...); }); +} + +// +// Fold (Reduce, Accumulate) +// (t, v, f) => f(...f(f(v,t_0),t_1),...,t_n) +// + +namespace detail { + +// This impl compiles much faster than cute::apply and variadic args +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +fold(T&& t, V&& v, F&& f, seq<>) +{ + return static_cast(v); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +fold(T&& t, V&& v, F&& f, seq) +{ + if constexpr (sizeof...(Is) == 0) { + return f(static_cast(v), get(static_cast(t))); + } else { + return fold(static_cast(t), + f(static_cast(v), get(static_cast(t))), + f, + seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +fold(T&& t, V&& v, F&& f) +{ + if constexpr (is_tuple>::value) { + return detail::fold(static_cast(t), + static_cast(v), + f, + tuple_seq{}); + } else { + return f(static_cast(v), static_cast(t)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +fold_first(T&& t, F&& f) +{ + if constexpr (is_tuple>::value) { + return detail::fold(static_cast(t), + get<0>(static_cast(t)), + f, + make_range<1,tuple_size>::value>{}); + } else { + return static_cast(t); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// front, back, take, select, unwrap +// + +// Get the first non-tuple element in a hierarchical tuple +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +front(T&& t) +{ + if constexpr (is_tuple>::value) { + return front(get<0>(static_cast(t))); + } else { + return static_cast(t); + } + + CUTE_GCC_UNREACHABLE; +} + +// Get the last non-tuple element in a hierarchical tuple +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +back(T&& t) +{ + if constexpr (is_tuple>::value) { + constexpr int N = tuple_size>::value; + + // MSVC needs a bit of extra help here deducing return types. + // We help it by peeling off the nonrecursive case a level "early." + if constexpr (! is_tuple(static_cast(t)))>>::value) { + return get(static_cast(t)); + } + else { + return back(get(static_cast(t))); + } + + } else { + return static_cast(t); + } + + CUTE_GCC_UNREACHABLE; +} + +// Takes the elements in the range [B,E) +template +CUTE_HOST_DEVICE constexpr +auto +take(T const& t) +{ + return detail::apply(t, [](auto const&... a) { return cute::make_tuple(a...); }, make_range{}); +} + +// +// Select tuple elements with given indices. +// + +template +CUTE_HOST_DEVICE constexpr +auto +select(T const & t) +{ + return cute::make_tuple(get(t)...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +select(T const & t, Indices const & indices) +{ + if constexpr (is_tuple::value) { + return cute::transform(indices, [&t](auto i) { return select(t, i); }); + } + else { + static_assert(is_static::value, "Order must be static"); + return get(t); + } +} + +// Wrap non-tuples into rank-1 tuples or forward +template +CUTE_HOST_DEVICE constexpr +auto +wrap(T const& t) +{ + if constexpr (is_tuple::value) { + return t; + } else { + return cute::make_tuple(t); + } + + CUTE_GCC_UNREACHABLE; +} + +// Unwrap rank-1 tuples until we're left with a rank>1 tuple or a non-tuple +template +CUTE_HOST_DEVICE constexpr +auto +unwrap(T const& t) +{ + if constexpr (is_tuple::value) { + if constexpr (tuple_size::value == 1) { + return unwrap(get<0>(t)); + } else { + return t; + } + } else { + return t; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Flatten a hierarchical tuple to a tuple of depth one. +// +// + +template +struct is_flat : true_type {}; + +template +struct is_flat> : bool_constant<(true && ... && (not is_tuple::value))> {}; + +template +CUTE_HOST_DEVICE constexpr +auto +flatten_to_tuple(T const& t) +{ + if constexpr (is_tuple::value) { + if constexpr (is_flat::value) { + return t; + } else + { + return filter_tuple(t, [](auto const& a) { return flatten_to_tuple(a); }); + } + } else { + return cute::make_tuple(t); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +flatten(T const& t) +{ + if constexpr (is_tuple::value) { + if constexpr (is_flat::value) { + return t; + } else + { + return filter_tuple(t, [](auto const& a) { return flatten_to_tuple(a); }); + } + } else { + return t; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// insert and remove and replace +// + +namespace detail { + +// Shortcut around cute::tuple_cat for common insert/remove/repeat cases +template +CUTE_HOST_DEVICE constexpr +auto +construct(T const& t, X const& x, seq, seq, seq) +{ + return cute::make_tuple(get(t)..., (void(J),x)..., get(t)...); +} + +} // end namespace detail + +// Insert x into the Nth position of the tuple +template +CUTE_HOST_DEVICE constexpr +auto +insert(T const& t, X const& x) +{ + return detail::construct(t, x, make_seq{}, seq<0>{}, make_range::value>{}); +} + +// Remove the Nth element of the tuple +template +CUTE_HOST_DEVICE constexpr +auto +remove(T const& t) +{ + return detail::construct(t, 0, make_seq{}, seq<>{}, make_range::value>{}); +} + +// Replace the Nth element of the tuple with x +template +CUTE_HOST_DEVICE constexpr +auto +replace(T const& t, X const& x) +{ + return detail::construct(t, x, make_seq{}, seq<0>{}, make_range::value>{}); +} + +// Replace the first element of the tuple with x +template +CUTE_HOST_DEVICE constexpr +auto +replace_front(T const& t, X const& x) +{ + if constexpr (is_tuple::value) { + return detail::construct(t, x, seq<>{}, seq<0>{}, make_range<1,tuple_size::value>{}); + } else { + return x; + } + + CUTE_GCC_UNREACHABLE; +} + +// Replace the last element of the tuple with x +template +CUTE_HOST_DEVICE constexpr +auto +replace_back(T const& t, X const& x) +{ + if constexpr (is_tuple::value) { + return detail::construct(t, x, make_seq::value-1>{}, seq<0>{}, seq<>{}); + } else { + return x; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Make a tuple of Xs of tuple_size N +// + +template +CUTE_HOST_DEVICE constexpr +auto +repeat(X const& x) +{ + if constexpr (N == 1) { + return x; + } else { + return detail::construct(0, x, seq<>{}, make_seq{}, seq<>{}); + } +} + +// +// Make a tuple of Xs the same profile as tuple +// + +template +CUTE_HOST_DEVICE constexpr +auto +repeat_like(T const& t, X const& x) +{ + if constexpr (is_tuple::value) { + return transform(t, [&](auto const& a) { return repeat_like(a,x); }); + } else { + return x; + } + + CUTE_GCC_UNREACHABLE; +} + +// Group the elements [B,E) of a T into a single element +// e.g. group<2,4>(T<_1,_2,_3,_4,_5,_6>{}) +// => T<_1,_2,T<_3,_4>,_5,_6>{} +template +CUTE_HOST_DEVICE constexpr +auto +group(T const& t) +{ + if constexpr (not is_tuple::value) { + if constexpr (E == -1) { + return group(t); + } else { + return detail::construct(t, take(t), make_seq{}, make_seq<(B < E)>{}, make_range{}); + } + } else + if constexpr (E == -1) { + return group::value>(t); + } else + if constexpr (B <= E) { + return detail::construct(t, take(t), make_seq{}, make_seq<(B < E)>{}, make_range::value>{}); + } else { + static_assert(B <= E); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Extend a T to rank N by appending/prepending an element +// + +template +CUTE_HOST_DEVICE constexpr +auto +append(T const& a, X const& x) +{ + if constexpr (is_tuple::value) { + if constexpr (N == tuple_size::value) { + return a; + } else { + static_assert(N > tuple_size::value); + return detail::construct(a, x, make_seq::value>{}, make_seq::value>{}, seq<>{}); + } + } else { + if constexpr (N == 1) { + return a; + } else { + return detail::construct(cute::make_tuple(a), x, seq<0>{}, make_seq{}, seq<>{}); + } + } + + CUTE_GCC_UNREACHABLE; +} +template +CUTE_HOST_DEVICE constexpr +auto +append(T const& a, X const& x) +{ + if constexpr (is_tuple::value) { + return detail::construct(a, x, make_seq::value>{}, seq<0>{}, seq<>{}); + } else { + return cute::make_tuple(a, x); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +prepend(T const& a, X const& x) +{ + if constexpr (is_tuple::value) { + if constexpr (N == tuple_size::value) { + return a; + } else { + static_assert(N > tuple_size::value); + return detail::construct(a, x, seq<>{}, make_seq::value>{}, make_seq::value>{}); + } + } else { + if constexpr (N == 1) { + return a; + } else { + static_assert(N > 1); + return detail::construct(cute::make_tuple(a), x, seq<>{}, make_seq{}, seq<0>{}); + } + } + + CUTE_GCC_UNREACHABLE; +} +template +CUTE_HOST_DEVICE constexpr +auto +prepend(T const& a, X const& x) +{ + if constexpr (is_tuple::value) { + return detail::construct(a, x, seq<>{}, seq<0>{}, make_seq::value>{}); + } else { + return cute::make_tuple(x, a); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Unflatten a flat tuple into a hierarchical one +// unflatten(x, flatten(x)) == x +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +unflatten_impl(FlatTuple const& flat_tuple, TargetProfile const& target_profile) +{ + if constexpr (is_tuple::value) { + return fold(target_profile, cute::make_tuple(cute::make_tuple(), flat_tuple), [](auto const& v, auto const& t) { + auto [result, remaining_tuple] = v; + auto [sub_result, sub_tuple] = unflatten_impl(remaining_tuple, t); + return cute::make_tuple(append(result, sub_result), sub_tuple); + }); + } else { + return cute::make_tuple(get<0>(flat_tuple), take<1, decltype(rank(flat_tuple))::value>(flat_tuple)); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +// @pre flatten(@a flat_tuple) == @a flat_tuple +// @pre rank(flatten(@a target_profile)) == rank(@a flat_tuple) +// @post congruent(@a result, @a target_profile) +// @post flatten(@a result) == @a flat_tuple +template +CUTE_HOST_DEVICE constexpr +auto +unflatten(FlatTuple const& flat_tuple, TargetProfile const& target_profile) +{ + auto [unflatten_tuple, flat_remainder] = detail::unflatten_impl(flat_tuple, target_profile); + CUTE_STATIC_ASSERT_V(rank(flat_remainder) == Int<0>{}); + return unflatten_tuple; +} + + +// +// Inclusive scan (prefix sum) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +iscan(T const& t, V const& v, F&& f, seq) +{ + // Apply the function to v and the element at I + auto v_next = f(v, get(t)); + // Replace I with v_next + auto t_next = replace(t, v_next); + +#if 0 + std::cout << "ISCAN i" << I << std::endl; + std::cout << " t " << t << std::endl; + std::cout << " i " << v << std::endl; + std::cout << " f(i,t) " << v_next << std::endl; + std::cout << " t_n " << t_next << std::endl; +#endif + + if constexpr (sizeof...(Is) == 0) { + return t_next; + } else { + return iscan(t_next, v_next, f, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +iscan(T const& t, V const& v, F&& f) +{ + return detail::iscan(t, v, f, tuple_seq{}); +} + +// +// Exclusive scan (prefix sum) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +escan(T const& t, V const& v, F&& f, seq) +{ + if constexpr (sizeof...(Is) == 0) { + // Replace I with v + return replace(t, v); + } else { + // Apply the function to v and the element at I + auto v_next = f(v, get(t)); + // Replace I with v + auto t_next = replace(t, v); + +#if 0 + std::cout << "ESCAN i" << I << std::endl; + std::cout << " t " << t << std::endl; + std::cout << " i " << v << std::endl; + std::cout << " f(i,t) " << v_next << std::endl; + std::cout << " t_n " << t_next << std::endl; +#endif + + // Recurse + return escan(t_next, v_next, f, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +escan(T const& t, V const& v, F&& f) +{ + return detail::escan(t, v, f, tuple_seq{}); +} + +// +// Zip (Transpose) +// + +// Take ((a,b,c,...),(x,y,z,...),...) rank-R0 x rank-R1 input +// to produce ((a,x,...),(b,y,...),(c,z,...),...) rank-R1 x rank-R0 output + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +zip_(Ts const&... ts) +{ + return cute::make_tuple(get(ts)...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +zip(T const& t, seq, seq) +{ + static_assert(conjunction>::value == tuple_size>::value>...>::value, "Mismatched Ranks"); + return cute::make_tuple(zip_(get(t)...)...); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +zip(T const& t) +{ + if constexpr (is_tuple::value) { + if constexpr (is_tuple>::value) { + return detail::zip(t, tuple_seq{}, tuple_seq>{}); + } else { + return cute::make_tuple(t); + } + } else { + return t; + } + + CUTE_GCC_UNREACHABLE; +} + +// Convenient to pass them in separately +template +CUTE_HOST_DEVICE constexpr +auto +zip(T0 const& t0, T1 const& t1, Ts const&... ts) +{ + return zip(cute::make_tuple(t0, t1, ts...)); +} + +// +// zip2_by -- A guided zip for rank-2 tuples +// Take a tuple like ((A,a),((B,b),(C,c)),d) +// and produce a tuple ((A,(B,C)),(a,(b,c),d)) +// where the rank-2 modes are selected by the terminals of the guide (X,(X,X)) +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +zip2_by(T const& t, TG const& guide, seq, seq) +{ + // zip2_by produces the modes like ((A,a),(B,b),...) + auto split = cute::make_tuple(zip2_by(get(t), get(guide))...); + + // Rearrange and append missing modes from t to make ((A,B,...),(a,b,...,x,y)) + return cute::make_tuple(cute::make_tuple(get<0>(get(split))...), + cute::make_tuple(get<1>(get(split))..., get(t)...)); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +zip2_by(T const& t, TG const& guide) +{ + if constexpr (is_tuple::value) { + constexpr int TR = tuple_size::value; + constexpr int GR = tuple_size::value; + static_assert(TR >= GR, "Mismatched ranks"); + return detail::zip2_by(t, guide, + make_range< 0, GR>{}, + make_range{}); + } else { + static_assert(tuple_size::value == 2, "Mismatched ranks"); + return t; + } + + CUTE_GCC_UNREACHABLE; +} + +/// @return A tuple of the elements of @c t in reverse order. +template +CUTE_HOST_DEVICE constexpr auto +reverse(T const& t) { + if constexpr (is_tuple::value) { + return detail::apply(t, [] (auto const&... a) { + return cute::make_tuple(a...); + }, tuple_rseq{}); + } + else { + return t; + } +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/cluster_sm90_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/cluster_sm90_hip.hpp new file mode 100644 index 000000000..cab12d359 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/cluster_sm90_hip.hpp @@ -0,0 +1,261 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) && \ + ((__CUDACC_VER_MAJOR__ >= 12) || ((__CUDACC_VER_MAJOR__ == 11) && (__CUDACC_VER_MINOR__ >= 8)))) +# define CUTE_ARCH_CLUSTER_SM90_ENABLED +#endif + +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) && (__CUDACC_VER_MAJOR__ >= 12)) +# define CUTE_ARCH_ELECT_ONE_SM90_ENABLED +#endif + +namespace cute { + +CUTE_DEVICE void cluster_arrive_relaxed() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + asm volatile("barrier.cluster.arrive.relaxed.aligned;\n" : : ); +#else + CUTE_RUNTIME_ASSERT("CUTE_ARCH_CLUSTER_SM90_ENABLED is not defined"); +#endif +} + +CUTE_DEVICE void cluster_arrive() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + asm volatile("barrier.cluster.arrive.aligned;\n" : : ); +#else + CUTE_RUNTIME_ASSERT("CUTE_ARCH_CLUSTER_SM90_ENABLED is not defined"); +#endif +} + +CUTE_DEVICE void cluster_wait() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + asm volatile("barrier.cluster.wait.aligned;\n" : : ); +#else + CUTE_RUNTIME_ASSERT("CUTE_ARCH_CLUSTER_SM90_ENABLED is not defined"); +#endif +} + +CUTE_DEVICE void cluster_sync() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + cluster_arrive(); + cluster_wait(); +#else + CUTE_RUNTIME_ASSERT("CUTE_ARCH_CLUSTER_SM90_ENABLED is not defined"); +#endif +} + +// Returns the dim3 grid size in terms of number of clusters. +CUTE_DEVICE dim3 cluster_grid_dims() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t x, y, z; + asm volatile("mov.u32 %0, %nclusterid.x;\n" : "=r"(x) : ); + asm volatile("mov.u32 %0, %nclusterid.y;\n" : "=r"(y) : ); + asm volatile("mov.u32 %0, %nclusterid.z;\n" : "=r"(z) : ); + return {x, y, z}; +#elif defined(DCU_ASM) + uint32_t x,y,z; + x = gridDim.x; + y = gridDim.y; + z = gridDim.z; + return {x,y,z}; +#elif defined(__HIP_DEVICE_COMPILE__) + // MSVC requires protecting use of gridDim with __HIP_DEVICE_COMPILE__. + return gridDim; +#elif defined(_MSC_VER) + CUTE_RUNTIME_ASSERT("cluster_grid_dims() can only be called on device"); +#else + return {0, 0, 0}; +#endif +} + +// Returns the dim3 cluster rank in the grid. +CUTE_DEVICE dim3 cluster_id_in_grid() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t x, y, z; + asm volatile("mov.u32 %0, %clusterid.x;\n" : "=r"(x) : ); + asm volatile("mov.u32 %0, %clusterid.y;\n" : "=r"(y) : ); + asm volatile("mov.u32 %0, %clusterid.z;\n" : "=r"(z) : ); + return {x, y, z}; +#elif defined(DCU_ASM) + uint32_t x,y,z; + x = blockIdx.x; + y = blockIdx.y; + z = blockIdx.z; + return {x,y,z}; +#elif defined(__HIP_DEVICE_COMPILE__) + // MSVC requires protecting use of blockIdx with __HIP_DEVICE_COMPILE__. + return blockIdx; +#elif defined(_MSC_VER) + CUTE_RUNTIME_ASSERT("cluster_id_in_grid() can only be called on device"); +#else + return {0, 0, 0}; +#endif +} + +// Returns the relative dim3 block rank local to the cluster. +CUTE_DEVICE dim3 block_id_in_cluster() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t x, y, z; + asm volatile("mov.u32 %0, %cluster_ctaid.x;\n" : "=r"(x) : ); + asm volatile("mov.u32 %0, %cluster_ctaid.y;\n" : "=r"(y) : ); + asm volatile("mov.u32 %0, %cluster_ctaid.z;\n" : "=r"(z) : ); + return {x, y, z}; +#else + return {0,0,0}; +#endif +} + +// Returns the dim3 cluster shape. +CUTE_DEVICE dim3 cluster_shape() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t x, y, z; + asm volatile("mov.u32 %0, %cluster_nctaid.x;\n" : "=r"(x) : ); + asm volatile("mov.u32 %0, %cluster_nctaid.y;\n" : "=r"(y) : ); + asm volatile("mov.u32 %0, %cluster_nctaid.z;\n" : "=r"(z) : ); + return {x, y, z}; +#else + return {1,1,1}; +#endif +} + +// Get 1D ctaid in a cluster. +CUTLASS_DEVICE uint32_t block_rank_in_cluster() +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t rank; + asm volatile("mov.u32 %0, %cluster_ctarank;\n" : "=r"(rank) :); + return rank; +#else + return 0; +#endif +} + +// Set the destination block-ID in cluster for a given SMEM Address +CUTLASS_DEVICE uint32_t set_block_rank(uint32_t smemAddr, uint32_t rank) +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t result; + asm volatile("mapa.shared::cluster.u32 %0, %1, %2;\n" + : "=r"(result) + : "r"(smemAddr), "r"(rank)); + return result; +#else + return smemAddr; +#endif +} + +// Elect one thread in the warp. The elected thread gets its predicate set to true, all others obtain false. +CUTE_HOST_DEVICE uint32_t elect_one_sync() +{ +#if defined(CUTE_ARCH_ELECT_ONE_SM90_ENABLED) + uint32_t pred = 0; + uint32_t laneid = 0; + asm volatile( + "{\n" + ".reg .b32 %rx;\n" + ".reg .pred %px;\n" + " elect.sync %rx|%px, %2;\n" + "@%px mov.s32 %1, 1;\n" + " mov.s32 %0, %rx;\n" + "}\n" + : "+r"(laneid), "+r"(pred) + : "r"(0xFFFFFFFF)); + return pred; +#elif defined(__HIP_DEVICE_COMPILE__) + return (threadIdx.x % 32) == 0; +#else + return true; +#endif +} + +struct ElectOneLaneIdReturnType { + uint32_t is_leader; + uint32_t leader_lane_id; +}; + +CUTE_HOST_DEVICE +ElectOneLaneIdReturnType +elect_one_leader_sync() +{ +#if defined(CUTE_ARCH_ELECT_ONE_SM90_ENABLED) + uint32_t pred = 0; + uint32_t laneid = 0; + asm volatile( + "{\n" + ".reg .b32 %rx;\n" + ".reg .pred %px;\n" + " elect.sync %rx|%px, %2;\n" + "@%px mov.s32 %1, 1;\n" + " mov.s32 %0, %rx;\n" + "}\n" + : "+r"(laneid), "+r"(pred) + : "r"(0xFFFFFFFF)); + return {pred, laneid}; +#elif defined(__HIP_DEVICE_COMPILE__) + return {(threadIdx.x % 32) == 0, 0}; +#else + return {true, 0}; +#endif +} + +// Store value to remote shared memory in the cluster +CUTE_DEVICE +void +store_shared_remote(uint32_t value, uint32_t smem_addr, uint32_t mbarrier_addr, uint32_t dst_cta_rank) +{ +#if defined(CUTE_ARCH_CLUSTER_SM90_ENABLED) + uint32_t dsmem_addr = set_block_rank(smem_addr, dst_cta_rank); + uint32_t remote_barrier_addr = set_block_rank(mbarrier_addr, dst_cta_rank); + asm volatile("st.async.shared::cluster.mbarrier::complete_tx::bytes.u32 [%0], %1, [%2];" + : : "r"(dsmem_addr), "r"(value), "r"(remote_barrier_addr)); +#endif +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_gfx928_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_gfx928_hip.hpp new file mode 100644 index 000000000..2c7b2abfc --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_gfx928_hip.hpp @@ -0,0 +1,292 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config + +namespace cute +{ +/*************************************************/ +/*not completed,to do ...*/ +/*************************************************/ +template +struct GFX928_CP_GLOBAL_DIRECT_TO_LDS +{ + using SRegisters = TS[1]; + using DRegisters = TD[1]; + + static_assert(sizeof(TS) == sizeof(TD), "cp.async requires sizeof(src_value_type) == sizeof(dst_value_type)"); + static_assert(sizeof(TS) == 4 || sizeof(TS) == 8 || sizeof(TS) == 16, "cp.async sizeof(TS) is not supported"); + + CUTE_HOST_DEVICE static void + copy(TS const& gmem_src, + TD & smem_dst) + { + // buffer_load_dwordx4 v[0:3], v[vgprGlobalOffsetA], s[sgprSrcA:sgprSrcA+3], 0, offen, offset:0x00, lds + } +}; + + +struct GFX928_DS_READ_DS_M32x8_B32 +{ + using SRegisters = uint128_t[1]; + using DRegisters = float[4]; + + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + float& dst0, + float& dst1, + float& dst2, + float& dst3) + { + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + v4f d; + + d = __builtin_amdgcn_ds_read_m32x8f32((float*)&smem_src, short(0)); + float * dst = reinterpret_cast(&d); + dst0 = dst[0]; + dst1 = dst[1]; + dst2 = dst[2]; + dst3 = dst[3]; + #endif + } +}; + + +struct GFX928_DS_READ_DS_M32x16_B16 +{ + //源共享内存数据是两个64位共享内存 + using SRegisters = uint64_t[2]; + using DRegisters = uint16_t[8]; + + CUTE_HOST_DEVICE static void + copy(uint64_t & smem_src1, + uint64_t const& smem_src2, + uint16_t& dst0, uint16_t& dst1, uint16_t& dst2, uint16_t& dst3, + uint16_t& dst4, uint16_t& dst5, uint16_t& dst6, uint16_t& dst7) + { + + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + #if 0 + uint32x4_t d; + int lds_offset = reinterpret_cast(&smem_src1) - (char *)(0x1000000000000); + if (thread0() || thread(64, 0)) { + printf("lds_offset:%d\n", lds_offset); + } + // __builtin_amdgcn_ds_read_m32x16f16_alt(lds,offset) + asm volatile("DS_READ_M32x16_B16 %0, %1, offset:0x00 \n\t" + "s_waitcnt lgkmcnt(0) \n\t" + : "+v"(d) + : "v"(lds_offset)); + + uint16_t * dst = reinterpret_cast(&d); + #else + __fp16 * lds = reinterpret_cast<__fp16 *>(&smem_src1); + const short offset = 0; + #ifdef HG_ROCM + auto d = __builtin_hygon_ds_read_m32x16_f16(lds); + #else + auto d = __builtin_amdgcn_ds_read_m32x16f16(lds, offset); + #endif + uint16_t * dst = reinterpret_cast(&d); + #endif + dst0 = dst[0]; + dst1 = dst[1]; + dst2 = dst[2]; + dst3 = dst[3]; + dst4 = dst[4]; + dst5 = dst[5]; + dst6 = dst[6]; + dst7 = dst[7]; + + #endif + + } + +}; + +struct GFX928_DS_READ_DS_M32x16_B16_ALT +{ + using SRegisters = uint128_t[1]; + using DRegisters = half_t[8]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + half_t& dst0, half_t& dst1, half_t& dst2, half_t& dst3, + half_t& dst4, half_t& dst5, half_t& dst6, half_t& dst7) + { + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + uint32x4_t d; + int lds_offset = reinterpret_cast(&smem_src) - (char *)(0x1000000000000); + asm volatile("DS_READ_M32x16_B16_alt %0, %1, offset:0x00 \n\t" + "s_waitcnt lgkmcnt(0) \n\t" + : "+v"(d) + : "v"(lds_offset)); + half_t * dst = reinterpret_cast(&d); + dst0 = dst[0]; + dst1 = dst[1]; + dst2 = dst[2]; + dst3 = dst[3]; + dst4 = dst[4]; + dst5 = dst[5]; + dst6 = dst[6]; + dst7 = dst[7]; + #endif + } +}; + +#if 1 +/*************************************************/ +struct GFX928_DS_READ_DS_M32x32_B8 +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint128_t& dst) + { + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + v4i d; + d = __builtin_amdgcn_ds_read_m32x32u8( (int*)(&smem_src), short(0)); + uint128_t* d_ptr = reinterpret_cast(&d); + + dst= *d_ptr; + + #endif + } +}; +#else +struct GFX928_DS_READ_DS_M32x32_B8 +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint8_t[16]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint8_t& dst00, uint8_t& dst01, uint8_t& dst02, uint8_t& dst03, + uint8_t& dst04, uint8_t& dst05, uint8_t& dst06, uint8_t& dst07, + uint8_t& dst08, uint8_t& dst09, uint8_t& dst10, uint8_t& dst11, + uint8_t& dst12, uint8_t& dst13, uint8_t& dst14, uint8_t& dst15) + { + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + v4i d; + int lds_offset = reinterpret_cast(&smem_src) - (char *)(0x1000000000000); + asm volatile("DS_READ_M32x32_B8 %0, %1, offset:0x00 \n\t" + "s_waitcnt lgkmcnt(0) \n\t" + : "+v"(d) + : "v"(lds_offset)); + uint8_t * dst = reinterpret_cast(&d); + dst00 = dst[0]; + dst01 = dst[1]; + dst02 = dst[2]; + dst03 = dst[3]; + dst04 = dst[4]; + dst05 = dst[5]; + dst06 = dst[6]; + dst07 = dst[7]; + dst08 = dst[8]; + dst09 = dst[9]; + dst10 = dst[10]; + dst11 = dst[11]; + dst12 = dst[12]; + dst13 = dst[13]; + dst14 = dst[14]; + dst15 = dst[15]; + #endif + } +}; +#endif +///////////////////////////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// used for flash attention // +///////////////////////////////////////////////////////////////////////////////////////////////// +struct GFX928_DS_READ_B128 +{ + //源共享内存数据是两个64位共享内存 + using SRegisters = uint64_t[2]; + using DRegisters = uint16_t[8]; + + CUTE_HOST_DEVICE static void + copy(uint64_t & smem_src1, + uint64_t const& smem_src2, + uint16_t& dst0, uint16_t& dst1, uint16_t& dst2, uint16_t& dst3, + uint16_t& dst4, uint16_t& dst5, uint16_t& dst6, uint16_t& dst7) + { + + #if (defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) + #if 0 + float * lds = reinterpret_cast(&smem_src1); + const short offset = 0; + auto d = __builtin_amdgcn_ds_read_v4f(lds, offset); + #else + uint32x4_t d; + int lds_offset = reinterpret_cast(&smem_src1) - (char *)(0x1000000000000); + asm volatile("ds_read_b128 %0, %1\n\t" + : "+v"(d) + : "v"(lds_offset)); + #endif + uint16_t * dst = reinterpret_cast(&d); + dst0 = dst[0]; + dst1 = dst[1]; + dst2 = dst[2]; + dst3 = dst[3]; + dst4 = dst[4]; + dst5 = dst[5]; + dst6 = dst[6]; + dst7 = dst[7]; + + #endif + + } + +}; + +struct GFX928_DS_READ_DS_M32x16_B16_WITH_STRIDE : GFX928_DS_READ_DS_M32x16_B16{}; +struct GFX928_DS_READ_DS_M32x16_B16_WITH_8x64 : GFX928_DS_READ_DS_M32x16_B16{}; +struct GFX928_DS_READ_DS_M32x16_B16_RAW : GFX928_DS_READ_DS_M32x16_B16{}; +struct GFX928_DS_READ_DS_M16x32_B16 : GFX928_DS_READ_DS_M32x16_B16{}; +struct GFX928_DS_READ_DS_M16x16_B32 : GFX928_DS_READ_DS_M32x8_B32{}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_hip.hpp new file mode 100644 index 000000000..7131f21fe --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_hip.hpp @@ -0,0 +1,77 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +// +// Direct Copy for any type +// + +template +struct UniversalCopy +{ + using SRegisters = S[1]; + using DRegisters = D[1]; + + CUTE_HOST_DEVICE static constexpr void + copy(S const& src, + D & dst) + { + dst = src; + } +}; + +// +// Placeholder for the copy algorithm's default, auto-vectorizing behavior +// + +struct DefaultCopy +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint128_t[1]; +}; + +using AutoVectorizingCopy = DefaultCopy; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm75_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm75_hip.hpp new file mode 100644 index 000000000..f7d6d1883 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm75_hip.hpp @@ -0,0 +1,242 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config +#if defined(__clang__) && defined(__CUDA__) + // ldmatrix PTX instructions added in Clang 14: https://reviews.llvm.org/D107046 + // ... but will not work until Clang 15: + // * https://reviews.llvm.org/D121666 + // * https://reviews.llvm.org/D126846 + #define CUTE_ARCH_CLANG_SUPPORTS_LDSM_SM75 (__clang_major__ >= 15) +#endif + +#if defined(__NVCC__) || defined(__CUDACC_RTC__) + // ldmatrix PTX instruction added in CUDA 10.2+ + #define CUTE_ARCH_NVCC_SUPPORTS_LDSM_SM75 ((__CUDACC_VER_MAJOR__ == 10 && __CUDACC_VER_MINOR__ >= 2) || __CUDACC_VER_MAJOR__ >= 11) +#endif + +#if ! defined(CUTE_ARCH_LDSM_SM75_SUPPORTED) + #define CUTE_ARCH_LDSM_SM75_SUPPORTED (CUTE_ARCH_NVCC_SUPPORTS_LDSM_SM75 || CUTE_ARCH_CLANG_SUPPORTS_LDSM_SM75) +#endif + +#if ! defined(CUTE_ARCH_LDSM_SM75_ENABLED) + #define CUTE_ARCH_LDSM_SM75_ENABLED (CUTE_ARCH_LDSM_SM75_SUPPORTED) +#endif + +#if (CUTE_ARCH_LDSM_SM75_ENABLED) && defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ >= 750 + #define CUTE_ARCH_LDSM_SM75_ACTIVATED 1 +#endif + +namespace cute +{ + +struct SM75_U32x1_LDSM_N +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x1.m8n8.shared.b16 {%0}, [%1];\n" + : "=r"(dst) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +struct SM75_U32x2_LDSM_N +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst0, uint32_t& dst1) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];\n" + : "=r"(dst0), "=r"(dst1) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +struct SM75_U32x4_LDSM_N +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst0, uint32_t& dst1, uint32_t& dst2, uint32_t& dst3) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n" + : "=r"(dst0), "=r"(dst1), "=r"(dst2), "=r"(dst3) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +struct SM75_U16x2_LDSM_T +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x1.trans.m8n8.shared.b16 {%0}, [%1];\n" + : "=r"(dst) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +struct SM75_U16x4_LDSM_T +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst0, uint32_t& dst1) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x2.trans.m8n8.shared.b16 {%0, %1}, [%2];\n" + : "=r"(dst0), "=r"(dst1) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +struct SM75_U16x8_LDSM_T +{ + using SRegisters = uint128_t[1]; + using DRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + copy(uint128_t const& smem_src, + uint32_t& dst0, uint32_t& dst1, uint32_t& dst2, uint32_t& dst3) + { +#if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_src); + asm volatile ("ldmatrix.sync.aligned.x4.trans.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n" + : "=r"(dst0), "=r"(dst1), "=r"(dst2), "=r"(dst3) + : "r"(smem_int_ptr)); +#else + CUTE_RUNTIME_ASSERT("Trying to use ldmatrix without CUTE_ARCH_LDSM_SM75_ACTIVATED."); +#endif + } +}; + +// +// Legacy LDSM interfaces that aren't very useful +// + +template +CUTE_HOST_DEVICE +void +copy_ldsm(uint128_t const* const smem_ptr, + T* rmem_ptr) +{ + uint32_t* reg_ptr = reinterpret_cast(rmem_ptr); + + // if constexpr + if (sizeof(T) == 4) { + SM75_U32x1_LDSM_N::copy(smem_ptr[0], reg_ptr[0]); + } + else if (sizeof(T) == 8) { + SM75_U32x2_LDSM_N::copy(smem_ptr[0], reg_ptr[0], reg_ptr[1]); + } + else if (sizeof(T) == 16) { + SM75_U32x4_LDSM_N::copy(smem_ptr[0], reg_ptr[0], reg_ptr[1], reg_ptr[2], reg_ptr[3]); + } + else { + static_assert(sizeof(T) == 4 || sizeof(T) == 8 || sizeof(T) == 16, "sizeof(T) is not supported"); + } +} + +template +CUTE_HOST_DEVICE +void +copy_ldsm_trans(uint128_t const* const smem_ptr, + T* rmem_ptr) +{ + uint32_t* reg_ptr = reinterpret_cast(rmem_ptr); + + // if constexpr + if (sizeof(T) == 4) { + SM75_U16x2_LDSM_T::copy(smem_ptr[0], reg_ptr[0]); + } + else if (sizeof(T) == 8) { + SM75_U16x4_LDSM_T::copy(smem_ptr[0], reg_ptr[0], reg_ptr[1]); + } + else if (sizeof(T) == 16) { + SM75_U16x8_LDSM_T::copy(smem_ptr[0], reg_ptr[0], reg_ptr[1], reg_ptr[2], reg_ptr[3]); + } + else { + static_assert(sizeof(T) == 4 || sizeof(T) == 8 || sizeof(T) == 16, "sizeof(T) is not supported"); + } +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm80_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm80_hip.hpp new file mode 100644 index 000000000..677cfef6a --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm80_hip.hpp @@ -0,0 +1,144 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800)) +# define CUTE_ARCH_CP_ASYNC_SM80_ENABLED +#endif + +namespace cute +{ + +/// Copy via cp.async with caching at all levels +template +struct SM80_CP_ASYNC_CACHEALWAYS +{ + using SRegisters = TS[1]; + using DRegisters = TD[1]; + + static_assert(sizeof(TS) == sizeof(TD), "cp.async requires sizeof(src_value_type) == sizeof(dst_value_type)"); + static_assert(sizeof(TS) == 4 || sizeof(TS) == 8 || sizeof(TS) == 16, "cp.async sizeof(TS) is not supported"); + + CUTE_HOST_DEVICE static void + copy(TS const& gmem_src, + TD & smem_dst) + { +#if defined(CUTE_ARCH_CP_ASYNC_SM80_ENABLED) + TS const* gmem_ptr = &gmem_src; + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile("cp.async.ca.shared.global [%0], [%1], %2;\n" + :: "r"(smem_int_ptr), + "l"(gmem_ptr), + "n"(sizeof(TS))); +#else + CUTE_RUNTIME_ASSERT("Support for cp.async instructions has not been enabled"); +#endif + } +}; + +/// Copy via cp.async with caching at global level +template +struct SM80_CP_ASYNC_CACHEGLOBAL +{ + using SRegisters = TS[1]; + using DRegisters = TD[1]; + + static_assert(sizeof(TS) == sizeof(TD), "cp.async requires sizeof(src_value_type) == sizeof(dst_value_type)"); + static_assert(sizeof(TS) == 4 || sizeof(TS) == 8 || sizeof(TS) == 16, "cp.async sizeof(TS) is not supported"); + + CUTE_HOST_DEVICE static void + copy(TS const& gmem_src, + TD & smem_dst) + { +#if defined(CUTE_ARCH_CP_ASYNC_SM80_ENABLED) + TS const* gmem_ptr = &gmem_src; + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile("cp.async.cg.shared.global [%0], [%1], %2;\n" + :: "r"(smem_int_ptr), + "l"(gmem_ptr), + "n"(sizeof(TS))); +#else + CUTE_RUNTIME_ASSERT("Support for cp.async instructions has not been enabled"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Establishes an ordering w.r.t previously issued cp.async instructions. Does not block. +CUTE_HOST_DEVICE +void +cp_async_fence() +{ +#if defined(CUTE_ARCH_CP_ASYNC_SM80_ENABLED) + asm volatile("cp.async.commit_group;\n" ::); +#endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Blocks until all but N previous cp.async.commit_group operations have committed. +template +CUTE_HOST_DEVICE +void +cp_async_wait() +{ +#if defined(CUTE_ARCH_CP_ASYNC_SM80_ENABLED) + if constexpr (N == 0) { + asm volatile("cp.async.wait_all;\n" ::); + } else { + asm volatile("cp.async.wait_group %0;\n" :: "n"(N)); + } +#endif +} + +template +CUTE_HOST_DEVICE +void +cp_async_wait(Int) +{ + return cp_async_wait(); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_desc_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_desc_hip.hpp new file mode 100644 index 000000000..dbb3a4b76 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_desc_hip.hpp @@ -0,0 +1,211 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if !defined(__CUDACC_RTC__) +#include +#include +#endif + +#include + +#include +#include + +#include +#include +#include // to_Format<[u]intX> +#include // to_Format + +namespace cute +{ + +////////////////////////////////////////////////////////////////////////////////////////////////////// +/// Barriers are 64-bit of user-managed information used in broadly two types syncronization patterns +/// 1) arrive/wait on threads (usage: cp.async and warp-specialized kernels) +/// 2) transaction-based (usage: TMA transaction where a CTA issues one transaction) +////////////////////////////////////////////////////////////////////////////////////////////////////// + +// Initialize barrier present in shared memory +CUTE_HOST_DEVICE +void +initialize_barrier(uint64_t& smem_barrier, // 64 bits user-manged barrier in smem + int thread_count = 1) // Thread count expected to arrive/wait on this barrier +{ +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_barrier); + asm volatile ("mbarrier.init.shared.b64 [%0], %1;\n" + :: "r"(smem_int_ptr), + "r"(thread_count)); +#endif +} + +// Set the number of bytes transfered per transaction and perform an arrive operation as well +CUTE_HOST_DEVICE +void +set_barrier_transaction_bytes(uint64_t& smem_barrier, // 64 bits user-manged barrier in smem + uint32_t bytes) // Number of bytes transfered by per TMA transaction +{ +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_barrier); + asm volatile ("mbarrier.arrive.expect_tx.shared.b64 _, [%0], %1;\n" + :: "r"(smem_int_ptr), + "r"(bytes)); +#endif +} + +// Barrier wait +CUTE_HOST_DEVICE +void +wait_barrier(uint64_t& smem_barrier, // 64 bits user-manged barrier in smem + int phase_bit) // Current phase bit the barrier waiting to flip +{ +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_barrier); + asm volatile( + "{\n" + ".reg .pred P1;\n" + "LAB_WAIT:\n" + "mbarrier.try_wait.parity.shared.b64 P1, [%0], %1;\n" + "@P1 bra.uni DONE;\n" + "bra.uni LAB_WAIT;\n" + "DONE:\n" + "}\n" + :: "r"(smem_int_ptr), + "r"(phase_bit)); + +#endif +} + +// Barrier arrive +CUTE_HOST_DEVICE +void +arrive_barrier(uint64_t& smem_barrier) // 64 bits user-manged barrier in smem +{ +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_barrier); + asm volatile( + "{\n" + ".reg .b64 state; \n" + "mbarrier.arrive.shared.b64 state, [%0];\n" + "}\n" + :: "r"(smem_int_ptr)); +#endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// TMA Descriptor and utilities +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace TMA { + +enum class SmemSwizzleBits : uint8_t { + DISABLE = 0, + B32 = 1, + B64 = 2, + B128 = 3, +}; + +#if (__CUDACC_VER_MAJOR__ >= 12) + +#if !defined(__CUDACC_RTC__) +/// @return The TMA descriptor datatype enum corresponding to T. +template +inline CUtensorMapDataType +to_CUtensorMapDataType() { + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT8; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT8; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT8; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT8; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT16; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT32; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_UINT64; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_INT32; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_INT64; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_FLOAT16; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_FLOAT32; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_FLOAT64; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_BFLOAT16; } else + if constexpr (is_same_v) { return CU_TENSOR_MAP_DATA_TYPE_TFLOAT32; } else + { static_assert(sizeof(T) < 0, "Unknown TMA Format!"); } +} + +inline CUtensorMapSwizzle +to_CUtensorMapSwizzle(SmemSwizzleBits const& t) { + switch (t) { + default: assert(false && "Unknown SmemSwizzleBits!"); + case SmemSwizzleBits::DISABLE: return CU_TENSOR_MAP_SWIZZLE_NONE; + case SmemSwizzleBits::B32: return CU_TENSOR_MAP_SWIZZLE_32B; + case SmemSwizzleBits::B64: return CU_TENSOR_MAP_SWIZZLE_64B; + case SmemSwizzleBits::B128: return CU_TENSOR_MAP_SWIZZLE_128B; + } +} +#endif // !defined(__CUDACC_RTC__) + +#endif // (__CUDACC_VER_MAJOR__ >= 12) + +} // end namespace TMA + +#if (__CUDACC_VER_MAJOR__ >= 12) && !defined(__CUDACC_RTC__) + using TmaDescriptor = CUtensorMap; +#else + using TmaDescriptor = struct alignas(64) { char bytes[128]; }; +#endif +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Initiates a TensorMap Prefetch +//////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTE_HOST_DEVICE +void +prefetch_tma_descriptor(TmaDescriptor const* desc_ptr) +{ +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + // Prefetch TMA Descriptor using generic addressing (i.e. no specific state space: const or param) + asm volatile ( + "prefetch.tensormap [%0];" + : + : "l"(gmem_int_desc) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use TMA Descriptor Prefetch without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif +} + +/////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_hip.hpp new file mode 100644 index 000000000..0e303a19e --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_hip.hpp @@ -0,0 +1,231 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) && (__CUDACC_VER_MAJOR__ >= 12)) +# define CUTE_ARCH_STSM_SM90_ENABLED +# define CUTE_ARCH_TMA_SM90_ENABLED +#endif + +namespace cute +{ + +struct SM90_U32x1_STSM_N +{ + using SRegisters = uint32_t[1]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src, + uint128_t & smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x1.m8n8.shared.b16 [%0], {%1};\n" + :: "r"(smem_int_ptr), + "r"(src)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +struct SM90_U32x2_STSM_N +{ + using SRegisters = uint32_t[2]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src0, uint32_t const& src1, + uint128_t& smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x2.m8n8.shared.b16 [%0], {%1, %2};\n" + :: "r"(smem_int_ptr), + "r"(src0), "r"(src1)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +struct SM90_U32x4_STSM_N +{ + using SRegisters = uint32_t[4]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src0, uint32_t const& src1, uint32_t const& src2, uint32_t const& src3, + uint128_t& smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x4.m8n8.shared.b16 [%0], {%1, %2, %3, %4};\n" + :: "r"(smem_int_ptr), + "r"(src0), "r"(src1), "r"(src2), "r"(src3)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +struct SM90_U16x2_STSM_T +{ + using SRegisters = uint32_t[1]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src, + uint128_t& smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x1.trans.m8n8.shared.b16 [%0], {%1};\n" + :: "r"(smem_int_ptr), + "r"(src)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +struct SM90_U16x4_STSM_T +{ + using SRegisters = uint32_t[2]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src0, uint32_t const& src1, + uint128_t& smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x2.trans.m8n8.shared.b16 [%0], {%1, %2};\n" + :: "r"(smem_int_ptr), + "r"(src0), "r"(src1)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +struct SM90_U16x8_STSM_T +{ + using SRegisters = uint32_t[4]; + using DRegisters = uint128_t[1]; + + CUTE_HOST_DEVICE static void + copy(uint32_t const& src0, uint32_t const& src1, uint32_t const& src2, uint32_t const& src3, + uint128_t& smem_dst) + { +#if defined(CUTE_ARCH_STSM_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(&smem_dst); + asm volatile ("stmatrix.sync.aligned.x4.trans.m8n8.shared.b16 [%0], {%1, %2, %3, %4};\n" + :: "r"(smem_int_ptr), + "r"(src0), "r"(src1), "r"(src2), "r"(src3)); +#else + CUTE_RUNTIME_ASSERT("Trying to use stmatrix without CUTE_ARCH_STSM_SM90_ENABLED."); +#endif + } +}; + +// +// Legacy STSM interfaces that aren't very useful +// + +template +CUTE_HOST_DEVICE +void +copy_stsm(T const* const rmem_ptr, + uint128_t* const smem_ptr) +{ + uint32_t const* reg_ptr = reinterpret_cast(rmem_ptr); + + // if constexpr + if (sizeof(T) == 4) { + SM90_U32x1_STSM_N::copy(reg_ptr[0], smem_ptr[0]); + } + else if (sizeof(T) == 8) { + SM90_U32x2_STSM_N::copy(reg_ptr[0], reg_ptr[1], smem_ptr[0]); + } + else if (sizeof(T) == 16) { + SM90_U32x4_STSM_N::copy(reg_ptr[0], reg_ptr[1], reg_ptr[2], reg_ptr[3], smem_ptr[0]); + } + else { + static_assert(sizeof(T) == 4 || sizeof(T) == 8 || sizeof(T) == 16, "sizeof(T) is not supported"); + } +} + +template +CUTE_HOST_DEVICE +void +copy_stsm_trans(T const* const rmem_ptr, + uint128_t* const smem_ptr) +{ + uint32_t const* reg_ptr = reinterpret_cast(rmem_ptr); + + // if constexpr + if (sizeof(T) == 4) { + SM90_U16x2_STSM_T::copy(reg_ptr[0], smem_ptr[0]); + } + else if (sizeof(T) == 8) { + SM90_U16x4_STSM_T::copy(reg_ptr[0], reg_ptr[1], smem_ptr[0]); + } + else if (sizeof(T) == 16) { + SM90_U16x8_STSM_T::copy(reg_ptr[0], reg_ptr[1], reg_ptr[2], reg_ptr[3], smem_ptr[0]); + } + else { + static_assert(sizeof(T) == 4 || sizeof(T) == 8 || sizeof(T) == 16, "sizeof(T) is not supported"); + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_tma_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_tma_hip.hpp new file mode 100644 index 000000000..5ebdd3b70 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_tma_hip.hpp @@ -0,0 +1,855 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +namespace cute +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// TMA_LOAD : Initiates a TMA copy from global memory to shared memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_1D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3}], [%2];" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(crd0) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_2D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4}], [%2];" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(crd0), "r"(crd1) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_3D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.3d.shared::cluster.global.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5}], [%2];" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(crd0), "r"(crd1), "r"(crd2) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_4D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.4d.shared::cluster.global.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5, %6}], [%2];" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_5D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.5d.shared::cluster.global.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5, %6, %7}], [%2];" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3), "r"(crd4) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0) + { + return SM90_TMA_LOAD_1D::copy(desc_ptr, smem_mbar, smem_ptr, crd0); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { + return SM90_TMA_LOAD_2D::copy(desc_ptr, smem_mbar, smem_ptr, crd0, crd1); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { + return SM90_TMA_LOAD_3D::copy(desc_ptr, smem_mbar, smem_ptr, crd0, crd1, crd2); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { + return SM90_TMA_LOAD_4D::copy(desc_ptr, smem_mbar, smem_ptr, crd0, crd1, crd2, crd3); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { + return SM90_TMA_LOAD_5D::copy(desc_ptr, smem_mbar, smem_ptr, crd0, crd1, crd2, crd3, crd4); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// TMA_LOAD im2col: Initiates a TMA copy, in im2col mode, from global memory to shared memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_IM2COL_3D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_n, + uint16_t const& offset_w) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.3d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5}], [%2], {%6};" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_n), + "h"(offset_w) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL_4D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_n, + uint16_t const& offset_w, + uint16_t const& offset_h) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.4d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5, %6}], [%2], {%7, %8};" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_h), "r"(coord_n), + "h"(offset_w), "h"(offset_h) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL_5D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_d, int32_t const& coord_n, + uint16_t const& offset_w, + uint16_t const& offset_h, + uint16_t const& offset_d) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.5d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes" + " [%0], [%1, {%3, %4, %5, %6, %7}], [%2], {%8, %9, %10};" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_h), "r"(coord_d), "r"(coord_n), + "h"(offset_w), "h"(offset_h), "h"(offset_d) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_n, + uint16_t const& offset_w) + { + return SM90_TMA_LOAD_IM2COL_3D::copy(desc_ptr, smem_mbar, smem_ptr, + coord_c, coord_w, coord_n, + offset_w); + } + + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_n, + uint16_t const& offset_w, + uint16_t const& offset_h) + { + return SM90_TMA_LOAD_IM2COL_4D::copy(desc_ptr, smem_mbar, smem_ptr, + coord_c, coord_w, coord_h, coord_n, + offset_w, offset_h); + } + + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_d, int32_t const& coord_n, + uint16_t const& offset_w, + uint16_t const& offset_h, + uint16_t const& offset_d) + { + return SM90_TMA_LOAD_IM2COL_5D::copy(desc_ptr, smem_mbar, smem_ptr, + coord_c, coord_w, coord_h, coord_d, coord_n, + offset_w, offset_h, offset_d); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// TMA_LOAD_MULTICAST: Initiates a TMA copy from global memory to shared memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_MULTICAST_1D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%4}], [%2], %3;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "h"(multicast_mask), + "r"(crd0) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_MULTICAST_2D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%4, %5}], [%2], %3;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "h"(multicast_mask), + "r"(crd0), "r"(crd1) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_MULTICAST_3D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.3d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%4, %5, %6}], [%2], %3;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "h"(multicast_mask), + "r"(crd0), "r"(crd1), "r"(crd2) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_MULTICAST_4D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.4d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%4, %5, %6, %7}], [%2], %3;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "h"(multicast_mask), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_MULTICAST_5D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.5d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%4, %5, %6, %7, %8}], [%2], %3;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "h"(multicast_mask), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3), "r"(crd4) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_MULTICAST +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0) + { + return SM90_TMA_LOAD_MULTICAST_1D::copy(desc_ptr, smem_mbar, multicast_mask, smem_ptr, crd0); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { + return SM90_TMA_LOAD_MULTICAST_2D::copy(desc_ptr, smem_mbar, multicast_mask, smem_ptr, crd0, crd1); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { + return SM90_TMA_LOAD_MULTICAST_3D::copy(desc_ptr, smem_mbar, multicast_mask, smem_ptr, crd0, crd1, crd2); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { + return SM90_TMA_LOAD_MULTICAST_4D::copy(desc_ptr, smem_mbar, multicast_mask, smem_ptr, crd0, crd1, crd2, crd3); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { + return SM90_TMA_LOAD_MULTICAST_5D::copy(desc_ptr, smem_mbar, multicast_mask, smem_ptr, crd0, crd1, crd2, crd3, crd4); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// TMA_LOAD_MULTICAST im2col: Initiates a TMA copy, in im2col mode, from global memory to shared memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_IM2COL_MULTICAST_3D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_n, + uint16_t const& offset_w) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.3d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%3, %4, %5}], [%2], {%6}, %7;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_n), + "h"(offset_w), + "h"(multicast_mask) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL_MULTICAST_4D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_n, + uint16_t const& offset_w, uint16_t const& offset_h) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.4d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%3, %4, %5, %6}], [%2], {%7, %8}, %9;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_h), "r"(coord_n), + "h"(offset_w), "h"(offset_h), + "h"(multicast_mask) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL_MULTICAST_5D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_d, int32_t const& coord_n, + uint16_t const& offset_w, uint16_t const& offset_h, uint16_t const& offset_d) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + // Copy from global to shared::cluster. + asm volatile ( + "cp.async.bulk.tensor.5d.shared::cluster.global.im2col.mbarrier::complete_tx::bytes.multicast::cluster" + " [%0], [%1, {%3, %4, %5, %6, %7}], [%2], {%8, %9, %10}, %11;" + : + : "r"(smem_int_ptr), "l"(gmem_int_desc), "r"(smem_int_mbar), + "r"(coord_c), "r"(coord_w), "r"(coord_h), "r"(coord_d), "r"(coord_n), + "h"(offset_w), "h"(offset_h), "h"(offset_d), + "h"(multicast_mask) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_LOAD_IM2COL_MULTICAST +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_n, + uint16_t const& offset_w) + { + return SM90_TMA_LOAD_IM2COL_MULTICAST_3D::copy(desc_ptr, smem_mbar, multicast_mask, + smem_ptr, + coord_c, coord_w, coord_n, + offset_w); + } + + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_n, + uint16_t const& offset_w, uint16_t const& offset_h) + { + return SM90_TMA_LOAD_IM2COL_MULTICAST_4D::copy(desc_ptr, smem_mbar, multicast_mask, + smem_ptr, + coord_c, coord_w, coord_h, coord_n, + offset_w, offset_h); + } + + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, uint64_t& smem_mbar, uint16_t multicast_mask, + void const* const smem_ptr, + int32_t const& coord_c, int32_t const& coord_w, int32_t const& coord_h, int32_t const& coord_d, int32_t const& coord_n, + uint16_t const& offset_w, uint16_t const& offset_h, uint16_t const& offset_d) + { + return SM90_TMA_LOAD_IM2COL_MULTICAST_5D::copy(desc_ptr, smem_mbar, multicast_mask, + smem_ptr, + coord_c, coord_w, coord_h, coord_d, coord_n, + offset_w, offset_h, offset_d); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// TMA_STORE : Initiates a TMA copy from shared memory to global memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_STORE_1D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.1d.global.shared::cta.bulk_group [%0, {%2}], [%1];" + : + : "l"(gmem_int_desc), "r"(smem_int_ptr), + "r"(crd0) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_STORE_2D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.2d.global.shared::cta.bulk_group [%0, {%2, %3}], [%1];" + : + : "l"(gmem_int_desc), "r"(smem_int_ptr), + "r"(crd0), "r"(crd1) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_STORE_3D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.3d.global.shared::cta.bulk_group [%0, {%2, %3, %4}], [%1];" + : + : "l"(gmem_int_desc), "r"(smem_int_ptr), + "r"(crd0), "r"(crd1), "r"(crd2) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_STORE_4D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.4d.global.shared::cta.bulk_group [%0, {%2, %3, %4, %5}], [%1];" + : + : "l"(gmem_int_desc), "r"(smem_int_ptr), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_STORE_5D +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint64_t gmem_int_desc = reinterpret_cast(desc_ptr); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile ( + "cp.async.bulk.tensor.5d.global.shared::cta.bulk_group [%0, {%2, %3, %4, %5, %6}], [%1];" + : + : "l"(gmem_int_desc), "r"(smem_int_ptr), + "r"(crd0), "r"(crd1), "r"(crd2), "r"(crd3), "r"(crd4) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_TMA_STORE +{ + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0) + { + return SM90_TMA_STORE_1D::copy(desc_ptr, smem_ptr, crd0); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1) + { + return SM90_TMA_STORE_2D::copy(desc_ptr, smem_ptr, crd0, crd1); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2) + { + return SM90_TMA_STORE_3D::copy(desc_ptr, smem_ptr, crd0, crd1, crd2); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3) + { + return SM90_TMA_STORE_4D::copy(desc_ptr, smem_ptr, crd0, crd1, crd2, crd3); + } + CUTE_HOST_DEVICE static void + copy(void const* const desc_ptr, + void const* const smem_ptr, + int32_t const& crd0, int32_t const& crd1, int32_t const& crd2, int32_t const& crd3, int32_t const& crd4) + { + return SM90_TMA_STORE_5D::copy(desc_ptr, smem_ptr, crd0, crd1, crd2, crd3, crd4); + } +}; + +// Indicate arrival of warp issuing TMA_STORE +CUTE_HOST_DEVICE static void +tma_store_arrive() { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + asm volatile("cp.async.bulk.commit_group;"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif +} + +// Wait on prior N (Count) TMA_STORE instructions to complete +template +CUTE_HOST_DEVICE static void +tma_store_wait() { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + asm volatile( + "cp.async.bulk.wait_group.read %0;" + : + : "n"(Count) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use tma without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// BULK_COPY : Copy a bulk of memory between shared memory and global memory +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM90_BULK_COPY_G2S +{ + CUTE_HOST_DEVICE static void + copy(void const* const gmem_ptr, uint64_t& smem_mbar, + void const* const smem_ptr, int32_t load_bytes) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_mbar = cast_smem_ptr_to_uint(&smem_mbar); + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile("cp.async.bulk.shared::cluster.global.mbarrier::complete_tx::bytes [%0], [%1], %2, [%3];\n" + : + : "r"(smem_int_ptr), "l"(gmem_ptr), "r"(load_bytes), "r"(smem_int_mbar) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use BULK_COPY without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_BULK_COPY_S2G +{ + CUTE_HOST_DEVICE static void + copy(void const* const smem_ptr, + void const* const gmem_ptr, int32_t store_bytes) + { +#if defined(CUTE_ARCH_TMA_SM90_ENABLED) + uint32_t smem_int_ptr = cast_smem_ptr_to_uint(smem_ptr); + asm volatile("cp.async.bulk.global.shared::cta.bulk_group [%0], [%1], %2;\n" + : + : "l"(gmem_ptr), "r"(smem_int_ptr), "r"(store_bytes) + : "memory"); +#else + CUTE_RUNTIME_ASSERT("Trying to use BULK_COPY without CUTE_ARCH_TMA_SM90_ENABLED."); +#endif + } +}; + +struct SM90_BULK_COPY_AUTO {}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx906_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx906_hip.hpp new file mode 100644 index 000000000..6297d610e --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx906_hip.hpp @@ -0,0 +1,90 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +struct GFX906_DP4A +{ + using DRegisters = int32_t[1]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = int32_t[1]; + + CUTE_HOST_DEVICE static void + fma(int32_t& d, uint32_t const& a, uint32_t const& b, int32_t const& c) + { + #if defined(DCU_ASM) + asm volatile("v_dot4_i32_i8 %0, %1, %2, %3;" + : "=v"(d) + : "v"(a), "v"(b), "v"(c)); + #endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// 1x1x2 half mix +// + +struct GFX906_DP2A +{ + using DRegisters = float[1]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = float[1]; + + CUTE_HOST_DEVICE static void + fma(float& d, uint32_t const& a, uint32_t const& b, float const& c) + { +#if defined(DCU_ASM) + asm volatile("v_dot2_f32_f16 %0,%1,%2,%3\n\t" + :"=v"(d) + :"v"(a),"v"(b),"v"(c)); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx926_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx926_hip.hpp new file mode 100644 index 000000000..c9e12556f --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx926_hip.hpp @@ -0,0 +1,158 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +/*************************************************************************************************** + * Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +struct GFX926_16x16x4_F32F32F32F32_NT +{ + using DRegisters = float[4]; + using ARegisters = float[1]; + using BRegisters = float[1]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float const& a0, + float const& b0, + float const& c0, float const& c1, float const& c2, float const& c3) + { +#if defined(__gfx926__) && defined(DCU_ASM) + v4f c; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + v4f d; + asm volatile("v_mmac_16x16x4_f32 %0, %1, %2, %3, vstep:0\n\t" + : "+v"(d) + : "v"(a0), "v"(b0), "v"(c)); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + // 模拟mmac计算 + // __shared__ float sA[8][4][16]; // assume max 8 warp + // __shared__ float sB[8][4][16]; // assume max 8 warp + + // // // int lane_id = __lane_id(); + // int lane_id = threadIdx.x % 64; + // int warp_id = threadIdx.x / 64; + + // sA[warp_id][lane_id / 16][lane_id % 16] = a0; + // sB[warp_id][lane_id / 16][lane_id % 16] = b0; + + // __syncthreads(); + + // float acc[4]; + + // acc[0] = c0; + // acc[1] = c1; + // acc[2] = c2; + // acc[3] = c3; + + // for(int i=0; i<4; i++){ + // for(int k=0; k<4; k++){ + // acc[i] += sA[warp_id][k][lane_id % 16] * sB[warp_id][k][lane_id / 16 + i * 4]; + // } + // } + // __syncthreads(); + + // d0 = acc[0]; + // d1 = acc[1]; + // d2 = acc[2]; + // d3 = acc[3]; + +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// SM75 MMA 8816 S8S8S32 +// + +struct GFX926_32x32x4_F32F32F32F32_NT +{ + using DRegisters = float[2]; + using ARegisters = float[2]; + using BRegisters = float[2]; + using CRegisters = float[2]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, + float const& a0, float const& a1, + float const& b0, float const& b1, + float const& c0, float const& c1) + { + + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct GFX926_64x64x4_F32F32F32F32_NT +{ + using DRegisters = float[4]; + using ARegisters = float[4]; + using BRegisters = float[4]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float const& a0, float const& a1, float const& a2, float const& a3, + float const& b0, float const& b1, float const& b2, float const& b3, + float const& c0, float const& c1, float const& c2, float const& c3) + { + + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx928_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx928_hip.hpp new file mode 100644 index 000000000..11837a588 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx928_hip.hpp @@ -0,0 +1,2326 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + + +namespace cute +{ + struct GFX928_16x16x8_F32F32F32F32_NT + { + using DRegisters = float[4]; + using ARegisters = float[2]; + using BRegisters = float[2]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float const& a0, float const& a1, + float const& b0, float const& b1, + float const& c0, float const& c1, float const& c2, float const& c3) + { + // printf("a:%f %f b:%f %f\n",a0,a1,b0,b1); + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + v2f a; + v2f b; + a.x = a0; + a.y = a1; + b.x = b0; + b.y = b1; + + #ifndef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x8f32(a, b, c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + v2f a; + v2f b; + a.x = a0; + a.y = a1; + b.x = b0; + b.y = b1; + + d = __builtin_hcu_mmac_16x16x8_f32_lit_lts(a, b, c,false,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + #endif + } + }; + + struct GFX928_16x16x8_F32TF32TF32F32_NT + { + using DRegisters = float[4]; + using ARegisters = float[2]; + using BRegisters = float[2]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float const& a0, float const& a1, + float const& b0, float const& b1, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + v2f a; + v2f b; + a.x = a0; + a.y = a1; + b.x = b0; + b.y = b1; + + #ifndef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x8tf32(a, b, c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + v2f a; + v2f b; + a.x = a0; + a.y = a1; + b.x = b0; + b.y = b1; + + d = __builtin_hcu_mmac_f32_16x16x8_tf32_lit_lts(a, b, c,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + + + #endif + } + }; + struct GFX928_16x16x16_F32F16F16F32_NT + { + using DRegisters = float[4]; + using ARegisters = half_t[4]; + using BRegisters = half_t[4]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A,B; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + + #ifdef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B,c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A,B,c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A,B; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A,B,c,false,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + #endif + } + + }; + + struct GFX928_16x16x16_F32BF16BF16F32_NT + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[4]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__) ) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; + a[1] = a1; + a[2] = a2; + a[3] = a3; + cutlass::Array b; + b[0] = b0; + b[1] = b1; + b[2] = b2; + b[3] = b3; + + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A,B; + A = *(reinterpret_cast<__bf16x4_t *>(&a)); + B = *(reinterpret_cast<__bf16x4_t *>(&b)); + + #ifdef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B, c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B, c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; + a[1] = a1; + a[2] = a2; + a[3] = a3; + cutlass::Array b; + b[0] = b0; + b[1] = b1; + b[2] = b2; + b[3] = b3; + + + __bf16x4_t A,B; + A = *(reinterpret_cast<__bf16x4_t *>(&a)); + B = *(reinterpret_cast<__bf16x4_t *>(&b)); + + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B, c,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #endif + } + }; + + struct GFX928_16x16x32_F32F16F16F32_NT + { + using DRegisters = float[4]; + using ARegisters = half_t[8]; + using BRegisters = half_t[8]; + using CRegisters = float[4]; + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5,half_t const& a6, half_t const& a7, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A0, A1, B0, B1; + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + #ifdef HG_ROCM + // d = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B,c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A0,B0,c); + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A1,B1,d); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A0, A1, B0, B1; + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0,B0,c,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1,B1,d,false,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #endif + } + + }; + + struct GFX928_16x16x64_F32F16uint8F32_NT + { + using DRegisters = float[4]; + using ARegisters = half_t[16]; + using BRegisters = uint8_t[16]; + using CRegisters = float[4]; + }; + + struct GFX928_16x16x64_F32BF16int8F32_NT + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[16]; + using BRegisters = uint8_t[16]; + using CRegisters = float[4]; + }; + + struct GFX928_16x16x64_F32F16F16F32_NT + { + using DRegisters = float[4]; + using ARegisters = half_t[16]; + using BRegisters = half_t[16]; + using CRegisters = float[4]; + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + half_t const& a0, half_t const& a1, half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5, half_t const& a6, half_t const& a7, + half_t const& a8, half_t const& a9, half_t const& a10, half_t const& a11, + half_t const& a12, half_t const& a13, half_t const& a14, half_t const& a15, + half_t const& b0, half_t const& b1, half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5, half_t const& b6, half_t const& b7, + half_t const& b8, half_t const& b9, half_t const& b10, half_t const& b11, + half_t const& b12, half_t const& b13, half_t const& b14, half_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A0, A1, A2, A3, B0, B1, B2, B3; + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + + A2.x = a8; A2.y = a9; A2.z = a10; A2.w = a11; + A3.x = a12; A3.y = a13; A3.z = a14; A3.w = a15; + + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + B2.x = b8; B2.y = b9; B2.z = b10; B2.w = b11; + B3.x = b12; B3.y = b13; B3.z = b14; B3.w = b15; + + #ifdef HG_ROCM + // d = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B,c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A0,B0,c); + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A1,B1,d); + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A2,B2,d); + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A3,B3,d); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A0, A1, A2, A3, B0, B1, B2, B3; + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + + A2.x = a8; A2.y = a9; A2.z = a10; A2.w = a11; + A3.x = a12; A3.y = a13; A3.z = a14; A3.w = a15; + + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + B2.x = b8; B2.y = b9; B2.z = b10; B2.w = b11; + B3.x = b12; B3.y = b13; B3.z = b14; B3.w = b15; + + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0,B0,c,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1,B1,d,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A2,B2,d,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A3,B3,d,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + + + #endif + } + + }; + struct GFX928_16x16x64_F32BF16BF16F32_NT + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[16]; + using BRegisters = bfloat16_t[16]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + bfloat16_t const& a0, bfloat16_t const& a1, bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& a4, bfloat16_t const& a5, bfloat16_t const& a6, bfloat16_t const& a7, + bfloat16_t const& a8, bfloat16_t const& a9, bfloat16_t const& a10, bfloat16_t const& a11, + bfloat16_t const& a12, bfloat16_t const& a13, bfloat16_t const& a14, bfloat16_t const& a15, + bfloat16_t const& b0, bfloat16_t const& b1, bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5, bfloat16_t const& b6, bfloat16_t const& b7, + bfloat16_t const& b8, bfloat16_t const& b9, bfloat16_t const& b10, bfloat16_t const& b11, + bfloat16_t const& b12, bfloat16_t const& b13, bfloat16_t const& b14, bfloat16_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0, a_1, a_2, a_3; + a_0[0] = a0; + a_0[1] = a1; + a_0[2] = a2; + a_0[3] = a3; + + a_1[0] = a4; + a_1[1] = a5; + a_1[2] = a6; + a_1[3] = a7; + + a_2[0] = a8; + a_2[1] = a9; + a_2[2] = a10; + a_2[3] = a11; + + a_3[0] = a12; + a_3[1] = a13; + a_3[2] = a14; + a_3[3] = a15; + + cutlass::Array b_0, b_1, b_2, b_3; + b_0[0] = b0; + b_0[1] = b1; + b_0[2] = b2; + b_0[3] = b3; + + b_1[0] = b4; + b_1[1] = b5; + b_1[2] = b6; + b_1[3] = b7; + + b_2[0] = b8; + b_2[1] = b9; + b_2[2] = b10; + b_2[3] = b11; + + b_3[0] = b12; + b_3[1] = b13; + b_3[2] = b14; + b_3[3] = b15; + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A0, A1, A2, A3, B0, B1, B2, B3; + A0 = *(reinterpret_cast<__bf16x4_t *>(&a_0)); + A1 = *(reinterpret_cast<__bf16x4_t *>(&a_1)); + A2 = *(reinterpret_cast<__bf16x4_t *>(&a_2)); + A3 = *(reinterpret_cast<__bf16x4_t *>(&a_3)); + + B0 = *(reinterpret_cast<__bf16x4_t *>(&b_0)); + B1 = *(reinterpret_cast<__bf16x4_t *>(&b_1)); + B2 = *(reinterpret_cast<__bf16x4_t *>(&b_2)); + B3 = *(reinterpret_cast<__bf16x4_t *>(&b_3)); + + #ifdef HG_ROCM + // d = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B, c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B0, c); + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B1, d); + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A2, B2, d); + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A3, B3, d); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0, a_1, a_2, a_3; + a_0[0] = a0; + a_0[1] = a1; + a_0[2] = a2; + a_0[3] = a3; + + a_1[0] = a4; + a_1[1] = a5; + a_1[2] = a6; + a_1[3] = a7; + + a_2[0] = a8; + a_2[1] = a9; + a_2[2] = a10; + a_2[3] = a11; + + a_3[0] = a12; + a_3[1] = a13; + a_3[2] = a14; + a_3[3] = a15; + + cutlass::Array b_0, b_1, b_2, b_3; + b_0[0] = b0; + b_0[1] = b1; + b_0[2] = b2; + b_0[3] = b3; + + b_1[0] = b4; + b_1[1] = b5; + b_1[2] = b6; + b_1[3] = b7; + + b_2[0] = b8; + b_2[1] = b9; + b_2[2] = b10; + b_2[3] = b11; + + b_3[0] = b12; + b_3[1] = b13; + b_3[2] = b14; + b_3[3] = b15; + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A0, A1, A2, A3, B0, B1, B2, B3; + A0 = *(reinterpret_cast<__bf16x4_t *>(&a_0)); + A1 = *(reinterpret_cast<__bf16x4_t *>(&a_1)); + A2 = *(reinterpret_cast<__bf16x4_t *>(&a_2)); + A3 = *(reinterpret_cast<__bf16x4_t *>(&a_3)); + + B0 = *(reinterpret_cast<__bf16x4_t *>(&b_0)); + B1 = *(reinterpret_cast<__bf16x4_t *>(&b_1)); + B2 = *(reinterpret_cast<__bf16x4_t *>(&b_2)); + B3 = *(reinterpret_cast<__bf16x4_t *>(&b_3)); + + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B0, c,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B1, d,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A2, B2, d,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A3, B3, d,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + + + + + #endif + } + }; + + struct GFX928_16x16x32_F32BF16BF16F32_NT + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[8]; + using BRegisters = bfloat16_t[8]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + bfloat16_t const& a0, bfloat16_t const& a1, bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& a4, bfloat16_t const& a5, bfloat16_t const& a6, bfloat16_t const& a7, + bfloat16_t const& b0, bfloat16_t const& b1, bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5, bfloat16_t const& b6, bfloat16_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0, a_1; + a_0[0] = a0; + a_0[1] = a1; + a_0[2] = a2; + a_0[3] = a3; + + a_1[0] = a4; + a_1[1] = a5; + a_1[2] = a6; + a_1[3] = a7; + cutlass::Array b_0, b_1; + b_0[0] = b0; + b_0[1] = b1; + b_0[2] = b2; + b_0[3] = b3; + + b_1[0] = b4; + b_1[1] = b5; + b_1[2] = b6; + b_1[3] = b7; + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A0, A1, B0, B1; + A0 = *(reinterpret_cast<__bf16x4_t *>(&a_0)); + A1 = *(reinterpret_cast<__bf16x4_t *>(&a_1)); + B0 = *(reinterpret_cast<__bf16x4_t *>(&b_0)); + B1 = *(reinterpret_cast<__bf16x4_t *>(&b_1)); + + #ifdef HG_ROCM + // d = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B, c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B0, c); + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B1, d); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0, a_1; + a_0[0] = a0; + a_0[1] = a1; + a_0[2] = a2; + a_0[3] = a3; + + a_1[0] = a4; + a_1[1] = a5; + a_1[2] = a6; + a_1[3] = a7; + cutlass::Array b_0, b_1; + b_0[0] = b0; + b_0[1] = b1; + b_0[2] = b2; + b_0[3] = b3; + + b_1[0] = b4; + b_1[1] = b5; + b_1[2] = b6; + b_1[3] = b7; + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A0, A1, B0, B1; + A0 = *(reinterpret_cast<__bf16x4_t *>(&a_0)); + A1 = *(reinterpret_cast<__bf16x4_t *>(&a_1)); + B0 = *(reinterpret_cast<__bf16x4_t *>(&b_0)); + B1 = *(reinterpret_cast<__bf16x4_t *>(&b_1)); + + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B0, c,false,false); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B1, d,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #endif + } + }; + + struct GFX928_16x16x32_I32I8I8I32_NT + { + using DRegisters = int[4]; + using ARegisters = int8_t[8]; + using BRegisters = int8_t[8]; + using CRegisters = int[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int & d0, int & d1, int & d2, int & d3, + int8_t const& a0, int8_t const& a1,int8_t const& a2, int8_t const& a3, + int8_t const& a4, int8_t const& a5,int8_t const& a6, int8_t const& a7, + int8_t const& b0, int8_t const& b1,int8_t const& b2, int8_t const& b3, + int8_t const& b4, int8_t const& b5,int8_t const& b6, int8_t const& b7, + int const& c0, int const& c1, int const& c2, int const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__) ) && defined(DCU_ASM) + intx4_t c; + intx4_t d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b; + b[0] = b0;b[1] = b1;b[2] = b2;b[3] = b3; + b[4] = b4;b[5] = b5;b[6] = b6;b[7] = b7; + long A, B; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b)); + + #ifndef HG_ROCM + d = __builtin_amdgcn_mmac_i32_16x16x32i8(A,B,c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + intx4_t c; + intx4_t d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b; + b[0] = b0;b[1] = b1;b[2] = b2;b[3] = b3; + b[4] = b4;b[5] = b5;b[6] = b6;b[7] = b7; + intx2_t A, B; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b)); + + d = __builtin_hcu_mmac_i32_16x16x32_i8_lit_clamp_lts(A,B,c,false,false,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + #endif + } + + }; + + struct GFX928_16x16x32_I32U8U8I32_NT + { + using DRegisters = int[4]; + using ARegisters = uint8_t[8]; + using BRegisters = uint8_t[8]; + using CRegisters = int[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int & d0, int & d1, int & d2, int & d3, + uint8_t const& a0, uint8_t const& a1,uint8_t const& a2, uint8_t const& a3, + uint8_t const& a4, uint8_t const& a5,uint8_t const& a6, uint8_t const& a7, + uint8_t const& b0, uint8_t const& b1,uint8_t const& b2, uint8_t const& b3, + uint8_t const& b4, uint8_t const& b5,uint8_t const& b6, uint8_t const& b7, + int const& c0, int const& c1, int const& c2, int const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__) ) && defined(DCU_ASM) + intx4_t c; + intx4_t d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b; + b[0] = b0;b[1] = b1;b[2] = b2;b[3] = b3; + b[4] = b4;b[5] = b5;b[6] = b6;b[7] = b7; + long A, B; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b)); + + #ifndef HG_ROCM + d = __builtin_amdgcn_mmac_i32_16x16x32u8(A,B,c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + intx4_t c; + intx4_t d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b; + b[0] = b0;b[1] = b1;b[2] = b2;b[3] = b3; + b[4] = b4;b[5] = b5;b[6] = b6;b[7] = b7; + intx2_t A, B; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b)); + + d = __builtin_hcu_mmac_i32_16x16x32_u8_lit_clamp_lts(A,B,c,false,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #endif + } + }; + +////////////////////////////v_mmac_32x32x16 concatenate by 2x16x16x16///////////////////////////////////////////// + struct GFX928_32x32x16_F32F16F16F32_NT + { + using DRegisters = float[16]; + using ARegisters = half_t[8]; + using BRegisters = half_t[8]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5,half_t const& a6, half_t const& a7, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + C2.x = c8; C2.y = c9; C2.z = c10; C2.w = c11; + C3.x = c12; C3.y = c13; C3.z = c14; C3.w = c15; + + __fp16x4_t A,B,A0,B0; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + A0.x = a4; A0.y = a5; A0.z = a6; A0.w = a7; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + B0.x = b4; B0.y = b5; B0.z = b6; B0.w = b7; + + + asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + : "+v"(D0) + : "v"(A), "v"(B), "v"(C0)); + asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + : "+v"(D1) + : "v"(A), "v"(B0), "v"(C1)); + asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + : "+v"(D2) + : "v"(A0), "v"(B), "v"(C2)); + asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + : "+v"(D3) + : "v"(A0), "v"(B0), "v"(C3)); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + d8 = D2.x; d9 = D2.y; d10 = D2.z; d11 = D2.w; + d12 = D3.x; d13 = D3.y; d14 = D3.z; d15 = D3.w; + + #endif + } + }; + + struct GFX928_32x32x16_F32F16F16F32_NT_ALT + { + using DRegisters = float[16]; + using ARegisters = half_t[8]; + using BRegisters = half_t[8]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5,half_t const& a6, half_t const& a7, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + C2.x = c8; C2.y = c9; C2.z = c10; C2.w = c11; + C3.x = c12; C3.y = c13; C3.z = c14; C3.w = c15; + + __fp16x4_t A,B,A0,B0; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + A0.x = a4; A0.y = a5; A0.z = a6; A0.w = a7; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + B0.x = b4; B0.y = b5; B0.z = b6; B0.w = b7; + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B,C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B0,C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0,B,C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0,B0,C3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A,B,C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A,B0,C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0,B,C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0,B0,C3); + #endif + + // asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + // : "+v"(D0) + // : "v"(A), "v"(B), "v"(C0)); + // asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + // : "+v"(D1) + // : "v"(A), "v"(B0), "v"(C1)); + // asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + // : "+v"(D2) + // : "v"(A0), "v"(B), "v"(C2)); + // asm volatile("v_mmac_f32_16x16x16_f16 %0, %1, %2, %3\n\t" + // : "+v"(D3) + // : "v"(A0), "v"(B0), "v"(C3)); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + d8 = D2.x; d9 = D2.y; d10 = D2.z; d11 = D2.w; + d12 = D3.x; d13 = D3.y; d14 = D3.z; d15 = D3.w; + + + #endif + + } + + }; + + struct GFX928_32x32x16_F32BF16BF16F32_NT_ALT + { + using DRegisters = float[16]; + using ARegisters = bfloat16_t[8]; + using BRegisters = bfloat16_t[8]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& a4, bfloat16_t const& a5,bfloat16_t const& a6, bfloat16_t const& a7, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + C2.x = c8; C2.y = c9; C2.z = c10; C2.w = c11; + C3.x = c12; C3.y = c13; C3.z = c14; C3.w = c15; + + cutlass::Array array_a0, array_a1; + cutlass::Array array_b0, array_b1; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + array_a1[0] = a4; array_a1[1] = a5; array_a1[2] = a6; array_a1[3] = a7; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + + __bf16x4_t A,B,A0,B0; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + A0 = *reinterpret_cast<__bf16x4_t*>(&array_a1); + B = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A,B,C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A,B0,C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0,B,C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0,B0,C3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A,B,C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A,B0,C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0,B,C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0,B0,C3); + #endif + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + d8 = D2.x; d9 = D2.y; d10 = D2.z; d11 = D2.w; + d12 = D3.x; d13 = D3.y; d14 = D3.z; d15 = D3.w; + + #endif + + } + + }; +////////////////////////////v_mmac_32x32x32 concatenate by 2x16x16x32///////////////////////////////////////////// + struct GFX928_32x32x32_I32I8I8I32_NT + { + using DRegisters = int[16]; + using ARegisters = int8_t[16]; + using BRegisters = int8_t[16]; + using CRegisters = int[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int & d00, int & d01, int & d02, int & d03, + int & d04, int & d05, int & d06, int & d07, + int & d08, int & d09, int & d10, int & d11, + int & d12, int & d13, int & d14, int & d15, + int8_t const& a00, int8_t const& a01, int8_t const& a02, int8_t const& a03, + int8_t const& a04, int8_t const& a05, int8_t const& a06, int8_t const& a07, + int8_t const& a08, int8_t const& a09, int8_t const& a10, int8_t const& a11, + int8_t const& a12, int8_t const& a13, int8_t const& a14, int8_t const& a15, + int8_t const& b00, int8_t const& b01, int8_t const& b02, int8_t const& b03, + int8_t const& b04, int8_t const& b05, int8_t const& b06, int8_t const& b07, + int8_t const& b08, int8_t const& b09, int8_t const& b10, int8_t const& b11, + int8_t const& b12, int8_t const& b13, int8_t const& b14, int8_t const& b15, + int const& c00, int const& c01, int const& c02, int const& c03, + int const& c04, int const& c05, int const& c06, int const& c07, + int const& c08, int const& c09, int const& c10, int const& c11, + int const& c12, int const& c13, int const& c14, int const& c15) + { + #if defined(__gfx928__) && defined(DCU_ASM) + intx4_t C0, C1, C2, C3; + intx4_t D0, D1, D2, D3; + C0.x = c00; C0.y = c01; C0.z = c02; C0.w = c03; + C1.x = c04; C1.y = c05; C1.z = c06; C1.w = c07; + C2.x = c08; C2.y = c09; C2.z = c10; C2.w = c11; + C3.x = c12; C3.y = c13; C3.z = c14; C3.w = c15; + + cutlass::Array a; + a[0] = a00; a[1] = a01; a[2] = a02; a[3] = a03; + a[4] = a04; a[5] = a05; a[6] = a06; a[7] = a07; + a[8] = a08; a[9] = a09; a[10] = a10; a[11] = a11; + a[12] = a12; a[13] = a13; a[14] = a14; a[15] = a15; + + cutlass::Array b; + b[0] = b00; b[1] = b01; b[2] = b02; b[3] = b03; + b[4] = b04; b[5] = b05; b[6] = b06; b[7] = b07; + b[8] = b08; b[9] = b09; b[10] = b10; b[11] = b11; + b[12] = b12; b[13] = b13; b[14] = b14; b[15] = b15; + long A0, A1, B0, B1; + A0 = *(reinterpret_cast(&a)); + B0 = *(reinterpret_cast(&b)); + A1 = *(reinterpret_cast(&a) + 1); + B1 = *(reinterpret_cast(&b) + 1); + #ifndef HG_ROCM + D0 = __builtin_amdgcn_mmac_i32_16x16x32i8(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_i32_16x16x32i8(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_i32_16x16x32i8(A1, B0, C2); + D3 = __builtin_amdgcn_mmac_i32_16x16x32i8(A1, B1, C3); + #endif + + d00 = D0.x; d01 = D0.y; d02 = D0.z; d03 = D0.w; + d04 = D1.x; d05 = D1.y; d06 = D1.z; d07 = D1.w; + d08 = D2.x; d09 = D2.y; d10 = D2.z; d11 = D2.w; + d12 = D3.x; d13 = D3.y; d14 = D3.z; d15 = D3.w; + #endif + } + + }; + + struct GFX928_32x32x32_I32U8U8I32_NT + { + using DRegisters = int[16]; + using ARegisters = uint8_t[16]; + using BRegisters = uint8_t[16]; + using CRegisters = int[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int & d00, int & d01, int & d02, int & d03, + int & d04, int & d05, int & d06, int & d07, + int & d08, int & d09, int & d10, int & d11, + int & d12, int & d13, int & d14, int & d15, + uint8_t const& a00, uint8_t const& a01, uint8_t const& a02, uint8_t const& a03, + uint8_t const& a04, uint8_t const& a05, uint8_t const& a06, uint8_t const& a07, + uint8_t const& a08, uint8_t const& a09, uint8_t const& a10, uint8_t const& a11, + uint8_t const& a12, uint8_t const& a13, uint8_t const& a14, uint8_t const& a15, + uint8_t const& b00, uint8_t const& b01, uint8_t const& b02, uint8_t const& b03, + uint8_t const& b04, uint8_t const& b05, uint8_t const& b06, uint8_t const& b07, + uint8_t const& b08, uint8_t const& b09, uint8_t const& b10, uint8_t const& b11, + uint8_t const& b12, uint8_t const& b13, uint8_t const& b14, uint8_t const& b15, + int const& c00, int const& c01, int const& c02, int const& c03, + int const& c04, int const& c05, int const& c06, int const& c07, + int const& c08, int const& c09, int const& c10, int const& c11, + int const& c12, int const& c13, int const& c14, int const& c15) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + intx4_t C0, C1, C2, C3; + intx4_t D0, D1, D2, D3; + C0.x = c00; C0.y = c01; C0.z = c02; C0.w = c03; + C1.x = c04; C1.y = c05; C1.z = c06; C1.w = c07; + C2.x = c08; C2.y = c09; C2.z = c10; C2.w = c11; + C3.x = c12; C3.y = c13; C3.z = c14; C3.w = c15; + + cutlass::Array a; + a[0] = a00; a[1] = a01; a[2] = a02; a[3] = a03; + a[4] = a04; a[5] = a05; a[6] = a06; a[7] = a07; + a[8] = a08; a[9] = a09; a[10] = a10; a[11] = a11; + a[12] = a12; a[13] = a13; a[14] = a14; a[15] = a15; + + cutlass::Array b; + b[0] = b00; b[1] = b01; b[2] = b02; b[3] = b03; + b[4] = b04; b[5] = b05; b[6] = b06; b[7] = b07; + b[8] = b08; b[9] = b09; b[10] = b10; b[11] = b11; + b[12] = b12; b[13] = b13; b[14] = b14; b[15] = b15; + long A0, A1, B0, B1; + A0 = *(reinterpret_cast(&a)); + B0 = *(reinterpret_cast(&b)); + A1 = *(reinterpret_cast(&a) + 1); + B1 = *(reinterpret_cast(&b) + 1); + #ifndef HG_ROCM + D0 = __builtin_amdgcn_mmac_i32_16x16x32u8(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_i32_16x16x32u8(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_i32_16x16x32u8(A1, B0, C2); + D3 = __builtin_amdgcn_mmac_i32_16x16x32u8(A1, B1, C3); + #endif + + d00 = D0.x; d01 = D0.y; d02 = D0.z; d03 = D0.w; + d04 = D1.x; d05 = D1.y; d06 = D1.z; d07 = D1.w; + d08 = D2.x; d09 = D2.y; d10 = D2.z; d11 = D2.w; + d12 = D3.x; d13 = D3.y; d14 = D3.z; d15 = D3.w; + #endif + } + }; +//////////////////////////////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////////////////////////////////////////// +// used for flash attention // +///////////////////////////////////////////////////////////////////////////////////////////////// + + struct GFX928_16x16x16_F32F16F16F32_NT_FOR_GEMM1 + { + using DRegisters = float[4]; + using ARegisters = half_t[4]; + using BRegisters = half_t[4]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A,B; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + #ifdef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x16_f16(A,B,c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16f16(A,B,c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + + __fp16x4_t A,B; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B.x = b0; B.y = b1; B.z = b2; B.w = b3; + + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A,B,c,false,false); + + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + + #endif + } + + }; + + struct GFX928_16x16x16_F32BF16BF16F32_NT_FOR_GEMM1 + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[4]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; + a[1] = a1; + a[2] = a2; + a[3] = a3; + cutlass::Array b; + b[0] = b0; + b[1] = b1; + b[2] = b2; + b[3] = b3; + + // asm volatile("v_mmac_f32_16x16x16_bf16 %0, %1, %2, %3\n\t" + // : "+v"(d) + // : "v"(a), "v"(b), "v"(c)); + __bf16x4_t A,B; + A = *(reinterpret_cast<__bf16x4_t *>(&a)); + B = *(reinterpret_cast<__bf16x4_t *>(&b)); + + #ifdef HG_ROCM + d = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B, c); + #else + d = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B, c); + #endif + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + // d0 = d.x; + // d1 = d.y; + // d2 = d.z; + // d3 = d.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a; + a[0] = a0; + a[1] = a1; + a[2] = a2; + a[3] = a3; + cutlass::Array b; + b[0] = b0; + b[1] = b1; + b[2] = b2; + b[3] = b3; + + __bf16x4_t A,B; + A = *(reinterpret_cast<__bf16x4_t *>(&a)); + B = *(reinterpret_cast<__bf16x4_t *>(&b)); + + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B, c,false,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + + #endif + } + + }; + + +////////////////////////////v_mmac_16x32x16 concatenate by 2x16x16x16///////////////////////////////////////////// + struct GFX928_16x32x16_F32F16F16F32_NT + { + using DRegisters = float[8]; + using ARegisters = half_t[4]; + using BRegisters = half_t[8]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + // C0.x = 0; C0.y = 0; C0.z = 0; C0.w = 0; + // C1.x = 0; C1.y = 0; C1.z = 0; C1.w = 0; + + __fp16x4_t A,B0,B1; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B1, C1); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B1, C1); + #endif + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + // C0.x = 0; C0.y = 0; C0.z = 0; C0.w = 0; + // C1.x = 0; C1.y = 0; C1.z = 0; C1.w = 0; + + __fp16x4_t A,B0,B1; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B1, C1,false,false); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + #endif + } + }; + + struct GFX928_16x32x16_F32BF16BF16F32_NT + { + using DRegisters = float[8]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[8]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + + __bf16x4_t A,B0,B1; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B1, C1); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B1, C1); + #endif + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + + __bf16x4_t A,B0,B1; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B1, C1,false,false); + + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + #endif + } + }; + + struct GFX928_16x64x16_FP8_F32F16F16F32_NT + { + using DRegisters = float[16]; + using ARegisters = half_t[4]; + using BRegisters = half_t[16]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float& d8, float& d9, float& d10, float& d11, + float& d12, float& d13, float& d14, float& d15, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + half_t const& b8, half_t const& b9,half_t const& b10, half_t const& b11, + half_t const& b12, half_t const& b13,half_t const& b14, half_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f C0,C1, C2, C3; + v4f D0,D1, D2, D3; + + C0.x = c0; C0.y = c2; C0.z = c4; C0.w = c6; + C1.x = c1; C1.y = c3; C1.z = c5; C1.w = c7; + + C2.x = c8; C2.y = c10; C2.z = c12; C2.w = c14; + C3.x = c9; C3.y = c11; C3.z = c13; C3.w = c15; + + __fp16x4_t A, B0, B1, B2, B3; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b2; B0.z = b4; B0.w = b6; + B1.x = b1; B1.y = b3; B1.z = b5; B1.w = b7; + B2.x = b8; B2.y = b10; B2.z = b12; B2.w = b14; + B3.x = b9; B3.y = b11; B3.z = b13; B3.w = b15; + + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B3, C3); + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f C0,C1, C2, C3; + v4f D0,D1, D2, D3; + + C0.x = c0; C0.y = c2; C0.z = c4; C0.w = c6; + C1.x = c1; C1.y = c3; C1.z = c5; C1.w = c7; + + C2.x = c8; C2.y = c10; C2.z = c12; C2.w = c14; + C3.x = c9; C3.y = c11; C3.z = c13; C3.w = c15; + + __fp16x4_t A, B0, B1, B2, B3; + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b2; B0.z = b4; B0.w = b6; + B1.x = b1; B1.y = b3; B1.z = b5; B1.w = b7; + B2.x = b8; B2.y = b10; B2.z = b12; B2.w = b14; + B3.x = b9; B3.y = b11; B3.z = b13; B3.w = b15; + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B3, C3,false,false); + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + + #endif + } + }; + + struct GFX928_16x64x16_FP8_F32BF16BF16F32_NT + { + using DRegisters = float[16]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[16]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float& d8, float& d9, float& d10, float& d11, + float& d12, float& d13, float& d14, float& d15, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + bfloat16_t const& b8, bfloat16_t const& b9,bfloat16_t const& b10, bfloat16_t const& b11, + bfloat16_t const& b12, bfloat16_t const& b13,bfloat16_t const& b14, bfloat16_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15 + ) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + v4f C0,C1, C2, C3; + v4f D0,D1, D2, D3; + + C0.x = c0; C0.y = c2; C0.z = c4; C0.w = c6; + C1.x = c1; C1.y = c3; C1.z = c5; C1.w = c7; + + C2.x = c8; C2.y = c10; C2.z = c12; C2.w = c14; + C3.x = c9; C3.y = c11; C3.z = c13; C3.w = c15; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + // array_a0[0] = 1.0; array_a0[1] = 1.0; array_a0[2] = 1.0; array_a0[3] = 1.0; + + array_b0[0] = b0; array_b0[1] = b2; array_b0[2] = b4; array_b0[3] = b6; + array_b1[0] = b1; array_b1[1] = b3; array_b1[2] = b5; array_b1[3] = b7; + + array_b2[0] = b8; array_b2[1] = b10; array_b2[2] = b12; array_b2[3] = b14; + array_b3[0] = b9; array_b3[1] = b11; array_b3[2] = b13; array_b3[3] = b15; + + __bf16x4_t A, B0, B1, B2, B3; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + + #ifdef HG_ROCM + // D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B0, C0); + // D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B1, C1); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B3, C3); + #endif + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1, C2, C3; + v4f D0,D1, D2, D3; + + C0.x = c0; C0.y = c2; C0.z = c4; C0.w = c6; + C1.x = c1; C1.y = c3; C1.z = c5; C1.w = c7; + + C2.x = c8; C2.y = c10; C2.z = c12; C2.w = c14; + C3.x = c9; C3.y = c11; C3.z = c13; C3.w = c15; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + // array_a0[0] = 1.0; array_a0[1] = 1.0; array_a0[2] = 1.0; array_a0[3] = 1.0; + + array_b0[0] = b0; array_b0[1] = b2; array_b0[2] = b4; array_b0[3] = b6; + array_b1[0] = b1; array_b1[1] = b3; array_b1[2] = b5; array_b1[3] = b7; + + array_b2[0] = b8; array_b2[1] = b10; array_b2[2] = b12; array_b2[3] = b14; + array_b3[0] = b9; array_b3[1] = b11; array_b3[2] = b13; array_b3[3] = b15; + + __bf16x4_t A, B0, B1, B2, B3; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B3, C3,false,false); + + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + #endif + + } + }; + + struct GFX928_16x32x16_F32F16F16F32_NT_FOR_GEMM1 : GFX928_16x32x16_F32F16F16F32_NT {}; + struct GFX928_16x32x16_F32BF16BF16F32_NT_FOR_GEMM1 : GFX928_16x32x16_F32BF16BF16F32_NT {}; +////////////////////////////v_mmac_16x64x16 concatenate by 4x16x16x16///////////////////////////////////////////// + struct GFX928_16x64x16_F32F16F16F32_NT + { + using DRegisters = float[16]; + using ARegisters = half_t[4]; + using BRegisters = half_t[16]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + half_t const& b8, half_t const& b9,half_t const& b10, half_t const& b11, + half_t const& b12, half_t const& b13,half_t const& b14, half_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + __fp16x4_t A,B0,B1,B2,B3; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + B2.x = b8; B2.y = b9; B2.z = b10; B2.w = b11; + B3.x = b12; B3.y = b13; B3.z = b14; B3.w = b15; + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A, B3, C3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16f16(A, B3, C3); + #endif + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + __fp16x4_t A,B0,B1,B2,B3; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + B2.x = b8; B2.y = b9; B2.z = b10; B2.w = b11; + B3.x = b12; B3.y = b13; B3.z = b14; B3.w = b15; + + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B3, C3,false,false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + + struct GFX928_16x64x16_F32BF16BF16F32_NT + { + using DRegisters = float[16]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[16]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + bfloat16_t const& b8, bfloat16_t const& b9,bfloat16_t const& b10, bfloat16_t const& b11, + bfloat16_t const& b12, bfloat16_t const& b13,bfloat16_t const& b14, bfloat16_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + array_b2[0] = b8; array_b2[1] = b9; array_b2[2] = b10; array_b2[3] = b11; + array_b3[0] = b12; array_b3[1] = b13; array_b3[2] = b14; array_b3[3] = b15; + + __bf16x4_t A,B0,B1,B2,B3; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A, B3, C3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A, B3, C3); + #endif + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + array_b2[0] = b8; array_b2[1] = b9; array_b2[2] = b10; array_b2[3] = b11; + array_b3[0] = b12; array_b3[1] = b13; array_b3[2] = b14; array_b3[3] = b15; + + __bf16x4_t A,B0,B1,B2,B3; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B3, C3,false,false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + +////////////////////////////v_mmac_16x64x32 concatenate by 8x16x16x16///////////////////////////////////////////// + struct GFX928_16x64x32_F32F16F16F32_NT + { + using DRegisters = float[16]; + using ARegisters = half_t[8]; + using BRegisters = half_t[32]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5,half_t const& a6, half_t const& a7, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + half_t const& b8, half_t const& b9,half_t const& b10, half_t const& b11, + half_t const& b12, half_t const& b13,half_t const& b14, half_t const& b15, + half_t const& b16, half_t const& b17,half_t const& b18, half_t const& b19, + half_t const& b20, half_t const& b21,half_t const& b22, half_t const& b23, + half_t const& b24, half_t const& b25,half_t const& b26, half_t const& b27, + half_t const& b28, half_t const& b29,half_t const& b30, half_t const& b31, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + __fp16x4_t A0,A1,B0,B1,B2,B3,B4,B5,B6,B7; + + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B4.x = b4; B4.y = b5; B4.z = b6; B4.w = b7; + B1.x = b8; B1.y = b9; B1.z = b10; B1.w = b11; + B5.x = b12; B5.y = b13; B5.z = b14; B5.w = b15; + B2.x = b16; B2.y = b17; B2.z = b18; B2.w = b19; + B6.x = b20; B6.y = b21; B6.z = b22; B6.w = b23; + B3.x = b24; B3.y = b25; B3.z = b26; B3.w = b27; + B7.x = b28; B7.y = b29; B7.z = b30; B7.w = b31; + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A0, B3, C3); + + D0 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A1, B4, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A1, B5, D1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A1, B6, D2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_f16(A1, B7, D3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16f16(A0, B3, C3); + + D0 = __builtin_amdgcn_mmac_f32_16x16x16f16(A1, B4, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16f16(A1, B5, D1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16f16(A1, B6, D2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16f16(A1, B7, D3); + #endif + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + __fp16x4_t A0,A1,B0,B1,B2,B3,B4,B5,B6,B7; + + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B4.x = b4; B4.y = b5; B4.z = b6; B4.w = b7; + B1.x = b8; B1.y = b9; B1.z = b10; B1.w = b11; + B5.x = b12; B5.y = b13; B5.z = b14; B5.w = b15; + B2.x = b16; B2.y = b17; B2.z = b18; B2.w = b19; + B6.x = b20; B6.y = b21; B6.z = b22; B6.w = b23; + B3.x = b24; B3.y = b25; B3.z = b26; B3.w = b27; + B7.x = b28; B7.y = b29; B7.z = b30; B7.w = b31; + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0, B3, C3,false,false); + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1, B4, D0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1, B5, D1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1, B6, D2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1, B7, D3,false,false); + + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + + struct GFX928_16x64x32_F32BF16BF16F32_NT + { + using DRegisters = float[16]; + using ARegisters = bfloat16_t[8]; + using BRegisters = bfloat16_t[32]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& a4, bfloat16_t const& a5,bfloat16_t const& a6, bfloat16_t const& a7, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + bfloat16_t const& b8, bfloat16_t const& b9,bfloat16_t const& b10, bfloat16_t const& b11, + bfloat16_t const& b12, bfloat16_t const& b13,bfloat16_t const& b14, bfloat16_t const& b15, + bfloat16_t const& b16, bfloat16_t const& b17,bfloat16_t const& b18, bfloat16_t const& b19, + bfloat16_t const& b20, bfloat16_t const& b21,bfloat16_t const& b22, bfloat16_t const& b23, + bfloat16_t const& b24, bfloat16_t const& b25,bfloat16_t const& b26, bfloat16_t const& b27, + bfloat16_t const& b28, bfloat16_t const& b29,bfloat16_t const& b30, bfloat16_t const& b31, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + + #if (defined(__gfx928__) || defined(__gfx936__) ) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + cutlass::Array array_a0, array_a1; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + cutlass::Array array_b4, array_b5, array_b6, array_b7; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + array_a1[0] = a4; array_a1[1] = a5; array_a1[2] = a6; array_a1[3] = a7; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b4[0] = b4; array_b4[1] = b5; array_b4[2] = b6; array_b4[3] = b7; + array_b1[0] = b8; array_b1[1] = b9; array_b1[2] = b10; array_b1[3] = b11; + array_b5[0] = b12; array_b5[1] = b13; array_b5[2] = b14; array_b5[3] = b15; + array_b2[0] = b16; array_b2[1] = b17; array_b2[2] = b18; array_b2[3] = b19; + array_b6[0] = b20; array_b6[1] = b21; array_b6[2] = b22; array_b6[3] = b23; + array_b3[0] = b24; array_b3[1] = b25; array_b3[2] = b26; array_b3[3] = b27; + array_b7[0] = b28; array_b7[1] = b29; array_b7[2] = b30; array_b7[3] = b31; + + __bf16x4_t A0,A1,B0,B1,B2,B3,B4,B5,B6,B7; + A0 = *reinterpret_cast<__bf16x4_t*>(&array_a0); + A1 = *reinterpret_cast<__bf16x4_t*>(&array_a1); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + B4 = *reinterpret_cast<__bf16x4_t*>(&array_b4); + B5 = *reinterpret_cast<__bf16x4_t*>(&array_b5); + B6 = *reinterpret_cast<__bf16x4_t*>(&array_b6); + B7 = *reinterpret_cast<__bf16x4_t*>(&array_b7); + + #ifdef HG_ROCM + D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A0, B3, C3); + + D0 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A1, B4, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A1, B5, D1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A1, B6, D2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16_bf16(A1, B7, D3); + #else + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B0, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B1, C1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B2, C2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A0, B3, C3); + + D0 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B4, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B5, D1); + D2 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B6, D2); + D3 = __builtin_amdgcn_mmac_f32_16x16x16bf16(A1, B7, D3); + #endif + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + #elif defined(__gfx938__) && defined(DCU_ASM) + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + cutlass::Array array_a0, array_a1; + cutlass::Array array_b0, array_b1, array_b2, array_b3; + cutlass::Array array_b4, array_b5, array_b6, array_b7; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + array_a1[0] = a4; array_a1[1] = a5; array_a1[2] = a6; array_a1[3] = a7; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b4[0] = b4; array_b4[1] = b5; array_b4[2] = b6; array_b4[3] = b7; + array_b1[0] = b8; array_b1[1] = b9; array_b1[2] = b10; array_b1[3] = b11; + array_b5[0] = b12; array_b5[1] = b13; array_b5[2] = b14; array_b5[3] = b15; + array_b2[0] = b16; array_b2[1] = b17; array_b2[2] = b18; array_b2[3] = b19; + array_b6[0] = b20; array_b6[1] = b21; array_b6[2] = b22; array_b6[3] = b23; + array_b3[0] = b24; array_b3[1] = b25; array_b3[2] = b26; array_b3[3] = b27; + array_b7[0] = b28; array_b7[1] = b29; array_b7[2] = b30; array_b7[3] = b31; + + __bf16x4_t A0,A1,B0,B1,B2,B3,B4,B5,B6,B7; + A0 = *reinterpret_cast<__bf16x4_t*>(&array_a0); + A1 = *reinterpret_cast<__bf16x4_t*>(&array_a1); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + B2 = *reinterpret_cast<__bf16x4_t*>(&array_b2); + B3 = *reinterpret_cast<__bf16x4_t*>(&array_b3); + B4 = *reinterpret_cast<__bf16x4_t*>(&array_b4); + B5 = *reinterpret_cast<__bf16x4_t*>(&array_b5); + B6 = *reinterpret_cast<__bf16x4_t*>(&array_b6); + B7 = *reinterpret_cast<__bf16x4_t*>(&array_b7); + + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B0, C0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B1, C1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B2, C2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0, B3, C3,false,false); + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B4, D0,false,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B5, D1,false,false); + D2 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B6, D2,false,false); + D3 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1, B7, D3,false,false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + + struct GFX928_16x32x16_F32F32F32F32_NT { + using DRegisters = float[8]; //* n 方向拼两次指令, 因此 D 翻倍 + using ARegisters = float[4]; //* k 方向拼两次指令 + using BRegisters = float[8]; //* k 方向和 n 方向拼两次指令 + using CRegisters = float[8]; //* n 方向拼两次 + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float const& a0, float const& a1, float const& a2, float const& a3, + float const& b0, float const& b1, float const& b2, float const& b3, + float const& b4, float const& b5, float const& b6, float const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + #if (defined(__gfx928__) || defined(__gfx936__)) && defined(DCU_ASM) + // 0 n 方向第一条 mmac 结果, 1 n 方向第一条 mmac 结果 + v4f C0, C1; + v4f D0, D1; + + C0.x = c0; C0.y = c2; C0.z = c4; C0.w = c6; + C1.x = c1; C1.y = c3; C1.z = c5; C1.w = c7; + + v2f A0, A1; + A0.x = a0; A0.y = a1; A1.x = a2; A1.y = a3; + // 01: 0 -> n 方向的第 1 条 mmac, 1 -> k 方向的第 2 条 mmac + v2f B00, B01, B10, B11; + + //nk + B00.x = b0; B00.y = b1; + B01.x = b2; B01.y = b3; + B10.x = b4; B10.y = b5; + B11.x = b6; B11.y = b7; + + + #ifdef HG_ROCM + //n k nk n + D0 = __builtin_amdgcn_mmac_f32_16x16x8_f32(A0, B00, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x8_f32(A0, B10, C1); + D0 = __builtin_amdgcn_mmac_f32_16x16x8_f32(A1, B01, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x8_f32(A1, B11, D1); + #else + //n k nk n + D0 = __builtin_amdgcn_mmac_f32_16x16x8f32(A0, B00, C0); + D1 = __builtin_amdgcn_mmac_f32_16x16x8f32(A0, B10, C1); + D0 = __builtin_amdgcn_mmac_f32_16x16x8f32(A1, B01, D0); + D1 = __builtin_amdgcn_mmac_f32_16x16x8f32(A1, B11, D1); + #endif + + d0 = D0.x; d2 = D0.y; d4 = D0.z; d6 = D0.w; + d1 = D1.x; d3 = D1.y; d5 = D1.z; d7 = D1.w; + + #endif + } + }; + + + struct GFX928_16x64x32_F32F16F16F32_NN : GFX928_16x64x32_F32F16F16F32_NT {}; + struct GFX928_16x64x32_F32BF16BF16F32_NN : GFX928_16x64x32_F32BF16BF16F32_NT {}; + struct GFX928_16x16x32_F32F16F16F32_NN : GFX928_16x16x32_F32F16F16F32_NT {}; + struct GFX928_16x16x32_F32BF16BF16F32_NN : GFX928_16x16x32_F32BF16BF16F32_NT {}; + struct GFX928_16x64x16_F32F16F16F32_NN : GFX928_16x64x16_F32F16F16F32_NT {}; + struct GFX928_16x64x16_F32BF16BF16F32_NN : GFX928_16x64x16_F32BF16BF16F32_NT {}; + struct GFX928_16x64x32_F32F16F16F32_NT_BLayout : GFX928_16x64x32_F32F16F16F32_NT {}; + struct GFX928_16x64x32_F32BF16BF16F32_NT_BLayout : GFX928_16x64x32_F32BF16BF16F32_NT {}; +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx938_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx938_hip.hpp new file mode 100644 index 000000000..b02a82c79 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx938_hip.hpp @@ -0,0 +1,1249 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + + +namespace cute +{ + ////////////////////////////v_mmac_16x16x32 concatenate by 2x16x16x16///////////////////////////////////////////// + struct GFX938_16x16x32_F32F16F16F32_NT_LIT + { + using DRegisters = float[4]; + using ARegisters = half_t[8]; + using BRegisters = half_t[8]; + using CRegisters = float[4]; + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& a4, half_t const& a5,half_t const& a6, half_t const& a7, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; c.y = c1; c.z = c2; c.w = c3; + + __fp16x4_t A0, A1, B0, B1; + A0.x = a0; A0.y = a1; A0.z = a2; A0.w = a3; + A1.x = a4; A1.y = a5; A1.z = a6; A1.w = a7; + + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A0,B0,c,true,false); + d = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A1,B1,d,true,false); + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #endif + } + + }; + + struct GFX938_16x16x32_F32BF16BF16F32_NT_LIT + { + using DRegisters = float[4]; + using ARegisters = bfloat16_t[8]; + using BRegisters = bfloat16_t[8]; + using CRegisters = float[4]; + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& a4, bfloat16_t const& a5,bfloat16_t const& a6, bfloat16_t const& a7, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if defined(__gfx938__) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; c.y = c1; c.z = c2; c.w = c3; + + cutlass::Array array_a0, array_a1, array_b0, array_b1; + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + array_a1[0] = a4; array_a1[1] = a5; array_a1[2] = a6; array_a1[3] = a7; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + + __bf16x4_t A0, A1, B0, B1; + A0 = *reinterpret_cast<__bf16x4_t*>(&array_a0); + A1 = *reinterpret_cast<__bf16x4_t*>(&array_a1); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0,B0,c,true,false); + d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1,B1,d,true,false); + + + // debug + // v4f d_; + // d_ = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A0,B0,c,true,false); + // d = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A1,B1,d_,true,false); + // if (blockIdx.x == 0) { + // printf("cute 16x16x32 tid:%d A0:%10.4f %10.4f %10.4f %10.4f A1:%10.4f %10.4f %10.4f %10.4f " + // "B0:%10.4f %10.4f %10.4f %10.4f B1:%10.4f %10.4f %10.4f %10.4f " + // "C0:%10.4f %10.4f %10.4f %10.4f " + // "D0:%10.4f %10.4f %10.4f %10.4f D1:%10.4f %10.4f %10.4f %10.4f\n", + // threadIdx.x, float(a0), float(a1), float(a2), float(a3), float(a4), float(a5), float(a6), float(a7), + // float(b0), float(b1), float(b2), float(b3), float(b4), float(b5), float(b6), float(b7), + // c0, c1, c2, c3, d_.x, d_.y, d_.z, d_.w, d.x, d.y, d.z, d.w + // ); + // } + + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #endif + } + + }; + + ////////////////////////////v_mmac_16x32x16 concatenate by 2x16x16x16///////////////////////////////////////////// + struct GFX938_16x32x16_F32F16F16F32_NT_LIT + { + using DRegisters = float[8]; + using ARegisters = half_t[4]; + using BRegisters = half_t[8]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + half_t const& a0, half_t const& a1,half_t const& a2, half_t const& a3, + half_t const& b0, half_t const& b1,half_t const& b2, half_t const& b3, + half_t const& b4, half_t const& b5,half_t const& b6, half_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + + #if defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + // C0.x = 0; C0.y = 0; C0.z = 0; C0.w = 0; + // C1.x = 0; C1.y = 0; C1.z = 0; C1.w = 0; + + __fp16x4_t A,B0,B1; + + A.x = a0; A.y = a1; A.z = a2; A.w = a3; + B0.x = b0; B0.y = b1; B0.z = b2; B0.w = b3; + B1.x = b4; B1.y = b5; B1.z = b6; B1.w = b7; + + D0 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B0, C0,true,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_f16_lit_lts(A, B1, C1,true,false); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + #endif + } + }; + + struct GFX938_16x32x16_F32BF16BF16F32_NT_LIT + { + using DRegisters = float[8]; + using ARegisters = bfloat16_t[4]; + using BRegisters = bfloat16_t[8]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + bfloat16_t const& a0, bfloat16_t const& a1,bfloat16_t const& a2, bfloat16_t const& a3, + bfloat16_t const& b0, bfloat16_t const& b1,bfloat16_t const& b2, bfloat16_t const& b3, + bfloat16_t const& b4, bfloat16_t const& b5,bfloat16_t const& b6, bfloat16_t const& b7, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + + #if defined(__gfx938__) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array array_a0; + cutlass::Array array_b0, array_b1; + + array_a0[0] = a0; array_a0[1] = a1; array_a0[2] = a2; array_a0[3] = a3; + + array_b0[0] = b0; array_b0[1] = b1; array_b0[2] = b2; array_b0[3] = b3; + array_b1[0] = b4; array_b1[1] = b5; array_b1[2] = b6; array_b1[3] = b7; + + __bf16x4_t A,B0,B1; + A = *reinterpret_cast<__bf16x4_t*>(&array_a0); + B0 = *reinterpret_cast<__bf16x4_t*>(&array_b0); + B1 = *reinterpret_cast<__bf16x4_t*>(&array_b1); + + D0 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B0, C0,true,false); + D1 = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(A, B1, C1,true,false); + + // debug + // if (blockIdx.x == 0) { + // printf("cute 16x32x16 tid:%d A:%10.4f %10.4f %10.4f %10.4f B0:%10.4f %10.4f %10.4f %10.4f " + // "B1:%10.4f %10.4f %10.4f %10.4f C0:%10.4f %10.4f %10.4f %10.4f C1:%10.4f %10.4f %10.4f %10.4f" + // "D0:%10.4f %10.4f %10.4f %10.4f D1:%10.4f %10.4f %10.4f %10.4f\n", + // threadIdx.x, float(a0), float(a1), float(a2), float(a3), float(b0), float(b1), float(b2), float(b3), + // float(b4), float(b5), float(b6), float(b7), c0, c1, c2, c3, c4, c5, c6, c7, + // D0.x, D0.y, D0.z, D0.w, D1.x, D1.y, D1.z, D1.w + // ); + // } + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + #endif + } + }; + + + //add 16x64x16 + struct GFX938_16x64x16_F32F8F8F32E4M3E4M3_NT + { + using DRegisters = float[16]; + using ARegisters = float_e4m3_t[4]; + using BRegisters = float_e4m3_t[16]; + using CRegisters = float[16]; + }; + + + //add 16x16x64 + struct GFX938_16x16x64_F32F8F8F32E4M3E4M3_NT + { + using DRegisters = float[4]; + using ARegisters = float_e4m3_t[16]; + using BRegisters = float_e4m3_t[16]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& a8, float_e4m3_t const& a9,float_e4m3_t const& a10, float_e4m3_t const& a11, + float_e4m3_t const& a12, float_e4m3_t const& a13,float_e4m3_t const& a14, float_e4m3_t const& a15, + float_e4m3_t const& b0, float_e4m3_t const& b1,float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5,float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9,float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13,float_e4m3_t const& b14, float_e4m3_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0; + a_0[0] = a0; a_0[1] = a1; a_0[2] = a2; a_0[3] = a3; + a_0[4] = a4; a_0[5] = a5; a_0[6] = a6; a_0[7] = a7; + + cutlass::Array a_1; + a_1[0] = a8; a_1[1] = a9; a_1[2] = a10; a_1[3] = a11; + a_1[4] = a12; a_1[5] = a13; a_1[6] = a14; a_1[7] = a15; + + cutlass::Array b_0; + b_0[0] = b0;b_0[1] = b1;b_0[2] = b2;b_0[3] = b3; + b_0[4] = b4;b_0[5] = b5;b_0[6] = b6;b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8;b_1[1] = b9;b_1[2] = b10;b_1[3] = b11; + b_1[4] = b12;b_1[5] = b13;b_1[6] = b14;b_1[7] = b15; + + intx2_t A, B,A1,B1; + A = *(reinterpret_cast(&a_0)); + B = *(reinterpret_cast(&b_0)); + A1 = *(reinterpret_cast(&a_1)); + B1 = *(reinterpret_cast(&b_1)); + + // #ifndef HG_ROCM + d = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B, c, false, false); + d = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A1, B1, d, false, false); + // if(thread0()) { + // printf("A0: %f %f %f %f %f %f %f %f\n" + // "A1: %f %f %f %f %f %f %f %f\n" + // "B0: %f %f %f %f %f %f %f %f \n" + // "B1: %f %f %f %f %f %f %f %f \n" + // "C0: %f %f %f %f \n" + // "D1: %f %f %f %f \n", + // float(a0), float(a1), float(a2), float(a3), + // float(a4), float(a5), float(a6), float(a7), + // float(a8), float(a9), float(a10), float(a11), + // float(a12), float(a13), float(a14), float(a15), + // float(b0), float(b1), float(b2), float(b3), + // float(b4), float(b5), float(b6), float(b7), + // float(b8), float(b9), float(b10), float(b11), + // float(b12), float(b13), float(b14), float(b15), + // c.x, c.y, c.z, c.w, d.x, d.y, d.z, d.w + // ); + // } + // #endif + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #endif + } + + }; + + + + + //add 16x16x64 + struct GFX938_16x16x64_F32F8F8F32E4M3E4M3_NT_LIT + { + using DRegisters = float[4]; + using ARegisters = float_e4m3_t[16]; + using BRegisters = float_e4m3_t[16]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& a8, float_e4m3_t const& a9,float_e4m3_t const& a10, float_e4m3_t const& a11, + float_e4m3_t const& a12, float_e4m3_t const& a13,float_e4m3_t const& a14, float_e4m3_t const& a15, + float_e4m3_t const& b0, float_e4m3_t const& b1,float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5,float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9,float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13,float_e4m3_t const& b14, float_e4m3_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + v4f c; + v4f d; + c.x = c0; + c.y = c1; + c.z = c2; + c.w = c3; + cutlass::Array a_0; + a_0[0] = a0; a_0[1] = a1; a_0[2] = a2; a_0[3] = a3; + a_0[4] = a4; a_0[5] = a5; a_0[6] = a6; a_0[7] = a7; + + cutlass::Array a_1; + a_1[0] = a8; a_1[1] = a9; a_1[2] = a10; a_1[3] = a11; + a_1[4] = a12; a_1[5] = a13; a_1[6] = a14; a_1[7] = a15; + + cutlass::Array b_0; + b_0[0] = b0;b_0[1] = b1;b_0[2] = b2;b_0[3] = b3; + b_0[4] = b4;b_0[5] = b5;b_0[6] = b6;b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8;b_1[1] = b9;b_1[2] = b10;b_1[3] = b11; + b_1[4] = b12;b_1[5] = b13;b_1[6] = b14;b_1[7] = b15; + + intx2_t A, B,A1,B1; + A = *(reinterpret_cast(&a_0)); + B = *(reinterpret_cast(&b_0)); + A1 = *(reinterpret_cast(&a_1)); + B1 = *(reinterpret_cast(&b_1)); + + // #ifndef HG_ROCM + d = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B, c, true, false); + d = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A1, B1, d, true, false); + // if(thread0()) { + // printf("A0: %f %f %f %f %f %f %f %f\n" + // "A1: %f %f %f %f %f %f %f %f\n" + // "B0: %f %f %f %f %f %f %f %f \n" + // "B1: %f %f %f %f %f %f %f %f \n" + // "C0: %f %f %f %f \n" + // "D1: %f %f %f %f \n", + // float(a0), float(a1), float(a2), float(a3), + // float(a4), float(a5), float(a6), float(a7), + // float(a8), float(a9), float(a10), float(a11), + // float(a12), float(a13), float(a14), float(a15), + // float(b0), float(b1), float(b2), float(b3), + // float(b4), float(b5), float(b6), float(b7), + // float(b8), float(b9), float(b10), float(b11), + // float(b12), float(b13), float(b14), float(b15), + // c.x, c.y, c.z, c.w, d.x, d.y, d.z, d.w + // ); + // } + // #endif + d0 = d.x; + d1 = d.y; + d2 = d.z; + d3 = d.w; + #endif + } + + }; + //add 16x32x32 + struct GFX938_16x32x32_F32F8F8F32E4M3E4M3_NT + { + using DRegisters = float[8]; + using ARegisters = float_e4m3_t[8]; + using BRegisters = float_e4m3_t[16]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& b0, float_e4m3_t const& b1,float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5,float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9,float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13,float_e4m3_t const& b14, float_e4m3_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b_0; + b_0[0] = b0;b_0[1] = b1;b_0[2] = b2;b_0[3] = b3; + b_0[4] = b4;b_0[5] = b5;b_0[6] = b6;b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8;b_1[1] = b9;b_1[2] = b10;b_1[3] = b11; + b_1[4] = b12;b_1[5] = b13;b_1[6] = b14;b_1[7] = b15; + + intx2_t A, B,B1; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b_0)); + B1 = *(reinterpret_cast(&b_1)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B, C0, false, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B1, C1, false, false); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + + #endif + } + + }; + +#if 1 + //add 16x32x32 + // struct GFX938_16x32x32_F32F8F8F32E4M3E4M3_NT_LIT + // { + // using DRegisters = float[8]; + // using ARegisters = intx2_t[1]; + // using BRegisters = intx2_t[2]; + // using CRegisters = float[8]; + + // // Register asm fma + // CUTE_HOST_DEVICE static void + // fma(float & d0, float & d1, float & d2, float & d3, + // float & d4, float & d5, float & d6, float & d7, + // intx2_t const& a0,intx2_t const& b0,intx2_t const& b1, + // float const& c0, float const& c1, float const& c2, float const& c3, + // float const& c4, float const& c5, float const& c6, float const& c7) + // { + // #if (defined(__gfx938__)) && defined(DCU_ASM) + + // v4f C0,C1; + // v4f D0,D1; + // C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + // C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + + // D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b0, C0, true, false); + // D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b1, C1, true, false); + + // d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + // d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + + // #endif + // } + + // }; + struct GFX938_16x32x32_F32F8F8F32E4M3E4M3_NT_LIT + { + using DRegisters = float[8]; + using ARegisters = float_e4m3_t[8]; + using BRegisters = intx2_t[2]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7,intx2_t const& b0,intx2_t const& b1, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + intx2_t A; + A = *(reinterpret_cast(&a)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, b0, C0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, b1, C1, true, false); + + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + + #endif + } + + }; +#else + //add 16x32x32 + struct GFX938_16x32x32_F32F8F8F32E4M3E4M3_NT_LIT + { + using DRegisters = float[8]; + using ARegisters = float_e4m3_t[8]; + using BRegisters = float_e4m3_t[16]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& b0, float_e4m3_t const& b1,float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5,float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9,float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13,float_e4m3_t const& b14, float_e4m3_t const& b15, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1; + v4f D0,D1; + C0.x = c0; C0.y = c1; C0.z = c2; C0.w = c3; + C1.x = c4; C1.y = c5; C1.z = c6; C1.w = c7; + + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + cutlass::Array b_0; + b_0[0] = b0;b_0[1] = b1;b_0[2] = b2;b_0[3] = b3; + b_0[4] = b4;b_0[5] = b5;b_0[6] = b6;b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8;b_1[1] = b9;b_1[2] = b10;b_1[3] = b11; + b_1[4] = b12;b_1[5] = b13;b_1[6] = b14;b_1[7] = b15; + + intx2_t A, B,B1; + A = *(reinterpret_cast(&a)); + B = *(reinterpret_cast(&b_0)); + B1 = *(reinterpret_cast(&b_1)); + // #ifndef HG_ROCM + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B, C0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A, B1, C1, true, false); + // #endif + // if(thread0()) { + // printf("A: %f %f %f %f %f %f %f %f\n" + // "B0: %f %f %f %f %f %f %f %f \n" + // "B1: %f %f %f %f %f %f %f %f \n" + // "C0: %f %f %f %f \n" + // "C1: %f %f %f %f \n" + // "D0: %f %f %f %f \n" + // "D1: %f %f %f %f \n", + // float(a0), float(a1), float(a2), float(a3), + // float(a4), float(a5), float(a6), float(a7), + // float(b0), float(b1), float(b2), float(b3), + // float(b4), float(b5), float(b6), float(b7), + // float(b8), float(b9), float(b10), float(b11), + // float(b12), float(b13), float(b14), float(b15), + // C0.x, C0.y, C0.z, C0.w, C1.x, C1.y, C1.z, C1.w, + // D0.x, D0.y, D0.z, D0.w, D1.x, D1.y, D1.z, D1.w + // ); + // } + d0 = D0.x; d1 = D0.y; d2 = D0.z; d3 = D0.w; + d4 = D1.x; d5 = D1.y; d6 = D1.z; d7 = D1.w; + + #endif + } + + }; +#endif + + struct GFX938_16x64x32_F32F8F8F32E4M3E4M3_NT + { + using DRegisters = float[16]; + using ARegisters = float_e4m3_t[8]; + using BRegisters = float_e4m3_t[32]; + // using ARegisters = intx2_t[1]; + // using BRegisters = intx2_t[4]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float_e4m3_t const& a0, float_e4m3_t const& a1, float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5, float_e4m3_t const& a6, float_e4m3_t const& a7, + + float_e4m3_t const& b0, float_e4m3_t const& b1, float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5, float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9, float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13, float_e4m3_t const& b14, float_e4m3_t const& b15, + float_e4m3_t const& b16, float_e4m3_t const& b17, float_e4m3_t const& b18, float_e4m3_t const& b19, + float_e4m3_t const& b20, float_e4m3_t const& b21, float_e4m3_t const& b22, float_e4m3_t const& b23, + float_e4m3_t const& b24, float_e4m3_t const& b25, float_e4m3_t const& b26, float_e4m3_t const& b27, + float_e4m3_t const& b28, float_e4m3_t const& b29, float_e4m3_t const& b30, float_e4m3_t const& b31, + // intx2_t const& a , + // intx2_t const& b0 , intx2_t const& b1 , intx2_t const& b2 , intx2_t const& b3, + + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0, C1, C2, C3; + v4f D0, D1, D2, D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + // Pack A (8) into cutlass::Array and then reinterpret as intx2_t for intrinsic + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + // // Pack B into four groups of 8 each + cutlass::Array b_0; + b_0[0] = b0; b_0[1] = b1; b_0[2] = b2; b_0[3] = b3; + b_0[4] = b4; b_0[5] = b5; b_0[6] = b6; b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8; b_1[1] = b9; b_1[2] = b10; b_1[3] = b11; + b_1[4] = b12; b_1[5] = b13; b_1[6] = b14; b_1[7] = b15; + + cutlass::Array b_2; + b_2[0] = b16; b_2[1] = b17; b_2[2] = b18; b_2[3] = b19; + b_2[4] = b20; b_2[5] = b21; b_2[6] = b22; b_2[7] = b23; + + cutlass::Array b_3; + b_3[0] = b24; b_3[1] = b25; b_3[2] = b26; b_3[3] = b27; + b_3[4] = b28; b_3[5] = b29; b_3[6] = b30; b_3[7] = b31; + + intx2_t A_pack = *(reinterpret_cast(&a)); + intx2_t B0_pack = *(reinterpret_cast(&b_0)); + intx2_t B1_pack = *(reinterpret_cast(&b_1)); + intx2_t B2_pack = *(reinterpret_cast(&b_2)); + intx2_t B3_pack = *(reinterpret_cast(&b_3)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_pack, B0_pack, C0, false, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_pack, B1_pack, C1, false, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_pack, B2_pack, C2, false, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_pack, B3_pack, C3, false, false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + + struct GFX938_16x64x32_F32BF8BF8F32E5M2E5M2_NT + { + using DRegisters = float[16]; + using ARegisters = float_e5m2_t[8]; + using BRegisters = float_e5m2_t[32]; + // using ARegisters = intx2_t[1]; + // using BRegisters = intx2_t[4]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float_e5m2_t const& a0, float_e5m2_t const& a1, float_e5m2_t const& a2, float_e5m2_t const& a3, + float_e5m2_t const& a4, float_e5m2_t const& a5, float_e5m2_t const& a6, float_e5m2_t const& a7, + + float_e5m2_t const& b0, float_e5m2_t const& b1, float_e5m2_t const& b2, float_e5m2_t const& b3, + float_e5m2_t const& b4, float_e5m2_t const& b5, float_e5m2_t const& b6, float_e5m2_t const& b7, + float_e5m2_t const& b8, float_e5m2_t const& b9, float_e5m2_t const& b10, float_e5m2_t const& b11, + float_e5m2_t const& b12, float_e5m2_t const& b13, float_e5m2_t const& b14, float_e5m2_t const& b15, + float_e5m2_t const& b16, float_e5m2_t const& b17, float_e5m2_t const& b18, float_e5m2_t const& b19, + float_e5m2_t const& b20, float_e5m2_t const& b21, float_e5m2_t const& b22, float_e5m2_t const& b23, + float_e5m2_t const& b24, float_e5m2_t const& b25, float_e5m2_t const& b26, float_e5m2_t const& b27, + float_e5m2_t const& b28, float_e5m2_t const& b29, float_e5m2_t const& b30, float_e5m2_t const& b31, + // intx2_t const& a , + // intx2_t const& b0 , intx2_t const& b1 , intx2_t const& b2 , intx2_t const& b3, + + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0, C1, C2, C3; + v4f D0, D1, D2, D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + // Pack A (8) into cutlass::Array and then reinterpret as intx2_t for intrinsic + cutlass::Array a; + a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3; + a[4] = a4; a[5] = a5; a[6] = a6; a[7] = a7; + + // // Pack B into four groups of 8 each + cutlass::Array b_0; + b_0[0] = b0; b_0[1] = b1; b_0[2] = b2; b_0[3] = b3; + b_0[4] = b4; b_0[5] = b5; b_0[6] = b6; b_0[7] = b7; + + cutlass::Array b_1; + b_1[0] = b8; b_1[1] = b9; b_1[2] = b10; b_1[3] = b11; + b_1[4] = b12; b_1[5] = b13; b_1[6] = b14; b_1[7] = b15; + + cutlass::Array b_2; + b_2[0] = b16; b_2[1] = b17; b_2[2] = b18; b_2[3] = b19; + b_2[4] = b20; b_2[5] = b21; b_2[6] = b22; b_2[7] = b23; + + cutlass::Array b_3; + b_3[0] = b24; b_3[1] = b25; b_3[2] = b26; b_3[3] = b27; + b_3[4] = b28; b_3[5] = b29; b_3[6] = b30; b_3[7] = b31; + + intx2_t A_pack = *(reinterpret_cast(&a)); + intx2_t B0_pack = *(reinterpret_cast(&b_0)); + intx2_t B1_pack = *(reinterpret_cast(&b_1)); + intx2_t B2_pack = *(reinterpret_cast(&b_2)); + intx2_t B3_pack = *(reinterpret_cast(&b_3)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_pack, B0_pack, C0, false, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_pack, B1_pack, C1, false, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_pack, B2_pack, C2, false, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_pack, B3_pack, C3, false, false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + #endif + } + }; + +struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT + { + using DRegisters = float[16]; + using ARegisters = float_e4m3_t[16]; + using BRegisters = float_e4m3_t[64]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + // A: 16 inputs (split into low 8 and high 8) + float_e4m3_t const& a0, float_e4m3_t const& a1, float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5, float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& a8, float_e4m3_t const& a9, float_e4m3_t const& a10, float_e4m3_t const& a11, + float_e4m3_t const& a12, float_e4m3_t const& a13, float_e4m3_t const& a14, float_e4m3_t const& a15, + + // B: 64 inputs (we'll group them into 8 groups of 8: b0..b7 (low32), b32..b39..b63 (high32)) + float_e4m3_t const& b0, float_e4m3_t const& b1, float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5, float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9, float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13, float_e4m3_t const& b14, float_e4m3_t const& b15, + float_e4m3_t const& b16, float_e4m3_t const& b17, float_e4m3_t const& b18, float_e4m3_t const& b19, + float_e4m3_t const& b20, float_e4m3_t const& b21, float_e4m3_t const& b22, float_e4m3_t const& b23, + float_e4m3_t const& b24, float_e4m3_t const& b25, float_e4m3_t const& b26, float_e4m3_t const& b27, + float_e4m3_t const& b28, float_e4m3_t const& b29, float_e4m3_t const& b30, float_e4m3_t const& b31, + + float_e4m3_t const& b32, float_e4m3_t const& b33, float_e4m3_t const& b34, float_e4m3_t const& b35, + float_e4m3_t const& b36, float_e4m3_t const& b37, float_e4m3_t const& b38, float_e4m3_t const& b39, + float_e4m3_t const& b40, float_e4m3_t const& b41, float_e4m3_t const& b42, float_e4m3_t const& b43, + float_e4m3_t const& b44, float_e4m3_t const& b45, float_e4m3_t const& b46, float_e4m3_t const& b47, + float_e4m3_t const& b48, float_e4m3_t const& b49, float_e4m3_t const& b50, float_e4m3_t const& b51, + float_e4m3_t const& b52, float_e4m3_t const& b53, float_e4m3_t const& b54, float_e4m3_t const& b55, + float_e4m3_t const& b56, float_e4m3_t const& b57, float_e4m3_t const& b58, float_e4m3_t const& b59, + float_e4m3_t const& b60, float_e4m3_t const& b61, float_e4m3_t const& b62, float_e4m3_t const& b63, + + // C (initial accumulators) + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7, + float const& c8, float const& c9, float const& c10, float const& c11, + float const& c12, float const& c13, float const& c14, float const& c15) + { + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0, C1, C2, C3; + v4f D0, D1, D2, D3; + + C0.x = c0; C0.y = c4; C0.z = c8; C0.w = c12; + C1.x = c1; C1.y = c5; C1.z = c9; C1.w = c13; + C2.x = c2; C2.y = c6; C2.z = c10; C2.w = c14; + C3.x = c3; C3.y = c7; C3.z = c11; C3.w = c15; + + cutlass::Array a_low; + a_low[0] = a0; a_low[1] = a1; a_low[2] = a2; a_low[3] = a3; + a_low[4] = a4; a_low[5] = a5; a_low[6] = a6; a_low[7] = a7; + + cutlass::Array a_high; + a_high[0] = a8; a_high[1] = a9; a_high[2] = a10; a_high[3] = a11; + a_high[4] = a12; a_high[5] = a13; a_high[6] = a14; a_high[7] = a15; + + intx2_t A_low_pack = *(reinterpret_cast(&a_low)); + intx2_t A_high_pack = *(reinterpret_cast(&a_high)); + + cutlass::Array b0_pack_arr; + b0_pack_arr[0] = b0; b0_pack_arr[1] = b1; b0_pack_arr[2] = b2; b0_pack_arr[3] = b3; + b0_pack_arr[4] = b4; b0_pack_arr[5] = b5; b0_pack_arr[6] = b6; b0_pack_arr[7] = b7; + + cutlass::Array b1_pack_arr; + b1_pack_arr[0] = b8; b1_pack_arr[1] = b9; b1_pack_arr[2] = b10; b1_pack_arr[3] = b11; + b1_pack_arr[4] = b12; b1_pack_arr[5] = b13; b1_pack_arr[6] = b14; b1_pack_arr[7] = b15; + + cutlass::Array b2_pack_arr; + b2_pack_arr[0] = b16; b2_pack_arr[1] = b17; b2_pack_arr[2] = b18; b2_pack_arr[3] = b19; + b2_pack_arr[4] = b20; b2_pack_arr[5] = b21; b2_pack_arr[6] = b22; b2_pack_arr[7] = b23; + + cutlass::Array b3_pack_arr; + b3_pack_arr[0] = b24; b3_pack_arr[1] = b25; b3_pack_arr[2] = b26; b3_pack_arr[3] = b27; + b3_pack_arr[4] = b28; b3_pack_arr[5] = b29; b3_pack_arr[6] = b30; b3_pack_arr[7] = b31; + + cutlass::Array b4_pack_arr; + b4_pack_arr[0] = b32; b4_pack_arr[1] = b33; b4_pack_arr[2] = b34; b4_pack_arr[3] = b35; + b4_pack_arr[4] = b36; b4_pack_arr[5] = b37; b4_pack_arr[6] = b38; b4_pack_arr[7] = b39; + + cutlass::Array b5_pack_arr; + b5_pack_arr[0] = b40; b5_pack_arr[1] = b41; b5_pack_arr[2] = b42; b5_pack_arr[3] = b43; + b5_pack_arr[4] = b44; b5_pack_arr[5] = b45; b5_pack_arr[6] = b46; b5_pack_arr[7] = b47; + + cutlass::Array b6_pack_arr; + b6_pack_arr[0] = b48; b6_pack_arr[1] = b49; b6_pack_arr[2] = b50; b6_pack_arr[3] = b51; + b6_pack_arr[4] = b52; b6_pack_arr[5] = b53; b6_pack_arr[6] = b54; b6_pack_arr[7] = b55; + + cutlass::Array b7_pack_arr; + b7_pack_arr[0] = b56; b7_pack_arr[1] = b57; b7_pack_arr[2] = b58; b7_pack_arr[3] = b59; + b7_pack_arr[4] = b60; b7_pack_arr[5] = b61; b7_pack_arr[6] = b62; b7_pack_arr[7] = b63; + + intx2_t B0_pack = *(reinterpret_cast(&b0_pack_arr)); + intx2_t B1_pack = *(reinterpret_cast(&b1_pack_arr)); + intx2_t B2_pack = *(reinterpret_cast(&b2_pack_arr)); + intx2_t B3_pack = *(reinterpret_cast(&b3_pack_arr)); + intx2_t B4_pack = *(reinterpret_cast(&b4_pack_arr)); + intx2_t B5_pack = *(reinterpret_cast(&b5_pack_arr)); + intx2_t B6_pack = *(reinterpret_cast(&b6_pack_arr)); + intx2_t B7_pack = *(reinterpret_cast(&b7_pack_arr)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_low_pack, B0_pack, C0, false, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_low_pack, B2_pack, C1, false, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_low_pack, B4_pack, C2, false, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_low_pack, B6_pack, C3, false, false); + + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_high_pack, B1_pack, D0, false, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_high_pack, B3_pack, D1, false, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_high_pack, B5_pack, D2, false, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_high_pack, B7_pack, D3, false, false); + + d0 = D0.x; d1 = D1.x; d2 = D2.x; d3 = D3.x; + d4 = D0.y; d5 = D1.y; d6 = D2.y; d7 = D3.y; + d8 = D0.z; d9 = D1.z; d10 = D2.z; d11 = D3.z; + d12 = D0.w; d13 = D1.w; d14 = D2.w; d15 = D3.w; + + + #endif + } + }; + + +#if 1 + + // struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_LIT + // { + // using DRegisters = float[16]; + // using ARegisters = intx2_t[2]; + // using BRegisters = intx2_t[8]; + // using CRegisters = float[16]; + + // // Register asm fma + // CUTE_HOST_DEVICE static void + // fma(float & d0, float & d1, float & d2, float & d3, + // float & d4, float & d5, float & d6, float & d7, + // float & d8, float & d9, float & d10, float & d11, + // float & d12, float & d13, float & d14, float & d15, + // intx2_t const& a0, intx2_t const& a1,intx2_t const& b0, intx2_t const& b1, + // intx2_t const& b2, intx2_t const& b3,intx2_t const& b4, intx2_t const& b5, + // intx2_t const& b6, intx2_t const& b7, + // float const& c0, float const&c1, float const& c2, float const& c3, + // float const&c4, float const& c5, float const& c6, float const& c7, + // float const&c8, float const& c9, float const& c10, float const& c11, + // float const&c12, float const& c13, float const& c14, float const& c15 ) + // { + + // #if (defined(__gfx938__)) && defined(DCU_ASM) + + // v4f C0,C1,C2,C3; + // v4f D0,D1,D2,D3; + + // C0.x=c0 ; C1.x=c1; C0.y=c2 ; C1.y= c3; + // C0.z=c4; C1.z=c5 ; C0.w=c6; C1.w=c7; + // C2.x=c8; C3.x=c9; C2.y=c10 ; C3.y=c11; + // C2.z=c12; C3.z=c13; C2.w=c14 ; C3.w=c15; + + // D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b0, C0, true, false); + // D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b2, C1, true, false); + // D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b4, C2, true, false); + // D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a0, b6, C3, true, false); + + + // D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a1, b1, D0, true, false); + // D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a1, b3, D1, true, false); + // D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a1, b5, D2, true, false); + // D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(a1, b7, D3, true, false); + + // d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + // d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + // d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + // d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + // #endif + + // } + // }; + + struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_LIT + { + using DRegisters = float[16]; + using ARegisters = float[4]; + using BRegisters = intx2_t[8]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float const& a0, float const& a1,float const& a2, float const& a3, + intx2_t const& b0, intx2_t const& b1,intx2_t const& b2, intx2_t const& b3, + intx2_t const& b4, intx2_t const& b5,intx2_t const& b6, intx2_t const& b7, + float const& c0, float const&c1, float const& c2, float const& c3, + float const&c4, float const& c5, float const& c6, float const& c7, + float const&c8, float const& c9, float const& c10, float const& c11, + float const&c12, float const& c13, float const& c14, float const& c15 ) + { + + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x=c0 ; C1.x=c1; C0.y=c2 ; C1.y= c3; + C0.z=c4; C1.z=c5 ; C0.w=c6; C1.w=c7; + C2.x=c8; C3.x=c9; C2.y=c10 ; C3.y=c11; + C2.z=c12; C3.z=c13; C2.w=c14 ; C3.w=c15; + + cutlass::Array A0,A1; + + A0[0] = a0; A0[1] = a1; + A1[0] = a2; A1[1] = a3; + + + intx2_t A_0, A_1; + A_0 = *(reinterpret_cast(&A0)); + A_1 = *(reinterpret_cast(&A1)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, b0, C0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, b2, C1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, b4, C2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, b6, C3, true, false); + + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, b1, D0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, b3, D1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, b5, D2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, b7, D3, true, false); + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + #endif + + } + }; + + struct GFX938_16x64x64_F32BF8BF8F32E5M2E5M2_NT_LIT + { + using DRegisters = float[16]; + using ARegisters = float[4]; + using BRegisters = intx2_t[8]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float const& a0, float const& a1,float const& a2, float const& a3, + intx2_t const& b0, intx2_t const& b1,intx2_t const& b2, intx2_t const& b3, + intx2_t const& b4, intx2_t const& b5,intx2_t const& b6, intx2_t const& b7, + float const& c0, float const&c1, float const& c2, float const& c3, + float const&c4, float const& c5, float const& c6, float const& c7, + float const&c8, float const& c9, float const& c10, float const& c11, + float const&c12, float const& c13, float const& c14, float const& c15 ) + { + + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x=c0 ; C1.x=c1; C0.y=c2 ; C1.y= c3; + C0.z=c4; C1.z=c5 ; C0.w=c6; C1.w=c7; + C2.x=c8; C3.x=c9; C2.y=c10 ; C3.y=c11; + C2.z=c12; C3.z=c13; C2.w=c14 ; C3.w=c15; + + cutlass::Array A0,A1; + + A0[0] = a0; A0[1] = a1; + A1[0] = a2; A1[1] = a3; + + + intx2_t A_0, A_1; + A_0 = *(reinterpret_cast(&A0)); + A_1 = *(reinterpret_cast(&A1)); + + D0 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_0, b0, C0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_0, b2, C1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_0, b4, C2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_0, b6, C3, true, false); + + + D0 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_1, b1, D0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_1, b3, D1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_1, b5, D2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_bf8_bf8_lit_lts(A_1, b7, D3, true, false); + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + + #endif + + } + }; + + +#else +////////////////////////////v_mmac_16x64x64 concatenate by 4x16x32x16///////////////////////////////////////////// + struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_LIT + { + using DRegisters = float[16]; + using ARegisters = float_e4m3_t[16]; + using BRegisters = float_e4m3_t[64]; + using CRegisters = float[16]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + float & d8, float & d9, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float_e4m3_t const& a0, float_e4m3_t const& a1,float_e4m3_t const& a2, float_e4m3_t const& a3, + float_e4m3_t const& a4, float_e4m3_t const& a5,float_e4m3_t const& a6, float_e4m3_t const& a7, + float_e4m3_t const& a8, float_e4m3_t const& a9,float_e4m3_t const& a10, float_e4m3_t const& a11, + float_e4m3_t const& a12, float_e4m3_t const& a13,float_e4m3_t const& a14, float_e4m3_t const& a15, + float_e4m3_t const& b0, float_e4m3_t const& b1,float_e4m3_t const& b2, float_e4m3_t const& b3, + float_e4m3_t const& b4, float_e4m3_t const& b5,float_e4m3_t const& b6, float_e4m3_t const& b7, + float_e4m3_t const& b8, float_e4m3_t const& b9,float_e4m3_t const& b10, float_e4m3_t const& b11, + float_e4m3_t const& b12, float_e4m3_t const& b13,float_e4m3_t const& b14, float_e4m3_t const& b15, + float_e4m3_t const& b16, float_e4m3_t const& b17,float_e4m3_t const& b18, float_e4m3_t const& b19, + float_e4m3_t const& b20, float_e4m3_t const& b21,float_e4m3_t const& b22, float_e4m3_t const& b23, + float_e4m3_t const& b24, float_e4m3_t const& b25,float_e4m3_t const& b26, float_e4m3_t const& b27, + float_e4m3_t const& b28, float_e4m3_t const& b29,float_e4m3_t const& b30, float_e4m3_t const& b31, + float_e4m3_t const& b32, float_e4m3_t const& b33,float_e4m3_t const& b34, float_e4m3_t const& b35, + float_e4m3_t const& b36, float_e4m3_t const& b37,float_e4m3_t const& b38, float_e4m3_t const& b39, + float_e4m3_t const& b40, float_e4m3_t const& b41,float_e4m3_t const& b42, float_e4m3_t const& b43, + float_e4m3_t const& b44, float_e4m3_t const& b45,float_e4m3_t const& b46, float_e4m3_t const& b47, + float_e4m3_t const& b48, float_e4m3_t const& b49,float_e4m3_t const& b50, float_e4m3_t const& b51, + float_e4m3_t const& b52, float_e4m3_t const& b53,float_e4m3_t const& b54, float_e4m3_t const& b55, + float_e4m3_t const& b56, float_e4m3_t const& b57,float_e4m3_t const& b58, float_e4m3_t const& b59, + float_e4m3_t const& b60, float_e4m3_t const& b61,float_e4m3_t const& b62, float_e4m3_t const& b63, + float const& c0, float const&c1, float const& c2, float const& c3, + float const&c4, float const& c5, float const& c6, float const& c7, + float const&c8, float const& c9, float const& c10, float const& c11, + float const&c12, float const& c13, float const& c14, float const& c15 ) + { + + #if (defined(__gfx938__)) && defined(DCU_ASM) + + v4f C0,C1,C2,C3; + v4f D0,D1,D2,D3; + + C0.x=c0 ; C1.x=c1; C0.y=c2 ; C1.y= c3; + C0.z=c4; C1.z=c5 ; C0.w=c6; C1.w=c7; + C2.x=c8; C3.x=c9; C2.y=c10 ; C3.y=c11; + C2.z=c12; C3.z=c13; C2.w=c14 ; C3.w=c15; + + cutlass::Array A0,A1,B0,B1,B2,B3,B4,B5,B6,B7; + + A0[0] = a0; A0[1] = a1; A0[2] = a2; A0[3] = a3; + A0[4] = a4; A0[5] = a5; A0[6] = a6; A0[7] = a7; + + A1[0] = a8; A1[1] = a9; A1[2] = a10; A1[3] = a11; + A1[4] = a12; A1[5] = a13; A1[6] = a14; A1[7] = a15; + + B0[0] = b0;B0[1] = b1;B0[2] = b2;B0[3] = b3; + B0[4] = b4;B0[5] = b5;B0[6] = b6;B0[7] = b7; + + + B1[0] = b16;B1[1] = b17;B1[2] = b18;B1[3] = b19; + B1[4] = b20;B1[5] = b21;B1[6] = b22;B1[7] = b23; + + B2[0] = b32;B2[1] = b33;B2[2] = b34;B2[3] = b35; + B2[4] = b36;B2[5] = b37;B2[6] = b38;B2[7] = b39; + + B3[0] = b48;B3[1] = b49;B3[2] = b50;B3[3] = b51; + B3[4] = b52;B3[5] = b53;B3[6] = b54;B3[7] = b55; + + B4[0] = b8;B4[1] = b9;B4[2] = b10;B4[3] = b11; + B4[4] = b12;B4[5] = b13;B4[6] = b14;B4[7] = b15; + + + B5[0] = b24;B5[1] = b25;B5[2] = b26;B5[3] = b27; + B5[4] = b28;B5[5] = b29;B5[6] = b30;B5[7] = b31; + + + B6[0] = b40;B6[1] = b41;B6[2] = b42;B6[3] = b43; + B6[4] = b44;B6[5] = b45;B6[6] = b46;B6[7] = b47; + + + B7[0] = b56;B7[1] = b57;B7[2] = b58;B7[3] = b59; + B7[4] = b60;B7[5] = b61;B7[6] = b62;B7[7] = b63; + + intx2_t A_0, A_1,B_0,B_1,B_2,B_3,B_4,B_5,B_6,B_7; + A_0 = *(reinterpret_cast(&A0)); + A_1 = *(reinterpret_cast(&A1)); + B_0 = *(reinterpret_cast(&B0)); + B_1 = *(reinterpret_cast(&B1)); + B_2 = *(reinterpret_cast(&B2)); + B_3 = *(reinterpret_cast(&B3)); + B_4 = *(reinterpret_cast(&B4)); + B_5 = *(reinterpret_cast(&B5)); + B_6 = *(reinterpret_cast(&B6)); + B_7 = *(reinterpret_cast(&B7)); + + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, B_0, C0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, B_1, C1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, B_2, C2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_0, B_3, C3, true, false); + + + D0 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, B_4, D0, true, false); + D1 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, B_5, D1, true, false); + D2 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, B_6, D2, true, false); + D3 = __builtin_hcu_mmac_f32_16x16x32_fp8_fp8_lit_lts(A_1, B_7, D3, true, false); + + d0 = D0.x; d1 = D1.x; d2 = D0.y; d3 = D1.y; + d4 = D0.z; d5 = D1.z; d6 = D0.w; d7 = D1.w; + + d8 = D2.x; d9 = D3.x; d10 = D2.y; d11 = D3.y; + d12 = D2.z; d13 = D3.z; d14 = D2.w; d15 = D3.w; + #endif + + } + }; +#endif + + + + + + struct GFX938_16x16x64_F32F8F8F32E4M3E4M3_NN_LIT : GFX938_16x16x64_F32F8F8F32E4M3E4M3_NT_LIT {}; + struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_LIT_BLayout : GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_LIT {}; + struct GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT_BLayout : GFX938_16x64x64_F32F8F8F32E4M3E4M3_NT {}; + struct GFX938_16x64x32_F32F8F8F32E4M3E4M3_NN : GFX938_16x64x32_F32F8F8F32E4M3E4M3_NT {}; + struct GFX938_16x64x32_F32F8F8F32E4M3E4M3_NN_Blayout : GFX938_16x64x32_F32F8F8F32E4M3E4M3_NT {}; + struct GFX938_16x64x32_F32F8F8F32E4M3E4M3_NT_BLayout : GFX938_16x64x32_F32F8F8F32E4M3E4M3_NT {}; + + struct GFX938_16x64x32_F32BF8BF8F32E5M2E5M2_NN : GFX938_16x64x32_F32BF8BF8F32E5M2E5M2_NT {}; + struct GFX938_16x64x64_F32BF8BF8F32E5M2E5M2_NT_LIT_BLayout : GFX938_16x64x64_F32BF8BF8F32E5M2E5M2_NT_LIT {}; +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_hip.hpp new file mode 100644 index 000000000..397935c90 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_hip.hpp @@ -0,0 +1,70 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +// +// Direct FMA for any type +// + +template +struct UniversalFMA +{ + using DRegisters = D[1]; + using ARegisters = A[1]; + using BRegisters = B[1]; + using CRegisters = C[1]; + + CUTE_HOST_DEVICE static constexpr void + fma(D & d, + A const& a, + B const& b, + C const& c) + { + // Forward to an ADL/cute free function for these types + using cute::fma; + fma(d, a, b, c); + } +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm61_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm61_hip.hpp new file mode 100644 index 000000000..8f433414e --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm61_hip.hpp @@ -0,0 +1,93 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +#pragma once + +#include +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 610)) +# define CUTE_ARCH_MMA_SM61_ENABLED +#endif + +namespace cute +{ + +struct SM61_DP4A +{ + using DRegisters = int32_t[1]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = int32_t[1]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int32_t& d, uint32_t const& a, uint32_t const& b, int32_t const& c) + { +#if defined(CUTE_ARCH_MMA_SM61_ENABLED) + asm volatile("dp4a.s32.s32 %0, %1, %2, %3;" + : "=r"(d) + : "r"(a), "r"(b), "r"(c)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM61_DP4A without CUTE_ARCH_MMA_SM61_ENABLED"); +#endif + } +}; + +struct SM61_DP2A +{ + using DRegisters = int32_t[1]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = int32_t[1]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(int32_t& d, uint32_t const& a, uint32_t const& b, int32_t const& c) + { +#if defined(CUTE_ARCH_MMA_SM61_ENABLED) + asm volatile("dp2a.s32.s32 %0, %1, %2, %3;" + : "=r"(d) + : "r"(a), "r"(b), "r"(c)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM61_DP2A without CUTE_ARCH_MMA_SM61_ENABLED"); +#endif + } +}; + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm70_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm70_hip.hpp new file mode 100644 index 000000000..32cc4b95a --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm70_hip.hpp @@ -0,0 +1,335 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config +#if ((__CUDACC_VER_MAJOR__ > 10) || (__CUDACC_VER_MAJOR__ == 10 && __CUDACC_VER_MINOR__ >= 1)) +# define CUTE_ARCH_MMA_SM70_SUPPORTED +# if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 700)) +# define CUTE_ARCH_MMA_SM70_ENABLED +# endif +#endif + +namespace cute +{ + +// +// SM70 MMA 884 F16F16F16 +// + +struct SM70_8x8x4_F16F16F16F16_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.row.col.f16.f16.f16.f16" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6, %7}," + "{%8, %9, %10, %11};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F16F16F16F16_TN without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F16F16F16F16_NT +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.col.row.f16.f16.f16.f16" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6, %7}," + "{%8, %9, %10, %11};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F16F16F16F16_NT without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F16F16F16F16_NN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.col.col.f16.f16.f16.f16" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6, %7}," + "{%8, %9, %10, %11};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F16F16F16F16_NN without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F16F16F16F16_TT +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.row.row.f16.f16.f16.f16" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6, %7}," + "{%8, %9, %10, %11};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F16F16F16F16_TT without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// SM70 MMA 884 F16F16F32 +// + +struct SM70_8x8x4_F32F16F16F32_TN +{ + using DRegisters = float[8]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.row.col.f32.f16.f16.f32" + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11}," + "{%12, %13, %14, %15, %16, %17, %18, %19};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), + "=f"(d4), "=f"(d5), "=f"(d6), "=f"(d7) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3), + "f"(c4), "f"(c5), "f"(c6), "f"(c7)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F32F16F16F32_TN without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F32F16F16F32_NT +{ + using DRegisters = float[8]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.col.row.f32.f16.f16.f32" + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11}," + "{%12, %13, %14, %15, %16, %17, %18, %19};" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), + "=f"(d4), "=f"(d5), "=f"(d6), "=f"(d7) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3), + "f"(c4), "f"(c5), "f"(c6), "f"(c7)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F32F16F16F32_NT without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F32F16F16F32_NN +{ + using DRegisters = float[8]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.col.col.f32.f16.f16.f32" + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11}," + "{%12, %13, %14, %15, %16, %17, %18, %19};" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), + "=f"(d4), "=f"(d5), "=f"(d6), "=f"(d7) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3), + "f"(c4), "f"(c5), "f"(c6), "f"(c7)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F32F16F16F32_NN without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SM70_8x8x4_F32F16F16F32_TT +{ + using DRegisters = float[8]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[2]; + using CRegisters = float[8]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, uint32_t const& b1, + float const& c0, float const& c1, float const& c2, float const& c3, + float const& c4, float const& c5, float const& c6, float const& c7) + { +#if defined(CUTE_ARCH_MMA_SM70_ENABLED) + asm volatile("mma.sync.aligned.m8n8k4.row.row.f32.f16.f16.f32" + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11}," + "{%12, %13, %14, %15, %16, %17, %18, %19};" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), + "=f"(d4), "=f"(d5), "=f"(d6), "=f"(d7) + : "r"(a0), "r"(a1), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3), + "f"(c4), "f"(c5), "f"(c6), "f"(c7)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM70_8x8x4_F32F16F16F32_TT without CUTE_ARCH_MMA_SM70_ENABLED"); +#endif + } + +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm75_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm75_hip.hpp new file mode 100644 index 000000000..38f466232 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm75_hip.hpp @@ -0,0 +1,126 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// Config +#if ((__CUDACC_VER_MAJOR__ > 10) || (__CUDACC_VER_MAJOR__ == 10 && __CUDACC_VER_MINOR__ >= 2)) +# define CUTE_ARCH_MMA_SM75_SUPPORTED +# if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 750)) +# define CUTE_ARCH_MMA_SM75_ENABLED +# endif +#endif + +namespace cute +{ + +// +// SM75 MMA 1688 F16F16F32 +// + +struct SM75_16x8x8_F32F16F16F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = float[4]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + float const& c0, float const& c1, float const& c2, float const& c3) + { +#if defined(CUTE_ARCH_MMA_SM75_ENABLED) + asm volatile("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM75_16x8x8_F32F16F16F32_TN without CUTE_ARCH_MMA_SM75_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// SM75 MMA 8816 S8S8S32 +// + +struct SM75_8x8x16_S32S8S8S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + // Register asm fma + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM75_ENABLED) + asm volatile("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32" + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM75_8x8x16_S32S8S8S32_TN without CUTE_ARCH_MMA_SM75_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm80_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm80_hip.hpp new file mode 100644 index 000000000..2589ea892 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm80_hip.hpp @@ -0,0 +1,2149 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif + /************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +#pragma once + +#include +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800)) +# define CUTE_ARCH_MMA_SM80_ENABLED + +#if (__HIP_DEVICE_COMPILE__ <= 900) +#define CUTE_ARCH_MMA_B1_AND_SM80_ENABLED +#endif + +#if (__HIP_DEVICE_COMPILE__ <= 890) +#define CUTE_ARCH_MMA_B1_XOR_SM80_ENABLED +#endif + +#endif + + + +namespace cute { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM80_16x8x8_F16F16F16F16_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 " + "{%0, %1}," + "{%2, %3}," + "{%4}," + "{%5, %6};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x8_F16F16F16F16_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_F16F16F16F16_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.f16.f16.f16.f16 " + "{%0, %1}," + "{%2, %3, %4, %5}," + "{%6, %7}," + "{%8, %9};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_F16F16F16F16_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM80_16x8x8_F32F16F16F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x8_F32F16F16F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_F32F16F16F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_F32F16F16F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM80_16x8x8_F32BF16BF16F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k8.row.col.f32.bf16.bf16.f32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x8_F32BF16BF16F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_F32BF16BF16F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_F32BF16BF16F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x4 TN +struct SM80_16x8x4_F32TF32TF32F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k4.row.col.f32.tf32.tf32.f32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x4_F32TF32TF32F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM80_16x8x8_F32TF32TF32F32_TN +{ + using DRegisters = float[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(float & d0, float & d1, float & d2, float & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + float const & c0, float const & c1, float const & c2, float const & c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "f"(c0), "f"(c1), "f"(c2), "f"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x8_F32TF32TF32F32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x4 TN +struct SM80_8x8x4_F64F64F64F64_TN +{ + using DRegisters = double[2]; + using ARegisters = double[1]; + using BRegisters = double[1]; + using CRegisters = double[2]; + + CUTE_HOST_DEVICE static void + fma(double & d0, double & d1, + double const& a0, + double const& b0, + double const& c0, double const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k4.row.col.f64.f64.f64.f64 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=d"(d0), "=d"(d1) + : "d"(a0), + "d"(b0), + "d"(c0), "d"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x4_F64F64F64F64_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +// MMA 8x8x4 TN with Planar Complex multiplication +struct SM80_8x8x4_C64C64C64C64_TN +{ + using DRegisters = complex[2]; + using ARegisters = complex[1]; + using BRegisters = complex[1]; + using CRegisters = complex[2]; + + CUTE_HOST_DEVICE static void + fma(complex & d0, complex & d1, + complex const& a0, + complex const& b0, + complex const& c0, complex const& c1) + { + // Because thrust::complex does not provide a mutable ref + double& rd0 = reinterpret_cast(d0)[0]; + double& id0 = reinterpret_cast(d0)[1]; + double& rd1 = reinterpret_cast(d1)[0]; + double& id1 = reinterpret_cast(d1)[1]; + + // d.real() = a.real() * b.real() + c.real(); + SM80_8x8x4_F64F64F64F64_TN::fma( + rd0, rd1, + a0.real(), + b0.real(), + c0.real(), c1.real()); + + // d.imag() = a.imag() * b.real() + c.imag(); + SM80_8x8x4_F64F64F64F64_TN::fma( + id0, id1, + a0.imag(), + b0.real(), + c0.imag(), c1.imag()); + + // d.real() = -a.imag() * b.imag() + d.real(); + SM80_8x8x4_F64F64F64F64_TN::fma( + rd0, rd1, + -a0.imag(), + b0.imag(), + d0.real(), d1.real()); + + // d.imag() = a.real() * b.imag() + d.imag(); + SM80_8x8x4_F64F64F64F64_TN::fma( + id0, id1, + a0.real(), + b0.imag(), + d0.imag(), d1.imag()); + } +}; + +// MMA 8x8x4 TN with Gaussian Complex multiplication: +// (a + bi)*(c + di) +// yields +// t0 += a*c +// t1 += b*d +// t2 += (a+b)*(c+d) +// then +// re = t0 - t1 +// im = t2 - t0 - t1 +struct SM80_8x8x4_GC64C64C64GC64_TN +{ + struct GaussComplex { + double t0, t1, t2; + + CUTE_HOST_DEVICE //constexpr + operator complex() const { return complex(t0 - t1, t2 - t0 - t1); } + + CUTE_HOST_DEVICE friend //constexpr + complex operator*(GaussComplex const& a, complex const& b) { return static_cast>(a) * b; } + CUTE_HOST_DEVICE friend //constexpr + complex operator*(complex const& a, GaussComplex const& b) { return b * a; } + + CUTE_HOST_DEVICE friend //constexpr + complex operator+(GaussComplex const& a, complex const& b) { return static_cast>(a) + b; } + CUTE_HOST_DEVICE friend //constexpr + complex operator+(complex const& a, GaussComplex const& b) { return b + a; } + }; + + using DRegisters = GaussComplex[2]; + using ARegisters = complex[1]; + using BRegisters = complex[1]; + using CRegisters = GaussComplex[2]; + + CUTE_HOST_DEVICE static void + fma(GaussComplex & d0, GaussComplex & d1, + complex const& a0, + complex const& b0, + GaussComplex const& c0, GaussComplex const& c1) + { + SM80_8x8x4_F64F64F64F64_TN::fma(d0.t0, d1.t0, + a0.real(), + b0.real(), + c0.t0, c1.t0); + SM80_8x8x4_F64F64F64F64_TN::fma(d0.t1, d1.t1, + a0.imag(), + b0.imag(), + c0.t1, c1.t1); + SM80_8x8x4_F64F64F64F64_TN::fma(d0.t2, d1.t2, + a0.real() + a0.imag(), + b0.real() + b0.imag(), + c0.t2, c1.t2); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32S8S8S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32S8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32S8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32S8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32S8S8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32S8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32S8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32S8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S8S8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32S8U8S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.s8.u8.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32S8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32S8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.s8.u8.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32S8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32S8U8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.s8.u8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32S8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32S8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.s8.u8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32S8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S8U8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s8.u8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s8.u8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32U8S8S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.u8.s8.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32U8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32U8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.u8.s8.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32U8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32U8S8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.u8.s8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32U8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32U8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.u8.s8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32U8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U8S8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u8.s8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U8S8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U8S8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u8.s8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U8S8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32U8U8S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.u8.u8.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32U8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x16 TN +struct SM80_8x8x16_S32U8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k16.row.col.s32.u8.u8.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x16_S32U8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32U8U8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.u8.u8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32U8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM80_16x8x16_S32U8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.s32.u8.u8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x16_S32U8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U8U8S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u8.u8.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U8U8S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U8U8S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u8.u8.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U8U8S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32S4S4S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.s4.s4.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32S4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32S4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.s4.s4.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32S4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S4S4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s4.s4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s4.s4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32S4S4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.s4.s4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32S4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32S4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.s4.s4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32S4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32S4U4S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.s4.u4.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32S4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32S4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.s4.u4.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32S4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S4U4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s4.u4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32S4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.s4.u4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32S4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32S4U4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.s4.u4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32S4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32S4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.s4.u4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32S4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32U4S4S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.u4.s4.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32U4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32U4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.u4.s4.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32U4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U4S4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u4.s4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u4.s4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32U4S4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.u4.s4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32U4S4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32U4S4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.u4.s4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32U4S4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32U4U4S32_TN +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.u4.u4.s32 " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32U4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x32 TN +struct SM80_8x8x32_S32U4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k32.row.col.s32.u4.u4.s32.satfinite " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x32_S32U4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U4U4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u4.u4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x32 TN +struct SM80_16x8x32_S32U4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k32.row.col.s32.u4.u4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x32_S32U4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32U4U4S32_TN +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.u4.u4.s32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32U4U4S32_TN without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x64 TN +struct SM80_16x8x64_S32U4U4S32_TN_SATURATE +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k64.row.col.s32.u4.u4.s32.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x64_S32U4U4S32_TN_SATURATE without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 8x8x128 TN +struct SM80_8x8x128_S32U1U1S32_TN_XORPOPC +{ + using DRegisters = uint32_t[2]; + using ARegisters = uint32_t[1]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, + uint32_t const& a0, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1) + { +#if defined(CUTE_ARCH_MMA_B1_XOR_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m8n8k128.row.col.s32.b1.b1.s32.xor.popc " + "{%0, %1}," + "{%2}," + "{%3}," + "{%4, %5};\n" + : "=r"(d0), "=r"(d1) + : "r"(a0), + "r"(b0), + "r"(c0), "r"(c1)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_8x8x128_S32U1U1S32_TN_XORPOPC without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x128 TN +struct SM80_16x8x128_S32U1U1S32_TN_XORPOPC +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[2]; + using BRegisters = uint32_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, + uint32_t const& b0, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_B1_XOR_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k128.row.col.s32.b1.b1.s32.xor.popc " + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), + "r"(b0), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x128_S32U1U1S32_TN_XORPOPC without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x256 TN +struct SM80_16x8x256_S32U1U1S32_TN_XORPOPC +{ + using DRegisters = uint32_t[4]; + using ARegisters = uint32_t[4]; + using BRegisters = uint32_t[2]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint32_t const& b0, uint32_t const& b1, + uint32_t const& c0, uint32_t const& c1, uint32_t const& c2, uint32_t const& c3) + { +#if defined(CUTE_ARCH_MMA_B1_XOR_SM80_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k256.row.col.s32.b1.b1.s32.xor.popc " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "r"(b0), "r"(b1), + "r"(c0), "r"(c1), "r"(c2), "r"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM80_16x8x256_S32U1U1S32_TN_XORPOPC without CUTE_ARCH_MMA_SM80_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_desc_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_desc_hip.hpp new file mode 100644 index 000000000..efde319a9 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_desc_hip.hpp @@ -0,0 +1,163 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +#pragma once + +#include + +#include + +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) && defined(__CUDA_ARCH_FEAT_SM90_ALL)) +# define CUTE_ARCH_MMA_SM90A_ENABLED +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cute { + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// GMMA Descriptor and utilities + +// GMMA enums and utilities +namespace GMMA +{ + +enum class LayoutType : uint8_t { + INTERLEAVE = 0, + B128 = 1, + B64 = 2, + B32 = 3, +}; + +CUTE_HOST_DEVICE char const* to_string(LayoutType const& t) { + switch (t) { + case LayoutType::INTERLEAVE: return "INTERLEAVE"; + case LayoutType::B128: return "B128"; + case LayoutType::B64: return "B64"; + case LayoutType::B32: return "B32"; + } + return nullptr; +} + +#if !defined(__CUDACC_RTC__) +// Output operator for all enums in this namespace +CUTE_HOST std::ostream& operator<<(std::ostream& os, LayoutType const& t) { + char const* s = to_string(t); + if (s) { + std::operator<<(os, s); // Explicit call to avoid ambiguity + } else { + os.setstate(std::ios_base::failbit); + } + return os; +} +#endif // !defined(__CUDACC_RTC__) + +} // end namespace GMMA + +union GmmaDescriptor +{ + + CUTE_HOST_DEVICE constexpr + GmmaDescriptor() noexcept : desc_(0) {} + CUTE_HOST_DEVICE constexpr + GmmaDescriptor(uint64_t desc) noexcept : desc_(desc) {} + CUTE_HOST_DEVICE constexpr + GmmaDescriptor(GmmaDescriptor const& t) noexcept : desc_(t.desc_) {} + CUTE_HOST_DEVICE constexpr + GmmaDescriptor(GmmaDescriptor && t) noexcept : desc_(t.desc_) {} + + CUTE_HOST_DEVICE constexpr + GmmaDescriptor& operator=(GmmaDescriptor const& t) noexcept { + desc_ = t.desc_; + return *this; + } + + CUTE_HOST_DEVICE constexpr + GmmaDescriptor& operator=(GmmaDescriptor && t) noexcept { + desc_ = t.desc_; + return *this; + } + + uint64_t desc_; + uint32_t reg32_[2]; + uint16_t reg16_[4]; + + // Bitfield implementation avoids the need for shifts in assignment + struct { + // start_address, bit [0,14), 4LSB not included + uint16_t start_address_ : 14, : 2; // 14 bits [0,14), 2 bits unused + // leading dimension byte offset, bit [16,30), 4LSB not included + // For N: This is the stride from the first col to the second col of the 8x2 brick in INTERLEAVED + // Unused for all SWIZZLE_* layouts (and assumed to be 1) + // For T: This is the stride from the first 8 rows to the next 8 rows. + uint16_t leading_byte_offset_ : 14, : 2; // 14 bits [0,14), 2 bits unused + // stride dimension byte offset, bit [32,46), 4LSB not included + // For N: This is the stride from the first 8 rows to the next 8 rows. + // For T: This is the stride fro mthe first 8 cols to the next 8 cols. + uint16_t stride_byte_offset_ : 14, : 2; // 14 bits [0,14), 2 bits unused + // base_offset, bit [49,52) + // Valid only for SWIZZLE_128B and SWIZZLE_64B + uint8_t : 1, base_offset_ : 3, : 4; // 1 bit unused, 3 bits [1,4), 4 bits unused + // layout type, bit [62,64) + // SWIZZLE_NONE = 0, SWIZZLE_32B = 3, SWIZZLE_64B = 2, SWIZZLE_128B = 1 + uint8_t : 6, layout_type_ : 2; // 6 bits unused, 2 bits [6,8) + } bitfield; + + // Decay to a uint64_t + CUTE_HOST_DEVICE constexpr + operator uint64_t() const noexcept { return desc_; } + + // Printer + CUTE_HOST_DEVICE friend void print(GmmaDescriptor const& t) + { + #if !defined(__CUDACC_RTC__) + printf("GmmaDescriptor: 0x%016llx\n", static_cast(t.desc_)); + printf(" start_addr : 0x%04x\n", t.bitfield.start_address_); + printf(" leading_off: 0x%04x (%d)\n", t.bitfield.leading_byte_offset_, t.bitfield.leading_byte_offset_); + printf(" stride_off : 0x%04x (%d)\n", t.bitfield.stride_byte_offset_, t.bitfield.stride_byte_offset_); + printf(" base_offset: 0x%01x\n", t.bitfield.base_offset_); + printf(" layout_type: 0x%01x (%s)\n", t.bitfield.layout_type_, to_string(static_cast(t.bitfield.layout_type_))); + #endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_gmma_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_gmma_hip.hpp new file mode 100644 index 000000000..74dc0e4e7 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_gmma_hip.hpp @@ -0,0 +1,20645 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +// Config +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) && defined(__CUDA_ARCH_FEAT_SM90_ALL)) +# define CUTE_ARCH_MMA_SM90A_ENABLED +#endif + +namespace cute { + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Warpgroup sync primitives + +CUTE_HOST_DEVICE +void +warpgroup_arrive() +{ +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile ("wgmma.fence.sync.aligned;\n" ::: "memory"); +#else + CUTE_RUNTIME_ASSERT("Attempting to use wgmma.fence without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif +} + +template +CUTE_HOST_DEVICE +void +warpgroup_wait() +{ + static_assert(N >= 0 && N <= 7, "WGMMA wait: N must be in range [0, 7]"); +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile("wgmma.wait_group.sync.aligned %0;\n" :: "n"(N) : "memory"); +#else + CUTE_RUNTIME_ASSERT("Attempting to use wgmma.wait_group without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif +} + +// Marks the commit point for one or more sized batch of warpgroup MMAs. +CUTE_HOST_DEVICE +void +warpgroup_commit_batch() +{ +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile("wgmma.commit_group.sync.aligned;\n" ::: "memory"); +#else + CUTE_RUNTIME_ASSERT("Attempting to use wgmma.commit_group without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif +} + +CUTE_HOST_DEVICE +void +warpgroup_fence_operand(uint32_t& reg) { + // MSVC emits a build error for 'asm volatile' + // even if it only occurs in a __device__ function. + // This prevents the error. +#if defined(__HIP_DEVICE_COMPILE__) + asm volatile("" : "+r"(reg) :: "memory"); +#endif +} + +CUTE_HOST_DEVICE +void +warpgroup_fence_operand(float& reg) { +#if defined(__HIP_DEVICE_COMPILE__) + asm volatile("" : "+f"(reg) :: "memory"); +#endif +} + +namespace GMMA { + +enum class Major { + K = 0, + MN = 1 +}; + +enum class ScaleOut { + Zero = 0, + One = 1 +}; + +enum class ScaleIn { + Neg = -1, + One = 1 +}; + +} // namespace GMMA + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// GMMA PTX definitions: C = (scaleA * A) * (scaleB * B) + (scaleD * C) +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %4, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f16.f16.f16 " + "{%0, %1}," + " %2," + " %3," + " p, %5, %6, %7, %8;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %7, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f16.f16.f16 " + "{%0, %1}," + "{%2, %3, %4, %5}," + " %6," + " p, %8, %9, %10;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f16.f16.f16 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8, %9, %10;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f16.f16.f16 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11, %12;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12, %13, %14;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15, %16;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20, %21, %22;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23, %24;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %26, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + " %24," + " %25," + " p, %27, %28, %29, %30;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %29, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + "{%24, %25, %26, %27}," + " %28," + " p, %30, %31, %32;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36, %37, %38;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39, %40;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52, %53, %54;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55, %56;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F16F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68, %69, %70;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F16F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F16+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F16F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f16.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71, %72;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F16F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f32.f16.f16 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8, %9, %10;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f32.f16.f16 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12, %13, %14;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15, %16;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20, %21, %22;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23, %24;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36, %37, %38;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39, %40;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52, %53, %54;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55, %56;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68, %69, %70;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71, %72;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100, %101, %102;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103, %104;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F32F16F16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132, %133, %134;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F32F16F16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F32+=F16*F16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F32F16F16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f32.f16.f16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135, %136;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F32F16F16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f32.bf16.bf16 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8, %9, %10;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k16.f32.bf16.bf16 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12, %13, %14;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15, %16;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20, %21, %22;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23, %24;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36, %37, %38;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39, %40;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52, %53, %54;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55, %56;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68, %69, %70;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71, %72;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100, %101, %102;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103, %104;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F32BF16BF16_SS +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132, %133, %134;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspA)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F32BF16BF16_SS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x16 F32+=BF16*BF16 +template < + GMMA::Major tnspA, + GMMA::Major tnspB, + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x16_F32BF16BF16_RS +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + static_assert(tnspA == GMMA::Major::K, + "Register source operand A must have K major layout."); + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k16.f32.bf16.bf16 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135, %136;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB)), "n"(int32_t(tnspB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x16_F32BF16BF16_RS without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k8.f32.tf32.tf32 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k8.f32.tf32.tf32 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x8_F32TF32TF32_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x8_F32TF32TF32_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x8 TN F32+=TF32*TF32 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x8_F32TF32TF32_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k8.f32.tf32.tf32 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x8_F32TF32TF32_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*S8 +struct SM90_64x8x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.s8 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*S8 +struct SM90_64x8x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*S8 +struct SM90_64x16x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*S8 +struct SM90_64x16x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*S8 +struct SM90_64x32x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*S8 +struct SM90_64x32x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*S8 +struct SM90_64x64x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*S8 +struct SM90_64x64x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*S8 +struct SM90_64x96x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*S8 +struct SM90_64x96x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*S8 +struct SM90_64x128x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*S8 +struct SM90_64x128x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*S8 +struct SM90_64x192x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*S8 +struct SM90_64x192x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*S8 +struct SM90_64x256x32_S32S8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*S8 +struct SM90_64x256x32_S32S8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*S8 +struct SM90_64x8x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.s8 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*S8 +struct SM90_64x8x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*S8 +struct SM90_64x16x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*S8 +struct SM90_64x16x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*S8 +struct SM90_64x32x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*S8 +struct SM90_64x32x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*S8 +struct SM90_64x64x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*S8 +struct SM90_64x64x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*S8 +struct SM90_64x96x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*S8 +struct SM90_64x96x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*S8 +struct SM90_64x128x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*S8 +struct SM90_64x128x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*S8 +struct SM90_64x192x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*S8 +struct SM90_64x192x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*S8 +struct SM90_64x256x32_S32S8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*S8 +struct SM90_64x256x32_S32S8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*U8 +struct SM90_64x8x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.u8 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*U8 +struct SM90_64x8x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*U8 +struct SM90_64x16x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*U8 +struct SM90_64x16x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*U8 +struct SM90_64x32x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*U8 +struct SM90_64x32x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*U8 +struct SM90_64x64x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*U8 +struct SM90_64x64x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*U8 +struct SM90_64x96x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*U8 +struct SM90_64x96x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*U8 +struct SM90_64x128x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*U8 +struct SM90_64x128x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*U8 +struct SM90_64x192x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*U8 +struct SM90_64x192x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*U8 +struct SM90_64x256x32_S32S8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*U8 +struct SM90_64x256x32_S32S8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*U8 +struct SM90_64x8x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.u8 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=S8*U8 +struct SM90_64x8x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*U8 +struct SM90_64x16x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=S8*U8 +struct SM90_64x16x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*U8 +struct SM90_64x32x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=S8*U8 +struct SM90_64x32x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*U8 +struct SM90_64x64x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=S8*U8 +struct SM90_64x64x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*U8 +struct SM90_64x96x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=S8*U8 +struct SM90_64x96x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*U8 +struct SM90_64x128x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=S8*U8 +struct SM90_64x128x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*U8 +struct SM90_64x192x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=S8*U8 +struct SM90_64x192x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*U8 +struct SM90_64x256x32_S32S8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=S8*U8 +struct SM90_64x256x32_S32S8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.s8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32S8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*S8 +struct SM90_64x8x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.s8 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*S8 +struct SM90_64x8x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*S8 +struct SM90_64x16x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*S8 +struct SM90_64x16x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*S8 +struct SM90_64x32x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*S8 +struct SM90_64x32x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*S8 +struct SM90_64x64x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*S8 +struct SM90_64x64x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*S8 +struct SM90_64x96x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*S8 +struct SM90_64x96x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*S8 +struct SM90_64x128x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*S8 +struct SM90_64x128x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*S8 +struct SM90_64x192x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*S8 +struct SM90_64x192x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*S8 +struct SM90_64x256x32_S32U8S8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8S8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*S8 +struct SM90_64x256x32_S32U8S8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8S8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*S8 +struct SM90_64x8x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.s8 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*S8 +struct SM90_64x8x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*S8 +struct SM90_64x16x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*S8 +struct SM90_64x16x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*S8 +struct SM90_64x32x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*S8 +struct SM90_64x32x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*S8 +struct SM90_64x64x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*S8 +struct SM90_64x64x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*S8 +struct SM90_64x96x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*S8 +struct SM90_64x96x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*S8 +struct SM90_64x128x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*S8 +struct SM90_64x128x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*S8 +struct SM90_64x192x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*S8 +struct SM90_64x192x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*S8 +struct SM90_64x256x32_S32U8S8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.s8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8S8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*S8 +struct SM90_64x256x32_S32U8S8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.s8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8S8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*U8 +struct SM90_64x8x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.u8 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*U8 +struct SM90_64x8x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*U8 +struct SM90_64x16x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*U8 +struct SM90_64x16x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*U8 +struct SM90_64x32x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*U8 +struct SM90_64x32x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*U8 +struct SM90_64x64x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*U8 +struct SM90_64x64x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*U8 +struct SM90_64x96x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*U8 +struct SM90_64x96x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*U8 +struct SM90_64x128x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*U8 +struct SM90_64x128x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*U8 +struct SM90_64x192x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*U8 +struct SM90_64x192x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*U8 +struct SM90_64x256x32_S32U8U8_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8U8_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*U8 +struct SM90_64x256x32_S32U8U8_SS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8U8_SS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*U8 +struct SM90_64x8x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.u8 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN S32+=U8*U8 +struct SM90_64x8x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*U8 +struct SM90_64x16x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN S32+=U8*U8 +struct SM90_64x16x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*U8 +struct SM90_64x32x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN S32+=U8*U8 +struct SM90_64x32x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*U8 +struct SM90_64x64x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN S32+=U8*U8 +struct SM90_64x64x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*U8 +struct SM90_64x96x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN S32+=U8*U8 +struct SM90_64x96x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*U8 +struct SM90_64x128x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN S32+=U8*U8 +struct SM90_64x128x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*U8 +struct SM90_64x192x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN S32+=U8*U8 +struct SM90_64x192x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + uint32_t & d64, uint32_t & d65, uint32_t & d66, uint32_t & d67, + uint32_t & d68, uint32_t & d69, uint32_t & d70, uint32_t & d71, + uint32_t & d72, uint32_t & d73, uint32_t & d74, uint32_t & d75, + uint32_t & d76, uint32_t & d77, uint32_t & d78, uint32_t & d79, + uint32_t & d80, uint32_t & d81, uint32_t & d82, uint32_t & d83, + uint32_t & d84, uint32_t & d85, uint32_t & d86, uint32_t & d87, + uint32_t & d88, uint32_t & d89, uint32_t & d90, uint32_t & d91, + uint32_t & d92, uint32_t & d93, uint32_t & d94, uint32_t & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63), + "+r"(d64), "+r"(d65), "+r"(d66), "+r"(d67), + "+r"(d68), "+r"(d69), "+r"(d70), "+r"(d71), + "+r"(d72), "+r"(d73), "+r"(d74), "+r"(d75), + "+r"(d76), "+r"(d77), "+r"(d78), "+r"(d79), + "+r"(d80), "+r"(d81), "+r"(d82), "+r"(d83), + "+r"(d84), "+r"(d85), "+r"(d86), "+r"(d87), + "+r"(d88), "+r"(d89), "+r"(d90), "+r"(d91), + "+r"(d92), "+r"(d93), "+r"(d94), "+r"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*U8 +struct SM90_64x256x32_S32U8U8_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.u8 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8U8_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN S32+=U8*U8 +struct SM90_64x256x32_S32U8U8_RS_TN_SATURATE +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + uint32_t & d000, uint32_t & d001, uint32_t & d002, uint32_t & d003, + uint32_t & d004, uint32_t & d005, uint32_t & d006, uint32_t & d007, + uint32_t & d008, uint32_t & d009, uint32_t & d010, uint32_t & d011, + uint32_t & d012, uint32_t & d013, uint32_t & d014, uint32_t & d015, + uint32_t & d016, uint32_t & d017, uint32_t & d018, uint32_t & d019, + uint32_t & d020, uint32_t & d021, uint32_t & d022, uint32_t & d023, + uint32_t & d024, uint32_t & d025, uint32_t & d026, uint32_t & d027, + uint32_t & d028, uint32_t & d029, uint32_t & d030, uint32_t & d031, + uint32_t & d032, uint32_t & d033, uint32_t & d034, uint32_t & d035, + uint32_t & d036, uint32_t & d037, uint32_t & d038, uint32_t & d039, + uint32_t & d040, uint32_t & d041, uint32_t & d042, uint32_t & d043, + uint32_t & d044, uint32_t & d045, uint32_t & d046, uint32_t & d047, + uint32_t & d048, uint32_t & d049, uint32_t & d050, uint32_t & d051, + uint32_t & d052, uint32_t & d053, uint32_t & d054, uint32_t & d055, + uint32_t & d056, uint32_t & d057, uint32_t & d058, uint32_t & d059, + uint32_t & d060, uint32_t & d061, uint32_t & d062, uint32_t & d063, + uint32_t & d064, uint32_t & d065, uint32_t & d066, uint32_t & d067, + uint32_t & d068, uint32_t & d069, uint32_t & d070, uint32_t & d071, + uint32_t & d072, uint32_t & d073, uint32_t & d074, uint32_t & d075, + uint32_t & d076, uint32_t & d077, uint32_t & d078, uint32_t & d079, + uint32_t & d080, uint32_t & d081, uint32_t & d082, uint32_t & d083, + uint32_t & d084, uint32_t & d085, uint32_t & d086, uint32_t & d087, + uint32_t & d088, uint32_t & d089, uint32_t & d090, uint32_t & d091, + uint32_t & d092, uint32_t & d093, uint32_t & d094, uint32_t & d095, + uint32_t & d096, uint32_t & d097, uint32_t & d098, uint32_t & d099, + uint32_t & d100, uint32_t & d101, uint32_t & d102, uint32_t & d103, + uint32_t & d104, uint32_t & d105, uint32_t & d106, uint32_t & d107, + uint32_t & d108, uint32_t & d109, uint32_t & d110, uint32_t & d111, + uint32_t & d112, uint32_t & d113, uint32_t & d114, uint32_t & d115, + uint32_t & d116, uint32_t & d117, uint32_t & d118, uint32_t & d119, + uint32_t & d120, uint32_t & d121, uint32_t & d122, uint32_t & d123, + uint32_t & d124, uint32_t & d125, uint32_t & d126, uint32_t & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.s32.u8.u8.satfinite " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p;\n" + "}\n" + : "+r"(d000), "+r"(d001), "+r"(d002), "+r"(d003), + "+r"(d004), "+r"(d005), "+r"(d006), "+r"(d007), + "+r"(d008), "+r"(d009), "+r"(d010), "+r"(d011), + "+r"(d012), "+r"(d013), "+r"(d014), "+r"(d015), + "+r"(d016), "+r"(d017), "+r"(d018), "+r"(d019), + "+r"(d020), "+r"(d021), "+r"(d022), "+r"(d023), + "+r"(d024), "+r"(d025), "+r"(d026), "+r"(d027), + "+r"(d028), "+r"(d029), "+r"(d030), "+r"(d031), + "+r"(d032), "+r"(d033), "+r"(d034), "+r"(d035), + "+r"(d036), "+r"(d037), "+r"(d038), "+r"(d039), + "+r"(d040), "+r"(d041), "+r"(d042), "+r"(d043), + "+r"(d044), "+r"(d045), "+r"(d046), "+r"(d047), + "+r"(d048), "+r"(d049), "+r"(d050), "+r"(d051), + "+r"(d052), "+r"(d053), "+r"(d054), "+r"(d055), + "+r"(d056), "+r"(d057), "+r"(d058), "+r"(d059), + "+r"(d060), "+r"(d061), "+r"(d062), "+r"(d063), + "+r"(d064), "+r"(d065), "+r"(d066), "+r"(d067), + "+r"(d068), "+r"(d069), "+r"(d070), "+r"(d071), + "+r"(d072), "+r"(d073), "+r"(d074), "+r"(d075), + "+r"(d076), "+r"(d077), "+r"(d078), "+r"(d079), + "+r"(d080), "+r"(d081), "+r"(d082), "+r"(d083), + "+r"(d084), "+r"(d085), "+r"(d086), "+r"(d087), + "+r"(d088), "+r"(d089), "+r"(d090), "+r"(d091), + "+r"(d092), "+r"(d093), "+r"(d094), "+r"(d095), + "+r"(d096), "+r"(d097), "+r"(d098), "+r"(d099), + "+r"(d100), "+r"(d101), "+r"(d102), "+r"(d103), + "+r"(d104), "+r"(d105), "+r"(d106), "+r"(d107), + "+r"(d108), "+r"(d109), "+r"(d110), "+r"(d111), + "+r"(d112), "+r"(d113), "+r"(d114), "+r"(d115), + "+r"(d116), "+r"(d117), "+r"(d118), "+r"(d119), + "+r"(d120), "+r"(d121), "+r"(d122), "+r"(d123), + "+r"(d124), "+r"(d125), "+r"(d126), "+r"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_S32U8U8_RS_TN_SATURATE without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %4, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e4m3.e4m3 " + "{%0, %1}," + " %2," + " %3," + " p, %5, %6;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %7, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e4m3.e4m3 " + "{%0, %1}," + "{%2, %3, %4, %5}," + " %6," + " p, %8, %9;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %26, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + " %24," + " %25," + " p, %27, %28;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %29, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + "{%24, %25, %26, %27}," + " %28," + " p, %30, %31;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E4M3E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E4M3E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E4M3*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E4M3E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e4m3.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E4M3E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %4, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e4m3.e5m2 " + "{%0, %1}," + " %2," + " %3," + " p, %5, %6;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %7, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e4m3.e5m2 " + "{%0, %1}," + "{%2, %3, %4, %5}," + " %6," + " p, %8, %9;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %26, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + " %24," + " %25," + " p, %27, %28;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %29, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + "{%24, %25, %26, %27}," + " %28," + " p, %30, %31;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E4M3E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E4M3E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E4M3*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E4M3E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e4m3.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E4M3E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %4, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e5m2.e4m3 " + "{%0, %1}," + " %2," + " %3," + " p, %5, %6;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %7, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e5m2.e4m3 " + "{%0, %1}," + "{%2, %3, %4, %5}," + " %6," + " p, %8, %9;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %26, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + " %24," + " %25," + " p, %27, %28;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %29, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + "{%24, %25, %26, %27}," + " %28," + " p, %30, %31;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E5M2E4M3_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E5M2E4M3_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E5M2*E4M3 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E5M2E4M3_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e5m2.e4m3 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E5M2E4M3_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %4, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e5m2.e5m2 " + "{%0, %1}," + " %2," + " %3," + " p, %5, %6;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[2]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %7, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f16.e5m2.e5m2 " + "{%0, %1}," + "{%2, %3, %4, %5}," + " %6," + " p, %8, %9;\n" + "}\n" + : "+r"(d0), "+r"(d1) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x8x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x8x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n8k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x8x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %6, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3}," + " %4," + " %5," + " p, %7, %8;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[4]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %9, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + " %8," + " p, %10, %11;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x16x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x16x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + float & d0, float & d1, float & d2, float & d3, + float & d4, float & d5, float & d6, float & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n16k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+f"(d0), "+f"(d1), "+f"(d2), "+f"(d3), + "+f"(d4), "+f"(d5), "+f"(d6), "+f"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x16x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %10, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + " %8," + " %9," + " p, %11, %12;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[8]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a0, uint32_t const& a1, uint32_t const& a2, uint32_t const& a3, + uint64_t const& desc_b, + uint32_t & d0, uint32_t & d1, uint32_t & d2, uint32_t & d3, + uint32_t & d4, uint32_t & d5, uint32_t & d6, uint32_t & d7, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %13, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7}," + "{%8, %9, %10, %11}," + " %12," + " p, %14, %15;\n" + "}\n" + : "+r"(d0), "+r"(d1), "+r"(d2), "+r"(d3), + "+r"(d4), "+r"(d5), "+r"(d6), "+r"(d7) + : "r"(a0), "r"(a1), "r"(a2), "r"(a3), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x32x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x32x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n32k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x32x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %18, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + " %16," + " %17," + " p, %19, %20;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[16]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %21, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15}," + "{%16, %17, %18, %19}," + " %20," + " p, %22, %23;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x64x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x64x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n64k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x64x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %26, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + " %24," + " %25," + " p, %27, %28;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[24]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %29, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23}," + "{%24, %25, %26, %27}," + " %28," + " p, %30, %31;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x96x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x96x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n96k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x96x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %34, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + " %32," + " %33," + " p, %35, %36;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[32]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %37, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31}," + "{%32, %33, %34, %35}," + " %36," + " p, %38, %39;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x128x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x128x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n128k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x128x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %50, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + " %48," + " %49," + " p, %51, %52;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[48]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %53, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47}," + "{%48, %49, %50, %51}," + " %52," + " p, %54, %55;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %98, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + " %96," + " %97," + " p, %99, %100;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x192x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x192x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[96]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + float & d00, float & d01, float & d02, float & d03, + float & d04, float & d05, float & d06, float & d07, + float & d08, float & d09, float & d10, float & d11, + float & d12, float & d13, float & d14, float & d15, + float & d16, float & d17, float & d18, float & d19, + float & d20, float & d21, float & d22, float & d23, + float & d24, float & d25, float & d26, float & d27, + float & d28, float & d29, float & d30, float & d31, + float & d32, float & d33, float & d34, float & d35, + float & d36, float & d37, float & d38, float & d39, + float & d40, float & d41, float & d42, float & d43, + float & d44, float & d45, float & d46, float & d47, + float & d48, float & d49, float & d50, float & d51, + float & d52, float & d53, float & d54, float & d55, + float & d56, float & d57, float & d58, float & d59, + float & d60, float & d61, float & d62, float & d63, + float & d64, float & d65, float & d66, float & d67, + float & d68, float & d69, float & d70, float & d71, + float & d72, float & d73, float & d74, float & d75, + float & d76, float & d77, float & d78, float & d79, + float & d80, float & d81, float & d82, float & d83, + float & d84, float & d85, float & d86, float & d87, + float & d88, float & d89, float & d90, float & d91, + float & d92, float & d93, float & d94, float & d95, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %101, 0;\n" + "wgmma.mma_async.sync.aligned.m64n192k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95}," + "{%96, %97, %98, %99}," + " %100," + " p, %102, %103;\n" + "}\n" + : "+f"(d00), "+f"(d01), "+f"(d02), "+f"(d03), + "+f"(d04), "+f"(d05), "+f"(d06), "+f"(d07), + "+f"(d08), "+f"(d09), "+f"(d10), "+f"(d11), + "+f"(d12), "+f"(d13), "+f"(d14), "+f"(d15), + "+f"(d16), "+f"(d17), "+f"(d18), "+f"(d19), + "+f"(d20), "+f"(d21), "+f"(d22), "+f"(d23), + "+f"(d24), "+f"(d25), "+f"(d26), "+f"(d27), + "+f"(d28), "+f"(d29), "+f"(d30), "+f"(d31), + "+f"(d32), "+f"(d33), "+f"(d34), "+f"(d35), + "+f"(d36), "+f"(d37), "+f"(d38), "+f"(d39), + "+f"(d40), "+f"(d41), "+f"(d42), "+f"(d43), + "+f"(d44), "+f"(d45), "+f"(d46), "+f"(d47), + "+f"(d48), "+f"(d49), "+f"(d50), "+f"(d51), + "+f"(d52), "+f"(d53), "+f"(d54), "+f"(d55), + "+f"(d56), "+f"(d57), "+f"(d58), "+f"(d59), + "+f"(d60), "+f"(d61), "+f"(d62), "+f"(d63), + "+f"(d64), "+f"(d65), "+f"(d66), "+f"(d67), + "+f"(d68), "+f"(d69), "+f"(d70), "+f"(d71), + "+f"(d72), "+f"(d73), "+f"(d74), "+f"(d75), + "+f"(d76), "+f"(d77), "+f"(d78), "+f"(d79), + "+f"(d80), "+f"(d81), "+f"(d82), "+f"(d83), + "+f"(d84), "+f"(d85), "+f"(d86), "+f"(d87), + "+f"(d88), "+f"(d89), "+f"(d90), "+f"(d91), + "+f"(d92), "+f"(d93), "+f"(d94), "+f"(d95) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x192x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %66, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + " %64," + " %65," + " p, %67, %68;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F16+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F16E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = uint32_t[64]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a00, uint32_t const& a01, uint32_t const& a02, uint32_t const& a03, + uint64_t const& desc_b, + uint32_t & d00, uint32_t & d01, uint32_t & d02, uint32_t & d03, + uint32_t & d04, uint32_t & d05, uint32_t & d06, uint32_t & d07, + uint32_t & d08, uint32_t & d09, uint32_t & d10, uint32_t & d11, + uint32_t & d12, uint32_t & d13, uint32_t & d14, uint32_t & d15, + uint32_t & d16, uint32_t & d17, uint32_t & d18, uint32_t & d19, + uint32_t & d20, uint32_t & d21, uint32_t & d22, uint32_t & d23, + uint32_t & d24, uint32_t & d25, uint32_t & d26, uint32_t & d27, + uint32_t & d28, uint32_t & d29, uint32_t & d30, uint32_t & d31, + uint32_t & d32, uint32_t & d33, uint32_t & d34, uint32_t & d35, + uint32_t & d36, uint32_t & d37, uint32_t & d38, uint32_t & d39, + uint32_t & d40, uint32_t & d41, uint32_t & d42, uint32_t & d43, + uint32_t & d44, uint32_t & d45, uint32_t & d46, uint32_t & d47, + uint32_t & d48, uint32_t & d49, uint32_t & d50, uint32_t & d51, + uint32_t & d52, uint32_t & d53, uint32_t & d54, uint32_t & d55, + uint32_t & d56, uint32_t & d57, uint32_t & d58, uint32_t & d59, + uint32_t & d60, uint32_t & d61, uint32_t & d62, uint32_t & d63, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %69, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f16.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63}," + "{%64, %65, %66, %67}," + " %68," + " p, %70, %71;\n" + "}\n" + : "+r"(d00), "+r"(d01), "+r"(d02), "+r"(d03), + "+r"(d04), "+r"(d05), "+r"(d06), "+r"(d07), + "+r"(d08), "+r"(d09), "+r"(d10), "+r"(d11), + "+r"(d12), "+r"(d13), "+r"(d14), "+r"(d15), + "+r"(d16), "+r"(d17), "+r"(d18), "+r"(d19), + "+r"(d20), "+r"(d21), "+r"(d22), "+r"(d23), + "+r"(d24), "+r"(d25), "+r"(d26), "+r"(d27), + "+r"(d28), "+r"(d29), "+r"(d30), "+r"(d31), + "+r"(d32), "+r"(d33), "+r"(d34), "+r"(d35), + "+r"(d36), "+r"(d37), "+r"(d38), "+r"(d39), + "+r"(d40), "+r"(d41), "+r"(d42), "+r"(d43), + "+r"(d44), "+r"(d45), "+r"(d46), "+r"(d47), + "+r"(d48), "+r"(d49), "+r"(d50), "+r"(d51), + "+r"(d52), "+r"(d53), "+r"(d54), "+r"(d55), + "+r"(d56), "+r"(d57), "+r"(d58), "+r"(d59), + "+r"(d60), "+r"(d61), "+r"(d62), "+r"(d63) + : "r"(a00), "r"(a01), "r"(a02), "r"(a03), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F16E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E5M2E5M2_SS_TN +{ + using DRegisters = void; + using ARegisters = uint64_t[1]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint64_t const& desc_a, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %130, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + " %128," + " %129," + " p, %131, %132;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "l"(desc_a), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E5M2E5M2_SS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// GMMA 64x256x32 TN F32+=E5M2*E5M2 +template < + GMMA::ScaleIn scaleA = GMMA::ScaleIn::One, + GMMA::ScaleIn scaleB = GMMA::ScaleIn::One +> +struct SM90_64x256x32_F32E5M2E5M2_RS_TN +{ + using DRegisters = void; + using ARegisters = uint32_t[4]; + using BRegisters = uint64_t[1]; + using CRegisters = float[128]; + + CUTE_HOST_DEVICE static void + fma(uint32_t const& a000, uint32_t const& a001, uint32_t const& a002, uint32_t const& a003, + uint64_t const& desc_b, + float & d000, float & d001, float & d002, float & d003, + float & d004, float & d005, float & d006, float & d007, + float & d008, float & d009, float & d010, float & d011, + float & d012, float & d013, float & d014, float & d015, + float & d016, float & d017, float & d018, float & d019, + float & d020, float & d021, float & d022, float & d023, + float & d024, float & d025, float & d026, float & d027, + float & d028, float & d029, float & d030, float & d031, + float & d032, float & d033, float & d034, float & d035, + float & d036, float & d037, float & d038, float & d039, + float & d040, float & d041, float & d042, float & d043, + float & d044, float & d045, float & d046, float & d047, + float & d048, float & d049, float & d050, float & d051, + float & d052, float & d053, float & d054, float & d055, + float & d056, float & d057, float & d058, float & d059, + float & d060, float & d061, float & d062, float & d063, + float & d064, float & d065, float & d066, float & d067, + float & d068, float & d069, float & d070, float & d071, + float & d072, float & d073, float & d074, float & d075, + float & d076, float & d077, float & d078, float & d079, + float & d080, float & d081, float & d082, float & d083, + float & d084, float & d085, float & d086, float & d087, + float & d088, float & d089, float & d090, float & d091, + float & d092, float & d093, float & d094, float & d095, + float & d096, float & d097, float & d098, float & d099, + float & d100, float & d101, float & d102, float & d103, + float & d104, float & d105, float & d106, float & d107, + float & d108, float & d109, float & d110, float & d111, + float & d112, float & d113, float & d114, float & d115, + float & d116, float & d117, float & d118, float & d119, + float & d120, float & d121, float & d122, float & d123, + float & d124, float & d125, float & d126, float & d127, + GMMA::ScaleOut const scale_D = GMMA::ScaleOut::One) + { +#if defined(CUTE_ARCH_MMA_SM90A_ENABLED) + asm volatile( + "{\n" + ".reg .pred p;\n" + "setp.ne.b32 p, %133, 0;\n" + "wgmma.mma_async.sync.aligned.m64n256k32.f32.e5m2.e5m2 " + "{%0, %1, %2, %3, %4, %5, %6, %7, " + " %8, %9, %10, %11, %12, %13, %14, %15, " + " %16, %17, %18, %19, %20, %21, %22, %23, " + " %24, %25, %26, %27, %28, %29, %30, %31, " + " %32, %33, %34, %35, %36, %37, %38, %39, " + " %40, %41, %42, %43, %44, %45, %46, %47, " + " %48, %49, %50, %51, %52, %53, %54, %55, " + " %56, %57, %58, %59, %60, %61, %62, %63, " + " %64, %65, %66, %67, %68, %69, %70, %71, " + " %72, %73, %74, %75, %76, %77, %78, %79, " + " %80, %81, %82, %83, %84, %85, %86, %87, " + " %88, %89, %90, %91, %92, %93, %94, %95, " + " %96, %97, %98, %99, %100, %101, %102, %103, " + " %104, %105, %106, %107, %108, %109, %110, %111, " + " %112, %113, %114, %115, %116, %117, %118, %119, " + " %120, %121, %122, %123, %124, %125, %126, %127}," + "{%128, %129, %130, %131}," + " %132," + " p, %134, %135;\n" + "}\n" + : "+f"(d000), "+f"(d001), "+f"(d002), "+f"(d003), + "+f"(d004), "+f"(d005), "+f"(d006), "+f"(d007), + "+f"(d008), "+f"(d009), "+f"(d010), "+f"(d011), + "+f"(d012), "+f"(d013), "+f"(d014), "+f"(d015), + "+f"(d016), "+f"(d017), "+f"(d018), "+f"(d019), + "+f"(d020), "+f"(d021), "+f"(d022), "+f"(d023), + "+f"(d024), "+f"(d025), "+f"(d026), "+f"(d027), + "+f"(d028), "+f"(d029), "+f"(d030), "+f"(d031), + "+f"(d032), "+f"(d033), "+f"(d034), "+f"(d035), + "+f"(d036), "+f"(d037), "+f"(d038), "+f"(d039), + "+f"(d040), "+f"(d041), "+f"(d042), "+f"(d043), + "+f"(d044), "+f"(d045), "+f"(d046), "+f"(d047), + "+f"(d048), "+f"(d049), "+f"(d050), "+f"(d051), + "+f"(d052), "+f"(d053), "+f"(d054), "+f"(d055), + "+f"(d056), "+f"(d057), "+f"(d058), "+f"(d059), + "+f"(d060), "+f"(d061), "+f"(d062), "+f"(d063), + "+f"(d064), "+f"(d065), "+f"(d066), "+f"(d067), + "+f"(d068), "+f"(d069), "+f"(d070), "+f"(d071), + "+f"(d072), "+f"(d073), "+f"(d074), "+f"(d075), + "+f"(d076), "+f"(d077), "+f"(d078), "+f"(d079), + "+f"(d080), "+f"(d081), "+f"(d082), "+f"(d083), + "+f"(d084), "+f"(d085), "+f"(d086), "+f"(d087), + "+f"(d088), "+f"(d089), "+f"(d090), "+f"(d091), + "+f"(d092), "+f"(d093), "+f"(d094), "+f"(d095), + "+f"(d096), "+f"(d097), "+f"(d098), "+f"(d099), + "+f"(d100), "+f"(d101), "+f"(d102), "+f"(d103), + "+f"(d104), "+f"(d105), "+f"(d106), "+f"(d107), + "+f"(d108), "+f"(d109), "+f"(d110), "+f"(d111), + "+f"(d112), "+f"(d113), "+f"(d114), "+f"(d115), + "+f"(d116), "+f"(d117), "+f"(d118), "+f"(d119), + "+f"(d120), "+f"(d121), "+f"(d122), "+f"(d123), + "+f"(d124), "+f"(d125), "+f"(d126), "+f"(d127) + : "r"(a000), "r"(a001), "r"(a002), "r"(a003), + "l"(desc_b), + "r"(int32_t(scale_D)), "n"(int32_t(scaleA)), "n"(int32_t(scaleB))); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_64x256x32_F32E5M2E5M2_RS_TN without CUTE_ARCH_MMA_SM90A_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_hip.hpp new file mode 100644 index 000000000..b49e5b17d --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_hip.hpp @@ -0,0 +1,1257 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +#pragma once + +#include + +#include + +// Config +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 900) +# define CUTE_ARCH_MMA_SM90_ENABLED +# define CUTE_ARCH_MMA_F64_SM90_ENABLED +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cute { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x4 TN +struct SM90_16x8x4_F64F64F64F64_TN +{ + using DRegisters = double[4]; + using ARegisters = double[2]; + using BRegisters = double[1]; + using CRegisters = double[4]; + + CUTE_HOST_DEVICE static void + fma(double & d0, double & d1, double & d2, double & d3, + double const& a0, double const& a1, + double const& b0, + double const& c0, double const& c1, double const& c2, double const& c3) + { +#if defined(CUTE_ARCH_MMA_F64_SM90_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k4.row.col.f64.f64.f64.f64" + "{%0, %1, %2, %3}," + "{%4, %5}," + "{%6}," + "{%7, %8, %9, %10};\n" + : "=d"(d0), "=d"(d1), "=d"(d2), "=d"(d3) + : "d"(a0), "d"(a1), + "d"(b0), + "d"(c0), "d"(c1), "d"(c2), "d"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_16x8x4_F64F64F64F64_TN without CUTE_ARCH_MMA_SM90_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM90_16x8x8_F64F64F64F64_TN +{ + using DRegisters = double[4]; + using ARegisters = double[4]; + using BRegisters = double[2]; + using CRegisters = double[4]; + + CUTE_HOST_DEVICE static void + fma(double & d0, double & d1, double & d2, double & d3, + double const& a0, double const& a1, double const& a2, double const& a3, + double const& b0, double const& b1, + double const& c0, double const& c1, double const& c2, double const& c3) + { +#if defined(CUTE_ARCH_MMA_F64_SM90_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k8.row.col.f64.f64.f64.f64" + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7}," + "{%8, %9}," + "{%10, %11, %12, %13};\n" + : "=d"(d0), "=d"(d1), "=d"(d2), "=d"(d3) + : "d"(a0), "d"(a1), "d"(a2), "d"(a3), + "d"(b0), "d"(b1), + "d"(c0), "d"(c1), "d"(c2), "d"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_16x8x8_F64F64F64F64_TN without CUTE_ARCH_MMA_SM90_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM90_16x8x16_F64F64F64F64_TN +{ + using DRegisters = double[4]; + using ARegisters = double[8]; + using BRegisters = double[4]; + using CRegisters = double[4]; + + CUTE_HOST_DEVICE static void + fma(double & d0, double & d1, double & d2, double & d3, + double const& a0, double const& a1, double const& a2, double const& a3, + double const& a4, double const& a5, double const& a6, double const& a7, + double const& b0, double const& b1, double const& b2, double const& b3, + double const& c0, double const& c1, double const& c2, double const& c3) + { +#if defined(CUTE_ARCH_MMA_F64_SM90_ENABLED) + asm volatile( + "mma.sync.aligned.m16n8k16.row.col.f64.f64.f64.f64" + "{%0, %1, %2, %3}," + "{%4, %5, %6, %7, %8, %9, %10, %11}," + "{%12, %13, %14, %15}," + "{%16, %17, %18, %19};\n" + : "=d"(d0), "=d"(d1), "=d"(d2), "=d"(d3) + : "d"(a0), "d"(a1), "d"(a2), "d"(a3), + "d"(a4), "d"(a5), "d"(a6), "d"(a7), + "d"(b0), "d"(b1), "d"(b2), "d"(b3), + "d"(c0), "d"(c1), "d"(c2), "d"(c3)); +#else + CUTE_RUNTIME_ASSERT("Attempting to use SM90_16x8x16_F64F64F64F64_TN without CUTE_ARCH_MMA_SM90_ENABLED"); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x4 TN +struct SM90_16x8x4_C64C64C64C64_TN +{ + using DRegisters = complex[4]; + using ARegisters = complex[2]; + using BRegisters = complex[1]; + using CRegisters = complex[4]; + + CUTE_HOST_DEVICE static void + fma(complex & d0, complex & d1, + complex & d2, complex & d3, + complex const& a0, complex const& a1, + complex const& b0, + complex const& c0, complex const& c1, + complex const& c2, complex const& c3) + { + // Because thrust::complex does not provide a mutable ref + double& rd0 = reinterpret_cast(d0)[0]; + double& id0 = reinterpret_cast(d0)[1]; + double& rd1 = reinterpret_cast(d1)[0]; + double& id1 = reinterpret_cast(d1)[1]; + double& rd2 = reinterpret_cast(d2)[0]; + double& id2 = reinterpret_cast(d2)[1]; + double& rd3 = reinterpret_cast(d3)[0]; + double& id3 = reinterpret_cast(d3)[1]; + + // d.real() = a.real() * b.real() + c.real(); + SM90_16x8x4_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + a0.real(), a1.real(), + b0.real(), + c0.real(), c1.real(), c2.real(), c3.real()); + + // d.imag() = a.imag() * b.real() + c.imag(); + SM90_16x8x4_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.imag(), a1.imag(), + b0.real(), + c0.imag(), c1.imag(), c2.imag(), c3.imag()); + + // d.real() = -a.imag() * b.imag() + d.real(); + SM90_16x8x4_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + -a0.imag(), -a1.imag(), + b0.imag(), + d0.real(), d1.real(), d2.real(), d3.real()); + + // d.imag() = a.real() * b.imag() + d.imag(); + SM90_16x8x4_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.real(), a1.real(), + b0.imag(), + d0.imag(), d1.imag(), d2.imag(), d3.imag()); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x8 TN +struct SM90_16x8x8_C64C64C64C64_TN +{ + using DRegisters = complex[4]; + using ARegisters = complex[4]; + using BRegisters = complex[2]; + using CRegisters = complex[4]; + + CUTE_HOST_DEVICE static void + fma(complex & d0, complex & d1, + complex & d2, complex & d3, + complex const& a0, complex const& a1, + complex const& a2, complex const& a3, + complex const& b0, complex const& b1, + complex const& c0, complex const& c1, + complex const& c2, complex const& c3) + { + // Because thrust::complex does not provide a mutable ref + double& rd0 = reinterpret_cast(d0)[0]; + double& id0 = reinterpret_cast(d0)[1]; + double& rd1 = reinterpret_cast(d1)[0]; + double& id1 = reinterpret_cast(d1)[1]; + double& rd2 = reinterpret_cast(d2)[0]; + double& id2 = reinterpret_cast(d2)[1]; + double& rd3 = reinterpret_cast(d3)[0]; + double& id3 = reinterpret_cast(d3)[1]; + + // d.real() = a.real() * b.real() + c.real(); + SM90_16x8x8_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + a0.real(), a1.real(), a2.real(), a3.real(), + b0.real(), b1.real(), + c0.real(), c1.real(), c2.real(), c3.real()); + + // d.imag() = a.imag() * b.real() + c.imag(); + SM90_16x8x8_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.imag(), a1.imag(), a2.imag(), a3.imag(), + b0.real(), b1.real(), + c0.imag(), c1.imag(), c2.imag(), c3.imag()); + + // d.real() = -a.imag() * b.imag() + d.real(); + SM90_16x8x8_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + -a0.imag(), -a1.imag(), -a2.imag(), -a3.imag(), + b0.imag(), b1.imag(), + d0.real(), d1.real(), d2.real(), d3.real()); + + // d.imag() = a.real() * b.imag() + d.imag(); + SM90_16x8x8_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.real(), a1.real(), a2.real(), a3.real(), + b0.imag(), b1.imag(), + d0.imag(), d1.imag(), d2.imag(), d3.imag()); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// MMA 16x8x16 TN +struct SM90_16x8x16_C64C64C64C64_TN +{ + using DRegisters = complex[4]; + using ARegisters = complex[8]; + using BRegisters = complex[4]; + using CRegisters = complex[4]; + + CUTE_HOST_DEVICE static void + fma(complex & d0, complex & d1, + complex & d2, complex & d3, + complex const& a0, complex const& a1, + complex const& a2, complex const& a3, + complex const& a4, complex const& a5, + complex const& a6, complex const& a7, + complex const& b0, complex const& b1, + complex const& b2, complex const& b3, + complex const& c0, complex const& c1, + complex const& c2, complex const& c3) + { + // Because thrust::complex does not provide a mutable ref + double& rd0 = reinterpret_cast(d0)[0]; + double& id0 = reinterpret_cast(d0)[1]; + double& rd1 = reinterpret_cast(d1)[0]; + double& id1 = reinterpret_cast(d1)[1]; + double& rd2 = reinterpret_cast(d2)[0]; + double& id2 = reinterpret_cast(d2)[1]; + double& rd3 = reinterpret_cast(d3)[0]; + double& id3 = reinterpret_cast(d3)[1]; + + // d.real() = a.real() * b.real() + c.real(); + SM90_16x8x16_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + a0.real(), a1.real(), a2.real(), a3.real(), + a4.real(), a5.real(), a6.real(), a7.real(), + b0.real(), b1.real(), b2.real(), b3.real(), + c0.real(), c1.real(), c2.real(), c3.real()); + + // d.imag() = a.imag() * b.real() + c.imag(); + SM90_16x8x16_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.imag(), a1.imag(), a2.imag(), a3.imag(), + a4.imag(), a5.imag(), a6.imag(), a7.imag(), + b0.real(), b1.real(), b2.real(), b3.real(), + c0.imag(), c1.imag(), c2.imag(), c3.imag()); + + // d.real() = -a.imag() * b.imag() + d.real(); + SM90_16x8x16_F64F64F64F64_TN::fma( + rd0, rd1, rd2, rd3, + -a0.imag(), -a1.imag(), -a2.imag(), -a3.imag(), + -a4.imag(), -a5.imag(), -a6.imag(), -a7.imag(), + b0.imag(), b1.imag(), b2.imag(), b3.imag(), + d0.real(), d1.real(), d2.real(), d3.real()); + + // d.imag() = a.real() * b.imag() + d.imag(); + SM90_16x8x16_F64F64F64F64_TN::fma( + id0, id1, id2, id3, + a0.real(), a1.real(), a2.real(), a3.real(), + a4.real(), a5.real(), a6.real(), a7.real(), + b0.imag(), b1.imag(), b2.imag(), b3.imag(), + d0.imag(), d1.imag(), d2.imag(), d3.imag()); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cute { +namespace GMMA { + +template < + class ElementA, + class ElementB, + class ElementC, + class TileShape_MNK, + GMMA::Major MajorA = GMMA::Major::K, + GMMA::Major MajorB = GMMA::Major::K, + auto... Args // e.g. GMMA::ScaleOut::One, [GMMA::ScaleIn::One, GMMA::ScaleIn::One] + // But most commonly leave empty for defaults +> +CUTE_HOST_DEVICE constexpr +auto +ss_op_selector() +{ + static_assert(is_static::value, "TileShape_MNK must be static."); + static_assert(rank(TileShape_MNK{}) == 3, "TileShape_MNK must be rank 3."); + static_assert(size<0>(TileShape_MNK{}) % 64 == 0, "Tile_M must be a multiple of 64."); + auto Tile_N = size<1>(TileShape_MNK{}); + + // FP16 accumulator + if constexpr (is_same_v) { + static_assert(is_same_v, "Element types for AB must be half if ElementC is half."); + static_assert(is_same_v, "Element types for AB must be half if ElementC is half."); + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + + // Dispatch against the Tile N mode size + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F16F16F16_SS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F16F16F16_SS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP32 accumulator + else if constexpr (is_same_v) { + + // FP16 inputs + if constexpr (is_same_v) { + static_assert(is_same_v, "ElementA and ElementB must be the same type for this config."); + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F32F16F16_SS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F32F16F16_SS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // BF16 inputs + else if constexpr (is_same_v) { + static_assert(is_same_v, "ElementA and ElementB must be the same type for this config."); + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F32BF16BF16_SS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F32BF16BF16_SS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // TF32 inputs + else if constexpr (is_same_v) { + static_assert(is_same_v, "ElementA and ElementB must be the same type for this config."); + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 8 == 0, "Tile_K must be a multiple of 8."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x8_F32TF32TF32_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x8_F32TF32TF32_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e4m3_t ; Input B: float_e4m3_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E4M3E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E4M3E4M3_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e4m3_t ; Input B: float_e5m2_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E4M3E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E4M3E5M2_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e5m2_t ; Input B: float_e5m2_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E5M2E5M2_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E5M2E5M2_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e5m2_t ; Input B: float_e4m3_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E5M2E4M3_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E5M2E4M3_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + else { + static_assert(sizeof(ElementA) == 0, "No eligible GMMA operator for request configuration."); + } + } + + // S32 accumulator + else if constexpr (is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + // ElementA == int8_t && ElementB == int8_t + if constexpr (is_same_v && is_same_v) { + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32S8S8_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32S8S8_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == int8_t && ElementB == uint8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32S8U8_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32S8U8_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == uint8_t && ElementB == int8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32U8S8_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32U8S8_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == uint8_t && ElementB == uint8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32U8U8_SS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32U8U8_SS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + } + + // Unknown accumulator type + else { + static_assert(sizeof(ElementC) == 0, "Unknown ElementC accumulator type."); + } +} + +template < + class ElementA, + class ElementB, + class ElementC, + class TileShape_MNK, + GMMA::Major MajorA = GMMA::Major::K, + GMMA::Major MajorB = GMMA::Major::K, + auto... Args // e.g. GMMA::ScaleOut::One, [GMMA::ScaleIn::One, GMMA::ScaleIn::One] + // But most commonly leave empty for defaults +> +CUTE_HOST_DEVICE constexpr +auto +rs_op_selector() +{ + static_assert(is_static::value, "TileShape_MNK must be static."); + static_assert(rank(TileShape_MNK{}) == 3, "TileShape_MNK must be rank 3."); + static_assert(size<0>(TileShape_MNK{}) % 64 == 0, "Tile_M must be a multiple of 64."); + static_assert(MajorA == GMMA::Major::K, "Register source A operand GMMAs must have K-major A layout."); + auto Tile_N = size<1>(TileShape_MNK{}); + + // FP16 accumulator + if constexpr (is_same_v) { + static_assert(is_same_v, "Element types for AB must be half if ElementC is half."); + static_assert(is_same_v, "Element types for AB must be half if ElementC is half."); + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + + // Dispatch against the Tile N mode size + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F16F16F16_RS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F16F16F16_RS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP32 accumulator + else if constexpr (is_same_v) { + static_assert(is_same_v, "ElementA and ElementB must be the same type for this config."); + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + + // FP16 inputs + if constexpr (is_same_v) { + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F32F16F16_RS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F32F16F16_RS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // BF16 inputs + else if constexpr (is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 16 == 0, "Tile_K must be a multiple of 16."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x16_F32BF16BF16_RS{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x16_F32BF16BF16_RS{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // TF32 inputs + else if constexpr (is_same_v) { + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 8 == 0, "Tile_K must be a multiple of 8."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x8_F32TF32TF32_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x8_F32TF32TF32_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e4m3_t ; Input B: float_e4m3_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E4M3E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E4M3E4M3_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e4m3_t ; Input B: float_e5m2_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E4M3E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E4M3E5M2_RS_TN{}; + } + else { + static_aRSert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e5m2_t ; Input B: float_e5m2_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E5M2E5M2_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E5M2E5M2_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // FP8 + // Input A: float_e5m2_t ; Input B: float_e4m3_t + else if constexpr (is_same_v && is_same_v) { + static_assert(MajorA == GMMA::Major::K, "MajorA must be GMMA::Major::K for this config."); + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_F32E5M2E4M3_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_F32E5M2E4M3_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + else { + static_assert(sizeof(ElementA) == 0, "No eligible GMMA operator for request configuration."); + } + } + + // S32 accumulator + else if constexpr (is_same_v) { + static_assert(MajorB == GMMA::Major::K, "MajorB must be GMMA::Major::K for this config."); + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + // ElementA == int8_t && ElementB == int8_t + if constexpr (is_same_v && is_same_v) { + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32S8S8_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32S8S8_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == int8_t && ElementB == uint8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32S8U8_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32S8U8_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == uint8_t && ElementB == int8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32U8S8_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32U8S8_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + + // ElementA == uint8_t && ElementB == uint8_t + else if constexpr (is_same_v && is_same_v) { + static_assert(size<2>(TileShape_MNK{}) % 32 == 0, "Tile_K must be a multiple of 32."); + + if constexpr (Tile_N % 256 == 0) { + return SM90_64x256x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 192 == 0) { + return SM90_64x192x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 128 == 0) { + return SM90_64x128x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 96 == 0) { + return SM90_64x96x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 64 == 0) { + return SM90_64x64x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 32 == 0) { + return SM90_64x32x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 16 == 0) { + return SM90_64x16x32_S32U8U8_RS_TN{}; + } + else if constexpr (Tile_N % 8 == 0) { + return SM90_64x8x32_S32U8U8_RS_TN{}; + } + else { + static_assert(Tile_N % 8 == 0, "Tile_N must be a multiple of 8."); + } + } + } + + // Unknown accumulator type + else { + static_assert(sizeof(ElementC) == 0, "Unknown ElementC accumulator type."); + } +} +} // end namespace GMMA +} // end namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/util_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/util_hip.hpp new file mode 100644 index 000000000..2456069fa --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/arch/util_hip.hpp @@ -0,0 +1,263 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +#if defined(__clang__) && defined(__CUDA__) + // __cvta_generic_to_shared was added in Clang 14: https://reviews.llvm.org/D111665 + #if __clang_major__ >= 14 + #define CUTE_CLANG_SUPPORTS_CVTA_GENERIC_TO_SHARED 1 + #endif + + // __nvvm_get_smem_pointer added in Clang 14: https://reviews.llvm.org/D111665 + // ... but will not work on Windows until Clang 15: https://reviews.llvm.org/D122897 + #if (!defined(_WIN32) && __clang_major__ >= 14) || __clang_major__ >= 15 + #define CUTE_CLANG_SUPPORTS_NVVM_GET_SMEM_POINTER 1 + #endif +#endif + +#if defined(__NVCC__) || defined(__CUDACC_RTC__) + // __cvta_generic_to_shared added in CUDA 11+ + #if __CUDACC_VER_MAJOR__ >= 11 + #define CUTE_NVCC_SUPPORTS_CVTA_GENERIC_TO_SHARED 1 + #endif + + // __nvvm_get_smem_pointer added in CUDA 10.2 + #if __CUDACC_VER_MAJOR__ == 10 && __CUDACC_VER_MINOR__ >= 2 + #define CUTE_NVCC_SUPPORTS_NVVM_GET_SMEM_POINTER 1 + #endif +#endif + +#if CUTE_NVCC_SUPPORTS_CVTA_GENERIC_TO_SHARED || CUTE_CLANG_SUPPORTS_CVTA_GENERIC_TO_SHARED + #define CUTE_CVTA_GENERIC_TO_SHARED_SUPPORTED 1 +#endif + +#if !defined(CUTE_CVTA_GENERIC_TO_SHARED_ACTIVATED) && CUTE_CVTA_GENERIC_TO_SHARED_SUPPORTED && defined(__HIP_DEVICE_COMPILE__) + #define CUTE_CVTA_GENERIC_TO_SHARED_ACTIVATED 1 +#endif + +#if CUTE_NVCC_SUPPORTS_NVVM_GET_SMEM_POINTER || CUTE_CLANG_SUPPORTS_NVVM_GET_SMEM_POINTER + #define CUTE_NVVM_GET_SMEM_POINTER_SUPPORTED 1 +#endif + +#if !defined(CUTE_NVVM_GET_SMEM_POINTER_ACTIVATED) && CUTE_NVVM_GET_SMEM_POINTER_SUPPORTED && defined(__HIP_DEVICE_COMPILE__) + #define CUTE_NVVM_GET_SMEM_POINTER_ACTIVATED 1 +#endif + +// Clang 14+ provides a declaration of __nvvm_get_smem_pointer, so we only need +// to provide one for NVCC +#if CUTE_NVCC_SUPPORTS_NVVM_GET_SMEM_POINTER + extern "C" { + // This NVVM intrinsic is subject to change in future versions of CUDA. + // Clients should not call it directly. + CUTE_DEVICE uint32_t __nvvm_get_smem_pointer(void*); + } +#endif + +namespace cute +{ + +/// CUTE helper to cast SMEM pointer to unsigned +CUTE_DEVICE +uint32_t +cast_smem_ptr_to_uint(void const* const ptr) +{ +// We prefer to use the new CVTA intrinsics if they are available, otherwise we will fall back to +// the previous internal intrinsics if they are available. +#ifdef DCU_ASM + return (uintptr_t)(ptr); +#elif CUTE_CVTA_GENERIC_TO_SHARED_ACTIVATED + // + // This NVVM intrinsic converts an address in shared memory to a plain + // unsigned integer. This is necessary to pass to shared memory instructions + // in inline PTX. + // + // In CUDA 11 and beyond, this replaces __nvvm_get_smem_pointer() [only available in 10.2]. + // + //__device__ size_t __cvta_generic_to_shared(void* ptr); + + /// CUTE helper to get SMEM pointer + return static_cast(__cvta_generic_to_shared(ptr)); + +#elif CUTE_NVVM_GET_SMEM_POINTER_ACTIVATED + + return __nvvm_get_smem_pointer(ptr); + +#elif defined(__HIP_DEVICE_COMPILE__) + + uint32_t smem_ptr; + + asm( + "{ .reg .u64 smem_ptr; cvta.to.shared.u64 smem_ptr, %1; cvt.u32.u64 %0, smem_ptr; }\n" + : "=r"(smem_ptr) : "l"(ptr)); + + return smem_ptr; + +#else + + + (void) ptr; + printf("ERROR: cast_smem_ptr_to_uint not supported but used.\n"); + return 0; + +#endif +} + +// +// Utility for pointer interfaces +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, + PtrS&& s, int_sequence, + PtrD&& d, int_sequence) +{ + return fn(s[Is]..., d[Id]...); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, + PtrA&& a, int_sequence, + PtrB&& b, int_sequence, + PtrC&& c, int_sequence) +{ + return fn(a[Ia]..., b[Ib]..., c[Ic]...); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, + PtrD&& d, int_sequence, + PtrA&& a, int_sequence, + PtrB&& b, int_sequence, + PtrC&& c, int_sequence) +{ + return fn(d[Id]..., a[Ia]..., b[Ib]..., c[Ic]...); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode_with_d_scaling(Fn fn, + PtrA&& a, int_sequence, + PtrB&& b, int_sequence, + PtrC&& c, int_sequence, + ParamType&& p0) +{ + return fn(a[Ia]..., b[Ib]..., c[Ic]..., p0); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode_with_d_scaling(Fn fn, + PtrD&& d, int_sequence, + PtrA&& a, int_sequence, + PtrB&& b, int_sequence, + PtrC&& c, int_sequence, + ParamType&& p0) +{ + return fn(d[Id]..., a[Ia]..., b[Ib]..., c[Ic]..., p0); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, PtrS&& s, PtrD&& d) +{ + return detail::explode(fn, + s, make_int_sequence{}, + d, make_int_sequence{}); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, PtrA&& a, PtrB&& b, PtrC&& c) +{ + return detail::explode(fn, + a, make_int_sequence{}, + b, make_int_sequence{}, + c, make_int_sequence{}); +} + +template +CUTE_HOST_DEVICE constexpr +void +explode(Fn fn, PtrD&& d, PtrA&& a, PtrB&& b, PtrC&& c) +{ + return detail::explode(fn, + d, make_int_sequence{}, + a, make_int_sequence{}, + b, make_int_sequence{}, + c, make_int_sequence{}); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_atom_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_atom_hip.hpp new file mode 100644 index 000000000..0d3cd05ff --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_atom_hip.hpp @@ -0,0 +1,787 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +#include + +#include + +namespace cute +{ + +template +struct Copy_Atom; + +template +struct Copy_Atom : Copy_Atom, T> +{}; + +template +struct Copy_Atom, T> + : Copy_Traits +{ + using Traits = Copy_Traits; + + // Bit and Thr layouts from the Copy_Traits + using ThrID = typename Traits::ThrID; + using BitLayoutSrc = typename Traits::SrcLayout; + using BitLayoutDst = typename Traits::DstLayout; + using BitLayoutRef = typename Traits::RefLayout; + + using ValType = T; + + using ValLayoutSrc = decltype(upcast::value>(BitLayoutSrc{})); + using ValLayoutDst = decltype(upcast::value>(BitLayoutDst{})); + using ValLayoutRef = decltype(upcast::value>(BitLayoutRef{})); + + CUTE_STATIC_ASSERT_V(size<0>(ValLayoutSrc{}) == size(ThrID{}), "CopyOperation is not valid for Src of ValType."); + CUTE_STATIC_ASSERT_V(size<0>(ValLayoutDst{}) == size(ThrID{}), "CopyOperation is not valid for Dst of ValType."); + CUTE_STATIC_ASSERT_V(size<0>(ValLayoutRef{}) == size(ThrID{}), "CopyOperation is not valid for Ref of ValType."); + + static constexpr int NumValSrc = size<1>(ValLayoutSrc{}); + static constexpr int NumValDst = size<1>(ValLayoutDst{}); + + // Additional Trait parameters/transformations + template + CUTE_HOST_DEVICE + auto + with(TraitsArgs&&... args) const { + auto traits = Traits::with(std::forward(args)...); + return Copy_Atom{traits}; + } + + // + // Tensor call interfaces + // + + // Check and call instruction, or recurse + template + CUTE_HOST_DEVICE + void + call(Tensor const& src, + Tensor & dst) const + { + static_assert(SLayout::rank == 1, "Expected rank-1 src tensor"); + static_assert(DLayout::rank == 1, "Expected rank-1 dst tensor"); + + if constexpr (is_constant::value || + is_constant::value) { + // Dispatch to unpack for instruction + return copy_unpack(*this, src, dst); + } else + if constexpr (is_tuple::value && + is_tuple::value) { + // If the size of the src/dst doesn't match the instruction, + // recurse this rank-1 layout by peeling off the mode + // ((A,B,C,...)) -> (A,B,C,...) + return copy(*this, tensor<0>(src), tensor<0>(dst)); + } else { + static_assert(sizeof(TS) < 0, "No instruction match and no recursion possible."); + } + } + + // Accept mutable temporaries + template + CUTE_HOST_DEVICE + void + call(Tensor const& src, + Tensor && dst) const + { + return call(src, dst); + } +}; + +// +// A tiling of copy atoms +// + +template +struct ThrCopy; + +template coord [Need not be 2D...] + class ShapeTile_MN> // coord space +struct TiledCopy : Copy_Atom +{ + // Layout information from the CopyAtom + using AtomThrID = typename Copy_Atom::ThrID; // thrid -> thr_idx + using AtomLayoutSrc = typename Copy_Atom::ValLayoutSrc; // (thr,val) -> offset + using AtomLayoutDst = typename Copy_Atom::ValLayoutDst; // (thr,val) -> offset + using AtomLayoutRef = typename Copy_Atom::ValLayoutRef; // (thr,val) -> offset + + using AtomNumThr = decltype(size<0>(AtomLayoutRef{})); + using AtomNumVal = decltype(size<1>(AtomLayoutRef{})); + + // Layout information for the TiledCopy + using Tiler_MN = ShapeTile_MN; + using TiledShape_MN = decltype(shape(ShapeTile_MN{})); + using TiledLayout_TV = LayoutCopy_TV; + using TiledNumThr = decltype(size<0>(TiledLayout_TV{})); + using TiledNumVal = decltype(size<1>(TiledLayout_TV{})); + + CUTE_STATIC_ASSERT_V(TiledNumThr{} % AtomNumThr{} == Int<0>{}, "TiledCopy uses too few thrs for selected CopyAtom"); + CUTE_STATIC_ASSERT_V(TiledNumVal{} % AtomNumVal{} == Int<0>{}, "TiledCopy uses too few vals for selected CopyAtom"); + + // Tile a tensor or a layout from shape + // (M,N,...) + // to shape + // ((ThrV,ThrX),FrgV,(RestM,RestN,...)) + // where + // ThrV: The threads local to a COPY_ATOM Src. + // ThrX: The threads tiled across COPY_ATOMs Src. + // FrgV: The values local to a COPY_ATOM Src. + // RestM: The values tiled in M. + // RestN: The values tiled in N. + template + CUTE_HOST_DEVICE constexpr static + auto + tidfrg_S(STensor&& stensor) + { + constexpr int R = remove_cvref_t::rank; + static_assert(R >= rank_v, "Rank of tensor to be partitioned too small."); + // Generalize the dimension checks for arbitrary rank + //CUTE_STATIC_ASSERT_V(size<0>(stensor) % size<0>(TiledShape_MNK{}) == Int<0>{}); + //CUTE_STATIC_ASSERT_V(size<1>(stensor) % size<1>(TiledShape_MNK{}) == Int<0>{}); + + return tile2thrfrg(zipped_divide(stensor,Tiler_MN{}), right_inverse(AtomLayoutRef{}).compose(AtomLayoutSrc{})); + } + + // Tile a tensor or a layout from shape + // (M,N,...) + // to shape + // ((ThrV,ThrX),FrgV,(RestM,RestN,...)) + // where + // ThrV: The threads local to a COPY_ATOM Dst. + // ThrX: The threads tiled across COPY_ATOMs Dst. + // FrgV: The values local to a COPY_ATOM Dst. + // RestM: The values tiled in M. + // RestN: The values tiled in N. + template + CUTE_HOST_DEVICE constexpr static + auto + tidfrg_D(DTensor&& dtensor) + { + constexpr int R = remove_cvref_t::rank; + static_assert(R >= rank_v, "Rank of tensor to be partitioned too small."); + // Generalize the dimension checks for arbitrary rank + //CUTE_STATIC_ASSERT_V(size<0>(stensor) % size<0>(TiledShape_MNK{}) == Int<0>{}); + //CUTE_STATIC_ASSERT_V(size<1>(stensor) % size<1>(TiledShape_MNK{}) == Int<0>{}); + + return tile2thrfrg(zipped_divide(dtensor,Tiler_MN{}), right_inverse(AtomLayoutRef{}).compose(AtomLayoutDst{})); + } + + // Tile a tensor or a layout from shape + // (Tile,(RestM,RestN,...)) + // to shape + // ((ThrV,ThrX),FrgV,(RestM,RestN,...)) + template + CUTE_HOST_DEVICE constexpr static + auto + tile2thrfrg(Tensor&& tensor, Ref2TrgLayout const& ref2trg) + { + // Take the thrs/vals that the atom is interested in + // NOTE: Assumes the AtomNumThr are contiguous and identity within TiledThrID + auto atom_layout_TV = zipped_divide(TiledLayout_TV{}, make_shape(AtomNumThr{}, AtomNumVal{})); + // ((atom_tid,atom_val),(rest_tid,rest_val)) -> (m,n) + + // Transform to the trg layout + auto trg_layout_TV = atom_layout_TV.compose(ref2trg, _); + // ((trg_tid,trg_val),(rest_tid,rest_val)) -> (m,n) + + // Transform the thrs mode from thrid to thr_idx + // NOTE: Assumes the AtomNumThr are contiguous and identity within TiledThrID + auto thrval2mn = coalesce(zip(trg_layout_TV), Shape<_1,Shape<_1,_1>>{}); + // ((trg_tid,rest_tid),(trg_val,rest_val)) -> (m,n) + + /// ================== + + // Transform the tile mode + auto tv_tensor = tensor.compose(thrval2mn, _); + // ((thrid,val),(RM,RN,...)) + + // Unfold and return + return tv_tensor(make_coord(_,_), _); + } + + // retile_S and retile_D assume they are working with the reference layout -- they are the same + template + CUTE_HOST_DEVICE constexpr static + auto + retile(Tensor&& tensor) + { + constexpr int R = remove_cvref_t::rank; + // Assert that AtomLayoutSrc|Dst is identity so we can skip the Ref transformation + + // Assume the first size<0>(tensor) elements are the first val_ids in TiledLayout_TV. + // Then, we only need the shape+layout of those size<0>(tensor) elements in TiledLayout_TV + // and that shape is what we gather from the other modes of tensor + + auto V = size<0>(tensor); + + auto frg_layout_mn = upcast(right_inverse(TiledLayout_TV{}).with_shape(TiledShape_MN{})); + // (m,n) -> v_idx -- The shape and order of the V inside of TiledLayout_TV + + auto frg_layout_v = zipped_divide(logical_product(make_layout(V), right_inverse(frg_layout_mn)), make_layout(AtomNumVal{})); + // (atom_vals,rest_vals) -> (v,m,n) + + /// ======= + + // Tile the tensor for TileFrg + auto t_tensor = zipped_divide(tensor, prepend(product_each(shape(frg_layout_mn)), V)); + // ((TileV,TileM,TileN,...),(1,RestM,RestN,...)) + + // Transform the tile mode + auto v_tensor = t_tensor.compose(frg_layout_v, _); + // ((atom_vals,rest_vals),(1,RM,RN,...)) + + // Unfold and return + return v_tensor(_, append(Int<0>{},_)); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutS_TV() + { + // (M,N) -> (M,N) + auto ref_S = make_layout(make_shape(TiledShape_MN{}, Int<1>{})); + // (thr_idx,val_idx) -> (M,N) + return tile2thrfrg(ref_S, right_inverse(AtomLayoutRef{}).compose(AtomLayoutSrc{}))(_,_,Int<0>{}); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutS_MN() + { + // (thr_idx,val_idx) -> (M,N) + auto layoutS_TV = get_layoutS_TV(); + // (M,K) -> (thr_idx,val_idx) + auto layoutS_MK = right_inverse(layoutS_TV).with_shape(TiledShape_MN{}); + + // athrid = (v,m,k) -> thr_idx + auto thrID_S = make_layout(size<0>(TiledLayout_TV{})); + + return cute::make_tuple(layoutS_MK, thrID_S); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutD_TV() + { + // (M,N) -> (M,N) + auto ref_D = make_layout(make_shape(TiledShape_MN{}, Int<1>{})); + // (thr_idx,val_idx) -> (M,N) + return tile2thrfrg(ref_D, right_inverse(AtomLayoutRef{}).compose(AtomLayoutDst{}))(_,_,Int<0>{}); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutD_MN() + { + // (thr_idx,val_idx) -> (M,N) + auto layoutD_TV = get_layoutD_TV(); + // (M,K) -> (thr_idx,val_idx) + auto layoutD_MK = right_inverse(layoutD_TV).with_shape(TiledShape_MN{}); + + // athrid = (v,m,k) -> thr_idx + auto thrID_D = make_layout(size<0>(TiledLayout_TV{})); + + return cute::make_tuple(layoutD_MK, thrID_D); + } + + template ::value)> + CUTE_HOST_DEVICE static + auto + get_slice(ThrIdx const& thr_idx) + { + return ThrCopy(thr_idx); + } + + template ::value)> + CUTE_HOST_DEVICE static + auto + get_thread_slice(ThrIdx const& thr_idx) + { + return get_slice(thr_idx); + } +}; + +template +struct ThrCopy +{ + ThrIdx thr_idx_; + + CUTE_HOST_DEVICE + ThrCopy(ThrIdx const& thr_idx) : thr_idx_(thr_idx) {} + + template + CUTE_HOST_DEVICE + auto + partition_S(STensor&& stensor) { + //static_assert(sizeof(typename remove_cvref_t::value_type) == sizeof(typename TiledCopy::ValType), + // "Expected ValType for tiling SrcTensor."); + auto thr_tensor = make_tensor(std::forward(stensor).data(), TiledCopy::tidfrg_S(stensor.layout())); + return thr_tensor(thr_idx_, _, repeat>(_)); + } + + template + CUTE_HOST_DEVICE + auto + partition_D(DTensor&& dtensor) { + //static_assert(sizeof(typename remove_cvref_t::value_type) == sizeof(typename TiledCopy::ValType), + // "Expected ValType for tiling DstTensor."); + auto thr_tensor = make_tensor(std::forward(dtensor).data(), TiledCopy::tidfrg_D(dtensor.layout())); + return thr_tensor(thr_idx_, _, repeat>(_)); + } + + template + CUTE_HOST_DEVICE static + auto + retile_S(STensor&& stensor) { + // static_assert(sizeof(typename remove_cvref_t::value_type) == sizeof(typename TiledCopy::ValType), + // "Expected ValType for tiling SrcTensor."); + return make_tensor(std::forward(stensor).data(), TiledCopy::retile(stensor.layout())); + } + + template + CUTE_HOST_DEVICE static + auto + retile_D(DTensor&& dtensor) { + // static_assert(sizeof(typename remove_cvref_t::value_type) == sizeof(typename TiledCopy::ValType), + // "Expected ValType for tiling DstTensor."); + return make_tensor(std::forward(dtensor).data(), TiledCopy::retile(dtensor.layout())); + } +}; + + +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_impl(Copy_Atom const& atom, + LayoutCopy_TV const&, + Tiler const&) +{ + return TiledCopy, LayoutCopy_TV, Tiler>{atom}; +} + +// +// These tile the Copy_Atom as a whole +// + +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_A(Copy_Atom const& copy_atom, + TiledMMA const& tiled_mma) +{ + using MNK = typename TiledMMA::TiledShape_MNK; + return make_tiled_copy_impl(copy_atom, tiled_mma.get_layoutA_TV(), make_shape(size<0>(MNK{}),size<2>(MNK{}))); +} + +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_B(Copy_Atom const& copy_atom, + TiledMMA const& tiled_mma) +{ + using MNK = typename TiledMMA::TiledShape_MNK; + return make_tiled_copy_impl(copy_atom, tiled_mma.get_layoutB_TV(), make_shape(size<1>(MNK{}),size<2>(MNK{}))); +} + +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_C(Copy_Atom const& copy_atom, + TiledMMA const& tiled_mma) +{ + using MNK = typename TiledMMA::TiledShape_MNK; + return make_tiled_copy_impl(copy_atom, tiled_mma.get_layoutC_TV(), make_shape(size<0>(MNK{}),size<1>(MNK{}))); +} + +// returns the smallest tiled copy that can retile LayoutC_TV +// for use with pipelined epilogues with subtiled stores +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_C_atom(Copy_Atom const& copy_atom, + TiledMMA const& tiled_mma) +{ + // Truncate the V-layout to just the Copy_Atom, keep the V-order + auto layoutC_TV = tiled_mma.get_layoutC_TV(); + auto copy_V = Int::NumValSrc>{}; + CUTE_STATIC_ASSERT_V(copy_V <= size<1>(layoutC_TV)); + auto layout_TV = composition(layoutC_TV, make_layout(make_shape(size<0>(layoutC_TV), copy_V))); + + // Recompute tiler and restride the TV layout for the new tiler + + // Tiler -- Find the active elements in the MMA tensor and generate a tiler to extract them + // Convert to the awkward by-mode tiler to preserve the modes of the tiled MMA + using MNK = typename TiledMMA::TiledShape_MNK; + auto mma_tiler = make_shape(size<0>(MNK{}),size<1>(MNK{})); + auto mma_zeros = repeat_like(mma_tiler, Int<0>{}); + + auto tiler = transform(make_seq{}, [&](auto i) { + return filter(composition(make_layout(mma_tiler, replace(mma_zeros, Int<1>{})), layout_TV)); + }); + + // Layout_TV -- Find the (tid,vid) -> tile coord transformation + // Apply the tiler to a reference and transform the codomain + // tile_coord -> mma_coord + auto tile2mma = composition(make_layout(mma_tiler), tiler); + + // (tid,vid) -> tile_coord + auto layout_tv = composition(left_inverse(tile2mma), layout_TV); + + + using MNK = typename TiledMMA::TiledShape_MNK; + return make_tiled_copy_impl(copy_atom, layout_tv, tiler); +} + +/** Produce a TiledCopy from logical thread and values layouts. + * The thread and value layouts map coordinates to thr_idx and val_idx. + * The product of these layouts is taken to produce the TV layout and the Tiler. + * Useful when threads and values need very specific mappings onto coordinates + * in the target tensors. + */ +template > +CUTE_HOST_DEVICE +auto +make_tiled_copy(Copy_Atom const& copy_atom, + ThrLayout const& thr_layout = {}, // (m,n) -> thr_idx + ValLayout const& val_layout = {}) // (m,n) -> val_idx +{ + constexpr int R = cute::max(rank_v, rank_v); + + auto thr_layout_mn = append(thr_layout, Layout<_1>{}); + auto val_layout_mn = append(val_layout, Layout<_1>{}); + + // Take the raked_products to compute the Layout_MN + auto layout_mn = raked_product(thr_layout_mn, val_layout_mn); + auto layout_tv = right_inverse(layout_mn).with_shape(make_shape(size(thr_layout), size(val_layout))); + // print("thr_layout: "); print(thr_layout_mn); print("\n"); + // print("val_layout: "); print(val_layout_mn); print("\n"); + // print("layout_mn : "); print(layout_mn); print("\n"); + // print("layout_tv : "); print(layout_tv); print("\n"); + + return make_tiled_copy_impl(copy_atom, layout_tv, product_each(shape(layout_mn))); +} + +/** Produce a TiledCopy from thread and value offset maps. + * The TV Layout maps threads and values to the codomain of the data_layout. + * It is verified that the intended codomain is valid within data_layout. + * Useful when threads and values don't care about owning specific coordinates, but + * care more about the vector-width and offsets between them. + */ +template +CUTE_HOST_DEVICE constexpr +auto +make_cotiled_copy(Copy_Atom const& copy_atom, + AtomTVLayout const& atom_tv_layout, // atom (thr,val) -> data addr + DataLayout const& data_layout) // coord -> data addr The target layout +{ + static_assert(is_static::value); + static_assert(is_static::value); + + // data addr -> data coord Append 1:0 so off-the-ends get the stride-0 + auto inv_data_layout = make_layout(left_inverse(data_layout), Layout<_1,_0>{}); + + // (tid,vid) -> data_coord + auto layout_tv_data = composition(inv_data_layout, atom_tv_layout); + + // Check validity + CUTE_STATIC_ASSERT_V(coalesce(composition(data_layout, layout<1>(layout_tv_data))) == coalesce(layout<1>(atom_tv_layout)), + "The memory pointed to by AtomTVLayout does not exist in the DataLayout."); + +#if 0 + if (thread0()) { + print("data_layout : "); print(data_layout); print("\n"); + print("atom_tv_layout : "); print(atom_tv_layout); print("\n"); + print("layout_tv_data : "); print(layout_tv_data); print("\n"); + } +#endif + + // + // Tiler -- Find the active elements in the DATA tensor and generate a tiler to extract them + // + + // Convert to the awkward by-mode tiler to preserve the modes of the tiled DATA + auto flat_data_shape = product_each(shape(data_layout)); + auto flat_data_zeros = repeat(Int<0>{}); + + auto tiler = transform(make_seq{}, [&](auto i) { + return filter(composition(make_layout(flat_data_shape, replace(flat_data_zeros, Int<1>{})), layout_tv_data)); + }); + + // + // Layout_TV -- Find the (tid,vid) -> tile coord transformation + // + + // Apply the tiler to a reference and transform the codomain + // tile_coord -> data_coord + auto tile2data = composition(make_layout(flat_data_shape), tiler); + + // (tid,vid) -> tile_coord + auto layout_tv = composition(left_inverse(tile2data), layout_tv_data); + +#if 0 + if (thread0()) { + print("tiler : "); print(tiler); print("\n"); + print("tile2data : "); print(tile2data); print("\n"); + print("layout_tv : "); print(layout_tv); print("\n"); + } +#endif + + return make_tiled_copy_impl(copy_atom, layout_tv, tiler); +} + +// Make a TiledCopy out of the copy_atom that matches the Src-Layout of tiled_copy +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_S(Copy_Atom const& copy_atom, + TiledCopy const& tiled_copy) +{ + return make_tiled_copy_impl(copy_atom, tiled_copy.get_layoutS_TV(), typename TiledCopy::Tiler_MN{}); +} + +// Make a TiledCopy out of the copy_atom that matches the Dst-Layout of tiled_copy +template +CUTE_HOST_DEVICE +auto +make_tiled_copy_D(Copy_Atom const& copy_atom, + TiledCopy const& tiled_copy) +{ + return make_tiled_copy_impl(copy_atom, tiled_copy.get_layoutD_TV(), typename TiledCopy::Tiler_MN{}); +} + +// +// Size +// + +// The logical size of a TileCopy +template +CUTE_HOST_DEVICE constexpr +auto +tile_size(TiledCopy const&) +{ + return size(typename TiledCopy::TiledShape_MN{}); +} + +// The number of threads involved in a TiledCopy +template +CUTE_HOST_DEVICE constexpr +auto +size(TiledCopy const&) +{ + return typename TiledCopy::TiledNumThr{}; +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE +void +print(Copy_Atom, T> const&) +{ + using Atom = Copy_Atom, T>; + print("Copy_Atom\n"); + print(" ThrID: "); print(typename Atom::ThrID{}); print("\n"); + print(" ValLayoutSrc: "); print(typename Atom::ValLayoutSrc{}); print("\n"); + print(" ValLayoutDst: "); print(typename Atom::ValLayoutDst{}); print("\n"); + print(" ValLayoutRef: "); print(typename Atom::ValLayoutRef{}); print("\n"); + print(" ValueType: %db\n", int(sizeof_bits::value)); +} + +template +CUTE_HOST_DEVICE +void +print(TiledCopy const& copy, char const* pad = "") +{ + using Copy = TiledCopy; + print("TiledCopy\n"); + print(" Tiler_MN: "); print(typename Copy::Tiler_MN{}); print("\n"); + print(" TiledLayout_TV: "); print(typename Copy::TiledLayout_TV{}); print("\n"); + print(static_cast(copy)); +} + +template +CUTE_HOST_DEVICE +void +print(ThrCopy const&) +{ + print(TiledCopy{}); +} + +template +CUTE_HOST_DEVICE +auto +print_latex(TiledCopy const& copy) +{ + auto [layoutS_MN, thrID_S] = copy.get_layoutS_MN(); + auto [layoutD_MN, thrID_D] = copy.get_layoutD_MN(); + + print_latex_copy(layoutS_MN, thrID_S, + layoutD_MN, thrID_D); +} + +// MNK Copy Layout to Latex TIKZ -- 8-value color coded by thread +template +CUTE_HOST_DEVICE +void +print_latex_copy(LayoutS const& S, ThrIDS const& TS, // (m,n) -> (tid,vid) and tid -> thr_idx + LayoutD const& D, ThrIDD const& TD) // (m,n) -> (tid,vid) and tid -> thr_idx +{ + CUTE_STATIC_ASSERT_V(rank(S) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(D) == Int<2>{}); + + assert(size<0>(S) == size<0>(D)); + assert(size<1>(S) == size<1>(D)); + + char const* latex_header = + "\\documentclass{standalone}\n" + "\\usepackage{tikz}\n" + "\\usetikzlibrary{external}\n" + "\\tikzexternalize\n" + "\\begin{document}\n" + "\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},box/.style={rectangle,draw=black,thick,minimum size=1cm,anchor=center}]\n\n"; + char const* latex_footer = + "\\end{tikzpicture}\n" + "\\end{document}\n"; + + char const* color_map[8] = {"{rgb,255:red,175;green,175;blue,255}", + "{rgb,255:red,175;green,255;blue,175}", + "{rgb,255:red,255;green,255;blue,175}", + "{rgb,255:red,255;green,175;blue,175}", + "{rgb,255:red,210;green,210;blue,255}", + "{rgb,255:red,210;green,255;blue,210}", + "{rgb,255:red,255;green,255;blue,210}", + "{rgb,255:red,255;green,210;blue,210}",}; + + // Header + printf("%% LayoutS: "); print(S); printf("\n"); + printf("%% ThrIDS : "); print(TS); printf("\n"); + printf("%% LayoutD: "); print(D); printf("\n"); + printf("%% ThrIDD : "); print(TD); printf("\n\n"); + + printf(latex_header); + + // S starting at 0,0 + for (int i = 0; i < size<0>(S); ++i) { + for (int j = 0; j < size<1>(S); ++j) { + int thrid = S(i,j) % size(TS); + int val_idx = S(i,j) / size(TS); + int thr_idx = TS(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + i, j, + thr_idx, val_idx); + } + } + + // D starting at 0,size<1>(S)+3 + for (int i = 0; i < size<0>(D); ++i) { + for (int j = 0; j < size<1>(D); ++j) { + int thrid = D(i,j) % size(TD); + int val_idx = D(i,j) / size(TD); + int thr_idx = TD(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + i, j + size<1>(S) + 3, + thr_idx, val_idx); + } + } + + // S Labels + for (int i = 0, j = -1; i < size<0>(S); ++i) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, i); + } + for (int j = 0, i = -1; j < size<1>(S); ++j) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, j); + } + // D Labels + for (int i = 0, j = size<1>(D); i < size<0>(S); ++i) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j + size<1>(S) + 3, i); + } + for (int j = 0, i = -1; j < size<1>(D); ++j) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j + size<1>(S) + 3, j); + } + + // Footer + printf(latex_footer); +} + +} // end namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +// Config +#if (__CUDACC_VER_MAJOR__ >= 12) +# define CUTE_COPY_ATOM_TMA_SM90_ENABLED +#endif + +#if defined(CUTE_COPY_ATOM_TMA_SM90_ENABLED) +#include +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_gfx928_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_gfx928_hip.hpp new file mode 100644 index 000000000..1bce1aed2 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_gfx928_hip.hpp @@ -0,0 +1,279 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +namespace cute +{ +/*****************************************************************************************/ +/*****************************************************************************************/ +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, + Stride<_128, _1>>; + + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape<_32,_2,_2>>, + Stride,Stride<_1,_1024,_512>>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; +/*****************************************************************************************/ +/*****************************************************************************************/ +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride< Stride<_128,_512>,_1>>; + // using SrcLayout = Layout,Shape,_1>>, + // Stride,Stride,_0>>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape<_16,_2,_4>>, + Stride,Stride<_1,_256,_512>>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride< Stride<_128,_512>,_1>>; + // using SrcLayout = Layout,Shape,_1>>, + // Stride,Stride,_0>>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape<_16,_4,_2>>, + Stride,Stride<_1,_512,_256>>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + + +template <> +struct Copy_Traits +{ + using ThrID = Layout<_64>; + + // // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride< Stride<_128,_512>,_1>>; + // // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape<_16,_2,_4>>, + Stride,Stride<_1,_16,_512>>>; + // // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; +/*****************************************************************************************/ +/* not complete,to do... */ +/*****************************************************************************************/ +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, _128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout, Shape<_8, _2, _8>>, + Stride, Stride<_1, _128, _256>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; +/*****************************************************************************************/ +/*****************************************************************************************/ + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// used for flash attention // +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, _128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout, _128>, + Stride, _1>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + // using DstLayout = Layout, Shape<_16, _2, _4>>, + // Stride, Stride<_1, _256, _2048>>>; + using DstLayout = Layout, Shape<_2, _4>>, + Stride, Stride<_256, _2048>>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, _128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape<_16,_2,_4>>, + Stride,Stride<_1,_256,_512>>>; + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + // Map from (src-thr,src-val) to bit + // using SrcLayout = Layout,_128>, + // Stride< Stride<_128,_512>,_1>>; + + // using SrcLayout = Layout, _2>, _128>, + // Stride, _4096>, _1>>; + + using SrcLayout = Layout, _16>, _128>, + Stride, _256>, _1>>; + + // Map from (dst-thr,dst-val) to bit + // using DstLayout = Layout,Shape<_16,_4,_2>>, + // Stride,Stride<_1,_512,_256>>>; + + using DstLayout = Layout, Shape<_16, _4, _2>>, + Stride, Stride<_1, _256, _4096>>>; + + + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + + using ThrID = Layout<_64>; + + using SrcLayout = Layout, _8>, _128>, + Stride, _512>, _1>>; + using DstLayout = Layout, Shape<_32, _2, _2>>, + Stride, Stride<_1, _512, _4096>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; + + // bool using_lds_offset = true; +}; + +} + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_hip.hpp new file mode 100644 index 000000000..f8382ceae --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_hip.hpp @@ -0,0 +1,176 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +/** + * concept Copy_Traits + * { + * using ThrID = // Logical thread id (tid) -> tidx + * + * using SrcLayout = // (Logical src thread id (tid), Logical src value id (vid)) -> bit + * using DstLayout = // (Logical dst thread id (tid), Logical dst value id (vid)) -> bit + * using RefLayout = // (Logical ref thread id (tid), Logical ref value id (vid)) -> bit + * }; + * + * The abstract bit ordering of the Copy_Traits (the codomain of SrcLayout, DstLayout, and RefLayout) + * is arbitrary and only used to construct maps + * (ref-tid,ref-vid) -> (src-tid,src-vid) + * (ref-tid,ref-vid) -> (dst-tid,dst-vid) + * in TiledCopy. The Layout_TV in TiledCopy is in accordance with the RefLayout of a Traits, then mapped to + * the Src or Dst (tid,vid) representation on demand. + * + */ + +template +struct Copy_Traits +{ + static_assert(sizeof(CopyOperation) == 0, "Copy_Traits not implemented for this Copy_Operation."); +}; + +namespace detail { + + template + struct supports_lds_offset { static constexpr bool value = false; }; + + template + struct supports_lds_offset().using_lds_offset)>> { static constexpr bool value = true; }; + +} + +template +struct Copy_Traits> +{ + // Logical thread id to thread idx (one-thread) + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout::value>>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout::value>>>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (one-thread) + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, Stride<_0,_0>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout, Stride<_0,_0>>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +// +// Generic copy_unpack for any Copy_Traits +// +template +CUTE_HOST_DEVICE constexpr +void +copy_unpack(Copy_Traits const&, + Tensor const& src, + Tensor & dst) +{ + // Specializations can generalize on these checks + //static_assert(is_smem::value, "Expected smem for this Copy_Traits"); + //static_assert(is_rmem::value, "Expected rmem for this Copy_Traits"); + + using RegistersSrc = typename Operation::SRegisters; + using RegistersDst = typename Operation::DRegisters; + using RegTypeSrc = typename remove_extent::type; + using RegTypeDst = typename remove_extent::type; + constexpr int RegNumSrc = extent::value; + constexpr int RegNumDst = extent::value; + + Tensor rS = recast(src); + Tensor rD = recast(dst); + + CUTE_STATIC_ASSERT_V(size(rS) == Int{}, + "In CopyAtom, src layout doesn't vectorize into registers. This src layout is incompatible with this tiled copy."); + CUTE_STATIC_ASSERT_V(size(rD) == Int{}, + "In CopyAtom, dst layout doesn't vectorize into registers. This dst layout is incompatible with this tiled copy."); + + using CopyTraits = Copy_Traits; + + // if constexpr (detail::supports_lds_offset::value) + // { + // detail::explode(Operation::copy, + // rS, make_int_sequence{}, + // rD, make_int_sequence{}, + // lds_offset); + // } + // else + { + detail::explode(Operation::copy, + rS, make_int_sequence{}, + rD, make_int_sequence{}); + } + +} + +// +// Accept mutable temporaries +// + +template +CUTE_HOST_DEVICE constexpr +void +copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor && dst) +{ + copy_unpack(traits, src, dst); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm75_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm75_hip.hpp new file mode 100644 index 000000000..ef6e0ab18 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm75_hip.hpp @@ -0,0 +1,149 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +namespace cute +{ + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout, + Stride<_32, _1>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>, + Stride<_32,Stride< _1,_1024>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, + Stride<_128, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>, + Stride<_32,Stride< _1,_1024>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape <_16, _2>>, + Stride,Stride< _1,_128>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout,_128>, + Stride, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape <_16, _2, _2>>, + Stride,Stride< _1,_128,_1024>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, + Stride<_128, _1>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout,Shape <_16, _2, _4>>, + Stride,Stride< _1,_128,_1024>>>; + + // Reference map from (thr,val) to bit + using RefLayout = DstLayout; +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm80_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm80_hip.hpp new file mode 100644 index 000000000..93d109094 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm80_hip.hpp @@ -0,0 +1,104 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +namespace cute +{ + +template +struct Copy_Traits> +{ + // Logical thread id to thread idx (one-thread) + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout::value>>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout::value>>>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template +struct Copy_Traits> +{ + // Logical thread id to thread idx (one-thread) + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout::value>>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout::value>>>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// Element copy selector +template +CUTE_HOST_DEVICE constexpr +auto +select_elementwise_copy(SrcTensor const&, DstTensor const&) +{ + using SrcType = typename SrcTensor::value_type; + using DstType = typename DstTensor::value_type; + +#if defined(CUTE_ARCH_CP_ASYNC_SM80_ENABLED) + if constexpr (is_gmem::value && is_smem::value && + sizeof(SrcType) == sizeof(DstType) && + (sizeof(SrcType) == 4 || sizeof(SrcType) == 8 || sizeof(SrcType) == 16)) + { + return SM80_CP_ASYNC_CACHEALWAYS{}; + } else { + return UniversalCopy{}; + } + + CUTE_GCC_UNREACHABLE; +#else + return UniversalCopy{}; +#endif +} + +} diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_hip.hpp new file mode 100644 index 000000000..f8669711b --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_hip.hpp @@ -0,0 +1,138 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +#include + +#include + +namespace cute +{ + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +template <> +struct Copy_Traits +{ + // Logical thread id to thread idx (warp) + using ThrID = Layout<_32>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = typename Copy_Traits::DstLayout; + // Map from (dst-thr,dst-val) to bit + using DstLayout = typename Copy_Traits::SrcLayout; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_hip.hpp new file mode 100644 index 000000000..4251ffb3d --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_hip.hpp @@ -0,0 +1,1033 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if !defined(__CUDACC_RTC__) +#include +#endif + +#include +#include +#include + +#include + +namespace cute +{ + +template +struct AuxTmaParams { + using GmemStrides = GmemStrides_; + GmemStrides g_stride_; +}; + +////////////////////////////////////////////////////////////////////////////// +///////////////////////////// TMA_LOAD /////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_OP : SM90_TMA_LOAD {}; + +// The executable SM90_TMA_LOAD with tma_desc and tma_mbar +template +struct Copy_Traits +{ + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_TMA_LOAD arguments + TmaDescriptor const& tma_desc_; + uint64_t& tma_load_mbar_; + + template + CUTE_HOST_DEVICE constexpr + void + copy_unpack_(void const* const dst_ptr, + Coord const& src_coord, seq) const + { + SM90_TMA_LOAD::copy(&tma_desc_, tma_load_mbar_, + dst_ptr, get(src_coord)...); + } + + // This is the copy_unpack dispatch for this Copy_Traits + // Src needs to be a gmem tensor with TmaCoordIterator .data() + // Dst needs to be a smem tensor + template + CUTE_HOST_DEVICE friend constexpr + void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) + { + static_assert(is_smem::value, "Expected smem dst for SM90_TMA_LOAD"); + + traits.copy_unpack_(cute::raw_pointer_cast(dst.data()), src.data().coord_, tuple_seq{}); + } +}; + +// The non-executable SM90_TMA_LOAD with tma_desc and no tma_mbar +// Use .with(tma_mbar) to construct an executable version +template +struct Copy_Traits +{ + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_TMA_LOAD arguments + TmaDescriptor tma_desc_; + using AuxParams = AuxParams_; + AuxParams aux_params_; + + // Return TmaDescriptor/TensorMap + CUTE_HOST_DEVICE constexpr + TmaDescriptor const* + get_tma_descriptor() const { + return &tma_desc_; + } + + // Construct an executable SM90_TMA_LOAD with tma_mbar + CUTE_HOST_DEVICE constexpr + Copy_Traits + with(uint64_t& tma_mbar, uint16_t const& multicast_mask = 0) const { + // We accept multicast_mask here to keep the API for both atoms consistent + // assert(multicast_mask == 0); + (void) multicast_mask; + return {tma_desc_, tma_mbar}; + } + + // Generate the TMA coord tensor + template + CUTE_HOST_DEVICE constexpr + auto + get_tma_tensor(GShape const& g_shape) const { + static_assert(is_congruent::value); + return make_counting_tensor(make_layout(g_shape, aux_params_.g_stride_)); + } + + // Don't try to execute a copy with SM90_TMA_LOAD before calling .with() + template + CUTE_HOST_DEVICE friend constexpr void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) = delete; +}; + +////////////////////////////////////////////////////////////////////////////// +///////////////////////////// TMA_LOAD_MULTICAST ///////////////////////////// +////////////////////////////////////////////////////////////////////////////// + +struct SM90_TMA_LOAD_MULTICAST_OP : SM90_TMA_LOAD_MULTICAST {}; + +template +struct Copy_Traits +{ + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_TMA_LOAD_MULTICAST arguments + TmaDescriptor const& tma_desc_; + uint64_t& tma_load_mbar_; + uint16_t const& multicast_mask_; + + template + CUTE_HOST_DEVICE constexpr + void + copy_unpack_(void const* const dst_ptr, + Coord const& src_coord, seq) const + { + SM90_TMA_LOAD_MULTICAST::copy(&tma_desc_, tma_load_mbar_, multicast_mask_, + dst_ptr, get(src_coord)...); + } + + template + CUTE_HOST_DEVICE friend constexpr + void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) + { + static_assert(is_smem::value, "Expected smem dst for SM90_TMA_LOAD_MULTICAST"); + + traits.copy_unpack_(cute::raw_pointer_cast(dst.data()), src.data().coord_, tuple_seq{}); + } +}; + +template +struct Copy_Traits +{ + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_TMA_LOAD_MULTICAST arguments + TmaDescriptor tma_desc_; + using AuxParams = AuxParams_; + AuxParams aux_params_; + + // Return TmaDescriptor/TensorMap + CUTE_HOST_DEVICE constexpr + TmaDescriptor const* + get_tma_descriptor() const { + return &tma_desc_; + } + + // Construct an executable SM90_TMA_LOAD_MULTICAST with tma_mbar + CUTE_HOST_DEVICE constexpr + Copy_Traits + with(uint64_t& tma_load_mbar, uint16_t const& multicast_mask) const { + return {tma_desc_, tma_load_mbar, multicast_mask}; + } + + // Generate the TMA coord tensor + template + CUTE_HOST_DEVICE constexpr + auto + get_tma_tensor(GShape const& g_shape) const { + static_assert(is_congruent::value); + return make_counting_tensor(make_layout(g_shape, aux_params_.g_stride_)); + } + + // Don't try to execute a copy with SM90_TMA_LOAD_MULTICAST before calling .with() + template + CUTE_HOST_DEVICE friend constexpr void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) = delete; +}; + +////////////////////////////////////////////////////////////////////////////// +///////////////////////////// TMA_STORE ////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// + +// The executable SM90_TMA_STORE with tma_desc +template +struct Copy_Traits +{ + using ThrID = Layout<_1>; + + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_TMA_STORE arguments + TmaDescriptor tma_desc_; + using AuxParams = AuxParams_; + AuxParams aux_params_; + + // Return TmaDescriptor/TensorMap + CUTE_HOST_DEVICE constexpr + TmaDescriptor const* + get_tma_descriptor() const { + return &tma_desc_; + } + + // Generate the TMA coord tensor + template + CUTE_HOST_DEVICE constexpr + auto + get_tma_tensor(GShape const& g_shape) const { + static_assert(is_congruent::value); + return make_counting_tensor(make_layout(g_shape, aux_params_.g_stride_)); + } + + template + CUTE_HOST_DEVICE constexpr + void + copy_unpack_(void const* const src_ptr, + Coord const& dst_coord, seq) const + { + SM90_TMA_STORE::copy(&tma_desc_, + src_ptr, get(dst_coord)...); + } + + // This is the copy_unpack dispatch for this Copy_Traits + // Src needs to be a smem tensor + // Dst needs to be a gmem tensor with TmaCoordIterator .data() + template + CUTE_HOST_DEVICE friend constexpr + void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) + { + static_assert(is_smem::value, "Expected smem src for SM90_TMA_STORE"); + //static_assert(is_gmem::value, "Expected gmem dst for SM90_TMA_STORE"); // TMA spoofed src tensor + + traits.copy_unpack_(cute::raw_pointer_cast(src.data()), dst.data().coord_, tuple_seq{}); + } +}; + +////////////////////////////////////////////////////////////////////////////// +///////////////////////////// BULK COPY ////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// + +template +struct Copy_Traits +{ + static_assert(int32_t(NumBitsPerTMA::value / 8) % 16 == 0, + "Bulk Copy requires copy vector size align to 16B."); + + using ThrID = Layout<_1>; + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_BULK_COPY_G2S arguments + // 0: uint64_t* bulk_load_memory_barrier + cute::tuple bulk_load_mbar_; + + template + CUTE_HOST_DEVICE friend constexpr + void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) + { + static_assert(is_same, cute::tuple>::value, + "Extra arguments not set. Set .with() before use."); + static_assert(is_gmem::value, "Expected gmem src for SM90_BULK_COPY_G2S"); + static_assert(is_smem::value, "Expected smem dst for SM90_BULK_COPY_G2S"); + SM90_BULK_COPY_G2S::copy(src.data().get(), *get<0>(traits.bulk_load_mbar_), + dst.data().get(), int32_t(NumBitsPerTMA::value / 8)); + } + + // Record the memory barrier for the instruction + CUTE_HOST_DEVICE constexpr + Copy_Traits + with(uint64_t& bulk_mbar) const { + return {{&bulk_mbar}}; + } +}; + +template +struct Copy_Traits +{ + static_assert(int32_t(NumBitsPerTMA::value / 8) % 16 == 0, + "Bulk Copy requires copy vector size align to 16B."); + + using ThrID = Layout<_1>; + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout>; + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + template + CUTE_HOST_DEVICE friend constexpr + void + copy_unpack(Copy_Traits const& traits, + Tensor const& src, + Tensor & dst) + { + static_assert(is_smem::value, "Expected smem src for SM90_BULK_COPY_S2G"); + static_assert(is_gmem::value, "Expected gmem dst for SM90_BULK_COPY_S2G"); + SM90_BULK_COPY_S2G::copy(src.data().get(), dst.data().get(), int32_t(NumBitsPerTMA::value / 8)); + } +}; + +// +// Placeholder for the bulk copy algorithm's default, auto-vectorizing behavior +// + +template +struct Copy_Traits +{ + // Logical thread id to thread idx (one-thread) + using ThrID = Layout<_1>; + // Map from (src-thr,src-val) to bit + using SrcLayout = Layout, Stride<_0,_0>>; + // Map from (dst-thr,dst-val) to bit + using DstLayout = Layout, Stride<_0,_0>>; + // Reference map from (thr,val) to bit + using RefLayout = SrcLayout; + + // SM90_UBULK_COPY arguments + // 0: uint64_t* bulk_load_memory_barrier [if this is a BULK_LOAD_G2S] + cute::tuple opargs_; + + // Record the memory barrier for the instruction + CUTE_HOST_DEVICE constexpr + Copy_Traits + with(uint64_t& bulk_mbar) const { + return {{&bulk_mbar}}; + } +}; + +// +// MAKE_TMA_COPY and related +// + +namespace detail { + +// Custom version of coalesce that greedily combines modes only up to size-256 +// Look at each element and the back of the stack (in order of priority) +// back(NewLayout) get(OldLayout) +// s0:d0 _1:d1 => continue +// _1:d0 s1:d1 => replace_back s1:d1 +// s0:d0 s1:s0*d0 => replace_back s0*s1:d0 if s0*s1 <= 256 +// s0:d0 s1:d1 => append s1:d1 +// +// @pre OldShape and OldStride are flat +template +CUTE_HOST_DEVICE constexpr +auto +coalesce_256_impl(OldShape const& old_shape, OldStride const& old_stride, + NewShape const& new_shape, NewStride const& new_stride) +{ + if constexpr (I == rank_v) { + // Base case, we're done + if constexpr (is_constant<1, NewShape>::value) { + return Layout<_1,_0>{}; + } else { + return Layout{new_shape,new_stride}; + } + } else if constexpr (is_constant<1, decltype(get(old_shape))>::value) { + // shape(layout) == _1, skip it and continue + return coalesce_256_impl(old_shape, old_stride, new_shape, new_stride); + } else if constexpr (is_constant<1, NewShape>::value) { + // Replace our shape-1 with anything (Can only happen on input new_shape/new_stride) + return coalesce_256_impl(old_shape, old_stride, get(old_shape), get(old_stride)); + } else if constexpr (is_constant(old_stride) && + get(old_shape) * back(new_shape) <= Int<256>{})>::value) { + // Merge modes because the shapes and strides match and the merge is 256 or less + return coalesce_256_impl(old_shape, old_stride, + replace_back(new_shape, get(old_shape) * back(new_shape)), + new_stride); + } else { + // Can't replace or merge, so append a new mode + return coalesce_256_impl(old_shape, old_stride, + append(new_shape, get(old_shape)), + append(new_stride, get(old_stride))); + } + + CUTE_GCC_UNREACHABLE; +} + +// Combine all the modes that are possible to combine +// Does not respect the profile of the layout, but does preserve total size +template +CUTE_HOST_DEVICE constexpr +auto +coalesce_256(Layout const& layout) +{ + auto flat_shape = flatten(layout.shape()); + auto flat_stride = flatten(layout.stride()); + return coalesce_256_impl<1>(flat_shape, flat_stride, get<0>(flat_shape), get<0>(flat_stride)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +coalesce_256(Tensor const& tensor) +{ + return make_tensor(tensor.data(), coalesce_256(tensor.layout())); +} + + +// Use a smem_inv_h to read through the GMEM tensor +// and construct a TMA Descriptor for the resulting instruction +// At the same time, construct the Tma Tensor's Stride to generate +// the TMA coordinates that the instruction consumes. +// +template +CUTE_HOST_RTC +auto +make_tma_copy_desc(Tensor const& gtensor, // The original GMEM Tensor + Layout const& smem_inv_h, // smem_idx to hier gmode + Swizzle const& swizzle) // Swizzle fn on smem_idx +{ + // The smem vector is the same units as gtensor, so compose first and then recast + // tma_val_idx:gmem_strides + Tensor tile_gstride = recast(gtensor.compose(smem_inv_h)); + // Coalesce modes up to size-256 (the maximum TMA box extent in units of TmaInternalType) + // tma_box_shape:gmem_strides + Tensor tma_gstride = coalesce_256(tile_gstride); + + // Perform the tiling to the gmem vector again, but with indirections to the gtensor modes + auto gbasis = make_identity_layout(shape(gtensor)); + auto tile_gbasis_tmp = gbasis.compose(smem_inv_h); + + // Instead of the recast (gbasis doesn't have type info), replace the shape with the already-recasted shape + // tma_box_shape:gmem_mode + auto tile_gbasis = make_layout(shape(tile_gstride), stride(tile_gbasis_tmp)); + + // Recast the original tensor for shape inspections + auto gtensor_T = recast(gtensor); + + // Find missing bases that don't appear in tile_gbasis + // NOTE This is essentially ArithmeticTuple complement... + // NOTE in pursuit of implementing an ArithmeticTuple logical_divide for smem_inv_h + auto tile_gbasis_remaining_stride = filter_tuple(flatten(shape (gtensor_T)), flatten(stride(gtensor_T)), + flatten(stride(gbasis)), + [&](auto s, auto d, auto e) + { + if constexpr (is_constant<1, decltype(s)>::value || is_constant<0, decltype(d)>::value) { + return cute::tuple<>{}; // If size-1 or stride-0, then don't append + } else { + using E = decltype(e); + auto has_e = any_of(stride(tile_gbasis), [] (auto tb) { return tb == E{}; }); + if constexpr (decltype(has_e)::value) { + return cute::tuple<>{}; // If d was found, then don't append + } else { + return cute::tuple(e); // Else, this is missing so append + } + } + }); + auto tile_gbasis_remaining_rank = rank(tile_gbasis_remaining_stride); + + // "Coalesce" the tile basis into a compatible shape with the tma + auto tma_gbasis_tile = tile_gbasis.compose(make_layout(wrap(shape(tma_gstride)))); + + // Append the remaining basis modes that contribute to the TMA with size-1 + auto tma_gbasis_full = make_layout(tuple_cat(wrap( shape(tma_gbasis_tile)), wrap(repeat(Int<1>{}))), + tuple_cat(wrap(stride(tma_gbasis_tile)), wrap(tile_gbasis_remaining_stride))); + + // Group the trailing modes to make this max rank-5 -- TMA rank limitation + // tma_box_shape:gmem_mode + auto tma_gbasis = group(tma_gbasis_full); + +#if 0 + print("smem_inv_h : "); print(smem_inv_h); print("\n"); + print("gtensor : "); print(gtensor); print("\n"); + print("tile_gstride : "); print(tile_gstride); print("\n"); + print("tma_gstride : "); print(tma_gstride); print("\n"); + print("gbasis : "); print(gbasis); print("\n"); + print("tile_gbasis : "); print(tile_gbasis); print("\n"); + print("tma_gbasis : "); print(tma_gbasis); print("\n"); +#endif + + // + // TMA desc creation + // + + constexpr int tma_dim = decltype(rank(tma_gbasis))::value; + + // + // TMA gmem desc info + // + + void* gmem_address = (void*) raw_pointer_cast(gtensor_T.data()); + auto gmem_layout = gtensor_T.layout(); + + cute::array gmem_prob_shape = {1,1,1,1,1}; + cute::array gmem_prob_stride = {0,0,0,0,0}; + // Use the indirections in tma_gbasis in the values of flat_glayout to construct the gmem shapes/strides + for_each(make_seq{}, [&](auto i) { + for_each(stride(tma_gbasis), [&](auto ej) { + // Problem stride + uint64_t stride_j = ceil_div(basis_get(ej, stride(gmem_layout)) * sizeof_bits_v, 8); + uint64_t old_stride = gmem_prob_stride[i]; + gmem_prob_stride[i] = gcd(gmem_prob_stride[i], stride_j); + + // Problem shape + uint64_t shape_j = basis_get(ej, shape(gmem_layout)); + if (gmem_prob_stride[i] != 0) { + // Recurrence: g_shape = (s_i - 1) * (d_i / gcd_j d_j) + 1 + gmem_prob_shape[i] = (gmem_prob_shape[i]-1) * (old_stride / gmem_prob_stride[i]) + + (shape_j-1) * (stride_j / gmem_prob_stride[i]) + + 1; + } else { + gmem_prob_shape[i] = shape_j; + } + }); + }); + + assert((reinterpret_cast(gmem_address) & 0b1111) == 0); // Address must be 16B-aligned + + assert(gmem_prob_shape[0] >= (uint64_t(1))); // Size must be min 1 + assert(gmem_prob_shape[0] <= (uint64_t(1) << 32)); // Size must be max 2^32 + assert(gmem_prob_shape[1] >= (uint64_t(1))); // Size must be min 1 + assert(gmem_prob_shape[1] <= (uint64_t(1) << 32)); // Size must be max 2^32 + assert(gmem_prob_shape[2] >= (uint64_t(1))); // Size must be min 1 + assert(gmem_prob_shape[2] <= (uint64_t(1) << 32)); // Size must be max 2^32 + assert(gmem_prob_shape[3] >= (uint64_t(1))); // Size must be min 1 + assert(gmem_prob_shape[3] <= (uint64_t(1) << 32)); // Size must be max 2^32 + assert(gmem_prob_shape[4] >= (uint64_t(1))); // Size must be min 1 + assert(gmem_prob_shape[4] <= (uint64_t(1) << 32)); // Size must be max 2^32 + + // TMA descriptor does not store the zeroth stride and assumes it is 1 (TmaInternalType element). + assert(gmem_prob_stride[0] == sizeof(TmaInternalType) && "Majorness of smem doesn't match majorness of gmem"); + + assert((gmem_prob_stride[1]) < (uint64_t(1) << 40)); // Stride must be max 2^40 + assert((gmem_prob_stride[1] & 0b1111) == 0); // Stride must be multiple of 16B (128b) + assert((gmem_prob_stride[2]) < (uint64_t(1) << 40)); // Stride must be max 2^40 + assert((gmem_prob_stride[2] & 0b1111) == 0); // Stride must be multiple of 16B (128b) + assert((gmem_prob_stride[3]) < (uint64_t(1) << 40)); // Stride must be max 2^40 + assert((gmem_prob_stride[3] & 0b1111) == 0); // Stride must be multiple of 16B (128b) + assert((gmem_prob_stride[4]) < (uint64_t(1) << 40)); // Stride must be max 2^40 + assert((gmem_prob_stride[4] & 0b1111) == 0); // Stride must be multiple of 16B (128b) + + // + // TMA smem desc info + // + + cute::array smem_box_shape = {1,1,1,1,1}; + cute::array smem_box_stride = {1,1,1,1,1}; + // The smem box is simply given by the sizes of the modes in tma_gbasis + for_each(make_seq{}, [&](auto i) { + smem_box_shape[i] *= size(tma_gbasis); + }); + + assert(smem_box_shape[0] >= (uint32_t(1))); // Size must be min 1 + assert(smem_box_shape[0] <= (uint32_t(1) << 8)); // Size must be max 2^8 = 256 + assert(smem_box_shape[1] >= (uint32_t(1))); // Size must be min 1 + assert(smem_box_shape[1] <= (uint32_t(1) << 8)); // Size must be max 2^8 = 256 + assert(smem_box_shape[2] >= (uint32_t(1))); // Size must be min 1 + assert(smem_box_shape[2] <= (uint32_t(1) << 8)); // Size must be max 2^8 = 256 + assert(smem_box_shape[3] >= (uint32_t(1))); // Size must be min 1 + assert(smem_box_shape[3] <= (uint32_t(1) << 8)); // Size must be max 2^8 = 256 + assert(smem_box_shape[4] >= (uint32_t(1))); // Size must be min 1 + assert(smem_box_shape[4] <= (uint32_t(1) << 8)); // Size must be max 2^8 = 256 + + assert(smem_box_stride[0] >= (uint32_t(1))); // Stride must be min 1 + assert(smem_box_stride[0] <= (uint32_t(8))); // Stride must be max 2^3 = 8 + assert(smem_box_stride[1] >= (uint32_t(1))); // Stride must be min 1 + assert(smem_box_stride[1] <= (uint32_t(8))); // Stride must be max 2^3 = 8 + assert(smem_box_stride[2] >= (uint32_t(1))); // Stride must be min 1 + assert(smem_box_stride[2] <= (uint32_t(8))); // Stride must be max 2^3 = 8 + assert(smem_box_stride[3] >= (uint32_t(1))); // Stride must be min 1 + assert(smem_box_stride[3] <= (uint32_t(8))); // Stride must be max 2^3 = 8 + assert(smem_box_stride[4] >= (uint32_t(1))); // Stride must be min 1 + assert(smem_box_stride[4] <= (uint32_t(8))); // Stride must be max 2^3 = 8 + + // + // Construct the descriptor + // + + TmaDescriptor tma_desc = {0}; + + // + // TMA general info + // + + #if (__CUDACC_VER_MAJOR__ >= 12) && !defined(__CUDACC_RTC__) + + CUtensorMapDataType tma_format = TMA::to_CUtensorMapDataType(); + CUtensorMapInterleave tma_interleave = CU_TENSOR_MAP_INTERLEAVE_NONE; + CUtensorMapL2promotion tma_l2Promotion = CU_TENSOR_MAP_L2_PROMOTION_L2_128B; + CUtensorMapFloatOOBfill tma_oobFill = CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE; + + // TMA smem swizzle type + CUtensorMapSwizzle smem_swizzle = TMA::to_CUtensorMapSwizzle(get_tma_swizzle_bits(swizzle)); + hipError_t result = cuTensorMapEncodeTiled( + &tma_desc, + tma_format, + tma_dim, + gmem_address, + gmem_prob_shape.data(), + gmem_prob_stride.data() + 1, // gmem_prob_stride[0] implicitly 1 + smem_box_shape.data(), + smem_box_stride.data(), + tma_interleave, + smem_swizzle, + tma_l2Promotion, + tma_oobFill); + + if (result != hipSuccess) { + std::cerr << "TMA Desc Addr: " << &tma_desc + << "\nformat " << tma_format + << "\ndim " << tma_dim + << "\ngmem_address " << gmem_address + << "\nglobalDim " << gmem_prob_shape + << "\nglobalStrides " << gmem_prob_stride + << "\nboxDim " << smem_box_shape + << "\nelementStrides " << smem_box_stride + << "\ninterleave " << tma_interleave + << "\nswizzle " << smem_swizzle + << "\nl2Promotion " << tma_l2Promotion + << "\noobFill " << tma_oobFill << std::endl; + std::cerr << "Error: Failed to initialize the TMA descriptor " << result << std::endl; + assert(false); + } + + #endif // (__CUDACC_VER_MAJOR__ >= 12) && !defined(__CUDACC_RTC__) + // Finally, get the inverse permutation of the E bases for the mocked gmem stride + // NOTE This is essentially ArithmeticTuple inverse... + auto gmem_stride_bases = transform_leaf(stride(gbasis), [&](auto ei) { + auto si = basis_get(ei, shape(gmem_layout)); + auto di = basis_get(ei, stride(gmem_layout)); + if constexpr (is_constant<1, decltype(si)>::value || is_constant<0, decltype(di)>::value) { + return Int<0>{}; // If size-1 or stride-0, return arithmetic identity -- no contribution to the TMA + } else { + auto tma_gbasis_stride = stride(tma_gbasis); + // Find j such that E is in stride(tma_gbasis) + using EI = decltype(ei); + [[maybe_unused]] auto j = find_if(tma_gbasis_stride, [&](auto tma_stride_j) { return any_of(tma_stride_j, [&](auto dj) { return dj == EI{}; }); }); + if constexpr (decltype(j == rank(tma_gbasis_stride))::value) { + return Int<0>{}; // If not-found, return arithmetic identity -- no contribution to the TMA + } else + if constexpr (decltype(j == Int<0>{})::value) { + auto scale = ratio(size(tma_gstride), size(smem_inv_h)) * basis_get(ei, stride(gtensor)); + return E{} * scale; // Return TMA Coord basis -- with a recast scale factor + } else + if constexpr (decltype(rank(tma_gbasis_stride) == Int<1>{})::value) { + return E{}; // Return TMA Coord basis -- known scale of Int<1>{} + } else { + int32_t scale = ceil_div(int32_t(di * sizeof_bits_v / cute::max(gmem_prob_stride[j], 16)), 8); + return E{} * scale; // Return TMA Coord basis -- with a dynamic scale factor + } + } + }); + + #if 0 + print("tma_gbasis : "); print(gmem_stride_bases); print("\n"); + #endif + + using AuxParams = AuxTmaParams; + return cute::make_tuple(tma_desc, AuxParams{gmem_stride_bases}); +} + +// The "logical TMA tid" is a map from the CTA rank to its logical id +// within the instruction. It works like a mask or ordering on the +// CTAs. For non-multicast TMA, all CTAs should map to 0. For +// multicast TMA of size 4, CTAs will be mapped to {0,1,2,3}. +template +CUTE_HOST_RTC +auto +make_tma_copy_tiled(CopyOp, + Tensor const& gtensor, // Full GMEM Tensor + SLayout const& slayout, // CTA Tile of SMEM + Layout const& cta_t_map, // T: CTA thr idx -> logical TMA tid + Layout const& cta_v_map) // V: CTA val idx -> gmem mode +{ + // + // TMA parameter checking + // + + CUTE_STATIC_ASSERT_V(product_each(shape(slayout)) == product_each(shape(cta_v_map)), + "TMA requires CTA_Tile and SLayout top-level shape equivalence."); + CUTE_STATIC_ASSERT_V(size(slayout) % cosize(cta_t_map) == Int<0>{}, + "Number of active CTAs in TMA must divide domain size of slayout."); + + // + // TMA slayout manipulation + // + + // Invert the smem to get the largest contiguous vector in the smem layout + // smem idx -> smem coord + auto inv_smem_layout = right_inverse(get_nonswizzle_portion(slayout)); + + // Compose with the V-Map to convert smem coord (CTA val idx) to gmem mode + // smem idx -> gmem mode + auto sidx_to_gmode = coalesce(composition(cta_v_map, inv_smem_layout)); + +#if 0 + print("g_tensor : "); print(gtensor); print("\n"); + print("s_layout : "); print(slayout); print("\n"); + print("cta_t_map : "); print(cta_t_map); print("\n"); + print("cta_v_map : "); print(cta_v_map); print("\n"); + print("inv_s_layout : "); print(inv_smem_layout); print("\n"); + print("sidx_to_gmode : "); print(sidx_to_gmode); print("\n"); +#endif + + // + // TMA gtensor manipulation + // + + // Generate a TupleBasis for the gtensor + // gmem coord -> gmem coord + auto glayout_basis = make_identity_layout(shape(gtensor)); + + // Tile the modes of gtensor with the truncated cta_v_map o inv_smem_layout_trunc + // smem idx -> gmem coord + auto tma_layout_full = flatten(composition(glayout_basis, sidx_to_gmode)); + + // Truncate any incompatibilities -- no starting in the middle of gmodes + auto smem_rank = find_if(stride(tma_layout_full), [](auto e) { + [[maybe_unused]] auto v = basis_value(e); + return not is_constant<1,decltype(v)>{}; + }); + static_assert(smem_rank > 0, "Could not find a common tile-gmem vectorization. Does the Tile select out major GMEM modes?"); + + // Keep only the static-1 basis modes into gmem + auto tma_layout_trunc = take<0,smem_rank>(tma_layout_full); + + // Keep only the portion each multicast CTA will be responsible for + auto tma_layout_v = composition(tma_layout_trunc, shape_div(size(tma_layout_trunc), cosize(cta_t_map))); + +#if 0 + print("glayout_basis : "); print(glayout_basis); print("\n"); + print("tma_layout_full : "); print(tma_layout_full); print("\n"); + + print("tma_layout_trunc: "); print(tma_layout_trunc); print("\n"); + print("tma_layout_v : "); print(tma_layout_v); print("\n"); +#endif + + // + // Construct the TMA Desc and the strides of the TMA Tensor + // + + auto [tma_desc, aux_params] = detail::make_tma_copy_desc(gtensor, + tma_layout_v, + get_swizzle_portion(slayout)); + + // + // Construct the Copy_Traits + // + + using T = typename GEngine::value_type; + constexpr int num_bits_per_tma = decltype(size(tma_layout_trunc))::value * sizeof_bits_v; + using Traits = Copy_Traits, decltype(aux_params)>; + using Atom = Copy_Atom; + + Traits tma_traits{tma_desc, aux_params}; + +#if 0 + print("num_bits_per_tma : "); print(num_bits_per_tma); print("\n"); + print("g_stride_bases : "); print(tma_traits.aux_params_.g_stride_); print("\n"); +#endif + + // + // Construct the TiledCopy + // + + auto cta_tiler = product_each(shape(cta_v_map)); + + // CTA V -> smem_coord + auto layout_v = composition(inv_smem_layout, size(tma_layout_trunc)); + auto layout_V = tile_to_shape(make_layout(layout_v), size(cta_v_map)); + // CTA T -> smem idx + auto layout_t = make_layout(cosize(cta_t_map), shape_div(size(tma_layout_trunc), cosize(cta_t_map))); + // CTA TID -> smem coord + auto layout_T = composition(inv_smem_layout, composition(layout_t, cta_t_map)); + // Combine with the T mapping + auto layout_TV = make_layout(layout_T, layout_V); + +#if 0 + print("cta_tiler : "); print(cta_tiler); print("\n"); + print("layout_VT : "); print(layout_VT); print("\n"); + print("layout_TV : "); print(layout_TV); print("\n"); +#endif + + return TiledCopy{tma_traits}; +} + +} // end namespace detail + +/** Make a CuTe CTA-collective TiledCopy for a TMA operation. + * + * @param CopyOp The target copy operation: SM90_TMA_LOAD, SM90_TMA_LOAD_MULTICAST, SM90_TMA_STORE + * @param gtensor The GMEM Tensor to be involved in the TMA. + * @param slayout The SMEM Layout to be involved in the TMA. + * @param cta_tile The CTA-local tile that each CTA will be tiling GMEM with. + * This is often the blk_shape that is used to tile the GMEM for CTAs: + * local_tile(gtensor, blk_shape, blk_coord) -> CTA-local tile of gtensor + * @param cluster_size When using SM90_TMA_LOAD_MULTICAST, this can be a (static) power-of-2 <= 16 + * defining the multicast size (used to further partition the SMEM) + * Else, static-1 + * + * This code attempts to maximize the TMA box size. It does this by tracing + * the SMEM "vector" -- the inverse of the smem layout -- to find the largest + * contiguous array of smem that can be written to/from global memory given + * the constraints that the TMA instruction imposes. + * + * This is accomplished by assigning "basis" strides to the GMEM to track which + * modes of SMEM map to which modes of GMEM, then reorder the modes of GMEM according + * to the SMEM vector, and then using those GMEM/SMEM modes to fill in the desc. + * + * Examples: + using T = float; + T* gptr = nullptr; + + { + // Simple 2D + Tensor gtensor = make_tensor(gptr, make_shape(1024, 256), GenRowMajor{}); // K-Major GMEM + auto slayout = make_layout(make_shape(_64{}, _32{}), GenRowMajor{}); // K-Major SMEM + auto tma = make_tma_copy(SM90_TMA_LOAD{}, gtensor, slayout); + } + + { + // GMMA 2D + Tensor gtensor = make_tensor(gptr, make_shape(1024, 256)); // MN-Major GMEM + auto slayout = tile_to_shape(GMMA::Layout_MN_SW128_Atom{}, make_shape(_128{},_64{})); // MN-Major Swizzled+Tiled 128x64 SMEM + auto tma = make_tma_copy(SM90_TMA_LOAD{}, gtensor, slayout); + } + + { + // 3D + Tensor gtensor = make_tensor(gptr, make_shape(1024, 32, 512), make_stride(64, Int<1>{}, 65536)); // GMEM + auto slayout = make_layout(make_shape(_16{}, _8{}, _2{}), make_stride(_16{}, _1{}, _8{})); // SMEM w/ same major-mode + auto tma = make_tma_copy(SM90_TMA_LOAD{}, gtensor, slayout); + } + + { + // cuTENSOR 4D + auto layout = make_shape(make_shape(32,40),make_shape(make_shape(8,8),656)); // GMEM + auto cta_tile = make_shape(_128{},make_shape(_32{},_2{})); // GMEM Tiling: + // Take 128-elem from m: m0 must divide 128, + // m-last may be predicated + // Take 32-elem from k0, 2-elem from k1 + auto slayout = make_layout(cta_tile); // Col-Major SMEM + auto tma = make_tma_copy(SM90_TMA_LOAD{}, gtensor, slayout, cta_tile, Int<1>{}); + } + * + * Check the TMA box size and desc: + print("TMA Box size: "); print(typename decltype(tma)::Tiler_MN{}); print("\n"); + print("TMA desc : "); print(tma.tma_desc_); print("\n"); + * + * Usage: + Tensor mA = tma_a.get_tma_tensor(make_shape(M,N)); // (M,N) TMA coord tensor + Tensor gA = local_tile(mA, cta_tile, cta_coord); // (BLK_M,BLK_N) TMA coord tensor for this CTA + Tensor sA = make_tensor(make_smem_ptr(sptr), slayout); // (BLK_M,BLK_N) SMEM tensor + + auto cta_tma = tma.get_slice(cta_idx_in_cluster); // Slice for multicast partitioning + Tensor tAgA = cta_tma.partition_S(gA); // Partition for src + Tensor tAsA = cta_tma.partition_D(sA); // Partition for dst + + copy(tma.with(barrier, mcast_mask), tAgA, tAsA); // copy with supporting TMA params + */ +template +CUTE_HOST_RTC +auto +make_tma_copy(CopyOp const& copy_op, + Tensor const& gtensor, + SLayout const& slayout, + CTA_Tile const& cta_tile, + Cluster_Size const& cluster_size) +{ + return detail::make_tma_copy_tiled(copy_op, + gtensor, + slayout, + make_layout(cluster_size), + make_identity_layout(cta_tile)); +} + +// Explicit defaulting +template +CUTE_HOST_RTC +auto +make_tma_copy(CopyOp const& copy_op, + Tensor const& gtensor, + SLayout const& slayout, + CTA_Tile const& cta_tile, + Cluster_Size const& cluster_size) +{ + using TmaInternalType = typename GEngine::value_type; + return make_tma_copy(copy_op, + gtensor, + slayout, + cta_tile, + cluster_size); +} + +template +CUTE_HOST_RTC +auto +make_tma_copy(CopyOp const& copy_op, + Tensor const& gtensor, + SLayout const& slayout) +{ + return make_tma_copy(copy_op, gtensor, slayout, product_each(shape(slayout)), Int<1>{}); +} + +template +CUTE_HOST_RTC +auto +make_tma_copy(CopyOp const& copy_op, + Tensor const& gtensor, + SLayout const& slayout, + Cluster_Size const& cluster_size) +{ + return make_tma_copy(copy_op, gtensor, slayout, product_each(shape(slayout)), cluster_size); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_swizzle_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_swizzle_hip.hpp new file mode 100644 index 000000000..b16b7dfa9 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_swizzle_hip.hpp @@ -0,0 +1,76 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +/// @file copy_traits_sm90_tma_swizzle.hpp +/// @brief Functions for converting swizzle layout to TMA descriptor + +#if !defined(__CUDACC_RTC__) +#include +#endif + +#include "cute/arch/copy_sm90_desc_hip.hpp" +#include "cute/swizzle_layout_hip.hpp" + +namespace cute::detail { + +template +TMA::SmemSwizzleBits +get_tma_swizzle_bits(Swizzle) +{ + if constexpr (M == 4) { + switch (B) { + default: static_assert(0 <= B && B <= 3, "Expected B = 0,1,2, or 3 when M == 4. Unsupported layout swizzle."); + case 3: return TMA::SmemSwizzleBits::B128; + case 2: return TMA::SmemSwizzleBits::B64; + case 1: return TMA::SmemSwizzleBits::B32; + case 0: return TMA::SmemSwizzleBits::DISABLE; + } + } else + { + static_assert(M < 0, "Unsupported layout swizzle."); + } +} + +template +TMA::SmemSwizzleBits +get_tma_swizzle_bits(Layout const& layout) +{ + return get_tma_swizzle_bits(get_swizzle_portion(layout)); +} + +} // namespace cute::detail diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_atom_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_atom_hip.hpp new file mode 100644 index 000000000..7bcab45e2 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_atom_hip.hpp @@ -0,0 +1,1075 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +#include + +#include + +namespace cute { + +template +struct MMA_Atom; + +template +struct MMA_Atom : MMA_Atom> +{}; + +template +struct MMA_Atom> + : MMA_Traits +{ + using Traits = MMA_Traits; + + // Element value types from the MMA_Traits + using ValTypeD = typename Traits::ElementDVal; + using ValTypeA = typename Traits::ElementAVal; + using ValTypeB = typename Traits::ElementBVal; + using ValTypeC = typename Traits::ElementCVal; + + // Thr-Val layouts from the MMA_Traits + using Shape_MNK = typename Traits::Shape_MNK; + using ThrID = typename Traits::ThrID; + using LayoutC_TV = typename Traits::CLayout; + using LayoutA_TV = typename Traits::ALayout; + using LayoutB_TV = typename Traits::BLayout; + + // Fragment value types from the MMA_Traits (optional, defaults to Val type) + using FrgTypeD = typename detail::FrgTypeC_or_Default::type; + using FrgTypeA = typename detail::FrgTypeA_or_Default::type; + using FrgTypeB = typename detail::FrgTypeB_or_Default::type; + using FrgTypeC = typename detail::FrgTypeC_or_Default::type; + + // Additional Trait parameters/transformations + template + CUTE_HOST_DEVICE + auto + with(TraitsArgs&&... args) const { + auto traits = Traits::with(std::forward(args)...); + return MMA_Atom{traits}; + } + + // + // Tensor call interfaces + // + + // Cast, check, and call fma + template + CUTE_HOST_DEVICE constexpr + void + call(Tensor & D, + Tensor const& A, + Tensor const& B, + Tensor const& C) const + { + static_assert(DLayout::rank == 1, "Expected rank-1 D tensor"); + static_assert(ALayout::rank == 1, "Expected rank-1 A tensor"); + static_assert(BLayout::rank == 1, "Expected rank-1 B tensor"); + static_assert(CLayout::rank == 1, "Expected rank-1 C tensor"); + + return mma_unpack(*this, D, A, B, C); + } + + // Three arguments reproduces C + template + CUTE_HOST_DEVICE constexpr + void + call(Tensor const& A, + Tensor const& B, + Tensor & C) const + { + return call(C, A, B, C); + } + + // + // make_fragment_A|B|C + // These functions are awkward as they expect already-partitioned tensors + // resulting from a previous call to partition_A|B|C + // The reasoning is that we can inspect the layout of the partitioned data + // and attempt to match it in generated fragment to promote vectorization + // when copying from partition to fragment. + // + + template + CUTE_HOST_DEVICE static constexpr + auto + make_fragment_C(CTensor&& ctensor) + { + // Check that this tensor is likely already partitioned + CUTE_STATIC_ASSERT_V(rank(ctensor) >= Int<3>{}); // VMN + CUTE_STATIC_ASSERT_V(size<0>(ctensor) == size<1>(LayoutC_TV{})); + // C is a bit special because we are after accumulators here + // The input/output type doesn't have to match the accumulator type + //static_assert(std::is_same::value_type>::value, "Expecting ValTypeC type"); + + // We'll never base the accumulator layout on the input tensor layout, so just return a FrgTypeC tensor + return make_tensor(shape(ctensor)); + } + + template + CUTE_HOST_DEVICE static constexpr + auto + make_fragment_A(ATensor&& atensor) + { + // Check that this tensor is likely already partitioned + CUTE_STATIC_ASSERT_V(rank(atensor) >= Int<3>{}); // VMK + CUTE_STATIC_ASSERT_V(size<0>(atensor) == size<1>(LayoutA_TV{})); + + if constexpr (has_dereference::value) { + // If the intended FrgTypeA is a view (of the current tensor), forward the whole + static_assert(is_same, typename remove_cvref_t::value_type>::value, "Expecting ValTypeA type"); + return make_tensor(std::forward(atensor)); + } else { + // Else, the intended FrgTypeA is a value type, construct a new tensor with a fragment layout + return make_fragment_like(atensor); + } + + CUTE_GCC_UNREACHABLE; + } + + template + CUTE_HOST_DEVICE static constexpr + auto + make_fragment_B(BTensor&& btensor) + { + // Check that this tensor is likely already partitioned + CUTE_STATIC_ASSERT_V(rank(btensor) >= Int<3>{}); // VNK + CUTE_STATIC_ASSERT_V(size<0>(btensor) == size<1>(LayoutB_TV{})); + + if constexpr (has_dereference::value) { + // If the intended FrgTypeB is a view (of the current tensor), forward the whole + static_assert(is_same::value_type>::value, "Expecting ValTypeB type"); + return make_tensor(std::forward(btensor)); + } else { + // Else, the intended FrgTypeB is a value type, construct a new tensor with a fragment layout + return make_fragment_like(btensor); + } + + CUTE_GCC_UNREACHABLE; + } +}; + +// +// A tiling of mma atoms +// + +template +struct ThrMMA; + +template >, + class ValLayoutMNK = Layout>, + class PermutationsMNK = Tile> +struct TiledMMA : MMA_Atom +{ + static_assert(rank_v == 3, "TiledMMA requires rank-3 AtomLayoutMNK"); + static_assert(rank_v == 3, "TiledMMA requires rank-3 ValLayoutMNK"); + static_assert(rank_v == 3, "TiledMMA requires rank-3 PermutationsMNK"); + + using AtomShape_MNK = typename MMA_Atom::Shape_MNK; + + using AtomLayoutC_TV = typename MMA_Atom::LayoutC_TV; + using AtomLayoutA_TV = typename MMA_Atom::LayoutA_TV; + using AtomLayoutB_TV = typename MMA_Atom::LayoutB_TV; + + // ThrV -> thread_idx + using AtomThrID = typename MMA_Atom::ThrID; + + // (M,N,K) + using TiledShape_MNK = decltype(make_shape(size<0>(AtomShape_MNK{})*size<0>(AtomLayoutMNK{})*size<0>(ValLayoutMNK{}), + size<1>(AtomShape_MNK{})*size<1>(AtomLayoutMNK{})*size<1>(ValLayoutMNK{}), + size<2>(AtomShape_MNK{})*size<2>(AtomLayoutMNK{})*size<2>(ValLayoutMNK{}))); + + // thrid = (ThrV,ThrM,ThrN,ThrK) -> thr_idx + using ThrLayoutVMNK = decltype(tiled_product(AtomThrID{}, AtomLayoutMNK{})); + + // thr_idx -> (ThrV,ThrM,ThrN,ThrK) + using TidLayout = decltype(right_inverse(ThrLayoutVMNK{})); + + // Tile a tensor or a layout from shape + // (M,N,...) + // to shape + // ((ThrV,(ThrM,ThrN)),(FrgV,(RestM,RestN,...))) + // where + // ThrV: The threads local to an MMA. layout<0>(ThrLayoutVMNK): ThrV -> thread_idx + // ThrM: The threads tiled in M. layout<1>(ThrLayoutVMNK): ThrM -> thread_idx + // ThrN: The threads tiled in N. layout<2>(ThrLayoutVMNK): ThrN -> thread_idx + // FrgV: The values local to an MMA. + // RestM: The values tiled in M. + // RestN: The values tiled in N. + template + CUTE_HOST_DEVICE constexpr static + auto + thrfrg_C(CTensor&& ctensor) + { + CUTE_STATIC_ASSERT_V(rank(ctensor) >= Int<2>{}); + CUTE_STATIC_ASSERT_V(size<0>(ctensor) % size<0>(TiledShape_MNK{}) == Int<0>{}); + CUTE_STATIC_ASSERT_V(size<1>(ctensor) % size<1>(TiledShape_MNK{}) == Int<0>{}); + + // Reorder the tensor for the TiledAtom + auto t_tile = make_tile(left_inverse(get<0>(PermutationsMNK{})), + left_inverse(get<1>(PermutationsMNK{}))); + auto t_tensor = logical_divide(ctensor, t_tile); // (PermM,PermN) + + // Tile the tensor for the Atom + auto a_tile = make_tile(make_layout(size<0>(AtomShape_MNK{})), + make_layout(size<1>(AtomShape_MNK{}))); + auto a_tensor = zipped_divide(t_tensor, a_tile); // ((AtomM,AtomN),(RestM,RestN)) + + // Transform the Atom mode from (M,K) to (Thr,Val) + auto tv_tensor = a_tensor.compose(AtomLayoutC_TV{},_); // ((ThrV,FrgV),(RestM,RestN)) + + // Tile the tensor for the C-threads + auto thr_tile = make_tile(_, + make_tile(make_layout(size<1>(ThrLayoutVMNK{})), + make_layout(size<2>(ThrLayoutVMNK{})))); + auto thr_tensor = zipped_divide(tv_tensor, thr_tile); // ((ThrV,(ThrM,ThrN)),(FrgV,(RestM,RestN))) + + return thr_tensor; + } + + // Tile from (M,N,...) + // to (thr_idx,(FrgV,(RestM,RestN,...))) + template + CUTE_HOST_DEVICE constexpr static + auto + tidfrg_C(CTensor&& ctensor) + { + // Don't need a ctile composition because ThrK is last mode in TidLayout + + return thrfrg_C(ctensor).compose(TidLayout{}, _); + } + + // Tile a tensor or a layout from shape + // (M,K,...) + // to shape + // ((ThrV,(ThrM,ThrK)),(FrgV,(RestM,RestK,...))) + // where + // ThrV: The threads local to an MMA. layout<0>(ThrLayoutVMNK): ThrV -> thread_idx + // ThrM: The threads tiled in M. layout<1>(ThrLayoutVMNK): ThrM -> thread_idx + // ThrK: The threads tiled in K. layout<3>(ThrLayoutVMNK): ThrK -> thread_idx + // FrgV: The values local to an MMA. + // RestM: The values tiled in M. + // RestK: The values tiled in K. + template + CUTE_HOST_DEVICE constexpr static + auto + thrfrg_A(ATensor&& atensor) + { + CUTE_STATIC_ASSERT_V(rank(atensor) >= Int<2>{}); + CUTE_STATIC_ASSERT_V(size<0>(atensor) % size<0>(TiledShape_MNK{}) == Int<0>{}); + CUTE_STATIC_ASSERT_V(size<1>(atensor) % size<2>(TiledShape_MNK{}) == Int<0>{}); + + // Reorder the tensor for the TiledAtom + auto t_tile = make_tile(left_inverse(get<0>(PermutationsMNK{})), + left_inverse(get<2>(PermutationsMNK{}))); + auto t_tensor = logical_divide(atensor, t_tile); // (PermM,PermK) + + // Tile the tensor for the Atom + auto a_tile = make_tile(make_layout(size<0>(AtomShape_MNK{})), + make_layout(size<2>(AtomShape_MNK{}))); + auto a_tensor = zipped_divide(t_tensor, a_tile); // ((AtomM,AtomK),(RestM,RestK)) + + // Transform the Atom mode from (M,K) to (Thr,Val) + auto tv_tensor = a_tensor.compose(AtomLayoutA_TV{},_); // ((ThrV,FrgV),(RestM,RestK)) + + // Tile the tensor for the Thread + auto thr_tile = make_tile(_, + make_tile(make_layout(size<1>(ThrLayoutVMNK{})), + make_layout(size<3>(ThrLayoutVMNK{})))); + auto thr_tensor = zipped_divide(tv_tensor, thr_tile); // ((ThrV,(ThrM,ThrK)),(FrgV,(RestM,RestK))) + + return thr_tensor; + } + + // Tile from (M,K,...) + // to (thr_idx,(FrgV,(RestM,RestK,...))) + template + CUTE_HOST_DEVICE constexpr static + auto + tidfrg_A(ATensor&& atensor) + { + auto atile = make_tile(_, + make_tile(make_layout(make_shape (size<1>(ThrLayoutVMNK{}), size<2>(ThrLayoutVMNK{})), + make_stride( Int<1>{} , Int<0>{} )), + _)); + // (ThrV,(ThrM,ThrK)) -> (ThrV,(ThrM,ThrN,ThrK)) + + return thrfrg_A(atensor).compose(atile, _).compose(TidLayout{}, _); + } + + // Tile a tensor or a layout from shape + // (N,K,...) + // to shape + // ((ThrV,(ThrN,ThrK)),(FrgV,(RestN,RestK,...))) + // where + // ThrV: The threads local to an MMA. layout<0>(ThrLayoutVMNK): ThrV -> thread_idx + // ThrN: The threads tiled in N. layout<2>(ThrLayoutVMNK): ThrN -> thread_idx + // ThrK: The threads tiled in K. layout<3>(ThrLayoutVMNK): ThrK -> thread_idx + // FrgV: The values local to an MMA. + // RestN: The values tiled in N. + // RestK: The values tiled in K. + template + CUTE_HOST_DEVICE constexpr static + auto + thrfrg_B(BTensor&& btensor) + { + CUTE_STATIC_ASSERT_V(rank(btensor) >= Int<2>{}); + CUTE_STATIC_ASSERT_V(size<0>(btensor) % size<1>(TiledShape_MNK{}) == Int<0>{}); + CUTE_STATIC_ASSERT_V(size<1>(btensor) % size<2>(TiledShape_MNK{}) == Int<0>{}); + + // Reorder the tensor for the TiledAtom + auto t_tile = make_tile(left_inverse(get<1>(PermutationsMNK{})), + left_inverse(get<2>(PermutationsMNK{}))); + auto t_tensor = logical_divide(btensor, t_tile); // (PermN,PermK) + + // Tile the tensor for the Atom + auto a_tile = make_tile(make_layout(size<1>(AtomShape_MNK{})), + make_layout(size<2>(AtomShape_MNK{}))); + auto a_tensor = zipped_divide(t_tensor, a_tile); // ((AtomN,AtomK),(RestN,RestK)) + + // Transform the Atom mode from (M,K) to (Thr,Val) + auto tv_tensor = a_tensor.compose(AtomLayoutB_TV{},_); // ((ThrV,FrgV),(RestN,RestK)) + + // Tile the tensor for the Thread + auto thr_tile = make_tile(_, + make_tile(make_layout(size<2>(ThrLayoutVMNK{})), + make_layout(size<3>(ThrLayoutVMNK{})))); + auto thr_tensor = zipped_divide(tv_tensor, thr_tile); // ((ThrV,(ThrN,ThrK)),(FrgV,(RestN,RestK))) + + return thr_tensor; + } + + // Tile from (N,K,...) + // to (thr_idx,(FrgV,(RestN,RestK,...))) + template + CUTE_HOST_DEVICE constexpr static + auto + tidfrg_B(BTensor&& btensor) + { + auto btile = make_tile(_, + make_tile(make_layout(make_shape (size<1>(ThrLayoutVMNK{}), size<2>(ThrLayoutVMNK{})), + make_stride( Int<0>{} , Int<1>{} )), + _)); + // (ThrV,(ThrN,ThrK)) -> (ThrV,(ThrM,ThrN,ThrK)) + + return thrfrg_B(btensor).compose(btile, _).compose(TidLayout{}, _); + } + + template ::value)> + CUTE_HOST_DEVICE static constexpr + auto + get_slice(ThrIdx const& thr_idx) + { + auto thr_vmnk = ThrLayoutVMNK{}.get_flat_coord(thr_idx); + return ThrMMA(thr_vmnk); + } + + template ::value)> + CUTE_HOST_DEVICE static constexpr + auto + get_thread_slice(ThrIdx const& thr_idx) + { + return get_slice(thr_idx); + } + + // + // Utility for printing and visualization + // + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutC_MN() + { + // (M,N) -> (M,N) + auto ref_C = make_layout(make_shape(size<0>(TiledShape_MNK{}), size<1>(TiledShape_MNK{}))); + // (cthrid,val) -> (M,N) + auto layoutC_TV = thrfrg_C(ref_C); + // (M,N) -> (cthrid,frg) + auto layoutC_MN = right_inverse(layoutC_TV).with_shape(shape(ref_C)); + + // cthrid = (v,m,n) -> thr_idx + auto thrID_C = ThrLayoutVMNK{}(_,_,_,Int<0>{}); + + return cute::make_tuple(layoutC_MN, thrID_C); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutC_TV() + { + // (M,N) -> (M,N) + auto ref_C = make_layout(make_shape(size<0>(TiledShape_MNK{}), size<1>(TiledShape_MNK{}))); + + return tidfrg_C(ref_C); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutA_MK() + { + // (M,K) -> (M,K) + auto ref_A = make_layout(make_shape(size<0>(TiledShape_MNK{}), size<2>(TiledShape_MNK{}))); + // (athrid,val) -> (M,K) + auto layoutA_TV = thrfrg_A(ref_A); + // (M,K) -> (athrid,frg) + auto layoutA_MK = right_inverse(layoutA_TV).with_shape(shape(ref_A)); + + // athrid = (v,m,k) -> thr_idx + auto thrID_A = ThrLayoutVMNK{}(_,_,Int<0>{},_); + + return cute::make_tuple(layoutA_MK, thrID_A); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutA_TV() + { + // (M,K) -> (M,K) + auto ref_A = make_layout(make_shape(size<0>(TiledShape_MNK{}), size<2>(TiledShape_MNK{}))); + + return tidfrg_A(ref_A); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutB_NK() + { + // (N,K) -> (N,K) + auto ref_B = make_layout(make_shape(size<1>(TiledShape_MNK{}), size<2>(TiledShape_MNK{}))); + // (bthrid,val) -> (N,K) + auto layoutB_TV = thrfrg_B(ref_B); + // (N,K) -> (bthrid,frg) + auto layoutB_NK = right_inverse(layoutB_TV).with_shape(shape(ref_B)); + + // bthrid = (v,n,k) -> thr_idx + auto thrID_B = ThrLayoutVMNK{}(_,Int<0>{},_,_); + + return cute::make_tuple(layoutB_NK, thrID_B); + } + + CUTE_HOST_DEVICE constexpr static + auto + get_layoutB_TV() + { + // (N,K) -> (N,K) + auto ref_B = make_layout(make_shape(size<1>(TiledShape_MNK{}), size<2>(TiledShape_MNK{}))); + + return tidfrg_B(ref_B); + } +}; + +template +struct ThrMMA : TiledMMA +{ + // Use ThrVMNK and thrfrg rather than thr_idx and tidfrg + // to support swizzled threads partitioning dynamic layouts + ThrVMNK thr_vmnk_; + + CUTE_HOST_DEVICE constexpr + ThrMMA(ThrVMNK const& thr_vmnk) : thr_vmnk_(thr_vmnk) {} + + template + CUTE_HOST_DEVICE constexpr + auto + partition_C(CTensor&& ctensor) const + { + auto thr_tensor = make_tensor(std::forward(ctensor).data(), TiledMMA::thrfrg_C(ctensor.layout())); + + auto thr_vmn = make_coord(get<0>(thr_vmnk_), make_coord(get<1>(thr_vmnk_), get<2>(thr_vmnk_))); + return thr_tensor(thr_vmn, make_coord(_, repeat(thr_tensor)>(_))); + } + + template + CUTE_HOST_DEVICE constexpr + auto + partition_A(ATensor&& atensor) const + { + auto thr_tensor = make_tensor(std::forward(atensor).data(), TiledMMA::thrfrg_A(atensor.layout())); + + auto thr_vmk = make_coord(get<0>(thr_vmnk_), make_coord(get<1>(thr_vmnk_), get<3>(thr_vmnk_))); + return thr_tensor(thr_vmk, make_coord(_, repeat(thr_tensor)>(_))); + } + + template + CUTE_HOST_DEVICE constexpr + auto + partition_B(BTensor&& btensor) const + { + auto thr_tensor = make_tensor(std::forward(btensor).data(), TiledMMA::thrfrg_B(btensor.layout())); + + auto thr_vnk = make_coord(get<0>(thr_vmnk_), make_coord(get<2>(thr_vmnk_), get<3>(thr_vmnk_))); + return thr_tensor(thr_vnk, make_coord(_, repeat(thr_tensor)>(_))); + } + + template + CUTE_HOST_DEVICE constexpr + auto + partition_fragment_C(CTensor&& ctensor) const + { + return this->make_fragment_C(partition_C(ctensor)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + partition_fragment_A(ATensor&& atensor) const + { + return this->make_fragment_A(partition_A(atensor)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + partition_fragment_B(BTensor&& btensor) const + { + return this->make_fragment_B(partition_B(btensor)); + } +}; + +// +// These tile the MMA_Atom as a whole +// + +template >, + class MMAValLayout = Layout>, + class Permutations = Tile> +CUTE_HOST_DEVICE constexpr +auto +make_tiled_mma(MMA_Atom const&, + MMAThrLayout const& thr_layout = {}, + MMAValLayout const& val_layout = {}, + Permutations const& permutations = {}) +{ + auto thr_layout_mnk = append<3>(thr_layout, Layout<_1,_0>{}); + auto val_layout_mnk = append<3>(val_layout, Layout<_1,_0>{}); + auto permutation_mnk = append<3>(permutations, _); + + return TiledMMA, + decltype(thr_layout_mnk), + decltype(val_layout_mnk), + decltype(permutation_mnk)>{}; +} + +template >, + class MMAValLayout = Layout>, + class Permutations = Tile> +CUTE_HOST_DEVICE constexpr +auto +make_tiled_mma(MMA_Op const&, + MMAThrLayout const& thr_layout = {}, + MMAValLayout const& val_layout = {}, + Permutations const& permutations = {}) +{ + // Attempt to wrap in an MMA_Atom<> and forward + return make_tiled_mma(MMA_Atom{}, thr_layout, val_layout, permutations); +} + +// +// partition_fragment_C -- static context +// + +template +CUTE_HOST_DEVICE constexpr +auto +partition_shape_C(TiledMMA const& mma, Shape_MN const& shape_MN) +{ + constexpr int R = rank_v; + static_assert(R >= 2, "Must have at least rank-2"); + auto atomMNK = typename TiledMMA::AtomShape_MNK{}; + auto thrVMNK = typename TiledMMA::ThrLayoutVMNK{}; + auto V = shape<1>(typename TiledMMA::AtomLayoutC_TV{}); + auto M = shape_div(size<0>(shape_MN), size<0>(atomMNK) * size<1>(thrVMNK)); + auto N = shape_div(size<1>(shape_MN), size<1>(atomMNK) * size<2>(thrVMNK)); + return cute::tuple_cat(make_shape(V,M,N), take<2,R>(shape_MN)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +partition_fragment_C(TiledMMA const& mma, Shape_MN const& shapeMN) +{ + return make_tensor::FrgTypeC>(partition_shape_C(mma, shapeMN)); +} + +// partition_fragment_A and partition_fragment_B often depend on the +// layout of A and B and/or the thread_idx that is requesting the partition. +// For these reasons, they should not be used in a static context. +// See TiledMMA::get_slice(thr_idx).partition_fragment_A(tensorA) instead. + +template +CUTE_HOST_DEVICE constexpr +auto +partition_shape_A(TiledMMA const& mma, Shape_MK const& shape_MK) +{ + constexpr int R = rank_v; + static_assert(R >= 2, "Must have at least rank-2"); + auto atomMNK = typename TiledMMA::AtomShape_MNK{}; + auto thrVMNK = typename TiledMMA::ThrLayoutVMNK{}; + auto V = shape<1>(typename TiledMMA::AtomLayoutA_TV{}); + auto M = shape_div(size<0>(shape_MK), size<0>(atomMNK) * size<1>(thrVMNK)); + auto K = shape_div(size<1>(shape_MK), size<2>(atomMNK) * size<3>(thrVMNK)); + return cute::tuple_cat(make_shape(V,M,K), take<2,R>(shape_MK)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +partition_shape_B(TiledMMA const& mma, Shape_NK const& shape_NK) +{ + constexpr int R = rank_v; + static_assert(R >= 2, "Must have at least rank-2"); + auto atomMNK = typename TiledMMA::AtomShape_MNK{}; + auto thrVMNK = typename TiledMMA::ThrLayoutVMNK{}; + auto V = shape<1>(typename TiledMMA::AtomLayoutB_TV{}); + auto N = shape_div(size<0>(shape_NK), size<1>(atomMNK) * size<2>(thrVMNK)); + auto K = shape_div(size<1>(shape_NK), size<2>(atomMNK) * size<3>(thrVMNK)); + return cute::tuple_cat(make_shape(V,N,K), take<2,R>(shape_NK)); +} + +// +// Size +// + +template +CUTE_HOST_DEVICE constexpr +auto +tile_size(TiledMMA const& mma) +{ + return size(typename TiledMMA::TiledShape_MNK{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tile_shape(TiledMMA const& mma) +{ + return shape(typename TiledMMA::TiledShape_MNK{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +size(TiledMMA const& mma) +{ + return size(typename TiledMMA::ThrLayoutVMNK{}); +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE +void +print(MMA_Atom> const&) +{ + using Atom = MMA_Atom>; + print("MMA_Atom\n"); + print(" ThrID: "); print(typename Atom::ThrID{}); print("\n"); + print(" LayoutA_TV: "); print(typename Atom::LayoutA_TV{}); print("\n"); + print(" LayoutB_TV: "); print(typename Atom::LayoutB_TV{}); print("\n"); + print(" LayoutC_TV: "); print(typename Atom::LayoutC_TV{}); print("\n"); +} + +template +CUTE_HOST_DEVICE +void +print(TiledMMA const& mma) +{ + using MMA = TiledMMA; + print("TiledMMA\n"); + print(" TiledThr: "); print(TiledThr{}); print("\n"); + print(" TiledVal: "); print(TiledVal{}); print("\n"); + print(" TiledPerm: "); print(TiledPerm{}); print("\n"); + print(" TiledShape_MNK: "); print(typename MMA::TiledShape_MNK{}); print("\n"); + print(" ThrLayoutVMNK: "); print(typename MMA::ThrLayoutVMNK{}); print("\n"); + print(static_cast(mma)); +} + +template +CUTE_HOST_DEVICE +void +print(ThrMMA const&) +{ + print(TiledMMA{}); +} + +template +CUTE_HOST_DEVICE +auto +print_latex(TiledMMA const& mma) +{ + auto layout_and_thrid_C = mma.get_layoutC_MN(); + auto layoutC_MN = get<0>(layout_and_thrid_C); + auto thrID_C = get<1>(layout_and_thrid_C); + + auto layout_and_thrid_A = mma.get_layoutA_MK(); + auto layoutA_MK = get<0>(layout_and_thrid_A); + auto thrID_A = get<1>(layout_and_thrid_A); + + auto layout_and_thrid_B = mma.get_layoutB_NK(); + auto layoutB_NK = get<0>(layout_and_thrid_B); + auto thrID_B = get<1>(layout_and_thrid_B); + + print_latex_mma(layoutC_MN, thrID_C, + layoutA_MK, thrID_A, + layoutB_NK, thrID_B); +} + +// EXPERIMENTAL -- Doesn't work with Swizzled Thr TileMMAs... +template +CUTE_HOST_DEVICE +auto +print_latex_2(TiledMMA const& mma) +{ + print_latex_mma(typename TiledMMA::TiledShape_MNK{}, + mma.get_layoutC_TV(), + mma.get_layoutA_TV(), + mma.get_layoutB_TV()); +} + +// MNK MMA Layout to console printer -- 8-value color coded by thread +template +CUTE_HOST_DEVICE +void +print_layout_mma(LayoutC const& C, ThrIDC const& TC, // (m,n) -> (tid,vid) and tid -> thr_idx + LayoutA const& A, ThrIDA const& TA, // (m,k) -> (tid,vid) and tid -> thr_idx + LayoutB const& B, ThrIDB const& TB) // (n,k) -> (tid,vid) and tid -> thr_idx +{ + CUTE_STATIC_ASSERT_V(rank(C) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(A) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(B) == Int<2>{}); + + assert(size<0>(A) == size<0>(C)); + assert(size<0>(B) == size<1>(C)); + assert(size<1>(A) == size<1>(B)); + + int a_width = size<1>(A) * 6 + 4; + + // Print out B (white-shifted) k-by-n + for (int k = 0; k < size<1>(B); ++k) { + // Header + printf("%*s", a_width, ""); + for (int n = 0; n < size<0>(B); ++n) printf("+-----"); + printf("+\n"); + // Values + printf("%*s", a_width, ""); + for (int n = 0; n < size<0>(B); ++n) printf("|T%02dV%1d", int(TB(B(n,k) % size(TB))), int(B(n,k) / size(TB))); + printf("|\n"); + } + // Footer + printf("%*s", a_width, ""); + for (int n = 0; n < size<0>(B); ++n) printf("+-----"); + printf("+\n\n"); + + // Print out A m-by-k and C m-by-n + for (int m = 0; m < size<0>(A); ++m) { + // Header + for (int k = 0; k < size<1>(A); ++k) printf("+-----"); + printf("+ "); + for (int n = 0; n < size<1>(C); ++n) printf("+-----"); + printf("+\n"); + // Values + for (int k = 0; k < size<1>(A); ++k) printf("|T%02dV%1d", int(TA(A(m,k) % size(TA))), int(A(m,k) / size(TA))); + printf("| "); + for (int n = 0; n < size<1>(C); ++n) printf("|T%02dV%1d", int(TC(C(m,n) % size(TC))), int(C(m,n) / size(TC))); + printf("|\n"); + } + // Footer + for (int k = 0; k < size<1>(A); ++k) printf("+-----"); + printf("+ "); + for (int n = 0; n < size<1>(C); ++n) printf("+-----"); + printf("+\n"); +} + +// MNK MMA Layout to Latex TIKZ -- 8-value color coded by thread +template +CUTE_HOST_DEVICE +void +print_latex_mma(LayoutC const& C, ThrIDC const& TC, // (m,n) -> (tid,vid) and tid -> thr_idx + LayoutA const& A, ThrIDA const& TA, // (m,k) -> (tid,vid) and tid -> thr_idx + LayoutB const& B, ThrIDB const& TB) // (n,k) -> (tid,vid) and tid -> thr_idx +{ + CUTE_STATIC_ASSERT_V(rank(C) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(A) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(B) == Int<2>{}); + + assert(size<0>(A) == size<0>(C)); + assert(size<0>(B) == size<1>(C)); + assert(size<1>(A) == size<1>(B)); + + char const* latex_header = + "\\documentclass{standalone}\n" + "\\usepackage{tikz}\n" + "\\usetikzlibrary{external}\n" + "\\tikzexternalize\n" + "\\begin{document}\n" + "\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},box/.style={rectangle,draw=black,thick,minimum size=1cm,anchor=center}]\n\n"; + char const* latex_footer = + "\\end{tikzpicture}\n" + "\\end{document}\n"; + + char const* color_map[8] = {"{rgb,255:red,175;green,175;blue,255}", + "{rgb,255:red,175;green,255;blue,175}", + "{rgb,255:red,255;green,255;blue,175}", + "{rgb,255:red,255;green,175;blue,175}", + "{rgb,255:red,210;green,210;blue,255}", + "{rgb,255:red,210;green,255;blue,210}", + "{rgb,255:red,255;green,255;blue,210}", + "{rgb,255:red,255;green,210;blue,210}"}; + + // Header + printf("%% LayoutC: "); print(C); printf("\n"); + printf("%% ThrIDC : "); print(TC); printf("\n"); + printf("%% LayoutA: "); print(A); printf("\n"); + printf("%% ThrIDA : "); print(TA); printf("\n"); + printf("%% LayoutB: "); print(B); printf("\n"); + printf("%% ThrIDB : "); print(TB); printf("\n\n"); + + printf(latex_header); + + // C starting at 0,0 + for (int m = 0; m < size<0>(C); ++m) { + for (int n = 0; n < size<1>(C); ++n) { + int thrid = C(m,n) % size(TC); + int val_idx = C(m,n) / size(TC); + int thr_idx = TC(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + m, n, + thr_idx, val_idx); + } + } + + // A starting at 0,-size<1>(A)-1 + for (int m = 0; m < size<0>(A); ++m) { + for (int k = 0; k < size<1>(A); ++k) { + int thrid = A(m,k) % size(TA); + int val_idx = A(m,k) / size(TA); + int thr_idx = TA(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + m, k-1-size<1>(A), + thr_idx, val_idx); + } + } + + // B starting at -size<1>(B)-1,0 + for (int n = 0; n < size<0>(B); ++n) { + for (int k = 0; k < size<1>(B); ++k) { + int thrid = B(n,k) % size(TB); + int val_idx = B(n,k) / size(TB); + int thr_idx = TB(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + k-1-size<1>(B), n, + thr_idx, val_idx); + } + } + + // A labels + for (int m = 0, k = -1; m < size<0>(A); ++m) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", m, k-1-size<1>(A), m); + } + for (int k = 0, m = -1; k < size<1>(A); ++k) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", m, k-1-size<1>(A), k); + } + // B labels + for (int n = 0, k = -1; n < size<0>(B); ++n) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", k-1-size<1>(B), n, n); + } + for (int k = 0, n = -1; k < size<1>(B); ++k) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", k-1-size<1>(B), n, k); + } + + // Footer + printf(latex_footer); +} + +// ThrVal MMA Layout to Latex TIKZ -- 8-value color coded by thread +template +CUTE_HOST_DEVICE +void +print_latex_mma(Shape_MNK const& shape_mnk, + LayoutC const& C, // (thr_idx,vid) -> (m,n) + LayoutA const& A, // (thr_idx,vid) -> (m,k) + LayoutB const& B) // (thr_idx,vid) -> (n,k) +{ + CUTE_STATIC_ASSERT_V(rank(C) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(A) == Int<2>{}); + CUTE_STATIC_ASSERT_V(rank(B) == Int<2>{}); + + char const* latex_header = + "\\documentclass{standalone}\n" + "\\usepackage{tikz}\n" + "\\usetikzlibrary{external}\n" + "\\tikzexternalize\n" + "\\begin{document}\n" + "\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},box/.style={rectangle,draw=black,thick,minimum size=1cm,anchor=center}]\n\n"; + char const* latex_footer = + "\\end{tikzpicture}\n" + "\\end{document}\n"; + + char const* color_map[8] = {"{rgb,255:red,175;green,175;blue,255}", + "{rgb,255:red,175;green,255;blue,175}", + "{rgb,255:red,255;green,255;blue,175}", + "{rgb,255:red,255;green,175;blue,175}", + "{rgb,255:red,210;green,210;blue,255}", + "{rgb,255:red,210;green,255;blue,210}", + "{rgb,255:red,255;green,255;blue,210}", + "{rgb,255:red,255;green,210;blue,210}"}; + + // Header + printf("%% Shape_MNK: "); print(shape_mnk); printf("\n"); + printf("%% LayoutC : "); print(C); printf("\n"); + printf("%% LayoutA : "); print(A); printf("\n"); + printf("%% LayoutB : "); print(B); printf("\n\n"); + + printf(latex_header); + + auto M = size<0>(shape_mnk); + auto N = size<1>(shape_mnk); + auto K = size<2>(shape_mnk); + + // C starting at 0,0 + bool c_filled[M][N] = {}; + for (int t = 0; t < size<0>(C); ++t) { + for (int v = 0; v < size<1>(C); ++v) { + int m = C(t,v) % M; + int n = C(t,v) / M; + + if (not c_filled[m][n]) { + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[t % 8], + m, n, + t, v); + c_filled[m][n] = true; + } + } + } + + // A starting at 0,-size<1>(A)-1 + bool a_filled[M][K] = {}; + for (int t = 0; t < size<0>(A); ++t) { + for (int v = 0; v < size<1>(A); ++v) { + int m = A(t,v) % M; + int k = A(t,v) / M; + + if (not a_filled[m][k]) { + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[t % 8], + m, k - 1 - K, + t, v); + a_filled[m][k] = true; + } + } + } + + // B starting at -size<1>(B)-1,0 + bool b_filled[N][K] = {}; + for (int t = 0; t < size<0>(B); ++t) { + for (int v = 0; v < size<1>(B); ++v) { + int n = B(t,v) % N; + int k = B(t,v) / N; + + if (not b_filled[n][k]) { + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[t % 8], + k - 1 - K, n, + t, v); + b_filled[n][k] = true; + } + } + } + + // A labels + for (int m = 0, k = -1; m < M; ++m) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", m, k - 1 - K, m); + } + for (int k = 0, m = -1; k < K; ++k) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", m, k - 1 - K, k); + } + // B labels + for (int n = 0, k = -1; n < N; ++n) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", k - 1 - K, n, n); + } + for (int k = 0, n = -1; k < K; ++k) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", k - 1 - K, n, k); + } + + // Footer + printf(latex_footer); +} + +} // namespace cute + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx906_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx906_hip.hpp new file mode 100644 index 000000000..9c12bd5ed --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx906_hip.hpp @@ -0,0 +1,82 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_1,_1,_4>; + using ThrID = Layout<_1>; + using ALayout = Layout>; + using BLayout = Layout>; + using CLayout = Layout>; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = int16_t; + using ElementBVal = int16_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_1,_1,_2>; + using ThrID = Layout<_1>; + using ALayout = Layout>; + using BLayout = Layout>; + using CLayout = Layout>; +}; + +/////////////////////////////////////////////////////////////////////////////// + + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx926_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx926_hip.hpp new file mode 100644 index 000000000..01a2a6fbb --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx926_hip.hpp @@ -0,0 +1,116 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float; + using ElementBVal = float; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_16,_4>; + using ThrID = Layout<_64>; + using ALayout = Layout,_1>, + Stride,_1>>; + using BLayout = Layout,_1>, + Stride,_1>>; + using CLayout = Layout, _4>, + Stride,_64>>; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + static constexpr int vn = 2; + + using ElementDVal = float; + using ElementAVal = float; + using ElementBVal = float; + using ElementCVal = float; + + using Shape_MNK = Shape,Int<16*vn>,_4>; + using ThrID = Layout<_64>; + using ALayout = Layout, Int>, + Stride, Int<16*vn>>, _1>>; + using BLayout = Layout, Int>, + Stride, Int<16*vn>>, _1>>; + using CLayout = Layout, + Shape , Int>, _4>>, + Stride, Int<16*vn*vn>>, + Stride>,Int<64*vn*vn>>>>; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + static constexpr int vn = 4; + + using ElementDVal = float; + using ElementAVal = float; + using ElementBVal = float; + using ElementCVal = float; + + using Shape_MNK = Shape,Int<16*vn>,_4>; + using ThrID = Layout<_64>; + using ALayout = Layout, Int>, + Stride, Int<16*vn>>, _1>>; + using BLayout = Layout, Int>, + Stride, Int<16*vn>>, _1>>; + using CLayout = Layout, + Shape , Int>, _4>>, + Stride, Int<16*vn*vn>>, + Stride>,Int<64*vn*vn>>>>; +}; + +/////////////////////////////////////////////////////////////////////////////// + + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx928_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx928_hip.hpp new file mode 100644 index 000000000..cb5208e56 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx928_hip.hpp @@ -0,0 +1,602 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ +/////////////////////////////////////v_mmac_16x16x8_f32////////////////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float; + using ElementBVal = float; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_16,_8>; + using ThrID = Layout<_64>; + using ALayout = Layout,_2>, + Stride,_16>>; + + using BLayout = Layout,_2>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; +}; + +/////////////////////////////////////v_mmac_f32_16x16x8_tf32////////////////////////////////////////// +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; +}; +/////////////////////////////////////v_mmac_f32_16x16x16_f16////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_16>; + using ThrID = Layout<_64>; + using ALayout = Layout,_4>, + Stride,_16>>; + + using BLayout = Layout,_4>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; +}; +//////////////////////v_mmac_f32_32x32x16_f16 concatenate by 2*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_32,_32,_16>; + using ThrID = Layout<_64>; + using ALayout = Layout,Shape<_2,_4>>, + Stride,Stride<_16,_32>>>; + + using BLayout = Layout,Shape<_2,_4>>, + Stride,Stride<_16,_32>>>; + using CLayout = Layout, Shape,_4>>, + Stride, Stride,_256>>>; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_32,_32,_16>; + using ThrID = Layout<_64>; + using ALayout = Layout,Shape<_2,_4>>, + Stride,Stride<_1,_32>>>; + + using BLayout = Layout,Shape<_2,_4>>, + Stride,Stride<_1,_32>>>; + + using CLayout = Layout, Shape<_4,_2,_2>>, + Stride, Stride<_256,_32,_1>>>; +}; + +/////////////////////////////////////v_mmac_f32_16x16x16_bf16////////////////////////////////////////// +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; +////////////////////////////v_mmac_i32_16x16x32_i8/////////////////////////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + using Shape_MNK = Shape<_16,_16,_32>; + using ThrID = Layout<_64>; + using ALayout = Layout,_8>, + Stride,_16>>; + + using BLayout = Layout,_8>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; +}; +//////////////////////v_mmac_f32_32x32x32_i8 concatenate by 2*16x16x16////////////////////////////// +template <> +struct MMA_Traits { + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_32, _32, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>; + using BLayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>; + using CLayout = Layout, Shape<_4, _2, _2>>, + Stride, Stride<_128, _512, _16>>>; +}; + +//////////////////////v_mmac_f32_32x32x32_u8 concatenate by 2*16x16x16////////////////////////////// +template<> +struct MMA_Traits { + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_32, _32, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>; + using BLayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>; + using CLayout = Layout, Shape<_4, _2, _2>>, + Stride, Stride<_128, _512, _16>>>; +}; +///////////////////////////////////v_mmac_i32_16x16x32_u8//////////////////////////////////////////// +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// used for flash attention // +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride,_64>>; + + using BLayout = Layout, _4>, + Stride,_64>>; + using CLayout = Layout, _4>, + Stride,_64>>; +}; +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + + template <> + struct MMA_Traits + { + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_32>; + using ThrID = Layout<_64>; + using ALayout = Layout,_8>, + Stride,_16>>; + + using BLayout = Layout,_8>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; + }; + + template <> + struct MMA_Traits + : MMA_Traits + { + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + }; + template <> + struct MMA_Traits + : MMA_Traits + { + using ALayout = Layout,_8>, + Stride,_16>>; + }; + template <> + struct MMA_Traits + : MMA_Traits + { + using ALayout = Layout,_8>, + Stride,_16>>; + }; + template <> + struct MMA_Traits + { + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_64>; + using ThrID = Layout<_64>; + using ALayout = Layout,_16>, + Stride,_16>>; + + using BLayout = Layout,_16>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; + }; + + template <> + struct MMA_Traits + : MMA_Traits + { + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + }; + + template <> + struct MMA_Traits + { + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = uint8_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_64>; + using ThrID = Layout<_64>; + using ALayout = Layout,_16>, + Stride,_16>>; + + using BLayout = Layout,_16>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_64>>; + }; + + template <> + struct MMA_Traits + : MMA_Traits + { + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = uint8_t; + using ElementCVal = float; + }; + +//////////////////////v_mmac_f32_16x32x16_f16 concatenate by 2*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _32, _16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride, _16>>; + using BLayout = Layout, Shape<_2, _4>>, + Stride, Stride<_16, _32>>>; + + using CLayout = Layout, Shape<_8>>, + Stride, Stride<_64>>>; + +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +//////////////////////v_mmac_f32_16x32x16_f16 concatenate by 2*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _64, _16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride, _16>>; + using BLayout = Layout, Shape, _4>>, + Stride, Stride, _64>>>; + + using CLayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _128>>>; + +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _32, _16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride,_64>>; + using BLayout = Layout, Shape<_2, _4>>, + Stride, Stride<_16, _32>>>; + + using CLayout = Layout, Shape<_8>>, + Stride, Stride<_64>>>; + +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +//////////////////////v_mmac_f32_16x64x16_f16 concatenate by 4*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _64, _16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride, _16>>; + using BLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_64, _1>>>; + + using CLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _256>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +//////////////////////v_mmac_f32_16x64x32_f16 concatenate by 8*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _64, _32>; + using ThrID = Layout<_64>; + using ALayout = Layout, _8>, + Stride, _16>>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _1>>>; + + using CLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _256>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + + +/* +change BLayout to avoid bank conflict +*/ +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _16>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _16>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_64, _16>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_64, _16>>>; +}; +/* +change BLayout to avoid bank conflict +only used for make make_tiled_copy_B +do not use for mmac +*/ +template <> +struct MMA_Traits + : MMA_Traits +{ + using Shape_MNK = Shape<_16, _32, _64>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_32, _8>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using Shape_MNK = Shape<_16, _32, _64>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_32, _8>>>; +}; + + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float; + using ElementBVal = float; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_32,_16>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _4>, + Stride, _16> + >; + + using BLayout = Layout, Shape< _4, _2>>, + Stride ,Stride<_32, _1>> + >; + using CLayout = Layout, Shape<_2, _4>>, + Stride, Stride<_16, _128>> + >; +}; + + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx938_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx938_hip.hpp new file mode 100644 index 000000000..641823074 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx938_hip.hpp @@ -0,0 +1,375 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +////////////////////////////v_mmac_f32_16x16x64_f8/////////////////////////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + using Shape_MNK = Shape<_16,_16,_64>; + using ThrID = Layout<_64>; + + using ALayout = Layout,_16>, //_1表示to到t1 _64表示to到t16 _16表示t0内部之间 + Stride,_16>>; //Stride,_16>>; + using BLayout = Layout,_16>, + Stride,_16>>; //Stride,_16>>; + + using CLayout = Layout, _4>, + Stride, _64>>; +}; + +//////////////////////v_mmac_f32_16x16x32 concatenate by 2*16x16x16////////////////////////////// + template <> + struct MMA_Traits + { + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16,_16,_32>; + using ThrID = Layout<_64>; + using ALayout = Layout,Shape<_4, _2>>, + Stride,Stride<_16,_256>>>; + + using BLayout = Layout,_8>, + Stride,_16>>; + using CLayout = Layout, _4>, + Stride,_1>>; + }; + + template <> + struct MMA_Traits + : MMA_Traits + { + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + }; + +//////////////////////v_mmac_f32_16x32x16_f16 concatenate by 2*16x16x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _32, _16>; + using ThrID = Layout<_64>; + using ALayout = Layout, _4>, + Stride, _16>>; + using BLayout = Layout, Shape<_2, _4>>, + Stride, Stride<_16, _32>>>; + + using CLayout = Layout, Shape<_8>>, + Stride, Stride<_64>>>; + +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + using Shape_MNK = Shape<_16,_16,_64>; + using ThrID = Layout<_64>; + + using ALayout = Layout,_16>, //_1表示to到t1 _64表示to到t16 _16表示t0内部之间 + Stride,_16>>; //Stride,_16>>; + using BLayout = Layout,_16>, + Stride,_16>>; //Stride,_16>>; + + using CLayout = Layout, _4>, + Stride, _16>>; +}; + + template <> + struct MMA_Traits + : MMA_Traits + { + using ALayout = Layout,_16>, + Stride,_16>>; + }; + +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _32, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _8>, // (16 * 4) *8 16*4个线程 每个拷8个 + Stride, _16>>; + using BLayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>;//_1表示to到t1 _256表示to到t16 _16表示t0内部之间 + using CLayout = Layout, Shape<_8>>, + Stride, Stride<_64>>>; +}; + +//////////////////////v_mmac_f32_16x32x32_f8////////////////////////////// +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _32, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _8>, // (16 * 4) *8 16*4个线程 每个拷8个 + Stride, _16>>; + using BLayout = Layout, Shape<_2, _8>>, + Stride, Stride<_16, _32>>>;//_1表示to到t1 _256表示to到t16 _16表示t0内部之间 + using CLayout = Layout, Shape<_4,_2>>, + Stride, Stride<_16,_256>>>; +}; + +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _64, _16>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _4>, // (16 * 4) *8 16*4个线程 每个拷8个 + Stride, _16>>; + using BLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _64>>>;//_1表示to到t1 _256表示to到t16 _16表示t0内部之间 + using CLayout = Layout, Shape<_16>>, + Stride, Stride<_64>>>; +}; + +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _64, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _8>, + Stride, _16>>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _1>>>; + + using CLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _256>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _16>>>; +}; + + +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _64, _32>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _8>, + Stride, _16>>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _1>>>; + + using CLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _256>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_64, _16>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using BLayout = Layout, Shape, _4>>, + Stride, Stride, _16>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using Shape_MNK = Shape<_16, _32, _64>; + using BLayout = Layout, Shape<_8, _4>>, + Stride, Stride<_32, _8>>>; +}; + +template <> +struct MMA_Traits { + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16, _64, _64>; + using ThrID = Layout<_64>; + + using ALayout = Layout, _16>, + Stride, _16>>; + + using BLayout = Layout, Shape<_16, _4>>, + Stride, Stride<_64, _1>>>; + + using CLayout = Layout, Shape<_4, _4>>, + Stride, Stride<_16, _256>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using Shape_MNK = Shape<_16, _64, _64>; + using BLayout = Layout, Shape<_16, _4>>, + Stride< Stride<_1024, _1>, Stride<_64, _16>>>; +}; + +//////////////////////v_mmac_f32_16x64x64_f8 concatenate by 4*16x32x16////////////////////////////// +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _64, _64>; + using ThrID = Layout<_64>; + using ALayout = Layout, _16>, + Stride, _16>>; + using BLayout = Layout, Shape<_16, Shape<_2,_2>>>, + Stride, Stride<_64, Stride<_1,_32>>>>; + + using CLayout = Layout, Shape<_8, _2>>, + Stride, Stride<_16, _512>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + + using BLayout = Layout, Shape<_16, _4>>, + Stride, Stride<_64, _16>>>; + +}; + + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + + using Shape_MNK = Shape<_16, _64, _64>; + using ThrID = Layout<_64>; + using ALayout = Layout, _16>, + Stride, _16>>; + using BLayout = Layout, Shape<_16, Shape<_2,_2>>>, + Stride, Stride<_64, Stride<_1,_32>>>>; + + using CLayout = Layout, Shape<_8, _2>>, + Stride, Stride<_16, _512>>>; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + + using BLayout = Layout, Shape<_16, _4>>, + Stride, Stride<_64, _16>>>; + +}; + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_hip.hpp new file mode 100644 index 000000000..83eeda736 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_hip.hpp @@ -0,0 +1,234 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +namespace detail { + +template +struct supports_output_scaling { static constexpr bool value = false; }; + +template +struct supports_output_scaling().accumulate_)>> { static constexpr bool value = true; }; + +} // end namespace detail + +/** + * concept MMA_Traits + * { + * using ElementDVal = // Logical A-value type + * using ElementAVal = // Logical B-value type + * using ElementBVal = // Logical C-value type + * using ElementCVal = // Logical D-value type (NOTE: Not used? Assumed == ElementDVal) + * + * using ElementAFrg = // A-type consumed by MMA (if ommitted, same as ElementAVal) + * using ElementBFrg = // B_type consumed by MMA (if ommitted, same as ElementBVal) + * using ElementCFrg = // C_type consumed by MMA (if ommitted, same as ElementCVal) + * + * using Shape_MNK = // Logical MxNxK shape of the MMA + * + * using ThrID = // Logical thread id (tid) -> tidx + * + * using ALayout = // (Logical thread id (tid), Logical value id (vid)) -> Flat MK-coord + * using BLayout = // (Logical thread id (tid), Logical value id (vid)) -> Flat NK-coord + * using CLayout = // (Logical thread id (tid), Logical value id (vid)) -> Flat MN-coord + * }; + */ + +template +struct MMA_Traits +{ + static_assert(sizeof(MMAOperation) == 0, "MMA_Traits not implemented for this MMA_Operation."); +}; + +template +struct MMA_Traits> +{ + using ElementDVal = D; + using ElementAVal = A; + using ElementBVal = B; + using ElementCVal = C; + + // Logical shape of the MMA + using Shape_MNK = Shape<_1,_1,_1>; + + // Logical thread id (tid) -> tidx + using ThrID = Layout<_1>; + + // (Logical thread id (tid), Logical value id (vid)) -> coord + + // (tid,vid) -> (m,k) + using ALayout = Layout>; + // (tid,vid) -> (n,k) + using BLayout = Layout>; + // (tid,vid) -> (m,n) + using CLayout = Layout>; +}; + +// +// Generic mma_unpack for any MMA_Traits +// +template +CUTE_HOST_DEVICE constexpr +void +mma_unpack(MMA_Traits const& traits, + Tensor & D, + Tensor const& A, + Tensor const& B, + Tensor const& C) +{ + static_assert(is_rmem::value, "Expected registers in MMA_Atom::call"); + static_assert(is_rmem::value, "Expected registers in MMA_Atom::call"); + static_assert(is_rmem::value, "Expected registers in MMA_Atom::call"); + static_assert(is_rmem::value, "Expected registers in MMA_Atom::call"); + + // Register value types from the MMA_Operation register arrays + using RegTypeD = typename remove_extent::type; + using RegTypeA = typename remove_extent::type; + using RegTypeB = typename remove_extent::type; + using RegTypeC = typename remove_extent::type; + using MMATraits = MMA_Traits; + + [[maybe_unused]] constexpr int RegNumD = extent::value; + constexpr int RegNumA = extent::value; + constexpr int RegNumB = extent::value; + constexpr int RegNumC = extent::value; + + Tensor rA = recast(A); + Tensor rB = recast(B); + + CUTE_STATIC_ASSERT_V(size(rA) == Int{}); + CUTE_STATIC_ASSERT_V(size(rB) == Int{}); + + if constexpr (is_same::value) + { + static_assert(is_same::value, "GMMA C and D value_type must match."); + static_assert(is_same::value, "GMMA C and D layouts must match."); + // assert((void*)&C == (void*)&D); + + Tensor rC = recast(D); // NOTE: D and C are same, so use mutable D + + //CUTE_STATIC_ASSERT_V(size(rC) == Int{}); + + if constexpr (detail::supports_output_scaling::value) { + detail::explode_with_d_scaling(MMA_Op::fma, + rA, make_int_sequence{}, + rB, make_int_sequence{}, + rC, make_int_sequence{}, + traits.accumulate_); + } + else { + detail::explode(MMA_Op::fma, + rA, make_int_sequence{}, + rB, make_int_sequence{}, + rC, make_int_sequence{}); + } + } + else { + Tensor rD = recast(D); + Tensor rC = recast(C); + + CUTE_STATIC_ASSERT_V(size(rD) == Int{}); + CUTE_STATIC_ASSERT_V(size(rC) == Int{}); + if constexpr (detail::supports_output_scaling::value) { + detail::explode_with_d_scaling(MMA_Op::fma, + rD, make_int_sequence{}, + rA, make_int_sequence{}, + rB, make_int_sequence{}, + rC, make_int_sequence{}, + traits.accumulate_); + } + else { + detail::explode(MMA_Op::fma, + rD, make_int_sequence{}, + rA, make_int_sequence{}, + rB, make_int_sequence{}, + rC, make_int_sequence{}); + } + } +} + +// +// Accept mutable temporaries +// + +template +CUTE_HOST_DEVICE constexpr +void +mma_unpack(MMA_Traits const& traits, + Tensor && D, + Tensor const& A, + Tensor const& B, + Tensor const& C) +{ + mma_unpack(traits, D, A, B, C); +} + +namespace detail { + +template +struct FrgTypeA_or_Default { using type = typename X::ElementAVal; }; +template +struct FrgTypeA_or_Default> { using type = typename X::ElementAFrg; }; + +template +struct FrgTypeB_or_Default { using type = typename X::ElementBVal; }; +template +struct FrgTypeB_or_Default> { using type = typename X::ElementBFrg; }; + +template +struct FrgTypeC_or_Default { using type = typename X::ElementCVal; }; +template +struct FrgTypeC_or_Default> { using type = typename X::ElementCFrg; }; + +} // end namespace detail + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm61_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm61_hip.hpp new file mode 100644 index 000000000..b9eb5883c --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm61_hip.hpp @@ -0,0 +1,79 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_1,_1,_4>; + using ThrID = Layout<_1>; + using ALayout = Layout>; + using BLayout = Layout>; + using CLayout = Layout>; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int16_t; + using ElementBVal = int16_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_1,_1,_2>; + using ThrID = Layout<_1>; + using ALayout = Layout>; + using BLayout = Layout>; + using CLayout = Layout>; +}; + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm70_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm70_hip.hpp new file mode 100644 index 000000000..f3b3139ed --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm70_hip.hpp @@ -0,0 +1,204 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +namespace { + +// Logical thread id to thread idx (quadpair) +using SM70_QuadPair = Layout, + Stride<_1,_16>>; +// (T8,V4) -> (M8,K4) +using SM70_8x4_Row = Layout, + Stride<_1,_8>>; +// (T8,V4) -> (M8,K4) +using SM70_8x4_Col = Layout,_4>, + Stride,_1>>; +// (T8,V8) -> (M8,N8) +using SM70_8x8_16b = Layout, + Stride<_1,_8>>; +// (T8,V8) -> (M8,N8) +using SM70_8x8_32b = Layout,Shape <_2,_2, _2>>, + Stride,Stride<_8,_2,_32>>>; + +} + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Row; + using BLayout = SM70_8x4_Row; + using CLayout = SM70_8x8_16b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Col; + using BLayout = SM70_8x4_Col; + using CLayout = SM70_8x8_16b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Col; + using BLayout = SM70_8x4_Row; + using CLayout = SM70_8x8_16b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Row; + using BLayout = SM70_8x4_Col; + using CLayout = SM70_8x8_16b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Row; + using BLayout = SM70_8x4_Row; + using CLayout = SM70_8x8_32b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Col; + using BLayout = SM70_8x4_Col; + using CLayout = SM70_8x8_32b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Col; + using BLayout = SM70_8x4_Row; + using CLayout = SM70_8x8_32b; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = SM70_QuadPair; + using ALayout = SM70_8x4_Row; + using BLayout = SM70_8x4_Col; + using CLayout = SM70_8x8_32b; +}; + +/////////////////////////////////////////////////////////////////////////////// +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm75_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm75_hip.hpp new file mode 100644 index 000000000..0b160b727 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm75_hip.hpp @@ -0,0 +1,87 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_8,_8>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; + using BLayout = Layout,_2>, + Stride,_8>>; + using CLayout = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; +}; + +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_8,_8,_16>; + using ThrID = Layout<_32>; + using ALayout = Layout,_4>, + Stride,_8>>; + using BLayout = Layout,_4>, + Stride,_8>>; + using CLayout = Layout,_2>, + Stride,_8>>; +}; + +/////////////////////////////////////////////////////////////////////////////// + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm80_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm80_hip.hpp new file mode 100644 index 000000000..fc104f998 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm80_hip.hpp @@ -0,0 +1,452 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +#include + +#include + +namespace cute +{ + +namespace { + +// (T32,V1) -> (M8,N8) +using SM80_8x4 = Layout,_1>, + Stride,_0>>; +// (T32,V2) -> (M8,N8) +using SM80_8x8_Row = Layout,_2>, + Stride,_8>>; +// (T32,V4) -> (M8,N16) +using SM80_8x16_Row = Layout,_4>, + Stride,_8>>; +// (T32,V4) -> (M16,N8) +using SM80_16x8_Row = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; + +} + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp16 = fp16 * fp16 + fp16 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_16,_8,_8>; + using ThrID = Layout<_32>; + using ALayout = SM80_16x8_Row; + using BLayout = SM80_8x8_Row; + using CLayout = SM80_16x8_Row; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using Shape_MNK = Shape<_16,_8,_16>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape < _2,_2, _2>>, + Stride,Stride<_16,_8,_128>>>; + using BLayout = Layout,Shape <_2, _2>>, + Stride,Stride<_8,_64>>>; + using CLayout = SM80_16x8_Row; +}; + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp32 = fp16 * fp16 + fp32 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; +}; + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp32 = bf16 * bf16 + fp32 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; +}; + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp32 = tf32 * tf32 + fp32 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = cutlass::tfloat32_t; + using ElementBVal = cutlass::tfloat32_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_8,_4>; + using ThrID = Layout<_32>; + using ALayout = Layout,_2>, + Stride,_8>>; + using BLayout = SM80_8x4; + using CLayout = SM80_16x8_Row; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = float; + using ElementAVal = cutlass::tfloat32_t; + using ElementBVal = cutlass::tfloat32_t; + using ElementCVal = float; + + using Shape_MNK = Shape<_16,_8,_8>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape <_2, _2>>, + Stride,Stride<_8,_64>>>; + using BLayout = Layout, _2>, + Stride,_32>>; + using CLayout = SM80_16x8_Row; +}; + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp64 = fp64 * fp64 + fp64 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = double; + using ElementAVal = double; + using ElementBVal = double; + using ElementCVal = double; + + using Shape_MNK = Shape<_8,_8,_4>; + using ThrID = Layout<_32>; + using ALayout = SM80_8x4; + using BLayout = SM80_8x4; + using CLayout = SM80_8x8_Row; +}; + +// Custom complex fp64 MMA composed of 4 fp64 MMAs -- same layouts +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = complex; + using ElementAVal = complex; + using ElementBVal = complex; + using ElementCVal = complex; +}; + +// Custom complex fp64 MMA composed of 3 fp64 MMAs -- same layouts +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = typename SM80_8x8x4_GC64C64C64GC64_TN::GaussComplex; + using ElementAVal = complex; + using ElementBVal = complex; + using ElementCVal = typename SM80_8x8x4_GC64C64C64GC64_TN::GaussComplex; +}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////// s32 = s8 * s8 + s32 /////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_8,_8,_16>; + using ThrID = Layout<_32>; + using ALayout = SM80_8x16_Row; + using BLayout = SM80_8x16_Row; + using CLayout = SM80_8x8_Row; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_16,_8,_16>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape < _4,_2>>, + Stride,Stride<_16,_8>>>; + using BLayout = SM80_8x16_Row; + using CLayout = SM80_16x8_Row; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_16,_8,_32>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape < _4,_2, _2>>, + Stride,Stride<_16,_8,_256>>>; + using BLayout = Layout, Shape <_4, _2>>, + Stride, Stride<_8,_128>>>; + using CLayout = SM80_16x8_Row; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////// s32 = s8 * u8 + s32 /////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////// s32 = u8 * s8 + s32 /////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////// s32 = u8 * u8 + s32 /////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; +}; + +template <> +struct MMA_Traits + : MMA_Traits {}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////// s32 = b1 ^ b1 + s32 /////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = cute::uint1b_t; + using ElementBVal = cute::uint1b_t; + using ElementCVal = int32_t; + + using Shape_MNK = Shape<_16,_8,_256>; + using ThrID = Layout<_32>; + using ALayout = Layout>, + Stride<_64,Stride<_64,_16,_8,_2048>>>; + using BLayout = Layout>, + Stride<_32,Stride< _1,_1024>>>; + using CLayout = SM80_16x8_Row; +}; +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_gmma_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_gmma_hip.hpp new file mode 100644 index 000000000..a5858cbad --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_gmma_hip.hpp @@ -0,0 +1,5929 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +namespace cute { + +// Fence between the async destination accumulators of GMMA & source for their dependent use +template +CUTE_HOST_DEVICE +void +warpgroup_fence_operand(Tensor& frg) { + CUTE_STATIC_ASSERT(is_static::value); + if constexpr (is_same_v) { + auto f32_frg = recast(frg); + CUTE_UNROLL + for (int i = 0; i < size(f32_frg); ++i) { + warpgroup_fence_operand(f32_frg(i)); + } + } + else { + CUTE_STATIC_ASSERT(is_rmem::value); + auto u32_frg = recast(frg); + CUTE_UNROLL + for (int i = 0; i < size(u32_frg); ++i) { + warpgroup_fence_operand(u32_frg(i)); + } + } +} + +namespace GMMA { + +/////////////////////////////////////////// +// Common layouts for GMMA Shared Memory // +/////////////////////////////////////////// + +// M|N-major GMMA layouts in units of bits +using Layout_MN_INTER_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride<_1, _128>>>; +using Layout_MN_SW32_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride<_1, _256>>>; +using Layout_MN_SW64_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride<_1, _512>>>; +using Layout_MN_SW128_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride<_1,_1024>>>; + +// K-major GMMA layouts in units of bits +using Layout_K_INTER_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride< _128,_1>>>; +using Layout_K_SW32_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride< _256,_1>>>; +using Layout_K_SW64_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride< _512,_1>>>; +using Layout_K_SW128_Atom_Bits = ComposedLayout, smem_ptr_flag, Layout,Stride<_1024,_1>>>; + +// M|N-major layouts in units of Type +template +using Layout_MN_INTER_Atom = decltype(upcast::value>(Layout_MN_INTER_Atom_Bits{})); +template +using Layout_MN_SW32_Atom = decltype(upcast::value>(Layout_MN_SW32_Atom_Bits{})); +template +using Layout_MN_SW64_Atom = decltype(upcast::value>(Layout_MN_SW64_Atom_Bits{})); +template +using Layout_MN_SW128_Atom = decltype(upcast::value>(Layout_MN_SW128_Atom_Bits{})); + +// K-major layouts in units of Type +template +using Layout_K_INTER_Atom = decltype(upcast::value>(Layout_K_INTER_Atom_Bits{})); +template +using Layout_K_SW32_Atom = decltype(upcast::value>(Layout_K_SW32_Atom_Bits{})); +template +using Layout_K_SW64_Atom = decltype(upcast::value>(Layout_K_SW64_Atom_Bits{})); +template +using Layout_K_SW128_Atom = decltype(upcast::value>(Layout_K_SW128_Atom_Bits{})); + +// With GMMA::Major param +template +using Layout_INTER_Atom = typename conditional, + Layout_K_INTER_Atom>::type; +template +using Layout_SW32_Atom = typename conditional, + Layout_K_SW32_Atom>::type; +template +using Layout_SW64_Atom = typename conditional, + Layout_K_SW64_Atom>::type; +template +using Layout_SW128_Atom = typename conditional, + Layout_K_SW128_Atom>::type; + +// +// Tensor to LayoutType utility +// + +// smem_ptr_swizzle LayoutType +template +CUTE_HOST_DEVICE constexpr +LayoutType +layout_type(Tensor>>, + Layout> const&) +{ + static_assert(M == 4, "Unsupported layout swizzle"); + static_assert(0 <= B && B <= 3, "Unsupported layout swizzle"); + static_assert(S == 3, "Unsupported layout swizzle"); + + switch (B) { + case 0: return LayoutType::INTERLEAVE; + case 1: return LayoutType::B32; + case 2: return LayoutType::B64; + case 3: return LayoutType::B128; + } + return LayoutType::INTERLEAVE; // ERROR +} + +// smem_ptr non-swizzled LayoutType +template +CUTE_HOST_DEVICE constexpr +LayoutType +layout_type(Tensor>, + Layout> const&) +{ + return LayoutType::INTERLEAVE; +} + +/////////////////////////////////////////////////////////////////////////////// +// Construction method for GMMA Descriptors +/////////////////////////////////////////////////////////////////////////////// + +/** +* /////////////////////////////// +* // make_gmma_desc // +* /////////////////////////////// +* Each GmmaDescriptor Major-MN describes a canonical layout of the form +* +* LayoutType::INTERLEAVE : Swizzle<0,4,3> o smem_ptr o ((T,1,m),(8,k)):((1,T,SBO),(1T,LBO)) +* LayoutType::B32 : Swizzle<1,4,3> o smem_ptr o ((T,2,m),(8,k)):((1,T,LBO),(2T,SBO)) +* LayoutType::B64 : Swizzle<2,4,3> o smem_ptr o ((T,4,m),(8,k)):((1,T,LBO),(4T,SBO)) +* LayoutType::B128 : Swizzle<3,4,3> o smem_ptr o ((T,8,m),(8,k)):((1,T,LBO),(8T,SBO)) +* +* where +* T : sizeof(uint128_t) / sizeof(value_type) +* m : integer in [1,16] corresponding to GMMA shape +* k : integer in [1,32] corresponding to GMMA shape +* SBO: stride byte offset +* LBO: leading byte offset +* +* See GMMA::Layout_MN_XXX_Atom for building canonical GmmaDescriptor Major-MN layouts. +* For example, +* auto smem_layout = tile_to_shape(Layout_MN_SW128_Atom{}, Shape<_128,_64>{}); +* is guaranteed to be accepted by make_gmma_desc for appropriate value_type. +* +* ////////////////////////////// +* // make_gmma_desc // +* ////////////////////////////// +* Each GmmaDescriptor Major-K describes a canonical layout of the form +* +* LayoutType::INTERLEAVE : Swizzle<0,4,3> o smem_ptr o ((8,m),(T,2)):((1T,SBO),(1,LBO)) +* LayoutType::B32 : Swizzle<1,4,3> o smem_ptr o ((8,m),(T,2)):((2T,SBO),(1, T )) +* LayoutType::B64 : Swizzle<2,4,3> o smem_ptr o ((8,m),(T,2)):((4T,SBO),(1, T )) +* LayoutType::B128 : Swizzle<3,4,3> o smem_ptr o ((8,m),(T,2)):((8T,SBO),(1, T )) +* +* See GMMA::Layout_K_XXX_Atom for building canonical GmmaDescriptor Major-K layouts. +* For example, +* auto smem_layout = tile_to_shape(Layout_K_SW128_Atom{}, Shape<_128,_64>{}); +* is guaranteed to be accepted by make_gmma_desc for appropriate value_type. +*/ +template +CUTE_HOST_DEVICE constexpr +GmmaDescriptor +make_gmma_desc(Tensor const& tensor) +{ + static_assert(is_smem::value, "GMMA Descriptors can only be constructed on smem."); + static_assert(TLayout::rank == 2, "GMMA Descriptors can only be constructed on rank-2 tensors."); + using value_type = typename TEngine::value_type; + + Tensor u128_tensor = recast(tensor); + + // Result + GmmaDescriptor desc; + + // Layout type + constexpr GMMA::LayoutType LAYOUT_TYPE = GMMA::layout_type(u128_tensor); + desc.bitfield.layout_type_ = uint8_t(LAYOUT_TYPE); + + // Start address (4LSB not included) + uint32_t start_address = cast_smem_ptr_to_uint(u128_tensor.data().get()); + desc.bitfield.start_address_ = start_address >> 4; + + constexpr uint8_t base_offset = 0; + desc.bitfield.base_offset_ = base_offset; + + // LayoutType meta + constexpr int W = LAYOUT_TYPE == GMMA::LayoutType::INTERLEAVE ? 1 : + LAYOUT_TYPE == GMMA::LayoutType::B32 ? 2 : + LAYOUT_TYPE == GMMA::LayoutType::B64 ? 4 : + LAYOUT_TYPE == GMMA::LayoutType::B128 ? 8 : -1; + + if constexpr (MajorMode == GMMA::Major::MN) + { + /* In units of uint128_t, each GmmaDescriptor Major-MN describes a canonical layout of the form + * + * LayoutType::INTERLEAVE : Swizzle<0,4,3> o smem_ptr o ((1,n),(8,k)):((X,SBO),(1,LBO)) + * LayoutType::B32 : Swizzle<1,4,3> o smem_ptr o ((2,n),(8,k)):((1,LBO),(2,SBO)) + * LayoutType::B64 : Swizzle<2,4,3> o smem_ptr o ((4,n),(8,k)):((1,LBO),(4,SBO)) + * LayoutType::B128 : Swizzle<3,4,3> o smem_ptr o ((8,n),(8,k)):((1,LBO),(8,SBO)) + */ + static_assert(size<1>(u128_tensor) == Int<(256 / cute::sizeof_bits::value)>{}, // K size + "Not a canonical GMMA_MN Layout: Expected K-size 256/sizeof_bits."); + + // Construct the canonical GMMA T Layout with shape ((W,n),(8,2)) + Layout canonical_layout = logical_divide(layout(u128_tensor), make_tile(Layout,_1>{}, Layout,_1>{})); + + // Check ranks of canonical + CUTE_STATIC_ASSERT_V(rank<0>(canonical_layout) == Int<2>{}, "Not a canonical GMMA_MN Layout: No flat offset mode"); + CUTE_STATIC_ASSERT_V(rank<1>(canonical_layout) == Int<2>{}, "Not a canonical GMMA_MN Layout: No flat offset mode"); + // Check canonical mode strides + constexpr uint32_t stride_00 = stride<0,0>(canonical_layout); + constexpr uint32_t expected_stride_00 = LAYOUT_TYPE == GMMA::LayoutType::INTERLEAVE ? stride<0,0>(canonical_layout) : 1; + static_assert(stride_00 == expected_stride_00, "Not a canonical GMMA_MN Layout: Expected stride failure."); + constexpr uint32_t stride_10 = stride<1,0>(canonical_layout); + constexpr uint32_t expected_stride_10 = W; + static_assert(stride_10 == expected_stride_10, "Not a canonical GMMA_MN Layout: Expected stride failure."); + + // stride dimension byte offset and leading dimension byte offset (4LSB not included == uint128_t units) + constexpr uint32_t stride_01 = stride<0,1>(canonical_layout); + constexpr uint32_t stride_11 = stride<1,1>(canonical_layout); + + desc.bitfield.stride_byte_offset_ = (LAYOUT_TYPE == GMMA::LayoutType::INTERLEAVE) ? stride_01 : stride_11; + desc.bitfield.leading_byte_offset_ = (LAYOUT_TYPE == GMMA::LayoutType::INTERLEAVE) ? stride_11 : stride_01; + } + else if constexpr (MajorMode == GMMA::Major::K) + { + /* In units of uint128_t, each GmmaDescriptor Major-K describes a canonical layout of the form + * + * LayoutType::INTERLEAVE : Swizzle<0,4,3> o smem_ptr o ((8,n),2):((1,SBO),LBO) + * LayoutType::B32 : Swizzle<1,4,3> o smem_ptr o ((8,n),2):((2,SBO),1) + * LayoutType::B64 : Swizzle<2,4,3> o smem_ptr o ((8,n),2):((4,SBO),1) + * LayoutType::B128 : Swizzle<3,4,3> o smem_ptr o ((8,n),2):((8,SBO),1) + */ + CUTE_STATIC_ASSERT_V(size<0>(u128_tensor) % Int<8>{} == Int<0>{}, // N|M size + "Not a canonical GMMA_K Layout: Expected MN-size multiple of 8."); + CUTE_STATIC_ASSERT_V(size<1>(u128_tensor) == Int<2>{}, // K size + "Not a canonical GMMA_K Layout: Expected K-size 2 (in units of uint128_t)."); + + // Construct the canonical GMMA N Layout with shape ((8,n),(2,1)) + Layout canonical_layout = logical_divide(layout(u128_tensor), make_tile(Layout<_8,_1>{}, Layout<_2,_1>{})); + + // Check ranks of canonical + CUTE_STATIC_ASSERT_V(rank<0>(canonical_layout) == Int<2>{}, "Not a canonical GMMA_K Layout: No flat offset mode"); + CUTE_STATIC_ASSERT_V(rank<1>(canonical_layout) == Int<2>{}, "Not a canonical GMMA_K Layout: No flat offset mode"); + // Check canonical mode strides + constexpr uint32_t stride_00 = stride<0,0>(canonical_layout); + constexpr uint32_t expected_stride_00 = W; + static_assert(stride_00 == expected_stride_00, "Not a canonical GMMA_K Layout: Expected stride failure."); + constexpr uint32_t stride_10 = stride<1,0>(canonical_layout); + constexpr uint32_t expected_stride_10 = (LAYOUT_TYPE == GMMA::LayoutType::INTERLEAVE) ? stride<1,0>(canonical_layout) : 1; + static_assert(stride_10 == expected_stride_10, "Not a canonical GMMA_K Layout: Expected stride failure."); + + // stride dimension byte offset and leading dimension byte offset (4LSB not included == uint128_t units) + constexpr uint32_t stride_01 = stride<0,1>(canonical_layout); + + desc.bitfield.stride_byte_offset_ = stride_01; + desc.bitfield.leading_byte_offset_ = stride_10; + } else { + static_assert(MajorMode != GMMA::Major::MN && MajorMode != GMMA::Major::K, "Unrecognized MajorMode!"); + } + +#if 0 + // DEBUG and SANITY + assert((start_address & 0b0000001111) == 0); // Must be 16B aligned (4LSB are 0) no negotiation + assert((start_address & 0b1110000000) == 0); // Assert base_offset is 0, generalize later + if (thread0()) { + print("smem_desc input tensor: "); print(tensor.data()); print(" o "); print(tensor.layout()); print("\n"); + print("smem_desc uint128_t tensor: "); print(u128_tensor.data()); print(" o "); print(u128_tensor.layout()); print("\n"); + //print(" desc canonical layout: "); print(canonical_layout); print("\n"); + print(desc); + } +#endif + + return desc; +} + +/////////////////////////////////////////////////////////////////////////////// +// Higher level GMMA Descriptor utilities +/////////////////////////////////////////////////////////////////////////////// + +struct DescriptorIterator +{ + GmmaDescriptor desc_; + + // Dereference returns the GmmaDescriptor + CUTE_HOST_DEVICE constexpr + GmmaDescriptor const& operator*() const { return desc_; } + + // Advance and return a new GmmaDescriptor + template + CUTE_HOST_DEVICE constexpr + GmmaDescriptor operator[](Index const& i) const { return *(*this + i); } + + // Return an advanced iterator + template + CUTE_HOST_DEVICE constexpr + DescriptorIterator operator+(Index const& offset) const + { + return { GmmaDescriptor {desc_ + uint64_t(offset)} }; + } + + CUTE_HOST_DEVICE friend void + print(DescriptorIterator const&) { printf("GMMA::DescriptorIterator"); } +}; + +// The GMMA Traits below have custom fragment type flags for their smem desc tensors. +// These flags specialize a MakeTensor customization point to correctly make the fragment that is desired. +template +struct smem_desc : DescriptorIterator {}; + +// Recast a DescriptorIterator Tensor to uint64_t, it's RegType +template +CUTE_HOST_DEVICE constexpr +auto +recast(Tensor,TLayout> const& tensor, type_list) +{ + static_assert(is_same::value, "Can only cast descriptors to uint64_t."); + return make_tensor(tensor.data(), Layout<_1,_0>{}); +} + +} // end namespace GMMA + +// Customization point for creating a GMMA::smem_desc Tensor +template +struct MakeTensor> +{ + template + CUTE_HOST_DEVICE constexpr auto + operator()(Tensor const& smem_tensor) + { + static_assert(is_smem::value, "Expected SMEM Tensor to construct a GMMA Desc Tensor"); + return make_tensor(GMMA::DescriptorIterator{GMMA::make_gmma_desc(tensor<0>(smem_tensor))}, + recast(smem_tensor).layout()); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////////// MMA_TRAITS /////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +namespace GMMA { + +// Accumulator layouts +using CLayout_64x8 = Layout,Shape < _2,_2>>, + Stride,Stride<_64,_8>>>; + +using CLayout_64x16 = Layout,Shape < _2,_2, _2>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x32 = Layout,Shape < _2,_2, _4>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x64 = Layout,Shape < _2,_2, _8>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x96 = Layout,Shape < _2,_2, _12>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x128 = Layout,Shape < _2,_2, _16>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x192 = Layout,Shape < _2,_2, _24>>, + Stride,Stride<_64,_8,_512>>>; + +using CLayout_64x256 = Layout,Shape < _2,_2, _32>>, + Stride,Stride<_64,_8,_512>>>; + +// Register source layout for 32-bit value types +using ALayout_64x8 = Layout,Shape < _2, _2>>, + Stride,Stride< _8,_256>>>; + +// Register source layout for 16-bit value types +using ALayout_64x16 = CLayout_64x16; + +// Register source layout for 8-bit value types +using ALayout_64x32 = Layout,Shape < _4,_2, _2>>, + Stride,Stride<_64,_8,_1024>>>; + +// Shared memory source layouts for any value type +template +using ABLayout = Layout,Int>>, + Stride< _0,Stride< _1,Int>>>; + +} // namespace GMMA + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = half_t; + using ElementBVal = half_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 8, 16>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 16, 16>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 32, 16>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 64, 16>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout< 96, 16>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<128, 16>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<192, 16>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 16>; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = bfloat16_t; + using ElementBVal = bfloat16_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_16>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x16; + using BLayout = GMMA::ABLayout<256, 16>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout< 8, 8>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout< 8, 8>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout< 16, 8>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout< 16, 8>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout< 32, 8>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout< 32, 8>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout< 64, 8>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout< 64, 8>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout< 96, 8>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout< 96, 8>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout<128, 8>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout<128, 8>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout<192, 8>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout<192, 8>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 8>; + using BLayout = GMMA::ABLayout<256, 8>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = tfloat32_t; + using ElementBVal = tfloat32_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_8>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x8; + using BLayout = GMMA::ABLayout<256, 8>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = int8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = int8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = int32_t; + using ElementAVal = uint8_t; + using ElementBVal = uint8_t; + using ElementCVal = int32_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e4m3_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e4m3_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_8,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 8, 32>; + using CLayout = GMMA::CLayout_64x8; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_16,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 16, 32>; + using CLayout = GMMA::CLayout_64x16; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_32,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 32, 32>; + using CLayout = GMMA::CLayout_64x32; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_64,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 64, 32>; + using CLayout = GMMA::CLayout_64x64; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_96,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout< 96, 32>; + using CLayout = GMMA::CLayout_64x96; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_128,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<128, 32>; + using CLayout = GMMA::CLayout_64x128; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_192,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<192, 32>; + using CLayout = GMMA::CLayout_64x192; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = half_t; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = half_t; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementAFrg = GMMA::smem_desc; + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ABLayout< 64, 32>; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct MMA_Traits> +{ + using ElementDVal = float; + using ElementAVal = float_e5m2_t; + using ElementBVal = float_e5m2_t; + using ElementCVal = float; + + using ElementBFrg = GMMA::smem_desc; + + using Shape_MNK = Shape<_64,_256,_32>; + using ThrID = Layout<_128>; + using ALayout = GMMA::ALayout_64x32; + using BLayout = GMMA::ABLayout<256, 32>; + using CLayout = GMMA::CLayout_64x256; + + GMMA::ScaleOut accumulate_ = GMMA::ScaleOut::One; +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_hip.hpp new file mode 100644 index 000000000..a5a7893d4 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_hip.hpp @@ -0,0 +1,138 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +#include + +namespace cute { + +/////////////////////////////////////////////////////////////////////////////// +//////////////////////// fp64 = fp64 * fp64 + fp64 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits +{ + using ElementDVal = double; + using ElementAVal = double; + using ElementBVal = double; + using ElementCVal = double; + + using Shape_MNK = Shape<_16,_8,_4>; + using ThrID = Layout<_32>; + using ALayout = Layout,_2>, + Stride,_8>>; + using BLayout = Layout,_1>, + Stride,_0>>; + using CLayout = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = double; + using ElementAVal = double; + using ElementBVal = double; + using ElementCVal = double; + + using Shape_MNK = Shape<_16,_8,_8>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape <_2, _2>>, + Stride,Stride<_8,_64>>>; + using BLayout = Layout, _2>, + Stride,_32>>; + using CLayout = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; +}; + +template <> +struct MMA_Traits +{ + using ElementDVal = double; + using ElementAVal = double; + using ElementBVal = double; + using ElementCVal = double; + + using Shape_MNK = Shape<_16,_8,_16>; + using ThrID = Layout<_32>; + using ALayout = Layout,Shape <_2, _4>>, + Stride,Stride<_8,_64>>>; + using BLayout = Layout, _4>, + Stride,_32>>; + using CLayout = Layout,Shape < _2,_2>>, + Stride,Stride<_16,_8>>>; +}; + +/////////////////////////////////////////////////////////////////////////////////// +//////////////////////// cfp64 = cfp64 * cfp64 + cfp64 //////////////////////////// +/////////////////////////////////////////////////////////////////////////////////// + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = complex; + using ElementAVal = complex; + using ElementBVal = complex; + using ElementCVal = complex; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = complex; + using ElementAVal = complex; + using ElementBVal = complex; + using ElementCVal = complex; +}; + +template <> +struct MMA_Traits + : MMA_Traits +{ + using ElementDVal = complex; + using ElementAVal = complex; + using ElementBVal = complex; + using ElementCVal = complex; +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/config_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/config_hip.hpp new file mode 100644 index 000000000..df6b9c57b --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/config_hip.hpp @@ -0,0 +1,184 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if defined(__HIPCC__) || defined(_NVHPC_CUDA) //DCU_ASM +# define CUTE_HOST_DEVICE __forceinline__ __host__ __device__ +# define CUTE_DEVICE __forceinline__ __device__ +# define CUTE_HOST __forceinline__ __host__ +#else +# define CUTE_HOST_DEVICE inline +# define CUTE_DEVICE inline +# define CUTE_HOST inline +#endif // CUTE_HOST_DEVICE, CUTE_DEVICE + +#if defined(__CUDACC_RTC__) +# define CUTE_HOST_RTC CUTE_HOST_DEVICE +#else +# define CUTE_HOST_RTC CUTE_HOST +#endif + +#ifdef DCU_ASM +# define CUTE_UNROLL +# define CUTE_NO_UNROLL +#elif !defined(__CUDACC_RTC__) && (defined(__HIP_DEVICE_COMPILE__) || defined(_NVHPC_CUDA)) +# define CUTE_UNROLL #pragma unroll +# define CUTE_NO_UNROLL #pragma unroll 1 +#elif defined(__CUDACC_RTC__) +# define CUTE_UNROLL _Pragma("unroll") +# define CUTE_NO_UNROLL _Pragma("unroll 1") +#else +# define CUTE_UNROLL +# define CUTE_NO_UNROLL +#endif // CUTE_UNROLL + +#if defined(__HIP_DEVICE_COMPILE__) || defined(_NVHPC_CUDA) +# define CUTE_INLINE_CONSTANT static const __device__ +#else +# define CUTE_INLINE_CONSTANT static constexpr +#endif + +// __grid_constant__ was introduced in CUDA 11.7. +#if ((__CUDACC_VER_MAJOR__ >= 12) || ((__CUDACC_VER_MAJOR__ == 11) && (__CUDACC_VER_MINOR__ >= 7))) +# define CUTE_GRID_CONSTANT_SUPPORTED +#endif + +// __grid_constant__ can be enabled only on SM70+. +#if (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 700)) +# define CUTE_GRID_CONSTANT_ENABLED +#endif + +#if ! defined(CUTE_GRID_CONSTANT) +# if defined(CUTE_GRID_CONSTANT_SUPPORTED) && defined(CUTE_GRID_CONSTANT_ENABLED) +# define CUTE_GRID_CONSTANT __grid_constant__ +# else +# define CUTE_GRID_CONSTANT +# endif +#endif + +// Some versions of GCC < 11 have trouble deducing that a +// function with "auto" return type and all of its returns in an "if +// constexpr ... else" statement must actually return. Thus, GCC +// emits spurious "missing return statement" build warnings. +// Developers can suppress these warnings by using the +// CUTE_GCC_UNREACHABLE macro, which must be followed by a semicolon. +// It's harmless to use the macro for other GCC versions or other +// compilers, but it has no effect. +#if ! defined(CUTE_GCC_UNREACHABLE) +# if defined(__clang__) || defined(__GNUC__) +# define CUTE_GCC_UNREACHABLE __builtin_unreachable() +# else +# define CUTE_GCC_UNREACHABLE +# endif +#endif + +#ifdef _MSC_VER +// Provides support for alternative operators 'and', 'or', and 'not' +#include +#endif // _MSC_VER + +#if defined(__CUDACC_RTC__) +#define CUTE_STL_NAMESPACE cuda::std +#define CUTE_STL_NAMESPACE_IS_CUDA_STD +#else +#define CUTE_STL_NAMESPACE std +#endif + +// +// Assertion helpers +// + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#define CUTE_STATIC_ASSERT static_assert +#define CUTE_STATIC_ASSERT_V(x,...) static_assert(decltype(x)::value, ##__VA_ARGS__) + +#if ( defined(__HIP_DEVICE_COMPILE__) and not defined(DCU_ASM) ) +# define CUTE_RUNTIME_ASSERT(x) __brkpt() +#else +# define CUTE_RUNTIME_ASSERT(x) assert(0 && x) +#endif + +// +// IO +// + +#if !defined(__CUDACC_RTC__) +#include +#include +#include +#endif + +// +// Support +// + +#include + +// +// Basic types +// + +#include +#include +#include +#include +#include +#include +#include +// +// Debugging utilities +// + +#include +#include + +namespace cute{ + typedef float v4f __attribute__((ext_vector_type(4))); + typedef int v4i __attribute__((ext_vector_type(4))); + typedef float v2f __attribute__((ext_vector_type(2))); + typedef uint32_t uint32x4_t __attribute__((ext_vector_type(4))); + typedef int intx4_t __attribute__((ext_vector_type(4))); + typedef int intx2_t __attribute__((ext_vector_type(2))); + typedef __fp16 __fp16x4_t __attribute__((ext_vector_type(4))); + typedef short __bf16x4_t __attribute__((ext_vector_type(4))); +}; + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/alignment_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/alignment_hip.hpp new file mode 100644 index 000000000..bad3f3e25 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/alignment_hip.hpp @@ -0,0 +1,76 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +// Test if a pointer is aligned to N bytes +template +CUTE_HOST_DEVICE constexpr +bool +is_byte_aligned(void const* const ptr) +{ + static_assert(N > 0 && (N & (N - 1)) == 0, "N must be a power of 2 in alignment check"); + return (reinterpret_cast(ptr) & (N-1)) == 0; +} + +#if defined(__HIPCC__) +# define CUTE_ALIGNAS(n) __align__(n) +#else +# define CUTE_ALIGNAS(n) alignas(n) +#endif + +template +struct aligned_struct {}; + +template <> struct CUTE_ALIGNAS( 1) aligned_struct< 1> {}; +template <> struct CUTE_ALIGNAS( 2) aligned_struct< 2> {}; +template <> struct CUTE_ALIGNAS( 4) aligned_struct< 4> {}; +template <> struct CUTE_ALIGNAS( 8) aligned_struct< 8> {}; +template <> struct CUTE_ALIGNAS( 16) aligned_struct< 16> {}; +template <> struct CUTE_ALIGNAS( 32) aligned_struct< 32> {}; +template <> struct CUTE_ALIGNAS( 64) aligned_struct< 64> {}; +template <> struct CUTE_ALIGNAS(128) aligned_struct<128> {}; +template <> struct CUTE_ALIGNAS(256) aligned_struct<256> {}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_aligned_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_aligned_hip.hpp new file mode 100644 index 000000000..e24ae5159 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_aligned_hip.hpp @@ -0,0 +1,48 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include + +namespace cute +{ + +template +struct CUTE_ALIGNAS(Alignment) array_aligned : cute::array {}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_hip.hpp new file mode 100644 index 000000000..cae803f92 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_hip.hpp @@ -0,0 +1,496 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute +{ + +template +struct array +{ + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; + using iterator = pointer; + using const_iterator = const_pointer; + + CUTE_HOST_DEVICE constexpr + reference operator[](size_type pos) + { + return begin()[pos]; + } + + CUTE_HOST_DEVICE constexpr + const_reference operator[](size_type pos) const + { + return begin()[pos]; + } + + CUTE_HOST_DEVICE constexpr + reference front() + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + const_reference front() const + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + reference back() + { + // return *rbegin(); + return operator[](N-1); + } + + CUTE_HOST_DEVICE constexpr + const_reference back() const + { + // return *rbegin(); + return operator[](N-1); + } + + CUTE_HOST_DEVICE constexpr + T* data() + { + return __elems_; + } + + CUTE_HOST_DEVICE constexpr + T const* data() const + { + return __elems_; + } + + CUTE_HOST_DEVICE constexpr + iterator begin() + { + return data(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator begin() const + { + return data(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cbegin() + { + return begin(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cbegin() const + { + return begin(); + } + + CUTE_HOST_DEVICE constexpr + iterator end() + { + return data() + size(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator end() const + { + return data() + size(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cend() + { + return end(); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cend() const + { + return end(); + } + + CUTE_HOST_DEVICE constexpr + bool empty() const + { + return size() == 0; + } + + CUTE_HOST_DEVICE constexpr + size_type size() const + { + return N; + } + + CUTE_HOST_DEVICE constexpr + size_type max_size() const + { + return size(); + } + + CUTE_HOST_DEVICE constexpr + void fill(const T& value) + { + for (auto& e : *this) { + e = value; + } + } + + CUTE_HOST_DEVICE constexpr + void clear() + { + fill(T(0)); + } + + CUTE_HOST_DEVICE constexpr + void swap(array& other) + { + using CUTE_STL_NAMESPACE::swap; + for (size_type i = 0; i < size(); ++i) { + swap((*this)[i], other[i]); + } + } + + value_type __elems_[N > 0 ? N : 1]; +}; + + +template +struct array +{ + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; + using const_iterator = const_pointer; + using iterator = const_iterator; + + CUTE_HOST_DEVICE constexpr + reference operator[](size_type pos) + { + return begin()[pos]; + } + + CUTE_HOST_DEVICE constexpr + const_reference operator[](size_type pos) const + { + return begin()[pos]; + } + + CUTE_HOST_DEVICE constexpr + reference front() + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + const_reference front() const + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + reference back() + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + const_reference back() const + { + return *begin(); + } + + CUTE_HOST_DEVICE constexpr + T* data() + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + T const* data() const + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + iterator begin() + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator begin() const + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator cbegin() + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator cbegin() const + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + iterator end() + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator end() const + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator cend() + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + const_iterator cend() const + { + return nullptr; + } + + CUTE_HOST_DEVICE constexpr + bool empty() const + { + return true; + } + + CUTE_HOST_DEVICE constexpr + size_type size() const + { + return 0; + } + + CUTE_HOST_DEVICE constexpr + size_type max_size() const + { + return 0; + } + + CUTE_HOST_DEVICE constexpr + void fill(const T& value) + {} + + CUTE_HOST_DEVICE constexpr + void clear() + {} + + CUTE_HOST_DEVICE constexpr + void swap(array& other) + {} +}; + +template +CUTE_HOST_DEVICE constexpr +bool operator==(array const& lhs, array const& rhs) +{ + for (size_t i = 0; i < N; ++i) { + if (lhs[i] != rhs[i]) { + return false; + } + } + return true; +} + +template +CUTE_HOST_DEVICE constexpr +void clear(array& a) +{ + a.fill(T(0)); +} + +template +CUTE_HOST_DEVICE constexpr +void fill(array& a, T const& value) +{ + a.fill(value); +} + +template +CUTE_HOST_DEVICE constexpr +void swap(array& a, array& b) +{ + a.swap(b); +} + +/// @return A cute::array of the elements of @c t in reverse order. +template +CUTE_HOST_DEVICE constexpr cute::array +reverse(cute::array const& t) { + if constexpr (N == 0u) { + return t; + } + else { + cute::array t_r{}; + for (size_t k = 0; k < N; ++k) { + t_r[k] = t[N - k - 1]; + } + return t_r; + } +} + +} // end cute + + +// +// Specialize tuple-related functionality for cute::array +// + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +namespace cute +{ + +template +CUTE_HOST_DEVICE constexpr +T& get(array& a) +{ + static_assert(I < N, "Index out of range"); + return a[I]; +} + +template +CUTE_HOST_DEVICE constexpr +T const& get(array const& a) +{ + static_assert(I < N, "Index out of range"); + return a[I]; +} + +template +CUTE_HOST_DEVICE constexpr +T&& get(array&& a) +{ + static_assert(I < N, "Index out of range"); + return std::move(a[I]); +} + +} // end namespace cute + +namespace CUTE_STL_NAMESPACE +{ + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +} // end namespace CUTE_STL_NAMESPACE + +#ifdef CUTE_STL_NAMESPACE_IS_CUDA_STD +namespace std +{ + +#if defined(__CUDACC_RTC__) +template +struct tuple_size; + +template +struct tuple_element; +#endif + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +} // end namespace std +#endif // CUTE_STL_NAMESPACE_IS_CUDA_STD diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_subbyte_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_subbyte_hip.hpp new file mode 100644 index 000000000..83b156108 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/array_subbyte_hip.hpp @@ -0,0 +1,577 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Statically sized array of elements that accommodates subbyte trivial types + in a packed storage. +*/ + +#pragma once + +#include + +#include // sizeof_bits +#include +#include // dummy_type + +namespace cute +{ + +// +// Underlying subbyte storage type +// +template +using subbyte_storage_type_t = conditional_t<(sizeof_bits_v <= 8), uint8_t, + conditional_t<(sizeof_bits_v <= 16), uint16_t, + conditional_t<(sizeof_bits_v <= 32), uint32_t, + conditional_t<(sizeof_bits_v <= 64), uint64_t, + conditional_t<(sizeof_bits_v <= 128), uint128_t, + dummy_type>>>>>; + +template +struct subbyte_iterator; + +// +// subbyte_reference +// Proxy object for sub-byte element references +// +template +struct subbyte_reference +{ + // Iterator Element type (const or non-const) + using element_type = T; + // Iterator Value type without type qulifier. + using value_type = remove_cv_t; + // Storage type (const or non-const) + using storage_type = conditional_t<(is_const_v), subbyte_storage_type_t const, subbyte_storage_type_t>; + + static_assert(!is_same_v, "Storage type is not supported"); + + static_assert(sizeof_bits_v <= sizeof_bits_v, + "Size of Element must not be greater than Storage."); + + // Number of logical elements per stored object + static constexpr uint8_t ElementsPerStoredItem = sizeof_bits_v / sizeof_bits_v; + // Bitmask for covering one item + static constexpr storage_type BitMask = storage_type((storage_type(1) << sizeof_bits_v) - 1); + +private: + + friend class subbyte_iterator; + + // Pointer to storage element + storage_type* ptr_ = nullptr; + + // Index into elements packed into storage_type element. RI: 0 <= idx_ < ElementsPerStoredItem + uint8_t idx_ = 0; + + // Ctor + template + CUTE_HOST_DEVICE constexpr + subbyte_reference(PointerType* ptr, uint8_t idx = 0) : ptr_(reinterpret_cast(ptr)), idx_(idx) {} + +public: + + // Copy Ctor + CUTE_HOST_DEVICE constexpr + subbyte_reference(subbyte_reference const& other) { + *this = element_type(other); + } + + // Copy Assignment + CUTE_HOST_DEVICE constexpr + subbyte_reference& operator=(subbyte_reference const& other) { + return *this = element_type(other); + } + + // Dtor + ~subbyte_reference() = default; + + // Assignment + template + CUTE_HOST_DEVICE constexpr + enable_if_t, subbyte_reference&> operator=(element_type x) { + static_assert(is_same_v, "Do not specify template arguments!"); + storage_type item = (reinterpret_cast(x) & BitMask); + storage_type kUpdateMask = storage_type(~(BitMask << (idx_ * sizeof_bits_v))); + *ptr_ = storage_type((*ptr_ & kUpdateMask) | (item << (idx_ * sizeof_bits_v))); + return *this; + } + + CUTE_HOST_DEVICE + element_type get() const { + if constexpr (is_same_v) { // Extract to bool -- potentially faster impl + return bool((*ptr_) & (BitMask << (idx_ * sizeof_bits_v))); + } else { // Extract to element_type + storage_type item = storage_type((*ptr_ >> (idx_ * sizeof_bits_v)) & BitMask); + return reinterpret_cast(item); + } + } + + // Extract to type element_type + CUTE_HOST_DEVICE constexpr + operator element_type() const { + return get(); + } +}; + + +// +// subbyte_iterator +// Random-access iterator over subbyte references +// +template +struct subbyte_iterator +{ + // Iterator Element type (const or non-const) + using element_type = T; + // Iterator Value type without type qulifier. + using value_type = remove_cv_t; + // Storage type (const or non-const) + using storage_type = conditional_t<(is_const_v), subbyte_storage_type_t const, subbyte_storage_type_t>; + // Reference proxy type + using reference = subbyte_reference; + + static_assert(!is_same_v, "Storage type is not supported"); + + static_assert(sizeof_bits_v <= sizeof_bits_v, + "Size of Element must not be greater than Storage."); + + // Number of logical elements per stored object + static constexpr uint8_t ElementsPerStoredItem = sizeof_bits_v / sizeof_bits_v; + +private: + + // Pointer to storage element + storage_type* ptr_ = nullptr; + + // Index into elements packed into storage_type element. RI: 0 <= idx_ < ElementsPerStoredItem + uint8_t idx_ = 0; + +public: + + template + CUTE_HOST_DEVICE constexpr + subbyte_iterator(PointerType* ptr, uint8_t idx = 0): ptr_(reinterpret_cast(ptr)), idx_(idx) { } + + subbyte_iterator() = default; + CUTE_HOST_DEVICE constexpr + subbyte_iterator& operator++() { + ++idx_; + if (idx_ == ElementsPerStoredItem) { + ++ptr_; + idx_ = 0; + } + return *this; + } + + CUTE_HOST_DEVICE constexpr + subbyte_iterator& operator--() { + if (idx_) { + --idx_; + } else { + --ptr_; + idx_ = ElementsPerStoredItem - 1; + } + return *this; + } + + CUTE_HOST_DEVICE constexpr + subbyte_iterator operator++(int) { + subbyte_iterator ret(*this); + ++(*this); + return ret; + } + + CUTE_HOST_DEVICE constexpr + subbyte_iterator operator--(int) { + subbyte_iterator ret(*this); + --(*this); + return ret; + } + + CUTE_HOST_DEVICE constexpr + subbyte_iterator& operator+=(uint64_t k) { + k += idx_; + ptr_ += k / ElementsPerStoredItem; + idx_ = k % ElementsPerStoredItem; + return *this; + } + + CUTE_HOST_DEVICE constexpr + subbyte_iterator operator+(uint64_t k) const { + return subbyte_iterator(ptr_,idx_) += k; + } + + CUTE_HOST_DEVICE constexpr + reference operator*() const { + return reference(ptr_, idx_); + } + + CUTE_HOST_DEVICE constexpr + reference operator[](uint64_t k) const { + return *(*this + k); + } + + CUTE_HOST_DEVICE constexpr + friend bool operator==(subbyte_iterator const& x, subbyte_iterator const& y) { + return x.ptr_ == y.ptr_ && x.idx_ == y.idx_; + } + + CUTE_HOST_DEVICE constexpr + friend bool operator!=(subbyte_iterator const& x, subbyte_iterator const& y) { + return !(x == y); + } +}; + +// +// array_subbyte +// Statically sized array for non-byte-aligned data types +// +template +struct array_subbyte +{ + using element_type = T; + using value_type = remove_cv_t; + using pointer = element_type*; + using const_pointer = element_type const*; + + using size_type = size_t; + using difference_type = ptrdiff_t; + + // + // References + // + using reference = subbyte_reference; + using const_reference = subbyte_reference; + + // + // Iterators + // + using iterator = subbyte_iterator; + using const_iterator = subbyte_iterator; + + // Storage type (const or non-const) + using storage_type = conditional_t<(is_const_v), subbyte_storage_type_t const, subbyte_storage_type_t>; + + static_assert(!is_same_v, "Storage type is not supported"); + + // Number of logical elements per stored object + static constexpr uint8_t ElementsPerStoredItem = sizeof_bits_v / sizeof_bits_v; + + // Bitmask for covering one item + static constexpr storage_type BitMask = ((storage_type(1) << sizeof_bits::value) - 1); + + // Number of storage elements + static constexpr size_type StorageElements = (N + ElementsPerStoredItem - 1) / ElementsPerStoredItem; + +private: + + // Internal storage + storage_type storage[StorageElements]; + +public: + + CUTE_HOST_DEVICE constexpr + array_subbyte() { } + + CUTE_HOST_DEVICE constexpr + array_subbyte(array_subbyte const& x) { + CUTE_UNROLL + for (size_type i = 0; i < StorageElements; ++i) { + storage[i] = x.storage[i]; + } + } + + CUTE_HOST_DEVICE constexpr + size_type size() const { + return N; + } + + CUTE_HOST_DEVICE constexpr + size_type max_size() const { + return N; + } + + CUTE_HOST_DEVICE constexpr + bool empty() const { + return !N; + } + + // Efficient clear method + CUTE_HOST_DEVICE constexpr + void clear() { + CUTE_UNROLL + for (size_type i = 0; i < StorageElements; ++i) { + storage[i] = storage_type(0); + } + } + + // Efficient fill method + CUTE_HOST_DEVICE constexpr + void fill(T const& value) { + storage_type item = (reinterpret_cast(value) & BitMask); + + // Reproduce the value over the bits of the storage item + CUTE_UNROLL + for (size_type s = sizeof_bits_v; s < sizeof_bits_v; s *= 2) { + item |= item << s; + } + + CUTE_UNROLL + for (size_type i = 0; i < StorageElements; ++i) { + storage[i] = item; + } + } + + CUTE_HOST_DEVICE constexpr + reference at(size_type pos) { + return iterator(storage)[pos]; + } + + CUTE_HOST_DEVICE constexpr + const_reference at(size_type pos) const { + return const_iterator(storage)[pos]; + } + + CUTE_HOST_DEVICE constexpr + reference operator[](size_type pos) { + return at(pos); + } + + CUTE_HOST_DEVICE constexpr + const_reference operator[](size_type pos) const { + return at(pos); + } + + CUTE_HOST_DEVICE constexpr + reference front() { + return at(0); + } + + CUTE_HOST_DEVICE constexpr + const_reference front() const { + return at(0); + } + + CUTE_HOST_DEVICE constexpr + reference back() { + return at(N-1); + } + + CUTE_HOST_DEVICE constexpr + const_reference back() const { + return at(N-1); + } + + CUTE_HOST_DEVICE constexpr + pointer data() { + return reinterpret_cast(storage); + } + + CUTE_HOST_DEVICE constexpr + const_pointer data() const { + return reinterpret_cast(storage); + } + + CUTE_HOST_DEVICE constexpr + storage_type* raw_data() { + return storage; + } + + CUTE_HOST_DEVICE constexpr + storage_type const* raw_data() const { + return storage; + } + + CUTE_HOST_DEVICE constexpr + iterator begin() { + return iterator(storage); + } + + CUTE_HOST_DEVICE constexpr + const_iterator begin() const { + return const_iterator(storage); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cbegin() const { + return begin(); + } + + CUTE_HOST_DEVICE constexpr + iterator end() { + return iterator(storage + N / ElementsPerStoredItem, N % ElementsPerStoredItem); + } + + CUTE_HOST_DEVICE constexpr + const_iterator end() const { + return const_iterator(storage + N / ElementsPerStoredItem, N % ElementsPerStoredItem); + } + + CUTE_HOST_DEVICE constexpr + const_iterator cend() const { + return end(); + } + + // + // Comparison operators + // + +}; + +// +// Operators +// + +template +CUTE_HOST_DEVICE constexpr +void clear(array_subbyte& a) +{ + a.clear(); +} + +template +CUTE_HOST_DEVICE constexpr +void fill(array_subbyte& a, T const& value) +{ + a.fill(value); +} + +} // namespace cute + +// +// Specialize tuple-related functionality for cute::array_subbyte +// + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +namespace cute +{ + +template +CUTE_HOST_DEVICE constexpr +T& get(array_subbyte& a) +{ + static_assert(I < N, "Index out of range"); + return a[I]; +} + +template +CUTE_HOST_DEVICE constexpr +T const& get(array_subbyte const& a) +{ + static_assert(I < N, "Index out of range"); + return a[I]; +} + +template +CUTE_HOST_DEVICE constexpr +T&& get(array_subbyte&& a) +{ + static_assert(I < N, "Index out of range"); + return std::move(a[I]); +} + +} // end namespace cute + +namespace CUTE_STL_NAMESPACE +{ + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +} // end namespace CUTE_STL_NAMESPACE + +#ifdef CUTE_STL_NAMESPACE_IS_CUDA_STD +namespace std +{ + +#if defined(__CUDACC_RTC__) +template +struct tuple_size; + +template +struct tuple_element; +#endif + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> +{ + using type = T; +}; + +} // end namespace std +#endif // CUTE_STL_NAMESPACE_IS_CUDA_STD diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/bit_field_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/bit_field_hip.hpp new file mode 100644 index 000000000..9946199f2 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/bit_field_hip.hpp @@ -0,0 +1,145 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Portable bit field that supports byte and word straddling that can + be used in unions to bit-wise define parameters. +*/ + +#pragma once + +#include + +#include // uint_bit_t + +namespace cute +{ + +class dummy_type {}; + +template +struct bit_field +{ + static_assert(0 < NumBits && NumBits <= 64, "bit_fields with more than 64 bits are not supported."); + + // value_type: Use the smallest value type that fits NumBits + static constexpr uint32_t value_type_bits = (NumBits <= 8) ? 8 : + (NumBits <= 16) ? 16 : + (NumBits <= 32) ? 32 : 64; + using value_type = cute::uint_bit_t; + // storage_type: Use the smallest storage_type that avoids boundary crossing + static constexpr uint32_t storage_type_bits = (BitStart / 8 == (BitStart + NumBits - 1) / 8) ? 8 : + (BitStart / 16 == (BitStart + NumBits - 1) / 16) ? 16 : + (BitStart / 32 == (BitStart + NumBits - 1) / 32) ? 32 : 64; + using storage_type = cute::uint_bit_t; + + static_assert(sizeof(OtherValueType) == sizeof(value_type) || is_same::value, + "sizeof(OtherValueType) must be same as sizeof(value_type)."); + + // Number of storage values needed: ceil_div(BitStart + NumBits, storage_type_bits) + static constexpr uint32_t N = (BitStart + NumBits + storage_type_bits - 1) / storage_type_bits; + // Index of storage value for BitStart + static constexpr uint32_t idx = BitStart / storage_type_bits; + // Bit of data_[idx] for BitStart + static constexpr uint32_t bit_lo = BitStart % storage_type_bits; + // Number of bits in data_[idx] used for NumBits if straddling, else 0 + static constexpr uint32_t bit_hi = (idx + 1 < N) ? (storage_type_bits - bit_lo) : 0; + +private: + // MSVC issues warning C4293 ("shift count negative or too big, undefined behavior") + // if we use NumBits directly in the shift expression, even if the shift occurs + // in the branch of a ternary expression where NumBits is known to be less than + // the number of bits of the value being shifted. + static constexpr uint32_t MollifiedNumBits = NumBits > 63u ? 63u : NumBits; +public: + + // NumBits mask + static constexpr value_type mask = (NumBits < 64u) ? ((uint64_t(1) << MollifiedNumBits) - 1) : uint64_t(-1); + // NumBits mask for BitStart + static constexpr storage_type mask_lo = storage_type(mask) << bit_lo; + // NumBits mask for leftover bits in data_[idx+1] if straddling, else 0 + static constexpr storage_type mask_hi = (idx + 1 < N) ? (storage_type(mask) >> bit_hi) : 0; + + storage_type data_[N]; + + // Get value + CUTE_HOST_DEVICE constexpr + value_type get() const { + storage_type result = (data_[idx] & mask_lo) >> bit_lo; + if constexpr (bit_hi) { + result |= (data_[idx+1] & mask_hi) << bit_hi; + } + return static_cast(result); + } + + // Set value + CUTE_HOST_DEVICE constexpr + void set(value_type x) { + storage_type item = static_cast(x & mask); + data_[idx] = static_cast((data_[idx] & ~mask_lo) | (item << bit_lo)); + if constexpr (bit_hi) { + data_[idx+1] = static_cast((data_[idx+1] & ~mask_hi) | (item >> bit_hi)); + } + } + + // Assign value + CUTE_HOST_DEVICE constexpr + bit_field& operator=(value_type x) { + set(x); + return *this; + } + + // Cast to value + CUTE_HOST_DEVICE constexpr + operator value_type () const { + return get(); + } + + // Assign OtherValueType + CUTE_HOST_DEVICE constexpr + bit_field& operator=(OtherValueType x) { + return *this = *reinterpret_cast(&x); + } + + // Cast to OtherValueType + CUTE_HOST_DEVICE constexpr + operator OtherValueType () const { + value_type x = get(); + return *reinterpret_cast(&x); + } +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/hip_types.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/hip_types.hpp new file mode 100644 index 000000000..d82088bc1 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/hip_types.hpp @@ -0,0 +1,193 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +#include +#include + +namespace cute +{ + +// +// dim3 +// + +using dim3 = ::dim3; + +// MSVC doesn't define its C++ version macro to match +// its C++ language version. This means that when +// building with MSVC, dim3 isn't constexpr-friendly. +template +CUTE_HOST_DEVICE +#if ! defined(_MSC_VER) +constexpr +#endif +uint32_t& get(dim3& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return a.x; + } else if constexpr (I == 1) { + return a.y; + } else if constexpr (I == 2) { + return a.z; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE +#if ! defined(_MSC_VER) +constexpr +#endif +uint32_t const& get(dim3 const& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return a.x; + } else if constexpr (I == 1) { + return a.y; + } else if constexpr (I == 2) { + return a.z; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE +#if ! defined(_MSC_VER) +constexpr +#endif +uint32_t&& get(dim3&& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return std::move(a.x); + } else if constexpr (I == 1) { + return std::move(a.y); + } else if constexpr (I == 2) { + return std::move(a.z); + } + + CUTE_GCC_UNREACHABLE; +} + +// Specialize cute::tuple-traits for external types +template <> +struct tuple_size + : integral_constant +{}; + +template +struct tuple_element +{ + using type = uint32_t; +}; + +// +// uint3 +// + +using uint3 = ::uint3; + +template +CUTE_HOST_DEVICE constexpr +uint32_t& get(uint3& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return a.x; + } else if constexpr (I == 1) { + return a.y; + } else if constexpr (I == 2) { + return a.z; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +uint32_t const& get(uint3 const& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return a.x; + } else if constexpr (I == 1) { + return a.y; + } else if constexpr (I == 2) { + return a.z; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +uint32_t&& get(uint3&& a) +{ + static_assert(I < 3, "Index out of range"); + if constexpr (I == 0) { + return std::move(a.x); + } else if constexpr (I == 1) { + return std::move(a.y); + } else if constexpr (I == 2) { + return std::move(a.z); + } + + CUTE_GCC_UNREACHABLE; +} + +// Specialize cute::tuple-traits for external types +template <> +struct tuple_size + : integral_constant +{}; + +template +struct tuple_element +{ + using type = uint32_t; +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/tuple_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/tuple_hip.hpp new file mode 100644 index 000000000..2b4c27a79 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/tuple_hip.hpp @@ -0,0 +1,740 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +#include // cute::true_type, cute::false_type +#include + +#include + +//#include // Advanced optimizations + +// +// cute::tuple is like std::tuple, with two differences. +// +// 1. It works on both host and device. +// 2. Its template arguments must be semiregular types. +// +// Semiregular types are default constructible and copyable. +// They include "value types" like int or float, +// but do _not_ include references like int& or float&. +// (See std::tie for an example of a tuple of references.) +// +// This is simplified over the implementations in std::, cuda::std::, and thrust:: by ignoring much of +// the conversion SFINAE, special overloading, and avoiding cvref template types. +// Furthermore, the empty base optimization (EBO) is MORE aggressive by avoiding +// construction calls, and ignoring any need for unique element addresses. +// +// Over standard-conforming tuple implementations, this appears to accelerate compilation times by over 3x. + +namespace cute +{ + +namespace detail +{ + +// EBO stands for "empty base optimization." +// We use this technique to ensure that cute::tuple +// doesn't need to waste space storing any template arguments +// of cute::tuple that have no data (like integral_constant). +// Otherwise, cute::tuple would need to spend at least 1 byte +// for each of its template arguments. +// +// EBO always "holds" a single value of type T. +// N is like an array index that TupleBase uses +// to access the desired tuple element. +template ::value> +struct EBO; + +template +CUTE_HOST_DEVICE constexpr C findt(EBO const&) +{ return {}; } + +// Specialization for types T that have no data; +// the "static tuple leaf." Valid T here include +// integral_constant, Int, +// and any other semiregular type +// for which std::is_empty_v is true. +template +struct EBO +{ + CUTE_HOST_DEVICE constexpr + EBO() {} + + CUTE_HOST_DEVICE constexpr + EBO(T const&) {} +}; + +template +CUTE_HOST_DEVICE constexpr T getv(EBO const&) +{ return {}; } + +// Specialization for types T that are not empty; +// the "dynamic tuple leaf." Valid T here include int, +// any other integral or floating-point type, +// or any semiregular type for which std::is_empty_v is false. +template +struct EBO +{ + CUTE_HOST_DEVICE constexpr + EBO() : t_{} {} + + template + CUTE_HOST_DEVICE constexpr + EBO(U const& u) : t_{u} {} + + T t_; +}; + +template +CUTE_HOST_DEVICE constexpr T const& getv(EBO const& x) +{ return x.t_; } + +template +CUTE_HOST_DEVICE constexpr T& getv(EBO& x) +{ return x.t_; } + +template +CUTE_HOST_DEVICE constexpr T&& getv(EBO&& x) +{ return static_cast(x.t_); } + +template +struct TupleBase; + +// Base class of cute::tuple. +// It inherits from EBO for each (i, t) in (I..., T...). +// The actual storage (for nonempty t) lives in the base classes. +// index_sequence is a way to wrap up a sequence of zero or more +// compile-time integer values in a single type. +// We only ever use index_sequence<0, 1, ..., sizeof...(T)> in practice, +// as the type alias TupleBase below indicates. +template +struct TupleBase, T...> + : EBO... +{ + CUTE_HOST_DEVICE constexpr + TupleBase() {} + + template + CUTE_HOST_DEVICE constexpr explicit + TupleBase(U const&... u) + : EBO(u)... {} + + template + CUTE_HOST_DEVICE constexpr + TupleBase(TupleBase, U...> const& u) + : EBO(getv(static_cast const&>(u)))... {} +}; + +} // end namespace detail + +// Attempting to use the following commented-out alias +// in the declaration of `struct tuple` causes MSVC 2022 build errors. +// +//template +//using TupleBase = detail::TupleBase, T...>; + +// This is the actual cute::tuple class. +// The storage (if any) lives in TupleBase's EBO base classes. +// +// Inheriting from the above alias TupleBase +// causes MSVC 2022 build errors when assigning one tuple to another: +// +// illegal member initialization: +// 'TupleBase< /* template arguments */ >' is not a base or member +// +// Not using the alias or any kind of alias fixed the errors. +// In summary: this is verbose as a work-around for MSVC build errors. +template +struct tuple : detail::TupleBase, T...> +{ + CUTE_HOST_DEVICE constexpr + tuple() {} + + template + CUTE_HOST_DEVICE constexpr + tuple(U const&... u) : detail::TupleBase, T...>(u...) {} + + template + CUTE_HOST_DEVICE constexpr + tuple(tuple const& u) + : detail::TupleBase, T...>(static_cast, U...> const&>(u)) {} +}; + +// +// get for cute::tuple (just like std::get for std::tuple) +// + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +get(tuple const& t) noexcept +{ + static_assert(I < sizeof...(T), "Index out of range"); + return detail::getv(t); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +get(tuple& t) noexcept +{ + static_assert(I < sizeof...(T), "Index out of range"); + return detail::getv(t); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +get(tuple&& t) noexcept +{ + static_assert(I < sizeof...(T), "Index out of range"); + return detail::getv(static_cast&&>(t)); +} + +// +// find a type X within a cute::tuple +// Requires X to be unique in tuple +// Returns a static integer +// + +template +CUTE_HOST_DEVICE constexpr +auto +find(tuple const& t) noexcept +{ + return detail::findt(t); +} + +// +// Custom is_tuple trait simply checks the existence of tuple_size +// and assumes std::get(.), std::tuple_element +// +namespace detail { + +template +auto has_tuple_size( T*) -> bool_constant<(0 <= tuple_size::value)>; +auto has_tuple_size(...) -> false_type; + +} // end namespace detail + +template +struct is_tuple : decltype(detail::has_tuple_size((T*)0)) {}; + +// +// make_tuple (value-based implementation) +// + +template +CUTE_HOST_DEVICE constexpr +tuple +make_tuple(T const&... t) +{ + return {t...}; +} + +// +// tuple_cat concatenates multiple cute::tuple into a single cute::tuple, +// just like std::tuple_cat for std::tuple. +// + +#if 0 +// Original implementation + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, + index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)...); +} + +} // end namespace detail + +CUTE_HOST_DEVICE constexpr +tuple<> +tuple_cat() +{ + return {}; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +Tuple const& +tuple_cat(Tuple const& t) +{ + return t; +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1) +{ + return detail::tuple_cat(t0, t1, + make_index_sequence::value>{}, + make_index_sequence::value>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, Ts const&... ts) +{ + return cute::tuple_cat(cute::tuple_cat(t0,t1),t2,ts...); +} +#endif + +#if 1 +// Extended implementation + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, + index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, + index_sequence, index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)..., get(t2)...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, T3 const& t3, + index_sequence, index_sequence, index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)..., get(t2)..., get(t3)...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, T3 const& t3, T4 const& t4, + index_sequence, index_sequence, index_sequence, index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)..., get(t2)..., get(t3)..., get(t4)...); +} + +template +struct tuple_cat_static; + +template +struct tuple_cat_static, tuple> { + using type = tuple; +}; + +} // end namespace detail + +CUTE_HOST_DEVICE constexpr +tuple<> +tuple_cat() +{ + return {}; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +Tuple const& +tuple_cat(Tuple const& t) +{ + return t; +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1) +{ + if constexpr (is_static::value && is_static::value && + is_tuple::value && is_tuple::value) { + return typename detail::tuple_cat_static::type{}; + } else + { + return detail::tuple_cat(t0, t1, + make_index_sequence::value>{}, + make_index_sequence::value>{}); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2) +{ + return detail::tuple_cat(t0, t1, t2, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, T3 const& t3) +{ + return detail::tuple_cat(t0, t1, t2, t3, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, T3 const& t3, T4 const& t4) +{ + return detail::tuple_cat(t0, t1, t2, t3, t4, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}, + make_index_sequence::value>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, T2 const& t2, T3 const& t3, T4 const& t4, T5 const& t5, Ts const&... ts) +{ + return cute::tuple_cat(cute::tuple_cat(t0,t1,t2,t3,t4), cute::tuple_cat(t5, ts...)); +} +#endif + +#if 0 +// Outer-Inner indexing trick to concat all tuples at once + +namespace detail { + +template +struct tuple_cat_helper +{ + static constexpr cute::array ns = {Ns...}; + + static constexpr size_t total_size() { + size_t sum = 0; + for (size_t n : ns) sum += n; + return sum; + } + static constexpr size_t total_size_ = total_size(); + + static constexpr auto values() { + cute::array outer_inner = {}; + + size_t idx = 0; + for (size_t i = 0; i < ns.size(); ++i) { + for (size_t j = 0; j < ns[i]; ++j, ++idx) { + outer_inner[idx][0] = i; + outer_inner[idx][1] = j; + } + } + return outer_inner; + } + static constexpr auto outer_inner_ = values(); + + using total_sequence = make_index_sequence; +}; + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(Tuple const& t, index_sequence) +{ + return cute::make_tuple(get(get(t))...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1, + index_sequence, index_sequence) +{ + return cute::make_tuple(get(t0)..., get(t1)...); +} + +} // end namespace detail + +CUTE_HOST_DEVICE constexpr +tuple<> +tuple_cat() +{ + return {}; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +Tuple const& +tuple_cat(Tuple const& t) +{ + return t; +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(T0 const& t0, T1 const& t1) +{ + return detail::tuple_cat(t0, t1, + make_index_sequence::value>{}, + make_index_sequence::value>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tuple_cat(Tuples const&... ts) +{ + using Helper = detail::tuple_cat_helper::value...>; + return detail::tuple_cat(cute::make_tuple(ts...), typename Helper::total_sequence{}); +} +#endif + +// +// Equality operators +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +equal_impl(TupleA const& a, TupleB const& b) +{ + if constexpr (I == tuple_size::value) { + return cute::true_type{}; // Terminal: TupleA is exhausted + } else if constexpr (I == tuple_size::value) { + return cute::false_type{}; // Terminal: TupleA is not exhausted, TupleB is exhausted + } else { + return (get(a) == get(b)) && equal_impl(a,b); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template ::value && is_tuple::value)> +CUTE_HOST_DEVICE constexpr +auto +operator==(TupleT const& t, TupleU const& u) +{ + return detail::equal_impl<0>(t, u); +} + +template ::value ^ is_tuple::value)> +CUTE_HOST_DEVICE constexpr +auto +operator==(TupleT const& t, TupleU const& u) +{ + return cute::false_type{}; +} + +template ::value && is_tuple::value)> +CUTE_HOST_DEVICE constexpr +auto +operator!=(TupleT const& t, TupleU const& u) +{ + return !(t == u); +} + +template ::value ^ is_tuple::value)> +CUTE_HOST_DEVICE constexpr +auto +operator!=(TupleT const& t, TupleU const& u) +{ + return cute::true_type{}; +} + +// +// Comparison operators +// + +// +// There are many ways to compare tuple of elements and because CuTe is built +// on parameterizing layouts of coordinates, some comparisons are appropriate +// only in certain cases. +// -- lexicographical comparison [reverse, reflected, revref] +// -- colexicographical comparison [reverse, reflected, revref] +// -- element-wise comparison [any,all] +// This can be very confusing. To avoid errors in selecting the appropriate +// comparison, op<|op<=|op>|op>= are *not* implemented for cute::tuple. +// +// That said, see int_tuple for more explicitly named common comparison ops. +// + +// +// Display utilities +// + +namespace detail { + +template +CUTE_HOST_DEVICE void print_tuple(Tuple const& t, + index_sequence, char s = '(', char e = ')') +{ + using eat = int[]; + using cute::print; + (void) eat {(print(s), 0), + (print(Is == 0 ? "" : ","), print(get(t)), 0)..., + (print(e), 0)}; +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& print_tuple_os(std::ostream& os, Tuple const& t, + index_sequence, char s = '(', char e = ')') +{ + using eat = int[]; + (void) eat {(void(os << s), 0), + (void(os << (Is == 0 ? "" : ",") << get(t)), 0)..., + (void(os << e), 0)}; + return os; +} +#endif // !defined(__CUDACC_RTC__) + +} // end namespace detail + +template ::value)> +CUTE_HOST_DEVICE void print(Tuple const& t) +{ + return detail::print_tuple(t, make_index_sequence::value>{}); +} + +#if !defined(__CUDACC_RTC__) +template ::value)> +CUTE_HOST std::ostream& operator<<(std::ostream& os, Tuple const& t) +{ + return detail::print_tuple_os(os, t, make_index_sequence::value>{}); +} +#endif // !defined(__CUDACC_RTC__) + +} // end namespace cute + +namespace CUTE_STL_NAMESPACE +{ + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +} // end namespace CUTE_STL_NAMESPACE + +// +// std compatibility +// + +#ifdef CUTE_STL_NAMESPACE_IS_CUDA_STD +namespace std +{ + +#if defined(__CUDACC_RTC__) +template +struct tuple_size; + +template +struct tuple_element; +#endif + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +} // end namepsace std +#endif // CUTE_STL_NAMESPACE_IS_CUDA_STD diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/type_list_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/type_list_hip.hpp new file mode 100644 index 000000000..8df4c4594 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/container/type_list_hip.hpp @@ -0,0 +1,142 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +namespace cute +{ + +template +struct type_c { + using type = T; +}; + +template +struct type_list {}; + +} // end namespace cute + +// +// Specialize tuple-related functionality for cute::type_list +// + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include + +namespace cute +{ + +template +CUTE_HOST_DEVICE constexpr +CUTE_STL_NAMESPACE::tuple_element_t> +get(type_list&) noexcept { + return {}; +} +template +CUTE_HOST_DEVICE constexpr +CUTE_STL_NAMESPACE::tuple_element_t> +get(type_list const& t) noexcept { + return {}; +} + +} // end namespace cute + +namespace CUTE_STL_NAMESPACE +{ + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : cute::type_c>::type> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : cute::type_c>::type> +{}; + +} // end namespace std + +#ifdef CUTE_STL_NAMESPACE_IS_CUDA_STD +namespace std +{ + +#if defined(__CUDACC_RTC__) +template +struct tuple_size; + +template +struct tuple_element; +#endif + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : cute::type_c>::type> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : cute::type_c>::type> +{}; + +} // end namespace std +#endif // CUTE_STL_NAMESPACE_IS_CUDA_STD diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/int_tuple_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/int_tuple_hip.hpp new file mode 100644 index 000000000..62bd69229 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/int_tuple_hip.hpp @@ -0,0 +1,932 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include + +/** IntTuple is an integer or a tuple of IntTuples. + * This file holds utilities for working with IntTuples, + * but does not hold a concrete concept or class of IntTuple. + */ + +namespace cute +{ + +// Implementation of get<0>(Integral). +// Even though is_tuple is false and tuple_size doesn't compile, +// CuTe defines rank(Integral) as 1, so it's useful for get<0>(Integral) to return its input +template >::value)> +CUTE_HOST_DEVICE constexpr +decltype(auto) +get(T&& t) noexcept +{ + static_assert(I == 0, "Index out of range"); + return static_cast(t); +} + +// Custom recursive get for anything that implements get(.) (for a single integer I). +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +get(T&& t) noexcept +{ + return get(get(static_cast(t))); +} + +// +// rank +// + +template +CUTE_HOST_DEVICE constexpr +auto +rank(IntTuple const& t) +{ + if constexpr (sizeof...(Is) == 0) { + if constexpr (is_tuple::value) { + return Int::value>{}; + } else { + return Int<1>{}; + } + } else { + return rank(get(t)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +using rank_t = decltype(rank(declval())); + +template +static constexpr int rank_v = rank_t::value; + +// +// shape +// + +template +CUTE_HOST_DEVICE constexpr +auto +shape(IntTuple const& s) +{ + if constexpr (is_tuple::value) { + return transform(s, [](auto const& a) { return shape(a); }); + } else { + return s; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +shape(IntTuple const& s) +{ + if constexpr (is_tuple::value) { + return shape(get(s)); + } else { + return get(shape(s)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// max +// + +template +CUTE_HOST_DEVICE constexpr +auto +max(T0 const& t0, Ts const&... ts) +{ + if constexpr (is_tuple::value) { + return cute::max(cute::apply(t0, [](auto const&... a){ return cute::max(a...); }), ts...); + } else if constexpr (sizeof...(Ts) == 0) { + return t0; + } else { + return cute::max(t0, cute::max(ts...)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// min +// + +template +CUTE_HOST_DEVICE constexpr +auto +min(T0 const& t0, Ts const&... ts) +{ + if constexpr (is_tuple::value) { + return cute::min(cute::apply(t0, [](auto const&... a){ return cute::min(a...); }), ts...); + } else if constexpr (sizeof...(Ts) == 0) { + return t0; + } else { + return cute::min(t0, cute::min(ts...)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// gcd +// + +template +CUTE_HOST_DEVICE constexpr +auto +gcd(T0 const& t0, Ts const&... ts) +{ + if constexpr (is_tuple::value) { + return cute::gcd(cute::apply(t0, [](auto const&... a){ return cute::gcd(a...); }), ts...); + } else if constexpr (sizeof...(Ts) == 0) { + return t0; + } else { + return cute::gcd(t0, cute::gcd(ts...)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// depth +// + +template +CUTE_HOST_DEVICE constexpr +auto +depth(IntTuple const& t) +{ + if constexpr (sizeof...(Is) == 0) { + if constexpr (is_tuple::value) { + return Int<1>{} + cute::apply(t, [](auto const&... v){ return cute::max(depth(v)...); }); + } else { + return Int<0>{}; + } + } else { + return depth(get(t)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +using depth_t = decltype(depth(declval())); + +template +static constexpr int depth_v = depth_t::value; + +// +// product +// + +template +CUTE_HOST_DEVICE constexpr +auto +product(IntTuple const& a) +{ + if constexpr (is_tuple::value) { + return cute::apply(a, [](auto const&... v){ return (Int<1>{} * ... * product(v)); }); + } else { + return a; + } + + CUTE_GCC_UNREACHABLE; +} + +// Return a rank(t) tuple @a result such that get(@a result) = product(get(@a t)) +template +CUTE_HOST_DEVICE constexpr +auto +product_each(Tuple const& t) +{ + return transform(wrap(t), [](auto const& x) { return product(x); }); +} + +// Take the product of Tuple at the leaves of TupleG +template +CUTE_HOST_DEVICE constexpr +auto +product_like(Tuple const& tuple, TupleG const& guide) +{ + return transform_leaf(guide, tuple, [](auto const& g, auto const& t) { return product(t); }); +} + +// Return the product of elements in a mode +template +CUTE_HOST_DEVICE constexpr +auto +size(IntTuple const& a) +{ + if constexpr (sizeof...(Is) == 0) { + return product(a); + } else { + return product(get(a)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +static constexpr int size_v = decltype(size(declval()))::value; + +// +// sum +// + +template +CUTE_HOST_DEVICE constexpr +auto +sum(IntTuple const& a) +{ + if constexpr (is_tuple::value) { + return cute::apply(a, [](auto const&... v){ return (Int<0>{} + ... + sum(v)); }); + } else { + return a; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// inner_product +// + +template +CUTE_HOST_DEVICE constexpr +auto +inner_product(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched ranks"); + return transform_apply(a, b, [](auto const& x, auto const& y) { return inner_product(x,y); }, + [](auto const&... v) { return (Int<0>{} + ... + v); }); + } else { + return a * b; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// ceil_div +// + +template +CUTE_HOST_DEVICE constexpr +auto +ceil_div(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + static_assert(tuple_size::value >= tuple_size::value, "Mismatched ranks"); + constexpr int R = tuple_size::value; // Missing ranks in TupleB are implictly 1 + return transform(a, append(b,Int<1>{}), [](auto const& x, auto const& y) { return ceil_div(x,y); }); + } else { + return (a + b - Int<1>{}) / b; + } + + CUTE_GCC_UNREACHABLE; +} + +/** Division for Shapes + * Case Tuple Tuple: + * Perform shape_div element-wise + * Case Tuple Int: + * Fold the division of b across each element of a + * Example: shape_div((4,5,6),40) -> shape_div((1,5,6),10) -> shape_div((1,1,6),2) -> (1,1,3) + * Case Int Tuple: + * Return shape_div(a, product(b)) + * Case Int Int: + * Enforce the divisibility condition a % b == 0 || b % a == 0 when possible + * Return a / b with rounding away from 0 (that is, 1 or -1 when a < b) + */ +template +CUTE_HOST_DEVICE constexpr +auto +shape_div(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value) { + if constexpr (is_tuple::value) { // tuple tuple + static_assert(tuple_size::value == tuple_size::value, "Mismatched ranks"); + return transform(a, b, [](auto const& x, auto const& y) { return shape_div(x,y); }); + } else { // tuple int + auto const [result, rest] = fold(a, cute::make_tuple(cute::make_tuple(), b), + [] (auto const& init, auto const& ai) { + return cute::make_tuple(append(get<0>(init), shape_div(ai, get<1>(init))), shape_div(get<1>(init), ai)); + }); + return result; + } + } else + if constexpr (is_tuple::value) { // int tuple + return shape_div(a, product(b)); + } else + if constexpr (is_static::value && is_static::value) { + static_assert(IntTupleA::value % IntTupleB::value == 0 || IntTupleB::value % IntTupleA::value == 0, "Static shape_div failure"); + return C{}; + } else { // int int + //assert(a % b == 0 || b % a == 0); // Wave dynamic assertion + return a / b != 0 ? a / b : signum(a) * signum(b); // Division with rounding away from zero + } + + CUTE_GCC_UNREACHABLE; +} + +/** Minimum for Shapes + */ +template +CUTE_HOST_DEVICE constexpr +auto +shape_min(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value || is_tuple::value) { + static_assert(dependent_false, "Not implemented."); + } else + if constexpr (is_constant<1, IntTupleA>::value || is_constant<1, IntTupleB>::value) { + return Int<1>{}; // _1 is less than all other shapes, preserve static + } else { + return cute::min(a, b); + } + + CUTE_GCC_UNREACHABLE; +} + +/** Return a tuple the same profile as A scaled by corresponding elements in B + */ +template +CUTE_HOST_DEVICE constexpr +auto +elem_scale(A const& a, B const& b) +{ + if constexpr (is_tuple::value) { + return transform(a, b, [](auto const& x, auto const& y) { return elem_scale(x,y); }); + } else { + return a * product(b); + } + + CUTE_GCC_UNREACHABLE; +} + +/** Test if two IntTuple have the same profile (hierarchical rank division) + */ +template +CUTE_HOST_DEVICE constexpr +auto +congruent(IntTupleA const& a, IntTupleB const& b) +{ + return bool_constant::value>{}; +} + +template +using is_congruent = decltype(congruent(declval(), declval())); + +/** Test if two IntTuple have the similar profiles up to Shape A (hierarchical rank division) + */ +template +CUTE_HOST_DEVICE constexpr +auto +weakly_congruent(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + if constexpr (tuple_size::value != tuple_size::value) { + return false_type{}; + } else { + return transform_apply(a, b, [](auto const& x, auto const& y) { return weakly_congruent(x,y); }, + [](auto const&... z) { return (true_type{} && ... && z); }); + } + } else if constexpr (is_integral::value) { + return true_type{}; + } else if constexpr (is_integral::value) { + return false_type{}; + } else { + return weakly_congruent(shape(a), shape(b)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +using is_weakly_congruent = decltype(weakly_congruent(declval(), declval())); + +/** Test if Shape B is compatible with Shape A: + * Any coordinate into A can also be used as a coordinate into B + * A <= B is a partially ordered set of factored shapes + */ +template +CUTE_HOST_DEVICE constexpr +auto +compatible(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + if constexpr (tuple_size::value != tuple_size::value) { + return false_type{}; + } else { + return transform_apply(a, b, [](auto const& x, auto const& y) { return compatible(x,y); }, + [](auto const&... z) { return (true_type{} && ... && z); }); + } + } else if constexpr (is_integral::value) { + return a == size(b); + } else if constexpr (is_integral::value) { + return false_type{}; + } else { + return compatible(shape(a), shape(b)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +using is_compatible = decltype(compatible(declval(), declval())); + +/** Test if Shape B is weakly compatible with Shape A: + * Shape B divides Shape A at some level of refinement + */ +template +CUTE_HOST_DEVICE constexpr +auto +weakly_compatible(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + if constexpr (tuple_size::value != tuple_size::value) { + return false_type{}; + } else { + return transform_apply(a, b, [](auto const& x, auto const& y) { return weakly_compatible(x,y); }, + [](auto const&... z) { return (true_type{} && ... && z); }); + } + } else if constexpr (is_integral::value) { + return a % size(b) == Int<0>{}; + } else if constexpr (is_integral::value) { + return false_type{}; + } else { + return weakly_compatible(shape(a), shape(b)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +using is_weakly_compatible = decltype(weakly_compatible(declval(), declval())); + +/** Replace the elements of Tuple B that are paired with an Int<0> with an Int<1> + */ +template +CUTE_HOST_DEVICE constexpr +auto +filter_zeros(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value) { + return transform(a, b, [](auto const& x, auto const& y) { return filter_zeros(x,y); }); + } else if constexpr (is_constant<0, IntTupleA>::value) { + return Int<1>{}; + } else { + return b; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +filter_zeros(Tuple const& t) +{ + return filter_zeros(t, t); +} + +// +// Converters and constructors with arrays and params +// + +/** Make an IntTuple of rank N from an Indexable array. + * Access elements up to a dynamic index n, then use init (requires compatible types) + * Consider cute::take if all indexing is known to be valid + * \code + * std::vector a = {6,3,4}; + * auto tup = make_int_tuple<5>(a, a.size(), 0) // (6,3,4,0,0) + * \endcode + */ +template +CUTE_HOST_DEVICE constexpr +auto +make_int_tuple(Indexable const& t, int n, T const& init) +{ + static_assert(N > 0); + if constexpr (N == 1) { + return 0 < n ? t[0] : init; + } else { + return transform(make_seq{}, [&](auto i) { return i < n ? t[i] : init; }); + } + + CUTE_GCC_UNREACHABLE; +} + +/** Fill the dynamic values of a Tuple with values from another Tuple + * \code + * auto params = make_tuple(6,3,4); + * cute::tuple, cute::tuple>, int, Int<2>> result; + * fill_int_tuple_from(result, params); // (_1,(6,3,_3),4,_2) + * \endcode + */ +template +CUTE_HOST_DEVICE constexpr +auto +fill_int_tuple_from(Tuple& result, TupleV const& vals) +{ + return fold(result, vals, [](auto const& init, auto&& r) { + if constexpr (is_static>::value) { // Skip static elements of result + return init; + } else if constexpr (is_tuple>::value) { // Recurse into tuples + return fill_int_tuple_from(r, init); + } else { // Assign and consume arg + static_assert(tuple_size>::value > 0, "Not enough values to fill with!"); + r = get<0>(init); + return remove<0>(init); + } + + CUTE_GCC_UNREACHABLE; + }); +} + +/** Make a "Tuple" by filling in the dynamic values in order from the arguments + * \code + * using result_t = cute::tuple, cute::tuple>, int, Int<2>>; + * auto result = make_int_tuple_from(6,3,4); // (_1,(6,3,_3),4,_2) + * \endcode + */ +template +CUTE_HOST_DEVICE constexpr +Tuple +make_int_tuple_from(Ts const&... ts) +{ + Tuple result = Tuple{}; + fill_int_tuple_from(result, cute::make_tuple(ts...)); + return result; +} + +/** Convert a tuple to a flat homogeneous array of type T + * \code + * auto tup = cute::make_tuple(Int<1>{}, cute::make_tuple(6,3,Int<3>{}),4,Int<2>{}); + * cute::array result = to_array(tup); // [1,6,3,3,4,2] + * \endcode + */ +template +CUTE_HOST_DEVICE constexpr +auto +to_array(IntTuple const& t) +{ + auto flat_t = flatten_to_tuple(t); + constexpr int N = tuple_size::value; + cute::array result; + for_each(make_seq{}, [&] (auto i) { result[i] = get(flat_t); }); + return result; +} + +// +// Comparison operators +// + +// +// There are many ways to compare tuple of elements and because CuTe is built +// on parameterizing layouts of coordinates, some comparisons are appropriate +// only in certain cases. +// -- lexicographical comparison [reverse, reflected, revref] : Correct for coords in RowMajor Layout +// -- colexicographical comparison [reverse, reflected, revref] : Correct for coords in ColMajor Layout +// -- element-wise comparison [any,all] : +// This can be very confusing. To avoid errors in selecting the appropriate +// comparison, op<|op<=|op>|op>= are *not* implemented for cute::tuple. +// +// When actually desiring to order coordinates, the user should map them to +// their indices within the Layout they came from: +// e.g. layoutX(coordA) < layoutX(coordB) +// That said, we implement the three most common ways to compare tuples below. +// These are implemented with slighly more explicit names than op<. +// + +template +CUTE_HOST_DEVICE constexpr +auto +lex_less(IntTupleA const& a, IntTupleB const& b); + +template +CUTE_HOST_DEVICE constexpr +auto +colex_less(IntTupleA const& a, IntTupleB const& b); + +template +CUTE_HOST_DEVICE constexpr +auto +elem_less(IntTupleA const& a, IntTupleB const& b); + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +lex_less_impl(TupleA const& a, TupleB const& b) +{ + if constexpr (I == tuple_size::value) { + return cute::false_type{}; // Terminal: TupleB is exhausted + } else if constexpr (I == tuple_size::value) { + return cute::true_type{}; // Terminal: TupleA is exhausted, TupleB is not exhausted + } else { + return lex_less(get(a), get(b)) || (get(a) == get(b) && lex_less_impl(a,b)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +colex_less_impl(TupleA const& a, TupleB const& b) +{ + if constexpr (I == tuple_size::value) { + return cute::false_type{}; // Terminal: TupleB is exhausted + } else if constexpr (I == tuple_size::value) { + return cute::true_type{}; // Terminal: TupleA is exhausted, TupleB is not exhausted + } else { + constexpr size_t A = tuple_size::value - 1 - I; + constexpr size_t B = tuple_size::value - 1 - I; + return colex_less(get(a), get(b)) || (get(a) == get(b) && colex_less_impl(a,b)); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +elem_less_impl(TupleA const& a, TupleB const& b) +{ + if constexpr (I == tuple_size::value) { + return cute::true_type{}; // Terminal: TupleA is exhausted + } else if constexpr (I == tuple_size::value) { + return cute::false_type{}; // Terminal: TupleA is not exhausted, TupleB is exhausted + } else { + return elem_less(get(a), get(b)) && elem_less_impl(a,b); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +// Lexicographical comparison + +template +CUTE_HOST_DEVICE constexpr +auto +lex_less(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + return detail::lex_less_impl<0>(a, b); + } else { + return a < b; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +lex_leq(T const& t, U const& u) { + return !lex_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +lex_gtr(T const& t, U const& u) { + return lex_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +lex_geq(T const& t, U const& u) { + return !lex_less(t, u); +} + +// Colexicographical comparison + +template +CUTE_HOST_DEVICE constexpr +auto +colex_less(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + return detail::colex_less_impl<0>(a, b); + } else { + return a < b; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +colex_leq(T const& t, U const& u) { + return !colex_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +colex_gtr(T const& t, U const& u) { + return colex_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +colex_geq(T const& t, U const& u) { + return !colex_less(t, u); +} + +// Elementwise [all] comparison + +template +CUTE_HOST_DEVICE constexpr +auto +elem_less(IntTupleA const& a, IntTupleB const& b) +{ + if constexpr (is_tuple::value && is_tuple::value) { + return detail::elem_less_impl<0>(a, b); + } else { + return a < b; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +elem_leq(T const& t, U const& u) { + return !elem_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +elem_gtr(T const& t, U const& u) { + return elem_less(u, t); +} + +template +CUTE_HOST_DEVICE constexpr +auto +elem_geq(T const& t, U const& u) { + return !elem_less(t, u); +} + +/** Increment a (dynamic) coord lexicographically within a shape + * \code + * auto shape = make_shape(1,2,make_shape(2,3),3); + * + * int i = 0; + * for (auto coord = repeat_like(shape, 0); back(coord) != back(shape); increment(coord, shape)) { + * std::cout << i++ << ": " << coord << std::endl; + * } + * assert(i == size(shape)); + * \endcode + */ +template +CUTE_HOST_DEVICE constexpr +void +increment(Coord& coord, Shape const& shape); + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +void +increment(Coord& coord, Shape const& shape, seq) +{ + cute::increment(get(coord), get(shape)); + if constexpr (sizeof...(Is) != 0) { + if (back(get(coord)) == back(get(shape))) { + back(get(coord)) = 0; + increment(coord, shape, seq{}); + } + } +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +void +increment(Coord& coord, Shape const& shape) +{ + if constexpr (is_integral::value && is_integral::value) { + ++coord; + } else if constexpr (is_tuple::value && is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched ranks"); + detail::increment(coord, shape, tuple_seq{}); + } else { + static_assert(sizeof(Coord) == 0, "Invalid parameters"); + } +} + +struct ForwardCoordIteratorSentinal +{}; + +// A forward iterator for a starting coordinate in a shape's domain, and a shape. +// The starting coordinate may be zero but need not necessarily be. +template +struct ForwardCoordIterator +{ + static_assert(is_congruent::value); + + CUTE_HOST_DEVICE constexpr + Coord const& operator*() const { return coord; } + + CUTE_HOST_DEVICE constexpr + ForwardCoordIterator& operator++() { increment(coord, shape); return *this; } + + // Sentinel for the end of the implied range + CUTE_HOST_DEVICE constexpr + bool operator< (ForwardCoordIteratorSentinal const&) const { return back(coord) < back(shape); } + CUTE_HOST_DEVICE constexpr + bool operator==(ForwardCoordIteratorSentinal const&) const { return back(coord) == back(shape); } + CUTE_HOST_DEVICE constexpr + bool operator!=(ForwardCoordIteratorSentinal const&) const { return back(coord) != back(shape); } + // NOTE: These are expensive, avoid use + CUTE_HOST_DEVICE constexpr + bool operator< (ForwardCoordIterator const& other) const { return colex_less(coord, other.coord); } + CUTE_HOST_DEVICE constexpr + bool operator==(ForwardCoordIterator const& other) const { return coord == other.coord; } + CUTE_HOST_DEVICE constexpr + bool operator!=(ForwardCoordIterator const& other) const { return coord != other.coord; } + + Coord coord; + Shape const& shape; +}; + +// A forward iterator for a coordinate that starts from a provided coordinate +template +CUTE_HOST_DEVICE constexpr +auto +make_coord_iterator(Coord const& coord, Shape const& shape) +{ + return ForwardCoordIterator{coord,shape}; +} + +// A forward iterator for a coordinate that starts from zero +template +CUTE_HOST_DEVICE constexpr +auto +make_coord_iterator(Shape const& shape) +{ + auto coord = repeat_like(shape, int(0)); + return make_coord_iterator(coord, shape); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_composed_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_composed_hip.hpp new file mode 100644 index 000000000..45ea60948 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_composed_hip.hpp @@ -0,0 +1,615 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +/* This implements a ComposedLayout of the form + * LayoutA o Offset o LayoutB + * and is useful in cases where composition() does not or cannot apply to LayoutA and LayoutB. + * For example, then the "divisibility condition" in shape_div is violated in composition(LayoutA, LayoutB). + * + * This ComposedLayout provides similar functionality to Layout including tiling, partitioning, + * coordinate-to-index mapping and layout manipulations, but is not considered a "normal" layout. + * For example, this layout provides shape() and size() functions, but does not provide stride() functions. + * Mostly, the similar functionality is accomplished by applying each operation to LayoutB only + * as LayoutB defines the domain. + */ + +namespace cute +{ + +// A Layout of non-trivially composable functions: F o I o L +template +struct ComposedLayout : private cute::tuple // EBO for static layouts +{ + CUTE_HOST_DEVICE constexpr + ComposedLayout(LayoutA const& layoutA = {}, + Offset const& offset = {}, + LayoutB const& layoutB = {}) + : cute::tuple(layoutA, offset, layoutB) + {} + + // + // Accessors + // + + static constexpr int rank = LayoutB::rank; + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout_a() const { + return get<0>(static_cast const&>(*this)); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + offset() const { + return get<1>(static_cast const&>(*this)); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout_b() const { + return get<2>(static_cast const&>(*this)); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout() const { + return *this; + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + shape() const { + return layout_b().shape(); + } + + // Doesn't really make sense to ask for the strides of this "layout" + CUTE_HOST_DEVICE constexpr + decltype(auto) + stride() const = delete; + + // + // Mappings + // + + // Map a logical coordinate to a linear index (Coord has no Underscore slice operators) + // OR + // Slice the layout and return the sublayout (Coord has an Underscore slice op) + template + CUTE_HOST_DEVICE constexpr + auto + operator()(Coord const& coord) const { + if constexpr (has_underscore::value) { + return slice(coord, *this); + } else { + return layout_a()(offset() + layout_b()(coord)); // (A o O o B)(c) + } + + CUTE_GCC_UNREACHABLE; + } + + // Convenience function for multi-dimensional coordinates + template + CUTE_HOST_DEVICE constexpr + auto + operator()(Coord0 const& c0, Coord1 const& c1, Coords const&... cs) const { + return operator()(make_coord(c0,c1,cs...)); + } + + // + // Compose + // + + template + CUTE_HOST_DEVICE constexpr + auto + compose(OtherLayout const& other) const { + return composition(*this, other); + } + + template + CUTE_HOST_DEVICE constexpr + auto + compose(Layouts const&... layouts) const { + return composition(*this, make_tile(layouts...)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + with_shape(OtherShape const& shape) const { + return composition(*this, make_layout(shape)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + with_shape(Shapes const&... shapes) const { + return composition(*this, make_layout(make_shape(shapes...))); + } + + // + // Tile + // + + template + CUTE_HOST_DEVICE constexpr + auto + tile(OtherLayout const& other) const { + return tiled_divide(*this, other); + } + + template + CUTE_HOST_DEVICE constexpr + auto + tile(Layouts const&... layouts) const { + return tiled_divide(*this, make_tile(layouts...)); + } +}; + +template +struct is_layout> : true_type {}; + +template +struct is_composed_layout : false_type {}; +template +struct is_composed_layout> : true_type {}; + +// +// Constructors +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_composed_layout(LayoutA const& layoutA, + Offset const& offset, + LayoutB const& layoutB) +{ + return ComposedLayout{layoutA, offset, layoutB}; +} + +// +// Utilities +// + +// Return the layout of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +layout(ComposedLayout const& clayout) +{ + return composition(clayout.layout_a(), clayout.offset(), layout(clayout.layout_b())); +} + +// Return the shape of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +shape(ComposedLayout const& layout) +{ + return shape(layout.layout_b()); +} + +// Doesn't make sense to directly ask for the strides of this "layout" +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +stride(ComposedLayout const& layout) = delete; + +// Return the number of elements in a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +size(ComposedLayout const& layout) +{ + return size(layout.layout_b()); +} + +// Return the number of modes +template +CUTE_HOST_DEVICE constexpr +auto +rank(ComposedLayout const& layout) +{ + return rank(layout.layout_b()); +} + +// Return the depth of the layout +template +CUTE_HOST_DEVICE constexpr +auto +depth(ComposedLayout const& layout) +{ + return depth(layout.layout_b()); +} + +// Return the codomain size of a mode +template +CUTE_HOST_DEVICE constexpr +auto +cosize(ComposedLayout const& layout) +{ + return cosize(layout.layout_b()); +} + +// +// Operations to manipulate Layouts like a tuple of pairs +// + +template +CUTE_HOST_DEVICE constexpr +auto +get(ComposedLayout const& a) +{ + return composition(a.layout_a(), a.offset(), get(a.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +take(ComposedLayout const& a) +{ + return composition(a.layout_a(), a.offset(), take(a.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +flatten(ComposedLayout const& a) +{ + return composition(a.layout_a(), a.offset(), flatten(a.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +append(ComposedLayout const& a, X const& x) +{ + return composition(a.layout_a(), a.offset(), append(a.layout_b(), x)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +group(ComposedLayout const& a) +{ + return composition(a.layout_a(), a.offset(), group(a.layout_b())); +} + +// +// Slice a ComposedLayout +// + +template +CUTE_HOST_DEVICE constexpr +auto +slice_and_offset(Coord const& coord, ComposedLayout const& layout) +{ + auto [slice, offset] = slice_and_offset(coord, layout.layout_b()); + return cute::make_tuple(ComposedLayout{layout.layout_a(), layout.offset() + offset, slice}, Int<0>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +slice(Coord const& coord, ComposedLayout const& layout) +{ + return get<0>(slice_and_offset(coord, layout)); +} + +// Compute a pointer offset and (potentially modified) layout from a coordinate +// For composed layout tensors the offset is accumulated in the layout itself while pointer is not updated +template +CUTE_HOST_DEVICE constexpr +auto +domain_offset(Coord const& coord, ComposedLayout const& layout) +{ + return cute::make_tuple(ComposedLayout{layout.layout_a(), layout.offset() + layout.layout_b()(coord), layout.layout_b()}, Int<0>{}); +} + +// +// composition +// + +template +CUTE_HOST_DEVICE constexpr +auto +composition(LayoutA const& layoutA, + Offset const& offset, + LayoutB const& layoutB) +{ + return ComposedLayout{layoutA, offset, layoutB}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(ComposedLayout const& a, + LayoutOrTile const& b) +{ + return composition(a.layout_a(), a.offset(), composition(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Layout const& a, + ComposedLayout const& b) +{ + CUTE_STATIC_ASSERT_V(b.offset() == Int<0>{}, "Require offset == 0."); + + return composition(composition(a, b.layout_a()), b.layout_b()); +} + +// +// complement +// + +template +CUTE_HOST_DEVICE constexpr +auto +complement(ComposedLayout const& layout, CoSizeHi const& cosize_hi) +{ + return complement(layout.layout_b(), cosize_hi); +} + +template +CUTE_HOST_DEVICE constexpr +auto +complement(ComposedLayout const& layout) +{ + return complement(layout, cosize(layout)); +} + +// +// inverse +// + +template +CUTE_HOST_DEVICE constexpr +auto +right_inverse(ComposedLayout const& layout) +{ + return composition(right_inverse(layout.layout_b()), right_inverse(layout.offset()), right_inverse(layout.layout_a())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +left_inverse(ComposedLayout const& layout) +{ + return composition(left_inverse(layout.layout_b()), left_inverse(layout.offset()), left_inverse(layout.layout_a())); +} + +// +// Other operations +// + +template +CUTE_HOST_DEVICE constexpr +auto +zip(ComposedLayout const& a) +{ + return composition(a.layout_a(), a.offset(), zip(a.layout_b())); +} + +// Partitions + +template +CUTE_HOST_DEVICE constexpr +auto +logical_divide(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), logical_divide(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tile_unzip(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), tile_unzip(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tiled_divide(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), tiled_divide(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +zipped_divide(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), zipped_divide(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +logical_product(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), logical_product(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tiled_product(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), tiled_product(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +blocked_product(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), blocked_product(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +raked_product(ComposedLayout const& a, + Tile const& b) +{ + return composition(a.layout_a(), a.offset(), raked_product(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tile_to_shape(ComposedLayout const& layout, + Shape const& trg_shape, + ModeOrder const& ord_shape = {}) +{ + return composition(layout.layout_a(), layout.offset(), tile_to_shape(layout.layout_b(), trg_shape, ord_shape)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +filter(ComposedLayout const& layout, Shape const& trg_profile) +{ + return composition(layout.layout_a(), layout.offset(), filter(layout.layout_b(), trg_profile)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +coalesce(ComposedLayout const& layout) +{ + return composition(layout.layout_a(), layout.offset(), coalesce(layout.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +coalesce(ComposedLayout const& layout, Shape const& trg_profile) +{ + return composition(layout.layout_a(), layout.offset(), coalesce(layout.layout_b(), trg_profile)); +} + +// +// Upcast and Downcast +// + +template +CUTE_HOST_DEVICE constexpr +auto +upcast(ComposedLayout const& layout) +{ + return composition(upcast(layout.layout_a()), upcast(layout.offset()), upcast(layout.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(ComposedLayout const& layout) +{ + return composition(downcast(layout.layout_a()), downcast(layout.offset()), downcast(layout.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(ComposedLayout const& layout) +{ + if constexpr (sizeof(NewType) == sizeof(OldType)) { + return layout; + } else if constexpr (sizeof(NewType) > sizeof(OldType)) { + static_assert(sizeof(NewType) % sizeof(OldType) == 0, "NewType must be a multiple of OldType"); + return upcast(layout); + } else if constexpr (sizeof(NewType) < sizeof(OldType)) { + static_assert(sizeof(OldType) % sizeof(NewType) == 0, "NewType must be a divisor of OldType"); + return downcast(layout); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(ComposedLayout const& layout) +{ + print(layout.layout_a()); print(" o "); print(layout.offset()); print(" o "); print(layout.layout_b()); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, ComposedLayout const& layout) +{ + return os << layout.layout_a() << " o " << layout.offset() << " o " << layout.layout_b(); +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_hip.hpp new file mode 100644 index 000000000..74ac6bf44 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/layout_hip.hpp @@ -0,0 +1,1826 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include + +namespace cute +{ + +// Aliases + +template +using Shape = cute::tuple; + +template +using Stride = cute::tuple; + +template +using Step = cute::tuple; + +template +using Coord = cute::tuple; + +template +CUTE_HOST_DEVICE constexpr +Shape +make_shape(Ts const&... t) { + return {t...}; +} +template +CUTE_HOST_DEVICE constexpr +Stride +make_stride(Ts const&... t) { + return {t...}; +} +template +CUTE_HOST_DEVICE constexpr +Step +make_step(Ts const&... t) { + return {t...}; +} +template +CUTE_HOST_DEVICE constexpr +Coord +make_coord(Ts const&... t) { + return {t...}; +} + + +template > +struct Layout + : private cute::tuple // EBO for static layouts +{ + // Expensive in compilation time... + //static_assert(is_congruent::value, "Shape and Stride must be congruent"); + + // NOTE: This defaults static Shapes/Strides correctly, but not dynamic + CUTE_HOST_DEVICE constexpr + Layout(Shape const& shape = {}, Stride const& stride = {}) + : cute::tuple(shape, stride) + {} + + // + // Accessors + // + + static constexpr int rank = rank_v; + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout() { + return *this; + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout() const { + return *this; + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + shape() { + return get<0,I...>(static_cast&>(*this)); + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + shape() const { + return get<0,I...>(static_cast const&>(*this)); + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + stride() { + return get<1,I...>(static_cast&>(*this)); + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + stride() const { + return get<1,I...>(static_cast const&>(*this)); + } + + // + // Mappings + // + + // Map a logical coordinate to a linear index (Coord has no Underscore slice operators) + // OR + // Slice the layout and return the sublayout (Coord has an Underscore slice op) + template + CUTE_HOST_DEVICE constexpr + auto + operator()(Coord const& coord) const { + if constexpr (has_underscore::value) { + return slice(coord, *this); + } else { + return crd2idx(coord, shape(), stride()); + } + + CUTE_GCC_UNREACHABLE; + } + + // Convenience function for multi-dimensional coordinates + template + CUTE_HOST_DEVICE constexpr + auto + operator()(Coord0 const& c0, Coord1 const& c1, Coords const&... cs) const { + return operator()(make_coord(c0,c1,cs...)); + } + + // Map a linear index to a hier ND logical coordinate + // NOTE: Dangerous and error-prone + template + CUTE_HOST_DEVICE constexpr + auto + operator[](Int const& linear_idx) const { + static_assert(is_integral::value); + return get_hier_coord(linear_idx); + } + + // + // Compose + // + + template + CUTE_HOST_DEVICE constexpr + auto + compose(OtherLayout const& other) const { + return composition(*this, other); + } + + template + CUTE_HOST_DEVICE constexpr + auto + compose(Layouts const&... layouts) const { + return composition(*this, make_tile(layouts...)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + with_shape(OtherShape const& shape) const { + return composition(*this, make_layout(shape)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + with_shape(Shapes const&... shapes) const { + return composition(*this, make_layout(make_shape(shapes...))); + } + + // + // Tile + // + + template + CUTE_HOST_DEVICE constexpr + auto + tile(OtherLayout const& other) const { + return tiled_divide(*this, other); + } + + template + CUTE_HOST_DEVICE constexpr + auto + tile(Layouts const&... layouts) const { + return tiled_divide(*this, make_tile(layouts...)); + } + + // + // Utility + // + + // + // Index to Coordinate + // + + // NOTE: Only valid for compact layouts + + // Return the (hierarchical) ND logical coordinate corresponding to the linear index + // @post crd2idx(@a result, shape(), stride()) == idx + // @post congruent(@a result, shape()) + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_hier_coord(IInt const& idx) const { + return cute::idx2crd(idx, shape(), stride()); + } + + // Return the (flat) ND logical coordinate corresponding to the linear index + // @post crd2idx(@a result, shape(), stride()) == idx + // @post rank(@a result) == rank(shape()) && depth(@a result) == 1 + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_flat_coord(IInt const& idx) const { + return cute::crd2crd(this->get_hier_coord(idx), shape(), repeat(Int<1>{})); + } + + // Return the generalized column-major 1D logical coordinate corresponding to the linear index + // @post crd2idx(@a result, shape(), stride()) == idx + // @post is_integral::value + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_1d_coord(IInt const& idx) const { + return cute::crd2idx(this->get_hier_coord(idx), shape()); + } + + // + // Coordinate to Coordinate + // + +#if 0 + // Return the (hierarchical) ND logical coordinate corresponding to the linear index + // @post congruent(@a result, shape()) + template + CUTE_HOST_DEVICE constexpr + auto + crd_2_hier_coord(Coord const& crd) const { + return cute::crd2crd(crd, shape(), shape()); + } + + // Return the (flat) ND logical coordinate corresponding to the linear index + // @post rank(@a result) == rank(shape()) && depth(@a result) == 1 + template + CUTE_HOST_DEVICE constexpr + auto + crd_2_flat_coord(Coord const& crd) const { + return cute::crd2crd(crd, shape(), product_each(shape())); + } + + // Return the generalized column-major 1D logical coordinate corresponding to the linear index + // @post is_integral::value + template + CUTE_HOST_DEVICE constexpr + auto + crd_2_1d_coord(Coord const& crd) const { + //return cute::crd2crd(crd, shape(), product(shape())); + return cute::crd2idx(crd, shape()); + } +#endif +}; + +template +struct is_layout : false_type {}; +template +struct is_layout> : true_type {}; + + +template ::value || is_integral::value) && + (is_tuple::value || is_integral::value))> +CUTE_HOST_DEVICE constexpr +auto +make_layout(Shape const& shape, Stride const& stride) +{ + return Layout(shape, stride); +} + +template ::value || is_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +make_layout(Shape const& shape) +{ + return make_layout(shape, compact_col_major(shape)); +} + +// Construct a layout from multiple layouts by +// concatenating each layout as an independent mode +template +CUTE_HOST_DEVICE constexpr +auto +make_layout(Layout const&... layouts) +{ + return make_layout(make_shape (layouts.shape()...), + make_stride(layouts.stride()...)); +} + +// +// Convenience tags for common layouts +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_layout(Shape const& shape, GenColMajor) +{ + return make_layout(shape, compact_col_major(shape)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_layout(Shape const& shape, GenRowMajor) +{ + return make_layout(shape, compact_row_major(shape)); +} + +// Follow the same ordering induced by the strides, but make the layout compact +template +CUTE_HOST_DEVICE constexpr +auto +make_ordered_layout(Shape const& shape, Order const& order) +{ + static_assert(is_static::value); + return make_layout(shape, compact_order(shape, order)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_ordered_layout(Layout const& layout) +{ + return make_ordered_layout(layout.shape(), layout.stride()); +} + +// Make a layout of the same shape that is either ordered or colmajor depending on staticness +template +CUTE_HOST_DEVICE constexpr +auto +make_layout_like(Layout const& layout) +{ + auto any_zero = any_of(layout.stride(), [](auto d) { return is_constant<0, decltype(d)>{}; }); + if constexpr (any_zero) { + // If there are static-0 strides, then make a col-major layout that keeps those 0s + return make_layout(layout.shape(), + compact_col_major(filter_zeros(layout.stride(), layout.shape()))); + } else + if constexpr (is_static::value && is_static::value) { + // If the layout is fully static, then make a layout that follows the same order as the strides + // Assumes the strides are unique + return make_ordered_layout(layout.shape(), layout.stride()); + } else { + return make_layout(layout.shape()); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Make a layout of the same shape, +// with mode-0 being colmajor then following the mode order in layout +// +template +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(Layout const& layout) +{ + constexpr int R = Layout::rank; + if constexpr (R > 1 && is_static::value && is_static::value) { + return tiled_product(make_layout(shape<0>(layout)), make_ordered_layout(take<1,R>(layout))); + } else { + return make_layout(layout.shape()); + } + + CUTE_GCC_UNREACHABLE; +} + +template ::value || is_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(Shape const& shape) +{ + return make_layout(shape); +} + +// +// Make an identity layout that maps a coordinate to itself +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_identity_layout(Shape const& shape) +{ + return make_layout(shape, make_basis_like(shape)); +} + +// +// Operations to manipulate Layouts like a tuple of pairs +// + +template +CUTE_HOST_DEVICE constexpr +auto +get(Layout const& layout) +{ + // Let the static_asserts in get(shape|stride) catch problems + return make_layout(get(layout.shape()), get(layout.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +take(Layout const& layout) +{ + // Let the static_asserts in take(shape|stride) catch problems + return make_layout(take(layout.shape()), take(layout.stride())); +} + +// +// Select layout modes according to an index sequence. +// + +template +CUTE_HOST_DEVICE constexpr +auto +select(Layout const& layout) +{ + return make_layout(select(layout.shape()), + select(layout.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +flatten(Layout const& layout) +{ + return make_layout(flatten(layout.shape()), flatten(layout.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +unflatten(Layout const& layout, TargetProfile const& target_profile) +{ + return make_layout(unflatten(layout.shape(), target_profile), + unflatten(layout.stride(), target_profile)); +} + +// +// Utilities +// + +// Return the layout of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +layout(Layout const& layout) +{ + if constexpr (sizeof...(Is) == 0) { + return layout; + } else { + return get(layout); + } + + CUTE_GCC_UNREACHABLE; +} + +// Return the shape of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +shape(Layout& layout) +{ + return layout.template shape(); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +shape(Layout const& layout) +{ + return layout.template shape(); +} + +// Return the stride of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +stride(Layout& layout) +{ + return layout.template stride(); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +stride(Layout const& layout) +{ + return layout.template stride(); +} + +// Return the number of elements in a mode +template +CUTE_HOST_DEVICE constexpr +auto +size(Layout const& layout) +{ + return size(shape(layout)); +} + +// Return the number of modes +template +CUTE_HOST_DEVICE constexpr +auto +rank(Layout const& layout) +{ + return rank(shape(layout)); +} + +// Return the depth of the layout +template +CUTE_HOST_DEVICE constexpr +auto +depth(Layout const& layout) +{ + return depth(shape(layout)); +} + +// Return the codomain shape of a mode +// @post size(coshape(@a a)) == cosize(@a a) +// @return C Coordinate with smallest elements such that that +// @a elem_less(sub_layout(c), C) for all c < size(@a sub_layout) +// where sub_layout = get(layout). +template +CUTE_HOST_DEVICE constexpr +auto +coshape(Layout const& layout) +{ + // Protect against negative strides + auto abs_sub_layout = make_layout(shape(layout), + transform_leaf(stride(layout), abs_fn{})); + auto co_coord = as_arithmetic_tuple(abs_sub_layout(size(abs_sub_layout) - Int<1>{})); + return co_coord + repeat_like(co_coord, Int<1>{}); +} + +// Return the codomain size of a mode +// @return M smallest integer such that +// @a sub_layout(c) < M for all c < size(@a sub_layout) +// where sub_layout = get(layout). +template +CUTE_HOST_DEVICE constexpr +auto +cosize(Layout const& layout) +{ + return size(coshape(layout)); +} + +template +using cosize_t = decltype(cosize(declval())); + +template +static constexpr int cosize_v = cosize_t::value; + +// Equality +// Return a static or dynamic boolean +template +CUTE_HOST_DEVICE constexpr +auto +operator==(Layout const& layoutA, Layout const& layoutB) +{ + return layoutA.shape() == layoutB.shape() && layoutA.stride() == layoutB.stride(); +} + +// With crd2idx(coord, shape), makes sense to have crd2idx(coord, Layout) as well +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx(Coord const& c, Layout const& layout) +{ + return crd2idx(c, layout.shape(), layout.stride()); +} + +// +// Slice and Dice a layout +// + +template +CUTE_HOST_DEVICE constexpr +auto +slice(Coord const& c, Layout const& layout) +{ + return make_layout(slice(c, layout.shape()), + slice(c, layout.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +slice_and_offset(Coord const& c, Layout const& layout) +{ + return cute::make_tuple(slice(c, layout), crd2idx(c, layout)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +dice(Coord const& c, Layout const& layout) +{ + return make_layout(dice(c, layout.shape()), + dice(c, layout.stride())); +} + +// Compute a pointer offset and (potentially modified) layout from a coordinate +// This exists so it can be overloaded for ComposedLayout +template +CUTE_HOST_DEVICE constexpr +auto +domain_offset(Coord const& coord, Layout const& layout) +{ + return cute::make_tuple(layout, layout(coord)); +} + +// +// Transform the modes of a layout +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +transform_layout(Tuple const& t, F&& f, seq) +{ + return make_layout(f(get(t))...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_layout(Tuple0 const& t0, Tuple1 const& t1, F&& f, seq, seq, seq) +{ + return make_layout(f(get(t0),get(t1))..., get(t0)..., get(t1)...); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +transform_layout(Tuple const& t, F&& f) +{ + return detail::transform_layout(t, f, make_seq{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +transform_layout(Tuple0 const& t0, Tuple1 const& t1, F&& f) +{ + constexpr int R0 = decltype(rank(t0))::value; + constexpr int R1 = decltype(rank(t1))::value; + constexpr int R = (R0 < R1) ? R0 : R1; + return detail::transform_layout(t0, t1, f, make_seq{}, make_range{}, make_range{}); +} + +// +// Coalesce and Filter +// + +namespace detail { + +// Look at each element and the front of the stack (in order of priority) +// front(NewLayout) get(Layout) +// s0:d0 _1:d1 => continue +// _1:d0 s1:d1 => replace_front s1:d1 +// s0:s1*d1 s1:d1 => replace_front s0*s1:d1 +// s0:d0 s1:d1 => prepend s1:d1 +// +// @pre OldShape and OldStride are flat +template +CUTE_HOST_DEVICE constexpr +auto +bw_coalesce(OldShape const& old_shape, OldStride const& old_stride, + NewShape const& new_shape, NewStride const& new_stride) +{ + if constexpr (I == -1) { + // Base case, we're done + if constexpr (is_constant<1, NewShape>::value) { + return Layout<_1,_0>{}; + } else { + return Layout{new_shape,new_stride}; + } + } else if constexpr (is_constant<1, decltype(get(old_shape))>::value) { + // shape(layout) == _1, skip it and continue + return bw_coalesce(old_shape, old_stride, new_shape, new_stride); + } else if constexpr (is_constant<1, NewShape>::value) { + // Replace our shape-1 with anything (Can only happen on input new_shape/new_stride) + return bw_coalesce(old_shape, old_stride, get(old_shape), get(old_stride)); + } else if constexpr (is_constant(old_shape) * get(old_stride) == get<0>(new_stride))>::value) { + // Merge modes because the shapes and strides match + return bw_coalesce(old_shape, old_stride, + replace_front(new_shape, get(old_shape) * get<0>(new_shape)), + replace_front(new_stride, get(old_stride))); + } else { + // Can't replace or merge, so prepend a new mode + return bw_coalesce(old_shape, old_stride, + prepend(new_shape, get(old_shape)), + prepend(new_stride, get(old_stride))); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +// Combine all the modes that are possible to combine +// Does not respect the profile of the layout, but does preserve total size +template +CUTE_HOST_DEVICE constexpr +auto +coalesce(Layout const& layout) +{ + auto flat_shape = flatten(layout.shape()); + auto flat_stride = flatten(layout.stride()); + + constexpr int R = decltype(rank(flat_shape))::value; + return detail::bw_coalesce(flat_shape, flat_stride, get(flat_shape), get(flat_stride)); +} + +// Apply coalesce at the terminals of trg_profile +template +CUTE_HOST_DEVICE constexpr +auto +coalesce(Layout const& layout, IntTuple const& trg_profile) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value <= Layout::rank); + return transform_layout(layout, trg_profile, [](auto const& l, auto const& t) { return coalesce(l,t); }); + } else { + return coalesce(layout); + } + + CUTE_GCC_UNREACHABLE; +} + +// Replace the modes in layout that have a 0-stride with a 1-size +template +CUTE_HOST_DEVICE constexpr +auto +filter_zeros(Layout const& layout) +{ + return make_layout(filter_zeros(layout.stride(), layout.shape()), layout.stride()); +} + +// Remove all of the 0-strides and 1-sizes +// Return 1-shape if empty +template +CUTE_HOST_DEVICE constexpr +auto +filter(Layout const& layout) +{ + return coalesce(filter_zeros(layout)); +} + +// Apply filter at the terminals of trg_profile +template +CUTE_HOST_DEVICE constexpr +auto +filter(Layout const& layout, IntTuple const& trg_profile) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value <= Layout::rank); + return transform_layout(layout, trg_profile, [](auto const& l, auto const& t) { return filter(l,t); }); + } else { + return filter(layout); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Append, Prepend, Replace +// + +template +CUTE_HOST_DEVICE constexpr +auto +append(Layout const& layout, + Layout const& x = {}) +{ + return make_layout(append(layout.shape(), x.shape()), + append(layout.stride(), x.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +append(Layout const& layout, + Layout const& x = {}) +{ + return make_layout(append(layout.shape(), x.shape()), + append(layout.stride(), x.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +prepend(Layout const& layout, + Layout const& x = {}) +{ + return make_layout(prepend(layout.shape(), x.shape()), + prepend(layout.stride(), x.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +prepend(Layout const& layout, + Layout const& x = {}) +{ + return make_layout(prepend(layout.shape(), x.shape()), + prepend(layout.stride(), x.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +replace(Layout const& layout, + Layout const& x) +{ + return make_layout(replace(layout.shape(), x.shape()), + replace(layout.stride(), x.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +group(Layout const& layout) +{ + return make_layout(group(layout.shape()), + group(layout.stride())); +} + +// +// Composition of two layouts: lhs o rhs +// @post compatible(rhs, result) +// @post result(c) = lhs(rhs(c)) +// for all c in the domain of result +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +composition_impl(Layout const& lhs, + RShape const& rhs_shape, RStride const& rhs_stride) +{ + if constexpr (is_tuple::value) { + // Apply the right-distributivity of Layout composition + return transform_layout(rhs_shape, rhs_stride, [&](auto const& s, auto const& d) { return composition_impl(lhs, s, d); }); + } else + if constexpr (is_scaled_basis::value) { + // Special case for a ScaledBasis stride + return composition_impl(get(lhs), rhs_shape, rhs_stride.value()); + } else + if constexpr (is_integral::value) { + // Integral Rstride (and RShape) + + // NOTE: Should only flatten once for efficiency + auto flat_shape = flatten(lhs.shape()); + [[maybe_unused]] auto flat_stride = flatten(lhs.stride()); + [[maybe_unused]] constexpr int R = rank(flat_shape); + + if constexpr (is_constant<0, RStride>::value) { + // Special case shortcut for any static stride-0 + return Layout{rhs_shape, rhs_stride}; + } else + if constexpr (is_integral::value) { + // Special case shortcut for any integral LShape + auto result_stride = rhs_stride * flat_stride; + return Layout{rhs_shape, result_stride}; + } else + if constexpr (is_constant<1, RStride>::value) { + // Special case shortcut for any static stride-1 + auto result_shape_0 = take<0,R-1>(flat_shape); + + // Mod out the rhs_shape from the lhs.shape() + auto const [result_shape_1, rest_shape] = fold(result_shape_0, cute::make_tuple(cute::make_tuple(), rhs_shape), + [] (auto const& init, auto const& si) { + return cute::make_tuple(append(get<0>(init), shape_min(abs(si), get<1>(init))), shape_div(get<1>(init), abs(si))); + }); + + // Jump into coalesce and append (rest_shape, get(lhs.stride()) + return detail::bw_coalesce(result_shape_1, flat_stride, rest_shape, get(flat_stride)); + } else + { + // General case + auto result_shape_0 = take<0,R-1>(flat_shape); + auto result_stride_0 = take<0,R-1>(flat_stride); + + // Divide out the rhs_stride from the lhs.shape() + auto const [result_shape_1, rest_stride] = fold(result_shape_0, cute::make_tuple(cute::make_tuple(), rhs_stride), + [] (auto const& init, auto const& di) { + return cute::make_tuple(append(get<0>(init), shape_div(di, get<1>(init))), shape_div(get<1>(init), di)); + }); + + // Apply any lhs.shape() changes to the stride + auto result_stride_1 = elem_scale(result_stride_0, shape_div(result_shape_0, result_shape_1)); + + // Mod out the rhs_shape from the lhs.shape() + auto const [result_shape_2, rest_shape] = fold(result_shape_1, cute::make_tuple(cute::make_tuple(), rhs_shape), + [] (auto const& init, auto const& si) { + return cute::make_tuple(append(get<0>(init), shape_min(abs(si), get<1>(init))), shape_div(get<1>(init), abs(si))); + }); + + // Jump into coalesce and append (rest_shape, rest_stride * get(lhs.stride()) + return detail::bw_coalesce(result_shape_2, result_stride_1, rest_shape, rest_stride * get(flat_stride)); + } + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Layout const& lhs, + Layout const& rhs) +{ + //return detail::composition_impl(flatten(lhs), rhs.shape(), rhs.stride()); + return detail::composition_impl(lhs, rhs.shape(), rhs.stride()); +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Layout const& lhs, + IntTuple const& rhs) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value <= Layout::rank); + // Drop any modes of lhs that aren't hit by rhs + return detail::transform_layout(lhs, rhs, [](auto const& l, auto const& r) { return composition(l,r); }, make_seq::value>{}, seq<>{}, seq<>{}); + } else if constexpr (is_underscore::value) { + return lhs; + } else { + return composition(lhs, make_layout(rhs)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Complement +// +// Build the complement of a layout. +// @post size(@a result) >= @a cosize_hi / size(filter(@a layout))); +// @post For all i in [1,size(@a result)), +// @a result(i) < @a result(i-1) +// For all j in [0, size(@a layout)), +// @a result(i) != @a layout(j) +// + +namespace detail { + +// @pre @a layout has been filtered (flattened and no stride-0 or size-1 modes). +template +CUTE_HOST_DEVICE constexpr +auto +complement(Shape const& shape, Stride const& stride, CoSizeHi const& cosize_hi) +{ + if constexpr (is_constant<0, Stride>::value) { + // Special case for irreducible rank-1 stride-0 layout + return make_layout(cosize_hi); + } else { + // General case + constexpr int R = rank_v; + static_assert(R == 1 || is_static::value, + "Dynamic-stride complement only for rank-1 layouts"); + + // Should just be a sort and a fold... + // Then we could even handle dynamic strides (but they would destroy all static strides) + auto [shape_, stride_, result_shape_, result_stride] = + fold(make_seq{}, + cute::make_tuple(shape, stride, cute::make_tuple(), cute::make_tuple(Int<1>{})), + [](auto const& init, auto i) + { + auto [shape, stride, result_shape, result_stride] = init; + auto min_stride = cute::min(stride); + auto min_idx = find(stride, min_stride); + + return cute::make_tuple(remove(shape), // Remove the min_idx from shape + remove(stride), // Remove the min_idx from stride + append(result_shape , min_stride / get(result_stride)), // new shape = min_stride / last_stride + append(result_stride, get(shape) * min_stride)); // new stride = curr_shape * min_stride + }); + + // Append the last shape mode + auto result_shape = append(result_shape_, get<0>(stride_) / get(result_stride)); // new shape = min_stride / last_stride + + // Compute the rest_shape and rest_stride + auto rest_stride = get<0>(shape_) * get<0>(stride_); + auto rest_shape = ceil_div(cosize_hi, rest_stride); + // Jump into coalesce and append (rest_shape, rest_stride) + return detail::bw_coalesce(result_shape, result_stride, rest_shape, rest_stride); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +complement(Layout const& layout, CoSizeHi const& cosize_hi) +{ + static_assert(cute::is_integral::value, "Expected integral codomain size in complement."); + auto filter_layout = filter(layout); + return detail::complement(filter_layout.shape(), filter_layout.stride(), cosize_hi); +} + +template +CUTE_HOST_DEVICE constexpr +auto +complement(Layout const& layout) +{ + auto filter_layout = filter(layout); + return detail::complement(filter_layout.shape(), filter_layout.stride(), cosize(filter_layout)); +} + +// +// Right-Inverse and Left-Inverse +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +inverse_seq(Shape const& shape, Stride const& stride, seq) +{ + auto next_I = find_if(stride, [](auto a) { return is_constant{}; }); + + if constexpr (next_I == decltype(rank(stride))::value) { + return seq{}; + } else { + // auto next_stride = get(shape) * get(stride); + // NOTE: Needed for g++-7 + using next_stride = decltype(get(shape) * get(stride)); + + if constexpr (is_static::value) { + return inverse_seq(shape, stride, seq{}); + } else { + return seq{}; + } + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +// +// Build the right-inverse of a layout +// @pre is_static +// @result A layout @a result such that +// @a layout(@a result(i)) == i for all i < size(@a result) +// @result A layout @a result such that +// composition(@a layout, @a result) is identical to make_layout(shape(result)) +// + +template +CUTE_HOST_DEVICE constexpr +auto +right_inverse(Layout const& layout) +{ + auto flat_layout = coalesce(layout); + auto astride = transform_leaf(flat_layout.stride(), abs_fn{}); + + // Find Int<1>{}, the starting stride, and follow the strides to gen inverse_seq + [[maybe_unused]] auto iseq = detail::inverse_seq<1>(flat_layout.shape(), astride, seq<>{}); + + if constexpr (iseq.size() == 0) { + return Layout<_1,_0>{}; // Empty case, nothing found + } else { + // Generate the corresponding new strides and construct + auto rstride = compact_col_major(flat_layout.shape()); + return make_layout(unwrap(transform(iseq, [&](auto i) { return shape(flat_layout); })), + unwrap(transform(iseq, [&](auto i) { return signum(stride(flat_layout)) * get(rstride); }))); + } + + CUTE_GCC_UNREACHABLE; +} + +CUTE_HOST_DEVICE constexpr +auto +right_inverse(Underscore const& _) +{ + return _; +} + +// +// Build the left-inverse of a layout +// @pre is_static +// @pre @a layout is an injective function +// @result A layout @a result such that +// @a result(@a layout(i)) == i for all i < size(@a layout) +// @result A layout @a result such that +// composition(@a result, @a layout) is identical to make_layout(shape(layout)) +// + +template +CUTE_HOST_DEVICE constexpr +auto +left_inverse(Layout const& layout) +{ + return right_inverse(make_layout(layout, complement(layout))); +} + +CUTE_HOST_DEVICE constexpr +auto +left_inverse(Underscore const& _) +{ + return _; +} + +// +// Max Common Layout +// + +/* Return a layout that points to the maximum number of contiguous elements + * that logically correspond in the layouts of @a a and @a b. This is, + * the elements that could reasonably be "vectorized" in the layouts. + * + * @returns Layout R + * @post For all 0 <= i < size(R), a(R(i)) == i && b(R(i)) == i + */ +template +CUTE_HOST_DEVICE constexpr +auto +max_common_layout(Layout const& a, + Layout const& b) +{ + if constexpr (is_static::value && is_static::value && + is_static::value && is_static::value) + { + Layout inv_b = right_inverse(b); + Layout common = coalesce(composition(a, inv_b)); + + if constexpr (is_constant<1, decltype(stride<0>(common))>::value) { + // Truncate to the size of the contiguous vector (static stride-1 mode) + return composition(inv_b, layout<0>(common)); + } else { + return Layout<_1,_0>{}; + } + } else { + // CASE: One of the layouts is dynamic, can't prove alignment+vectorization is valid + // NOTE: Could weaken if we assume dynamic shapes/strides obey alignment requirements + // (i.e. are large and multiples of the vector) + return Layout<_1,_0>{}; + } +} + +/* Return Int such that N is the maximum number of contiguous elements + * that logically correspond in the layouts of @a a and @a b. This is, + * the number of elements that could reasonably be "vectorized" in the layouts. + * + * @returns Int with N >= 1 + * @post For all 0 <= n < N, a(b[n]) == n (NOTE: Problems with negative strides/coords in this post-condition) + */ +template +CUTE_HOST_DEVICE constexpr +auto +max_common_vector(Layout const& a, + Layout const& b) +{ + if constexpr (is_static::value && is_static::value && + is_static::value && is_static::value) + { + Layout common = coalesce(composition(a, right_inverse(b))); + + if constexpr (is_constant<1, decltype(stride<0>(common))>::value) { + // Truncate to the size of the contiguous vector (static stride-1 mode) + return shape<0>(common); + } else { + return Int<1>{}; + } + } else { + // CASE: One of the layouts is dynamic, can't prove alignment+vectorization is valid + // NOTE: Could weaken if we assume dynamic shapes/strides obey alignment requirements + // (i.e. are large and multiples of the vector) + return Int<1>{}; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Kernel (Nullspace) of a Layout +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +nullspace_seq(Stride const& stride, seq) +{ + if constexpr (NextI == rank_v) { + return seq{}; + } else + if constexpr (is_constant<0, decltype(get(stride))>::value) { + return detail::nullspace_seq(stride, seq{}); + } else { + return detail::nullspace_seq(stride, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +// +// Build the nullspace of a layout +// @result A layout @a result such that +// size(@a result) == size(@a layout) / size(filter(@a layout)) +// @a layout(@a result(i)) == 0 for all i < size(@a result) +// + +template +CUTE_HOST_DEVICE constexpr +auto +nullspace(Layout const& layout) +{ + auto flat_layout = flatten(layout); + + auto iseq = detail::nullspace_seq<0>(flat_layout.stride(), seq<>{}); + + if constexpr (iseq.size() == 0) { + return Layout<_1,_0>{}; // Empty case, nothing found + } else { + // Generate the corresponding new strides and construct + auto rstride = compact_col_major(flat_layout.shape()); + return make_layout(unwrap(transform(iseq, [&](auto i) { return shape(flat_layout); })), + unwrap(transform(iseq, [&](auto i) { return get(rstride); }))); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Zip +// + +template +CUTE_HOST_DEVICE constexpr +auto +zip(Layout const& layout) +{ + return make_layout(zip(layout.shape()), + zip(layout.stride())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +zip(Layout const& layoutA, + Layout const& layoutB) +{ + return make_layout(zip(layoutA.shape(), layoutB.shape()), + zip(layoutA.stride(), layoutB.stride())); +} + +// +// Tile unzip +// Logical product and logical divide (on layouts) produce rank-2 results by design. +// Follow the profile of @a tile and zip the rank-2 modes located at the terminals into +// their own mode. +// + +template +CUTE_HOST_DEVICE constexpr +auto +tile_unzip(Layout const& layout, + IntTuple const& tile) +{ + return make_layout(zip2_by(layout.shape(), tile), + zip2_by(layout.stride(), tile)); +} + +// +// Logical divide +// + +template +CUTE_HOST_DEVICE constexpr +auto +logical_divide(Layout const& layout, + Layout const& tile) +{ + //CUTE_STATIC_ASSERT_V(size(layout) % size(tile) == Int<0>{}, + // "Tiling does not evenly divide the block"); + // NOTE: With tiles that have stride-0, this doesn't have to be true + + return composition(layout, make_layout(tile, complement(tile, size(layout)))); +} + +template +CUTE_HOST_DEVICE constexpr +auto +logical_divide(Layout const& layout, + IntTuple const& tile) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value <= Layout::rank, "logical_divide: Too many modes in tile."); + return transform_layout(layout, tile, [](auto const& l, auto const& t) { return logical_divide(l,t); }); + } else if constexpr (is_underscore::value) { + return layout; + } else if constexpr (is_integral::value) { + return logical_divide(layout, make_layout(tile)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Convenience operator +// that produces layouts like ((BLK_A,BLK_B,...),(a,b,...,x,y)) +// by gathering the tile modes and residuals into a rank-2 result. +// + +template +CUTE_HOST_DEVICE constexpr +auto +zipped_divide(Layout const& layout, + Tile const& tile) +{ + return tile_unzip(logical_divide(layout, tile), tile); +} + +// Same as zipped_divide, but unpacks the second mode: ((BLK_A,BLK_B,...),a,b,...,x,y) +template +CUTE_HOST_DEVICE constexpr +auto +tiled_divide(Layout const& layout, + Tile const& tile) +{ + auto div = zipped_divide(layout, tile); + + auto R = rank<1>(div); + return div(_, repeat(_)); +} + +// +// Logical product +// + +template +CUTE_HOST_DEVICE constexpr +auto +logical_product(Layout const& layout, + Layout const& tile) +{ + return make_layout(layout, composition(complement(layout, size(layout)*cosize(tile)), tile)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +logical_product(Layout const& layout, + IntTuple const& tile) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value <= Layout::rank); + return transform_layout(layout, tile, [](auto const& l, auto const& t) { return logical_product(l,t); }); + } else if constexpr (is_underscore::value) { + return layout; + } else if constexpr (is_integral::value) { + return logical_product(layout, make_layout(tile)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Convenience operator +// that produces layouts like ((BLK_A,BLK_B,...),(a,b,...,x,y)) +// by gathering the block modes and products into a rank-2 result. +// + +template +CUTE_HOST_DEVICE constexpr +auto +zipped_product(Layout const& layout, + Tile const& tile) +{ + return tile_unzip(logical_product(layout, tile), tile); +} + +// Same as zipped_product, but unpacks the second mode: ((BLK_A,BLK_B,...),a,b,...,x,y) +template +CUTE_HOST_DEVICE constexpr +auto +tiled_product(Layout const& layout, + Tile const& tile) +{ + auto div = zipped_product(layout, tile); + + auto R = rank(tile); + return div(_, repeat(_)); +} + +// Attempts to reproduce layout "block" over layout "layout" +// That is, think of every element of "layout" as a "block" +// and return the layout of the resulting structure +template +CUTE_HOST_DEVICE constexpr +auto +blocked_product(Layout const& block, + Layout const& layout) +{ + constexpr int R = cute::max(rank_v, rank_v); + auto padded_block = append(block); + auto padded_layout = append(layout); + + auto result = logical_product(padded_block, padded_layout); + + return coalesce(zip(get<0>(result), get<1>(result)), repeat(Int<1>{})); +} + +template +CUTE_HOST_DEVICE constexpr +auto +raked_product(Layout const& block, + Layout const& layout) +{ + constexpr int R = cute::max(rank_v, rank_v); + auto padded_block = append(block); + auto padded_layout = append(layout); + + auto result = logical_product(padded_block, padded_layout); + + return coalesce(zip(get<1>(result), get<0>(result)), repeat(Int<1>{})); +} + +template +CUTE_HOST_DEVICE constexpr +auto +tile_to_shape(Layout const& layout, + TrgShape const& trg_shape, + ModeOrder const& ord_shape = {}) +{ + CUTE_STATIC_ASSERT_V(rank(layout) <= rank(trg_shape), "Rank of layout must be <= rank of target shape."); + constexpr int R = rank_v; + + auto padded_layout = append(layout); + + auto layout_shape = product_each(padded_layout.shape()); + auto target_shape = product_each(trg_shape); + + // Assert proper division + CUTE_STATIC_ASSERT_V(sum(transform(target_shape, layout_shape, modulus{})) == Int<0>{}, + "Layout shape does not divide the target shape."); + + auto product_shape = shape_div(target_shape, layout_shape); + + return coalesce(blocked_product(padded_layout, make_ordered_layout(product_shape, ord_shape)), product_shape); +} + +// +// Upcast +// For stride-1 mode, divide size by N. Divide all other strides by N. +// + +template +CUTE_HOST_DEVICE constexpr +auto +upcast(Shape const& shape, Stride const& stride) +{ + if constexpr (is_tuple::value) { // tuple stride + return transform_layout(shape, stride, [](auto const& s, auto const& d) { return upcast(s,d); }); + } else if constexpr (is_constant<0, Stride>::value) { // static-0 stride + return Layout{shape,stride}; + } else if constexpr (is_static::value) { // static stride + return make_layout(shape_div(shape, shape_div(Int{}, abs(stride))), + shape_div(stride, Int{})); + } else { // dynamic stride + // assume dynamic strides are larger than N and divisible + // assert(stride % N == 0); + return make_layout(shape, safe_div(stride, Int{})); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +upcast(Layout const& layout) +{ + return upcast(layout.shape(), layout.stride()); +} + +// +// Downcast +// For stride-1 mode, multiply size by N. Multiply all other strides by N. +// + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(Shape const& shape, Stride const& stride) +{ + if constexpr (is_tuple::value) { + return transform_layout(shape, stride, [](auto const& s, auto const& d) { return downcast(s,d); }); + } else if constexpr (is_constant<1, Stride>::value || is_constant<-1, Stride>::value) { + return make_layout(shape * Int{}, stride); + } else { + return make_layout(shape, stride * Int{}); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(Layout const& layout) +{ + CUTE_STATIC_ASSERT(has_int1::value, "Downcast requires adjacent elements"); + return downcast(layout.shape(), layout.stride()); +} + +// +// Recast +// + +template +CUTE_HOST_DEVICE constexpr +auto +recast(Layout const& layout) +{ + if constexpr (sizeof_bits::value == sizeof_bits::value) { + return layout; + } else if constexpr (sizeof_bits::value > sizeof_bits::value) { + static_assert(sizeof_bits::value % sizeof_bits::value == 0, "NewType must be a multiple of OldType"); + return upcast::value/sizeof_bits::value>(layout); + } else if constexpr (sizeof_bits::value < sizeof_bits::value) { + static_assert(sizeof_bits::value % sizeof_bits::value == 0, "NewType must be a divisor of OldType"); + return downcast::value/sizeof_bits::value>(layout); + } + CUTE_GCC_UNREACHABLE; +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(Layout const& layout) +{ + print(layout.shape()); print(":"); print(layout.stride()); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, Layout const& layout) +{ + return os << shape(layout) << ":" << stride(layout); +} +#endif + +// Generic 2D Layout to console table +template +CUTE_HOST_DEVICE +void +print_layout(Layout const& layout) // (m,n) -> idx +{ + CUTE_STATIC_ASSERT_V(rank(layout) == Int<2>{}); + + int idx_width = num_digits(cosize(layout)) + 2; + const char* delim = "+-----------------------"; + + print(layout); print("\n"); + + // Column indices + print(" "); + for (int n = 0; n < size<1>(layout); ++n) { printf(" %*d ", idx_width-2, n); } + printf("\n"); + + // Print out A m-by-n + for (int m = 0; m < size<0>(layout); ++m) { + // Header + print(" "); + for (int n = 0; n < size<1>(layout); ++n) { printf("%.*s", idx_width+1, delim); } + printf("+\n"); + // Values + printf("%2d ", m); // Row indices + for (int n = 0; n < size<1>(layout); ++n) { printf("| %*d ", idx_width-2, int(layout(m,n))); } + printf("|\n"); + } + // Footer + print(" "); + for (int n = 0; n < size<1>(layout); ++n) { printf("%.*s", idx_width+1, delim); } + printf("+\n"); +} + +// Generic ThrVal 2D Layout to console table +template +CUTE_HOST_DEVICE +void +print_layout(Layout const& layout, ThrID const& thrid) // (m,n) -> (tid,vid) and tid -> thr_idx +{ + CUTE_STATIC_ASSERT_V(rank(layout) == Int<2>{}); + + print(layout); print("\n"); + print(thrid); print("\n"); + + // Print out m-by-n + for (int m = 0; m < size<0>(layout); ++m) { + // Header + for (int n = 0; n < size<1>(layout); ++n) printf("+------"); + printf("+\n"); + // Values + for (int n = 0; n < size<1>(layout); ++n) printf("|%03d-%02d", int(thrid(layout(m,n) % size(thrid))), int(layout(m,n) / size(thrid))); + printf("|\n"); + } + // Footer + for (int n = 0; n < size<1>(layout); ++n) printf("+------"); + printf("+\n"); +} + +// Generic 2D Layout to Latex printer -- B&W 8-value color coding +template +CUTE_HOST_DEVICE +void +print_latex(Layout const& layout) // (m,n) -> idx +{ + CUTE_STATIC_ASSERT_V(rank(layout) == Int<2>{}); + + char const* latex_header = + "\\documentclass[convert]{standalone}\n" + "\\usepackage{tikz}\n\n" + "\\begin{document}\n" + "\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},box/.style={rectangle,draw=black,thick,minimum size=1cm,anchor=center,font=\\Large}]\n\n"; + char const* latex_footer = + "\\end{tikzpicture}\n" + "\\end{document}\n"; + + char const* color_map[8] = {"black!00", + "black!40", + "black!20", + "black!60", + "black!10", + "black!50", + "black!30", + "black!70"}; + + // Header + printf("%% Layout: "); print(layout); printf("\n"); + + printf(latex_header); + + // Layout + for (int i = 0; i < size<0>(layout); ++i) { + for (int j = 0; j < size<1>(layout); ++j) { + int idx = layout(i,j); + + printf("\\node[box,fill=%s] at (%d,%d) {%d};\n", + color_map[idx % 8], + i, j, + idx); + } + } + + // Labels + for (int i = 0, j = -1; i < size<0>(layout); ++i) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, i); + } + for (int j = 0, i = -1; j < size<1>(layout); ++j) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, j); + } + + // Footer + printf(latex_footer); +} + +// Generic ThrVal 2D Layout to Latex TIKZ -- 8-value color coded by thread +template +CUTE_HOST_DEVICE +void +print_latex(Layout const& layout, ThrID const& thr) // (m,n) -> (tid,vid) and tid -> thr_idx +{ + CUTE_STATIC_ASSERT_V(rank(layout) == Int<2>{}); + + char const* latex_header = + "\\documentclass[convert]{standalone}\n" + "\\usepackage{tikz}\n\n" + "\\begin{document}\n" + "\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},box/.style={rectangle,draw=black,thick,minimum size=1cm,anchor=center}]\n\n"; + char const* latex_footer = + "\\end{tikzpicture}\n" + "\\end{document}\n"; + + char const* color_map[8] = {"{rgb,255:red,175;green,175;blue,255}", + "{rgb,255:red,175;green,255;blue,175}", + "{rgb,255:red,255;green,255;blue,175}", + "{rgb,255:red,255;green,175;blue,175}", + "{rgb,255:red,210;green,210;blue,255}", + "{rgb,255:red,210;green,255;blue,210}", + "{rgb,255:red,255;green,255;blue,210}", + "{rgb,255:red,255;green,210;blue,210}"}; + + // Header + printf("%% layout: "); print(layout); printf("\n"); + printf("%% thrid: "); print(thr); printf("\n\n"); + + printf(latex_header); + + // Layout + for (int i = 0; i < size<0>(layout); ++i) { + for (int j = 0; j < size<1>(layout); ++j) { + int thrid = layout(i,j) % size(thr); + int val_idx = layout(i,j) / size(thr); + int thr_idx = thr(thrid); + + printf("\\node[box,fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n", + color_map[thr_idx % 8], + i, j, + thr_idx, val_idx); + } + } + + // Labels + for (int i = 0, j = -1; i < size<0>(layout); ++i) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, i); + } + for (int j = 0, i = -1; j < size<1>(layout); ++j) { + printf("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n", i, j, j); + } + + // Footer + printf(latex_footer); +} + +} // end namespace cute + +// +// Extended Layouts +// + +#include diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/arithmetic_tuple_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/arithmetic_tuple_hip.hpp new file mode 100644 index 000000000..1de69eb48 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/arithmetic_tuple_hip.hpp @@ -0,0 +1,552 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include +#include + +namespace cute +{ + +template +struct ArithmeticTuple : tuple +{ + template + CUTE_HOST_DEVICE constexpr + ArithmeticTuple(ArithmeticTuple const& u) + : tuple(static_cast const&>(u)) {} + + template + CUTE_HOST_DEVICE constexpr + ArithmeticTuple(tuple const& u) + : tuple(u) {} + + template + CUTE_HOST_DEVICE constexpr + ArithmeticTuple(U const&... u) + : tuple(u...) {} +}; + +template +struct is_tuple> : true_type {}; + +template +CUTE_HOST_DEVICE constexpr +auto +make_arithmetic_tuple(T const&... t) { + return ArithmeticTuple(t...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(tuple const& t) { + return ArithmeticTuple(t); +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +T const& +as_arithmetic_tuple(T const& t) { + return t; +} + +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(ArithmeticTuple const& t) { + return t; +} + +// +// Numeric operators +// + +// Addition +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ArithmeticTuple const& t, ArithmeticTuple const& u) { + constexpr int R = cute::max(int(sizeof...(T)), int(sizeof...(U))); + return transform_apply(append(t,Int<0>{}), append(u,Int<0>{}), plus{}, [](auto const&... a){ return make_arithmetic_tuple(a...); }); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ArithmeticTuple const& t, tuple const& u) { + constexpr int R = cute::max(int(sizeof...(T)), int(sizeof...(U))); + return transform_apply(append(t,Int<0>{}), append(u,Int<0>{}), plus{}, [](auto const&... a){ return make_arithmetic_tuple(a...); }); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(tuple const& t, ArithmeticTuple const& u) { + constexpr int R = cute::max(int(sizeof...(T)), int(sizeof...(U))); + return transform_apply(append(t,Int<0>{}), append(u,Int<0>{}), plus{}, [](auto const&... a){ return make_arithmetic_tuple(a...); }); +} + +// +// Special cases +// + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(C, ArithmeticTuple const& u) { + if constexpr (t == 0) { + return u; + } else { + static_assert(t == 0, "Artihmetic tuple op+ error!"); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ArithmeticTuple const& t, C) { + if constexpr (u == 0) { + return t; + } else { + static_assert(u == 0, "Artihmetic tuple op+ error!"); + } +} + +// +// ArithmeticTupleIterator +// + +template +struct ArithmeticTupleIterator +{ + ArithTuple coord_; + + CUTE_HOST_DEVICE constexpr + ArithmeticTupleIterator() : coord_() {} + CUTE_HOST_DEVICE constexpr + ArithmeticTupleIterator(ArithTuple const& coord) : coord_(coord) {} + + CUTE_HOST_DEVICE constexpr + ArithTuple const& operator*() const { return coord_; } + + template + CUTE_HOST_DEVICE constexpr + auto operator+(Coord const& c) const { + return ArithmeticTupleIterator(coord_ + c); + } + + template + CUTE_HOST_DEVICE constexpr + auto operator[](Coord const& c) const { return *(*this + c); } +}; + +template +CUTE_HOST_DEVICE void print(ArithmeticTupleIterator const& iter) { + printf("ArithTuple"); print(iter.coord_); +} + +// +// ArithmeticTuple "basis" elements +// A ScaledBasis is a (at least) rank-N+1 ArithmeticTuple: +// (_0,_0,...,T,_0,...) +// with value T in the Nth mode + +template +struct ScaledBasis : private tuple +{ + CUTE_HOST_DEVICE constexpr + ScaledBasis(T const& t = {}) : tuple(t) {} + + CUTE_HOST_DEVICE constexpr + decltype(auto) value() { return get<0>(static_cast &>(*this)); } + CUTE_HOST_DEVICE constexpr + decltype(auto) value() const { return get<0>(static_cast const&>(*this)); } + + CUTE_HOST_DEVICE static constexpr + auto mode() { return Int{}; } +}; + +template +struct is_scaled_basis : false_type {}; +template +struct is_scaled_basis> : true_type {}; + +template +struct is_integral> : true_type {}; + +// Get the scalar T out of a ScaledBasis +template +CUTE_HOST_DEVICE constexpr auto +basis_value(SB const& e) +{ + if constexpr (is_scaled_basis::value) { + return basis_value(e.value()); + } else { + return e; + } + CUTE_GCC_UNREACHABLE; +} + +// Apply the N... pack to another Tuple +template +CUTE_HOST_DEVICE constexpr auto +basis_get(SB const& e, Tuple const& t) +{ + if constexpr (is_scaled_basis::value) { + return basis_get(e.value(), get(t)); + } else { + return t; + } + CUTE_GCC_UNREACHABLE; +} + +namespace detail { + +template +struct Basis; + +template <> +struct Basis<> { + using type = Int<1>; +}; + +template +struct Basis { + using type = ScaledBasis::type, N>; +}; + +} // end namespace detail + +// Shortcut for writing ScaledBasis, N0>, N1>, ...> +// E<> := _1 +// E<0> := (_1,_0,_0,...) +// E<1> := (_0,_1,_0,...) +// E<0,0> := ((_1,_0,_0,...),_0,_0,...) +// E<0,1> := ((_0,_1,_0,...),_0,_0,...) +// E<1,0> := (_0,(_1,_0,_0,...),_0,...) +// E<1,1> := (_0,(_0,_1,_0,...),_0,...) +template +using E = typename detail::Basis::type; + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(T const& t, seq, seq) { + return make_arithmetic_tuple((void(I),Int<0>{})..., t, (void(J),Int<0>{})...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(ArithmeticTuple const& t, seq, seq) { + return make_arithmetic_tuple(get(t)..., (void(J),Int<0>{})...); +} + +} // end namespace detail + +// Turn a ScaledBases into a rank-M ArithmeticTuple +// with N prefix 0s: (_0,_0,...N...,_0,T,_0,...,_0,_0) +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(ScaledBasis const& t) { + static_assert(M > N, "Mismatched ranks"); + return detail::as_arithmetic_tuple(t.value(), make_seq{}, make_seq{}); +} + +// Turn a ScaledBases into a rank-N ArithmeticTuple +// with N prefix 0s: (_0,_0,...N...,_0,T) +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(ScaledBasis const& t) { + return as_arithmetic_tuple(t); +} + +// Turn an ArithmeticTuple into a rank-M ArithmeticTuple +// with postfix 0s: (t0,t1,t2,...,_0,...,_0,_0) +template +CUTE_HOST_DEVICE constexpr +auto +as_arithmetic_tuple(ArithmeticTuple const& t) { + static_assert(M >= sizeof...(T), "Mismatched ranks"); + return detail::as_arithmetic_tuple(t, make_seq{}, make_seq{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +safe_div(ScaledBasis const& b, U const& u) +{ + auto t = safe_div(b.value(), u); + return ScaledBasis{t}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +shape_div(ScaledBasis const& b, U const& u) +{ + auto t = shape_div(b.value(), u); + return ScaledBasis{t}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_basis_like(Shape const& shape) +{ + if constexpr (is_integral::value) { + return Int<1>{}; + } + else { + // Generate bases for each rank of shape + return transform(tuple_seq{}, shape, [](auto I, auto si) { + // Generate bases for each rank of si and add an i on front + using I_type = decltype(I); + return transform_leaf(make_basis_like(si), [](auto e) { + // MSVC has trouble capturing variables as constexpr, + // so that they can be used as template arguments. + // This is exactly what the code needs to do with i, unfortunately. + // The work-around is to define i inside the inner lambda, + // by using just the type from the enclosing scope. + constexpr int i = I_type::value; + return ScaledBasis{}; + }); + }); + } + + CUTE_GCC_UNREACHABLE; +} + +// Equality +template +CUTE_HOST_DEVICE constexpr +auto +operator==(ScaledBasis const& t, ScaledBasis const& u) { + return bool_constant{} && t.value() == u.value(); +} + +// Not equal to anything else +template +CUTE_HOST_DEVICE constexpr +false_type +operator==(ScaledBasis const&, U const&) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +false_type +operator==(T const&, ScaledBasis const&) { + return {}; +} + +// Abs +template +CUTE_HOST_DEVICE constexpr +auto +abs(ScaledBasis const& e) { + return ScaledBasis{abs(e.value())}; +} + +// Multiplication +template +CUTE_HOST_DEVICE constexpr +auto +operator*(A const& a, ScaledBasis const& e) { + auto r = a * e.value(); + return ScaledBasis{r}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator*(ScaledBasis const& e, B const& b) { + auto r = e.value() * b; + return ScaledBasis{r}; +} + +// Addition +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ScaledBasis const& t, ArithmeticTuple const& u) { + constexpr int R = cute::max(N+1, int(sizeof...(U))); + return as_arithmetic_tuple(t) + as_arithmetic_tuple(u); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ArithmeticTuple const& t, ScaledBasis const& u) { + constexpr int R = cute::max(int(sizeof...(T)), M+1); + return as_arithmetic_tuple(t) + as_arithmetic_tuple(u); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ScaledBasis const& t, tuple const& u) { + constexpr int R = cute::max(N+1, int(sizeof...(U))); + return as_arithmetic_tuple(t) + as_arithmetic_tuple(u); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(tuple const& t, ScaledBasis const& u) { + constexpr int R = cute::max(int(sizeof...(T)), M+1); + return as_arithmetic_tuple(t) + as_arithmetic_tuple(u); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ScaledBasis const& t, ScaledBasis const& u) { + constexpr int R = cute::max(N+1,M+1); + return as_arithmetic_tuple(t) + as_arithmetic_tuple(u); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(C, ScaledBasis const& u) { + if constexpr (t == 0) { + return u; + } else { + static_assert(t == 0, "ScaledBasis op+ error!"); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator+(ScaledBasis const& t, C) { + if constexpr (u == 0) { + return t; + } else { + static_assert(u == 0, "ScaledBasis op+ error!"); + } +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(ScaledBasis const& e) { + print(e.value()); printf("@%d", N); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, ScaledBasis const& e) { + return os << e.value() << "@" << N; +} +#endif + +} // end namespace cute + + +namespace CUTE_STL_NAMESPACE +{ + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +} // end namespace CUTE_STL_NAMESPACE + +#ifdef CUTE_STL_NAMESPACE_IS_CUDA_STD +namespace std +{ + +#if defined(__CUDACC_RTC__) +template +struct tuple_size; + +template +struct tuple_element; +#endif + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +template +struct tuple_size> + : CUTE_STL_NAMESPACE::integral_constant +{}; + +template +struct tuple_element> + : CUTE_STL_NAMESPACE::tuple_element> +{}; + +} // end namespace std +#endif // CUTE_STL_NAMESPACE_IS_CUDA_STD diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/bfloat_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/bfloat_hip.hpp new file mode 100644 index 000000000..23a7c9081 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/bfloat_hip.hpp @@ -0,0 +1,59 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute { + +using cutlass::bfloat16_t; + +// +// Display utilities +// + +#if !defined(__CUDACC_RTC__) +CUTE_HOST std::ostream& operator<<(std::ostream& os, bfloat16_t const& v) +{ + return os << float(v); +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/complex_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/complex_hip.hpp new file mode 100644 index 000000000..1286e8a27 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/complex_hip.hpp @@ -0,0 +1,79 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +#include + +namespace cute +{ + +using cutlass::complex; +using cutlass::is_complex; +using cutlass::RealType; +using cutlass::real; +using cutlass::imag; +using cutlass::conj; + +/// Fused multiply-add for complex numbers +template +CUTE_HOST_DEVICE constexpr +void +fma(complex & d, + complex const& a, + complex const& b, + complex const& c) +{ + d.real(fma( a.real(), b.real(), c.real())); + d.imag(fma( a.real(), b.imag(), c.imag())); + d.real(fma(-a.imag(), b.imag(), d.real())); + d.imag(fma( a.imag(), b.real(), d.imag())); +} + +/// Fused multiply-add for triplets +template +CUTE_HOST_DEVICE constexpr +void +fma(complex const& a, + complex const& b, + complex & c) +{ + return fma(c, a, b, c); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/float8_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/float8_hip.hpp new file mode 100644 index 000000000..e907cc706 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/float8_hip.hpp @@ -0,0 +1,49 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute { + +using cutlass::float_e4m3_t; +using cutlass::float_e5m2_t; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/half_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/half_hip.hpp new file mode 100644 index 000000000..f9dc32be5 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/half_hip.hpp @@ -0,0 +1,47 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +#include + +namespace cute { + +using cutlass::half_t; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/int_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/int_hip.hpp new file mode 100644 index 000000000..4a375e447 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/int_hip.hpp @@ -0,0 +1,152 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include +#include + +namespace cute +{ + +// +// Signed integers +// + +using int2_t = cute::int2b_t; +using int4_t = cute::int4b_t; +using int8_t = CUTE_STL_NAMESPACE::int8_t; +using int16_t = CUTE_STL_NAMESPACE::int16_t; +using int32_t = CUTE_STL_NAMESPACE::int32_t; +using int64_t = CUTE_STL_NAMESPACE::int64_t; + +template struct int_bit; +template <> struct int_bit< 2> { using type = cute::int2b_t; }; +template <> struct int_bit< 4> { using type = cute::int4b_t; }; +template <> struct int_bit< 8> { using type = int8_t; }; +template <> struct int_bit< 16> { using type = int16_t; }; +template <> struct int_bit< 32> { using type = int32_t; }; +template <> struct int_bit< 64> { using type = int64_t; }; + +template +using int_bit_t = typename int_bit::type; + +template +using int_byte = int_bit<8*N>; + +template +using int_byte_t = typename int_byte::type; + +// +// Unsigned integers +// + +using uint1_t = cute::uint1b_t; +using uint2_t = cute::uint2b_t; +using uint4_t = cute::uint4b_t; +using uint8_t = CUTE_STL_NAMESPACE::uint8_t; +using uint16_t = CUTE_STL_NAMESPACE::uint16_t; +using uint32_t = CUTE_STL_NAMESPACE::uint32_t; +using uint64_t = CUTE_STL_NAMESPACE::uint64_t; +using uint128_t = cute::uint128_t; + +template struct uint_bit; +template <> struct uint_bit< 1> { using type = cute::uint1b_t; }; +template <> struct uint_bit< 2> { using type = cute::uint2b_t; }; +template <> struct uint_bit< 4> { using type = cute::uint4b_t; }; +template <> struct uint_bit< 8> { using type = uint8_t; }; +template <> struct uint_bit< 16> { using type = uint16_t; }; +template <> struct uint_bit< 32> { using type = uint32_t; }; +template <> struct uint_bit< 64> { using type = uint64_t; }; +template <> struct uint_bit<128> { using type = cute::uint128_t; }; + +template +using uint_bit_t = typename uint_bit::type; + +template +using uint_byte = uint_bit<8*N>; + +template +using uint_byte_t = typename uint_byte::type; + +// +// sizeof_bytes +// + +template +struct sizeof_bytes { + static constexpr size_t value = sizeof(T); +}; +template +static constexpr int sizeof_bytes_v = sizeof_bytes::value; + +// +// sizeof_bits +// + +template +struct sizeof_bits { + static constexpr size_t value = sizeof(T) * 8; +}; + +template +struct sizeof_bits: sizeof_bits {}; + +template <> +struct sizeof_bits { + static constexpr size_t value = 0; +}; + +template <> +struct sizeof_bits { + static constexpr size_t value = 1; +}; + +template +struct sizeof_bits> { + static constexpr size_t value = Bits; +}; + +template +static constexpr int sizeof_bits_v = sizeof_bits::value; + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_sequence_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_sequence_hip.hpp new file mode 100644 index 000000000..846a36943 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_sequence_hip.hpp @@ -0,0 +1,157 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include +#include +#include + +namespace cute +{ + +using CUTE_STL_NAMESPACE::integer_sequence; +using CUTE_STL_NAMESPACE::make_integer_sequence; + +namespace detail { + +template +struct range_impl; + +template +struct range_impl, Begin> { + using type = integer_sequence; +}; + +template +struct reverse_impl; + +template +struct reverse_impl> { + using type = integer_sequence; +}; + +} // end namespace detail + +template +using make_integer_range = typename detail::range_impl< + T, + make_integer_sequence 0) ? (End-Begin) : 0>, + Begin>::type; + +template +using make_integer_sequence_reverse = typename detail::reverse_impl< + make_integer_sequence>::type; + +// +// Common aliases +// + +// int_sequence + +template +using int_sequence = integer_sequence; + +template +using make_int_sequence = make_integer_sequence; + +template +using make_int_rsequence = make_integer_sequence_reverse; + +template +using make_int_range = make_integer_range; + +// index_sequence + +template +using index_sequence = integer_sequence; + +template +using make_index_sequence = make_integer_sequence; + +template +using make_index_rsequence = make_integer_sequence_reverse; + +template +using make_index_range = make_integer_range; + +// +// Shortcuts +// + +template +using seq = int_sequence; + +template +using make_seq = make_int_sequence; + +template +using make_rseq = make_int_rsequence; + +template +using make_range = make_int_range; + +template +using tuple_seq = make_seq>::value>; + +template +using tuple_rseq = make_rseq>::value>; + +// +// Specialize cute::tuple-traits for std::integer_sequence +// + +template +struct tuple_size> + : cute::integral_constant +{}; + +template +struct tuple_element> +{ + constexpr static T idx[sizeof...(Is)] = {Is...}; + using type = cute::integral_constant; +}; + +template +CUTE_HOST_DEVICE constexpr +tuple_element_t> +get(integer_sequence) { + static_assert(I < sizeof...(Ints), "Index out of range"); + return {}; +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_subbyte_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_subbyte_hip.hpp new file mode 100644 index 000000000..7a0419a73 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_subbyte_hip.hpp @@ -0,0 +1,239 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include +#include + +namespace cute { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct integer_subbyte +{ + /// Storage type + using Storage = uint8_t; + + /// Number of bits + static_assert(Bits <= 8*sizeof(Storage), "Require a subbyte of bits in integer_subbyte"); + + /// External type + using xint_t = typename conditional::type; + + /// Bitmask for truncation from larger integers + static constexpr Storage bits_mask_ = Storage((1 << Bits) - 1); + /// Bitmask for the sign bit + static constexpr Storage sign_mask_ = Storage((Signed ? 1 : 0) << (Bits - 1)); + + // + // Data members + // + + Storage storage; + + // + // Methods + // + + /// No operation + CUTE_HOST_DEVICE constexpr + integer_subbyte() {} + + /// Conversion from integer type + CUTE_HOST_DEVICE constexpr + integer_subbyte(int value) // NOTE: Sign extension? + : storage(reinterpret_cast(value) & bits_mask_) {} + + CUTE_HOST_DEVICE constexpr + integer_subbyte(unsigned value) + : storage(reinterpret_cast(value) & bits_mask_) {} + + /// Convert to int or unsigned + CUTE_HOST_DEVICE constexpr + operator xint_t() const { + if (sign_mask_ & storage) { // Sign extend + return xint_t(storage) | ~xint_t(bits_mask_); + } else { + return xint_t(storage); + } + } + + /// Equality + CUTE_HOST_DEVICE constexpr + bool operator==(integer_subbyte const& rhs) const { + return storage == rhs.storage; + } + + /// Inequality + CUTE_HOST_DEVICE constexpr + bool operator!=(integer_subbyte const& rhs) const { + return storage != rhs.storage; + } + + /// Less than or equal + CUTE_HOST_DEVICE constexpr + bool operator<=(integer_subbyte const& rhs) const { + if (sign_mask_ & storage) { + return !(rhs.storage < storage); + } else { + return storage <= rhs.storage; + } + } + + /// Less than + CUTE_HOST_DEVICE constexpr + bool operator<(integer_subbyte const& rhs) const { + if (sign_mask_ & storage) { + return !(rhs.storage <= storage); + } else { + return storage < rhs.storage; + } + } + + /// Greater than or equal + CUTE_HOST_DEVICE constexpr + bool operator>=(integer_subbyte const& rhs) const { + return !(*this < rhs); + } + + /// Greater than + CUTE_HOST_DEVICE constexpr + bool operator>(integer_subbyte const& rhs) const { + return !(*this <= rhs); + } +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// 1-bit unsigned integer type +using uint1b_t = integer_subbyte<1, false>; + +/// 2-bit integer type +using int2b_t = integer_subbyte<2, true>; + +/// 2-bit unsigned integer type +using uint2b_t = integer_subbyte<2, false>; + +/// 4-bit integer type +using int4b_t = integer_subbyte<4, true>; + +/// 4-bit unsigned integer type +using uint4b_t = integer_subbyte<4, false>; + +/// 1-bit binary type +using bin1_t = bool; + +} // namespace cute + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#if !defined(__CUDACC_RTC__) + +#include + +namespace CUTE_STL_NAMESPACE { + +template <> +struct numeric_limits { + CUTE_HOST_DEVICE static constexpr + cute::uint1b_t const lowest() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint1b_t const min() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint1b_t const max() noexcept { return 1; } + static constexpr bool is_integer = true; + static constexpr bool is_signed = false; +}; + +template <> +struct numeric_limits { + CUTE_HOST_DEVICE static constexpr + cute::int2b_t lowest() noexcept { return -2; } + CUTE_HOST_DEVICE static constexpr + cute::int2b_t min() noexcept { return -2; } + CUTE_HOST_DEVICE static constexpr + cute::int2b_t max() noexcept { return 1; } + static constexpr bool is_integer = true; + static constexpr bool is_signed = true; +}; + +template <> +struct numeric_limits { + CUTE_HOST_DEVICE static constexpr + cute::uint2b_t const lowest() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint2b_t const min() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint2b_t const max() noexcept { return 3; } + static constexpr bool is_integer = true; + static constexpr bool is_signed = false; +}; + +template <> +struct numeric_limits { + CUTE_HOST_DEVICE static constexpr + cute::int4b_t lowest() noexcept { return -8; } + CUTE_HOST_DEVICE static constexpr + cute::int4b_t min() noexcept { return -8; } + CUTE_HOST_DEVICE static constexpr + cute::int4b_t max() noexcept { return 7; } + static constexpr bool is_integer = true; + static constexpr bool is_signed = true; +}; + +template <> +struct numeric_limits { + CUTE_HOST_DEVICE static constexpr + cute::uint4b_t const lowest() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint4b_t const min() noexcept { return 0; } + CUTE_HOST_DEVICE static constexpr + cute::uint4b_t const max() noexcept { return 15; } + static constexpr bool is_integer = true; + static constexpr bool is_signed = false; +}; + +} // namespace std + +#endif // !defined(__CUDACC_RTC__) diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_constant_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_constant_hip.hpp new file mode 100644 index 000000000..92a5b1008 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_constant_hip.hpp @@ -0,0 +1,437 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include "cute/util/print_hip.hpp" +#include "cute/util/type_traits_hip.hpp" +#include "cute/numeric/math_hip.hpp" + +namespace cute +{ + +// A constant value: short name and type-deduction for fast compilation +template +struct C { + using type = C; + static constexpr auto value = v; + using value_type = decltype(v); + CUTE_HOST_DEVICE constexpr operator value_type() const noexcept { return value; } + CUTE_HOST_DEVICE constexpr value_type operator()() const noexcept { return value; } +}; + +// Deprecate +template +using constant = C; + +template +using bool_constant = C; + +using true_type = bool_constant; +using false_type = bool_constant; + +// A more std:: conforming integral_constant that enforces type but interops with C +template +struct integral_constant : C { + using type = integral_constant; + static constexpr T value = v; + using value_type = T; + // Disambiguate C::operator value_type() + //CUTE_HOST_DEVICE constexpr operator value_type() const noexcept { return value; } + CUTE_HOST_DEVICE constexpr value_type operator()() const noexcept { return value; } +}; + +// +// Traits +// + +// Use cute::is_std_integral to match built-in integral types (int, int64_t, unsigned, etc) +// Use cute::is_integral to match both built-in integral types AND static integral types. + +template +struct is_integral : bool_constant::value> {}; +template +struct is_integral > : true_type {}; +template +struct is_integral> : true_type {}; + +// is_static detects if an (abstract) value is defined completely by it's type (no members) + +template +struct is_static : bool_constant::value> {}; + +template +constexpr bool is_static_v = is_static::value; + +// is_constant detects if a type is a static integral type and if v is equal to a value + +template +struct is_constant : false_type {}; +template +struct is_constant : is_constant {}; +template +struct is_constant : is_constant {}; +template +struct is_constant : is_constant {}; +template +struct is_constant : is_constant {}; +template +struct is_constant > : bool_constant {}; +template +struct is_constant> : bool_constant {}; + +// +// Specializations +// + +template +using Int = C; + +using _m32 = Int<-32>; +using _m24 = Int<-24>; +using _m16 = Int<-16>; +using _m12 = Int<-12>; +using _m10 = Int<-10>; +using _m9 = Int<-9>; +using _m8 = Int<-8>; +using _m7 = Int<-7>; +using _m6 = Int<-6>; +using _m5 = Int<-5>; +using _m4 = Int<-4>; +using _m3 = Int<-3>; +using _m2 = Int<-2>; +using _m1 = Int<-1>; +using _0 = Int<0>; +using _1 = Int<1>; +using _2 = Int<2>; +using _3 = Int<3>; +using _4 = Int<4>; +using _5 = Int<5>; +using _6 = Int<6>; +using _7 = Int<7>; +using _8 = Int<8>; +using _9 = Int<9>; +using _10 = Int<10>; +using _12 = Int<12>; +using _16 = Int<16>; +using _24 = Int<24>; +using _32 = Int<32>; +using _64 = Int<64>; +using _96 = Int<96>; +using _128 = Int<128>; +using _192 = Int<192>; +using _256 = Int<256>; +using _512 = Int<512>; +using _1024 = Int<1024>; +using _2048 = Int<2048>; +using _4096 = Int<4096>; +using _8192 = Int<8192>; +using _16384 = Int<16384>; +using _32768 = Int<32768>; +using _65536 = Int<65536>; +using _131072 = Int<131072>; +using _262144 = Int<262144>; +using _524288 = Int<524288>; + +/***************/ +/** Operators **/ +/***************/ + +#define CUTE_LEFT_UNARY_OP(OP) \ + template \ + CUTE_HOST_DEVICE constexpr \ + C<(OP t)> operator OP (C) { \ + return {}; \ + } +#define CUTE_RIGHT_UNARY_OP(OP) \ + template \ + CUTE_HOST_DEVICE constexpr \ + C<(t OP)> operator OP (C) { \ + return {}; \ + } +#define CUTE_BINARY_OP(OP) \ + template \ + CUTE_HOST_DEVICE constexpr \ + C<(t OP u)> operator OP (C, C) { \ + return {}; \ + } + +CUTE_LEFT_UNARY_OP(+); +CUTE_LEFT_UNARY_OP(-); +CUTE_LEFT_UNARY_OP(~); +CUTE_LEFT_UNARY_OP(!); +CUTE_LEFT_UNARY_OP(*); + +CUTE_BINARY_OP( +); +CUTE_BINARY_OP( -); +CUTE_BINARY_OP( *); +CUTE_BINARY_OP( /); +CUTE_BINARY_OP( %); +CUTE_BINARY_OP( &); +CUTE_BINARY_OP( |); +CUTE_BINARY_OP( ^); +CUTE_BINARY_OP(<<); +CUTE_BINARY_OP(>>); + +CUTE_BINARY_OP(&&); +CUTE_BINARY_OP(||); + +CUTE_BINARY_OP(==); +CUTE_BINARY_OP(!=); +CUTE_BINARY_OP( >); +CUTE_BINARY_OP( <); +CUTE_BINARY_OP(>=); +CUTE_BINARY_OP(<=); + +#undef CUTE_BINARY_OP +#undef CUTE_LEFT_UNARY_OP +#undef CUTE_RIGHT_UNARY_OP + +// +// Mixed static-dynamic special cases +// + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator*(C, U) { + return {}; +} + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator*(U, C) { + return {}; +} + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator/(C, U) { + return {}; +} + +template ::value && (t == 1 || t == -1))> +CUTE_HOST_DEVICE constexpr +C<0> +operator%(U, C) { + return {}; +} + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator%(C, U) { + return {}; +} + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator&(C, U) { + return {}; +} + +template ::value && t == 0)> +CUTE_HOST_DEVICE constexpr +C<0> +operator&(U, C) { + return {}; +} + +template ::value && !bool(t))> +CUTE_HOST_DEVICE constexpr +C +operator&&(C, U) { + return {}; +} + +template ::value && !bool(t))> +CUTE_HOST_DEVICE constexpr +C +operator&&(U, C) { + return {}; +} + +template ::value && bool(t))> +CUTE_HOST_DEVICE constexpr +C +operator||(C, U) { + return {}; +} + +template ::value && bool(t))> +CUTE_HOST_DEVICE constexpr +C +operator||(U, C) { + return {}; +} + +// +// Named functions from math.hpp +// + +#define CUTE_NAMED_UNARY_FN(OP) \ + template \ + CUTE_HOST_DEVICE constexpr \ + C OP (C) { \ + return {}; \ + } +#define CUTE_NAMED_BINARY_FN(OP) \ + template \ + CUTE_HOST_DEVICE constexpr \ + C OP (C, C) { \ + return {}; \ + } \ + template ::value)> \ + CUTE_HOST_DEVICE constexpr \ + auto OP (C, U u) { \ + return OP(t,u); \ + } \ + template ::value)> \ + CUTE_HOST_DEVICE constexpr \ + auto OP (T t, C) { \ + return OP(t,u); \ + } + +CUTE_NAMED_UNARY_FN(abs); +CUTE_NAMED_UNARY_FN(signum); +CUTE_NAMED_UNARY_FN(has_single_bit); + +CUTE_NAMED_BINARY_FN(max); +CUTE_NAMED_BINARY_FN(min); +CUTE_NAMED_BINARY_FN(shiftl); +CUTE_NAMED_BINARY_FN(shiftr); +CUTE_NAMED_BINARY_FN(gcd); +CUTE_NAMED_BINARY_FN(lcm); + +#undef CUTE_NAMED_UNARY_FN +#undef CUTE_NAMED_BINARY_FN + +// +// Other functions +// + +template +CUTE_HOST_DEVICE constexpr +C +safe_div(C, C) { + static_assert(t % u == 0, "Static safe_div requires t % u == 0"); + return {}; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +safe_div(C, U u) { + return t / u; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +safe_div(T t, C) { + return t / u; +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +conditional_return(true_type, TrueType&& t, FalseType&&) { + return static_cast(t); +} + +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +conditional_return(false_type, TrueType&&, FalseType&& f) { + return static_cast(f); +} + +// TrueType and FalseType must have a common type +template +CUTE_HOST_DEVICE constexpr +auto +conditional_return(bool b, TrueType const& t, FalseType const& f) { + return b ? t : f; +} + +// TrueType and FalseType don't require a common type +template +CUTE_HOST_DEVICE constexpr +auto +conditional_return(TrueType const& t, FalseType const& f) { + if constexpr (b) { + return t; + } else { + return f; + } +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(C) { + printf("_"); + ::cute::print(Value); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, C const&) { + return os << "_" << t; +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_ratio_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_ratio_hip.hpp new file mode 100644 index 000000000..7bb651dc8 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_ratio_hip.hpp @@ -0,0 +1,181 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include + +namespace cute +{ + +/** Compile-time rational arithmetic type. + * Like cute::C for std::integral_constant, cute::R for std::ratio has a short name + * for error messages and compile times. + * The static data members @a num and @a den represent the reduced numerator and denominator + * of the rational value. Thus, two cute::R types with different @a n or @a d are distinct types + * even if they represent the same rational value. A cute::R exposes the reduced canonical type + * via its type member. That is, cute::R<3,6>::type is cute::R<1,2> and cute::R<6,3>::type is cute::C<2> + */ +template +class R { + static_assert(d != 0); + static constexpr auto an = abs(n); + static constexpr auto ad = abs(d); + static constexpr auto g = gcd(an, ad); + + public: + static constexpr auto num = signum(n) * signum(d) * an / g; + static constexpr auto den = ad / g; + // RI: den >= 1 && gcd(abs(num),den) == 1 + using type = typename conditional, R>::type; +}; + +template +CUTE_HOST_DEVICE constexpr +typename R::type +ratio(C, C) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator*(R, R) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator*(R, C) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator*(C, R) { + return {}; +} + +// Product with dynamic type needs to produce an integer... +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +operator*(C const& c, R) { + return c * R::num / R::den; +} + +// Product with dynamic type needs to produce an integer... +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +operator*(R, C const& c) { + return c * R::num / R::den; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator+(R, R) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator+(R, C) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +operator+(C, R) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +bool_constant::num == R::num && R::den == R::den> +operator==(R, R) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +bool_constant::num == c && R::den == 1> +operator==(R, C) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +bool_constant::num == c && R::den == 1> +operator==(C, R) { + return {}; +} + +template +CUTE_HOST_DEVICE constexpr +typename R::type +abs(R) { + return {}; +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(R) { + print(C{}); print("/"); print(C{}); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, R) { + return os << "_" << C{} << "/" << C{}; +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/math_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/math_hip.hpp new file mode 100644 index 000000000..f79f0b44b --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/math_hip.hpp @@ -0,0 +1,317 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +// +// Common Operations +// + +template ::value && + is_arithmetic::value)> +CUTE_HOST_DEVICE constexpr +auto +max(T const& t, U const& u) { + return t < u ? u : t; +} + +template ::value && + is_arithmetic::value)> +CUTE_HOST_DEVICE constexpr +auto +min(T const& t, U const& u) { + return t < u ? t : u; +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +abs(T const& t) { + if constexpr (is_signed::value) { + return t < T(0) ? -t : t; + } else { + return t; + } + + CUTE_GCC_UNREACHABLE; +} + +// Returns 1 if x > 0, -1 if x < 0, and 0 if x is zero. +template ::value)> +CUTE_HOST_DEVICE constexpr +int +signum(T const& x) { + if constexpr (is_signed::value) { + return (T(0) < x) - (x < T(0)); + } else { + return T(0) < x; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// C++17 operations +// + +// Greatest common divisor of two positive integers +template ::value && + is_std_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +gcd(T t, U u) { + while (true) { + if (t == 0) { return u; } + u %= t; + if (u == 0) { return t; } + t %= u; + } +} + +// Least common multiple of two positive integers +template ::value && + is_std_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +lcm(T const& t, U const& u) { + return (t / gcd(t,u)) * u; +} + +// +// C++20 operations +// + +// Checks if a number is an integral power of two +template +CUTE_HOST_DEVICE constexpr +bool +has_single_bit(T x) { + return x != 0 && (x & (x - 1)) == 0; +} + +// Smallest number of bits needed to represent the given value +// bit_width( 0b0000 ) = 0 +// bit_width( 0b0001 ) = 1 +// bit_width( 0b0010 ) = 2 +// bit_width( 0b0011 ) = 2 +// bit_width( 0b0100 ) = 3 +// bit_width( 0b0101 ) = 3 +// bit_width( 0b0110 ) = 3 +// bit_width( 0b0111 ) = 3 +template +CUTE_HOST_DEVICE constexpr +T +bit_width(T x) { + static_assert(is_unsigned::value, "Only to be used for unsigned types."); + constexpr int N = (numeric_limits::digits == 64 ? 6 : + (numeric_limits::digits == 32 ? 5 : + (numeric_limits::digits == 16 ? 4 : + (numeric_limits::digits == 8 ? 3 : (assert(false),0))))); + T r = 0; + for (int i = N - 1; i >= 0; --i) { + T shift = (x > ((T(1) << (T(1) << i))-1)) << i; + x >>= shift; + r |= shift; + } + return r + (x != 0); +} + +// Smallest integral power of two not less than the given value +// bit_ceil( 0b00000000 ) = 0b00000001 +// bit_ceil( 0b00000001 ) = 0b00000001 +// bit_ceil( 0b00000010 ) = 0b00000010 +// bit_ceil( 0b00000011 ) = 0b00000100 +// bit_ceil( 0b00000100 ) = 0b00000100 +// bit_ceil( 0b00000101 ) = 0b00001000 +// bit_ceil( 0b00000110 ) = 0b00001000 +// bit_ceil( 0b00000111 ) = 0b00001000 +// bit_ceil( 0b00001000 ) = 0b00001000 +// bit_ceil( 0b00001001 ) = 0b00010000 +template +CUTE_HOST_DEVICE constexpr +T +bit_ceil(T x) { + return x == 0 ? T(1) : (T(1) << bit_width(x - 1)); +} + +// Largest integral power of two not greater than the given value +// bit_floor( 0b00000000 ) = 0b00000000 +// bit_floor( 0b00000001 ) = 0b00000001 +// bit_floor( 0b00000010 ) = 0b00000010 +// bit_floor( 0b00000011 ) = 0b00000010 +// bit_floor( 0b00000100 ) = 0b00000100 +// bit_floor( 0b00000101 ) = 0b00000100 +// bit_floor( 0b00000110 ) = 0b00000100 +// bit_floor( 0b00000111 ) = 0b00000100 +// bit_floor( 0b00001000 ) = 0b00001000 +// bit_floor( 0b00001001 ) = 0b00001000 +template +CUTE_HOST_DEVICE constexpr +T +bit_floor(T x) { + return x == 0 ? 0 : (T(1) << (bit_width(x) - 1)); +} + +template +CUTE_HOST_DEVICE constexpr T rotl(T x, int s); +template +CUTE_HOST_DEVICE constexpr T rotr(T x, int s); + +// Computes the result of circular bitwise left-rotation +template +CUTE_HOST_DEVICE constexpr +T +rotl(T x, int s) { + constexpr int N = numeric_limits::digits; + return s == 0 ? x : s > 0 ? (x << s) | (x >> (N - s)) : rotr(x, -s); +} + +// Computes the result of circular bitwise right-rotation +template +CUTE_HOST_DEVICE constexpr +T +rotr(T x, int s) { + constexpr int N = numeric_limits::digits; + return s == 0 ? x : s > 0 ? (x >> s) | (x << (N - s)) : rotl(x, -s); +} + +// Counts the number of consecutive 0 bits, starting from the most significant bit +// countl_zero( 0b00000000 ) = 8 +// countl_zero( 0b11111111 ) = 0 +// countl_zero( 0b00011100 ) = 3 +template +CUTE_HOST_DEVICE constexpr +T +countl_zero(T x) { + return numeric_limits::digits - bit_width(x); +} + +// Counts the number of consecutive 1 bits, starting from the most significant bit +// countl_one( 0b00000000 ) = 0 +// countl_one( 0b11111111 ) = 8 +// countl_one( 0b11100011 ) = 3 +template +CUTE_HOST_DEVICE constexpr +T +countl_one(T x) { + return countl_zero(~x); +} + +// Counts the number of consecutive 0 bits, starting from the least significant bit +// countr_zero( 0b00000000 ) = 8 +// countr_zero( 0b11111111 ) = 0 +// countr_zero( 0b00011100 ) = 2 +template +CUTE_HOST_DEVICE constexpr +T +countr_zero(T x) { + return x == 0 ? numeric_limits::digits : bit_width(T(x & T(-x))) - 1; // bit_width of the LSB +} + +// Counts the number of consecutive 1 bits, starting from the least significant bit +// countr_one( 0b00000000 ) = 0 +// countr_one( 0b11111111 ) = 8 +// countr_one( 0b11100011 ) = 2 +template +CUTE_HOST_DEVICE constexpr +T +countr_one(T x) { + return countr_zero(~x); +} + +// Counts the number of 1 bits in an unsigned integer +// popcount( 0b00000000 ) = 0 +// popcount( 0b11111111 ) = 8 +// popcount( 0b00011101 ) = 4 +template +CUTE_HOST_DEVICE constexpr +int +popcount(T x) { + int c = 0; + while (x) { + ++c; + x &= x - 1; // clear the least significant bit set + } + return c; +} + +// +// Custom operations +// + +// Computes the result of bitwise left-shift +template +CUTE_HOST_DEVICE constexpr +T +shiftl(T x, int s) { + return s >= 0 ? (x << s) : (x >> -s); +} + +// Computes the result of bitwise right-shift +template +CUTE_HOST_DEVICE constexpr +T +shiftr(T x, int s) { + return s >= 0 ? (x >> s) : (x << -s); +} + +// Safe divide +// @pre t % u == 0 +// @result t / u +template ::value && + is_std_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +safe_div(T const& t, U const& u) { + //assert(t % u == 0); + return t / u; +} + +} // namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/real_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/real_hip.hpp new file mode 100644 index 000000000..8bb88aab0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/real_hip.hpp @@ -0,0 +1,62 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +namespace cute +{ + +/// Generic fused multiply-add +template +CUTE_HOST_DEVICE constexpr +void +fma(D& d, A const& a, B const& b, C const& c) +{ + d = a * b + c; +} + +/// Fused multiply-add for triplets +template +CUTE_HOST_DEVICE constexpr +void +fma(A const& a, B const& b, C& c) +{ + return fma(c, a, b, c); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/tfloat_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/tfloat_hip.hpp new file mode 100644 index 000000000..b1cf97147 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/tfloat_hip.hpp @@ -0,0 +1,59 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace cute { + +using cutlass::tfloat32_t; + +// +// Display utilities +// + +#if !defined(__CUDACC_RTC__) +CUTE_HOST std::ostream& operator<<(std::ostream& os, tfloat32_t const& v) +{ + return os << float(v); +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/uint128_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/uint128_hip.hpp new file mode 100644 index 000000000..bab6f78b6 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/numeric/uint128_hip.hpp @@ -0,0 +1,265 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#include +#include +#include +#include +#endif + +#include + +/// Optionally enable GCC's built-in type +#if defined(__x86_64) && !defined(__HIP_DEVICE_COMPILE__) +# if defined(__GNUC__) && 0 +# define CUTE_UINT128_NATIVE +# elif defined(_MSC_VER) +# define CUTE_INT128_ARITHMETIC +# include +# endif +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cute { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +///! Unsigned 128b integer type +struct alignas(16) uint128_t +{ + /// Size of one part of the uint's storage in bits + static constexpr int storage_bits_ = 64; + + struct hilo + { + uint64_t lo; + uint64_t hi; + }; + + // Use a union to store either low and high parts or, if present, a built-in 128b integer type. + union + { + struct hilo hilo_; + +#if defined(CUTE_UINT128_NATIVE) + unsigned __int128 native; +#endif // defined(CUTE_UINT128_NATIVE) + }; + + // + // Methods + // + + /// Default ctor + CUTE_HOST_DEVICE constexpr + uint128_t() : hilo_{0, 0} {} + + /// Constructor from uint64 + CUTE_HOST_DEVICE constexpr + uint128_t(uint64_t lo_) : hilo_{lo_, 0} {} + + /// Constructor from two 64b unsigned integers + CUTE_HOST_DEVICE constexpr + uint128_t(uint64_t lo_, uint64_t hi_) : hilo_{lo_, hi_} {} + + /// Optional constructor from native value +#if defined(CUTE_UINT128_NATIVE) + uint128_t(unsigned __int128 value) : native(value) { } +#endif + + /// Lossily cast to uint64 + CUTE_HOST_DEVICE constexpr + explicit operator uint64_t() const + { + return hilo_.lo; + } + + template + CUTE_HOST_DEVICE constexpr + static void exception() + { + //static_assert(sizeof(Dummy) == 0, "Not implemented exception!"); + //abort(); + //printf("uint128 not implemented!\n"); + } + + /// Add + CUTE_HOST_DEVICE constexpr + uint128_t operator+(uint128_t const& rhs) const + { + uint128_t y; +#if defined(CUTE_UINT128_NATIVE) + y.native = native + rhs.native; +#else + y.hilo_.lo = hilo_.lo + rhs.hilo_.lo; + y.hilo_.hi = hilo_.hi + rhs.hilo_.hi + (y.hilo_.lo < hilo_.lo); +#endif + return y; + } + + /// Subtract + CUTE_HOST_DEVICE constexpr + uint128_t operator-(uint128_t const& rhs) const + { + uint128_t y; +#if defined(CUTE_UINT128_NATIVE) + y.native = native - rhs.native; +#else + y.hilo_.lo = hilo_.lo - rhs.hilo_.lo; + y.hilo_.hi = hilo_.hi - rhs.hilo_.hi - (rhs.hilo_.lo && y.hilo_.lo > hilo_.lo); +#endif + return y; + } + + /// Multiply by unsigned 64b integer yielding 128b integer + CUTE_HOST_DEVICE constexpr + uint128_t operator*(uint64_t const& rhs) const + { + uint128_t y; +#if defined(CUTE_UINT128_NATIVE) + y.native = native * rhs; +#elif defined(CUTE_INT128_ARITHMETIC) + // Multiply by the low part + y.hilo_.lo = _umul128(hilo_.lo, rhs, &y.hilo_.hi); + + // Add the high part and ignore the overflow + uint64_t overflow; + y.hilo_.hi += _umul128(hilo_.hi, rhs, &overflow); +#else + exception(); +#endif + return y; + } + + /// Divide 128b operation by 64b operation yielding a 64b quotient + CUTE_HOST_DEVICE constexpr + uint64_t operator/(uint64_t const& divisor) const + { + uint64_t quotient = 0; +#if defined(CUTE_UINT128_NATIVE) + quotient = uint64_t(native / divisor); +#elif defined(CUTE_INT128_ARITHMETIC) + // implemented using MSVC's arithmetic intrinsics + uint64_t remainder = 0; + quotient = _udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + exception(); +#endif + return quotient; + } + + /// Divide 128b operation by 64b operation yielding a 64b quotient + CUTE_HOST_DEVICE constexpr + uint64_t operator%(uint64_t const& divisor) const + { + uint64_t remainder = 0; +#if defined(CUTE_UINT128_NATIVE) + remainder = uint64_t(native % divisor); +#elif defined(CUTE_INT128_ARITHMETIC) + // implemented using MSVC's arithmetic intrinsics + (void)_udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + exception(); +#endif + return remainder; + } + + /// Computes the quotient and remainder in a single method. + CUTE_HOST_DEVICE constexpr + uint64_t divmod(uint64_t &remainder, uint64_t divisor) const + { + uint64_t quotient = 0; +#if defined(CUTE_UINT128_NATIVE) + quotient = uint64_t(native / divisor); + remainder = uint64_t(native % divisor); +#elif defined(CUTE_INT128_ARITHMETIC) + // implemented using MSVC's arithmetic intrinsics + quotient = _udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + exception(); +#endif + return quotient; + } + + /// Left-shifts a 128b unsigned integer + CUTE_HOST_DEVICE constexpr + uint128_t operator<<(int sh) const + { + if (sh == 0) { + return *this; + } + else if (sh >= storage_bits_) { + return uint128_t(0, hilo_.lo << (sh - storage_bits_)); + } + else { + return uint128_t( + (hilo_.lo << sh), + (hilo_.hi << sh) | uint64_t(hilo_.lo >> (storage_bits_ - sh)) + ); + } + } + + /// Right-shifts a 128b unsigned integer + CUTE_HOST_DEVICE constexpr + uint128_t operator>>(int sh) const + { + if (sh == 0) { + return *this; + } + else if (sh >= storage_bits_) { + return uint128_t((hilo_.hi >> (sh - storage_bits_)), 0); + } + else { + return uint128_t( + (hilo_.lo >> sh) | (hilo_.hi << (storage_bits_ - sh)), + (hilo_.hi >> sh) + ); + } + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cute + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/pointer_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/pointer_hip.hpp new file mode 100644 index 000000000..4c3cc9763 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/pointer_hip.hpp @@ -0,0 +1,399 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include + +namespace cute +{ + +// +// has_dereference to determine if a type is a pointer concept +// + +template +struct has_dereference : false_type { +}; + +template +struct has_dereference())>> : true_type { +}; + +template +CUTE_HOST_DEVICE constexpr +T* +raw_pointer_cast(T* ptr) { + return ptr; +} + +// +// Extract the physical type from a logical elem type. +// +template +struct get_raw_type +{ + using type = T; +}; + +template +using get_raw_type_t = typename get_raw_type::type; + + +// +// Pointer categories +// + +template +struct is_gmem : false_type {}; + +template +struct is_smem : false_type {}; + +// Anything that is not gmem or smem is rmem +template +struct is_rmem : bool_constant< not (is_gmem::value || is_smem::value)> {}; + +// +// A very simplified wrapper for pointers -- use for constructing tagged pointers +// +template +struct device_ptr +{ + using value_type = T; + + // static const uint32_t ElementsPerStoredItem = sizeof(T) * 8 / sizeof_bits_v; + + CUTE_HOST_DEVICE constexpr + device_ptr(T* ptr) : ptr_(ptr) {} + + CUTE_HOST_DEVICE constexpr + T* get() const { return ptr_; } + + CUTE_HOST_DEVICE constexpr + T& operator*() const { return *ptr_; } + + template + CUTE_HOST_DEVICE constexpr + T& operator[](Index const& i) const { + static_assert(sizeof_bits_v >= 8, "Use subbyte_iterator to access the element"); + return ptr_[i]; + } + + // template + // CUTE_HOST_DEVICE constexpr + // DerivedType operator+(Index const& i) const { return {ptr_ + i / ElementsPerStoredItem}; } + + template + CUTE_HOST_DEVICE constexpr + DerivedType operator+(Index const& i) const { return {ptr_ + i}; } + + CUTE_HOST_DEVICE constexpr friend + ptrdiff_t operator-(device_ptr const& a, + device_ptr const& b) { + return a.ptr_ - b.ptr_; + } + + T* ptr_; +}; + +template +CUTE_HOST_DEVICE constexpr +T* +raw_pointer_cast(device_ptr ptr) { + return ptr.get(); +} + +// +// gmem_ptr +// + +template +struct gmem_ptr : device_ptr> { + using device_ptr>::device_ptr; +}; + +template +CUTE_HOST_DEVICE constexpr +gmem_ptr +make_gmem_ptr(T* ptr) { + return {ptr}; +} + +template +CUTE_HOST_DEVICE constexpr +gmem_ptr +make_gmem_ptr(void* ptr) { + return {reinterpret_cast(ptr)}; +} + +template +CUTE_HOST_DEVICE constexpr +gmem_ptr +make_gmem_ptr(void const* ptr) { + return {reinterpret_cast(ptr)}; +} + +// nullptr_t overloads are needed because otherwise, +// make_gmem_ptr(nullptr) will be ambiguous, +// as std::nullptr_t can be converted to any pointer +// or pointer to member type. +template +CUTE_HOST_DEVICE constexpr +gmem_ptr +make_gmem_ptr(decltype(nullptr)) { // nullptr_t + return {static_cast(nullptr)}; +} + +template +struct is_gmem> : true_type {}; + +// +// smem_ptr +// + +template +struct smem_ptr : device_ptr> { + using device_ptr>::device_ptr; +}; + +template +CUTE_HOST_DEVICE constexpr +smem_ptr +make_smem_ptr(T* ptr) { + return {ptr}; +} + +template +CUTE_HOST_DEVICE constexpr +smem_ptr +make_smem_ptr(void* ptr) { + return {reinterpret_cast(ptr)}; +} + +template +CUTE_HOST_DEVICE constexpr +smem_ptr +make_smem_ptr(void const* ptr) { + return {reinterpret_cast(ptr)}; +} + +template +struct is_smem> : true_type {}; + +// +// rmem_ptr +// + +template +struct rmem_ptr : device_ptr> { + using device_ptr>::device_ptr; +}; + +template +CUTE_HOST_DEVICE constexpr +rmem_ptr +make_rmem_ptr(T* ptr) { + return {ptr}; +} + +template +CUTE_HOST_DEVICE constexpr +rmem_ptr +make_rmem_ptr(void* ptr) { + return {reinterpret_cast(ptr)}; +} + +template +CUTE_HOST_DEVICE constexpr +rmem_ptr +make_rmem_ptr(void const* ptr) { + return {reinterpret_cast(ptr)}; +} + +template +struct is_rmem> : true_type {}; + +// +// counting iterator -- quick and dirty +// + +struct counting +{ + using index_type = int; + using value_type = index_type; + + CUTE_HOST_DEVICE constexpr + counting() : n_(0) {} + CUTE_HOST_DEVICE constexpr + counting(index_type const& n) : n_(n) {} + + CUTE_HOST_DEVICE constexpr + index_type operator[](index_type const& i) const { return n_ + i; } + + CUTE_HOST_DEVICE constexpr + index_type const& operator*() const { return n_; } + + CUTE_HOST_DEVICE constexpr + counting operator+(index_type const& i) const { return {n_ + i}; } + CUTE_HOST_DEVICE constexpr + counting& operator++() { ++n_; return *this; } + + CUTE_HOST_DEVICE constexpr + bool operator==(counting const& other) const { return n_ == other.n_; } + CUTE_HOST_DEVICE constexpr + bool operator!=(counting const& other) const { return n_ != other.n_; } + + CUTE_HOST_DEVICE constexpr + bool operator< (counting const& other) const { return n_ < other.n_; } + + index_type n_; +}; + +// +// recast +// + +template +CUTE_HOST_DEVICE constexpr +auto +recast(T* ptr) { + return reinterpret_cast(ptr); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(T const* ptr) { + return reinterpret_cast(ptr); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(gmem_ptr const& ptr) { + return make_gmem_ptr(recast(ptr.ptr_)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(gmem_ptr const& ptr) { + return make_gmem_ptr(recast(ptr.ptr_)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(smem_ptr const& ptr) { + return make_smem_ptr(recast(ptr.ptr_)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(smem_ptr const& ptr) { + return make_smem_ptr(recast(ptr.ptr_)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(rmem_ptr const& ptr) { + return make_rmem_ptr(recast(ptr.ptr_)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(rmem_ptr const& ptr) { + return make_rmem_ptr(recast(ptr.ptr_)); +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(T const* const ptr) +{ + printf("raw_ptr_%db(%p)", int(sizeof_bits::value), ptr); +} + +template +CUTE_HOST_DEVICE void print(gmem_ptr const& ptr) +{ + printf("gmem_ptr_%db(%p)", int(sizeof_bits::value), ptr.get()); +} + +template +CUTE_HOST_DEVICE void print(smem_ptr const& ptr) +{ + printf("smem_ptr_%db(%p)", int(sizeof_bits::value), ptr.get()); +} + +template +CUTE_HOST_DEVICE void print(rmem_ptr const& ptr) +{ + printf("rmem_ptr_%db(%p)", int(sizeof_bits::value), ptr.get()); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, gmem_ptr const& ptr) +{ + return os << "gmem_ptr_" << int(sizeof_bits::value) << "b"; +} + +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, smem_ptr const& ptr) +{ + return os << "smem_ptr_" << int(sizeof_bits::value) << "b"; +} + +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, rmem_ptr const& ptr) +{ + return os << "rmem_ptr_" << int(sizeof_bits::value) << "b"; +} + +#endif // !defined(__CUDACC_RTC__) + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/stride_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/stride_hip.hpp new file mode 100644 index 000000000..db9b68a4a --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/stride_hip.hpp @@ -0,0 +1,458 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +/** crd2idx maps a coordinate within to an index + * This is computed as follows: + * [coord, shape, and stride are all integers => step forward by stride] + * op(c, s, d) => c * d + * [coord is integer, shape and stride are tuple => divmod coord for each mode] + * op(c, (s,S), (d,D)) => op(c % prod(s), s, d) + op(c / prod(s), (S), (D)) + * [coord, shape, and stride are all tuples => consider each mode independently] + * op((c,C), (s,S), (d,D)) => op(c, s, d) + op((C), (S), (D)) + */ + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx(Coord const& coord, + Shape const& shape, + Stride const& stride); + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx_ttt(Coord const& coord, + Shape const& shape, + Stride const& stride, seq) +{ + return (... + crd2idx(get(coord), get(shape), get(stride))); +} + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx_itt(CInt const& coord, + STuple const& shape, + DTuple const& stride, seq) +{ + if constexpr (sizeof...(Is) == 0) { // Avoid recursion and mod on single/last iter + return crd2idx(coord, get(shape), get(stride)); + } else if constexpr (is_constant<0, CInt>::value) { + return crd2idx(_0{}, get(shape), get(stride)) + + (_0{} + ... + crd2idx(_0{}, get(shape), get(stride))); + } else { // General case + return crd2idx(coord % product(get(shape)), get(shape), get(stride)) + + crd2idx_itt(coord / product(get(shape)), shape, stride, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx(Coord const& coord, + Shape const& shape, + Stride const& stride) +{ + if constexpr (is_tuple::value) { + if constexpr (is_tuple::value) { // tuple tuple tuple + static_assert(tuple_size::value == tuple_size< Shape>::value, "Mismatched Ranks"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return detail::crd2idx_ttt(coord, shape, stride, tuple_seq{}); + } else { // tuple "int" "int" + static_assert(sizeof(Coord) == 0, "Invalid parameters"); + } + } else { + if constexpr (is_tuple::value) { // "int" tuple tuple + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return detail::crd2idx_itt(coord, shape, stride, tuple_seq{}); + } else { // "int" "int" "int" + return coord * stride; + } + } + + CUTE_GCC_UNREACHABLE; +} + +// +// If we know Stride is default [CompactColMajor], then we can take shortcuts +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx_horner(CTuple const& coord, + STuple const& shape, seq) +{ + if constexpr (sizeof...(Is) == 0) { // No recursion on single/last iter + return get(coord); + } else { // General case + return get(coord) + get(shape) * crd2idx_horner(coord, shape, seq{}); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +crd2idx(Coord const& coord, + Shape const& shape) +{ + static_assert(decltype(congruent(coord,shape))::value, "Mismatched Ranks"); + if constexpr (is_tuple::value) { + // Flatten and apply Horner's method + auto flat_coord = flatten(coord); + auto flat_shape = flatten(shape); + return detail::crd2idx_horner(flat_coord, flat_shape, tuple_seq{}); + } else { + return coord; + } + + CUTE_GCC_UNREACHABLE; +} + +/** idx2crd splits an index to a coordinate within . + * + * This is computed as follows: + * [index, shape, and stride are all integers => determine 1D coord] + * op(i, s, d) => (i / d) % s + * [index is integer, shape and stride are tuple => determine component for each mode] + * op(i, (s,S), (d,D)) => (op(i, s, d), op(i, S, D)...) + * [index, shape, and stride are all tuples => consider each mode independently] + * op((i,I), (s,S), (d,D)) => (op(i, s, d), op((I), (S), (D))) + * + * NOTE: This only works for compact shape+stride layouts. A more general version would + * apply to all surjective layouts + */ + +template +CUTE_HOST_DEVICE constexpr +auto +idx2crd(Index const& idx, + Shape const& shape, + Stride const& stride) +{ + if constexpr (is_tuple::value) { + if constexpr (is_tuple::value) { // tuple tuple tuple + static_assert(tuple_size::value == tuple_size< Shape>::value, "Mismatched Ranks"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return transform(idx, shape, stride, [](auto const& i, auto const& s, auto const& d){ return idx2crd(i,s,d); }); + } else { // tuple "int" "int" + static_assert(sizeof(Index) == 0, "Invalid parameters"); + } + } else { + if constexpr (is_tuple::value) { + if constexpr (is_tuple::value) { // "int" tuple tuple + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return transform(shape, stride, [&](auto const& s, auto const& d){ return idx2crd(idx,s,d); }); + } else { // "int" tuple "int" + return transform(shape, compact_col_major(shape, stride), [&](auto const& s, auto const& d){ return idx2crd(idx,s,d); }); + } + } else { // "int" "int" "int" + if constexpr (is_constant<1, Shape>::value) { + // Skip potential stride-0 division + return Int<0>{}; + } else { + return (idx / stride) % shape; + } + } + } + + CUTE_GCC_UNREACHABLE; +} + +// +// If we know Stride is default [CompactColMajor], then we can take shortcuts +// + +//(idx / 1) % s0 +//(idx / s0) % s1 +//(idx / (s0 * s1)) % s2 +//... + +template +CUTE_HOST_DEVICE constexpr +auto +idx2crd(Index const& idx, + Shape const& shape) +{ + if constexpr (is_tuple::value) { + if constexpr (is_tuple::value) { // tuple tuple + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return transform(idx, shape, [](auto const& i, auto const& s) { return idx2crd(i,s); }); + } else { // tuple "int" + static_assert(sizeof(Index) == 0, "Invalid parameters"); + } + } else { + if constexpr (is_tuple::value) { // "int" tuple + return idx2crd(idx, shape, compact_col_major(shape)); + } else { // "int" "int" + return idx; + } + } + + CUTE_GCC_UNREACHABLE; +} + +// +// crd2crd +// + +template +CUTE_HOST_DEVICE constexpr +auto +crd2crd(Coord const& coord, + SShape const& src_shape, + DShape const& dst_shape) +{ + if constexpr (is_tuple::value && is_tuple::value && is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return transform(coord, src_shape, dst_shape, [](auto const& c, auto const& s, auto const& d) { return crd2crd(c,s,d); }); + } else { + // assert(size(src_shape) == size(dst_shape)) + return idx2crd(crd2idx(coord, src_shape), dst_shape); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Compact Major +// + +// Tags for common layouts and dispatching +struct LayoutLeft; // Col-major layout mapping; leftmost extent has stride 1 +using GenColMajor = LayoutLeft; // Alias + +struct LayoutRight; // Row-major layout mapping; rightmost extent has stride 1 +using GenRowMajor = LayoutRight; // Alias + +namespace detail { + +// For GCC8.5 -- Use of lambdas in unevaluated contexts. Instead use function objects. +template +struct CompactLambda; + +// @pre is_integral +// Return (result, current * product(shape)) to enable recurrence +template +CUTE_HOST_DEVICE constexpr +auto +compact(Shape const& shape, + Current const& current) +{ + if constexpr (is_tuple::value) { // Shape::tuple Current::int + using Lambda = CompactLambda; // Append or Prepend + using Seq = typename Lambda::template seq; // Seq or RSeq + return cute::detail::fold(shape, cute::make_tuple(cute::make_tuple(), current), Lambda{}, Seq{}); + } else { // Shape::int Current::int + if constexpr (is_constant<1, Shape>::value) { + return cute::make_tuple(Int<0>{}, current); // If current is dynamic, this could save a reg + } else { + return cute::make_tuple(current, current * shape); + } + } + + CUTE_GCC_UNREACHABLE; +} + +// For GCC8.5 -- Specialization LayoutLeft +template <> +struct CompactLambda +{ + template + CUTE_HOST_DEVICE constexpr auto + operator()(Init const& init, Shape const& si) { + auto result = detail::compact(si, get<1>(init)); + return cute::make_tuple(append(get<0>(init), get<0>(result)), get<1>(result)); // Append + } + + template + using seq = tuple_seq; // Seq +}; + +// For GCC8.5 -- Specialization LayoutRight +template <> +struct CompactLambda +{ + template + CUTE_HOST_DEVICE constexpr auto + operator()(Init const& init, Shape const& si) { + auto result = detail::compact(si, get<1>(init)); + return cute::make_tuple(prepend(get<0>(init), get<0>(result)), get<1>(result)); // Prepend + } + + template + using seq = tuple_rseq; // RSeq +}; + +} // end namespace detail + +template , + __CUTE_REQUIRES(is_tuple::value || is_integral::value)> +CUTE_HOST_DEVICE constexpr +auto +compact_major(Shape const& shape, + Current const& current = {}) +{ + if constexpr (is_tuple::value) { // Shape::tuple Current::tuple + static_assert(is_tuple::value, "Invalid parameters"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + // Recurse to apply to the terminals of current + return transform(shape, current, [&](auto const& s, auto const& c){ return compact_major(s,c); }); + } else { + return get<0>(detail::compact(shape, current)); + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Compact Col Major +// + +struct LayoutLeft { + template + using Apply = decltype(compact_major(declval())); +}; + +template > +CUTE_HOST_DEVICE constexpr +auto +compact_col_major(Shape const& shape, + Current const& current = {}) +{ + return compact_major(shape, current); +} + +// +// Compact Row Major +// + +struct LayoutRight { + template + using Apply = decltype(compact_major(declval())); +}; + +template > +CUTE_HOST_DEVICE constexpr +auto +compact_row_major(Shape const& shape, + Current const& current = {}) +{ + return compact_major(shape, current); +} + +// +// Compact Order -- compute a compact stride based on an ordering of the modes +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +compact_order(Shape const& shape, Order const& order, + OrigShape const& orig_shape, OrigOrder const& orig_order) +{ + if constexpr (is_tuple::value) { + return transform(shape, order, [&](auto const& x, auto const& y) { return compact_order(x, y, orig_shape, orig_order); }); + } else { + auto d = product(transform(orig_shape, orig_order, + [&](auto const& s, auto const& o) { + return conditional_return(o < order, product(s), Int<1>{}); + })); + return compact_col_major(shape, d); + } + + CUTE_GCC_UNREACHABLE; +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +compact_order(Shape const& shape, Order const& order) +{ + if constexpr(is_congruent::value) { + return detail::compact_order(shape, order, flatten_to_tuple(shape), flatten_to_tuple(order)); + } + else + { + // Here we only want to apply order to top-level subshapes and default (col-major) order on other levels + static_assert(rank(Shape{}) == rank(Order{}), "Need equal rank of shape and order"); + return detail::compact_order(shape, order, shape, order); + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +compact_order(Shape const& shape, GenColMajor const& major) +{ + return compact_major(shape); +} + +template +CUTE_HOST_DEVICE constexpr +auto +compact_order(Shape const& shape, GenRowMajor const& major) +{ + return compact_major(shape); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_hip.hpp new file mode 100644 index 000000000..400dcbbac --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_hip.hpp @@ -0,0 +1,559 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include +#include + +namespace cute +{ + +// A generic Swizzle functor +/* 0bxxxxxxxxxxxxxxxYYYxxxxxxxZZZxxxx + * ^--^ MBase is the number of least-sig bits to keep constant + * ^-^ ^-^ BBits is the number of bits in the mask + * ^---------^ SShift is the distance to shift the YYY mask + * (pos shifts YYY to the right, neg shifts YYY to the left) + * + * e.g. Given + * 0bxxxxxxxxxxxxxxxxYYxxxxxxxxxZZxxx + * the result is + * 0bxxxxxxxxxxxxxxxxYYxxxxxxxxxAAxxx where AA = ZZ xor YY + */ +template +struct Swizzle +{ + static constexpr int num_bits = BBits; + static constexpr int num_base = MBase; + static constexpr int num_shft = SShift; + + static_assert(num_base >= 0, "MBase must be positive."); + static_assert(num_bits >= 0, "BBits must be positive."); + static_assert(abs(num_shft) >= num_bits, "abs(SShift) must be more than BBits."); + + // using 'int' type here to avoid unintentially casting to unsigned... unsure. + using bit_msk = cute::constant; + using yyy_msk = cute::constant; + using zzz_msk = cute::constant; + using msk_sft = cute::constant; + + static constexpr uint32_t swizzle_code = uint32_t(yyy_msk{} | zzz_msk{}); + + template + CUTE_HOST_DEVICE constexpr static + auto + apply(Offset const& offset) + { + return offset ^ shiftr(offset & yyy_msk{}, msk_sft{}); // ZZZ ^= YYY + } + + template + CUTE_HOST_DEVICE constexpr + auto + operator()(Offset const& offset) const + { + return apply(offset); + } +}; + +// +// make_swizzle<0b1000, 0b0100>() -> Swizzle<1,2,1> +// make_swizzle<0b11000000, 0b00000110>() -> Swizzle<2,1,5> +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_swizzle() +{ + constexpr uint32_t BZ = popcount(Y); // Number of swizzle bits + constexpr uint32_t BY = popcount(Z); // Number of swizzle bits + static_assert(BZ == BY, "Number of bits in Y and Z don't match"); + constexpr uint32_t TZ_Y = countr_zero(Y); // Number of trailing zeros in Y + constexpr uint32_t TZ_Z = countr_zero(Z); // Number of trailing zeros in Z + constexpr uint32_t M = cute::min(TZ_Y, TZ_Z) % 32; + constexpr int32_t S = int32_t(TZ_Y) - int32_t(TZ_Z); // Difference in trailing zeros + static_assert((Y | Z) == Swizzle::swizzle_code, "Something went wrong."); + return Swizzle{}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Swizzle, Swizzle) +{ + static_assert(S0 == S1, "Can only merge swizzles of the same shift."); + constexpr uint32_t Y = Swizzle::yyy_msk::value ^ Swizzle::yyy_msk::value; + constexpr uint32_t Z = Swizzle::zzz_msk::value ^ Swizzle::zzz_msk::value; + return make_swizzle(); + + //return ComposedFn, Swizzle>{}; +} + +// +// Inverse +// + +template +CUTE_HOST_DEVICE constexpr +Swizzle +right_inverse(Swizzle const& sw) +{ + return sw; +} + +template +CUTE_HOST_DEVICE constexpr +Swizzle +left_inverse(Swizzle const& sw) +{ + return sw; +} + +// Kludge -- Probably want an OffsetFn here instead +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +right_inverse(T const& t) +{ + return -t; +} + +// Kludge -- Probably want an OffsetFn here instead +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +left_inverse(T const& t) +{ + return -t; +} + +// +// Upcast and Downcast +// + +template +CUTE_HOST_DEVICE constexpr +auto +upcast(Swizzle 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{}; + } else { + return Swizzle{}; + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(Swizzle 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{}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(Swizzle const& swizzle) +{ + if constexpr (sizeof_bits::value == sizeof_bits::value) { + return swizzle; + } else if constexpr (sizeof_bits::value > sizeof_bits::value) { + static_assert(sizeof_bits::value % sizeof_bits::value == 0, "NewType must be a multiple of OldType"); + return upcast::value/sizeof_bits::value>(swizzle); + } else if constexpr (sizeof_bits::value < sizeof_bits::value) { + static_assert(sizeof_bits::value % sizeof_bits::value == 0, "NewType must be a divisor of OldType"); + return downcast::value/sizeof_bits::value>(swizzle); + } +} + +// +// Utility for slicing and swizzle "offsets" +// + +// For swizzle functions, it is often needed to keep track of which bits are +// consumed and which bits are free. Furthermore, it is useful to know whether +// each of these bits is known statically or dynamically. + +// MixedBits is an 32-bit unsigned integer class where some bits are known statically +// and some bits are known dynamically. These sets of bits are disjoint and it is +// known statically which bits are known dynamically. + +// MixedBits can only be manipulated through bitwise operations + +// Abstract value: StaticInt | (dynamic_int_ & StaticFlags) +template // 0: static, 1: dynamic +struct MixedBits +{ + // Representation invariants + static_assert(StaticFlags != 0, "Should be at least one dynamic bit in MixedBits."); + static_assert((StaticInt & StaticFlags) == 0, "No static/dynamic overlap allowed in MixedBits."); + + uint32_t dynamic_int_; + // assert((dynamic_int_ & ~StaticFlags) == 0); + + CUTE_HOST_DEVICE constexpr operator uint32_t() const noexcept { return StaticInt | dynamic_int_; } +}; + +// Return a value representing (C{} | (d & C)) potentially using MixedBits to track s and f. +// This maker does allow ((s & f) != 0) and enforces the MixedBits invariant before creation. +template +CUTE_HOST_DEVICE constexpr +auto +make_mixed_bits(C, DynamicType const& d, C) +{ + static_assert(is_integral::value); + constexpr uint32_t new_f = uint32_t(f) & ~uint32_t(s); // StaticBits take precedence, M<0,f>{d} | C{} + if constexpr (new_f == 0 || is_static::value) { + return C{} | (d & C{}); // Just return a static int + } else { + return MixedBits{uint32_t(d) & new_f}; // MixedBits + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Operators +// + +// Equality +template +CUTE_HOST_DEVICE constexpr +auto +operator==(MixedBits const& m, C) +{ + return (S0 == (uint32_t(S1) & ~F0)) && (m.dynamic_int_ == (uint32_t(S1) & F0)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator==(C s, MixedBits const& m) +{ + return m == s; +} + +// Bitwise AND +template +CUTE_HOST_DEVICE constexpr +auto +operator&(MixedBits const& m0, MixedBits const& m1) +{ + // Truth table for (S0,D0,F0) & (S1,D1,F1) -> (S,D,F) + // S0D0F0 | 0X0 | 001 | 011 | 1X0 | + // S1D1F1 + // 0X0 | 0X0 | 0X0 | 0X0 | 0X0 | + // 001 | 0X0 | 001 | 001 | 001 | + // 011 | 0X0 | 001 | 011 | 011 | + // 1X0 | 0X0 | 001 | 011 | 1X0 | + + return make_mixed_bits(C{}, + //(S0 | m0.dynamic_int_) & (S1 | m1.dynamic_int_), + ((S1 & F0) & m0.dynamic_int_) | ((S0 & F1) & m1.dynamic_int_) | (m0.dynamic_int_ & m1.dynamic_int_), + C<(S1 & F0) | (S0 & F1) | (F0 & F1)>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator&(MixedBits const& m, C) +{ + return make_mixed_bits(C{}, + m.dynamic_int_, + C{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator&(C s, MixedBits const& m) +{ + return m & s; +} + +// Bitwise OR +template +CUTE_HOST_DEVICE constexpr +auto +operator|(MixedBits const& m0, MixedBits const& m1) +{ + // Truth table for (S0,D0,F0) | (S1,D1,F1) -> (S,D,F) + // S0D0F0 | 0X0 | 001 | 011 | 1X0 | + // S1D1F1 + // 0X0 | 0X0 | 001 | 011 | 1X0 | + // 001 | 001 | 001 | 011 | 1X0 | + // 011 | 011 | 011 | 011 | 1X0 | + // 1X0 | 1X0 | 1X0 | 1X0 | 1X0 | + + return make_mixed_bits(C{}, + ((~S1 & F0) & m0.dynamic_int_) | ((~S0 & F1) & m1.dynamic_int_), + C<(~S0 & F1) | (~S1 & F0)>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator|(MixedBits const& m, C) +{ + return make_mixed_bits(C{}, + m.dynamic_int_, + C{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator|(C s, MixedBits const& m) +{ + return m | s; +} + +// Bitwise XOR +template +CUTE_HOST_DEVICE constexpr +auto +operator^(MixedBits const& m0, MixedBits const& m1) +{ + // Truth table for (S0,D0,F0) ^ (S1,D1,F1) -> (S,D,F) + // S0D0F0 | 0X0 | 001 | 011 | 1X0 | + // S1D1F1 + // 0X0 | 0X0 | 001 | 011 | 1X0 | + // 001 | 001 | 001 | 011 | 011 | + // 011 | 011 | 011 | 001 | 001 | + // 1X0 | 1X0 | 011 | 001 | 0X0 | + + return make_mixed_bits(C<(~S0 & S1 & ~F0) | (S0 & ~S1 & ~F1)>{}, + (S0 | m0.dynamic_int_) ^ (S1 | m1.dynamic_int_), + C{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator^(MixedBits const& m, C) +{ + return make_mixed_bits(C<(~S0 & uint32_t(S1) & ~F0) | (S0 & ~uint32_t(S1))>{}, + (S0 | m.dynamic_int_) ^ uint32_t(S1), + C{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator^(C s, MixedBits const& m) +{ + return m ^ s; +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator<<(MixedBits const& m, C) +{ + return make_mixed_bits(C<(S0 << S1)>{}, + m.dynamic_int_ << S1, + C<(F0 << S1)>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +operator>>(MixedBits const& m, C) +{ + return make_mixed_bits(C<(S0 >> S1)>{}, + m.dynamic_int_ >> S1, + C<(F0 >> S1)>{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +shiftl(MixedBits const& m, C s) +{ + if constexpr (S1 >= 0) { + return m << s; + } else { + return m >> -s; + } +} + +template +CUTE_HOST_DEVICE constexpr +auto +shiftr(MixedBits const& m, C s) +{ + if constexpr (S1 >= 0) { + return m >> s; + } else { + return m << -s; + } +} + +// +// upcast and downcast +// + +template +CUTE_HOST_DEVICE constexpr +auto +safe_div(MixedBits const& m, C s) +{ + static_assert(has_single_bit(uint32_t(S1)), "Only divide MixedBits by powers of two."); + return make_mixed_bits(safe_div(C{}, s), + safe_div(m.dynamic_int_, s), + safe_div(C{}, s)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +upcast(MixedBits const& m) +{ + static_assert(has_single_bit(N), "Only divide MixedBits by powers of two."); + return safe_div(m, C{}); +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +upcast(T const& m) +{ + return safe_div(m, C{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(MixedBits const& m) +{ + static_assert(has_single_bit(N), "Only scale MixedBits by powers of two."); + return make_mixed_bits(C{}, + m.dynamic_int_ * N, + C{}); +} + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +downcast(T const& m) +{ + return m * C{}; +} + +// +// Convert a Pow2Layout+Coord to a MixedBits +// + +template +CUTE_HOST_DEVICE constexpr +auto +to_mixed_bits(Shape const& shape, Stride const& stride, Coord const& coord) +{ + if constexpr (is_tuple::value && is_tuple::value && is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched ranks"); + static_assert(tuple_size::value == tuple_size::value, "Mismatched ranks"); + return transform_apply(shape, stride, coord, [](auto const& s, auto const& d, auto const& c) { return to_mixed_bits(s,d,c); }, + [](auto const&... a) { return (a ^ ...); }); + } else if constexpr (is_integral::value && is_integral::value && is_integral::value) { + static_assert(decltype(shape*stride)::value == 0 || has_single_bit(decltype(shape*stride)::value), "Requires pow2 shape*stride."); + return make_mixed_bits(Int<0>{}, coord * stride, (shape - Int<1>{}) * stride); + } else { + static_assert(is_integral::value && is_integral::value && is_integral::value, "Either Shape, Stride, and Coord must be all tuples, or they must be all integral (in the sense of cute::is_integral)."); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +to_mixed_bits(Layout const& layout, Coord const& coord) +{ + return to_mixed_bits(layout.shape(), layout.stride(), idx2crd(coord, layout.shape())); +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(MixedBits const& m) +{ + printf("M_%u|(%u&%u)=%u", S, m.dynamic_int_, F, uint32_t(m)); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, MixedBits const& m) +{ + return os << "M_" << S << "|(" << m.dynamic_int_ << "&" << F << ")=" << uint32_t(m); +} + +template +CUTE_HOST_DEVICE void print(Swizzle const&) +{ + print("S<%d,%d,%d>", B, M, S); +} + +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, Swizzle const&) +{ + return os << "S<" << B << "," << M << "," << S << ">"; +} +#endif // !defined(__CUDACC_RTC__) + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_layout_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_layout_hip.hpp new file mode 100644 index 000000000..050fc0d12 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_layout_hip.hpp @@ -0,0 +1,454 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include + +#include + +/* Specialized functionality for a ComposedLayout of the form + * InvolutionFn o Offset o LayoutB + * where the InvolutionFn is a Swizzle and is not linear (hence the need for the Offset). + * + * Because these are specializations for core functions of ComposedLayout, these Swizzle Layouts + * provide similar functionality to Layout including tiling, partitioning, + * coordinate-to-index mapping and layout manipulations, but are not considered "normal" layouts. + * For example, these provide shape() and size() functions, but do not provide stride() functions. + * + * Furthermore, each of these specializations uses Swizzle<>-specific knowledge in its implementation and + * attempts to decay itself to a normal-layout with dynamic or static strides when certain slicing conditions + * are met. This is possible by determining the subdomain of the Swizzle<> function that is identity and + * testing if LayoutB's codomain is contained within it. In general, MizedBits is used as the Offset to track + * statically-vs-dynamically known bits in the Offset to improve the decay to static or dynamic normal layouts. + */ + +namespace cute +{ + +// +// Constructors +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_layout(Swizzle const& sxor) +{ + return composition(sxor, Layout,Int<1>>{}); +} + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +transfer_swizzle(Layout const& old_layout, + Layout const& new_layout) +{ + // Our goal is to determine a new swizzle for the strides in new_layout for consistent vectorizations + + // This is accomplished by identifying + // S o L :=: S? o L* + // We identify the "active" portion of S by computing (P o L)(c*) where P is a projection generated by S + // Then that active identifier is transformed through the layouts: + // L*(L[(P o L)(c*)]) + // which is a new swizzle identifier for S?, the new swizzle + + // Projections of the swizzle layout for composition, P + 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>{})); + + // Compose with the tile to get the swizzle projection, P o L [The Z and Y contributing portions of L] + auto layout_only_zy = composition(swizzle_only_zy, old_layout); + // Transform the end coordinate to get the active bits of the swizzle, (P o L)(c*) + auto swizzle_active_bits = layout_only_zy(size(layout_only_zy)-Int<1>{}); + + // Get the Z bit and the Y bits -- keep only those that are active in Z *and* Y + auto zzz_msk = typename Swizzle::zzz_msk{}; + auto yyy_msk = typename Swizzle::yyy_msk{}; + auto msk_sft = typename Swizzle::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; + + // Pass the identifiers through the old layout and new layout to make a new swizzle identifier, L*(L[(P o L)(c*)]) + 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)); + + // Use this new swizzle identifier to construct the new swizzle for new_layout + // (this also makes sure it's a "valid" swizzle that Swizzle can represent) + return composition(make_swizzle(), new_layout); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(ComposedLayout,Offset,Layout> const& layout) +{ + return detail::transfer_swizzle(layout.layout_b(), make_fragment_like(layout.layout_b())); +} + +// +// Utilities +// + +namespace detail { + +// Get just the Swizzle part of a composed layout. +template +CUTE_HOST_DEVICE constexpr +auto +get_swizzle_portion(ComposedLayout,Offset,LayoutB>) +{ + return Swizzle{}; +} + +// A non-swizzled layout's "Swizzle part" is the identity swizzle. +template +CUTE_HOST_DEVICE constexpr +auto +get_swizzle_portion(Layout) +{ + return Swizzle<0,4,3>{}; +} + +// Get the "non-swizzle" part of a composed layout, +// which is the underlying (non-composed) Layout. +template +auto +get_nonswizzle_portion(ComposedLayout,Offset,LayoutB> const& slayout) +{ + return slayout.layout_b(); +} + +// The non-swizzle part of a non-swizzled layout is just the Layout. +template +auto +get_nonswizzle_portion(Layout const& slayout) +{ + return slayout; +} + +} // namespace detail + +// +// Slice a Swizzled ComposedLayout +// + +namespace detail { + +template +CUTE_HOST_DEVICE constexpr +auto +make_swizzle_strides(true_type, + IntZ const& Z, + IntY const& Y, + Offset const& offset, + int_sequence) +{ + // Below is an optimized/compressed version of: + //return cute::make_tuple((swizzle(offset + Z*Int<(1 << I)>{}) - swizzle(offset))...); + // with knowledge of Swizzle, I... ranges for each B bits, + // and the layout won't slice along z-bits that are already set + + // y\z 0 1 + // 0 Z DC + // 1 -Z DC + + return cute::make_tuple(conditional_return((offset & (Y << Int{})) == Int<0>{}, Z << Int{}, -(Z << Int{}))...); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_swizzle_strides(false_type, + IntZ const& Z, + IntY const& Y, + Offset const& offset, + int_sequence) +{ + // Below is an optimized/compressed version of: + //return cute::make_tuple((swizzle(offset + Y*Int<(1 << I)>{}) - swizzle(offset))...); + // with knowledge of Swizzle, I... ranges for each B bits, + // and the layout won't slice along y-bits that are already set + + // y\z 0 1 + // 0 Y+Z Y-Z + // 1 DC DC + + return cute::make_tuple(conditional_return((offset & (Z << Int{})) == Int<0>{}, (Y+Z) << Int{}, (Y-Z) << Int{})...); +} + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr +auto +slice_and_offset(Coord const& coord, ComposedLayout,Offset,Layout> const& layout) +{ + if constexpr (all_underscore::value) { + // Skip the expensive/complicated attempt to decay to a normal layout and just reshape + return cute::make_tuple(composition(layout.layout_a(), layout.offset(), slice(coord, layout.layout_b())), Int<0>{}); + } else { + + // Projections of the swizzle layout for composition + 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>{})); + + // The portion of the layout that is not yet consumed + auto sliced_layout = slice(coord, layout.layout_b()); + + // If the sliced_layout hits two bits that are swizzled together, then don't attempt to decay + + // Compose with the layout to get the swizzle projection, P o L [The Z and Y contributing portions of L] + // (this also tests that shape/stride of layout compose with swizzle) + auto sliced_layout_only_zy = composition(swizzle_only_zy, sliced_layout); + // Transform the end coordinate to get the active bits of the swizzle, (P o L)(c*) + auto swizzle_active_bits = sliced_layout_only_zy(size(sliced_layout_only_zy)-Int<1>{}); + // Determine if any active bits collide under the swizzle + auto hit_ZandY = !(swizzle_active_bits & ~layout.layout_a()(swizzle_active_bits)); + + // The portion of the layout that we are consuming now + 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); + + // New swizzle and offset + auto swizzle = layout.layout_a(); + // offset_only_zy interacts with swizzle and gets accumulated with layout.offset() + // being careful about the static/dynamic contributions from diced_layout and diced_coord + auto offset_only_zy = layout.offset() ^ to_mixed_bits(diced_layout_only_zy, diced_coord); + // offset_anti_zy always gets passed through, no interaction with swizzle + auto offset_anti_zy = diced_layout_anti_zy(diced_coord); + + // If Layout's codomain hits on Y AND Z, then it's not reducible + // If Layout's codomain hits on Y XOR Z, then it's dynamic-normal + // If Layout's codomain hits on neither Y NOR Z, then it's static-normal + + // Test the sliced layout for hit_X & hit_Y for potential decay + if constexpr (is_constant::value) + { // Hits on Y AND Z, so it's not reducible + return cute::make_tuple(composition(swizzle, offset_only_zy, sliced_layout), offset_anti_zy); + } else + { // Misses on Y or Z, so it's static-normal or dynamic-normal + + // Lowest bit of the Z and Y masks + auto Z = typename Swizzle::zzz_msk{} & -typename Swizzle::zzz_msk{}; + auto Y = typename Swizzle::yyy_msk{} & -typename Swizzle::yyy_msk{}; + auto stride_lo = detail::make_swizzle_strides(Z < Y, Z, Y, offset_only_zy, make_int_sequence{}); + auto stride_hi = detail::make_swizzle_strides(Z > Y, Z, Y, offset_only_zy, make_int_sequence{}); + + // Construct a (dynamic) layout that we can perform the composition with + auto swizzle_layout = make_layout(make_shape (Int<(1 << M)>{}, repeat(Int<2>{}), Int<(1 << (abs(S)-B))>{}, repeat(Int<2>{}), Int< 1>{}), + make_stride(Int< 1>{}, stride_lo, Int<(1 << (M+B))>{}, stride_hi , Int<(1 << (M+B+abs(S)))>{})); + + // Decay to a normal layout with offset + return cute::make_tuple(composition(swizzle_layout, sliced_layout), + swizzle(offset_only_zy) + offset_anti_zy); + } + } + + CUTE_GCC_UNREACHABLE; +} + +// +// composition +// + +// Ignore identity case +template +CUTE_HOST_DEVICE constexpr +auto +composition(Swizzle<0,M,S> const&, + Int<0> const&, + Layout const& layout) +{ + return layout; +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Swizzle const& sxor, + Layout const& layout) +{ + return composition(sxor, Int<0>{}, layout); +} + +template +CUTE_HOST_DEVICE constexpr +auto +composition(Layout const& a, + Swizzle const& b) +{ + // Get the Z bits and the Y bits + auto active_Y = a(typename Swizzle::yyy_msk{}); + auto active_Z = a(typename Swizzle::zzz_msk{}); + + // Works in simple cases... but could be greatly generalized + + return composition(make_swizzle(), a); +} + +// +// inverse +// + +// Specialization to attempt to pass-through the Swizzle back to the left -- Needed? +template +CUTE_HOST_DEVICE constexpr +auto +right_inverse(ComposedLayout,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())); + } +} + +// Specialization to attempt to pass-through the Swizzle back to the left -- Needed? +template +CUTE_HOST_DEVICE constexpr +auto +left_inverse(ComposedLayout,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())); + } +} + +// +// Other operations +// + +template +CUTE_HOST_DEVICE constexpr +auto +max_common_vector(ComposedLayout,Offset,LayoutB> const& a, + Layout const& b) +{ + // This assumes that Offset is in the YZ domain of the Swizzle... + return cute::min(Int<(1 << M)>{}, max_common_vector(a.layout_b(), b)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +max_common_vector(Layout const& a, + ComposedLayout,Offset,LayoutB> const& b) +{ + return max_common_vector(b, a); +} + +template +CUTE_HOST_DEVICE constexpr +auto +max_common_vector(ComposedLayout,Offset0,LayoutB0> const& a, + ComposedLayout,Offset1,LayoutB1> const& b) +{ + auto result = coalesce(composition(a, right_inverse(b))); + + if constexpr (is_constant<1, decltype(stride<0>(result.layout_b()))>::value) { + return shape<0>(result); + } else { + return Int<1>{}; + } + + CUTE_GCC_UNREACHABLE; +} + +/////////////////////////////////////////////////////////////////////////////// +// ComposedLayout as second argument is often more difficult... + +template +CUTE_HOST_DEVICE constexpr +auto +logical_product(Layout const& block, + ComposedLayout,Offset,LayoutT> const& tile) +{ + CUTE_STATIC_ASSERT_V(tile.offset() == Int<0>{}, "Require Swizzle offset == 0."); + // The new layout -- if swizzle wasn't an issue, this is the result + // our goal is to determine a new swizzle for these strides + auto new_layout = logical_product(block, tile.layout_b()); + + // This is accomplished by identifying + // S o L :=: S? o L* + // We identify the "active" portion of S by computing (P o L)(c*) where P is a projection generated by S + // Then that active identifier is transformed through the layouts: + // L*(L[(P o L)(c*)]) + // which is a new swizzle identifier for S?, the new swizzle + + // Projections of the swizzle layout for composition, P + 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>{})); + + // Compose with the tile to get the swizzle projection, P o L [The Z and Y contributing portions of L] + auto layout_only_zy = composition(swizzle_only_zy, tile.layout_b()); + // Transform the end coordinate to get the active bits of the swizzle, (P o L)(c*) + auto swizzle_active_bits = layout_only_zy(size(layout_only_zy)-Int<1>{}); + // Get the Z bit and the Y bits + auto active_Z = swizzle_active_bits & typename Swizzle::zzz_msk{}; + auto active_Y = swizzle_active_bits & typename Swizzle::yyy_msk{}; + + // Pass the identifiers through the old layout and new layout to make a new swizzle identifier, L*(L[(P o L)(c*)]) + auto new_active_Z = new_layout(Int<0>{}, tile.layout_b()[active_Z]); + auto new_active_Y = new_layout(Int<0>{}, tile.layout_b()[active_Y]); + + // Use this new swizzle identifier to construxt the new swizzle for new_layout + // (this also makes sure it's a "valid" swizzle that Swizzle can represent) + return composition(make_swizzle(), new_layout); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_ptr_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_ptr_hip.hpp new file mode 100644 index 000000000..fbc66e5db --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_ptr_hip.hpp @@ -0,0 +1,309 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +#include +#include + +#include +#include +#include + +/* This implements a swizzle pointer of the form + * InvolutionFn o PtrAdd + * where the InvolutionFn need not be linear. + * + * This differs subtly from swizzle_layout because the smem pointer is used + * as the offset. That means that swizzle_layout will implement position-independent + * swizzle layouts, while swizzle_ptr implements position-dependent swizzle tensors. + * Arch chose to design hardware with position-dependent swizzles. + * + * For clarity: + * NormalLayout : DeRef <- PtrAdd <- [Layout] + * ComposedLayout: DeRef <- PtrAdd <- [Swizzle <- OffsetAdd <- Layout] + * SwizzlePtr : [DeRef <- Swizzle <- PtrAdd] <- Layout + * + * Furthermore, for known swizzles, this pointer attempts to decay itself + * to a normal-pointer with a new layout containing dynamic or static strides. + * This is possible by determining the subdomain of the InvolutionFn + * that is identity and testing if the Layout's codomain is contained + * within it. + */ + +namespace cute +{ + +template +struct smem_ptr_swizzle +{ + static_assert(is_empty::value, "Swizzle can't have state."); + + static const uint32_t ElementsPerStoredItem = sizeof(T) * 8 / sizeof_bits_v; + + CUTE_HOST_DEVICE constexpr + T* get() const + { + return ptr_; + } + + CUTE_HOST_DEVICE constexpr static + Swizzle get_swizzle() + { + return {}; + } + + CUTE_HOST_DEVICE constexpr static + T* apply_swizzle(T* ptr) + { + return reinterpret_cast(Swizzle::apply(reinterpret_cast(ptr))); + } + + CUTE_HOST_DEVICE constexpr + T& operator*() const + { + return *apply_swizzle(get()); + } + + template + CUTE_HOST_DEVICE constexpr + T& operator[](Int const& i) const + { + static_assert(sizeof_bits_v >= 8, "Use subbyte_iterator to access the element"); + return *apply_swizzle(get() + i); + } + + template + CUTE_HOST_DEVICE constexpr + smem_ptr_swizzle operator+(Int const& i) const + { + return {ptr_ + i / ElementsPerStoredItem}; + } + + T* ptr_; +}; + +template +struct is_smem> : true_type {}; + +// Make a swizzle pointer +template +CUTE_HOST_DEVICE constexpr +auto +make_smem_ptr(T* ptr, Swizzle const&) +{ + return smem_ptr_swizzle{ptr}; +} + +// Specialization for immediate decay +template +CUTE_HOST_DEVICE constexpr +auto +make_smem_ptr(T* ptr, Swizzle<0,M,S> const&) +{ + return make_smem_ptr(ptr); +} + +// A model of a nullptr smem_ptr with B == sizeof_bits::value +// That represents an unset pointer. This is a placeholder type that is waiting for an smem_ptr +template +struct smem_ptr_flag_bits : Int<0> {}; + +using smem_ptr_flag = smem_ptr_flag_bits<1>; + +// A flagged construction method to transform ComposedLayout +// Make a swizzle pointer tensor and check that the intended type size matches +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor(smem_ptr const& ptr, + ComposedLayout,Layout> const& layout) +{ + static_assert(B == sizeof_bits::value, "Expected a B-bit pointer type."); + return make_tensor(make_smem_ptr(ptr.get(), layout.layout_a()), + layout.layout_b()); +} + +// NOTE: To preserve smem_ptr_flag_bits under recast ops +template +CUTE_HOST_DEVICE constexpr +auto +upcast(ComposedLayout,Layout> const& layout) +{ + return composition(layout.layout_a(), smem_ptr_flag_bits{}, upcast(layout.layout_b())); +} + +template +CUTE_HOST_DEVICE constexpr +auto +downcast(ComposedLayout,Layout> const& layout) +{ + return composition(layout.layout_a(), smem_ptr_flag_bits{}, downcast(layout.layout_b())); +} + +// +// Recast +// Swizzle operates on the pointer address, so it doesn't care about the type +// + +template +CUTE_HOST_DEVICE constexpr +auto +recast(smem_ptr_swizzle const& ptr) +{ + return smem_ptr_swizzle{recast(ptr.ptr_)}; +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(smem_ptr_swizzle const& ptr) +{ + return smem_ptr_swizzle{recast(ptr.ptr_)}; +} + +template +CUTE_HOST_DEVICE constexpr +T* +raw_pointer_cast(smem_ptr_swizzle ptr) { + return ptr.get(); +} + +// +// Conversion with swizzle_layout +// + +template +CUTE_HOST_DEVICE +auto +as_position_independent_swizzle_layout(ComposedLayout,Layout> const& layout) +{ + return composition(recast,uint_bit_t>(layout.layout_a()), Int<0>{}, layout.layout_b()); +} + +template +CUTE_HOST_DEVICE +auto +as_position_independent_swizzle_tensor(Tensor>, Layout> const& tensor) +{ + { + uint32_t address = cast_smem_ptr_to_uint(tensor.data().get()); + uint32_t mask = ((uint32_t(1) << Swizzle::num_base) - 1) & (Swizzle::swizzle_code); + assert((address & mask) == 0); // Alignment to the Base, Z, and Y of Swizzle + } + auto new_swizzle = recast,uint_bit_t>>(tensor.data().get_swizzle()); + return make_tensor(make_smem_ptr(tensor.data().get()), composition(new_swizzle, Int<0>{}, tensor.layout())); +} + +template +CUTE_HOST_DEVICE +auto +as_position_independent_swizzle_tensor(Tensor>, Layout>& tensor) +{ + { + [[maybe_unused]] uint32_t address = cast_smem_ptr_to_uint(tensor.data().get()); + [[maybe_unused]] uint32_t mask = ((uint32_t(1) << Swizzle::num_base) - 1) & (Swizzle::swizzle_code); + assert((address & mask) == 0); // Alignment to the Base, Z, and Y of Swizzle + } + auto new_swizzle = recast,uint_bit_t>>(tensor.data().get_swizzle()); + return make_tensor(make_smem_ptr(tensor.data().get()), composition(new_swizzle, Int<0>{}, tensor.layout())); +} + +template +CUTE_HOST_DEVICE +auto +as_position_independent_swizzle_tensor(Tensor>, Layout>&& tensor) +{ + return as_position_independent_swizzle_tensor(tensor); +} + +// Pass through everything else +// Used if the tensor doesn't have a swizzled layout, e.g. Layout_MN_INTER_Atom, Layout_K_INTER_Atom +template +CUTE_HOST_DEVICE constexpr +auto +as_position_independent_swizzle_tensor(Tensor const& tensor) +{ + return tensor; +} + +template +CUTE_HOST_DEVICE constexpr +auto +as_position_independent_swizzle_tensor(Tensor&& tensor) +{ + return tensor; +} + +// +// Print +// + +// Capture and cast smem_ptr_flag Layouts to offset-0 layouts +template +CUTE_HOST_DEVICE +void +print_latex(ComposedLayout,Layout> const& layout) +{ + auto new_swizzle = recast,uint_bit_t>(layout.layout_a()); + print_latex(composition(new_swizzle, Int<0>{}, layout.layout_b())); +} + +template +CUTE_HOST_DEVICE void print(smem_ptr_flag_bits const& ptr) +{ + printf("smem_ptr_%db(unset)", B); +} + +template +CUTE_HOST_DEVICE void print(smem_ptr_swizzle> const& ptr) +{ + printf("smem_ptr_S<%d,%d,%d>_%db(%p)", B, M, S, int(sizeof_bits::value), ptr.get()); +} + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, smem_ptr_swizzle> const&) +{ + return os << "smem_ptr_S<" << B << "," << M << "," << S << ">_" << int(sizeof_bits::value) << "b"; +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_hip.hpp new file mode 100644 index 000000000..f3ebce659 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_hip.hpp @@ -0,0 +1,1027 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace cute +{ + +// +// Engine -- owning or non-owning data store +// + +// concept Engine { +// using value_type = ; +// iterator begin(); +// }; + +template +using ArrayEngine = typename conditional<(sizeof_bits::value % 8 == 0), + array_aligned, + array_subbyte>::type; + +template +struct ViewEngine +{ + using value_type = typename cute::remove_cvref())>::type; + + using iterator = Iterator; + iterator storage_; + + CUTE_HOST_DEVICE constexpr + iterator const& + begin() const { + return storage_; + } + + CUTE_HOST_DEVICE constexpr + iterator& + begin() { + return storage_; + } +}; + +template +struct is_rmem> : is_rmem {}; +template +struct is_smem> : is_smem {}; +template +struct is_gmem> : is_gmem {}; +template +struct ConstViewEngine +{ + using value_type = typename cute::remove_cvref())>::type; + + using iterator = Iterator; + iterator storage_; + + CUTE_HOST_DEVICE constexpr + iterator const& + begin() const { + return storage_; + } +}; + +template +struct is_rmem> : is_rmem {}; +template +struct is_smem> : is_smem {}; +template +struct is_gmem> : is_gmem {}; +// +// Tensor +// + +template +struct Tensor +{ + using value_type = typename Engine::value_type; + //using pointer = typename engine_traits::pointer; + //using const_pointer = typename engine_traits::const_pointer; + //using reference = typename engine_traits::reference; + //using const_reference = typename engine_traits::const_reference; + + using engine_type = Engine; + using layout_type = Layout; + + CUTE_HOST_DEVICE constexpr + Tensor() {} + + template + CUTE_HOST_DEVICE constexpr + Tensor(Ptr const& ptr, Layout const& layout) + : rep_(layout, ptr) { + } + + // + // Accessors + // + + static constexpr int rank = Layout::rank; + + CUTE_HOST_DEVICE constexpr + decltype(auto) + tensor() const { + return *this; + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + layout() const { + return get<0>(rep_); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + engine() const { + return get<1>(rep_); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + engine() { + return get<1>(rep_); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + data() const { + return engine().begin(); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + data() { + return engine().begin(); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + shape() const { + return layout().shape(); + } + + CUTE_HOST_DEVICE constexpr + auto + size() const { + return cute::size(shape()); + } + + CUTE_HOST_DEVICE constexpr + decltype(auto) + stride() const { + return layout().stride(); + } + + // + // Indexing op() and op[] + // + + // Index into this tensor like an array by computing the offset via layout() + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator[](Coord const& coord) { + return data()[layout()(coord)]; + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator[](Coord const& coord) const { + return data()[layout()(coord)]; + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator()(Coord const& coord) { + if constexpr (has_underscore::value) { + auto const& [sliced_layout,offset] = slice_and_offset(coord, layout()); + return make_tensor(data() + offset, sliced_layout); + } else { + return data()[layout()(coord)]; + } + + CUTE_GCC_UNREACHABLE; + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator()(Coord const& coord) const { + if constexpr (has_underscore::value) { + auto const& [sliced_layout,offset] = slice_and_offset(coord, layout()); + return make_tensor(data() + offset, sliced_layout); + } else { + return data()[layout()(coord)]; + } + + CUTE_GCC_UNREACHABLE; + } + + // op() convenience function for multi-dimensional coordinates + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator()(Coord0 const& c0, Coord1 const& c1, Coords const&... cs) { + return operator()(make_coord(c0,c1,cs...)); + } + + template + CUTE_HOST_DEVICE constexpr + decltype(auto) + operator()(Coord0 const& c0, Coord1 const& c1, Coords const&... cs) const { + return operator()(make_coord(c0,c1,cs...)); + } + + // + // Compose + // + + template + CUTE_HOST_DEVICE constexpr + auto + compose(Layouts const&... layouts) { + return make_tensor(data(), layout().compose(layouts...)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + compose(Layouts const&... layouts) const { + return make_tensor(data(), layout().compose(layouts...)); + } + + // + // Tile + // + + template + CUTE_HOST_DEVICE constexpr + auto + tile(Layouts const&... layouts) { + return make_tensor(data(), layout().tile(layouts...)); + } + + template + CUTE_HOST_DEVICE constexpr + auto + tile(Layouts const&... layouts) const { + return make_tensor(data(), layout().tile(layouts...)); + } + + // + // Utility + // + + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_1d_coord(Int const& linear_idx) const { + return layout().get_1d_coord(linear_idx); + } + + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_hier_coord(Int const& linear_idx) const { + return layout().get_hier_coord(linear_idx); + } + + template ::value)> + CUTE_HOST_DEVICE constexpr + auto + get_flat_coord(Int const& linear_idx) const { + return layout().get_flat_coord(linear_idx); + } + + cute::tuple rep_; +}; + + +template +struct is_tensor : false_type {}; +template +struct is_tensor> : true_type {}; + +template +struct is_rmem> : is_rmem {}; +template +struct is_smem> : is_smem {}; +template +struct is_gmem> : is_gmem {}; +// Customization point for creation of owning and non-owning Tensors +template +struct MakeTensor +{ + template ::value && + is_layout::value)> + CUTE_HOST_DEVICE constexpr auto + operator()(Layout const& layout) const + { + static_assert(is_static::value, "Dynamic owning tensors not supported"); + using Engine = ArrayEngine>; + return Tensor(); + } + + template ::value && + is_layout::value)> + CUTE_HOST_DEVICE constexpr auto + operator()(T const& iter, Layout const& layout) + { + using Engine = ViewEngine; + return Tensor(iter, layout); + } + + template ::value)> + CUTE_HOST_DEVICE constexpr auto + operator()(LayoutArg const& arg, LayoutArgs const&... args) const + { + return operator()(make_layout(arg, args...)); + } + + template ::value)> + CUTE_HOST_DEVICE constexpr auto + operator()(T const& iter, LayoutArg const& arg, LayoutArgs const&... args) + { + return operator()(iter, make_layout(arg, args...)); + } +}; + +// +// make_tensor +// + +// Make an owning Tensor that will allocate a static array +// e.g. make_tensor(Int<12>{}) +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor(Args const&... args) +{ + return MakeTensor{}(args...); +} + +// Make a non-owning Tensor that will use a pointer (view) +// e.g. make_tensor(vec.data(), 12) +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor(Iterator const& iter, Args const&... args) +{ + return MakeTensor{}(iter, args...); +} + +// +// make_tensor_like +// Make a register tensor the same type and shape and (if possible) order as another tensor +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor_like(Layout const& layout) +{ + return make_tensor(make_layout_like(layout)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor_like(Tensor const& tensor) +{ + return make_tensor_like(tensor.layout()); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_tensor_like(Tensor const& tensor) +{ + return make_tensor_like(tensor.layout()); +} + +// +// make_fragment_like -- +// Make a tensor the same shape and (if possible) order as another tensor, with special +// consideration of the 0th mode. The 0th mode is commonly used for MMA_Atoms or Copy_Atoms +// so this allocates the 0th mode with LayoutLeft regardless of the reference layout. +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(Layout const& layout) +{ + return make_tensor(make_fragment_like(layout)); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(Tensor const& tensor) +{ + return make_fragment_like(tensor.layout()); +} + +template +CUTE_HOST_DEVICE constexpr +auto +make_fragment_like(Tensor const& tensor) +{ + return make_fragment_like(tensor.layout()); +} + +// +// make_counting_tensor +// Make a tensor from a layout by binding it to a counting iter with 0-offset of the same profile as the codomain. +// + +template ::value)> +CUTE_HOST_DEVICE constexpr +auto +make_counting_tensor(Layout const& layout) +{ + return make_tensor(ArithmeticTupleIterator(as_arithmetic_tuple(repeat_like(coshape(layout), Int<0>{}))), + layout); +} + +// +// make_identity_tensor +// Make a tensor that maps coordinates within a shape to themselves. +// + +template +CUTE_HOST_DEVICE constexpr +auto +make_identity_tensor(Shape const& shape) +{ + return make_counting_tensor(make_identity_layout(shape)); +} + +// +// Utilities +// + +// Return the subtensor of a mode +template >::value)> +CUTE_HOST_DEVICE constexpr +decltype(auto) +tensor(Tensor&& tensor) +{ + return std::forward(tensor); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +decltype(auto) +tensor(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), get(tensor.layout())); +} + +// Return the subtensor of a range of modes +template >::value)> +CUTE_HOST_DEVICE constexpr +decltype(auto) +take(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), take(tensor.layout())); +} + +// Return the layout of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +layout(Tensor const& tensor) +{ + return layout(tensor.layout()); +} + +// Return the shape of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +shape(Tensor const& tensor) +{ + return shape(tensor.layout()); +} + +// Return the stride of a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +stride(Tensor const& tensor) +{ + return stride(tensor.layout()); +} + +// Return the number of elements in a mode +template +CUTE_HOST_DEVICE constexpr +decltype(auto) +size(Tensor const& tensor) +{ + return size(tensor.layout()); +} + +// Return the rank of a mode +template +CUTE_HOST_DEVICE constexpr +auto +rank(Tensor const& tensor) +{ + return rank(tensor.layout()); +} + +// Return the depth of a mode +template +CUTE_HOST_DEVICE constexpr +auto +depth(Tensor const& tensor) +{ + return depth(tensor.layout()); +} + +// +// Operations to manipulate Tensors like a Layout +// + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +flatten(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), flatten(tensor.layout())); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +coalesce(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), coalesce(tensor.layout())); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +coalesce(Tensor&& tensor, Profile const& profile) +{ + return make_tensor(std::forward(tensor).data(), coalesce(tensor.layout(), profile)); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +filter_zeros(Tensor&& tensor) +{ + return make_tensor(cute::forward(tensor).data(), filter_zeros(tensor.layout())); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +filter(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), filter(tensor.layout())); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +filter(Tensor&& tensor, Profile const& profile) +{ + return make_tensor(std::forward(tensor).data(), filter(tensor.layout(), profile)); +} + +// Return a tensor with the same shape as input but offset by a given coordinate +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +domain_offset(Coord const& coord, Tensor&& tensor) +{ + auto [layout, ptr_offset] = domain_offset(coord, tensor.layout()); + return make_tensor(std::forward(tensor).data() + ptr_offset, layout); +} + +// Group the modes [B,E) into a single mode +// e.g. group<2,4>(make_tensor(Layout>{})) +// => make_tensor(Layout,_5,_6>>{}) +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +group_modes(Tensor&& tensor) +{ + return make_tensor(std::forward(tensor).data(), + group(tensor.layout())); +} + +// +// Recast +// + +// NOTE: This is very dangerous to do +// -- doesn't check dynamic integer divisibility +// -- doesn't check alignment + +// A tagged version for dispatching +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +recast(Tensor&& tensor, type_list) +{ + using OldType = typename remove_cvref_t::value_type; + auto old_layout = tensor.layout(); + auto new_layout = recast(old_layout); + + // If this is an upcast of a normal Layout with static negative strides, then offset as well + if constexpr (sizeof(OldType) < sizeof(NewType) && not is_composed_layout::value) { + auto shape_diff = transform(flatten(old_layout.shape()), flatten(new_layout.shape()), minus{}); + auto extent_diff = transform(shape_diff, flatten(old_layout.stride()), multiplies{}); + auto offset = fold(extent_diff, Int<0>{}, [](auto const& i, auto const& a) { return i + cute::min(a,Int<0>{}); }); + + return make_tensor(recast(std::forward(tensor).data() + offset), new_layout); + } else { + return make_tensor(recast(std::forward(tensor).data() ), new_layout); + } + + CUTE_GCC_UNREACHABLE; +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(Tensor const& tensor) +{ + return recast(tensor, type_list{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(Tensor& tensor) +{ + return recast(tensor, type_list{}); +} + +template +CUTE_HOST_DEVICE constexpr +auto +recast(Tensor&& tensor) +{ + return recast(std::forward>(tensor), type_list{}); +} + +// +// max_common_vector +// + +/* Return Int such that N is the maximum number of continguous elements + * that logically correspond in the tensors of @a a and @a b. This is, + * the number of elements that could reasonably be vectorized into a single load/store. + * + * @returns Int with N >= 0 + * + * A return value of Int<0> indicates that no such conclusion can be made and no + * vectorization should be attempted. + */ +template +CUTE_HOST_DEVICE constexpr +auto +max_common_vector(Tensor const& a, + Tensor const& b) +{ + using SrcType = typename Tensor::value_type; + using DstType = typename Tensor::value_type; + + using SrcRef = decltype(*(a.data())); + using DstRef = decltype(*(b.data())); + + // Determine if vectorization candidates at all + if constexpr (// Should be the same value_types, else the copy is also performing a cast + sizeof(SrcType) == sizeof(DstType) && + // The types should be trivially copyable so that vectorization is valid + is_trivially_copyable::value && + is_trivially_copyable::value && + // Should be load/storing real data, rather than implicit iterators or such + is_reference::value && + is_reference::value) + { + return max_common_vector(a.layout(), b.layout()); + } else { + return Int<0>{}; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Key algebraic operations +// + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +logical_divide(Tensor && tensor, + Tile const& tile) +{ + return make_tensor(std::forward(tensor).data(), + logical_divide(tensor.layout(), tile)); +} + +// zipped_divide is logical_divide with modes gathered into standard form ((BLK_A,BLK_B),(a,b)) +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +zipped_divide(Tensor && tensor, + Tile const& tile) // Layout or Tile +{ + return make_tensor(std::forward(tensor).data(), + zipped_divide(tensor.layout(), tile)); +} + +// tiled_divide is logical_divide with the second output mode flattened ((BLK_A,BLK_B),a,b) +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +tiled_divide(Tensor && tensor, + Tile const& tile) // Layout or Tile +{ + return make_tensor(std::forward(tensor).data(), + tiled_divide(tensor.layout(), tile)); +} + +// logical_product on a Tensor doesn't make sense since it often increases cosize + +// +// Logical Divide utilities: local_partition and local_tile +// + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +local_partition(Tensor && tensor, + Tile const& tile, + Coord const& coord) +{ + constexpr int R1 = decltype(rank(tensor))::value; + + // Split the modes of tensor according to the modes of tile + // zipped_divide returns something like ((VEC_A,VEC_B,...),(a,b,...)) + + // The_coord is the coord into the first mode, flatten the rest + return zipped_divide(std::forward(tensor), tile)(coord, repeat(_)); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +local_partition(Tensor && tensor, + Tile const& tile, + Coord const& coord, + Projection const& proj) +{ + return local_partition(std::forward(tensor), + dice(proj, tile), + dice(proj, coord)); +} + +// Special case with Layout and Integral that extracts the coord first +// e.g. local_partition(tensor, ThrLayout, threadIdx.x) +template >::value && + is_integral::value)> +CUTE_HOST_DEVICE +auto +local_partition(Tensor && tensor, + Layout const& tile, + Index const& index) +{ + return local_partition(std::forward(tensor), + product_each(shape(tile)), + tile.get_flat_coord(index)); +} + +// Special case with Layout and Integral that extracts the coord first +// e.g. local_partition(tensor, ThrLayout, threadIdx.x, Step<_1,X,_1>{}) +template >::value && + is_integral::value)> +CUTE_HOST_DEVICE +auto +local_partition(Tensor && tensor, + Layout const& tile, + Index const& index, + Projection const& proj) +{ + return local_partition(std::forward(tensor), + dice(proj, product_each(shape(tile))), + dice(proj, tile).get_flat_coord(index)); +} + +template >::value)> +CUTE_HOST_DEVICE constexpr +auto +local_tile(Tensor && tensor, + Tile const& tile, + Coord const& coord) +{ + constexpr int R0 = decltype(rank(tile))::value; + constexpr int R1 = decltype(rank(tensor))::value; + + // Split the modes of tensor according to the modes of tile + // zipped_divide returns something like ((VEC_A,VEC_B,...),(a,b,...)) + + // The padded_coord is the coord into the second mode, flatten the rest + return zipped_divide(std::forward(tensor), tile)(repeat(_), append(coord,_)); +} + +template >::value)> +CUTE_HOST_DEVICE +auto +local_tile(Tensor && tensor, + Tile const& tile, + Coord const& coord, + Proj const& proj) +{ + return local_tile(std::forward(tensor), + dice(proj, tile), + dice(proj, coord)); +} + +// +// Display utilities +// + +template +CUTE_HOST_DEVICE void print(Tensor const& tensor) +{ + print(tensor.data()); print(" o "); print(tensor.layout()); +} + +template +CUTE_HOST_DEVICE void print_tensor(Tensor const& tensor) +{ + print(tensor); print(":\n"); + + auto format = get_format(tensor(0)); + using type = typename decltype(format)::type; + + if constexpr (Layout::rank == 1) + { + for (int m = 0; m < size(tensor); ++m) { + printf(format.format, format.digits, type(tensor(m))); + printf("\n"); + } + } else + if constexpr (Layout::rank == 2) + { + for (int m = 0; m < size<0>(tensor); ++m) { + for (int n = 0; n < size<1>(tensor); ++n) { + printf(format.format, format.digits, type(tensor(m,n))); + } + printf("\n"); + } + } else + if constexpr (Layout::rank == 3) + { + print_tensor(tensor(_,_,0)); + for (int k = 1; k < size<2>(tensor); ++k) { + for (int i = 0; i < format.digits*size<1>(tensor); ++i) { print("-"); } print("\n"); + print_tensor(tensor(_,_,k)); + } + } else + if constexpr (Layout::rank == 4) + { + print_tensor(tensor(_,_,_,0)); + for (int p = 1; p < size<3>(tensor); ++p) { + for (int i = 0; i < format.digits*size<1>(tensor); ++i) { print("="); } print("\n"); + print_tensor(tensor(_,_,_,p)); + } + } +} + + + +#if !defined(__CUDACC_RTC__) +template +CUTE_HOST std::ostream& print_tensor_os(std::ostream& os, Tensor const& tensor) +{ + int digits = 9; + + if constexpr (Layout::rank == 1) + { + for (int m = 0; m < size(tensor); ++m) { + os << std::setw(digits) << tensor(m) << std::endl; + } + } else + if constexpr (Layout::rank == 2) + { + for (int m = 0; m < size<0>(tensor); ++m) { + for (int n = 0; n < size<1>(tensor); ++n) { + os << std::setw(digits) << tensor(m,n); + } + os << std::endl; + } + } else + if constexpr (Layout::rank == 3) + { + print_tensor_os(os, tensor(_,_,0)); + for (int k = 1; k < size<2>(tensor); ++k) { + for (int i = 0; i < digits*size<1>(tensor); ++i) { os << "-"; } os << std::endl; + print_tensor_os(os, tensor(_,_,k)); + } + } else + if constexpr (Layout::rank == 4) + { + print_tensor_os(os, tensor(_,_,_,0)); + for (int p = 1; p < size<3>(tensor); ++p) { + for (int i = 0; i < digits*size<1>(tensor); ++i) { os << "="; } os << std::endl; + print_tensor_os(os, tensor(_,_,_,p)); + } + } + + return os; +} + +template +CUTE_HOST std::ostream& operator<<(std::ostream& os, Tensor const& tensor) +{ + os << tensor.layout() << std::endl; + return print_tensor_os(os, tensor); +} +#endif // !defined(__CUDACC_RTC__) + +} // end namespace cute + +// +// Extended Engines +// + +#include +// +// Tensor Algorithms +// + +#include +#include +#include +#include +#include +#include diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_predicate_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_predicate_hip.hpp new file mode 100644 index 000000000..18db9da36 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tensor_predicate_hip.hpp @@ -0,0 +1,69 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +template +struct ConstantTensor +{ + template + CUTE_HOST_DEVICE constexpr + T const& + operator()(Coords const&...) const { + return val_; + } + + T val_; +}; + +struct TrivialPredTensor +{ + template + CUTE_HOST_DEVICE constexpr + true_type + operator()(Coords const&...) const { + return {}; + } +}; + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tile_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tile_hip.hpp new file mode 100644 index 000000000..7bca242b1 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/tile_hip.hpp @@ -0,0 +1,64 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +namespace cute +{ + +// +// A Tile is not a Layout, it's a tuple of Layouts or Tiles or Underscores +// + +template +using Tile = tuple; + +template +using is_tile = is_tuple; + +template +CUTE_HOST_DEVICE constexpr +auto +make_tile(Layouts const&... layouts) +{ + return Tile(layouts...); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/underscore_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/underscore_hip.hpp new file mode 100644 index 000000000..d722ab90c --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/underscore_hip.hpp @@ -0,0 +1,156 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include +#include +#include +#include + +namespace cute +{ + +// For slicing +struct Underscore : Int<0> {}; + +CUTE_INLINE_CONSTANT Underscore _; + +// Treat Underscore as an integral like integral_constant +template <> +struct is_integral : true_type {}; + +template +struct is_underscore : false_type {}; +template <> +struct is_underscore : true_type {}; + +// Tuple trait for detecting static member element +template +struct has_elem : false_type {}; +template +struct has_elem : true_type {}; +template +struct has_elem::value> > + : has_elem > {}; +template +struct has_elem> + : disjunction, Elem>...> {}; + +// Tuple trait for detecting static member element +template +struct all_elem : false_type {}; +template +struct all_elem : true_type {}; +template +struct all_elem::value> > + : all_elem > {}; +template +struct all_elem> + : conjunction, Elem>...> {}; + +// Tuple trait for detecting Underscore member +template +using has_underscore = has_elem; + +template +using all_underscore = all_elem; + +template +using has_int1 = has_elem>; + +template +using has_int0 = has_elem>; + +// +// Slice keeps only the elements of Tuple B that are paired with an Underscore +// + +template +CUTE_HOST_DEVICE constexpr +auto +slice(A const& a, B const& b) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return filter_tuple(a, b, [](auto const& x, auto const& y) { return slice(x,y); }); + } else if constexpr (is_underscore::value) { + return cute::tuple{b}; + } else { + return cute::tuple<>{}; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Dice keeps only the elements of Tuple B that are paired with an Int +// + +template +CUTE_HOST_DEVICE constexpr +auto +dice(A const& a, B const& b) +{ + if constexpr (is_tuple::value) { + static_assert(tuple_size::value == tuple_size::value, "Mismatched Ranks"); + return filter_tuple(a, b, [](auto const& x, auto const& y) { return dice(x,y); }); + } else if constexpr (is_underscore::value) { + return cute::tuple<>{}; + } else { + return cute::tuple{b}; + } + + CUTE_GCC_UNREACHABLE; +} + +// +// Display utilities +// + +CUTE_HOST_DEVICE void print(Underscore const&) { + printf("_"); +} + +#if !defined(__CUDACC_RTC__) +CUTE_HOST std::ostream& operator<<(std::ostream& os, Underscore const&) { + return os << "_"; +} +#endif + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/debug_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/debug_hip.hpp new file mode 100644 index 000000000..ecc394ea0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/debug_hip.hpp @@ -0,0 +1,170 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +/** + * \file + * \brief Debugging and logging functionality + */ + +#include + +#include + +namespace cute +{ + +/****************************************************************************** + * Debug and logging macros + ******************************************************************************/ + +/** + * Formats and prints the given message to stdout + */ +#if !defined(CUTE_LOG) +# if !defined(__HIP_DEVICE_COMPILE__) +# define CUTE_LOG(format, ...) printf(format, __VA_ARGS__) +# else +# define CUTE_LOG(format, ...) \ + printf("[block (%d,%d,%d), thread (%d,%d,%d)]: " format, \ + blockIdx.x, blockIdx.y, blockIdx.z, \ + threadIdx.x, threadIdx.y, threadIdx.z, \ + __VA_ARGS__); +# endif +#endif + +/** + * Formats and prints the given message to stdout only if DEBUG is defined + */ +#if !defined(CUTE_LOG_DEBUG) +# ifdef DEBUG +# define CUTE_LOG_DEBUG(format, ...) CUTE_LOG(format, __VA_ARGS__) +# else +# define CUTE_LOG_DEBUG(format, ...) +# endif +#endif + +/** + * \brief Perror macro with exit + */ +#if !defined(CUTE_ERROR_EXIT) +# define CUTE_ERROR_EXIT(e) \ + do { \ + hipError_t code = (e); \ + if (code != hipSuccess) { \ + fprintf(stderr, "<%s:%d> %s:\n %s: %s\n", \ + __FILE__, __LINE__, #e, \ + hipGetErrorName(code), hipGetErrorString(code)); \ + fflush(stderr); \ + exit(1); \ + } \ + } while (0) +#endif + +#if !defined(CUTE_CHECK_LAST) +# define CUTE_CHECK_LAST() CUTE_ERROR_EXIT(hipPeekAtLastError()); CUTE_ERROR_EXIT(hipDeviceSynchronize()) +#endif + +#if !defined(CUTE_CHECK_ERROR) +# define CUTE_CHECK_ERROR(e) CUTE_ERROR_EXIT(e) +#endif + +// A dummy function that uses compilation failure to print a type +template +CUTE_HOST_DEVICE void +print_type() { + static_assert(sizeof...(T) < 0, "Printing type T."); +} + +template +CUTE_HOST_DEVICE void +print_type(T&&...) { + static_assert(sizeof...(T) < 0, "Printing type T."); +} + +// +// Device-specific helpers +// +// e.g. +// if (thread0()) print(...); +// if (block0()) print(...); +// if (thread(42)) print(...); + +CUTE_HOST_DEVICE +bool +block(int bid) +{ +#if defined(__HIP_DEVICE_COMPILE__) + return blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y == bid; +#else + return true; +#endif +} + +CUTE_HOST_DEVICE +bool +thread(int tid, int bid) +{ +#if defined(__HIP_DEVICE_COMPILE__) + return (threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.x*blockDim.y == tid) && block(bid); +#else + return true; +#endif +} + +CUTE_HOST_DEVICE +bool +thread(int tid) +{ + return thread(tid,0); +} + +CUTE_HOST_DEVICE +bool +thread0() +{ + return thread(0,0); +} + +CUTE_HOST_DEVICE +bool +block0() +{ + return block(0); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/print_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/print_hip.hpp new file mode 100644 index 000000000..e681b6e08 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/print_hip.hpp @@ -0,0 +1,204 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +// +// CUDA compatible print and printf +// + +namespace cute +{ + +CUTE_HOST_DEVICE +int +num_digits(int x) +{ + return (x < 10 ? 1 : + (x < 100 ? 2 : + (x < 1000 ? 3 : + (x < 10000 ? 4 : + (x < 100000 ? 5 : + (x < 1000000 ? 6 : + (x < 10000000 ? 7 : + (x < 100000000 ? 8 : + (x < 1000000000 ? 9 : + 10))))))))); +} + +template +struct format_and_size { + using type = T; + char const* format; + int digits; +}; + +CUTE_HOST_DEVICE +format_and_size +get_format(bool) { + return {"%*d", 3}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(int32_t) { + return {"%*d", 5}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(uint32_t) { + return {"%*d", 5}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(int64_t) { + return {"%*d", 5}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(uint64_t) { + return {"%*d", 5}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(half_t) { + return {"%*.4f", 8}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(float) { + return {"%*.4f", 10}; +} + +CUTE_HOST_DEVICE +format_and_size +get_format(double) { + return {"%*.3e", 11}; +} + +// +// print dispatcher +// + +CUTE_HOST_DEVICE +void +print(char c) { + printf("%c", c); +} + +CUTE_HOST_DEVICE +void +print(signed char a) { + printf("%hhd", a); +} + +CUTE_HOST_DEVICE +void +print(unsigned char a) { + printf("%hhu", a); +} + +CUTE_HOST_DEVICE +void +print(short a) { + printf("%hd", a); +} + +CUTE_HOST_DEVICE +void +print(unsigned short a) { + printf("%hu", a); +} + +CUTE_HOST_DEVICE +void +print(int a) { + printf("%d", a); +} + +CUTE_HOST_DEVICE +void +print(unsigned int a) { + printf("%u", a); +} + +CUTE_HOST_DEVICE +void +print(long a) { + printf("%ld", a); +} + +CUTE_HOST_DEVICE +void +print(unsigned long a) { + printf("%lu", a); +} + +CUTE_HOST_DEVICE +void +print(long long a) { + printf("%lld", a); +} + +CUTE_HOST_DEVICE +void +print(unsigned long long a) { + printf("%llu", a); +} + +template +CUTE_HOST_DEVICE +void +print(char const* format, T const&... t) { + printf(format, t...); +} + +CUTE_HOST_DEVICE +void +print(char const* format) { + printf("%s", format); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/type_traits_hip.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/type_traits_hip.hpp new file mode 100644 index 000000000..a58b6a323 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cute/util/type_traits_hip.hpp @@ -0,0 +1,247 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#include +#include +#include +#include +#else +#include +#include // tuple_size, tuple_element +#include // ptrdiff_t +#include // uintptr_t +#include // numeric_limits +#endif + +#include + +namespace cute +{ + using CUTE_STL_NAMESPACE::enable_if; + using CUTE_STL_NAMESPACE::enable_if_t; +} + +#define __CUTE_REQUIRES(...) typename cute::enable_if<(__VA_ARGS__)>::type* = nullptr +#define __CUTE_REQUIRES_V(...) typename cute::enable_if::type* = nullptr + +namespace cute +{ + +// +using CUTE_STL_NAMESPACE::conjunction; +using CUTE_STL_NAMESPACE::conjunction_v; + +using CUTE_STL_NAMESPACE::disjunction; +using CUTE_STL_NAMESPACE::disjunction_v; + +using CUTE_STL_NAMESPACE::negation; +using CUTE_STL_NAMESPACE::negation_v; + +using CUTE_STL_NAMESPACE::void_t; +using CUTE_STL_NAMESPACE::is_void_v; + +using CUTE_STL_NAMESPACE::is_base_of; +using CUTE_STL_NAMESPACE::is_base_of_v; + +using CUTE_STL_NAMESPACE::is_const_v; + +// using CUTE_STL_NAMESPACE::true_type; +// using CUTE_STL_NAMESPACE::false_type; + +using CUTE_STL_NAMESPACE::conditional; +using CUTE_STL_NAMESPACE::conditional_t; + +using CUTE_STL_NAMESPACE::remove_const_t; +using CUTE_STL_NAMESPACE::remove_cv_t; +using CUTE_STL_NAMESPACE::remove_reference_t; + +using CUTE_STL_NAMESPACE::extent; +using CUTE_STL_NAMESPACE::remove_extent; + +using CUTE_STL_NAMESPACE::decay; +using CUTE_STL_NAMESPACE::decay_t; + +using CUTE_STL_NAMESPACE::is_lvalue_reference; +using CUTE_STL_NAMESPACE::is_lvalue_reference_v; + +using CUTE_STL_NAMESPACE::is_reference; +using CUTE_STL_NAMESPACE::is_trivially_copyable; + +using CUTE_STL_NAMESPACE::is_same; +using CUTE_STL_NAMESPACE::is_same_v; + +using CUTE_STL_NAMESPACE::is_arithmetic; +using CUTE_STL_NAMESPACE::is_unsigned; +using CUTE_STL_NAMESPACE::is_unsigned_v; +using CUTE_STL_NAMESPACE::is_signed; +using CUTE_STL_NAMESPACE::is_signed_v; + +using CUTE_STL_NAMESPACE::make_signed; +using CUTE_STL_NAMESPACE::make_signed_t; + +// using CUTE_STL_NAMESPACE::is_integral; +template +using is_std_integral = CUTE_STL_NAMESPACE::is_integral; + +using CUTE_STL_NAMESPACE::is_empty; + +using CUTE_STL_NAMESPACE::invoke_result_t; + +// +using CUTE_STL_NAMESPACE::declval; + +template< class T > +constexpr T&& forward(remove_reference_t& t) noexcept +{ + return static_cast(t); +} + +template< class T > +constexpr T&& forward(remove_reference_t&& t) noexcept +{ + static_assert(! is_lvalue_reference_v, + "T cannot be an lvalue reference (e.g., U&)."); + return static_cast(t); +} + +template< class T > +constexpr remove_reference_t&& move( T&& t ) noexcept +{ + return static_cast&&>(t); +} + +// +using CUTE_STL_NAMESPACE::numeric_limits; + +// +using CUTE_STL_NAMESPACE::ptrdiff_t; + +// +using CUTE_STL_NAMESPACE::uintptr_t; + +// C++20 +// using std::remove_cvref; +template +struct remove_cvref { + using type = remove_cv_t>; +}; + +// C++20 +// using std::remove_cvref_t; +template +using remove_cvref_t = typename remove_cvref::type; + +// +// dependent_false +// +// @brief An always-false value that depends on one or more template parameters. +// See +// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1830r1.pdf +// https://github.com/cplusplus/papers/issues/572 +// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html +template +inline constexpr bool dependent_false = false; + +// +// tuple_size, tuple_element +// +// @brief CuTe-local tuple-traits to prevent conflicts with other libraries. +// For cute:: types, we specialize std::tuple-traits, which is explicitly allowed. +// cute::tuple, cute::array, cute::array_subbyte, etc +// But CuTe wants to treat some external types as tuples as well. For those, +// we specialize cute::tuple-traits to avoid polluting external traits. +// dim3, uint3, etc + +template +struct tuple_size; + +template +struct tuple_size::type>> : CUTE_STL_NAMESPACE::integral_constant::value> {}; + +// S = : std::integral_constant::value> {}; + +template +constexpr size_t tuple_size_v = tuple_size::value; + +template +struct tuple_element; + +template +struct tuple_element::type>> : CUTE_STL_NAMESPACE::tuple_element {}; + +template +using tuple_element_t = typename tuple_element::type; + +// +// is_valid +// + +namespace detail { + +template ()(declval()...))> +CUTE_HOST_DEVICE constexpr auto +is_valid_impl(int) { return CUTE_STL_NAMESPACE::true_type{}; } + +template +CUTE_HOST_DEVICE constexpr auto +is_valid_impl(...) { return CUTE_STL_NAMESPACE::false_type{}; } + +template +struct is_valid_fn { + template + CUTE_HOST_DEVICE constexpr auto + operator()(Args&&...) const { return is_valid_impl(int{}); } +}; + +} // end namespace detail + +template +CUTE_HOST_DEVICE constexpr auto +is_valid(F&&) { + return detail::is_valid_fn{}; +} + +template +CUTE_HOST_DEVICE constexpr auto +is_valid(F&&, Args&&...) { + return detail::is_valid_impl(int{}); +} + +} // end namespace cute diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/barrier_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/barrier_hip.h new file mode 100644 index 000000000..9277466d5 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/barrier_hip.h @@ -0,0 +1,510 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +/*************************************************************************************************** + * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Barrier Operations on SM90+ +*/ + +#pragma once + +// for support dtk23.04 and early version +#if __has_include() + #include +#else +__device__ +inline +__attribute__((convergent)) +void __syncwarp(unsigned mask=0xffffffff){ + return __syncthreads(); +} +#endif + +#include +#include +#if defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ >= 900 && (__CUDACC_VER_MAJOR__ >= 12) +#define CUDA_BARRIER_ENABLED 1 +#else +#define CUDA_BARRIER_ENABLED 0 +#endif + +namespace cutlass { +/// @brief +namespace arch { + +//////////////////////////////////////////////////////////////////////////////////////////////////// +class NamedBarrier { + + // Data Members: + + // Range = [1 , NUM_THREADS_PER_CTA] + // Range % warp-size (i.e 32) == 0 + uint32_t const num_threads_; + + // Range : [0, 15] + uint32_t const id_; + + public: + + CUTLASS_DEVICE + NamedBarrier(uint32_t num_threads, uint32_t id = 0) + : num_threads_(num_threads), id_(id) {} + + CUTLASS_DEVICE + void arrive_and_wait() const { + NamedBarrier::arrive_and_wait(num_threads_, id_); + } + + CUTLASS_DEVICE + void arrive() const { + NamedBarrier::arrive(num_threads_, id_); + } + + CUTLASS_DEVICE + void sync() const { + NamedBarrier::arrive_and_wait(); + } + + // Static variants + CUTLASS_DEVICE + static void arrive_and_wait(uint32_t num_threads, uint32_t barrier_id) { +#if CUDA_BARRIER_ENABLED + asm volatile("bar.sync %0, %1;" : : "r"(barrier_id), "r"(num_threads)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + CUTLASS_DEVICE + static void arrive(uint32_t num_threads, uint32_t barrier_id) { +#if CUDA_BARRIER_ENABLED + asm volatile("bar.arrive %0, %1;" : : "r"(barrier_id), "r"(num_threads)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + CUTLASS_DEVICE + static void sync(uint32_t num_threads, uint32_t barrier_id) { + NamedBarrier::arrive_and_wait(num_threads, barrier_id); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// Hopper introduces a new cluster-wide barrier which handle with Cluster-wide arrive-wait behaviour. +// This is an extension to the Ampere arrive-wait barriers +// Note : Ampere arrive-wait Barriers have a larger max-arrive count (2^30) than Hopper arrive-wait Barriers (2^20). +struct ClusterBarrier { + + using ValueType = uint64_t; + +protected: + // Can never be initialized - can only be aliased to smem + ValueType barrier_; + +public: + + CUTLASS_DEVICE + ClusterBarrier() = delete; + + CUTLASS_DEVICE + void init(uint32_t arrive_count) const { + ClusterBarrier::init(&this->barrier_, arrive_count); + } + + CUTLASS_DEVICE + uint32_t test_wait(uint32_t phase, uint32_t pred=true) const { + return ClusterBarrier::test_wait(&this->barrier_, phase, pred); + } + + CUTLASS_DEVICE + uint32_t try_wait(uint32_t phase) const { + return ClusterBarrier::try_wait(&this->barrier_, phase); + } + + CUTLASS_DEVICE + void wait(uint32_t phase) const { + ClusterBarrier::wait(&this->barrier_, phase); + } + + // Barrier arrive on local smem + CUTLASS_DEVICE + void arrive() const { + ClusterBarrier::arrive(&this->barrier_); + } + + // Remote SMEM arrive with a perdicate (usually done to pick the thread doing the arrive) + CUTLASS_DEVICE + void arrive(uint32_t cta_id, uint32_t pred = true ) const { + ClusterBarrier::arrive(&this->barrier_, cta_id, pred); + } + + // + // Static Versions + // + CUTLASS_DEVICE + static void init(ValueType const* smem_ptr, uint32_t arrive_count) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + "mbarrier.init.shared.b64 [%1], %0; \n" + "}" + : + : "r"(arrive_count), "r"(smem_addr)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // Static version of wait - in case we don't want to burn a register + CUTLASS_DEVICE + static void wait(ValueType const* smem_ptr, uint32_t phase) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + // Arbitrarily large timer value after which try-wait expires and re-tries. + uint32_t ticks = 0x989680; + asm volatile( + "{\n\t" + ".reg .pred P1; \n\t" + "LAB_WAIT: \n\t" + "mbarrier.try_wait.parity.shared.b64 P1, [%0], %1, %2; \n\t" + "@P1 bra.uni DONE; \n\t" + "bra.uni LAB_WAIT; \n\t" + "DONE: \n\t" + "}" + : + : "r"(smem_addr), "r"(phase), "r"(ticks)); + +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + CUTLASS_DEVICE + static uint32_t test_wait(ValueType const* smem_ptr, uint32_t phase, uint32_t pred) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + uint32_t waitComplete; + + asm volatile( + "{\n\t" + ".reg .pred P1; \n\t" + ".reg .pred P2; \n\t" + "setp.eq.u32 P2, %3, 1;\n\t" + "@P2 mbarrier.test_wait.parity.shared.b64 P1, [%1], %2; \n\t" + "selp.b32 %0, 1, 0, P1; \n\t" + "}" + : "=r"(waitComplete) + : "r"(smem_addr), "r"(phase), "r"(pred)); + + return waitComplete; +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + return 0; + } + + CUTLASS_DEVICE + static uint32_t try_wait(ValueType const* smem_ptr, uint32_t phase) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + uint32_t waitComplete; + + asm volatile( + "{\n\t" + ".reg .pred P1; \n\t" + "mbarrier.try_wait.parity.shared.b64 P1, [%1], %2; \n\t" + "selp.b32 %0, 1, 0, P1; \n\t" + "}" + : "=r"(waitComplete) + : "r"(smem_addr), "r"(phase)); + + return waitComplete; +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + return 0; + } + + // Static Predicated version of the above - in case we know the address. + CUTLASS_DEVICE + static void arrive(ValueType const* smem_ptr, uint32_t cta_id, uint32_t pred) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + ".reg .pred p;\n\t" + ".reg .b32 remAddr32;\n\t" + "setp.eq.u32 p, %2, 1;\n\t" + "@p mapa.shared::cluster.u32 remAddr32, %0, %1;\n\t" + "@p mbarrier.arrive.shared::cluster.b64 _, [remAddr32];\n\t" + "}" + : + : "r"(smem_addr), "r"(cta_id), "r"(pred)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // Barrier arrive on local smem + CUTLASS_DEVICE + static void arrive(ValueType const* smem_ptr) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + uint64_t state = 0; + asm volatile( + "{\n\t" + "mbarrier.arrive.shared.b64 %1, [%0];\n\t" + "}" + : + : "r"(smem_addr), "l"(state)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + CUTLASS_DEVICE + static void invalidate(ValueType const* smem_ptr) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + "mbarrier.ival.shared.b64 [%0]; \n\t" + "}" + : + : "r"(smem_addr)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// SM90 also introduces a new type of cluster-barrier which supports sync. +// not just based on Arrive Count, but also transaction count (in bytes) +struct ClusterTransactionBarrier : public ClusterBarrier { + + CUTLASS_DEVICE + ClusterTransactionBarrier() = delete; + + // Performs an arrive operation + expected transaction bytes increment + CUTLASS_DEVICE + void arrive_and_expect_tx(uint32_t transaction_bytes) const { + ClusterTransactionBarrier::arrive_and_expect_tx(&this->barrier_, transaction_bytes); + } + + // Performs an arrive operation + expected transaction bytes increment + CUTLASS_DEVICE + void arrive_and_expect_tx(uint32_t transaction_bytes, uint32_t cta_id) const { + ClusterTransactionBarrier::arrive_and_expect_tx(&this->barrier_, transaction_bytes , cta_id, true); + } + + // Performs an expected transaction bytes increment without doing an arrive operation + CUTLASS_DEVICE + void expect_transaction(uint32_t transaction_bytes) const { + ClusterTransactionBarrier::expect_transaction(&this->barrier_, transaction_bytes); + } + + // Performs an expected transaction bytes decrement without doing an arrive operation + CUTLASS_DEVICE + void complete_transaction(uint32_t transaction_bytes, uint32_t pred = 1) const { + uint32_t cta_rank = cute::block_rank_in_cluster(); + ClusterTransactionBarrier::complete_transaction(&this->barrier_, cta_rank, transaction_bytes, pred); + } + + // Performs an expected transaction bytes decrement without doing an arrive operation + CUTLASS_DEVICE + void complete_transaction(uint32_t dst_cta_id, uint32_t transaction_bytes, uint32_t pred) const { + ClusterTransactionBarrier::complete_transaction(&this->barrier_, dst_cta_id, transaction_bytes, pred); + } + + // + // Static Versions + // + + // Performs an arrive operation + expected transaction bytes increment + CUTLASS_DEVICE + static void arrive_and_expect_tx(ValueType const* smem_ptr, uint32_t transaction_bytes) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + "mbarrier.arrive.expect_tx.shared.b64 _, [%1], %0; \n\t" + "}" + : + : "r"(transaction_bytes), "r"(smem_addr)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // Performs an arrive operation + expected transaction bytes increment for a remote cta_id in a Cluster + CUTLASS_DEVICE + static void arrive_and_expect_tx( + ValueType const* smem_ptr, uint32_t transaction_bytes, uint32_t cta_id, uint32_t pred) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + ".reg .pred p;\n\t" + ".reg .b32 remAddr32;\n\t" + "setp.eq.u32 p, %2, 1;\n\t" + "@p mapa.shared::cluster.u32 remAddr32, %0, %1;\n\t" + "@p mbarrier.arrive.expect_tx.shared::cluster.b64 _, [remAddr32], %3;\n\t" + "}" + : + : "r"(smem_addr), "r"(cta_id), "r"(pred), "r"(transaction_bytes)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // Performs an expected transaction bytes increment without doing an arrive operation + CUTLASS_DEVICE + static void expect_transaction(ValueType const* smem_ptr, uint32_t transaction_bytes) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + asm volatile( + "{\n\t" + "mbarrier.expect_tx.shared.b64 [%1], %0; \n\t" + "}" + : + : "r"(transaction_bytes), "r"(smem_addr)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // Performs an expected transaction bytes decrement without doing an arrive operation + CUTLASS_DEVICE + static void complete_transaction( + ValueType const* smem_ptr, uint32_t dst_cta_id, uint32_t transaction_bytes, uint32_t pred = 1) { +#if CUDA_BARRIER_ENABLED + uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr); + smem_addr = cute::set_block_rank(smem_addr, dst_cta_id); + asm volatile( + "{\n\t" + ".reg .pred p;\n\t" + "setp.eq.u32 p, %2, 1;\n\t" + "@p mbarrier.complete_tx.shared::cluster.relaxed.cluster.b64 [%1], %0;" + "}" + : + : "r"(transaction_bytes), "r"(smem_addr), "r"(pred)); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif + } + + // + // DEPRECATED APIs + // + [[deprecated("Use arrive_and_expect_tx instead")]] CUTLASS_DEVICE + void arrive_and_reset_bytes(uint32_t transaction_bytes) const { + arrive_and_expect_tx(transaction_bytes); + } + [[deprecated("Use arrive_and_expect_tx instead")]] CUTLASS_DEVICE + void arrive_and_reset_bytes(uint32_t transaction_bytes, uint32_t cta_id) const { + arrive_and_expect_tx(transaction_bytes, cta_id); + } + [[deprecated("Use expect_transaction instead")]] CUTLASS_DEVICE + void reset_bytes(uint32_t transaction_bytes) const { + expect_transaction(transaction_bytes); + } + [[deprecated("Use complete_transaction instead")]] CUTLASS_DEVICE + void commit(uint32_t transaction_bytes, uint32_t pred = 1) const { + complete_transaction(transaction_bytes, pred); + } + [[deprecated("Use complete_transaction instead")]] CUTLASS_DEVICE + void commit(uint32_t dst_cta_id, uint32_t transaction_bytes, uint32_t pred) const { + complete_transaction(dst_cta_id, transaction_bytes, pred); + } + [[deprecated("Use arrive_and_expect_tx instead")]] CUTLASS_DEVICE + static void arrive_and_reset_bytes(ValueType const* smem_ptr, uint32_t transaction_bytes) { + arrive_and_expect_tx(smem_ptr, transaction_bytes); + } + [[deprecated("Use arrive_and_expect_tx instead")]] CUTLASS_DEVICE + static void arrive_and_reset_bytes(ValueType const* smem_ptr, uint32_t transaction_bytes, uint32_t cta_id, uint32_t pred) { + arrive_and_expect_tx(smem_ptr, transaction_bytes, cta_id, pred); + } + [[deprecated("Use expect_transaction instead")]] CUTLASS_DEVICE + static void reset_bytes(ValueType const* smem_ptr, uint32_t transaction_bytes) { + expect_transaction(smem_ptr, transaction_bytes); + } + [[deprecated("Use complete_transaction instead")]] CUTLASS_DEVICE + static void commit(ValueType const* smem_ptr, uint32_t dst_cta_id, uint32_t transaction_bytes, uint32_t pred = 1) { + complete_transaction(smem_ptr, dst_cta_id, transaction_bytes, pred); + } +}; + +// Helps with visibility of barrier init operations across warps / cta / cluster +// Available as a separate function so as to batch inits across barriers and fence once +// Note : It must be composed with an appropriate sync instruction with the right scope +// to ensure visibility eg. __syncthreads() or a cluster_arrive() + cluster_wait() +CUTLASS_DEVICE +void fence_barrier_init() { +#if CUDA_BARRIER_ENABLED + asm volatile( + "{\n\t" + "fence.mbarrier_init.release.cluster; \n" + "}" + ::); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif +} + +// Issue a shared memory fence for async operations +CUTLASS_DEVICE +void fence_view_async_shared() { +#if CUDA_BARRIER_ENABLED + asm volatile ( + "{\n\t" + "fence.proxy.async.shared::cta; \n" + "}" + ::); +#elif defined(__HIP_DEVICE_COMPILE__) + asm volatile ("brkpt;\n" ::); +#endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +} // end namespace arch +} // end namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/memory_sm75_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/memory_sm75_hip.h new file mode 100644 index 000000000..5307cbbf5 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/memory_sm75_hip.h @@ -0,0 +1,336 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Architecture-specific operators on memory added for SM75 +*/ + +#pragma once + +#include "cutlass/array_hip.h" +#include "cutlass/layout/matrix_hip.h" +#include "cute/arch/copy_sm75_hip.hpp" +#include "cute/arch/util_hip.hpp" + +namespace cutlass { +namespace arch { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template < + /// Layout of destination matrix (column-major implies transpose) + typename Layout, + /// .x1, .x2, or .x4 + int MatrixCount +> +inline __device__ void ldsm(Array & D, void const* ptr); + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Determine the appropriate way to target PTX's "ldmatrix" instruction. +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// CUTLASS helper to get SMEM pointer +inline __device__ unsigned cutlass_get_smem_pointer(void *ptr) { + return cute::cast_smem_ptr_to_uint(ptr); +} + +/// CUTLASS helper to get SMEM pointer +inline __device__ unsigned cutlass_get_smem_pointer(void const *ptr) { + return cutlass_get_smem_pointer(const_cast(ptr)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x; + asm volatile ("ldmatrix.sync.aligned.x1.m8n8.shared.b16 {%0}, [%1];" : "=r"(x) : "r"(addr)); + reinterpret_cast(D) = x; + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x, y; + asm volatile ("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];" : "=r"(x), "=r"(y) : "r"(addr)); + reinterpret_cast(D) = make_int2(x, y); + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x, y, z, w; + asm volatile ("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];" : "=r"(x), "=r"(y), "=r"(z), "=r"(w) : "r"(addr)); + reinterpret_cast(D) = make_int4(x, y, z, w); + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Transpose on 16b granularity +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x; + asm volatile ("ldmatrix.sync.aligned.x1.trans.m8n8.shared.b16 {%0}, [%1];" : "=r"(x) : "r"(addr)); + reinterpret_cast(D) = x; + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x, y; + asm volatile ("ldmatrix.sync.aligned.x2.trans.m8n8.shared.b16 {%0, %1}, [%2];" : "=r"(x), "=r"(y) : "r"(addr)); + reinterpret_cast(D) = make_int2(x, y); + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +inline __device__ void ldsm( + Array & D, + void const* ptr) { + + #if defined(CUTE_ARCH_LDSM_SM75_ACTIVATED) + + unsigned addr = cutlass_get_smem_pointer(ptr); + + int x, y, z, w; + asm volatile ("ldmatrix.sync.aligned.x4.trans.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];" : "=r"(x), "=r"(y), "=r"(z), "=r"(w) : "r"(addr)); + reinterpret_cast(D) = make_int4(x, y, z, w); + + #else + + CUTLASS_UNUSED(D); + CUTLASS_UNUSED(ptr); + CUTLASS_NOT_IMPLEMENTED(); + + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_load_op { + CUTLASS_DEVICE + shared_load_op(AccessType &D, void const *ptr) { + D = *reinterpret_cast(ptr); + } +}; + +template +CUTLASS_DEVICE void shared_load(AccessType &D, void const *ptr) { + shared_load_op(D, ptr); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_load_op { + CUTLASS_DEVICE + shared_load_op(AccessType &D, void const *ptr) { +#ifndef DCU_ASM + unsigned addr = cutlass_get_smem_pointer(ptr); + + uint4 v; + asm volatile ("ld.shared.v4.b32 {%0, %1, %2, %3}, [%4];" : + "=r"(v.x), "=r"(v.y), "=r"(v.z), "=r"(v.w) : "r"(addr)); +#else + uint4 const *mptr = reinterpret_cast(ptr); + uint4 v = *mptr; +#endif + D = reinterpret_cast(v); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_load_op { + CUTLASS_DEVICE + shared_load_op(AccessType &D, void const *ptr) { +#ifndef DCU_ASM + unsigned addr = cutlass_get_smem_pointer(ptr); + + uint2 v; + asm volatile ("ld.shared.v2.b32 {%0, %1}, [%2];" : + "=r"(v.x), "=r"(v.y) : "r"(addr)); +#else + uint2 const *mptr = reinterpret_cast(ptr); + uint2 v = *mptr; +#endif + D = reinterpret_cast(v); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_store_op { + CUTLASS_DEVICE + shared_store_op(AccessType &D, void *ptr) { + *reinterpret_cast(ptr) = D; + } +}; + +template +CUTLASS_DEVICE void shared_store(AccessType &D, void *ptr) { + shared_store_op(D, ptr); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_store_op { + CUTLASS_DEVICE + shared_store_op(AccessType &D, void *ptr) { + uint4 v = reinterpret_cast(D); +#ifndef DCU_ASM + unsigned addr = cutlass_get_smem_pointer(ptr); + + asm volatile ("st.shared.v4.u32 [%0], {%1, %2, %3, %4};" : : + "r"(addr), "r"(v.x), "r"(v.y), "r"(v.z), "r"(v.w)); +#else + uint4 *mptr = reinterpret_cast(ptr); + *mptr = v; +#endif + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct shared_store_op { + CUTLASS_DEVICE + shared_store_op(AccessType &D, void *ptr) { + uint2 v = reinterpret_cast(D); +#ifndef DCU_ASM + unsigned addr = cutlass_get_smem_pointer(ptr); + + asm volatile ("st.shared.v2.u32 [%0], {%1, %2};" : : + "r"(addr), "r"(v.x), "r"(v.y)); +#else + uint2 *mptr = reinterpret_cast(ptr); + *mptr = v; +#endif + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace arch +} // namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_hip.h new file mode 100644 index 000000000..b036e48bd --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_hip.h @@ -0,0 +1,2594 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Statically sized array of elements that accommodates all CUTLASS-supported numeric types + and is safe to use in a union. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once +#include "cutlass/cutlass.h" +#include "cutlass/functional.h" +#include "cutlass/numeric_size.h" +#include "cutlass/half.h" +#include "cutlass/integer_subbyte.h" +#include "cutlass/tfloat32.h" +#include "cutlass/bfloat16.h" +#include "cutlass/half.h" +namespace cutlass { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Statically sized array for any data type +template < + typename T, + int N, + bool RegisterSized = sizeof_bits::value >= 32 +> +class Array; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Defines the size of an Array<> in bits +template +struct sizeof_bits > { + static int const value = + int(sizeof(typename Array::Storage)) * 8 * int(Array::kStorageElements); +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Returns true if the argument is a power of 2 +CUTLASS_HOST_DEVICE +constexpr bool ispow2(unsigned x) { + return x && (!(x & (x - 1))); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Returns the largest power of two not greater than the argument. +CUTLASS_HOST_DEVICE +constexpr unsigned floor_pow_2(unsigned x) { + return (x == 0 || ispow2(x)) ? x : ((floor_pow_2(x >> 1)) << 1); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Statically sized array for any data type +template < + typename T, + int N +> +class Array { +public: + + /// Storage type + using Storage = T; + + /// Element type + using Element = T; + + /// Number of storage elements + //static std::size_t const kStorageElements = N; + static size_t const kStorageElements = N; + + /// Number of logical elements + static size_t const kElements = N; + + // + // C++ standard members + // + + typedef T value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef value_type &reference; + typedef value_type const & const_reference; + typedef value_type *pointer; + typedef value_type const * const_pointer; + + // + // Iterators + // + + /// Bidirectional iterator over elements + class iterator { + + /// Pointer to object + T *ptr_; + + public: + + CUTLASS_HOST_DEVICE + iterator(): ptr_(nullptr) { } + + CUTLASS_HOST_DEVICE + iterator(T *_ptr): ptr_(_ptr) { } + + CUTLASS_HOST_DEVICE + iterator &operator++() { + ++ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + iterator &operator--() { + --ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + iterator operator++(int) { + iterator ret(*this); + ++ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + iterator operator--(int) { + iterator ret(*this); + --ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + T &operator*() const { + return *ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator==(iterator const &other) const { + return ptr_ == other.ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(iterator const &other) const { + return ptr_ != other.ptr_; + } + }; + + /// Bidirectional constant iterator over elements + class const_iterator { + + /// Pointer to object + const T *ptr_; + + public: + + CUTLASS_HOST_DEVICE + const_iterator(): ptr_(nullptr) { } + + CUTLASS_HOST_DEVICE + const_iterator(T const *_ptr): ptr_(_ptr) { } + + CUTLASS_HOST_DEVICE + const_iterator &operator++() { + ++ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + const_iterator &operator--() { + --ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + const_iterator operator++(int) { + const_iterator ret(*this); + ++ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + const_iterator operator--(int) { + const_iterator ret(*this); + --ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + T const &operator*() const { + return *ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator==(const_iterator const &other) const { + return ptr_ == other.ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(const_iterator const &other) const { + return ptr_ != other.ptr_; + } + }; + + /// Bidirectional iterator over elements + class reverse_iterator { + + /// Pointer to object + T *ptr_; + + public: + + CUTLASS_HOST_DEVICE + reverse_iterator(): ptr_(nullptr) { } + + CUTLASS_HOST_DEVICE + reverse_iterator(T *_ptr): ptr_(_ptr) { } + + CUTLASS_HOST_DEVICE + reverse_iterator &operator++() { + --ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + reverse_iterator &operator--() { + ++ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + reverse_iterator operator++(int) { + iterator ret(*this); + --ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + reverse_iterator operator--(int) { + iterator ret(*this); + ++ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + T &operator*() const { + return *(ptr_ - 1); + } + + CUTLASS_HOST_DEVICE + bool operator==(reverse_iterator const &other) const { + return ptr_ == other.ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(reverse_iterator const &other) const { + return ptr_ != other.ptr_; + } + }; + + /// Bidirectional constant iterator over elements + class const_reverse_iterator { + + /// Pointer to object + T const *ptr_; + + public: + + CUTLASS_HOST_DEVICE + const_reverse_iterator(): ptr_(nullptr) { } + + CUTLASS_HOST_DEVICE + const_reverse_iterator(T const *_ptr): ptr_(_ptr) { } + + CUTLASS_HOST_DEVICE + const_reverse_iterator &operator++() { + --ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator &operator--() { + ++ptr_; + return *this; + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator operator++(int) { + const_reverse_iterator ret(*this); + --ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator operator--(int) { + const_reverse_iterator ret(*this); + ++ptr_; + return ret; + } + + CUTLASS_HOST_DEVICE + T const &operator*() const { + return *(ptr_ - 1); + } + + CUTLASS_HOST_DEVICE + bool operator==(const_iterator const &other) const { + return ptr_ == other.ptr_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(const_iterator const &other) const { + return ptr_ != other.ptr_; + } + }; + +private: + + /// Internal storage + Storage storage[kElements]; + +public: + + #if 0 + CUTLASS_HOST_DEVICE + Array() { } + + CUTLASS_HOST_DEVICE + Array(Array const &x) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < kElements; ++i) { + storage[i] = x.storage[i]; + } + } + #endif + + /// Efficient clear method + CUTLASS_HOST_DEVICE + void clear() { + fill(T(0)); + } + + CUTLASS_HOST_DEVICE + reference at(size_type pos) { + return reinterpret_cast(storage[pos]); + } + + CUTLASS_HOST_DEVICE + const_reference at(size_type pos) const { + return reinterpret_cast(storage[pos]); + } + + CUTLASS_HOST_DEVICE + reference operator[](size_type pos) { + return reinterpret_cast(storage[pos]); + } + + CUTLASS_HOST_DEVICE + const_reference operator[](size_type pos) const { + return reinterpret_cast(storage[pos]); + } + + CUTLASS_HOST_DEVICE + reference front() { + return reinterpret_cast(storage[0]); + } + + CUTLASS_HOST_DEVICE + const_reference front() const { + return reinterpret_cast(storage[0]); + } + + CUTLASS_HOST_DEVICE + reference back() { + return reinterpret_cast(storage[kStorageElements - 1]); + } + + CUTLASS_HOST_DEVICE + const_reference back() const { + return reinterpret_cast(storage[kStorageElements - 1]); + } + + CUTLASS_HOST_DEVICE + pointer data() { + return reinterpret_cast(storage); + } + + CUTLASS_HOST_DEVICE + const_pointer data() const { + return reinterpret_cast(storage); + } + + CUTLASS_HOST_DEVICE + pointer raw_data() { + return reinterpret_cast(storage); + } + + CUTLASS_HOST_DEVICE + const_pointer raw_data() const { + return reinterpret_cast(storage); + } + + + CUTLASS_HOST_DEVICE + constexpr bool empty() const { + return !kElements; + } + + CUTLASS_HOST_DEVICE + constexpr size_type size() const { + return kElements; + } + + CUTLASS_HOST_DEVICE + constexpr size_type max_size() const { + return kElements; + } + + CUTLASS_HOST_DEVICE + void fill(T const &value) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < kElements; ++i) { + storage[i] = static_cast(value); + } + } + + CUTLASS_HOST_DEVICE + iterator begin() { + return iterator(storage); + } + + CUTLASS_HOST_DEVICE + const_iterator begin() const { + return cbegin(); + } + + CUTLASS_HOST_DEVICE + const_iterator cbegin() const { + return const_iterator(storage); + } + + CUTLASS_HOST_DEVICE + iterator end() { + return iterator(reinterpret_cast(storage + kStorageElements)); + } + + CUTLASS_HOST_DEVICE + const_iterator end() const { + return cend(); + } + + CUTLASS_HOST_DEVICE + const_iterator cend() const { + return const_iterator(reinterpret_cast(storage + kStorageElements)); + } + + CUTLASS_HOST_DEVICE + reverse_iterator rbegin() { + return reverse_iterator(reinterpret_cast(storage + kStorageElements)); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator rbegin() const { + return crbegin(); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator crbegin() const { + return const_reverse_iterator(reinterpret_cast(storage + kStorageElements)); + } + + CUTLASS_HOST_DEVICE + reverse_iterator rend() { + return reverse_iterator(reinterpret_cast(storage)); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator rend() const { + return crend(); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator crend() const { + return const_reverse_iterator(reinterpret_cast(storage)); + } + + // + // Comparison operators + // + +}; + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Factories +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +CUTLASS_HOST_DEVICE +Array make_Array(Element x) { + Array m; + m[0] = x; + return m; +} + +template +CUTLASS_HOST_DEVICE +Array make_Array(Element x, Element y) { + Array m; + m[0] = x; + m[1] = y; + return m; +} + +template +CUTLASS_HOST_DEVICE +Array make_Array(Element x, Element y, Element z) { + Array m; + m[0] = x; + m[1] = y; + m[2] = z; + return m; +} + +template +CUTLASS_HOST_DEVICE +Array make_Array(Element x, Element y, Element z, Element w) { + Array m; + m[0] = x; + m[1] = y; + m[2] = z; + m[3] = w; + return m; +} + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// functional.h numeric specializations +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct absolute_value_op< Array > { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs) const { + + Array result; + absolute_value_op scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i]); + } + + return result; + } +}; + +template +struct plus> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + plus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + plus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( T const &scalar, Array const &rhs) const { + + Array result; + plus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; +template +struct minus> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + minus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + minus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( T const &scalar, Array const &rhs) const { + + Array result; + minus scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct multiplies> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + multiplies scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + multiplies scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( T const &scalar, Array const &rhs) const { + + Array result; + multiplies scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct scale> { + T const scaling_factor_; + + CUTLASS_HOST_DEVICE + scale(T scaling_factor) : scaling_factor_(scaling_factor) { + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & rhs) const { + Array result; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = rhs[i] * scaling_factor_; + } + + return result; + } +}; + +template +struct divides> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + divides scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + divides scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( T const &scalar, Array const &rhs) const { + + Array result; + divides scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct maximum, false> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &rhs) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct maximum, true> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &rhs) const { + + Array result; + maximum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct minimum, false> { + + CUTLASS_HOST_DEVICE + static T scalar_op(T const &lhs, T const &rhs) { + return (rhs < lhs ? rhs : lhs); + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &rhs) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct minimum, true> { + + CUTLASS_HOST_DEVICE + static T scalar_op(T const &lhs, T const &rhs) { + return (rhs < lhs ? rhs : lhs); + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, Array const &rhs) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], rhs[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs, T const &scalar) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i], scalar); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &rhs) const { + + Array result; + minimum scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, rhs[i]); + } + + return result; + } +}; + +template +struct negate> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &lhs) const { + + Array result; + negate scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(lhs[i]); + } + + return result; + } +}; + +/// Fused multiply-add +template +struct multiply_add, Array, Array> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, Array const &b, Array const &c) const { + + Array result; + multiply_add scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(a[i], b[i], c[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, T const &scalar, Array const &c) const { + + Array result; + multiply_add scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(a[i], scalar, c[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &b, Array const &c) const { + + Array result; + multiply_add scalar_op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = scalar_op(scalar, b[i], c[i]); + } + + return result; + } +}; + +/// Fused multiply-add-relu0 +template +struct multiply_add_relu0, Array, Array> { + + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, Array const &b, Array const &c) const { + + Array result; + multiply_add scalar_op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(scalar_op(a[i], b[i], c[i]), T(0)); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, T const &scalar, Array const &c) const { + + Array result; + multiply_add scalar_op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(scalar_op(a[i], scalar, c[i]), T(0)); + } + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(T const &scalar, Array const &b, Array const &c) const { + + Array result; + multiply_add scalar_op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(scalar_op(scalar, b[i], c[i]), T(0)); + } + + return result; + } +}; + + +template +struct conjugate > { + CUTLASS_HOST_DEVICE + Array operator()(Array const &a) const { + + conjugate conj_op; + + Array ca; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + ca[i] = conj_op(a[i]); + } + return ca; + } +}; + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// functional.h numeric specializations targeting SIMD instructions in device code. +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct plus> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hadd2(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + __half d_residual = __hadd(a_residual_ptr[N - 1], b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] + rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hadd2(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + __half d_residual = __hadd(reinterpret_cast<__half const &>(lhs), b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs + rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hadd2(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half d_residual = __hadd(a_residual_ptr[N - 1], reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] + rhs; + } + #endif + + return result; + } +}; + +template +struct minus> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hsub2(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + __half d_residual = __hsub(a_residual_ptr[N - 1], b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] - rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hsub2(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + __half d_residual = __hsub(reinterpret_cast<__half const &>(lhs), b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs - rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hsub2(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half d_residual = __hsub(a_residual_ptr[N - 1], reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] - rhs; + } + #endif + + return result; + } +}; + +template +struct multiplies> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmul2(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + __half d_residual = __hmul(a_residual_ptr[N - 1], b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] * rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmul2(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hmul( + reinterpret_cast<__half const &>(lhs), + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs * rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmul2(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + + __half d_residual = __hmul( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] * rhs; + } + #endif + + return result; + } +}; + +template +struct divides> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __h2div(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hdiv( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] / rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __h2div(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hdiv( + reinterpret_cast<__half const &>(lhs), + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs / rhs[i]; + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __h2div(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + + __half d_residual = __hdiv( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = lhs[i] / rhs; + } + #endif + + return result; + } +}; + +template +struct negate> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *source_ptr = reinterpret_cast<__half2 const *>(&lhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hneg2(source_ptr[i]); + } + + if constexpr (N % 2) { + half_t x = -lhs[N - 1]; + __half lhs_val = reinterpret_cast<__half const &>(x); + result[N - 1] = reinterpret_cast(lhs_val); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = -lhs[i]; + } + #endif + + return result; + } +}; + +/// Fused multiply-add +template +struct multiply_add, Array, Array> { + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2(a_ptr[i], b_ptr[i], c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + + __half d_residual = __hfma( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1], + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b[i], c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + half_t const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 a_pair = __half2half2(reinterpret_cast<__half const &>(a)); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2(a_pair, b_ptr[i], c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + __half d_residual = __hfma( + reinterpret_cast<__half const &>(a), + b_residual_ptr[N - 1], + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a, b[i], c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + half_t const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 b_pair = __half2half2(reinterpret_cast<__half const &>(b)); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2(a_ptr[i], b_pair, c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + + __half d_residual = __hfma( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(b), + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b, c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + half_t const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 c_pair = __half2half2(reinterpret_cast<__half const &>(c)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2(a_ptr[i], b_ptr[i], c_pair); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + + __half d_residual = __hfma( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1], + reinterpret_cast<__half const &>(c)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b[i], c); + } + #endif + + return result; + } +}; + +/// Fused multiply-add-relu0 +template +struct multiply_add_relu0, Array, Array> { + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2_relu(a_ptr[i], b_ptr[i], c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + + __half d_residual = __hfma_relu( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1], + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(op(a[i], b[i], c[i]), (half_t)0); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + half_t const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 a_pair = __half2half2(reinterpret_cast<__half const &>(a)); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2_relu(a_pair, b_ptr[i], c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + __half d_residual = __hfma_relu( + reinterpret_cast<__half const &>(a), + b_residual_ptr[N - 1], + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(op(a, b[i], c[i]), half_t(0)); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + half_t const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 b_pair = __half2half2(reinterpret_cast<__half const &>(b)); + __half2 const *c_ptr = reinterpret_cast<__half2 const *>(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2_relu(a_ptr[i], b_pair, c_ptr[i]); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *c_residual_ptr = reinterpret_cast<__half const *>(&c); + + __half d_residual = __hfma_relu( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(b), + c_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(op(a[i], b, c[i]), half_t(0)); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + half_t const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *a_ptr = reinterpret_cast<__half2 const *>(&a); + __half2 const *b_ptr = reinterpret_cast<__half2 const *>(&b); + __half2 c_pair = __half2half2(reinterpret_cast<__half const &>(c)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hfma2_relu(a_ptr[i], b_ptr[i], c_pair); + } + + if constexpr (N % 2) { + + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&a); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&b); + + __half d_residual = __hfma_relu( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1], + reinterpret_cast<__half const &>(c)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + multiply_add op; + maximum mx; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = mx(op(a[i], b[i], c), half_t(0)); + } + #endif + + return result; + } +}; + +template +struct minimum, false> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmin2(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hmin( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (rhs[i] < lhs[i] ? rhs[i] : lhs[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmin2(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hmin( + reinterpret_cast<__half const &>(lhs), + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (rhs[i] < lhs ? rhs[i] : lhs); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmin2(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + + __half d_residual = __hmin( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (rhs < lhs[i] ? rhs : lhs[i]); + } + #endif + + return result; + } +}; + +template +struct maximum, false> { + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmax2(lhs_ptr[i], rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hmax( + a_residual_ptr[N - 1], + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (lhs[i] < rhs[i] ? rhs[i] : lhs[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(half_t const & lhs, Array const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 lhs_pair = __half2half2(reinterpret_cast<__half const &>(lhs)); + __half2 const *rhs_ptr = reinterpret_cast<__half2 const *>(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmax2(lhs_pair, rhs_ptr[i]); + } + + if constexpr (N % 2) { + __half const *b_residual_ptr = reinterpret_cast<__half const *>(&rhs); + + __half d_residual = __hmax( + reinterpret_cast<__half const &>(lhs), + b_residual_ptr[N - 1]); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (lhs < rhs[i] ? rhs[i] : lhs); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()(Array const & lhs, half_t const &rhs) const { + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + __half2 *result_ptr = reinterpret_cast<__half2 *>(&result); + __half2 const *lhs_ptr = reinterpret_cast<__half2 const *>(&lhs); + __half2 rhs_pair = __half2half2(reinterpret_cast<__half const &>(rhs)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = __hmax2(lhs_ptr[i], rhs_pair); + } + + if constexpr (N % 2) { + __half const *a_residual_ptr = reinterpret_cast<__half const *>(&lhs); + + __half d_residual = __hmax( + a_residual_ptr[N - 1], + reinterpret_cast<__half const &>(rhs)); + + result[N - 1] = reinterpret_cast(d_residual); + } + + #else + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = (lhs[i] < rhs ? rhs : lhs[i]); + } + #endif + + return result; + } +}; + +/// Fused multiply-add +template +struct multiply_add, Array, Array> { + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + unsigned *result_ptr = reinterpret_cast(&result); + unsigned const *a_ptr = reinterpret_cast(&a); + unsigned const *b_ptr = reinterpret_cast(&b); + unsigned const *c_ptr = reinterpret_cast(&c); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + asm ("fma.rn.bf16x2 %0, %1, %2, %3;\n" + : "=r"(result_ptr[i]) + : "r"(a_ptr[i]), "r"(b_ptr[i]), "r"(c_ptr[i]) + ); + } + + if constexpr (N % 2) { + + uint16_t *result_ptr = reinterpret_cast(&result); + uint16_t const *a_residual_ptr = reinterpret_cast(&a); + uint16_t const *b_residual_ptr = reinterpret_cast(&b); + uint16_t const *c_residual_ptr = reinterpret_cast(&c); + + asm ("fma.rn.bf16 %0, %1, %2, %3;\n" + : "=h"(result_ptr[N - 1]) + : "h"(a_residual_ptr[N - 1]), "h"(b_residual_ptr[N - 1]), "h"(c_residual_ptr[N - 1]) + ); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b[i], c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + bfloat16_t const &a, + Array const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + unsigned *result_ptr = reinterpret_cast(&result); + + unsigned const *b_ptr = reinterpret_cast(&b); + unsigned const *c_ptr = reinterpret_cast(&c); + + unsigned a_packed = static_cast(a.raw()); + a_packed = (a_packed | (a_packed << 16)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + asm ("fma.rn.bf16x2 %0, %1, %2, %3;\n" + : "=r"(result_ptr[i]) + : "r"(a_packed), "r"(b_ptr[i]), "r"(c_ptr[i]) + ); + } + + if constexpr (N % 2) { + + uint16_t *result_ptr = reinterpret_cast(&result); + uint16_t const *a_residual_ptr = reinterpret_cast(&a); + uint16_t const *b_residual_ptr = reinterpret_cast(&b); + uint16_t const *c_residual_ptr = reinterpret_cast(&c); + + asm ("fma.rn.bf16 %0, %1, %2, %3;\n" + : "=h"(result_ptr[N - 1]) + : "h"(a_residual_ptr[0]), "h"(b_residual_ptr[N - 1]), "h"(c_residual_ptr[N - 1]) + ); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a, b[i], c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + bfloat16_t const &b, + Array const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + unsigned *result_ptr = reinterpret_cast(&result); + + unsigned const *a_ptr = reinterpret_cast(&a); + unsigned const *c_ptr = reinterpret_cast(&c); + + unsigned b_packed = static_cast(b.raw()); + b_packed = (b_packed | (b_packed << 16)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + asm ("fma.rn.bf16x2 %0, %1, %2, %3;\n" + : "=r"(result_ptr[i]) + : "r"(a_ptr[i]), "r"(b_packed), "r"(c_ptr[i]) + ); + } + + if constexpr (N % 2) { + + uint16_t *result_ptr = reinterpret_cast(&result); + uint16_t const *a_residual_ptr = reinterpret_cast(&a); + uint16_t const *b_residual_ptr = reinterpret_cast(&b); + uint16_t const *c_residual_ptr = reinterpret_cast(&c); + + asm ("fma.rn.bf16 %0, %1, %2, %3;\n" + : "=h"(result_ptr[N - 1]) + : "h"(a_residual_ptr[N - 1]), "h"(b_residual_ptr[0]), "h"(c_residual_ptr[N - 1]) + ); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b, c[i]); + } + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + Array operator()( + Array const &a, + Array const &b, + bfloat16_t const &c) const { + + Array result; + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + + unsigned *result_ptr = reinterpret_cast(&result); + + unsigned const *a_ptr = reinterpret_cast(&a); + unsigned const *b_ptr = reinterpret_cast(&b); + + unsigned c_packed = static_cast(c.raw()); + c_packed = (c_packed | (c_packed << 16)); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + asm ("fma.rn.bf16x2 %0, %1, %2, %3;\n" + : "=r"(result_ptr[i]) + : "r"(a_ptr[i]), "r"(b_ptr[i]), "r"(c_packed) + ); + } + + if constexpr (N % 2) { + + uint16_t *result_ptr = reinterpret_cast(&result); + uint16_t const *a_residual_ptr = reinterpret_cast(&a); + uint16_t const *b_residual_ptr = reinterpret_cast(&b); + uint16_t const *c_residual_ptr = reinterpret_cast(&c); + + asm ("fma.rn.bf16 %0, %1, %2, %3;\n" + : "=h"(result_ptr[N - 1]) + : "h"(a_residual_ptr[N - 1]), "h"(b_residual_ptr[N - 1]), "h"(c_residual_ptr[0]) + ); + } + + #else + + multiply_add op; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = op(a[i], b[i], c); + } + #endif + + return result; + } +}; + + +/// bit_and +template +struct bit_and> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, Array const &b) const { + using ArrayType = Array; + using Storage = typename ArrayType::Storage; + ArrayType result; + + Storage *result_data = result.raw_data(); + Storage const *a_data = a.raw_data(); + Storage const *b_data = b.raw_data(); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ArrayType::kStorageElements; ++i) { + result_data[i] = (a_data[i] & b_data[i]); + } + + return result; + } +}; + + +/// bit_or +template +struct bit_or> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, Array const &b) const { + using ArrayType = Array; + using Storage = typename ArrayType::Storage; + ArrayType result; + + Storage *result_data = result.raw_data(); + Storage const *a_data = a.raw_data(); + Storage const *b_data = b.raw_data(); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ArrayType::kStorageElements; ++i) { + result_data[i] = (a_data[i] | b_data[i]); + } + + return result; + } +}; + + +/// bit_not +template +struct bit_not> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &a) const { + using ArrayType = Array; + using Storage = typename ArrayType::Storage; + ArrayType result; + + Storage *result_data = result.raw_data(); + Storage const *a_data = a.raw_data(); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ArrayType::kStorageElements; ++i) { + result_data[i] = (~a_data[i]); + } + + return result; + } +}; + + +/// bit_xor +template +struct bit_xor> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &a, Array const &b) const { + using ArrayType = Array; + using Storage = typename ArrayType::Storage; + ArrayType result; + + Storage *result_data = result.raw_data(); + Storage const *a_data = a.raw_data(); + Storage const *b_data = b.raw_data(); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ArrayType::kStorageElements; ++i) { + result_data[i] = (a_data[i] ^ b_data[i]); + } + + return result; + } +}; + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// Operator overloads +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +CUTLASS_HOST_DEVICE +Array operator+(Array const &lhs, Array const &rhs) { + plus> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator-(Array const &lhs, Array const &rhs) { + minus> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator-(Array const &lhs) { + negate> op; + return op(lhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator*(Array const &lhs, Array const &rhs) { + multiplies> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator*(T lhs, Array const &rhs) { + multiplies> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator*(Array const &lhs, T rhs) { + multiplies> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array operator/(Array const &lhs, Array const &rhs) { + divides> op; + return op(lhs, rhs); +} + +template +CUTLASS_HOST_DEVICE +Array fma(Array const &a, Array const &b, Array const &c) { + multiply_add> op; + return op(a, b, c); +} + +template +CUTLASS_HOST_DEVICE +Array fma(T a, Array const &b, Array const &c) { + multiply_add> op; + return op(a, b, c); +} + +template +CUTLASS_HOST_DEVICE +Array fma(Array const &a, T b, Array const &c) { + multiply_add> op; + return op(a, b, c); +} + +template +CUTLASS_HOST_DEVICE +Array fma(Array const &a, Array const &b, T c) { + multiply_add> op; + return op(a, b, c); +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// + + + +} // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "cutlass/array_subbyte_hip.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// AlignedArray +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Aligned array type +template < + /// Element type + typename T, + /// Number of elements in the array + int N, + /// Alignment requirement in bytes + int Alignment = sizeof_bits::value * N / 8 +> +class alignas(Alignment) AlignedArray: public Array { +public: + +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_subbyte_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_subbyte_hip.h new file mode 100644 index 000000000..6dc995ef0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/array_subbyte_hip.h @@ -0,0 +1,579 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Statically sized array of elements that accommodates all CUTLASS-supported numeric types + and is safe to use in a union. +*/ +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/array_hip.h" +#include "cutlass/platform/platform.h" + +namespace cutlass { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Statically sized array for any data type +template < + typename T, + int N +> +class Array { +public: + + static int const kSizeBits = sizeof_bits::value * N; + + /// Storage type + using Storage = typename platform::conditional< + ((kSizeBits % 32) != 0), + typename platform::conditional< + ((kSizeBits % 16) != 0), + uint8_t, + uint16_t + >::type, + uint32_t + >::type; + + /// Element type + using Element = T; + + /// Number of logical elements per stored object + static int const kElementsPerStoredItem = int(sizeof(Storage) * 8) / sizeof_bits::value; + + /// Number of storage elements + static size_t const kStorageElements = N / kElementsPerStoredItem; + + /// Number of logical elements + static size_t const kElements = N; + + /// Bitmask for covering one item + static Storage const kMask = ((Storage(1) << sizeof_bits::value) - 1); + + // + // C++ standard members with pointer types removed + // + + typedef T value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef value_type *pointer; + typedef value_type const *const_pointer; + + // + // References + // + + /// Reference object inserts or extracts sub-byte items + class reference { + /// Pointer to storage element + Storage *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + /// Default ctor + CUTLASS_HOST_DEVICE + reference(): ptr_(nullptr), idx_(0) { } + + /// Ctor + CUTLASS_HOST_DEVICE + reference(Storage *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + + /// Assignment + CUTLASS_HOST_DEVICE + reference &operator=(T x) { + Storage item = (reinterpret_cast(x) & kMask); + + Storage kUpdateMask = Storage(~(kMask << (idx_ * sizeof_bits::value))); + *ptr_ = Storage(((*ptr_ & kUpdateMask) | (item << idx_ * sizeof_bits::value))); + + return *this; + } + + CUTLASS_HOST_DEVICE + T get() const { + Storage item = Storage((*ptr_ >> (idx_ * sizeof_bits::value)) & kMask); + return reinterpret_cast(item); + } + + /// Extract + CUTLASS_HOST_DEVICE + operator T() const { + return get(); + } + + /// Explicit cast to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + return int(get()); + } + + /// Explicit cast to float + CUTLASS_HOST_DEVICE + explicit operator float() const { + return float(get()); + } + }; + + /// Reference object extracts sub-byte items + class const_reference { + + /// Pointer to storage element + Storage const *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + /// Default ctor + CUTLASS_HOST_DEVICE + const_reference(): ptr_(nullptr), idx_(0) { } + + /// Ctor + CUTLASS_HOST_DEVICE + const_reference(Storage const *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + + CUTLASS_HOST_DEVICE + const T get() const { + Storage item = (*ptr_ >> (idx_ * sizeof_bits::value)) & kMask; + return reinterpret_cast(item); + } + + /// Extract + CUTLASS_HOST_DEVICE + operator T() const { + Storage item = Storage(Storage(*ptr_ >> Storage(idx_ * sizeof_bits::value)) & kMask); + return reinterpret_cast(item); + } + + /// Explicit cast to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + return int(get()); + } + + /// Explicit cast to float + CUTLASS_HOST_DEVICE + explicit operator float() const { + return float(get()); + } + }; + + // + // Iterators + // + + /// Bidirectional iterator over elements + class iterator { + + /// Pointer to storage element + Storage *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + CUTLASS_HOST_DEVICE + iterator(): ptr_(nullptr), idx_(0) { } + + CUTLASS_HOST_DEVICE + iterator(Storage *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + + CUTLASS_HOST_DEVICE + iterator &operator++() { + ++idx_; + if (idx_ == kElementsPerStoredItem) { + ++ptr_; + idx_ = 0; + } + return *this; + } + + CUTLASS_HOST_DEVICE + iterator &operator--() { + if (!idx_) { + --ptr_; + idx_ = kElementsPerStoredItem - 1; + } + else { + --idx_; + } + return *this; + } + + CUTLASS_HOST_DEVICE + iterator operator++(int) { + iterator ret(*this); + ++idx_; + if (idx_ == kElementsPerStoredItem) { + ++ptr_; + idx_ = 0; + } + return ret; + } + + CUTLASS_HOST_DEVICE + iterator operator--(int) { + iterator ret(*this); + if (!idx_) { + --ptr_; + idx_ = kElementsPerStoredItem - 1; + } + else { + --idx_; + } + return ret; + } + + CUTLASS_HOST_DEVICE + reference operator*() const { + return reference(ptr_, idx_); + } + + CUTLASS_HOST_DEVICE + bool operator==(iterator const &other) const { + return ptr_ == other.ptr_ && idx_ == other.idx_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(iterator const &other) const { + return !(*this == other); + } + }; + + /// Bidirectional constant iterator over elements + class const_iterator { + + /// Pointer to storage element + Storage const *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + CUTLASS_HOST_DEVICE + const_iterator(): ptr_(nullptr), idx_(0) { } + + CUTLASS_HOST_DEVICE + const_iterator(Storage const *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + + CUTLASS_HOST_DEVICE + iterator &operator++() { + ++idx_; + if (idx_ == kElementsPerStoredItem) { + ++ptr_; + idx_ = 0; + } + return *this; + } + + CUTLASS_HOST_DEVICE + iterator &operator--() { + if (!idx_) { + --ptr_; + idx_ = kElementsPerStoredItem - 1; + } + else { + --idx_; + } + return *this; + } + + CUTLASS_HOST_DEVICE + iterator operator++(int) { + iterator ret(*this); + ++idx_; + if (idx_ == kElementsPerStoredItem) { + ++ptr_; + idx_ = 0; + } + return ret; + } + + CUTLASS_HOST_DEVICE + iterator operator--(int) { + iterator ret(*this); + if (!idx_) { + --ptr_; + idx_ = kElementsPerStoredItem - 1; + } + else { + --idx_; + } + return ret; + } + + CUTLASS_HOST_DEVICE + const_reference operator*() const { + return const_reference(ptr_, idx_); + } + + CUTLASS_HOST_DEVICE + bool operator==(iterator const &other) const { + return ptr_ == other.ptr_ && idx_ == other.idx_; + } + + CUTLASS_HOST_DEVICE + bool operator!=(iterator const &other) const { + return !(*this == other); + } + }; + + /// Bidirectional iterator over elements + class reverse_iterator { + + /// Pointer to storage element + Storage *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + CUTLASS_HOST_DEVICE + reverse_iterator(): ptr_(nullptr), idx_(0) { } + + CUTLASS_HOST_DEVICE + reverse_iterator(Storage *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + }; + + /// Bidirectional constant iterator over elements + class const_reverse_iterator { + + /// Pointer to storage element + Storage const *ptr_; + + /// Index into elements packed into Storage object + int idx_; + + public: + + CUTLASS_HOST_DEVICE + const_reverse_iterator(): ptr_(nullptr), idx_(0) { } + + CUTLASS_HOST_DEVICE + const_reverse_iterator(Storage const *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } + }; + +private: + + /// Internal storage + Storage storage[kStorageElements]; + +public: + + #if 0 + CUTLASS_HOST_DEVICE + Array() { } + + CUTLASS_HOST_DEVICE + Array(Array const &x) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < int(kStorageElements); ++i) { + storage[i] = x.storage[i]; + } + } + #endif + + /// Efficient clear method + CUTLASS_HOST_DEVICE + void clear() { + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < int(kStorageElements); ++i) { + storage[i] = Storage(0); + } + } + + CUTLASS_HOST_DEVICE + reference at(size_type pos) { + return reference(storage + pos / kElementsPerStoredItem, pos % kElementsPerStoredItem); + } + + CUTLASS_HOST_DEVICE + const_reference at(size_type pos) const { + return const_reference(storage + pos / kElementsPerStoredItem, pos % kElementsPerStoredItem); + } + + CUTLASS_HOST_DEVICE + reference operator[](size_type pos) { + return at(pos); + } + + CUTLASS_HOST_DEVICE + const_reference operator[](size_type pos) const { + return at(pos); + } + + CUTLASS_HOST_DEVICE + reference front() { + return at(0); + } + + CUTLASS_HOST_DEVICE + const_reference front() const { + return at(0); + } + + CUTLASS_HOST_DEVICE + reference back() { + return reference(storage + kStorageElements - 1, kElementsPerStoredItem - 1); + } + + CUTLASS_HOST_DEVICE + const_reference back() const { + return const_reference(storage + kStorageElements - 1, kElementsPerStoredItem - 1); + } + + CUTLASS_HOST_DEVICE + pointer data() { + return reinterpret_cast(storage); + } + + CUTLASS_HOST_DEVICE + const_pointer data() const { + return reinterpret_cast(storage); + } + + CUTLASS_HOST_DEVICE + Storage * raw_data() { + return storage; + } + + CUTLASS_HOST_DEVICE + Storage const * raw_data() const { + return storage; + } + + + CUTLASS_HOST_DEVICE + constexpr bool empty() const { + return !kElements; + } + + CUTLASS_HOST_DEVICE + constexpr size_type size() const { + return kElements; + } + + CUTLASS_HOST_DEVICE + constexpr size_type max_size() const { + return kElements; + } + + CUTLASS_HOST_DEVICE + void fill(T const &value) { + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < kElementsPerStoredItem; ++i) { + reference ref(storage, i); + ref = value; + } + + CUTLASS_PRAGMA_UNROLL + for (int i = 1; i < kStorageElements; ++i) { + storage[i] = storage[0]; + } + } + + CUTLASS_HOST_DEVICE + iterator begin() { + return iterator(storage); + } + + CUTLASS_HOST_DEVICE + const_iterator cbegin() const { + return const_iterator(storage); + } + + CUTLASS_HOST_DEVICE + iterator end() { + return iterator(storage + kStorageElements); + } + + CUTLASS_HOST_DEVICE + const_iterator cend() const { + return const_iterator(storage + kStorageElements); + } + + CUTLASS_HOST_DEVICE + reverse_iterator rbegin() { + return reverse_iterator(storage + kStorageElements); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator crbegin() const { + return const_reverse_iterator(storage + kStorageElements); + } + + CUTLASS_HOST_DEVICE + reverse_iterator rend() { + return reverse_iterator(storage); + } + + CUTLASS_HOST_DEVICE + const_reverse_iterator crend() const { + return const_reverse_iterator(storage); + } + + // + // Comparison operators + // + +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/bfloat16.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/bfloat16.h new file mode 100644 index 000000000..a8e838c34 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/bfloat16.h @@ -0,0 +1,539 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines a proxy class for storing non-standard 16-bit floating point values with + 8 bits of exponent and 7 bit of mantissa. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#if defined(__CUDACC_RTC__) +#include "cutlass/floating_point_nvrtc.h" +#else +#include +#include +#include +#include +#endif + +#include "cutlass/cutlass.h" + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Floating-point type with 8 bits of exponent and 7 bits of mantissa. +struct alignas(2) bfloat16_t { + + // + // Data members + // + + /// Storage type + uint16_t storage; + + // + // Methods + // + + /// Constructs from an unsigned short + CUTLASS_HOST_DEVICE + static bfloat16_t bitcast(uint16_t x) { + bfloat16_t h; + h.storage = x; + return h; + } + + /// Default constructor + bfloat16_t() = default; + + /// Floating-point conversion - round toward nearest + CUTLASS_HOST_DEVICE + explicit bfloat16_t(float x) { + + #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) && (__CUDACC_VER_MAJOR__ >= 11) + + asm("cvt.rn.bf16.f32 %0, %1;\n" : "=h"(storage) : "f"(x)); + + #else + + #if BF162FLOAT_USE_ASM + union + { + float fp32; + uint32_t int32; + } u = {x}; + + static constexpr uint32_t FP32_NAN = 0x7fff0000; + static constexpr uint32_t ROUND_BIAS_FOR_BF16 = 0x7fff; + + using uint32x2_t = uint32_t __attribute__((ext_vector_type(2))); + uint32x2_t check_nan; + uint32_t tmp; + asm volatile("\n \ + v_cmp_u_f32 %0, %2, %2 \n \ + v_bfe_u32 %1, %2, 16, 1 \n \ + v_add3_u32 %1, %2, %1, %3 \n \ + v_cndmask_b32 %2, %1, %4, %0 \n \ + v_lshrrev_b32 %2, 16, %2 \n \ + " + : "=s"(check_nan), "+v"(tmp), "+v"(u.fp32) + : "v"(ROUND_BIAS_FOR_BF16), "v"(FP32_NAN)); + + storage = uint16_t(u.int32); + #else // BF162FLOAT_USE_ASM + uint32_t bits; + + #if defined(__HIP_DEVICE_COMPILE__) + bits = reinterpret_cast(x); + #else + std::memcpy(&bits, &x, sizeof(bits)); + #endif + + if ((bits & 0x7f800000) != 0x7f800000) { + + bool mantissa_bit = ((bits & (1 << 16)) != 0); + bool round_bit = ((bits & (1 << 15)) != 0); + bool sticky_bit = ((bits & ((1 << 15) - 1)) != 0); + + if ((round_bit && sticky_bit) || (round_bit && mantissa_bit)) { + bits += uint32_t(1 << 16); + } + } + else if (bits & ~0xff800000) { + bits = 0x7fffffff; + } + + storage = uint16_t((bits >> 16) & 0xffff); + #endif // BF162FLOAT_USE_ASM + + #endif + } + + /// Floating-point conversion - round toward nearest + CUTLASS_HOST_DEVICE + explicit bfloat16_t(double x): bfloat16_t(float(x)) { + + } + + /// Integer conversion - round toward nearest + CUTLASS_HOST_DEVICE + explicit bfloat16_t(int x) { + float flt = static_cast(x); + uint32_t bits; + + #if defined(__HIP_DEVICE_COMPILE__) + bits = reinterpret_cast(flt); + #else + std::memcpy(&bits, &flt, sizeof(bits)); + #endif + + storage = uint16_t(bits >> 16); + } + + /// Converts to float + CUTLASS_HOST_DEVICE + operator float() const { + unsigned bits = (unsigned(storage) << 16); + #if defined(__HIP_DEVICE_COMPILE__) + return reinterpret_cast(bits); + #else + float flt; + std::memcpy(&flt, &bits, sizeof(flt)); + return flt; + #endif + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator double() const { + return double(float(*this)); + } + + /// Converts to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + return int(float(*this)); + } + + /// Casts to bool + CUTLASS_HOST_DEVICE + explicit operator bool() const { + return (float(*this) != 0.0f); + } + + /// Obtains raw bits + CUTLASS_HOST_DEVICE + uint16_t raw() const { + return storage; + } + /// Returns the sign bit + CUTLASS_HOST_DEVICE + bool signbit() const { + return ((raw() & 0x8000) != 0); + } + + /// Returns the biased exponent + CUTLASS_HOST_DEVICE + int exponent_biased() const { + return int((raw() >> 7) & 0x0ff); + } + + /// Returns the unbiased exponent + CUTLASS_HOST_DEVICE + int exponent() const { + return exponent_biased() - 127; + } + + /// Returns the mantissa + CUTLASS_HOST_DEVICE + int mantissa() const { + return int(raw() & 0x7f); + } +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool signbit(cutlass::bfloat16_t const& h) { + return h.signbit(); +} + +CUTLASS_HOST_DEVICE +cutlass::bfloat16_t abs(cutlass::bfloat16_t const& h) { + return cutlass::bfloat16_t::bitcast(h.raw() & 0x7fffffff); +} + +CUTLASS_HOST_DEVICE +bool isnan(cutlass::bfloat16_t const& h) { + return (h.exponent_biased() == 0x0ff) && h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isfinite(cutlass::bfloat16_t const& h) { + return (h.exponent_biased() != 0x0ff); +} + +CUTLASS_HOST_DEVICE +cutlass::bfloat16_t nan_bf16(const char*) { + // NVIDIA canonical NaN + return cutlass::bfloat16_t::bitcast(0x7fff); +} + +CUTLASS_HOST_DEVICE +bool isinf(cutlass::bfloat16_t const& h) { + return (h.exponent_biased() == 0x0ff) && !h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isnormal(cutlass::bfloat16_t const& h) { + return h.exponent_biased() && h.exponent_biased() != 0x0ff; +} + +CUTLASS_HOST_DEVICE +int fpclassify(cutlass::bfloat16_t const& h) { + int exp = h.exponent_biased(); + int mantissa = h.mantissa(); + if (exp == 0x0ff) { + if (mantissa) { + return FP_NAN; + } + else { + return FP_INFINITE; + } + } + else if (!exp) { + if (mantissa) { + return FP_SUBNORMAL; + } + else { + return FP_ZERO; + } + } + return FP_NORMAL; +} + +CUTLASS_HOST_DEVICE +cutlass::bfloat16_t sqrt(cutlass::bfloat16_t const& h) { +#if defined(__CUDACC_RTC__) + return cutlass::bfloat16_t(sqrtf(float(h))); +#else + return cutlass::bfloat16_t(std::sqrt(float(h))); +#endif +} + +CUTLASS_HOST_DEVICE +bfloat16_t copysign(bfloat16_t const& a, bfloat16_t const& b) { + + uint16_t a_bits; + uint16_t b_bits; + + #if defined(__HIP_DEVICE_COMPILE__) + a_bits = reinterpret_cast(a); + b_bits = reinterpret_cast(b); + #else + std::memcpy(&a_bits, &a, sizeof(a_bits)); + std::memcpy(&b_bits, &b, sizeof(b_bits)); + #endif + + uint16_t a_mag = (a_bits & 0x7fff); + uint16_t b_sign = (b_bits & 0x8000); + uint16_t result = (a_mag | b_sign); + + return bfloat16_t::bitcast(result); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Standard Library operations and definitions +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace std { + +#if !defined(__CUDACC_RTC__) +/// Numeric limits +template <> +struct numeric_limits { + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_infinity = true; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; + static std::float_denorm_style const has_denorm = std::denorm_present; + static bool const has_denorm_loss = true; + static std::float_round_style const round_style = std::round_to_nearest; + static bool const is_iec559 = false; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = 7; + + /// Least positive value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t min() { return cutlass::bfloat16_t::bitcast(0x01); } + + /// Minimum finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t lowest() { return cutlass::bfloat16_t::bitcast(0xff7f); } + + /// Maximum finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t max() { return cutlass::bfloat16_t::bitcast(0x7f7f); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t epsilon() { return cutlass::bfloat16_t::bitcast(0x1000); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t round_error() { return cutlass::bfloat16_t(0.5f); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t infinity() { return cutlass::bfloat16_t::bitcast(0x7f80); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t quiet_NaN() { return cutlass::bfloat16_t::bitcast(0x7fff); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t signaling_NaN() { return cutlass::bfloat16_t::bitcast(0x7fff); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::bfloat16_t denorm_min() { return cutlass::bfloat16_t::bitcast(0x1); } +}; +#endif + +} // namespace std + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Arithmetic operators +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool operator==(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) == float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator!=(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) != float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) < float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<=(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) <= float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) > float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>=(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return float(lhs) >= float(rhs); +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator+(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return bfloat16_t(float(lhs) + float(rhs)); +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator-(bfloat16_t const& lhs) { + return bfloat16_t(-float(lhs)); +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator-(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return bfloat16_t(float(lhs) - float(rhs)); +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator*(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return bfloat16_t(float(lhs) * float(rhs)); +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator/(bfloat16_t const& lhs, bfloat16_t const& rhs) { + return bfloat16_t(float(lhs) / float(rhs)); +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator+=(bfloat16_t & lhs, bfloat16_t const& rhs) { + lhs = bfloat16_t(float(lhs) + float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator-=(bfloat16_t & lhs, bfloat16_t const& rhs) { + lhs = bfloat16_t(float(lhs) - float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator*=(bfloat16_t & lhs, bfloat16_t const& rhs) { + lhs = bfloat16_t(float(lhs) * float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator/=(bfloat16_t & lhs, bfloat16_t const& rhs) { + lhs = bfloat16_t(float(lhs) / float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator++(bfloat16_t & lhs) { + float tmp(lhs); + ++tmp; + lhs = bfloat16_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t& operator--(bfloat16_t & lhs) { + float tmp(lhs); + --tmp; + lhs = bfloat16_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator++(bfloat16_t & lhs, int) { + bfloat16_t ret(lhs); + float tmp(lhs); + tmp++; + lhs = bfloat16_t(tmp); + return ret; +} + +CUTLASS_HOST_DEVICE +bfloat16_t operator--(bfloat16_t & lhs, int) { + bfloat16_t ret(lhs); + float tmp(lhs); + tmp--; + lhs = bfloat16_t(tmp); + return ret; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// User-defined literals +// + +CUTLASS_HOST_DEVICE +cutlass::bfloat16_t operator "" _bf16(long double x) { + return cutlass::bfloat16_t(float(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::bfloat16_t operator "" _bf16(unsigned long long int x) { + return cutlass::bfloat16_t(int(x)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/complex_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/complex_hip.h new file mode 100644 index 000000000..7752e90d5 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/complex_hip.h @@ -0,0 +1,728 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include + +#include + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include "cutlass/cutlass.h" +#include "cutlass/functional.h" +#include "cutlass/half.h" +#include "cutlass/real.h" + +#include "cutlass/bfloat16.h" +#include "cutlass/tfloat32.h" + +#include "cutlass/fast_math_hip.h" + +#if !defined(__CUDACC_RTC__) +#include +#endif + +namespace cutlass { + + + + +///////////////////////////////////////////////////////////////////////////////////////////////// +/// Enumeraed type describing a transformation on a complex value. +enum class ComplexTransform { + kNone, + kConjugate +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +/// Defines ComplexTransform inversions +template +struct InvertComplexTransform; + +/// Invert ComplexTransform from kNone to kConjugate +template <> +struct InvertComplexTransform { + static ComplexTransform const transform = ComplexTransform::kConjugate; +}; + +/// Invert ComplexTransform from kConjugate to kNone +template <> +struct InvertComplexTransform { + static ComplexTransform const transform = ComplexTransform::kNone; +}; +///////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// Accessors for CUDA complex types +// + +#if !defined(__CUDACC_RTC__) +/// Returns the real part of the complex number +CUTLASS_HOST_DEVICE +float const &real(hipFloatComplex const &z) { return z.x; } + +/// Returns the real part of the complex number +CUTLASS_HOST_DEVICE +float &real(hipFloatComplex &z) { return z.x; } + +/// Returns the real part of the complex number +CUTLASS_HOST_DEVICE +double const &real(hipDoubleComplex const &z) { return z.x; } + +/// Returns the real part of the complex number +CUTLASS_HOST_DEVICE +double &real(hipDoubleComplex &z) { return z.x; } + +/// Returns the imaginary part of the complex number +CUTLASS_HOST_DEVICE +float const &imag(hipFloatComplex const &z) { return z.y; } + +/// Returns the imaginary part of the complex number +CUTLASS_HOST_DEVICE +float &imag(hipFloatComplex &z) { return z.y; } + +/// Returns the imaginary part of the complex number +CUTLASS_HOST_DEVICE +double const &imag(hipDoubleComplex const &z) { return z.y; } + +/// Returns the imaginary part of the complex number +CUTLASS_HOST_DEVICE +double &imag(hipDoubleComplex &z) { return z.y; } +#endif + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Class for representing and manipulating complex numbers with conversions from built-in CUDA +/// complex types. + +template +class complex +{ + public: + /// Type alias for scalar type + using value_type = T; + + private: + // + // Data members + // + + /// Real part + T _real; + + /// Imaginary part + T _imag; + + public: + +// +// Methods +// + + /// Default constructor + complex() = default; + + /// Constructor + CUTLASS_HOST_DEVICE + complex(T r) : _real(r), _imag(T(0)) {} + + /// Constructor + CUTLASS_HOST_DEVICE + complex(T r, T i) : _real(r), _imag(i) {} + + /// Constructor + template + CUTLASS_HOST_DEVICE + complex(complex const &z) : _real(static_cast(z.real())), _imag(static_cast(z.imag())) {} + + + #if !defined(__CUDACC_RTC__) + /// Conversion from hipFloatComplex + CUTLASS_HOST_DEVICE + complex(hipFloatComplex const &z) : _real(static_cast(hipCrealf(z))), _imag(static_cast(hipCimagf(z))) {} + + /// Conversion from hipDoubleComplex + CUTLASS_HOST_DEVICE + complex(hipDoubleComplex const &z) : _real(static_cast(hipCreal(z))), _imag(static_cast(hipCimag(z))) {} + #endif + + /// Equality operator + CUTLASS_HOST_DEVICE bool operator==(complex const &rhs) const { + return this->real() == rhs.real() && this->imag() == rhs.imag(); + } + + /// Inequality operator + CUTLASS_HOST_DEVICE bool operator!=(complex const &rhs) const { + return !(*this == rhs); + } + + /// Addition + template + CUTLASS_HOST_DEVICE complex operator+(complex const &rhs) const { + return complex(this->real() + rhs.real(), this->imag() + rhs.imag()); + } + + /// Reduction into memory address. Components may update out of order. + template + CUTLASS_DEVICE void red(complex *ptr) const { + static_assert(platform::is_same::value, "Component type must match"); + cutlass::atomic_add reduce; + reduce(&ptr->_real, _real); + reduce(&ptr->_imag, _imag); + } + + /// Reduction into memory address. Components may update out of order. (Half specialization) + CUTLASS_DEVICE void red(complex *ptr) const { + static_assert(platform::is_same::value, "Component type must match"); + half2 *h2_ptr = reinterpret_cast(ptr); + half2 h2_data = reinterpret_cast(*this); + cutlass::atomic_add reduce; + reduce(h2_ptr, h2_data); + } + + /// Subtraction + template + CUTLASS_HOST_DEVICE complex operator-(complex const &rhs) const { + return complex(this->real() - rhs.real(), this->imag() - rhs.imag()); + } + + /// Multiplication + template + CUTLASS_HOST_DEVICE complex operator*(complex const &rhs) const { + return complex(this->real() * rhs.real() - this->imag() * rhs.imag(), + this->real() * rhs.imag() + this->imag() * rhs.real()); + } + + /// Scalar Multiplication + template + CUTLASS_HOST_DEVICE complex operator*(A const &s) const { + return complex(this->real() * s, this->imag() * s); + } + + /// Division + template + CUTLASS_HOST_DEVICE complex operator/(complex const &rhs) const { + T d = T(rhs.real() * rhs.real() + rhs.imag() * rhs.imag()); + + return complex( + (real() * rhs.real() + imag() * rhs.imag()) / d, + (imag() * rhs.real() - real() * rhs.imag()) / d + ); + } + + /// Scalar Division + template + CUTLASS_HOST_DEVICE complex operator/(A const &s) const { + return complex(this->real() / s, this->imag() / s); + } + + /// Addition + template + CUTLASS_HOST_DEVICE complex &operator+=(complex const &rhs) { + *this = *this + rhs; + return *this; + } + + /// Subtraction + template + CUTLASS_HOST_DEVICE complex &operator-=(complex const &rhs) { + *this = *this - rhs; + return *this; + } + + /// Multiplication + template + CUTLASS_HOST_DEVICE complex &operator*=(complex const &rhs) { + *this = *this * rhs; + return *this; + } + + /// Scalar multiplication + template + CUTLASS_HOST_DEVICE complex &operator*=(A s) { + *this = *this * s; + return *this; + } + + /// Division + template + CUTLASS_HOST_DEVICE complex &operator/=(complex const &rhs) { + *this = *this / rhs; + return *this; + } + + /// Accesses the real part of the complex number + CUTLASS_HOST_DEVICE + T const &real() const { return _real; } + + /// Accesses the real part of the complex number + CUTLASS_HOST_DEVICE + T &real() { return _real; } + + /// Accesses the imaginary part of the complex number + CUTLASS_HOST_DEVICE + T const &imag() const { return _imag; } + + /// Accesses the imaginary part of the complex number + CUTLASS_HOST_DEVICE + T &imag() { return _imag; } + + /// Set the real part of the complex number + CUTLASS_HOST_DEVICE + void real(T real) { _real = real; } + + /// Set the imaginary part of the complex number + CUTLASS_HOST_DEVICE + void imag(T imag) { _imag = imag; } + + #if !defined(__CUDACC_RTC__) + /// Converts to hipFloatComplex + CUTLASS_HOST_DEVICE + explicit operator hipFloatComplex() const { return make_hipFloatComplex(float(real()), float(imag())); } + + /// Converts to hipDoubleComplex + CUTLASS_HOST_DEVICE + explicit operator hipDoubleComplex() const { return make_hipDoubleComplex(real(), imag()); } + #endif +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// Accessors for complex template +// + +/// Returns the real part of the complex number +template +CUTLASS_HOST_DEVICE T const &real(complex const &z) { + return z.real(); +} + +/// Returns the real part of the complex number +template +CUTLASS_HOST_DEVICE T &real(complex &z) { + return z.real(); +} + +/// Returns the imaginary part of the complex number +template +CUTLASS_HOST_DEVICE T const &imag(complex const &z) { + return z.imag(); +} + +/// Returns the imaginary part of the complex number +template +CUTLASS_HOST_DEVICE T &imag(complex &z) { + return z.imag(); +} + +/// Returns the real part of the real number +template +CUTLASS_HOST_DEVICE T const &real(T const &r) { + return r; +} + +/// Returns the real part of the real number +template +CUTLASS_HOST_DEVICE T &real(T &r) { + return r; +} + +/// Returns the imaginary part of the real number +template +CUTLASS_HOST_DEVICE T const &imag(T const &r) { + return T(); +} + +/// Returns the imaginary part of the complex number +template +CUTLASS_HOST_DEVICE T &imag(T &r) { + return T(); +} + +// +// Output operators +// + +#if !defined(__CUDACC_RTC__) +template +std::ostream &operator<<(std::ostream &out, complex const &z) { + T _r = real(z); + T _i = imag(z); + + if (bool(_i)) { + return out << _r << "+i" << _i; + } + return out << _r; +} +#endif + +// +// Non-member operators defined for complex types +// + + +// +// Non-member functions defined for complex numbers +// + +/// Returns the magnitude of the complex number +template +CUTLASS_HOST_DEVICE T abs(complex const &z) { + return sqrt(norm(z)); +} + +/// Returns the magnitude of the complex number +template +CUTLASS_HOST_DEVICE T arg(complex const &z) { + return atan2(imag(z), real(z)); +} + +/// Returns the squared magnitude of a real number +template +CUTLASS_HOST_DEVICE T norm(T const &z) { + return z * z; +} + +/// Returns the squared magnitude of a real number +template <> +CUTLASS_HOST_DEVICE int8_t norm(int8_t const &z) { + return static_cast(z * z); +} + +/// Returns the squared magnitude of a complex number +template +CUTLASS_HOST_DEVICE double norm(complex const &z) { + return real(z) * real(z) + imag(z) * imag(z); +} + +/// Norm-accumulate calculation +template +CUTLASS_HOST_DEVICE R norm_accumulate(T const &x, R const & accumulator) { + return accumulator + static_cast(x) * static_cast(x); +} + +/// Norm accumulate specialized for complex types +template +CUTLASS_HOST_DEVICE R norm_accumulate(complex const &z, R const &accumulator) { + return accumulator + static_cast(real(z)) * static_cast(real(z)) + + static_cast(imag(z)) * static_cast(imag(z)); +} + +CUTLASS_HOST_DEVICE float conj(float const &z) { + return z; +} + +CUTLASS_HOST_DEVICE double conj(double const &z) { + return z; +} + +CUTLASS_HOST_DEVICE half_t conj(half_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE int32_t conj(int32_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE uint32_t conj(uint32_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE int4b_t conj(int4b_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE uint4b_t conj(uint4b_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE bfloat16_t conj(bfloat16_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE uint1b_t conj(uint1b_t const& z) { + return z; +} + +CUTLASS_HOST_DEVICE tfloat32_t conj(tfloat32_t const& z) { + return z; +} + +/// Returns the complex conjugate +template +CUTLASS_HOST_DEVICE complex conj(complex const &z) { + return complex(real(z), -imag(z)); +} + +/// Projects the complex number z onto the Riemann sphere +template +CUTLASS_HOST_DEVICE complex proj(complex const &z) { + T d = real(z) * real(z) + imag(z) * imag(z) + T(1); + return complex((T(2) * real(z)) / d, (T(2) * imag(z)) / d); +} + +/// Returns a complex number with magnitude r and phase theta +template +CUTLASS_HOST_DEVICE complex polar(T const &r, T const &theta = T()) { + return complex(r * cos(theta), r * sin(theta)); +} + +/// Computes the complex exponential of z. +template +CUTLASS_HOST_DEVICE complex exp(complex const &z) { + return complex(fast_exp(real(z)) * fast_cos(imag(z)), fast_exp(real(z)) * fast_sin(imag(z))); +} + +/// Computes the log of z +template +CUTLASS_HOST_DEVICE complex log(complex const &z) { + return complex(log(abs(z)), arg(z)); +} + +/// Computes the log base 10 of z +template +CUTLASS_HOST_DEVICE complex log10(complex const &z) { + return log(z) / T(log(T(10))); +} + +/// Computes the square root of complex number z +template +CUTLASS_HOST_DEVICE complex sqrt(complex const &z) { + return sqrt(T(2)) / T(2) * + complex(sqrt(sqrt(norm(z)) + real(z)), + (imag(z) < 0 ? T(-1) : T(1)) * sqrt(sqrt(norm(z)) - real(z))); +} + +/// Computes the cosine of complex z. +template +CUTLASS_HOST_DEVICE complex cos(complex const &z) { + return (exp(z) + exp(-z)) / T(2); +} + +/// Computes the sin of complex z. +template +CUTLASS_HOST_DEVICE complex sin(complex const &z) { + return (exp(-z) - exp(z)) * complex(T(0), T(1) / T(2)); +} + +/// Comparison +template +CUTLASS_HOST_DEVICE bool operator<(complex const &lhs, complex const &rhs) { + return true; +} + +////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for complex-valued type. +template +struct RealType< complex > +{ + using Type = T; + + /// Number of elements + static int const kExtent = 2; + + CUTLASS_HOST_DEVICE + static complex from_real(double x) { + return complex(static_cast(x)); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template <> +CUTLASS_HOST_DEVICE +cutlass::complex from_real >(double r) { + return cutlass::complex(half_t(r)); +} + +template <> +CUTLASS_HOST_DEVICE +cutlass::complex from_real >(double r) { + return cutlass::complex(float(r)); +} + +template <> +CUTLASS_HOST_DEVICE +cutlass::complex from_real >(double r) { + return cutlass::complex(r); +} + +////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct is_complex { + static bool const value = false; +}; + +template +struct is_complex> { + static bool const value = true; +}; + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// functional.h numeric specializations +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Squares with optional conversion +template +struct magnitude_squared, Output> { + CUTLASS_HOST_DEVICE + Output operator()(complex lhs) const { + multiplies mul_op; + + Output y_r = Output(lhs.real()); + Output y_i = Output(lhs.imag()); + + return mul_op(y_r, y_r) + mul_op(y_i, y_i); + } +}; + +/// Fused multiply-add +template +struct multiply_add, complex, complex> { + CUTLASS_HOST_DEVICE + complex operator()( + complex const &a, + complex const &b, + complex const &c) const { + + T real = c.real(); + T imag = c.imag(); + + real += a.real() * b.real(); + real += -a.imag() * b.imag(); + imag += a.real() * b.imag(); + imag += a.imag () * b.real(); + + return complex{ + real, + imag + }; + } +}; + +/// Fused multiply-add +template +struct multiply_add, T, complex> { + CUTLASS_HOST_DEVICE + complex operator()( + complex const &a, + T const &b, + complex const &c) const { + + T real = c.real(); + T imag = c.imag(); + + real += a.real() * b; + imag += a.imag () * b; + + return complex{ + real, + imag + }; + } +}; + +/// Fused multiply-add +template +struct multiply_add, complex> { + CUTLASS_HOST_DEVICE + complex operator()( + T const &a, + complex const &b, + complex const &c) const { + + T real = c.real(); + T imag = c.imag(); + + real += a * b.real(); + imag += a * b.imag(); + + return complex{ + real, + imag + }; + } +}; + +/// Conjugate +template +struct conjugate> { + CUTLASS_HOST_DEVICE + complex operator()(complex const &a) const { + return conj(a); + } +}; + +/// Computes the square of a difference with optional conversion +template +struct magnitude_squared_difference, Output> { + CUTLASS_HOST_DEVICE + Output operator()(complex lhs, complex rhs) const { + multiplies mul_op; + + Output y_r = Output(lhs.real()) - Output(rhs.real()); + Output y_i = Output(lhs.imag()) - Output(rhs.imag()); + + return mul_op(y_r, y_r) + mul_op(y_i, y_i); + } +}; + +/// Reduces value into the data pointed to by ptr (complex specialization) +template +struct atomic_add> { + CUTLASS_DEVICE + void operator()(complex *ptr, const complex &data) + { + data.red(ptr); + } +}; + + +////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/coord.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/coord.h new file mode 100644 index 000000000..50fd51930 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/coord.h @@ -0,0 +1,490 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief A Coord is a coordinate of arbitrary rank into a tensor or matrix +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include "cutlass/cutlass.h" + +namespace cutlass { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Statically-sized array specifying Coords within a tensor +template < + int Rank_, ///< Logical rank of coordinate + typename Index_ = int, ///< Index type used for each dimension + typename LongIndex_ = int64_t ///< Long index type used for linear offsets +> +struct Coord { + +public: + + // + // Type and constant definitions + // + + /// Number of elements in Coord + static int const kRank = Rank_; + + /// Index type used to store elements + using Index = Index_; + + /// Type used to represent linear offsets + using LongIndex = LongIndex_; + +private: + + // + // Data members + // + + /// Indices + Index idx[kRank]; + +public: + + // + // Methods + // + + /// Default ctor initializes uniformly + CUTLASS_HOST_DEVICE + explicit Coord(Index value = Index(0)) { + for (int i = 0; i < kRank; ++i) { + idx[i] = value; + } + } + + /// Constructs from an array of integers + CUTLASS_HOST_DEVICE + Coord(Index const (&_idx)[kRank]) { + for (int i = 0; i < kRank; ++i) { + idx[i] = _idx[i]; + } + } + + /// Constructs from some other Coord + template + CUTLASS_HOST_DEVICE + Coord(Coord other) { + for (int i = 0; i < kRank; ++i) { + idx[i] = other[i]; + } + } + + /// Returns a slice of the Coord which may be larger or smaller in rank + /// than this. + template + CUTLASS_HOST_DEVICE + Coord slice(int start = 0, Index identity = 0) const { + Coord result; + for (int i = 0; i < Slice; ++i) { + if (i + start < kRank) { + result[i] = idx[i + start]; + } + else { + result[i] = identity; + } + } + return result; + } + + /// Returns the index of the dimension with least value + CUTLASS_HOST_DEVICE + int min_dim_index() const { + int i = 0; + for (int j = 1; j < kRank; ++j) { + if (idx[j] < idx[i]) { + i = j; + } + } + return i; + } + + /// Returns the index of the dimension with greatest value + CUTLASS_HOST_DEVICE + int max_dim_index() const { + int i = 0; + for (int j = 1; j < kRank; ++j) { + if (idx[j] > idx[i]) { + i = j; + } + } + return i; + } + + /// Returns true if Coord is non-zero. + CUTLASS_HOST_DEVICE + explicit operator bool() const { + for (int i = 0; i < kRank; ++i) { + if (idx[i]) { + return true; + } + } + return false; + } + + /// Returns true if Coord is uniformly zero. + CUTLASS_HOST_DEVICE + bool operator!() const { + for (int i = 0; i < kRank; ++i) { + if (idx[i]) { + return false; + } + } + return true; + } + + /// Element-wise addition + CUTLASS_HOST_DEVICE + Coord operator+(Coord const& b) const { + Coord c; + for (int i = 0; i < kRank; ++i) { + c.idx[i] = idx[i] + b.idx[i]; + } + return c; + } + + /// Element-wise subtraction + CUTLASS_HOST_DEVICE + Coord operator-(Coord const& b) const { + Coord c; + for (int i = 0; i < kRank; ++i) { + c.idx[i] = idx[i] - b.idx[i]; + } + return c; + } + + /// Element-wise multiplication + CUTLASS_HOST_DEVICE + Coord operator*(Coord const& b) const { + Coord c; + for (int i = 0; i < kRank; ++i) { + c.idx[i] = idx[i] * b.idx[i]; + } + return c; + } + + /// Element-wise division + CUTLASS_HOST_DEVICE + Coord operator/(Coord const& b) const { + Coord c; + for (int i = 0; i < kRank; ++i) { + c.idx[i] = idx[i] / b.idx[i]; + } + return c; + } + + /// In-place addition + CUTLASS_HOST_DEVICE + Coord& operator+=(Coord const& b) { + for (int i = 0; i < kRank; ++i) { + idx[i] += b.idx[i]; + } + return *this; + } + + /// In-place subtraction + CUTLASS_HOST_DEVICE + Coord& operator-=(Coord const& b) { + for (int i = 0; i < kRank; ++i) { + idx[i] -= b.idx[i]; + } + return *this; + } + + /// In-place multiplication + CUTLASS_HOST_DEVICE + Coord& operator*=(Coord const& b) { + for (int i = 0; i < kRank; ++i) { + idx[i] *= b.idx[i]; + } + return *this; + } + + /// In-place division + CUTLASS_HOST_DEVICE + Coord& operator/=(Coord const& b) { + for (int i = 0; i < kRank; ++i) { + idx[i] /= b.idx[i]; + } + return *this; + } + + /// Member access operator + CUTLASS_HOST_DEVICE Index& operator[](int dim) { return idx[dim]; } + + /// Member access operator + CUTLASS_HOST_DEVICE Index const& operator[](int dim) const { return idx[dim]; } + + /// Computes the dot product with anotherCoord object + CUTLASS_HOST_DEVICE + LongIndex dot(Coord const& b, LongIndex sum = LongIndex(0)) const { + for (int i = 0; i < kRank; ++i) { + sum += idx[i] * b.idx[i]; + } + return sum; + } + + /// Gets the index of a given Coord element + template + CUTLASS_HOST_DEVICE Index& at() { + return idx[Dim]; + } + + /// Access via index; may limit unrolling potential + CUTLASS_HOST_DEVICE + Index& at(int dim) { return idx[dim]; } + + /// Gets the index of a given Coord element + template + CUTLASS_HOST_DEVICE Index const& at() const { + return idx[Dim]; + } + + /// Access via index; may limit unrolling potential + CUTLASS_HOST_DEVICE + Index const& at(int dim) const { return idx[dim]; } + + /// Determines if two Coord<> objects are equal + CUTLASS_HOST_DEVICE + bool operator==(Coord const& b) const { + bool equal = true; + for (int i = 0; equal && i < kRank; ++i) { + equal = (idx[i] == b.idx[i]); + } + return equal; + } + + /// Not equal + CUTLASS_HOST_DEVICE + bool operator!=(Coord const& b) const { return !(*this == b); } + + /// Clamps a coordinate to a range specified by maximum and minimum values + CUTLASS_HOST_DEVICE + Coord& clamp(Coord const& max, Coord const& min = Coord()) { + for (int i = 0; i < kRank; ++i) { + idx[i] = __NV_STD_MAX(__NV_STD_MIN(idx[i], max.idx[i]), min.idx[i]); + } + return *this; + } + + /// Returns the sum of all elements + CUTLASS_HOST_DEVICE + Index sum() const { + Index sum_(idx[0]); + for (int i = 1; i < kRank; ++i) { + sum_ += idx[i]; + } + return sum_; + } + + /// Returns the product of all elements + CUTLASS_HOST_DEVICE + LongIndex product() const { + LongIndex product_(idx[0]); + for (int i = 1; i < kRank; ++i) { + product_ *= idx[i]; + } + return product_; + } + + /// Less than operator + CUTLASS_HOST_DEVICE + bool operator<(Coord const &b) const { + for (int i = 0; i < kRank; ++i) { + if (!(idx[i] < b[i])) { + return false; + } + } + return true; + } + + /// Less than or equals operator + CUTLASS_HOST_DEVICE + bool operator<=(Coord const &b) const { + for (int i = 0; i < kRank; ++i) { + if (!(idx[i] <= b[i])) { + return false; + } + } + return true; + } + + /// Greater than operator + CUTLASS_HOST_DEVICE + bool operator>(Coord const &b) const { + return !(*this <= b); + } + + /// Greater than or equals operator + CUTLASS_HOST_DEVICE + bool operator>=(Coord const &b) const { + return !(*this < b); + } +}; + +} // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + + +/// Scalar multiplication +template +CUTLASS_HOST_DEVICE +Coord operator*(Index s, Coord coord) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank; ++i) { + coord[i] *= s; + } + return coord; +} + +/// Scalar multiplication +template +CUTLASS_HOST_DEVICE +Coord operator*(Coord coord, Index s) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank; ++i) { + coord[i] *= s; + } + return coord; +} + +/// Scalar division +template +CUTLASS_HOST_DEVICE +Coord operator/(Index s, Coord coord) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank; ++i) { + coord[i] = s / coord[i]; + } + return coord; +} + +/// Scalar division +template +CUTLASS_HOST_DEVICE +Coord operator/(Coord coord, Index s) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank; ++i) { + coord[i] /= s; + } + return coord; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Integer-valued make_Coord +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Helper to make a 2-element coordinate +template +CUTLASS_HOST_DEVICE +Coord<1, T> make_Coord(T _0) { + T values[1] = {_0}; + return Coord<1, T>(values); +} + +/// Helper to make a 2-element coordinate +template +CUTLASS_HOST_DEVICE +Coord<2, T> make_Coord(T _0, T _1) { + T values[2] = {_0, _1}; + return Coord<2, T>(values); +} + +/// Helper to make a 3-element coordinate +template +CUTLASS_HOST_DEVICE +Coord<3, T> make_Coord(T _0, T _1, T _2) { + T values[3] = {_0, _1, _2}; + return Coord<3, T>(values); +} + +/// Helper to make a 4-element coordinate +template +CUTLASS_HOST_DEVICE +Coord<4, T> make_Coord(T _0, T _1, T _2, T _3) { + T values[4] = {_0, _1, _2, _3}; + return Coord<4, T>(values); +} + +/// Helper to make a 5-element coordinate +template +CUTLASS_HOST_DEVICE +Coord<5, T> make_Coord(T _0, T _1, T _2, T _3, T _4) { + T values[5] = {_0, _1, _2, _3, _4}; + return Coord<5, T>(values); +} + +/// Helper to make a 1-element coordinate +template +CUTLASS_HOST_DEVICE +Coordmake_Coord_with_padding(T _0) { + Coord coord; + + CUTLASS_PRAGMA_UNROLL + for (int i = N - 1; i > 0; --i) { + coord[i] = 0; + } + + coord[0] = _0; + + return coord; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/cutlass.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/cutlass.h new file mode 100644 index 000000000..00b662c2d --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/cutlass.h @@ -0,0 +1,228 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +/*! \file + \brief Basic include for CUTLASS. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#include "cutlass/detail/helper_macros.hpp" +#include "hip/hip_runtime.h" + +// Support __shfl_sync while dtk do not have this head file +#if __has_include() // check if __shfl_sync is provided by dtk + // has __shfl_sync +#else + +// #define __hip_check_mask(MASK) \ +// do { \ +// __hip_assert(MASK && "mask must be non-zero"); \ +// bool done = false; \ +// while (__any(!done)) { \ +// if (!done) { \ +// auto chosen_mask = __hip_readfirstlane(MASK); \ +// if (MASK == chosen_mask) { \ +// __hip_assert(MASK == __ballot(true) && \ +// "all threads specified in the mask" \ +// " must execute the same operation with the same mask"); \ +// done = true; \ +// } \ +// } \ +// } \ +// } while(0) + +template +__device__ inline +T __shfl_sync(MaskT mask, T var, int srcLane, + int width = __AMDGCN_WAVEFRONT_SIZE) { + // dtk23.04 + // static_assert( + // __hip_internal::is_integral::value && sizeof(MaskT) == 8, + // "The mask must be a 64-bit integer. " + // "Implicitly promoting a smaller integer is almost always an error."); + // __hip_check_mask(mask); + return __shfl(var, srcLane, width); +} + +#endif // end check + +#define WARP_SIZE_GPU 64 +/* + Note: use v_fma_mix_fp32 +*/ +// #define MIX_FP16XFP16_SM60 +/* + Note: use dot instruction +*/ +#define MIX_FP16_DOT2 + +/* + Note: default epilogue use acc-fp32 compute-fp32, compute finish convertTo fp16. + Turn on this macro, which means we try to compute in fp16 data type. + Also we should change epilogue_vector_length(in generator.py) to use v_cvt_pkrtz_f16_f32 instruction. +*/ +// #define MIX_FP16_EPILOGUE +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/// Status code returned by CUTLASS operations +enum class Status { + kSuccess, ///< Operation was successful. + kErrorMisalignedOperand, ///< operands fail alignment requirements. + kErrorInvalidDataType, ///< DataType fails requirement. + kErrorInvalidLayout, ///< Layout fails alignment requirement. + kErrorInvalidProblem, ///< Specified problem size is not supported by operator. + kErrorNotSupported, ///< Operation is not supported on current device. + kErrorWorkspaceNull, ///< The given workspace is null when it is required to be non-null. + kErrorInternal, ///< An error within CUTLASS occurred. + kErrorArchMismatch, ///< CUTLASS runs on a device that it was not compiled for. + kErrorInsufficientDriver, ///< CUTLASS runs with a driver that is too old. + kErrorMemoryAllocation, ///< Kernel launch failed due to insufficient device memory. + kInvalid ///< Status is unspecified. +}; + +/// Convert cutlass status to status strings +CUTLASS_HOST_DEVICE +static char const* cutlassGetStatusString(cutlass::Status status) { + switch (status) { + case cutlass::Status::kSuccess: + return "Success"; + case cutlass::Status::kErrorMisalignedOperand: + return "Error Misaligned Operand"; + case cutlass::Status::kErrorInvalidDataType: + return "Error Invalid Data Type"; + case cutlass::Status::kErrorInvalidLayout: + return "Error Invalid Layout"; + case cutlass::Status::kErrorInvalidProblem: + return "Error Invalid Problem"; + case cutlass::Status::kErrorNotSupported: + return "Error Not Supported"; + case cutlass::Status::kErrorWorkspaceNull: + return "Error Workspace Null"; + case cutlass::Status::kErrorInternal: + return "Error Internal"; + case cutlass::Status::kErrorInsufficientDriver: + return "Error Insufficient Driver"; + case cutlass::Status::kErrorArchMismatch: + return "Error Architecture Mismatch"; + case cutlass::Status::kErrorMemoryAllocation: + return "Error Memory Allocation failed"; + case cutlass::Status::kInvalid: break; + } + + return "Invalid status"; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// static const int NumThreadsPerWarp = 32; +static const int NumThreadsPerWarp = WARP_SIZE_GPU; +static const int NumThreadsPerWarpGroup = 128; +static const int NumWarpsPerWarpGroup = NumThreadsPerWarpGroup / NumThreadsPerWarp; +static const int NumThreadsPerHalfWarp = NumThreadsPerWarp / 2; +static const int NumThreadsPerQuad = 4; +static const int NumThreadsPerQuadPair = NumThreadsPerQuad * 2; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Helper function to return true when called by thread 0 of threadblock 0. +CUTLASS_HOST_DEVICE bool thread0() { + #if defined(__HIP_DEVICE_COMPILE__) + return (!threadIdx.x && !threadIdx.y && !threadIdx.z) && (!blockIdx.x && !blockIdx.y && !blockIdx.z); + #else + return false; + #endif +} + +/// Returns a lane index in the warp. The threads in warp may not be convergent +CUTLASS_DEVICE +int canonical_lane_idx() { + #if defined(__HIP_DEVICE_COMPILE__) + return threadIdx.x % NumThreadsPerWarp; + #else + return 0; + #endif +} + +/// Returns a warp-uniform value indicating the canonical warp index of the calling threads. +/// Threads within the warp must be converged. +CUTLASS_DEVICE +int canonical_warp_idx_sync() { + #if defined(__HIP_DEVICE_COMPILE__) + #ifdef DCU_ASM + return (threadIdx.x / NumThreadsPerWarp); + #else + return __shfl_sync((uint64_t)0xffffffff, threadIdx.x / NumThreadsPerWarp, 0); + #endif + #else + return 0; + #endif +} + +/// Returns a warp index in the CTA. The threads in warp may not be convergent +/// As it doesn't sync the warp, it faster and allows forward progress +CUTLASS_DEVICE +int canonical_warp_idx() { + #if defined(__HIP_DEVICE_COMPILE__) + return threadIdx.x / NumThreadsPerWarp; + #else + return 0; + #endif +} + +/// Returns a warp-uniform value indicating the canonical warp group index of the calling threads. +/// Threads within the warp must be converged. +CUTLASS_DEVICE +int canonical_warp_group_idx() { + #if defined(__HIP_DEVICE_COMPILE__) + // return __shfl_sync((uint64_t)0xffffffff, threadIdx.x / NumThreadsPerWarpGroup, 0); + return __shfl(threadIdx.x / NumThreadsPerWarpGroup, 0); + #else + return 0; + #endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/detail/helper_macros.hpp b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/detail/helper_macros.hpp new file mode 100644 index 000000000..897a3a6ba --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/detail/helper_macros.hpp @@ -0,0 +1,194 @@ +/*************************************************************************************************** + * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +/*! \file + \brief Helper macros for the CUTLASS library +*/ + +#pragma once +#include +//////////////////////////////////////////////////////////////////////////////////////////////////// + + +#ifdef CUTLASS_NAMESPACE +#define concat_tok(a, b) a ## b +#define mkcutlassnamespace(pre, ns) concat_tok(pre, ns) +#define cutlass mkcutlassnamespace(cutlass_, CUTLASS_NAMESPACE) +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if defined DCU_ASM +#define CUTLASS_HOST_DEVICE __forceinline__ __device__ __host__ +#define CUTLASS_DEVICE __forceinline__ __device__ +#elif defined(__CUDACC_RTC__) +#define CUTLASS_HOST_DEVICE __forceinline__ __device__ +#define CUTLASS_DEVICE __forceinline__ __device__ +#else +#define CUTLASS_HOST_DEVICE inline +#define CUTLASS_DEVICE inline +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +CUTLASS_HOST_DEVICE void __CUTLASS_UNUSED(T const &) +{ } + +#if defined(__GNUC__) + #define CUTLASS_UNUSED(expr) __CUTLASS_UNUSED(expr) +#else + #define CUTLASS_UNUSED(expr) do { ; } while (&expr != &expr) +#endif + +#ifdef _MSC_VER +// Provides support for alternative operators 'and', 'or', and 'not' +#include +#endif // _MSC_VER + +#if !defined(__CUDACC_RTC__) +#include +#endif + +#ifdef DCU_ASM + #define CUTLASS_NOT_IMPLEMENTED() { printf("not implemented\n"); assert(0); } +#elif defined(__HIP_DEVICE_COMPILE__) + #if defined(_MSC_VER) + #define CUTLASS_NOT_IMPLEMENTED() { printf("%s not implemented\n", __FUNCSIG__); asm volatile ("brkpt;\n"); } + #else + #define CUTLASS_NOT_IMPLEMENTED() { printf("%s not implemented\n", __PRETTY_FUNCTION__); asm volatile ("brkpt;\n"); } + #endif +#else + #if defined(_MSC_VER) + #define CUTLASS_NOT_IMPLEMENTED() assert(0 && __FUNCSIG__) + #else + #define CUTLASS_NOT_IMPLEMENTED() assert(0 && __PRETTY_FUNCTION__) + #endif +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + + +#ifndef CUTLASS_CONV_UNIT_TEST_RIGOROUS_SIZE_ENABLED +#define CUTLASS_CONV_UNIT_TEST_RIGOROUS_SIZE_ENABLED 0 +#endif + + +// CUDA 10.1 introduces the mma instruction +#if !defined(CUTLASS_ENABLE_TENSOR_CORE_MMA) +#define CUTLASS_ENABLE_TENSOR_CORE_MMA 0 +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#define CUTLASS_ASSERT(x) assert(x) + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// CUTLASS_PRAGMA_(UNROLL|NO_UNROLL) optimization directives for the CUDA compiler. +#if defined(__HIP_DEVICE_COMPILE__) && !defined(__INTELLISENSE__) + #if defined(__HIPCC__) || (defined(__clang__) && defined(__CUDA__)) + #define CUTLASS_PRAGMA_UNROLL _Pragma("unroll") + #define CUTLASS_PRAGMA_NO_UNROLL _Pragma("unroll 1") + #else + #define CUTLASS_PRAGMA_UNROLL #pragma unroll + #define CUTLASS_PRAGMA_NO_UNROLL #pragma unroll 1 + #endif + + #define CUTLASS_GEMM_LOOP CUTLASS_PRAGMA_NO_UNROLL + +#else + + #define CUTLASS_PRAGMA_UNROLL + #define CUTLASS_PRAGMA_NO_UNROLL _Pragma("nounroll") + #define CUTLASS_GEMM_LOOP CUTLASS_PRAGMA_NO_UNROLL + +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#if !defined(__CUDACC_RTC__) +#define CUTLASS_THREAD_LOCAL thread_local +#else +#define CUTLASS_THREAD_LOCAL +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#if (201700L <= __cplusplus) +#define CUTLASS_CONSTEXPR_IF_CXX17 constexpr +#define CUTLASS_CXX17_OR_LATER 1 +#else +#define CUTLASS_CONSTEXPR_IF_CXX17 +#define CUTLASS_CXX17_OR_LATER 0 +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#define HAS_ATTRIBUTE_DEF(Attribute) \ + template \ + constexpr auto has_attribute_##Attribute(int) \ + -> decltype(std::declval(), std::true_type()); \ + template \ + constexpr std::false_type has_attribute_##Attribute(...); \ + template \ + constexpr bool has_attribute_##Attribute##_v = \ + decltype(has_attribute_##Attribute(0))::value; + +#define HAS_MEMBER_DEF(Member) \ + template \ + static constexpr auto has_member_##Member(T &&) \ + -> decltype(typename std::decay< \ + decltype(std::declval().Member)>::type(), \ + std::true_type()) { \ + return std::true_type(); \ + } \ + static constexpr std::false_type has_member_##Member(...) { \ + return std::false_type(); \ + } \ + template \ + static constexpr bool has_member_##Member##_v = \ + decltype(has_member_##Member(std::declval()))::value; \ + template \ + static constexpr auto get_constant_member_##Member(Tvalue default_value) { \ + if constexpr (has_member_##Member##_v) { \ + return Tclass::Member; \ + } else { \ + return default_value; \ + } \ + } + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +}; // namespace cutlass + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/fast_math_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/fast_math_hip.h new file mode 100644 index 000000000..05d4efe85 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/fast_math_hip.h @@ -0,0 +1,1020 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#include +#include +#endif + +#include "cutlass/cutlass.h" +#include "cutlass/array_hip.h" +#include "cutlass/uint128.h" +#include "cutlass/coord.h" +#include "cutlass/half.h" + +/** + * \file + * \brief Math utilities + */ + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +CUTLASS_HOST_DEVICE void swap(T &lhs, T &rhs) { + T tmp = lhs; + lhs = rhs; + rhs = tmp; +} + +/****************************************************************************** + * Static math utilities + ******************************************************************************/ + +/// Mixed precision dot product +template +CUTLASS_HOST_DEVICE LongIndex dot( + Coord const &coord, + Coord const &stride, + LongIndex acc = LongIndex()) { + + CUTLASS_PRAGMA_UNROLL + for (int n = 0; n < N; ++n) { + acc += LongIndex(coord[n]) * stride[n]; + } + return acc; +} + +/** + * Statically determine if N is a power-of-two + */ +template +struct is_pow2 { + static bool const value = ((N & (N - 1)) == 0); +}; + +/** + * Statically determine log2(N), rounded down + */ +template +struct log2_down { + /// Static logarithm value + enum { value = log2_down> 1), Count + 1>::value }; +}; + +// Base case +template +struct log2_down { + enum { value = Count }; +}; + +/** + * Statically determine log2(N), rounded up + */ +template +struct log2_up { + /// Static logarithm value + enum { value = log2_up> 1), Count + 1>::value }; +}; + +// Base case +template +struct log2_up { + enum { value = ((1 << Count) < N) ? Count + 1 : Count }; +}; + +/** + * Statically estimate sqrt(N) to the nearest power-of-two + */ +template +struct sqrt_est { + enum { value = 1 << (log2_up::value / 2) }; +}; + +/** + * For performing a constant-division with a compile-time assertion that the + * Divisor evenly-divides the Dividend. + */ +template +struct divide_assert { + enum { value = Dividend / Divisor }; + + static_assert((Dividend % Divisor == 0), "Not an even multiple"); +}; + +/****************************************************************************** + * Rounding + ******************************************************************************/ + +/** + * Round dividend up to the nearest multiple of divisor + */ +template +CUTLASS_HOST_DEVICE dividend_t round_nearest(dividend_t dividend, divisor_t divisor) { + return ((dividend + divisor - 1) / divisor) * divisor; +} + +template +CUTLASS_HOST_DEVICE +constexpr +value_t abs_for_integer(value_t a) { + return ((a > 0) ? a : -a); +} +/** + * Greatest common divisor + */ +template +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 +value_t gcd(value_t a, value_t b) { + for (;;) { + if (a == 0) return cutlass::abs_for_integer(b); + b %= a; + if (b == 0) return cutlass::abs_for_integer(a); + a %= b; + } +} + +/** + * Least common multiple + */ +template +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 +value_t lcm(value_t a, value_t b) { + value_t temp = gcd(a, b); + + return temp ? (cutlass::abs_for_integer(a) / temp * cutlass::abs_for_integer(b)) : 0; +} + +/** + * Greatest common divisor + */ +template +CUTLASS_HOST_DEVICE +constexpr +value_t gcd_cxx11(value_t a, value_t b) { + return (a == 0 || b == 0) ? cutlass::abs_for_integer(a | b) : gcd_cxx11(b, a % b); +} + +/** + * Least common multiple + */ +template +CUTLASS_HOST_DEVICE +constexpr +value_t lcm_cxx11(value_t a, value_t b) { + return gcd_cxx11(a, b) ? (cutlass::abs_for_integer(a) / gcd_cxx11(a, b) * cutlass::abs_for_integer(b)) : 0; +} +/// Returns the smallest value in the half-open range [a, a+b) that is a multiple of b +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 +int round_up(int a, int b) { + return ((a + b - 1) / b) * b; +} + +/// Returns the ceiling of (a / b) +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 +int ceil_div(int a, int b) { + return (a + b - 1) / b; +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * log2 computation, what's the + * difference between the below codes and + * log2_up/down codes? + */ +template +CUTLASS_HOST_DEVICE value_t clz(value_t x) { + for (int i = 31; i >= 0; --i) { + if ((1 << i) & x) return 31 - i; + } + return 32; +} + +template +CUTLASS_HOST_DEVICE value_t find_log2(value_t x) { + int a = int(31 - clz(x)); + a += (x & (x - 1)) != 0; // Round up, add 1 if not a power of 2. + return a; +} + + +/** + * Find divisor, using find_log2 + */ +CUTLASS_HOST_DEVICE +void find_divisor(unsigned int& mul, unsigned int& shr, unsigned int denom) { + if (denom == 1) { + mul = 0; + shr = 0; + } else { + unsigned int p = 31 + find_log2(denom); + unsigned m = unsigned(((1ull << p) + unsigned(denom) - 1) / unsigned(denom)); + + mul = m; + shr = p - 32; + } +} + +/** + * Find quotient and remainder using device-side intrinsics + */ +CUTLASS_HOST_DEVICE +void fast_divmod(int& quo, int& rem, int src, int div, unsigned int mul, unsigned int shr) { + + #if defined(__HIP_DEVICE_COMPILE__) + // Use IMUL.HI if div != 1, else simply copy the source. + quo = (div != 1) ? __umulhi(src, mul) >> shr : src; + #else + quo = int((div != 1) ? int(((int64_t)src * mul) >> 32) >> shr : src); + #endif + + // The remainder. + rem = src - (quo * div); +} + +// For long int input +CUTLASS_HOST_DEVICE +void fast_divmod(int& quo, int64_t& rem, int64_t src, int div, unsigned int mul, unsigned int shr) { + + #if defined(__HIP_DEVICE_COMPILE__) + // Use IMUL.HI if div != 1, else simply copy the source. + quo = (div != 1) ? __umulhi(src, mul) >> shr : src; + #else + quo = int((div != 1) ? ((src * mul) >> 32) >> shr : src); + #endif + // The remainder. + rem = src - (quo * div); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Object to encapsulate the fast division+modulus operation. +/// +/// This object precomputes two values used to accelerate the computation and is best used +/// when the divisor is a grid-invariant. In this case, it may be computed in host code and +/// marshalled along other kernel arguments using the 'Params' pattern. +/// +/// Example: +/// +/// +/// int quotient, remainder, dividend, divisor; +/// +/// FastDivmod divmod(divisor); +/// +/// divmod(quotient, remainder, dividend); +/// +/// // quotient = (dividend / divisor) +/// // remainder = (dividend % divisor) +/// +struct FastDivmod { + + int divisor; + unsigned int multiplier; + unsigned int shift_right; + + /// Find quotient and remainder using device-side intrinsics + CUTLASS_HOST_DEVICE + void fast_divmod(int& quotient, int& remainder, int dividend) const { + +#if defined(__HIP_DEVICE_COMPILE__) + // Use IMUL.HI if divisor != 1, else simply copy the source. + quotient = (divisor != 1) ? __umulhi(dividend, multiplier) >> shift_right : dividend; +#else + quotient = int((divisor != 1) ? int(((int64_t)dividend * multiplier) >> 32) >> shift_right : dividend); +#endif + + // The remainder. + remainder = dividend - (quotient * divisor); + } + + /// For long int input + CUTLASS_HOST_DEVICE + void fast_divmod(int& quotient, int64_t& remainder, int64_t dividend) const { + +#if defined(__HIP_DEVICE_COMPILE__) + // Use IMUL.HI if divisor != 1, else simply copy the source. + quotient = (divisor != 1) ? __umulhi(dividend, multiplier) >> shift_right : dividend; +#else + quotient = int((divisor != 1) ? ((dividend * multiplier) >> 32) >> shift_right : dividend); +#endif + // The remainder. + remainder = dividend - (quotient * divisor); + } + + + /// Construct the FastDivmod object, in host code ideally. + /// + /// This precomputes some values based on the divisor and is computationally expensive. + + CUTLASS_HOST_DEVICE + FastDivmod(): divisor(0), multiplier(0), shift_right(0) { } + + CUTLASS_HOST_DEVICE + FastDivmod(int divisor): divisor(divisor) { + + if (divisor != 1) { + unsigned int p = 31 + find_log2(divisor); + unsigned m = unsigned(((1ull << p) + unsigned(divisor) - 1) / unsigned(divisor)); + + multiplier = m; + shift_right = p - 32; + } else { + multiplier = 0; + shift_right = 0; + } + } + + /// Computes integer division and modulus using precomputed values. This is computationally + /// inexpensive. + CUTLASS_HOST_DEVICE + void operator()(int "ient, int &remainder, int dividend) const { + fast_divmod(quotient, remainder, dividend); + } + + /// Computes integer division using precomputed values. This is computationally + /// inexpensive. + CUTLASS_HOST_DEVICE + int div(int dividend) const { + int quotient, remainder; + fast_divmod(quotient, remainder, dividend); + return quotient; + } + + /// Computes integer division and modulus using precomputed values. This is computationally + /// inexpensive. + /// + /// Simply returns the quotient + CUTLASS_HOST_DEVICE + int divmod(int &remainder, int dividend) const { + int quotient; + fast_divmod(quotient, remainder, dividend); + return quotient; + } + + /// Computes integer division and modulus using precomputed values. This is computationally + /// inexpensive. + CUTLASS_HOST_DEVICE + void operator()(int "ient, int64_t &remainder, int64_t dividend) const { + fast_divmod(quotient, remainder, dividend); + } + + /// Computes integer division and modulus using precomputed values. This is computationally + /// inexpensive. + CUTLASS_HOST_DEVICE + int divmod(int64_t &remainder, int64_t dividend) const { + int quotient; + fast_divmod(quotient, remainder, dividend); + return quotient; + } + + /// Returns the divisor when cast to integer + CUTLASS_HOST_DEVICE + operator int() const { return divisor; } + +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Object to encapsulate the fast division+modulus operation for 64b integer division. +/// +/// This object precomputes two values used to accelerate the computation and is best used +/// when the divisor is a grid-invariant. In this case, it may be computed in host code and +/// marshalled along other kernel arguments using the 'Params' pattern. +/// +/// Example: +/// +/// +/// uint64_t quotient, remainder, dividend, divisor; +/// +/// FastDivmodU64 divmod(divisor); +/// +/// divmod(quotient, remainder, dividend); +/// +/// // quotient = (dividend / divisor) +/// // remainder = (dividend % divisor) +/// +struct FastDivmodU64 { + + uint64_t divisor; + uint64_t multiplier; + unsigned int shift_right; + unsigned int round_up; + + // + // Static methods + // + + /// Computes b, where 2^b is the greatest power of two that is less than or equal to x + CUTLASS_HOST_DEVICE + static uint32_t integer_log2(uint64_t x) { + uint32_t n = 0; + while (x >>= 1) { + ++n; + } + return n; + } + + /// Default ctor + CUTLASS_HOST_DEVICE + FastDivmodU64(): divisor(0), multiplier(0), shift_right(0), round_up(0) { } + + /// Construct the FastDivmod object, in host code ideally. + /// + /// This precomputes some values based on the divisor and is computationally expensive. + CUTLASS_HOST_DEVICE + FastDivmodU64(uint64_t divisor_): divisor(divisor_), multiplier(1), shift_right(0), round_up(0) { + + if (divisor) { + shift_right = integer_log2(divisor); + + if ((divisor & (divisor - 1)) == 0) { + multiplier = 0; + } + else { + uint64_t power_of_two = (uint64_t(1) << shift_right); + uint64_t multiplier_lo = uint128_t(0, power_of_two) / divisor; + multiplier = uint128_t(power_of_two, power_of_two) / divisor; + round_up = (multiplier_lo == multiplier ? 1 : 0); + } + } + } + + /// Returns the quotient of floor(dividend / divisor) + CUTLASS_HOST_DEVICE + uint64_t divide(uint64_t dividend) const { + uint64_t quotient = 0; + + #ifdef __HIP_DEVICE_COMPILE__ + uint64_t x = dividend; + if (multiplier) { + x = __umul64hi(dividend + round_up, multiplier); + } + quotient = (x >> shift_right); + #else + quotient = dividend / divisor; + #endif + + return quotient; + } + + /// Computes the remainder given a computed quotient and dividend + CUTLASS_HOST_DEVICE + uint64_t modulus(uint64_t quotient, uint64_t dividend) const { + return uint32_t(dividend - quotient * divisor); + } + + /// Returns the quotient of floor(dividend / divisor) and computes the remainder + CUTLASS_HOST_DEVICE + uint64_t divmod(uint64_t &remainder, uint64_t dividend) const { + uint64_t quotient = divide(dividend); + remainder = modulus(quotient, dividend); + return quotient; + } + + /// Computes integer division and modulus using precomputed values. This is computationally + /// inexpensive. + CUTLASS_HOST_DEVICE + void operator()(uint64_t "ient, uint64_t &remainder, uint64_t dividend) const { + quotient = divmod(remainder, dividend); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Computes the coordinate decomposition from a linear index (64-bit linear index => coord) +/// +/// This decomposition is accelerated by the FastDivmodU64 object. It is assumed that +/// a coordinate of indices can be decomposed by div/mod operations. +/// Note, is assumed that element divmod[0] divides by extent[1]. +/// +/// For example, assume 4-D coordinate (n, p, q, c) is mapped to a linear index `npqc`. This +/// can be decomposed via three divide and modulus operations: +/// +/// c = npqc % C; | divmod[2] = FastDivmodU64(C) +/// npq = npqc / C; | coord[3] = c +/// +/// q = npq % Q; | divmod[1] = FastDivmodU64(Q) +/// np = npq / Q; | coord[2] = q +/// +/// p = np % P; | divmod[0] = FastDivmodU64(P) +/// n = np / P; | coord[1] = p +/// +/// | coord[0] = n +/// +template +CUTLASS_HOST_DEVICE Coord CoordinateDecomposition( + uint64_t linear_idx, ///< Linear index to decompose + FastDivmodU64 const *divmod) { ///< Pointer to array of Rank-1 FastDivmodU64 objects + + static_assert(Rank > 0, "CoordinateDecomposition requires Rank=1 or greater."); + + Coord coord; + + CUTLASS_PRAGMA_UNROLL + for (int i = Rank; i > 1; --i) { + uint64_t remainder; + linear_idx = divmod[i - 2].divmod(remainder, linear_idx); + coord[i - 1] = int(remainder); + } + + coord[0] = int(linear_idx); + + return coord; +} + +/// Computes the coordinate decomposition from a linear index (32-bit linear index => coord) +template +CUTLASS_HOST_DEVICE Coord CoordinateDecomposition( + int linear_idx, ///< Linear index to decompose + FastDivmod const *divmod) { ///< Pointer to array of Rank-1 FastDivmodU64 objects + + static_assert(Rank > 0, "CoordinateDecomposition requires Rank=1 or greater."); + + Coord coord; + + CUTLASS_PRAGMA_UNROLL + for (int i = Rank; i > 1; --i) { + int remainder; + linear_idx = divmod[i - 2].divmod(remainder, linear_idx); + coord[i - 1] = int(remainder); + } + + coord[0] = int(linear_idx); + + return coord; +} + +template +CUTLASS_HOST_DEVICE Coord CoordinateDecompositionLittleEndian( + uint64_t linear_idx, ///< Linear index to decompose + FastDivmodU64 const *divmod) { ///< Pointer to array of Rank-1 FastDivmodU64 objects + + static_assert(Rank > 0, "CoordinateDecomposition requires Rank=1 or greater."); + + Coord coord; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank - 1; ++i) { + uint64_t remainder; + linear_idx = divmod[i].divmod(remainder, linear_idx); + coord[i] = int(remainder); + } + + coord[Rank - 1] = int(linear_idx); + + return coord; +} + +/// Computes the coordinate decomposition from a linear index (32-bit linear index => coord) +template +CUTLASS_HOST_DEVICE Coord CoordinateDecompositionLittleEndian( + int linear_idx, ///< Linear index to decompose + FastDivmod const *divmod) { ///< Pointer to array of Rank-1 FastDivmodU64 objects + + static_assert(Rank > 0, "CoordinateDecomposition requires Rank=1 or greater."); + + Coord coord; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Rank - 1; ++i) { + int remainder; + linear_idx = divmod[i].divmod(remainder, linear_idx); + coord[i] = int(remainder); + } + + coord[Rank - 1] = int(linear_idx); + + return coord; +} + +/// Safely computes the offset of a linear index in bytes for all types +template +CUTLASS_HOST_DEVICE int64_t OffsetBytes(int64_t index) { + + static_assert( + (sizeof_bits::value >= 8 && !(sizeof_bits::value % 8)) || + (sizeof_bits::value < 8 && !(8 % sizeof_bits::value)), + "Size of numeric type in bits must either be divisible by 8 bits, or 8 bits must be divisible by the size."); + + if (sizeof_bits::value >= 8) { + return index * (sizeof_bits::value / 8); + } + else { + int const kElementsPerByte = ((8 / sizeof_bits::value) + ((sizeof_bits::value >= 8) ? 1 : 0)); + return index / kElementsPerByte; + } +} + +CUTLASS_HOST_DEVICE int64_t OffsetBytes(int64_t index, int64_t element_sizeof_bits) { + if (element_sizeof_bits >= 8) { + return index * (element_sizeof_bits / 8); + } + else { + int64_t const kElementsPerByte = ((8 / element_sizeof_bits) + ((element_sizeof_bits >= 8) ? 1 : 0)); + return index / kElementsPerByte; + } +} + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// Min/Max +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct Min { + static int const kValue = (A < B) ? A : B; +}; + +template +struct Max { + static int const kValue = (A > B) ? A : B; +}; + +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 int const_min(int a, int b) { + return (b < a ? b : a); +} + +CUTLASS_HOST_DEVICE +CUTLASS_CONSTEXPR_IF_CXX17 int const_max(int a, int b) { + return (b > a ? b : a); +} + +template +CUTLASS_HOST_DEVICE +T fast_min(T a, T b) { + return (b < a ? b : a); +} + +template <> +CUTLASS_HOST_DEVICE +float fast_min(float a, float b) { + return fminf(a, b); +} + +template +CUTLASS_HOST_DEVICE +T fast_max(T a, T b) { + return (a < b ? b : a); +} + +template <> +CUTLASS_HOST_DEVICE +float fast_max(float a, float b) { + return fmaxf(a, b); +} + +CUTLASS_HOST_DEVICE +float fast_cos(float theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::cosf(theta); + #else + return std::cos(theta); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_cos(double theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::cos(theta); + #else + return std::cos(theta); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_sin(float theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::sinf(theta); + #else + return std::sin(theta); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_sin(double theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::sin(theta); + #else + return std::sin(theta); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_acos(float theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::acosf(theta); + #else + return std::acos(theta); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_acos(double theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::acos(theta); + #else + return std::acos(theta); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_asin(float theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::asinf(theta); + #else + return std::asin(theta); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_asin(double theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::asin(theta); + #else + return std::asin(theta); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_sqrt(float theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::sqrtf(theta); + #else + return std::sqrt(theta); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_sqrt(double theta) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::sqrt(theta); + #else + return std::sqrt(theta); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_exp(float x) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::expf(x); + #else + return std::exp(x); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_exp(double x) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::exp(x); + #else + return std::exp(x); + #endif +} + +CUTLASS_HOST_DEVICE +half_t fast_exp(half_t x) { + #if defined(__HIP_DEVICE_COMPILE__) && (__CUDACC_VER_MAJOR__ >= 10) && (__HIP_DEVICE_COMPILE__ >= 750) + return (half_t)(::hexp(x.to_half())); + #else + return (half_t)(fast_exp(float(x))); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_log(float x) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::logf(x); + #else + return std::log(x); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_log(double x) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::log(x); + #else + return std::log(x); + #endif +} + +CUTLASS_HOST_DEVICE +float fast_tanh(float x) { + #if defined(__HIP_DEVICE_COMPILE__) + #if (__CUDACC_VER_MAJOR__ >= 11) && (__HIP_DEVICE_COMPILE__ >= 750) + float y; + asm volatile ( "tanh.approx.f32 %0, %1; " : "=f"(y) : "f"(x)); + return y; + #else + return ::tanhf(x); + #endif + #else + return std::tanh(x); + #endif +} + +CUTLASS_HOST_DEVICE +double fast_tanh(double x) { + #if defined(__HIP_DEVICE_COMPILE__) + return ::tanh(x); + #else + return std::tanh(x); + #endif +} + +CUTLASS_HOST_DEVICE +half_t fast_tanh(half_t x) { + #if defined(__HIP_DEVICE_COMPILE__) && (__CUDACC_VER_MAJOR__ >= 11) && (__HIP_DEVICE_COMPILE__ >= 750) + + asm volatile ( "tanh.approx.f16 %0, %1;" : "=h"(x.raw()) : "h"(x.raw())); + return x; + + #else + return half_t(fast_tanh(float(x))); + #endif +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct fast_exp_op { + CUTLASS_HOST_DEVICE + T operator()(T const &rhs) const { + return fast_exp(rhs); + } +}; + +#if defined(__HIP_DEVICE_COMPILE__) && (__CUDACC_VER_MAJOR__ >= 10) && (__HIP_DEVICE_COMPILE__ >= 750) +template +struct fast_exp_op> { + CUTLASS_DEVICE + Array operator()(Array const &rhs) const { + + Array result; + + // use x2 specialization + __half2 const *in = reinterpret_cast<__half2 const *>(&rhs); + __half2 *out = reinterpret_cast<__half2 *>(&result); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + out[i] = ::h2exp(in[i]); + } + + // residual + if (N % 2) { + half_t last = rhs[N - 1]; + result[N - 1] = half_t(::hexp(last.to_half())); + } + + return result; + } +}; +#endif // #if defined(__HIP_DEVICE_COMPILE__) + +template +struct fast_exp_op> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &rhs) const { + + fast_exp_op fast_op; + Array y; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + y[i] = fast_op(rhs[i]); + } + + return y; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct fast_tanh_op { + CUTLASS_HOST_DEVICE + T operator()(T const &rhs) const { + return fast_tanh(rhs); + } +}; + +#if defined(__HIP_DEVICE_COMPILE__) && (__CUDACC_VER_MAJOR__ >= 11) && (__HIP_DEVICE_COMPILE__ >= 750) +template +struct fast_tanh_op> { + CUTLASS_DEVICE + Array operator()(Array const &rhs) const { + + Array result; + + // use x2 specialization + uint32_t const *in = reinterpret_cast(&rhs); + uint32_t *out = reinterpret_cast(&result); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + asm volatile ("tanh.approx.f16x2 %0, %1;" : "=r"(out[i]) : "r"(in[i])); + } + + // residual + if (N % 2) { + uint16_t const *in = reinterpret_cast(&rhs); + uint16_t *out = reinterpret_cast(&result); + asm volatile ("tanh.approx.f16 %0, %1;" : "=h"(out[N - 1]) : "h"(in[N - 1])); + } + + return result; + } +}; +#endif // #if defined(__HIP_DEVICE_COMPILE__) + +template +struct fast_tanh_op> { + CUTLASS_HOST_DEVICE + Array operator()(Array const &rhs) const { + + fast_tanh_op fast_op; + Array y; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + y[i] = fast_op(rhs[i]); + } + + return y; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Absolute value function +template +CUTLASS_HOST_DEVICE +T absolute_value(T x) { + if (x < T()) { + return -x; + } + return x; +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/float8.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/float8.h new file mode 100644 index 000000000..1cd08db58 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/float8.h @@ -0,0 +1,1516 @@ +/************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines a class for using IEEE half-precision floating-point types in host or + device code. +*/ + +#pragma once + +// FP8 types are available starting CUDA 11.8+ +#if (__CUDACC_VER_MAJOR__ >= 12) || ((__CUDACC_VER_MAJOR__ == 11) && (__CUDACC_VER_MINOR__ >= 8)) +//#define CUDA_FP8_ENABLED 1 +#endif +#undef CUDA_FP8_ENABLED + +#if defined(__HIP_DEVICE_COMPILE__) +# if (__HIP_DEVICE_COMPILE__ >= 900) +# if (__CUDACC_VER_MAJOR__ >= 12) || ((__CUDACC_VER_MAJOR__ == 11) && (__CUDACC_VER_MINOR__ >= 8)) +# define CUDA_PTX_FP8_CVT_ENABLED 1 +# endif // (__CUDACC_VER_MAJOR__ >= 12) || ((__CUDACC_VER_MAJOR__ == 11) && (__CUDACC_VER_MINOR__ >= 8)) +# elif (__HIP_DEVICE_COMPILE__ == 890) +# if (__CUDACC_VER_MAJOR__ > 12) || ((__CUDACC_VER_MAJOR__ == 12) && (__CUDACC_VER_MINOR__ >= 1)) +# define CUDA_PTX_FP8_CVT_ENABLED 1 +# endif // (__CUDACC_VER_MAJOR__ > 12) || ((__CUDACC_VER_MAJOR__ == 12) && (__CUDACC_VER_MINOR__ >= 1)) +# endif // (__HIP_DEVICE_COMPILE__ >= 900) +#endif // defined(__HIP_DEVICE_COMPILE__) + +#ifdef __GNUC__ +// Ignore checks on reinterpret-casts that are being used for bitcasts. +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif + +#if (defined(__gfx938__)) && __HIP_DEVICE_COMPILE__ +#define HIP_FP8_CVT_ENABLE 1 +#endif + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(__CUDACC_RTC__) + +#include "cutlass/floating_point_nvrtc.h" + +#else +// +// Standard Library headers belong here to avoid conflicts with NVRTC. +// +#include +#include +#include +#include +#endif + +#ifdef CUDA_FP8_ENABLED + +#include +#endif +#include +#include +#include +#include + +#include "cutlass/cutlass.h" +#include "cutlass/numeric_size.h" +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// FP8 Has 2 encodings possible : E4M3 and E5M2 +// +// E4M3 : 7 | 6 5 4 3 | 2 1 0 +// E5M2 : 7 | 6 5 4 3 2 | 1 0 +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct data_type_traits; + +template<> +struct data_type_traits { + static constexpr int NUM_BITS = 32; + static constexpr int NUM_EXPONENT_BITS = 8; + static constexpr int NUM_MANTISSA_BITS = 23; + static constexpr int NUM_EXPECT_SIGN = 31; + static constexpr uint32_t MAX_NAN = 0x7fffffff; // max nan + static constexpr uint32_t INFINITY_MASK = 0x7f800000; // inf mask + static constexpr int MAX_EXPONENT = 127; + static constexpr int MIN_EXPONENT = -126; + static constexpr int EXPONENT_BIAS = 127; + + using storage_type = uint32_t; + + CUTLASS_HOST_DEVICE + static bool isfinite(float flt) { + uint32_t s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + + return (s & INFINITY_MASK) < INFINITY_MASK; + } + + CUTLASS_HOST_DEVICE + static bool isnan(float flt) { + uint32_t s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + return (s & 0x7fffffff) > INFINITY_MASK; + } + + CUTLASS_HOST_DEVICE + static bool isinf(float flt) { + uint32_t s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + return (s == INFINITY_MASK) || (s == (INFINITY_MASK | (1 << NUM_EXPECT_SIGN))); + } +}; + + +template<> +struct data_type_traits<__half> { + static constexpr int NUM_BITS = 16; + static constexpr int NUM_EXPONENT_BITS = 5; + static constexpr int NUM_MANTISSA_BITS = 10; + static constexpr int NUM_EXPECT_SIGN = 15; + static constexpr uint32_t MAX_NAN = 0x7fff; + static constexpr uint32_t INFINITY_MASK = 0x7C00; + static constexpr int MAX_EXPONENT = 15; + static constexpr int MIN_EXPONENT = -14; + static constexpr int EXPONENT_BIAS = 15; + + using storage_type = uint16_t; + + CUTLASS_HOST_DEVICE + static bool isfinite(__half flt) { + storage_type s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + return (s & INFINITY_MASK) < INFINITY_MASK; + } + + CUTLASS_HOST_DEVICE + static bool isnan(__half flt) { + storage_type s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + return (s & 0x7fff) > INFINITY_MASK; + } + + CUTLASS_HOST_DEVICE + static bool isinf(__half flt) { + storage_type s; +#ifdef __HIPCC__ + s = reinterpret_cast(flt); +#else + std::memcpy(&s, &flt, sizeof(s)); +#endif + return (s == INFINITY_MASK) || (s == (INFINITY_MASK | (1 << NUM_EXPECT_SIGN))); + } +}; + + +enum class FloatEncoding { + E4M3, + E5M2 +}; + +template +struct alignas(1) float8_base { + + static constexpr bool IS_E4M3 = (T == FloatEncoding::E4M3); + static constexpr bool IS_E5M2 = (T == FloatEncoding::E5M2); + + // Number of Bits representing mantissa and exponents + static constexpr int FP32_NUM_BITS = 32; + static constexpr int FP32_NUM_EXPONENT_BITS = 8; + static constexpr int FP32_NUM_MANTISSA_BITS = 23; + static constexpr uint32_t FP32_NAN = 0x7fffffff; + static constexpr uint32_t FP32_INFINITY_MASK = 0x7f800000; + static constexpr int FP32_MAX_EXPONENT = 127; + static constexpr int FP32_MIN_EXPONENT = -126; + static constexpr int FP32_EXPONENT_BIAS = 127; + + static constexpr int FP16_NUM_BITS = 16; + static constexpr int FP16_NUM_EXPONENT_BITS = 5; + static constexpr int FP16_NUM_MANTISSA_BITS = 10; + static constexpr uint16_t FP16_NAN = 0x7fff; + static constexpr uint16_t FP16_INFINITY_MASK = 0x7c00; + static constexpr int FP16_MAX_EXPONENT = 15; + static constexpr int FP16_MIN_EXPONENT = -14; + static constexpr int FP16_EXPONENT_BIAS = 15; + + static constexpr int FP8_NUM_BITS = 8; + static constexpr int FP8_NUM_EXPONENT_BITS = IS_E4M3 ? 4 : 5; + static constexpr int FP8_NUM_MANTISSA_BITS = IS_E4M3 ? 3 : 2; + static constexpr uint8_t FP8_NAN = 0x7f; // Also F8_INF + static constexpr uint8_t FP8_INFINITY_MASK = IS_E4M3 ? 0x78 : 0x7c; + static constexpr int FP8_MAX_EXPONENT = IS_E4M3 ? 7 : 15; + static constexpr int FP8_MIN_EXPONENT = IS_E4M3 ? -6 : -14; + static constexpr int FP8_EXPONENT_BIAS = IS_E4M3 ? 7 : 15; + + static constexpr uint8_t FP8_EXPONENT_MASK = (1 << FP8_NUM_EXPONENT_BITS) - 1; + static constexpr uint8_t FP8_MANTISSA_MASK = (1 << FP8_NUM_MANTISSA_BITS) - 1; + + static constexpr uint8_t FP8_MAX_FLT = (IS_E4M3 ? 0x7e : 0x7b); + + // 256 in float + static constexpr uint32_t FP8_SAT_VAL_FP32 = 0x43800000; + + // + // Data members + // + + /// Data container + uint8_t storage; + + /// Ctors. + CUTLASS_HOST_DEVICE + float8_base() : storage(0) { } + + /// Is finite implementation + CUTLASS_HOST_DEVICE + static bool isfinite(float flt) { + uint32_t s; + + #if defined(__HIP_DEVICE_COMPILE__) + s = reinterpret_cast(flt); + #else + std::memcpy(&s, &flt, sizeof(s)); + #endif + + return (s & 0x7f800000) < 0x7f800000; + } + + /// Is NaN implementation + CUTLASS_HOST_DEVICE + static bool isnan(float flt) { + uint32_t s; + + #if defined(__HIP_DEVICE_COMPILE__) + s = reinterpret_cast(flt); + #else + std::memcpy(&s, &flt, sizeof(s)); + #endif + + return (s & 0x7fffffff) > 0x7f800000; + } + + /// Is infinite implementation + CUTLASS_HOST_DEVICE + static bool isinf(float flt) { + uint32_t s; + + #if defined(__HIP_DEVICE_COMPILE__) + s = reinterpret_cast(flt); + #else + std::memcpy(&s, &flt, sizeof(s)); + #endif + + // Sign = 0 for +inf, 1 for -inf + // Exponent = all ones + // Mantissa = all zeros + return (s == 0x7f800000) || (s == 0xff800000); + } + +#if defined(HIP_FP8_CVT_ENABLE) + template + CUTLASS_DEVICE + static uint8_t convert_float_to_fp8_device_impl(float const& flt) { + union { + float fval; + uint32_t uval; + } val; + + val.fval = flt; + + if constexpr (SAT) { + if ((val.uval & 0x7F800000) != 0x7F800000) { + if constexpr (IS_E4M3) { + val.fval = __builtin_amdgcn_fmed3f(val.fval, 448.0, -448.0); + } else { + val.fval = __builtin_amdgcn_fmed3f(val.fval, 57344.0, -57334.0); + } + } + } + + + uint32_t dst; + + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wuninitialized" + // __builtin_amdgcn_cvt_pk_fp8_f32() this builtin require the old value, and + // will generate a v_mov_b32 vxxx [old] before cvt, which result in unwanted ISA + // so we prepare an uninitialized variable purposely, and turn off the warning + int dummy_old; + float tmp = 0.0f; + if constexpr (IS_E4M3) { + // float32 -> fp8 + dst = __builtin_hcu_cvt_pk_fp8_f32(val.fval, val.fval, dummy_old, false); + } else { + dst = __builtin_hcu_cvt_pk_bf8_f32(val.fval, val.fval, dummy_old, false); + } + #pragma clang diagnostic pop + + return (dst & 0xFF); + } + + CUTLASS_DEVICE + static float convert_fp8_to_float_device_impl(uint8_t const& x) { + float result = float(); + uint32_t val = static_cast(x); + if constexpr (IS_E4M3) { + // asm volatile("v_cvt_f32_fp8 %0 %1\n\t": "=v"(result): "v"(val)); + result = __builtin_amdgcn_cvt_f32_fp8(val, 0); + } else { + // asm volatile("v_cvt_f32_bf8 %0 %1\n\t": "=v"(result): "v"(val)); + result = __builtin_amdgcn_cvt_f32_bf8(val, 0); + } + return result; + } +#endif + + // software implementation rounds toward nearest even + template + CUTLASS_HOST_DEVICE + static uint8_t convert_to_fp8(SRC_TYPE const& flt) { + using src_desc = data_type_traits; + using src_stg_type = typename src_desc::storage_type; + src_stg_type s; + + static_assert(sizeof_bits::value == 16 || sizeof_bits::value == 32); + + #if defined(__HIPCC__) + s = reinterpret_cast(flt); + #else + std::memcpy(&s, &flt, sizeof(s)); + #endif + + // Extract the bits in the SRC type + uint8_t sign = uint8_t((s >> (src_desc::NUM_BITS - 8) & 0x80)); + + int exp = int( + (s >> src_desc::NUM_MANTISSA_BITS) & ((1 << src_desc::NUM_EXPONENT_BITS) - 1)) - + src_desc::EXPONENT_BIAS; + + int mantissa = s & ((1 << src_desc::NUM_MANTISSA_BITS) - 1); + + uint8_t u = 0; + // sim with inst + uint8_t const kF8_NaN = IS_E4M3 ? 0x7f : 0x7e; + uint8_t const kF8_INF = IS_E4M3 ? kF8_NaN : FP8_INFINITY_MASK; + + // NaN => NaN + if (src_desc::isnan(flt)) { + return kF8_NaN; + } + + // Inf => MAX_FLT (satfinite) + if (src_desc::isinf(flt)) { + if constexpr (SAT) { + return sign | FP8_MAX_FLT; + } else { + return sign | kF8_INF; + } + } + + int sticky_bit = 0; + bool skip_sign = false; + + if ( (exp >= FP8_MIN_EXPONENT) && (exp <= FP8_MAX_EXPONENT) ) { + // normal fp32 to normal fp8 + exp = exp + FP8_EXPONENT_BIAS; + u = uint8_t((uint32_t(exp) & FP8_EXPONENT_MASK) << FP8_NUM_MANTISSA_BITS); + u = uint8_t(u | (mantissa >> (src_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS))); + } else if(exp < FP8_MIN_EXPONENT) { + // for snorm + int rshift = (FP8_MIN_EXPONENT - exp); + if ((s & src_desc::INFINITY_MASK) == 0) { + // If the exp bits of src are all 0, which means it is an snorm. + u = (uint8_t(mantissa >> (src_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)) & FP8_MANTISSA_MASK); + } else if (rshift < src_desc::NUM_MANTISSA_BITS) { + // source is norm + mantissa |= (1 << src_desc::NUM_MANTISSA_BITS); + sticky_bit = ((mantissa & ((1 << rshift) - 1)) != 0); + mantissa = (mantissa >> rshift); + u = (uint8_t(mantissa >> (src_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)) & FP8_MANTISSA_MASK); + } else { + mantissa = 0; + u = 0; + } + // Exponent > FP8_MAX_EXPONENT - this is a special case done to match HW + // 0x4380_0000 to 0x43e0_0000 - maps from 256 to 448, and does not saturate / inf. + } else { + if( exp == (FP8_MAX_EXPONENT + 1) ) { + uint8_t mantissa_tmp = uint8_t(mantissa >> (src_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)); + if( mantissa_tmp < FP8_MANTISSA_MASK) { + exp = exp + FP8_EXPONENT_BIAS; + u = uint8_t(uint32_t(exp) << FP8_NUM_MANTISSA_BITS) | mantissa_tmp; + + + // may_be_nan = (mantissa_tmp == (FP8_MANTISSA_MASK-1)); + } else { + if constexpr (SAT) { + return (sign | FP8_MAX_FLT); + } else { + return (sign | kF8_INF); + } + } + } else{ + if constexpr (SAT) { + return (sign | FP8_MAX_FLT); + } else { + return (sign | kF8_INF); + } + } + } + + // round to nearest even + int NUM_BITS_SHIFT = src_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS - 1; + int round_bit = ((mantissa >> NUM_BITS_SHIFT) & 1); + sticky_bit |= ((mantissa & ((1 << NUM_BITS_SHIFT) - 1)) != 0); + + if ((round_bit && sticky_bit) || (round_bit && (u & 1))) { + u = uint8_t(u + 1); + } + + if (u > FP8_MAX_FLT) { + if constexpr (SAT) { + u = (sign | FP8_MAX_FLT); + } else { + u = (sign | kF8_INF); + } + return u; + } + + u |= sign; + + return u; + } + + /// FP32 -> FP8 conversion - rounds to nearest even + template + CUTLASS_HOST_DEVICE + static uint8_t convert_float_to_fp8(float const& flt) { +#if defined(HIP_FP8_CVT_ENABLE) + return convert_float_to_fp8_device_impl(flt); +#else + return convert_to_fp8(flt); +#endif + } + + template + CUTLASS_HOST_DEVICE + static auto convert_from_fp8(uint8_t x)-> DST_TYPE { + using dst_desc = data_type_traits; + using dst_stg_type = typename dst_desc::storage_type; + + static_assert(sizeof_bits::value == 16 || + sizeof_bits::value == 32); + + dst_stg_type constexpr src_NaN = dst_desc::MAX_NAN; + + uint8_t const& fp8 = x; + uint32_t sign = (fp8 >> (FP8_NUM_BITS - 1)) & 1; + + uint32_t exp = (fp8 >> FP8_NUM_MANTISSA_BITS) & FP8_EXPONENT_MASK; + uint32_t mantissa = fp8 & FP8_MANTISSA_MASK; + + dst_stg_type f = (sign << (dst_desc::NUM_BITS - 1)); + + if (IS_E4M3 && exp == 15 && mantissa == 0x7) { + f = src_NaN; + } else if (exp > 0 && (IS_E4M3 || exp < (FP8_MAX_EXPONENT + FP8_EXPONENT_BIAS + 1))) { + exp += (dst_desc::EXPONENT_BIAS - FP8_EXPONENT_BIAS); + + f = f | + (exp << dst_desc::NUM_MANTISSA_BITS) | + (mantissa << (dst_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)); + } else if (exp == 0) { + // snorm + if (mantissa) { + if constexpr (dst_desc::NUM_EXPONENT_BITS == FP8_NUM_EXPONENT_BITS) { + f = f | (mantissa << (dst_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)); + } else { + exp += (dst_desc::EXPONENT_BIAS - FP8_EXPONENT_BIAS) + 1; + while ((mantissa & (1 << FP8_NUM_MANTISSA_BITS)) == 0) { + mantissa <<= 1; + exp--; + } + mantissa &= FP8_MANTISSA_MASK; + + f = f | + (exp << dst_desc::NUM_MANTISSA_BITS) | + (mantissa << (dst_desc::NUM_MANTISSA_BITS - FP8_NUM_MANTISSA_BITS)); + } + } + } else { + if (mantissa == 0) { + f = (f | dst_desc::INFINITY_MASK); + } else { + f = src_NaN; + } + } + + #ifdef __HIPCC__ + return reinterpret_cast(f); + #else + DST_TYPE flt; + std::memcpy(&flt, &f, sizeof(flt)); + return flt; + #endif + } + + /// Converts a fp8 value stored as a uint8_t to a float + CUTLASS_HOST_DEVICE + static float convert_fp8_to_float(uint8_t const& x) { +#if defined(HIP_FP8_CVT_ENABLE) + return convert_fp8_to_float_device_impl(x); +#else + return convert_from_fp8(x); +#endif + } +}; + + +// Forward declaration of float_e5m2_t to define float_e4m3_t <=> float_e5m2_t +// conversions in class float_e4m3_t +struct float_e5m2_t; + + +/////////////////////////////////////////////////////////////// +/// +/// floating-point 8 type : E4M3 +/// +/////////////////////////////////////////////////////////////// +struct alignas(1) float_e4m3_t : float8_base { + + using Base = float8_base; + + static constexpr int MAX_EXPONENT = Base::FP8_MAX_EXPONENT; + + // + // Static conversion operators + // + + /// Constructs from an uint8_t + CUTLASS_HOST_DEVICE + static float_e4m3_t bitcast(uint8_t x) { + float_e4m3_t f; + f.storage = x; + return f; + } + + /// FP32 -> FP8 conversion - rounds to nearest even + template + CUTLASS_HOST_DEVICE + static float_e4m3_t from_float(float const& flt) { + return bitcast(Base::convert_float_to_fp8(flt)); + } + + /// FP16 -> E5M2 conversion - rounds to nearest even + + template + CUTLASS_HOST_DEVICE + static float_e4m3_t from_half(__half const& flt) { +#if defined(HIP_FP8_CVT_ENABLE) + + return bitcast(Base::convert_float_to_fp8(__half2float(flt))); +#else + + return bitcast(Base::convert_to_fp8<__half, SAT>(flt)); +#endif + } + + template + CUTLASS_HOST_DEVICE + static float_e4m3_t from_bfloat16(__hip_bfloat16 const& flt) { + + return bitcast(Base::convert_float_to_fp8(__bfloat162float(flt))); + } + + // E4M3 -> half + CUTLASS_HOST_DEVICE + static __half to_half(float_e4m3_t const& x) { +#if defined(HIP_FP8_CVT_ENABLE) + return __float2half(Base::convert_fp8_to_float(x.storage)); +#else + return Base::convert_from_fp8<__half>(x.storage); +#endif + } + + CUTLASS_HOST_DEVICE + static __hip_bfloat16 to_bfloat16(float_e4m3_t const& x) { + + return __float2bfloat16(Base::convert_from_fp8(x.storage)); + } + + // E4M3 -> Float + CUTLASS_HOST_DEVICE + static float to_float(float_e4m3_t const& x) { + return Base::convert_fp8_to_float(x.storage); + } + + // + // Methods + // + + /// Constructor inheritance + using Base::Base; + + /// Default constructor + float_e4m3_t() = default; + +#ifdef CUDA_FP8_ENABLED + /// Conversion from CUDA's FP8 type + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(__nv_fp8_e4m3 x) { + storage = x.__x; + } +#endif + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(float x) { + storage = from_float(x).storage; + } + + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(__half x) { + storage = from_half(x).storage; + } + + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(__hip_bfloat16 x) { + storage = from_bfloat16(x).storage; + } + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(double x): float_e4m3_t(float(x)) { + } + + /// Integer conversion + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(int x): float_e4m3_t(float(x)) { + } + + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(unsigned x): float_e4m3_t(float(x)) { + } + + /// E5M2 conversion. Defined after float_e5m2_t is defined. + CUTLASS_HOST_DEVICE + explicit float_e4m3_t(float_e5m2_t x); + + +#ifdef CUDA_FP8_ENABLED + /// Assignment from CUDA's FP8 type + CUTLASS_HOST_DEVICE + float_e4m3_t & operator=(__nv_fp8_e4m3 x) { + storage = x.__x; + return *this; + } +#endif + + /// Converts to float + CUTLASS_HOST_DEVICE + operator float() const { + return to_float(*this); + } + + /// Converts to half + CUTLASS_HOST_DEVICE + operator __half() const { + return to_half(*this); + } + + + /// Converts to bfloat16 + CUTLASS_HOST_DEVICE + operator __hip_bfloat16() const { + return to_bfloat16(*this); + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator double() const { + return double(to_float(*this)); + } + + /// Converts to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + #if defined(__HIP_DEVICE_COMPILE__) + return __half2int_rn(to_half(*this)); + #else + return int(to_float(*this)); + #endif + } + + /// Casts to bool + CUTLASS_HOST_DEVICE + explicit operator bool() const { + #if defined(__HIP_DEVICE_COMPILE__) + return bool(__half2int_rn(to_half(*this))); + #else + return bool(int(to_float(*this))); + #endif + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint8_t& raw() { + return storage; + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint8_t raw() const { + return storage; + } + + /// Returns the sign bit + CUTLASS_HOST_DEVICE + bool signbit() const { + return ((storage & (1 << (Base::FP8_NUM_BITS - 1))) != 0); + } + + /// Returns the biased exponent + CUTLASS_HOST_DEVICE + int exponent_biased() const { + return int((storage >> FP8_NUM_MANTISSA_BITS) & Base::FP8_EXPONENT_MASK); + } + + /// Returns the unbiased exponent + CUTLASS_HOST_DEVICE + int exponent() const { + return exponent_biased() - 15; + } + + /// Returns the mantissa + CUTLASS_HOST_DEVICE + int mantissa() const { + return int(storage & Base::FP8_MANTISSA_MASK); + } +}; +/////////////////////////////////////////////////////////////// +/// +/// floating-point 8 type : E5M2 +/// +/////////////////////////////////////////////////////////////// +struct alignas(1) float_e5m2_t : float8_base { + + using Base = float8_base; + + static constexpr int MAX_EXPONENT = Base::FP8_MAX_EXPONENT; + + // + // Static conversion operators + // + + /// Constructs from an uint8_t + CUTLASS_HOST_DEVICE + static float_e5m2_t bitcast(uint8_t x) { + float_e5m2_t f; + f.storage = x; + return f; + } + + /// FP32 -> FP8 conversion - rounds to nearest even + template + CUTLASS_HOST_DEVICE + static float_e5m2_t from_float(float const& flt) { + return bitcast(Base::convert_float_to_fp8(flt)); + } + + /// FP16 -> E5M2 conversion - rounds to nearest even + template + CUTLASS_HOST_DEVICE + static float_e5m2_t from_half(__half const& flt) { +#if defined(HIP_FP8_CVT_ENABLE) + return bitcast(Base::convert_float_to_fp8(__half2float(flt))); +#else + uint16_t s; + float_e5m2_t res; + #ifdef __HIPCC__ + s = reinterpret_cast(flt); + #else + memcpy(&s, &flt, sizeof(uint16_t)); + #endif + + uint8_t sign = (s & 0x8000) >> 8; + + constexpr uint8_t kFP8_INF = SAT ? 0x7B : 0x7C; + + if ((s & 0x7C00) == 0x7C00) { + // source is inf/nan + + uint8_t exp_and_mantissa = (s & 0x03FF) ? 0x7E : kFP8_INF; + res = float_e5m2_t::bitcast(((s & 0x8000) >> 8) | exp_and_mantissa); + return res; + } else if (~s & 0x7C00) { + + uint16_t round_bit = 0x7F + ((s >> 8) & 1); + s += round_bit; + } + + if ((s & 0x7C00) == 0x7C00) { + res = float_e5m2_t::bitcast(sign | kFP8_INF); + return res; + } + + res = float_e5m2_t::bitcast(s >> 8); + return res; +#endif + } + + template + CUTLASS_HOST_DEVICE + static float_e5m2_t from_bfloat16(__hip_bfloat16 const& flt) { + return bitcast(Base::convert_float_to_fp8(__bfloat162float(flt))); + } + + // E5M2 -> half + CUTLASS_HOST_DEVICE + static half to_half(float_e5m2_t const& x) { + uint8_t s = x.storage; + uint16_t data = s << 8; +#if defined(__HIPCC__) + return reinterpret_cast<__half const&>(data); +#else + __half res; + std::memcpy(&res, &data, sizeof(uint16_t)); + return res; +#endif + } + + CUTLASS_HOST_DEVICE + static __hip_bfloat16 to_bfloat16(float_e5m2_t const& x) { + return __float2bfloat16(Base::convert_fp8_to_float(x.storage)); + } + + // E5M2 -> Float + CUTLASS_HOST_DEVICE + static float to_float(float_e5m2_t const& x) { + return Base::convert_fp8_to_float(x.storage); + } + + // + // Methods + // + + /// Constructor inheritance + using Base::Base; + + /// Default constructor + float_e5m2_t() = default; + +#ifdef CUDA_FP8_ENABLED + /// Conversion from CUDA's FP8 type + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(__nv_fp8_e5m2 x) { + storage = x.__x; + } +#endif + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(float x) { + storage = from_float(x).storage; + } + + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(__half x) { + storage = from_half(x).storage; + } + + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(__hip_bfloat16 x) { + storage = from_bfloat16(x).storage; + } + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(double x): float_e5m2_t(float(x)) { + } + + /// Integer conversion + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(int x): float_e5m2_t(float(x)) { + } + + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(unsigned x): float_e5m2_t(float(x)) { + } + + /// E4M3 conversion + CUTLASS_HOST_DEVICE + explicit float_e5m2_t(float_e4m3_t x); + +#ifdef CUDA_FP8_ENABLED + /// Assignment from CUDA's FP8 type + CUTLASS_HOST_DEVICE + float_e5m2_t & operator=(__nv_fp8_e5m2 x) { + storage = x.__x; + return *this; + } +#endif + + /// Converts to float + CUTLASS_HOST_DEVICE + operator float() const { + return to_float(*this); + } + + /// Converts to half + CUTLASS_HOST_DEVICE + operator __half() const { + return to_half(*this); + } + + CUTLASS_HOST_DEVICE + operator __hip_bfloat16() const { + return to_bfloat16(*this); + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator double() const { + return double(to_float(*this)); + } + + /// Converts to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + #if defined(__HIP_DEVICE_COMPILE__) + return __half2int_rn(to_half(*this)); + #else + return int(to_float(*this)); + #endif + } + + /// Casts to bool + CUTLASS_HOST_DEVICE + explicit operator bool() const { + #if defined(__HIP_DEVICE_COMPILE__) + return bool(__half2int_rn(to_half(*this))); + #else + return bool(int(to_float(*this))); + #endif + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint8_t& raw() { + return storage; + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint8_t raw() const { + return storage; + } + + /// Returns the sign bit + CUTLASS_HOST_DEVICE + bool signbit() const { + return ((storage & (1 << (Base::FP8_NUM_BITS - 1))) != 0); + } + + /// Returns the biased exponent + CUTLASS_HOST_DEVICE + int exponent_biased() const { + return int((storage >> FP8_NUM_MANTISSA_BITS) & Base::FP8_EXPONENT_MASK); + } + + /// Returns the unbiased exponent + CUTLASS_HOST_DEVICE + int exponent() const { + return exponent_biased() - 15; + } + + /// Returns the mantissa + CUTLASS_HOST_DEVICE + int mantissa() const { + return int(storage & Base::FP8_MANTISSA_MASK); + } +}; +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Arithmetic operators +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool operator==(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) == float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator!=(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) != float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) < float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<=(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) <= float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) > float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>=(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float(lhs) >= float(rhs); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator+(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float_e4m3_t(float(lhs) + float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator-(float_e4m3_t const& lhs) { + return float_e4m3_t(-float(lhs)); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator-(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float_e4m3_t(float(lhs) - float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator*(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float_e4m3_t(float(lhs) * float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator/(float_e4m3_t const& lhs, float_e4m3_t const& rhs) { + return float_e4m3_t(float(lhs) / float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator+=(float_e4m3_t & lhs, float_e4m3_t const& rhs) { + lhs = float_e4m3_t(float(lhs) + float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator-=(float_e4m3_t & lhs, float_e4m3_t const& rhs) { + lhs = float_e4m3_t(float(lhs) - float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator*=(float_e4m3_t & lhs, float_e4m3_t const& rhs) { + lhs = float_e4m3_t(float(lhs) * float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator/=(float_e4m3_t & lhs, float_e4m3_t const& rhs) { + lhs = float_e4m3_t(float(lhs) / float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator++(float_e4m3_t & lhs) { + float tmp(lhs); + ++tmp; + lhs = float_e4m3_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t& operator--(float_e4m3_t & lhs) { + float tmp(lhs); + --tmp; + lhs = float_e4m3_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator++(float_e4m3_t & lhs, int) { + float_e4m3_t ret(lhs); + float tmp(lhs); + tmp++; + lhs = float_e4m3_t(tmp); + return ret; +} + +CUTLASS_HOST_DEVICE +float_e4m3_t operator--(float_e4m3_t & lhs, int) { + float_e4m3_t ret(lhs); + float tmp(lhs); + tmp--; + lhs = float_e4m3_t(tmp); + return ret; +} + +CUTLASS_HOST_DEVICE +bool operator==(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) == float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator!=(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) != float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) < float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<=(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) <= float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) > float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>=(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float(lhs) >= float(rhs); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator+(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float_e5m2_t(float(lhs) + float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator-(float_e5m2_t const& lhs) { + return float_e5m2_t(-float(lhs)); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator-(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float_e5m2_t(float(lhs) - float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator*(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float_e5m2_t(float(lhs) * float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator/(float_e5m2_t const& lhs, float_e5m2_t const& rhs) { + return float_e5m2_t(float(lhs) / float(rhs)); +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator+=(float_e5m2_t & lhs, float_e5m2_t const& rhs) { + lhs = float_e5m2_t(float(lhs) + float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator-=(float_e5m2_t & lhs, float_e5m2_t const& rhs) { + lhs = float_e5m2_t(float(lhs) - float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator*=(float_e5m2_t & lhs, float_e5m2_t const& rhs) { + lhs = float_e5m2_t(float(lhs) * float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator/=(float_e5m2_t & lhs, float_e5m2_t const& rhs) { + lhs = float_e5m2_t(float(lhs) / float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator++(float_e5m2_t & lhs) { + float tmp(lhs); + ++tmp; + lhs = float_e5m2_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t& operator--(float_e5m2_t & lhs) { + float tmp(lhs); + --tmp; + lhs = float_e5m2_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator++(float_e5m2_t & lhs, int) { + float_e5m2_t ret(lhs); + float tmp(lhs); + tmp++; + lhs = float_e5m2_t(tmp); + return ret; +} + +CUTLASS_HOST_DEVICE +float_e5m2_t operator--(float_e5m2_t & lhs, int) { + float_e5m2_t ret(lhs); + float tmp(lhs); + tmp--; + lhs = float_e5m2_t(tmp); + return ret; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// float_e4m3_t <=> float_e5m2_t conversions +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// float_e4m3_t <= float_e5m2_t +CUTLASS_HOST_DEVICE +float_e4m3_t::float_e4m3_t(float_e5m2_t x) { + storage = from_float(float_e5m2_t::to_float(x)).storage; +} + +/// float_e5m2_t <= float_e4m3_t +CUTLASS_HOST_DEVICE +float_e5m2_t::float_e5m2_t(float_e4m3_t x) { + storage = from_float(float_e4m3_t::to_float(x)).storage; +} + +/////////////////////////////////////////////////////////////// +/// +/// Umbrella floating-point 8-bit data type : type_erased_dynamic_float8_t +/// This umbrella datatype can be enabled when a user provides a specific +/// datatype in runtime argument list. +/// +/// Currently supported runtime datatypes compatible with type_erased_dynamic_float8_t: +/// QMMAFormat::E5M2 +/// QMMAFormat::E4M3 +/// +/////////////////////////////////////////////////////////////// + +union type_erased_dynamic_float8_t { + uint8_t data; + cutlass::float_e5m2_t e5m2; + cutlass::float_e4m3_t e4m3; + CUTLASS_HOST_DEVICE + explicit operator cutlass::float_e5m2_t() const { + return e5m2; + } + + CUTLASS_HOST_DEVICE + explicit operator cutlass::float_e4m3_t() const { + return e4m3; + } + +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Standard Library operations and definitions +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#if !defined(__CUDACC_RTC__) +namespace std { + +/// Numeric limits common to all float8 types +template +struct float8_base_numeric_limits { +private: + using F8Type = T; +public: + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; + static std::float_denorm_style const has_denorm = std::denorm_present; + static bool const has_denorm_loss = true; + static std::float_round_style const round_style = std::round_to_nearest; + static bool const is_iec559 = false; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = F8Type::FP8_NUM_MANTISSA_BITS; + + /// Least positive value + CUTLASS_HOST_DEVICE + static F8Type min() { return F8Type::bitcast(0x01); } + + /// Maximum finite value + CUTLASS_HOST_DEVICE + static F8Type max() { return F8Type::bitcast(F8Type::FP8_MAX_FLT); } + + /// Returns maximum rounding error + CUTLASS_HOST_DEVICE + static F8Type round_error() { return F8Type(0.5f); } + + /// Returns positive infinity value + CUTLASS_HOST_DEVICE + static F8Type infinity() { return F8Type::bitcast(F8Type::FP8_INFINITY_MASK); } + + /// Returns quiet NaN value + CUTLASS_HOST_DEVICE + static F8Type quiet_NaN() { return F8Type::bitcast(F8Type::FP8_NAN); } + + /// Returns signaling NaN value + CUTLASS_HOST_DEVICE + static F8Type signaling_NaN() { return F8Type::bitcast(F8Type::FP8_NAN); } + + /// Returns smallest positive subnormal value + CUTLASS_HOST_DEVICE + static F8Type denorm_min() { return F8Type::bitcast(0x01); } +}; + +/// Numeric limits for float_e4m3_t +template <> +struct numeric_limits : + public float8_base_numeric_limits { + static bool const has_infinity = false; + + /// Minimum finite value + static cutlass::float_e4m3_t lowest() { return cutlass::float_e4m3_t::bitcast(0xfe); } + + /// Machine epsilon, that is, the difference between 1.0 and the next representable value + static cutlass::float_e4m3_t epsilon() { return cutlass::float_e4m3_t::bitcast(0x20); } +}; + +/// Numeric limits for float_e5m2_t +template <> +struct numeric_limits : + public float8_base_numeric_limits { + static bool const has_infinity = true; + + /// Minimum finite value + static cutlass::float_e5m2_t lowest() { return cutlass::float_e5m2_t::bitcast(0xfb); } + + /// Machine epsilon, that is, the difference between 1.0 and the next representable value + static cutlass::float_e5m2_t epsilon() { return cutlass::float_e5m2_t::bitcast(0x34); } +}; + +} // namespace std +#endif + +namespace platform { + +/// Numeric limits common to all float8 types +template +struct float8_base_numeric_limits { +private: + using F8Type = T; +public: + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; +#if !defined(__CUDACC_RTC__) + static std::float_denorm_style const has_denorm = std::denorm_present; +#endif + static bool const has_denorm_loss = true; +#if !defined(__CUDACC_RTC__) + static std::float_round_style const round_style = std::round_to_nearest; +#endif + static bool const is_iec559 = false; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = F8Type::FP8_NUM_MANTISSA_BITS; + + /// Least positive value + CUTLASS_HOST_DEVICE + static F8Type min() { return F8Type::bitcast(0x01); } + + /// Maximum finite value + CUTLASS_HOST_DEVICE + static F8Type max() { return F8Type::bitcast(F8Type::FP8_MAX_FLT); } + + /// Returns maximum rounding error + CUTLASS_HOST_DEVICE + static F8Type round_error() { return F8Type(0.5f); } + + /// Returns positive infinity value + CUTLASS_HOST_DEVICE + static F8Type infinity() { return F8Type::bitcast(F8Type::FP8_INFINITY_MASK); } + + /// Returns quiet NaN value + CUTLASS_HOST_DEVICE + static F8Type quiet_NaN() { return F8Type::bitcast(F8Type::FP8_NAN); } + + /// Returns signaling NaN value + CUTLASS_HOST_DEVICE + static F8Type signaling_NaN() { return F8Type::bitcast(F8Type::FP8_NAN); } + + /// Returns smallest positive subnormal value + CUTLASS_HOST_DEVICE + static F8Type denorm_min() { return F8Type::bitcast(0x01); } +}; + +/// std::numeric_limits +template +struct numeric_limits; + +/// Numeric limits for float_e4m3_t +template <> +struct numeric_limits : + public float8_base_numeric_limits { + static bool const has_infinity = false; + + /// Minimum finite value + static cutlass::float_e4m3_t lowest() { return cutlass::float_e4m3_t::bitcast(0xfe); } + + /// Machine epsilon, that is, the difference between 1.0 and the next representable value + static cutlass::float_e4m3_t epsilon() { return cutlass::float_e4m3_t::bitcast(0x20); } +}; + +/// Numeric limits for float_e5m2_t +template <> +struct numeric_limits : + public float8_base_numeric_limits { + static bool const has_infinity = true; + + /// Minimum finite value + static cutlass::float_e5m2_t lowest() { return cutlass::float_e5m2_t::bitcast(0xfb); } + + /// Machine epsilon, that is, the difference between 1.0 and the next representable value + static cutlass::float_e5m2_t epsilon() { return cutlass::float_e5m2_t::bitcast(0x34); } +}; + +} // namespace platform + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// User-defined literals +// + +CUTLASS_HOST_DEVICE +cutlass::float_e4m3_t operator "" _fe4m3(long double x) { + return cutlass::float_e4m3_t(float(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::float_e4m3_t operator "" _fe4m3(unsigned long long int x) { + return cutlass::float_e4m3_t(int(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::float_e5m2_t operator "" _fe5m2(long double x) { + return cutlass::float_e5m2_t(float(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::float_e5m2_t operator "" _fe5m2(unsigned long long int x) { + return cutlass::float_e5m2_t(int(x)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/floating_point_nvrtc.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/floating_point_nvrtc.h new file mode 100644 index 000000000..99deff523 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/floating_point_nvrtc.h @@ -0,0 +1,65 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines categories for floating point numbers for use in NVRTC-compiled code +*/ + +#pragma once + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// All floating-point numbers can be put in one of these categories. +enum { + FP_NAN = +# define FP_NAN 0 + FP_NAN, + FP_INFINITE = +# define FP_INFINITE 1 + FP_INFINITE, + FP_ZERO = +# define FP_ZERO 2 + FP_ZERO, + FP_SUBNORMAL = +# define FP_SUBNORMAL 3 + FP_SUBNORMAL, + FP_NORMAL = +# define FP_NORMAL 4 + FP_NORMAL +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/functional.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/functional.h new file mode 100644 index 000000000..2337cf99f --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/functional.h @@ -0,0 +1,627 @@ + /*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Define basic numeric operators + + This is inspired by the Standard Library's header. +*/ +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/half.h" +#include "cutlass/tfloat32.h" +#include "cutlass/bfloat16.h" + +#if defined(CUTLASS_ARCH_WMMA_ENABLED) +#include +#endif // defined(CUTLASS_ARCH_WMMA_ENABLED) + +#ifdef _MSC_VER +// Provides support for alternate operators such as 'and', 'or', ... +#include +#endif // _MSC_VER + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct absolute_value_op { + CUTLASS_HOST_DEVICE + T operator()(T lhs) const { + return abs(lhs); + } +}; + +template <> +struct absolute_value_op { + CUTLASS_HOST_DEVICE + float operator()(float lhs) const { return fabs(lhs); } +}; + +template +struct plus { + CUTLASS_HOST_DEVICE + T operator()(T lhs, T const &rhs) const { + lhs += rhs; + return lhs; + } +}; + +template +struct minus { + CUTLASS_HOST_DEVICE + T operator()(T lhs, T const &rhs) const { + lhs -= rhs; + return lhs; + } +}; + +template +struct multiplies { + CUTLASS_HOST_DEVICE + T operator()(T lhs, T const &rhs) const { + lhs *= rhs; + return lhs; + } +}; + +template +struct scale { + T const scaling_factor_; + + CUTLASS_HOST_DEVICE + scale(float scaling_factor) : scaling_factor_(scaling_factor) { + } + + T operator()(T const &rhs) const { + T result = rhs * scaling_factor_; + return result; + } +}; + +#if defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ >= 530 +/// Partial specializations needed when __CUDA_NO_HALF2_OPERATORS__ is set +template<> +struct plus<__half2> { + CUTLASS_HOST_DEVICE + __half2 operator()(__half2 lhs, __half2 const &rhs) const { + return __hadd2(lhs, rhs); + } +}; + +template<> +struct minus<__half2> { + CUTLASS_HOST_DEVICE + __half2 operator()(__half2 lhs, __half2 const &rhs) const { + return __hsub2(lhs, rhs); + } +}; + +template<> +struct multiplies<__half2> { + CUTLASS_HOST_DEVICE + __half2 operator()(__half2 lhs, __half2 const &rhs) const { + return __hmul2(lhs, rhs); + } +}; + +/// Partial specializations needed when __CUDA_NO_HALF_OPERATORS__ is set +template<> +struct plus<__half> { + CUTLASS_HOST_DEVICE + __half operator()(__half lhs, __half const &rhs) const { + return __hadd(lhs, rhs); + } +}; + +template<> +struct minus<__half> { + CUTLASS_HOST_DEVICE + __half operator()(__half lhs, __half const &rhs) const { + return __hsub(lhs, rhs); + } +}; + +template<> +struct multiplies<__half> { + CUTLASS_HOST_DEVICE + __half operator()(__half lhs, __half const &rhs) const { + return __hmul(lhs, rhs); + } +}; +#endif // defined(__HIP_DEVICE_COMPILE__) + + +/// Squares with optional conversion +template +struct square { + CUTLASS_HOST_DEVICE + Output operator()(T lhs) const { + multiplies mul_op; + + Output y = Output(lhs); + return mul_op(y, y); + } +}; + +/// Returns the magnitude squared of an element. +template +struct magnitude_squared { + CUTLASS_HOST_DEVICE + Output operator()(T lhs) const { + multiplies mul_op; + + Output y = Output(lhs); + return mul_op(y, y); + } +}; + +/// Computes the square of a difference with optional conversion +template +struct square_difference { + CUTLASS_HOST_DEVICE + Output operator()(T lhs, T rhs) const { + multiplies mul_op; + + Output y = Output(lhs) - Output(rhs); + return mul_op(y, y); + } +}; + +/// Computes the square of a difference with optional conversion +template +struct magnitude_squared_difference { + CUTLASS_HOST_DEVICE + Output operator()(T lhs, T rhs) const { + multiplies mul_op; + + Output y = Output(lhs) - Output(rhs); + return mul_op(y, y); + } +}; + +/// Divides +template +struct divides { + CUTLASS_HOST_DEVICE + T operator()(T lhs, T const &rhs) const { + lhs /= rhs; + return lhs; + } +}; + +/// Negate +template +struct negate { + CUTLASS_HOST_DEVICE + T operator()(T lhs) const { + return -lhs; + } +}; + +/// Greater equal +template +struct greater_equal { + CUTLASS_HOST_DEVICE + bool operator()(T const &lhs, T const &rhs) const { + return (lhs >= rhs); + } +}; + +/// Greater +template +struct greater { + CUTLASS_HOST_DEVICE + bool operator()(T const &lhs, T const &rhs) const { + return (lhs > rhs); + } +}; + +/// Less equal +template +struct less_equal { + CUTLASS_HOST_DEVICE + bool operator()(T const &lhs, T const &rhs) const { + return (lhs <= rhs); + } +}; + +/// Less +template +struct less { + CUTLASS_HOST_DEVICE + bool operator()(T const &lhs, T const &rhs) const { + return (lhs < rhs); + } +}; + +template //=false DCU_ASM +struct maximum { + CUTLASS_HOST_DEVICE + T operator()(T const &lhs, T const &rhs) const { + return (lhs < rhs ? rhs : lhs); + } +}; + +// Maximum with nan propogation +// To propgate the NANs, the "max" of a two element that contains NaNs should also return a NaN +template +struct maximum { + CUTLASS_HOST_DEVICE + T operator()(T const &lhs, T const &rhs) const { +#if defined(__HIP_DEVICE_COMPILE__) + return lhs > rhs or isnan(lhs) ? lhs : rhs; +#else + return lhs > rhs or std::isnan(lhs) ? lhs : rhs; +#endif + } +}; + +template <> +struct maximum { + CUTLASS_HOST_DEVICE + float operator()(float const &lhs, float const &rhs) const { + return fmaxf(lhs, rhs); + } +}; + +template <> +struct maximum { + CUTLASS_HOST_DEVICE + float operator()(float const lhs, float const rhs) const { + float res; +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + asm volatile("max.NaN.f32 %0, %1, %2;\n" : "=f"(res) : "f"(lhs), "f"(rhs)); +#elif defined(__HIP_DEVICE_COMPILE__) + res = lhs > rhs or isnan(lhs) ? lhs : rhs; +#else + res = lhs > rhs or std::isnan(lhs) ? lhs : rhs; +#endif + return res; + } +}; + +template +using maximum_with_nan_propogation = maximum; + +template +struct minimum{ + CUTLASS_HOST_DEVICE + T operator()(T const &lhs, T const &rhs) const { + return (rhs < lhs ? rhs : lhs); + } +}; + +template +struct minimum { + CUTLASS_HOST_DEVICE + T operator()(T const &lhs, T const &rhs) const { +#if defined(__HIP_DEVICE_COMPILE__) + return lhs < rhs or isnan(lhs) ? lhs : rhs; +#else + return lhs < rhs or std::isnan(lhs) ? lhs : rhs; +#endif + } +}; + +template <> +struct minimum { + CUTLASS_HOST_DEVICE + float operator()(float const &lhs, float const &rhs) const { + return fminf(lhs, rhs); + } +}; + +template +struct maximum_absolute_value { + CUTLASS_HOST_DEVICE + float operator()(T const &lhs, T const &rhs) const { + absolute_value_op abs_op; + maximum max_op; + + return max_op(abs_op(lhs), abs_op(rhs)); + } +}; + +// assumes the left operand is already an absolute value +template +struct maximum_absolute_value_reduction { + CUTLASS_HOST_DEVICE + float operator()(T const &lhs, T const &rhs) const { + absolute_value_op abs_op; + maximum max_op; + + return max_op(lhs, abs_op(rhs)); + } +}; + +/// Fused multiply-add +template +struct multiply_add { + CUTLASS_HOST_DEVICE + C operator()(A const &a, B const &b, C const &c) const { + return C(a) * C(b) + c; + } +}; + +/// Fused multiply-add +template +struct multiply_add_relu0 { + CUTLASS_HOST_DEVICE + C operator()(A const &a, B const &b, C const &c) const { + maximum mx; + return mx(C(a) * C(b) + c, C(0)); + } +}; + +/// Fused multiply-add +template +struct and_add { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b, T const &c) const { + return ((a & b) + c); + } +}; + + +/// Fused multiply-add +template +struct xor_add { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b, T const &c) const { + return ((a ^ b) + c); + } +}; + +template +struct conjugate { + CUTLASS_HOST_DEVICE + T operator()(T const &a) const { + return a; + } +}; + +template +struct first { + CUTLASS_HOST_DEVICE + T operator()(T const & first, T const &...) const { + return first; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct logical_and { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b) const { + return ((a && b) ? T(1) : T()); + } +}; + +template +struct logical_or { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b) const { + return ((a || b) ? T(1) : T()); + } +}; + +template +struct logical_not { + CUTLASS_HOST_DEVICE + T operator()(T const &a) const { + return T(!(a)); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct bit_and { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b) const { + return a & b; + } +}; + +template +struct bit_or { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b) const { + return a | b; + } +}; + +template +struct bit_not { + CUTLASS_HOST_DEVICE + T operator()(T const &a) const { + return ~a; + } +}; + +template +struct bit_xor { + CUTLASS_HOST_DEVICE + T operator()(T const &a, T const &b) const { + return a ^ b; + } +}; + + + +////////////////////////////////////////////////////////////////////////////////////////////////// +/// Atomic reductions + +template +struct atomic_add +{ + CUTLASS_DEVICE + void operator()(T *ptr, const T &data) + { +#if defined(__HIP_DEVICE_COMPILE__) + atomicAdd(ptr, data); +#endif + } +}; + +template<> +struct atomic_add +{ + CUTLASS_DEVICE + void operator()(double *ptr, const double &data) + { +#if !defined(__HIP_DEVICE_COMPILE__) + CUTLASS_UNUSED(ptr); + CUTLASS_UNUSED(data); +#elif (__HIP_DEVICE_COMPILE__ >= 600) + atomicAdd(ptr, data); +#else + // Use CAS loop + unsigned long long int* ptr_int = reinterpret_cast(ptr); + unsigned long long int old_int = *ptr_int; + unsigned long long int assumed_int; + + do { + double update = data + __longlong_as_double(old_int); + assumed_int = old_int; + old_int = atomicCAS(ptr_int, assumed_int, __double_as_longlong(update)); + } while (assumed_int != old_int); +#endif // (__HIP_DEVICE_COMPILE__ >= 600) + } +}; + +template<> +struct atomic_add +{ + CUTLASS_DEVICE + void operator()(half2 *ptr, const half2 &data) + { +#if !defined(__HIP_DEVICE_COMPILE__) || (defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ < 600)) + CUTLASS_UNUSED(ptr); + CUTLASS_UNUSED(data); +#else + // Vector-2 atomic reduction requires .target sm_60 or higher + uint32_t word = reinterpret_cast(data); + asm volatile ("red.gpu.global.add.noftz.f16x2 [%0], %1;\n" : : "l"(ptr), "r"(word)); +#endif // (__HIP_DEVICE_COMPILE__ >= 600) + } +}; + +template +using red [[deprecated("use atomic_add instead")]] = atomic_add; + +template +struct atomic_maximum { + CUTLASS_DEVICE + T operator()(T *ptr, T value) const { +#if defined(__HIP_DEVICE_COMPILE__) + return atomicMax(ptr, value); +#else + CUTLASS_UNUSED(ptr); + CUTLASS_UNUSED(value); + CUTLASS_NOT_IMPLEMENTED(); + return 0; +#endif + } +}; + +template <> +struct atomic_maximum { + CUTLASS_DEVICE + float operator()(float *ptr, float value) const { +#if defined(__HIP_DEVICE_COMPILE__) + return !signbit(value) ? + __int_as_float(atomicMax((int*)ptr, __float_as_int(value))) : + __uint_as_float(atomicMin((unsigned int*)ptr, __float_as_uint(value))); +#else + CUTLASS_UNUSED(ptr); + CUTLASS_UNUSED(value); + CUTLASS_NOT_IMPLEMENTED(); + return 0; +#endif + } +}; + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for nvcuda::wmma::fragment +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(CUTLASS_ARCH_WMMA_ENABLED) + +template +struct plus> +{ + using Fragment = nvcuda::wmma::fragment; + using ElementType = typename Fragment::element_type; + + CUTLASS_HOST_DEVICE + Fragment operator()(Fragment const &lhs, Fragment const &rhs) const + { + Fragment result; + plus scalar_op; + + ElementType *result_elts = reinterpret_cast(&result); + const ElementType *lhs_elts = reinterpret_cast(&lhs); + const ElementType *rhs_elts = reinterpret_cast(&rhs); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < Fragment::num_elements; i++) { + result_elts[i] = scalar_op(lhs_elts[i], rhs_elts[i]); + } + + return result; + } +}; + +#endif // defined(CUTLASS_ARCH_WMMA_ENABLED) + + + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/half.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/half.h new file mode 100644 index 000000000..736a6b701 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/half.h @@ -0,0 +1,935 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines a class for using IEEE half-precision floating-point types in host or + device code. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#ifndef CUTLASS_ENABLE_F16C +#define CUTLASS_ENABLE_F16C 0 +#endif + +#if defined(__CUDACC_RTC__) + +#include "cutlass/floating_point_nvrtc.h" + +// F16C extensions are not meaningful when compiling for NVRTC which only accommodates device code. +#undef CUTLASS_ENABLE_F16C +#define CUTLASS_ENABLE_F16C 0 + +#else +// +// Standard Library headers belong here to avoid conflicts with NVRTC. +// +#include +#include +#include +#include +#endif + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#include + +#include "cutlass/cutlass.h" +#include "cutlass/float8.h" +#include "cutlass/platform/platform.h" + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// Optionally target F16C extentions to accelerate half-precision conversion. +#if !defined(__HIP_DEVICE_COMPILE__) && (CUTLASS_ENABLE_F16C) +#if defined(_MSC_VER) + +#include + +#if defined(__i386__) || defined(__x86_64__) +#include +#endif + +#define F16C_ROUND_NEAREST 0 + +#if !defined(__HIP_DEVICE_COMPILE__) +extern __inline float _cvtsh_ss (unsigned short __S) { + __m128i packed; + std::memcpy(&packed, &__S, sizeof(__S)); + + __m128 result = _mm_cvtph_ps(packed); + + float flt; + std::memcpy(&flt, &result, sizeof(flt)); + + return flt; +} + +__inline unsigned short _cvtss_sh (float __F, const int) { + __m128 packed; + std::memcpy(&packed, &__F, sizeof(__F)); + + __m128i result = _mm_cvtps_ph(packed, F16C_ROUND_NEAREST); + + unsigned short u; + std::memcpy(&u, &result, sizeof(u)); + + return u; +} +#endif + +#else + +// Linux +#include + +#if defined(__i386__) || defined(__x86_64__) +#include +#endif + +#define F16C_ROUND_NEAREST (_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC) + +#endif // _MSC_VER + +class CpuId { + + bool f16c_enabled; + + CpuId() { + #if defined(__i386__) || defined(__x86_64__) + #if defined(_MSC_VER) + int exx[4]; + + __cpuid (exx, 1); + f16c_enabled = exx[2] & 0x20000000; + + #else + // GCC / Clang + int eax, ebx, ecx, edx; + + __cpuid (1 , eax, ebx, ecx, edx); + f16c_enabled = ecx & 0x20000000; + #endif + #else + // Arm / PowerPC etc. + f16c_enabled = false; + #endif + } + +public: + + bool is_f16c_supported() const { + return f16c_enabled; + } + + static const CpuId& instance() { + static CpuId cpu; + return cpu; + } +}; +#endif // !defined(__HIP_DEVICE_COMPILE__) && CUTLASS_ENABLE_F16C + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// IEEE half-precision floating-point type +struct alignas(2) half_t { + + // + // Data members + // + + /// Storage type + uint16_t storage; + + // + // Static conversion operators + // + + /// Constructs from an unsigned short + CUTLASS_HOST_DEVICE + static half_t bitcast(uint16_t x) { + half_t h; + h.storage = x; + return h; + } + + /// FP32 -> FP16 conversion - rounds to nearest even + // #if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ < 530) + #if defined(__gfx926__) + // Avoid inlining in device code if no hardware support + __device__ __noinline__ + #else //gfx906 gfx928 硬件支持 + CUTLASS_HOST_DEVICE + #endif + static half_t convert(float const& flt) { + #if defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__) || defined(__gfx906__) //defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__float2half_rn(flt)); + #else + #if !defined(__HIP_DEVICE_COMPILE__) && CUTLASS_ENABLE_F16C + if( CpuId::instance().is_f16c_supported() ) { + unsigned short u = _cvtss_sh(flt, F16C_ROUND_NEAREST); + return bitcast(u); + } + #endif + + // software implementation rounds toward nearest even + unsigned s; + + #if defined(__HIP_DEVICE_COMPILE__) + s = reinterpret_cast(flt); + #else + std::memcpy(&s, &flt, sizeof(s)); + #endif + + uint16_t sign = uint16_t((s >> 16) & 0x8000); + int16_t exp = uint16_t(((s >> 23) & 0xff) - 127); + int mantissa = s & 0x7fffff; + uint16_t u = 0; + + if ((s & 0x7fffffff) == 0) { + // sign-preserving zero + return bitcast(sign); + } + + if (exp > 15) { + if (exp == 128 && mantissa) { + // not a number + u = 0x7fff; + } else { + // overflow to infinity + u = sign | 0x7c00; + } + return bitcast(u); + } + + int sticky_bit = 0; + + if (exp >= -14) { + // normal fp32 to normal fp16 + exp = uint16_t(exp + uint16_t(15)); + u = uint16_t(((exp & 0x1f) << 10)); + u = uint16_t(u | (mantissa >> 13)); + } else { + // normal single-precision to subnormal half_t-precision representation + int rshift = (-14 - exp); + if (rshift < 32) { + mantissa |= (1 << 23); + + sticky_bit = ((mantissa & ((1 << rshift) - 1)) != 0); + + mantissa = (mantissa >> rshift); + u = (uint16_t(mantissa >> 13) & 0x3ff); + } else { + mantissa = 0; + u = 0; + } + } + + // round to nearest even + int round_bit = ((mantissa >> 12) & 1); + sticky_bit |= ((mantissa & ((1 << 12) - 1)) != 0); + + if ((round_bit && sticky_bit) || (round_bit && (u & 1))) { + u = uint16_t(u + 1); + } + + u |= sign; + + return bitcast(u); + #endif + } + + /// FP32 -> FP16 conversion - rounds to nearest even + // __host__ + // static half_t convert(int const& n) { + // return convert(float(n)); + // } + /// FP32 -> FP16 conversion - rounds to nearest even + // CUTLASS_DEVICE + CUTLASS_HOST_DEVICE + static half_t convert(int const& n) { + #if defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__) || defined(__gfx906__) //defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__int2half_rn(n)); + #else + return convert(float(n)); + #endif + } + + /// FP32 -> FP16 conversion - rounds to nearest even + CUTLASS_HOST_DEVICE + static half_t convert(unsigned const& n) { + #if defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__) || defined(__gfx906__) //defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__uint2half_rn(n)); + #else + return convert(float(n)); + #endif + } + + /// Converts a half-precision value stored as a uint16_t to a float + #if defined(__gfx926__)//defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ < 530) + // Avoid inlining in device code if no hardware support + __device__ __noinline__ + #else + CUTLASS_HOST_DEVICE + #endif + static float convert(half_t const& x) { + #if defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__) || defined(__gfx906__) //(defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530)) || defined(DCU_ASM) + return __half2float(x.to_half()); + #else + + #if !defined(__HIP_DEVICE_COMPILE__) && CUTLASS_ENABLE_F16C + if( CpuId::instance().is_f16c_supported() ) { + unsigned short u = x.storage; + return _cvtsh_ss(u); + } + #endif + + uint16_t const &h = x.storage; + uint32_t sign = ((h >> 15) & 1); + uint32_t exp = ((h >> 10) & 0x1f); + uint32_t mantissa = (h & 0x3ff); + unsigned f = 0; + + if (exp > 0 && exp < 31) { + // normal + exp += 112; + f = (sign << 31) | (exp << 23) | (mantissa << 13); + } else if (exp == 0) { + if (mantissa) { + // subnormal + exp += 113; + while ((mantissa & (1 << 10)) == 0) { + mantissa <<= 1; + exp--; + } + mantissa &= 0x3ff; + f = (sign << 31) | (exp << 23) | (mantissa << 13); + } else { + // sign-preserving zero + f = (sign << 31); + } + } else if (exp == 31) { + if (mantissa) { + f = 0x7fffffff; // not a number + } else { + f = (0xff << 23) | (sign << 31); // inf + } + } + #if defined(__HIP_DEVICE_COMPILE__) + return reinterpret_cast(f); + #else + float flt; + std::memcpy(&flt, &f, sizeof(flt)); + return flt; + #endif + #endif + } + + // + // Methods + // + + /// Default constructor + half_t() = default; + + /// Reinterpret cast from CUDA's half type + CUTLASS_HOST_DEVICE + explicit half_t(half const & x) { + #if defined(__HIP_DEVICE_COMPILE__) + storage = reinterpret_cast(x); + #else + __half_raw raw(x); + std::memcpy(&storage, &raw.x, sizeof(storage)); + #endif + } + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit half_t(float x) { + storage = convert(x).storage; + } + + /// Floating point conversion + CUTLASS_HOST_DEVICE + explicit half_t(double x): half_t(float(x)) { + + } + + /// float_e4m3_t conversion + CUTLASS_HOST_DEVICE + explicit half_t(float_e4m3_t x): half_t(float(x)) { + + } + + /// float_e5m2_t conversion + CUTLASS_HOST_DEVICE + explicit half_t(float_e5m2_t x): half_t(float(x)) { + + } + + /// Integer conversion - round to nearest even + CUTLASS_HOST_DEVICE + explicit half_t(int x) { + storage = convert(x).storage; + } + + /// Integer conversion - round toward zero + CUTLASS_HOST_DEVICE + explicit half_t(unsigned x) { + storage = convert(x).storage; + } + + /// Assignment + CUTLASS_HOST_DEVICE + half_t & operator=(half const &x) { + #if defined(__HIP_DEVICE_COMPILE__) + storage = reinterpret_cast(x); + #else + __half_raw raw(x); + std::memcpy(&storage, &raw.x, sizeof(storage)); + #endif + return *this; + } + + /// Converts to float + CUTLASS_HOST_DEVICE + operator float() const { + return convert(*this); + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator double() const { + return double(convert(*this)); + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator int() const { + return int(convert(*this)); + } + + /// Casts to bool + CUTLASS_HOST_DEVICE + explicit operator bool() const { + return (convert(*this) != 0.0f); + } + + /// Bitcasts to CUDA's half type + CUTLASS_HOST_DEVICE + half to_half() const { + #if defined(__HIP_DEVICE_COMPILE__) + return reinterpret_cast(storage); + #else + __half_raw raw; + std::memcpy(&raw.x, &storage, sizeof(raw.x)); + return half(raw); + #endif + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint16_t& raw() { + return storage; + } + + /// Accesses raw internal state + CUTLASS_HOST_DEVICE + uint16_t raw() const { + return storage; + } + + /// Returns the sign bit + CUTLASS_HOST_DEVICE + bool signbit() const { + return ((storage & 0x8000) != 0); + } + + /// Returns the biased exponent + CUTLASS_HOST_DEVICE + int exponent_biased() const { + return int((storage >> 10) & 0x1f); + } + + /// Returns the unbiased exponent + CUTLASS_HOST_DEVICE + int exponent() const { + return exponent_biased() - 15; + } + + /// Returns the mantissa + CUTLASS_HOST_DEVICE + int mantissa() const { + return int(storage & 0x3ff); + } +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool signbit(cutlass::half_t const& h) { + return ((h.raw() & 0x8000) != 0); +} + +CUTLASS_HOST_DEVICE +cutlass::half_t abs(cutlass::half_t const& h) { + return cutlass::half_t::bitcast(h.raw() & 0x7fff); +} + +CUTLASS_HOST_DEVICE +bool isnan(cutlass::half_t const& h) { + return (h.exponent_biased() == 0x1f) && h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isfinite(cutlass::half_t const& h) { + return (h.exponent_biased() != 0x1f); +} + +CUTLASS_HOST_DEVICE +cutlass::half_t nanh(const char*) { + // NVIDIA canonical NaN + return cutlass::half_t::bitcast(0x7fff); +} + +CUTLASS_HOST_DEVICE +bool isinf(cutlass::half_t const& h) { + return (h.exponent_biased() == 0x1f) && !h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isnormal(cutlass::half_t const& h) { + return h.exponent_biased() && h.exponent_biased() != 0x1f; +} + +CUTLASS_HOST_DEVICE +int fpclassify(cutlass::half_t const& h) { + int exp = h.exponent_biased(); + int mantissa = h.mantissa(); + if (exp == 0x1f) { + if (mantissa) { + return FP_NAN; + } + else { + return FP_INFINITE; + } + } + else if (!exp) { + if (mantissa) { + return FP_SUBNORMAL; + } + else { + return FP_ZERO; + } + } + return FP_NORMAL; +} + +CUTLASS_HOST_DEVICE +cutlass::half_t sqrt(cutlass::half_t const& h) { +#if defined(__CUDACC_RTC__) + return cutlass::half_t(sqrtf(float(h))); +#else + return cutlass::half_t(std::sqrt(float(h))); +#endif +} + +CUTLASS_HOST_DEVICE +half_t copysign(half_t const& a, half_t const& b) { + + uint16_t a_mag = (a.raw() & 0x7fff); + uint16_t b_sign = (b.raw() & 0x8000); + uint16_t result = (a_mag | b_sign); + + return half_t::bitcast(result); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Standard Library operations and definitions +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#if !defined(__CUDACC_RTC__) +namespace std { + +/// Numeric limits +template <> +struct numeric_limits { + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_infinity = true; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; + static std::float_denorm_style const has_denorm = std::denorm_present; + static bool const has_denorm_loss = true; + static std::float_round_style const round_style = std::round_to_nearest; + static bool const is_iec559 = true; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = 10; + + /// Least positive value + static cutlass::half_t min() { return cutlass::half_t::bitcast(0x0001); } + + /// Minimum finite value + static cutlass::half_t lowest() { return cutlass::half_t::bitcast(0xfbff); } + + /// Maximum finite value + static cutlass::half_t max() { return cutlass::half_t::bitcast(0x7bff); } + + /// Returns smallest finite value + static cutlass::half_t epsilon() { return cutlass::half_t::bitcast(0x1800); } + + /// Returns maximum rounding error + static cutlass::half_t round_error() { return cutlass::half_t(0.5f); } + + /// Returns positive infinity value + static cutlass::half_t infinity() { return cutlass::half_t::bitcast(0x7c00); } + + /// Returns quiet NaN value + static cutlass::half_t quiet_NaN() { return cutlass::half_t::bitcast(0x7fff); } + + /// Returns signaling NaN value + static cutlass::half_t signaling_NaN() { return cutlass::half_t::bitcast(0x7fff); } + + /// Returns smallest positive subnormal value + static cutlass::half_t denorm_min() { return cutlass::half_t::bitcast(0x0001); } +}; +} // namespace std +#endif + +namespace platform { + +/// std::numeric_limits +template +struct numeric_limits; + +/// Numeric limits +template <> +struct numeric_limits { + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_infinity = true; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; +#if !defined(__CUDACC_RTC__) + static std::float_denorm_style const has_denorm = std::denorm_present; +#endif + static bool const has_denorm_loss = true; +#if !defined(__CUDACC_RTC__) + static std::float_round_style const round_style = std::round_to_nearest; +#endif + static bool const is_iec559 = true; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = 10; + + /// Least positive value + CUTLASS_HOST_DEVICE + static cutlass::half_t min() { return cutlass::half_t::bitcast(0x0001); } + + /// Minimum finite value + CUTLASS_HOST_DEVICE + static cutlass::half_t lowest() { return cutlass::half_t::bitcast(0xfbff); } + + /// Maximum finite value + CUTLASS_HOST_DEVICE + static cutlass::half_t max() { return cutlass::half_t::bitcast(0x7bff); } + + /// Returns smallest finite value + CUTLASS_HOST_DEVICE + static cutlass::half_t epsilon() { return cutlass::half_t::bitcast(0x1800); } + + /// Returns maximum rounding error + CUTLASS_HOST_DEVICE + static cutlass::half_t round_error() { return cutlass::half_t(0.5f); } + + /// Returns positive infinity value + CUTLASS_HOST_DEVICE + static cutlass::half_t infinity() { return cutlass::half_t::bitcast(0x7c00); } + + /// Returns quiet NaN value + CUTLASS_HOST_DEVICE + static cutlass::half_t quiet_NaN() { return cutlass::half_t::bitcast(0x7fff); } + + /// Returns signaling NaN value + CUTLASS_HOST_DEVICE + static cutlass::half_t signaling_NaN() { return cutlass::half_t::bitcast(0x7fff); } + + /// Returns smallest positive subnormal value + CUTLASS_HOST_DEVICE + static cutlass::half_t denorm_min() { return cutlass::half_t::bitcast(0x0001); } +}; +} // namespace platform + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Arithmetic operators +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool operator==(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __heq(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) == float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +bool operator!=(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __hne(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) != float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +bool operator<(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __hlt(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) < float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +bool operator<=(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __hle(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) <= float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +bool operator>(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __hgt(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) > float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +bool operator>=(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return __hge(lhs.to_half(), rhs.to_half()); +#else + return float(lhs) >= float(rhs); +#endif +} + +CUTLASS_HOST_DEVICE +half_t operator+(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__hadd(lhs.to_half(), rhs.to_half())); +#else + return half_t(float(lhs) + float(rhs)); +#endif +} + +CUTLASS_HOST_DEVICE +half_t operator-(half_t const& lhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__hneg(lhs.to_half())); +#else + return half_t(-float(lhs)); +#endif +} + +CUTLASS_HOST_DEVICE +half_t operator-(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__hsub(lhs.to_half(), rhs.to_half())); +#else + return half_t(float(lhs) - float(rhs)); +#endif +} + +CUTLASS_HOST_DEVICE +half_t operator*(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__hmul(lhs.to_half(), rhs.to_half())); +#else + return half_t(float(lhs) * float(rhs)); +#endif +} + +CUTLASS_HOST_DEVICE +half_t operator/(half_t const& lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + return half_t(__hdiv(lhs.to_half(), rhs.to_half())); +#else + return half_t(float(lhs) / float(rhs)); +#endif +} + +CUTLASS_HOST_DEVICE +half_t& operator+=(half_t & lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hadd(lhs.to_half(), rhs.to_half())); +#else + lhs = half_t(float(lhs) + float(rhs)); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t& operator-=(half_t & lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hsub(lhs.to_half(), rhs.to_half())); +#else + lhs = half_t(float(lhs) - float(rhs)); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t& operator*=(half_t & lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hmul(lhs.to_half(), rhs.to_half())); +#else + lhs = half_t(float(lhs) * float(rhs)); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t& operator/=(half_t & lhs, half_t const& rhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hdiv(lhs.to_half(), rhs.to_half())); +#else + lhs = half_t(float(lhs) / float(rhs)); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t& operator++(half_t & lhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hadd(lhs.to_half(), half_t(1.0f).to_half())); +#else + float tmp(lhs); + ++tmp; + lhs = half_t(tmp); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t& operator--(half_t & lhs) { +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hsub(lhs.to_half(), half_t(1.0f).to_half())); +#else + float tmp(lhs); + --tmp; + lhs = half_t(tmp); +#endif + return lhs; +} + +CUTLASS_HOST_DEVICE +half_t operator++(half_t & lhs, int) { + half_t ret(lhs); +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hadd(lhs.to_half(), half_t(1.0f).to_half())); +#else + float tmp(lhs); + tmp++; + lhs = half_t(tmp); +#endif + return ret; +} + +CUTLASS_HOST_DEVICE +half_t operator--(half_t & lhs, int) { + half_t ret(lhs); +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + lhs = half_t(__hsub(lhs.to_half(), half_t(1.0f).to_half())); +#else + float tmp(lhs); + tmp--; + lhs = half_t(tmp); +#endif + return ret; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// User-defined literals +// + +CUTLASS_HOST_DEVICE +cutlass::half_t operator "" _hf(long double x) { + return cutlass::half_t(float(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::half_t operator "" _hf(unsigned long long int x) { + return cutlass::half_t(int(x)); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/integer_subbyte.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/integer_subbyte.h new file mode 100644 index 000000000..4c8ea36af --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/integer_subbyte.h @@ -0,0 +1,254 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines a class for using integer types smaller than one byte in host or + device code. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#endif + +#include "cutlass/cutlass.h" +#include "cutlass/numeric_size.h" +#include "cutlass/platform/platform.h" + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// 4-bit signed integer type +template +struct integer_subbyte { + + /// Number of bits + static int const kBits = Bits; + + /// Whether type is signed + static bool const kSigned = Signed; + + /// External type + using T = typename platform::conditional::type; + + /// Storage type + using Storage = uint8_t; + + /// Bitmask used to truncate from larger integers + static Storage const kMask = Storage((1 << kBits) - 1); + + // + // Data members + // + + Storage storage; + + // + // Methods + // + + /// No operation + integer_subbyte() = default; + + /// Conversion from integer type + CUTLASS_HOST_DEVICE + integer_subbyte(int value) + : storage(reinterpret_cast(value) & kMask) {} + + CUTLASS_HOST_DEVICE + integer_subbyte(unsigned value) + : storage(reinterpret_cast(value) & kMask) {} + + CUTLASS_HOST_DEVICE + integer_subbyte(double value) { + T tmp = static_cast(value); + storage = Storage(reinterpret_cast(tmp) & kMask); + } + + /// + CUTLASS_HOST_DEVICE + operator T() const { + if (kSigned) { + // Sign extend + if (storage & Storage(1 << (kBits - 1))) { + return T(storage) | ~T(kMask); + } + } + return T(storage); + } + + /// Equality + CUTLASS_HOST_DEVICE + bool operator==(integer_subbyte const &rhs) const { + return storage == rhs.storage; + } + + /// Inequality + CUTLASS_HOST_DEVICE + bool operator!=(integer_subbyte const &rhs) const { + return storage != rhs.storage; + } + + /// Less than or equal + CUTLASS_HOST_DEVICE + bool operator<=(integer_subbyte const &rhs) const { + if (kSigned) { + if (storage & (1 << (kBits - 1))) { + return !(rhs.storage < storage); + } + } + return storage <= rhs.storage; + } + + /// Less than + CUTLASS_HOST_DEVICE + bool operator<(integer_subbyte const &rhs) const { + if (kSigned) { + if (storage & (1 << (kBits - 1))) { + return !(rhs.storage <= storage); + } + } + return storage < rhs.storage; + } + + /// Greater than or equal + CUTLASS_HOST_DEVICE + bool operator>=(integer_subbyte const &rhs) const { + return !(*this < rhs); + } + + /// Greater than + CUTLASS_HOST_DEVICE + bool operator>(integer_subbyte const &rhs) const { + return !(*this <= rhs); + } +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + + +/// 1-bit Unsigned integer type +using uint1b_t = integer_subbyte<1, false>; + +/// 2-bit Integer type +using int2b_t = integer_subbyte<2, true>; + +/// 2-bit Unsigned integer type +using uint2b_t = integer_subbyte<2, false>; + +/// 4-bit Integer type +using int4b_t = integer_subbyte<4, true>; + +/// 4-bit Unsigned integer type +using uint4b_t = integer_subbyte<4, false>; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Defines the size of an element in bits - specialized for uint1b_t +template <> +struct sizeof_bits { + static int const value = 1; +}; + +/// Defines the size of an element in bits - specialized for int2b_t +template <> +struct sizeof_bits { + static int const value = 2; +}; + +/// Defines the size of an element in bits - specialized for uint2b_t +template <> +struct sizeof_bits { + static int const value = 2; +}; + +/// Defines the size of an element in bits - specialized for int4b_t +template <> +struct sizeof_bits { + static int const value = 4; +}; + +/// Defines the size of an element in bits - specialized for uint4b_t +template <> +struct sizeof_bits { + static int const value = 4; +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace platform { + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static cutlass::int4b_t const lowest() noexcept { return -8;} + CUTLASS_HOST_DEVICE + static cutlass::int4b_t const max() noexcept { return 7;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static cutlass::uint4b_t const lowest() noexcept { return 0;} + CUTLASS_HOST_DEVICE + static cutlass::uint4b_t const max() noexcept { return 15;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static cutlass::uint1b_t const lowest() noexcept { return 0;} + CUTLASS_HOST_DEVICE + static cutlass::uint1b_t const max() noexcept { return 1;} + static constexpr bool is_integer = true; +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace platform +} // namespace cutlass + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/layout/matrix_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/layout/matrix_hip.h new file mode 100644 index 000000000..d6094d56f --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/layout/matrix_hip.h @@ -0,0 +1,1365 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Defines layout functions used by TensorRef and derived classes. + + Layout functions map logical coordinates to linear memory. They often require additional + data to describe strides between elements. + + Layout functions must implement all members in the public interface of IdentityTensorLayout<> + defined in cutlass/tensor_ref.h. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by this unit test: `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/fast_math_hip.h" +#include "cutlass/matrix_coord.h" +#include "cutlass/pitch_linear_coord.h" + +namespace cutlass { +namespace layout { + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Defines data layouts of various matrix formats usable by TensorRef and other classes. +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Mapping function for row-major matrices. +class RowMajor { +public: + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Constructor + CUTLASS_HOST_DEVICE + RowMajor(LongIndex ldm = 0): stride_(ldm) { } + + /// Ctor + CUTLASS_HOST_DEVICE + RowMajor(Stride stride): stride_(stride) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static RowMajor packed(MatrixCoord const &extent) { + return RowMajor(extent.column()); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return LongIndex(coord.row()) * LongIndex(stride_[0]) + coord.column(); + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + return MatrixCoord(Index(offset / stride_[0]), Index(offset % stride_[0])); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return LongIndex(extent.row()) * LongIndex(stride_[0]); + } +}; + +/// Mapping function for column-major matrices. +class ColumnMajor { +public: + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + ColumnMajor(LongIndex ldm = 0): stride_(ldm) { } + + /// Ctor + CUTLASS_HOST_DEVICE + ColumnMajor(Stride stride): stride_(stride) { } + + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static ColumnMajor packed(MatrixCoord const &extent) { + return ColumnMajor(extent.row()); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return LongIndex(coord.column()) * LongIndex(stride_[0]) + coord.row(); + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + return MatrixCoord(Index(offset % stride_[0]), Index(offset / stride_[0])); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return LongIndex(extent.column()) * LongIndex(stride_[0]); + } +}; + +/// Mapping function for interleaved matrices. Matrix is structured +/// as row-major arrangement of fixed-size columns. +template +struct RowMajorInterleaved { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + + /// Size of interleaved columns + static int const kInterleave = Interleave; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + RowMajorInterleaved(LongIndex ldm = 0): stride_(ldm) { } + + /// Ctor + CUTLASS_HOST_DEVICE + RowMajorInterleaved(Stride stride): stride_(stride) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static RowMajorInterleaved packed(MatrixCoord const &extent) { + return RowMajorInterleaved(extent.column() * kInterleave); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + Index row_major = coord.row() / kInterleave; + Index row_minor = coord.row() % kInterleave; + return LongIndex(row_major) * LongIndex(stride_[0]) + LongIndex(coord.column()) * kInterleave + row_minor; + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + + Index row_major = Index(offset / stride_[0]); + Index residual = Index(offset % stride_[0]); + + Index column = residual / kInterleave; + Index row_minor = residual % kInterleave; + + return MatrixCoord(row_major * kInterleave + row_minor, column); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return (extent.row() + kInterleave - 1) / kInterleave * stride_[0]; + } +}; + +/// Mapping function for interleaved matrices. Matrix is structured +/// as column-major arrangement of fixed-size rows. +template +struct ColumnMajorInterleaved { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + + /// Size of interleaved columns + static int const kInterleave = Interleave; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + ColumnMajorInterleaved(LongIndex ldm = 0): stride_(ldm) { } + + /// Ctor + CUTLASS_HOST_DEVICE + ColumnMajorInterleaved(Stride stride): stride_(stride) { } + + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static ColumnMajorInterleaved packed(MatrixCoord const &extent) { + return ColumnMajorInterleaved(extent.row() * kInterleave); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + Index column_major = coord.column() / kInterleave; + Index column_minor = coord.column() % kInterleave; + return LongIndex(column_major) * LongIndex(stride_[0]) + LongIndex(coord.row()) * kInterleave + column_minor; + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + + Index column_major = Index(offset / stride_[0]); + Index residual = Index(offset % stride_[0]); + + Index row = residual / kInterleave; + Index column_minor = residual % kInterleave; + + return MatrixCoord(row, column_major * kInterleave + column_minor); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return (extent.column() + kInterleave - 1) / kInterleave * stride_[0]; + } +}; + +/// Enumerated type for canonical pitch-linear matrix layouts +enum class Matrix { + kColumnMajor, ///< leading dimension refers to stride between columns; stride along rows is 1 + kRowMajor ///< leading dimension refers to stride between rows; stride along columns is 1 +}; + +/// Mapping function for scenario in which layout is row-major or column-major but this information +/// is only available at runtime. +struct ContiguousMatrix { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + + /// Enumerated type indicating canonical matrix layout + Matrix layout_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + ContiguousMatrix( + Index ldm = 0, + Matrix layout = Matrix::kColumnMajor + ): + stride_(ldm), layout_(layout) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static ContiguousMatrix packed( + MatrixCoord const &extent, + Matrix layout = Matrix::kColumnMajor) { + + Index ldm = 0; + if (layout == Matrix::kColumnMajor) { + ldm = extent.row(); + } + else if (layout == Matrix::kRowMajor) { + ldm = extent.column(); + } + return ContiguousMatrix(ldm, layout); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + if (layout_ == Matrix::kColumnMajor) { + return coord.row() + coord.column() * stride_[0]; + } + else if (layout_ == Matrix::kRowMajor) { + return coord.row() * stride_[0] + coord.column(); + } + else { + // degenerate case + return 0; + } + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + CUTLASS_UNUSED(offset); + return MatrixCoord(0, 0); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + if (layout_ == Matrix::kColumnMajor) { + return stride_[0] * extent.column(); + } + else if (layout_ == Matrix::kRowMajor) { + return stride_[0] * extent.row(); + } + else { + // degenerate case + return 0; + } + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Mapping function for scenario in which both rows and columns are separated by a stride. +template +struct AffineRankN { + + /// Logical rank of tensor + static int const kRank = Rank; + + /// Rank of stride vector + static int const kStrideRank = kRank; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = Coord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRankN( + Stride const &stride = Stride() + ): + stride_(stride) { } + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRankN( + Coord const &stride_m, + Coord const &stride_n + ) { + + // Concatenate the strides + CUTLASS_PRAGMA_UNROLL + for (int m = 0; m < kRank/2; ++m) { + stride_[m] = stride_m[m]; + } + + CUTLASS_PRAGMA_UNROLL + for (int n = 0; n < kRank/2; ++n) { + stride_[n + kRank/2] = stride_n[n]; + } + } + + /// Ctor for N = 2 + CUTLASS_HOST_DEVICE + AffineRankN( + LongIndex const &stride_m, + LongIndex const &stride_n + ) { + stride_[0] = stride_m; + stride_[1] = stride_n; + } + + /// Ctor for N = 2 + CUTLASS_HOST_DEVICE + AffineRankN( + LongIndex const &stride + ) { + stride_[0] = stride; + stride_[1] = 1; + } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static AffineRankN packed(TensorCoord const &extent) { + + AffineRankN layout; + layout.stride_[kRank - 1] = 1; + + CUTLASS_PRAGMA_UNROLL + for (int i = kRank - 1; i > 0; --i) { + layout.stride_[i - 1] = layout.stride_[i] * extent[i]; + } + + return layout; + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(TensorCoord const &coord) const { + return dot(coord, stride_); + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + TensorCoord inverse(LongIndex offset) const { + return TensorCoord(); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(TensorCoord const &extent) const { + int idx = stride_.max_dim_index(); + return extent[idx] * stride_[idx]; + } +}; + +/// Mapping function for scenario in which both rows and columns are separated by a stride. +/// Row stride is smaller than column stride in AffineRank2ColumnMajor. +struct AffineRank2ColumnMajor { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 2; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2ColumnMajor( + Stride const &stride = Stride() + ): + stride_(stride) { } + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2ColumnMajor( + LongIndex row_stride, ///< stride between elements in consecutive rows + LongIndex column_stride ///< stride between elements in consecutive columns + ) + { stride_[0] = row_stride; stride_[1] = column_stride;} + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2ColumnMajor( + LongIndex stride + ) + { stride_[0] = 1; stride_[1] = stride;} + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static AffineRank2ColumnMajor packed(MatrixCoord const &extent) { + return AffineRank2ColumnMajor(1, extent.row()); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return dot(coord, stride_); + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + CUTLASS_UNUSED(offset); + return MatrixCoord(0, 0); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return extent.column() * stride_[1]; + } +}; + +/// Mapping function for scenario in which both rows and columns are separated by a stride. +/// Column stride is smaller than row stride in AffineRank2RowMajor. +struct AffineRank2RowMajor { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 2; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2RowMajor( + Stride const &stride = Stride() + ): + stride_(stride) { } + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2RowMajor( + LongIndex row_stride, ///< stride between elements in consecutive rows + LongIndex column_stride ///< stride between elements in consecutive columns + ) { stride_[0] = row_stride; stride_[1] = column_stride;} + + /// Ctor + CUTLASS_HOST_DEVICE + AffineRank2RowMajor( + LongIndex stride + ) { stride_[0] = stride; stride_[1] = 1;} + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static AffineRank2RowMajor packed(MatrixCoord const &extent) { + return AffineRank2RowMajor(1, extent.row()); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return dot(coord, stride_); + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + CUTLASS_UNUSED(offset); + return MatrixCoord(0, 0); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return extent.row() * stride_[0]; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +// Utility functions to convert stride_factor to the strides used by the Affine2 layout. +// +// stride_factor is the logical distance between two coorinates. +// +// All Coodinates used here are matrix coordinates. stride[0] and extent[0] are for the +// rows. stride[1] and extent[1] are for the columns. +template + struct Affine2Layout_Factory { + CUTLASS_HOST_DEVICE + static Affine2Layout layout_factory(cutlass::Coord<2> const &extent, typename Affine2Layout::Stride stride_factor) { + return Affine2Layout::packed(extent); + } +}; + +template <> +struct Affine2Layout_Factory { +CUTLASS_HOST_DEVICE +static cutlass::layout::AffineRank2ColumnMajor layout_factory( + cutlass::Coord<2> const &extent, + typename cutlass::layout::AffineRank2ColumnMajor::Stride stride_factor) { + return cutlass::layout::AffineRank2ColumnMajor({ stride_factor[0], stride_factor[0] * stride_factor[1] * extent[0] }); + } +}; + +template <> +struct Affine2Layout_Factory { +CUTLASS_HOST_DEVICE +static cutlass::layout::AffineRank2RowMajor layout_factory( + cutlass::Coord<2> const &extent, + typename cutlass::layout::AffineRank2RowMajor::Stride stride_factor) { + return cutlass::layout::AffineRank2RowMajor({ stride_factor[0] * stride_factor[1] * extent[1], stride_factor[1] }); + } +}; + +// The base layout cutlass::layout::AffineRankN<2> is similar to AffineRank2ColumnMajor +template <> +struct Affine2Layout_Factory> { +CUTLASS_HOST_DEVICE +static cutlass::layout::AffineRankN<2> layout_factory( + cutlass::Coord<2> const &extent, + typename cutlass::layout::AffineRankN<2>::Stride stride_factor) { + return cutlass::layout::AffineRankN<2>({ stride_factor[0], stride_factor[0] * stride_factor[1] * extent[0] }); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Mapping function for block-linear matrices. Matrix is structured +/// as column-major arrangement of 2D tiles (that are column-major). +template +struct ColumnMajorBlockLinear { + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + + /// Size of a block in rows + static int const kBlockRows = BlockRows; + + /// Size of a block in columns + static int const kBlockColumns = BlockColumns; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + ColumnMajorBlockLinear(Index ldm = 0): stride_(ldm) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static ColumnMajorBlockLinear packed(MatrixCoord const &extent) { + return ColumnMajorBlockLinear(extent.row() * kBlockRows * kBlockColumns); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return + (coord.row() % kBlockRows) + + (coord.column() % kBlockColumns) * kBlockRows + + (coord.row() / kBlockRows) * kBlockRows * kBlockColumns + + (coord.column() / kBlockColumns) * stride_[0]; + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + + return MatrixCoord(0, 0); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return (extent.column() + kBlockColumns - 1) / kBlockColumns * stride_[0]; + } +}; + +/// Mapping function for block-linear matrices. Matrix is structured +/// as row-major arrangement of 2D tiles (that are row-major) +template +struct RowMajorBlockLinear { + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 1; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + + /// Size of a block in rows + static int const kBlockRows = BlockRows; + + /// Size of a block in columns + static int const kBlockColumns = BlockColumns; + +private: + // + // Data members + // + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + RowMajorBlockLinear(Index ldm = 0): stride_(ldm) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static RowMajorBlockLinear packed(MatrixCoord const &extent) { + return RowMajorBlockLinear(extent.column() * kBlockRows * kBlockColumns); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + return + (coord.column() % kBlockColumns) + + (coord.row() % kBlockRows) * kBlockColumns + + (coord.column() / kBlockColumns) * kBlockRows * kBlockColumns + + (coord.row() / kBlockRows) * stride_[0]; + } + + /// Inverse of layout function, mapping linear offset to logical coordinate + CUTLASS_HOST_DEVICE + MatrixCoord inverse(LongIndex offset) const { + return MatrixCoord(0, 0); + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + return (extent.row() + kBlockRows - 1) / kBlockRows * stride_[0]; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +struct GeneralMatrix { + + /// Logical rank of tensor + static int const kRank = 2; + + /// Rank of stride vector + static int const kStrideRank = 2; + + /// Index type used for coordinates + using Index = int32_t; + + /// Long index type used for offsets + using LongIndex = int64_t; + + /// Logical coordinate + using TensorCoord = MatrixCoord; + + /// Stride vector + using Stride = Coord; + +private: + // + // Data members + // + + Matrix layout_id_; + + /// Stride data member + Stride stride_; + +public: + // + // Methods + // + + /// Ctor + CUTLASS_HOST_DEVICE + GeneralMatrix(): layout_id_(Matrix::kColumnMajor), stride_(make_Coord(0, 1)) { } + + /// Ctor + CUTLASS_HOST_DEVICE + GeneralMatrix( + Matrix layout_id, + Index ldm, + Index interleave): layout_id_(layout_id), stride_(make_Coord(ldm, interleave)) { } + + /// Helper returns a layout to a tightly packed tensor + CUTLASS_HOST_DEVICE + static GeneralMatrix packed( + MatrixCoord const &extent, + Matrix layout_id = Matrix::kColumnMajor, + Index interleave = 1) { + + Index c; + if (layout_id == Matrix::kRowMajor) { + c = extent.column(); + } + else { + c = extent.row(); + } + + Index ldm = c * interleave; + + return GeneralMatrix(layout_id, ldm, interleave); + } + + /// Returns the offset of a coordinate in linear memory. + /// Assumes coordinate has convention (row, column) + CUTLASS_HOST_DEVICE + LongIndex operator()(MatrixCoord const &coord) const { + Index c, s; + if (layout_id_ == Matrix::kRowMajor) { + c = coord.column(); + s = coord.row(); + } + else { + s = coord.column(); + c = coord.row(); + } + + Index v = s / stride_[1]; + Index residual = (s % stride_[1]); + + return LongIndex(c) * LongIndex(stride_[1]) + LongIndex(v) * LongIndex(stride_[0]) + residual; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride stride() const { + return stride_; + } + + CUTLASS_HOST_DEVICE + Matrix layout_id() const { + return layout_id_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + Stride & stride() { + return stride_; + } + + CUTLASS_HOST_DEVICE + Matrix & layout_id() { + return layout_id_; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index stride(int idx) const { + return stride_[idx]; + } + + /// Returns the stride of the layout + CUTLASS_HOST_DEVICE + typename Stride::Index & stride(int idx) { + return stride_[idx]; + } + + /// Compute the number of contiguous elements needed to store a tensor with the given size + CUTLASS_HOST_DEVICE + LongIndex capacity(MatrixCoord const &extent) const { + Index s; + if (layout_id_ == Matrix::kRowMajor) { + s = extent.row(); + } + else { + s = extent.column(); + } + + Index v = Index((s + stride_[1] - 1) / stride_[1]); + return LongIndex(v) * LongIndex(stride_[0]); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Defines transposes of matrix layouts +template +struct LayoutTranspose; + +/// Transpose of row-major is column-major +template <> +struct LayoutTranspose { + using type = layout::ColumnMajor; +}; + +/// Transpose of column-major is row-major +template <> +struct LayoutTranspose { + using type = layout::RowMajor; +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace layout +} // namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/matrix_coord.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/matrix_coord.h new file mode 100644 index 000000000..156357544 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/matrix_coord.h @@ -0,0 +1,164 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Defines a canonical coordinate for rank=2 matrices offering named indices. +*/ +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/coord.h" + +namespace cutlass { + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// MatrixCoord wraps Coord<2, int> to provide a helper for accessing named dimensions. Classes +/// expecting a coordinate in the rank=2 index space of a matrix should use MatrixCoord. +struct MatrixCoord : public Coord<2, int> { + +public: + + /// Integer-valued index + using Index = int; + + /// Base type is a Coord of rank=2 + using Base = Coord<2, Index>; + + /// LongIndex type + using LongIndex = typename Base::LongIndex; + +private: + + /// Rows dimension + static int const kRow = 0; + + /// Columns dimension + static int const kColumn = 1; + +public: + + // + // Methods + // + + /// Default ctor + CUTLASS_HOST_DEVICE + MatrixCoord() { } + + /// Constructs from Coord<2> + CUTLASS_HOST_DEVICE + MatrixCoord(Coord<2, Index> const &coord): Base(coord) { } + + /// Helper to construct from a row and column + CUTLASS_HOST_DEVICE + MatrixCoord(Index row, Index column): Base(make_Coord(row, column)) { } + + /// Helper to construct from a row and column, which are LongIndex based + CUTLASS_HOST_DEVICE + MatrixCoord(LongIndex row, LongIndex column): Base(make_Coord(Index(row), Index(column))) { } + + /// Returns the row of the coordinate + CUTLASS_HOST_DEVICE + Index const & row() const { return this->at(kRow); } + + /// Returns the row of the coordinate + CUTLASS_HOST_DEVICE + Index & row() { return this->at(kRow); } + + /// Returns the column of the coordinate + CUTLASS_HOST_DEVICE + Index const & column() const { return this->at(kColumn); } + + /// Returns the column of the coordinate + CUTLASS_HOST_DEVICE + Index & column() { return this->at(kColumn); } + + // + // Coord operators + // + + /// Element-wise addition + CUTLASS_HOST_DEVICE + MatrixCoord operator+(Base const& b) const { + return MatrixCoord(Base::operator+(b)); + } + + /// Element-wise subtraction + CUTLASS_HOST_DEVICE + MatrixCoord operator-(Base const& b) const { + return MatrixCoord(Base::operator-(b)); + } + + /// Element-wise multiplication + CUTLASS_HOST_DEVICE + MatrixCoord operator*(Base const& b) const { + return MatrixCoord(Base::operator*(b)); + } + + /// Element-wise division + CUTLASS_HOST_DEVICE + MatrixCoord operator/(Base const& b) const { + return MatrixCoord(Base::operator/(b)); + } + + /// In-place addition + CUTLASS_HOST_DEVICE + MatrixCoord& operator+=(Base const& b) { + Base::operator+=(b); + return *this; + } + + /// In-place subtraction + CUTLASS_HOST_DEVICE + MatrixCoord& operator-=(Base const& b) { + Base::operator-=(b); + return *this; + } + + /// In-place multiplication + CUTLASS_HOST_DEVICE + MatrixCoord& operator*=(Base const& b) { + Base::operator*=(b); + return *this; + } + + /// In-place division + CUTLASS_HOST_DEVICE + MatrixCoord& operator/=(Base const& b) { + Base::operator/=(b); + return *this; + } + +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_conversion_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_conversion_hip.h new file mode 100644 index 000000000..369ff66ad --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_conversion_hip.h @@ -0,0 +1,2685 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Boost-like numeric conversion operator for CUTLASS numeric types +*/ +#pragma once + +#if !defined(__CUDACC_RTC__) +#include +#endif + +#include "cutlass/cutlass.h" +#include "cutlass/numeric_types.h" +#include "cutlass/transform/thread/unary_op_hip.h" + +#include "cutlass/array_hip.h" +#include "cutlass/half.h" + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Floating-point rounding style similare to Standard Library's formats but supporting +/// additional rounding options. +enum class FloatRoundStyle { + round_indeterminate, ///< rounding mode unknown + round_toward_zero, ///< round toward zero + round_to_nearest, ///< round to nearest even + round_to_nearest_satfinite, ///< round to nearest even, capping value to min and max of destination type + round_toward_infinity, ///< round toward infinity + round_toward_neg_infinity, ///< round toward negative infinity + round_half_ulp_truncate, ///< add 0.5ulp to integer representation then round toward zero + round_half_ulp_trunc_dntz ///< like round_half_ulp_truncate, except denorms are rounded *toward* zero +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template < + typename T, + typename S, + FloatRoundStyle Round = FloatRoundStyle::round_to_nearest +> +struct NumericConverter { + + using result_type = T; + using source_type = S; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return static_cast(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for float => int32_t +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(__HIP_DEVICE_COMPILE__) +template <> +struct NumericConverter { + + using result_type = int32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return __float2int_rn(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + + using result_type = int32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return __float2int_rz(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#elif !defined(__CUDACC_RTC__) + +template <> +struct NumericConverter { + + using result_type = int32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + static result_type convert(source_type const & s) { + std::fesetround(FE_TONEAREST); + return (result_type)std::nearbyint(s); + } + + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + + using result_type = int32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + static result_type convert(source_type const & s) { + std::fesetround(FE_TOWARDZERO); + return (result_type)std::nearbyint(s); + } + + result_type operator()(source_type const &s) const { + return convert(s); + } +}; +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for float => int8_t +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(__HIP_DEVICE_COMPILE__) +template <> +struct NumericConverter { + + using result_type = int8_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + int32_t intermediate; +#ifndef DCU_ASM + asm volatile("cvt.rni.sat.s8.f32 %0, %1;" : "=r"(intermediate) : "f"(s)); +#else + intermediate = (int32_t)s; +#endif + + return static_cast(intermediate); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + + using result_type = int8_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + int32_t intermediate; +#ifndef DCU_ASM + asm volatile("cvt.rzi.sat.s8.f32 %0, %1;" : "=r"(intermediate) : "f"(s)); +#else + intermediate = (int32_t)s; +#endif + + return static_cast(intermediate); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#elif !defined(__CUDACC_RTC__) + +template <> +struct NumericConverter { + + using result_type = int8_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + static result_type convert(source_type const & s) { + std::fesetround(FE_TONEAREST); + int32_t intermediate = (int32_t)std::nearbyint(s); + + // Low-end saturation + intermediate = std::max(intermediate, (int32_t)std::numeric_limits::lowest()); + + // High-end saturation + intermediate = std::min(intermediate, (int32_t)std::numeric_limits::max()); + + return static_cast(intermediate); + } + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + + using result_type = int8_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + static result_type convert(source_type const & s) { + std::fesetround(FE_TOWARDZERO); + int32_t intermediate = (int32_t)std::nearbyint(s); + + // Low-end saturation + intermediate = std::max(intermediate, (int32_t)std::numeric_limits::lowest()); + + // High-end saturation + intermediate = std::min(intermediate, (int32_t)std::numeric_limits::max()); + + return static_cast(intermediate); + } + + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for float <= half_t +template +struct NumericConverter { + + using result_type = T; + using source_type = T; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return s; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for float <=> half_t +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for float <= half_t +template +struct NumericConverter { + + using result_type = float; + using source_type = half_t; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + result_type result = static_cast(s); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Specialization for round-to-nearest +template <> +struct NumericConverter { + + using result_type = half_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + result_type result = static_cast(s); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + + +template <> +struct NumericConverter { + + using result_type = float_e5m2_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + result_type result = float_e5m2_t(s); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + + return convert(s); + } +}; + +template <> +struct NumericConverter { + + using result_type = float_e4m3_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + result_type result = float_e4m3_t(s); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + + return convert(s); + } +}; + +/// Specialization for round-toward-zero +template <> +struct NumericConverter { + + using result_type = half_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + /// Round toward zero + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & flt) { + + #if (defined(__gfx906__) || defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM) //defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + + return half_t(__float2half_rz(flt)); + #else + // software implementation rounds toward nearest even + unsigned const& s = reinterpret_cast(flt); + uint16_t sign = uint16_t((s >> 16) & 0x8000); + int32_t exp = int32_t((s >> 23) & 0xff) - 127; + int mantissa = s & 0x7fffff; + uint16_t u = 0; + + if ((s & 0x7fffffff) == 0) { + // sign-preserving zero + return cutlass::half_t::bitcast(sign); + } + + if (exp > 15) { + if (exp == 128 && mantissa) { + // not a number + u = 0x7fff; + } else { + // overflow to infinity + u = sign | 0x7c00; + } + return half_t::bitcast(u); + } + + if (exp >= -14) { + // normal fp32 to normal fp16 + u = uint16_t((uint32_t(exp + 15) & 0x1f) << 10); + u = uint16_t(u | (mantissa >> 13)); + } else { + // normal single-precision to subnormal half_t-precision representation + int rshift = (-14 - exp); + if (rshift < 32) { + mantissa |= (1 << 23); + mantissa = (mantissa >> rshift); + u = (uint16_t(mantissa >> 13) & 0x3ff); + } else { + mantissa = 0; + u = 0; + } + } + + u |= sign; + + return half_t::bitcast(u); + + #endif // defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for float <=> bfloat16_t +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for float <= bfloat16_t +template +struct NumericConverter { + + using result_type = float; + using source_type = bfloat16_t; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return static_cast(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = bfloat16_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + uint32_t x32 = reinterpret_cast(s); + x32 += 0x7fff + ((x32 >> 16) & 1); + return bfloat16_t::bitcast(uint16_t(x32 >> 16)); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = bfloat16_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_half_ulp_truncate; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + uint32_t x32 = reinterpret_cast(s); + x32 += 0x8000u; + return bfloat16_t::bitcast(uint16_t(x32 >> 16)); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = bfloat16_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + #ifdef DCU_ASM + union + { + float fp32; + uint32_t int32; + } u = {s}; + return bfloat16_t::bitcast(u.int32 >> 16); + #else + uint32_t x32 = reinterpret_cast(s); + uint16_t x16 = uint16_t(x32 >> 16); + + return bfloat16_t::bitcast(x16); + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for float <=> tfloat32_t +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for float <= tfloat32_t +template +struct NumericConverter { + + using result_type = float; + using source_type = tfloat32_t; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + return static_cast(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = tfloat32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + unsigned storage = reinterpret_cast(s); + +#if defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ >= 900 + asm volatile("cvt.rn.tf32.f32 %0, %1;" : "=r"(storage) : "r"(storage)); +#else + if ((storage & 0x7f800000) != 0x7f800000) { + + bool mantissa_bit = ((storage & (1 << 13)) != 0); + bool round_bit = ((storage & (1 << 12)) != 0); + bool sticky_bit = ((storage & ((1 << 12) - 1)) != 0); + + if ((round_bit && sticky_bit) || (round_bit && mantissa_bit)) { + storage += uint32_t(1 << 13); + } + + // Note, the following is intentionally commented out. TF32 + // does not define the low order bits, so they may be left in + // an undefined state. + // + // By not truncating these bit explicitly, we avoid an extra logical + // operation. + // + // TF32 may be implicitly converted to float by performing this + // operation as needed. + // + // storage = (storage & ~0x1fff); + } + else if (storage & ~0xff800000) { + storage = 0x7fffffff; + } +#endif + + return tfloat32_t::bitcast(storage); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = tfloat32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_half_ulp_truncate; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + return tfloat32_t::round_half_ulp_truncate(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// This rounding operation is similar to half_ulp_truncate except it rounds denorms toward zero. +/// It avoids predicated code, though it requires a temporary register. +template <> +struct NumericConverter { + using result_type = tfloat32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_half_ulp_trunc_dntz; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + unsigned y = reinterpret_cast(s); + y = y & 0xff800000; + float d = reinterpret_cast(y); + float z = d / float(1 << 11) + s; + + return reinterpret_cast(z); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template <> +struct NumericConverter { + using result_type = tfloat32_t; + using source_type = float; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + uint32_t x = reinterpret_cast(s); + return tfloat32_t::bitcast(x & 0xffffe000); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Conversion operator for float to tfloat32_t big and small values +// +///////////////////////////////////////////////////////////////////////////////////////////////// +template < + FloatRoundStyle RoundBig = FloatRoundStyle::round_toward_zero, + FloatRoundStyle RoundSmall = FloatRoundStyle::round_half_ulp_truncate +> +struct NumericConverterFastF32 { + + // result_type holds big tfloat32_t at idx(0) and small tfloat32_t at idx(1) + using result_type = Array; + + // source data type + using source_type = float; + + // rounding styles for big and small part + static FloatRoundStyle const kRoundBig = RoundBig; + static FloatRoundStyle const kRoundSmall = RoundSmall; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + result_type result; + NumericConverter convert_big_; + NumericConverter convert_small_; + + // convert and fill tfloat32_t big at idx 0 + result[0] = convert_big_(source); + + // convert and fill tfloat32_t small at idx 1 + result[1] = convert_small_(source - static_cast(result[0])); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Conversion and Clamp operator for Integers +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +template < + typename T, + typename S +> +struct NumericConverterClamp { + + using result_type = T; + using source_type = S; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + NumericConverter convert_op; + result_type const kClamp_max = platform::numeric_limits::max(); + result_type const kClamp_min = platform::numeric_limits::lowest(); + if (s < (source_type)kClamp_min) + return kClamp_min; + if (s > (source_type)kClamp_max) + return kClamp_max; + return convert_op(s); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +// This converter is needed to enable half_t output types when using int32_t accumulators. +// Since floating-point types do not require a clamp, this converter simply casts from +// the source type to half_t. +template < + typename S +> +struct NumericConverterClamp { + + using result_type = cutlass::half_t; + using source_type = S; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const &source) { + return static_cast(source); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Conversion operator for Array +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Conversion operator for Array +template < + typename T, + typename S, + int N, + FloatRoundStyle Round = FloatRoundStyle::round_to_nearest, + typename Transform = cutlass::transform::thread::UnaryTransform::Identity +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + static_assert(platform::is_same::value || + platform::is_same::value, + "Unary Operator not supported."); + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & s) { + + result_type result; + NumericConverter convert_; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + if (platform::is_same::value) { + result[i] = convert_(s[i]); + } else { // conjugate + result[i] = conj(convert_(s[i])); + } + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +template < + typename T, + int N, + FloatRoundStyle Round, + typename Transform +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + static_assert(platform::is_same::value || + platform::is_same::value, + "Unary Operator not supported."); + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const &source) { + if (platform::is_same::value) { + return source; + } else { + result_type result; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + result[i] = conj(source[i]); + } + return result; + } + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array, round to nearest +template <> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + Array result; + #ifdef MIX_FP16_EPILOGUE + + // 使用指令float2 转换为 half2 V_CVT_PKRTZ_F16_F32 + __half2 rst; + float s1 = source[0]; + float s2 = source[1]; + asm volatile( + "v_cvt_pkrtz_f16_f32 %0 %1 %2" + :"=v"(rst),"+v"(s1),"+v"(s2) + ); + + result = reinterpret_cast &>(rst); + #else + #if (defined(__gfx906__) || defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM)//defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + float s1 = source[0]; + float s2 = source[1]; + // if(threadIdx.x == 0 && blockIdx.x == 0 && blockIdx.y == 0 && blockIdx.z == 0) + // { + // printf("src value:%f %f \n",s1,s2); + // } + //__half2 rst; + auto rst = __builtin_amdgcn_cvt_pkrtz(s1, s2); + // asm volatile( + // "v_cvt_pkrtz_f16_f32 %0 %1 %2\n\t" + // :"+v"(rst),"+v"(s1),"+v"(s2) + // ); + + // asm volatile("v_cvt_f16_f32 %0,%1\n\t" + // :"=v"(rst.x) + // :"v"(s1)); + // asm volatile("v_cvt_f16_f32 %0,%1\n\t" + // :"=v"(rst.y) + // :"v"(s2)); + result = reinterpret_cast &>(rst); + + // reinterpret_cast<__half2 &>(result) = __float22half2_rn(reinterpret_cast(source)); + #else + NumericConverter convert_; + result[0] = convert_(source[0]); + result[1] = convert_(source[1]); + #endif + #endif + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array, round to nearest +template +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + Array result; + + #if (defined(__gfx906__) || defined(__gfx928__) || defined(__gfx936__) || defined(__gfx938__)) && defined(DCU_ASM)//defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 530) + reinterpret_cast(result) = __half22float2(reinterpret_cast<__half2 const &>(source)); + #else + NumericConverter convert_; + result[0] = convert_(source[0]); + result[1] = convert_(source[1]); + #endif + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + NumericConverter convert_element_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + if (N % 2) { + result[N - 1] = convert_element_(source[N - 1]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + NumericConverter convert_element_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + if (N % 2) { + result[N - 1] = convert_element_(source[N - 1]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(__gfx938__) +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array, round to nearest +template <> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = FloatRoundStyle::round_to_nearest; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + // unsigned d; + + // asm volatile("v_cvt_pk_bf16_f32 %0 %1 %2" : + // "=v"(d) : + // "v"(source[0]), "v"(source[1])); + auto d = __builtin_hcu_cvt_pk_bf16_f32(0, source[0], 0, source[1], 0); + + + return reinterpret_cast(d); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array, round_toward_zero +template <> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = FloatRoundStyle::round_toward_zero; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + result_type result; + NumericConverter convert_; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 2; ++i) { + + result[i] = convert_(source[i]); + + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array, round_half_ulp_truncate +template <> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = FloatRoundStyle::round_half_ulp_truncate; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + result_type result; + NumericConverter convert_; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 2; ++i) { + + result[i] = convert_(source[i]); + + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + NumericConverter convert_element_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 2; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + if (N % 2) { + result[N - 1] = convert_element_(source[N - 1]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#endif // if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 800) + +///////////////////////////////////////////////////////////////////////////////////////////////// + +// Conditional guards to enable partial specialization for packed integers +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 720) && \ + ((__CUDACC_VER_MAJOR__ > 10) || \ + ((__CUDACC_VER_MAJOR__ >= 10) && (__CUDACC_VER_MINOR__ >= 2))) + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + NumericConverter convert_element_; + + result_type result; + + result[0] = convert_element_(source[0]); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + uint32_t tmp; + + asm volatile( + "cvt.pack.sat.s8.s32.b32 %0, %2, %1, 0;\n" + : "=r"(tmp) : "r"(source[0]), "r"(source[1])); + + uint16_t out = (tmp & 0xffff); + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + unsigned out; + + asm volatile( + "{ .reg .u32 r4;" + "cvt.pack.sat.s8.s32.b32 r4, %4, %3, 0;" + "cvt.pack.sat.s8.s32.b32 %0, %2, %1, r4;" + "}" + : "=r"(out) : "r"(source[0]), "r"(source[1]), "r"(source[2]), "r"(source[3])); + + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + static_assert(!(N % 4), "N must be multiple of 4."); + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 4; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + NumericConverter convert_element_; + + result_type result; + + result[0] = convert_element_(source[0]); + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + uint32_t tmp; + + asm volatile( + "cvt.pack.sat.u8.s32.b32 %0, %2, %1, 0;\n" + : "=r"(tmp) : "r"(source[0]), "r"(source[1])); + + uint16_t out = (tmp & 0xffff); + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + unsigned out; + + asm volatile( + "{ .reg .u32 r4;" + "cvt.pack.sat.u8.s32.b32 r4, %4, %3, 0;" + "cvt.pack.sat.u8.s32.b32 %0, %2, %1, r4;" + "}" + : "=r"(out) : "r"(source[0]), "r"(source[1]), "r"(source[2]), "r"(source[3])); + + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + static_assert(!(N % 4), "N must be multiple of 4."); + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 4; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for Array <=> Array +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float; + using source_element = float_e4m3_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out_fp16[2]; + uint32_t const& src_packed = reinterpret_cast(source); + + asm volatile( \ + "{\n" \ + ".reg .b16 lo, hi;\n" \ + "mov.b32 {lo, hi}, %2;\n" \ + "cvt.rn.f16x2.e4m3x2 %0, lo;\n" \ + "cvt.rn.f16x2.e4m3x2 %1, hi;\n" \ + "}\n" : "=r"(out_fp16[0]), "=r"(out_fp16[1]) : "r"(src_packed)); + + float2 res0 = __half22float2(reinterpret_cast<__half2 &>(out_fp16[0])); + float2 res1 = __half22float2(reinterpret_cast<__half2 &>(out_fp16[1])); + + result_type out; + out[0] = res0.x; + out[1] = res0.y; + out[2] = res1.x; + out[3] = res1.y; + return out; + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e4m3_t; + using source_element = float; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(HIP_FP8_CVT_ENABLE) + int32_t result; + result = __builtin_hcu_cvt_pk_fp8_f32(source[0], source[1], result, false); + result = __builtin_hcu_cvt_pk_fp8_f32(source[2], source[3], result, true); + + return reinterpret_cast(result); + + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float; + using source_element = float_e5m2_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out_fp16[2]; + uint32_t const& src_packed = reinterpret_cast(source); + + asm volatile( \ + "{\n" \ + ".reg .b16 lo, hi;\n" \ + "mov.b32 {lo, hi}, %2;\n" \ + "cvt.rn.f16x2.e5m2x2 %0, lo;\n" \ + "cvt.rn.f16x2.e5m2x2 %1, hi;\n" \ + "}\n" : "=r"(out_fp16[0]), "=r"(out_fp16[1]) : "r"(src_packed)); + + float2 res0 = __half22float2(reinterpret_cast<__half2 &>(out_fp16[0])); + float2 res1 = __half22float2(reinterpret_cast<__half2 &>(out_fp16[1])); + + result_type out; + out[0] = res0.x; + out[1] = res0.y; + out[2] = res1.x; + out[3] = res1.y; + return out; + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e5m2_t; + using source_element = float; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + // #if defined(CUDA_PTX_FP8_CVT_ENABLED) + // uint32_t out; + + // asm volatile( \ + // "{\n" \ + // ".reg .b16 lo;\n" \ + // ".reg .b16 hi;\n" \ + // "cvt.rn.satfinite.e5m2x2.f32 lo, %2, %1;\n" \ + // "cvt.rn.satfinite.e5m2x2.f32 hi, %4, %3;\n" \ + // "mov.b32 %0, {lo, hi};\n" \ + // "}" \ + // : "=r"(out) : "f"(source[0]), "f"(source[1]), "f"(source[2]), "f"(source[3])); + + // return reinterpret_cast(out); + // #else + // result_type result; + // NumericConverter converter; + + // CUTLASS_PRAGMA_UNROLL + // for (int i = 0; i < 4; ++i) { + // result[i] = converter(source[i]); + // } + + // return result; + // #endif + + #if defined(HIP_FP8_CVT_ENABLE) + int32_t result; + result = __builtin_hcu_cvt_pk_bf8_f32(source[0], source[1], result, false); + result = __builtin_hcu_cvt_pk_bf8_f32(source[2], source[3], result, true); + + return reinterpret_cast(result); + + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for Array <=> Array +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = half_t; + using source_element = float_e4m3_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out[2]; + uint32_t const& src_packed = reinterpret_cast(source); + asm volatile( \ + "{\n" \ + ".reg .b16 lo, hi;\n" \ + "mov.b32 {lo, hi}, %2;\n" \ + "cvt.rn.f16x2.e4m3x2 %0, lo;\n" \ + "cvt.rn.f16x2.e4m3x2 %1, hi;\n" \ + "}\n" : "=r"(out[0]), "=r"(out[1]) : "r"(src_packed)); + return reinterpret_cast(out); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e4m3_t; + using source_element = half_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out; + uint32_t const* src_packed = reinterpret_cast(&source); + + asm volatile( \ + "{\n" \ + ".reg .b16 lo;\n" \ + ".reg .b16 hi;\n" \ + "cvt.rn.satfinite.e4m3x2.f16x2 lo, %1;\n" \ + "cvt.rn.satfinite.e4m3x2.f16x2 hi, %2;\n" \ + "mov.b32 %0, {lo, hi};\n" \ + "}" \ + : "=r"(out) : "r"(src_packed[0]), "r"(src_packed[1])); + + return reinterpret_cast(out); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = half_t; + using source_element = float_e5m2_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out[2]; + uint32_t const& src_packed = reinterpret_cast(source); + asm volatile( \ + "{\n" \ + ".reg .b16 lo, hi;\n" \ + "mov.b32 {lo, hi}, %2;\n" \ + "cvt.rn.f16x2.e5m2x2 %0, lo;\n" \ + "cvt.rn.f16x2.e5m2x2 %1, hi;\n" \ + "}\n" : "=r"(out[0]), "=r"(out[1]) : "r"(src_packed)); + return reinterpret_cast(out); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e5m2_t; + using source_element = half_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + uint32_t out; + uint32_t const* src_packed = reinterpret_cast(&source); + + asm volatile( \ + "{\n" \ + ".reg .b16 lo;\n" \ + ".reg .b16 hi;\n" \ + "cvt.rn.satfinite.e5m2x2.f16x2 lo, %1;\n" \ + "cvt.rn.satfinite.e5m2x2.f16x2 hi, %2;\n" \ + "mov.b32 %0, {lo, hi};\n" \ + "}" \ + : "=r"(out) : "r"(src_packed[0]), "r"(src_packed[1])); + + return reinterpret_cast(out); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for Array <=> Array +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = bfloat16_t; + using source_element = float_e4m3_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + // Convert f8 to float + NumericArrayConverter src2float; + Array tmp_floats = src2float(source); + + // Convert float to bf16 + result_type out; + Array* packed_tmp = reinterpret_cast*>(&tmp_floats); + Array* packed_out = reinterpret_cast*>(&out); + NumericArrayConverter float2result; + packed_out[0] = float2result(packed_tmp[0]); + packed_out[1] = float2result(packed_tmp[1]); + + return out; + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e4m3_t; + using source_element = bfloat16_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + // Convert bf16 to float + Array tmp; + Array* packed_tmp = reinterpret_cast*>(&tmp); + Array const* packed_source = reinterpret_cast const*>(&source); + NumericArrayConverter src2float; + packed_tmp[0] = src2float(packed_source[0]); + packed_tmp[1] = src2float(packed_source[1]); + + // Convert float to f8 + NumericArrayConverter float2result; + return float2result(tmp); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = bfloat16_t; + using source_element = float_e5m2_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + // Convert f8 to float + NumericArrayConverter src2float; + Array tmp_floats = src2float(source); + + // Convert float to bf16 + result_type out; + Array* packed_tmp = reinterpret_cast*>(&tmp_floats); + Array* packed_out = reinterpret_cast*>(&out); + NumericArrayConverter float2result; + packed_out[0] = float2result(packed_tmp[0]); + packed_out[1] = float2result(packed_tmp[1]); + + return out; + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e5m2_t; + using source_element = bfloat16_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + + #if defined(CUDA_PTX_FP8_CVT_ENABLED) + // Convert bf16 to float + Array tmp; + Array* packed_tmp = reinterpret_cast*>(&tmp); + Array const* packed_source = reinterpret_cast const*>(&source); + NumericArrayConverter src2float; + packed_tmp[0] = src2float(packed_source[0]); + packed_tmp[1] = src2float(packed_source[1]); + + // Convert float to f8 + NumericArrayConverter float2result; + return float2result(tmp); + #else + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + #endif + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for Array <=> Array +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e4m3_t; + using source_element = float_e5m2_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e5m2_t; + using source_element = float_e4m3_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + result_type result; + NumericConverter converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + result[i] = converter(source[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for: +// Array <=> Array +// Array <=> Array +// +// These are needed to avoid multiple-matching-template compilation errors (e.g., when +// compiling float_e4m3_t <=> float_e4m3_t, which among T <= float_e4m3_t and float_e4m3_t <= T +// should be used?) +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e4m3_t; + using source_element = float_e4m3_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const &source) { + return source; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + using result_element = float_e5m2_t; + using source_element = float_e5m2_t; + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const &source) { + return source; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// Partial specializations for: +// Array <=> Array +// Array <=> Array +// using packed converter under the hood +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +template < + typename T, + typename S, + int N, + FloatRoundStyle Round +> +struct PackedNumericArrayConverter { + using result_element = T; + using source_element = S; + + using result_type = Array; + using source_type = Array; + + static FloatRoundStyle const round_style = Round; + +private: + using packed_result_type = Array; + using packed_source_type = Array; + +public: + CUTLASS_DEVICE + static result_type convert(source_type const & source) { + result_type result; + packed_result_type* packed_result = reinterpret_cast(&result); + const packed_source_type* packed_source = reinterpret_cast(&source); + + NumericArrayConverter packed_converter; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 4; ++i) { + packed_result[i] = packed_converter(packed_source[i]); + } + + // Handle leftovers + NumericConverter converter; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N % 4; ++i) { + int idx = ((N / 4) * 4) + i; + result[idx] = converter(source[idx]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const{ + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + typename T, + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + typename T, + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + typename S, + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + typename S, + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter : + public PackedNumericArrayConverter {}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Partial specialization for Array <= Array +/// Conversion is performed with saturation regardless of setting of +/// the `Round` template parameter. +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + // Convert float to int + Array temporary; + + NumericArrayConverter compute_converter; + temporary = compute_converter(source); + + // Convert to int to int8_t + NumericArrayConverter destination_converter; + return destination_converter(temporary); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +#if defined(__HIP_DEVICE_COMPILE__) && (__HIP_DEVICE_COMPILE__ >= 750) && \ + ((__CUDACC_VER_MAJOR__ > 10) || \ + ((__CUDACC_VER_MAJOR__ >= 10) && (__CUDACC_VER_MINOR__ >= 2))) + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + unsigned out; + + asm volatile( + "{ .reg .u32 r4;" + "cvt.pack.sat.s4.s32.b32 r4, %8, %7, 0;" + "cvt.pack.sat.s4.s32.b32 r4, %6, %5, r4;" + "cvt.pack.sat.s4.s32.b32 r4, %4, %3, r4;" + "cvt.pack.sat.s4.s32.b32 %0, %2, %1, r4;" + "}" + : "=r"(out) + : "r"(source[0]), "r"(source[1]), "r"(source[2]), "r"(source[3]), + "r"(source[4]), "r"(source[5]), "r"(source[6]), "r"(source[7])); + + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + static_assert(!(N % 8), "N must be multiple of 8."); + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 8; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + FloatRoundStyle Round +> +struct NumericArrayConverter { + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + unsigned out; + + asm volatile( + "{ .reg .u32 r4;" + "cvt.pack.sat.u4.s32.b32 r4, %8, %7, 0;" + "cvt.pack.sat.u4.s32.b32 r4, %6, %5, r4;" + "cvt.pack.sat.u4.s32.b32 r4, %4, %3, r4;" + "cvt.pack.sat.u4.s32.b32 %0, %2, %1, r4;" + "}" + : "=r"(out) + : "r"(source[0]), "r"(source[1]), "r"(source[2]), "r"(source[3]), + "r"(source[4]), "r"(source[5]), "r"(source[6]), "r"(source[7])); + + return reinterpret_cast(out); + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +/// Partial specialization for Array <= Array +template < + int N, + FloatRoundStyle Round +> +struct NumericArrayConverter { + static_assert(!(N % 8), "N must be multiple of 8."); + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_HOST_DEVICE + static result_type convert(source_type const & source) { + + NumericArrayConverter convert_vector_; + + result_type result; + + Array *result_ptr = reinterpret_cast *>(&result); + Array const *source_ptr = reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 8; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + return result; + } + + CUTLASS_HOST_DEVICE + result_type operator()(source_type const &s) const { + return convert(s); + } +}; + +#endif // Conditional guards to enable partial specialization for packed integers + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// FastNumericArrayConverter only works when the source is within center range. +/// Conversion operator for Array. See the comments before +/// FastLinearCombinationClamp. +template +struct FastNumericArrayConverter { + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const &s) { + NumericArrayConverter convert_; + + return convert_(s); + } + + CUTLASS_DEVICE + result_type operator()(source_type const &s) const { return convert(s); } +}; + +/// Partial specialization for Array <= Array +template +struct FastNumericArrayConverter { + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const &source) { + result_type result; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + int tmp = source[i] + 1262485504 /*0x4B400000*/; + result[i] = reinterpret_cast(tmp) - 12582912.0f; + } + + return result; + } + + CUTLASS_DEVICE + result_type operator()(source_type const &s) const { return convert(s); } +}; + +/// Partial specialization for Array <= Array +template +struct FastNumericArrayConverter { + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const &source) { + Array result; + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < 4; ++i) { + float tmp = source[i] + 12582912.0f; + result[i] = reinterpret_cast(tmp); + } + + result[0] = __byte_perm(result[0], result[1], 0x40); + result[2] = __byte_perm(result[2], result[3], 0x40); + result[0] = __byte_perm(result[0], result[2], 0x5410); + + return reinterpret_cast(result[0]); + } + + CUTLASS_DEVICE + result_type operator()(source_type const &s) const { return convert(s); } +}; + +/// Partial specialization for Array <= Array +template +struct FastNumericArrayConverter { + static_assert(!(N % 4), "N must be multiple of 4."); + + using result_type = Array; + using source_type = Array; + static FloatRoundStyle const round_style = Round; + + CUTLASS_DEVICE + static result_type convert(source_type const &source) { + FastNumericArrayConverter convert_vector_; + + result_type result; + + Array *result_ptr = + reinterpret_cast *>(&result); + Array const *source_ptr = + reinterpret_cast const *>(&source); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N / 4; ++i) { + result_ptr[i] = convert_vector_(source_ptr[i]); + } + + return result; + } + + CUTLASS_DEVICE + result_type operator()(source_type const &s) const { return convert(s); } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Defines preferred rounding mode for a pair of types +template +struct PreferredRoundingMode { + static FloatRoundStyle const kRound = FloatRoundStyle::round_to_nearest; +}; + +#if defined(__HIP_DEVICE_COMPILE__) && __HIP_DEVICE_COMPILE__ < 900 +/// Defines preferred rounding mode for a pair of types +template <> +struct PreferredRoundingMode { + static FloatRoundStyle const kRound = FloatRoundStyle::round_half_ulp_truncate; +}; +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Packs predicates into an array. +template +struct PackPredicates { + using result_type = Array; + + static_assert(!(N % 4), "Must pack predicates in a count that is a multiple of 4"); + + CUTLASS_HOST_DEVICE + result_type operator()(bool const predicates[]) { + + result_type packed; + packed.clear(); + + int const kWordSize = 8; + uint8_t *bytes = reinterpret_cast(packed.data()); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + int word_idx = (i / kWordSize); + int bit_idx = (i % kWordSize); + + uint8_t mask = ((predicates[i] ? 1u : 0u) << bit_idx); + bytes[word_idx] = (bytes[word_idx] | mask); + } + return packed; + } +}; + +/// Packs predicates into an array +template +struct UnpackPredicates { + using result_type = Array; + + static_assert(!(N % 4), "Must unpack predicates in a count that is a multiple of 4"); + + CUTLASS_HOST_DEVICE + void operator()(bool predicates[], result_type const &packed) { + + int const kWordSize = 8; + uint8_t const *bytes = reinterpret_cast(packed.data()); + + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < N; ++i) { + int word_idx = (i / kWordSize); + int bit_idx = (i % kWordSize); + + predicates[i] = bool((bytes[word_idx] >> bit_idx) & 0x1); + } + + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_size.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_size.h new file mode 100644 index 000000000..951e521c0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_size.h @@ -0,0 +1,93 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Top-level include for all CUTLASS numeric types. +*/ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +#pragma once + +#include "cutlass/cutlass.h" + +///////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Defines the size of an element in bits +template +struct sizeof_bits { + static int const value = int(sizeof(T) * 8); +}; + +template +struct sizeof_bits: sizeof_bits {}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +// +// Definitions for 1-bit binary and 4-bit integer types +// + +/// 1-bit binary type +using bin1_t = bool; + +/// Defines the size of an element in bits - specialized for bin1_t +template <> +struct sizeof_bits { + static int const value = 1; +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Returns the number of bytes required to hold a specified number of bits +CUTLASS_HOST_DEVICE +constexpr int +bits_to_bytes(int bits) { + return (bits + 7) / 8; +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_types.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_types.h new file mode 100644 index 000000000..f7cd654f0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_types.h @@ -0,0 +1,82 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Top-level include for all CUTLASS numeric types. +*/ +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/numeric_size.h" + +///////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct index_sequence; + +template +struct index_sequence_helper : index_sequence_helper {}; + +template +struct index_sequence_helper<0, 0, Next...> { + using type = index_sequence<0, Next...>; +}; + +template +using make_index_sequence = typename index_sequence_helper::type; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// + +#include "cutlass/integer_subbyte.h" + +#include "cutlass/half.h" +#include "cutlass/bfloat16.h" +#include "cutlass/tfloat32.h" +#include "cutlass/float8.h" +///////////////////////////////////////////////////////////////////////////////////////////////// + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/pitch_linear_coord.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/pitch_linear_coord.h new file mode 100644 index 000000000..2cd7bfee3 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/pitch_linear_coord.h @@ -0,0 +1,181 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Defines layout functions used by TensorRef and derived classes for pitch-linear memory. +*/ +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/coord.h" + +namespace cutlass { + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Template defining a shape used by pitch-linear operators +template < + int Contiguous, + int Strided +> +struct PitchLinearShape { + static int const kContiguous = Contiguous; + static int const kStrided = Strided; + static int const kCount = Contiguous * Strided; +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +/// Coordinate in pitch-linear space +struct PitchLinearCoord : public Coord<2, int> { +public: + + /// Integer-valued index + using Index = int; + + /// Base type is a Coord of rank=2 + using Base = Coord<2, Index>; + + /// Long integer type + using LongIndex = typename Base::LongIndex; + +private: + + /// Rows dimension + static int const kContiguous = 0; + + /// Columns dimension + static int const kStrided = 1; + +public: + + // + // Methods + // + + /// Default ctor + CUTLASS_HOST_DEVICE + PitchLinearCoord() { } + + /// Constructs from Coord<2> + CUTLASS_HOST_DEVICE + PitchLinearCoord(Coord<2, Index> const &coord): Base(coord) { } + + /// Helper to construct from a row and column + CUTLASS_HOST_DEVICE + PitchLinearCoord(Index contiguous_, Index strided_): Base(make_Coord(contiguous_, strided_)) { } + + /// Helper to construct from a row and column based on LongIndex + CUTLASS_HOST_DEVICE + PitchLinearCoord(LongIndex contiguous_, LongIndex strided_) + : Base(make_Coord(Index(contiguous_), Index(strided_))) { } + + /// Returns the contiguous dimension + CUTLASS_HOST_DEVICE + Index const & contiguous() const { return this->at(kContiguous); } + + /// Returns the contiguous dimension + CUTLASS_HOST_DEVICE + Index & contiguous() { return this->at(kContiguous); } + + /// Returns the column of the coordinate + CUTLASS_HOST_DEVICE + Index const & strided() const { return this->at(kStrided); } + + /// Returns the column of the coordinate + CUTLASS_HOST_DEVICE + Index & strided() { return this->at(kStrided); } + + // + // Coord operators + // + + /// Element-wise addition + CUTLASS_HOST_DEVICE + PitchLinearCoord operator+(Base const& b) const { + return PitchLinearCoord(Base::operator+(b)); + } + + /// Element-wise subtraction + CUTLASS_HOST_DEVICE + PitchLinearCoord operator-(Base const& b) const { + return PitchLinearCoord(Base::operator-(b)); + } + + CUTLASS_HOST_DEVICE + PitchLinearCoord operator-() const { + return PitchLinearCoord(-at(0), -at(1)); + } + + /// Element-wise multiplication + CUTLASS_HOST_DEVICE + PitchLinearCoord operator*(Base const& b) const { + return PitchLinearCoord(Base::operator*(b)); + } + + /// Element-wise division + CUTLASS_HOST_DEVICE + PitchLinearCoord operator/(Base const& b) const { + return PitchLinearCoord(Base::operator/(b)); + } + + /// In-place addition + CUTLASS_HOST_DEVICE + PitchLinearCoord& operator+=(Base const& b) { + Base::operator+=(b); + return *this; + } + + /// In-place subtraction + CUTLASS_HOST_DEVICE + PitchLinearCoord& operator-=(Base const& b) { + Base::operator-=(b); + return *this; + } + + /// In-place multiplication + CUTLASS_HOST_DEVICE + PitchLinearCoord& operator*=(Base const& b) { + Base::operator*=(b); + return *this; + } + + /// In-place division + CUTLASS_HOST_DEVICE + PitchLinearCoord& operator/=(Base const& b) { + Base::operator/=(b); + return *this; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/platform/platform.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/platform/platform.h new file mode 100644 index 000000000..4e8ee96f8 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/platform/platform.h @@ -0,0 +1,972 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ + +#pragma once + +/** + * \file + * \brief C++ features that may be otherwise unimplemented for CUDA device functions. + * + * This file has three components: + * + * (1) Macros: + * - Empty macro defines for C++ keywords not supported by the current + * version of C++. These simply allow compilation to proceed (but do + * not provide the added semantics). + * - \p noexcept + * - \p constexpr + * - \p nullptr + * - \p static_assert + * + * - Macro functions that we need in constant expressions because the + * C++ equivalents require constexpr compiler support. These are + * prefixed with \p __NV_STD_* + * - \p __NV_STD_MAX + * - \p __NV_STD_MIN + * + * (2) Re-implementations of STL functions and types: + * - C++ features that need the \p __device__ annotation. These are + * placed into the \p platform namespace. + * - \p abs + * - \p plus + * - \p less + * - \p greater + * - \p min + * - \p max + * - \p methods on std::pair (==, !=, <, <=, >, >=, and make_pair()) + * + * (3) Stop-gap implementations of unsupported STL functions and types: + * - STL functions and types defined by C++ 11/14/17/etc. that are not + * provided by the current version of C++. These are placed into the + * \p platform namespace + * - \p integral_constant + * - \p nullptr_t + * - \p true_type + * - \p false_type + * - \p bool_constant + * - \p enable_if + * - \p conditional + * - \p is_same + * - \p is_base_of + * - \p remove_const + * - \p remove_volatile + * - \p remove_cv + * - \p is_volatile + * - \p is_pointer + * - \p is_void + * - \p is_integral + * - \p is_floating_point + * - \p is_arithmetic + * - \p is_fundamental + * - \p is_trivially_copyable + * - \p alignment_of + * - \p aligned_storage + * + * The idea is that, as we drop support for older compilers, we can simply #define + * the \p __NV_STD_XYZ macros and \p platform namespace to alias their C++ + * counterparts (or trivially find-and-replace their occurrences in code text). + */ + +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ + +//----------------------------------------------------------------------------- +// Dependencies +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) +#include +#include +#include +#include +#include +#else +#include +#endif + +#if !defined(__CUDACC_RTC__) +//----------------------------------------------------------------------------- +// Include STL files that platform provides functionality for +//----------------------------------------------------------------------------- + +#include // Minimum/maximum operations +#include // nullptr_t +#include // Arithmetic operations +#include // For methods on std::pair +#if (!defined(_MSC_VER) && (__cplusplus >= 201103L)) || (defined(_MSC_VER) && (_MS_VER >= 1500)) +#include // For integral constants, conditional metaprogramming, and type traits +#endif + +#include "cutlass/cutlass.h" + +#endif + +//----------------------------------------------------------------------------- +// OS +//----------------------------------------------------------------------------- +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#define CUTLASS_OS_WINDOWS +#endif + +/****************************************************************************** + * Macros + ******************************************************************************/ +/// std +#if !defined(CUTLASS_STL_NAMESPACE) +#if defined(__CUDACC_RTC__) +#define CUTLASS_STL_NAMESPACE cuda::std +#else +#define CUTLASS_STL_NAMESPACE std +#endif +#endif + +/// builtin_unreachable +#if !defined(CUTLASS_GCC_UNREACHABLE) +# if defined(__clang__) || defined(__GNUC__) +# define CUTLASS_GCC_UNREACHABLE __builtin_unreachable() +# else +# define CUTLASS_GCC_UNREACHABLE +# endif +#endif + +//----------------------------------------------------------------------------- +// Keywords +//----------------------------------------------------------------------------- + +/// noexcept, constexpr +#if (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1900)) +#ifndef noexcept +#define noexcept +#endif +#ifndef constexpr +#define constexpr +#endif +#endif + +/// nullptr +#if (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1310)) +#ifndef nullptr +#define nullptr 0 +#endif +#endif + +/// static_assert +#if (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1600)) +#ifndef static_assert +#define __platform_cat_(a, b) a##b +#define __platform_cat(a, b) __platform_cat_(a, b) +#define static_assert(__e, __m) typedef int __platform_cat(AsSeRt, __LINE__)[(__e) ? 1 : -1] +#endif +#endif + +//----------------------------------------------------------------------------- +// Functions +//----------------------------------------------------------------------------- + +/// Select maximum(a, b) +#ifndef __NV_STD_MAX +#define __NV_STD_MAX(a, b) (((b) > (a)) ? (b) : (a)) +#endif + +/// Select minimum(a, b) +#ifndef __NV_STD_MIN +#define __NV_STD_MIN(a, b) (((b) < (a)) ? (b) : (a)) +#endif + +/****************************************************************************** + * Re-implementations + ******************************************************************************/ +namespace cutlass { +namespace platform { + +//----------------------------------------------------------------------------- +// Abs operations +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) +/// std::abs +CUTLASS_HOST_DEVICE constexpr int abs(int a) { + return (a < 0) ? -a : a; +} +CUTLASS_HOST_DEVICE constexpr long long abs(long long a) { + return (a < 0) ? -a : a; +} +#else +using std::abs; +#endif + +//----------------------------------------------------------------------------- +// Minimum/maximum operations +//----------------------------------------------------------------------------- + +/// std::min +template +CUTLASS_HOST_DEVICE constexpr const T& min(const T& a, const T& b) { + return (b < a) ? b : a; +} + +/// std::max +template +CUTLASS_HOST_DEVICE constexpr const T& max(const T& a, const T& b) { + return (a < b) ? b : a; +} + +#if !defined(__CUDACC_RTC__) +//----------------------------------------------------------------------------- +// Methods on std::pair +//----------------------------------------------------------------------------- + +using std::pair; + +template +CUTLASS_HOST_DEVICE constexpr bool operator==(const pair& lhs, const pair& rhs) { + return (lhs.first == rhs.first) && (lhs.second == rhs.second); +} + +template +CUTLASS_HOST_DEVICE constexpr bool operator!=(const pair& lhs, const pair& rhs) { + return (lhs.first != rhs.first) && (lhs.second != rhs.second); +} + +template +CUTLASS_HOST_DEVICE constexpr bool operator<(const pair& lhs, const pair& rhs) { + return (lhs.first < rhs.first) ? true : (rhs.first < lhs.first) ? false + : (lhs.second < rhs.second); +} + +template +CUTLASS_HOST_DEVICE constexpr bool operator<=(const pair& lhs, const pair& rhs) { + return !(rhs < lhs); +} + +template +CUTLASS_HOST_DEVICE constexpr bool operator>(const pair& lhs, const pair& rhs) { + return (rhs < lhs); +} + +template +CUTLASS_HOST_DEVICE constexpr bool operator>=(const pair& lhs, const pair& rhs) { + return !(lhs < rhs); +} + +template +CUTLASS_HOST_DEVICE std::pair make_pair(T1 t, T2 u) { + std::pair retval; + retval.first = t; + retval.second = u; + return retval; +} +#endif + +} // namespace platform + +/****************************************************************************** + * Implementations of C++ 11/14/17/... STL features + ******************************************************************************/ + +namespace platform { + +//----------------------------------------------------------------------------- +// Integral constant helper types +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1500)) + +/// std::integral_constant +template +struct integral_constant; + +/// std::integral_constant +template +struct integral_constant { + static const value_t value = V; + + typedef value_t value_type; + typedef integral_constant type; + + CUTLASS_HOST_DEVICE operator value_type() const { return value; } + + CUTLASS_HOST_DEVICE const value_type operator()() const { return value; } +}; + +#else + +using std::integral_constant; +using std::pair; + +#endif + +/// The type used as a compile-time boolean with true value. +typedef integral_constant true_type; + +/// The type used as a compile-time boolean with false value. +typedef integral_constant false_type; + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus <= 201402L)) || (defined(_MSC_VER) && (_MSC_VER < 1900)) + +/// std::bool_constant +template +struct bool_constant : platform::integral_constant {}; + +#else + +using std::bool_constant; + +#endif + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1700)) + +/// std::nullptr_t +struct nullptr_t {}; + +#else + +using std::nullptr_t; + +#endif + +//----------------------------------------------------------------------------- +// Conditional metaprogramming +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201700L)) || (defined(_MSC_VER) && (_MSC_VER < 1600)) + +/// std::enable_if (true specialization) +template +struct enable_if { + typedef T type; +}; + +/// std::enable_if (false specialization) +template +struct enable_if {}; + +/// std::conditional (true specialization) +template +struct conditional { + typedef T type; +}; + +/// std::conditional (false specialization) +template +struct conditional { + typedef F type; +}; + +#else + +using std::enable_if; +using std::conditional; + +#endif + +#if (201703L <=__cplusplus) +/// std::conditional_t +using CUTLASS_STL_NAMESPACE::conditional_t; +#endif + +//----------------------------------------------------------------------------- +// Const/volatility specifiers +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201703L)) || (defined(_MSC_VER) && (_MSC_VER < 1500)) + +/// std::remove_const (non-const specialization) +template +struct remove_const { + typedef T type; +}; + +/// std::remove_const (const specialization) +template +struct remove_const { + typedef T type; +}; + +/// std::remove_volatile (non-volatile specialization) +template +struct remove_volatile { + typedef T type; +}; + +/// std::remove_volatile (volatile specialization) +template +struct remove_volatile { + typedef T type; +}; + +/// std::remove_cv +template +struct remove_cv { + typedef typename remove_volatile::type>::type type; +}; + +#else + +using std::remove_const; +using std::remove_volatile; +using std::remove_cv; + +#endif + +#if (201703L <=__cplusplus) + +/// std::remove_cv_t +using CUTLASS_STL_NAMESPACE::remove_cv_t; +/// std::remove_reference_t +using CUTLASS_STL_NAMESPACE::remove_reference_t; + +// C++20 +// using std::remove_cvref; +template +struct remove_cvref { + using type = remove_cv_t>; +}; + +// C++20 +// using std::remove_cvref_t; +template +using remove_cvref_t = typename remove_cvref::type; + +#endif + + +//----------------------------------------------------------------------------- +// Type relationships +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1500)) + +/// std::is_same (false specialization) +template +struct is_same : false_type {}; + +/// std::is_same (true specialization) +template +struct is_same : true_type {}; + +/// Helper for std::is_base_of +template +struct is_base_of_helper { + typedef char (&yes)[1]; + typedef char (&no)[2]; + + template + struct dummy { + CUTLASS_HOST_DEVICE operator B*() const; + CUTLASS_HOST_DEVICE operator D*(); + }; + + template + CUTLASS_HOST_DEVICE static yes check(DerivedT*, T); + + CUTLASS_HOST_DEVICE static no check(BaseT*, int); + + static const bool value = sizeof(check(dummy(), int())) == sizeof(yes); +}; + +/// std::is_base_of +template +struct is_base_of + : integral_constant::type, + typename remove_cv::type>::value) || + (is_same::type, + typename remove_cv::type>::value)> {}; + +#else + +using std::is_same; +using std::is_base_of; + +#endif + +//----------------------------------------------------------------------------- +// Type properties +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1500)) + +/// std::is_volatile +template +struct is_volatile : false_type {}; +template +struct is_volatile : true_type {}; + +/// Helper for std::is_pointer (false specialization) +template +struct is_pointer_helper : false_type {}; + +/// Helper for std::is_pointer (true specialization) +template +struct is_pointer_helper : true_type {}; + +/// std::is_pointer +template +struct is_pointer : is_pointer_helper::type> {}; + +/// std::is_void +template +struct is_void : is_same::type> {}; + +/// std::is_integral +template +struct is_integral : false_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template <> +struct is_integral : true_type {}; +template +struct is_integral : is_integral {}; +template +struct is_integral : is_integral {}; +template +struct is_integral : is_integral {}; + +/// std::is_floating_point +template +struct is_floating_point + : integral_constant::type>::value || + is_same::type>::value)> {}; + +/// std::is_arithmetic +template +struct is_arithmetic + : integral_constant::value || is_floating_point::value)> {}; + +/// std::is_fundamental +template +struct is_fundamental + : integral_constant::value || is_void::value || + is_same::type>::value)> {}; + +#else + +using std::is_volatile; +using std::is_pointer; +using std::is_void; +using std::is_integral; +using std::is_floating_point; +using std::is_arithmetic; +using std::is_fundamental; + +#endif + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1800)) || \ + (defined(__GNUG__) && (__GNUC__ < 5)) + +/** + * std::is_trivially_copyable + * + * This implementation only evaluates true if T is fundamental or pointer + * + * Without help from partial template specializations provided by the user for + * a specific class or struct, this trait will never report that the specified + * class or struct is trivially-copyable ; this is always safe, + * if possibly sub-optimal. + */ +template +struct is_trivially_copyable + : integral_constant::value || is_pointer::value)> {}; + +#else + +using std::is_trivially_copyable; + +#endif + +#if (201703L <=__cplusplus) + +/// std::is_unsigned_v +using CUTLASS_STL_NAMESPACE::is_integral_v; +/// std::is_unsigned_v +using CUTLASS_STL_NAMESPACE::is_unsigned_v; + +#endif + +//----------------------------------------------------------------------------- +// bit_cast +//----------------------------------------------------------------------------- + +template< class To, class From > +constexpr To CUTLASS_HOST_DEVICE bit_cast(const From& from ) noexcept; + +template +constexpr To CUTLASS_HOST_DEVICE bit_cast(const From& src) noexcept +{ + static_assert(sizeof(To) == sizeof(From), "sizes must match"); + return reinterpret_cast(src); +} + +//----------------------------------------------------------------------------- +// Alignment and layout utilities +//----------------------------------------------------------------------------- + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1500)) + +/// std::alignment_of +template +struct alignment_of { + struct pad { + value_t val; + char byte; + }; + + enum { value = sizeof(pad) - sizeof(value_t) }; +}; + +#else + +template +struct alignment_of : std::alignment_of {}; + +#endif + +/* 16B specializations where 32-bit Win32 host compiler disagrees with device compiler */ +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; +template <> +struct alignment_of { + enum { value = 16 }; +}; + +// Specializations for volatile/const qualified types +template +struct alignment_of : alignment_of {}; +template +struct alignment_of : alignment_of {}; +template +struct alignment_of : alignment_of {}; + +#if defined(__CUDACC_RTC__) || (!defined(_MSC_VER) && (__cplusplus < 201103L)) || (defined(_MSC_VER) && (_MSC_VER < 1800)) + +template +struct aligned_chunk; +template <> +struct __align__(1) aligned_chunk<1> { + uint8_t buff; +}; +template <> +struct __align__(2) aligned_chunk<2> { + uint16_t buff; +}; +template <> +struct __align__(4) aligned_chunk<4> { + uint32_t buff; +}; +template <> +struct __align__(8) aligned_chunk<8> { + uint32_t buff[2]; +}; +template <> +struct __align__(16) aligned_chunk<16> { + uint32_t buff[4]; +}; +template <> +struct __align__(32) aligned_chunk<32> { + uint32_t buff[8]; +}; +template <> +struct __align__(64) aligned_chunk<64> { + uint32_t buff[16]; +}; +template <> +struct __align__(128) aligned_chunk<128> { + uint32_t buff[32]; +}; +template <> +struct __align__(256) aligned_chunk<256> { + uint32_t buff[64]; +}; +template <> +struct __align__(512) aligned_chunk<512> { + uint32_t buff[128]; +}; +template <> +struct __align__(1024) aligned_chunk<1024> { + uint32_t buff[256]; +}; +template <> +struct __align__(2048) aligned_chunk<2048> { + uint32_t buff[512]; +}; +template <> +struct __align__(4096) aligned_chunk<4096> { + uint32_t buff[1024]; +}; + +/// std::aligned_storage +template +struct aligned_storage { + typedef aligned_chunk type[Len / sizeof(aligned_chunk)]; +}; + +#else + +using std::aligned_storage; + +#endif + +#if !defined(__CUDACC_RTC__) +/// Default deleter +template +struct default_delete { + void operator()(T* ptr) const { delete ptr; } +}; + +/// Partial specialization for deleting array types +template +struct default_delete { + void operator()(T* ptr) const { delete[] ptr; } +}; + +/// std::unique_ptr +template > +class unique_ptr { + public: + typedef T* pointer; + typedef T element_type; + typedef Deleter deleter_type; + + private: + /// Pointer to memory + pointer _ptr; + + /// Deleter + deleter_type _deleter; + + public: + unique_ptr() : _ptr(nullptr) {} + unique_ptr(pointer p) : _ptr(p) {} + + ~unique_ptr() { + if (_ptr) { + _deleter(_ptr); + } + } + /// Returns a pointer to the managed object or nullptr if no object is owned. + pointer get() const noexcept { return _ptr; } + + /// Releases ownership of the managed object, if any + pointer release() noexcept { + pointer p(_ptr); + _ptr = nullptr; + return p; + } + + /// Replaces the managed object, deleting the old object. + void reset(pointer p = pointer()) noexcept { + pointer old_ptr = _ptr; + _ptr = p; + if (old_ptr != nullptr) { + get_deleter()(old_ptr); + } + } + + /// Swaps the managed objects with *this and another unique_ptr + void swap(unique_ptr& other) noexcept { std::swap(_ptr, other._ptr); } + + /// Returns the deleter object + Deleter& get_deleter() noexcept { return _deleter; } + + /// Returns the deleter object + Deleter const& get_deleter() const noexcept { return _deleter; } + + /// Checks whether an object is owned + operator bool() const noexcept { return _ptr != nullptr; } + + /// Dereferences the unique_ptr + T& operator*() const { return *_ptr; } + + /// Returns a pointer to the managed object + pointer operator->() const noexcept { return _ptr; } + + /// Array access to managed object + T& operator[](size_t i) const { return _ptr[i]; } +}; + +/// Specializes the swap algorithm +template +void swap(unique_ptr& lhs, unique_ptr& rhs) noexcept { + lhs.swap(rhs); +} +#endif + +/// std::numeric_limits +template +struct numeric_limits; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr int32_t lowest() noexcept { return -2147483647 - 1;} + CUTLASS_HOST_DEVICE + static constexpr int32_t max() noexcept { return 2147483647;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr int16_t lowest() noexcept { return -32768;} + CUTLASS_HOST_DEVICE + static constexpr int16_t max() noexcept { return 32767;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr int8_t lowest() noexcept { return -128;} + CUTLASS_HOST_DEVICE + static constexpr int8_t max() noexcept { return 127;} + static constexpr bool is_integer = true; +}; + + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr uint32_t lowest() noexcept { return 0;} + CUTLASS_HOST_DEVICE + static constexpr uint32_t max() noexcept { return 4294967295U;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr uint16_t lowest() noexcept { return 0;} + CUTLASS_HOST_DEVICE + static constexpr uint16_t max() noexcept { return 65535U;} + static constexpr bool is_integer = true; +}; + +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr uint8_t lowest() noexcept { return 0;} + CUTLASS_HOST_DEVICE + static constexpr uint8_t max() noexcept { return 255U;} + static constexpr bool is_integer = true; +}; + +#if !defined(__CUDACC_RTC__) +template <> +struct numeric_limits { + CUTLASS_HOST_DEVICE + static constexpr float infinity() noexcept { return bit_cast(0x7f800000);} + static constexpr bool is_integer = false; + static constexpr bool has_infinity = true; +}; +#endif + +/// std::float_round_style +using CUTLASS_STL_NAMESPACE::float_round_style; +using CUTLASS_STL_NAMESPACE::round_indeterminate; +using CUTLASS_STL_NAMESPACE::round_toward_zero; +using CUTLASS_STL_NAMESPACE::round_to_nearest; +using CUTLASS_STL_NAMESPACE::round_toward_infinity; +using CUTLASS_STL_NAMESPACE::round_toward_neg_infinity; + +/// std::float_denorm_style +using CUTLASS_STL_NAMESPACE::float_denorm_style; +using CUTLASS_STL_NAMESPACE::denorm_indeterminate; +using CUTLASS_STL_NAMESPACE::denorm_absent; +using CUTLASS_STL_NAMESPACE::denorm_present; + +} // namespace platform +} // namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/real.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/real.h new file mode 100644 index 000000000..ed9018a03 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/real.h @@ -0,0 +1,61 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/** + \file + \brief This class provides helpers to support real<> and complex<> types in generic code. +*/ + +#pragma once + +namespace cutlass { + +/// Used to determine the real-valued underlying type of a numeric type T. +template +struct RealType { + using Type = T; + + /// Number of elements + static int const kExtent = 1; + +CUTLASS_HOST_DEVICE + static T from_real(double x) { + return static_cast(x); + } +}; + +template +CUTLASS_HOST_DEVICE +static T from_real(double r) { + return T(r); +} + + +} // namespace cutlass diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/tfloat32.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/tfloat32.h new file mode 100644 index 000000000..660f2c49c --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/tfloat32.h @@ -0,0 +1,477 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines a proxy class for storing Tensor Float 32 data type. +*/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include "cutlass/floating_point_nvrtc.h" +#else +#include +#include +#include +#endif + +#include "cutlass/cutlass.h" + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Tensor Float 32 data type +struct alignas(4) tfloat32_t { + + // + // Data members + // + + /// Storage type + uint32_t storage; + + // + // Methods + // + + /// Constructs from an unsigned int + CUTLASS_HOST_DEVICE + static tfloat32_t bitcast(uint32_t x) { + tfloat32_t h; + h.storage = x; + return h; + } + + /// Emulated rounding is fast in device code + CUTLASS_HOST_DEVICE + static tfloat32_t round_half_ulp_truncate(float const &s) { + uint32_t x = reinterpret_cast(s); + + #if defined(__HIP_DEVICE_COMPILE__) + if (::isfinite(s)) { + x += 0x1000u; + } + #else + if (std::isfinite(s)) { + x += 0x1000u; + } + #endif + + return tfloat32_t::bitcast(x); + } + + /// Default constructor + tfloat32_t() = default; + + /// Floating-point conversion - round toward nearest even + CUTLASS_HOST_DEVICE +// explicit tfloat32_t(float x): storage(round_half_ulp_truncate(x).storage) { } + tfloat32_t(float x): storage(round_half_ulp_truncate(x).storage) { } + + /// Floating-point conversion - round toward nearest even + CUTLASS_HOST_DEVICE +// explicit tfloat32_t(double x): tfloat32_t(float(x)) { + tfloat32_t(double x): tfloat32_t(float(x)) { + } + + /// Integer conversion - round toward zero + CUTLASS_HOST_DEVICE +// explicit tfloat32_t(int x) { + tfloat32_t(int x) { + float flt = static_cast(x); + #if defined(__HIP_DEVICE_COMPILE__) + storage = reinterpret_cast(flt); + #else + std::memcpy(&storage, &flt, sizeof(storage)); + #endif + } + + /// Converts to float + CUTLASS_HOST_DEVICE + operator float() const { + + // Conversions to IEEE single-precision requires clearing dont-care bits + // of the mantissa. + unsigned bits = (storage & ~0x1fffu); + + #if defined(__HIP_DEVICE_COMPILE__) + return reinterpret_cast(bits); + #else + float flt; + std::memcpy(&flt, &bits, sizeof(flt)); + return flt; + #endif + } + + /// Converts to float + CUTLASS_HOST_DEVICE + explicit operator double() const { + return double(float(*this)); + } + + /// Converts to int + CUTLASS_HOST_DEVICE + explicit operator int() const { + return int(float(*this)); + } + + /// Casts to bool + CUTLASS_HOST_DEVICE + explicit operator bool() const { + return (float(*this) != 0.0f); + } + + /// Obtains raw bits + CUTLASS_HOST_DEVICE + uint32_t raw() const { + return storage; + } + + /// Returns the sign bit + CUTLASS_HOST_DEVICE + bool signbit() const { + return ((raw() & 0x80000000) != 0); + } + + /// Returns the biased exponent + CUTLASS_HOST_DEVICE + int exponent_biased() const { + return int((raw() >> 23) & 0x0ff); + } + + /// Returns the unbiased exponent + CUTLASS_HOST_DEVICE + int exponent() const { + return exponent_biased() - 127; + } + + /// Returns the mantissa + CUTLASS_HOST_DEVICE + int mantissa() const { + return int(raw() & 0x7fffff); + } +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool signbit(cutlass::tfloat32_t const& h) { + return h.signbit(); +} + +CUTLASS_HOST_DEVICE +cutlass::tfloat32_t abs(cutlass::tfloat32_t const& h) { + return cutlass::tfloat32_t::bitcast(h.raw() & 0x7fffffff); +} + +CUTLASS_HOST_DEVICE +bool isnan(cutlass::tfloat32_t const& h) { + return (h.exponent_biased() == 0x0ff) && h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isfinite(cutlass::tfloat32_t const& h) { + return (h.exponent_biased() != 0x0ff); +} + +CUTLASS_HOST_DEVICE +cutlass::tfloat32_t nan_tf32(const char*) { + // NVIDIA canonical NaN + return cutlass::tfloat32_t::bitcast(0x7fffffff); +} + +CUTLASS_HOST_DEVICE +bool isinf(cutlass::tfloat32_t const& h) { + return (h.exponent_biased() == 0x0ff) && !h.mantissa(); +} + +CUTLASS_HOST_DEVICE +bool isnormal(cutlass::tfloat32_t const& h) { + return h.exponent_biased() && h.exponent_biased() != 0x0ff; +} + +CUTLASS_HOST_DEVICE +int fpclassify(cutlass::tfloat32_t const& h) { + int exp = h.exponent_biased(); + int mantissa = h.mantissa(); + if (exp == 0x0ff) { + if (mantissa) { + return FP_NAN; + } + else { + return FP_INFINITE; + } + } + else if (!exp) { + if (mantissa) { + return FP_SUBNORMAL; + } + else { + return FP_ZERO; + } + } + return FP_NORMAL; +} + +CUTLASS_HOST_DEVICE +cutlass::tfloat32_t sqrt(cutlass::tfloat32_t const& h) { +#if defined(__CUDACC_RTC__) + return cutlass::tfloat32_t(sqrtf(float(h))); +#else + return cutlass::tfloat32_t(std::sqrt(float(h))); +#endif +} + +CUTLASS_HOST_DEVICE +tfloat32_t copysign(tfloat32_t const& a, tfloat32_t const& b) { + + uint32_t a_mag = (reinterpret_cast(a) & 0x7fffffff); + uint32_t b_sign = (reinterpret_cast(b) & 0x80000000); + uint32_t result = (a_mag | b_sign); + + return reinterpret_cast(result); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Standard Library operations and definitions +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace std { + +#if !defined(__CUDACC_RTC__) +/// Numeric limits +template <> +struct numeric_limits { + static bool const is_specialized = true; + static bool const is_signed = true; + static bool const is_integer = false; + static bool const is_exact = false; + static bool const has_infinity = true; + static bool const has_quiet_NaN = true; + static bool const has_signaling_NaN = false; + static std::float_denorm_style const has_denorm = std::denorm_present; + static bool const has_denorm_loss = true; + static std::float_round_style const round_style = std::round_to_nearest; + static bool const is_iec559 = false; + static bool const is_bounded = true; + static bool const is_modulo = false; + static int const digits = 19; + + /// Least positive value + static cutlass::tfloat32_t min() { return cutlass::tfloat32_t::bitcast(0x01); } + + /// Minimum finite value + static cutlass::tfloat32_t lowest() { return cutlass::tfloat32_t::bitcast(0xff7fffff); } + + /// Maximum finite value + static cutlass::tfloat32_t max() { return cutlass::tfloat32_t::bitcast(0x7f7fffff); } + + /// Returns smallest finite value + static cutlass::tfloat32_t epsilon() { return cutlass::tfloat32_t::bitcast(0x1000); } + + /// Returns smallest finite value + static cutlass::tfloat32_t round_error() { return cutlass::tfloat32_t(0.5f); } + + /// Returns smallest finite value + static cutlass::tfloat32_t infinity() { return cutlass::tfloat32_t::bitcast(0x7f800000); } + + /// Returns smallest finite value + static cutlass::tfloat32_t quiet_NaN() { return cutlass::tfloat32_t::bitcast(0x7fffffff); } + + /// Returns smallest finite value + static cutlass::tfloat32_t signaling_NaN() { return cutlass::tfloat32_t::bitcast(0x7fffffff); } + + /// Returns smallest finite value + static cutlass::tfloat32_t denorm_min() { return cutlass::tfloat32_t::bitcast(0x1); } +}; +#endif + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace std + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Arithmetic operators +// +/////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace cutlass { + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +CUTLASS_HOST_DEVICE +bool operator==(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) == float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator!=(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) != float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) < float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator<=(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) <= float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) > float(rhs); +} + +CUTLASS_HOST_DEVICE +bool operator>=(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return float(lhs) >= float(rhs); +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator+(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return tfloat32_t(float(lhs) + float(rhs)); +} + + +CUTLASS_HOST_DEVICE +tfloat32_t operator-(tfloat32_t const& lhs) { + union u_tff32 { + float val_f32; + tfloat32_t val_tf; + CUTLASS_HOST_DEVICE u_tff32() : val_f32(0) { } + }; + union u_tff32 x; x.val_f32 = -reinterpret_cast(lhs); + return x.val_tf; +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator-(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return tfloat32_t(float(lhs) - float(rhs)); +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator*(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return tfloat32_t(float(lhs) * float(rhs)); +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator/(tfloat32_t const& lhs, tfloat32_t const& rhs) { + return tfloat32_t(float(lhs) / float(rhs)); +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator+=(tfloat32_t & lhs, tfloat32_t const& rhs) { + lhs = tfloat32_t(float(lhs) + float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator-=(tfloat32_t & lhs, tfloat32_t const& rhs) { + lhs = tfloat32_t(float(lhs) - float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator*=(tfloat32_t & lhs, tfloat32_t const& rhs) { + lhs = tfloat32_t(float(lhs) * float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator/=(tfloat32_t & lhs, tfloat32_t const& rhs) { + lhs = tfloat32_t(float(lhs) / float(rhs)); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator++(tfloat32_t & lhs) { + float tmp(lhs); + ++tmp; + lhs = tfloat32_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t& operator--(tfloat32_t & lhs) { + float tmp(lhs); + --tmp; + lhs = tfloat32_t(tmp); + return lhs; +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator++(tfloat32_t & lhs, int) { + tfloat32_t ret(lhs); + float tmp(lhs); + tmp++; + lhs = tfloat32_t(tmp); + return ret; +} + +CUTLASS_HOST_DEVICE +tfloat32_t operator--(tfloat32_t & lhs, int) { + tfloat32_t ret(lhs); + float tmp(lhs); + tmp--; + lhs = tfloat32_t(tmp); + return ret; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +// +// User-defined literals +// + +CUTLASS_HOST_DEVICE +cutlass::tfloat32_t operator "" _tf32(long double x) { + return cutlass::tfloat32_t(float(x)); +} + +CUTLASS_HOST_DEVICE +cutlass::tfloat32_t operator "" _tf32(unsigned long long int x) { + return cutlass::tfloat32_t(int(x)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/transform/thread/unary_op_hip.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/transform/thread/unary_op_hip.h new file mode 100644 index 000000000..d4fa453ce --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/transform/thread/unary_op_hip.h @@ -0,0 +1,111 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +#pragma once + +#include "cutlass/cutlass.h" +#include "cutlass/complex_hip.h" + +namespace cutlass { +namespace transform { +namespace thread { + +namespace UnaryTransform { + struct Identity; ///< None (i.e., identity) + struct Conjugate; ///< Complex conjugate +} + +/// Element-wise unary operator that transforms one element of a fragment at a time +template< + typename FragmentIn, ///< Input Fragment + typename FragmentOut,///< Output Fragment + typename Transform> ///< Unary transform operator +class UnaryOp +{ + public: + CUTLASS_DEVICE + static FragmentOut execute(FragmentIn &in) + { + static_assert(FragmentIn::kElements == FragmentOut::kElements, "Number of elements must match."); + static_assert(platform::is_same::value || + platform::is_same::value, + "Unary Operator not supported."); + + FragmentOut out; + if (platform::is_same::value ) + { + CUTLASS_PRAGMA_UNROLL + for (int i=0; i < FragmentIn::kElements; ++i){ + out[i] = static_cast(in[i]); + } + } + else if (platform::is_same::value ) + { + for (int i=0; i < FragmentIn::kElements; ++i){ + out[i] = conj(static_cast(in[i])); + } + } + return out; + } +}; + +template +class UnaryOp +{ + public: + CUTLASS_DEVICE + static FragmentIn execute(FragmentIn &in) + { + static_assert(platform::is_same::value || + platform::is_same::value, + "Unary Operator not supported."); + + if (platform::is_same::value ) + { + return in; + } + else if (platform::is_same::value ) + { + for(int i=0; i < FragmentIn::kElements; ++i){ + in[i] = conj(in[i]); + } + } + return in; + } + }; + } + } +} diff --git a/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/uint128.h b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/uint128.h new file mode 100644 index 000000000..0cf1707bf --- /dev/null +++ b/third_party/flashmla_hygon/csrc/cutlass/cutlass_3.2.1/include/cutlass/uint128.h @@ -0,0 +1,270 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! + \file + \brief Defines an unsigned 128b integer with several operators to support 64-bit integer division. +*/ +/* + Note: CUTLASS 3x increases the host compiler requirements to C++17. However, certain + existing integrations of CUTLASS require C++11 host compilers. + + Until this requirement can be lifted, certain headers with this annotation are required + to be remain consistent with C++11 syntax. + + C++11 compatibility is enforced by `cutlass_test_unit_core_cpp11`. +*/ +#pragma once + +#if defined(__CUDACC_RTC__) +#include +#else +#include +#include +#include +#include +#include +#endif + +#include "cutlass/cutlass.h" + +/// Optionally enable GCC's built-in type +#if (defined(__x86_64) || defined (__aarch64__)) && !defined(__HIP_DEVICE_COMPILE__) && defined(__GNUC__) +#define CUTLASS_UINT128_NATIVE +#elif defined(_MSC_VER) && defined(_M_AMD64) && !defined(__HIP_DEVICE_COMPILE__) +#define CUTLASS_INT128_ARITHMETIC +#include +#if _MSC_VER >= 1920 +#define CUTLASS_INT128_ARITHMETIC_DIV +#include +#endif +#endif + +namespace cutlass { + +///! Unsigned 128b integer type +struct uint128_t { + + /// Size of one part of the uint's storage in bits + static constexpr int kPartSize = sizeof_bits::value; + + struct hilo { + uint64_t lo; + uint64_t hi; + + hilo() = default; + + CUTLASS_HOST_DEVICE hilo(uint64_t lo_, uint64_t hi_):lo(lo_), hi(hi_) {} + }; + + // Use a union to store either low and high parts or, if present, a built-in 128b integer type. + union { + struct hilo hilo_; + + #if defined(CUTLASS_UINT128_NATIVE) + unsigned __int128 native; + #endif // defined(CUTLASS_UINT128_NATIVE) + }; + + // + // Methods + // + + /// Default ctor + uint128_t() = default; + + /// Constructor from uint64 + CUTLASS_HOST_DEVICE + uint128_t(uint64_t lo_): hilo_(lo_, 0) { } + + /// Constructor from two 64b unsigned integers + CUTLASS_HOST_DEVICE + uint128_t(uint64_t lo_, uint64_t hi_): hilo_(lo_, hi_) { + + } + + /// Optional constructor from native value + #if defined(CUTLASS_UINT128_NATIVE) + uint128_t(unsigned __int128 value): native(value) { } + #endif + + /// Lossily cast to uint64 + CUTLASS_HOST_DEVICE + explicit operator uint64_t() const { + return hilo_.lo; + } + + CUTLASS_HOST_DEVICE + static void exception() { +#if defined(__HIP_DEVICE_COMPILE__) + asm volatile (" brkpt;\n"); +#else + // throw std::runtime_error("Not yet implemented."); + abort(); +#endif + } + + /// Add + CUTLASS_HOST_DEVICE + uint128_t operator+(uint128_t const &rhs) const { + uint128_t y; +#if defined(CUTLASS_UINT128_NATIVE) + y.native = native + rhs.native; +#else + y.hilo_.lo = hilo_.lo + rhs.hilo_.lo; + y.hilo_.hi = hilo_.hi + rhs.hilo_.hi + (!y.hilo_.lo && (rhs.hilo_.lo)); +#endif + return y; + } + + /// Subtract + CUTLASS_HOST_DEVICE + uint128_t operator-(uint128_t const &rhs) const { + uint128_t y; +#if defined(CUTLASS_UINT128_NATIVE) + y.native = native - rhs.native; +#else + y.hilo_.lo = hilo_.lo - rhs.hilo_.lo; + y.hilo_.hi = hilo_.hi - rhs.hilo_.hi - (rhs.hilo_.lo && y.hilo_.lo > hilo_.lo); +#endif + return y; + } + + /// Multiply by unsigned 64b integer yielding 128b integer + CUTLASS_HOST_DEVICE + uint128_t operator*(uint64_t const &rhs) const { + uint128_t y{}; +#if defined(CUTLASS_UINT128_NATIVE) + y.native = native * rhs; +#elif defined(CUTLASS_INT128_ARITHMETIC) + // Multiply by the low part + y.hilo_.lo = _umul128(hilo_.lo, rhs, &y.hilo_.hi); + + // Add the high part and ignore the overflow + uint64_t overflow; + y.hilo_.hi += _umul128(hilo_.hi, rhs, &overflow); +#else + CUTLASS_UNUSED(rhs); + exception(); +#endif + return y; + } + + /// Divide 128b operation by 64b operation yielding a 64b quotient + CUTLASS_HOST_DEVICE + uint64_t operator/(uint64_t const &divisor) const { + uint64_t quotient = 0; +#if defined(CUTLASS_UINT128_NATIVE) + quotient = uint64_t(native / divisor); +#elif defined(CUTLASS_INT128_ARITHMETIC_DIV) + // implemented using MSVC's arithmetic intrinsics + uint64_t remainder = 0; + quotient = _udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + CUTLASS_UNUSED(divisor); + exception(); +#endif + return quotient; + } + + /// Divide 128b operation by 64b operation yielding a 64b quotient + CUTLASS_HOST_DEVICE + uint64_t operator%(uint64_t const &divisor) const { + uint64_t remainder = 0; +#if defined(CUTLASS_UINT128_NATIVE) + remainder = uint64_t(native % divisor); +#elif defined(CUTLASS_INT128_ARITHMETIC_DIV) + // implemented using MSVC's arithmetic intrinsics + (void)_udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + CUTLASS_UNUSED(divisor); + exception(); +#endif + return remainder; + } + + /// Computes the quotient and remainder in a single method. + CUTLASS_HOST_DEVICE + uint64_t divmod(uint64_t &remainder, uint64_t divisor) const { + uint64_t quotient = 0; +#if defined(CUTLASS_UINT128_NATIVE) + quotient = uint64_t(native / divisor); + remainder = uint64_t(native % divisor); +#elif defined(CUTLASS_INT128_ARITHMETIC_DIV) + // implemented using MSVC's arithmetic intrinsics + quotient = _udiv128(hilo_.hi, hilo_.lo, divisor, &remainder); +#else + CUTLASS_UNUSED(remainder); + CUTLASS_UNUSED(divisor); + exception(); +#endif + return quotient; + } + + /// Left-shifts a 128b unsigned integer + CUTLASS_HOST_DEVICE + uint128_t operator<<(int sh) const { + if (sh == 0) { + return *this; + } + else if (sh >= kPartSize) { + return uint128_t(0, hilo_.lo << (sh - kPartSize)); + } + else { + return uint128_t( + (hilo_.lo << sh), + (hilo_.hi << sh) | uint64_t(hilo_.lo >> (kPartSize - sh)) + ); + } + } + + /// Right-shifts a 128b unsigned integer + CUTLASS_HOST_DEVICE + uint128_t operator>>(int sh) const { + if (sh == 0) { + return *this; + } + else if (sh >= kPartSize) { + return uint128_t((hilo_.hi >> (sh - kPartSize)), 0); + } + else { + return uint128_t( + (hilo_.lo >> sh) | (hilo_.hi << (kPartSize - sh)), + (hilo_.hi >> sh) + ); + } + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////// + +} // namespace cutlass + +///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/third_party/flashmla_hygon/csrc/defines_hip.h b/third_party/flashmla_hygon/csrc/defines_hip.h new file mode 100644 index 000000000..e127e3b2b --- /dev/null +++ b/third_party/flashmla_hygon/csrc/defines_hip.h @@ -0,0 +1,32 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include +// #include + +using bf16 = cutlass::bfloat16_t; +using fp8 = unsigned char; +// using transac_bar_t = cutlass::arch::ClusterTransactionBarrier; +// using cutlass::arch::fence_view_async_shared; +// using cutlass::arch::fence_barrier_init; +// using cutlass::arch::NamedBarrier; + +// struct int32x8_t { +// int a0, a1, a2, a3, a4, a5, a6, a7; +// }; + +// struct float8 { +// // float2 a01, a23, a45, a67; +// }; + +// struct bf16x8 { +// // __hip_bfloat162 a01; +// // __hip_bfloat162 a23; +// // __hip_bfloat162 a45; +// // __hip_bfloat162 a67; +// }; diff --git a/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine.hip b/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine.hip new file mode 100644 index 000000000..96510268b --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine.hip @@ -0,0 +1,328 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +#include "combine_hip.h" + +// #include +#include +#include +#include +#include + +#include + +#include "params_hip.h" +#include "utils_hip.h" +#define CUDART_L2E_F 1.442695041F + +using namespace cute; + +namespace gfx9::decode { + +template +__global__ void __launch_bounds__(NUM_THREADS, 1) +flash_fwd_mla_combine_kernel(const CombineParams params) { + // grid_shape: [batch_size, s_q, h_q/BLOCK_SIZE_M] + // Each CTA gathers the activation of some heads from one batch, do scaling & accumulation, and save the result + static_assert(NUM_THREADS/64 == BLOCK_SIZE_M); // The number of warps == block_size_m + const int batch_idx = blockIdx.x; + const int s_q_idx = blockIdx.y; + const int h_block_idx = blockIdx.z; + const int warp_idx = threadIdx.x / 64; + const int lane_idx = threadIdx.x % 64; + + int num_valid_heads = ::min(BLOCK_SIZE_M, params.h_q - BLOCK_SIZE_M*h_block_idx); + if (warp_idx >= num_valid_heads) { + return; + } + + int start_split_idx; + int end_split_idx; + int my_num_splits; + if constexpr (USE_SPLIT_KV) + { + start_split_idx = batch_idx * params.num_splits; + end_split_idx = (batch_idx + 1) * params.num_splits; + int seqlen_k = __ldg(params.seqlens_k_ptr + batch_idx); + end_split_idx = ::min(cute::ceil_div(cute::ceil_div(seqlen_k, 64), params.partition_block_nums), params.num_splits) + start_split_idx; + // if (lane_idx == 0 && batch_idx == 61) + // { + // printf(" batch_idx = %d start_split_idx = %d end_split_idx = %d seqlen_k = %d \n",batch_idx, start_split_idx, end_split_idx, seqlen_k); + // } + my_num_splits = end_split_idx - start_split_idx; + if (my_num_splits == 1) { + return; + } + } + else + { + start_split_idx = __ldg(params.num_splits_ptr + batch_idx); + end_split_idx = __ldg(params.num_splits_ptr + batch_idx + 1); + my_num_splits = end_split_idx - start_split_idx; + if (my_num_splits == 1) { + return; + } + } + + + + + // FLASH_DEVICE_ASSERT(my_num_splits <= MAX_SPLITS); + + Tensor gLseAccum = make_tensor( + make_gmem_ptr((float*)params.lse_accum + start_split_idx*params.stride_lse_accum_split + s_q_idx*params.stride_lse_accum_s_q + h_block_idx*BLOCK_SIZE_M), + Shape, Int>{}, + make_stride(params.stride_lse_accum_split, _1{}) + ); + Tensor gLse = make_tensor( + make_gmem_ptr((float*)params.lse + batch_idx*params.stride_lse_b + s_q_idx*params.stride_lse_s_q + h_block_idx*BLOCK_SIZE_M), + Shape>{}, + Stride<_1>{} + ); + + __shared__ float smem_buf[BLOCK_SIZE_M][MAX_SPLITS]; + // __syncthreads(); + + // Warp #i gathers LseAccum for seq #i + { + constexpr int NUM_LSE_PER_THREAD = cute::ceil_div(MAX_SPLITS, 64); + float local_lse[NUM_LSE_PER_THREAD]; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < NUM_LSE_PER_THREAD; ++i) { + const int split_idx = i*64 + lane_idx; + local_lse[i] = split_idx < my_num_splits ? gLseAccum(split_idx, warp_idx) : -INFINITY; + } + + float max_lse = -INFINITY; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < NUM_LSE_PER_THREAD; ++i) + max_lse = max(max_lse, local_lse[i]); + CUTLASS_PRAGMA_UNROLL + for (int offset = 32; offset >= 1; offset /= 2) + max_lse = max(max_lse, __shfl_xor(max_lse, offset)); + max_lse = max_lse == -INFINITY ? 0.0f : max_lse; // In case all local LSEs are -inf + + float sum_lse = 0; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < NUM_LSE_PER_THREAD; ++i) + sum_lse = sum_lse + __builtin_amdgcn_exp2f((local_lse[i] - max_lse) * CUDART_L2E_F); + CUTLASS_PRAGMA_UNROLL + for (int offset = 32; offset >= 1; offset /= 2) + sum_lse = sum_lse + __shfl_xor(sum_lse, offset); + + float global_lse = (sum_lse == 0.f || sum_lse == -INFINITY) ? INFINITY : logf(sum_lse) + max_lse; + if (lane_idx == 0) + gLse(warp_idx) = global_lse; + float o_scale = 1.0f; + if (params.attn_sink != nullptr) { + int q_head_idx = h_block_idx*BLOCK_SIZE_M + warp_idx; + float attn_sink = __ldg(params.attn_sink + q_head_idx); + if (flash::is_positive_infinity(attn_sink)) + { + o_scale = 0.0f; + } + else + { + if (!flash::is_positive_infinity(global_lse)) + { + float Attn_sink_exp2 = __builtin_amdgcn_exp2f(attn_sink * CUDART_L2E_F); + float lse_exp2 = __builtin_amdgcn_exp2f(global_lse * CUDART_L2E_F); + o_scale = lse_exp2 / (lse_exp2 + Attn_sink_exp2); + } + } + + + // if (global_lse != INFINITY) { + // // If attn_sink is +inf, global_lse will be +inf and scale factors will be exp2f(local_lse - inf) = 0 (since local_lse never becomes +inf) + // // If attn_sink is -inf, this has no effect on global_lse + // global_lse += log2f(1 + __builtin_amdgcn_exp2f(attn_sink*CUDART_L2E_F - global_lse)); + // } else { + // // We have no tokens to attend, so global lse should be attn_sink*CUDART_L2E_F (+inf if it's -inf or +inf) + // global_lse = attn_sink == -INFINITY ? +INFINITY : attn_sink*CUDART_L2E_F; + // } + } + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < NUM_LSE_PER_THREAD; ++i) { + const int split_idx = i*64 + lane_idx; + if (split_idx < my_num_splits) { + // printf("local_lse %.2f global_lse = %.2f \n", local_lse[i], global_lse); + smem_buf[warp_idx][split_idx] = __builtin_amdgcn_exp2f((local_lse[i] - global_lse) * CUDART_L2E_F) * o_scale; + } + } + } + + __syncthreads(); + static_assert(HEAD_DIM_V % (64*4) == 0); + constexpr int ELEMS_PER_THREAD = HEAD_DIM_V / (64*4); + static_assert(ELEMS_PER_THREAD == 2); + float* oaccum_ptr = params.o_accum + start_split_idx*params.stride_o_accum_split + s_q_idx*params.stride_o_accum_s_q + (h_block_idx*BLOCK_SIZE_M + warp_idx)*params.stride_o_accum_h_q; + float4 datas[ELEMS_PER_THREAD]; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ELEMS_PER_THREAD; ++i) { + datas[i] = *(float4*)(oaccum_ptr + lane_idx*8 + i*4); // NOTE We don't use __ldg here since it is incompatible with PDL + } + // Warp #i accumulates activation for seq #i + { + float4 result[ELEMS_PER_THREAD]; + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ELEMS_PER_THREAD; ++i) + result[i] = {0.0f, 0.0f, 0.0f, 0.0f}; + + #pragma unroll 1 + for (int split = 0; split < my_num_splits; ++split) { + float lse_scale = smem_buf[warp_idx][split]; + // if (warp_idx == 2 && threadIdx.x == 128) + // { + // printf("threadIdx.x = %d %.3f %.3f lse_scale = %.2f \n",threadIdx.x, datas[0].x, datas[1].x, lse_scale); + // } + // if (lse_scale != 0.f) { + CUTLASS_PRAGMA_UNROLL + for (int i = 0; i < ELEMS_PER_THREAD; ++i) { + result[i].x += lse_scale * datas[i].x; + result[i].y += lse_scale * datas[i].y; + result[i].z += lse_scale * datas[i].z; + result[i].w += lse_scale * datas[i].w; + if (split != my_num_splits-1) { + datas[i] = *(float4*)(oaccum_ptr + (split+1)*params.stride_o_accum_split + lane_idx*8 + i*4); + } + } + // } + } + // if (warp_idx == 2) + // { + // printf(" %.3f \n", result[0].x); + // } + auto float2bf16 = [] (float s) -> uint16_t { + uint32_t x32 = reinterpret_cast(s); + #ifndef FLASH_MLA_BF16_TYPE + #define FLASH_MLA_BF16_TYPE 0 + #endif + #if FLASH_MLA_BF16_TYPE == 1 + x32 += 0x8000u; + #endif + return uint16_t(x32 >> 16); + }; + + const int h_q_idx = h_block_idx*BLOCK_SIZE_M + warp_idx; + ElementT* o_ptr = (ElementT*)params.out + batch_idx*params.stride_o_b + s_q_idx*params.stride_o_s_q + h_q_idx*params.stride_o_h_q + lane_idx * 8; + ElementT data_converted[8]; + using result_type = cutlass::Array; + for (int i = 0; i < ELEMS_PER_THREAD; ++i) { + if constexpr(std::is_same_v) { + #if defined(__gfx938__) + auto d0 = __builtin_hcu_cvt_pk_bf16_f32(0, result[i].x, 0, result[i].y, 0); + auto d1 = __builtin_hcu_cvt_pk_bf16_f32(0, result[i].z, 0, result[i].w, 0); + auto res0 = reinterpret_cast(d0); + auto res1 = reinterpret_cast(d1); + o_ptr[i * 4] = res0[0]; + o_ptr[i * 4 + 1] = res0[1]; + o_ptr[i * 4 + 2] = res1[0]; + o_ptr[i * 4 + 3] = res1[1]; + #else + // auto float32_to_bfloat16 = [&](float v) -> ElementT { + // union { + // float fp32; + // uint32_t int32; + // } u = {v}; + // ElementT res; + // res.storage = (u.int32 >> 16); + // return res; + // }; + + float4 data = result[i]; + // o_ptr[i * 4] = float32_to_bfloat16((data.x)); + // o_ptr[i * 4 + 1] = float32_to_bfloat16((data.y)); + // o_ptr[i * 4 + 2] = float32_to_bfloat16((data.z)); + // o_ptr[i * 4 + 3] = float32_to_bfloat16((data.w)); + o_ptr[i * 4].storage = float2bf16(data.x); + o_ptr[i * 4 + 1].storage = float2bf16(data.y); + o_ptr[i * 4 + 2].storage = float2bf16(data.z); + o_ptr[i * 4 + 3].storage = float2bf16(data.w); + #endif + } else { + auto d0 = __builtin_hcu_cvt_pkrtz(result[i].x, result[i].y); + auto d1 = __builtin_hcu_cvt_pkrtz(result[i].z, result[i].w); + auto res0 = reinterpret_cast(d0); + auto res1 = reinterpret_cast(d1); + o_ptr[i * 4] = res0[0]; + o_ptr[i * 4 + 1] = res0[1]; + o_ptr[i * 4 + 2] = res1[0]; + o_ptr[i * 4 + 3] = res1[1]; + } + + } + } +} + + +#define MLA_NUM_SPLITS_SWITCH(NUM_SPLITS, NAME, ...) \ + [&] { \ + if (NUM_SPLITS <= 32) { \ + constexpr static int NAME = 32; \ + return __VA_ARGS__(); \ + } else if (NUM_SPLITS <= 64) { \ + constexpr static int NAME = 64; \ + return __VA_ARGS__(); \ + } else if (NUM_SPLITS <= 96) { \ + constexpr static int NAME = 96; \ + return __VA_ARGS__(); \ + } else if (NUM_SPLITS <= 128) { \ + constexpr static int NAME = 128; \ + return __VA_ARGS__(); \ + } else if (NUM_SPLITS <= 160) { \ + constexpr static int NAME = 160; \ + return __VA_ARGS__(); \ + } else if (NUM_SPLITS <= 240) { \ + constexpr static int NAME = 240; \ + return __VA_ARGS__(); \ + } else { \ + FLASH_ASSERT(false); \ + } \ + }() + + +template +void run_flash_mla_combine_kernel(CombineParams ¶ms) { + static constexpr int HEAD_DIM_V = 512; // Since only this head dimension is supported by Flash MLA + FLASH_ASSERT(params.d_v == HEAD_DIM_V); + if (params.use_split_kv) + { + MLA_NUM_SPLITS_SWITCH(params.num_splits, NUM_SPLITS, [&] { + constexpr int BLOCK_SIZE_M = 4; + constexpr int NUM_THREADS = BLOCK_SIZE_M*64; + constexpr size_t smem_size = BLOCK_SIZE_M*(NUM_SPLITS+1)*sizeof(float); + auto combine_kernel = &flash_fwd_mla_combine_kernel; + hipLaunchKernelGGL(( combine_kernel), dim3(dim3(params.b, params.s_q, ku::ceil_div(params.h_q, BLOCK_SIZE_M))), + dim3(NUM_THREADS), + smem_size, + params.stream, params); + }); + } + else + { + MLA_NUM_SPLITS_SWITCH(params.num_sm_parts, NUM_SPLITS, [&] { + constexpr int BLOCK_SIZE_M = 4; + constexpr int NUM_THREADS = BLOCK_SIZE_M*64; + constexpr size_t smem_size = BLOCK_SIZE_M*(NUM_SPLITS+1)*sizeof(float); + auto combine_kernel = &flash_fwd_mla_combine_kernel; + hipLaunchKernelGGL(( combine_kernel), dim3(dim3(params.b, params.s_q, ku::ceil_div(params.h_q, BLOCK_SIZE_M))), + dim3(NUM_THREADS), + smem_size, + params.stream, params); + }); + } + + CHECK_CUDA_KERNEL_LAUNCH(); +} + +template void run_flash_mla_combine_kernel(CombineParams ¶ms); + +#ifndef FLASH_MLA_DISABLE_FP16 +template void run_flash_mla_combine_kernel(CombineParams ¶ms); +#endif + +} diff --git a/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine_hip.h b/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine_hip.h new file mode 100644 index 000000000..f60a7dacd --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx9/decode/combine/combine_hip.h @@ -0,0 +1,16 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "params_hip.h" + +namespace gfx9::decode { + +template +void run_flash_mla_combine_kernel(CombineParams ¶ms); + +} diff --git a/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip b/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip new file mode 100644 index 000000000..f4c324e95 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip @@ -0,0 +1,175 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "get_decoding_sched_meta_hip.h" + +#include +#include +#include +#include + +#include "utils_hip.h" + +namespace gfx9::decode { + +__global__ void __launch_bounds__(64, 1) +get_mla_metadata_kernel(const GetDecodeSchedMetaParams params) { + int *seqlens_k_ptr = params.seqlens_k_ptr; + DecodingSchedMeta *tile_scheduler_metadata_ptr = params.tile_scheduler_metadata_ptr; + int *num_splits_ptr = params.num_splits_ptr; + int batch_size = params.b; + int block_size_n = params.block_size_n; + int fixed_overhead_num_blocks = params.fixed_overhead_num_blocks; + int num_sm_parts = params.num_sm_parts; + + extern __shared__ int shared_mem[]; + int* num_blocks_shared = shared_mem; // [batch_size] + int* num_splits_shared = shared_mem + batch_size; // [batch_size+1] + int* seqlens_k_shared = shared_mem + batch_size*2+1; // [batch_size] + int* first_block_idx_shared = shared_mem + batch_size*3+1; // [batch_size] + int* last_block_idx_shared = shared_mem + batch_size*4+1; // [batch_size] + + int total_num_blocks = 0; + for (int i = threadIdx.x; i < batch_size; i += 64) { + int cur_s_k; + if (params.topk == -1) { + // Dense model, cur_s_k = actual s_k + cur_s_k = __ldg(seqlens_k_ptr + i); + } else { + // Sparse model, cur_s_k = topk (+ extra topk) + cur_s_k = params.topk_length ? __ldg(params.topk_length + i) : params.topk; + if (cur_s_k == 0) cur_s_k = 1; // Ensure the main loop will never be empty + if (params.extra_topk) { + cur_s_k = ku::ceil(cur_s_k, block_size_n); + cur_s_k += params.extra_topk_length ? __ldg(params.extra_topk_length + i) : params.extra_topk; + } + } + seqlens_k_shared[i] = cur_s_k; + int first_token_idx = 0; + int last_token_idx = max(cur_s_k-1, 0); + int cur_first_block_idx = first_token_idx / block_size_n; + int cur_last_block_idx = last_token_idx / block_size_n; + // NOTE Should attend to tokens [first_token_idx, last_token_idx], i.e. blocks [cur_first_block_idx, cur_last_block_idx] + // NOTE if seqlens_k is 0, then first_token_idx == last_token_idx == cur_first_block_idx == cur_last_block_idx == 0. So the sequence will have 1 block. We will correct this later in this kernel. + int num_blocks = cur_last_block_idx - cur_first_block_idx + 1; + total_num_blocks += num_blocks + fixed_overhead_num_blocks; + num_blocks_shared[i] = num_blocks; + first_block_idx_shared[i] = cur_first_block_idx; + last_block_idx_shared[i] = cur_last_block_idx; + } + for (int offset = 32; offset >= 1; offset /= 2) { + total_num_blocks += __shfl_xor(total_num_blocks, offset); + } + __syncthreads(); + if (threadIdx.x == 0) { + int payload = cutlass::ceil_div(total_num_blocks, num_sm_parts) + fixed_overhead_num_blocks; + + int now_req_idx = 0, now_block = 0, now_n_split_idx = 0, cum_num_splits = 0; + num_splits_shared[0] = 0; + for (int i = 0; i < num_sm_parts; ++i) { + DecodingSchedMeta cur_meta; + cur_meta.begin_req_idx = now_req_idx; + cur_meta.begin_block_idx = now_block + first_block_idx_shared[now_req_idx]; + cur_meta.begin_split_idx = now_n_split_idx; + cur_meta.is_first_req_splitted = (now_block != 0); + int remain_payload = payload; + while (now_req_idx < batch_size) { + int num_blocks = num_blocks_shared[now_req_idx]; + int now_remain_blocks = num_blocks - now_block; + if (remain_payload >= now_remain_blocks + fixed_overhead_num_blocks) { + cum_num_splits += now_n_split_idx + 1; + num_splits_shared[now_req_idx + 1] = cum_num_splits; + remain_payload -= now_remain_blocks + fixed_overhead_num_blocks; + ++now_req_idx; + now_block = 0; + now_n_split_idx = 0; + } else { + if (remain_payload - fixed_overhead_num_blocks > 0) { + now_block += remain_payload - fixed_overhead_num_blocks; + ++now_n_split_idx; + remain_payload = 0; + } + break; + } + } + cur_meta.end_req_idx = now_block > 0 ? now_req_idx : now_req_idx - 1; + cur_meta.end_block_idx = now_block > 0 ? now_block + first_block_idx_shared[now_req_idx] : (seqlens_k_shared[now_req_idx-1] == 0 ? 0 : last_block_idx_shared[now_req_idx-1] + 1); + cur_meta.is_last_req_splitted = cur_meta.end_block_idx != last_block_idx_shared[cur_meta.end_req_idx] + 1 && seqlens_k_shared[cur_meta.end_req_idx] != 0; + if (cur_meta.begin_req_idx == cur_meta.end_req_idx) { + cur_meta.is_first_req_splitted = cur_meta.is_last_req_splitted = cur_meta.is_first_req_splitted || cur_meta.is_last_req_splitted; + } + tile_scheduler_metadata_ptr[i] = cur_meta; + } + // FLASH_DEVICE_ASSERT(now_req_idx == batch_size && now_block == 0 && now_n_split_idx == 0); + } + __syncthreads(); + + for (int i = threadIdx.x; i <= batch_size; i += 32) { + num_splits_ptr[i] = num_splits_shared[i]; + } +} + +__global__ void __launch_bounds__(256, 1) +get_mla_metadata_no_split_kernel(const GetDecodeSchedMetaParams params) { + DecodingSchedMeta *tile_scheduler_metadata_ptr = params.tile_scheduler_metadata_ptr; + int *num_splits_ptr = params.num_splits_ptr; + int batch_size = params.b; + int block_size_n = params.block_size_n; + int num_sm_parts = params.num_sm_parts; + + for (int part_idx = blockIdx.x * blockDim.x + threadIdx.x; + part_idx < num_sm_parts; + part_idx += blockDim.x * gridDim.x) { + int begin_req_idx = (static_cast(part_idx) * batch_size) / num_sm_parts; + int end_req_idx_exclusive = (static_cast(part_idx + 1) * batch_size) / num_sm_parts; + + DecodingSchedMeta cur_meta; + cur_meta.begin_req_idx = begin_req_idx; + cur_meta.end_req_idx = end_req_idx_exclusive - 1; + cur_meta.begin_block_idx = 0; + cur_meta.begin_split_idx = 0; + cur_meta.is_first_req_splitted = 0; + cur_meta.is_last_req_splitted = 0; + cur_meta._pad[0] = 0; + + int cur_s_k = 0; + if (begin_req_idx < end_req_idx_exclusive) { + if (params.topk == -1) { + cur_s_k = __ldg(params.seqlens_k_ptr + cur_meta.end_req_idx); + } else { + cur_s_k = params.topk_length ? __ldg(params.topk_length + cur_meta.end_req_idx) : params.topk; + if (cur_s_k == 0) cur_s_k = 1; + if (params.extra_topk) { + cur_s_k = ku::ceil(cur_s_k, block_size_n); + cur_s_k += params.extra_topk_length ? __ldg(params.extra_topk_length + cur_meta.end_req_idx) : params.extra_topk; + } + } + } + cur_meta.end_block_idx = cutlass::ceil_div(cur_s_k, block_size_n); + tile_scheduler_metadata_ptr[part_idx] = cur_meta; + } + + for (int i = blockIdx.x * blockDim.x + threadIdx.x; + i <= batch_size; + i += blockDim.x * gridDim.x) { + num_splits_ptr[i] = i; + } +} + +void run_get_decoding_sched_meta_kernel(GetDecodeSchedMetaParams ¶ms) { + int smem_size = sizeof(int) * (static_cast(params.b) * 5 + 1); + hipLaunchKernelGGL(( get_mla_metadata_kernel), dim3(1), dim3(64), smem_size, params.stream, params); + CHECK_CUDA_KERNEL_LAUNCH(); +} + +void run_get_decoding_sched_meta_no_split_kernel(GetDecodeSchedMetaParams ¶ms) { + int grid = cutlass::ceil_div(::max(params.num_sm_parts, params.b + 1), 256); + grid = ::min(grid, 1024); + hipLaunchKernelGGL(( get_mla_metadata_no_split_kernel), dim3(grid), dim3(256), 0, params.stream, params); + CHECK_CUDA_KERNEL_LAUNCH(); +} + +} diff --git a/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h b/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h new file mode 100644 index 000000000..a4a650588 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h @@ -0,0 +1,16 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "params_hip.h" + +namespace gfx9::decode { + +void run_get_decoding_sched_meta_kernel(GetDecodeSchedMetaParams ¶ms); +void run_get_decoding_sched_meta_no_split_kernel(GetDecodeSchedMetaParams ¶ms); + +} diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/config_hip.h b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/config_hip.h new file mode 100644 index 000000000..c4ff8c5d8 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/config_hip.h @@ -0,0 +1,27 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include +// #include +#include +#include "defines_hip.h" + +using namespace cute; + +namespace gfx93::decode::sparse_fp8 { + +static constexpr int HEAD_DIM_K = 576; +static constexpr int HEAD_DIM_V = 512; +static constexpr int HEAD_DIM_NOPE = HEAD_DIM_V; +static constexpr int HEAD_DIM_ROPE = HEAD_DIM_K - HEAD_DIM_V; +static constexpr int QUANT_TILE_SIZE = 128; +static constexpr int NUM_SCALES = HEAD_DIM_NOPE / QUANT_TILE_SIZE; +static constexpr int NUM_BYTES_PER_TOKEN = HEAD_DIM_NOPE + NUM_SCALES*sizeof(float) + HEAD_DIM_ROPE*sizeof(bf16); +static constexpr int PAGE_BLOCK_SIZE = 64; + +} \ No newline at end of file diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/dequant_hip.h b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/dequant_hip.h new file mode 100644 index 000000000..5a8110505 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/dequant_hip.h @@ -0,0 +1,133 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +// #include +// #include + +#include "defines_hip.h" + +namespace gfx93::decode::sparse_fp8 { + +// struct fp8x8 { +// // __nv_fp8x4_e4m3 lo; +// // __nv_fp8x4_e4m3 hi; +// }; + +// struct fp8x16 { +// fp8x8 lo; +// fp8x8 hi; +// }; + +// __device__ __forceinline__ +// bf16x8 cvt_fp8x8_bf16x8(const fp8x8 &inputs, const __hip_bfloat162 &scale_bf162) { +// #define DEQUANT_FP8x4(OUTPUT_BF16_LO, OUTPUT_BF16_HI, FP8x4) \ +// { \ +// float4 fp32x4 = (float4)(FP8x4); \ +// OUTPUT_BF16_LO = __float22bfloat162_rn({fp32x4.x, fp32x4.y})*scale_bf162; \ +// OUTPUT_BF16_HI = __float22bfloat162_rn({fp32x4.z, fp32x4.w})*scale_bf162; \ +// } + +// bf16x8 result; +// DEQUANT_FP8x4(result.a01, result.a23, inputs.lo); +// DEQUANT_FP8x4(result.a45, result.a67, inputs.hi); + +// return result; +// } + +// enum class L1CacheHint { +// NO_ALLOCATE, +// EVICT_FIRST, +// EVICT_NORMAL, +// EVICT_LAST +// }; + +// enum class L2PrefetchHint { +// B64, +// B128, +// B256 +// }; + +// template< +// typename T, +// L1CacheHint l1_cache_hint, +// L2PrefetchHint l2_prefetch_hint +// > +// __device__ __forceinline__ +// T load_128b_from_gmem(const void* addr) { +// static_assert(sizeof(T) == 128/8); +// int4 ret; + +// #define EXEC(L1_HINT_STR, L2_HINT_STR) { \ +// asm volatile("ld.global.nc.L1::" L1_HINT_STR ".L2::" L2_HINT_STR ".v4.s32 {%0, %1, %2, %3}, [%4];" \ +// : "=r"(ret.x), "=r"(ret.y), "=r"(ret.z), "=r"(ret.w) \ +// : "l"(addr)); \ +// } + +// #define DISPATCH_L2(L1_HINT_STR) { \ +// if constexpr(l2_prefetch_hint == L2PrefetchHint::B64) \ +// EXEC(L1_HINT_STR, "64B") \ +// else if constexpr(l2_prefetch_hint == L2PrefetchHint::B128) \ +// EXEC(L1_HINT_STR, "128B") \ +// else if constexpr(l2_prefetch_hint == L2PrefetchHint::B256) \ +// EXEC(L1_HINT_STR, "256B") \ +// } + +// if constexpr(l1_cache_hint == L1CacheHint::NO_ALLOCATE) +// DISPATCH_L2("no_allocate") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_FIRST) +// DISPATCH_L2("evict_first") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_NORMAL) +// DISPATCH_L2("evict_normal") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_LAST) +// DISPATCH_L2("evict_last") + +// #undef EXEC +// #undef DISPATCH_L2 +// return *reinterpret_cast(&ret); +// } + +// template< +// typename T, +// L1CacheHint l1_cache_hint, +// L2PrefetchHint l2_prefetch_hint +// > +// __device__ __forceinline__ +// T load_64b_from_gmem(const void* addr) { +// static_assert(sizeof(T) == 64/8); +// int2 ret; + +// #define EXEC(L1_HINT_STR, L2_HINT_STR) { \ +// asm volatile("ld.global.nc.L1::" L1_HINT_STR ".L2::" L2_HINT_STR ".v2.s32 {%0, %1}, [%2];" \ +// : "=r"(ret.x), "=r"(ret.y) \ +// : "l"(addr)); \ +// } + +// #define DISPATCH_L2(L1_HINT_STR) { \ +// if constexpr(l2_prefetch_hint == L2PrefetchHint::B64) \ +// EXEC(L1_HINT_STR, "64B") \ +// else if constexpr(l2_prefetch_hint == L2PrefetchHint::B128) \ +// EXEC(L1_HINT_STR, "128B") \ +// else if constexpr(l2_prefetch_hint == L2PrefetchHint::B256) \ +// EXEC(L1_HINT_STR, "256B") \ +// } + +// if constexpr(l1_cache_hint == L1CacheHint::NO_ALLOCATE) +// DISPATCH_L2("no_allocate") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_FIRST) +// DISPATCH_L2("evict_first") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_NORMAL) +// DISPATCH_L2("evict_normal") +// else if constexpr(l1_cache_hint == L1CacheHint::EVICT_LAST) +// DISPATCH_L2("evict_last") + +// #undef EXEC +// #undef DISPATCH_L2 +// return *reinterpret_cast(&ret); +// } + +} diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/helpers_hip.h b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/helpers_hip.h new file mode 100644 index 000000000..f099497f9 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/components/helpers_hip.h @@ -0,0 +1,115 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include +#include + +#include "config_hip.h" + +using namespace cute; + +namespace gfx93::decode::sparse_fp8 { + +// // In the layout of fragment A and fragment C during WGMMA, data each thread holds resides in two particular rows. This function converts the local_row_idx (0~1) to the actual row_idx +// // You may refer to this link for the detailed layout: https://docs.nvidia.com/cuda/parallel-thread-execution/#wgmma-64n16-a +// __forceinline__ __device__ int get_AorC_row_idx(int local_row_idx, int idx_in_warpgroup) { +// int row_idx = (idx_in_warpgroup/32)*16 + local_row_idx*8 + (idx_in_warpgroup%32/4); +// return row_idx; +// } + + +// // Adapted from https://github.com/Dao-AILab/flash-attention/blob/cdaf2de6e95cb05400959b5ab984f66e4c7df317/hopper/utils.h +// template +// __forceinline__ __device__ void gemm(TiledMma &tiled_mma, Tensor0 const &tCrA, Tensor1 const &tCrB, Tensor2 &tCrC) { +// constexpr bool Is_RS = !cute::is_base_of::value; +// // Need to cast away const on tCrA since warpgroup_fence_operand doesn't take const +// if constexpr (Is_RS) { cute::warpgroup_fence_operand(const_cast(tCrA)); } +// warpgroup_fence_operand(tCrC); +// if constexpr (arrive) { +// warpgroup_arrive(); +// } +// if constexpr (zero_init) { +// tiled_mma.accumulate_ = GMMA::ScaleOut::Zero; +// // Unroll the K mode manually to set scale D to 1 +// CUTLASS_PRAGMA_UNROLL +// for (int k_block = 0; k_block < size<2>(tCrA); ++k_block) { +// cute::gemm(tiled_mma, tCrA(_,_,k_block), tCrB(_,_,k_block), tCrC); +// tiled_mma.accumulate_ = GMMA::ScaleOut::One; +// } +// } else { +// // cute::gemm(tiled_mma, tCrA, tCrB, tCrC); +// // Unroll the K mode manually to set scale D to 1 +// CUTLASS_PRAGMA_UNROLL +// for (int k_block = 0; k_block < size<2>(tCrA); ++k_block) { +// cute::gemm(tiled_mma, tCrA(_,_,k_block), tCrB(_,_,k_block), tCrC); +// tiled_mma.accumulate_ = GMMA::ScaleOut::One; +// } +// } +// if constexpr (commit) { +// warpgroup_commit_batch(); +// } +// if constexpr (wg_wait >= 0) { warpgroup_wait(); } +// warpgroup_fence_operand(tCrC); +// if constexpr (Is_RS) { warpgroup_fence_operand(const_cast(tCrA)); } +// } + +// template< +// typename TMA, +// typename Tensor0, +// typename Tensor1 +// > +// CUTE_DEVICE +// void launch_tma_copy( +// const TMA &tma_copy, +// const Tensor0 &src, +// Tensor1 &dst, +// transac_bar_t &bar, +// const cute::TMA::CacheHintSm90 &cache_hint = cute::TMA::CacheHintSm90::EVICT_NORMAL, +// const uint16_t &multicast_mask = 0 +// ) { +// auto thr_tma = tma_copy.get_slice(_0{}); +// cute::copy( +// tma_copy.with(reinterpret_cast(bar), multicast_mask, cache_hint), +// thr_tma.partition_S(src), +// thr_tma.partition_D(dst) +// ); +// } + +// template +// CUTE_DEVICE +// static void st_async_128b(void* dst_ptr, const T& data, const transac_bar_t* mbar_ptr) { +// long2 data_long2 = *reinterpret_cast(&data); +// uint32_t dst_addr = cute::cast_smem_ptr_to_uint(dst_ptr); +// uint32_t mbar_addr = cute::cast_smem_ptr_to_uint(mbar_ptr); +// asm volatile ( +// "st.async.weak.shared::cluster.mbarrier::complete_tx::bytes.v2.s64 [%0], {%1, %2}, [%3]; \n" +// : +// : "r"(dst_addr), "l"(data_long2.x), "l"(data_long2.y), "r"(mbar_addr) +// ); +// } + +// CUTE_DEVICE +// static void cp_async_bulk_shared_cta_shared_cluster(void* dst_ptr, void* src_ptr, int size, transac_bar_t* mbar_ptr) { +// uint32_t dst_addr = cute::cast_smem_ptr_to_uint(dst_ptr); +// uint32_t src_addr = cute::cast_smem_ptr_to_uint(src_ptr); +// uint32_t mbar_addr = cute::cast_smem_ptr_to_uint(mbar_ptr); +// asm volatile ( +// "cp.async.bulk.shared::cluster.shared::cta.mbarrier::complete_tx::bytes [%0], [%1], %2, [%3]; \n" +// : +// : "r"(dst_addr), "r"(src_addr), "r"(size), "r"(mbar_addr) +// ); +// } + +// static constexpr int PEER_ADDR_MASK = 16777216; // peer_addr = my_addr ^ PEER_ADDR_MASK. +// template +// CUTE_DEVICE +// T* get_peer_addr(T* p) { +// return (T*)((int64_t)(p) ^ PEER_ADDR_MASK); +// } + +} diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/config_hip.h b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/config_hip.h new file mode 100644 index 000000000..e458070be --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/config_hip.h @@ -0,0 +1,126 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include +#include +#include +#include + +#include "defines_hip.h" +#include "params_hip.h" + +using namespace cute; + +namespace gfx93::decode::sparse_fp8 { + +template +class KernelTemplate { +public: + +static_assert(NUM_HEADS == 64 || NUM_HEADS == 128 || NUM_HEADS == 16); +// todo only support tp8 +static constexpr int BLOCK_M = 16; +static constexpr int NUM_M_BLOCKS = NUM_HEADS / BLOCK_M; +static constexpr bool Is_causal = false; +static constexpr int HEAD_DIM_K = MODEL_TYPE == ModelType::V32 ? 576 : 512; +static constexpr int HEAD_DIM_V = 512; +static constexpr int HEAD_DIM_ROPE = 64; +static constexpr int HEAD_DIM_NOPE = HEAD_DIM_K - HEAD_DIM_ROPE; + +static constexpr int QUANT_TILE_SIZE = MODEL_TYPE == ModelType::V32 ? 128 : 64; +static constexpr int NUM_SCALES = MODEL_TYPE == ModelType::V32 ? 4 : 8; // For MODEL1: 7 fp8_e4m3 + 1 padding + +static constexpr int NUM_THREADS = 256; +static constexpr int TOPK_BLOCK_SIZE = 64; +using elem_type = cutlass::bfloat16_t; +using MMA_Atom_Arch = std::conditional_t< + std::is_same_v, + MMA_Atom, + MMA_Atom +>; +static constexpr int kNWarps = 4; +using ValLayoutMNK = Layout>; +using TiledMma = TiledMMA< + MMA_Atom_Arch, + Layout, _1>>, // 1x4x1 or 1x8x1 thread group + ValLayoutMNK>; + +using MMA_Atom_Arch_16_16_32 = std::conditional_t< + std::is_same_v, + MMA_Atom, + MMA_Atom +>; +using TiledMma_16_16_32 = TiledMMA< + MMA_Atom_Arch_16_16_32, + Layout, _1>>, // 1x4x1 or 1x8x1 thread group + ValLayoutMNK>; + +using MMA_Atom_Arch_16x32_NT = std::conditional_t< + std::is_same_v, + MMA_Atom, + MMA_Atom +>; +using TiledMma_O = TiledMMA< + MMA_Atom_Arch_16x32_NT, + Layout, _1>>, // 1x4x1 or 1x8x1 thread group + ValLayoutMNK>; + +using SmemLayoutAtomK = decltype(composition( + Swizzle<3, 3, 3>{}, + Layout, Int<32>>, Stride, _1>>{})); +using SmemLayoutK = decltype(tile_to_shape( + SmemLayoutAtomK{}, + Shape, Int<8 * 32>>{})); + +using SmemLayoutAtomV = SmemLayoutAtomK; +using SmemLayoutV = decltype(tile_to_shape( + SmemLayoutAtomV{}, + Shape, Int<512>>{})); + +using SmemLayoutVtransposed = decltype( + composition(SmemLayoutV{}, make_layout(Shape, Int>{}, GenRowMajor{}))); +using SmemLayoutVtransposedNoSwizzle = decltype(get_nonswizzle_portion(SmemLayoutVtransposed{})); + +using SmemLayoutAtomP = Layout>, Stride>>; +using SmemLayoutP = decltype(tile_to_shape( + SmemLayoutAtomP{}, + Shape>{})); +using SmemLayoutRow = Layout, Stride<_1>>; + +using Element = cutlass::bfloat16_t; +using ElementAccum = float; +struct SharedMemoryPlan { + union { + struct { + cute::array_aligned> smem_v; + }; + struct { + // cute::array_aligned> smem_v_tmp; // Double buffer + cute::array_aligned> smem_p; + cute::array_aligned> smem_row_sum; + cute::array_aligned> smem_row_max; + + }; + }; +}; + + +static __device__ __forceinline__ void +compute_attn_1rowblock_splitkv_sparse_mla_fp8(const SparseAttnDecodeParams ¶ms, const DecodingSchedMeta& sched_meta, int batch_idx); + +static __device__ __forceinline__ void +devfunc(const SparseAttnDecodeParams ¶ms); + +static void run(const SparseAttnDecodeParams ¶ms); + +}; + + + + +} diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip new file mode 100644 index 000000000..13a23ca28 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip @@ -0,0 +1,14 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "../splitkv_mla_hip.cuh" + +namespace gfx93::decode::sparse_fp8 { + +template void run_flash_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams ¶ms); + +} + diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.cuh b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.cuh new file mode 100644 index 000000000..d9972c91d --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.cuh @@ -0,0 +1,1018 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +#pragma once + +#include "splitkv_mla_hip.h" + +// #include +// #include +// #include +// #include +// #include +// #include + +#include + +#include "utils_hip.h" +#include "components/dequant_hip.h" +#include "components/helpers_hip.h" +#include "config_hip.h" +#include "softmax_hip.h" +using namespace cute; + +namespace gfx93::decode::sparse_fp8 { +#define CUDART_L2E_F 1.442695041F + +static constexpr float MAX_INIT_VAL = -1e30; // Prevent (-inf) - (-inf) = nan + +template +__device__ void KernelTemplate::compute_attn_1rowblock_splitkv_sparse_mla_fp8(const SparseAttnDecodeParams ¶ms, const DecodingSchedMeta& sched_meta, int batch_idx) +{ + using Element = cutlass::bfloat16_t; + using index_t = int64_t; + const int tidx = threadIdx.x; + const int lane_idx = tidx % 64; + const int warp_idx = __builtin_amdgcn_readfirstlane(tidx / 64); + const int head_block_idx = NUM_M_BLOCKS == 1 ? 0 : blockIdx.x; + const int s_q_idx = blockIdx.y; + extern __shared__ char shared_memory[]; + SharedMemoryPlan &plan = *reinterpret_cast(shared_memory); + + const index_t row_offset_q = batch_idx * params.stride_q_b + head_block_idx * BLOCK_M * params.stride_q_h_q + s_q_idx * params.stride_q_s_q; + Tensor gQ = make_tensor(make_gmem_ptr(reinterpret_cast(params.q) + row_offset_q), + Shape, Int>{}, + make_stride(params.stride_q_h_q, _1{})); + const index_t row_offset_k = 0; + Tensor gK = make_tensor(make_gmem_ptr(reinterpret_cast(params.kv) + row_offset_k), + Shape, Int>{}, + make_stride(params.stride_kv_row, _1{})); + Tensor sV = make_tensor(make_smem_ptr(plan.smem_v.data()), SmemLayoutV{}); + Tensor sK = make_tensor(make_smem_ptr(plan.smem_v.data()), SmemLayoutK{}); + Tensor sP = make_tensor(make_smem_ptr(plan.smem_p.data()), SmemLayoutP{}); + Tensor sVt = make_tensor(sV.data(), SmemLayoutVtransposed{}); + Tensor sVtNoSwizzle = make_tensor(sV.data(), SmemLayoutVtransposedNoSwizzle{}); + Tensor sRow_max_reduce_buffer = make_tensor(make_smem_ptr(plan.smem_row_max.data()), SmemLayoutRow{}); + Tensor sRow_sum_reduce_buffer = make_tensor(make_smem_ptr(plan.smem_row_sum.data()), SmemLayoutRow{}); + + const index_t row_offset_topk = batch_idx * params.stride_indices_b + s_q_idx * params.stride_indices_s_q; // todo + int* gIndices = reinterpret_cast(params.indices) + row_offset_topk; + int* gExtraIndices = params.extra_indices + batch_idx*params.stride_extra_indices_b + s_q_idx*params.stride_extra_indices_s_q; // (extra_topk) : (1) + TiledMMA tiled_mma = TiledMma{}; + auto thr_mma = tiled_mma.get_thread_slice(tidx); + TiledMMA tiled_mma_16x16x32 = TiledMma_16_16_32{}; + auto thr_mma_16x16x32 = tiled_mma_16x16x32.get_thread_slice(tidx); + TiledMMA tiled_mma_o = TiledMma_O{}; + auto thr_mma_o = tiled_mma_o.get_thread_slice(tidx); +#if 0 + // load Q + auto gmem_tiled_copy_Q = make_tiled_copy_A(Copy_Atom{}, tiled_mma); + auto gmem_thr_copy_Q = gmem_tiled_copy_Q.get_thread_slice(tidx); + Tensor tSgQ = gmem_thr_copy_Q.partition_S(gQ); + Tensor tSrQ = thr_mma.partition_fragment_A(gQ); + Tensor cQ = make_identity_tensor(make_shape(size<0>(gQ), size<1>(gQ))); + Tensor tQcQ = gmem_thr_copy_Q.partition_S(cQ); + Tensor tQpQ = make_tensor(make_shape(size<2>(tSgQ))); + flash::copy(gmem_tiled_copy_Q, tSgQ, tSrQ, tQcQ, tQpQ, params.h_q - head_block_idx * BLOCK_M); + __syncthreads(); +#else + Tensor tSrQ = thr_mma.partition_fragment_A(gQ); + // 需要的最大空间为 16 * 576 * 2 + Element* s_q = reinterpret_cast(shared_memory); + auto lds_direct_copy_q = [&](const int k_idx, const int offset_k) { + // static_assert(offset_k == 0 || offset_k == 1); + // static_assert(k_idx < 3); + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(gQ.data().get()); + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + constexpr int elements_per_thread = 8; + constexpr int bytes_per_warp = 64 * 8 * 2; + constexpr int bytes_per_block = bytes_per_warp * 4; + const int row_idx = lane_idx % 16; + const int col_idx = lane_idx / 16; + const int row_offset = row_idx; + if constexpr (MODEL_TYPE == ModelType::V32) + { + int col_offset; + if (k_idx == 2) + { + col_offset = k_idx * 256 + warp_idx * 8 + col_idx * 16; + } + else + { + col_offset = k_idx * 256 + warp_idx * 64 + col_idx * 16 + offset_k * 8; + } + int offset_v = (row_offset * params.stride_q_h_q + col_offset) * 2; + if (head_block_idx * BLOCK_M + row_idx >= params.h_q) { + offset_v = -1; + } + if (k_idx == 2 && warp_idx >= 2) + { + offset_v = -1; + } + const int offset_s = 0; + int ldsAddrPerWave = reinterpret_cast(s_q) + warp_idx * bytes_per_warp + k_idx * bytes_per_block + + offset_k * 3 * bytes_per_block; + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + } + else + { + const int col_offset = k_idx * 256 + warp_idx * 64 + col_idx * 16 + offset_k * 8; + int offset_v = (row_offset * params.stride_q_h_q + col_offset) * 2; + if (head_block_idx * BLOCK_M + row_idx >= params.h_q) { + offset_v = -1; + } + + const int offset_s = 0; + int ldsAddrPerWave = reinterpret_cast(s_q) + warp_idx * bytes_per_warp + k_idx * bytes_per_block + + offset_k * 2 * bytes_per_block; + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + } + + }; + + if constexpr (MODEL_TYPE == ModelType::V32) + { + // __builtin_amdgcn_sched_barrier(0); + lds_direct_copy_q(0, 0); + lds_direct_copy_q(1, 0); + lds_direct_copy_q(0, 1); + lds_direct_copy_q(1, 1); + lds_direct_copy_q(2, 0); + Element* s_q_read_ptr = s_q + lane_idx * 8; + asm volatile("s_waitcnt vmcnt(4) \n s_barrier"); + for (int k = 0; k < 4; k++) + { + for (int i = 0; i < 8; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(3) \n s_barrier"); + for (int k = 4; k < 8; k++) + { + for (int i = 0; i < 8; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(2) \n s_barrier"); + s_q_read_ptr = s_q + lane_idx * 8 + 3 * 4 * 16 * 4 * 8; + for (int k = 0; k < 4; k++) + { + for (int i = 8; i < 16; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i - 8]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(1) \n s_barrier"); + for (int k = 4; k < 8; k++) + { + for (int i = 8; i < 16; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i - 8]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(0) \n s_barrier"); + s_q_read_ptr = s_q + lane_idx * 8 + 2 * 4 * 16 * 4 * 8; + for (int k = 8; k < 9; k++) + { + for (int i = 0; i < 8; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i]; + } + s_q_read_ptr += 16 * 32; + } + for (int k = 8; k < 9; k++) + { + for (int i = 8; i < 16; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i-8]; + } + s_q_read_ptr += 16 * 32; + } + // __syncthreads(); + asm volatile("s_waitcnt lgkmcnt(0) \n s_barrier"); + // __builtin_amdgcn_sched_barrier(0); + } + else + { + // __builtin_amdgcn_sched_barrier(0); + lds_direct_copy_q(0, 0); + lds_direct_copy_q(1, 0); + lds_direct_copy_q(0, 1); + lds_direct_copy_q(1, 1); + + Element* s_q_read_ptr = s_q + lane_idx * 8; + asm volatile("s_waitcnt vmcnt(3) \n s_barrier"); + for (int k = 0; k < 4; k++) + { + for (int i = 0; i < 8; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(2) \n s_barrier"); + for (int k = 4; k < 8; k++) + { + for (int i = 0; i < 8; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(1) \n s_barrier"); + for (int k = 0; k < 4; k++) + { + for (int i = 8; i < 16; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i - 8]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt vmcnt(0) \n s_barrier"); + for (int k = 4; k < 8; k++) + { + for (int i = 8; i < 16; i++) + { + tSrQ(i, 0, k) = s_q_read_ptr[i - 8]; + } + s_q_read_ptr += 16 * 32; + } + asm volatile("s_waitcnt lgkmcnt(0) \n s_barrier"); + // __builtin_amdgcn_sched_barrier(0); + } + +#endif + // zhj debug + // if (head_block_idx == 0) + // { + // printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(tSrQ(0)), float(tSrQ(1)), float(tSrQ(2)), float(tSrQ(3))); + // } + Tensor tSrK = thr_mma.partition_fragment_B(gK); + auto smem_tiled_copy_K = make_tiled_copy_B(Copy_Atom{}, tiled_mma_16x16x32); + auto smem_thr_copy_K = smem_tiled_copy_K.get_thread_slice(tidx); + Tensor tOsV = smem_thr_copy_K.partition_S(sK); + + auto smem_tiled_copy_V = make_tiled_copy_B(Copy_Atom{}, tiled_mma_o); + auto smem_thr_copy_V = smem_tiled_copy_V.get_thread_slice(tidx); + Tensor tOsVt = smem_thr_copy_V.partition_S(sVt); + Tensor tOrVt = thr_mma_o.partition_fragment_B(sVt); + Tensor tOrVt_copy_view = smem_thr_copy_V.retile_D(tOrVt); + + const auto gK_data = gK.data(); + typedef unsigned int __hip_fp8x4_storage_t; + typedef unsigned short int __hip_fp8x2_storage_t; + typedef unsigned char __hip_fp8_storage_t; + typedef __fp16 __fp16x8_t __attribute__((ext_vector_type(8))); + typedef __fp16 __fp16x4_t __attribute__((ext_vector_type(4))); + typedef int v2i __attribute__((ext_vector_type(2))); + + union Fp8_storage{ + __fp16x8_t data_128; + __hip_fp8x4_storage_t fp8_array[4]; + }; + + union bf16_storage{ + uint32x4_t data_128; + uint16_t data_array[8]; + }; + struct MainloopArgs { + int start_block_idx, end_block_idx; + bool is_no_split; + + // The following fields are only valid for MODEL1 + int topk_length, extra_topk_length, num_orig_kv_blocks; + }; + + auto get_cur_req_info = [&](int batch_idx) -> MainloopArgs { + MainloopArgs args; + int total_topk_padded; + if constexpr (MODEL_TYPE == ModelType::V32) { + total_topk_padded = params.topk; + } else { + int topk_length = params.topk_length ? __ldg(params.topk_length + batch_idx) : params.topk; + int orig_topk_padded = max(ku::ceil(topk_length, (int)TOPK_BLOCK_SIZE), (int)TOPK_BLOCK_SIZE); + int extra_topk_length = params.extra_topk_length ? __ldg(params.extra_topk_length + batch_idx) : params.extra_topk; + total_topk_padded = orig_topk_padded + ku::ceil(extra_topk_length, (int)TOPK_BLOCK_SIZE); + args.topk_length = topk_length; + args.extra_topk_length = extra_topk_length; + args.num_orig_kv_blocks = orig_topk_padded / TOPK_BLOCK_SIZE; + } + + args.start_block_idx = batch_idx == sched_meta.begin_req_idx ? sched_meta.begin_block_idx : 0; + args.end_block_idx = batch_idx == sched_meta.end_req_idx ? sched_meta.end_block_idx : total_topk_padded / TOPK_BLOCK_SIZE; + args.is_no_split = batch_idx == sched_meta.begin_req_idx ? !sched_meta.is_first_req_splitted : (batch_idx == sched_meta.end_req_idx ? !sched_meta.is_last_req_splitted : true); + + return args; + }; + + Tensor acc_o = partition_fragment_C(tiled_mma_o, Shape, Int>{}); + clear(acc_o); + flash::Softmax(acc_o)> softmax; + MainloopArgs args = get_cur_req_info(batch_idx); + + struct IsOrigBlock {}; + struct IsExtraBlock {}; + auto process_one_block = [&](int block_idx, auto is_extra_block_t) { + static constexpr bool IS_EXTRA_BLOCK = std::is_same_v; + Tensor acc_s = partition_fragment_C(tiled_mma, Shape, Int>{}); + clear(acc_s); + int col_idx = lane_idx / 16; + + int* indices_base; + int page_block_size; + int64_t k_block_stride, k_row_stride; + uint8_t* k_ptr; + if constexpr (!IS_EXTRA_BLOCK) { + indices_base = gIndices + (block_idx)*TOPK_BLOCK_SIZE; + page_block_size = params.page_block_size; + k_block_stride = params.stride_kv_block; + k_row_stride = params.stride_kv_row; + k_ptr = (uint8_t*)params.kv; + } else { + indices_base = gExtraIndices + (block_idx-args.num_orig_kv_blocks)*TOPK_BLOCK_SIZE; + page_block_size = params.extra_page_block_size; + k_block_stride = params.stride_extra_kv_block; + k_row_stride = params.stride_extra_kv_row; + k_ptr = (uint8_t*)params.extra_kv; + } + [[maybe_unused]] int topk_length = IS_EXTRA_BLOCK ? args.extra_topk_length : args.topk_length; + [[maybe_unused]] int rel_block_idx = IS_EXTRA_BLOCK ? (block_idx - args.num_orig_kv_blocks) : block_idx; + int token_index = indices_base[(lane_idx % 16) + warp_idx * 16]; + if constexpr (MODEL_TYPE == ModelType::MODEL1) { + if (rel_block_idx*TOPK_BLOCK_SIZE + (lane_idx % 16) + warp_idx * 16 >= topk_length) + { + token_index = -1; + } + } + int block_index = token_index == -1 ? 0 : (int)((uint32_t)token_index/(uint32_t)page_block_size); // Use uint32_t division and mod to improve performance const int token_indexrel_idx_in_block = (token_index + page_block_size) % page_block_size; + int rel_idx_in_block = (uint32_t)token_index % (uint32_t)page_block_size; // NOTE When token_index is -1 (UINT_MAX), UINT_MAX%page_block_size < page_block_size, so there will be no illegal-memory-access error + const index_t offset_k = (index_t)(block_index) * k_block_stride; + uint8_t* gK_base; + float scales[NUM_SCALES]; + if constexpr (MODEL_TYPE == ModelType::V32) { + gK_base = k_ptr + offset_k + rel_idx_in_block * k_row_stride; + float* scale_ptr = (float*)(gK_base + HEAD_DIM_NOPE); + static_assert(NUM_SCALES == 4); + static_assert(HEAD_DIM_NOPE == 512); + if (token_index == -1) + { + for (int i = 0; i < NUM_SCALES; i++) + { + scales[i] = 0.0f; + } + } + else + { + for (int i = 0; i < NUM_SCALES; i++) + { + scales[i] = scale_ptr[i]; + } + } + } else { + gK_base = k_ptr + offset_k + rel_idx_in_block*(HEAD_DIM_NOPE + HEAD_DIM_ROPE*2);; + static_assert(NUM_SCALES == 8); + #if 1 + uint8_t* scale_ptr = k_ptr + offset_k + page_block_size*(HEAD_DIM_NOPE+HEAD_DIM_ROPE*2) + rel_idx_in_block*NUM_SCALES; + if (token_index == -1) + { + for (int i = 0; i < NUM_SCALES; i++) + { + scales[i] = 0.0f; + } + } + else + { + union Scale_e8m0 + { + __fp16x4_t tmp; + __hip_fp8_storage_t fp8_e8m0[NUM_SCALES]; + }; + Scale_e8m0 scale_e8m0; + scale_e8m0.tmp = *(__fp16x4_t*)(scale_ptr); + union Fp32{ + uint32_t as_bits; + float as_value; + }; + Fp32 fp32; + for (int i = 0; i < NUM_SCALES - 1; i++) + { + fp32.as_bits = (scale_e8m0.fp8_e8m0[i] << 23); + scales[i] = fp32.as_value; + } + + } + #else + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(k_ptr + offset_k + page_block_size*(HEAD_DIM_NOPE+HEAD_DIM_ROPE*2)); + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + int offset_v = token_index == -1 ? -1: rel_idx_in_block*NUM_SCALES; + union Scale_e8m0 + { + v2i tmp; + __hip_fp8_storage_t fp8_e8m0[NUM_SCALES]; + }; + Scale_e8m0 scale_e8m0; + scale_e8m0.tmp = __builtin_amdgcn_buffer_load_dwordx2(global_addr, 0, offset_v, 0, 0); + union Fp32{ + uint32_t as_bits; + float as_value; + }; + Fp32 fp32; + for (int i = 0; i < NUM_SCALES - 1; i++) + { + fp32.as_bits = (scale_e8m0.fp8_e8m0[i] << 23); + scales[i] = fp32.as_value; + } + #endif + + // if (block0() && threadIdx.x < 64) + // { + // printf("tidx = %d, %.3f %.2f %.2f \n",tidx, scales[0], scales[1], scales[2]); + // } + } + + // // zhj debug + // if (head_block_idx == 0 && threadIdx.x < 64) + // { + // printf("tidx = %d, %.2f %.2f %.2f %.2f %d offset_k = %d rel_idx_in_block = %d params.stride_kv_row = %d %p params.kv = %p \n", tidx, float(scales[0]), float(scales[1]), float(scales[2]), float(scales[3]), + // token_index, + // offset_k, + // rel_idx_in_block, + // params.stride_kv_row, + // gK_base, + // params.kv + // ); + // } + auto dequant_to_bf16 = [&](const Fp8_storage& data0, const float& kv_scale, int idx) -> std::tuple { + #if defined(__gfx938__) + auto res1 = __builtin_amdgcn_cvt_pk_f32_fp8(data0.fp8_array[idx/4], false); + auto res2 = __builtin_amdgcn_cvt_pk_f32_fp8(data0.fp8_array[idx/4], true); + + auto f1 = res1[0]; + auto f2 = res1[1]; + auto f3 = res2[0]; + auto f4 = res2[1]; + #else + const auto fp8x2_low = *reinterpret_cast(&data0.fp8_array[idx / 4]); + const auto fp8x2_high = *(reinterpret_cast(&(data0.fp8_array[idx / 4])) + 1); + auto f1 = flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = flash::fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + #endif + + f1 *= kv_scale; + f2 *= kv_scale; + f3 *= kv_scale; + f4 *= kv_scale; + cutlass::NumericConverter convert_; + auto rst0 = convert_(f1); + auto rst1 = convert_(f2); + auto rst2 = convert_(f3); + auto rst3 = convert_(f4); + + return {rst0, rst1, rst2, rst3}; + }; + if constexpr (MODEL_TYPE == ModelType::V32) + { + Fp8_storage data[4]; + for (int k_idx = 4; k_idx < 8; k_idx++) + { + if (token_index == -1) { + data[k_idx - 4].data_128 = {0}; + } else { + data[k_idx - 4].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64)); + } + } + for (int k_idx = 4; k_idx < 8; k_idx++) + { + for (int j = 0; j < 16; j+=4) { + auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx - 4], scales[k_idx / 2], j); + + tSrK(j, 0, k_idx) = rst0; + tSrK(j + 1, 0, k_idx) = rst1; + tSrK(j + 2, 0, k_idx) = rst2; + tSrK(j + 3, 0, k_idx) = rst3; + + } + // cute::copy(smem_tiled_copy_K, tSrK(_, _, k_idx), tOsV(_, _, k_idx % 4)); + // __builtin_amdgcn_sched_barrier(0); + + #pragma unroll + for (int j = 0; j < 8; j++) { + tOsV(j, 0, (k_idx - 4) * 2) = tSrK(j, 0, k_idx); + } + #pragma unroll + for (int j = 8; j < 16; j++) { + tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) = tSrK(j, 0, k_idx); + } + // __builtin_amdgcn_sched_barrier(0); + + cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s); + } + } + else + { + Fp8_storage data[3]; + for (int k_idx = 4; k_idx < 7; k_idx++) + { + if (token_index == -1) { + data[k_idx - 4].data_128 = {0}; + } else { + data[k_idx - 4].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64)); + } + } + for (int k_idx = 4; k_idx < 7; k_idx++) + { + for (int j = 0; j < 16; j+=4) { + auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx - 4], scales[k_idx], j); + + tSrK(j, 0, k_idx) = rst0; + tSrK(j + 1, 0, k_idx) = rst1; + tSrK(j + 2, 0, k_idx) = rst2; + tSrK(j + 3, 0, k_idx) = rst3; + + } + // cute::copy(smem_tiled_copy_K, tSrK(_, _, k_idx), tOsV(_, _, k_idx % 4)); + // __builtin_amdgcn_sched_barrier(0); + + #pragma unroll + for (int j = 0; j < 8; j++) { + tOsV(j, 0, (k_idx - 4) * 2) = tSrK(j, 0, k_idx); + } + #pragma unroll + for (int j = 8; j < 16; j++) { + tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) = tSrK(j, 0, k_idx); + } + // __builtin_amdgcn_sched_barrier(0); + + cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s); + } + // if (head_block_idx == 0) + // { + // printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3))); + // } + bf16_storage bf16_data0; + bf16_storage bf16_data1; + if (token_index == -1) + { + bf16_data0.data_128 = {0}; + bf16_data1.data_128 = {0}; + } + else + { + bf16_data0.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + HEAD_DIM_NOPE)); + bf16_data1.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 8 * 2 + HEAD_DIM_NOPE)); + } + for (int j = 0; j < 8; j++) { + auto rst = cutlass::bfloat16_t::bitcast(bf16_data0.data_array[j]); + tSrK(j, 0, 7) = rst; + } + for (int j = 8; j < 16; j++) { + auto rst = cutlass::bfloat16_t::bitcast(bf16_data1.data_array[j - 8]); + tSrK(j, 0, 7) = rst; + } + constexpr static int k_idx = 7; + // if (block0()) + // { + // printf(" %.4f %.4f %.4f %.4f \n", + // float(tSrK(0, 0, 7)), + // float(tSrK(1, 0, 7)), + // float(tSrK(2, 0, 7)), + // float(tSrK(3, 0, 7)) + + // ); + // } + cute::gemm(tiled_mma, tSrQ(_, _, 7), tSrK(_, _, 7), acc_s); + #pragma unroll + for (int j = 0; j < 8; j++) { + // tOsV(j, 0, (k_idx - 4) * 2) = Element(j); + tOsV(j, 0, (k_idx - 4) * 2) = tSrK(j, 0, k_idx); + } + #pragma unroll + for (int j = 8; j < 16; j++) { + tOsV(j - 8, 0, (k_idx - 4) * 2 + 1) = tSrK(j, 0, k_idx); + } + } + + __syncthreads(); + __builtin_amdgcn_sched_barrier(0); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 0, 2>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 1, 2>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 2, 2>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 3, 2>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<1, 0, 3>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<1, 1, 3>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<1, 2, 3>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<1, 3, 3>(tOsVt, tOrVt_copy_view); + + __syncthreads(); + // if (block0() && threadIdx.x >= 192) + // { + // printf(" %.4f %.4f %.4f %.4f %p %p\n", + // float(tOsVt(0, 3, 0)), float(tOsVt(1, 3, 0)), float( tSrK(8, 0, 7)), float( tSrK(9, 0, 7)), + // &(tOsVt(0, 1, 3)), &(tOsV(0, 0, 7))); + // } + __builtin_amdgcn_sched_barrier(0); + Fp8_storage data[4]; + // __ds_read_m64x16_row_col_rrow<0, 0, 4>(tOsVt, tOrVt_copy_view); + for (int k_idx = 0; k_idx < 4; k_idx++) + { + if (token_index == -1) { + data[k_idx].data_128 = {0}; + } else { + data[k_idx].data_128 = *((__fp16x8_t*)(gK_base + col_idx * 16 + k_idx * 64)); + } + } + for (int k_idx = 0; k_idx < 4; k_idx++) + { + for (int j = 0; j < 16; j+=4) { + auto [rst0, rst1, rst2, rst3] = dequant_to_bf16(data[k_idx], scales[MODEL_TYPE == ModelType::V32 ? k_idx / 2 : k_idx], j); + tSrK(j, 0, k_idx) = rst0; + tSrK(j + 1, 0, k_idx) = rst1; + tSrK(j + 2, 0, k_idx) = rst2; + tSrK(j + 3, 0, k_idx) = rst3; + + } + // for (int j = 0; j < 16; j++) { + // tOsV(j % 8, 0, (k_idx % 4) * 2 + ( j / 8) ) = tSrK(j, 0, k_idx); + // } + // __builtin_amdgcn_sched_barrier(0); + #pragma unroll + for (int j = 0; j < 8; j++) { + tOsV(j, 0, k_idx * 2) = tSrK(j, 0, k_idx); + } + #pragma unroll + for (int j = 8; j < 16; j++) { + tOsV(j - 8, 0, k_idx * 2 + 1) = tSrK(j, 0, k_idx); + } + // __builtin_amdgcn_sched_barrier(0); + cute::gemm(tiled_mma, tSrQ(_, _, k_idx), tSrK(_, _, k_idx), acc_s); + } + + __syncthreads(); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 0, 0>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 1, 0>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 2, 0>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_rrow_alt<0, 3, 0>(tOsVt, tOrVt_copy_view); + + if constexpr (MODEL_TYPE == ModelType::V32) + { + bf16_storage bf16_data0; + bf16_storage bf16_data1; + bf16_data0.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 512 + 16)); + bf16_data1.data_128 = *((uint32x4_t*)(gK_base + col_idx * 16 * 2 + 8 * 2 + 512 + 16)); + for (int j = 0; j < 8; j++) { + auto rst = cutlass::bfloat16_t::bitcast(bf16_data0.data_array[j]); + tSrK(j, 0, 8) = rst; + } + for (int j = 8; j < 16; j++) { + auto rst = cutlass::bfloat16_t::bitcast(bf16_data1.data_array[j - 8]); + tSrK(j, 0, 8) = rst; + } + cute::gemm(tiled_mma, tSrQ(_, _, 8), tSrK(_, _, 8), acc_s); + + } + // zhj debug + // if (head_block_idx == 0) + // { + // printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3))); + // } + asm volatile("s_waitcnt lgkmcnt(0) \n\t s_barrier\n\t"); + + Tensor cS = make_identity_tensor(Shape, Int>{}); + Tensor tScS = thr_mma.partition_C(cS); + auto is_index_valid = [&](int index) -> bool { + if constexpr (MODEL_TYPE == ModelType::V32) { + return indices_base[int(get<1>(tScS(index)))] != -1; + } else { + return indices_base[int(get<1>(tScS(index)))] != -1 && (rel_block_idx*TOPK_BLOCK_SIZE + int(get<1>(tScS(index))) < topk_length); + } + }; + for (int i = 0; i < size(acc_s); ++i) { + // int idx = indices_base[int(get<1>(tScS(i)))] ; + if (not is_index_valid(i)) acc_s(i) = -INFINITY; + } + block_idx == args.start_block_idx + ? softmax.template softmax_rescale_o_prefill(acc_s, acc_o, sRow_max_reduce_buffer, params.sm_scale_div_log2) + : softmax.template softmax_rescale_o_prefill(acc_s, acc_o, sRow_max_reduce_buffer, params.sm_scale_div_log2); + // if (head_block_idx == 0 && batch_idx == 0) + // { + // printf("tidx = %d, %.2f %.2f %.2f %.2f \n", tidx, float(acc_s(0)), float(acc_s(1)), float(acc_s(2)), float(acc_s(3))); + // } + Tensor rP = flash::convert_type(acc_s); + Tensor tOrP = flash::convert_layout_acc_Aregs(tiled_mma, tiled_mma_o, rP, sP); + + { + // __ds_read_m32x16_row_col<0, 0>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_alt<1, 0>(tOsVt, tOrVt_copy_view); + // __ds_read_m32x16_row_col<2, 0>(tOsVt, tOrVt_copy_view); + + // __ds_read_m32x16_row_col<0, 1>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_alt<1, 1>(tOsVt, tOrVt_copy_view); + // __ds_read_m32x16_row_col<2, 1>(tOsVt, tOrVt_copy_view); + cute::gemm(tiled_mma_o, tOrP(_, _, 0), tOrVt(_, _, 0), acc_o); + cute::gemm(tiled_mma_o, tOrP(_, _, 1), tOrVt(_, _, 1), acc_o); + // __ds_read_m32x16_row_col<0, 2>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_alt<1, 2>(tOsVt, tOrVt_copy_view); + // __ds_read_m32x16_row_col<2, 2>(tOsVt, tOrVt_copy_view); + + + // __ds_read_m32x16_row_col<0, 3>(tOsVt, tOrVt_copy_view); + flash::__ds_read_m32x16_row_col_alt<1, 3>(tOsVt, tOrVt_copy_view); + // __ds_read_m32x16_row_col<2, 3>(tOsVt, tOrVt_copy_view); + + + cute::gemm(tiled_mma_o, tOrP(_, _, 2), tOrVt(_, _, 2), acc_o); + cute::gemm(tiled_mma_o, tOrP(_, _, 3), tOrVt(_, _, 3), acc_o); + } + }; + + if constexpr (MODEL_TYPE == ModelType::V32) { + for (int block_idx = args.start_block_idx; block_idx < args.end_block_idx; block_idx++) { + process_one_block(block_idx, IsOrigBlock{}); + } + } else { + for (int block_idx = args.start_block_idx; block_idx < min(args.num_orig_kv_blocks, args.end_block_idx); ++block_idx) { + process_one_block(block_idx, IsOrigBlock{}); + } + for (int block_idx = max(args.start_block_idx, args.num_orig_kv_blocks); block_idx < args.end_block_idx; ++block_idx) { + process_one_block(block_idx, IsExtraBlock{}); + } + } + // if (head_block_idx == 0 && threadIdx.x < 64 && batch_idx == 0) + // { + // printf(" %.4f %.4f \n", acc_o(0), acc_o(1)); + // } + auto float2bf16 = [] (float s) -> uint16_t { + uint32_t x32 = reinterpret_cast(s); + #ifndef FLASH_MLA_BF16_TYPE + #define FLASH_MLA_BF16_TYPE 0 + #endif + #if FLASH_MLA_BF16_TYPE == 1 + x32 += 0x8000u; + #endif + return uint16_t(x32 >> 16); + }; + if (args.is_no_split) { + int start_head_idx = head_block_idx*BLOCK_M; + Tensor lse = softmax.template normalize_softmax_lse(acc_o, sRow_sum_reduce_buffer, params.sm_scale); + const index_t row_offset_o = batch_idx * params.stride_o_b + start_head_idx * params.stride_o_h_q + s_q_idx * params.stride_o_s_q ; + Tensor gO = make_tensor(make_gmem_ptr(reinterpret_cast(params.out) + row_offset_o), + Shape, Int>{}, + make_stride(params.stride_o_h_q, _1{})); + if (params.attn_sink != nullptr) { + float rAttn_sink = __ldg((float*)params.attn_sink + start_head_idx + lane_idx % 16); + if (flash::is_positive_infinity(rAttn_sink)) + { + for (int i = 0; i < size(acc_o); i++) + { + acc_o(i) = 0.0f; + } + } + else + { + if (!flash::is_positive_infinity(lse(0))) + { + float lse_exp2 = __builtin_amdgcn_exp2f(lse[0] * CUDART_L2E_F); + float rAttn_sink_exp2 = __builtin_amdgcn_exp2f(rAttn_sink * CUDART_L2E_F); + float o_scale = lse_exp2 / (lse_exp2 + rAttn_sink_exp2); + for (int i = 0; i < size(acc_o); i++) + { + acc_o(i) *= o_scale; + } + } + } + + } + // if (block0() && tidx % 16 == 0) + // { + // printf(" tidx = %d %.3f %.3f %.3f %.3f %.3f %.3f %.3f %3f \n ", + // tidx, + // float(acc_o(0)), + // float(acc_o(1)), + // float(acc_o(2)), + // float(acc_o(3)), + // float(acc_o(4)), + // float(acc_o(5)), + // float(acc_o(6)), + // float(acc_o(7)) + // ); + // } + float* gSoftmaxLse = (float*)params.lse + batch_idx * params.stride_lse_b + start_head_idx + s_q_idx * params.stride_lse_s_q; // (BLOCK_M) : (1) + { + // auto rO = flash::convert_type(acc_o); + using result_type = cutlass::Array; + int row, col; + const int warpId = tidx / 64; + const int laneId = tidx % 64; + for (int mi = 0; mi < size<1>(acc_o); ++mi) { + row = mi * BLOCK_M + laneId % 16; + if (row < params.h_q) { + for (int ni = 0; ni < size<2>(acc_o); ++ni) { + // col = (laneId / 16) + ni * 128 + warpId * 32 ; + // 为了使用global_loadx4指令, V矩阵吸入lds的时候 N方向发生了了交换 + /* + ------------------- N 方向---------------------- + |0 1 ... 7 16 ... 31 40 ... 47 56... 64 8 .. 15 32 ... 39 + | + | + k + 方向 + | + | + | + */ + col = (laneId / 16) * 2 + ni * 128 + (warpId % 2) * 8 + (warpId / 2) * 64; + for (int i = 0; i < 4; i ++) { + + #if defined(__gfx938__) + auto d = __builtin_hcu_cvt_pk_bf16_f32(0, acc_o(i, mi, ni), 0, acc_o(i + 4, mi, ni), 0); + auto res = reinterpret_cast(d); + #else + result_type res; + Element e0, e1; + e0.storage = float2bf16(acc_o(i, mi, ni)); + e1.storage = float2bf16(acc_o(i + 4, mi, ni)); + res[0] = e0; + res[1] = e1; + #endif + // gO(row, col) = res[0]; + // gO(row, col + 1) = res[1]; + *(result_type*)(&gO(row, col)) = res; + col += 16; + // for (int j = 0; j < 2; j++) { + // gO(row, col) = rO(i * 2 + j, mi, ni); + // col += 4; + // } + // col += 8; + } + // for (int ei = 0; ei < size<0>(acc_o); ++ei) { + // gO(row, col) = rO(ei, mi, ni); + // col += 4; + // } + } + gSoftmaxLse[row] = lse(mi); + } + + + // if (s_q_idx == 1) + // { + // printf(" %.2f \n", lse(mi)); + // } + + + // gMax_logits[row] = softmax.row_max(mi) * params.sm_scale_div_log2; + + } + } + + + } else { + int start_head_idx = head_block_idx*BLOCK_M; + Tensor lse = softmax.template normalize_softmax_lse(acc_o, sRow_sum_reduce_buffer, params.sm_scale); + int n_split_idx = batch_idx == sched_meta.begin_req_idx ? sched_meta.begin_split_idx : 0; + int split_idx = __ldg(params.num_splits_ptr+batch_idx) + n_split_idx; + float* oaccum_ptr = (float*)params.o_accum + split_idx*params.stride_o_accum_split + s_q_idx*params.stride_o_accum_s_q + start_head_idx*params.stride_o_accum_h_q; // (BLOCK_M, HEAD_DIM_V) : (params.stride_o_accum_h_q, 1) + Tensor gOaccum = make_tensor(make_gmem_ptr(oaccum_ptr), make_layout( + Shape, Int>{}, + make_stride(params.stride_o_accum_h_q, _1{}) + )); + float* gSoftmaxLseAccum = (float*)params.lse_accum + split_idx*params.stride_lse_accum_split + s_q_idx*params.stride_lse_accum_s_q + start_head_idx; // (BLOCK_M) : (1) + { + // auto rO = flash::convert_type(acc_o); + int row, col; + const int warpId = tidx / 64; + const int laneId = tidx % 64; + for (int mi = 0; mi < size<1>(acc_o); ++mi) { + row = mi * BLOCK_M + laneId % 16; + if (row < params.h_q) { + for (int ni = 0; ni < size<2>(acc_o); ++ni) { + // col = (laneId / 16) + ni * 128 + warpId * 32 ; + // for (int ei = 0; ei < size<0>(acc_o); ++ei) { + // gOaccum(row, col) = acc_o(ei, mi, ni); + // col += 4; + // } + col = (laneId / 16) * 2 + ni * 128 + (warpId % 2) * 8 + (warpId / 2) * 64; + for (int i = 0; i < 4; i ++) { + gOaccum(row, col) = acc_o(i, mi, ni); + gOaccum(row, col + 1) = acc_o(i + 4, mi, ni); + // for (int j = 0; j < 2; j++) { + // gOaccum(row, col) = acc_o(i * 2 + j, mi, ni); + // col += 4; + // } + col += 16; + } + } + + gSoftmaxLseAccum[row] = lse(mi); + } + + // gMax_logits[row] = softmax.row_max(mi) * params.sm_scale_div_log2; + + } + } + } + +} + + +template +__device__ void KernelTemplate::devfunc(const SparseAttnDecodeParams ¶ms) { + const int partition_idx = blockIdx.z; + DecodingSchedMeta sched_meta = params.tile_scheduler_metadata_ptr[partition_idx]; + + if (sched_meta.begin_req_idx >= params.b) return; + for (int batch_idx = sched_meta.begin_req_idx; batch_idx <= sched_meta.end_req_idx; ++batch_idx) { + // if (threadIdx.x == 0) + // { + // printf(" batch_idx = %d end_req_idx = %d \n ", batch_idx, sched_meta.end_req_idx); + // } + if (batch_idx > sched_meta.begin_req_idx) { + __syncthreads(); + } + compute_attn_1rowblock_splitkv_sparse_mla_fp8(params, sched_meta, batch_idx); + + } +} + +template +__global__ void __launch_bounds__(Kernel::NUM_THREADS, 1) +flash_fwd_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams params) { +#if defined(__gfx936__) || defined(__gfx938__) + Kernel::devfunc(params); +#endif +} + +template +void KernelTemplate::run(const SparseAttnDecodeParams ¶ms) { + KU_ASSERT(params.h_kv == 1); + KU_ASSERT(params.topk % TOPK_BLOCK_SIZE == 0); + KU_ASSERT(params.d_qk == HEAD_DIM_K); + KU_ASSERT(params.d_v == HEAD_DIM_V); + // KU_ASSERT(params.h_q % BLOCK_M == 0); + if constexpr (MODEL_TYPE == ModelType::MODEL1) { + constexpr int BYTES_PER_TOKEN = HEAD_DIM_NOPE + 2*HEAD_DIM_ROPE + 8; + KU_ASSERT(params.stride_kv_row == BYTES_PER_TOKEN, "Each page block in KV cache must be contiguous for head64 sparse fp8 decoding attention in MODEL1"); // Each block must be contiguous + if (params.extra_kv != nullptr) { + KU_ASSERT(params.stride_extra_kv_row == BYTES_PER_TOKEN, "Each page block in extra KV cache must be contiguous for head64 sparse fp8 decoding attention in MODEL1"); // Each block must be contiguous + } + } else { + KU_ASSERT(params.extra_kv == nullptr, "V3.2 does not support extra KV cache"); + KU_ASSERT(params.topk_length == nullptr, "V3.2 does not support dynamic topk length"); + KU_ASSERT(params.stride_kv_row == 656); // number of bytes per token (512 fp8 + 4 float32 + 64 bfloat16) + } + auto mla_kernel = &flash_fwd_splitkv_mla_fp8_sparse_kernel>; + constexpr size_t smem_size = 32768; // lds复用 + // zhj debug + // printf("NUM_M_BLOCKS = %d smem_size = %d \n",NUM_M_BLOCKS, smem_size); + hipLaunchKernelGGL(( mla_kernel), dim3(dim3(NUM_M_BLOCKS, params.s_q, params.num_sm_parts)), dim3(NUM_THREADS), smem_size, params.stream, params); + +} + +template +void run_flash_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams ¶ms) { + KernelTemplate::run(params); +} + +} + + + diff --git a/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.h b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.h new file mode 100644 index 000000000..029bb2cbe --- /dev/null +++ b/third_party/flashmla_hygon/csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.h @@ -0,0 +1,17 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "params_hip.h" + +namespace gfx93::decode::sparse_fp8 { + +template +void run_flash_splitkv_mla_fp8_sparse_kernel(const SparseAttnDecodeParams ¶ms); + +} + diff --git a/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/common/common.h b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/common/common.h new file mode 100644 index 000000000..92459b7c2 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/common/common.h @@ -0,0 +1,8 @@ +#pragma once + +namespace kerutils {} + +#define KU_PRINTLN(fmt, ...) { cute::print(fmt, ##__VA_ARGS__); print("\n"); } + +namespace ku = kerutils; + diff --git a/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/common_hip.h b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/common_hip.h new file mode 100644 index 000000000..8fa1a674f --- /dev/null +++ b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/common_hip.h @@ -0,0 +1,76 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +/* +Common data types and macros that are used across the kerutils library. +*/ +#pragma once + +// #include +// #include + +#include +// #include +#include // For CUTE_DEVICE + +namespace kerutils { + +// // Cache hints +// enum class CacheHint { +// EVICT_FIRST, +// EVICT_NORMAL, +// EVICT_LAST, +// EVICT_UNCHANGED, +// NO_ALLOCATE +// }; + +// // Prefetch size +// enum class PrefetchSize { +// B64, +// B128, +// B256 +// }; + +// using nvbf16 = __hip_bfloat16; +// using nvbf16x2 = __hip_bfloat162; +// using nve4m3 = __nv_fp8_e4m3; +// using nve4m3x2 = __nv_fp8x2_e4m3; +// using nve4m3x4 = __nv_fp8x4_e4m3; + +using bf16 = cutlass::bfloat16_t; +// using transac_bar_t = cutlass::arch::ClusterTransactionBarrier; + +} + +// #if (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ >= 800)) +// #define KERUTILS_ENABLE_SM80 +// #elif (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ < 800)) +// static_assert(false, "kerutils doesn't support SM architectures below SM80"); +// #endif + +// #if (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ >= 900)) +// #define KERUTILS_ENABLE_SM90 +// #endif + +// #if (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ >= 900 && __FPT_DTK_ARCH__ < 1000)) +// #define KERUTILS_ENABLE_SM90A +// #endif + +// #if (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ >= 1000)) +// #define KERUTILS_ENABLE_SM100 +// #endif + +// #if (defined(__FPT_DTK_ARCH__) && (__FPT_DTK_ARCH__ >= 1000 && __FPT_DTK_ARCH__ < 1200)) +// #define KERUTILS_ENABLE_SM100A +// #endif + +// #if (defined(__CLION_IDE__) || defined(__VSCODE_IDE__)) +// #define KERUTILS_ENABLE_SM80 +// #define KERUTILS_ENABLE_SM90 +// #define KERUTILS_ENABLE_SM90A +// #define KERUTILS_ENABLE_SM100 +// #define KERUTILS_ENABLE_SM100A +// #endif diff --git a/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/device_hip.cuh b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/device_hip.cuh new file mode 100644 index 000000000..b05d90bb7 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/device/device_hip.cuh @@ -0,0 +1,19 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "kerutils/common/common.h" + +#include "common_hip.h" +// #include "sm80/intrinsics.cuh" +// #include "sm80/helpers.cuh" +// #include "gfx93/intrinsics.cuh" +// #include "gfx93/helpers.cuh" +// #include "sm100/intrinsics.cuh" +// #include "sm100/helpers.cuh" +// #include "sm100/gemm.cuh" +// #include "sm100/tma_cta_group2_nosplit.cuh" diff --git a/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/host/host_hip.h b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/host/host_hip.h new file mode 100644 index 000000000..598b47802 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/host/host_hip.h @@ -0,0 +1,161 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include +#include +#include +#include + +#include +#include + +// #include + +#include "kerutils/common/common.h" + +namespace kerutils { + +class KUException final : public std::exception { + std::string message = {}; + +public: + template + explicit KUException(const char *name, const char* file, const int line, Args&&... args) { + std::ostringstream oss; + + oss << name << " error (" << file << ":" << line << "): "; + (oss << ... << args); + message = oss.str(); + } + + const char *what() const noexcept override { + return message.c_str(); + } +}; + +#define THROW_KU_EXCEPTION(name, ...) \ + throw kerutils::KUException(name, __FILE__, __LINE__, __VA_ARGS__) + +#define KU_CUDA_CHECK(call) \ +do { \ + hipError_t status_ = call; \ + if (status_ != hipSuccess) { \ + fprintf(stderr, "CUDA error (%s:%d): %s\n", __FILE__, __LINE__, hipGetErrorString(status_)); \ + THROW_KU_EXCEPTION("CUDA", "CUDA error: ", hipGetErrorString(status_)); \ + } \ +} while(0) + +#define KU_CUTLASS_CHECK(call) \ +do { \ + cutlass::Status status_ = call; \ + if (status_ != cutlass::Status::kSuccess) { \ + fprintf(stderr, "CUTLASS error (%s:%d): %d\n", __FILE__, __LINE__, static_cast(status_)); \ + THROW_KU_EXCEPTION("CUTLASS", "CUTLASS error: ", static_cast(status_)); \ + } \ +} while(0) + +// This `KU_ASSERT` is triggered no matter if the code is compiled with `-DNDEBUG` or not. +#define KU_ASSERT(cond, ...) \ + do { \ + if (not (cond)) { \ + fprintf(stderr, "Assertion `%s` failed (%s:%d): ", #cond, __FILE__, __LINE__); \ + if constexpr (sizeof(#__VA_ARGS__) > 1) { \ + fprintf(stderr, ", " __VA_ARGS__); \ + } \ + fprintf(stderr, "\n"); \ + THROW_KU_EXCEPTION("Assertion", "Assertion `", #cond, "` failed."); \ + } \ + } while(0) + +#define KU_CHECK_KERNEL_LAUNCH() KU_CUDA_CHECK(hipGetLastError()) + +template +inline __host__ __device__ constexpr T ceil_div(const T &a, const T &b) { + return (a + b - 1) / b; +} + +template +inline __host__ __device__ constexpr T ceil(const T &a, const T &b) { + return (a + b - 1) / b * b; +} + +// // A wrapper for make_tensor_map +// static inline CUtensorMap make_tensor_map( +// const std::vector &size, +// const std::vector &strides, // PAY ATTENTION: In BYTES +// const std::vector &box_size, +// void* global_ptr, +// CUtensorMapDataType data_type, +// CUtensorMapSwizzle swizzle_mode, +// CUtensorMapL2promotion l2_promotion, +// CUtensorMapInterleave interleave_mode = CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE, +// CUtensorMapFloatOOBfill oob_fill = CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE, +// const std::vector &element_strides_ = {} +// ) { +// int dim = size.size(); +// KU_ASSERT(dim >= 1); + +// std::vector element_strides; +// if (element_strides_.empty()) { +// for (int i = 0; i < dim; ++i) +// element_strides.push_back(1); +// } else { +// element_strides = element_strides_; +// } +// KU_ASSERT(strides.size() == (uint32_t)dim-1 && box_size.size() == (uint32_t)dim && element_strides.size() == (uint32_t)dim); + +// CUtensorMap result; +// hipError_t ret_code = CUTLASS_CUDA_DRIVER_WRAPPER_CALL(cuTensorMapEncodeTiled)( +// &result, +// data_type, +// dim, +// global_ptr, +// size.data(), +// strides.data(), +// box_size.data(), +// element_strides.data(), +// interleave_mode, +// swizzle_mode, +// l2_promotion, +// oob_fill +// ); +// if (ret_code != hipError_t::hipSuccess) { +// auto print_vector = [&](auto t, const char* fmt, const char end='\n') { +// for (auto elem : t) { +// printf(fmt, elem); +// } +// printf("%c", end); +// }; +// fprintf(stderr, "Failed to create tensormap\n"); +// fprintf(stderr, "Dim: %d\n", dim); +// printf("size: "); print_vector(size, "%lu "); +// printf("strides: "); print_vector(strides, "%lu "); +// printf("box_size: "); print_vector(box_size, "%u "); +// printf("element_strides: "); print_vector(element_strides, "%u "); +// printf("global ptr: 0x%lx\n", (int64_t)global_ptr); +// printf("data_type: %d\n", (int)data_type); +// printf("swizzle_mode: %d\n", (int)swizzle_mode); +// printf("l2_promotion: %d\n", (int)l2_promotion); +// printf("interleave_mode: %d\n", (int)interleave_mode); +// printf("oob_fill: %d\n", (int)oob_fill); +// KU_ASSERT(false); +// } +// return result; +// } + +// // Given strides (in number of elements), this function converts their datatype in uint64_t and then multiplies by elem_size +// template +// static inline std::vector make_stride_helper(const std::vector &strides_in_elems, size_t elem_size) { +// std::vector res; +// for (auto stride : strides_in_elems) { +// res.push_back(((uint64_t)stride) * elem_size); +// } +// return res; +// } + +} \ No newline at end of file diff --git a/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/kerutils_hip.cuh b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/kerutils_hip.cuh new file mode 100644 index 000000000..1b29924e8 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/kerutils/include/kerutils/kerutils_hip.cuh @@ -0,0 +1,10 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "host/host_hip.h" +#include "device/device_hip.cuh" diff --git a/third_party/flashmla_hygon/csrc/params_hip.h b/third_party/flashmla_hygon/csrc/params_hip.h new file mode 100644 index 000000000..8010bb899 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/params_hip.h @@ -0,0 +1,208 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#pragma once + +#include "cutlass/bfloat16.h" + +enum class ModelType { + V32, + MODEL1 +}; + +struct alignas(32) DecodingSchedMeta { + int begin_req_idx, end_req_idx; // Both inclusive + int begin_block_idx, end_block_idx; // Inclusive, exclusive + int begin_split_idx; + int is_first_req_splitted, is_last_req_splitted; + int _pad[1]; +}; +static constexpr int DecodingSchedMetaSize = sizeof(DecodingSchedMeta); + +struct DenseAttnDecodeParams { // TODO Change name to DenseAttnDecodeParams + using index_t = int64_t; + + int b; // batch size + int s_q; + int q_seq_per_hk; // The number of q(s) per KV head, = h_q / h_k * s_q + int d, d_v; // K/V dimension + int h_q, h_k; // The number of Q/K heads + int num_blocks; // Number of blocks in total + int q_head_per_hk; // The number of q_head(s) per KV head, = h_q / h_k + bool is_causal; + float scale_softmax, scale_softmax_log2; + + void *__restrict__ q_ptr; + void *__restrict__ k_ptr; + void *__restrict__ o_ptr; + float *__restrict__ softmax_lse_ptr; + + index_t q_batch_stride; + index_t k_batch_stride; + index_t o_batch_stride; + index_t q_row_stride; + index_t k_row_stride; + index_t o_row_stride; + index_t q_head_stride; + index_t k_head_stride; + index_t o_head_stride; + + int *__restrict__ block_table; + index_t block_table_batch_stride; + int page_block_size; + int *__restrict__ seqlens_k_ptr; + + DecodingSchedMeta *__restrict__ tile_scheduler_metadata_ptr; + int num_sm_parts; + int *__restrict__ num_splits_ptr; + + int total_num_splits; + float *__restrict__ softmax_lseaccum_ptr; + float *__restrict__ oaccum_ptr; + + hipStream_t stream; + + bool use_split_kv; + int partition_block_nums; + bool is_gfx928; +}; + +struct DenseAttnDecodeParams_fp8 : public DenseAttnDecodeParams { + float* __restrict__ descale_q_ptr = nullptr; + float* __restrict__ descale_k_ptr = nullptr; +}; + +struct SparseAttnDecodeParams { + int b, s_q; + int h_q, h_kv; + int d_qk, d_v; + float sm_scale, sm_scale_div_log2; + int num_blocks, page_block_size, topk; + ModelType model_type; + + cutlass::bfloat16_t* __restrict__ q; // [b, s_q, h_q, d_qk] + cutlass::bfloat16_t* __restrict__ kv; // [num_blocks, page_block_size, d_qk] + int* __restrict__ indices; // [b, s_q, topk] + int* __restrict__ topk_length; // [b], may be nullptr + float* __restrict__ attn_sink; // [h_q], may be nullptr + + float* __restrict__ lse; // [b, s_q, h_q] + cutlass::bfloat16_t* __restrict__ out; // [b, s_q, h_q, d_v] + + int extra_num_blocks, extra_page_block_size, extra_topk; + cutlass::bfloat16_t* __restrict__ extra_kv; // [extra_num_blocks, extra_page_block_size, d_qk] + int* __restrict__ extra_indices; // [b, s_q, extra_topk] + int* __restrict__ extra_topk_length; // [b], may be nullptr + + int stride_q_b, stride_q_s_q, stride_q_h_q; + int stride_kv_block, stride_kv_row; + int stride_indices_b, stride_indices_s_q; + int stride_lse_b, stride_lse_s_q; + int stride_o_b, stride_o_s_q, stride_o_h_q; + int stride_extra_kv_block, stride_extra_kv_row; + int stride_extra_indices_b, stride_extra_indices_s_q; + + hipStream_t stream; + + // SplitKV-related parameters + float* __restrict__ lse_accum; // [num_splits, s_q, h_q] + float* __restrict__ o_accum; // [num_splits, s_q, h_q, d_v] + int stride_lse_accum_split, stride_lse_accum_s_q; + int stride_o_accum_split, stride_o_accum_s_q, stride_o_accum_h_q; + DecodingSchedMeta* __restrict__ tile_scheduler_metadata_ptr; // [num_sm_parts, ], contiguous + int* __restrict__ num_splits_ptr; // [batch_size+1, ], contiguous + int num_sm_parts; +}; + +struct CombineParams { + int b, s_q, h_q, d_v; + + float* __restrict__ lse; // [b, s_q, h_q] + void* __restrict__ out; // [b, s_q, h_q, d_v] + int stride_lse_b, stride_lse_s_q; + int stride_o_b, stride_o_s_q, stride_o_h_q; + + float* __restrict__ lse_accum; // [num_splits, s_q, h_q] + float* __restrict__ o_accum; // [num_splits, s_q, h_q, d_v] + int stride_lse_accum_split, stride_lse_accum_s_q; + int stride_o_accum_split, stride_o_accum_s_q, stride_o_accum_h_q; + + DecodingSchedMeta* __restrict__ tile_scheduler_metadata_ptr; // [num_sm_parts, ], contiguous + int* __restrict__ num_splits_ptr; // [batch_size+1, ], contiguous + int num_sm_parts; + + float* attn_sink; // [h_q], may be nullptr + + hipStream_t stream; + + + bool use_split_kv; + int num_splits; + int *__restrict__ seqlens_k_ptr; + int partition_block_nums; +}; + +struct GetDecodeSchedMetaParams { + int b; // batch size + int s_q; + int block_size_n; + int fixed_overhead_num_blocks; + + int topk, extra_topk; // -1 if sparse attention (or extra topk) is disabled + int *__restrict__ topk_length, *__restrict__ extra_topk_length; + + int *__restrict__ seqlens_k_ptr; // Only necessary for dense attention + + DecodingSchedMeta *__restrict__ tile_scheduler_metadata_ptr; + int *__restrict__ num_splits_ptr; + int num_sm_parts; + + hipStream_t stream; +}; + +struct SparseAttnFwdParams { + int s_q, s_kv, h_q, h_kv, d_qk, d_v, topk; + float sm_scale, sm_scale_div_log2; + + // Input tensors + cutlass::bfloat16_t* __restrict__ q; // [s_q, h_q, d_qk] + cutlass::bfloat16_t* __restrict__ kv; // [s_kv, h_kv, d_qk] + int* __restrict__ indices; // [s_q, h_kv, topk] + float* __restrict__ attn_sink; // [h_q], may be nullptr + int* __restrict__ topk_length; // [s_q], may be nullptr + + // Strides + int stride_q_s_q; int stride_q_h_q; + int stride_kv_s_kv; int stride_kv_h_kv; + int stride_indices_s_q; int stride_indices_h_kv; + + // Output tensors + cutlass::bfloat16_t* __restrict__ out; // [s_q, h_q, d_v] + float* __restrict__ max_logits; // [s_q, h_q] + float* __restrict__ lse; // [s_q, h_q] + + int num_sm; + hipStream_t stream; +}; + +// We have some kernels that implement both prefill and decode modes in a single kernel (with different template instantiations). The following enum helps to distinguish the modes. +enum class SparseAttnFwdMode { + Prefill, // Normal prefill mode + DecodeWithSplitKV, // To trigger decoding mode for kernels that support both prefill and decode +}; + +template +inline constexpr bool is_decode_v = std::bool_constant::value; + +template +using SparseFwdArgT = std::conditional_t, SparseAttnDecodeParams, SparseAttnFwdParams>; + +// enum class Fp8KVCacheDataType { +// kAuto = 0, +// kFp8E4M3 = 1, +// kFp8E5M2 = 2, +// kInt8 = 3, +// }; diff --git a/third_party/flashmla_hygon/csrc/softmax_hip.h b/third_party/flashmla_hygon/csrc/softmax_hip.h new file mode 100644 index 000000000..3fb045253 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/softmax_hip.h @@ -0,0 +1,704 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +// Adapted from https://github.com/Dao-AILab/flash-attention/blob/main/csrc/flash_attn/src/softmax.h + +#pragma once + +#include + +#include +#include + +#include "utils_hip.h" + +namespace flash { + +using namespace cute; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +__device__ __forceinline__ void thread_reduce_(Tensor const &tensor, Tensor &summary, Operator &op) { + static_assert(Layout0::rank == 2, "Only support 2D Tensor"); + static_assert(Layout1::rank == 1, "Only support 1D Tensor"); + CUTE_STATIC_ASSERT_V(size<0>(summary) == size<0>(tensor)); + #pragma unroll + for (int mi = 0; mi < size<0>(tensor); mi++) { + summary(mi) = zero_init ? tensor(mi, 0) : op(summary(mi), tensor(mi, 0)); + #pragma unroll + for (int ni = 1; ni < size<1>(tensor); ni++) { + summary(mi) = op(summary(mi), tensor(mi, ni)); + } + } +} + +template +__device__ __forceinline__ void quad_allreduce_(Tensor &dst, Tensor &src, Operator &op) { + CUTE_STATIC_ASSERT_V(size(dst) == size(src)); + #pragma unroll + for (int i = 0; i < size(dst); i++){ + dst(i) = Allreduce<64>::run(src(i), op); + } +} + +template +__device__ __forceinline__ void warp_allreduce_(Tensor &dst, Tensor &smem_reduce, Operator &op) { + const int tidx = threadIdx.x; + const int row = tidx % 16; + const int col = tidx / 64; + const int warp_id = tidx / 64; + // static_assert(size(dst) == 1); + // 这里两种写法,一种是写连续,读不连续;另一种是读不连续,写连续。如何权衡?性能影响不大 + if ((tidx % 64) / 16 == 0) + // if (tidx >= warp_id * 64 && tidx <= warp_id * 64 + 16) + { + // smem_reduce(row + warp_id * 16) = dst(0); + smem_reduce(row * 4 + warp_id * 1) = dst(0); + // smem_reduce(row, col) = dst(0); + } + __syncthreads(); + // if (tidx < 16) + // { + // smem_reduce(row + 64) = op(op(smem_reduce(row * 4), smem_reduce(row * 4 + 1)), op(smem_reduce(row * 4 + 2), smem_reduce(row * 4 + 3))); + // } + // __syncthreads(); + // dst(0) = smem_reduce(row + 64); + dst(0) = op(op(smem_reduce(row * 4), smem_reduce(row * 4 + 1)), op(smem_reduce(row * 4 + 2), smem_reduce(row * 4 + 3))); +} + +template +__device__ __forceinline__ void warp_allreduce_tp1(Tensor &dst, Tensor &smem_reduce, Operator &op) { + const int tidx = threadIdx.x; + const int col = (tidx % 64) / 16; + const int warp_id = tidx / 64; + const int row = tidx % 16 + (warp_id % 4) * 16; + // 0-4 1-5 2-6 3-7 + if (col == 0) { + // printf("sum %d %d %d %d %.2f \n", row * 2 + (warp_id / 4), row, tidx, warp_id, dst(0)); + smem_reduce[row * 2 + (warp_id / 4)] = dst[0]; + } + __syncthreads(); + // if (col == 0 && warp_id < 4) { + // // printf("sum %d %d %d %.2f %.2f \n", row, tidx, warp_id, smem_reduce[row * 2], smem_reduce[row * 2 + warp_id / 4]); + // smem_reduce[128 + row] = op(smem_reduce[row * 2], smem_reduce[row * 2 + 1]); + // } + // __syncthreads(); + // dst(0) = smem_reduce(128 + row); + dst(0) = op(smem_reduce[row * 2], smem_reduce[row * 2 + 1]); +} +template +__device__ __forceinline__ void warp_allreduce_tp4(Tensor &dst, Tensor &smem_reduce, Operator &op) { + const int tidx = threadIdx.x; + const int col = (tidx % 64) / 16; + const int warp_id = tidx / 64; + const int row = tidx % 16 + (warp_id % 2) * 16; + // 0-4 1-5 2-6 3-7 + if (col == 0) { + // printf("sum %d %d %d %d %.2f \n", row * 2 + (warp_id / 4), row, tidx, warp_id, dst(0)); + smem_reduce[row * 2 + (warp_id / 2)] = dst[0]; + } + __syncthreads(); + if (col == 0 && warp_id < 2) { + // printf("sum %d %d %d %.2f %.2f \n", row, tidx, warp_id, smem_reduce[row * 2], smem_reduce[row * 2 + warp_id / 4]); + smem_reduce[row + 64] = op(smem_reduce[row * 2], smem_reduce[row * 2 + 1]); + } + __syncthreads(); + dst(0) = smem_reduce(row + 64); +} + + +template +__device__ __forceinline__ void reduce_(Tensor const& tensor, Tensor &summary, Operator &op) { + thread_reduce_(tensor, summary, op); + quad_allreduce_(summary, summary, op); +} + +template +__device__ __forceinline__ void reduce_max(Tensor const& tensor, Tensor &max){ + MaxOp max_op; + reduce_(tensor, max, max_op); +} + +template +__device__ __forceinline__ void reduce_sum(Tensor const& tensor, Tensor &sum){ + SumOp sum_op; + thread_reduce_(tensor, sum, sum_op); +} + +// Apply the exp to all the elements. +template +__forceinline__ __device__ void scale_apply_exp2(Tensor &tensor, Tensor const &max, const float scale) { + static_assert(Layout0::rank == 2, "Only support 2D Tensor"); + static_assert(Layout1::rank == 1, "Only support 1D Tensor"); + CUTE_STATIC_ASSERT_V(size<0>(max) == size<0>(tensor)); + #pragma unroll + for (int mi = 0; mi < size<0>(tensor); ++mi) { + // If max is -inf, then all elements must have been -inf (possibly due to masking). + // We don't want (-inf - (-inf)) since that would give NaN. + // If we don't have float around M_LOG2E the multiplication is done in fp64. + const float max_scaled = max(mi) == -INFINITY ? 0.f : max(mi) * (Scale_max ? scale : float(M_LOG2E)); + #pragma unroll + for (int ni = 0; ni < size<1>(tensor); ++ni) { + // Instead of computing exp(x - max), we compute exp2(x * log_2(e) - + // max * log_2(e)) This allows the compiler to use the ffma + // instruction instead of fadd and fmul separately. + // The following macro will disable the use of fma. + // See: https://github.com/pytorch/pytorch/issues/121558 for more details + // This macro is set in PyTorch and not FlashAttention + #if 0 + #ifdef UNFUSE_FMA + tensor(mi, ni) = exp2f(__fmul_rn(tensor(mi, ni), scale) - max_scaled); + #else + tensor(mi, ni) = exp2f(tensor(mi, ni) * scale - max_scaled); + #endif + #else + tensor(mi, ni) = __builtin_amdgcn_exp2f(tensor(mi, ni) * scale - max_scaled); + #endif + } + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct Softmax { + using TensorT = decltype(make_tensor(Shape>{})); + TensorT row_max, row_sum; + __forceinline__ __device__ Softmax() {}; + + template + __forceinline__ __device__ void softmax_rescale_o(Tensor0 &acc_s, Tensor1 &acc_o, Tensor2 &sRow_max_reduce_buffer, float softmax_scale_log2) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if (Is_first) { + flash::template reduce_max(scores, row_max); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_max, sRow_max_reduce_buffer, max_op); + } + else + { + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_max, sRow_max_reduce_buffer, max_op); + } + else + { + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + } // Reshape acc_o from (MMA=4, MMA_M, MMA_K) to (nrow=(2, MMA_M), ncol=(2, MMA_K)) + Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size(row_max); ++mi) { + float scores_max_cur = !Check_inf + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + + // if (blockIdx.x == 0 && threadIdx.x == 0) + // { + // printf("threadIdx.x %.2f, scores_scale = %.4f\n",row_sum(mi), scores_scale ); + // } + row_sum(mi) *= scores_scale; + #pragma unroll + for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scores_scale; } + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + // if (block0()) + // { + // printf("normalize_softmax_lse %.4f\n", row_sum(0)); + // } + }; + + template + __forceinline__ __device__ void softmax_rescale_o_fp8(Tensor0 &acc_s, Tensor2 &sRow_max_reduce_buffer, float softmax_scale_log2, + v4f& c0_0, v4f& c0_1, v4f& c1_0, v4f& c1_1, v4f& c2_0, v4f& c2_1, v4f& c3_0, v4f& c3_1 + ) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if (Is_first) { + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + // Reshape acc_o from (MMA=4, MMA_M, MMA_K) to (nrow=(2, MMA_M), ncol=(2, MMA_K)) + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(1 == kNRows); + // #pragma unroll + // for (int mi = 0; mi < size(row_max); ++mi) + { + int mi = 0; + float scores_max_cur = !Check_inf + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + row_sum(mi) *= scores_scale; + // #pragma unroll + // for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scores_scale; } + c0_0.x *= scores_scale; c0_0.y *= scores_scale; c0_0.z *= scores_scale; c0_0.w *= scores_scale; + c0_1.x *= scores_scale; c0_1.y *= scores_scale; c0_1.z *= scores_scale; c0_1.w *= scores_scale; + c1_0.x *= scores_scale; c1_0.y *= scores_scale; c1_0.z *= scores_scale; c1_0.w *= scores_scale; + c1_1.x *= scores_scale; c1_1.y *= scores_scale; c1_1.z *= scores_scale; c1_1.w *= scores_scale; + + c2_0.x *= scores_scale; c2_0.y *= scores_scale; c2_0.z *= scores_scale; c2_0.w *= scores_scale; + c2_1.x *= scores_scale; c2_1.y *= scores_scale; c2_1.z *= scores_scale; c2_1.w *= scores_scale; + c3_0.x *= scores_scale; c3_0.y *= scores_scale; c3_0.z *= scores_scale; c3_0.w *= scores_scale; + c3_1.x *= scores_scale; c3_1.y *= scores_scale; c3_1.z *= scores_scale; c3_1.w *= scores_scale; + + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + }; + + template + __forceinline__ __device__ TensorT normalize_softmax_lse(Tensor0 &acc_o, Tensor1& sRow_sum_reduce_buffer, float softmax_scale, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_sum, sRow_sum_reduce_buffer, sum_op); + } + else + { + flash::template warp_allreduce_(row_sum, sRow_sum_reduce_buffer, sum_op); + } + // if (block0()) + // { + // printf("is_tp1 %d %d normalize_softmax_lse %.4f\n",is_tp1, threadIdx.x, row_sum(0)); + // } + TensorT lse = make_fragment_like(row_sum); + Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size<0>(acc_o_rowcol); ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : 1.f / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + #pragma unroll + for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scale; } + } + return lse; + }; + + template + __forceinline__ __device__ void softmax_rescale_o_prefill(Tensor0 &acc_s, Tensor1 &acc_o, Tensor2 &sRow_max_reduce_buffer, float softmax_scale_log2) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if (Is_first) { + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + // Reshape acc_o from (MMA=4, MMA_M, MMA_K) to (nrow=(2, MMA_M), ncol=(2, MMA_K)) + Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size(row_max); ++mi) { + float scores_max_cur = !true + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); + + +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + // if (blockIdx.x == 0 && threadIdx.x == 0) + // { + // printf("threadIdx.x %.2f, scores_scale = %.4f\n",row_sum(mi), scores_scale ); + // } + row_sum(mi) *= scores_scale; + #pragma unroll + for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scores_scale; } + } + + // if (blockIdx.x == 2) + // { + // printf("threadIdx.x %.2f \n",row_sum(mi) ); + // } + + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + }; + + template + __forceinline__ __device__ TensorT normalize_softmax_lse_prefill(Tensor0 &acc_o, Tensor1& sRow_sum_reduce_buffer, float softmax_scale, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + flash::template warp_allreduce_(row_sum, sRow_sum_reduce_buffer, sum_op); + TensorT lse = make_fragment_like(row_sum); + Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size<0>(acc_o_rowcol); ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : 1.f / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + #pragma unroll + for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scale; } + } + return lse; + }; + + template + __forceinline__ __device__ TensorT normalize_softmax_lse_fp8(Tensor0 &acc_o, Tensor1& sRow_sum_reduce_buffer, float softmax_scale,float descale_v, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + flash::template warp_allreduce_(row_sum, sRow_sum_reduce_buffer, sum_op); + + TensorT lse = make_fragment_like(row_sum); + Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size<0>(acc_o_rowcol); ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : descale_v / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + #pragma unroll + for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scale; } + } + return lse; + }; + + template + __forceinline__ __device__ void softmax_rescale_o_fp8_tp1(Tensor0 &acc_s, Tensor2 &sRow_max_reduce_buffer, float softmax_scale_log2, + v4f *acco_f32 + ) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if constexpr (Is_first) { + flash::template reduce_max(scores, row_max); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_max, sRow_max_reduce_buffer, max_op); + } + else + { + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_max, sRow_max_reduce_buffer, max_op); + } + else + { + flash::template warp_allreduce_(row_max, sRow_max_reduce_buffer, max_op); + } // Reshape acc_o from (MMA=4, MMA_M, MMA_K) to (nrow=(2, MMA_M), ncol=(2, MMA_K)) + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(1 == kNRows); + // #pragma unroll + // for (int mi = 0; mi < size(row_max); ++mi) + { + int mi = 0; + float scores_max_cur = !Check_inf + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + row_sum(mi) *= scores_scale; + for (int i = 0; i < 16; i++) + { + acco_f32[i].x *= scores_scale; + acco_f32[i].y *= scores_scale; + acco_f32[i].z *= scores_scale; + acco_f32[i].w *= scores_scale; + } + // #pragma unroll + // for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scores_scale; } + // c0_0.x *= scores_scale; c0_0.y *= scores_scale; c0_0.z *= scores_scale; c0_0.w *= scores_scale; + // c0_1.x *= scores_scale; c0_1.y *= scores_scale; c0_1.z *= scores_scale; c0_1.w *= scores_scale; + // c1_0.x *= scores_scale; c1_0.y *= scores_scale; c1_0.z *= scores_scale; c1_0.w *= scores_scale; + // c1_1.x *= scores_scale; c1_1.y *= scores_scale; c1_1.z *= scores_scale; c1_1.w *= scores_scale; + + // c2_0.x *= scores_scale; c2_0.y *= scores_scale; c2_0.z *= scores_scale; c2_0.w *= scores_scale; + // c2_1.x *= scores_scale; c2_1.y *= scores_scale; c2_1.z *= scores_scale; c2_1.w *= scores_scale; + // c3_0.x *= scores_scale; c3_0.y *= scores_scale; c3_0.z *= scores_scale; c3_0.w *= scores_scale; + // c3_1.x *= scores_scale; c3_1.y *= scores_scale; c3_1.z *= scores_scale; c3_1.w *= scores_scale; + + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + }; + template + __forceinline__ __device__ void softmax_rescale_o_fp8_tp4(Tensor0 &acc_s, Tensor2 &sRow_max_reduce_buffer, float softmax_scale_log2, + v4f *acco_f32 + ) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if constexpr (Is_first) { + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_tp4(row_max, sRow_max_reduce_buffer, max_op); + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + flash::template warp_allreduce_tp4(row_max, sRow_max_reduce_buffer, max_op); + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + static_assert(1 == kNRows); + // #pragma unroll + // for (int mi = 0; mi < size(row_max); ++mi) + { + int mi = 0; + float scores_max_cur = !Check_inf + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + row_sum(mi) *= scores_scale; + for (int i = 0; i < 16; i++) + { + acco_f32[i].x *= scores_scale; + acco_f32[i].y *= scores_scale; + acco_f32[i].z *= scores_scale; + acco_f32[i].w *= scores_scale; + } + // #pragma unroll + // for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scores_scale; } + // c0_0.x *= scores_scale; c0_0.y *= scores_scale; c0_0.z *= scores_scale; c0_0.w *= scores_scale; + // c0_1.x *= scores_scale; c0_1.y *= scores_scale; c0_1.z *= scores_scale; c0_1.w *= scores_scale; + // c1_0.x *= scores_scale; c1_0.y *= scores_scale; c1_0.z *= scores_scale; c1_0.w *= scores_scale; + // c1_1.x *= scores_scale; c1_1.y *= scores_scale; c1_1.z *= scores_scale; c1_1.w *= scores_scale; + + // c2_0.x *= scores_scale; c2_0.y *= scores_scale; c2_0.z *= scores_scale; c2_0.w *= scores_scale; + // c2_1.x *= scores_scale; c2_1.y *= scores_scale; c2_1.z *= scores_scale; c2_1.w *= scores_scale; + // c3_0.x *= scores_scale; c3_0.y *= scores_scale; c3_0.z *= scores_scale; c3_0.w *= scores_scale; + // c3_1.x *= scores_scale; c3_1.y *= scores_scale; c3_1.z *= scores_scale; c3_1.w *= scores_scale; + + } + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + }; + + template + __forceinline__ __device__ TensorT normalize_softmax_lse_fp8_tp1(v4f *acco_f, Tensor1& sRow_sum_reduce_buffer, float softmax_scale,float descale_v, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + if constexpr (is_tp1) + { + flash::template warp_allreduce_tp1(row_sum, sRow_sum_reduce_buffer, sum_op); + } + else + { + flash::template warp_allreduce_(row_sum, sRow_sum_reduce_buffer, sum_op); + } + TensorT lse = make_fragment_like(row_sum); + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + // static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < 1; ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : descale_v / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + // #pragma unroll + // for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scale; } + for (int i = 0; i < 16; i++) + { + acco_f[i].x *= scale; + acco_f[i].y *= scale; + acco_f[i].z *= scale; + acco_f[i].w *= scale; + } + } + return lse; + }; + template + __forceinline__ __device__ TensorT normalize_softmax_lse_fp8_tp4(v4f *acco_f, Tensor1& sRow_sum_reduce_buffer, float softmax_scale,float descale_v, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + flash::template warp_allreduce_tp4(row_sum, sRow_sum_reduce_buffer, sum_op); + + TensorT lse = make_fragment_like(row_sum); + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + // static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < 1; ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : descale_v / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + // #pragma unroll + // for (int ni = 0; ni < size<1>(acc_o_rowcol); ++ni) { acc_o_rowcol(mi, ni) *= scale; } + for (int i = 0; i < 16; i++) + { + acco_f[i].x *= scale; + acco_f[i].y *= scale; + acco_f[i].z *= scale; + acco_f[i].w *= scale; + } + } + return lse; + }; + + template + __forceinline__ __device__ void softmax_rescale_o_prefill_4x1(Tensor0& scores, v4f* acc_o, float softmax_scale_log2) { + // Reshape acc_s from (MMA=4, MMA_M, MMA_N) to (nrow=(2, MMA_M), ncol=(2, MMA_N)) + MaxOp max_op; + // Tensor scores = make_tensor(acc_s.data(), flash::convert_layout_acc_rowcol(acc_s.layout())); + static_assert(decltype(size<0>(scores))::value == kNRows); + if constexpr(Is_first) { + flash::template reduce_max(scores, row_max); + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + flash::reduce_sum(scores, row_sum); + } else { + Tensor scores_max_prev = make_fragment_like(row_max); + cute::copy(row_max, scores_max_prev); + flash::template reduce_max(scores, row_max); + // Reshape acc_o from (MMA=4, MMA_M, MMA_K) to (nrow=(2, MMA_M), ncol=(2, MMA_K)) + // static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + #pragma unroll + for (int mi = 0; mi < size(row_max); ++mi) { + float scores_max_cur = !Check_inf + ? row_max(mi) + : (row_max(mi) == -INFINITY ? 0.0f : row_max(mi)); + + +#if 0 + float scores_scale = exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#else + float scores_scale = __builtin_amdgcn_exp2f((scores_max_prev(mi) - scores_max_cur) * softmax_scale_log2); +#endif + // if (blockIdx.x == 0 && threadIdx.x == 0) + // { + // printf("threadIdx.x %.2f, scores_scale = %.4f\n",row_sum(mi), scores_scale ); + // } + row_sum(mi) *= scores_scale; + for (int i = 0; i < 32; i++) + { + acc_o[i].x *= scores_scale; + acc_o[i].y *= scores_scale; + acc_o[i].z *= scores_scale; + acc_o[i].w *= scores_scale; + } + } + + // if (blockIdx.x == 2) + // { + // printf("threadIdx.x %.2f \n",row_sum(mi) ); + // } + + flash::scale_apply_exp2(scores, row_max, softmax_scale_log2); + // We don't do the reduce across threads here since we don't need to use the row_sum. + // We do that reduce at the end when we need to normalize the softmax. + flash::reduce_sum(scores, row_sum); + + } + // if (thread0()) + // { + // printf("max sum %.3f %.3f \n", row_max(0), row_sum(0)); + // } + }; + + template + __forceinline__ __device__ TensorT normalize_softmax_lse_prefill_4x1(v4f *acc_o, float softmax_scale, float rp_dropout=1.0) { + SumOp sum_op; + quad_allreduce_(row_sum, row_sum, sum_op); + // flash::template warp_allreduce_(row_sum, sRow_sum_reduce_buffer, sum_op); + TensorT lse = make_fragment_like(row_sum); + // Tensor acc_o_rowcol = make_tensor(acc_o.data(), flash::convert_layout_acc_rowcol(acc_o.layout())); + // static_assert(decltype(size<0>(acc_o_rowcol))::value == kNRows); + // if (thread0()) + // { + // printf(" %.3f %.3f \n", row_max(0), row_sum(0)); + // } + #pragma unroll + for (int mi = 0; mi < 1; ++mi) { + float sum = row_sum(mi); + float inv_sum = (sum == 0.f || sum != sum) ? 1.f : 1.f / sum; + lse(mi) = (sum == 0.f || sum != sum) ? (Split ? -INFINITY : INFINITY) : row_max(mi) * softmax_scale + __logf(sum); + float scale = !Is_dropout ? inv_sum : inv_sum * rp_dropout; + for (int i = 0; i < 32; i++) + { + acc_o[i].x *= scale; + acc_o[i].y *= scale; + acc_o[i].z *= scale; + acc_o[i].w *= scale; + } + } + return lse; + }; + +}; + + +} // namespace flash diff --git a/third_party/flashmla_hygon/csrc/utils_hip.h b/third_party/flashmla_hygon/csrc/utils_hip.h new file mode 100644 index 000000000..744cfdae0 --- /dev/null +++ b/third_party/flashmla_hygon/csrc/utils_hip.h @@ -0,0 +1,1674 @@ +// !!! This is a file automatically generated by hipify!!! +#ifndef __FPT_DTK_ARCH__ +#define __FPT_DTK_ARCH__ 520 +#define FPT_DTK_VERSION 11800 +#define FPT_DTKRT_VERSION 11080 +#endif +#include "hip/hip_runtime.h" +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "defines_hip.h" +#include "params_hip.h" +#define CHECK_CUDA(call) \ + do { \ + hipError_t status_ = call; \ + if (status_ != hipSuccess) { \ + fprintf(stderr, "CUDA error (%s:%d): %s\n", __FILE__, __LINE__, hipGetErrorString(status_)); \ + exit(1); \ + } \ + } while(0) + +#define CHECK_CUDA_KERNEL_LAUNCH() CHECK_CUDA(hipGetLastError()) + + +#define FLASH_ASSERT(cond) \ + do { \ + if (not (cond)) { \ + fprintf(stderr, "Assertion failed (%s:%d): %s\n", __FILE__, __LINE__, #cond); \ + exit(1); \ + } \ + } while(0) + + +#define FLASH_DEVICE_ASSERT(cond) \ + do { \ + if (not (cond)) { \ + printf("Assertion failed (%s:%d): %s\n", __FILE__, __LINE__, #cond); \ + asm volatile("s_trap 0 \n\t"); \ + } \ + } while(0) + +#define println(fmt, ...) { print(fmt, ##__VA_ARGS__); print("\n"); } + +template +__inline__ __host__ __device__ T ceil_div(const T &a, const T &b) { + return (a + b - 1) / b; +} + +#ifndef TRAP_ONLY_DEVICE_ASSERT +#define TRAP_ONLY_DEVICE_ASSERT(cond) \ +do { \ + if (not (cond)) \ + asm("trap;"); \ +} while (0) +#endif + +#ifndef TRAP_ONLY_DEVICE_ASSERT +#define TRAP_ONLY_DEVICE_ASSERT(cond) \ +do { \ + if (not (cond)) \ + asm("trap;"); \ +} while (0) +#endif + + +struct RingBufferState { + uint32_t cur_block_idx = 0u; + + __device__ __forceinline__ + void update() { + cur_block_idx += 1; + } + + template + __device__ __forceinline__ + std::pair get() const { + uint32_t stage_idx = cur_block_idx % NUM_STAGES; + bool phase = (cur_block_idx / NUM_STAGES) & 1; + return {stage_idx, phase}; + } + + __device__ __forceinline__ + RingBufferState offset_by(const int offset) const { + // Must guarantee no underflow + uint32_t new_block_idx = static_cast(static_cast(cur_block_idx) + offset); + RingBufferState new_state; + new_state.cur_block_idx = new_block_idx; + return new_state; + } +}; + +#define BOOL_SWITCH(COND, CONST_NAME, ...) \ + [&] { \ + if (COND) { \ + constexpr static bool CONST_NAME = true; \ + return __VA_ARGS__(); \ + } else { \ + constexpr static bool CONST_NAME = false; \ + return __VA_ARGS__(); \ + } \ + }() + + +namespace flash { + +using namespace cute; +//////////////////////////////////////////////////////////////////////////////////////////////////// + + +template +struct MaxOp { +__device__ __forceinline__ T operator()(T const & x, T const & y) { return x > y ? x : y; } +}; + +template <> +struct MaxOp { +// This is slightly faster +__device__ __forceinline__ float operator()(float const &x, float const &y) { return max(x, y); } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +template +struct SumOp { +__device__ __forceinline__ T operator()(T const & x, T const & y) { return x + y; } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +struct Allreduce { + static_assert(THREADS == 64 || THREADS == 32 || THREADS == 16 || THREADS == 8 || THREADS == 4 || THREADS == 2); + template + static __device__ __forceinline__ T run(T x, Operator &op) { + constexpr int OFFSET = THREADS / 2; + x = op(x, __shfl_xor(x, OFFSET, 64)); + return Allreduce::run(x, op); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template<> +struct Allreduce<1> { + // static_assert(THREADS == 64 || THREADS == 32 || THREADS == 16 || THREADS == 8 || THREADS == 4 || THREADS == 2); + template + static __device__ __forceinline__ T run(T x, Operator &op) { + return x; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template<> +struct Allreduce<32> { +template +static __device__ __forceinline__ T run(T x, Operator &op) { + x = op(x, __shfl_xor(x, 16, 64)); + return x; +} +}; + +template +__forceinline__ __device__ void copy(TiledCopy tiled_copy, Tensor const &S, + Tensor &D, Tensor const &identity_MN, + Tensor const &predicate_K, const int max_MN=0, int begin_k=0) { + CUTE_STATIC_ASSERT_V(rank(S) == Int<3>{}); + CUTE_STATIC_ASSERT_V(rank(D) == Int<3>{}); + CUTE_STATIC_ASSERT_V(size<0>(S) == size<0>(D)); // MMA + CUTE_STATIC_ASSERT_V(size<1>(S) == size<1>(D)); // MMA_M + CUTE_STATIC_ASSERT_V(size<2>(S) == size<2>(D)); // MMA_K + // There's no case where !Clear_OOB_K && Clear_OOB_MN + static_assert(!(Clear_OOB_MN && !Clear_OOB_K)); + #pragma unroll + for (int m = 0; m < size<1>(S); ++m) { + if (Is_even_MN || get<0>(identity_MN(0, m, 0)) < max_MN) { + #pragma unroll + for (int k = 0; k < size<2>(S); ++k) { + if (Is_even_K || predicate_K(k)) { + cute::copy(tiled_copy, S(_, m, k), D(_, m, k)); + } else if (Clear_OOB_K) { + cute::clear(D(_, m, k)); + } + } + } else if (Clear_OOB_MN) { + cute::clear(D(_, m, _)); + } + } +} + +template +__forceinline__ __device__ void __ds_read_m32x16_row_col_rrow(Tensor0& src, Tensor1& dst) +{ + + auto lds = reinterpret_cast<__fp16 *>(src.data().get()); + auto layout = src.layout(); + constexpr short offset = layout(0, row, col) * 2; + + auto d = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset); + + uint16_t * d_ptr = reinterpret_cast(&d); + uint16_t * dst_ptr = reinterpret_cast(&(dst(0, r_row, col))); + + dst_ptr[0] = d_ptr[0]; + dst_ptr[1] = d_ptr[1]; + dst_ptr[2] = d_ptr[2]; + dst_ptr[3] = d_ptr[3]; + dst_ptr[4] = d_ptr[4]; + dst_ptr[5] = d_ptr[5]; + dst_ptr[6] = d_ptr[6]; + dst_ptr[7] = d_ptr[7]; +} +template +__forceinline__ __device__ void __ds_read_m32x16_row_col_rrow_alt(Tensor0& src, Tensor1& dst) +{ + + auto lds = reinterpret_cast<__fp16 *>(src.data().get()); + auto layout = src.layout(); + constexpr short offset = layout(0, row, col) * 2; + + auto d = __builtin_amdgcn_ds_read_m32x16f16_alt((__attribute__((address_space(3))) __fp16*)(lds), offset); + + uint16_t * d_ptr = reinterpret_cast(&d); + uint16_t * dst_ptr = reinterpret_cast(&(dst(0, r_row, col))); + + dst_ptr[0] = d_ptr[0]; + dst_ptr[1] = d_ptr[1]; + dst_ptr[2] = d_ptr[2]; + dst_ptr[3] = d_ptr[3]; + dst_ptr[4] = d_ptr[4]; + dst_ptr[5] = d_ptr[5]; + dst_ptr[6] = d_ptr[6]; + dst_ptr[7] = d_ptr[7]; +} + +template +__forceinline__ __device__ void __ds_read_m32x16_row_col(Tensor0& src, Tensor1& dst) +{ + + auto lds = reinterpret_cast<__fp16 *>(src.data().get()); + auto layout = src.layout(); + constexpr short offset = layout(0, row, col) * 2; + + auto d = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset); + + uint16_t * d_ptr = reinterpret_cast(&d); + uint16_t * dst_ptr = reinterpret_cast(&(dst(0, row, col))); + + dst_ptr[0] = d_ptr[0]; + dst_ptr[1] = d_ptr[1]; + dst_ptr[2] = d_ptr[2]; + dst_ptr[3] = d_ptr[3]; + dst_ptr[4] = d_ptr[4]; + dst_ptr[5] = d_ptr[5]; + dst_ptr[6] = d_ptr[6]; + dst_ptr[7] = d_ptr[7]; +} +template +__forceinline__ __device__ void __ds_read_m32x16_row_col_alt(Tensor0& src, Tensor1& dst) +{ + + auto lds = reinterpret_cast<__fp16 *>(src.data().get()); + auto layout = src.layout(); + constexpr short offset = layout(0, row, col) * 2; + + auto d = __builtin_amdgcn_ds_read_m32x16f16_alt((__attribute__((address_space(3))) __fp16*)(lds), offset); + + uint16_t * d_ptr = reinterpret_cast(&d); + uint16_t * dst_ptr = reinterpret_cast(&(dst(0, row, col))); + + dst_ptr[0] = d_ptr[0]; + dst_ptr[1] = d_ptr[1]; + dst_ptr[2] = d_ptr[2]; + dst_ptr[3] = d_ptr[3]; + dst_ptr[4] = d_ptr[4]; + dst_ptr[5] = d_ptr[5]; + dst_ptr[6] = d_ptr[6]; + dst_ptr[7] = d_ptr[7]; +} + +inline __device__ float fp8e4m3_to_fp32(const fp8& input) { + const uint32_t w = (uint32_t)input << 24; + const uint32_t sign = w & UINT32_C(0x80000000); + const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF); + uint32_t renorm_shift = __clz(nonsign); + renorm_shift = renorm_shift > 4 ? renorm_shift - 4 : 0; + uint32_t result = sign | ((nonsign << renorm_shift >> 4) + ((0x78 - renorm_shift) << 23)); + + union { + uint32_t as_bits; + float as_value; + } fp32 = {result}; + return fp32.as_value; +} + +template +__forceinline__ __device__ auto convert_layout_acc_rowcol(Layout acc_layout) { + // static_assert(decltype(size<0>(acc_layout))::value == 4 || decltype(size<0>(acc_layout))::value == 8); + static_assert(decltype(rank(acc_layout))::value == 3); + auto l = logical_divide(acc_layout, Shape<_1>{}); // (_4,_1,_2):(_1,_0,_4) -> ((_1,_4),_1,_2):((_0,_1),_0,_4) + + return make_layout(make_layout(get<1>(l)), make_layout(get<1>(get<0>(l)), get<2>(l))); // (1, (4, 2)):((_0),(_1,_4)) +}; + + +template +__forceinline__ __device__ auto convert_type(Tensor const &tensor) { + using From_type = typename Engine::value_type; + if constexpr (std::is_same_v) + { + return tensor; + } + + constexpr int numel = decltype(size(tensor))::value; + Tensor tensor_To_type = make_tensor(layout(tensor)); + cutlass::Array *result_ptr = reinterpret_cast *>(tensor_To_type.data()); + #if defined(__gfx938__) + { + if constexpr (std::is_same_v) { + cutlass::NumericArrayConverter convert_op; + *result_ptr = convert_op(*reinterpret_cast *>(tensor.data())); + } + else if constexpr (std::is_same_v) { + cutlass::NumericArrayConverter convert_op; + *result_ptr = convert_op(*reinterpret_cast *>(tensor.data())); + } + else { + cutlass::NumericArrayConverter convert_op; + *result_ptr = convert_op(*reinterpret_cast *>(tensor.data())); + } + return tensor_To_type; + } + #else + { + if constexpr (std::is_same_v) { + #ifndef FLASH_MLA_BF16_TYPE + #define FLASH_MLA_BF16_TYPE 0 + #endif + #if FLASH_MLA_BF16_TYPE == 0 + cutlass::NumericArrayConverter convert_op; + #else + cutlass::NumericArrayConverter convert_op; + #endif + *result_ptr = convert_op(*reinterpret_cast *>(tensor.data())); + } else { + cutlass::NumericArrayConverter convert_op; + *result_ptr = convert_op(*reinterpret_cast *>(tensor.data())); + } + return tensor_To_type; + } + #endif + // cutlass::NumericArrayConverter convert_op; + // // HACK: this requires tensor to be "contiguous" + // auto frag = convert_op(*reinterpret_cast *>(tensor.data())); + // return make_tensor(make_rmem_ptr(&frag), tensor.layout()); +} + + +template +__forceinline__ __device__ auto convert_layout_acc_Aregs(const TiledMma& tiled_mma, const TiledMma_O& tiled_mma_o, Tensor const& tOrP, + Tensor const& sAcc) +{ + using Value_type = typename Engine0::value_type; + + int tid = threadIdx.x % 64; + int warp_id = threadIdx.x / 64; + + sAcc((tid % 16 ) * 8 + (tid / 16) + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(0, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 1 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(1, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 2 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(2, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 3 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(3, 0, 0); + __syncthreads(); + + using SmemLayoutAtomP = Layout, Int<64>>, Stride, _1>>; + using SmemLayoutP = decltype(tile_to_shape( + SmemLayoutAtomP{}, + Shape, Int<64>>{})); + Tensor sP_tmp = make_tensor(sAcc.data(),SmemLayoutP{}); + auto thr_mma = tiled_mma_o.get_thread_slice(tid); + Tensor tSrACC = thr_mma.partition_fragment_A(sP_tmp); + + tSrACC(0, 0, 0) = sAcc(tid * 8 + 0); + tSrACC(1, 0, 0) = sAcc(tid * 8 + 1); + tSrACC(2, 0, 0) = sAcc(tid * 8 + 2); + tSrACC(3, 0, 0) = sAcc(tid * 8 + 3); + + tSrACC(0, 0, 1) = sAcc(tid * 8 + 0 + 4); + tSrACC(1, 0, 1) = sAcc(tid * 8 + 1 + 4); + tSrACC(2, 0, 1) = sAcc(tid * 8 + 2 + 4); + tSrACC(3, 0, 1) = sAcc(tid * 8 + 3 + 4); + + tSrACC(0, 0, 2) = sAcc(tid * 8 + 0 + 16*32); + tSrACC(1, 0, 2) = sAcc(tid * 8 + 1 + 16*32); + tSrACC(2, 0, 2) = sAcc(tid * 8 + 2 + 16*32); + tSrACC(3, 0, 2) = sAcc(tid * 8 + 3 + 16*32); + + tSrACC(0, 0, 3) = sAcc(tid * 8 + 0 + 4 + 16*32); + tSrACC(1, 0, 3) = sAcc(tid * 8 + 1 + 4 + 16*32); + tSrACC(2, 0, 3) = sAcc(tid * 8 + 2 + 4 + 16*32); + tSrACC(3, 0, 3) = sAcc(tid * 8 + 3 + 4 + 16*32); + + + return tSrACC; +} + +__forceinline__ __device__ bool +is_positive_infinity(const float& f_val) +{ + union Fp32{ + uint32_t as_bits; + float as_value; + }; + Fp32 fp32; + fp32.as_value = f_val; + Fp32 inf_tmp; + inf_tmp.as_value = INFINITY; + + return fp32.as_bits == inf_tmp.as_bits; +} + +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool Is_load_Q=false, + class SrcEngine, class SrcLayout, + class DstEngine, class DstLayout> +CUTE_HOST_DEVICE +void +lds_direct_copy( + Tensor const& src, + Tensor & dst, + int k_idx_, const int row_stride, + const int max_MN=0) +{ + #if defined(__gfx936__) || defined(__gfx938__) + { + if constexpr (Is_load_Q) { + // // 32x64 + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 2; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + const int offset_s = 0; + + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + constexpr int elements_per_thread = 8; + constexpr int bytes_per_warp = warp_size * 8 * element_size; + int mma_k = 16*128; + + int row = lane % 16; + int col = lane / 16; + + int row_offset = row ; + int col_offset = (col + warp_id * 4) * elements_per_thread + k_idx * 128; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + + if (!Is_even_K && col_offset >= 576) offset_v = -1; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + k_idx * mma_k * element_size; + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + __builtin_amdgcn_sched_barrier(0); + + + } + else { + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 2; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + const int offset_s = 0; + + // global addr + // uint32x4_t global_addr = {0}; + // *(uint64_t*)&global_addr = reinterpret_cast(src.data().get()); + // global_addr[1] += 0x41000000; // 62 bit: cache swizzle; 48~61: Stride + // global_addr[2] = 0xfffffffe; + // global_addr[3] = 0x00020000; + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + + + constexpr int elements_per_thread = 8; + constexpr int bytes_per_warp = warp_size * 8 * element_size; + int mma_k = 32*64; + + + // int row = lane / 4; + // int col = lane % 4; + // int swizzle_col = ((row / 2) ^ (col )) * 4 + (col % 4); + // 此处待优化,后8行,行号需要交换 + int virtual_row = lane / 8; + int virtual_col = lane % 8; + int swizzle_col = virtual_row ^ virtual_col; + int row = lane / 4; + // 8->9 9->8 + row = (row >= 8 ) ^ row; + // row = row >= 8 ? (swizzle_col / 4) > 0 ? row + 1 : row - 1 : row; + int col = swizzle_col % 4; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 32; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + k_idx * mma_k * element_size; + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + __builtin_amdgcn_sched_barrier(0); + } + + } + #endif +} + +template +CUTE_HOST_DEVICE +void +lds_direct_copy_for_prefill_sparse_mla( + Tensor const& src, + Tensor & dst, + int row_offset, + int col, + int k_idx_, const int row_stride, int max_MN=0) +{ + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 2; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + const int offset_s = 0; + + // global addr + // uint32x4_t global_addr = {0}; + // *(uint64_t*)&global_addr = reinterpret_cast(src.data().get()); + // global_addr[1] += 0x41000000; // 62 bit: cache swizzle; 48~61: Stride + // global_addr[2] = 0xfffffffe; + // global_addr[3] = 0x00020000; + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + glob_ptr.latter |= ((row_stride * 2) << 16); // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = max_MN; + global_addr[3] = 0x00020000; + + + + constexpr int elements_per_thread = 8; + constexpr int bytes_per_warp = warp_size * 8 * element_size; + int mma_k = 32*64; + + int col_offset = col * elements_per_thread + k_idx * 32; + int offset_v = (col_offset) * element_size; // bytes + // int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + // if (!Is_even_MN && (row_offset >= max_MN || row_offset < 0)) offset_v = -1; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + (k_idx % 4) * mma_k * element_size; + typedef uint32_t uint32x2_t __attribute__((ext_vector_type(2))); + uint32x2_t index_offset = {0}; + index_offset[0] = row_offset == -1 ? max_MN : row_offset; + index_offset[1] = offset_v; + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 , idxen offen offset:0, lds \n" ::"v"(index_offset), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + __builtin_amdgcn_sched_barrier(0); + +} +template< +class SrcEngine, class SrcLayout> +CUTE_HOST_DEVICE +void +asm_ds_write(const uint128_t & src, Tensor & dst, int k_idx) +{ + + uint128_t* d = reinterpret_cast(&dst(0, 0, k_idx)); + d[0] = src; + +} +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool mma_layout = false, + bool use_asm = false, + class SrcEngine, class SrcLayout + > +CUTE_HOST_DEVICE +void +buffer_load_copy( + Tensor const& src, + uint128_t & dst, + int k_idx_, const int row_stride, + int offset_k, + const int max_MN=0) +{ + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 2; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + constexpr int elements_per_thread = 8; + if constexpr (mma_layout) + { + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = __builtin_amdgcn_readfirstlane(glob_ptr.former); + global_addr[1] = __builtin_amdgcn_readfirstlane(glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + int mma_k = 32*64; + int row = tidx % 16; + int col = lane / 16; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 32; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + + if constexpr(use_asm) { + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "buffer_load_dwordx4 %0, %1, %2 ,0 offen offset:0 \n" + " \n\t" :"=v"(dst), + "+v"(offset_v), "+s"(global_addr) + ); + __builtin_amdgcn_sched_barrier(0); + } + else { + auto res = __builtin_amdgcn_buffer_load_dwordx4(global_addr, 0, offset_v, false, false); + + dst = *reinterpret_cast(&res); + } + + + } + else + { + uint32x4_t global_addr = {0}; + *(uint64_t*)&global_addr = reinterpret_cast(src.data().get()); + // global_addr[1] += 0x41000000; // 62 bit: cache swizzle; 48~61: Stride + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + int mma_k = 32*64; + int row = tidx / 4; + int col = lane % 4; + int row_offset = row; + int col_offset = col * elements_per_thread + k_idx * 32; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + if constexpr(use_asm) { + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "buffer_load_dwordx4 %0, %1, %2 ,0 offen offset:0 \n" + " \n\t" :"=v"(dst), + "+v"(offset_v), "+s"(global_addr) + ); + __builtin_amdgcn_sched_barrier(0); + } + else { + auto res = __builtin_amdgcn_buffer_load_dwordx4(global_addr, 0, offset_v, false, false); + + dst = *reinterpret_cast(&res); + } + + } + + +} + +template< +class SrcEngine, class SrcLayout> +CUTE_HOST_DEVICE +void +buffer_to_tensor(const uint128_t & src, Tensor & dst, int k_idx) +{ + + uint128_t* d = reinterpret_cast(&dst(0, 0, k_idx)); + d[0] = src; + +} + +template +__forceinline__ __device__ auto convert_layout_acc_Aregs_dense(const TiledMma& tiled_mma, const TiledMma_O& tiled_mma_o, Tensor const& tOrP, + Tensor const& sAcc) +{ + using Value_type = typename Engine0::value_type; + + int tid = threadIdx.x % 64; + int warp_id = threadIdx.x / 64; + // __fp16 *smem_ptr = + + // sAcc((tid % 16 ) * 4 + (tid / 16) + warp_id * 16 * 16) = tOrP(0, 0, 0); + // sAcc((tid % 16 ) * 4 + (tid / 16) + 16 * 4 + warp_id * 16 * 16) = tOrP(1, 0, 0); + // sAcc((tid % 16 ) * 4 + (tid / 16) + 2 * 16 * 4 + warp_id * 16 * 16) = tOrP(2, 0, 0); + // sAcc((tid % 16 ) * 4 + (tid / 16) + 3 * 16 * 4 + warp_id * 16 * 16) = tOrP(3, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(0, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 1 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(1, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 2 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(2, 0, 0); + sAcc((tid % 16 ) * 8 + (tid / 16) + 3 * 16 * 8 + (warp_id % 2) * 4 + (warp_id / 2) * 16 * 32) = tOrP(3, 0, 0); + __syncthreads(); + + using SmemLayoutAtomP = Layout, Int<64>>, Stride, _1>>; + using SmemLayoutP = decltype(tile_to_shape( + SmemLayoutAtomP{}, + Shape, Int<64>>{})); + Tensor sP_tmp = make_tensor(sAcc.data(),SmemLayoutP{}); + auto thr_mma = tiled_mma_o.get_thread_slice(tid); + Tensor tSrACC = thr_mma.partition_fragment_A(sP_tmp); + + + tSrACC(0, 0, 0) = sAcc(tid * 8 + 0); + tSrACC(1, 0, 0) = sAcc(tid * 8 + 1); + tSrACC(2, 0, 0) = sAcc(tid * 8 + 2); + tSrACC(3, 0, 0) = sAcc(tid * 8 + 3); + + tSrACC(0, 0, 1) = sAcc(tid * 8 + 0 + 4); + tSrACC(1, 0, 1) = sAcc(tid * 8 + 1 + 4); + tSrACC(2, 0, 1) = sAcc(tid * 8 + 2 + 4); + tSrACC(3, 0, 1) = sAcc(tid * 8 + 3 + 4); + + tSrACC(0, 0, 2) = sAcc(tid * 8 + 0 + 16*32); + tSrACC(1, 0, 2) = sAcc(tid * 8 + 1 + 16*32); + tSrACC(2, 0, 2) = sAcc(tid * 8 + 2 + 16*32); + tSrACC(3, 0, 2) = sAcc(tid * 8 + 3 + 16*32); + + tSrACC(0, 0, 3) = sAcc(tid * 8 + 0 + 4 + 16*32); + tSrACC(1, 0, 3) = sAcc(tid * 8 + 1 + 4 + 16*32); + tSrACC(2, 0, 3) = sAcc(tid * 8 + 2 + 4 + 16*32); + tSrACC(3, 0, 3) = sAcc(tid * 8 + 3 + 4 + 16*32); + + return tSrACC; +} + +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool Is_load_Q=false, + class SrcEngine, class SrcLayout, + class DstEngine, class DstLayout> +CUTE_HOST_DEVICE +void +lds_direct_copy_qkvfp8( + Tensor const& src, + Tensor & dst, + int k_idx_, const int row_stride, + const int max_MN=0) +{ + + if constexpr (Is_load_Q) { + + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 1; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + const int offset_s = 0; + + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = (glob_ptr.former); + global_addr[1] = (glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + constexpr int elements_per_thread = 16; + constexpr int bytes_per_warp = warp_size * elements_per_thread * element_size; + int mma_k = 16*256; + + int row = lane % 16; + int col = lane / 16; + + int row_offset = row ; + int col_offset = (col + warp_id * 4) * elements_per_thread + k_idx * 256; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + + if (!Is_even_K && col_offset >= 576) offset_v = -1; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + k_idx * mma_k * element_size; + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + __builtin_amdgcn_sched_barrier(0); + + + + } else { + constexpr int warp_size = 64; + int tidx = threadIdx.x;//0-256 + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size;//0-63 + constexpr int element_size = 1; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_);//576 + const int offset_s = 0; + + // global addr + // uint32x4_t global_addr = {0}; + // *(uint64_t*)&global_addr = reinterpret_cast(src.data().get()); + // global_addr[1] += 0x41000000; // 62 bit: cache swizzle; 48~61: Stride + // global_addr[2] = 0xfffffffe; + // global_addr[3] = 0x00020000; + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = __builtin_amdgcn_readfirstlane(glob_ptr.former); + global_addr[1] = __builtin_amdgcn_readfirstlane(glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + + + constexpr int elements_per_thread = 16; + constexpr int bytes_per_warp = warp_size * elements_per_thread * element_size;//64*16*1 + int mma_k = 64*64; + + + // int row = lane / 4; + // int col = lane % 4; + // int swizzle_col = ((row / 2) ^ (col )) * 4 + (col % 4); + // 此处待优化,后8行,行号需要交换 + int virtual_row = lane / 8;//0 + int virtual_col = lane % 8;//0 + int swizzle_col = virtual_row ^ virtual_col; + int row = lane / 4;//0 + // 8->9 9->8 + row = (row >= 8 ) ^ row; + // row = row >= 8 ? (swizzle_col / 4) > 0 ? row + 1 : row - 1 : row; + int col = swizzle_col % 4; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 64; + int offset_v = row_offset * row_stride + (col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + //int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + (k_idx % 2) * mma_k * element_size; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + (k_idx) * mma_k * element_size; + + __builtin_amdgcn_sched_barrier(0); + #if defined(__gfx938__) + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + #endif + __builtin_amdgcn_sched_barrier(0); + } +} + + +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool mma_layout = false, + bool use_asm = false, + class SrcEngine, class SrcLayout + > +CUTE_HOST_DEVICE +void +buffer_load_copy_qkvfp8( + Tensor const& src, + uint128_t & dst, + int k_idx_, const int row_stride, + int offset_k, + const int max_MN=0) +{ + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 1; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + constexpr int elements_per_thread = 16; + if constexpr (mma_layout) + { + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = __builtin_amdgcn_readfirstlane(glob_ptr.former); + global_addr[1] = __builtin_amdgcn_readfirstlane(glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + int mma_k = 32*64; + int row = tidx % 16; + int col = lane / 16; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 64; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + + if constexpr(use_asm) { + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "buffer_load_dwordx4 %0, %1, %2 ,0 offen offset:0 \n" + " \n\t" :"=v"(dst), + "+v"(offset_v), "+s"(global_addr) + ); + __builtin_amdgcn_sched_barrier(0); + } + else { + auto res = __builtin_amdgcn_buffer_load_dwordx4(global_addr, 0, offset_v, false, false); + + dst = *reinterpret_cast(&res); + } + + + } +} + +template +__forceinline__ __device__ void __ds_read_m32x32_row_col_rrow(Tensor0& src, intx4_t& dst) +{ + + auto lds = reinterpret_cast(src.data().get()); + auto layout = src.layout(); + constexpr short offset = layout(0, row, col) * 1; + + auto d = __builtin_amdgcn_ds_read_m32x32u8((__attribute__((address_space(3))) int*)(lds), offset); + dst = d; +} + +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool Is_load_Q=false, + class SrcEngine, class SrcLayout, + class DstEngine, class DstLayout> +CUTE_HOST_DEVICE +void +lds_direct_copy_fp8( + Tensor const& src, + Tensor & dst, + int k_idx_, const int row_stride, + const int max_MN=0) +{ + + if constexpr (Is_load_Q) { + + } else { + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 1; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + const int offset_s = 0; + + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = __builtin_amdgcn_readfirstlane(glob_ptr.former); + global_addr[1] = __builtin_amdgcn_readfirstlane(glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + + + constexpr int elements_per_thread = 16; + constexpr int bytes_per_warp = warp_size * elements_per_thread * element_size; + int mma_k = 64*64; + + + // int row = lane / 4; + // int col = lane % 4; + // int swizzle_col = ((row / 2) ^ (col )) * 4 + (col % 4); + // 此处待优化,后8行,行号需要交换 + int virtual_row = lane / 8; + int virtual_col = lane % 8; + int swizzle_col = virtual_row ^ virtual_col; + int row = lane / 4; + // 8->9 9->8 + row = (row >= 8 ) ^ row; + // row = row >= 8 ? (swizzle_col / 4) > 0 ? row + 1 : row - 1 : row; + int col = swizzle_col % 4; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 64; + int offset_v = row_offset * row_stride + (col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + int ldsAddrPerWave = reinterpret_cast(dst.data().get()) + warp_id * bytes_per_warp + k_idx * mma_k * element_size; + // if (thread(0)) + // { + // printf("offset_v = %d %d \n", offset_v, warp_id * bytes_per_warp + k_idx * mma_k * element_size); + // } + __builtin_amdgcn_sched_barrier(0); + #if defined(__gfx936__) || defined(__gfx938__) + asm volatile( + "s_mov_b32 m0, %1 \n\t" + "s_nop 0 \n\t" + "buffer_load_dwordx4 %0, %2, %3 ,offen offset:0, lds \n" ::"v"(offset_v), + "s"(ldsAddrPerWave), "s"(global_addr), "s"(offset_s) + :); + __builtin_amdgcn_sched_barrier(0); + #endif + } +} +__forceinline__ __device__ cutlass::bfloat16_t fp8e4m3_to_bf16(const fp8& input) { + const uint16_t w = (uint16_t)input << 8; + const uint16_t sign = w & UINT16_C(0x8000); + const uint16_t nonsign = w & UINT16_C(0x7FFF); + constexpr uint16_t exp_offset=(0x78 << 7); + uint16_t result = sign | ((nonsign >> 4) + exp_offset); + // if(nonsign == 0x0000) result = 0x0000; + + // if (thread0() && nonsign == 0x0000) + // { + // printf(" input = %x result = %x\n", input, result); + // } + return cutlass::bfloat16_t::bitcast(result); +} +__forceinline__ __device__ float fp8e5m2_to_fp32(const fp8& input) { + union uf16{ + uint16_t as_bits; + _Float16 as_value; + } ; + union uf32 { + uint32_t as_bits; + float as_value; + }; + uf16 u16; + uf32 u32; + u16.as_bits = (uint16_t)input << 8; + u32.as_value = (float)u16.as_value; +// return u32.as_bits>>16; + return u32.as_value; +} +__forceinline__ __device__ cutlass::half_t fp8e5m2_to_fp16(const fp8& input) { + union uf16{ + uint16_t as_bits; + __fp16 as_value; + } ; + union uf32 { + uint32_t as_bits; + float as_value; + }; + uf16 u16; +// uf32 u32; +// u16.as_bits = (uint16_t)input << 8; +// u32.as_value = (float)u16.as_value; +// return u32.as_bits>>16; + + uint16_t output = (uint16_t)(input << 8); + return cutlass::half_t::bitcast(output); +} + +template +CUTE_HOST_DEVICE +void wait_vmcnt() { + __builtin_amdgcn_sched_barrier(0); + asm volatile("s_waitcnt vmcnt(%0) ;\n\t" + "s_barrier; \n\t" + :: "n"(N)); + __builtin_amdgcn_sched_barrier(0); +} +#if 0 +template +__forceinline__ __device__ void gemm_rs_fp8(Tensor0 &acc, Tensor1 &tCrA, Tensor2 &tCrB_int8, Tensor3 &tCrB, Tensor4 const& tCsB, + TiledMma tiled_mma, TiledCopy smem_tiled_copy_B, + ThrCopy smem_thr_copy_B, const float& k_scale ) +{ + typedef __fp16 __fp16x8_t __attribute__((ext_vector_type(8))); + typedef unsigned int __hip_fp8x4_storage_t; + typedef unsigned short int __hip_fp8x2_storage_t; + typedef unsigned char __hip_fp8_storage_t; + union { + __fp16x8_t data_128; + __hip_fp8x4_storage_t fp8_array[4]; + } data[8]; + __builtin_amdgcn_sched_barrier(0); + wait_vmcnt<8>(); + data[0].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 0)); + wait_vmcnt<7>(); + data[1].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 1)); + wait_vmcnt<6>(); + data[2].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 2)); + wait_vmcnt<5>(); + data[3].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 3)); + wait_vmcnt<4>(); + data[4].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 4)); + wait_vmcnt<3>(); + data[5].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 5)); + wait_vmcnt<2>(); + data[6].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 6)); + wait_vmcnt<1>(); + data[7].data_128 = *reinterpret_cast<__fp16x8_t *>(&tCsB(0, 0, 7)); + __builtin_amdgcn_sched_barrier(0); + #pragma unroll + for (int k_idx = 0; k_idx < 8; k_idx++) + { + #pragma unroll + for (int j = 0; j < 16; j+=4) { + auto fp8x2_low = *reinterpret_cast<__hip_fp8x2_storage_t*>(&data[k_idx].fp8_array[j / 4]); + auto fp8x2_high = *(reinterpret_cast<__hip_fp8x2_storage_t*>(&(data[k_idx].fp8_array[j / 4])) + 1); + + if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v) { + auto f1 = (static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = (static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = (static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = (static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + auto rst0 = fp8e4m3_to_bf16(f1); + auto rst1 = fp8e4m3_to_bf16(f2); + auto rst2 = fp8e4m3_to_bf16(f3); + auto rst3 = fp8e4m3_to_bf16(f4); + tCrB(j, 0, k_idx) = rst0; + tCrB(j + 1, 0, k_idx) = rst1; + tCrB(j + 2, 0, k_idx) = rst2; + tCrB(j + 3, 0, k_idx) = rst3; + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + auto f1 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + // if (block0()) + // { + // printf("threadIdx.x = %d %.2f %.2f %.2f %.2f \n", threadIdx.x, f1, f2, f3, f4); + // } + cutlass::NumericConverter convert_; + auto rst0 = convert_(f1); + auto rst1 = convert_(f2); + auto rst2 = convert_(f3); + auto rst3 = convert_(f4); + tCrB(j, 0, k_idx) = rst0; + tCrB(j + 1, 0, k_idx) = rst1; + tCrB(j + 2, 0, k_idx) = rst2; + tCrB(j + 3, 0, k_idx) = rst3; + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + // auto f1 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + // auto f2 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + // auto f3 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + // auto f4 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + + // tCrB(j, 0, k_idx) = f1; + // tCrB(j + 1, 0, k_idx) = f2; + // tCrB(j + 2, 0, k_idx) = f3; + // tCrB(j + 3, 0, k_idx) = f4; + __hip_fp8x4_storage_t fp8_data = data[k_idx].fp8_array[j / 4]; + + union Fp8_data_union{ + __hip_fp8x4_storage_t fp8x4; + uint16_t fp16[2]; + }; + Fp8_data_union first_fp8, last_fp8; + first_fp8.fp8x4 = ((fp8_data & 0xff00ff00)); + last_fp8.fp8x4 = ((fp8_data & 0x00ff00ff) << 8); + tCrB(j, 0, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[0]); + tCrB(j + 1, 0, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[0]);; + tCrB(j + 2, 0, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[1]);; + tCrB(j + 3, 0, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[1]);;; + + } + else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v) { + auto f1 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + auto rst0 = __builtin_amdgcn_cvt_pkrtz(f1, f2); + auto rst1 = __builtin_amdgcn_cvt_pkrtz(f3, f4); + cutlass::Array result0 = reinterpret_cast &>(rst0); + cutlass::Array result1 = reinterpret_cast &>(rst1); + tCrB(j, 0, k_idx) = result0[0]; + tCrB(j + 1, 0, k_idx) = result0[1]; + tCrB(j + 2, 0, k_idx) = result1[0]; + tCrB(j + 3, 0, k_idx) = result1[1]; + } + } + cute::gemm(tiled_mma, tCrA(_, _, k_idx), tCrB(_, _, k_idx), acc); + } +} + +template +__forceinline__ __device__ void gemm_k_rs_fp8(Tensor0 &acc, Tensor1 &tCrA, Tensor2 &tCrB, TiledMma tiled_mma, uint32x4_t& _data, const float& k_scale) +{ + typedef __fp16 __fp16x8_t __attribute__((ext_vector_type(8))); + typedef unsigned int __hip_fp8x4_storage_t; + typedef unsigned short int __hip_fp8x2_storage_t; + typedef unsigned char __hip_fp8_storage_t; + union { + uint32x4_t data_128; + __hip_fp8x4_storage_t fp8_array[4]; + } data; + data.data_128 = _data; + + for (int j = 0; j < 16; j+=4) { + auto fp8x2_low = *reinterpret_cast<__hip_fp8x2_storage_t*>(&data.fp8_array[j / 4]); + auto fp8x2_high = *(reinterpret_cast<__hip_fp8x2_storage_t*>(&(data.fp8_array[j / 4])) + 1); + + if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v) { + auto f1 = (static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = (static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = (static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = (static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + auto rst0 = fp8e4m3_to_bf16(f1); + auto rst1 = fp8e4m3_to_bf16(f2); + auto rst2 = fp8e4m3_to_bf16(f3); + auto rst3 = fp8e4m3_to_bf16(f4); + tCrB(j, 0, k_idx) = rst0; + tCrB(j + 1, 0, k_idx) = rst1; + tCrB(j + 2, 0, k_idx) = rst2; + tCrB(j + 3, 0, k_idx) = rst3; + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + auto f1 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + // if (thread0()) { + // printf(" static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8) = %x f1 = %.2f\n", static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8), f1); + // } + cutlass::NumericConverter convert_; + auto rst0 = convert_(f1); + auto rst1 = convert_(f2); + auto rst2 = convert_(f3); + auto rst3 = convert_(f4); + tCrB(j, 0, k_idx) = rst0; + tCrB(j + 1, 0, k_idx) = rst1; + tCrB(j + 2, 0, k_idx) = rst2; + tCrB(j + 3, 0, k_idx) = rst3; + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + + __hip_fp8x4_storage_t fp8_data = data.fp8_array[j / 4]; + + union Fp8_data_union{ + __hip_fp8x4_storage_t fp8x4; + uint16_t fp16[2]; + } ; + Fp8_data_union first_fp8, last_fp8; + first_fp8.fp8x4 = ((fp8_data & 0xff00ff00)); + last_fp8.fp8x4 = ((fp8_data & 0x00ff00ff) << 8); + tCrB(j, 0, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[0]); + tCrB(j + 1, 0, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[0]);; + tCrB(j + 2, 0, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[1]);; + tCrB(j + 3, 0, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[1]);;; + } + else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v) { + auto f1 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + auto rst0 = __builtin_amdgcn_cvt_pkrtz(f1, f2); + auto rst1 = __builtin_amdgcn_cvt_pkrtz(f3, f4); + cutlass::Array result0 = reinterpret_cast &>(rst0); + cutlass::Array result1 = reinterpret_cast &>(rst1); + tCrB(j, 0, k_idx) = result0[0]; + tCrB(j + 1, 0, k_idx) = result0[1]; + tCrB(j + 2, 0, k_idx) = result1[0]; + tCrB(j + 3, 0, k_idx) = result1[1]; + } + } + cute::gemm(tiled_mma, tCrA(_, _, k_idx), tCrB(_, _, k_idx), acc); +} +#endif +template < + bool Is_even_MN=true, + bool Is_even_K=true, + bool mma_layout = false, + bool use_asm = false, + class SrcEngine, class SrcLayout + > +CUTE_HOST_DEVICE +void +buffer_load_copy_fp8( + Tensor const& src, + uint32x4_t & dst, + int k_idx_, const int row_stride, + int offset_k, + const int max_MN=0) +{ + constexpr int warp_size = 64; + int tidx = threadIdx.x; + int warp_id = __builtin_amdgcn_readfirstlane(tidx / warp_size); + int lane = tidx % warp_size; + constexpr int element_size = 1; + int k_idx = __builtin_amdgcn_readfirstlane(k_idx_); + constexpr int elements_per_thread = 16; + if constexpr (mma_layout) + { + struct PtrWrapper { + uint32_t former; + uint32_t latter; + }; + PtrWrapper glob_ptr; + *(uint64_t*)&glob_ptr = reinterpret_cast(src.data().get()); + // glob_ptr.latter |= 0x40000000; // 62 bit: cache swizzle; 48~61: Stride + + uint32x4_t global_addr = {0}; + global_addr[0] = __builtin_amdgcn_readfirstlane(glob_ptr.former); + global_addr[1] = __builtin_amdgcn_readfirstlane(glob_ptr.latter); + global_addr[2] = 0x80000000; + global_addr[3] = 0x00020000; + + int mma_k = 32*64; + int row = tidx % 16; + int col = lane / 16; + int row_offset = row + (warp_id * 16) ; + int col_offset = col * elements_per_thread + k_idx * 64; + int offset_v = (row_offset * row_stride + col_offset) * element_size; // bytes + if (!Is_even_MN && row_offset >= max_MN) offset_v = -1; + + + if constexpr(use_asm) { + __builtin_amdgcn_sched_barrier(0); + asm volatile( + "buffer_load_dwordx4 %0, %1, %2 ,0 offen offset:0 \n" + " \n\t" :"=v"(dst), + "+v"(offset_v), "+s"(global_addr) + ); + __builtin_amdgcn_sched_barrier(0); + } + else { + auto res = __builtin_amdgcn_buffer_load_dwordx4(global_addr, 0, offset_v, false, false); + + dst = *reinterpret_cast(&res); + } + + + } +} +#if 0 +template +__forceinline__ __device__ void gemm1_rs_fp8(Tensor0 &acc, Tensor1 &tCrA, Tensor3 &tCrB, Tensor4 const& tCsB, + TiledMma tiled_mma, TiledCopy smem_tiled_copy_B, + ThrCopy smem_thr_copy_B, const float& k_scale ) { + typedef __fp16 __fp16x8_t __attribute__((ext_vector_type(8))); + typedef unsigned int __hip_fp8x4_storage_t; + typedef unsigned short int __hip_fp8x2_storage_t; + typedef unsigned char __hip_fp8_storage_t; + + auto lds = reinterpret_cast<__fp16 *>(&tCsB(0, 0, 0)); + auto layout = tCsB.layout(); + union { + __fp16x8_t data_128; + __hip_fp8x4_storage_t fp8_array[4]; + } data[8]; + constexpr short offset0 = layout(0, 0, 0) * 2; + data[0].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset0); + constexpr short offset1 = layout(0, 1, 0) * 2; + data[1].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset1); + constexpr short offset2 = layout(0, 0, 1) * 2; + data[2].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset2); + constexpr short offset3 = layout(0, 1, 1) * 2; + data[3].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset3); + + constexpr short offset4 = layout(0, 0, 2) * 2; + data[4].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset4); + constexpr short offset5 = layout(0, 1, 2) * 2; + data[5].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset5); + constexpr short offset6 = layout(0, 0, 3) * 2; + data[6].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset6); + constexpr short offset7 = layout(0, 1, 3) * 2; + data[7].data_128 = __builtin_amdgcn_ds_read_m32x16f16((__attribute__((address_space(3))) __fp16*)(lds), offset7); + + #pragma unroll + for (int k_idx = 0; k_idx < 4; k_idx++) { + #pragma unroll + for (int i = 0; i < 2; i++) { + #pragma unroll + for (int j = 0; j < 16; j+=4) { + + auto fp8x2_low = *reinterpret_cast<__hip_fp8x2_storage_t*>(&data[k_idx * 2 + i].fp8_array[j / 4]); + auto fp8x2_high = *(reinterpret_cast<__hip_fp8x2_storage_t*>(&(data[k_idx * 2 + i].fp8_array[j / 4])) + 1); + + + if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v) { + // cutlass::NumericConverter convert_; + auto f1 = (static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = (static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = (static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = (static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + auto rst0 = fp8e4m3_to_bf16(f1); + auto rst1 = fp8e4m3_to_bf16(f2); + auto rst2 = fp8e4m3_to_bf16(f3); + auto rst3 = fp8e4m3_to_bf16(f4); + tCrB(j, i, k_idx) = rst0; + tCrB(j + 1, i, k_idx) = rst1; + tCrB(j + 2, i, k_idx) = rst2; + tCrB(j + 3, i, k_idx) = rst3; + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + auto f1 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e5m2_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + cutlass::NumericConverter convert_; + auto rst0 = convert_(f1); + auto rst1 = convert_(f2); + auto rst2 = convert_(f3); + auto rst3 = convert_(f4); + tCrB(j, i, k_idx) = rst0; + tCrB(j + 1, i, k_idx) = rst1; + tCrB(j + 2, i, k_idx) = rst2; + tCrB(j + 3, i, k_idx) = rst3; + + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E5M2 && std::is_same_v) { + // auto f1 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + // auto f2 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + // auto f3 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + // auto f4 = fp8e5m2_to_fp16(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + + // tCrB(j, i, k_idx) = f1; + // tCrB(j + 1, i, k_idx) = f2; + // tCrB(j + 2, i, k_idx) = f3; + // tCrB(j + 3, i, k_idx) = f4; + + __hip_fp8x4_storage_t fp8_data = data[k_idx * 2 + i].fp8_array[j / 4]; + + union Fp8_data_union{ + __hip_fp8x4_storage_t fp8x4; + uint16_t fp16[2]; + } ; + Fp8_data_union first_fp8, last_fp8; + first_fp8.fp8x4 = ((fp8_data & 0xff00ff00)); + last_fp8.fp8x4 = ((fp8_data & 0x00ff00ff) << 8); + tCrB(j, i, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[0]); + tCrB(j + 1, i, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[0]);; + tCrB(j + 2, i, k_idx) = cutlass::half_t::bitcast(last_fp8.fp16[1]);; + tCrB(j + 3, i, k_idx) = cutlass::half_t::bitcast(first_fp8.fp16[1]);;; + + } else if constexpr (KV_DTYPE == Fp8KVCacheDataType::kFp8E4M3 && std::is_same_v){ + auto f1 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low << 8) >> 8)); + auto f2 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_low >> 8))); + auto f3 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high << 8) >> 8)); + auto f4 = fp8e4m3_to_fp32(static_cast<__hip_fp8_storage_t>((fp8x2_high) >> 8)); + if constexpr(!is_scale_equal_one) { + f1 *= k_scale; + f2 *= k_scale; + f3 *= k_scale; + f4 *= k_scale; + } + auto rst0 = __builtin_amdgcn_cvt_pkrtz(f1, f3); + auto rst1 = __builtin_amdgcn_cvt_pkrtz(f2, f4); + cutlass::Array result0 = reinterpret_cast &>(rst0); + cutlass::Array result1 = reinterpret_cast &>(rst1); + tCrB(j, i, k_idx) = result0[0]; + tCrB(j + 1, i, k_idx) = result1[0]; + tCrB(j + 2, i, k_idx) = result0[1]; + tCrB(j + 3, i, k_idx) = result1[1]; + } + + } + } + cute::gemm(tiled_mma, tCrA(_, _, k_idx), tCrB(_, _, k_idx), acc); + } + +} +#endif +typedef __bf16 __fp16x8_t __attribute__((ext_vector_type(8))); + +template +__forceinline__ __device__ void qk_gemm(const __fp16x8_t& q_data, Element* k_lds_read_ptr, v4f* accs_f32) +{ + typedef __bf16 __fp16x8_t __attribute__((ext_vector_type(8))); + typedef __bf16 __fp16x4_t __attribute__((ext_vector_type(4))); + union Bf16_storage { + __fp16x8_t data_128; + __fp16x4_t data_64[2]; + uint16_t data_array[8]; + }; + constexpr int k_idx_even = k_idx % k_mod; + constexpr int n_offset = 16 * 32; + constexpr int k_offset = k_idx_even * 64 * 32; + Bf16_storage q_reg; + Bf16_storage k_reg; + q_reg.data_128 = q_data; + k_reg.data_128 = *reinterpret_cast<__fp16x8_t*>(k_lds_read_ptr + k_offset); + // q_reg.data_128 = __builtin_hcu_ds_read_matrix_trans_format_bf16((__attribute__((address_space(3))) short*)(q_lds_read_ptr), k_offset, 2, 1, 0); + // k_reg.data_128 = __builtin_hcu_ds_read_matrix_trans_format_bf16((__attribute__((address_space(3))) short*)(k_lds_read_ptr), 0 * n_offset + k_offset, 2, 1, 0); +#if defined(__gfx938__) + accs_f32[0] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[0], k_reg.data_64[0], accs_f32[0], true,false); + accs_f32[0] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[1], k_reg.data_64[1], accs_f32[0], true,false); +#else + accs_f32[0] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[0], k_reg.data_64[0], accs_f32[0]); + accs_f32[0] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[1], k_reg.data_64[1], accs_f32[0]); +#endif + + k_reg.data_128 = *reinterpret_cast<__fp16x8_t*>(k_lds_read_ptr + k_offset + 1 * n_offset); +#if defined(__gfx938__) + accs_f32[1] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[0], k_reg.data_64[0], accs_f32[1], true,false); + accs_f32[1] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[1], k_reg.data_64[1], accs_f32[1], true,false); +#else + accs_f32[1] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[0], k_reg.data_64[0], accs_f32[1]); + accs_f32[1] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[1], k_reg.data_64[1], accs_f32[1]); +#endif + // k_reg.data_128 = __builtin_hcu_ds_read_matrix_trans_format_bf16((__attribute__((address_space(3))) short*)(k_lds_read_ptr), 1 * n_offset + k_offset, 2, 1, 0); + + + k_reg.data_128 = *reinterpret_cast<__fp16x8_t*>(k_lds_read_ptr + k_offset + 2 * n_offset); + // k_reg.data_128 = __builtin_hcu_ds_read_matrix_trans_format_bf16((__attribute__((address_space(3))) short*)(k_lds_read_ptr), 2 * n_offset + k_offset, 2, 1, 0); +#if defined(__gfx938__) + accs_f32[2] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[0], k_reg.data_64[0], accs_f32[2], true,false); + accs_f32[2] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[1], k_reg.data_64[1], accs_f32[2], true,false); +#else + accs_f32[2] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[0], k_reg.data_64[0], accs_f32[2]); + accs_f32[2] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[1], k_reg.data_64[1], accs_f32[2]); +#endif + k_reg.data_128 = *reinterpret_cast<__fp16x8_t*>(k_lds_read_ptr + k_offset + 3 * n_offset); + // k_reg.data_128 = __builtin_hcu_ds_read_matrix_trans_format_bf16((__attribute__((address_space(3))) short*)(k_lds_read_ptr), 3 * n_offset + k_offset, 2, 1, 0); +#if defined(__gfx938__) + accs_f32[3] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[0], k_reg.data_64[0], accs_f32[3], true,false); + accs_f32[3] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(q_reg.data_64[1], k_reg.data_64[1], accs_f32[3], true,false); +#else + accs_f32[3] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[0], k_reg.data_64[0], accs_f32[3]); + accs_f32[3] = __builtin_amdgcn_mmac_f32_16x16x16bf16(q_reg.data_64[1], k_reg.data_64[1], accs_f32[3]); +#endif +} + +typedef __bf16 __fp16x4_t __attribute__((ext_vector_type(4))); + +template +__forceinline__ __device__ void pv_gemm(const __fp16x4_t& p, int v_lds_read_ptr, v4f* acco_f32) +{ + constexpr int k_idx_even = k_idx; + constexpr int n_offset = 16 * 32 * 2; + typedef __bf16 __fp16x8_t __attribute__((ext_vector_type(8))); + union Bf16_storage { + __fp16x8_t data_128; + __fp16x4_t data_64[2]; + uint16_t data_array[8]; + }; + constexpr int k_offset = k_idx_even * 16 * 128 * 2; + // #if 1 + Bf16_storage v_reg; + + v_reg.data_128 = __builtin_amdgcn_ds_read_m32x16f16_alt((__attribute__((address_space(3))) __fp16*)(v_lds_read_ptr), k_offset + (n_idx_val % 4) * n_offset); +#if defined(__gfx938__) + acco_f32[n_idx_val * 2] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(p, v_reg.data_64[0], acco_f32[n_idx_val * 2], true, false); + acco_f32[n_idx_val * 2 + 1] = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(p, v_reg.data_64[1], acco_f32[n_idx_val * 2 + 1], true, false); +#else + acco_f32[n_idx_val * 2] = __builtin_amdgcn_mmac_f32_16x16x16bf16(p, v_reg.data_64[0], acco_f32[n_idx_val * 2]); + acco_f32[n_idx_val * 2 + 1] = __builtin_amdgcn_mmac_f32_16x16x16bf16(p, v_reg.data_64[1], acco_f32[n_idx_val * 2 + 1]); +#endif +} + +} \ No newline at end of file diff --git a/third_party/flashmla_hygon/sources.json b/third_party/flashmla_hygon/sources.json new file mode 100644 index 000000000..ed9507ef8 --- /dev/null +++ b/third_party/flashmla_hygon/sources.json @@ -0,0 +1,548 @@ +{ + "upstream": "FlashMLA Hygon gfx93 MODEL1 sparse FP8 decode", + "entries": [ + "gfx9/decode/combine/combine.hip", + "gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip", + "gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip" + ], + "headers": "Compiler-independent include closure, including CUTLASS and Kerutils", + "local_change": "Replace ATen macro include with its three scalar definitions", + "files": { + "csrc/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip": { + "original_sha256": "945405abc0d00f958ed7ae0e53db940cdbede6f2216fc84f0b0b06f35f484a81", + "sha256": "d1dd4b3831383fe14a60da2bb3e1a68d660ffcd641193a7500bcbfa3fc0f019b" + }, + "csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.cuh": { + "original_sha256": "4718d6d60d9dd9787e7195f6d50fd4cf8da0d797b7e24754dda58f913bafd257", + "sha256": "d12317f1c087d94e89097fd875fbed289b27a56cf097625d8d47177dbbf154bc" + }, + "csrc/softmax_hip.h": { + "original_sha256": "58f3a649d8e0c7d19f562c821c24aab50fc2cad9720cd0893f3946aa19a227e1", + "sha256": "673d649a06e7a6264e4566f75301e3b4bc6ad07c0a4b323e1ce3bfb85a502e59" + }, + "csrc/utils_hip.h": { + "original_sha256": "502ebf2bcb93b1a0f843d0fcbbf0cbed824f8f6add38c83b90b13cc0e7bfcf63", + "sha256": "2778b2528ee109092b231ac39c2227c15c8ec07b7ab6bcc7c00366915136f3fd" + }, + "csrc/params_hip.h": { + "original_sha256": "b29ee270bc2e19dd737c4c45d46bae9ef9fff29ed69cb1c20ce20c6d5eb864ed", + "sha256": "220cabcaa7593e45572861795b8a78000f7408e2671402a99e5d0b58ae8b3862" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/bfloat16.h": { + "original_sha256": "7043c93bc4e245dc1fabf6bb81277687ca2b60d752c19f23054985c287d947a0", + "sha256": "7043c93bc4e245dc1fabf6bb81277687ca2b60d752c19f23054985c287d947a0" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/cutlass.h": { + "original_sha256": "c6acb3e79f922d569af2a4de0352590279d704e61d5263ff88787d91a576668a", + "sha256": "c6acb3e79f922d569af2a4de0352590279d704e61d5263ff88787d91a576668a" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/detail/helper_macros.hpp": { + "original_sha256": "82f7dba7ce0227dcd53ef27faca34e6643d285352edd33acd53f96db78dcba6e", + "sha256": "82f7dba7ce0227dcd53ef27faca34e6643d285352edd33acd53f96db78dcba6e" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/floating_point_nvrtc.h": { + "original_sha256": "1ef4ea03319f626306e54d41476c3cc033ee42711acac1002e555eef2dfa0cc3", + "sha256": "1ef4ea03319f626306e54d41476c3cc033ee42711acac1002e555eef2dfa0cc3" + }, + "csrc/defines_hip.h": { + "original_sha256": "6d5ed78f399da19ad84cfe1b933ae66bdce831ba718067325ac49d5b66f60ceb", + "sha256": "8127411eb6d786b59f546eb856db587b5b0008f200dae6204724164a6b9a8eac" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/tensor_hip.hpp": { + "original_sha256": "ff4e4d365729f56506f2eac5d5e15a7b4c744f8dd619412b3d9bcecd84af6d5e", + "sha256": "4b2aee07f98e9233bef8c096b2b3eff4bfb0c17584f17a46eef4bcce6b694c97" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/gemm_hip.hpp": { + "original_sha256": "2929f97eb953e080dac258e77e244637cfab70426bcd74215126c14619bb3157", + "sha256": "b7d5b8ab9214aee96438bb15b26b68b50e89923194ac52d976bef641860aa0f0" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_atom_hip.hpp": { + "original_sha256": "fc3eb07d050310d4f031b9ca67315c91ae509c719bef2d22ee7197ef9aa602e7", + "sha256": "9880d72e501decb0cb109d9ddf58c018ea04e4fe0c4334290c8606d7e2334a80" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx938_hip.hpp": { + "original_sha256": "5c46ba652ab3ceebbcb47c94be6b762cc907d194b5ec332248815cb33b8ebe7f", + "sha256": "052eb6a7db5445060eb52b87733da5c0ac829f518c15f772b15fb19b210a5db7" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/layout_hip.hpp": { + "original_sha256": "39fa243926af57b16605c2ba2b09e85317fc19bd64fae9369717992b41093398", + "sha256": "413fa9c56a8d10f27836e8d01a50ebd8f9cb06aaf88b24122de585b48f22f766" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_layout_hip.hpp": { + "original_sha256": "76b788d7d7f4db717b32861d0150af55d6037dfaae93f0beb429af7f431e783e", + "sha256": "42f5065068ae4a6f15add624dfec76293067c74e4bfe22f4e55711c3ca9edeff" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_hip.hpp": { + "original_sha256": "e503cbba418e6133dcc20239527ea277cf41cf7f1b261817cc385de241a7a392", + "sha256": "c7a0cdc0e33c673f89b2ceb19738c22ec7b804b68f63fde30c33a842b9936e52" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/math_hip.hpp": { + "original_sha256": "5e64ba6b4830a72477fdb9ce02a25f5628979f776386944d8dbfcba7bdcda1d9", + "sha256": "0d0c19e98f3b25d2a94b5a36d597e0b915f08e3db86182d29bed37861910acf5" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/util/type_traits_hip.hpp": { + "original_sha256": "741e1c4442eb8b7502adcb7343274fb9641e18828358e43e5c23311992fd5bee", + "sha256": "b3a5d0deff33ee3280f763e007a908d8cf6aa215c9d99112647db9d46845fff4" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/config_hip.hpp": { + "original_sha256": "c561c4893ee5577d140acadcafdb9cc1a984555100395b3a66253f7d4236b882", + "sha256": "41a76ffeb5c1da748e8c7c443b89a795b01303d672aa73a8ea8f8fbcab830882" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/util/debug_hip.hpp": { + "original_sha256": "b7f03edfce8cc89b5a08e2909e2d95c37abd109146e460b910339a1083a17dc6", + "sha256": "c5652e2f8643c683a96cd0d8342d2287a9c6a7d4388811e7f7c243e15c17692f" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/util/print_hip.hpp": { + "original_sha256": "39e4689b677dde1e1d8c27617ed0d264abfc2629b7b7c2af82a15405677576cb", + "sha256": "c5189470c64388c4d765ffdb63d120d74c35fb42e627894ea1b9289b4f65f615" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/complex_hip.hpp": { + "original_sha256": "b2e838c42e6c625d5ded221b996393986068f3f2af055202d0734ae5e4f9f903", + "sha256": "1e5f97f039d2691b80a40bc71d9e2f2488afa82e64366a12a0c9e6737f6ba1c4" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/complex_hip.h": { + "original_sha256": "318560e1dcd3a757cfbc177cf7e40f295b12e7900d8018fb26e40984c3fd2a8c", + "sha256": "1208da9d2d096392923a0d9ebd8f01b15fac13d05cf0d309ee9f15d79f33bf65" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/fast_math_hip.h": { + "original_sha256": "2d8a614f7b63ecce6a9d3e3d8b467d5a865f6cfc77bdc5e2d9a69b64017cc1cd", + "sha256": "3dfb99604fae00183f8f7de8b192b512b55112691de484d46c8f67b7659fbcce" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/half.h": { + "original_sha256": "634e26b8d59c460fa267cdd2395a6aa4fbed019fb1ffe2b0c712632c2d356151", + "sha256": "634e26b8d59c460fa267cdd2395a6aa4fbed019fb1ffe2b0c712632c2d356151" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/platform/platform.h": { + "original_sha256": "5d28420550a8efaa54999a583063bffd1b060562bdaa8afa2315c7babf8c8f07", + "sha256": "5d28420550a8efaa54999a583063bffd1b060562bdaa8afa2315c7babf8c8f07" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/float8.h": { + "original_sha256": "e053014b678b4d1b23809b94b1bfbad4b334c7fc67855d8cd4784681ebf31703", + "sha256": "e053014b678b4d1b23809b94b1bfbad4b334c7fc67855d8cd4784681ebf31703" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_size.h": { + "original_sha256": "80612621b84fed0c630a7b2246b381673c28a9026b7d21f4e9e450da341c2d50", + "sha256": "80612621b84fed0c630a7b2246b381673c28a9026b7d21f4e9e450da341c2d50" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/coord.h": { + "original_sha256": "87b9534bda17faf16034e2f92081d0ccc87596838ad2bad4bb59cd06da6aeada", + "sha256": "87b9534bda17faf16034e2f92081d0ccc87596838ad2bad4bb59cd06da6aeada" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/uint128.h": { + "original_sha256": "26c74aa8536f7e8718a164ac3ef2c62ea0cca90c8ff5a42366ef88683f6bc0f5", + "sha256": "26c74aa8536f7e8718a164ac3ef2c62ea0cca90c8ff5a42366ef88683f6bc0f5" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/array_hip.h": { + "original_sha256": "9ea2cab4301143c03baf6c57635e1a3bbe8f6ffe1c6cd27122e4592dc88822c4", + "sha256": "0d243676ab23c673dfec1339a94a788d25354a115267063cb009aed8d69e8e67" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/array_subbyte_hip.h": { + "original_sha256": "01ef67a4634bd9eefac15142a34f27c98b6f69528b2619e3618d3662d957fd1a", + "sha256": "7be845c5d2b0d0ad9e078ff8aad7257f44bb2c762942f1624d7c773bf881c73c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/tfloat32.h": { + "original_sha256": "300c7b1ce33c0c504b2f6032986d0bcc9e5a2cb7cbd9cca10c8e7e967ed4f11f", + "sha256": "300c7b1ce33c0c504b2f6032986d0bcc9e5a2cb7cbd9cca10c8e7e967ed4f11f" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/integer_subbyte.h": { + "original_sha256": "8a2fb1319f3f11004f986b18576f01340be9fe040e4ca6066c32fcef5f3034aa", + "sha256": "8a2fb1319f3f11004f986b18576f01340be9fe040e4ca6066c32fcef5f3034aa" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/functional.h": { + "original_sha256": "a734a46d1efedcfb7c0c76f6b101cf54c120d686364c356dc64e901b83dd303e", + "sha256": "a734a46d1efedcfb7c0c76f6b101cf54c120d686364c356dc64e901b83dd303e" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/real.h": { + "original_sha256": "a3207a902abee8a5d7006780a4f20f17f32abfb222341cee0c5b800733a78faf", + "sha256": "a3207a902abee8a5d7006780a4f20f17f32abfb222341cee0c5b800733a78faf" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/tfloat_hip.hpp": { + "original_sha256": "03424322038538ea26762bd4177bee06729340aa985f53551ddf6b38927e1424", + "sha256": "3deae047522328e97403e7de6be4c07d8f4cea2d72c501f0333215d130767661" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_types.h": { + "original_sha256": "efee119763f95ff5cd5f6bfe40510f5ed0c3eea900e3d872abf10b45485f6191", + "sha256": "efee119763f95ff5cd5f6bfe40510f5ed0c3eea900e3d872abf10b45485f6191" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/bfloat_hip.hpp": { + "original_sha256": "c6168655012943f8d7b76d9879dc9859593e0b221e1682a96ea4b664d3a6c3b0", + "sha256": "3dc251d5175cae3315148dd011bc87d8fecbdb19382564594bb52d5597143e5b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/float8_hip.hpp": { + "original_sha256": "07fb4902d3bd54b919363b03c5f8d376b025862947d75c98959d46293582d063", + "sha256": "c9c684685b2f32e94afbf5b47629b579215a361a4cb58dd9adbd79351809fd14" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/half_hip.hpp": { + "original_sha256": "6b47363f0866985a64b04e9721a29ccdbf4395e2b38ba4f95d945ded6e289455", + "sha256": "d9183e72653c8c7aa48e25cd756e1d6c52a201d5dea60873f5f865b748a66d04" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/real_hip.hpp": { + "original_sha256": "d72a73d72492e573cb5e790c9a19546a4b92373955f8d7ae60b33c472962ff87", + "sha256": "c294a7ec820fce02a6225405da252f34e4e56baf042063779b32a8f1db344388" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/int_hip.hpp": { + "original_sha256": "2653bc0329c2f9bfa3a88c4201d24207df0572405cd46501d6122ffb06945f2b", + "sha256": "395cc5bd4f523cac25d6e419e3c80b48dc0ee7353be33b6727a9bca7989ce925" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/uint128_hip.hpp": { + "original_sha256": "4a5b75259c0a93ae6007f680f9e875b02b4a20556034e530bcdc21eb24179bae", + "sha256": "c665461585efb34e04d3fe3ffb54bc7509a34fbe6b8842bd45d52ed8214dfa3a" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_subbyte_hip.hpp": { + "original_sha256": "d61a2ea669ef4321a000b3d05d916203f11658dd832cac8fe27f2810d7dc1548", + "sha256": "2dd17ab381bc6fb86328d48d27e7afb09e128d54dae22867f2e23ff64d6f1e7e" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_constant_hip.hpp": { + "original_sha256": "c61a76df672ea6738e6b3d99e782a72f469f5a0e8cfb8329d6faa3fad38f2732", + "sha256": "8caf4d95b5c458ab264ef832309e5f20ae8f62fc8e9d91aa54f68168a08351ee" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integer_sequence_hip.hpp": { + "original_sha256": "2cc77aedf18b061a3fd860293d51f309933018e042b6849946547729e2dbaf46", + "sha256": "ca9bd51e1f59e942473f5b5e21c4d23effc93738ee847ab0bd77ef1a6b66810c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tuple_algorithms_hip.hpp": { + "original_sha256": "19da2c8337207ca1d8792628c0d2bf255311f58265812bc1a364f005bf277330", + "sha256": "5166ca860a98dad43fbf88a6e8d358512f6aaf2f497fdd2e33cd517bc955c984" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/functional_hip.hpp": { + "original_sha256": "2b5990715ec449758f7f11be9fc1a03881ad82451d86147f15d66f076b5164a4", + "sha256": "888e40575515c6fc07b541177a496f9c1ca00827de0a1a0253ed7ce33b9818c2" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/tuple_hip.hpp": { + "original_sha256": "69636a148eae3a0807cb1f2e325b9ba2f0640bef7e668088a21ca94add0711ee", + "sha256": "975744820c0ce85ac522baabaf8f9fe90135109dcf6ebdfa33077fe7fe1119c5" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/hip_types.hpp": { + "original_sha256": "3863e2b8209da96ee7cb4242ad5420bb77700f17e4990e2e612aacba2cea088e", + "sha256": "2bb2c7a7bb236e930fe03a3da116f38ff434723c72bbd2bca89aea57fb0cbdb9" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/layout_composed_hip.hpp": { + "original_sha256": "060c267b35a60cafb819353643b1a70099f7e1dcf48c0da6897d25cacfbfeea8", + "sha256": "f61d324b815b417e410c39eb9a894b17fc1d2a4db6b8c61d38597c5cfb615c16" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/arithmetic_tuple_hip.hpp": { + "original_sha256": "a18db3d13022a37829da7d88b0b43e6a6dab561b2592bc92ce18071872251325", + "sha256": "4d53de0f81702d2763cd63f0fa7e66d402607897f43c2e947420e548f0d92c15" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/stride_hip.hpp": { + "original_sha256": "ac66ce7d8d9acba4802ed0b58471d5ad7f9c71ca23b072d94766ecfe652a6c4d", + "sha256": "77605f5546eea1049d0cd1abda35bfaac1f94e278128df84950c12660a2cc1c3" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/int_tuple_hip.hpp": { + "original_sha256": "15c46cdf2d0bda1d6eb5707ed57e1e97cfa8ac8b36d2bab4c0ba83cf2fc4651e", + "sha256": "97a8fd70364d9123a5ae99d3fc8ac137e666ab27fe7ec34d75d2a5d4c40b99c5" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/array_hip.hpp": { + "original_sha256": "943669ffc97c68c6f745d341c379ed080d26553f2e3606dcd6608f28e27a5fe4", + "sha256": "6211397fd378b6488046ec33ffafc58791e4ca737c74cd5b06392ee6bcc1ce19" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/underscore_hip.hpp": { + "original_sha256": "45370f03b0bcb89ae773b91a90065fa944c3353e018e38dc18283da42690fc62", + "sha256": "6c8eebc72b715e3c00a1d5b8d2309f834419126330fb644cf334dce91b0c7301" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_hip.hpp": { + "original_sha256": "6ae18beced68ecf2b409658653295ab88ffb6311206187f7f1c1c4aca29787bc", + "sha256": "4da0255eb420d75a7ded26ca91f715b7c16be2a36ad643847fff65187b8cb78d" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_hip.hpp": { + "original_sha256": "ef0eb7798a994a91c49f3995fa19287df1fa63d7dea81942b93f630020ac56c6", + "sha256": "01685f72dbd9ec1d3aaae07931b441192a715035392d2319e513b192cf03fe96" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/util_hip.hpp": { + "original_sha256": "45dda5c46c4294c5f4313e9e60281fc2c34ce91518fd231513e42f0a7676936c", + "sha256": "515182ed2f2a3e155df2188f3085d9ee5a18c0f2ade9c275ac684c2f11e12ef6" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx938_hip.hpp": { + "original_sha256": "9007a69b7189623170e81dbe5560d145d1a21381c633ca2922e13b6877c888b6", + "sha256": "f6fd98cbd2b9aa73bf0fa2057f51ac68b02adaa135b2b0075a40437353858118" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx928_hip.hpp": { + "original_sha256": "e9bab8b527f496659d03895c115bb31f64d76d2ad76b9dcfd0db6a41f5ba729a", + "sha256": "d74316bc33312508dae75b4fe29ccc11d514c69f1c3f774c0bb71df13107cf0c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx928_hip.hpp": { + "original_sha256": "d8e31b220f0285f3d29fa34f83263e4aed9bdfa88e97cb55993316cdfab92f75", + "sha256": "9f89d01f2ca111751a51ec202e76fb1e9f5e817075b00034013ca40c73047f07" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx926_hip.hpp": { + "original_sha256": "5e1e7c217fc31b0d99ec5d180387206a408f724c1caa752912f47abba1a7ccdb", + "sha256": "f7a5b9f72a05cb4a209cd80b395501682183c71e1d8ec8c3e7bba4cb5dd5dbef" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx926_hip.hpp": { + "original_sha256": "c2a8d281af3f281f321d0ac833b4fb1bed355389a272ccad9704ee67b4210283", + "sha256": "74a9493f95269e4b99eab62ec3c7652cb57bd3c867e5f025a4ca9cea4142568c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_gfx906_hip.hpp": { + "original_sha256": "1cf361498d7515f1b63134ae21c541189d95507150e7d4e38b1a69d5205e3e1e", + "sha256": "f54488ea55a6017c8d67acc79d68e3d2b4791b4cf6410c1ba8ed845148ddbb29" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_gfx906_hip.hpp": { + "original_sha256": "e69d89e460df5cf60ff012053c7eacc4894466c08a4b216ea35d7549afc8cded", + "sha256": "129a60745589d004d342e654c67597d08fb1f1f4a32c71f5e247e2498d21639b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_gmma_hip.hpp": { + "original_sha256": "56eed91ccc821e597be4ed7eea02da5c4883c429b851435a71f7fb1060fa3952", + "sha256": "7a37a8d2e993a99d30af274d9afa7d2d708a45ab93a95fbd6caf34fa5dc28920" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_hip.hpp": { + "original_sha256": "07372b798e43106e0ad1052370741eabeaf342a2f33687d84fa8006a8535150b", + "sha256": "bad8968b7502873fd54556a02941ec2a275c49876f7813cd385fe2e7d68f21c4" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_gmma_hip.hpp": { + "original_sha256": "c6c1e049fa98dd6cab663f39066b32bad787a6d8c78b9a59b16d15edae9ab164", + "sha256": "c63c549a4a1866ce0857a72097fd1f0b7ed72d3641f4756e24a1e56bff870892" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm90_desc_hip.hpp": { + "original_sha256": "0fab177c479d76d27b5fa50c1f603f5335b8aacac14f00ce0c1e71d3c0009326", + "sha256": "d27c948096af65311fe909354167e4e255451116ccbe8f21011a3364b749c402" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm90_hip.hpp": { + "original_sha256": "5eafc1b425dfbc090d3352282294213cebcbc25bb984887809f4431813afc196", + "sha256": "5f6620d2d15420df961f3d62880f612a49e71faa3f20d5eb49876d49459f4556" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm80_hip.hpp": { + "original_sha256": "73ed35a47647a6f8183d173b4bb167b60b55f6ff74b1887060fa07f89ac33e20", + "sha256": "6500095f2027de94f571bb86cdcbd3f0b1840c06cf58dd5e112bebc9c46a869c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm80_hip.hpp": { + "original_sha256": "06ad97031586fe6bcc407f32810a814131cb9556d7f7ddb5d29406aae76fc71b", + "sha256": "22337b32ed51341bea4d610cd4155e37647aa0d95331d44c4108e4b3d1cdf26f" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm75_hip.hpp": { + "original_sha256": "940402f1f6e8db1990749e9ab670c85effa913b9df25ba5049b1270e07efdc85", + "sha256": "2e6951a374f703959120f7e2f08adaa588f8c273b0598f85e874860e8c831578" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm75_hip.hpp": { + "original_sha256": "1f54147d9460b026605ea819b817191bbe0e6fb0edee0e886416e209ba9f4521", + "sha256": "ff6f51bdc74ad891e3e6cd7f5633f7b5a047be1172215e0e3a1e24100b4f451c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm70_hip.hpp": { + "original_sha256": "fb025b654db919888703c881866c4b8ffbd1155b30836aa0e7b86a3940ee6637", + "sha256": "ff257b5e3cd37ad23ae290e9909b7df1854658fdac3ebb102e353a2df5bdd251" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm70_hip.hpp": { + "original_sha256": "ccb662666ce1b4c439ab222583723c7e4a1746d77c166f3699a5b490cf6c9b42", + "sha256": "7a13b71902f1f58d34e96a5c828d53e71da1282d684944cc3e3ad1ebbc909b52" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/mma_traits_sm61_hip.hpp": { + "original_sha256": "16fc2a0ae83867d4395b86ce46186478dd17b667d00ed36ad1cd6aa89d13c1b1", + "sha256": "502cf0a47250ad9fcdec173ccc6b65bdd6c910eb3d8894951c7e5d86ccc2ae2b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/mma_sm61_hip.hpp": { + "original_sha256": "63daf1fe38cf8bb03a2a2a1d7dd2d5c7167e2be2af297b86d0c46749cdd4893f", + "sha256": "1217c25454fdf6078dcf958394abff10f63854b8cc67f4462b8653e04c3644ae" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/axpby_hip.hpp": { + "original_sha256": "b5d500b011c998a2da7afc5693e7c196a3dec44218149380af4f5ba36405502f", + "sha256": "cf9e8cb97dc344353589ffb1db54e5ed603e76c3ccc2c8946369510dbdc1ae64" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/copy_hip.hpp": { + "original_sha256": "7ddc306d06c40ff80fc085936e62ef4fe8d5facfe10c8ac608ec3b027ff739c4", + "sha256": "a931c4ffce1e5e60d03d46116c5494c5303be0843f1e52ec714ff76060d874d3" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_atom_hip.hpp": { + "original_sha256": "50bd9baa0c85d8897492d3c42ac0c64c82091d36f4e2ef3ff8e917773f2dcb85", + "sha256": "b756c5df6585b9c2bc704d0647558f3b08562a6ea7307f516cc50d5d1edb119a" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_hip.hpp": { + "original_sha256": "04613465fccbf4b882334ea0691f9fac80a71745257b223e90103b57b822f79a", + "sha256": "bbc9b0a55de99d8bf26e7cf6b934e0168db238e9c954a8bc69affc02e9e02283" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/numeric/integral_ratio_hip.hpp": { + "original_sha256": "baaa1c5079d2a1f4a7cbd816352589e744a58dd9bbbd703f800dae656657ee0a", + "sha256": "86fb749426e1401d0814b1a37401bd72af003a43211a69606b3c23dcd38c5559" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_hip.hpp": { + "original_sha256": "88f2ae59195e52609485cd6dedd755a522ec4f6c7a62e101a88422de3c66da06", + "sha256": "416c4ebe5f85b710ed687d479e402646be35a421249779c155659b12937b4746" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_hip.hpp": { + "original_sha256": "e93afdbd9f054cefcf8cf23056bf130bfddd6345222a4f52d8044642a8e6e35a", + "sha256": "2facfcc862f61bc78c650e93541cfddfecdcd828c8e4f7cf252d03798f64d26e" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_tma_swizzle_hip.hpp": { + "original_sha256": "90012f836ca201efb63a7ec725ac6fdc02f3dce2acc498c7bede40562c9004a7", + "sha256": "b486d25cf19078339eb6fe61a6a683b6e3c19ac63424f755020218ab754c3f68" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_desc_hip.hpp": { + "original_sha256": "0257f8a8af5bb9b679179032b556271d2b0bd42e0ec9e4b4e4e8e0f51c39659a", + "sha256": "63c1e9512f74a7a96813e4099f00aace165f1093cc6140651ae4959f685b5f83" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/bit_field_hip.hpp": { + "original_sha256": "693b58034234c9a1db1f987f310046a4855d630e849ab736d200f0df7494cdc5", + "sha256": "685db7ceb55aae027e1e853c0347d6bf50c4595a1147d8b192111406e0cfe1ed" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/alignment_hip.hpp": { + "original_sha256": "b8f63267dd526d6cd61b1fdaac3e4a320e3e4382a24140b708c30d576c1949a0", + "sha256": "e97c199926effa679e49574d2137645e1aa13f7d4ec1bb9d8652412c21b7215b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_hip.hpp": { + "original_sha256": "637d8f38e59f9c0419b07b41dc48bab7f4e2c04da01a4994b90a928c77fa4765", + "sha256": "17f3240a27b7327d0c79d5a7bc98b5dba7ea7983e39a2bd905e30c71929cc233" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm90_tma_hip.hpp": { + "original_sha256": "c26768d828cdfbe98540c02c3fd2d58774641da598af00f67c07b2c5f4361cad", + "sha256": "cf709e7ec617f9680d7bda39876d6bbafc3461b19220cf9836f719610c2451b1" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_gfx928_hip.hpp": { + "original_sha256": "a21fb47610d406a27a6ba7f8deed2824a3a4fc9f063f4aed26aa29fd9eadd07d", + "sha256": "e92b5f6b3e996e11832ecb62f7bf644ab3678e081ea4ee8a7593380f157e0e38" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_gfx928_hip.hpp": { + "original_sha256": "06355374a5e5cb394504f9d7cefea8e2a5989871911472b3b3e232fa2f185ded", + "sha256": "ebafb04c0a4879c00eb8d88a10355145a2772193bf2a5f8887f994e41706d9ff" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm90_hip.hpp": { + "original_sha256": "f3490c676d73d612f1e1a3c94f95350675ed8cee1a9d33b8ddef5ae7e41898c5", + "sha256": "2c1b67cdc3171a622cec1683bb90a6f87f45e3560d5cd9702c1f4836d114bfe0" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm75_hip.hpp": { + "original_sha256": "4250a4e6b4730e460df4300a020c75051594ad81889deebf336c6870b5e8ed1c", + "sha256": "dcd1ce4a334ee819dce209cae42ee73e6a726de1399f248bf0e7c3c4193b9f46" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm75_hip.hpp": { + "original_sha256": "457eb8e969cbbe1fd8cda117b4b7cb6c26608a92b070def5dfba4c3e7ba04dcd", + "sha256": "43ff97c529bd1fcf9b8a9989304aabdac6d9a9352ce41913d02bb52868e20628" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/atom/copy_traits_sm80_hip.hpp": { + "original_sha256": "f59761c5874021ef09e3897d2b1b4fd66182681c221237ef77b984b076c569e0", + "sha256": "39f974ec44c3a4e8b1bafce1cfaa6b25c98e4a6cdf4da8299657de66b526f247" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/copy_sm80_hip.hpp": { + "original_sha256": "e4dbfd2b20de2b2cd8cb89b9bf0db32fc1f4ce6fe4274c2040dc6d411ab47683", + "sha256": "727642ee2e378ad802b7b1205ad98f130504d399e1e38f826461281ab40b7568" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/tensor_predicate_hip.hpp": { + "original_sha256": "2998573b97a49430e789006d6ce956afea2d69d7e2cb3464ba23177398066955", + "sha256": "94ecf2fc0d5d70cd5d5651bf3e50410506297eeefae284e89470ad599225c9b7" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/clear_hip.hpp": { + "original_sha256": "3b4d103ce2730ffeef8d1ddde20b9beab82b2b5a34881d17cd38444f3723a5a8", + "sha256": "c9db6f2ad07b67a26c5ad5927164d91875d64b857162edc53d01ca8f8af8021b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/fill_hip.hpp": { + "original_sha256": "50ea529bca84e4fb0d1101b99367e5b3457b916750fe47e26797c6432149cddf", + "sha256": "e5d4f86895d5fab12ebb069c46af50880a4c361a6bd8e99f65e0b5f01c619964" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/prefer.hpp": { + "original_sha256": "f74cf27c3c7e0015aa1e5149699b634d33a498b426c142da1b5619c513a0a284", + "sha256": "f74cf27c3c7e0015aa1e5149699b634d33a498b426c142da1b5619c513a0a284" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/algorithm/tensor_algorithms_hip.hpp": { + "original_sha256": "e641b339edc085d272ae185e0b7d43fe65ead7c7b25a1783fee4282b7c370c14", + "sha256": "a502cf4df836febe23f180fad07b8c22587c12cf008ea413a9088388811e6fa1" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/swizzle_ptr_hip.hpp": { + "original_sha256": "f11fb29745ecab152089573bd02bba857fc4d880ea72f5ddaa1f17200a9fc788", + "sha256": "fce970bbccd4e0b28b981c5ca7dcf92b77ab640c70b355b75fa64230b863c9d8" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/pointer_hip.hpp": { + "original_sha256": "7f318ff65e42a5572b403eaf0151799cbc5da19027accd45eb939f7184438c44", + "sha256": "2bf3e5b60c0f04cedc1605b91ee9c2fde3ac84bf9eee9b36fffa247b151dcf8b" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/tile_hip.hpp": { + "original_sha256": "0759ca0c7991ccc029cdec6bce0fdd59b12e03e97cdc2d90e0dfdcc5a4d6e054", + "sha256": "d886c4669caa4826859d4afbc4e7796554d91d0f37380ce110b4e2cae8c79fb7" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/type_list_hip.hpp": { + "original_sha256": "0710cf580970e2c70f0e1844df9db520be4bb36002d29d27b6501cf42b7352e2", + "sha256": "6a5c144bc28e30f1678d6b727c4b8f90fac1c2a9d26d49b32c42436145aee768" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/array_subbyte_hip.hpp": { + "original_sha256": "ed6f9a031ad3710f0733f62a8c55d7eda1208fbde0e8b0e8fb7fe1fd6656988d", + "sha256": "856dfe1a4b4cf1109fe98a5b5576ab709d3120c3d35f1932382afdbb7636a25c" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/container/array_aligned_hip.hpp": { + "original_sha256": "ad2c54981475c8ba3a6fc64e5743d6b3c2491807024c4da6dcfe5a772a78a855", + "sha256": "b54599096912b3c63e99be6f184f25f46fae84e3260e56cba86103fd1f0afb2f" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/numeric_conversion_hip.h": { + "original_sha256": "d4bb2d560d236e481a67e5ee7399b8dbdad587be4ff95186b4c727d60d7fa3e7", + "sha256": "5e3ab66ffc10d27218d49501d58d065025d19731929c88aba4c2012772d3e17f" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/transform/thread/unary_op_hip.h": { + "original_sha256": "611cc1f3d2bf970588f4177bf4869a64cf6f8a9cab1ad48af6d3ddf12c690542", + "sha256": "596386c8f181d6ada1ecd7a1bd7aaaafd1b0ce42dc6d324b551555c11540404e" + }, + "csrc/gfx93/decode/sparse_fp8/config_hip.h": { + "original_sha256": "710dde1c8080fd67de6117b19b48c573b1aac530543d398c6a0155753963af93", + "sha256": "0bae6708ebd96085ea6c48adfa58449fbe9324769492808d6bc63aca2deb322d" + }, + "csrc/kerutils/include/kerutils/kerutils_hip.cuh": { + "original_sha256": "277af8fb1bcda01de853b715189c500028bbd1152a3d4f57f422ffcefbbafdcd", + "sha256": "a158fdf2f45acc7471b47dc08d5499830f182131b0c709d04b84491d0d4199f3" + }, + "csrc/kerutils/include/kerutils/device/device_hip.cuh": { + "original_sha256": "034bc5220230ac2266a7ab8271d70b63a30c3536745b098a41bbd877cd8322c6", + "sha256": "6c11e497ab5d495ad39c9742db790a091d60da9a3031407517c3739e00b359ed" + }, + "csrc/kerutils/include/kerutils/device/common_hip.h": { + "original_sha256": "a51dd15e841d829a6b9e1d3a1c39555e3fb8301ad6e79b8deacdede1587475a3", + "sha256": "02c88a429a6ed46371d2b77ec918407f86e9ee7cd21315a9f3c4525cb5eb5a5d" + }, + "csrc/kerutils/include/kerutils/common/common.h": { + "original_sha256": "eb3639b1aaa1ea61638ddfafd1e0e0ed02e3a3d99c0a8a6cf660dd0b285a8c51", + "sha256": "eb3639b1aaa1ea61638ddfafd1e0e0ed02e3a3d99c0a8a6cf660dd0b285a8c51" + }, + "csrc/kerutils/include/kerutils/host/host_hip.h": { + "original_sha256": "e94d3df86a087d739cb636d82455e0a9284e34ce734e42925418153ab9eb9f19", + "sha256": "02ff7d138095efe95594a9165a9db3e277b61e80606945e09f714125577b49b0" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/barrier_hip.h": { + "original_sha256": "0aa1d04d5b5d1dfd7948c29e2f20dd0489bbeee16bd2f73844678d0f73fd0f60", + "sha256": "ade08e8738a9f0f9738e4c6bc0d07b5a23d1e0b5cdb774a30f181e063677eca1" + }, + "csrc/cutlass/cutlass_3.2.1/include/cute/arch/cluster_sm90_hip.hpp": { + "original_sha256": "b71d2a008f64df369c013992dec2abcff9e8efcf4cbc18b08ae51764a3fe944c", + "sha256": "a1f69ed7fd0d698abab1b22666b8fd93fff3bf19b331fa1d7554a5adfb83290d" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/arch/memory_sm75_hip.h": { + "original_sha256": "a8595f2ff9ff10ce2798cac1ef69c84fc93f22dee26b3e2f5d59ada2cbb32019", + "sha256": "8248975ee7dc540c55b3390e9a327ea79eb1e5ec8c33ef8d2e23b856e14b1033" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/layout/matrix_hip.h": { + "original_sha256": "05d05bac300210845d865a541f7bdf92cff3f320deefe2031c195a1c0b29ea63", + "sha256": "c0cdf52f1c40c05f097f097023371e6e581d23b8d9a3419b66b08a7d49a32229" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/pitch_linear_coord.h": { + "original_sha256": "e50964ed5dd73bdd12e52eebd10c52d0e52febc319a3c56ac619d57d60c6890d", + "sha256": "e50964ed5dd73bdd12e52eebd10c52d0e52febc319a3c56ac619d57d60c6890d" + }, + "csrc/cutlass/cutlass_3.2.1/include/cutlass/matrix_coord.h": { + "original_sha256": "d30b7813cc9e87b76797bfebf5c486bc5eb383d8668c8136b3093e1385597376", + "sha256": "d30b7813cc9e87b76797bfebf5c486bc5eb383d8668c8136b3093e1385597376" + }, + "csrc/gfx93/decode/sparse_fp8/components/helpers_hip.h": { + "original_sha256": "3b396c88976593661bc593aa016dee5bcc6180bc38026b2095298b223739e822", + "sha256": "4c54d9084cd181a0edcebb4f8d079277de278ea789212fa4fb120a674e08d8f8" + }, + "csrc/gfx93/decode/sparse_fp8/components/config_hip.h": { + "original_sha256": "e5658093c563dd1321485e0dd580c6920b99083cd720263111576b6d0a2726f9", + "sha256": "639d8656a5cb27c34b8ee2f9719f929b3d89157a9cb66c12e67fb22d8ab846ca" + }, + "csrc/gfx93/decode/sparse_fp8/components/dequant_hip.h": { + "original_sha256": "40d5298acdd62837aa77822fc0c8b48a2067eba1fd6346ae01a3a9b491340faf", + "sha256": "b30f82c90f4e884304ef5953249602a7299241a4b5a29ccaad285aad9d970cf3" + }, + "csrc/gfx93/decode/sparse_fp8/splitkv_mla_hip.h": { + "original_sha256": "3e2bb39ebbeb1025b626be7ef5ffa269eb53cb6a19c7fb26c7bba5b85843508a", + "sha256": "2a39a7d005530f25433e9d526a98831b356498756b5e0df35945aacd7aa2d6d2" + }, + "csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip": { + "original_sha256": "5667d52b8c0a959b76c013ed24411f30f5671bce0f0e3a6ed1548f3fd0a531b9", + "sha256": "6f6875f4c0754e359f9d4e6c6b18ea0dbf30e0b852ae8fc6ab5f72a8b31ccd79" + }, + "csrc/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta_hip.h": { + "original_sha256": "324ecf1d763662a0eeb5420314a26854af1989d6ad4411d48abe7051cc1eef3b", + "sha256": "9aef033c17f7e3489d53c2d3fdd431f1639329bc8cddb5909821b52117084ea3" + }, + "csrc/gfx9/decode/combine/combine.hip": { + "original_sha256": "2e68052bf893dcf6a0f08d52b7eb9b2ba259edf72a95db6ce438ce3c4a6790ca", + "sha256": "f3bbc011fbde47d569bc7cef5595a2e0c8dfd3e95f281f2e3162f8744cda4d2f" + }, + "csrc/gfx9/decode/combine/combine_hip.h": { + "original_sha256": "3d757be1e088f9b8cc4cd953592826eabb23c1f3d537f2f762acded6fdd75793", + "sha256": "5f7d6ec6edbae255b4538ef7041ff42d6e0b1173e3bc05c8c1ee3df2148e8c78" + } + } +} From 2b0cd1ae29a4ef7490ea05a6272caf22a2fdd3b8 Mon Sep 17 00:00:00 2001 From: songzhl Date: Mon, 16 Nov 2026 11:28:55 +0800 Subject: [PATCH 02/18] fix: support device graph capture for Hygon operators --- src/native/cuda/hygon/ops/add/kernel.h | 2 ++ src/native/cuda/hygon/ops/copy/kernel.h | 2 ++ src/native/cuda/hygon/ops/embedding/kernel.h | 2 ++ src/native/cuda/hygon/ops/gemm/cublas.h | 12 +++++++++++- src/native/cuda/hygon/ops/rotary_embedding/kernel.h | 2 ++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/native/cuda/hygon/ops/add/kernel.h b/src/native/cuda/hygon/ops/add/kernel.h index 53876b507..13676749d 100644 --- a/src/native/cuda/hygon/ops/add/kernel.h +++ b/src/native/cuda/hygon/ops/add/kernel.h @@ -12,6 +12,8 @@ class Operator : public CudaAdd> { public: using CudaAdd>::CudaAdd; + + bool capture_safe() const override { return true; } }; } // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/copy/kernel.h b/src/native/cuda/hygon/ops/copy/kernel.h index 159e82eda..3a3f1fc77 100644 --- a/src/native/cuda/hygon/ops/copy/kernel.h +++ b/src/native/cuda/hygon/ops/copy/kernel.h @@ -12,6 +12,8 @@ class Operator : public CudaCopy> { public: using CudaCopy>::CudaCopy; + + bool capture_safe() const override { return true; } }; } // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/embedding/kernel.h b/src/native/cuda/hygon/ops/embedding/kernel.h index ea15b0ce3..0ffe32a38 100644 --- a/src/native/cuda/hygon/ops/embedding/kernel.h +++ b/src/native/cuda/hygon/ops/embedding/kernel.h @@ -12,6 +12,8 @@ class Operator : public CudaEmbedding> { public: using CudaEmbedding>::CudaEmbedding; + + bool capture_safe() const override { return true; } }; } // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/gemm/cublas.h b/src/native/cuda/hygon/ops/gemm/cublas.h index 0ed717c11..6b97a1905 100644 --- a/src/native/cuda/hygon/ops/gemm/cublas.h +++ b/src/native/cuda/hygon/ops/gemm/cublas.h @@ -10,7 +10,17 @@ template <> class Operator : public BlasGemm> { public: - using BlasGemm>::BlasGemm; + using Base = BlasGemm>; + Operator(Tensor a, Tensor b, const std::optional c, + std::optional alpha, std::optional beta, + std::optional trans_a, std::optional trans_b, Tensor y) + : Base(a,b,c,alpha,beta,trans_a,trans_b,y) { + // Make runs before stream capture; initialize the rank's BLAS handle here. + (void)this->GetHandle(); + } + Operator(Tensor a, Tensor b, Tensor y) + : Operator(a,b,std::nullopt,std::nullopt,std::nullopt,std::nullopt,std::nullopt,y) {} + bool capture_safe() const override { return true; } }; } // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/rotary_embedding/kernel.h b/src/native/cuda/hygon/ops/rotary_embedding/kernel.h index d134065a4..179c9f7ea 100644 --- a/src/native/cuda/hygon/ops/rotary_embedding/kernel.h +++ b/src/native/cuda/hygon/ops/rotary_embedding/kernel.h @@ -12,6 +12,8 @@ class Operator : public CudaRotaryEmbedding> { public: using CudaRotaryEmbedding>::CudaRotaryEmbedding; + + bool capture_safe() const override { return true; } }; } // namespace infini::ops From e058c3a5da0c2b4de7079f8421f9cda25469cf4b Mon Sep 17 00:00:00 2001 From: songzhl Date: Mon, 16 Nov 2026 11:28:55 +0800 Subject: [PATCH 03/18] fix: align Hygon RMSNorm rounding with SGLang --- src/native/cuda/hygon/ops/rms_norm/kernel.h | 133 ++++++++++++++++++++ src/native/cuda/ops/rms_norm/kernel.cuh | 6 + 2 files changed, 139 insertions(+) diff --git a/src/native/cuda/hygon/ops/rms_norm/kernel.h b/src/native/cuda/hygon/ops/rms_norm/kernel.h index fc51c65d8..02fbe0b17 100644 --- a/src/native/cuda/hygon/ops/rms_norm/kernel.h +++ b/src/native/cuda/hygon/ops/rms_norm/kernel.h @@ -6,12 +6,145 @@ #include "native/cuda/ops/rms_norm/kernel.h" namespace infini::ops { +namespace hygon_rms_detail { + +// Warp-per-head reduction for the FP32 512-wide attention head path. +static __global__ void NormHeads(const float* input, const float* weight, + float* output, size_t heads, size_t rows, + int64_t input_row_stride, int64_t input_head_stride, + int64_t output_row_stride, int64_t output_head_stride, + float epsilon) { + const unsigned lane = threadIdx.x; + const size_t head = blockIdx.x; + const size_t row = head / heads; + if (row >= rows) return; + input += row * input_row_stride + (head % heads) * input_head_stride; + output += row * output_row_stride + (head % heads) * output_head_stride; + float squared_sum = 0.0f; +#pragma unroll + for (unsigned tile = 0; tile < 2; ++tile) { +#pragma unroll + for (unsigned element = 0; element < 8; ++element) { + const float x = input[tile * 256 + lane * 8 + element]; + squared_sum = __fmaf_rn(x, x, squared_sum); + } + } +#pragma unroll + for (unsigned offset = 16; offset; offset >>= 1) + squared_sum += __shfl_xor_sync(0xffffffff, squared_sum, offset, 32); + const float inverse_rms = rsqrtf(squared_sum / 512.0f + epsilon); + for (unsigned i = lane; i < 512; i += 32) + output[i] = __fmul_rn(__fmul_rn(input[i], inverse_rms), weight[i]); +} +static __global__ void NormKv(const float* input, const float* weight, + float* output, int64_t input_stride, + int64_t output_stride, float epsilon) { + __shared__ float partial[8]; + const unsigned thread = threadIdx.x, lane = thread % 32; + input += blockIdx.x * input_stride; + output += blockIdx.x * output_stride; + float sum = 0.0f; +#pragma unroll + for (unsigned j=0;j<2;++j) { + const float x=input[2*thread+j]; + sum=__fmaf_rn(x,x,sum); + } +#pragma unroll + for (unsigned offset=16;offset;offset>>=1) + sum+=__shfl_xor_sync(0xffffffff,sum,offset,32); + if (!lane) partial[thread/32]=sum; + __syncthreads(); + sum=partial[lane%8]; +#pragma unroll + for (unsigned offset=4;offset;offset>>=1) + sum+=__shfl_xor_sync(0xffffffff,sum,offset,8); + const float inverse_rms=rsqrtf(sum/512.0f+epsilon); +#pragma unroll + for (unsigned j=0;j<2;++j) { + const unsigned i=2*thread+j; + output[i]=__fmul_rn(__fmul_rn(input[i],inverse_rms),weight[i]); + } +} +static __global__ __launch_bounds__(512) void NormHidden(const __nv_bfloat16* input, const __nv_bfloat16* weight, + __nv_bfloat16* output, int64_t input_stride, + int64_t output_stride, size_t width, float epsilon) { + __shared__ float partial[8]; + const unsigned thread = threadIdx.x; + input += blockIdx.x * input_stride; + output += blockIdx.x * output_stride; + float squared_sum = 0.0f; +#pragma unroll + for (unsigned j = 0; j < 8; ++j) { + const float value = __bfloat162float(input[thread * 8 + j]); + squared_sum = __fmaf_rn(value, value, squared_sum); + } +#pragma unroll + for (unsigned offset = 32; offset; offset >>= 1) + squared_sum += __shfl_xor_sync(0xffffffff, squared_sum, offset, 64); + if (thread % 64 == 0) partial[thread / 64] = squared_sum; + __syncthreads(); + if (thread < 64) { + float total = thread < blockDim.x/64 ? partial[thread] : 0.0f; +#pragma unroll + for (unsigned offset = 32; offset; offset >>= 1) + total += __shfl_xor_sync(0xffffffff, total, offset, 64); + if (thread == 0) partial[0] = rsqrtf(total / static_cast(width) + epsilon); + } + __syncthreads(); + const float inverse_rms = partial[0]; +#pragma unroll + for (unsigned j = 0; j < 8; ++j) { + const unsigned index = thread * 8 + j; + const float normalized = __fmul_rn(__bfloat162float(input[index]), inverse_rms); + output[index] = __float2bfloat16_rn(__fmul_rn(normalized, __bfloat162float(weight[index]))); + } +} +} // namespace hygon_rms_detail + template <> class Operator : public CudaRmsNorm> { public: using CudaRmsNorm>::CudaRmsNorm; + + void operator()(const Tensor input, const Tensor weight, float epsilon, + Tensor out) const override { + if (input.ndim() == 3 && input.size(-1) == 512 && + input.dtype() == DataType::kFloat32 && weight.dtype() == DataType::kFloat32 && + NativeSupports(input, weight, out)) { + const size_t heads = input.size(1), rows = input.size(0); + if (!heads || !rows) return; + if (heads == 1) { + hygon_rms_detail::NormKv<<(stream_)>>>( + static_cast(input.data()),static_cast(weight.data()), + static_cast(out.data()),input.stride(0),out.stride(0),epsilon); + if (cudaGetLastError()!=cudaSuccess) throw std::runtime_error("Hygon KV RMSNorm launch failed"); + return; + } + hygon_rms_detail::NormHeads<<(stream_)>>>( + static_cast(input.data()), static_cast(weight.data()), + static_cast(out.data()), heads, rows, input.stride(0), input.stride(1), + out.stride(0), out.stride(1), epsilon); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon attention head RMSNorm launch failed"); + return; + } + if (input.ndim() == 2 && (input.size(1) == 4096 || input.size(1) == 1024) && + input.dtype() == DataType::kBFloat16 && weight.dtype() == DataType::kBFloat16 && + NativeSupports(input, weight, out)) { + if (!input.size(0)) return; + hygon_rms_detail::NormHidden<<(stream_)>>>( + static_cast(input.data()), static_cast(weight.data()), + static_cast<__nv_bfloat16*>(out.data()), input.stride(0), out.stride(0), input.size(1), epsilon); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon hidden RMSNorm launch failed"); + return; + } + CudaRmsNorm>::operator()(input, weight, epsilon, out); + } + + bool capture_safe() const override { return true; } }; } // namespace infini::ops diff --git a/src/native/cuda/ops/rms_norm/kernel.cuh b/src/native/cuda/ops/rms_norm/kernel.cuh index 91036a37f..d39777c69 100644 --- a/src/native/cuda/ops/rms_norm/kernel.cuh +++ b/src/native/cuda/ops/rms_norm/kernel.cuh @@ -53,9 +53,15 @@ __global__ void RmsNormKernel(TData* __restrict__ y, int64_t stride_y_batch, __syncthreads(); for (size_t i = threadIdx.x; i < dim; i += block_size) { + if constexpr (kDev == Device::Type::kHygon) { + const float normalized = __fmul_rn(Caster::template Cast(x_ptr[i]), rms); + y_ptr[i] = Caster::template Cast( + __fmul_rn(normalized, Caster::template Cast(w_ptr[i]))); + } else { y_ptr[i] = Caster::template Cast( Caster::template Cast(x_ptr[i]) * Caster::template Cast(w_ptr[i]) * rms); + } } } From 0f88982a94610a3d16c9a958e1f24220108449b2 Mon Sep 17 00:00:00 2001 From: songzhl Date: Mon, 16 Nov 2026 11:28:55 +0800 Subject: [PATCH 04/18] fix: align Hygon rotary embedding rounding with SGLang --- src/native/cuda/ops/rotary_embedding/kernel.cuh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/native/cuda/ops/rotary_embedding/kernel.cuh b/src/native/cuda/ops/rotary_embedding/kernel.cuh index f94804195..c19f1df40 100644 --- a/src/native/cuda/ops/rotary_embedding/kernel.cuh +++ b/src/native/cuda/ops/rotary_embedding/kernel.cuh @@ -3,6 +3,7 @@ #include #include +#include #include "native/cuda/caster.cuh" @@ -25,6 +26,21 @@ __device__ __forceinline__ void ApplyRotaryPair(T* data, const TCache* cache, const float x = Caster::template Cast(data[x_index]); const float y = Caster::template Cast(data[y_index]); + if constexpr (kDev == Device::Type::kHygon) { + if (inverse || std::is_same_v) { + // Hygon inverse and FP32 cache rotation round sine products before FMA. + const float x_sin = __fmul_rn(x, sin_value); + const float y_sin = __fmul_rn(y, sin_value); + data[x_index] = Caster::template Cast(__fmaf_rn(x, cos_value, -y_sin)); + if (inverse) { + data[y_index] = Caster::template Cast(__fmaf_rn(y, cos_value, x_sin)); + } else { + const float y_cos = __fmul_rn(y, cos_value); + data[y_index] = Caster::template Cast(__fmaf_rn(x, sin_value, y_cos)); + } + return; + } + } data[x_index] = Caster::template Cast(x * cos_value - y * sin_value); data[y_index] = Caster::template Cast(y * cos_value + x * sin_value); } From 35685eed402bd7019be57ff7b9373e4fa8c9546c Mon Sep 17 00:00:00 2001 From: songzhl Date: Wed, 18 Nov 2026 21:02:37 +0800 Subject: [PATCH 05/18] perf: optimize DeepSeek V4 Flash mHC on Hygon --- .../hygon/ops/deepseek_v4_mhc_post/kernel.h | 22 ++++- .../ops/deepseek_v4_mhc_post/residual_mix.cuh | 29 ++++++ .../ops/deepseek_v4_mhc_pre/decoder_mix.cuh | 97 +++++++++++++++++++ .../decoder_projection.cuh | 95 ++++++++++++++++++ .../hygon/ops/deepseek_v4_mhc_pre/kernel.h | 41 +++++++- 5 files changed, 274 insertions(+), 10 deletions(-) create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_post/residual_mix.cuh create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/decoder_mix.cuh create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_mhc_pre/decoder_projection.cuh diff --git a/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h index 4ce8fbdcf..7c2cdf076 100644 --- a/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h +++ b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/kernel.h @@ -2,6 +2,7 @@ #include #include #include "base/deepseek_v4_mhc_post.h" +#include "residual_mix.cuh" namespace infini::ops { namespace v4_mhc_post { @@ -37,11 +38,22 @@ class Operator : public DeepseekV4MhcPos } void operator()(Tensor x,Tensor r,Tensor p,Tensor m,Tensor y) const override { if(!supports(x,r,p,m,y)) throw std::invalid_argument("Invalid DeepSeek V4 mHC post"); - v4_mhc_post::mix<<(std::min((y.numel()-1)/128+1,65535)),128,0, - static_cast(stream_)>>>( - static_cast(x.data()),static_cast(r.data()), - static_cast(p.data()),static_cast(m.data()), - static_cast<__nv_bfloat16*>(y.data()),y.numel(),r.size(2)); + // Reuse inputs across streams for prefill. M<4 retains the original kernel: + // the extra occupancy is faster for the small decode shape on BW/gfx936. + if (r.size(2) == 4096 && r.size(0) >= 4) { + const auto elements = r.size(0) * r.size(2); + v4_mhc_post_opt::mix_reuse<<(std::min((elements-1)/256+1,65535)),256,0, + static_cast(stream_)>>>( + static_cast(x.data()),static_cast(r.data()), + static_cast(p.data()),static_cast(m.data()), + static_cast<__nv_bfloat16*>(y.data()),r.size(0),r.size(2)); + } else { + v4_mhc_post::mix<<(std::min((y.numel()-1)/128+1,65535)),128,0, + static_cast(stream_)>>>( + static_cast(x.data()),static_cast(r.data()), + static_cast(p.data()),static_cast(m.data()), + static_cast<__nv_bfloat16*>(y.data()),y.numel(),r.size(2)); + } if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC post launch failed"); } }; diff --git a/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/residual_mix.cuh b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/residual_mix.cuh new file mode 100644 index 000000000..a65bbe55d --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_mhc_post/residual_mix.cuh @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +namespace infini::ops::v4_mhc_post_opt { +// Reuse the four residual inputs for the four output streams. Do not reassociate +// the residual-first FMA chain or change the final round-toward-zero conversion. +static __global__ void mix_reuse(const __nv_bfloat16* activation,const __nv_bfloat16* residual, + const float* post,const float* matrix,__nv_bfloat16* out,size_t rows,size_t hidden) { + const size_t count=rows*hidden; + for(size_t index=blockIdx.x*blockDim.x+threadIdx.x;index +#include + +namespace infini::ops::v4_mhc_opt { +static __device__ __forceinline__ float ordered_splits(const float* values,size_t stride) { + float partials[4]={}; + for(unsigned split=0;split<32;split+=4) + for(unsigned group=0;group<4;++group)partials[group]+=values[(split+group)*stride]; + return ((partials[0]+partials[1])+partials[2])+partials[3]; +} +static __device__ __forceinline__ float ordered_pairs(float a,float b,float c,float d) { + return (a+b)+(c+d); +} +static __device__ __forceinline__ float wave_value(float value,unsigned source) { + return __shfl(value,source,64); +} +template +static __device__ __forceinline__ float logistic(float value) { + return 1.0f/(1.0f+(Split?__builtin_amdgcn_exp2f(-value*1.4426950408889634f):expf(-value))); +} + +// Lanes 0..3 produce pre, 4..7 post, and 8..23 the 4x4 matrix. Every matrix +// element follows the scalar reference's exact FP32 operation/reduction order. +template +static __global__ void mix_parallel(const __nv_bfloat16* residual,const float* values, + const float* scales,const float* bias,__nv_bfloat16* collapsed,float* post, + float* combination,size_t rows,size_t hidden,float eps,int64_t iterations, + const float* squares,float norm_eps) { + __shared__ float pre[4]; + const unsigned tid=threadIdx.x; + for(size_t row=blockIdx.x;row(value*scales[0]+bias[tid])+eps; + if(tid>=4 && tid<8)post[row*4+tid-4]=2*logistic(value*scales[1]+bias[tid]); + if(tid>=8 && tid<24) { + const unsigned i=(tid-8)/4,j=(tid-8)%4,base=8+i*4; + float matrix=value*scales[2]+bias[tid]; + const float m0=wave_value(matrix,base),m1=wave_value(matrix,base+1); + const float m2=wave_value(matrix,base+2),m3=wave_value(matrix,base+3); + float maximum=-INFINITY; + maximum=fmaxf(maximum,m0);maximum=fmaxf(maximum,m1); + maximum=fmaxf(maximum,m2);maximum=fmaxf(maximum,m3); + matrix=expf(matrix-maximum); + const float e0=wave_value(matrix,base),e1=wave_value(matrix,base+1); + const float e2=wave_value(matrix,base+2),e3=wave_value(matrix,base+3); + float total; + if constexpr(Split)total=ordered_pairs(e0,e1,e2,e3); + else {total=0.0f;total+=e0;total+=e1;total+=e2;total+=e3;} + matrix=matrix/total+eps; + for(int64_t iteration=0;iteration +#include + +namespace infini::ops::v4_mhc_opt { +using Float2 = float __attribute__((ext_vector_type(2))); +using Float4 = float __attribute__((ext_vector_type(4))); +constexpr unsigned Splits=32, Outputs=24, Threads=128; + +// Adjacent BF16/FP32 loads preserve the original MMAC4/MMAC8 instruction order. +// Only the first output tile needs the squared norm; keep that branch outside K. +template +static __device__ __forceinline__ void split_body( + const __nv_bfloat16* x,const float* w,float* dots,float* squares, + size_t rows,size_t width) { + const unsigned lane=threadIdx.x,group=lane/16; + const size_t row=blockIdx.x*16+lane%16,col=blockIdx.y*16+lane%16; + const size_t start=blockIdx.z*(width/Splits); + Float4 dot={}; float square=0.0f; + for(size_t tile=0;tile(x+row*width+k) : 0u; + a0=__uint_as_float(raw<<16); a1=__uint_as_float(raw&0xffff0000u); + const Float2 b=col(w+col*width+k) : Float2{}; + b0=b[0]; b1=b[1]; + } else { + a0=row=rows)return; + for(unsigned i=0;i<4;++i) { + const size_t column=blockIdx.y*16+group+i*4; + if(column +static __global__ void project_split(const __nv_bfloat16* x,const float* w, + float* dots,float* squares,size_t rows,size_t width) { + if(blockIdx.y==0)split_body(x,w,dots,squares,rows,width); + else split_body(x,w,dots,squares,rows,width); +} + +// Larger prefill fallback: reuse each input load and squared norm across four +// columns, retaining the original per-column K order and 128-thread reduction. +static __global__ void project_tiled(const __nv_bfloat16* x,const float* w,float* values, + size_t rows,size_t width,size_t outputs,float eps) { + __shared__ float reduce[5][Threads]; + const unsigned lane=threadIdx.x; + const size_t column=blockIdx.y*4; + for(size_t row=blockIdx.x;row #include "base/deepseek_v4_mhc_pre.h" #include "head.cuh" +#include "decoder_projection.cuh" +#include "decoder_mix.cuh" +#include namespace infini::ops { namespace v4_mhc { @@ -227,18 +230,46 @@ class Operator : public DeepseekV4MhcPre return; } float* squares = nullptr; + // Only the measured Flash decoder shape opts in. Keep the head and other + // hidden widths on their established paths. + const bool flash_decoder = it > 0 && x.size(2) == 4096; if (use_split_projection(rows,x.size(2),it)) { squares = v + rows*kSplits*kWidth; const dim3 split_grid((rows+15)/16,2,kSplits); - if (rows > 64) project_split<<>>(residual,weight,v,squares,rows,width); - else project_split<<>>(residual,weight,v,squares,rows,width); + if (flash_decoder) { + // Contiguous views can still start at a scalar offset. Such inputs use + // scalar loads rather than assuming vector alignment. + const bool aligned = (reinterpret_cast(residual) & 3u) == 0 + && (reinterpret_cast(weight) & 7u) == 0; + if (aligned) { + if (rows > 64) v4_mhc_opt::project_split<<>>(residual,weight,v,squares,rows,width); + else v4_mhc_opt::project_split<<>>(residual,weight,v,squares,rows,width); + } else { + if (rows > 64) v4_mhc_opt::project_split<<>>(residual,weight,v,squares,rows,width); + else v4_mhc_opt::project_split<<>>(residual,weight,v,squares,rows,width); + } + } else { + if (rows > 64) project_split<<>>(residual,weight,v,squares,rows,width); + else project_split<<>>(residual,weight,v,squares,rows,width); + } + } else if (flash_decoder) { + v4_mhc_opt::project_tiled<<>>(residual,weight,v,rows,width,w.size(0),float(e)); } else { project<<>>(residual,weight,v,rows,width,w.size(0),float(e)); } if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC projection launch failed"); - mix<<>>(residual,v,static_cast(s.data()), - static_cast(b.data()),static_cast<__nv_bfloat16*>(y.data()), - static_cast(p.data()),static_cast(c.data()),x.size(0),x.size(2),float(m),it,squares,float(e)); + if (flash_decoder) { + if (squares) v4_mhc_opt::mix_parallel<<>>(residual,v,static_cast(s.data()), + static_cast(b.data()),static_cast<__nv_bfloat16*>(y.data()), + static_cast(p.data()),static_cast(c.data()),rows,x.size(2),float(m),it,squares,float(e)); + else v4_mhc_opt::mix_parallel<<>>(residual,v,static_cast(s.data()), + static_cast(b.data()),static_cast<__nv_bfloat16*>(y.data()), + static_cast(p.data()),static_cast(c.data()),rows,x.size(2),float(m),it,nullptr,float(e)); + } else { + mix<<>>(residual,v,static_cast(s.data()), + static_cast(b.data()),static_cast<__nv_bfloat16*>(y.data()), + static_cast(p.data()),static_cast(c.data()),x.size(0),x.size(2),float(m),it,squares,float(e)); + } if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 mHC pre launch failed"); } private: From 02d009fa362a0a24b1add3175e8f5859d10654a1 Mon Sep 17 00:00:00 2001 From: songzhl Date: Wed, 18 Nov 2026 21:02:38 +0800 Subject: [PATCH 06/18] fix: enable device graph capture for Hygon Argmax --- src/native/cuda/hygon/ops/argmax/kernel.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/native/cuda/hygon/ops/argmax/kernel.h b/src/native/cuda/hygon/ops/argmax/kernel.h index 02a2829ee..e51c4819b 100644 --- a/src/native/cuda/hygon/ops/argmax/kernel.h +++ b/src/native/cuda/hygon/ops/argmax/kernel.h @@ -12,6 +12,10 @@ class Operator : public CudaArgmax> { public: using CudaArgmax>::CudaArgmax; + + // Construction and workspace preparation happen before stream capture. + // Launch only enqueues CUB's reduction and the index store on the supplied stream. + bool capture_safe() const override { return true; } }; } // namespace infini::ops From e8698a47e6614883a1f4018ec2fab9b2757683dc Mon Sep 17 00:00:00 2001 From: songzhl Date: Wed, 18 Nov 2026 21:02:38 +0800 Subject: [PATCH 07/18] feat: add native Hygon MoE block alignment --- .../hygon/ops/moe_align_block_size/kernel.cu | 81 +++++++++++++++++ .../hygon/ops/moe_align_block_size/kernel.cuh | 86 +++++++++++++++++++ .../hygon/ops/moe_align_block_size/kernel.h | 35 ++++++++ 3 files changed, 202 insertions(+) create mode 100644 src/native/cuda/hygon/ops/moe_align_block_size/kernel.cu create mode 100644 src/native/cuda/hygon/ops/moe_align_block_size/kernel.cuh create mode 100644 src/native/cuda/hygon/ops/moe_align_block_size/kernel.h diff --git a/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cu b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cu new file mode 100644 index 000000000..4efde0f4f --- /dev/null +++ b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cu @@ -0,0 +1,81 @@ +#include + +#include +#include +#include + +#include "native/cuda/hygon/ops/moe_align_block_size/kernel.cuh" +#include "native/cuda/hygon/ops/moe_align_block_size/kernel.h" + +namespace infini::ops { +namespace { + +class DeviceGuard { + public: + explicit DeviceGuard(int device_index) { + auto status = cudaGetDevice(&previous_device_); + assert(status == cudaSuccess && + "`MoeAlignBlockSize` failed to query the current CUDA device"); + + if (previous_device_ != device_index) { + status = cudaSetDevice(device_index); + assert(status == cudaSuccess && + "`MoeAlignBlockSize` failed to select the input CUDA device"); + restore_ = true; + } + } + + ~DeviceGuard() { + if (restore_) { + const auto status = cudaSetDevice(previous_device_); + assert(status == cudaSuccess && + "`MoeAlignBlockSize` failed to restore the CUDA device"); + } + } + + private: + int previous_device_{0}; + + bool restore_{false}; +}; + +} // namespace + +Operator::Operator( + const Tensor topk_ids, const std::optional expert_map, + const int64_t num_experts, const int64_t block_size, + Tensor sorted_token_ids, Tensor experts_ids, Tensor num_tokens_post_pad) + : MoeAlignBlockSize{topk_ids, expert_map, num_experts, + block_size, sorted_token_ids, experts_ids, + num_tokens_post_pad}, + device_index_{topk_ids.device().index()} {} + +void Operator::Run( + const Tensor topk_ids, const std::optional maybe_expert_map, + const int64_t num_experts, const int64_t block_size, + Tensor sorted_token_ids, Tensor experts_ids, + Tensor num_tokens_post_pad) const { + DeviceGuard device_guard{device_index_}; + + constexpr int32_t kThreads = 256; + const auto shared_memory_size = + static_cast(num_experts_) * sizeof(int32_t); + moe_align_block_size_hygon_detail::MoeAlignBlockSizeKernel<<< + 1, kThreads, shared_memory_size, static_cast(stream_)>>>( + reinterpret_cast(topk_ids.data()), + maybe_expert_map + ? reinterpret_cast(maybe_expert_map->data()) + : nullptr, + reinterpret_cast(sorted_token_ids.data()), + reinterpret_cast(experts_ids.data()), + reinterpret_cast(num_tokens_post_pad.data()), numel_, + static_cast(num_experts_), static_cast(block_size_), + sorted_token_ids_size_, experts_ids_size_, + maybe_expert_map ? static_cast(maybe_expert_map->numel()) : 0); + + const auto status = cudaGetLastError(); + assert(status == cudaSuccess && + "`MoeAlignBlockSize` CUDA kernel launch failed"); +} + +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cuh b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cuh new file mode 100644 index 000000000..2568645ca --- /dev/null +++ b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.cuh @@ -0,0 +1,86 @@ +#ifndef INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_CUH_ +#define INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_CUH_ + +#include +#include + +namespace infini::ops { +namespace moe_align_block_size_hygon_detail { + +__device__ __forceinline__ int32_t MapExpertId(int32_t expert_id, + const int32_t* expert_map, + int32_t num_experts, + int32_t map_size) { + if (expert_id < 0 || expert_id >= (expert_map ? map_size : num_experts)) { + return -1; + } + if (expert_map != nullptr) { + expert_id = expert_map[expert_id]; + } + + return expert_id >= 0 && expert_id < num_experts ? expert_id : -1; +} + +__global__ void MoeAlignBlockSizeKernel( + const int32_t* __restrict__ topk_ids, + const int32_t* __restrict__ expert_map, + int32_t* __restrict__ sorted_token_ids, int32_t* __restrict__ experts_ids, + int32_t* __restrict__ num_tokens_post_pad, size_t numel, + int32_t num_experts, int32_t block_size, size_t sorted_token_ids_size, + size_t experts_ids_size, int32_t map_size) { + extern __shared__ int32_t expert_offsets[]; + + for (size_t i = threadIdx.x; i < sorted_token_ids_size; i += blockDim.x) { + sorted_token_ids[i] = static_cast(numel); + } + for (size_t i = threadIdx.x; i < experts_ids_size; i += blockDim.x) { + experts_ids[i] = -1; + } + for (int32_t i = threadIdx.x; i < num_experts; i += blockDim.x) { + expert_offsets[i] = 0; + } + if (threadIdx.x == 0) { + *num_tokens_post_pad = 0; + } + __syncthreads(); + + for (size_t i = threadIdx.x; i < numel; i += blockDim.x) { + const auto expert_id = + MapExpertId(topk_ids[i], expert_map, num_experts, map_size); + if (expert_id >= 0) { + atomicAdd(&expert_offsets[expert_id], 1); + } + } + __syncthreads(); + + if (threadIdx.x == 0) { + int32_t token_offset = 0; + for (int32_t expert_id = 0; expert_id < num_experts; ++expert_id) { + const int32_t count = expert_offsets[expert_id]; + expert_offsets[expert_id] = token_offset; + const int32_t padded_count = + (count + block_size - 1) / block_size * block_size; + for (int32_t i = 0; i < padded_count; i += block_size) { + experts_ids[(token_offset + i) / block_size] = expert_id; + } + token_offset += padded_count; + } + *num_tokens_post_pad = token_offset; + } + __syncthreads(); + + if (threadIdx.x == 0) { + for (size_t i = 0; i < numel; ++i) { + const auto expert_id = + MapExpertId(topk_ids[i], expert_map, num_experts, map_size); + if (expert_id >= 0) { + sorted_token_ids[expert_offsets[expert_id]++] = static_cast(i); + } + } + } +} + +} // namespace moe_align_block_size_hygon_detail +} // namespace infini::ops + +#endif // INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_CUH_ diff --git a/src/native/cuda/hygon/ops/moe_align_block_size/kernel.h b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.h new file mode 100644 index 000000000..0984b5205 --- /dev/null +++ b/src/native/cuda/hygon/ops/moe_align_block_size/kernel.h @@ -0,0 +1,35 @@ +#ifndef INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_H_ +#define INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_H_ + +#include + +#include "base/moe_align_block_size.h" + +namespace infini::ops { + +template <> +class Operator + : public MoeAlignBlockSize { + public: + Operator(const Tensor topk_ids, const std::optional expert_map, + const int64_t num_experts, const int64_t block_size, + Tensor sorted_token_ids, Tensor experts_ids, + Tensor num_tokens_post_pad); + + using MoeAlignBlockSize::operator(); + + bool capture_safe() const override { return true; } + + protected: + void Run(const Tensor topk_ids, const std::optional maybe_expert_map, + const int64_t num_experts, const int64_t block_size, + Tensor sorted_token_ids, Tensor experts_ids, + Tensor num_tokens_post_pad) const override; + + private: + int device_index_{0}; +}; + +} // namespace infini::ops + +#endif // INFINI_OPS_HYGON_MOE_ALIGN_BLOCK_SIZE_KERNEL_H_ From c0cdcb5180a86071f506acaa1b17f343e30de460 Mon Sep 17 00:00:00 2001 From: songzhl Date: Wed, 18 Nov 2026 21:02:38 +0800 Subject: [PATCH 08/18] fix: preserve expert remapping and bias rounding in Torch MoE --- .../ops/moe_fused_dense/moe_fused_dense.cc | 70 +++++++++++ .../ops/moe_fused_dense/moe_fused_dense.h | 15 +++ tests/test_moe_fused_dense_torch.py | 118 ++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 src/torch/ops/moe_fused_dense/moe_fused_dense.cc create mode 100644 src/torch/ops/moe_fused_dense/moe_fused_dense.h create mode 100644 tests/test_moe_fused_dense_torch.py diff --git a/src/torch/ops/moe_fused_dense/moe_fused_dense.cc b/src/torch/ops/moe_fused_dense/moe_fused_dense.cc new file mode 100644 index 000000000..c247b514d --- /dev/null +++ b/src/torch/ops/moe_fused_dense/moe_fused_dense.cc @@ -0,0 +1,70 @@ +#include "torch/ops/moe_fused_dense/moe_fused_dense.h" +#include "torch/instantiate.h" +#include "torch/stream_guard.h" +#include "torch/tensor_.h" +#include +namespace infini::ops { +template +void Operator::operator()( + Tensor output, const Tensor hidden, const Tensor w13, const Tensor w2, + const Tensor weights, const Tensor ids, const Tensor sorted, + const Tensor experts, const Tensor total, size_t block_size, + const std::optional bias1, const std::optional bias2) const { + this->Invocation(output, hidden, w13, w2, weights, ids, sorted, experts, total, + block_size, bias1, bias2); + const int device = output.device().index(); + const TorchStreamGuard guard{this->stream_, device}; + const auto view = [device](const Tensor &x) { + return ToAtenTensor(const_cast(x.data()), x.shape(), x.strides(), x.dtype(), device); + }; + auto x = view(hidden), a = view(w13), b = view(w2), route = view(weights); + auto y = view(output); + const auto n = x.size(0), k = ids.size(1), h = x.size(1), mid = b.size(2); + // The alignment metadata carries local expert IDs after EP remapping. + // Raw top-k IDs can still be global, so reconstruct the per-pair local IDs. + auto pair_ids = view(sorted).to(at::kLong); + auto positions = at::arange(pair_ids.numel(), pair_ids.options()); + auto active = positions.lt(view(total).to(at::kLong).reshape({1})) + .logical_and(pair_ids.ge(0)).logical_and(pair_ids.lt(n * k)); + auto active_pairs = pair_ids.masked_select(active); + auto blocks = at::floor_divide(positions.masked_select(active), static_cast(block_size)); + auto local_experts = view(experts).to(at::kLong).index_select(0, blocks); + auto indices = at::full({n * k}, -1, pair_ids.options()); + indices.index_copy_(0, active_pairs, local_experts); + indices = indices.view({n, k}); + // Bound temporary expert gathers to one token's top-k, independent of prefill length. + for (int64_t token = 0; token < n; ++token) { + auto expert = indices.select(0, token); + auto valid = expert.ge(0).logical_and(expert.lt(a.size(0))); + auto safe_expert = expert.clamp(0, a.size(0) - 1); + auto wa = a.index_select(0, safe_expert); + auto wb = b.index_select(0, safe_expert); + auto input = x.narrow(0, token, 1).unsqueeze(0).expand({k, 1, h}).contiguous(); + auto gate_up = at::bmm(input, wa.transpose(1, 2)).squeeze(1); + if (bias1) { + at::Tensor accumulated; + if constexpr (kDev == Device::Type::kCpu) { + accumulated = at::bmm(input.to(at::kFloat), wa.transpose(1, 2).to(at::kFloat)); + } else { + accumulated = at::bmm(input, wa.transpose(1, 2), at::kFloat); + } + gate_up = (accumulated.squeeze(1) + view(*bias1).index_select(0, safe_expert).to(at::kFloat)).to(x.scalar_type()); + } + auto gate = gate_up.narrow(1, 0, mid).to(at::kFloat); + auto up = gate_up.narrow(1, mid, mid).to(at::kFloat); + auto activated = (at::silu(gate) * up).to(x.scalar_type()).unsqueeze(1); + at::Tensor down; + if constexpr (kDev == Device::Type::kCpu) { + down = at::bmm(activated.to(at::kFloat), wb.transpose(1, 2).to(at::kFloat)); + } else { + down = at::bmm(activated, wb.transpose(1, 2), at::kFloat); + } + down = down.squeeze(1); + if (bias2) down = down + view(*bias2).index_select(0, safe_expert).to(at::kFloat); + auto scaled = down * route.select(0, token).unsqueeze(1); + auto weighted = at::where(valid.unsqueeze(1), scaled, at::zeros_like(scaled)).to(x.scalar_type()); + y.select(0, token).copy_(weighted.to(at::kFloat).sum(0)); + } +} +INFINI_OPS_INSTANTIATE_TORCH_OPERATOR(MoeFusedDense, 2) +} // namespace infini::ops diff --git a/src/torch/ops/moe_fused_dense/moe_fused_dense.h b/src/torch/ops/moe_fused_dense/moe_fused_dense.h new file mode 100644 index 000000000..32e96dc40 --- /dev/null +++ b/src/torch/ops/moe_fused_dense/moe_fused_dense.h @@ -0,0 +1,15 @@ +#pragma once +#include "base/moe_fused_dense.h" +namespace infini::ops { +template +class Operator : public MoeFusedDense { + public: + using MoeFusedDense::MoeFusedDense; + using MoeFusedDense::operator(); + void operator()(Tensor output, const Tensor hidden, const Tensor w13, + const Tensor w2, const Tensor weights, const Tensor ids, + const Tensor sorted, const Tensor experts, const Tensor total, + size_t block_size, const std::optional bias1 = std::nullopt, + const std::optional bias2 = std::nullopt) const override; +}; +} // namespace infini::ops diff --git a/tests/test_moe_fused_dense_torch.py b/tests/test_moe_fused_dense_torch.py new file mode 100644 index 000000000..e23d4f9a0 --- /dev/null +++ b/tests/test_moe_fused_dense_torch.py @@ -0,0 +1,118 @@ +"""CPU regression for the Torch MoE backend's routing map and bias boundaries.""" +import importlib.util +import os +from pathlib import Path +import subprocess +import sysconfig +import tempfile +import unittest + +import torch +import torch.nn.functional as F +from torch.utils.cpp_extension import include_paths + +CPP = r''' +#include +#include +#include +#include +#include +at::Tensor run_moe(at::Tensor x, at::Tensor a, at::Tensor b, at::Tensor route, + at::Tensor ids, at::Tensor sorted, at::Tensor experts, + at::Tensor total, int64_t block, std::vector biases) { + using namespace infini::ops; + auto output = at::empty_like(x); + const auto wrap = [](const at::Tensor &t) { + auto type = t.scalar_type() == at::kFloat ? DataType::kFloat32 + : t.scalar_type() == at::kInt ? DataType::kInt32 + : t.scalar_type() == at::kHalf ? DataType::kFloat16 : DataType::kBFloat16; + return Tensor(const_cast(t.data_ptr()), + std::vector(t.sizes().begin(), t.sizes().end()), + type, Device{Device::Type::kCpu}, + std::vector(t.strides().begin(), t.strides().end())); + }; + std::optional bias1, bias2; + if (!biases.empty()) { bias1.emplace(wrap(biases.at(0))); bias2.emplace(wrap(biases.at(1))); } + MoeFusedDense::Call(wrap(output), wrap(x), wrap(a), wrap(b), wrap(route), wrap(ids), + wrap(sorted), wrap(experts), wrap(total), static_cast(block), bias1, bias2); + return output; +} +PYBIND11_MODULE(_moe_dense_probe, m) { m.def("run_moe", &run_moe); } +''' + + +@unittest.skipUnless(os.environ.get('INFINI_ROOT'), 'requires an installed InfiniOps') +class MoeFusedDenseTorchTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.temp = tempfile.TemporaryDirectory() + folder = Path(cls.temp.name) + source = folder / 'probe.cpp' + source.write_text(CPP) + extension = folder / ('_moe_dense_probe' + sysconfig.get_config_var('EXT_SUFFIX')) + root = Path(os.environ['INFINI_ROOT']) + torchlib = Path(torch.__file__).parent / 'lib' + command = ['g++', '-shared', '-fPIC', '-std=c++17', '-O1', '-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI)), str(source), '-o', str(extension)] + for directory in [*include_paths(), sysconfig.get_paths()['include'], str(root/'include')]: + command.extend(['-I', directory]) + command.extend(['-L'+str(root/'lib'), '-linfiniops', '-L'+str(torchlib), '-ltorch', '-ltorch_cpu', '-ltorch_python', '-lc10', '-Wl,-rpath,'+str(root/'lib'), '-Wl,-rpath,'+str(torchlib)]) + subprocess.run(command, check=True) + spec = importlib.util.spec_from_file_location('_moe_dense_probe', extension) + cls.module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cls.module) + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + def check_case(self, remapped, bias): + torch.manual_seed(123) + dtype = torch.bfloat16 + x = (torch.randn(2, 8)*0.5).to(dtype) + a = (torch.randn(2, 8, 8)*0.5).to(dtype) + b = (torch.randn(2, 8, 4)*0.5).to(dtype) + route = torch.tensor([[0.25, 0.75], [0.625, 0.375]], dtype=torch.float32) + ids = torch.tensor([[0, 3], [2, 1]] if remapped else [[0, 1], [1, 0]], dtype=torch.int32) + # Every local expert owns one padded block; remaining entries are sentinels. + sorted_ids = torch.full((16,), 4, dtype=torch.int32) + experts = torch.tensor([0, 1, -1, -1], dtype=torch.int32) + if remapped: + sorted_ids[0], sorted_ids[4] = 2, 0 + mapping = {0: 1, 2: 0} + else: + sorted_ids[:2] = torch.tensor([0, 3]); sorted_ids[4:6] = torch.tensor([1, 2]) + mapping = {0: 0, 1: 1} + biases = [(torch.randn(2, 8)*0.1).to(dtype), (torch.randn(2, 8)*0.1).to(dtype)] if bias else [] + actual = self.module.run_moe(x, a, b, route, ids, sorted_ids, experts, torch.tensor([8],dtype=torch.int32), 4, biases) + expected = torch.zeros_like(x) + for token in range(2): + parts = [] + for slot in range(2): + expert = mapping.get(int(ids[token, slot])) + if expert is None: + continue + gate_up = F.linear(x[token].float(), a[expert].float()) + if bias: + gate_up += biases[0][expert].float() + gate_up = gate_up.to(dtype) + activated = (F.silu(gate_up[:4].float())*gate_up[4:].float()).to(dtype) + down = F.linear(activated.float(), b[expert].float()) + if bias: + down += biases[1][expert].float() + parts.append((down*route[token, slot]).to(dtype).float()) + if parts: + expected[token] = torch.stack(parts).sum(0).to(dtype) + torch.testing.assert_close(actual, expected, rtol=0.0, atol=0.002) + + def test_tensor_parallel_routes(self): + self.check_case(False, False) + + def test_remapped_local_experts_and_absent_routes(self): + self.check_case(True, False) + + def test_bias_is_added_before_reduced_precision_rounding(self): + self.check_case(True, True) + + +if __name__ == '__main__': + unittest.main() From 9baca9ffa2865a726c646379cc0706f15f14ff64 Mon Sep 17 00:00:00 2001 From: songzhl Date: Wed, 18 Nov 2026 21:02:38 +0800 Subject: [PATCH 09/18] fix: link Python for Hygon FlashAttention builds --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf0cb2f4e..9b6afc6ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,7 +283,7 @@ endif() # consume DLPack views and their own runtime. Discover Torch lazily only when # native Torch wrappers or a selected linked Torch transport need it. macro(infiniops_find_torch) - if(WITH_MARS AND WITH_TORCH AND WITH_LINKED) + if((WITH_MARS OR WITH_HYGON) AND WITH_TORCH AND WITH_LINKED) find_package(Python COMPONENTS Interpreter Development.Embed REQUIRED) else() find_package(Python COMPONENTS Interpreter REQUIRED) @@ -351,10 +351,10 @@ macro(infiniops_find_torch) -Wl,--no-as-needed ${TORCH_HIP_LIB} ${C10_HIP_LIB} -Wl,--as-needed) endif() - if(WITH_MARS AND WITH_TORCH AND WITH_LINKED) - # The Mars FlashAttention wheel loads libtorch_python without declaring + if((WITH_MARS OR WITH_HYGON) AND WITH_TORCH AND WITH_LINKED) + # The Mars/Hygon FlashAttention wheels load libtorch_python without declaring # its libpython dependency, so keep the embedding library in our chain. - list(APPEND TORCH_LIBRARIES Python::Python) + list(APPEND TORCH_LIBRARIES -Wl,--no-as-needed Python::Python -Wl,--as-needed) endif() if(WITH_METAX) From 945af9a5a324a4d180aba2e40ddd45ce209ac162 Mon Sep 17 00:00:00 2001 From: 2506363862 <2506363862@qq.com> Date: Thu, 19 Nov 2026 16:56:54 +0800 Subject: [PATCH 10/18] feat(hygon): add optimized VideoNSA operators --- docs/videonsa_hygon_migration.md | 70 + src/CMakeLists.txt | 8 + src/base/nsa_compress_paged_cache.h | 41 + src/base/nsa_paged_attention.h | 36 + src/base/nsa_videonsa_common.h | 22 + src/base/nsa_videonsa_prefill.h | 22 + src/base/nsa_videonsa_selector.h | 21 + .../cuda/hygon/ops/layer_norm/kernel.cu | 89 + src/native/cuda/hygon/ops/layer_norm/kernel.h | 182 ++ .../cuda/hygon/ops/layer_norm/view_checks.h | 76 + src/native/cuda/hygon/ops/mrope/kernel.h | 13 + .../nsa_compress_paged_cache/arithmetic.cuh | 100 ++ .../ops/nsa_compress_paged_cache/kernel.h | 49 + .../ops/nsa_paged_attention/arithmetic.cuh | 249 +++ .../hygon/ops/nsa_paged_attention/kernel.h | 97 ++ .../hygon/ops/nsa_videonsa_prefill/kernel.h | 51 + .../legacy/attention_fallback.cu | 707 ++++++++ .../legacy/compression.cu | 372 ++++ .../legacy/compression_wmma.cuh | 99 ++ .../ops/nsa_videonsa_prefill/legacy/gate.cu | 69 + .../legacy/pair_exact.cuh | 25 + .../nsa_videonsa_prefill/legacy/selected.cu | 157 ++ .../legacy/selected_fallback.cu | 104 ++ .../legacy/selected_parent.cu | 1538 +++++++++++++++++ .../nsa_videonsa_prefill/legacy/selector.cu | 348 ++++ .../ops/nsa_videonsa_prefill/legacy/window.cu | 192 ++ .../legacy/window_wmma.cuh | 125 ++ .../hygon/ops/nsa_videonsa_selector/kernel.h | 17 + 28 files changed, 4879 insertions(+) create mode 100644 docs/videonsa_hygon_migration.md create mode 100644 src/base/nsa_compress_paged_cache.h create mode 100644 src/base/nsa_paged_attention.h create mode 100644 src/base/nsa_videonsa_common.h create mode 100644 src/base/nsa_videonsa_prefill.h create mode 100644 src/base/nsa_videonsa_selector.h create mode 100644 src/native/cuda/hygon/ops/layer_norm/kernel.cu create mode 100644 src/native/cuda/hygon/ops/layer_norm/kernel.h create mode 100644 src/native/cuda/hygon/ops/layer_norm/view_checks.h create mode 100644 src/native/cuda/hygon/ops/mrope/kernel.h create mode 100644 src/native/cuda/hygon/ops/nsa_compress_paged_cache/arithmetic.cuh create mode 100644 src/native/cuda/hygon/ops/nsa_compress_paged_cache/kernel.h create mode 100644 src/native/cuda/hygon/ops/nsa_paged_attention/arithmetic.cuh create mode 100644 src/native/cuda/hygon/ops/nsa_paged_attention/kernel.h create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/kernel.h create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/attention_fallback.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression_wmma.cuh create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/gate.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/pair_exact.cuh create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_fallback.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_parent.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selector.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window.cu create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window_wmma.cuh create mode 100644 src/native/cuda/hygon/ops/nsa_videonsa_selector/kernel.h diff --git a/docs/videonsa_hygon_migration.md b/docs/videonsa_hygon_migration.md new file mode 100644 index 000000000..894701f7c --- /dev/null +++ b/docs/videonsa_hygon_migration.md @@ -0,0 +1,70 @@ +# VideoNSA on the separated InfiniOps architecture + +This migration retains the qualified Hygon/gfx936 kernels from the indicator04 +szl-based implementation (InfiniCore commit 772f0c237a2cd9560f6e99e8955d3529f0f68590). +The arithmetic translation units and auxiliary headers under +`src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/` are copied without edits. +The old descriptor ABI is replaced by ordinary InfiniOps operator keys. + +## Interfaces + +- `NsaVideonsaPrefill(q,k,v,g_cmp,g_slc,g_swa,top,block,window,out)` executes + compression, compression attention, selection, selected attention, window + attention and gate combination. It includes K/V packing where required. +- `NsaVideonsaSelector(q,k,lse,top,block,scale,out)` returns selector IDs. +- The paged decode keys are `NsaCompressPagedCache` and `NsaPagedAttention`. + +Prefill Q is contiguous `[B,T,Hq,128]`; K/V are contiguous `[B,T,Hkv,128]`. +Q/K/V share FP16 or BF16. Gates are contiguous `[B,T,Hq]` and share Q dtype or +FP32; output has Q shape and gate dtype. B is 1..8, T is 1..8192, +Hq is 1..1024, Hkv is 1..32, and Hq/Hkv is an integer at most 32. +Block is a power of two in 2..128; top is 1..min(32,block/2). Window is +nonnegative. Full prefill uses the original D^-1/2 scale. + +Selector K is `[B,ceil(T/block),Hkv,128]`; LSE is contiguous FP32 in physical +`[B,Hq,T]` order. Output is contiguous int32 `[B,T,Hkv,top]`. Its supplied scale +must be finite. The original sorting network, tie semantics, causal masks and +low precision rounding are retained. + +## Workspace and stream ownership + +Use the public `Make` result's `workspace_size_in_bytes()` to reserve a stable +256-byte-aligned device workspace for Prefill. Retain workspace, inputs and +outputs until their stream completes, including every graph replay. A plain +`Handle` has no automatic workspace allocator; either set its workspace/size or +provide a stream-safe lease allocator. Selector needs no workspace. Providers +opt into capture only with this storage lifetime contract. The portable Runner +prepares and retains storage outside capture. + +The CMake source property preserves the old `-Xclang +-fno-cuda-host-device-constexpr` option only for these legacy CUDA files; other +operators keep their original compilation flags. The qualified target is gfx936. + +## Build and regression + +Add `nsa_videonsa_prefill` and `nsa_videonsa_selector` with +`{"implementations":"all"}` to the operator configuration used for a Hygon build. +Build and install the `infiniops` library target with bounded parallelism. + +The portable test consumer uses the installed SDK: + +```sh +cmake -S tests/videonsa_hygon -B /tmp/videonsa-consumer \ + -DCMAKE_PREFIX_PATH="$INFINI_ROOT" \ + -DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1" +cmake --build /tmp/videonsa-consumer -j 2 +INFINI_VIDEONSA_CONSUMER=/tmp/videonsa-consumer/libprefill_consumer.so \ + python tests/videonsa_hygon/test_frozen.py \ + --stage2-root /path/to/benchmark_test/stage2 \ + --output /path/to/personal-results/frozen.json +``` + +Run the shared benchmark with `tests/videonsa_hygon` on PYTHONPATH and +`--infini-adapter native_adapter --mode both`. Set +`INFINI_VIDEONSA_CONSUMER` to that consumer library and retain `INFINI_ROOT`. +Numerical references, oracle, payloads, SGLang and acceptance tolerances remain +external and unchanged. Outputs belong in a personal result directory. + +This is operator migration. It does not enable a model-default full NSA Prefill +path or establish whole-model performance. Actual model layer tests, frozen +subgraph replay and real-weight model runs must be reported separately. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 07042b7fb..1cdade607 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -301,6 +301,14 @@ if(WITH_HYGON) target_compile_definitions(infiniops PUBLIC WITH_HYGON=1) target_sources(infiniops PRIVATE ${HYGON_SOURCES}) + # Preserve the qualified VideoNSA kernel compile mode from the old build. + foreach(_nsa_src IN LISTS HYGON_SOURCES) + if(_nsa_src MATCHES "/nsa_videonsa_prefill/legacy/.*[.]cu$") + set_property(SOURCE "${_nsa_src}" APPEND PROPERTY COMPILE_OPTIONS + -Xclang -fno-cuda-host-device-constexpr) + endif() + endforeach() + find_package(CUDAToolkit REQUIRED) target_link_libraries(infiniops PUBLIC CUDA::cudart CUDA::cublas) diff --git a/src/base/nsa_compress_paged_cache.h b/src/base/nsa_compress_paged_cache.h new file mode 100644 index 000000000..8411c5305 --- /dev/null +++ b/src/base/nsa_compress_paged_cache.h @@ -0,0 +1,41 @@ +#pragma once +#include "operator.h" +#include +#include +namespace infini::ops { +class NsaCompressPagedCache : public Operator { + public: + static bool supports(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo) { + if(kc.ndim()!=4 || vc.shape()!=kc.shape() || ko.ndim()!=3 || vo.shape()!=ko.shape() || + bt.ndim()!=2 || lens.ndim()!=1 || lens.size(0)!=bt.size(0) || + (kc.dtype()!=DataType::kFloat16 && kc.dtype()!=DataType::kBFloat16) || + vc.dtype()!=kc.dtype() || ko.dtype()!=kc.dtype() || vo.dtype()!=kc.dtype() || + lens.dtype()!=bt.dtype() || (bt.dtype()!=DataType::kInt32 && bt.dtype()!=DataType::kInt64 && bt.dtype()!=DataType::kUInt32) || + !kc.size(0) || !kc.size(1) || !kc.size(2) || kc.size(3)!=128 || block<=0 || + kc.size(2)%size_t(block)!=0 || bt.size(1)==0 || + ko.size(0)!=kc.size(0)*(kc.size(2)/size_t(block)) || ko.size(1)!=kc.size(1) || ko.size(2)!=128 || + kc.stride(3)!=1 || vc.stride(3)!=1 || ko.stride(2)!=1 || vo.stride(2)!=1 || bt.stride(1)!=1 || lens.stride(0)!=1) return false; + for(const auto& t:{kc,vc,bt,lens,ko,vo}) { + if(t.device()!=kc.device())return false; + for(auto stride:t.strides())if(stride<0)return false; + } + (void)tail; return true; + } + NsaCompressPagedCache(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo):metadata_{kc,vc,bt,lens,ko,vo},block_{block},tail_{tail} { + if(!supports(kc,vc,bt,lens,block,tail,ko,vo)) throw std::invalid_argument("Invalid NSA compressed paged cache metadata"); + } + virtual void operator()(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo) const=0; + protected: + void Check(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo) const { + if(block!=block_ || tail!=tail_ || !supports(kc,vc,bt,lens,block,tail,ko,vo))throw std::invalid_argument("NSA compressed cache invocation differs from plan"); + const std::array values{kc,vc,bt,lens,ko,vo}; + for(size_t i=0;i{}(values[i],metadata_[i]))throw std::invalid_argument("NSA compressed cache tensor metadata differs from plan"); + } + std::array metadata_; + int block_; bool tail_; +}; +} diff --git a/src/base/nsa_paged_attention.h b/src/base/nsa_paged_attention.h new file mode 100644 index 000000000..4398b003a --- /dev/null +++ b/src/base/nsa_paged_attention.h @@ -0,0 +1,36 @@ +#pragma once +#include "base/nsa_compress_paged_cache.h" +#include +#include +namespace infini::ops { +class NsaPagedAttention : public Operator { + public: + static bool supports(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out) { + if(!NsaCompressPagedCache::supports(kc,vc,bt,lens,block,false,km,vm) || + q.ndim()!=3 || q.size(0)!=bt.size(0) || !q.size(1) || q.size(1)%kc.size(1)!=0 || q.size(2)!=128 || + q.dtype()!=kc.dtype() || out.shape()!=q.shape() || out.dtype()!=q.dtype() || q.stride(2)!=1 || out.stride(2)!=1 || + gates.ndim()!=3 || gates.shape()!=Tensor::Shape{q.size(0),3,q.size(1)} || + (gates.dtype()!=q.dtype() && gates.dtype()!=DataType::kFloat32) || !std::isfinite(scale) || window<0 || selected<=0)return false; + for(const auto& t:{q,out,gates}) { + if(t.device()!=kc.device())return false; + for(auto stride:t.strides())if(stride<0)return false; + } + return true; + } + NsaPagedAttention(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out):metadata_{q,km,vm,kc,vc,bt,lens,gates,out},scale_{scale},block_{block},window_{window},selected_{selected} { + if(!supports(q,km,vm,kc,vc,bt,lens,gates,scale,block,window,selected,out))throw std::invalid_argument("Invalid NSA paged attention metadata"); + } + virtual void operator()(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out) const=0; + protected: + void Check(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out) const { + if(scale!=scale_ || block!=block_ || window!=window_ || selected!=selected_ || !supports(q,km,vm,kc,vc,bt,lens,gates,scale,block,window,selected,out))throw std::invalid_argument("NSA paged attention invocation differs from plan"); + const std::array values{q,km,vm,kc,vc,bt,lens,gates,out}; + for(size_t i=0;i{}(values[i],metadata_[i]))throw std::invalid_argument("NSA paged attention tensor metadata differs from plan"); + } + std::array metadata_;float scale_;int block_,window_,selected_; +}; +} diff --git a/src/base/nsa_videonsa_common.h b/src/base/nsa_videonsa_common.h new file mode 100644 index 000000000..bb92cb5e1 --- /dev/null +++ b/src/base/nsa_videonsa_common.h @@ -0,0 +1,22 @@ +#pragma once +#include "operator.h" +#include +#include +#include +namespace infini::ops::nsa_video_detail { +inline bool base(const Tensor q,const Tensor k,int top,int block){ + if(q.ndim()!=4||k.ndim()!=4||!q.IsContiguous()||!k.IsContiguous()|| + (q.dtype()!=DataType::kFloat16&&q.dtype()!=DataType::kBFloat16)||k.dtype()!=q.dtype())return false; + const auto b=q.size(0),t=q.size(1),hq=q.size(2),hkv=k.size(2); + return b>=1&&b<=8&&t>=1&&t<=8192&&hq>=1&&hq<=1024&&hkv>=1&&hkv<=32&&hq%hkv==0&&hq/hkv<=32&& + q.size(3)==128&&k.size(0)==b&&k.size(3)==128&&block>=2&&block<=128&&(block&(block-1))==0&&top>=1&&top<=32&&top<=block/2; +} +template inline bool common(const std::array& ts){ + for(const auto& t:ts)if(t.device()!=ts[0].device()||!t.IsContiguous())return false; + return true; +} +template inline bool same(const std::array& a,const std::array& b){ + for(size_t i=0;i{}(a[i],b[i]))return false; + return true; +} +} diff --git a/src/base/nsa_videonsa_prefill.h b/src/base/nsa_videonsa_prefill.h new file mode 100644 index 000000000..8ec3faf53 --- /dev/null +++ b/src/base/nsa_videonsa_prefill.h @@ -0,0 +1,22 @@ +#pragma once +#include "nsa_videonsa_common.h" +namespace infini::ops { +class NsaVideonsaPrefill:public Operator{ + public: + static bool supports(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out){ + if(!nsa_video_detail::base(q,k,top,block)||!nsa_video_detail::common(std::array{q,k,v,gc,gs,gw,out}))return false; + return k.size(1)==q.size(1)&&v.shape()==k.shape()&&v.dtype()==q.dtype()&&out.shape()==q.shape()&&window>=0&& + (gc.dtype()==q.dtype()||gc.dtype()==DataType::kFloat32)&&gs.dtype()==gc.dtype()&&gw.dtype()==gc.dtype()&&out.dtype()==gc.dtype()&& + gc.shape()==Tensor::Shape{q.size(0),q.size(1),q.size(2)}&&gs.shape()==gc.shape()&&gw.shape()==gc.shape(); + } + NsaVideonsaPrefill(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out):meta_{q,k,v,gc,gs,gw,out},top_(top),block_(block),window_(window){ + if(!supports(q,k,v,gc,gs,gw,top,block,window,out))throw std::invalid_argument("Invalid VideoNSA prefill metadata"); + } + virtual void operator()(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out) const=0; + protected: + void Check(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out) const{ + if(top!=top_||block!=block_||window!=window_||!supports(q,k,v,gc,gs,gw,top,block,window,out)||!nsa_video_detail::same(meta_,std::array{q,k,v,gc,gs,gw,out}))throw std::invalid_argument("VideoNSA prefill invocation differs from plan"); + } + std::array meta_;int top_,block_,window_; +}; +} diff --git a/src/base/nsa_videonsa_selector.h b/src/base/nsa_videonsa_selector.h new file mode 100644 index 000000000..b5a48db10 --- /dev/null +++ b/src/base/nsa_videonsa_selector.h @@ -0,0 +1,21 @@ +#pragma once +#include "nsa_videonsa_common.h" +namespace infini::ops { +class NsaVideonsaSelector:public Operator{ + public: + static bool supports(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out){ + if(!nsa_video_detail::base(q,k,top,block)||!nsa_video_detail::common(std::array{q,k,lse,out}))return false; + return k.size(1)==(q.size(1)+block-1)/block&&std::isfinite(scale)&&lse.dtype()==DataType::kFloat32&&out.dtype()==DataType::kInt32&& + lse.shape()==Tensor::Shape{q.size(0),q.size(2),q.size(1)}&&out.shape()==Tensor::Shape{q.size(0),q.size(1),k.size(2),size_t(top)}; + } + NsaVideonsaSelector(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out):meta_{q,k,lse,out},top_(top),block_(block),scale_(scale){ + if(!supports(q,k,lse,top,block,scale,out))throw std::invalid_argument("Invalid VideoNSA selector metadata"); + } + virtual void operator()(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out) const=0; + protected: + void Check(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out) const{ + if(top!=top_||block!=block_||scale!=scale_||!supports(q,k,lse,top,block,scale,out)||!nsa_video_detail::same(meta_,std::array{q,k,lse,out}))throw std::invalid_argument("VideoNSA selector invocation differs from plan"); + } + std::array meta_;int top_,block_;float scale_; +}; +} diff --git a/src/native/cuda/hygon/ops/layer_norm/kernel.cu b/src/native/cuda/hygon/ops/layer_norm/kernel.cu new file mode 100644 index 000000000..1041afe6f --- /dev/null +++ b/src/native/cuda/hygon/ops/layer_norm/kernel.cu @@ -0,0 +1,89 @@ +#include "native/cuda/hygon/ops/layer_norm/kernel.h" + +#include +#include +#include +#include + +#include + +namespace infini::ops::hygon_layer_norm_detail { +namespace { +constexpr unsigned kThreads = 256; + +template +__global__ void LayerNormKernel(const T* input, const T* weight, const T* bias, + T* out, Layout layout, float eps) { + std::size_t row = blockIdx.x; + std::size_t input_offset = 0; + std::size_t out_offset = 0; + for (unsigned i = layout.rank - 1; i > 0; --i) { + const auto axis = i - 1; + const auto coordinate = row % layout.shape[axis]; + row /= layout.shape[axis]; + input_offset += coordinate * layout.input_strides[axis]; + out_offset += coordinate * layout.out_strides[axis]; + } + + using Reduce = cub::BlockReduce; + __shared__ typename Reduce::TempStorage reduce_storage; + __shared__ float mean; + __shared__ float inv_std; + float partial = 0.0f; + for (std::size_t d = threadIdx.x; d < layout.dim; d += kThreads) + partial += static_cast(input[input_offset + + d * layout.input_strides[layout.rank - 1]]); + const float sum = Reduce(reduce_storage).Sum(partial); + if (threadIdx.x == 0) mean = sum / static_cast(layout.dim); + __syncthreads(); + + partial = 0.0f; + for (std::size_t d = threadIdx.x; d < layout.dim; d += kThreads) { + const float centered = static_cast(input[input_offset + + d * layout.input_strides[layout.rank - 1]]) - mean; + partial += centered * centered; + } + const float variance_sum = Reduce(reduce_storage).Sum(partial); + if (threadIdx.x == 0) + inv_std = rsqrtf(variance_sum / static_cast(layout.dim) + eps); + __syncthreads(); + + for (std::size_t d = threadIdx.x; d < layout.dim; d += kThreads) { + const float x = static_cast(input[input_offset + + d * layout.input_strides[layout.rank - 1]]); + const float scale = static_cast(weight[d * layout.weight_stride]); + const float shift = bias ? static_cast(bias[d * layout.bias_stride]) : 0.0f; + out[out_offset + d * layout.out_strides[layout.rank - 1]] = + static_cast((x - mean) * inv_std * scale + shift); + } +} + +template +void Submit(const void* input, const void* weight, const void* bias, void* out, + Layout layout, std::size_t rows, float eps, cudaStream_t stream) { + LayerNormKernel<<(rows), kThreads, 0, stream>>>( + static_cast(input), static_cast(weight), + static_cast(bias), static_cast(out), layout, eps); +} +} // namespace + +void Launch(const void* input, const void* weight, const void* bias, void* out, + DataType dtype, Layout layout, std::size_t rows, + float eps, cudaStream_t stream) { + switch (dtype) { + case DataType::kFloat16: + Submit<__half>(input, weight, bias, out, layout, rows, eps, stream); + break; + case DataType::kBFloat16: + Submit<__nv_bfloat16>(input, weight, bias, out, layout, rows, eps, stream); + break; + case DataType::kFloat32: + Submit(input, weight, bias, out, layout, rows, eps, stream); + break; + default: + throw std::invalid_argument("Hygon LayerNorm unsupported dtype"); + } + const auto status = cudaPeekAtLastError(); + if (status != cudaSuccess) throw std::runtime_error(cudaGetErrorString(status)); +} +} // namespace infini::ops::hygon_layer_norm_detail diff --git a/src/native/cuda/hygon/ops/layer_norm/kernel.h b/src/native/cuda/hygon/ops/layer_norm/kernel.h new file mode 100644 index 000000000..27fb59109 --- /dev/null +++ b/src/native/cuda/hygon/ops/layer_norm/kernel.h @@ -0,0 +1,182 @@ +#ifndef INFINI_OPS_HYGON_LAYER_NORM_KERNEL_H_ +#define INFINI_OPS_HYGON_LAYER_NORM_KERNEL_H_ + +#include + +#include +#include +#include +#include +#include +#include + +#include "base/layer_norm.h" +#include "native/cuda/hygon/ops/layer_norm/view_checks.h" +#include "native/cuda/hygon/runtime_.h" + +namespace infini::ops { +namespace hygon_layer_norm_detail { +struct Layout { + std::size_t shape[8]; + std::size_t input_strides[8]; + std::size_t out_strides[8]; + std::size_t weight_stride; + std::size_t bias_stride; + std::size_t dim; + unsigned rank; +}; + +void Launch(const void* input, const void* weight, const void* bias, void* out, + DataType dtype, Layout layout, std::size_t rows, float eps, + cudaStream_t stream); +} // namespace hygon_layer_norm_detail + +template <> +class Operator : public LayerNorm { + public: + static bool supports(const Tensor input, const Tensor weight, + const std::optional bias, float, Tensor out) { + using namespace hygon_layer_norm_view; + const auto rank = input.shape().size(); + if (input.device().type() != Device::Type::kHygon || + input.device() != weight.device() || input.device() != out.device() || + (bias && input.device() != bias->device()) || rank < 1 || rank > 8 || + rank != input.strides().size() || out.shape() != input.shape() || + out.strides().size() != rank || weight.shape().size() != 1 || + weight.strides().size() != 1 || + (bias && (bias->shape().size() != 1 || bias->strides().size() != 1)) || + input.shape()[rank - 1] == 0 || + weight.shape()[0] != input.shape()[rank - 1] || + (bias && bias->shape()[0] != input.shape()[rank - 1]) || + input.dtype() != weight.dtype() || input.dtype() != out.dtype() || + (bias && bias->dtype() != input.dtype()) || + (input.dtype() != DataType::kFloat16 && + input.dtype() != DataType::kBFloat16 && + input.dtype() != DataType::kFloat32) || + !NonOverlappingOutput(out)) + return false; + + int current = -1; + if (cudaGetDevice(¤t) != cudaSuccess || + current != input.device().index()) + return false; + std::size_t rows = 1; + for (std::size_t i = 0; i + 1 < rank; ++i) { + if (input.shape()[i] && + rows > static_cast(std::numeric_limits::max()) / + input.shape()[i]) + return false; + rows *= input.shape()[i]; + } + if (rows > static_cast(std::numeric_limits::max()) || + input.shape()[rank - 1] > + static_cast(std::numeric_limits::max()) || + weight.strides()[0] < 0 || (bias && bias->strides()[0] < 0)) + return false; + std::size_t input_bytes, weight_bytes, out_bytes, bias_bytes = 0; + if (!SpanBytes(input, &input_bytes) || !SpanBytes(weight, &weight_bytes) || + !SpanBytes(out, &out_bytes) || (bias && !SpanBytes(*bias, &bias_bytes))) + return false; + const auto width = input.element_size(); + if ((input_bytes && + reinterpret_cast(input.data()) % width) || + (weight_bytes && + reinterpret_cast(weight.data()) % width) || + (out_bytes && reinterpret_cast(out.data()) % width) || + (bias_bytes && reinterpret_cast(bias->data()) % width)) + return false; + const bool exact_inplace = input.data() == out.data() && + input.strides() == out.strides() && + NonOverlappingOutput(input); + return !Overlaps(out, out_bytes, weight, weight_bytes) && + (!bias || !Overlaps(out, out_bytes, *bias, bias_bytes)) && + (!Overlaps(out, out_bytes, input, input_bytes) || exact_inplace); + } + + Operator(const Tensor input, const Tensor weight, + const std::optional bias, float eps, Tensor out) + : LayerNorm(checked(input, weight, bias, eps, out), weight, bias, eps, + out) { + using namespace hygon_layer_norm_view; + SpanBytes(input, &input_bytes_); + SpanBytes(weight, &weight_bytes_); + SpanBytes(out, &out_bytes_); + if (bias) SpanBytes(*bias, &bias_bytes_); + // An identical input layout inherits the validated output non-overlap. + exact_inplace_layout_ = input_meta_.strides() == out_meta_.strides(); + layout_.rank = static_cast(input.ndim()); + layout_.dim = input.size(-1); + layout_.weight_stride = static_cast(weight.stride(0)); + layout_.bias_stride = bias ? static_cast(bias->stride(0)) : 0; + for (std::size_t i = 0; i < input.ndim(); ++i) { + layout_.shape[i] = input.shape()[i]; + layout_.input_strides[i] = static_cast(input.strides()[i]); + layout_.out_strides[i] = static_cast(out.strides()[i]); + if (i + 1 < input.ndim()) rows_ *= input.shape()[i]; + } + } + + // Replay submits a kernel with by-value layout and no device allocation. + bool capture_safe() const override { return true; } + + void operator()(const Tensor input, const Tensor weight, + const std::optional bias, float eps, + Tensor out) const override { + if (!std::equal_to{}(input, input_meta_) || + !std::equal_to{}(weight, weight_meta_) || + !std::equal_to{}(out, out_meta_) || + bias.has_value() != bias_meta_.has_value() || + (bias && !std::equal_to{}(*bias, *bias_meta_)) || + bits(eps) != bits(eps_)) + throw std::invalid_argument( + "Hygon LayerNorm call metadata or alias changed after Make"); + + using namespace hygon_layer_norm_view; + const auto bound = [](const Tensor tensor, std::size_t bytes) { + return !bytes || + (tensor.data() && reinterpret_cast(tensor.data()) % + tensor.element_size() == + 0); + }; + int current = -1; + if (cudaGetDevice(¤t) != cudaSuccess || current != device_.index() || + !bound(input, input_bytes_) || !bound(weight, weight_bytes_) || + !bound(out, out_bytes_) || (bias && !bound(*bias, bias_bytes_)) || + Overlaps(out, out_bytes_, weight, weight_bytes_) || + (bias && Overlaps(out, out_bytes_, *bias, bias_bytes_)) || + (Overlaps(out, out_bytes_, input, input_bytes_) && + !(input.data() == out.data() && exact_inplace_layout_))) + throw std::invalid_argument( + "Hygon LayerNorm current device, pointer or alias changed after " + "Make"); + if (!rows_) return; + hygon_layer_norm_detail::Launch( + input.data(), weight.data(), bias ? bias->data() : nullptr, out.data(), + input_meta_.dtype(), layout_, rows_, eps_, + static_cast(stream_ ? stream_ : 0)); + } + + private: + hygon_layer_norm_detail::Layout layout_{}; + std::size_t rows_ = 1; + std::size_t input_bytes_ = 0, weight_bytes_ = 0, out_bytes_ = 0, + bias_bytes_ = 0; + bool exact_inplace_layout_ = false; + + static std::uint32_t bits(float value) { + std::uint32_t result; + std::memcpy(&result, &value, sizeof(result)); + return result; + } + static Tensor checked(const Tensor input, const Tensor weight, + const std::optional bias, float eps, + Tensor out) { + if (!supports(input, weight, bias, eps, out)) + throw std::invalid_argument( + "unsupported Hygon LayerNorm metadata, device or alias"); + return input; + } +}; +} // namespace infini::ops + +#endif diff --git a/src/native/cuda/hygon/ops/layer_norm/view_checks.h b/src/native/cuda/hygon/ops/layer_norm/view_checks.h new file mode 100644 index 000000000..ba2249a93 --- /dev/null +++ b/src/native/cuda/hygon/ops/layer_norm/view_checks.h @@ -0,0 +1,76 @@ +#ifndef INFINI_OPS_HYGON_BINARY_VIEW_CAPTURE_H_ +#define INFINI_OPS_HYGON_BINARY_VIEW_CAPTURE_H_ + +#include + +#include +#include +#include +#include +#include + +#include "tensor.h" + +namespace infini::ops::hygon_layer_norm_view { + +inline bool SpanBytes(const Tensor tensor, std::size_t* bytes) { + if (tensor.shape().size() != tensor.strides().size() || + !tensor.element_size()) + return false; + for (auto dim : tensor.shape()) + if (!dim) { + *bytes = 0; + return true; + } + std::size_t last = 0; + for (std::size_t i = 0; i < tensor.ndim(); ++i) { + if (tensor.strides()[i] < 0) return false; + const auto stride = static_cast(tensor.strides()[i]); + const auto extent = tensor.shape()[i] - 1; + if (extent && + stride > (std::numeric_limits::max() - last) / extent) + return false; + last += extent * stride; + } + if (last == std::numeric_limits::max() || + last + 1 > + std::numeric_limits::max() / tensor.element_size()) + return false; + *bytes = (last + 1) * tensor.element_size(); + return !*bytes || tensor.data(); +} + +inline bool NonOverlappingOutput(const Tensor out) { + if (out.shape().size() != out.strides().size()) return false; + if (out.HasBroadcastDim()) return false; + for (auto dim : out.shape()) + if (!dim) return true; + std::vector> dimensions; + for (std::size_t i = 0; i < out.ndim(); ++i) { + if (out.strides()[i] < 0) return false; + if (out.shape()[i] > 1) + dimensions.emplace_back(static_cast(out.strides()[i]), + out.shape()[i]); + } + std::sort(dimensions.begin(), dimensions.end()); + std::size_t span = 1; + for (const auto& [stride, extent] : dimensions) { + if (stride < span || + extent - 1 > (std::numeric_limits::max() - span) / stride) + return false; + span += (extent - 1) * stride; + } + return true; +} + +inline bool Overlaps(const Tensor a, std::size_t a_bytes, const Tensor b, + std::size_t b_bytes) { + if (!a_bytes || !b_bytes) return false; + const auto lhs = reinterpret_cast(a.data()); + const auto rhs = reinterpret_cast(b.data()); + return lhs <= rhs ? rhs - lhs < a_bytes : lhs - rhs < b_bytes; +} + +} // namespace infini::ops::hygon_layer_norm_view + +#endif diff --git a/src/native/cuda/hygon/ops/mrope/kernel.h b/src/native/cuda/hygon/ops/mrope/kernel.h new file mode 100644 index 000000000..2a0e65511 --- /dev/null +++ b/src/native/cuda/hygon/ops/mrope/kernel.h @@ -0,0 +1,13 @@ +#pragma once +#include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/runtime_.h" +#include "native/cuda/ops/mrope/kernel.h" + +namespace infini::ops { +template <> +class Operator + : public CudaMrope> { + public: + using CudaMrope>::CudaMrope; +}; +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/nsa_compress_paged_cache/arithmetic.cuh b/src/native/cuda/hygon/ops/nsa_compress_paged_cache/arithmetic.cuh new file mode 100644 index 000000000..0057445a5 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_compress_paged_cache/arithmetic.cuh @@ -0,0 +1,100 @@ +#ifndef __NSA_COMPRESS_PAGED_CACHE_HYGON_KERNEL_CUH__ +#define __NSA_COMPRESS_PAGED_CACHE_HYGON_KERNEL_CUH__ + +#include +#include +#include +#include + +namespace op::nsa_compress_paged_cache::hygon { + +template +__device__ inline float loadFloat(const T *ptr) { return static_cast(*ptr); } +template <> +__device__ inline float loadFloat<__half>(const __half *ptr) { return __half2float(*ptr); } +template <> +__device__ inline float loadFloat<__nv_bfloat16>(const __nv_bfloat16 *ptr) { return __bfloat162float(*ptr); } +template +__device__ inline void storeFloat(T *ptr, float value) { *ptr = static_cast(value); } +template <> +__device__ inline void storeFloat<__half>(__half *ptr, float value) { *ptr = __float2half(value); } +template <> +__device__ inline void storeFloat<__nv_bfloat16>(__nv_bfloat16 *ptr, float value) { *ptr = __float2bfloat16(value); } + +template +__device__ void compressPagedCacheKernel( + Tdata *k_cmp, + Tdata *v_cmp, + const Tdata *k_cache, + const Tdata *v_cache, + const Tindex *block_tables, + const Tindex *cache_lens, + size_t max_num_blocks_per_seq, + size_t page_block_size, + size_t subblocks_per_page, + int nsa_block_size, + int update_last_only, + ptrdiff_t k_cmp_block_stride, + ptrdiff_t k_cmp_head_stride, + ptrdiff_t v_cmp_block_stride, + ptrdiff_t v_cmp_head_stride, + ptrdiff_t k_cache_batch_stride, + ptrdiff_t k_cache_head_stride, + ptrdiff_t k_cache_row_stride, + ptrdiff_t v_cache_batch_stride, + ptrdiff_t v_cache_head_stride, + ptrdiff_t v_cache_row_stride, + ptrdiff_t block_table_batch_stride, + ptrdiff_t cache_lens_stride) { + const size_t seq = blockIdx.x; + size_t logical_nsa_block = blockIdx.y; + const size_t kv_head = blockIdx.z; + const int dim = threadIdx.x; + constexpr int kHeadDim = 128; + if (dim >= kHeadDim) { + return; + } + + const int64_t seq_len = static_cast(cache_lens[seq * cache_lens_stride]); + if (seq_len <= 0) { + return; + } + if (update_last_only) { + logical_nsa_block = static_cast((seq_len - 1) / nsa_block_size); + } + const int64_t tok_begin = static_cast(logical_nsa_block) * nsa_block_size; + if (tok_begin >= seq_len) { + return; + } + const int64_t tok_end = (tok_begin + nsa_block_size < seq_len) ? (tok_begin + nsa_block_size) : seq_len; + const size_t logical_page = static_cast(tok_begin / static_cast(page_block_size)); + if (logical_page >= max_num_blocks_per_seq) { + return; + } + const size_t subblock = static_cast((tok_begin % static_cast(page_block_size)) / nsa_block_size); + const Tindex physical = block_tables[seq * block_table_batch_stride + logical_page]; + const size_t cmp_block = static_cast(physical) * subblocks_per_page + subblock; + // Descriptor requires page_block_size % nsa_block_size == 0. A logical + // compression block therefore stays in one physical page, including tails. + // Hoist page/row address arithmetic; retain FP32 accumulation order. + const size_t first_row = static_cast(tok_begin % static_cast(page_block_size)); + const size_t k_first = static_cast(physical) * k_cache_batch_stride + kv_head * k_cache_head_stride + first_row * k_cache_row_stride; + const size_t v_first = static_cast(physical) * v_cache_batch_stride + kv_head * v_cache_head_stride + first_row * v_cache_row_stride; + size_t k_offset = k_first + dim; + size_t v_offset = v_first + dim; + float k_sum = 0.0f; + float v_sum = 0.0f; + for (int64_t tok = tok_begin; tok < tok_end; ++tok) { + k_sum += loadFloat(k_cache + k_offset); + v_sum += loadFloat(v_cache + v_offset); + k_offset += k_cache_row_stride; + v_offset += v_cache_row_stride; + } + const float inv = 1.0f / static_cast(tok_end - tok_begin); + storeFloat(k_cmp + cmp_block * k_cmp_block_stride + kv_head * k_cmp_head_stride + dim, k_sum * inv); + storeFloat(v_cmp + cmp_block * v_cmp_block_stride + kv_head * v_cmp_head_stride + dim, v_sum * inv); +} + +} // namespace op::nsa_compress_paged_cache::hygon + +#endif // __NSA_COMPRESS_PAGED_CACHE_HYGON_KERNEL_CUH__ diff --git a/src/native/cuda/hygon/ops/nsa_compress_paged_cache/kernel.h b/src/native/cuda/hygon/ops/nsa_compress_paged_cache/kernel.h new file mode 100644 index 000000000..b57ffb8da --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_compress_paged_cache/kernel.h @@ -0,0 +1,49 @@ +#pragma once +#include "base/nsa_compress_paged_cache.h" +#include "arithmetic.cuh" +#include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/runtime_.h" +#include +#include +namespace infini::ops { +namespace hygon_nsa_cache { +template +__global__ void Launch(T* ko,T* vo,const T* kc,const T* vc,const I* bt,const I* lens, + size_t width,size_t page,size_t sub,int block,int tail, + ptrdiff_t kob,ptrdiff_t koh,ptrdiff_t vob,ptrdiff_t voh, + ptrdiff_t kcb,ptrdiff_t kch,ptrdiff_t kcr,ptrdiff_t vcb,ptrdiff_t vch,ptrdiff_t vcr, + ptrdiff_t btb,ptrdiff_t ls) { + op::nsa_compress_paged_cache::hygon::compressPagedCacheKernel( + ko,vo,kc,vc,bt,lens,width,page,sub,block,tail,kob,koh,vob,voh,kcb,kch,kcr,vcb,vch,vcr,btb,ls); +} +} +template<> +class Operator : public NsaCompressPagedCache { + public: + using NsaCompressPagedCache::NsaCompressPagedCache; + static bool supports(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo) { + return kc.device().type()==Device::Type::kHygon && + NsaCompressPagedCache::supports(kc,vc,bt,lens,block,tail,ko,vo) && + bt.size(0)<=std::numeric_limits::max() && kc.size(1)<=65535 && + bt.size(1)<=size_t(65535)*size_t(block)/kc.size(2); + } + bool capture_safe() const override {return true;} + void operator()(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens, + int block,bool tail,Tensor ko,Tensor vo) const override { + Check(kc,vc,bt,lens,block,tail,ko,vo); + if(!supports(kc,vc,bt,lens,block,tail,ko,vo))throw std::invalid_argument("Unsupported Hygon NSA cache launch dimensions"); + if(!bt.size(0))return; + DispatchFunc,List>( + {int64_t(kc.dtype()),int64_t(bt.dtype())},[&](auto tag){ + using T=TypeMapType(tag)>;using I=TypeMapType(tag)>; + const size_t max_blocks=tail?1:bt.size(1)*(kc.size(2)/size_t(block)); + hygon_nsa_cache::Launch<<(stream_)>>>( + static_cast(ko.data()),static_cast(vo.data()),static_cast(kc.data()),static_cast(vc.data()),static_cast(bt.data()),static_cast(lens.data()), + bt.size(1),kc.size(2),kc.size(2)/size_t(block),block,int(tail), + ko.stride(0),ko.stride(1),vo.stride(0),vo.stride(1),kc.stride(0),kc.stride(1),kc.stride(2),vc.stride(0),vc.stride(1),vc.stride(2),bt.stride(0),lens.stride(0)); + },"HygonNsaCompressPagedCache"); + const auto result=cudaGetLastError();if(result!=cudaSuccess)throw std::runtime_error(cudaGetErrorString(result)); + } +}; +} diff --git a/src/native/cuda/hygon/ops/nsa_paged_attention/arithmetic.cuh b/src/native/cuda/hygon/ops/nsa_paged_attention/arithmetic.cuh new file mode 100644 index 000000000..183edd1e6 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_paged_attention/arithmetic.cuh @@ -0,0 +1,249 @@ +#ifndef __NSA_PAGED_ATTENTION_HYGON_KERNEL_CUH__ +#define __NSA_PAGED_ATTENTION_HYGON_KERNEL_CUH__ + +#include +#include +#include +#include +#include + +namespace op::nsa_paged_attention::hygon { + +template +__device__ inline float loadFloat(const T *ptr) { + return static_cast(*ptr); +} + +template <> +__device__ inline float loadFloat<__half>(const __half *ptr) { + return __half2float(*ptr); +} + +template <> +__device__ inline float loadFloat<__nv_bfloat16>(const __nv_bfloat16 *ptr) { + return __bfloat162float(*ptr); +} + +template +__device__ inline void storeFloat(T *ptr, float value) { + *ptr = static_cast(value); +} + +template <> +__device__ inline void storeFloat<__half>(__half *ptr, float value) { + *ptr = __float2half(value); +} + +template <> +__device__ inline void storeFloat<__nv_bfloat16>(__nv_bfloat16 *ptr, float value) { + *ptr = __float2bfloat16(value); +} + +__device__ inline float blockReduceSum128(float value) { + constexpr unsigned kFullMask = 0xffffffffu; + const int lane = threadIdx.x & 31; + const int warp = threadIdx.x >> 5; + +#if !defined(ENABLE_ILUVATAR_API) && !defined(ENABLE_HYGON_API) +#pragma unroll +#endif + for (int offset = 16; offset > 0; offset >>= 1) { + value += __shfl_down_sync(kFullMask, value, offset); + } + + __shared__ float warp_sums[4]; + if (lane == 0) { + warp_sums[warp] = value; + } + __syncthreads(); + + float sum = threadIdx.x < 4 ? warp_sums[lane] : 0.0f; + if (warp == 0) { +#if !defined(ENABLE_ILUVATAR_API) && !defined(ENABLE_HYGON_API) +#pragma unroll +#endif + for (int offset = 2; offset > 0; offset >>= 1) { + sum += __shfl_down_sync(kFullMask, sum, offset); + } + } + if (threadIdx.x == 0) { + warp_sums[0] = sum; + } + __syncthreads(); + return warp_sums[0]; +} + +template +__device__ void nsaPagedDecodeHd128Kernel( + Tdata *out, + const Tdata *q, + const Tdata *k_cmp, + const Tdata *v_cmp, + const Tdata *k_cache, + const Tdata *v_cache, + const Tindex *block_tables, + const Tindex *cache_lens, + const Tgate *gates, + size_t num_heads, + size_t num_kv_heads, + float scale, + size_t max_num_blocks_per_seq, + size_t page_block_size, + size_t subblocks_per_page, + int nsa_block_size, + int window_size, + int select_blocks, + ptrdiff_t q_stride, + ptrdiff_t q_head_stride, + ptrdiff_t k_cmp_block_stride, + ptrdiff_t k_cmp_head_stride, + ptrdiff_t v_cmp_block_stride, + ptrdiff_t v_cmp_head_stride, + ptrdiff_t k_batch_stride, + ptrdiff_t k_head_stride, + ptrdiff_t k_row_stride, + ptrdiff_t v_batch_stride, + ptrdiff_t v_head_stride, + ptrdiff_t v_row_stride, + ptrdiff_t o_stride, + ptrdiff_t o_head_stride, + ptrdiff_t block_table_batch_stride, + ptrdiff_t cache_lens_stride, + ptrdiff_t gates_seq_stride, + ptrdiff_t gates_branch_stride, + ptrdiff_t gates_head_stride) { + constexpr int kHeadDim = 128; + + const size_t seq = static_cast(blockIdx.x); + const size_t head = static_cast(blockIdx.y); + const int dim = threadIdx.x; + if (dim >= kHeadDim) { + return; + } + + const size_t kv_head = head / (num_heads / num_kv_heads); + const int64_t seq_len = static_cast(cache_lens[seq * cache_lens_stride]); + const float qd = loadFloat(q + seq * q_stride + head * q_head_stride + dim); + float comp_acc = 0.0f; + float comp_m = -INFINITY; + float comp_l = 0.0f; + + constexpr int kMaxSelectBlocks = 32; + const int active_select_blocks = max(1, min(select_blocks, kMaxSelectBlocks)); + float top_scores[kMaxSelectBlocks]; + int top_blocks[kMaxSelectBlocks]; +#if !defined(ENABLE_ILUVATAR_API) && !defined(ENABLE_HYGON_API) +#pragma unroll +#endif + for (int i = 0; i < active_select_blocks; ++i) { + top_scores[i] = -INFINITY; + top_blocks[i] = -1; + } + + const int64_t nsa_blocks = (seq_len + nsa_block_size - 1) / nsa_block_size; + for (int64_t nsa_block = 0; nsa_block < nsa_blocks; ++nsa_block) { + const int64_t tok_begin = nsa_block * nsa_block_size; + const size_t logical_page = static_cast(tok_begin / static_cast(page_block_size)); + if (logical_page >= max_num_blocks_per_seq) { + continue; + } + const size_t subblock = static_cast((tok_begin % static_cast(page_block_size)) / nsa_block_size); + const Tindex physical = block_tables[seq * block_table_batch_stride + logical_page]; + const size_t cmp_block = static_cast(physical) * subblocks_per_page + subblock; + const size_t base_k = cmp_block * k_cmp_block_stride + kv_head * k_cmp_head_stride; + const size_t base_v = cmp_block * v_cmp_block_stride + kv_head * v_cmp_head_stride; + const float kd = loadFloat(k_cmp + base_k + dim); + const float score = blockReduceSum128(qd * kd) * scale; + if (score > top_scores[active_select_blocks - 1]) { + int insert_pos = active_select_blocks - 1; + while (insert_pos > 0 && score > top_scores[insert_pos - 1]) { + top_scores[insert_pos] = top_scores[insert_pos - 1]; + top_blocks[insert_pos] = top_blocks[insert_pos - 1]; + --insert_pos; + } + top_scores[insert_pos] = score; + top_blocks[insert_pos] = static_cast(nsa_block); + } + const float vd = loadFloat(v_cmp + base_v + dim); + const float new_m = fmaxf(comp_m, score); + const float alpha = expf(comp_m - new_m); + const float beta = expf(score - new_m); + comp_acc = comp_acc * alpha + beta * vd; + comp_l = comp_l * alpha + beta; + comp_m = new_m; + } + const float comp_out = comp_l > 0.0f ? comp_acc / comp_l : 0.0f; + + float sel_acc = 0.0f; + float sel_m = -INFINITY; + float sel_l = 0.0f; +#if !defined(ENABLE_ILUVATAR_API) && !defined(ENABLE_HYGON_API) +#pragma unroll +#endif + for (int selected = 0; selected < active_select_blocks; ++selected) { + const int nsa_block = top_blocks[selected]; + if (nsa_block < 0) { + continue; + } + const int64_t tok_begin = static_cast(nsa_block) * nsa_block_size; + const int64_t tok_end = min(tok_begin + static_cast(nsa_block_size), seq_len); + // The descriptor requires page_block_size % nsa_block_size == 0. + // A selected compression block never crosses a physical-page boundary. + const size_t logical_block = static_cast(tok_begin / static_cast(page_block_size)); + if (logical_block >= max_num_blocks_per_seq) { + continue; + } + const size_t first_row = static_cast(tok_begin % static_cast(page_block_size)); + const Tindex physical = block_tables[seq * block_table_batch_stride + logical_block]; + size_t k_offset = static_cast(physical) * k_batch_stride + kv_head * k_head_stride + first_row * k_row_stride + dim; + size_t v_offset = static_cast(physical) * v_batch_stride + kv_head * v_head_stride + first_row * v_row_stride + dim; + for (int64_t tok = tok_begin; tok < tok_end; ++tok) { + const float kd = loadFloat(k_cache + k_offset); + const float score = blockReduceSum128(qd * kd) * scale; + const float vd = loadFloat(v_cache + v_offset); + const float new_m = fmaxf(sel_m, score); + const float alpha = expf(sel_m - new_m); + const float beta = expf(score - new_m); + sel_acc = sel_acc * alpha + beta * vd; + sel_l = sel_l * alpha + beta; + sel_m = new_m; + k_offset += k_row_stride; + v_offset += v_row_stride; + } + } + const float sel_out = sel_l > 0.0f ? sel_acc / sel_l : 0.0f; + + float win_acc = 0.0f; + float win_m = -INFINITY; + float win_l = 0.0f; + const int64_t win_begin = window_size > 0 ? ((seq_len > window_size) ? (seq_len - window_size) : 0) : seq_len; + for (int64_t tok = win_begin; tok < seq_len; ++tok) { + const size_t logical_block = static_cast(tok / static_cast(page_block_size)); + const size_t block_offset = static_cast(tok % static_cast(page_block_size)); + if (logical_block >= max_num_blocks_per_seq) { + continue; + } + const Tindex physical = block_tables[seq * block_table_batch_stride + logical_block]; + const size_t base_k = static_cast(physical) * k_batch_stride + kv_head * k_head_stride + block_offset * k_row_stride; + const size_t base_v = static_cast(physical) * v_batch_stride + kv_head * v_head_stride + block_offset * v_row_stride; + const float kd = loadFloat(k_cache + base_k + dim); + const float score = blockReduceSum128(qd * kd) * scale; + const float vd = loadFloat(v_cache + base_v + dim); + const float new_m = fmaxf(win_m, score); + const float alpha = expf(win_m - new_m); + const float beta = expf(score - new_m); + win_acc = win_acc * alpha + beta * vd; + win_l = win_l * alpha + beta; + win_m = new_m; + } + const float win_out = win_l > 0.0f ? win_acc / win_l : 0.0f; + + const float g_cmp = loadFloat(gates + seq * gates_seq_stride + 0 * gates_branch_stride + head * gates_head_stride); + const float g_sel = loadFloat(gates + seq * gates_seq_stride + 1 * gates_branch_stride + head * gates_head_stride); + const float g_swa = loadFloat(gates + seq * gates_seq_stride + 2 * gates_branch_stride + head * gates_head_stride); + storeFloat(out + seq * o_stride + head * o_head_stride + dim, g_cmp * comp_out + g_sel * sel_out + g_swa * win_out); +} + +} // namespace op::nsa_paged_attention::hygon + +#endif // __NSA_PAGED_ATTENTION_HYGON_KERNEL_CUH__ diff --git a/src/native/cuda/hygon/ops/nsa_paged_attention/kernel.h b/src/native/cuda/hygon/ops/nsa_paged_attention/kernel.h new file mode 100644 index 000000000..11d271a03 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_paged_attention/kernel.h @@ -0,0 +1,97 @@ +#pragma once +#include "base/nsa_paged_attention.h" +#ifndef ENABLE_HYGON_API +#define INFINI_NSA_LOCAL_HYGON_MACRO +#define ENABLE_HYGON_API 1 +#endif +#include "arithmetic.cuh" +#ifdef INFINI_NSA_LOCAL_HYGON_MACRO +#undef ENABLE_HYGON_API +#undef INFINI_NSA_LOCAL_HYGON_MACRO +#endif +#include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/runtime_.h" +#include +#include +namespace infini::ops { +namespace hygon_nsa_attention { +template +__global__ void launchNsaPagedDecodeHygonAddressHd128( + Tdata *out, + const Tdata *q, + const Tdata *k_cmp, + const Tdata *v_cmp, + const Tdata *k_cache, + const Tdata *v_cache, + const Tindex *block_tables, + const Tindex *cache_lens, + const Tgate *gates, + size_t num_heads, + size_t num_kv_heads, + float scale, + size_t max_num_blocks_per_seq, + size_t page_block_size, + size_t subblocks_per_page, + int nsa_block_size, + int window_size, + int select_blocks, + ptrdiff_t q_stride, + ptrdiff_t q_head_stride, + ptrdiff_t k_cmp_block_stride, + ptrdiff_t k_cmp_head_stride, + ptrdiff_t v_cmp_block_stride, + ptrdiff_t v_cmp_head_stride, + ptrdiff_t k_batch_stride, + ptrdiff_t k_head_stride, + ptrdiff_t k_row_stride, + ptrdiff_t v_batch_stride, + ptrdiff_t v_head_stride, + ptrdiff_t v_row_stride, + ptrdiff_t o_stride, + ptrdiff_t o_head_stride, + ptrdiff_t block_table_batch_stride, + ptrdiff_t cache_lens_stride, + ptrdiff_t gates_seq_stride, + ptrdiff_t gates_branch_stride, + ptrdiff_t gates_head_stride) { + op::nsa_paged_attention::hygon::nsaPagedDecodeHd128Kernel( + out, q, k_cmp, v_cmp, k_cache, v_cache, block_tables, cache_lens, gates, num_heads, num_kv_heads, scale, + max_num_blocks_per_seq, page_block_size, subblocks_per_page, nsa_block_size, window_size, select_blocks, q_stride, q_head_stride, + k_cmp_block_stride, k_cmp_head_stride, v_cmp_block_stride, v_cmp_head_stride, + k_batch_stride, k_head_stride, k_row_stride, v_batch_stride, v_head_stride, v_row_stride, + o_stride, o_head_stride, block_table_batch_stride, cache_lens_stride, gates_seq_stride, + gates_branch_stride, gates_head_stride); +} + + +} +template<> +class Operator : public NsaPagedAttention { + public: + using NsaPagedAttention::NsaPagedAttention; + static bool supports(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out) { + return q.device().type()==Device::Type::kHygon && NsaPagedAttention::supports(q,km,vm,kc,vc,bt,lens,gates,scale,block,window,selected,out) && + q.size(0)<=std::numeric_limits::max() && q.size(1)<=65535 && + bt.size(1)<=size_t(std::numeric_limits::max())/kc.size(2); + } + bool capture_safe() const override {return true;} + void operator()(const Tensor q,const Tensor km,const Tensor vm,const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,const Tensor gates, + float scale,int block,int window,int selected,Tensor out) const override { + Check(q,km,vm,kc,vc,bt,lens,gates,scale,block,window,selected,out); + if(!supports(q,km,vm,kc,vc,bt,lens,gates,scale,block,window,selected,out))throw std::invalid_argument("Unsupported Hygon NSA attention launch dimensions"); + if(!q.size(0))return; + DispatchFunc,List,List>( + {int64_t(q.dtype()),int64_t(bt.dtype()),int64_t(gates.dtype())},[&](auto tag){ + using T=TypeMapType(tag)>; + using I=TypeMapType(tag)>; + using G=TypeMapType(tag)>; + hygon_nsa_attention::launchNsaPagedDecodeHygonAddressHd128<<(stream_)>>>( + static_cast(out.data()),static_cast(q.data()),static_cast(km.data()),static_cast(vm.data()),static_cast(kc.data()),static_cast(vc.data()),static_cast(bt.data()),static_cast(lens.data()),static_cast(gates.data()), + q.size(1),kc.size(1),scale,bt.size(1),kc.size(2),kc.size(2)/size_t(block),block,window,selected, + q.stride(0),q.stride(1),km.stride(0),km.stride(1),vm.stride(0),vm.stride(1),kc.stride(0),kc.stride(1),kc.stride(2),vc.stride(0),vc.stride(1),vc.stride(2),out.stride(0),out.stride(1),bt.stride(0),lens.stride(0),gates.stride(0),gates.stride(1),gates.stride(2)); + },"HygonNsaPagedAttention"); + const auto result=cudaGetLastError();if(result!=cudaSuccess)throw std::runtime_error(cudaGetErrorString(result)); + } +}; +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/kernel.h b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/kernel.h new file mode 100644 index 000000000..f057f6579 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/kernel.h @@ -0,0 +1,51 @@ +#pragma once +#include "base/nsa_videonsa_prefill.h" +#include "base/nsa_videonsa_selector.h" +#include +#include +extern "C" { +size_t personal_nsa_selected_scratch_r50_matrix_clean2(int,int,int,int,int,int,int); +int personal_nsa_branch_r49_cdkv32(const void*,const void*,const void*,void*,float*,int,int,int,int,int,int,int,int,int,void*); +int personal_nsa_branch_r50_window_final(const void*,const void*,const void*,void*,float*,int,int,int,int,int,int,int,int,int,void*); +int personal_nsa_pool(const void*,const void*,void*,void*,int,int,int,int,int,void*); +int personal_nsa_prefill_selector(const void*,const void*,const float*,int32_t*,int,int,int,int,int,int,int,int,float,int,void*); +int personal_nsa_selected_r50_matrix_clean2(const void*,const void*,const void*,void*,const int32_t*,void*,int,int,int,int,int,int,float,int,void*); +int personal_nsa_gate_r51_final(const void*,const void*,const void*,const void*,const void*,const void*,void*,int64_t,int,int,void*); +} +namespace infini::ops { +namespace nsa_video_detail { +struct Plan { + int b,t,hq,hkv,dtype,block,top,window,gate32;size_t workspace{};std::array offsets{}; + Plan(const Tensor q,const Tensor k,const Tensor gate,int tp,int bs,int win):b(q.size(0)),t(q.size(1)),hq(q.size(2)),hkv(k.size(2)),dtype(q.dtype()==DataType::kBFloat16),block(bs),top(tp),window(win),gate32(gate.dtype()==DataType::kFloat32){ + const size_t compressed=size_t(b)*((t+bs-1)/bs)*hkv*128*2,output=size_t(b)*t*hq*128*2; + offsets[0]=reserve(compressed);offsets[1]=reserve(compressed);offsets[2]=reserve(size_t(b)*t*hq*4);offsets[3]=reserve(size_t(b)*t*hkv*top*4); + for(int i=4;i<7;++i)offsets[i]=reserve(output); + if(hq/hkv==7&&bs==64&&t>=1024)offsets[7]=reserve(size_t(b)*hkv*((t+63)/64)*128*64*2); + auto extra=personal_nsa_selected_scratch_r50_matrix_clean2(b,t,hq,hkv,top,bs,dtype);if(extra)offsets[7]=reserve(extra); + } + size_t reserve(size_t bytes){if(bytes>std::numeric_limits::max()-workspace-255)throw std::overflow_error("NSA workspace overflow");auto at=workspace;workspace=(workspace+bytes+255)&~size_t(255);return at;} +}; +inline void launch_check(int code){if(code)throw std::runtime_error("Hygon VideoNSA launch failed: "+std::to_string(code));} +} +template<> class Operator:public NsaVideonsaPrefill{ + nsa_video_detail::Plan plan_; + public: + static bool supports(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out){return q.device().type()==Device::Type::kHygon&&NsaVideonsaPrefill::supports(q,k,v,gc,gs,gw,top,block,window,out);} + Operator(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out):NsaVideonsaPrefill(q,k,v,gc,gs,gw,top,block,window,out),plan_(q,k,gc,top,block,window){} + size_t workspace_size_in_bytes() const override{return plan_.workspace;} + bool capture_safe() const override{return true;} + void operator()(const Tensor q,const Tensor k,const Tensor v,const Tensor gc,const Tensor gs,const Tensor gw,int top,int block,int window,Tensor out) const override{ + Check(q,k,v,gc,gs,gw,top,block,window,out); + if(!workspace_||workspace_size_in_bytes_(workspace_)%256)throw std::invalid_argument("VideoNSA prefill requires sufficient 256-aligned workspace"); + const auto& m=plan_;auto ptr=[&](int i){return static_cast(static_cast(workspace_)+m.offsets[i]);};const int blocks=(m.t+m.block-1)/m.block;constexpr float scale=0x1.6a09e6p-4f; + using nsa_video_detail::launch_check; + launch_check(personal_nsa_pool(k.data(),v.data(),ptr(0),ptr(1),m.b,m.t,m.hkv,m.block,m.dtype,stream_)); + launch_check(personal_nsa_branch_r49_cdkv32(q.data(),ptr(0),ptr(1),ptr(4),static_cast(ptr(2)),m.b,m.t,blocks,m.hq,m.hkv,m.block,m.window,0,m.dtype,stream_)); + launch_check(personal_nsa_prefill_selector(q.data(),ptr(0),static_cast(ptr(2)),static_cast(ptr(3)),m.b,m.t,m.hq,m.hkv,128,blocks,m.block,m.top,scale,m.dtype,stream_)); + void* packed=((m.hq/m.hkv==7&&m.block==64&&m.t>=1024)||personal_nsa_selected_scratch_r50_matrix_clean2(m.b,m.t,m.hq,m.hkv,m.top,m.block,m.dtype))?ptr(7):nullptr; + launch_check(personal_nsa_selected_r50_matrix_clean2(q.data(),k.data(),v.data(),packed,static_cast(ptr(3)),ptr(5),m.b,m.t,m.hq,m.hkv,m.top,m.block,scale,m.dtype,stream_)); + launch_check(personal_nsa_branch_r50_window_final(q.data(),k.data(),v.data(),ptr(6),nullptr,m.b,m.t,m.t,m.hq,m.hkv,m.block,m.window,1,m.dtype,stream_)); + launch_check(personal_nsa_gate_r51_final(ptr(4),ptr(5),ptr(6),gc.data(),gs.data(),gw.data(),out.data(),int64_t(m.b)*m.t*m.hq*128,m.dtype,m.gate32,stream_)); + } +}; +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/attention_fallback.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/attention_fallback.cu new file mode 100644 index 000000000..5e6fa0cf0 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/attention_fallback.cu @@ -0,0 +1,707 @@ +#include +#include +#include +#include +#include +#include +#include "compression_wmma.cuh" + +__device__ float approxWindowP(float score, float maximum, float scale, float* pv) { + float c=__uint_as_float(0x3e6e28f6u); + float z=fminf(1.f,fmaxf(0.f,fmaf(score,scale*(1.f/64.f),fmaf(-maximum,scale*(1.f/64.f),c)))); + unsigned short b=(unsigned short)floorf(z*65535.f+0.5f); + half a=__ushort_as_half(b),d=__ushort_as_half((unsigned short)(b+512)); + half r=__float2half_rn(fmaf(__half2float(a),__half2float(__ushort_as_half(0x3da8)),__half2float(d))); + unsigned short bits=__half_as_ushort(r); + *pv=__bfloat162float(__ushort_as_bfloat16((unsigned short)(((bits>>3)<0x80)?0:((bits>>3)+0x3800)))); + return __half2float(r); +} + +#include "window_wmma.cuh" +template __global__ __launch_bounds__(128) +void nsaPrefillPoolCuda(const T* k,const T* v,T* kc,T* vc,int tokens,int kvheads,int blocks,int bs) { + int dim=threadIdx.x,kv=blockIdx.y,block=blockIdx.x%blocks,batch=blockIdx.x/blocks; + int start=block*bs,count=min(bs,tokens-start); + int64_t off=((int64_t(batch)*tokens+start)*kvheads+kv)*128+dim; + float sk=0,sv=0; + for(int j=0;j __global__ __launch_bounds__(256) +void nsaPrefillBranchCuda(const T* q,const T* k,const T* v,T* out,float* lse, + int tokens,int keytokens,int hq,int hkv,int bs,int window,int kind,int approx) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + int group=hq/hkv,gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=gh0?max(0,query-window+1):0); + int end=kind==0?keytokens:query+1; + float maximum=-INFINITY,denom=0; + __shared__ float scores[8][64]; + int first=begin/64*64,last=(end-1)/64*64; + int count=(last-first)/64+1; + for(int iteration=0;iteration=begin&&pos::value) pv=p; else p=pv; } + else pv=float(T(p)); + sum+=p;scores[warp][j]=pv; + } + for(int delta=16;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,32); + denom=denom*alpha+sum;maximum=nm; + for(int d=0;d<4;++d) acc[d]*=alpha; + __syncwarp(); + if(active) for(int j=max(0,begin-start);j<64&&start+j __device__ float gate_materialize(float x); +template<> __device__ float gate_materialize(float x) { + volatile unsigned short bits=__half_as_ushort(__float2half_rn(x)); + return __half2float(__ushort_as_half(bits)); +} +template<> __device__ float gate_materialize<__nv_bfloat16>(float x) { + volatile unsigned short bits=__bfloat16_as_ushort(__float2bfloat16_rn(x)); + return __bfloat162float(__ushort_as_bfloat16(bits)); +} +template<> __device__ float gate_materialize(float x) { + volatile float result=x; + return result; +} + +template __global__ +void nsaPrefillGateCuda(const T* oc,const T* os,const T* ow,const G* gc,const G* gs,const G* gw,G* out,int64_t elements) { + int64_t i=int64_t(blockIdx.x)*blockDim.x+threadIdx.x; + if(i(__fmul_rn(float(oc[i]),float(gc[row]))); + float ps=gate_materialize(__fmul_rn(float(os[i]),float(gs[row]))); + float pw=gate_materialize(__fmul_rn(float(ow[i]),float(gw[row]))); + float partial=gate_materialize(__fadd_rn(pc,ps)); + out[i]=G(__fadd_rn(partial,pw)); + } + return; +} + +extern "C" int personal_nsa_pool(const void* k,const void* v,void* kc,void* vc,int b,int t,int hkv,int bs,int dtype,void* stream) { + int n=(t+bs-1)/bs;dim3 grid(b*n,hkv); + if(dtype==0) nsaPrefillPoolCuda<<>>((const half*)k,(const half*)v,(half*)kc,(half*)vc,t,hkv,n,bs); + else nsaPrefillPoolCuda<<>>((const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)kc,(__nv_bfloat16*)vc,t,hkv,n,bs); + return int(cudaGetLastError()); +} +extern "C" int personal_nsa_branch(const void* q,const void* k,const void* v,void* out,float* lse, + int b,int t,int n,int hq,int hkv,int bs,int window,int kind,int dtype,void* stream) { + if(kind==0) { + dim3 cg((t+15)/16,b,hq); + if(dtype==0)nsaCompressionWmmaCuda<<>>((const half*)q,(const half*)k,(const half*)v,(half*)out,t,n,hq,hkv,bs,lse); + else nsaCompressionWmmaCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,n,hq,hkv,bs,lse); + return int(cudaGetLastError()); + } + int dev=0,sm=0;cudaError_t e=cudaGetDevice(&dev);if(e!=cudaSuccess)return int(e); + e=cudaDeviceGetAttribute(&sm,cudaDevAttrMultiProcessorCount,dev);if(e!=cudaSuccess)return int(e); + int approx=t>=1024&&int64_t((t+63)/64)*b*hq>=int64_t(sm)*4; + if(kind==1 && approx && dtype==1) { + dim3 wg((t+15)/16,b,hq); + if(dtype==0)nsaPrefillWindowRegisterCuda<<>>((const half*)q,(const half*)k,(const half*)v,(half*)out,t,hq,hkv,window); + else nsaPrefillWindowRegisterCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,hq,hkv,window); + return int(cudaGetLastError()); + } + dim3 grid(b*t,hkv,(hq/hkv+7)/8); + if(dtype==0) nsaPrefillBranchCuda<<>>((const half*)q,(const half*)k,(const half*)v,(half*)out,lse,t,n,hq,hkv,bs,window,kind,approx); + else nsaPrefillBranchCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,lse,t,n,hq,hkv,bs,window,kind,approx); + return int(cudaGetLastError()); +} +extern "C" int personal_nsa_gate(const void* oc,const void* os,const void* ow,const void* gc,const void* gs,const void* gw,void* out, + int64_t elements,int dtype,int gate32,void* stream) { + dim3 grid((elements+255)/256); + if(dtype==0&&gate32==0) nsaPrefillGateCuda<<>>((const half*)oc,(const half*)os,(const half*)ow,(const half*)gc,(const half*)gs,(const half*)gw,(half*)out,elements); + else if(dtype==1&&gate32==0) nsaPrefillGateCuda<<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const __nv_bfloat16*)gc,(const __nv_bfloat16*)gs,(const __nv_bfloat16*)gw,(__nv_bfloat16*)out,elements); + else if(dtype==0) nsaPrefillGateCuda<<>>((const half*)oc,(const half*)os,(const half*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,elements); + else nsaPrefillGateCuda<<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,elements); + return int(cudaGetLastError()); +} + +template __global__ __launch_bounds__(256) +void nsaCompressionRegisterR47Cuda(const T* q,const T* k,const T* v,T* out, + int tokens,int keytokens,int hq,int hkv,int bs,float* lse) { + using namespace nvcuda; + constexpr int DS=136,NS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*DS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128; + qs[(i/128)*DS+d]=row output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=0;start c; + wmma::fill_fragment(c,0.f); + for(int d=0;d<128;d+=16) { + wmma::fragment a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+d,DS); + wmma::load_matrix_sync(b,ks+col*DS+d,DS); + wmma::mma_sync(c,a,b,c); + } + wmma::store_matrix_sync(scores+col,c,NS,wmma::mem_row_major); + } + } + __syncthreads(); + for(int i=tid;i<64*128;i+=256) { + int pos=start+i/128,d=i%128; + int64_t off=((int64_t(batch)*keytokens+pos)*hkv+kv)*128+d; + vs[(i/128)*DS+d]=pos=tokens||pos=keytokens) scores[row*NS+j]=-INFINITY; + scores[row*NS+j]=__fmul_rn(__fmul_rn(scores[row*NS+j],0.08838834764831845f),1.4426950408889634f); + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=2;delta;delta/=2) mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,4)); + // A query can have an entirely masked leading/trailing tile. + float nm=fmaxf(maximum[row],mx); + float alpha=isfinite(nm)?exp2f(maximum[row]-nm):1.f; + float sum=0; + for(int j=lane;j<64;j+=4) { + float prob=isfinite(nm)?exp2f(scores[row*NS+j]-nm):0.f; + sum+=prob;ps[row*NS+j]=T(prob); + } + for(int delta=2;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0) {denom[row]=denom[row]*alpha+sum;maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + for(int i=tid;i<16*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4) { + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*DS+col,DS); + wmma::mma_sync(c,a,b,c); + } + } + } + __syncthreads(); + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row __global__ __launch_bounds__(256) +void nsaWindowR47SoftCuda(const T* q,const T* k,const T* v,T* out, + int tokens,int hq,int hkv,int window) { + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*KS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+15); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 c; + wmma::fill_fragment(c,0.f); + for(int half=0;half<2;++half) { + for(int i=tid;i<64*64;i+=256) { + int pos=start+i/64,d=half*64+i%64; + int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + ks[(i/64)*KS+i%64]=pos a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+half*64+d,DS); + wmma::load_matrix_sync(b,ks+warp*16*KS+d,KS); + wmma::mma_sync(c,a,b,c); + } + __syncthreads(); + } + if(warp<4)wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + } else { + for(int i=tid;i<16*64;i+=256)scores[(i/64)*NS+i%64]=-INFINITY; + } + __syncthreads(); + { + const int row=tid/16,lane=tid%16,query=query0+row; + const int begin=window>0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=16) { + const int pos=start+j; + if(query>=tokens||posquery)scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=8;delta;delta/=2)mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,16)); + const float nm=fmaxf(maximum[row],mx); + const float alpha=isfinite(nm)?exp2f((maximum[row]-nm)*scale_log2):1.f; + for(int j=lane;j<64;j+=16) { + float prob=0.f,pv=0.f; + if(isfinite(nm)){prob=approxWindowP(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + if(lane==0){maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + if(tid<64) { + const int row=tid/4,lane=tid%4; + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){const int j=tile*16+lane*4+pair*2;lo=halfRound(lo+scores[row*NS+j]);hi=halfRound(hi+scores[row*NS+j+1]);} + float sum=halfRound(lo+hi); + for(int delta=2;delta;delta/=2)sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0)denom[row]=denom[row]*rescale[row]+sum; + } + __syncthreads(); + for(int i=tid;i<16*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4){ + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*KS+warp*16,KS); + wmma::mma_sync(c,a,b,c); + } + + } + __syncthreads(); + } + } + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + } + __syncthreads(); + } + return; +} + +template __global__ __launch_bounds__(256) +void nsaWindowR47GateCuda(const T* q,const T* k,const T* v,G* out,const T* oc,const T* os,const G* gc,const G* gs,const G* gw, + int tokens,int hq,int hkv,int window) { + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*KS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+15); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 c; + wmma::fill_fragment(c,0.f); + for(int half=0;half<2;++half) { + for(int i=tid;i<64*64;i+=256) { + int pos=start+i/64,d=half*64+i%64; + int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + ks[(i/64)*KS+i%64]=pos a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+half*64+d,DS); + wmma::load_matrix_sync(b,ks+warp*16*KS+d,KS); + wmma::mma_sync(c,a,b,c); + } + __syncthreads(); + } + if(warp<4)wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + } else { + for(int i=tid;i<16*64;i+=256)scores[(i/64)*NS+i%64]=-INFINITY; + } + __syncthreads(); + if(tid<64) { + int row=tid/4,lane=tid%4; + int query=query0+row,begin=window>0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=4) { + int pos=start+j; + if(query>=tokens||posquery) scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=2;delta;delta/=2) mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,4)); + // A query can have an entirely masked leading/trailing tile. + float nm=fmaxf(maximum[row],mx); + float alpha=isfinite(nm)?exp2f((maximum[row]-nm)*scale_log2):1.f; + float sum=0; + for(int j=lane;j<64;j+=4) { + float prob=0.f,pv=0.f; if(isfinite(nm)) {prob=approxWindowP(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + __syncwarp(); + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){int j=tile*16+lane*4+pair*2;lo=halfRound(lo+scores[row*NS+j]);hi=halfRound(hi+scores[row*NS+j+1]);} + sum=halfRound(lo+hi); + for(int delta=2;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0) {denom[row]=denom[row]*alpha+sum;maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + for(int i=tid;i<16*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4){ + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*KS+warp*16,KS); + wmma::mma_sync(c,a,b,c); + } + + } + __syncthreads(); + } + } + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + const float pc=gate_materialize(__fmul_rn(float(oc[index]),float(gc[headrow]))); + const float ps_=gate_materialize(__fmul_rn(float(os[index]),float(gs[headrow]))); + const float pw=gate_materialize(__fmul_rn(float(ow),float(gw[headrow]))); + const float partial=gate_materialize(__fadd_rn(pc,ps_)); + out[index]=G(__fadd_rn(partial,pw)); + } + } + __syncthreads(); + } + return; +} + +template __global__ __launch_bounds__(256) +void nsaWindowR47SoftGateCuda(const T* q,const T* k,const T* v,G* out,const T* oc,const T* os,const G* gc,const G* gs,const G* gw, + int tokens,int hq,int hkv,int window) { + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*KS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+15); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 c; + wmma::fill_fragment(c,0.f); + for(int half=0;half<2;++half) { + for(int i=tid;i<64*64;i+=256) { + int pos=start+i/64,d=half*64+i%64; + int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + ks[(i/64)*KS+i%64]=pos a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+half*64+d,DS); + wmma::load_matrix_sync(b,ks+warp*16*KS+d,KS); + wmma::mma_sync(c,a,b,c); + } + __syncthreads(); + } + if(warp<4)wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + } else { + for(int i=tid;i<16*64;i+=256)scores[(i/64)*NS+i%64]=-INFINITY; + } + __syncthreads(); + { + const int row=tid/16,lane=tid%16,query=query0+row; + const int begin=window>0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=16) { + const int pos=start+j; + if(query>=tokens||posquery)scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=8;delta;delta/=2)mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,16)); + const float nm=fmaxf(maximum[row],mx); + const float alpha=isfinite(nm)?exp2f((maximum[row]-nm)*scale_log2):1.f; + for(int j=lane;j<64;j+=16) { + float prob=0.f,pv=0.f; + if(isfinite(nm)){prob=approxWindowP(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + if(lane==0){maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + if(tid<64) { + const int row=tid/4,lane=tid%4; + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){const int j=tile*16+lane*4+pair*2;lo=halfRound(lo+scores[row*NS+j]);hi=halfRound(hi+scores[row*NS+j+1]);} + float sum=halfRound(lo+hi); + for(int delta=2;delta;delta/=2)sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0)denom[row]=denom[row]*rescale[row]+sum; + } + __syncthreads(); + for(int i=tid;i<16*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4){ + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*KS+warp*16,KS); + wmma::mma_sync(c,a,b,c); + } + + } + __syncthreads(); + } + } + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + const float pc=gate_materialize(__fmul_rn(float(oc[index]),float(gc[headrow]))); + const float ps_=gate_materialize(__fmul_rn(float(os[index]),float(gs[headrow]))); + const float pw=gate_materialize(__fmul_rn(float(ow),float(gw[headrow]))); + const float partial=gate_materialize(__fadd_rn(pc,ps_)); + out[index]=G(__fadd_rn(partial,pw)); + } + } + __syncthreads(); + } + return; +} + +static int r47_window_fast(int b,int t,int hq,int dtype,int *fast) { + if(dtype!=1||t<1024){*fast=0;return 0;} + int device=0,sm=0;auto e=cudaGetDevice(&device);if(e!=cudaSuccess)return int(e); + e=cudaDeviceGetAttribute(&sm,cudaDevAttrMultiProcessorCount,device);if(e!=cudaSuccess)return int(e); + *fast=dtype==1&&t>=1024&&int64_t((t+63)/64)*b*hq>=int64_t(sm)*4;return 0; +} +extern "C" int personal_nsa_branch_r47_creg(const void* q,const void* k,const void* v,void* out,float* lse, + int b,int t,int n,int hq,int hkv,int bs,int window,int kind,int dtype,void* stream) { + if(kind!=0)return personal_nsa_branch(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); + dim3 grid((t+15)/16,b,hq); + if(dtype==0)nsaCompressionRegisterR47Cuda<<>>((const half*)q,(const half*)k,(const half*)v,(half*)out,t,n,hq,hkv,bs,lse); + else nsaCompressionRegisterR47Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,n,hq,hkv,bs,lse); + return int(cudaGetLastError()); +} +extern "C" int personal_nsa_branch_r47_wsoft(const void* q,const void* k,const void* v,void* out,float* lse, + int b,int t,int n,int hq,int hkv,int bs,int window,int kind,int dtype,void* stream) { + if(kind!=1)return personal_nsa_branch(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); + int fast=0;int code=r47_window_fast(b,t,hq,dtype,&fast);if(code)return code; + if(!fast)return personal_nsa_branch(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); + dim3 grid((t+15)/16,b,hq); + nsaWindowR47SoftCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,hq,hkv,window); + return int(cudaGetLastError()); +} + +extern "C" int personal_nsa_window_gate_r47_wgate(const void* q,const void* k,const void* v, + const void* oc,const void* os,void* ow,const void* gc,const void* gs,const void* gw,void* out, + int b,int t,int hq,int hkv,int bs,int window,int dtype,int gate32,void* stream) { + int fast=0;int code=r47_window_fast(b,t,hq,dtype,&fast);if(code)return code; + if(!fast){code=personal_nsa_branch(q,k,v,ow,nullptr,b,t,t,hq,hkv,bs,window,1,dtype,stream);if(code)return code; + return personal_nsa_gate(oc,os,ow,gc,gs,gw,out,int64_t(b)*t*hq*128,dtype,gate32,stream);} + dim3 grid((t+15)/16,b,hq); + if(gate32)nsaWindowR47GateCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(float*)out, + (const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const float*)gc,(const float*)gs,(const float*)gw,t,hq,hkv,window); + else nsaWindowR47GateCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out, + (const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)gc,(const __nv_bfloat16*)gs,(const __nv_bfloat16*)gw,t,hq,hkv,window); + return int(cudaGetLastError()); +} + +extern "C" int personal_nsa_window_gate_r47_wsoftgate(const void* q,const void* k,const void* v, + const void* oc,const void* os,void* ow,const void* gc,const void* gs,const void* gw,void* out, + int b,int t,int hq,int hkv,int bs,int window,int dtype,int gate32,void* stream) { + int fast=0;int code=r47_window_fast(b,t,hq,dtype,&fast);if(code)return code; + if(!fast){code=personal_nsa_branch(q,k,v,ow,nullptr,b,t,t,hq,hkv,bs,window,1,dtype,stream);if(code)return code; + return personal_nsa_gate(oc,os,ow,gc,gs,gw,out,int64_t(b)*t*hq*128,dtype,gate32,stream);} + dim3 grid((t+15)/16,b,hq); + if(gate32)nsaWindowR47SoftGateCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(float*)out, + (const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const float*)gc,(const float*)gs,(const float*)gw,t,hq,hkv,window); + else nsaWindowR47SoftGateCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out, + (const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)gc,(const __nv_bfloat16*)gs,(const __nv_bfloat16*)gw,t,hq,hkv,window); + return int(cudaGetLastError()); +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression.cu new file mode 100644 index 000000000..e2fb549d3 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression.cu @@ -0,0 +1,372 @@ +#include +#include +#include +#include +#include +#include +#include +static __device__ float approxWindowP(float score, float maximum, float scale, float* pv) { + float c=__uint_as_float(0x3e6e28f6u); + float z=fminf(1.f,fmaxf(0.f,fmaf(score,scale*(1.f/64.f),fmaf(-maximum,scale*(1.f/64.f),c)))); + unsigned short b=(unsigned short)floorf(z*65535.f+0.5f); + half a=__ushort_as_half(b),d=__ushort_as_half((unsigned short)(b+512)); + half r=__float2half_rn(fmaf(__half2float(a),__half2float(__ushort_as_half(0x3da8)),__half2float(d))); + unsigned short bits=__half_as_ushort(r); + *pv=__bfloat162float(__ushort_as_bfloat16((unsigned short)(((bits>>3)<0x80)?0:((bits>>3)+0x3800)))); + return __half2float(r); +} +static __device__ float halfRound(float x){volatile unsigned short b=__half_as_ushort(__float2half_rn(x));return __half2float(__ushort_as_half(b));} +__device__ __forceinline__ unsigned int r49_keep_bits(unsigned int bits) { +#if defined(__AMDGCN__) + asm volatile("" : "+v"(bits)); +#else + asm volatile("" : "+r"(bits)); +#endif + return bits; +} +__device__ __forceinline__ float r49_half_round(float x) { + const unsigned int bits=r49_keep_bits(__half_as_ushort(__float2half_rn(x))); + return __half2float(__ushort_as_half((unsigned short)bits)); +} +template __device__ __forceinline__ float r49_gate_round(float x) { + if constexpr(std::is_same::value)return r49_half_round(x); + else if constexpr(std::is_same::value) { + const unsigned int bits=r49_keep_bits(__bfloat16_as_ushort(__float2bfloat16_rn(x))); + return __bfloat162float(__ushort_as_bfloat16((unsigned short)bits)); + } else { + return __uint_as_float(r49_keep_bits(__float_as_uint(x))); + } +} +template __global__ void nsaGateR49Reg(const T* oc,const T* os,const T* ow, + const G* gc,const G* gs,const G* gw,G* out,int64_t elements) { + const int64_t i=int64_t(blockIdx.x)*blockDim.x+threadIdx.x; + if(i(__fmul_rn(float(oc[i]),float(gc[row]))); + const float ps=r49_gate_round(__fmul_rn(float(os[i]),float(gs[row]))); + const float pw=r49_gate_round(__fmul_rn(float(ow[i]),float(gw[row]))); + const float partial=r49_gate_round(__fadd_rn(pc,ps)); + out[i]=G(__fadd_rn(partial,pw)); + } +} +extern "C" int personal_nsa_gate_r49_reg(const void* oc,const void* os,const void* ow, + const void* gc,const void* gs,const void* gw,void* out,int64_t n,int dtype,int gate32,void* stream) { + const dim3 grid((n+255)/256); + if(dtype==0&&gate32==0)nsaGateR49Reg<<>>((const half*)oc,(const half*)os,(const half*)ow,(const half*)gc,(const half*)gs,(const half*)gw,(half*)out,n); + else if(dtype==1&&gate32==0)nsaGateR49Reg<<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const __nv_bfloat16*)gc,(const __nv_bfloat16*)gs,(const __nv_bfloat16*)gw,(__nv_bfloat16*)out,n); + else if(dtype==0)nsaGateR49Reg<<>>((const half*)oc,(const half*)os,(const half*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,n); + else nsaGateR49Reg<<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,n); + return int(cudaGetLastError()); +} +extern "C" int personal_nsa_branch_r47_wsoft(const void*,const void*,const void*,void*,float*,int,int,int,int,int,int,int,int,int,void*); +extern "C" int personal_nsa_branch_r47_creg(const void*,const void*,const void*,void*,float*,int,int,int,int,int,int,int,int,int,void*); +template __global__ __launch_bounds__(256) +void nsaAttentionR49wdkv32Cuda(const T* q,const T* k,const T* v,T* out, + int tokens,int hq,int hkv,int window) { + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*32,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) union {T query[32*DS];float score[32*NS];} query_score; + T* qs=query_score.query;float* scores=query_score.score; + __shared__ __align__(32) T ps[32*NS]; + // K is no longer needed when P*V begins. + + __shared__ float maximum[32],denom[32],rescale[32]; + for(int i=tid;i<32*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[4]; + if(warp<4)for(int h=0;h<4;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + wmma::fragment qcache[2][8]; + if(warp<4) { + #pragma unroll + for(int sub=0;sub<2;++sub) { + #pragma unroll + for(int di=0;di<8;++di)wmma::load_matrix_sync(qcache[sub][di],qs+sub*16*DS+di*16,DS); + } + } + __syncthreads(); + unsigned long long qk_bmap=0,pv_bmap=0; + T* operand_marker=reinterpret_cast(scores); + for(int i=tid;i<256;i+=256)operand_marker[(i/16)*NS+i%16]=T(float(i)); + __syncthreads(); + if(warp<4) { + wmma::fragment qk; + wmma::fragment pv; + static_assert(qk.num_elements<=8&&pv.num_elements<=8,"operand maps fit64 bits"); + wmma::load_matrix_sync(qk,operand_marker,NS);wmma::load_matrix_sync(pv,operand_marker,NS); + #pragma unroll + for(int i=0;i(float(qk.x[i]))<<(i*8); + #pragma unroll + for(int i=0;i(float(pv.x[i]))<<(i*8); + } + __syncthreads(); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+31); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 dots[2]; + #pragma unroll + for(int sub=0;sub<2;++sub)wmma::fill_fragment(dots[sub],0.f); + #pragma unroll + for(int di=0;di<8;++di) { + wmma::fragment b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+warp*16+m/16,d=di*16+m%16; + const int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + b.x[i]=pos0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=16) { + const int pos=start+j; + if(query>=tokens||posquery)scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=8;delta;delta/=2)mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,16)); + const float nm=fmaxf(maximum[row],mx); + const float alpha=isfinite(nm)?exp2f((maximum[row]-nm)*scale_log2):1.f; + for(int j=lane;j<64;j+=16) { + float prob=0.f,pv=0.f; + if(isfinite(nm)){prob=approxWindowP(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + if(lane==0){maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + if(tid<128) { + const int row=tid/4,lane=tid%4; + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){const int j=tile*16+lane*4+pair*2;lo=r49_half_round(lo+scores[row*NS+j]);hi=r49_half_round(hi+scores[row*NS+j+1]);} + float sum=r49_half_round(lo+hi); + for(int delta=2;delta;delta/=2)sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0)denom[row]=denom[row]*rescale[row]+sum; + } + __syncthreads(); + for(int i=tid;i<32*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4)for(int sub=0;sub<2;++sub) { + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores+sub*16*NS,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+j+m/16,d=half*64+warp*16+m%16; + b.x[i]=pos a; + wmma::load_matrix_sync(a,ps+sub*16*NS+j,NS); + wmma::mma_sync(output[sub*2+half],a,b,output[sub*2+half]); + } + } + } + } + __syncthreads(); + } + for(int h=0;h<2;++h) { + if(warp<4) for(int sub=0;sub<2;++sub)wmma::store_matrix_sync(scores+sub*16*NS+warp*16,output[sub*2+h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<32*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + } + __syncthreads(); + } + return; +} +extern "C" int personal_nsa_branch_r49_wdkv32(const void* q,const void* k,const void* v,void* out,float* lse, + int b,int t,int n,int hq,int hkv,int bs,int window,int kind,int dtype,void* stream) { + if(kind==1&&dtype==1&&t>=1024&&hq/hkv==16) { + int device=0,sm=0;auto e=cudaGetDevice(&device);if(e!=cudaSuccess)return int(e); + e=cudaDeviceGetAttribute(&sm,cudaDevAttrMultiProcessorCount,device);if(e!=cudaSuccess)return int(e); + if(int64_t((t+63)/64)*b*hq>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,hq,hkv,window); + return int(cudaGetLastError()); + } + return personal_nsa_branch_r47_wsoft(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); +} +extern "C" int personal_nsa_resources_r49_wdkv32(int* values) { + cudaFuncAttributes a;auto e=cudaFuncGetAttributes(&a,nsaAttentionR49wdkv32Cuda<__nv_bfloat16>); + if(e!=cudaSuccess)return int(e);int blocks=0;e=cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaAttentionR49wdkv32Cuda<__nv_bfloat16>,256,0); + values[0]=a.numRegs;values[1]=a.sharedSizeBytes;values[2]=a.localSizeBytes;values[3]=blocks;return int(e); +} +template __global__ __launch_bounds__(256) +void nsaAttentionR49cdkv32Cuda(const T* q,const T* k,const T* v,T* out, + int tokens,int keytokens,int hq,int hkv,int bs,float* lse) { + using namespace nvcuda; + constexpr int DS=136,NS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*32,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) union {T query[32*DS];float score[32*NS];} query_score; + T* qs=query_score.query;float* scores=query_score.score; + __shared__ __align__(32) T ps[32*NS]; + // K is no longer needed when P*V begins. + + __shared__ float maximum[32],denom[32],rescale[32]; + for(int i=tid;i<32*128;i+=256) { + int row=query0+i/128,d=i%128; + qs[(i/128)*DS+d]=row output[4]; + if(warp<4)for(int h=0;h<4;++h)wmma::fill_fragment(output[h],0.f); + wmma::fragment qcache[2][8]; + if(warp<4) { + #pragma unroll + for(int sub=0;sub<2;++sub) { + #pragma unroll + for(int di=0;di<8;++di)wmma::load_matrix_sync(qcache[sub][di],qs+sub*16*DS+di*16,DS); + } + } + __syncthreads(); + unsigned long long qk_bmap=0,pv_bmap=0; + T* operand_marker=reinterpret_cast(scores); + for(int i=tid;i<256;i+=256)operand_marker[(i/16)*NS+i%16]=T(float(i)); + __syncthreads(); + if(warp<4) { + wmma::fragment qk; + wmma::fragment pv; + static_assert(qk.num_elements<=8&&pv.num_elements<=8,"operand maps fit64 bits"); + wmma::load_matrix_sync(qk,operand_marker,NS);wmma::load_matrix_sync(pv,operand_marker,NS); + #pragma unroll + for(int i=0;i(float(qk.x[i]))<<(i*8); + #pragma unroll + for(int i=0;i(float(pv.x[i]))<<(i*8); + } + __syncthreads(); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=0;start dots[2]; + #pragma unroll + for(int sub=0;sub<2;++sub)wmma::fill_fragment(dots[sub],0.f); + #pragma unroll + for(int di=0;di<8;++di) { + wmma::fragment b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+warp*16+m/16,d=di*16+m%16; + const int gd=d; + b.x[i]=pos=tokens||pos=keytokens) scores[row*NS+j]=-INFINITY; + scores[row*NS+j]=__fmul_rn(__fmul_rn(scores[row*NS+j],0.08838834764831845f),1.4426950408889634f); + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=2;delta;delta/=2) mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,4)); + // A query can have an entirely masked leading/trailing tile. + float nm=fmaxf(maximum[row],mx); + float alpha=isfinite(nm)?exp2f(maximum[row]-nm):1.f; + float sum=0; + for(int j=lane;j<64;j+=4) { + float prob=isfinite(nm)?exp2f(scores[row*NS+j]-nm):0.f; + sum+=prob;ps[row*NS+j]=T(prob); + } + for(int delta=2;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0) {denom[row]=denom[row]*alpha+sum;maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + for(int i=tid;i<32*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4)for(int sub=0;sub<2;++sub) { + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores+sub*16*NS,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+j+m/16,d=half*64+warp*16+m%16; + b.x[i]=pos a; + wmma::load_matrix_sync(a,ps+sub*16*NS+j,NS); + wmma::mma_sync(output[sub*2+half],a,b,output[sub*2+half]); + } + } + } + } + __syncthreads(); + } + for(int h=0;h<2;++h) { + if(warp<4) for(int sub=0;sub<2;++sub)wmma::store_matrix_sync(scores+sub*16*NS+warp*16,output[sub*2+h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<32*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row=1024&&hq/hkv==16) { + + nsaAttentionR49cdkv32Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,n,hq,hkv,bs,lse); + return int(cudaGetLastError()); + } + return personal_nsa_branch_r47_creg(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); +} +extern "C" int personal_nsa_resources_r49_cdkv32(int* values) { + cudaFuncAttributes a;auto e=cudaFuncGetAttributes(&a,nsaAttentionR49cdkv32Cuda<__nv_bfloat16>); + if(e!=cudaSuccess)return int(e);int blocks=0;e=cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaAttentionR49cdkv32Cuda<__nv_bfloat16>,256,0); + values[0]=a.numRegs;values[1]=a.sharedSizeBytes;values[2]=a.localSizeBytes;values[3]=blocks;return int(e); +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression_wmma.cuh b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression_wmma.cuh new file mode 100644 index 000000000..3eb62485f --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/compression_wmma.cuh @@ -0,0 +1,99 @@ +#include + +// Four native CUDA WMMA warps share 16 query rows and a 64-key tile. +// Inactive extra warps on a 32-lane target still participate in CTA barriers. +template __global__ __launch_bounds__(256) +void nsaCompressionWmmaCuda(const T* q,const T* k,const T* v,T* out, + int tokens,int keytokens,int hq,int hkv,int bs,float* lse) { + using namespace nvcuda; + constexpr int DS=136,NS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*DS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS],acc[16*DS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128; + qs[(i/128)*DS+d]=row c; + wmma::fill_fragment(c,0.f); + for(int d=0;d<128;d+=16) { + wmma::fragment a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+d,DS); + wmma::load_matrix_sync(b,ks+col*DS+d,DS); + wmma::mma_sync(c,a,b,c); + } + wmma::store_matrix_sync(scores+col,c,NS,wmma::mem_row_major); + } + } + __syncthreads(); + for(int i=tid;i<64*128;i+=256) { + int pos=start+i/128,d=i%128; + int64_t off=((int64_t(batch)*keytokens+pos)*hkv+kv)*128+d; + vs[(i/128)*DS+d]=pos=tokens||pos=keytokens) scores[row*NS+j]=-INFINITY; + scores[row*NS+j]=__fmul_rn(__fmul_rn(scores[row*NS+j],0.08838834764831845f),1.4426950408889634f); + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=2;delta;delta/=2) mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,4)); + // A query can have an entirely masked leading/trailing tile. + float nm=fmaxf(maximum[row],mx); + float alpha=isfinite(nm)?exp2f(maximum[row]-nm):1.f; + float sum=0; + for(int j=lane;j<64;j+=4) { + float prob=isfinite(nm)?exp2f(scores[row*NS+j]-nm):0.f; + sum+=prob;ps[row*NS+j]=T(prob); + } + for(int delta=2;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0) {denom[row]=denom[row]*alpha+sum;maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + for(int i=tid;i<16*128;i+=256) acc[(i/128)*DS+i%128]*=rescale[i/128]; + __syncthreads(); + if(warp<4) { + for(int col=warp*16;col<128;col+=64) { + wmma::fragment c; + wmma::load_matrix_sync(c,acc+col,DS,wmma::mem_row_major); + for(int j=0;j<64;j+=16) { + wmma::fragment a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*DS+col,DS); + wmma::mma_sync(c,a,b,c); + } + wmma::store_matrix_sync(acc+col,c,DS,wmma::mem_row_major); + } + } + __syncthreads(); + } + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128; + if(row +#include +#include +#include +#include +#include +#include +__device__ __forceinline__ unsigned int r51_keep_bits(unsigned int bits) { +#if defined(__AMDGCN__) + asm volatile("" : "+v"(bits)); +#else + asm volatile("" : "+r"(bits)); +#endif + return bits; +} +__device__ __forceinline__ float r51_half_round(float x) { + const unsigned int bits=r51_keep_bits(__half_as_ushort(__float2half_rn(x))); + return __half2float(__ushort_as_half((unsigned short)bits)); +} +template __device__ __forceinline__ float r51_gate_round(float x) { + if constexpr(std::is_same::value)return r51_half_round(x); + else if constexpr(std::is_same::value) { + const unsigned int bits=r51_keep_bits(__bfloat16_as_ushort(__float2bfloat16_rn(x))); + return __bfloat162float(__ushort_as_bfloat16((unsigned short)bits)); + } else { + return __uint_as_float(r51_keep_bits(__float_as_uint(x))); + } +} + +template static __device__ __forceinline__ G r51_fuse(T oc,T os,T ow,G gc,G gs,G gw){ + const float pc=r51_gate_round(__fmul_rn(float(oc),float(gc))); + const float ps=r51_gate_round(__fmul_rn(float(os),float(gs))); + const float pw=r51_gate_round(__fmul_rn(float(ow),float(gw))); + const float partial=r51_gate_round(__fadd_rn(pc,ps)); + return G(__fadd_rn(partial,pw)); +} + +extern "C" int personal_nsa_gate_r49_reg(const void*,const void*,const void*,const void*,const void*,const void*,void*,int64_t,int,int,void*); +extern "C" size_t personal_nsa_selected_scratch_r50_matrix_clean2(int,int,int,int,int,int,int); + +template __global__ void nsaGateR51FinalCuda(const T* oc,const T* os,const T* ow,const G* gc,const G* gs,const G* gw,G* out,int64_t elements){ + const int64_t i=(int64_t(blockIdx.x)*blockDim.x+threadIdx.x)*N; + if(i>=elements)return; + using Load=typename std::conditional::type>::type; + T c[N],s[N],w[N]; + Load cv=*reinterpret_cast(oc+i),sv=*reinterpret_cast(os+i),wv=*reinterpret_cast(ow+i); + __builtin_memcpy(c,&cv,sizeof(c));__builtin_memcpy(s,&sv,sizeof(s));__builtin_memcpy(w,&wv,sizeof(w)); + const int64_t row=i/128;const G a=gc[row],b=gs[row],d=gw[row];G values[N]; + #pragma unroll + for(int j=0;j(c[j],s[j],w[j],a,b,d); + if constexpr(sizeof(G)*N==4){uint32_t bits;__builtin_memcpy(&bits,values,4);*reinterpret_cast(out+i)=bits;} + else if constexpr(sizeof(G)*N==8){uint64_t bits;__builtin_memcpy(&bits,values,8);*reinterpret_cast(out+i)=bits;} + else if constexpr(sizeof(G)*N==16){uint4 bits;__builtin_memcpy(&bits,values,16);*reinterpret_cast(out+i)=bits;} + else {uint4 bits[2];__builtin_memcpy(bits,values,32);reinterpret_cast(out+i)[0]=bits[0];reinterpret_cast(out+i)[1]=bits[1];} +} +extern "C" int personal_nsa_gate_r51_final(const void* oc,const void* os,const void* ow,const void* gc,const void* gs,const void* gw,void* out,int64_t n,int dtype,int gate32,void* stream){ + const uintptr_t alignment=8*2,out_alignment=gate32?(4*8>16?16:4*8):8*2; + if(n<1048576||n%8!=0||(reinterpret_cast(oc)%alignment)||(reinterpret_cast(os)%alignment)||(reinterpret_cast(ow)%alignment)||(reinterpret_cast(out)%out_alignment))return personal_nsa_gate_r49_reg(oc,os,ow,gc,gs,gw,out,n,dtype,gate32,stream); + const dim3 grid((n+256*8-1)/(256*8)); + if(dtype==0&&gate32==0)nsaGateR51FinalCuda<<>>((const half*)oc,(const half*)os,(const half*)ow,(const half*)gc,(const half*)gs,(const half*)gw,(half*)out,n); + else if(dtype==1&&gate32==0)nsaGateR51FinalCuda<__nv_bfloat16,__nv_bfloat16,8><<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const __nv_bfloat16*)gc,(const __nv_bfloat16*)gs,(const __nv_bfloat16*)gw,(__nv_bfloat16*)out,n); + else if(dtype==0)nsaGateR51FinalCuda<<>>((const half*)oc,(const half*)os,(const half*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,n); + else nsaGateR51FinalCuda<__nv_bfloat16,float,8><<>>((const __nv_bfloat16*)oc,(const __nv_bfloat16*)os,(const __nv_bfloat16*)ow,(const float*)gc,(const float*)gs,(const float*)gw,(float*)out,n); + return int(cudaGetLastError()); +} +extern "C" int personal_nsa_resources_r51_final(int* values,int gate32){ + cudaFuncAttributes a;auto e=gate32?cudaFuncGetAttributes(&a,nsaGateR51FinalCuda<__nv_bfloat16,float,8>):cudaFuncGetAttributes(&a,nsaGateR51FinalCuda<__nv_bfloat16,__nv_bfloat16,8>);if(e!=cudaSuccess)return int(e);int blocks=0; + e=gate32?cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaGateR51FinalCuda<__nv_bfloat16,float,8>,256,0):cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaGateR51FinalCuda<__nv_bfloat16,__nv_bfloat16,8>,256,0);values[0]=a.numRegs;values[1]=a.sharedSizeBytes;values[2]=a.localSizeBytes;values[3]=blocks;return int(e); +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/pair_exact.cuh b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/pair_exact.cuh new file mode 100644 index 000000000..d83eba404 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/pair_exact.cuh @@ -0,0 +1,25 @@ +__device__ float pair_accumulate_exact(float p0,float p1,float c) { + const double a=double(p0),b=double(p1),cc=double(c); + const double s=__dadd_rn(a,b),t=__dadd_rn(s,cc); + if(!isfinite(t))return __double2float_rn(t); + // Products of finite FP16 values are exact FP32 values. Recover both + // FP64 addition residuals, including exact cancellation and midpoint ties. + const double vb=__dsub_rn(s,a); + const double e=__dadd_rn(__dsub_rn(a,__dsub_rn(s,vb)),__dsub_rn(b,vb)); + const double vc=__dsub_rn(t,s); + const double f=__dadd_rn(__dsub_rn(s,__dsub_rn(t,vc)),__dsub_rn(cc,vc)); + const double error=__dadd_rn(e,f); + const float r=__double2float_rn(t); + if(error==0.)return r; + if(r==0.f)return __double2float_rn(error); + const unsigned bits=__float_as_uint(r); + const float upper=__uint_as_float(r>0.f?bits+1:bits-1); + const float lower=__uint_as_float(r>0.f?bits-1:bits+1); + const double hm=__dmul_rn(__dadd_rn(double(r),double(upper)),.5); + const double lm=__dmul_rn(__dadd_rn(double(r),double(lower)),.5); + const double hd=__dadd_rn(__dsub_rn(t,hm),error); + const double ld=__dadd_rn(__dsub_rn(t,lm),error); + if(hd>0. || (hd==0. && (bits&1)))return upper; + if(ld<0. || (ld==0. && (bits&1)))return lower; + return r; +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected.cu new file mode 100644 index 000000000..e9555f6c6 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected.cu @@ -0,0 +1,157 @@ + +#include +#include +#include +#include +#include +extern "C" int personal_nsa_selected_r48_exact16(const void*,const void*,const void*,void*,const int32_t*,void*,int,int,int,int,int,int,float,int,void*); + + +static __device__ __forceinline__ float r50_selected_exp2_final(float x){return exp2f(__fmul_rn(x,1.4426950408889634f));} +template __global__ __launch_bounds__(256) +void r50_fragment_pack_matrix_clean2(const T* k,const T* v,T* pk,T* pv,int tokens,int hkv,int batches){ + using namespace nvcuda;const int tid=threadIdx.x,warp=tid/warpSize,lane=tid%warpSize,blocks=(tokens+15)/16; + __shared__ __align__(32) T marker[16*24]; + for(int i=tid;i<256;i+=256)marker[(i/16)*24+i%16]=T(float(i)); + __syncthreads(); + const int task=blockIdx.x*4+warp,total=batches*hkv*blocks*8;if(task>=total)return; + const int di=task%8,block=(task/8)%blocks,kv=(task/(8*blocks))%hkv,batch=task/(8*blocks*hkv); + const int64_t target=int64_t(task)*256+lane*4; + if(1){ + wmma::fragment map,b; + static_assert(map.num_elements==4,"native pack contract");wmma::load_matrix_sync(map,marker,24); + #pragma unroll + for(int i=0;i(pk+target)=words; + } + if(1){ + wmma::fragment map,b; + static_assert(map.num_elements==4,"native pack contract");wmma::load_matrix_sync(map,marker,24); + #pragma unroll + for(int i=0;i(pv+target)=words; + } +} +template __global__ __launch_bounds__(256) +void nsaSelectedR50matrix_clean2Cuda(const T* q,const T* k,const T* v,const int32_t* ids,T* out,const T* packedk,const T* packedv, + int tokens,int hq,int hkv,int width,int bs,float scale) { + using namespace nvcuda; + constexpr int DS=136,NS=72; + const int tid=threadIdx.x,warp=tid/warpSize,lane=tid%32; + const int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int64_t io=((int64_t(batch)*tokens+query)*hkv+kv)*width; + __shared__ __align__(32) T qs[16*DS],ps[16*NS]; + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],alpha[16]; + for(int i=tid;i<16*128;i+=256) { + const int head=i/128,d=i%128; + qs[head*DS+d]=T(float(q[((int64_t(batch)*tokens+query)*hq+kv*16+head)*128+d])*scale); + } + if(tid<16){maximum[tid]=-INFINITY;denom[tid]=0.f;} + __syncthreads(); + unsigned long long qkmap=0,pvmap=0;unsigned int rowmap=0; + T* marker=reinterpret_cast(scores); + for(int i=tid;i<256;i+=256)marker[(i/16)*NS+i%16]=T(float(i)); + __syncthreads(); + if(warp<4){ + wmma::fragment kb; + wmma::fragment vb; + static_assert(kb.num_elements<=8&&vb.num_elements<=8,"maps fit64 bits"); + wmma::load_matrix_sync(kb,marker,NS);wmma::load_matrix_sync(vb,marker,NS); + for(int i=0;i(float(kb.x[i]))<<(i*8); + for(int i=0;i(float(vb.x[i]))<<(i*8); + } + __syncthreads(); + for(int i=tid;i<256;i+=256)scores[(i/16)*NS+i%16]=float(i/16); + __syncthreads(); + if(warp<4){ + wmma::fragment rows; + wmma::load_matrix_sync(rows,scores,NS,wmma::mem_row_major); + static_assert(rows.num_elements<=8,"row map fits32 bits"); + for(int i=0;i qcache[8]; + if(warp<4){ + #pragma unroll + for(int i=0;i<8;++i)wmma::load_matrix_sync(qcache[i],qs+i*16,DS); + } + wmma::fragment output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + __syncthreads(); + for(int slot=0;slotquery)continue; + const int start=id*64,valid=min(64,query+1-start); + if(warp<4){ + wmma::fragment c;wmma::fill_fragment(c,0.f); + #pragma unroll + for(int di=0;di<8;++di){ + wmma::fragment b; + const int block=(start/16)+warp,blocks=(tokens+15)/16; + const int64_t off=(((int64_t(batch)*hkv+kv)*blocks+block)*8+di)*256+(tid%warpSize)*b.num_elements; + const uint64_t words=*reinterpret_cast(packedk+off);__builtin_memcpy(b.x,&words,sizeof(b.x)); + wmma::mma_sync(c,qcache[di],b,c); + } + wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + } + __syncthreads(); + #pragma unroll + for(int u=0;u<2;++u){ + const int head=tid/32+u*8;float localmax=-INFINITY; + for(int j=lane;j<64;j+=32){if(j>=valid)scores[head*NS+j]=-INFINITY;localmax=fmaxf(localmax,scores[head*NS+j]);} + for(int delta=16;delta;delta/=2)localmax=fmaxf(localmax,__shfl_xor_sync(0xffffffff,localmax,delta,32)); + const float nm=fmaxf(maximum[head],localmax),a=r50_selected_exp2_final(maximum[head]-nm);float blocksum=0.f; + for(int j=lane;j<64;j+=32){const float p=r50_selected_exp2_final(scores[head*NS+j]-nm);blocksum+=p;ps[head*NS+j]=T(p);} + for(int delta=16;delta;delta/=2)blocksum+=__shfl_xor_sync(0xffffffff,blocksum,delta,32); + if(lane==0){alpha[head]=a;maximum[head]=nm;denom[head]=denom[head]*a+blocksum;} + } + __syncthreads(); + if(warp<4){ + for(int h=0;h<2;++h)for(int i=0;i>(i*4))&15]; + #pragma unroll + for(int half=0;half<2;++half){ + #pragma unroll + for(int j=0;j<64;j+=16){ + wmma::fragment a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + const int block=(start+j)/16,blocks=(tokens+15)/16,di=half*4+warp; + const int64_t off=(((int64_t(batch)*hkv+kv)*blocks+block)*8+di)*256+(tid%warpSize)*b.num_elements; + const uint64_t words=*reinterpret_cast(packedv+off);__builtin_memcpy(b.x,&words,sizeof(b.x)); + if(j+16>valid){ + #pragma unroll + for(int i=0;i>(i*8))&255)/16>=valid)b.x[i]=T(0.f); + } + wmma::mma_sync(output[half],a,b,output[half]); + } + } + } + __syncthreads(); + + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + const int head=i/64,d=h*64+i%64; + out[((int64_t(batch)*tokens+query)*hq+kv*16+head)*128+d]=T(scores[head*NS+i%64]/denom[head]); + } + __syncthreads(); + } +} +extern "C" size_t personal_nsa_selected_scratch_r50_matrix_clean2(int b,int t,int hq,int hkv,int width,int bs,int dtype){ + if(b<1||b>8||t<1024||t>8192||hq<1||hq>1024||hkv<1||hkv>32||hq%hkv||hq/hkv!=16||width<1||width>32||bs!=64||dtype!=1)return 0; + return size_t(b)*hkv*((t+15)/16)*128*16*sizeof(__nv_bfloat16)*2; +} +extern "C" int personal_nsa_selected_r50_matrix_clean2(const void* q,const void* k,const void* v,void* scratch,const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream){ + size_t bytes=personal_nsa_selected_scratch_r50_matrix_clean2(b,t,hq,hkv,width,bs,dtype); + if(!bytes||!scratch)return personal_nsa_selected_r48_exact16(q,k,v,scratch,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); + auto pk=(__nv_bfloat16*)scratch;auto pv=pk+bytes/4; + const int tasks=b*hkv*((t+15)/16)*8; + r50_fragment_pack_matrix_clean2<<<(tasks+3)/4,256,0,(cudaStream_t)stream>>>((const __nv_bfloat16*)k,(const __nv_bfloat16*)v,pk,pv,t,hkv,b); + auto e=cudaGetLastError();if(e!=cudaSuccess)return int(e); + nsaSelectedR50matrix_clean2Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,pk,pv,t,hq,hkv,width,bs,scale); + return int(cudaGetLastError()); +} + +extern "C" int personal_nsa_resources_r50_matrix_clean2(int* v){cudaFuncAttributes a;auto e=cudaFuncGetAttributes(&a,nsaSelectedR50matrix_clean2Cuda<__nv_bfloat16>);if(e!=cudaSuccess)return int(e);int blocks=0;e=cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaSelectedR50matrix_clean2Cuda<__nv_bfloat16>,256,0);v[0]=a.numRegs;v[1]=a.sharedSizeBytes;v[2]=a.localSizeBytes;v[3]=blocks;return int(e);} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_fallback.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_fallback.cu new file mode 100644 index 000000000..0cd97b2cc --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_fallback.cu @@ -0,0 +1,104 @@ + +#include +#include +#include +#include +#include +extern "C" int personal_nsa_selected_packed_r46_bf16u8(const void*,const void*,const void*,void*,const int32_t*,void*,int,int,int,int,int,int,float,int,void*); +template __global__ __launch_bounds__(256) +void nsaSelectedR48Exact16Cuda(const T* q,const T* k,const T* v,const int32_t* ids,T* out, + int tokens,int hq,int hkv,int width,int bs,float scale) { + using namespace nvcuda; + constexpr int DS=136,NS=72; + const int tid=threadIdx.x,warp=tid/warpSize,lane=tid%32; + const int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int64_t io=((int64_t(batch)*tokens+query)*hkv+kv)*width; + __shared__ float rounded_q[128][17]; + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],alpha[16]; + for(int i=tid;i<16*128;i+=256) { + const int head=i/128,d=i%128; + rounded_q[d][head]=float(T(float(q[((int64_t(batch)*tokens+query)*hq+kv*16+head)*128+d])*scale)); + } + if(tid<16){maximum[tid]=-INFINITY;denom[tid]=0.f;} + const int pv_lane=tid%64,pv_head=(tid/64)*4; + float acc[8]={}; + __syncthreads(); + for(int slot=0;slotquery)continue; + const int start=id*64,valid=min(64,query+1-start); + for(int task=tid;task<16*64;task+=256) { + const int lh=task%16,j=task/16; + float dot=-INFINITY; + if(j=valid)scores[head*NS+j]=-INFINITY; + localmax=fmaxf(localmax,scores[head*NS+j]); + } + for(int delta=16;delta;delta/=2)localmax=fmaxf(localmax,__shfl_xor_sync(0xffffffff,localmax,delta,32)); + const float nm=fmaxf(maximum[head],localmax),a=expf(maximum[head]-nm); + float blocksum=0.f; + for(int j=lane;j<64;j+=32) { + const float probability=expf(scores[head*NS+j]-nm); + blocksum+=probability;scores[head*NS+j]=float(T(probability)); + } + for(int delta=16;delta;delta/=2)blocksum+=__shfl_xor_sync(0xffffffff,blocksum,delta,32); + if(lane==0){alpha[head]=a;maximum[head]=nm;denom[head]=denom[head]*a+blocksum;} + } + __syncthreads(); + #pragma unroll + for(int h=0;h<4;++h)for(int d=0;d<2;++d)acc[h*2+d]*=alpha[pv_head+h]; + #pragma unroll 8 + for(int j=0;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[(pv_head+h)*NS+j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + __syncthreads(); + + } + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/denom[pv_head+h]); + } +} + +extern "C" int personal_nsa_selected_r48_exact16(const void* q,const void* k,const void* v,void* scratch, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(dtype==1&&hq/hkv==16&&bs==64&&t>=1024&&(reinterpret_cast(v)&7)==0) { + nsaSelectedR48Exact16Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k, + (const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + return int(cudaGetLastError()); + } + return personal_nsa_selected_packed_r46_bf16u8(q,k,v,scratch,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_parent.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_parent.cu new file mode 100644 index 000000000..665d010f1 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selected_parent.cu @@ -0,0 +1,1538 @@ +#include +#include +#include +#include +#include + +// Independent 32-thread CUDA subgroups, one query head/subgroup. No K/V LDS +// tiles or block-wide barriers; only scores/probabilities occupy shared memory. +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedWarpCuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + int group=hq/hkv,gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j __global__ __launch_bounds__(256) +void nsaPrefillSelectedPV4Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=7;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[4];__builtin_memcpy(values,&packed,8); + #pragma unroll + for(int d=0;d<4;++d)acc[d]=fmaf(probability,float(values[d]),acc[d]); + } + __syncwarp(); + } + if(active) for(int d=0;d<4;++d) out[qo+lane*4+d]=T(acc[d]/denom); + return; +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedG16PV4Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[4];__builtin_memcpy(values,&packed,8); + #pragma unroll + for(int d=0;d<4;++d)acc[d]=fmaf(probability,float(values[d]),acc[d]); + } + __syncwarp(); + } + if(active) for(int d=0;d<4;++d) out[qo+lane*4+d]=T(acc[d]/denom); + return; +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedG7BS64Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + bs=64; + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=7;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[8];__builtin_memcpy(values,&packed,16); + #pragma unroll + for(int d=0;d<8;++d)acc[d]=fmaf(probability,float(values[d]),acc[d]); + } + } + __syncthreads(); + } + if(pv_active) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*7+pv_head)*128+pv_lane*8; + #pragma unroll + for(int d=0;d<8;++d)out[po+d]=T(acc[d]/head_denom[pv_head]); + } + return; +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedG16VReuse2Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[4];__builtin_memcpy(values,&packed,8); + #pragma unroll + for(int h=0;h<2;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<4;++d)acc[h*4+d]=fmaf(probability,float(values[d]),acc[h*4+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<2;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+lane*4; + #pragma unroll + for(int d=0;d<4;++d)out[po+d]=T(acc[h*4+d]/head_denom[pv_head+h]); + } + } + return; +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedG16PvUnroll8Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[4];__builtin_memcpy(values,&packed,8); + #pragma unroll + for(int h=0;h<2;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<4;++d)acc[h*4+d]=fmaf(probability,float(values[d]),acc[h*4+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<2;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+lane*4; + #pragma unroll + for(int d=0;d<4;++d)out[po+d]=T(acc[h*4+d]/head_denom[pv_head+h]); + } + } + return; +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectedG16VReuse4D2Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/head_denom[pv_head+h]); + } + } + return; +} + +template __global__ __launch_bounds__(512) +void nsaPrefillSelectedG16H16PV4Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*16+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*16+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/head_denom[pv_head+h]); + } + } + return; +} +extern "C" int personal_nsa_prefill_selected(const void* q,const void* k,const void* v, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(b<=0||t<=0||hq<=0||hkv<=0||hq%hkv||width<=0||bs<=0||bs>128||dtype<0||dtype>1) return -1; + dim3 grid(b*t,hkv,(hq/hkv+7)/8); + if(hq/hkv==7 && bs==64 && t>=1024 && (reinterpret_cast(v)&15)==0) { + if(dtype==0)nsaPrefillSelectedG7BS64Cuda<<>>((const half*)q,(const half*)k,(const half*)v,ids,(half*)out,t,hq,hkv,width,bs,scale); + else nsaPrefillSelectedG7BS64Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + } else if(hq/hkv==16 && bs==64 && t>=1024 && (reinterpret_cast(v)&7)==0) { + if(dtype==0)nsaPrefillSelectedG16H16PV4Cuda<<>>((const half*)q,(const half*)k,(const half*)v,ids,(half*)out,t,hq,hkv,width,bs,scale); + else nsaPrefillSelectedG16VReuse4D2Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + } else if(hq/hkv==7 && t>=1024 && (reinterpret_cast(v)&7)==0) { + if(dtype==0) nsaPrefillSelectedPV4Cuda<<>>((const half*)q,(const half*)k,(const half*)v,ids,(half*)out,t,hq,hkv,width,bs,scale); + else nsaPrefillSelectedPV4Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + } else { + if(dtype==0) nsaPrefillSelectedWarpCuda<<>>((const half*)q,(const half*)k,(const half*)v,ids,(half*)out,t,hq,hkv,width,bs,scale); + else nsaPrefillSelectedWarpCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + } + return int(cudaGetLastError()); +} + +template __global__ void nsaPackK64Cuda(const T* input,T* packed,int tokens,int hkv) { + __shared__ T tile[32][33]; + int x=threadIdx.x,y=threadIdx.y,batch=blockIdx.z/hkv,kv=blockIdx.z%hkv; + for(int j=0;j<32;j+=8) { + int token=blockIdx.y*32+y+j,d=blockIdx.x*32+x; + tile[y+j][x]=token __global__ __launch_bounds__(256) +void nsaPrefillSelectedG7PackedCuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + bs=64; + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=7;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[8];__builtin_memcpy(values,&packed,16); + #pragma unroll + for(int d=0;d<8;++d)acc[d]=fmaf(probability,float(values[d]),acc[d]); + } + } + __syncthreads(); + } + if(pv_active) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*7+pv_head)*128+pv_lane*8; + #pragma unroll + for(int d=0;d<8;++d)out[po+d]=T(acc[d]/head_denom[pv_head]); + } + return; +} +extern "C" int personal_nsa_selected_packed(const void* q,const void* k,const void* v,void* packed, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(b<=0||t<=0||hq<=0||hkv<=0||hq%hkv||width<=0||bs<=0||bs>128||dtype<0||dtype>1)return -1; + if(hq/hkv!=7 || bs!=64 || t<1024 || (reinterpret_cast(v)&15)!=0) + return personal_nsa_prefill_selected(q,k,v,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); + if(!packed)return -2; + dim3 pg(4,2*((t+63)/64),b*hkv),pt(32,8),grid(b*t,hkv,1); + if(dtype==0) { + nsaPackK64Cuda<<>>((const half*)k,(half*)packed,t,hkv); + nsaPrefillSelectedG7PackedCuda<<>>((const half*)q,(const half*)packed,(const half*)v,ids,(half*)out,t,hq,hkv,width,bs,scale); + } else { + nsaPackK64Cuda<<>>((const __nv_bfloat16*)k,(__nv_bfloat16*)packed,t,hkv); + nsaPrefillSelectedG7PackedCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)packed,(const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + } + return int(cudaGetLastError()); +} + +template __global__ __launch_bounds__(256) +void nsaSelectedBF16Unroll4Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/head_denom[pv_head+h]); + } + } + return; +} + +extern "C" int personal_nsa_selected_packed_r46_bf16u4(const void* q,const void* k,const void* v,void* scratch, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(dtype==1&&hq/hkv==16&&bs==64&&t>=1024&&(reinterpret_cast(v)&7)==0) { + dim3 grid(b*t,hkv,2); + nsaSelectedBF16Unroll4Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k, + (const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + return int(cudaGetLastError()); + } + return personal_nsa_selected_packed(q,k,v,scratch,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); +} + +template __global__ __launch_bounds__(256) +void nsaSelectedBF16Unroll8Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/head_denom[pv_head+h]); + } + } + return; +} + +extern "C" int personal_nsa_selected_packed_r46_bf16u8(const void* q,const void* k,const void* v,void* scratch, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(dtype==1&&hq/hkv==16&&bs==64&&t>=1024&&(reinterpret_cast(v)&7)==0) { + dim3 grid(b*t,hkv,2); + nsaSelectedBF16Unroll8Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k, + (const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + return int(cudaGetLastError()); + } + return personal_nsa_selected_packed(q,k,v,scratch,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); +} + +template __global__ __launch_bounds__(256) +void nsaSelectedBF16Unroll16Cuda(const T* q,const T* k,const T* v,const int32_t* ids, + T* out,int tokens,int hq,int hkv,int width,int bs,float scale) { + int lane=threadIdx.x%32,warp=threadIdx.x/32; + int query=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int group=16;int gh=blockIdx.z*8+warp,head=kv*group+gh; + bool active=ghquery) continue; + int start=id*bs; + int valid=min(bs,query+1-start); + if(group==16)__syncthreads(); + if(group==7) { + for(int j=lane;j(v+off); + T values[2];__builtin_memcpy(values,&packed,4); + #pragma unroll + for(int h=0;h<4;++h) { + const float probability=scores[pv_head+h][j]; + #pragma unroll + for(int d=0;d<2;++d)acc[h*2+d]=fmaf(probability,float(values[d]),acc[h*2+d]); + } + } + } + __syncthreads(); + } + if(pv_active) { + #pragma unroll + for(int h=0;h<4;++h) { + const int64_t po=((int64_t(batch)*tokens+query)*hq+kv*16+blockIdx.z*8+pv_head+h)*128+pv_lane*2; + #pragma unroll + for(int d=0;d<2;++d)out[po+d]=T(acc[h*2+d]/head_denom[pv_head+h]); + } + } + return; +} + +extern "C" int personal_nsa_selected_packed_r46_bf16u16(const void* q,const void* k,const void* v,void* scratch, + const int32_t* ids,void* out,int b,int t,int hq,int hkv,int width,int bs,float scale,int dtype,void* stream) { + if(dtype==1&&hq/hkv==16&&bs==64&&t>=1024&&(reinterpret_cast(v)&7)==0) { + dim3 grid(b*t,hkv,2); + nsaSelectedBF16Unroll16Cuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k, + (const __nv_bfloat16*)v,ids,(__nv_bfloat16*)out,t,hq,hkv,width,bs,scale); + return int(cudaGetLastError()); + } + return personal_nsa_selected_packed(q,k,v,scratch,ids,out,b,t,hq,hkv,width,bs,scale,dtype,stream); +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selector.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selector.cu new file mode 100644 index 000000000..0125a0894 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/selector.cu @@ -0,0 +1,348 @@ +// Personal CUDA prototype for the frozen author's fixed-length prefill selector. +// This does not register or change a public InfiniCore operator ABI. +#include +#include +#include +#include +#include +#include +#include "pair_exact.cuh" +#include + +template +__device__ float read_float(const T *p) { return static_cast(*p); } + +template +__device__ float scaled_query(const T *p, float scale) { + // The author multiplies Q by scale, then rounds back to Q dtype BEFORE dot. + return static_cast(static_cast(read_float(p) * scale)); +} + +// Literal scalar form of the frozen author's compare/swap network. In a +// descending stage equality swaps too: replacing this by stable argsort or +// a block-index secondary key changes membership at a tied top-k boundary. +__device__ void author_merge(float* x,int* ids,int n,int stage,int order) { + int tid=threadIdx.x; + for(int round=0;round>stage)&1):bool(order); + bool swap=(x[left]>x[right])!=flip; + int selected=swap?(tid^distance):tid; + value=x[selected];id=ids[selected]; + } + __syncthreads(); + if(tid +__global__ __launch_bounds__(128) void nsaPrefillSelectorCuda( + const T *q, const T *k, const float *lse, int32_t *output, + int tokens, int qheads, int kvheads, int compressed_blocks, + int block_size, int topk, float scale) { + const int token = blockIdx.x % tokens; + const int batch = blockIdx.x / tokens; + const int kv = blockIdx.y; + const int group = qheads / kvheads; + const int causal_blocks = token / block_size + 1; + const int tid = threadIdx.x; + const size_t out_base = (static_cast(blockIdx.x) * kvheads + kv) * topk; + + // Each thread owns one (past block, Q head) dot product. Within that + // dot, dimensions still accumulate as d=0..127 with scalar fmaf. + // Group sums and the author's sorting network retain their exact order. + extern __shared__ float importance[]; + float *probabilities=importance+compressed_blocks; + float *rounded_q=probabilities+compressed_blocks*group; + const int q_stride=group|1; // odd shared-memory pitch for both G7/G16 + if(causal_blocks>1) { + for(int i=tid;i<128*group;i+=blockDim.x) { + const int h=i/128,d=i%128; + const size_t qb=(size_t(blockIdx.x)*qheads+kv*group+h)*128; + rounded_q[d*q_stride+h]=scaled_query(q+qb+d,scale); + } + } + __syncthreads(); + for(int task=tid;task<(causal_blocks-1)*group;task+=blockDim.x) { + const int block=task/group,h=task%group; + const size_t kb=((size_t(batch)*compressed_blocks+block)*kvheads+kv)*128; + float dot=0.f; + if(group==7 && std::is_same::value) { + // Match the author's FP16 two-product FP32 accumulation boundary. + // Error-compensated CUDA arithmetic preserves cancellation and + // midpoint ties before one FP32 rounding per pair. + for(int d=0;d<128;d+=2) { + const float p0=__fmul_rn(rounded_q[d*q_stride+h],read_float(k+kb+d)); + const float p1=__fmul_rn(rounded_q[(d+1)*q_stride+h],read_float(k+kb+d+1)); + dot=pair_accumulate_exact(p0,p1,dot); + } + } else for(int d=0;d<128;++d) + dot=fmaf(rounded_q[d*q_stride+h],read_float(k+kb+d),dot); + const float head_lse=lse[size_t(blockIdx.x)*qheads+kv*group+h]; + const float probability=group==7?exp2f((dot-head_lse)*1.4426950408889634f):expf(dot-head_lse); + probabilities[block*group+h]=probability<0x1p-126f?0.f:probability; + } + __syncthreads(); + for(int block=tid;block1;n>>=1) ++dims; + for(int start=0;start>stage)&1):bool(order); + const bool compare=(tid&distance)?peer>value:value>peer; + const bool swap=compare!=flip; + if(swap){value=peer;id=peer_id;} + } +} +template __global__ __launch_bounds__(256) +void nsaPrefillSelectorG16WaveCuda(const T* q,const T* k,const float* lse,int32_t* output,int tokens,int qheads,int kvheads,int compressed_blocks,int block_size,int topk,float scale) { + using namespace nvcuda; + constexpr int DS=136,KS=72,NS=72; + const int tid=threadIdx.x,warp=tid/warpSize; + const int token=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int causal_blocks=token/block_size+1,nc=causal_blocks-1; + const size_t out_base=(size_t(blockIdx.x)*kvheads+kv)*topk; + extern __shared__ float importance[]; + float* probabilities=importance+compressed_blocks; + __shared__ __align__(32) T qs[16*DS]; + __shared__ __align__(32) float scores[16*NS]; + for(int i=tid;i<16*128;i+=256) { + const int h=i/128,d=i%128;const int gd=d; + const size_t qb=(size_t(blockIdx.x)*qheads+kv*16+h)*128+gd; + qs[h*DS+d]=T(scaled_query(q+qb,scale)); + } + __syncthreads(); + unsigned long long qkmap=0; + T* marker=reinterpret_cast(scores); + for(int i=tid;i<256;i+=256)marker[(i/16)*NS+i%16]=T(float(i)); + __syncthreads(); + if(warp<4){ + wmma::fragment map; + wmma::load_matrix_sync(map,marker,NS); + static_assert(map.num_elements<=8,"mapping fits64 bits"); + for(int i=0;i(float(map.x[i]))<<(i*8); + } + __syncthreads(); + for(int start=0;start c; + wmma::fill_fragment(c,0.f); + if(warp<4){ + #pragma unroll + for(int di=0;di<8;++di){ + wmma::fragment a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+di*16,DS); + #pragma unroll + for(int i=0;i>(i*8))&255,block=start+warp*16+m/16,d=di*16+m%16; + b.x[i]=block1;n>>=1)++dims; + for(int start=0;start __global__ __launch_bounds__(256) +void nsaPrefillSelectorG16WmmaCuda(const T* q,const T* k,const float* lse,int32_t* output,int tokens,int qheads,int kvheads,int compressed_blocks,int block_size,int topk,float scale) { + using namespace nvcuda; + constexpr int DS=136,KS=72,NS=72; + const int tid=threadIdx.x,warp=tid/warpSize; + const int token=blockIdx.x%tokens,batch=blockIdx.x/tokens,kv=blockIdx.y; + const int causal_blocks=token/block_size+1,nc=causal_blocks-1; + const size_t out_base=(size_t(blockIdx.x)*kvheads+kv)*topk; + extern __shared__ float importance[]; + float* probabilities=importance+compressed_blocks; + __shared__ __align__(32) T qs[16*DS],ks[64*KS]; + __shared__ __align__(32) float scores[16*NS]; + for(int i=tid;i<16*128;i+=256) { + const int h=i/128,d=i%128;const int gd=d; + const size_t qb=(size_t(blockIdx.x)*qheads+kv*16+h)*128+gd; + qs[h*DS+d]=T(scaled_query(q+qb,scale)); + } + __syncthreads(); + for(int start=0;start c; + wmma::fill_fragment(c,0.f); + for(int half=0;half<2;++half) { + for(int i=tid;i<64*64;i+=256) { + const int block=start+i/64,d=half*64+i%64;const int gd=d; + const size_t kb=((size_t(batch)*compressed_blocks+block)*kvheads+kv)*128+gd; + ks[(i/64)*KS+i%64]=block a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+half*64+di*16,DS); + wmma::load_matrix_sync(b,ks+warp*16*KS+di*16,KS); + wmma::mma_sync(c,a,b,c); + } + __syncthreads(); + } + if(warp<4)wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + __syncthreads(); + for(int task=tid;task<64*16;task+=256) { + const int h=task%16,j=task/16,block=start+j; + if(block1;n>>=1) ++dims; + for(int start=0;start 8192 || + qheads <= 0 || kvheads <= 0 || qheads % kvheads || dim != 128 || + block_size < 2 || block_size > 128 || (block_size & (block_size-1)) || topk > block_size/2 || topk <= 0 || topk > 32 || !std::isfinite(scale) || + compressed_blocks != (tokens + block_size - 1) / block_size || + qheads / kvheads > 32 || (dtype != 0 && dtype != 1)) return -1; + const int group = qheads / kvheads; + if(group==16 && compressed_blocks<=512) { + const size_t shared=sizeof(float)*compressed_blocks*17; + dim3 grid(batch*tokens,kvheads); + if(block_size==64){ + int device=0,wave=0;auto err=cudaGetDevice(&device);if(err!=cudaSuccess)return int(err);err=cudaDeviceGetAttribute(&wave,cudaDevAttrWarpSize,device);if(err!=cudaSuccess)return int(err); + if(wave==64){ + if(dtype==0)nsaPrefillSelectorG16WaveCuda<<(stream)>>>((const __half*)q,(const __half*)k,lse,output,tokens,qheads,kvheads,compressed_blocks,block_size,topk,scale); + else nsaPrefillSelectorG16WaveCuda<<(stream)>>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,lse,output,tokens,qheads,kvheads,compressed_blocks,block_size,topk,scale); + return int(cudaGetLastError()); + } + } + if(dtype==0)nsaPrefillSelectorG16WmmaCuda<<(stream)>>>((const __half*)q,(const __half*)k,lse,output,tokens,qheads,kvheads,compressed_blocks,block_size,topk,scale); + else nsaPrefillSelectorG16WmmaCuda<<(stream)>>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,lse,output,tokens,qheads,kvheads,compressed_blocks,block_size,topk,scale); + return int(cudaGetLastError()); + } + + const int threads = 128; + const dim3 grid(batch * tokens, kvheads); + const size_t shared_bytes = sizeof(float)*(compressed_blocks*(group+1)+128*(group|1)); + if (dtype == 0) { + nsaPrefillSelectorCuda<__half><<(stream)>>>( + static_cast(q), static_cast(k), lse, output, + tokens, qheads, kvheads, compressed_blocks, block_size, topk, scale); + } else { + nsaPrefillSelectorCuda<__nv_bfloat16><<(stream)>>>( + static_cast(q), static_cast(k), lse, output, + tokens, qheads, kvheads, compressed_blocks, block_size, topk, scale); + } + return static_cast(cudaGetLastError()); +} + +extern "C" int personal_nsa_resources_selector_wave(int* v){cudaFuncAttributes a;auto e=cudaFuncGetAttributes(&a,nsaPrefillSelectorG16WaveCuda<__nv_bfloat16>);if(e!=cudaSuccess)return int(e);int blocks=0;e=cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaPrefillSelectorG16WaveCuda<__nv_bfloat16>,256,37*sizeof(float));v[0]=a.numRegs;v[1]=a.sharedSizeBytes;v[2]=a.localSizeBytes;v[3]=blocks;return int(e);} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window.cu b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window.cu new file mode 100644 index 000000000..716cc1b5a --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window.cu @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include +#include +__device__ __forceinline__ unsigned int r49_keep_bits(unsigned int bits) { +#if defined(__AMDGCN__) + asm volatile("" : "+v"(bits)); +#else + asm volatile("" : "+r"(bits)); +#endif + return bits; +} +__device__ __forceinline__ float r49_half_round(float x) { + const unsigned int bits=r49_keep_bits(__half_as_ushort(__float2half_rn(x))); + return __half2float(__ushort_as_half((unsigned short)bits)); +} +static __device__ __forceinline__ unsigned short r50_builtin_unorm(float z){return __builtin_amdgcn_cvt_pknorm_u16(z,z)[0];} +static __device__ __forceinline__ float r50_builtin_rcp(float z){return __builtin_amdgcn_rcp(z);} +static __device__ __forceinline__ float r50_builtin_exp2(float z){return __builtin_amdgcn_exp2f(z);} +static __device__ __forceinline__ float r50_den_keep(float x){return __uint_as_float(r49_keep_bits(__float_as_uint(x)));} +static __device__ float r50_prob_window_final(float score, float maximum, float scale, float* pv) { + float c=__uint_as_float(0x3e6e28f6u); + float z=fminf(1.f,fmaxf(0.f,fmaf(score,scale*(1.f/64.f),fmaf(-maximum,scale*(1.f/64.f),c)))); + unsigned short b=r50_builtin_unorm(z); + half a=__ushort_as_half(b),d=__ushort_as_half((unsigned short)(b+512)); + half r=__float2half_rn(fmaf(__half2float(a),__half2float(__ushort_as_half(0x3da8)),__half2float(d))); + unsigned short bits=__half_as_ushort(r); + *pv=__bfloat162float(__ushort_as_bfloat16((unsigned short)(((bits>>3)<0x80)?0:(bits>>3)))); + return __half2float(r); +} +template __global__ __launch_bounds__(256) +void nsaWindowR50FinalCuda(const T* q,const T* k,const T* v,T* out, + int tokens,int hq,int hkv,int window) { + __shared__ float denparts[128];if(threadIdx.x<128)denparts[threadIdx.x]=0.f; + + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*32,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) union {T query[32*DS];float score[32*NS];} query_score; + T* qs=query_score.query;float* scores=query_score.score; + __shared__ __align__(32) T ps[32*NS]; + // K is no longer needed when P*V begins. + + __shared__ float maximum[32],denom[32],rescale[32]; + for(int i=tid;i<32*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[4]; + if(warp<4)for(int h=0;h<4;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + wmma::fragment qcache[2][8]; + if(warp<4) { + #pragma unroll + for(int sub=0;sub<2;++sub) { + #pragma unroll + for(int di=0;di<8;++di)wmma::load_matrix_sync(qcache[sub][di],qs+sub*16*DS+di*16,DS); + } + } + __syncthreads(); + unsigned long long qk_bmap=0,pv_bmap=0; + T* operand_marker=reinterpret_cast(scores); + for(int i=tid;i<256;i+=256)operand_marker[(i/16)*NS+i%16]=T(float(i)); + __syncthreads(); + if(warp<4) { + wmma::fragment qk; + wmma::fragment pv; + static_assert(qk.num_elements<=8&&pv.num_elements<=8,"operand maps fit64 bits"); + wmma::load_matrix_sync(qk,operand_marker,NS);wmma::load_matrix_sync(pv,operand_marker,NS); + #pragma unroll + for(int i=0;i(float(qk.x[i]))<<(i*8); + #pragma unroll + for(int i=0;i(float(pv.x[i]))<<(i*8); + } + __syncthreads(); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+31); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 dots[2]; + #pragma unroll + for(int sub=0;sub<2;++sub)wmma::fill_fragment(dots[sub],0.f); + #pragma unroll + for(int di=0;di<8;++di) { + wmma::fragment b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+warp*16+m/16,d=di*16+m%16; + const int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + b.x[i]=pos0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=16) { + const int pos=start+j; + if(query>=tokens||posquery)scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=8;delta;delta/=2)mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,16)); + const float nm=fmaxf(maximum[row],mx); + const float alpha=isfinite(nm)?r50_builtin_exp2((maximum[row]-nm)*scale_log2):1.f; + for(int j=lane;j<64;j+=16) { + float prob=0.f,pv=0.f; + if(isfinite(nm)){prob=r50_prob_window_final(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + if(lane==0){maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + if(tid<128) { + const int row=tid/4,lane=tid%4; + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){const int j=tile*16+lane*4+pair*2;lo=r49_half_round(lo+scores[row*NS+j]);hi=r49_half_round(hi+scores[row*NS+j+1]);} + float sum=r49_half_round(lo+hi);sum=r50_den_keep(denparts[row*4+lane]*rescale[row])+sum;denparts[row*4+lane]=sum; + for(int delta=2;delta;delta/=2)sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0)denom[row]=sum; + } + __syncthreads(); + for(int i=tid;i<32*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4)for(int sub=0;sub<2;++sub) { + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores+sub*16*NS,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i b; + #pragma unroll + for(int i=0;i>(i*8))&255,pos=start+j+m/16,d=half*64+warp*16+m%16; + b.x[i]=pos a; + wmma::load_matrix_sync(a,ps+sub*16*NS+j,NS); + wmma::mma_sync(output[sub*2+half],a,b,output[sub*2+half]); + } + } + } + } + __syncthreads(); + } + for(int h=0;h<2;++h) { + if(warp<4) for(int sub=0;sub<2;++sub)wmma::store_matrix_sync(scores+sub*16*NS+warp*16,output[sub*2+h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<32*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + } + __syncthreads(); + } + return; +} +extern "C" int personal_nsa_branch_r49_wdkv32(const void*,const void*,const void*,void*,float*,int,int,int,int,int,int,int,int,int,void*); +extern "C" int personal_nsa_branch_r50_window_final(const void* q,const void* k,const void* v,void* out,float* lse,int b,int t,int n,int hq,int hkv,int bs,int window,int kind,int dtype,void* stream){ + if(kind==1&&dtype==1&&t>=1024&&hq/hkv==16){ + int device=0,sm=0;auto e=cudaGetDevice(&device);if(e!=cudaSuccess)return int(e);e=cudaDeviceGetAttribute(&sm,cudaDevAttrMultiProcessorCount,device);if(e!=cudaSuccess)return int(e); + if(int64_t((t+63)/64)*b*hq>=int64_t(sm)*4){nsaWindowR50FinalCuda<<>>((const __nv_bfloat16*)q,(const __nv_bfloat16*)k,(const __nv_bfloat16*)v,(__nv_bfloat16*)out,t,hq,hkv,window);return int(cudaGetLastError());} + } + return personal_nsa_branch_r49_wdkv32(q,k,v,out,lse,b,t,n,hq,hkv,bs,window,kind,dtype,stream); +} +extern "C" int personal_nsa_resources_r50_window_final(int* values){cudaFuncAttributes a;auto e=cudaFuncGetAttributes(&a,nsaWindowR50FinalCuda<__nv_bfloat16>);if(e!=cudaSuccess)return int(e);int blocks=0;e=cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks,nsaWindowR50FinalCuda<__nv_bfloat16>,256,0);values[0]=a.numRegs;values[1]=a.sharedSizeBytes;values[2]=a.localSizeBytes;values[3]=blocks;return int(e);} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window_wmma.cuh b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window_wmma.cuh new file mode 100644 index 000000000..171cb690d --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/window_wmma.cuh @@ -0,0 +1,125 @@ +#include +__device__ float halfRound(float x){volatile unsigned short b=__half_as_ushort(__float2half_rn(x));return __half2float(__ushort_as_half(b));} + +// Four native CUDA WMMA warps share 16 query rows and a 64-key tile. +// Inactive extra warps on a 32-lane target still participate in CTA barriers. +template __global__ __launch_bounds__(256) +void nsaPrefillWindowRegisterCuda(const T* q,const T* k,const T* v,T* out, + int tokens,int hq,int hkv,int window) { + using namespace nvcuda; + constexpr int DS=136,NS=72,KS=72; // Padding avoids power-of-two shared-memory strides. + int tid=threadIdx.x,warp=tid/warpSize,query0=blockIdx.x*16,batch=blockIdx.y,head=blockIdx.z; + int kv=head/(hq/hkv); + __shared__ __align__(32) T qs[16*DS],tiles[64*KS],ps[16*NS]; + T* ks=tiles;T* vs=tiles; // K is no longer needed when P*V begins. + __shared__ __align__(32) float scores[16*NS]; + __shared__ float maximum[16],denom[16],rescale[16]; + for(int i=tid;i<16*128;i+=256) { + int row=query0+i/128,d=i%128;int gd=d;gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + qs[(i/128)*DS+d]=row output[2]; + if(warp<4)for(int h=0;h<2;++h)wmma::fill_fragment(output[h],0.f); + int earliest=window>0?max(0,(query0/128)*128-window+1):0; + int latest=min(tokens-1,(query0/128)*128+127); + constexpr float scale_log2=float(double(0.08838834764831845f)*1.4426950408889634); + for(int start=latest/64*64;start>=earliest/64*64;start-=64) { + const int query_end=min(tokens-1,query0+15); + const int query_begin=window>0?max(0,query0-window+1):0; + const bool empty=start>query_end || start+63 c; + wmma::fill_fragment(c,0.f); + for(int half=0;half<2;++half) { + for(int i=tid;i<64*64;i+=256) { + int pos=start+i/64,d=half*64+i%64; + int gd=(d/32)*32+((d%16)/4)*8+d%4+((d%32)/16)*4; + ks[(i/64)*KS+i%64]=pos a; + wmma::fragment b; + wmma::load_matrix_sync(a,qs+half*64+d,DS); + wmma::load_matrix_sync(b,ks+warp*16*KS+d,KS); + wmma::mma_sync(c,a,b,c); + } + __syncthreads(); + } + if(warp<4)wmma::store_matrix_sync(scores+warp*16,c,NS,wmma::mem_row_major); + } else { + for(int i=tid;i<16*64;i+=256)scores[(i/64)*NS+i%64]=-INFINITY; + } + __syncthreads(); + if(tid<64) { + int row=tid/4,lane=tid%4; + int query=query0+row,begin=window>0?max(0,query-window+1):0; + float mx=-INFINITY; + for(int j=lane;j<64;j+=4) { + int pos=start+j; + if(query>=tokens||posquery) scores[row*NS+j]=-INFINITY; + mx=fmaxf(mx,scores[row*NS+j]); + } + for(int delta=2;delta;delta/=2) mx=fmaxf(mx,__shfl_xor_sync(0xffffffff,mx,delta,4)); + // A query can have an entirely masked leading/trailing tile. + float nm=fmaxf(maximum[row],mx); + float alpha=isfinite(nm)?exp2f((maximum[row]-nm)*scale_log2):1.f; + float sum=0; + for(int j=lane;j<64;j+=4) { + float prob=0.f,pv=0.f; if(isfinite(nm)) {prob=approxWindowP(scores[row*NS+j],nm,scale_log2,&pv);if(std::is_same::value)pv=prob;} + scores[row*NS+j]=prob;ps[row*NS+j]=T(pv); + } + __syncwarp(); + float lo=0.f,hi=0.f; + for(int tile=0;tile<4;++tile)for(int pair=0;pair<2;++pair){int j=tile*16+lane*4+pair*2;lo=halfRound(lo+scores[row*NS+j]);hi=halfRound(hi+scores[row*NS+j+1]);} + sum=halfRound(lo+hi); + for(int delta=2;delta;delta/=2) sum+=__shfl_xor_sync(0xffffffff,sum,delta,4); + if(lane==0) {denom[row]=denom[row]*alpha+sum;maximum[row]=nm;rescale[row]=alpha;} + } + __syncthreads(); + for(int i=tid;i<16*16;i+=256)scores[(i/16)*NS+i%16]=rescale[i/16]; + __syncthreads(); + if(warp<4){ + wmma::fragment factor; + wmma::load_matrix_sync(factor,scores,NS,wmma::mem_row_major); + for(int h=0;h<2;++h)for(int i=0;i a; + wmma::fragment b; + wmma::load_matrix_sync(a,ps+j,NS); + wmma::load_matrix_sync(b,vs+j*KS+warp*16,KS); + wmma::mma_sync(c,a,b,c); + } + + } + __syncthreads(); + } + } + } + for(int h=0;h<2;++h) { + if(warp<4)wmma::store_matrix_sync(scores+warp*16,output[h],NS,wmma::mem_row_major); + __syncthreads(); + for(int i=tid;i<16*64;i+=256) { + int row=query0+i/64,d=h*64+i%64; + if(row>16)))); + } + __syncthreads(); + } + return; +} diff --git a/src/native/cuda/hygon/ops/nsa_videonsa_selector/kernel.h b/src/native/cuda/hygon/ops/nsa_videonsa_selector/kernel.h new file mode 100644 index 000000000..5092e68e8 --- /dev/null +++ b/src/native/cuda/hygon/ops/nsa_videonsa_selector/kernel.h @@ -0,0 +1,17 @@ +#pragma once +#include "base/nsa_videonsa_selector.h" +#include +extern "C" int personal_nsa_prefill_selector(const void*,const void*,const float*,int32_t*,int,int,int,int,int,int,int,int,float,int,void*); +namespace infini::ops { +template<> class Operator:public NsaVideonsaSelector{ + public: + using NsaVideonsaSelector::NsaVideonsaSelector; + static bool supports(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out){return q.device().type()==Device::Type::kHygon&&NsaVideonsaSelector::supports(q,k,lse,top,block,scale,out);} + bool capture_safe() const override{return true;} + void operator()(const Tensor q,const Tensor k,const Tensor lse,int top,int block,float scale,Tensor out) const override{ + Check(q,k,lse,top,block,scale,out); + const auto code=personal_nsa_prefill_selector(q.data(),k.data(),static_cast(lse.data()),static_cast(out.data()),q.size(0),q.size(1),q.size(2),k.size(2),128,k.size(1),block,top,scale,q.dtype()==DataType::kBFloat16,stream_); + if(code)throw std::runtime_error("Hygon VideoNSA selector launch failed"); + } +}; +} From cbb11eda3dc7fe0db0fb02b866fe7ce3b82af2c7 Mon Sep 17 00:00:00 2001 From: zhangxk <850999235@qq.com> Date: Fri, 20 Nov 2026 11:02:01 +0800 Subject: [PATCH 11/18] feat: add optimized Hygon Mamba conv and scan kernels Add dedicated Hygon providers with optimized decode and short-sequence paths while retaining FP32 scan state and generic layout fallbacks. Validation: clang-format and gfx936 provider instantiation compilation. --- .../cuda/hygon/ops/causal_conv1d/kernel.h | 1334 +++++++++++++++++ .../hygon/ops/mamba_selective_scan/kernel.h | 1199 +++++++++++++++ 2 files changed, 2533 insertions(+) create mode 100644 src/native/cuda/hygon/ops/causal_conv1d/kernel.h create mode 100644 src/native/cuda/hygon/ops/mamba_selective_scan/kernel.h diff --git a/src/native/cuda/hygon/ops/causal_conv1d/kernel.h b/src/native/cuda/hygon/ops/causal_conv1d/kernel.h new file mode 100644 index 000000000..54805a54a --- /dev/null +++ b/src/native/cuda/hygon/ops/causal_conv1d/kernel.h @@ -0,0 +1,1334 @@ +// Hygon-specific causal_conv1d kernels migrated from the InfiniCore +// indicator-5 submission (zxk branch, 2026-10-31): +// src/infiniop/ops/causal_conv1d/hygon/causal_conv1d_hygon.cu. +// Decode, tiled prefill, and general indexed/strided paths share the +// InfiniOps operator-specialization interface. +// Copyright 2025 InfiniTensor; MIT notice: LICENSES/InfiniCore-MIT.txt. +#ifndef INFINI_OPS_HYGON_CAUSAL_CONV1D_KERNEL_H_ +#define INFINI_OPS_HYGON_CAUSAL_CONV1D_KERNEL_H_ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "base/causal_conv1d.h" +#include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/runtime_.h" + +namespace infini::ops { +namespace hygon_conv_detail { + +struct ConvInfo { + size_t request_count, T, C, pool_size, total_tokens; + bool has_bias, has_cu_seqlens, indexed_state_pool; + int cu_seqlens_kind, initial_indices_kind, + final_indices_kind; // 0=none 1=i32 2=i64 + ptrdiff_t out_strides[3]{}, conv_state_strides[3]{}, qkv_strides[3]{}; + ptrdiff_t weight_strides[3]{}; + std::vector bias_strides; + std::vector final_conv_state_strides; +}; + +// Metadata shared by contiguous K4 launch selection and decode capture. +inline bool has_indexed_k4_layout(const ConvInfo &info) { + return info.indexed_state_pool && info.T > 0 && info.T <= 128 && + info.C == 4096 && info.initial_indices_kind != 2 && + info.final_indices_kind == 1 && info.has_bias && + info.qkv_strides[0] == static_cast(info.T * 4096) && + info.qkv_strides[1] == 4096 && info.qkv_strides[2] == 1 && + info.out_strides[0] == static_cast(info.T * 4096) && + info.out_strides[1] == 4096 && info.out_strides[2] == 1 && + info.conv_state_strides[0] == 4096 * 3 && + info.conv_state_strides[1] == 3 && info.conv_state_strides[2] == 1 && + info.weight_strides[0] == 4 && info.weight_strides[2] == 1 && + !info.bias_strides.empty() && info.bias_strides[0] == 1; +} + +namespace { +struct __align__(16) half8 { half lane[8]; }; + +struct __align__(16) bfloat168 { __nv_bfloat16 lane[8]; }; + +template +struct causal_vec16; + +template <> +struct causal_vec16 { + using type = float4; +}; + +template <> +struct causal_vec16 { + using type = half8; +}; + +template <> +struct causal_vec16<__nv_bfloat16> { + using type = bfloat168; +}; + +template +__device__ __forceinline__ float load_as_float(const T *ptr, ptrdiff_t offset) { + return static_cast(ptr[offset]); +} + +template <> +__device__ __forceinline__ float load_as_float(const half *ptr, + ptrdiff_t offset) { + return __half2float(ptr[offset]); +} + +template <> +__device__ __forceinline__ float load_as_float<__nv_bfloat16>( + const __nv_bfloat16 *ptr, ptrdiff_t offset) { + return __bfloat162float(ptr[offset]); +} + +template +__device__ __forceinline__ T cast_from_float(float x) { + return static_cast(x); +} + +template <> +__device__ __forceinline__ half cast_from_float(float x) { + return __float2half(x); +} + +template <> +__device__ __forceinline__ __nv_bfloat16 +cast_from_float<__nv_bfloat16>(float x) { + const uint32_t bits = __float_as_uint(x); + const uint32_t rounded = bits + 0x7fffu + ((bits >> 16) & 1u); + const uint16_t result = (bits & 0x7fffffffu) > 0x7f800000u + ? 0x7fffu + : static_cast(rounded >> 16); + return __ushort_as_bfloat16(result); +} + +template +__device__ __forceinline__ float conv4_with_bias(float w0, float h0, float w1, + float h1, float w2, float h2, + float w3, float h3, + float bias) { + float acc = w0 * h0 + w1 * h1 + w2 * h2 + w3 * h3; + if constexpr (std::is_same_v) { + T rounded = cast_from_float(acc); + acc = load_as_float(&rounded, 0); + } + return acc + bias; +} + +template +__device__ __forceinline__ void load_vec16(const T *ptr, ptrdiff_t offset, + float *values) { + using Vec = typename causal_vec16::type; + const Vec value = *reinterpret_cast(ptr + offset); + const T *lanes = reinterpret_cast(&value); +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + values[lane] = load_as_float(lanes, lane); + } +} + +template +__device__ __forceinline__ void store_vec16(T *ptr, ptrdiff_t offset, + const float *values) { + using Vec = typename causal_vec16::type; + Vec value; + T *lanes = reinterpret_cast(&value); +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + lanes[lane] = cast_from_float(values[lane]); + } + *reinterpret_cast(ptr + offset) = value; +} + +__device__ __forceinline__ int64_t load_optional_index(const void *indices, + bool is_i64, int idx, + int fallback) { + if (indices == nullptr) { + return static_cast(fallback); + } + return is_i64 + ? static_cast(indices)[idx] + : static_cast(static_cast(indices)[idx]); +} + +template +__global__ +__launch_bounds__(64) void causal_conv1d_k4_lowp_contiguous_t16_kernel( + T *out, T *conv_state, const T *qkv, const T *weight, const T *bias, + const int32_t *initial_state_indices, const int32_t *final_state_indices, + size_t pool_size) { + constexpr int kChannels = 4096; + constexpr int kTokens = 16; + constexpr int kTile = 4; + constexpr int kStateSize = 3; + const int request = static_cast(blockIdx.x); + const int chunk = static_cast(blockIdx.y); + const int channel_start = static_cast( + blockIdx.z * blockDim.x * kChannelsPerThread + threadIdx.x); + const int read_slot = + kHasInitialState ? initial_state_indices[request] : request; + const int write_slot = final_state_indices[request]; + if (read_slot < 0 || write_slot < 0 || + read_slot >= static_cast(pool_size) || + write_slot >= static_cast(pool_size)) { + return; + } + +#pragma unroll + for (int channel_offset = 0; channel_offset < kChannelsPerThread; + ++channel_offset) { + const int channel = channel_start + channel_offset * blockDim.x; + if (channel >= kChannels) { + continue; + } + + const int weight_offset = channel * 4; + const float weight0 = load_as_float(weight, weight_offset); + const float weight1 = load_as_float(weight, weight_offset + 1); + const float weight2 = load_as_float(weight, weight_offset + 2); + const float weight3 = load_as_float(weight, weight_offset + 3); + const float bias_value = load_as_float(bias, channel); + const ptrdiff_t batch_base = + static_cast(request) * kTokens * kChannels + channel; + + if (chunk == 0) { + const ptrdiff_t read_base = + (static_cast(read_slot) * kChannels + channel) * + kStateSize; + float history0 = load_as_float(conv_state, read_base); + float history1 = load_as_float(conv_state, read_base + 1); + float history2 = load_as_float(conv_state, read_base + 2); +#pragma unroll + for (int token = 0; token < 3; ++token) { + const ptrdiff_t offset = batch_base + token * kChannels; + const float current = load_as_float(qkv, offset); + const float result = + conv4_with_bias(weight0, history0, weight1, history1, weight2, + history2, weight3, current, bias_value); + out[offset] = cast_from_float(result); + history0 = history1; + history1 = history2; + history2 = current; + } + + const ptrdiff_t final_offset = batch_base + 15 * kChannels; + const float previous0 = load_as_float(qkv, batch_base + 12 * kChannels); + const float previous1 = load_as_float(qkv, batch_base + 13 * kChannels); + const float previous2 = load_as_float(qkv, batch_base + 14 * kChannels); + const float current = load_as_float(qkv, final_offset); + const float result = + conv4_with_bias(weight0, previous0, weight1, previous1, weight2, + previous2, weight3, current, bias_value); + out[final_offset] = cast_from_float(result); + + const ptrdiff_t write_base = + (static_cast(write_slot) * kChannels + channel) * + kStateSize; + conv_state[write_base] = cast_from_float(previous1); + conv_state[write_base + 1] = cast_from_float(previous2); + conv_state[write_base + 2] = cast_from_float(current); + } else { + const int first_token = 3 + (chunk - 1) * kTile; + const ptrdiff_t first_offset = batch_base + first_token * kChannels; + float history0 = load_as_float(qkv, first_offset - 3 * kChannels); + float history1 = load_as_float(qkv, first_offset - 2 * kChannels); + float history2 = load_as_float(qkv, first_offset - kChannels); +#pragma unroll + for (int tile_token = 0; tile_token < kTile; ++tile_token) { + const ptrdiff_t offset = first_offset + tile_token * kChannels; + const float current = load_as_float(qkv, offset); + const float result = + conv4_with_bias(weight0, history0, weight1, history1, weight2, + history2, weight3, current, bias_value); + out[offset] = cast_from_float(result); + history0 = history1; + history1 = history2; + history2 = current; + } + } + } +} + +template +__global__ __launch_bounds__(kThreads) void causal_conv1d_k4_decode_kernel( + T *out, T *conv_state, const T *qkv, const T *weight, const T *bias, + const int32_t *initial_state_indices, const int32_t *final_state_indices, + size_t pool_size) { + constexpr int kChannels = 4096; + const int request = blockIdx.y; + const int channel = blockIdx.x * kThreads + threadIdx.x; + const int read_slot = initial_state_indices == nullptr + ? request + : initial_state_indices[request]; + const int write_slot = final_state_indices[request]; + if (read_slot < 0 || write_slot < 0 || + static_cast(read_slot) >= pool_size || + static_cast(write_slot) >= pool_size) { + return; + } + const ptrdiff_t read_base = + (static_cast(read_slot) * kChannels + channel) * 3; + const ptrdiff_t write_base = + (static_cast(write_slot) * kChannels + channel) * 3; + const T history0 = conv_state[read_base]; + const T history1 = conv_state[read_base + 1]; + const T history2 = conv_state[read_base + 2]; + const ptrdiff_t input_index = + static_cast(request) * kChannels + channel; + const T current = qkv[input_index]; + float weights[4]; + if ((reinterpret_cast(weight) & 7) == 0) { + const uint2 packed = *reinterpret_cast(weight + channel * 4); + const T *values = reinterpret_cast(&packed); +#pragma unroll + for (int index = 0; index < 4; ++index) { + weights[index] = load_as_float(values, index); + } + } else { +#pragma unroll + for (int index = 0; index < 4; ++index) { + weights[index] = load_as_float(weight, channel * 4 + index); + } + } + const float result = conv4_with_bias( + weights[0], load_as_float(&history0, 0), weights[1], + load_as_float(&history1, 0), weights[2], load_as_float(&history2, 0), + weights[3], load_as_float(¤t, 0), load_as_float(bias, channel)); + out[input_index] = cast_from_float(result); + conv_state[write_base] = history1; + conv_state[write_base + 1] = history2; + conv_state[write_base + 2] = current; +} + +template +__device__ __forceinline__ float causal_decode_bits(unsigned int bits) { + const unsigned short raw = static_cast(bits); + T value; + __builtin_memcpy(&value, &raw, sizeof(T)); + return load_as_float(&value, 0); +} + +template +__device__ __forceinline__ unsigned short causal_encode_bits(float value) { + const T converted = cast_from_float(value); + unsigned short bits; + __builtin_memcpy(&bits, &converted, sizeof(bits)); + return bits; +} + +struct alignas(4) CausalHistoryPair { + unsigned int values[3]; +}; + +template +__global__ __launch_bounds__(kThreads) void causal_conv1d_k4_decode_pair_kernel( + T *out, T *conv_state, const T *qkv, const T *weight, const T *bias, + const int32_t *initial_state_indices, const int32_t *final_state_indices, + size_t pool_size) { + constexpr int kChannels = 4096; + const int request = kSingleRequestPool ? 0 : blockIdx.y; + const int channel = (blockIdx.x * kThreads + threadIdx.x) * 2; + const int read_slot = initial_state_indices == nullptr + ? request + : initial_state_indices[request]; + const int write_slot = final_state_indices[request]; + if constexpr (kSingleRequestPool) { + if (read_slot != 0 || write_slot != 0) return; + } else if (read_slot < 0 || write_slot < 0 || + static_cast(read_slot) >= pool_size || + static_cast(write_slot) >= pool_size) { + return; + } + const ptrdiff_t read_base = + (static_cast(kSingleRequestPool ? 0 : read_slot) * kChannels + + channel) * + 3; + const ptrdiff_t write_base = + (static_cast(kSingleRequestPool ? 0 : write_slot) * kChannels + + channel) * + 3; + const ptrdiff_t input_base = + static_cast(request) * kChannels + channel; + const CausalHistoryPair history = + *reinterpret_cast(conv_state + read_base); + const uint4 weights = *reinterpret_cast(weight + channel * 4); + const unsigned int current = + *reinterpret_cast(qkv + input_base); + const unsigned int biases = + *reinterpret_cast(bias + channel); + const unsigned int packed_weights[4] = {weights.x, weights.y, weights.z, + weights.w}; + const unsigned int raw_history[6] = { + history.values[0], history.values[0] >> 16, + history.values[1], history.values[1] >> 16, + history.values[2], history.values[2] >> 16}; + unsigned short encoded[2]; +#pragma unroll + for (int offset = 0; offset < 2; ++offset) { + const unsigned int first = packed_weights[offset * 2]; + const unsigned int second = packed_weights[offset * 2 + 1]; + const float result = + conv4_with_bias(causal_decode_bits(first), + causal_decode_bits(raw_history[offset * 3]), + causal_decode_bits(first >> 16), + causal_decode_bits(raw_history[offset * 3 + 1]), + causal_decode_bits(second), + causal_decode_bits(raw_history[offset * 3 + 2]), + causal_decode_bits(second >> 16), + causal_decode_bits(current >> (offset * 16)), + causal_decode_bits(biases >> (offset * 16))); + encoded[offset] = causal_encode_bits(result); + } + *reinterpret_cast(out + input_base) = + static_cast(encoded[0]) | + (static_cast(encoded[1]) << 16); + CausalHistoryPair updated; + updated.values[0] = (history.values[0] >> 16) | (history.values[1] << 16); + updated.values[1] = (current & 0xffffu) | (history.values[2] << 16); + updated.values[2] = (history.values[2] >> 16) | (current & 0xffff0000u); + *reinterpret_cast(conv_state + write_base) = updated; +} + +template +__global__ void causal_conv1d_k4_contiguous_tiled_kernel( + T *out, T *conv_state, const T *qkv, const T *weight, const T *bias, + const int32_t *initial_state_indices, const int32_t *final_state_indices, + size_t token_count, size_t pool_size) { + constexpr int kChannels = 4096; + const int request = blockIdx.x; + const int chunk = blockIdx.y; + const int channel = blockIdx.z * blockDim.x + threadIdx.x; + const int read_slot = initial_state_indices == nullptr + ? request + : initial_state_indices[request]; + const int write_slot = final_state_indices[request]; + if (read_slot < 0 || write_slot < 0 || + read_slot >= static_cast(pool_size) || + write_slot >= static_cast(pool_size)) { + return; + } + + const ptrdiff_t weight_offset = static_cast(channel) * 4; + const float weight0 = load_as_float(weight, weight_offset); + const float weight1 = load_as_float(weight, weight_offset + 1); + const float weight2 = load_as_float(weight, weight_offset + 2); + const float weight3 = load_as_float(weight, weight_offset + 3); + const float bias_value = load_as_float(bias, channel); + const ptrdiff_t batch_base = + static_cast(request) * token_count * kChannels + channel; + + if (chunk == 0) { + const ptrdiff_t read_base = + (static_cast(read_slot) * kChannels + channel) * 3; + float history0 = load_as_float(conv_state, read_base); + float history1 = load_as_float(conv_state, read_base + 1); + float history2 = load_as_float(conv_state, read_base + 2); + for (int token = 0; token < 3; ++token) { + if (token >= token_count) { + break; + } + const ptrdiff_t offset = batch_base + token * kChannels; + const float current = load_as_float(qkv, offset); + const float result = + conv4_with_bias(weight0, history0, weight1, history1, weight2, + history2, weight3, current, bias_value); + out[offset] = cast_from_float(result); + history0 = history1; + history1 = history2; + history2 = current; + } + + if (token_count >= 4) { + const ptrdiff_t final_offset = batch_base + (token_count - 1) * kChannels; + const float previous0 = load_as_float(qkv, final_offset - 3 * kChannels); + const float previous1 = load_as_float(qkv, final_offset - 2 * kChannels); + const float previous2 = load_as_float(qkv, final_offset - kChannels); + const float current = load_as_float(qkv, final_offset); + const float result = + conv4_with_bias(weight0, previous0, weight1, previous1, weight2, + previous2, weight3, current, bias_value); + out[final_offset] = cast_from_float(result); + history0 = previous1; + history1 = previous2; + history2 = current; + } + + const ptrdiff_t write_base = + (static_cast(write_slot) * kChannels + channel) * 3; + conv_state[write_base] = cast_from_float(history0); + conv_state[write_base + 1] = cast_from_float(history1); + conv_state[write_base + 2] = cast_from_float(history2); + } else { + const int64_t first_token = 3 + static_cast(chunk - 1) * kTimeTile; + const ptrdiff_t first_offset = batch_base + first_token * kChannels; + float history0 = load_as_float(qkv, first_offset - 3 * kChannels); + float history1 = load_as_float(qkv, first_offset - 2 * kChannels); + float history2 = load_as_float(qkv, first_offset - kChannels); +#pragma unroll + for (int tile_token = 0; tile_token < kTimeTile; ++tile_token) { + const int64_t token = first_token + tile_token; + if (token < static_cast(token_count) - 1) { + const ptrdiff_t offset = first_offset + tile_token * kChannels; + const float current = load_as_float(qkv, offset); + const float result = + conv4_with_bias(weight0, history0, weight1, history1, weight2, + history2, weight3, current, bias_value); + out[offset] = cast_from_float(result); + history0 = history1; + history1 = history2; + history2 = current; + } + } + } +} + +template +__global__ __launch_bounds__(256) void causal_conv1d_k4_time_tiled_vec_kernel( + T *out, T *conv_state, T *final_conv_state, const T *qkv, const T *weight, + const T *bias, const void *initial_state_indices, + const void *final_state_indices, bool has_bias, + bool initial_state_indices_i64, bool final_state_indices_i64, + size_t T_tokens, size_t C, size_t pool_size, ptrdiff_t out_s0, + ptrdiff_t out_s1, ptrdiff_t out_s2, ptrdiff_t state_s0, ptrdiff_t state_s1, + ptrdiff_t state_s2, ptrdiff_t final_s0, ptrdiff_t final_s1, + ptrdiff_t final_s2, ptrdiff_t qkv_s0, ptrdiff_t qkv_s1, ptrdiff_t qkv_s2, + ptrdiff_t weight_s0, ptrdiff_t weight_s2, ptrdiff_t bias_s0) { + constexpr int kChannelsPerBlock = kThreads * kVec; + constexpr bool kSingleChunk = kIndexed && kTimeTile == 16; + + const int64_t request = static_cast(blockIdx.x); + const int64_t chunk_start = + kSingleChunk ? 0 : static_cast(blockIdx.y) * kTimeTile; + const int64_t token_count = static_cast(T_tokens); + if (chunk_start >= token_count && !(token_count == 0 && chunk_start == 0)) { + return; + } + + const int64_t chunk_end = + kSingleChunk ? token_count : min(chunk_start + kTimeTile, token_count); + const int64_t channel_start = + static_cast(blockIdx.z) * kChannelsPerBlock + + static_cast(threadIdx.x) * kVec; + if (channel_start >= static_cast(C)) { + return; + } + + int64_t read_slot = request; + int64_t write_slot = request; + if constexpr (kIndexed) { + read_slot = load_optional_index( + initial_state_indices, initial_state_indices_i64, request, request); + if (final_state_indices != nullptr) { + write_slot = load_optional_index( + final_state_indices, final_state_indices_i64, request, request); + } + if (read_slot < 0 || write_slot < 0 || + read_slot >= static_cast(pool_size) || + (final_state_indices != nullptr && + write_slot >= static_cast(pool_size))) { + return; + } + } + + float w0[kVec]; + float w1[kVec]; + float w2[kVec]; + float w3[kVec]; + float bias_value[kVec]; + float history0[kVec]; + float history1[kVec]; + float history2[kVec]; + + const ptrdiff_t request_state_base = + static_cast(read_slot) * state_s0; + const ptrdiff_t request_qkv_base = static_cast(request) * qkv_s0; + const ptrdiff_t request_out_base = static_cast(request) * out_s0; + +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + const int64_t channel = channel_start + lane; + if (channel >= static_cast(C)) { + w0[lane] = 0.0f; + w1[lane] = 0.0f; + w2[lane] = 0.0f; + w3[lane] = 0.0f; + bias_value[lane] = 0.0f; + history0[lane] = 0.0f; + history1[lane] = 0.0f; + history2[lane] = 0.0f; + continue; + } + + const ptrdiff_t state_base = + request_state_base + static_cast(channel) * state_s1; + const ptrdiff_t weight_base = static_cast(channel) * weight_s0; + w0[lane] = load_as_float(weight, weight_base); + w1[lane] = load_as_float(weight, weight_base + weight_s2); + w2[lane] = load_as_float(weight, weight_base + 2 * weight_s2); + w3[lane] = load_as_float(weight, weight_base + 3 * weight_s2); + bias_value[lane] = + has_bias + ? load_as_float(bias, static_cast(channel) * bias_s0) + : 0.0f; + + if (chunk_start == 0) { + history0[lane] = load_as_float(conv_state, state_base); + history1[lane] = load_as_float(conv_state, state_base + state_s2); + history2[lane] = load_as_float(conv_state, state_base + 2 * state_s2); + } else if (chunk_start == 1) { + history0[lane] = load_as_float(conv_state, state_base + state_s2); + history1[lane] = load_as_float(conv_state, state_base + 2 * state_s2); + history2[lane] = load_as_float( + qkv, request_qkv_base + static_cast(channel) * qkv_s2); + } else if (chunk_start == 2) { + history0[lane] = load_as_float(conv_state, state_base + 2 * state_s2); + history1[lane] = load_as_float( + qkv, request_qkv_base + static_cast(channel) * qkv_s2); + history2[lane] = + load_as_float(qkv, request_qkv_base + qkv_s1 + + static_cast(channel) * qkv_s2); + } else { + const ptrdiff_t history_base = + request_qkv_base + static_cast(channel) * qkv_s2; + history0[lane] = load_as_float( + qkv, history_base + static_cast(chunk_start - 3) * qkv_s1); + history1[lane] = load_as_float( + qkv, history_base + static_cast(chunk_start - 2) * qkv_s1); + history2[lane] = load_as_float( + qkv, history_base + static_cast(chunk_start - 1) * qkv_s1); + } + } + + ptrdiff_t qkv_offset = request_qkv_base + chunk_start * qkv_s1 + + static_cast(channel_start) * qkv_s2; + ptrdiff_t out_offset = request_out_base + chunk_start * out_s1 + + static_cast(channel_start) * out_s2; + + const bool full_vector = + channel_start + static_cast(kVec) <= static_cast(C); + for (int64_t token = chunk_start; token < chunk_end; ++token) { + float current[kVec]; + if constexpr (kVectorIO) { + if (full_vector) { + float result[kVec]; + load_vec16(qkv, qkv_offset, current); +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + result[lane] = conv4_with_bias( + w0[lane], history0[lane], w1[lane], history1[lane], w2[lane], + history2[lane], w3[lane], current[lane], bias_value[lane]); + } + store_vec16(out, out_offset, result); + } else { +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + const int64_t channel = channel_start + lane; + if (channel < static_cast(C)) { + current[lane] = load_as_float( + qkv, qkv_offset + static_cast(lane) * qkv_s2); + const float acc = conv4_with_bias( + w0[lane], history0[lane], w1[lane], history1[lane], w2[lane], + history2[lane], w3[lane], current[lane], bias_value[lane]); + out[out_offset + static_cast(lane) * out_s2] = + cast_from_float(acc); + } else { + current[lane] = 0.0f; + } + } + } + } else { +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + const int64_t channel = channel_start + lane; + if (channel < static_cast(C)) { + current[lane] = load_as_float( + qkv, qkv_offset + static_cast(lane) * qkv_s2); + const float acc = conv4_with_bias( + w0[lane], history0[lane], w1[lane], history1[lane], w2[lane], + history2[lane], w3[lane], current[lane], bias_value[lane]); + out[out_offset + static_cast(lane) * out_s2] = + cast_from_float(acc); + } else { + current[lane] = 0.0f; + } + } + } + +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + const int64_t channel = channel_start + lane; + if (channel < static_cast(C)) { + history0[lane] = history1[lane]; + history1[lane] = history2[lane]; + history2[lane] = current[lane]; + } + } + qkv_offset += qkv_s1; + out_offset += out_s1; + } + + if (chunk_end == token_count) { + T *final_target = final_conv_state; + ptrdiff_t final_request_base = static_cast(request) * final_s0; + ptrdiff_t final_channel_stride = final_s1; + ptrdiff_t final_tap_stride = final_s2; + if constexpr (kIndexed) { + if (final_state_indices != nullptr) { + final_target = conv_state; + final_request_base = static_cast(write_slot) * state_s0; + final_channel_stride = state_s1; + final_tap_stride = state_s2; + } + } +#pragma unroll + for (int lane = 0; lane < kVec; ++lane) { + const int64_t channel = channel_start + lane; + if (channel < static_cast(C)) { + const ptrdiff_t final_base = + final_request_base + + static_cast(channel) * final_channel_stride; + final_target[final_base] = cast_from_float(history0[lane]); + final_target[final_base + final_tap_stride] = + cast_from_float(history1[lane]); + final_target[final_base + 2 * final_tap_stride] = + cast_from_float(history2[lane]); + } + } + } +} + +template +__global__ void causal_conv1d_k4_kernel( + T *out, T *conv_state, T *final_conv_state, const T *qkv, const T *weight, + const T *bias, const void *cu_seqlens, const void *initial_state_indices, + const void *final_state_indices, bool has_bias, bool has_cu_seqlens, + bool cu_seqlens_i64, bool initial_state_indices_i64, + bool final_state_indices_i64, bool indexed_state_pool, size_t T_tokens, + size_t C, size_t total_tokens, size_t pool_size, ptrdiff_t out_s0, + ptrdiff_t out_s1, ptrdiff_t out_s2, ptrdiff_t state_s0, ptrdiff_t state_s1, + ptrdiff_t state_s2, ptrdiff_t final_s0, ptrdiff_t final_s1, + ptrdiff_t final_s2, ptrdiff_t qkv_s0, ptrdiff_t qkv_s1, ptrdiff_t qkv_s2, + ptrdiff_t weight_s0, ptrdiff_t weight_s2, ptrdiff_t bias_s0) { + const int request = blockIdx.x; + const int channel = static_cast(blockIdx.y * blockDim.x + threadIdx.x); + if (channel >= C) { + return; + } + + int64_t token_begin = 0; + int64_t token_end = static_cast(T_tokens); + int token_batch = request; + if (has_cu_seqlens) { + token_begin = load_optional_index(cu_seqlens, cu_seqlens_i64, request, 0); + token_end = load_optional_index(cu_seqlens, cu_seqlens_i64, request + 1, 0); + token_batch = 0; + if (token_begin < 0 || token_end < token_begin || + token_end > static_cast(total_tokens)) { + return; + } + } + const int64_t request_len = token_end - token_begin; + + int64_t read_slot = + indexed_state_pool + ? load_optional_index(initial_state_indices, + initial_state_indices_i64, request, request) + : static_cast(request); + int64_t write_slot = + indexed_state_pool && final_state_indices != nullptr + ? load_optional_index(final_state_indices, final_state_indices_i64, + request, request) + : static_cast(request); + if (read_slot < 0 || write_slot < 0 || + read_slot >= static_cast(pool_size) || + (final_state_indices != nullptr && + write_slot >= static_cast(pool_size))) { + return; + } + + const ptrdiff_t state_base = static_cast(read_slot) * state_s0 + + static_cast(channel) * state_s1; + const ptrdiff_t weight_base = static_cast(channel) * weight_s0; + const float w0 = load_as_float(weight, weight_base); + const float w1 = load_as_float(weight, weight_base + weight_s2); + const float w2 = load_as_float(weight, weight_base + 2 * weight_s2); + const float w3 = load_as_float(weight, weight_base + 3 * weight_s2); + const float bias_value = + has_bias ? load_as_float(bias, static_cast(channel) * bias_s0) + : 0.0f; + + float history0 = load_as_float(conv_state, state_base); + float history1 = load_as_float(conv_state, state_base + state_s2); + float history2 = load_as_float(conv_state, state_base + 2 * state_s2); + + ptrdiff_t qkv_offset = static_cast(token_batch) * qkv_s0 + + static_cast(token_begin) * qkv_s1 + + static_cast(channel) * qkv_s2; + ptrdiff_t out_offset = static_cast(token_batch) * out_s0 + + static_cast(token_begin) * out_s1 + + static_cast(channel) * out_s2; + + for (int64_t t = 0; t < request_len; ++t) { + const float current = load_as_float(qkv, qkv_offset); + float acc = conv4_with_bias(w0, history0, w1, history1, w2, history2, w3, + current, bias_value); + out[out_offset] = cast_from_float(acc); + + history0 = history1; + history1 = history2; + history2 = current; + qkv_offset += qkv_s1; + out_offset += out_s1; + } + + T *final_target = + final_state_indices != nullptr ? conv_state : final_conv_state; + ptrdiff_t final_base; + ptrdiff_t final_stride2; + if (final_state_indices != nullptr) { + final_base = static_cast(write_slot) * state_s0 + + static_cast(channel) * state_s1; + final_stride2 = state_s2; + } else { + final_base = static_cast(request) * final_s0 + + static_cast(channel) * final_s1; + final_stride2 = final_s2; + } + + final_target[final_base] = cast_from_float(history0); + final_target[final_base + final_stride2] = cast_from_float(history1); + final_target[final_base + 2 * final_stride2] = cast_from_float(history2); +} +} // anonymous namespace +namespace { + +template +void launch_k4_time_tiled_vec_impl(const ConvInfo &info, void *out, + void *conv_state, void *final_conv_state, + const void *qkv, const void *weight, + const void *bias, + const void *initial_state_indices, + const void *final_state_indices, + cudaStream_t stream) { + constexpr unsigned int kChannelsPerBlock = kThreads * kVec; + + size_t time_chunks_size = (info.T + kTimeTile - 1) / kTimeTile; + size_t channel_chunks_size = + (info.C + kChannelsPerBlock - 1) / kChannelsPerBlock; + if (time_chunks_size == 0) { + time_chunks_size = 1; + } + if (channel_chunks_size == 0) { + channel_chunks_size = 1; + } + + const dim3 grid(static_cast(info.request_count), + static_cast(time_chunks_size), + static_cast(channel_chunks_size)); + + // If the final-state buffer aliases conv_state, the block holding the last + // token overwrites the history that the first chunk still has to read. + // Stage through a scratch buffer and copy back once the launch is done. + T *aliased_scratch = nullptr; + size_t aliased_elems = 0; + void *final_state_arg = final_conv_state; + if (final_conv_state != nullptr && final_conv_state == conv_state && + time_chunks_size > 1) { + const size_t rows = info.pool_size ? info.pool_size : 1; + aliased_elems = + (rows - 1) * static_cast(info.conv_state_strides[0]) + + (info.C ? info.C - 1 : 0) * + static_cast(info.conv_state_strides[1]) + + 2 * static_cast(info.conv_state_strides[2]) + 1; + if (cudaMalloc(reinterpret_cast(&aliased_scratch), + aliased_elems * sizeof(T)) == cudaSuccess) { + final_state_arg = aliased_scratch; + } + } + + causal_conv1d_k4_time_tiled_vec_kernel + <<>>( + static_cast(out), static_cast(conv_state), + static_cast(final_state_arg), static_cast(qkv), + static_cast(weight), static_cast(bias), + initial_state_indices, final_state_indices, info.has_bias, + info.initial_indices_kind == 2, info.final_indices_kind == 2, info.T, + info.C, info.pool_size, info.out_strides[0], info.out_strides[1], + info.out_strides[2], info.conv_state_strides[0], + info.conv_state_strides[1], info.conv_state_strides[2], + info.final_conv_state_strides.empty() + ? 0 + : info.final_conv_state_strides[0], + info.final_conv_state_strides.empty() + ? 0 + : info.final_conv_state_strides[1], + info.final_conv_state_strides.empty() + ? 0 + : info.final_conv_state_strides[2], + info.qkv_strides[0], info.qkv_strides[1], info.qkv_strides[2], + info.weight_strides[0], info.weight_strides[2], + info.bias_strides.empty() ? 0 : info.bias_strides[0]); + if (cudaGetLastError() != cudaSuccess) { + if (aliased_scratch != nullptr) cudaFree(aliased_scratch); + throw std::runtime_error("Hygon causal conv1d vec launch failed"); + } + if (aliased_scratch != nullptr) { + cudaMemcpyAsync(conv_state, aliased_scratch, aliased_elems * sizeof(T), + cudaMemcpyDeviceToDevice, stream); + cudaFree(aliased_scratch); + } +} + +template +bool can_use_k4_vector_io(const ConvInfo &info, const void *out, + const void *qkv) { + constexpr size_t kAlignment = 16; + const auto is_aligned = [](const void *ptr) { + return (reinterpret_cast(ptr) & (kAlignment - 1)) == 0; + }; + const auto stride_is_aligned = [](ptrdiff_t stride) { + return (static_cast(stride) * sizeof(T) & (kAlignment - 1)) == 0; + }; + return is_aligned(out) && is_aligned(qkv) && + stride_is_aligned(info.out_strides[0]) && + stride_is_aligned(info.qkv_strides[0]) && + stride_is_aligned(info.out_strides[1]) && + stride_is_aligned(info.qkv_strides[1]); +} + +template +void launch_k4_time_tiled_vec_for_tile(const ConvInfo &info, void *out, + void *conv_state, void *final_conv_state, + const void *qkv, const void *weight, + const void *bias, + const void *initial_state_indices, + const void *final_state_indices, + cudaStream_t stream, bool vector_io) { + if (vector_io) { + return launch_k4_time_tiled_vec_impl( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); + } + return launch_k4_time_tiled_vec_impl( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); +} + +template +void launch_k4_time_tiled_vec_for_channels( + const ConvInfo &info, void *out, void *conv_state, void *final_conv_state, + const void *qkv, const void *weight, const void *bias, + const void *initial_state_indices, const void *final_state_indices, + cudaStream_t stream, bool vector_io) { + if constexpr (kIndexed) { + return launch_k4_time_tiled_vec_impl( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); + } + if (info.C <= 512) { + constexpr int kSmallVec = sizeof(T) == 4 ? 2 : 4; + return launch_k4_time_tiled_vec_impl( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); + } + constexpr int kFullVec = sizeof(T) == 4 ? 4 : 8; + if (info.C <= 4096) { + return launch_k4_time_tiled_vec_for_tile( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + return launch_k4_time_tiled_vec_for_tile( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); +} + +template +void launch_k4_time_tiled_vec(const ConvInfo &info, void *out, void *conv_state, + void *final_conv_state, const void *qkv, + const void *weight, const void *bias, + const void *initial_state_indices, + const void *final_state_indices, + cudaStream_t stream, bool vector_io) { + if constexpr (kIndexed) { + if (info.T == 16) { + return launch_k4_time_tiled_vec_for_channels( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + } + if (info.T <= 16) { + return launch_k4_time_tiled_vec_for_channels( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + if (info.T <= 64) { + return launch_k4_time_tiled_vec_for_channels( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + return launch_k4_time_tiled_vec_for_channels( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); +} + +template +void launch_k4_contiguous_tiled(const ConvInfo &info, void *out, + void *conv_state, const void *qkv, + const void *weight, const void *bias, + const void *initial_state_indices, + const void *final_state_indices, + cudaStream_t stream) { + const size_t middle_chunks = + info.T < 4 ? 0 : (info.T - 4 + kTimeTile - 1) / kTimeTile; + const dim3 grid(static_cast(info.request_count), + static_cast(1 + middle_chunks), 64); + causal_conv1d_k4_contiguous_tiled_kernel + <<>>( + static_cast(out), static_cast(conv_state), + static_cast(qkv), static_cast(weight), + static_cast(bias), + static_cast(initial_state_indices), + static_cast(final_state_indices), info.T, + info.pool_size); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon causal conv1d tiled launch failed"); +} + +template +void launch_k4_lowp_t16(const ConvInfo &info, void *out, void *conv_state, + const void *qkv, const void *weight, const void *bias, + const void *initial_state_indices, + const void *final_state_indices, cudaStream_t stream) { + constexpr unsigned int kThreads = 64; + const dim3 grid( + static_cast(info.request_count), 4, + (static_cast(info.C) + kThreads * kChannelsPerThread - 1) / + (kThreads * kChannelsPerThread)); + if (initial_state_indices == nullptr) { + causal_conv1d_k4_lowp_contiguous_t16_kernel + <<>>( + static_cast(out), static_cast(conv_state), + static_cast(qkv), static_cast(weight), + static_cast(bias), nullptr, + static_cast(final_state_indices), info.pool_size); + } else { + causal_conv1d_k4_lowp_contiguous_t16_kernel + <<>>( + static_cast(out), static_cast(conv_state), + static_cast(qkv), static_cast(weight), + static_cast(bias), + static_cast(initial_state_indices), + static_cast(final_state_indices), info.pool_size); + } + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon causal conv1d T16 launch failed"); +} + +template +void launch_k4(const ConvInfo &info, void *out, void *conv_state, + void *final_conv_state, const void *qkv, const void *weight, + const void *bias, const void *cu_seqlens, + const void *initial_state_indices, + const void *final_state_indices, cudaStream_t stream) { + if (!info.has_cu_seqlens && cu_seqlens == nullptr && out != qkv && + (final_state_indices != nullptr || + !info.final_conv_state_strides.empty()) && + info.qkv_strides[2] == 1 && info.out_strides[2] == 1) { + if (has_indexed_k4_layout(info) && + (final_conv_state == nullptr || final_conv_state == conv_state) && + final_state_indices != nullptr && bias != nullptr) { + if constexpr (sizeof(T) == 2) { + if (info.T == 1 && info.request_count <= 65535) { + if ((info.request_count == 1 || info.request_count == 4 || + info.request_count == 8) && + (reinterpret_cast(out) & 3) == 0 && + (reinterpret_cast(conv_state) & 3) == 0 && + (reinterpret_cast(qkv) & 3) == 0 && + (reinterpret_cast(bias) & 3) == 0 && + (reinterpret_cast(weight) & 15) == 0) { + // A single request with a one-row pool has constant state offsets. + // Keep device index validation even when the offsets are constant. + const auto launch_pair = [&](auto single_pool) { + constexpr int kPairThreads = 128; + constexpr bool kSingleRequestPool = decltype(single_pool)::value; + causal_conv1d_k4_decode_pair_kernel + <<>>( + static_cast(out), static_cast(conv_state), + static_cast(qkv), + static_cast(weight), + static_cast(bias), + static_cast(initial_state_indices), + static_cast(final_state_indices), + info.pool_size); + }; + if (info.request_count == 1 && info.pool_size == 1) + launch_pair(std::true_type{}); + else + launch_pair(std::false_type{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error( + "Hygon causal conv1d decode pair launch failed"); + return; + } + constexpr int kThreads = 64; + causal_conv1d_k4_decode_kernel + <<>>(static_cast(out), static_cast(conv_state), + static_cast(qkv), + static_cast(weight), + static_cast(bias), + static_cast(initial_state_indices), + static_cast(final_state_indices), + info.pool_size); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error( + "Hygon causal conv1d decode launch failed"); + return; + } + if (info.T == 16) { + // Small F16 batches favor more independent channel blocks; + // BF16 B8 benefits from two channels per thread. + if constexpr (std::is_same_v) { + if (info.request_count == 1) + return launch_k4_lowp_t16( + info, out, conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); + return launch_k4_lowp_t16(info, out, conv_state, qkv, weight, + bias, initial_state_indices, + final_state_indices, stream); + } else { + if (info.request_count == 8) + return launch_k4_lowp_t16( + info, out, conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream); + return launch_k4_lowp_t16(info, out, conv_state, qkv, weight, + bias, initial_state_indices, + final_state_indices, stream); + } + } + } + if constexpr (std::is_same_v) { + if (info.request_count == 4 && info.T == 128) + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + if (info.T <= 16) { + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + if (info.request_count == 1 && info.T <= 64) { + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + if (info.request_count > 1 && info.request_count <= 4 && + (info.T < 128 || !std::is_same_v)) { + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + if (info.request_count == 1 && info.T == 128) { + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + return launch_k4_contiguous_tiled( + info, out, conv_state, qkv, weight, bias, initial_state_indices, + final_state_indices, stream); + } + const bool vector_io = can_use_k4_vector_io(info, out, qkv); + if (info.indexed_state_pool) { + return launch_k4_time_tiled_vec( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + return launch_k4_time_tiled_vec( + info, out, conv_state, final_conv_state, qkv, weight, bias, + initial_state_indices, final_state_indices, stream, vector_io); + } + + constexpr unsigned int threads = 128; + dim3 grid(static_cast(info.request_count), + (static_cast(info.C) + threads - 1) / threads, 1); + causal_conv1d_k4_kernel<<>>( + static_cast(out), static_cast(conv_state), + static_cast(final_conv_state), static_cast(qkv), + static_cast(weight), static_cast(bias), cu_seqlens, + initial_state_indices, final_state_indices, info.has_bias, + info.has_cu_seqlens, info.cu_seqlens_kind == 2, + info.initial_indices_kind == 2, info.final_indices_kind == 2, + info.indexed_state_pool, info.T, info.C, info.total_tokens, + info.pool_size, info.out_strides[0], info.out_strides[1], + info.out_strides[2], info.conv_state_strides[0], + info.conv_state_strides[1], info.conv_state_strides[2], + info.final_conv_state_strides.empty() ? 0 + : info.final_conv_state_strides[0], + info.final_conv_state_strides.empty() ? 0 + : info.final_conv_state_strides[1], + info.final_conv_state_strides.empty() ? 0 + : info.final_conv_state_strides[2], + info.qkv_strides[0], info.qkv_strides[1], info.qkv_strides[2], + info.weight_strides[0], info.weight_strides[2], + info.bias_strides.empty() ? 0 : info.bias_strides[0]); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon causal conv1d launch failed"); +} +} // namespace +} // namespace hygon_conv_detail + +template <> +class Operator : public CausalConv1d { + public: + Operator(const Tensor qkv, const Tensor weight, Tensor conv_state, + const std::optional bias, + const std::optional cu_seqlens, + const std::optional initial_state_indices, + const std::optional final_state_indices, Tensor out, + std::optional final_conv_state) + : CausalConv1d(qkv, weight, conv_state, bias, cu_seqlens, + initial_state_indices, final_state_indices, out, + final_conv_state) { + if (device_.type() != Device::Type::kHygon) + throw std::invalid_argument("Hygon causal conv requires HYGON device"); + if (weight.shape()[2] != 4 || conv_state.shape()[2] != 3) + throw std::invalid_argument( + "Hygon causal conv kernel requires K4 and state_len3"); + check_device(); + if (requests_ >= static_cast(INT_MAX) || channels_ > 65535) + throw std::invalid_argument( + "Hygon causal conv request/channel launch extent unsupported"); + info_.request_count = requests_; + info_.T = tokens_; + info_.C = channels_; + info_.pool_size = pool_; + info_.total_tokens = qkv.numel() / (channels_ ? channels_ : 1); + info_.has_bias = bias.has_value(); + info_.has_cu_seqlens = cu_seqlens.has_value(); + info_.indexed_state_pool = + final_state_indices.has_value() || final_conv_state.has_value(); + info_.cu_seqlens_kind = index_kind_(cu_seqlens); + info_.initial_indices_kind = index_kind_(initial_state_indices); + info_.final_indices_kind = index_kind_(final_state_indices); + auto fill3 = [&](ptrdiff_t dst[3], const std::optional &t) { + if (!t) return; + const auto &s = t->strides(); + for (size_t j = 0; j < s.size() && j < 3; ++j) dst[j] = s[j]; + }; + fill3(info_.out_strides, out); + fill3(info_.conv_state_strides, conv_state); + fill3(info_.qkv_strides, qkv); + if (weight.strides().size() >= 2) { + info_.weight_strides[0] = weight.strides()[0]; + info_.weight_strides[2] = weight.strides()[2]; + } + if (bias) + info_.bias_strides = {static_cast( + bias->strides().empty() ? 1 : bias->strides()[0])}; + if (final_conv_state) + for (auto s : final_conv_state->strides()) + info_.final_conv_state_strides.push_back(s); + // Only these metadata shapes are advertised for device-graph capture. + decode_capture_safe_ = + hygon_conv_detail::has_indexed_k4_layout(info_) && + (info_.T == 1 || info_.T == 16 || info_.T == 128) && + (info_.request_count == 1 || info_.request_count == 4 || + info_.request_count == 8) && + (dtype_ == DataType::kFloat16 || dtype_ == DataType::kBFloat16); + } + + void operator()(const Tensor qkv, const Tensor weight, Tensor conv_state, + const std::optional bias, + const std::optional cu_seqlens, + const std::optional initial_state_indices, + const std::optional final_state_indices, Tensor out, + std::optional final_conv_state) const override { + const Views views{qkv, + weight, + conv_state, + bias, + cu_seqlens, + initial_state_indices, + final_state_indices, + out, + final_conv_state}; + for (size_t i = 0; i < views.size(); ++i) { + const auto &t = views[i]; + if (t.has_value() != metadata_[i].has_value() || + (t && (!std::equal_to{}(*t, *metadata_[i]) || + (t->numel() && !t->data())))) + throw std::invalid_argument( + "Hygon causal conv metadata changed or nonempty storage is null"); + } + check_device(); + if (!requests_ || !channels_) return; + auto ptr = [&](size_t i) -> void * { + return views[i] ? const_cast(views[i]->data()) : nullptr; + }; + const auto stream = static_cast(stream_ ? stream_ : nullptr); + switch (dtype_) { + case DataType::kFloat32: + hygon_conv_detail::launch_k4(info_, ptr(7), ptr(2), ptr(8), + ptr(0), ptr(1), ptr(3), ptr(4), + ptr(5), ptr(6), stream); + break; + case DataType::kFloat16: + hygon_conv_detail::launch_k4(info_, ptr(7), ptr(2), ptr(8), + ptr(0), ptr(1), ptr(3), ptr(4), + ptr(5), ptr(6), stream); + break; + case DataType::kBFloat16: + hygon_conv_detail::launch_k4<__nv_bfloat16>( + info_, ptr(7), ptr(2), ptr(8), ptr(0), ptr(1), ptr(3), ptr(4), + ptr(5), ptr(6), stream); + break; + default: + throw std::invalid_argument("Hygon causal conv dtype unsupported"); + } + } + + bool capture_safe() const override { return decode_capture_safe_; } + + private: + hygon_conv_detail::ConvInfo info_{}; + // Canonical indexed K4 kernels launch only GPU work after construction. + bool decode_capture_safe_ = false; + static int index_kind_(const std::optional &t) { + if (!t) return 0; + return t->dtype() == DataType::kInt64 ? 2 : 1; + } + void check_device() const { + int current = -1; + if (cudaGetDevice(¤t) != cudaSuccess) + throw std::runtime_error("Hygon causal conv device query failed"); + if (current != device_.index()) + throw std::invalid_argument("Hygon causal conv owner device mismatch"); + } +}; + +} // namespace infini::ops + +#endif // INFINI_OPS_HYGON_CAUSAL_CONV1D_KERNEL_H_ diff --git a/src/native/cuda/hygon/ops/mamba_selective_scan/kernel.h b/src/native/cuda/hygon/ops/mamba_selective_scan/kernel.h new file mode 100644 index 000000000..eae8f81b3 --- /dev/null +++ b/src/native/cuda/hygon/ops/mamba_selective_scan/kernel.h @@ -0,0 +1,1199 @@ +// Hygon-specific Mamba selective scan kernels migrated from the +// InfiniCore indicator-5 submission (zxk branch, 2026-10-31): +// src/infiniop/ops/mamba_selective_scan/hygon/mamba_selective_scan_hygon.cu. +// Ported to the InfiniOps operator-specialization architecture. +// Copyright 2025 InfiniTensor; MIT notice: LICENSES/InfiniCore-MIT.txt. +#ifndef INFINI_OPS_HYGON_MAMBA_SELECTIVE_SCAN_KERNEL_H_ +#define INFINI_OPS_HYGON_MAMBA_SELECTIVE_SCAN_KERNEL_H_ + +#include +#include +#include +#include + +#include "base/mamba_selective_scan.h" +#include "caster.h" +#include "dispatcher.h" +#include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/runtime_.h" +#include "native/cuda/ops/mamba_selective_scan/kernel.h" + +namespace infini::ops { +namespace hygon_mamba_scan_detail { +namespace { +template +__device__ float to_float(T v) { + return static_cast(v); +} +template <> +__device__ float to_float(half v) { + return __half2float(v); +} +template <> +__device__ float to_float<__nv_bfloat16>(__nv_bfloat16 v) { + return __bfloat162float(v); +} +template +__device__ T from_float(float v) { + return static_cast(v); +} +template <> +__device__ half from_float(float v) { + return __float2half_rn(v); +} +template <> +__device__ __nv_bfloat16 from_float<__nv_bfloat16>(float v) { + return __float2bfloat16_rn(v); +} + +__device__ float softplusf_stable(float x) { + return x > 20.0f ? x : log1pf(expf(x)); +} +__device__ float siluf(float x) { return x / (1.0f + expf(-x)); } +__device__ __forceinline__ float softplusf_t1(float x) { + if (x > 20.0f) return x; + const float e = __expf(x); + // Preserve small deltas when adding exp(x) to one would round to one. + return x < -8.0f ? e * (1.0f - 0.5f * e) : __logf(1.0f + e); +} +__device__ __forceinline__ float siluf_t1(float x) { + return x / (1.0f + __expf(-x)); +} + +template +__device__ __forceinline__ float round_to_dtype(float v) { + return v; +} +template <> +__device__ __forceinline__ float round_to_dtype(float v) { + return __half2float(__float2half_rn(v)); +} +template <> +__device__ __forceinline__ float round_to_dtype<__nv_bfloat16>(float v) { + return __bfloat162float(__float2bfloat16_rn(v)); +} + +template +__device__ __forceinline__ float finish_mamba_output(float state_dot, float x, + float d, float gate) { + float y = round_to_dtype(state_dot); + y = round_to_dtype(y + round_to_dtype(x * d)); + y = round_to_dtype(y * round_to_dtype(siluf(gate))); + return y; +} + +template +__device__ __forceinline__ float finish_mamba_output_t1(float state_dot, + float x, float d, + float gate) { + float y = round_to_dtype(state_dot); + y = round_to_dtype(y + round_to_dtype(x * d)); + y = round_to_dtype(y * round_to_dtype(siluf_t1(gate))); + return y; +} + +constexpr int kWavefrontSize = 64; +constexpr int kWavesPerBlock = 4; +constexpr unsigned long long kWavefrontMask = 0xffffffffffffffffull; + +struct AffinePair { + float a; + float u; +}; + +__device__ __forceinline__ AffinePair compose_affine(AffinePair prefix, + AffinePair next) { + return {next.a * prefix.a, next.a * prefix.u + next.u}; +} + +__device__ __forceinline__ AffinePair warp_inclusive_affine(AffinePair value) { + for (int offset = 1; offset < kWavefrontSize; offset <<= 1) { + const float previous_a = __shfl_up_sync(kWavefrontMask, value.a, offset); + const float previous_u = __shfl_up_sync(kWavefrontMask, value.u, offset); + if ((threadIdx.x & (kWavefrontSize - 1)) >= offset) { + value = compose_affine({previous_a, previous_u}, value); + } + } + return value; +} + +template +__global__ __launch_bounds__(kThreads) void mamba_selective_scan_short16_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state, + size_t intermediate_arg, size_t state_size_arg) { + const size_t intermediate = kCanonical ? 4096 : intermediate_arg; + const size_t state_size = kCanonical ? 16 : state_size_arg; + constexpr int kTokens = 16; + constexpr int kSharedStride = kTokens + (kCanonical ? 4 : 0); + constexpr int kChannelsPerBlock = kThreads / kTokens; + const size_t linear = blockIdx.x * kChannelsPerBlock + threadIdx.x / kTokens; + const size_t ch = linear % intermediate; + const size_t batch_idx = linear / intermediate; + const int token = threadIdx.x % kTokens; + const size_t state_base = linear * state_size; + + extern __shared__ unsigned char smem[]; + T *b_shared = reinterpret_cast(smem); + T *c_shared = b_shared + kSharedStride * state_size; + if constexpr (kCanonical) { + // Coalesce global B/C loads, then pad transposed shared rows. + for (size_t index = threadIdx.x; index < kTokens * state_size; + index += kThreads) { + const size_t n = index % state_size, t = index / state_size; + const size_t global = batch_idx * kTokens * state_size + index; + b_shared[n * kSharedStride + t] = b[global]; + c_shared[n * kSharedStride + t] = c[global]; + } + } else { + const size_t state_idx = threadIdx.x / kTokens; + const size_t global_index = + (batch_idx * kTokens + token) * state_size + state_idx; + b_shared[state_idx * kSharedStride + token] = b[global_index]; + c_shared[state_idx * kSharedStride + token] = c[global_index]; + } + __syncthreads(); + + const size_t x_idx = (batch_idx * kTokens + token) * intermediate + ch; + const float x_value = to_float(x[x_idx]); + const float dt_value = round_to_dtype( + softplusf_stable(to_float(dt[x_idx]) + to_float(dt_bias[ch]))); + const float d_value = to_float(d[ch]); + const float gate_value = to_float(gate[x_idx]); + const float lane_a_coeff = + -__expf(to_float(a_log[ch * state_size + token])); + float y = 0.0f; + +#pragma unroll 2 + for (int state_idx = 0; state_idx < 16; ++state_idx) { + const float a_coeff = + __shfl_sync(kWavefrontMask, lane_a_coeff, state_idx, kTokens); + const float b_value = + to_float(b_shared[state_idx * kSharedStride + token]); + AffinePair prefix = {__expf(a_coeff * dt_value), + dt_value * b_value * x_value}; + for (int offset = 1; offset < kTokens; offset <<= 1) { + const float previous_a = + __shfl_up_sync(kWavefrontMask, prefix.a, offset, kTokens); + const float previous_u = + __shfl_up_sync(kWavefrontMask, prefix.u, offset, kTokens); + if (token >= offset) { + prefix = compose_affine({previous_a, previous_u}, prefix); + } + } + float initial_state = token == 0 ? state[state_base + state_idx] : 0.0f; + initial_state = __shfl_sync(kWavefrontMask, initial_state, 0, kTokens); + const float current_state = prefix.a * initial_state + prefix.u; + y += round_to_dtype(current_state) * + to_float(c_shared[state_idx * kSharedStride + token]); + if (token == kTokens - 1) { + state[state_base + state_idx] = current_state; + } + } + + y = finish_mamba_output(y, x_value, d_value, gate_value); + out[x_idx] = from_float(y); +} + +template +__global__ +__launch_bounds__(kThreads) void mamba_selective_scan_sequential_s16_kernel( + DataType *out, const DataType *x, const DataType *dt, const DataType *b, + const DataType *c, const DataType *a_log, const DataType *d, + const DataType *gate, const DataType *dt_bias, float *state, size_t batch, + size_t seq_len, size_t intermediate) { + constexpr int kStateSize = 16; + constexpr int kChannelsPerBlock = kThreads / kStateSize; + const int state_index = threadIdx.x % kStateSize; + const size_t linear = + blockIdx.x * kChannelsPerBlock + threadIdx.x / kStateSize; + if (linear >= batch * intermediate) { + return; + } + const size_t channel = linear % intermediate; + const size_t batch_index = linear / intermediate; + const size_t state_base = linear * kStateSize; + const float bias_value = to_float(dt_bias[channel]); + const float d_value = + state_index == 0 ? to_float(d[channel]) : 0.0f; + const size_t prepared_index = + (batch_index * seq_len + state_index) * intermediate + channel; + float prepared_x = 0.0f; + float prepared_delta = 0.0f; + float prepared_gate = 0.0f; + if (state_index < seq_len) { + prepared_x = to_float(x[prepared_index]); + prepared_delta = round_to_dtype( + softplusf_stable(to_float(dt[prepared_index]) + bias_value)); + prepared_gate = round_to_dtype( + siluf(to_float(gate[prepared_index]))); + } + float state_value = state[state_base + state_index]; + const float a_value = + -expf(to_float(a_log[channel * kStateSize + state_index])); + for (size_t token = 0; token < seq_len; ++token) { + const size_t input_index = + (batch_index * seq_len + token) * intermediate + channel; + const size_t token_base = (batch_index * seq_len + token) * kStateSize; + const float x_value = + __shfl_sync(kWavefrontMask, prepared_x, token, kStateSize); + const float delta = + __shfl_sync(kWavefrontMask, prepared_delta, token, kStateSize); + const float gate_value = + __shfl_sync(kWavefrontMask, prepared_gate, token, kStateSize); + const float discrete_a = expf(a_value * delta); + const float b_value = to_float(b[token_base + state_index]); + const float c_value = to_float(c[token_base + state_index]); + state_value = discrete_a * state_value + delta * b_value * x_value; + float state_dot = round_to_dtype(state_value) * c_value; +#pragma unroll + for (int offset = kStateSize / 2; offset > 0; offset >>= 1) { + state_dot += + __shfl_down_sync(kWavefrontMask, state_dot, offset, kStateSize); + } + if (state_index == 0) { + float result = round_to_dtype(state_dot); + result = round_to_dtype( + result + round_to_dtype(x_value * d_value)); + result = round_to_dtype(result * gate_value); + out[input_index] = from_float(result); + } + } + state[state_base + state_index] = state_value; +} + +template +__global__ +__launch_bounds__(256) void mamba_selective_scan_short_s16_4lane_kernel( + DataType *out, const DataType *x, const DataType *dt, const DataType *b, + const DataType *c, const DataType *a_log, const DataType *d, + const DataType *gate, const DataType *dt_bias, float *state, size_t batch, + size_t intermediate) { + constexpr int kStateSize = 16; + constexpr int kLanesPerChannel = 4; + constexpr int kStatesPerLane = kStateSize / kLanesPerChannel; + constexpr int kChannelsPerBlock = 256 / kLanesPerChannel; + const size_t linear = + blockIdx.x * kChannelsPerBlock + threadIdx.x / kLanesPerChannel; + if (linear >= batch * intermediate) { + return; + } + + const int lane = threadIdx.x % kLanesPerChannel; + const size_t channel = linear % intermediate; + const size_t batch_index = linear / intermediate; + const size_t state_base = linear * kStateSize; + const float bias_value = to_float(dt_bias[channel]); + const float d_value = lane == 0 ? to_float(d[channel]) : 0.0f; + float state_value[kStatesPerLane]; + float a_value[kStatesPerLane]; +#pragma unroll + for (int index = 0; index < kStatesPerLane; ++index) { + const int state_index = index * kLanesPerChannel + lane; + state_value[index] = state[state_base + state_index]; + a_value[index] = + -expf(to_float(a_log[channel * kStateSize + state_index])); + } + +#pragma unroll + for (int token = 0; token < kTokens; ++token) { + const size_t input_index = + (batch_index * kTokens + token) * intermediate + channel; + const size_t token_base = (batch_index * kTokens + token) * kStateSize; + float prepared_x = 0.0f; + float prepared_delta = 0.0f; + float prepared_gate = 0.0f; + if (lane == 0) { + prepared_x = to_float(x[input_index]); + prepared_delta = round_to_dtype( + kTokens == 1 + ? softplusf_t1(to_float(dt[input_index]) + bias_value) + : softplusf_stable(to_float(dt[input_index]) + + bias_value)); + prepared_gate = round_to_dtype( + kTokens == 1 ? siluf_t1(to_float(gate[input_index])) + : siluf(to_float(gate[input_index]))); + } + const float x_value = + __shfl_sync(kWavefrontMask, prepared_x, 0, kLanesPerChannel); + const float delta = + __shfl_sync(kWavefrontMask, prepared_delta, 0, kLanesPerChannel); + const float gate_value = + __shfl_sync(kWavefrontMask, prepared_gate, 0, kLanesPerChannel); + float state_dot = 0.0f; +#pragma unroll + for (int index = 0; index < kStatesPerLane; ++index) { + const int state_index = index * kLanesPerChannel + lane; + const float b_value = to_float(b[token_base + state_index]); + const float c_value = to_float(c[token_base + state_index]); + state_value[index] = expf(a_value[index] * delta) * state_value[index] + + delta * b_value * x_value; + state_dot += round_to_dtype(state_value[index]) * c_value; + } + state_dot += + __shfl_down_sync(kWavefrontMask, state_dot, 2, kLanesPerChannel); + state_dot += + __shfl_down_sync(kWavefrontMask, state_dot, 1, kLanesPerChannel); + if (lane == 0) { + float result = round_to_dtype(state_dot); + result = round_to_dtype( + result + round_to_dtype(x_value * d_value)); + result = round_to_dtype(result * gate_value); + out[input_index] = from_float(result); + } + } + +#pragma unroll + for (int index = 0; index < kStatesPerLane; ++index) { + state[state_base + index * kLanesPerChannel + lane] = state_value[index]; + } +} + +template +__global__ void mamba_selective_scan_scalar_short_s16_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state, size_t batch, + size_t seq_len, size_t intermediate) { + constexpr int kStateSize = 16; + const size_t linear = blockIdx.x * blockDim.x + threadIdx.x; + const size_t total = batch * intermediate; + if (linear >= total) { + return; + } + + const size_t channel = linear % intermediate; + const size_t batch_index = linear / intermediate; + const size_t state_base = linear * kStateSize; + const float bias_value = to_float(dt_bias[channel]); + const float d_value = to_float(d[channel]); + + float state_value[kStateSize]; + float a_value[kStateSize]; +#pragma unroll + for (int state_index = 0; state_index < kStateSize; ++state_index) { + state_value[state_index] = state[state_base + state_index]; + const float log_value = + to_float(a_log[channel * kStateSize + state_index]); + if constexpr (kMaxTokens == 1 && std::is_same_v) { + a_value[state_index] = -__expf(log_value); + } else { + a_value[state_index] = -expf(log_value); + } + } + + for (size_t token = 0; token < seq_len && token < kMaxTokens; ++token) { + const size_t input_index = + (batch_index * seq_len + token) * intermediate + channel; + const float x_value = to_float(x[input_index]); + const float delta = round_to_dtype( + kMaxTokens == 1 + ? softplusf_t1(to_float(dt[input_index]) + bias_value) + : softplusf_stable(to_float(dt[input_index]) + bias_value)); + const float gate_value = to_float(gate[input_index]); + const size_t token_base = (batch_index * seq_len + token) * kStateSize; + float result = 0.0f; + +#pragma unroll + for (int state_index = 0; state_index < kStateSize; ++state_index) { + const float b_value = to_float(b[token_base + state_index]); + const float c_value = to_float(c[token_base + state_index]); + const float exponent = a_value[state_index] * delta; + const float discrete_a = + kMaxTokens == 1 && std::is_same_v ? __expf(exponent) + : expf(exponent); + state_value[state_index] = + discrete_a * state_value[state_index] + delta * b_value * x_value; + result += round_to_dtype(state_value[state_index]) * c_value; + } + + result = + kMaxTokens == 1 + ? finish_mamba_output_t1(result, x_value, d_value, gate_value) + : finish_mamba_output(result, x_value, d_value, gate_value); + out[input_index] = from_float(result); + } + +#pragma unroll + for (int state_index = 0; state_index < kStateSize; ++state_index) { + state[state_base + state_index] = state_value[state_index]; + } +} + +template +__global__ void mamba_selective_scan_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state, size_t batch, + size_t seq_len, size_t intermediate, size_t state_size) { + size_t linear = blockIdx.x * blockDim.x + threadIdx.x; + size_t total = batch * intermediate; + if (linear >= total) { + return; + } + size_t ch = linear % intermediate; + size_t batch_idx = linear / intermediate; + size_t state_base = (batch_idx * intermediate + ch) * state_size; + const float dt_bias_ch = to_float(dt_bias[ch]); + const float d_ch = to_float(d[ch]); + const T *a_ch = a_log + ch * state_size; + for (size_t t = 0; t < seq_len; ++t) { + size_t x_idx = (batch_idx * seq_len + t) * intermediate + ch; + float xt = to_float(x[x_idx]); + float dtv = round_to_dtype( + softplusf_stable(to_float(dt[x_idx]) + dt_bias_ch)); + float y = 0.0f; + const size_t token_base = (batch_idx * seq_len + t) * state_size; + const T *b_t = b + token_base; + const T *c_t = c + token_base; +#pragma unroll 4 + for (size_t n = 0; n < state_size; ++n) { + float a = -expf(to_float(a_ch[n])); + float discrete_a = expf(a * dtv); + float bn = to_float(b_t[n]); + float cn = to_float(c_t[n]); + float s = discrete_a * state[state_base + n] + dtv * bn * xt; + state[state_base + n] = s; + y += round_to_dtype(s) * cn; + } + y = finish_mamba_output(y, xt, d_ch, to_float(gate[x_idx])); + out[x_idx] = from_float(y); + } +} + +template +__global__ +__launch_bounds__(kWavefrontSize) void mamba_selective_scan_parallel_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state, size_t batch, + size_t seq_len, size_t intermediate, size_t state_size) { + const size_t linear = blockIdx.x; + const size_t total = batch * intermediate; + if (linear >= total) { + return; + } + + const size_t ch = linear % intermediate; + const size_t batch_idx = linear / intermediate; + const size_t state_base = linear * state_size; + const size_t lane = threadIdx.x & (kWavefrontSize - 1); + const T *a_ch = a_log + ch * state_size; + const float d_ch = to_float(d[ch]); + const float dt_bias_ch = to_float(dt_bias[ch]); + + extern __shared__ unsigned char smem[]; + const size_t tile_tokens = static_cast(kWavefrontSize * kItems); + T *b_shared = reinterpret_cast(smem); + T *c_shared = b_shared + tile_tokens * state_size; + if constexpr (kUseShared) { + const size_t valid_tokens = seq_len < tile_tokens ? seq_len : tile_tokens; + const size_t tile_elements = valid_tokens * state_size; + for (size_t index = lane; index < tile_elements; index += kWavefrontSize) { + const size_t token = index / state_size; + const size_t state_idx = index - token * state_size; + const size_t global_index = + (batch_idx * seq_len + token) * state_size + state_idx; + b_shared[state_idx * tile_tokens + token] = b[global_index]; + c_shared[state_idx * tile_tokens + token] = c[global_index]; + } + __syncthreads(); + } + + float x_vals[kItems]; + float dt_vals[kItems]; + float gate_vals[kItems]; + size_t token_indices[kItems]; +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = lane * kItems + i; + token_indices[i] = t; + if (t < seq_len) { + const size_t x_idx = (batch_idx * seq_len + t) * intermediate + ch; + x_vals[i] = to_float(x[x_idx]); + dt_vals[i] = round_to_dtype( + softplusf_stable(to_float(dt[x_idx]) + dt_bias_ch)); + gate_vals[i] = to_float(gate[x_idx]); + } else { + x_vals[i] = 0.0f; + dt_vals[i] = 0.0f; + gate_vals[i] = 0.0f; + } + } + + float y_vals[kItems] = {}; + for (size_t n = 0; n < state_size; ++n) { + float a_coeff = lane == 0 ? -expf(to_float(a_ch[n])) : 0.0f; + a_coeff = __shfl_sync(kWavefrontMask, a_coeff, 0); + AffinePair local_prefix[kItems]; +#pragma unroll + for (int i = 0; i < kItems; ++i) { + local_prefix[i] = {1.0f, 0.0f}; + const size_t t = token_indices[i]; + if (t < seq_len) { + float b_value; + if constexpr (kUseShared) { + b_value = to_float(b_shared[n * tile_tokens + t]); + } else { + b_value = to_float(b[(batch_idx * seq_len + t) * state_size + n]); + } + const AffinePair current = { + expf(a_coeff * dt_vals[i]), + dt_vals[i] * b_value * x_vals[i], + }; + local_prefix[i] = + i == 0 ? current : compose_affine(local_prefix[i - 1], current); + } else if (i > 0) { + local_prefix[i] = local_prefix[i - 1]; + } + } + + const AffinePair lane_prefix = + warp_inclusive_affine(local_prefix[kItems - 1]); + const float previous_a = __shfl_up_sync(kWavefrontMask, lane_prefix.a, 1); + const float previous_u = __shfl_up_sync(kWavefrontMask, lane_prefix.u, 1); + const AffinePair prefix_before_lane = + lane == 0 ? AffinePair{1.0f, 0.0f} : AffinePair{previous_a, previous_u}; + float initial_state = lane == 0 ? state[state_base + n] : 0.0f; + initial_state = __shfl_sync(kWavefrontMask, initial_state, 0); + float final_state = initial_state; + +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = token_indices[i]; + if (t < seq_len) { + const AffinePair prefix = + compose_affine(prefix_before_lane, local_prefix[i]); + const float current_state = prefix.a * initial_state + prefix.u; + float c_value; + if constexpr (kUseShared) { + c_value = to_float(c_shared[n * tile_tokens + t]); + } else { + c_value = to_float(c[(batch_idx * seq_len + t) * state_size + n]); + } + y_vals[i] += round_to_dtype(current_state) * c_value; + final_state = current_state; + } + } + + if (lane * kItems <= seq_len - 1 && (lane + 1) * kItems > seq_len - 1) { + state[state_base + n] = final_state; + } + } + +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = token_indices[i]; + if (t < seq_len) { + const size_t x_idx = (batch_idx * seq_len + t) * intermediate + ch; + float y = + finish_mamba_output(y_vals[i], x_vals[i], d_ch, gate_vals[i]); + out[x_idx] = from_float(y); + } + } +} + +template +__global__ +__launch_bounds__(kWavefrontSize *kGroupWaves) void mamba_selective_scan_grouped_shared_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state, size_t batch, + size_t seq_len, size_t intermediate_arg, size_t state_size_arg) { + const size_t intermediate = kCanonical ? 4096 : intermediate_arg; + const size_t state_size = kCanonical ? 16 : state_size_arg; + const size_t total = batch * intermediate; + + const size_t block_linear = blockIdx.x; + const size_t base_linear = block_linear * kGroupWaves; + if (base_linear >= total) { + return; + } + const size_t wave = threadIdx.x / kWavefrontSize; + const size_t lane = threadIdx.x & (kWavefrontSize - 1); + const size_t linear = base_linear + wave; + const size_t batch_idx = base_linear / intermediate; + + extern __shared__ unsigned char smem[]; + const size_t tile_tokens = static_cast(kWavefrontSize * kItems); + const size_t shared_stride = tile_tokens + (kCanonical ? 4 : 0); + T *b_shared = reinterpret_cast(smem); + T *c_shared = b_shared + shared_stride * state_size; + const size_t valid_tokens = seq_len < tile_tokens ? seq_len : tile_tokens; + const size_t tile_elements = valid_tokens * state_size; + for (size_t index = threadIdx.x; index < tile_elements; index += blockDim.x) { + const size_t token = index / state_size; + const size_t state_idx = index - token * state_size; + const size_t global_index = + (batch_idx * seq_len + token) * state_size + state_idx; + b_shared[state_idx * shared_stride + token] = b[global_index]; + c_shared[state_idx * shared_stride + token] = c[global_index]; + } + __syncthreads(); + if (linear >= total) { + return; + } + const size_t ch = linear % intermediate; + const size_t state_base = linear * state_size; + const T *a_ch = a_log + ch * state_size; + const float d_ch = to_float(d[ch]); + const float dt_bias_ch = to_float(dt_bias[ch]); + + float x_vals[kItems]; + float dt_vals[kItems]; + float gate_vals[kItems]; + size_t token_indices[kItems]; +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = lane * kItems + i; + token_indices[i] = t; + if (t < seq_len) { + const size_t x_idx = (batch_idx * seq_len + t) * intermediate + ch; + x_vals[i] = to_float(x[x_idx]); + dt_vals[i] = round_to_dtype( + softplusf_stable(to_float(dt[x_idx]) + dt_bias_ch)); + gate_vals[i] = to_float(gate[x_idx]); + } else { + x_vals[i] = 0.0f; + dt_vals[i] = 0.0f; + gate_vals[i] = 0.0f; + } + } + + float y_vals[kItems] = {}; + for (size_t n = 0; n < state_size; ++n) { + float a_coeff = lane == 0 ? -expf(to_float(a_ch[n])) : 0.0f; + a_coeff = __shfl_sync(kWavefrontMask, a_coeff, 0); + AffinePair local_prefix[kItems]; +#pragma unroll + for (int i = 0; i < kItems; ++i) { + local_prefix[i] = {1.0f, 0.0f}; + const size_t t = token_indices[i]; + if (t < seq_len) { + const float b_value = to_float(b_shared[n * shared_stride + t]); + const AffinePair current = { + kItems == 2 ? __expf(a_coeff * dt_vals[i]) + : expf(a_coeff * dt_vals[i]), + dt_vals[i] * b_value * x_vals[i], + }; + local_prefix[i] = + i == 0 ? current : compose_affine(local_prefix[i - 1], current); + } else if (i > 0) { + local_prefix[i] = local_prefix[i - 1]; + } + } + + const AffinePair lane_prefix = + warp_inclusive_affine(local_prefix[kItems - 1]); + const float previous_a = __shfl_up_sync(kWavefrontMask, lane_prefix.a, 1); + const float previous_u = __shfl_up_sync(kWavefrontMask, lane_prefix.u, 1); + const AffinePair prefix_before_lane = + lane == 0 ? AffinePair{1.0f, 0.0f} : AffinePair{previous_a, previous_u}; + float initial_state = lane == 0 ? state[state_base + n] : 0.0f; + initial_state = __shfl_sync(kWavefrontMask, initial_state, 0); + float final_state = initial_state; + +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = token_indices[i]; + if (t < seq_len) { + const AffinePair prefix = + compose_affine(prefix_before_lane, local_prefix[i]); + const float current_state = prefix.a * initial_state + prefix.u; + const float c_value = to_float(c_shared[n * shared_stride + t]); + y_vals[i] += round_to_dtype(current_state) * c_value; + final_state = current_state; + } + } + + if (lane * kItems <= seq_len - 1 && (lane + 1) * kItems > seq_len - 1) { + state[state_base + n] = final_state; + } + } + +#pragma unroll + for (int i = 0; i < kItems; ++i) { + const size_t t = token_indices[i]; + if (t < seq_len) { + const size_t x_idx = (batch_idx * seq_len + t) * intermediate + ch; + float y = + finish_mamba_output(y_vals[i], x_vals[i], d_ch, gate_vals[i]); + out[x_idx] = from_float(y); + } + } +} + +__device__ __forceinline__ float decode_expf(float value) { + return __builtin_amdgcn_exp2f(value * 1.4426950408889634f); +} + +__device__ __forceinline__ float decode_logf(float value) { + return __builtin_amdgcn_logf(value) * 0.6931471805599453f; +} + +__device__ __forceinline__ float decode_softplusf(float value) { + if (value > 20.0f) { + return value; + } + const float exponential = decode_expf(value); + if (value < -8.0f) { + return exponential * (1.0f - 0.5f * exponential); + } + return decode_logf(1.0f + exponential); +} + +template +__device__ __forceinline__ float decode_quad(float value) { + return __int_as_float(__builtin_amdgcn_mov_dpp(__float_as_int(value), + kPattern, 0xf, 0xf, false)); +} + +template +__device__ __forceinline__ T decode_from_float(float value) { + return from_float(value); +} + +template <> +__device__ __forceinline__ __nv_bfloat16 +decode_from_float<__nv_bfloat16>(float value) { + const unsigned int bits = __float_as_uint(value); + const unsigned int rounded = bits + 0x7fffu + ((bits >> 16) & 1u); + const unsigned short result = + (bits & 0x7fffffffu) > 0x7f800000u + ? 0x7fffu + : static_cast(rounded >> 16); + return __ushort_as_bfloat16(result); +} + +template +struct alignas(sizeof(T) * kCount) DecodeValues { + T values[kCount]; +}; + +template +__device__ __forceinline__ T *decode_offset(T *base, int index) { + using Byte = std::conditional_t, const char, char>; + const unsigned int offset = + static_cast(index) * static_cast(sizeof(T)); + return reinterpret_cast(reinterpret_cast(base) + offset); +} + +template +__global__ +__launch_bounds__(kThreads) void mamba_selective_scan_decode_s16_kernel( + T *out, const T *x, const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, float *state) { + constexpr int kStatesPerLane = 16 / kLanes; + using StateVector = DecodeValues; + using InputVector = DecodeValues; + const int channel = blockIdx.x * (kThreads / kLanes) + threadIdx.x / kLanes; + const int lane = threadIdx.x % kLanes; + const int batch_index = kBatchOne ? 0 : blockIdx.y; + const int linear = batch_index * 4096 + channel; + const int state_base = linear * 16 + lane * kStatesPerLane; + StateVector previous; + if constexpr (kAligned) { + previous = *reinterpret_cast( + decode_offset(state, state_base)); + } else { + __builtin_memcpy(&previous, decode_offset(state, state_base), + sizeof(StateVector)); + } + InputVector log_values; + InputVector b_values; + InputVector c_values; + if constexpr (kAligned) { + log_values = *reinterpret_cast( + decode_offset(a_log, channel * 16 + lane * kStatesPerLane)); + b_values = *reinterpret_cast( + decode_offset(b, batch_index * 16 + lane * kStatesPerLane)); + c_values = *reinterpret_cast( + decode_offset(c, batch_index * 16 + lane * kStatesPerLane)); + } + const float x_value = static_cast(*decode_offset(x, linear)); + const float dt_value = static_cast(*decode_offset(dt, linear)) + + static_cast(*decode_offset(dt_bias, channel)); + const float delta = decode_softplusf(dt_value); + const float raw_gate = static_cast(*decode_offset(gate, linear)); + const float gate_value = + raw_gate * __builtin_amdgcn_rcpf(1.0f + decode_expf(-raw_gate)); + const float scaled_input = delta * x_value; + const float scaled_delta = delta * 1.4426950408889634f; + float residual = 0.0f; + if constexpr (kLanes == 4) { + residual = static_cast(*decode_offset(d, channel)); + if constexpr (!kEarlyState) asm volatile("" : "+v"(residual)); + } + StateVector updated; + float result = 0.0f; + // Keep the original four-lane (sum0 + sum2) + (sum1 + sum3) reduction order. + float second_result = 0.0f; +#pragma unroll + for (int index = 0; index < kStatesPerLane; ++index) { + const int state_index = lane * kStatesPerLane + index; + const float log_value = + kAligned + ? to_float(log_values.values[index]) + : to_float(*decode_offset(a_log, channel * 16 + state_index)); + const float coefficient = -decode_expf(log_value); + const float b_value = + kAligned + ? to_float(b_values.values[index]) + : to_float(*decode_offset(b, batch_index * 16 + state_index)); + const float c_value = + kAligned + ? to_float(c_values.values[index]) + : to_float(*decode_offset(c, batch_index * 16 + state_index)); + const float current = __builtin_amdgcn_exp2f(coefficient * scaled_delta) * + previous.values[index] + + scaled_input * b_value; + updated.values[index] = current; + if constexpr (kPreserveQuadSum) { + if (index < 4) { + result += current * c_value; + } else { + second_result += current * c_value; + } + } else { + result += current * c_value; + } + } + // B1 can issue state stores before the output reduction to shorten liveness. + if constexpr (kLanes != 4 || kEarlyState) { + *reinterpret_cast(decode_offset(state, state_base)) = + updated; + } + if constexpr (kPreserveQuadSum) { + result += decode_quad<0xb1>(result); + second_result += decode_quad<0xb1>(second_result); + result += second_result; + } else { + if constexpr (kLanes >= 4) { + result += decode_quad<0x4e>(result); + } + result += decode_quad<0xb1>(result); + } + if (lane == 0) { + if constexpr (kLanes != 4) { + residual = static_cast(*decode_offset(d, channel)); + } + *decode_offset(out, linear) = + decode_from_float((result + x_value * residual) * gate_value); + } + if constexpr (kLanes == 4 && !kEarlyState) { + if constexpr (kAligned) { + *reinterpret_cast(decode_offset(state, state_base)) = + updated; + } else { + __builtin_memcpy(decode_offset(state, state_base), &updated, + sizeof(StateVector)); + } + } +} + +template +void launch_decode_s16_impl(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, + float *state, cudaStream_t stream) { + const dim3 blocks(4096 / (kThreads / kLanes), + static_cast(p.batch)); + mamba_selective_scan_decode_s16_kernel< + T, kThreads, kAligned, kLanes, kPreserveQuadSum, kBatchOne, kEarlyState> + <<>>(out, x, dt, b, c, a_log, d, gate, + dt_bias, state); +} + +template +void launch_decode_s16(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, + float *state, cudaStream_t stream) { + const bool aligned = (reinterpret_cast(state) & 15) == 0 && + (reinterpret_cast(a_log) & 7) == 0 && + (reinterpret_cast(b) & 7) == 0 && + (reinterpret_cast(c) & 7) == 0; + const bool wide_aligned = (reinterpret_cast(state) & 31) == 0 && + (reinterpret_cast(a_log) & 15) == 0 && + (reinterpret_cast(b) & 15) == 0 && + (reinterpret_cast(c) & 15) == 0; + if (p.batch == 1 && aligned) { + return launch_decode_s16_impl( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); + } + if constexpr (std::is_same_v) { + if (p.batch == 8 && wide_aligned) { + return launch_decode_s16_impl( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); + } + } + if (p.batch == 4 && wide_aligned) { + return launch_decode_s16_impl( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); + } + if (aligned) { + return launch_decode_s16_impl(p, out, x, dt, b, c, a_log, d, + gate, dt_bias, state, stream); + } + if (p.batch == 1) { + return launch_decode_s16_impl(p, out, x, dt, b, c, a_log, d, + gate, dt_bias, state, stream); + } + return launch_decode_s16_impl(p, out, x, dt, b, c, a_log, d, + gate, dt_bias, state, stream); +} + +// --------------------------------------------------------------------------- +// Host-side launcher: shape-based dispatch migrated from the old Hygon +// descriptor. T=1 decode launches the standalone optimized scan. +// --------------------------------------------------------------------------- +template +void launch_grouped_scan_impl(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, + const T *a_log, const T *d, const T *gate, + const T *dt_bias, float *state, + cudaStream_t stream) { + const size_t states = kCanonical ? 16 : p.states; + const size_t stride = kWavefrontSize * kItems + (kCanonical ? 4 : 0); + const size_t shared_bytes = 2 * stride * states * sizeof(T); + const dim3 blocks((p.batch * p.channels + kGroupWaves - 1) / kGroupWaves); + mamba_selective_scan_grouped_shared_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, p.batch, p.length, + p.channels, p.states); +} + +template +void launch_grouped_scan(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, + float *state, cudaStream_t stream) { + if (p.length == 128 && p.channels == 4096 && p.states == 16) + return launch_grouped_scan_impl( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); + return launch_grouped_scan_impl( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); +} + +template +void launch_short16_scan(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, + float *state, cudaStream_t stream) { + const size_t stride = 16 + (kCanonical ? 4 : 0); + const dim3 blocks(p.batch * p.channels / (kThreads / 16)); + mamba_selective_scan_short16_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, p.channels, + p.states); +} + +template +void launch_scan_dispatch(const MambaScanLayout &p, T *out, const T *x, + const T *dt, const T *b, const T *c, const T *a_log, + const T *d, const T *gate, const T *dt_bias, + float *state, cudaStream_t stream) { + const size_t batch = p.batch, length = p.length; + const size_t channels = p.channels, states = p.states; + const size_t total = batch * channels; + + // F16/BF16 decode: single token, state 16, channels 4096. + if constexpr (!std::is_same_v) { + if (length == 1 && states == 16 && channels == 4096 && + (batch == 1 || batch == 4 || batch == 8)) { + launch_decode_s16(p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, + stream); + return; + } + } + + // F16 (B 1/4/8) / BF16 (B 1) short-s16 4-lane decode. + if (((std::is_same_v && + (batch == 1 || batch == 4 || batch == 8)) || + (std::is_same_v && batch == 1)) && + length == 1 && states == 16 && channels % 64 == 0) { + constexpr int kThreads = 256; + constexpr int kChannelsPerBlock = kThreads / 4; + const dim3 blocks((total + kChannelsPerBlock - 1) / kChannelsPerBlock); + mamba_selective_scan_short_s16_4lane_kernel + <<>>(out, x, dt, b, c, a_log, d, gate, + dt_bias, state, batch, channels); + return; + } + + // Scalar short-s16 kernel for B=4 short sequences and BF16 B 4/8 T=1. + if (((batch == 4 && (length == 3 || length == 4 || length == 7)) || + (std::is_same_v && (batch == 4 || batch == 8) && + length == 1) || + (std::is_same_v && batch == 1 && + (length == 3 || length == 4 || length == 7))) && + states == 16 && channels % 16 == 0) { + const size_t threads = total <= 1024 ? 128 : 256; + const dim3 blocks((total + threads - 1) / threads); + if (length == 1) { + mamba_selective_scan_scalar_short_s16_kernel + <<>>(out, x, dt, b, c, a_log, d, gate, + dt_bias, state, batch, length, + channels); + } else { + mamba_selective_scan_scalar_short_s16_kernel + <<>>(out, x, dt, b, c, a_log, d, gate, + dt_bias, state, batch, length, + channels); + } + return; + } + + // BF16 sequential s16 for 1 < length < 16. + if constexpr (std::is_same_v) { + if (length > 0 && length < 16 && states == 16 && channels % 16 == 0) { + constexpr size_t kThreads = 256; + constexpr size_t kChannelsPerBlock = kThreads / 16; + const dim3 blocks((total + kChannelsPerBlock - 1) / kChannelsPerBlock); + mamba_selective_scan_sequential_s16_kernel + <<>>(out, x, dt, b, c, a_log, d, gate, + dt_bias, state, batch, length, + channels); + return; + } + } + + // Keep dynamic-channel layouts on the original staging path. + if (length == 16 && states == 16 && channels % 16 == 0) { + if (channels == 4096) { + if constexpr (std::is_same_v) { + if (batch == 1) + return launch_short16_scan( + p, out, x, dt, b, c, a_log, d, gate, dt_bias, state, stream); + } + return launch_short16_scan(p, out, x, dt, b, c, a_log, d, + gate, dt_bias, state, stream); + } + return launch_short16_scan(p, out, x, dt, b, c, a_log, d, + gate, dt_bias, state, stream); + } + + // 16 < length <= 128: parallel / grouped-shared kernels. + if (length >= 16 && length <= 128) { + const bool use_grouped_shared = + channels % kWavesPerBlock == 0 && states <= 64; + if (length <= 64) { + const size_t shared_bytes = 2 * kWavefrontSize * states * sizeof(T); + if (use_grouped_shared) { + launch_grouped_scan(p, out, x, dt, b, c, a_log, d, gate, + dt_bias, state, stream); + } else { + const dim3 blocks(total); + const bool use_shared = states > 32; + if (use_shared) { + mamba_selective_scan_parallel_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, batch, + length, channels, states); + } else { + mamba_selective_scan_parallel_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, batch, + length, channels, states); + } + } + } else { + const size_t shared_bytes = 4 * kWavefrontSize * states * sizeof(T); + if (use_grouped_shared) { + if (batch >= 4 && channels % 16 == 0) { + // Small channel sets need more independent blocks. + if (total <= 1024) + launch_grouped_scan(p, out, x, dt, b, c, a_log, d, gate, + dt_bias, state, stream); + else + launch_grouped_scan(p, out, x, dt, b, c, a_log, d, gate, + dt_bias, state, stream); + } else { + launch_grouped_scan(p, out, x, dt, b, c, a_log, d, gate, + dt_bias, state, stream); + } + } else { + const dim3 blocks(total); + const bool use_shared = states > 32; + if (use_shared) { + mamba_selective_scan_parallel_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, batch, + length, channels, states); + } else { + mamba_selective_scan_parallel_kernel + <<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, batch, + length, channels, states); + } + } + } + return; + } + + // Distribute long S16 work across more CUs without changing the math. + const unsigned int threads = + length > 128 && states == 16 && total <= 4096 ? 64 : 256; + const dim3 blocks((total + threads - 1) / threads); + mamba_selective_scan_kernel<<>>( + out, x, dt, b, c, a_log, d, gate, dt_bias, state, batch, length, channels, + states); +} +} // anonymous namespace +} // namespace hygon_mamba_scan_detail + +struct MambaScanHygonBackend : Runtime { + static void CheckLaunch() { + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon MambaSelectiveScan CUDA launch failed"); + } +}; + +template <> +class Operator + : public CudaMambaSelectiveScan { + public: + using CudaMambaSelectiveScan::CudaMambaSelectiveScan; + + void operator()(Tensor out, const Tensor x, const Tensor dt, const Tensor b, + const Tensor c, const Tensor a_log, const Tensor d, + const Tensor gate, const Tensor dt_bias, + Tensor state) const override { + // Optimized paths require contiguous low-precision data and matching + // parameter dtypes. Other supported layouts/dtypes use the generic path. + if (layout_.contiguous && a_log.dtype() == dtype_ && d.dtype() == dtype_ && + dtype_ != DataType::kFloat32) { + const auto stream = static_cast(stream_); + DispatchFunc>( + dtype_, + [&](auto tag) { + using T = TypeMapType; + hygon_mamba_scan_detail::launch_scan_dispatch( + layout_, static_cast(out.data()), + static_cast(x.data()), + static_cast(dt.data()), + static_cast(b.data()), + static_cast(c.data()), + static_cast(a_log.data()), + static_cast(d.data()), + static_cast(gate.data()), + static_cast(dt_bias.data()), + static_cast(state.data()), stream); + }, + "HygonMambaSelectiveScan"); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon mamba scan launch failed"); + return; + } + CudaMambaSelectiveScan::operator()( + out, x, dt, b, c, a_log, d, gate, dt_bias, state); + } + + bool capture_safe() const override { return true; } +}; + +} // namespace infini::ops + +#endif // INFINI_OPS_HYGON_MAMBA_SELECTIVE_SCAN_KERNEL_H_ From 1eff391010dba6590ecbf739b446a6775ba8d4d2 Mon Sep 17 00:00:00 2001 From: 2506363862 <2506363862@qq.com> Date: Fri, 20 Nov 2026 16:33:16 +0800 Subject: [PATCH 12/18] chore: remove the VideoNSA migration document --- docs/videonsa_hygon_migration.md | 70 -------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 docs/videonsa_hygon_migration.md diff --git a/docs/videonsa_hygon_migration.md b/docs/videonsa_hygon_migration.md deleted file mode 100644 index 894701f7c..000000000 --- a/docs/videonsa_hygon_migration.md +++ /dev/null @@ -1,70 +0,0 @@ -# VideoNSA on the separated InfiniOps architecture - -This migration retains the qualified Hygon/gfx936 kernels from the indicator04 -szl-based implementation (InfiniCore commit 772f0c237a2cd9560f6e99e8955d3529f0f68590). -The arithmetic translation units and auxiliary headers under -`src/native/cuda/hygon/ops/nsa_videonsa_prefill/legacy/` are copied without edits. -The old descriptor ABI is replaced by ordinary InfiniOps operator keys. - -## Interfaces - -- `NsaVideonsaPrefill(q,k,v,g_cmp,g_slc,g_swa,top,block,window,out)` executes - compression, compression attention, selection, selected attention, window - attention and gate combination. It includes K/V packing where required. -- `NsaVideonsaSelector(q,k,lse,top,block,scale,out)` returns selector IDs. -- The paged decode keys are `NsaCompressPagedCache` and `NsaPagedAttention`. - -Prefill Q is contiguous `[B,T,Hq,128]`; K/V are contiguous `[B,T,Hkv,128]`. -Q/K/V share FP16 or BF16. Gates are contiguous `[B,T,Hq]` and share Q dtype or -FP32; output has Q shape and gate dtype. B is 1..8, T is 1..8192, -Hq is 1..1024, Hkv is 1..32, and Hq/Hkv is an integer at most 32. -Block is a power of two in 2..128; top is 1..min(32,block/2). Window is -nonnegative. Full prefill uses the original D^-1/2 scale. - -Selector K is `[B,ceil(T/block),Hkv,128]`; LSE is contiguous FP32 in physical -`[B,Hq,T]` order. Output is contiguous int32 `[B,T,Hkv,top]`. Its supplied scale -must be finite. The original sorting network, tie semantics, causal masks and -low precision rounding are retained. - -## Workspace and stream ownership - -Use the public `Make` result's `workspace_size_in_bytes()` to reserve a stable -256-byte-aligned device workspace for Prefill. Retain workspace, inputs and -outputs until their stream completes, including every graph replay. A plain -`Handle` has no automatic workspace allocator; either set its workspace/size or -provide a stream-safe lease allocator. Selector needs no workspace. Providers -opt into capture only with this storage lifetime contract. The portable Runner -prepares and retains storage outside capture. - -The CMake source property preserves the old `-Xclang --fno-cuda-host-device-constexpr` option only for these legacy CUDA files; other -operators keep their original compilation flags. The qualified target is gfx936. - -## Build and regression - -Add `nsa_videonsa_prefill` and `nsa_videonsa_selector` with -`{"implementations":"all"}` to the operator configuration used for a Hygon build. -Build and install the `infiniops` library target with bounded parallelism. - -The portable test consumer uses the installed SDK: - -```sh -cmake -S tests/videonsa_hygon -B /tmp/videonsa-consumer \ - -DCMAKE_PREFIX_PATH="$INFINI_ROOT" \ - -DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1" -cmake --build /tmp/videonsa-consumer -j 2 -INFINI_VIDEONSA_CONSUMER=/tmp/videonsa-consumer/libprefill_consumer.so \ - python tests/videonsa_hygon/test_frozen.py \ - --stage2-root /path/to/benchmark_test/stage2 \ - --output /path/to/personal-results/frozen.json -``` - -Run the shared benchmark with `tests/videonsa_hygon` on PYTHONPATH and -`--infini-adapter native_adapter --mode both`. Set -`INFINI_VIDEONSA_CONSUMER` to that consumer library and retain `INFINI_ROOT`. -Numerical references, oracle, payloads, SGLang and acceptance tolerances remain -external and unchanged. Outputs belong in a personal result directory. - -This is operator migration. It does not enable a model-default full NSA Prefill -path or establish whole-model performance. Actual model layer tests, frozen -subgraph replay and real-weight model runs must be reported separately. From 5df0b1784b932a301826bf5c8de5f9bca98d0936 Mon Sep 17 00:00:00 2001 From: 2506363862 <2506363862@qq.com> Date: Fri, 20 Nov 2026 16:49:01 +0800 Subject: [PATCH 13/18] refactor: move the shared VideoNSA validation header --- src/base/nsa_videonsa_prefill.h | 2 +- src/base/nsa_videonsa_selector.h | 2 +- src/{base => common/op_utils}/nsa_videonsa_common.h | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{base => common/op_utils}/nsa_videonsa_common.h (100%) diff --git a/src/base/nsa_videonsa_prefill.h b/src/base/nsa_videonsa_prefill.h index 8ec3faf53..b8c609bd3 100644 --- a/src/base/nsa_videonsa_prefill.h +++ b/src/base/nsa_videonsa_prefill.h @@ -1,5 +1,5 @@ #pragma once -#include "nsa_videonsa_common.h" +#include "common/op_utils/nsa_videonsa_common.h" namespace infini::ops { class NsaVideonsaPrefill:public Operator{ public: diff --git a/src/base/nsa_videonsa_selector.h b/src/base/nsa_videonsa_selector.h index b5a48db10..38b59af3e 100644 --- a/src/base/nsa_videonsa_selector.h +++ b/src/base/nsa_videonsa_selector.h @@ -1,5 +1,5 @@ #pragma once -#include "nsa_videonsa_common.h" +#include "common/op_utils/nsa_videonsa_common.h" namespace infini::ops { class NsaVideonsaSelector:public Operator{ public: diff --git a/src/base/nsa_videonsa_common.h b/src/common/op_utils/nsa_videonsa_common.h similarity index 100% rename from src/base/nsa_videonsa_common.h rename to src/common/op_utils/nsa_videonsa_common.h From eab14ff714f9f8222c17ba7e896e73b3183fb519 Mon Sep 17 00:00:00 2001 From: qin guoyong <305039273@qq.com> Date: Wed, 25 Nov 2026 00:14:41 +0800 Subject: [PATCH 14/18] perf(hygon): migrate validated routing and INT8 kernels Add wave64 Top-6 routing, fixed-width per-channel quantization and SDOT4 single-row INT8 GEMV paths with generic fallbacks. Add HYGON per-tensor quantization and dequantization with strided and graph support. Preserve the new repository rounding, scale and normalization rules. Validate on gfx936: routing 168, channel quantization 84, GEMV 72, per-tensor quantization 270 and dequantization 378 numerical checks; 130 graph replays, 9 invalid-input and 12 public API checks. Full model integration and final performance acceptance remain pending. --- .../hygon/ops/deepseek_v4_route/kernel.cuh | 172 +++++++++++++++++ .../cuda/hygon/ops/deepseek_v4_route/kernel.h | 155 ++++++--------- .../cuda/hygon/ops/i8_gemm/gemv_wave64.cuh | 79 ++++++++ src/native/cuda/hygon/ops/i8_gemm/kernel.h | 127 +++++++++--- .../hygon/ops/per_channel_quant_i8/kernel.h | 66 +++++-- .../per_channel_quant_i8/quantize_fixed.cuh | 70 +++++++ .../hygon/ops/per_tensor_dequant_i8/kernel.h | 56 ++++++ .../hygon/ops/per_tensor_quant_i8/kernel.h | 68 +++++++ src/native/cuda/hygon/per_tensor_i8.cuh | 181 ++++++++++++++++++ tests/native/hygon_migration/CMakeLists.txt | 15 ++ .../hygon_migration/check_hygon_migration.py | 136 +++++++++++++ .../native/hygon_migration/check_tensor_i8.py | 157 +++++++++++++++ .../native/hygon_migration/migration_probe.cu | 82 ++++++++ .../native/hygon_migration/tensor_i8_probe.cu | 55 ++++++ 14 files changed, 1274 insertions(+), 145 deletions(-) create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_route/kernel.cuh create mode 100644 src/native/cuda/hygon/ops/i8_gemm/gemv_wave64.cuh create mode 100644 src/native/cuda/hygon/ops/per_channel_quant_i8/quantize_fixed.cuh create mode 100644 src/native/cuda/hygon/ops/per_tensor_dequant_i8/kernel.h create mode 100644 src/native/cuda/hygon/ops/per_tensor_quant_i8/kernel.h create mode 100644 src/native/cuda/hygon/per_tensor_i8.cuh create mode 100644 tests/native/hygon_migration/CMakeLists.txt create mode 100644 tests/native/hygon_migration/check_hygon_migration.py create mode 100644 tests/native/hygon_migration/check_tensor_i8.py create mode 100644 tests/native/hygon_migration/migration_probe.cu create mode 100644 tests/native/hygon_migration/tensor_i8_probe.cu diff --git a/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.cuh b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.cuh new file mode 100644 index 000000000..da108f36d --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.cuh @@ -0,0 +1,172 @@ +#pragma once + +#include + +#include + +namespace infini::ops { +namespace v4_route { + +// Reproduce the Triton gate's stable log1p expansion and HCU math. +static __device__ float probability(float x) { + const float exponent = + __builtin_amdgcn_exp2f(-fabsf(x) * 1.4426950408889634f); + volatile float shifted = 1.0f + exponent; + const float log2 = __builtin_amdgcn_logf(shifted); + const float ln2_hi = __uint_as_float(0x3f317217); + const float ln2_lo = __uint_as_float(0x3377d1cf); + const float product = __fmul_rn(log2, ln2_hi); + float correction = fmaf(log2, ln2_hi, -product); + correction = fmaf(log2, ln2_lo, correction); + volatile float natural_log = fmaf(log2, ln2_hi, correction); + volatile float numerator = exponent * natural_log; + const float log1p = shifted == 1.0f ? exponent : numerator / (shifted - 1.0f); + return __builtin_amdgcn_sqrtf(fmaxf(x, 0.0f) + log1p); +} + +// HCU's hash gate uses stable sqrt(softplus), then a six-lane warp sum. +static __device__ float hash_probability(float x) { + return sqrtf(fmaxf(x, 0.f) + log1pf(expf(-fabsf(x)))); +} + +template +__device__ void RouteRow(const float* logits, const float* bias, + const Token* tokens, const int64_t* hash, float scale, + float* weights, int64_t* ids, size_t rows, + size_t experts, size_t k, size_t vocab, size_t row) { + const auto* x = logits + row * experts; + auto* selected = ids + row * k; + auto* w = weights + row * k; + const auto token = tokens ? static_cast(tokens[row]) : -1; + float norm = 0; + for (size_t i = 0; i < k; ++i) { + int64_t id = -1; + if (hash) { + if (token >= 0 && size_t(token) < vocab) id = hash[size_t(token) * k + i]; + } else { + float best = -INFINITY; + for (size_t j = 0; j < experts; ++j) { + const float rank = probability(x[j]) + (bias ? bias[j] : 0); + if (rank <= best) continue; + bool used = false; + for (size_t prev = 0; prev < i; ++prev) + used |= selected[prev] == int64_t(j); + if (!used) { + best = rank; + id = j; + } + } + } + const bool valid = id >= 0 && size_t(id) < experts; + selected[i] = valid ? id : -1; + w[i] = valid ? (hash ? hash_probability(x[id]) : probability(x[id])) : 0.f; + norm += w[i]; + } + if (k == 6) { + // The two reference kernels pack the six weights into lanes differently. + if (hash) { + norm = ((w[0] + w[4]) + w[2]) + ((w[1] + w[5]) + w[3]); + } else { + volatile float first = (((w[0] + w[1]) + w[2]) + w[3]); + volatile float second = w[4] + w[5]; + norm = first + second; + } + } + for (size_t i = 0; i < k; ++i) w[i] = norm > 0 ? (w[i] / norm) * scale : 0.f; +} +template +__global__ void route(const float* logits, const float* bias, + const Token* tokens, const int64_t* hash, float scale, + float* weights, int64_t* ids, size_t rows, size_t experts, + size_t k, size_t vocab) { + for (size_t row = blockIdx.x * blockDim.x + threadIdx.x; row < rows; + row += gridDim.x * blockDim.x) { + RouteRow(logits, bias, tokens, hash, scale, weights, ids, rows, experts, k, + vocab, row); + } +} + +__device__ __forceinline__ uint64_t RouteKey(float score, unsigned expert) { + // Comparisons treat both signed zeros as equal; preserve expert-id tie order. + const uint32_t bits = score == 0.0f ? 0u : __float_as_uint(score); + const uint32_t ordered = bits ^ ((bits >> 31) ? 0xffffffffu : 0x80000000u); + return (static_cast(ordered) << 32) | + static_cast(~expert); +} + +__device__ __forceinline__ void SortPair(uint64_t& a, uint64_t& b) { + const uint64_t hi = a > b ? a : b; + b = a > b ? b : a; + a = hi; +} + +static __global__ void RouteWave64(const float* logits, const float* bias, + float scale, float* weights, int64_t* ids, + size_t rows) { + constexpr int kWave = 64; + constexpr unsigned long long kMask = 0xffffffffffffffffull; + const int lane = threadIdx.x % kWave; + const size_t waves = blockDim.x / kWave; + for (size_t row = blockIdx.x * waves + threadIdx.x / kWave; row < rows; + row += gridDim.x * waves) { + uint64_t keys[4]; + bool unusual = false; +#pragma unroll + for (int i = 0; i < 4; ++i) { + const unsigned expert = lane + i * kWave; + const float score = probability(logits[row * 256 + expert]) + + (bias ? bias[expert] : 0.0f); + unusual |= !isfinite(score); + keys[i] = RouteKey(score, expert); + } + if (__any_sync(kMask, unusual)) { + if (lane == 0) { + RouteRow(logits, bias, nullptr, nullptr, scale, weights, ids, + rows, 256, 6, 0, row); + } + continue; + } + SortPair(keys[0], keys[1]); + SortPair(keys[2], keys[3]); + SortPair(keys[0], keys[2]); + SortPair(keys[1], keys[3]); + SortPair(keys[1], keys[2]); + int selected = -1; +#pragma unroll + for (int k = 0; k < 6; ++k) { + uint64_t best = keys[0]; +#pragma unroll + for (int offset = kWave / 2; offset; offset >>= 1) { + const uint64_t other = __shfl_xor_sync(kMask, best, offset, kWave); + best = best > other ? best : other; + } + const unsigned expert = ~static_cast(best); + if (lane == k) selected = static_cast(expert); + if (keys[0] == best) { + keys[0] = keys[1]; + keys[1] = keys[2]; + keys[2] = keys[3]; + keys[3] = 0; + } + } + const float value = + lane < 6 ? probability(logits[row * 256 + selected]) : 0.0f; + const float w0 = __shfl_sync(kMask, value, 0, kWave); + const float w1 = __shfl_sync(kMask, value, 1, kWave); + const float w2 = __shfl_sync(kMask, value, 2, kWave); + const float w3 = __shfl_sync(kMask, value, 3, kWave); + const float w4 = __shfl_sync(kMask, value, 4, kWave); + const float w5 = __shfl_sync(kMask, value, 5, kWave); + volatile float first = (((w0 + w1) + w2) + w3); + volatile float second = w4 + w5; + const float norm = first + second; + if (lane < 6) { + weights[row * 6 + lane] = norm > 0 ? (value / norm) * scale : 0.0f; + ids[row * 6 + lane] = selected; + } + } +} + +} // namespace v4_route + +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h index 7b8f32985..2f4abcc53 100644 --- a/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h +++ b/src/native/cuda/hygon/ops/deepseek_v4_route/kernel.h @@ -1,110 +1,71 @@ #pragma once #include + #include "base/deepseek_v4_route.h" +#include "kernel.cuh" namespace infini::ops { -namespace v4_route { - -// Reproduce the Triton gate's stable log1p expansion and HCU math. -static __device__ float probability(float x) { - const float exponent = __builtin_amdgcn_exp2f(-fabsf(x) * 1.4426950408889634f); - volatile float shifted = 1.0f + exponent; - const float log2 = __builtin_amdgcn_logf(shifted); - const float ln2_hi = __uint_as_float(0x3f317217); - const float ln2_lo = __uint_as_float(0x3377d1cf); - const float product = __fmul_rn(log2, ln2_hi); - float correction = fmaf(log2, ln2_hi, -product); - correction = fmaf(log2, ln2_lo, correction); - volatile float natural_log = fmaf(log2, ln2_hi, correction); - volatile float numerator = exponent * natural_log; - const float log1p = shifted == 1.0f ? exponent : numerator / (shifted - 1.0f); - return __builtin_amdgcn_sqrtf(fmaxf(x, 0.0f) + log1p); -} - -// HCU's hash gate uses stable sqrt(softplus), then a six-lane warp sum. -static __device__ float hash_probability(float x) { - return sqrtf(fmaxf(x,0.f)+log1pf(expf(-fabsf(x)))); -} - -template -__global__ void route(const float* logits,const float* bias,const Token* tokens, - const int64_t* hash,float scale,float* weights,int64_t* ids, - size_t rows,size_t experts,size_t k,size_t vocab) { - for(size_t row=blockIdx.x*blockDim.x+threadIdx.x;row(tokens[row]) : -1; - float norm=0; - for(size_t i=0;i=0 && size_t(token)=0 && size_t(id)0 ? (w[i]/norm)*scale : 0.f; - } -} -} // namespace v4_route -template<> -class Operator : public DeepseekV4Route { +template <> +class Operator : public DeepseekV4Route { public: - using Opt=std::optional; + using Opt = std::optional; using DeepseekV4Route::DeepseekV4Route; - bool capture_safe() const override {return true;} - static bool supports(Tensor x,Opt b,Opt t,Opt h,double s,Tensor w,Tensor i) { - if(x.ndim()!=2 || !x.size(0) || !x.size(1) || w.ndim()!=2 || w.size(0)!=x.size(0) || - !w.size(1) || w.size(1)>x.size(1) || w.shape()!=i.shape() || - x.dtype()!=DataType::kFloat32 || w.dtype()!=DataType::kFloat32 || i.dtype()!=DataType::kInt64 || - x.device().type()!=Device::Type::kHygon || !std::isfinite(s) || s<=0 || - bool(t)!=bool(h) || (t && b)) return false; - if(b && (b->shape()!=Tensor::Shape{x.size(1)} || b->dtype()!=DataType::kFloat32)) return false; - if(t && (t->shape()!=Tensor::Shape{x.size(0)} || - (t->dtype()!=DataType::kInt32 && t->dtype()!=DataType::kInt64) || - h->ndim()!=2 || !h->size(0) || h->size(1)!=w.size(1) || - h->dtype()!=DataType::kInt64)) return false; - for(const auto& a:{x,w,i}) if(a.device()!=x.device() || !a.IsContiguous()) return false; - for(const auto& a:{b,t,h}) if(a && (a->device()!=x.device() || !a->IsContiguous())) return false; + bool capture_safe() const override { return true; } + static bool supports(Tensor x, Opt b, Opt t, Opt h, double s, Tensor w, + Tensor i) { + if (x.ndim() != 2 || !x.size(0) || !x.size(1) || w.ndim() != 2 || + w.size(0) != x.size(0) || !w.size(1) || w.size(1) > x.size(1) || + w.shape() != i.shape() || x.dtype() != DataType::kFloat32 || + w.dtype() != DataType::kFloat32 || i.dtype() != DataType::kInt64 || + x.device().type() != Device::Type::kHygon || !std::isfinite(s) || + s <= 0 || bool(t) != bool(h) || (t && b)) + return false; + if (b && (b->shape() != Tensor::Shape{x.size(1)} || + b->dtype() != DataType::kFloat32)) + return false; + if (t && + (t->shape() != Tensor::Shape{x.size(0)} || + (t->dtype() != DataType::kInt32 && t->dtype() != DataType::kInt64) || + h->ndim() != 2 || !h->size(0) || h->size(1) != w.size(1) || + h->dtype() != DataType::kInt64)) + return false; + for (const auto& a : {x, w, i}) + if (a.device() != x.device() || !a.IsContiguous()) return false; + for (const auto& a : {b, t, h}) + if (a && (a->device() != x.device() || !a->IsContiguous())) return false; return true; } - void operator()(Tensor x,Opt b,Opt t,Opt h,double s,Tensor w,Tensor i) const override { - if(!supports(x,b,t,h,s,w,i)) throw std::invalid_argument("Invalid DeepSeek V4 routing inputs"); - auto launch=[&](auto tag) { - using T=decltype(tag); - v4_route::route<<(std::min((x.size(0)-1)/64+1,65535)),64,0, - static_cast(stream_)>>>( - static_cast(x.data()),b ? static_cast(b->data()) : nullptr, - t ? static_cast(t->data()) : nullptr,h ? static_cast(h->data()) : nullptr, - float(s),static_cast(w.data()),static_cast(i.data()), - x.size(0),x.size(1),w.size(1),h ? h->size(0) : 0); + void operator()(Tensor x, Opt b, Opt t, Opt h, double s, Tensor w, + Tensor i) const override { + if (!supports(x, b, t, h, s, w, i)) + throw std::invalid_argument("Invalid DeepSeek V4 routing inputs"); + auto launch = [&](auto tag) { + using T = decltype(tag); + v4_route::route<<(std::min( + (x.size(0) - 1) / 64 + 1, 65535)), + 64, 0, static_cast(stream_)>>>( + static_cast(x.data()), + b ? static_cast(b->data()) : nullptr, + t ? static_cast(t->data()) : nullptr, + h ? static_cast(h->data()) : nullptr, float(s), + static_cast(w.data()), static_cast(i.data()), + x.size(0), x.size(1), w.size(1), h ? h->size(0) : 0); }; - if(t && t->dtype()==DataType::kInt32) launch(int32_t{}); - else launch(int64_t{}); - if(cudaGetLastError()!=cudaSuccess) throw std::runtime_error("DeepSeek V4 routing launch failed"); + if (!h && x.size(1) == 256 && w.size(1) == 6) { + v4_route::RouteWave64<<(std::min( + (x.size(0) - 1) / 4 + 1, 65535)), + 256, 0, static_cast(stream_)>>>( + static_cast(x.data()), + b ? static_cast(b->data()) : nullptr, float(s), + static_cast(w.data()), static_cast(i.data()), + x.size(0)); + } else if (t && t->dtype() == DataType::kInt32) + launch(int32_t{}); + else + launch(int64_t{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("DeepSeek V4 routing launch failed"); } }; -} // namespace infini::ops +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/i8_gemm/gemv_wave64.cuh b/src/native/cuda/hygon/ops/i8_gemm/gemv_wave64.cuh new file mode 100644 index 000000000..68c92803f --- /dev/null +++ b/src/native/cuda/hygon/ops/i8_gemm/gemv_wave64.cuh @@ -0,0 +1,79 @@ +#pragma once + +#include + +#include + +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops::hygon_i8_gemm { + +template +__global__ void gemv_i8_hygon(Tdata *__restrict__ y, const Tdata *bias, + const int8_t *__restrict__ x, + const float *x_scale, + const int8_t *__restrict__ w, + const float *w_scale, int n, ptrdiff_t bias_col) { + const int lane = threadIdx.x & 63; + const int col = blockIdx.x * (blockDim.x / 64) + threadIdx.x / 64; + if (col >= n) return; + const int32_t *xp = reinterpret_cast(x); + const int32_t *wp = + reinterpret_cast(w + static_cast(col) * K); + int32_t sum = 0; +#pragma unroll + for (int step = 0; step < K / 256; ++step) { + const int i = lane + step * 64; + sum = __builtin_amdgcn_sdot4(__ldg(xp + i), __ldg(wp + i), sum, false); + } +#pragma unroll + for (int offset = 32; offset > 0; offset >>= 1) + sum += __shfl_down_sync(0xffffffffffffffffull, sum, offset, 64); + if (lane == 0) { + const float scale = __fmul_rn(x_scale[0], w_scale[col]); + float output = __fmul_rn(scale, static_cast(sum)); + if (bias != nullptr) + output += Caster::template Cast( + bias[col * bias_col]); + y[col] = Caster::template Cast(output); + } +} +// Aligned 16-byte loads reduce load instructions for the measured BW decode +// shape. +template +__global__ void gemv_vector_hygon(Tdata *__restrict__ y, const Tdata *bias, + const int8_t *__restrict__ x, + const float *x_scale, + const int8_t *__restrict__ w, + const float *w_scale, int n, + ptrdiff_t bias_col) { + const int lane = threadIdx.x & 63; + const int col = blockIdx.x * (blockDim.x / 64) + threadIdx.x / 64; + if (col >= n) return; + const int4 *xp = reinterpret_cast(x); + const int4 *wp = + reinterpret_cast(w + static_cast(col) * K); + int32_t sum = 0; +#pragma unroll + for (int step = 0; step < K / 1024; ++step) { + const int i = lane + step * 64; + const int4 a = __ldg(xp + i), b = __ldg(wp + i); + sum = __builtin_amdgcn_sdot4(a.x, b.x, sum, false); + sum = __builtin_amdgcn_sdot4(a.y, b.y, sum, false); + sum = __builtin_amdgcn_sdot4(a.z, b.z, sum, false); + sum = __builtin_amdgcn_sdot4(a.w, b.w, sum, false); + } +#pragma unroll + for (int offset = 32; offset > 0; offset >>= 1) + sum += __shfl_down_sync(0xffffffffffffffffull, sum, offset, 64); + if (lane == 0) { + const float scale = __fmul_rn(x_scale[0], w_scale[col]); + float output = __fmul_rn(scale, static_cast(sum)); + if (bias != nullptr) + output += Caster::template Cast( + bias[col * bias_col]); + y[col] = Caster::template Cast(output); + } +} + +} // namespace infini::ops::hygon_i8_gemm diff --git a/src/native/cuda/hygon/ops/i8_gemm/kernel.h b/src/native/cuda/hygon/ops/i8_gemm/kernel.h index 5cdccb661..e56a4ab61 100644 --- a/src/native/cuda/hygon/ops/i8_gemm/kernel.h +++ b/src/native/cuda/hygon/ops/i8_gemm/kernel.h @@ -1,25 +1,31 @@ #pragma once #include + +#include + #include "base/i8_gemm.h" +#include "gemv_wave64.cuh" #include "native/cuda/hygon/caster.cuh" namespace infini::ops { namespace hygon_i8_gemm { -template -__global__ void matmul(T* out, const int8_t* a, const float* as, const int8_t* b, - const float* bs, const T* bias, size_t m, size_t n, size_t k, - ptrdiff_t bias_row, ptrdiff_t bias_col) { +template +__global__ void matmul(T* out, const int8_t* a, const float* as, + const int8_t* b, const float* bs, const T* bias, + size_t m, size_t n, size_t k, ptrdiff_t bias_row, + ptrdiff_t bias_col) { __shared__ int32_t partial[128]; - for (size_t idx = blockIdx.x; idx < m*n; idx += gridDim.x) { - const size_t row = idx/n, col = idx%n; + for (size_t idx = blockIdx.x; idx < m * n; idx += gridDim.x) { + const size_t row = idx / n, col = idx % n; int32_t sum = 0; for (size_t j = threadIdx.x; j < k; j += blockDim.x) - sum += int32_t(a[row*k+j]) * b[col*k+j]; + sum += int32_t(a[row * k + j]) * b[col * k + j]; partial[threadIdx.x] = sum; __syncthreads(); for (unsigned step = 64; step; step /= 2) { - if (threadIdx.x < step) partial[threadIdx.x] += partial[threadIdx.x+step]; + if (threadIdx.x < step) + partial[threadIdx.x] += partial[threadIdx.x + step]; __syncthreads(); } if (threadIdx.x == 0) { @@ -27,14 +33,16 @@ __global__ void matmul(T* out, const int8_t* a, const float* as, const int8_t* b // The W8A8 epilogue rounds the joint scale before applying it to the dot. const float scale = __fmul_rn(as[row], bs[col]); float value = __fmul_rn(static_cast(partial[0]), scale); - if (bias) value += Convert::template Cast(bias[row*bias_row+col*bias_col]); + if (bias) + value += Convert::template Cast( + bias[row * bias_row + col * bias_col]); out[idx] = Convert::template Cast(value); } __syncthreads(); } } -} // namespace hygon_i8_gemm +} // namespace hygon_i8_gemm template <> class Operator : public I8Gemm { @@ -43,38 +51,95 @@ class Operator : public I8Gemm { bool capture_safe() const override { return true; } static bool supports(Tensor c, Tensor a, Tensor as, Tensor b, Tensor bs, const std::optional bias) { - if (a.ndim() != 2 || b.ndim() != 2 || c.ndim() != 2 || - a.size(1) == 0 || a.size(1) > INT32_MAX/(128*128) || b.size(0) != a.size(1) || + if (a.ndim() != 2 || b.ndim() != 2 || c.ndim() != 2 || a.size(1) == 0 || + a.size(1) > INT32_MAX / (128 * 128) || b.size(0) != a.size(1) || a.dtype() != DataType::kInt8 || b.dtype() != DataType::kInt8 || as.dtype() != DataType::kFloat32 || bs.dtype() != DataType::kFloat32 || - (c.dtype() != DataType::kFloat32 && c.dtype() != DataType::kBFloat16 && c.dtype() != DataType::kFloat16) || - c.shape() != Tensor::Shape{a.size(0),b.size(1)} || - as.shape() != Tensor::Shape{a.size(0),1} || bs.shape() != Tensor::Shape{b.size(1),1} || - !a.IsContiguous() || !c.IsContiguous() || !as.IsContiguous() || !bs.IsContiguous() || + (c.dtype() != DataType::kFloat32 && c.dtype() != DataType::kBFloat16 && + c.dtype() != DataType::kFloat16) || + c.shape() != Tensor::Shape{a.size(0), b.size(1)} || + as.shape() != Tensor::Shape{a.size(0), 1} || + bs.shape() != Tensor::Shape{b.size(1), 1} || !a.IsContiguous() || + !c.IsContiguous() || !as.IsContiguous() || !bs.IsContiguous() || b.stride(0) != 1 || b.stride(1) != static_cast(b.size(0)) || c.device().type() != Device::Type::kHygon || a.device() != c.device() || - b.device() != c.device() || as.device() != c.device() || bs.device() != c.device()) return false; - return !bias || (bias->device() == c.device() && bias->dtype() == c.dtype() && - bias->shape() == c.shape() && bias->stride(0) >= 0 && bias->stride(1) >= 0); + b.device() != c.device() || as.device() != c.device() || + bs.device() != c.device()) + return false; + return !bias || (bias->device() == c.device() && + bias->dtype() == c.dtype() && bias->shape() == c.shape() && + bias->stride(0) >= 0 && bias->stride(1) >= 0); } void operator()(Tensor c, Tensor a, Tensor as, Tensor b, Tensor bs, const std::optional bias) const override { - if (!supports(c,a,as,b,bs,bias)) throw std::invalid_argument("Unsupported Hygon INT8 GEMM"); + if (!supports(c, a, as, b, bs, bias)) + throw std::invalid_argument("Unsupported Hygon INT8 GEMM"); if (!c.numel()) return; auto launch = [&](auto tag) { using T = decltype(tag); - hygon_i8_gemm::matmul<<(std::min(c.numel(),65535)), - 128, 0, static_cast(stream_)>>>( - static_cast(c.data()), static_cast(a.data()), - static_cast(as.data()), static_cast(b.data()), - static_cast(bs.data()), bias ? static_cast(bias->data()) : nullptr, - c.size(0),c.size(1),a.size(1),bias ? bias->stride(0) : 0,bias ? bias->stride(1) : 0); + if (a.size(0) == 1 && (a.size(1) == 1024 || a.size(1) == 4096) && + c.size(1) <= INT32_MAX - 8 && + (reinterpret_cast(a.data()) & 3u) == 0 && + (reinterpret_cast(b.data()) & 3u) == 0) { + auto run = [&](auto width, auto vectorized) { + constexpr int k = decltype(width)::value; + const int n = static_cast(c.size(1)); + const int threads = + ((k == 1024 && n == 4096) || vectorized) ? 512 : 256; + const int blocks = (n + threads / 64 - 1) / (threads / 64); + if constexpr (decltype(vectorized)::value) { + hygon_i8_gemm::gemv_vector_hygon + <<(stream_)>>>( + static_cast(c.data()), + bias ? static_cast(bias->data()) : nullptr, + static_cast(a.data()), + static_cast(as.data()), + static_cast(b.data()), + static_cast(bs.data()), n, + bias ? bias->stride(1) : 0); + } else { + hygon_i8_gemm::gemv_i8_hygon + <<(stream_)>>>( + static_cast(c.data()), + bias ? static_cast(bias->data()) : nullptr, + static_cast(a.data()), + static_cast(as.data()), + static_cast(b.data()), + static_cast(bs.data()), n, + bias ? bias->stride(1) : 0); + } + }; + if (a.size(1) == 1024) { + run(std::integral_constant{}, std::false_type{}); + } else if (c.size(1) == 1280 && + (reinterpret_cast(a.data()) & 15u) == 0 && + (reinterpret_cast(b.data()) & 15u) == 0) { + run(std::integral_constant{}, std::true_type{}); + } else { + run(std::integral_constant{}, std::false_type{}); + } + return; + } + hygon_i8_gemm::matmul + <<(std::min(c.numel(), 65535)), 128, 0, + static_cast(stream_)>>>( + static_cast(c.data()), static_cast(a.data()), + static_cast(as.data()), + static_cast(b.data()), + static_cast(bs.data()), + bias ? static_cast(bias->data()) : nullptr, c.size(0), + c.size(1), a.size(1), bias ? bias->stride(0) : 0, + bias ? bias->stride(1) : 0); }; - if (c.dtype() == DataType::kFloat32) launch(float{}); - else if (c.dtype() == DataType::kFloat16) launch(__half{}); - else launch(__nv_bfloat16{}); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("Hygon INT8 GEMM launch failed"); + if (c.dtype() == DataType::kFloat32) + launch(float{}); + else if (c.dtype() == DataType::kFloat16) + launch(__half{}); + else + launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon INT8 GEMM launch failed"); } }; -} // namespace infini::ops +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h b/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h index 51c47984f..3e011819c 100644 --- a/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h +++ b/src/native/cuda/hygon/ops/per_channel_quant_i8/kernel.h @@ -1,39 +1,71 @@ #pragma once #include "base/per_channel_quant_i8.h" #include "native/cuda/hygon/dynamic_int8_quant.cuh" +#include "quantize_fixed.cuh" namespace infini::ops { template <> -class Operator : public PerChannelQuantI8 { +class Operator + : public PerChannelQuantI8 { public: using PerChannelQuantI8::PerChannelQuantI8; bool capture_safe() const override { return true; } static bool supports(Tensor x, Tensor packed, Tensor scale) { - return x.device().type() == Device::Type::kHygon && packed.device() == x.device() && - scale.device() == x.device() && x.ndim() == 2 && x.size(1) > 0 && - (x.dtype() == DataType::kBFloat16 || x.dtype() == DataType::kFloat32 || - x.dtype() == DataType::kFloat16) && packed.dtype() == DataType::kInt8 && + return x.device().type() == Device::Type::kHygon && + packed.device() == x.device() && scale.device() == x.device() && + x.ndim() == 2 && x.size(1) > 0 && + (x.dtype() == DataType::kBFloat16 || + x.dtype() == DataType::kFloat32 || + x.dtype() == DataType::kFloat16) && + packed.dtype() == DataType::kInt8 && scale.dtype() == DataType::kFloat32 && packed.shape() == x.shape() && - scale.shape() == Tensor::Shape{x.size(0), 1} && - x.IsContiguous() && packed.IsContiguous() && scale.IsContiguous(); + scale.shape() == Tensor::Shape{x.size(0), 1} && x.IsContiguous() && + packed.IsContiguous() && scale.IsContiguous(); } void operator()(Tensor x, Tensor packed, Tensor scale) const override { - if (!supports(x, packed, scale)) throw std::invalid_argument("Unsupported Hygon INT8 quantization"); + if (!supports(x, packed, scale)) + throw std::invalid_argument("Unsupported Hygon INT8 quantization"); if (!x.size(0)) return; using namespace hygon_dynamic_int8; auto launch = [&](auto tag) { using T = decltype(tag); - Quantize<<(std::min(x.size(0), 65535)), - kThreads, 0, static_cast(stream_)>>>( - static_cast(x.data()), static_cast(packed.data()), - static_cast(scale.data()), x.size(0), x.size(1)); + if constexpr (sizeof(T) == 2) { + const unsigned blocks = + static_cast(std::min(x.size(0), 65535)); + if (x.size(1) == 1024) { + QuantizeFixed + <<(stream_)>>>( + static_cast(x.data()), + static_cast(packed.data()), + static_cast(scale.data()), x.size(0)); + return; + } + if (x.size(1) == 4096) { + QuantizeFixed + <<(stream_)>>>( + static_cast(x.data()), + static_cast(packed.data()), + static_cast(scale.data()), x.size(0)); + return; + } + } + Quantize + <<(std::min(x.size(0), 65535)), + kThreads, 0, static_cast(stream_)>>>( + static_cast(x.data()), + static_cast(packed.data()), + static_cast(scale.data()), x.size(0), x.size(1)); }; - if (x.dtype() == DataType::kFloat32) launch(float{}); - else if (x.dtype() == DataType::kFloat16) launch(__half{}); - else launch(__nv_bfloat16{}); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("Hygon INT8 quantization launch failed"); + if (x.dtype() == DataType::kFloat32) + launch(float{}); + else if (x.dtype() == DataType::kFloat16) + launch(__half{}); + else + launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon INT8 quantization launch failed"); } }; -} // namespace infini::ops +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/per_channel_quant_i8/quantize_fixed.cuh b/src/native/cuda/hygon/ops/per_channel_quant_i8/quantize_fixed.cuh new file mode 100644 index 000000000..ca1e7d5a2 --- /dev/null +++ b/src/native/cuda/hygon/ops/per_channel_quant_i8/quantize_fixed.cuh @@ -0,0 +1,70 @@ +#pragma once + +#include + +#include "native/cuda/hygon/caster.cuh" + +namespace infini::ops::hygon_dynamic_int8 { + +// Keep each fixed-width row in registers while four wave64 groups reduce it. +template +__global__ void QuantizeFixed(const T* input, int8_t* output, float* scales, + size_t rows) { + constexpr int kWave = 64; + constexpr int kValues = Width / Threads; + const int tid = threadIdx.x; + const int lane = tid & (kWave - 1); + __shared__ float wave_maxima[Threads / kWave]; + for (size_t row = blockIdx.x; row < rows; row += gridDim.x) { + float values[kValues]; + float maximum = 0.0f; +#pragma unroll + for (int i = 0; i < kValues; ++i) { + values[i] = Caster::template Cast( + input[row * Width + tid + i * Threads]); + maximum = fmaxf(maximum, fabsf(values[i])); + } +#if defined(__gfx936__) + uint32_t a = __float_as_uint(maximum); + uint32_t b = __builtin_amdgcn_mov_dpp(a, 0xb1, 0xf, 0xf, false); + a = a > b ? a : b; + b = __builtin_amdgcn_mov_dpp(a, 0x4e, 0xf, 0xf, false); + a = a > b ? a : b; + b = __builtin_amdgcn_mov_dpp(a, 0x141, 0xf, 0xf, false); + a = a > b ? a : b; + b = __builtin_amdgcn_mov_dpp(a, 0x140, 0xf, 0xf, false); + a = a > b ? a : b; + b = __shfl_xor_sync(0xffffffffffffffffull, a, 16, kWave); + a = a > b ? a : b; + b = __shfl_xor_sync(0xffffffffffffffffull, a, 32, kWave); + maximum = __uint_as_float(a > b ? a : b); +#else +#pragma unroll + for (int offset = kWave / 2; offset; offset >>= 1) { + maximum = fmaxf(maximum, __shfl_xor_sync(0xffffffffffffffffull, maximum, + offset, kWave)); + } +#endif + if (lane == 0) wave_maxima[tid / kWave] = maximum; + __syncthreads(); +#pragma unroll + for (int i = 0; i < Threads / kWave; ++i) { + maximum = fmaxf(maximum, wave_maxima[i]); + } + // Preserve the new API's direct ratio, scale floor and ties-to-even rule. + const float absmax = fmaxf(maximum, 1.0e-10f); + const float scale = absmax / 127.0f; + const float inverse = 127.0f / absmax; + if (tid == 0) scales[row] = scale; +#pragma unroll + for (int i = 0; i < kValues; ++i) { + const int q = __float2int_rn(values[i] * inverse); + output[row * Width + tid + i * Threads] = + static_cast(min(max(q, -127), 127)); + } + // Blocks may process several rows when the grid is capped. + __syncthreads(); + } +} + +} // namespace infini::ops::hygon_dynamic_int8 diff --git a/src/native/cuda/hygon/ops/per_tensor_dequant_i8/kernel.h b/src/native/cuda/hygon/ops/per_tensor_dequant_i8/kernel.h new file mode 100644 index 000000000..be85aac22 --- /dev/null +++ b/src/native/cuda/hygon/ops/per_tensor_dequant_i8/kernel.h @@ -0,0 +1,56 @@ +#pragma once + +#include "base/per_tensor_dequant_i8.h" +#include "native/cuda/hygon/per_tensor_i8.cuh" + +namespace infini::ops { + +template <> +class Operator + : public PerTensorDequantI8 { + public: + using PerTensorDequantI8::PerTensorDequantI8; + bool capture_safe() const override { return true; } + + static bool supports(Tensor x, Tensor packed, Tensor scale, + std::optional zero) { + using namespace hygon_tensor_i8; + return x.device().type() == Device::Type::kHygon && IsFloat(x) && + packed.dtype() == DataType::kInt8 && packed.device() == x.device() && + packed.shape() == x.shape() && ScalarOnDevice(scale, x) && + (!zero || ScalarOnDevice(*zero, x)) && ValidLayout(x, true) && + ValidLayout(packed, false); + } + + void operator()(Tensor x, Tensor packed, Tensor scale, + std::optional zero) const override { + using namespace hygon_tensor_i8; + if (!supports(x, packed, scale, zero)) + throw std::invalid_argument( + "Unsupported Hygon per-tensor INT8 dequantization"); + const auto count = x.numel(); + if (!count) return; + const auto stream = static_cast(stream_); + const unsigned blocks = static_cast( + std::min((count - 1) / kThreads + 1, 65535)); + const auto in_layout = MakeLayout(packed), out_layout = MakeLayout(x); + auto launch = [&](auto tag) { + using T = decltype(tag); + Dequantize<<>>( + static_cast(x.data()), static_cast(packed.data()), + static_cast(scale.data()), + zero ? static_cast(zero->data()) : nullptr, count, + out_layout, in_layout); + }; + if (x.dtype() == DataType::kFloat32) + launch(float{}); + else if (x.dtype() == DataType::kFloat16) + launch(__half{}); + else + launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon per-tensor dequantization launch failed"); + } +}; + +} // namespace infini::ops diff --git a/src/native/cuda/hygon/ops/per_tensor_quant_i8/kernel.h b/src/native/cuda/hygon/ops/per_tensor_quant_i8/kernel.h new file mode 100644 index 000000000..9c11114fd --- /dev/null +++ b/src/native/cuda/hygon/ops/per_tensor_quant_i8/kernel.h @@ -0,0 +1,68 @@ +#pragma once + +#include "base/per_tensor_quant_i8.h" +#include "native/cuda/hygon/per_tensor_i8.cuh" + +namespace infini::ops { + +template <> +class Operator + : public PerTensorQuantI8 { + public: + using PerTensorQuantI8::PerTensorQuantI8; + bool capture_safe() const override { return true; } + + static bool supports(Tensor x, Tensor packed, Tensor scale, + std::optional zero, bool is_static) { + using namespace hygon_tensor_i8; + return !zero && x.device().type() == Device::Type::kHygon && IsFloat(x) && + packed.dtype() == DataType::kInt8 && packed.device() == x.device() && + packed.shape() == x.shape() && ScalarOnDevice(scale, x) && + ValidLayout(x, false) && ValidLayout(packed, true); + } + + void operator()(Tensor x, Tensor packed, Tensor scale, + std::optional zero, bool is_static) const override { + using namespace hygon_tensor_i8; + if (!supports(x, packed, scale, zero, is_static)) + throw std::invalid_argument( + "Unsupported Hygon per-tensor INT8 quantization"); + const auto stream = static_cast(stream_); + const auto count = x.numel(); + const auto in_layout = MakeLayout(x), out_layout = MakeLayout(packed); + auto launch = [&](auto tag) { + using T = decltype(tag); + const auto* input = static_cast(x.data()); + auto* output = static_cast(packed.data()); + auto* scales = static_cast(scale.data()); + if (!is_static && count <= 4096) { + DynamicSmall<<<1, kThreads, 0, stream>>>( + input, output, scales, count, in_layout, out_layout); + return; + } + const unsigned blocks = count ? static_cast(std::min( + (count - 1) / kThreads + 1, 65535)) + : 0; + if (!is_static) { + if (cudaMemsetAsync(scales, 0, sizeof(float), stream) != cudaSuccess) + throw std::runtime_error("Hygon quantization scale reset failed"); + AbsMaximum + <<>>(input, scales, count, in_layout); + FinalizeScale<<<1, 1, 0, stream>>>(scales); + } + if (count) + Quantize<<>>( + input, output, scales, count, in_layout, out_layout); + }; + if (x.dtype() == DataType::kFloat32) + launch(float{}); + else if (x.dtype() == DataType::kFloat16) + launch(__half{}); + else + launch(__nv_bfloat16{}); + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("Hygon per-tensor quantization launch failed"); + } +}; + +} // namespace infini::ops diff --git a/src/native/cuda/hygon/per_tensor_i8.cuh b/src/native/cuda/hygon/per_tensor_i8.cuh new file mode 100644 index 000000000..1dac482ee --- /dev/null +++ b/src/native/cuda/hygon/per_tensor_i8.cuh @@ -0,0 +1,181 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +#include "native/cuda/hygon/caster.cuh" +#include "tensor.h" + +namespace infini::ops::hygon_tensor_i8 { + +using Convert = Caster; +constexpr int kThreads = 256; +constexpr int kMaxRank = 8; + +struct Layout { + size_t shape[kMaxRank]{}; + ptrdiff_t strides[kMaxRank]{}; + int rank{}; + bool contiguous{}; + + __device__ size_t offset(size_t index) const { + if (contiguous) return index; + size_t result = 0; + for (int dim = rank - 1; dim >= 0; --dim) { + result += (index % shape[dim]) * strides[dim]; + index /= shape[dim]; + } + return result; + } +}; + +inline bool ValidLayout(Tensor tensor, bool output) { + if (tensor.ndim() > kMaxRank) return false; + std::vector> dimensions; + size_t span = 1; + for (size_t dim = 0; dim < tensor.ndim(); ++dim) { + if (tensor.stride(dim) < 0) return false; + if (tensor.size(dim) > 1) { + const size_t stride = tensor.stride(dim); + if (stride > (PTRDIFF_MAX - span) / (tensor.size(dim) - 1)) return false; + span += stride * (tensor.size(dim) - 1); + dimensions.emplace_back(tensor.stride(dim), tensor.size(dim)); + } + } + if (output && tensor.numel()) { + std::sort(dimensions.begin(), dimensions.end()); + size_t extent = 1; + for (auto [stride, length] : dimensions) { + if (static_cast(stride) < extent) return false; + extent += (length - 1) * stride; + } + } + return true; +} + +inline Layout MakeLayout(Tensor tensor) { + Layout layout; + layout.rank = static_cast(tensor.ndim()); + layout.contiguous = tensor.IsContiguous(); + for (int dim = 0; dim < layout.rank; ++dim) { + layout.shape[dim] = tensor.size(dim); + layout.strides[dim] = tensor.stride(dim); + } + return layout; +} + +inline bool IsFloat(Tensor tensor) { + return tensor.dtype() == DataType::kFloat32 || + tensor.dtype() == DataType::kFloat16 || + tensor.dtype() == DataType::kBFloat16; +} + +inline bool ScalarOnDevice(Tensor scalar, Tensor tensor) { + return scalar.dtype() == DataType::kFloat32 && scalar.numel() == 1 && + scalar.IsContiguous() && scalar.device() == tensor.device(); +} + +__device__ __forceinline__ float BlockMaximum(float value, float* scratch) { + const int lane = threadIdx.x % 64; + for (int offset = 32; offset; offset >>= 1) { + value = + fmaxf(value, __shfl_xor_sync(0xffffffffffffffffull, value, offset, 64)); + } + if (lane == 0) scratch[threadIdx.x / 64] = value; + __syncthreads(); + for (int wave = 0; wave < kThreads / 64; ++wave) + value = fmaxf(value, scratch[wave]); + return value; +} + +__device__ __forceinline__ int8_t Quantized(float value, float scale) { + const float inverse = 1.0f / scale; + const float normalized = + isfinite(inverse) ? value * inverse : __fdiv_rn(value, scale); + // Saturate before integer conversion, while retaining the new ties-to-even + // rule. + const float bounded = fminf(fmaxf(normalized, -127.0f), 127.0f); + return static_cast(__float2int_rn(bounded)); +} + +template +__global__ void DynamicSmall(const T* input, int8_t* output, float* scale, + size_t count, Layout in_layout, + Layout out_layout) { + __shared__ float scratch[kThreads / 64]; + float values[16]; + float maximum = 0.0f; + for (int i = 0; i < 16; ++i) { + const size_t index = threadIdx.x + i * kThreads; + values[i] = + index < count + ? Convert::template Cast(input[in_layout.offset(index)]) + : 0.0f; + maximum = fmaxf(maximum, fabsf(values[i])); + } + const float s = fmaxf(BlockMaximum(maximum, scratch), 1.0e-10f) / 127.0f; + if (threadIdx.x == 0) scale[0] = s; + for (int i = 0; i < 16; ++i) { + const size_t index = threadIdx.x + i * kThreads; + if (index < count) + output[out_layout.offset(index)] = Quantized(values[i], s); + } +} + +template +__global__ void AbsMaximum(const T* input, float* maximum, size_t count, + Layout layout) { + __shared__ float scratch[kThreads / 64]; + float value = 0.0f; + for (size_t i = blockIdx.x * size_t(blockDim.x) + threadIdx.x; i < count; + i += size_t(blockDim.x) * gridDim.x) { + value = fmaxf( + value, fabsf(Convert::template Cast(input[layout.offset(i)]))); + } + value = BlockMaximum(value, scratch); + if (threadIdx.x == 0) + atomicMax(reinterpret_cast(maximum), __float_as_uint(value)); +} + +static __global__ void FinalizeScale(float* scale) { + scale[0] = fmaxf(scale[0], 1.0e-10f) / 127.0f; +} + +template +__global__ void Quantize(const T* input, int8_t* output, const float* scale, + size_t count, Layout in_layout, Layout out_layout) { + const float s = scale[0]; + for (size_t i = blockIdx.x * size_t(blockDim.x) + threadIdx.x; i < count; + i += size_t(blockDim.x) * gridDim.x) { + output[out_layout.offset(i)] = + Quantized(Convert::template Cast(input[in_layout.offset(i)]), s); + } +} + +template +__global__ void Dequantize(T* output, const int8_t* input, const float* scale, + const float* zero, size_t count, Layout out_layout, + Layout in_layout) { + const float s = scale[0]; + for (size_t i = blockIdx.x * size_t(blockDim.x) + threadIdx.x; i < count; + i += size_t(blockDim.x) * gridDim.x) { + const float q = static_cast(input[in_layout.offset(i)]); + const float value = zero ? q - zero[0] : q; + float product; +#if defined(__gfx936__) || defined(__gfx938__) + // Preserve signed zero before converting to a reduced-precision output. + asm("v_mul_f32 %0, %1, %2" : "=v"(product) : "v"(value), "v"(s)); +#else + product = __fmul_rn(value, s); +#endif + output[out_layout.offset(i)] = Convert::template Cast(product); + } +} + +} // namespace infini::ops::hygon_tensor_i8 diff --git a/tests/native/hygon_migration/CMakeLists.txt b/tests/native/hygon_migration/CMakeLists.txt new file mode 100644 index 000000000..ebaabc7dd --- /dev/null +++ b/tests/native/hygon_migration/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.18) +project(HygonMigrationChecks LANGUAGES CXX CUDA) +find_package(CUDAToolkit REQUIRED) +add_library(migration_probe SHARED migration_probe.cu tensor_i8_probe.cu) +target_compile_features(migration_probe PRIVATE cxx_std_17 cuda_std_17) +target_include_directories(migration_probe PRIVATE ../../../src "$ENV{INFINI_ROOT}/include") +target_compile_definitions(migration_probe PRIVATE WITH_HYGON=1) +target_link_libraries(migration_probe PRIVATE CUDA::cudart) + +target_link_libraries(migration_probe PRIVATE "$ENV{INFINI_ROOT}/lib/libinfinirt.so" "$ENV{INFINI_ROOT}/lib/libinfiniops.so") + +if(NOT "$ENV{INFINILM_CXX11_ABI}" MATCHES "^[01]$") + message(FATAL_ERROR "Source the personal environment to match the installed SDK ABI") +endif() +target_compile_definitions(migration_probe PRIVATE _GLIBCXX_USE_CXX11_ABI=$ENV{INFINILM_CXX11_ABI}) diff --git a/tests/native/hygon_migration/check_hygon_migration.py b/tests/native/hygon_migration/check_hygon_migration.py new file mode 100644 index 000000000..ce14297dc --- /dev/null +++ b/tests/native/hygon_migration/check_hygon_migration.py @@ -0,0 +1,136 @@ +"""Compare migrated kernels against preserved kernels and integer references.""" + +import argparse +import ctypes +import json +from pathlib import Path + +import torch + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--library', required=True) + parser.add_argument('--output', required=True) + args = parser.parse_args() + lib = ctypes.CDLL(args.library) + ptr = ctypes.c_void_p + integer = ctypes.c_int + size = ctypes.c_size_t + lib.check_route.argtypes = [ptr, ptr, ptr, ptr, size, ctypes.c_float, integer, ptr] + lib.check_quant.argtypes = [ptr, ptr, ptr, size, integer, integer, integer, ptr] + lib.check_gemv.argtypes = [ptr] * 6 + [integer] * 4 + [ctypes.c_ssize_t, ptr] + for name in ['check_route', 'check_quant', 'check_gemv']: + getattr(lib, name).restype = integer + torch.manual_seed(20261124) + counts = {'route': 0, 'quant': 0, 'gemv': 0, 'graph_replays': 0} + def stream(): + return ptr(torch.cuda.current_stream().cuda_stream) + def addr(x): + return ptr(x.data_ptr()) if x is not None else None + def exact(a, b): + if a.is_floating_point(): + assert torch.equal(torch.isnan(a), torch.isnan(b)) + mask = ~torch.isnan(a) + if a.dtype == torch.float32: + assert torch.equal(a.view(torch.int32)[mask], b.view(torch.int32)[mask]) + else: + assert torch.equal(a.view(torch.int16)[mask], b.view(torch.int16)[mask]) + else: + assert torch.equal(a, b) + + for rows in [1, 3, 4, 5, 31, 64, 257]: + for pattern in ['random', 'ties', 'negative', 'nan', 'infinity', 'signed_zero']: + x = torch.randn((rows, 256), device='cuda') + bias = torch.randn(256, device='cuda') * 0.1 + if pattern == 'ties': + x.zero_(); bias.zero_() + elif pattern == 'negative': + x.fill_(-80); bias.zero_() + elif pattern == 'nan': + x[:, 3] = float('nan') + elif pattern == 'infinity': + x[:, 5] = float('inf'); bias[7] = -float('inf') + elif pattern == 'signed_zero': + x.fill_(-float('inf')); bias.zero_(); bias[::2] = -0.0 + for use_bias in [False, True]: + b = bias if use_bias else None + w = [torch.empty((rows, 6), device='cuda') for _ in range(2)] + ids = [torch.empty((rows, 6), device='cuda', dtype=torch.int64) for _ in range(2)] + for scale in [1.0, 2.5]: + for opt in [0, 1]: + assert lib.check_route(addr(x), addr(b), addr(w[opt]), addr(ids[opt]), rows, scale, opt, stream()) == 0 + exact(ids[0], ids[1]); exact(w[0], w[1]) + counts['route'] += 1 + if use_bias and pattern == 'random': + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + assert lib.check_route(addr(x), addr(b), addr(w[1]), addr(ids[1]), rows, 1.0, 1, stream()) == 0 + for replay in range(5): + x.normal_(); b.normal_() + if replay == 4: + x[:, 1] = float('nan') + graph.replay() + assert lib.check_route(addr(x), addr(b), addr(w[0]), addr(ids[0]), rows, 1.0, 0, stream()) == 0 + exact(ids[0], ids[1]); exact(w[0], w[1]) + counts['graph_replays'] += 1 + + for dtype in [torch.float16, torch.bfloat16]: + for width in [1024, 4096]: + for rows in [1, 3, 64, 257, 65536]: + storage = torch.empty(rows * width + 1, device='cuda', dtype=dtype) + x = storage[1:].view(rows, width) + x.normal_() + packed = [torch.empty_like(x, dtype=torch.int8) for _ in range(2)] + scales = [torch.empty(rows, device='cuda') for _ in range(2)] + patterns = ['random'] if rows > 257 else ['random', 'zero', 'tiny', 'ties', 'nonfinite'] + for pattern in patterns: + if pattern == 'zero': + x.zero_() + elif pattern == 'tiny': + x.fill_(1e-12) + elif pattern == 'ties': + x.fill_(127); x[:, 0] = 0.5; x[:, 1] = -0.5; x[:, 2] = 1.5 + elif pattern == 'nonfinite': + x.fill_(1); x[:, 0] = float('inf'); x[:, 1] = float('nan') + for opt in [0, 1]: + assert lib.check_quant(addr(x), addr(packed[opt]), addr(scales[opt]), rows, width, dtype == torch.bfloat16, opt, stream()) == 0 + exact(packed[0], packed[1]); exact(scales[0], scales[1]) + counts['quant'] += 1 + if rows == 3: + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + assert lib.check_quant(addr(x), addr(packed[1]), addr(scales[1]), rows, width, dtype == torch.bfloat16, 1, stream()) == 0 + for _ in range(5): + x.normal_(); graph.replay() + assert lib.check_quant(addr(x), addr(packed[0]), addr(scales[0]), rows, width, dtype == torch.bfloat16, 0, stream()) == 0 + exact(packed[0], packed[1]); exact(scales[0], scales[1]) + counts['graph_replays'] += 1 + + for dtype_id, dtype in enumerate([torch.float32, torch.float16, torch.bfloat16]): + for width, n in [(1024, 13), (1024, 4096), (4096, 13), (4096, 1280)]: + x = torch.randint(-128, 128, (width,), device='cuda', dtype=torch.int8) + w = torch.randint(-128, 128, (n, width), device='cuda', dtype=torch.int8) + xs = torch.tensor([0.015], device='cuda') + ws = torch.rand(n, device='cuda') * 0.01 + dots = (x.cpu().to(torch.int64) * w.cpu().to(torch.int64)).sum(1).float() + base = dots * (xs.cpu() * ws.cpu()) + out = torch.empty(n, device='cuda', dtype=dtype) + for bias_stride in [-1, 0, 1, 2]: + bias = None if bias_stride < 0 else torch.randn(max(n * bias_stride, 1), device='cuda', dtype=dtype) + expected = base.clone() + if bias is not None: + expected += bias.cpu().float()[torch.arange(n) * bias_stride] + vector_modes = [0, 1] if width == 4096 else [0] + for vector in vector_modes: + assert lib.check_gemv(addr(out), addr(bias), addr(x), addr(xs), addr(w), addr(ws), n, width, dtype_id, vector, max(bias_stride, 0), stream()) == 0 + exact(out.cpu(), expected.to(dtype)) + counts['gemv'] += 1 + torch.cuda.synchronize() + result = {'passed': True, 'checks': counts, 'library': str(Path(args.library).resolve())} + Path(args.output).write_text(json.dumps(result, indent=2)) + print(json.dumps(result)) + + +if __name__ == '__main__': + main() diff --git a/tests/native/hygon_migration/check_tensor_i8.py b/tests/native/hygon_migration/check_tensor_i8.py new file mode 100644 index 000000000..8038ef336 --- /dev/null +++ b/tests/native/hygon_migration/check_tensor_i8.py @@ -0,0 +1,157 @@ +"""Validate per-tensor INT8 providers, strided layouts and changing graph inputs.""" +import argparse +import ctypes +import json +from pathlib import Path + +import torch + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--library', required=True) + parser.add_argument('--output', required=True) + args = parser.parse_args() + lib = ctypes.CDLL(args.library) + ptr = ctypes.c_void_p + lib.check_tensor_i8.argtypes = [ptr] * 4 + [ctypes.c_int] * 2 + [ptr] * 3 + [ctypes.c_int] * 2 + [ptr] + lib.check_tensor_i8.restype = ctypes.c_int + torch.manual_seed(20261124) + counts = {'quant': 0, 'dequant': 0, 'graph_replays': 0, 'invalid': 0, 'public_api': 0} + + def call(x, q, scale, zero=None, dequant=False, static=False, api=False): + shape = (ctypes.c_size_t * x.ndim)(*x.shape) + xs = (ctypes.c_ssize_t * x.ndim)(*x.stride()) + qs = (ctypes.c_ssize_t * x.ndim)(*q.stride()) + dtype = [torch.float32, torch.float16, torch.bfloat16].index(x.dtype) + return lib.check_tensor_i8(x.data_ptr(), q.data_ptr(), scale.data_ptr(), + None if zero is None else zero.data_ptr(), dtype, + x.ndim, ctypes.cast(shape, ptr), ctypes.cast(xs, ptr), + ctypes.cast(qs, ptr), int(dequant) + 2 * api, static, + torch.cuda.current_stream().cuda_stream) + + def exact(actual, expected, case): + actual = actual.cpu().contiguous() + expected = expected.contiguous() + if actual.dtype == torch.int8: + assert torch.equal(actual, expected), case + return + assert torch.equal(torch.isnan(actual), torch.isnan(expected)), case + mask = ~torch.isnan(actual) + bits = torch.int32 if actual.dtype == torch.float32 else torch.int16 + assert torch.equal(actual.view(bits)[mask], expected.view(bits)[mask]), case + + def reference(x, static_scale=None): + x = x.cpu().float() + if static_scale is None: + absmax = torch.nan_to_num(x.abs(), nan=0., posinf=float('inf')).amax() if x.numel() else torch.tensor(0.) + scale = absmax.clamp_min(1.e-10) / 127. + else: + scale = static_scale.cpu().reshape(()) + inverse = 1. / scale + normalized = x * inverse if torch.isfinite(inverse) else x / scale + q = torch.nan_to_num(normalized, nan=-127., posinf=127., neginf=-127.).clamp(-127, 127).round().to(torch.int8) + return q, scale.reshape(1) + + for dtype in [torch.float32, torch.float16, torch.bfloat16]: + layouts = [torch.empty(n, device='cuda', dtype=dtype) for n in [0, 1, 63, 257, 4096, 4097, 16387]] + layouts += [torch.empty(9, 34, device='cuda', dtype=dtype)[:, ::2].T, + torch.empty(2, 3, 514, device='cuda', dtype=dtype)[:, :, ::2]] + for x in layouts: + q_store = torch.full((x.numel() * 2 + 1,), -111, dtype=torch.int8, device='cuda') + q = q_store[1::2].view(x.shape) + scale = torch.empty(1, device='cuda') + for pattern in ['random', 'zero', 'ties', 'tiny', 'nonfinite']: + if pattern == 'random': x.normal_() + elif pattern == 'zero': x.zero_() + elif pattern == 'ties': + x.fill_(127.) + if x.numel(): + values = torch.tensor([.5, -.5, 1.5, -1.5], device='cuda', dtype=dtype) + x.copy_(values.repeat((x.numel() + 3) // 4)[:x.numel()].reshape(x.shape)) + elif pattern == 'tiny': x.fill_(1.e-12) + else: + x.fill_(1.) + if x.numel(): + values = torch.tensor([float('inf'), float('nan'), -float('inf'), 0.], device='cuda', dtype=dtype) + x.copy_(values.repeat((x.numel() + 3) // 4)[:x.numel()].reshape(x.shape)) + for static in [False, True]: + scale.fill_(1. if pattern == 'ties' else .015) + expected, expected_scale = reference(x, scale if static else None) + case = (str(dtype), list(x.shape), pattern, static) + assert call(x, q, scale, static=static) == 0, case + exact(q, expected, case) + exact(scale, expected_scale, case) + assert torch.all(q_store[::2] == -111), ('padding', case) + counts['quant'] += 1 + for scalar in [0., -0., .015, -2., 1.e-8, float('inf'), float('nan')]: + q.random_(-128, 128) + scale.fill_(scalar) + for with_zero in [False, True]: + zero = torch.tensor([3.5], device='cuda') if with_zero else None + qf = q.cpu().float() + expected = ((qf - zero.cpu()[0] if zero is not None else qf) * scale.cpu()[0]).to(dtype) + assert call(x, q, scale, zero, dequant=True) == 0 + exact(x, expected, ('dequant', str(dtype), list(x.shape), scalar, with_zero)) + counts['dequant'] += 1 + + for count in [4096, 4097]: + x = torch.empty(count, device='cuda', dtype=dtype) + q = torch.empty_like(x, dtype=torch.int8) + scale = torch.ones(1, device='cuda') + for static in [False, True]: + x.normal_() + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + assert call(x, q, scale, static=static) == 0 + for magnitude in [100., .01, 0., 2., .001]: + x.normal_().mul_(magnitude) + scale.fill_(.02 if static else 999.) + expected, expected_scale = reference(x, scale if static else None) + graph.replay() + exact(q, expected, ('graph', count, static, magnitude)) + exact(scale, expected_scale, ('graph-scale', count, static, magnitude)) + counts['graph_replays'] += 1 + + zero = torch.zeros(1, device='cuda') + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + assert call(x, q, scale, zero, dequant=True) == 0 + for value in [0., .01, -.5, 2., 1.e-8]: + q.random_(-128, 128) + scale.fill_(value) + zero.fill_(value * 3.) + expected = ((q.cpu().float() - zero.cpu()[0]) * scale.cpu()[0]).to(dtype) + graph.replay() + exact(x, expected, ('dequant-graph', str(dtype), value)) + counts['graph_replays'] += 1 + + x = torch.randn(9, 34, device='cuda', dtype=dtype)[:, ::2].T + q = torch.empty_like(x, dtype=torch.int8) + scale = torch.ones(1, device='cuda') + for static in [False, True]: + expected, expected_scale = reference(x, scale if static else None) + assert call(x, q, scale, static=static, api=True) == 0 + exact(q, expected, ('public-quant', str(dtype), static)) + exact(scale, expected_scale, ('public-scale', str(dtype), static)) + restored = torch.empty_like(x) + assert call(restored, q, scale, dequant=True, api=True) == 0 + exact(restored, (q.cpu().float() * scale.cpu()[0]).to(dtype), ('public-dequant', str(dtype), static)) + counts['public_api'] += 2 + + x = torch.ones(8, device='cuda', dtype=dtype) + q = torch.empty(8, device='cuda', dtype=torch.int8) + scale = torch.ones(1, device='cuda') + zero = torch.zeros_like(scale) + assert call(x, q, scale, zero) == 1 + assert call(x, q[:1].expand(8), scale) == 1 + assert call(x[:1].expand(8), q, scale, dequant=True) == 1 + counts['invalid'] += 3 + torch.cuda.synchronize() + result = {'passed': True, 'checks': counts} + Path(args.output).write_text(json.dumps(result, indent=2)) + print(json.dumps(result)) + + +if __name__ == '__main__': + main() diff --git a/tests/native/hygon_migration/migration_probe.cu b/tests/native/hygon_migration/migration_probe.cu new file mode 100644 index 000000000..900ffe6dc --- /dev/null +++ b/tests/native/hygon_migration/migration_probe.cu @@ -0,0 +1,82 @@ +#include "native/cuda/hygon/dynamic_int8_quant.cuh" +#include "native/cuda/hygon/ops/deepseek_v4_route/kernel.cuh" +#include "native/cuda/hygon/ops/i8_gemm/gemv_wave64.cuh" +#include "native/cuda/hygon/ops/per_channel_quant_i8/quantize_fixed.cuh" + +extern "C" int check_route(const float* x, const float* bias, float* w, + int64_t* ids, size_t rows, float scale, + int optimized, void* stream) { + using namespace infini::ops::v4_route; + const auto s = static_cast(stream); + if (optimized) { + RouteWave64<<<(rows + 3) / 4, 256, 0, s>>>(x, bias, scale, w, ids, rows); + } else { + route<<<(rows + 63) / 64, 64, 0, s>>>( + x, bias, nullptr, nullptr, scale, w, ids, rows, 256, 6, 0); + } + return static_cast(cudaGetLastError()); +} + +template +void quant(const void* x, int8_t* packed, float* scales, size_t rows, int width, + int optimized, cudaStream_t stream) { + using namespace infini::ops::hygon_dynamic_int8; + const unsigned blocks = rows > 65535 ? 65535 : rows; + if (optimized && width == 1024) { + QuantizeFixed<<>>(static_cast(x), + packed, scales, rows); + } else if (optimized && width == 4096) { + QuantizeFixed<<>>(static_cast(x), + packed, scales, rows); + } else { + Quantize<<>>( + static_cast(x), packed, scales, rows, width); + } +} + +extern "C" int check_quant(const void* x, int8_t* packed, float* scales, + size_t rows, int width, int bf16, int optimized, + void* stream) { + if (bf16) + quant<__nv_bfloat16>(x, packed, scales, rows, width, optimized, + static_cast(stream)); + else + quant<__half>(x, packed, scales, rows, width, optimized, + static_cast(stream)); + return static_cast(cudaGetLastError()); +} + +template +void gemv(void* out, const void* bias, const int8_t* x, const float* xs, + const int8_t* w, const float* ws, int n, int width, int vectorized, + ptrdiff_t bias_col, cudaStream_t stream) { + using namespace infini::ops::hygon_i8_gemm; + if (width == 1024) { + gemv_i8_hygon<1024, T><<<(n + 7) / 8, 512, 0, stream>>>( + static_cast(out), static_cast(bias), x, xs, w, ws, n, + bias_col); + } else if (vectorized) { + gemv_vector_hygon<4096, T><<<(n + 7) / 8, 512, 0, stream>>>( + static_cast(out), static_cast(bias), x, xs, w, ws, n, + bias_col); + } else { + gemv_i8_hygon<4096, T><<<(n + 3) / 4, 256, 0, stream>>>( + static_cast(out), static_cast(bias), x, xs, w, ws, n, + bias_col); + } +} + +extern "C" int check_gemv(void* out, const void* bias, const int8_t* x, + const float* xs, const int8_t* w, const float* ws, + int n, int width, int dtype, int vectorized, + ptrdiff_t bias_col, void* stream) { + auto s = static_cast(stream); + if (dtype == 0) + gemv(out, bias, x, xs, w, ws, n, width, vectorized, bias_col, s); + else if (dtype == 1) + gemv<__half>(out, bias, x, xs, w, ws, n, width, vectorized, bias_col, s); + else + gemv<__nv_bfloat16>(out, bias, x, xs, w, ws, n, width, vectorized, bias_col, + s); + return static_cast(cudaGetLastError()); +} diff --git a/tests/native/hygon_migration/tensor_i8_probe.cu b/tests/native/hygon_migration/tensor_i8_probe.cu new file mode 100644 index 000000000..ffdc3db07 --- /dev/null +++ b/tests/native/hygon_migration/tensor_i8_probe.cu @@ -0,0 +1,55 @@ +#include + +#include "native/cuda/hygon/ops/per_tensor_dequant_i8/kernel.h" +#include "native/cuda/hygon/ops/per_tensor_quant_i8/kernel.h" + +extern "C" int check_tensor_i8(void* floating, void* packed, float* scale, + float* zero, int dtype, int rank, + const size_t* shape, + const ptrdiff_t* float_strides, + const ptrdiff_t* packed_strides, int dequantize, + int is_static, void* stream) { + using namespace infini::ops; + try { + const Tensor::Shape dims(shape, shape + rank); + const Tensor::Strides fs(float_strides, float_strides + rank); + const Tensor::Strides ps(packed_strides, packed_strides + rank); + const Device device{Device::Type::kHygon, 0}; + const DataType type = dtype == 0 ? DataType::kFloat32 + : dtype == 1 ? DataType::kFloat16 + : DataType::kBFloat16; + const Tensor x(floating, dims, type, device, fs); + const Tensor q(packed, dims, DataType::kInt8, device, ps); + const Tensor s(scale, Tensor::Shape{1}, DataType::kFloat32, device); + const auto z = + zero ? std::optional{Tensor(zero, Tensor::Shape{1}, + DataType::kFloat32, device)} + : std::nullopt; + if (dequantize >= 2) { + Handle handle; + handle.set_stream(stream); + if (dequantize & 1) { + const auto config = PerTensorDequantI8::DefaultConfig(device.type()); + PerTensorDequantI8::Call(handle, config, x, q, s, z); + } else { + const auto config = PerTensorQuantI8::DefaultConfig(device.type()); + const bool fixed = is_static; + PerTensorQuantI8::Call(handle, config, x, q, s, z, fixed); + } + } else if (dequantize) { + Operator op(x, q, s, z); + op.set_stream(stream); + op(x, q, s, z); + } else { + Operator op(x, q, s, z, + bool(is_static)); + op.set_stream(stream); + op(x, q, s, z, bool(is_static)); + } + return 0; + } catch (const std::invalid_argument&) { + return 1; + } catch (...) { + return 2; + } +} From 974f94f9dc6e76b33b811a3d3281c8a146b19f67 Mon Sep 17 00:00:00 2001 From: qin guoyong <305039273@qq.com> Date: Wed, 25 Nov 2026 03:25:50 +0800 Subject: [PATCH 15/18] fix(ops): preserve scalar metadata and reject unsafe CPU casts Populate scalar Mul shape and strides so the Torch backend writes every element. Validate CPU Cast storage spans and aliases before writes, including cached calls, and use each operand's rank for strided indexing. Add standalone CPU/Hygon public API regressions. Verified 24 CPU scalar, 48 combined CPU/Hygon scalar, and 9 CPU Cast checks. The actual LM W8A8 layer passes 16 eager cases and 48 graph replays after the scalar fix. Performance acceptance remains pending. --- src/base/mul.h | 15 ++- src/native/cpu/ops/cast/cast.h | 24 ++-- src/native/cpu/ops/cast/validation.h | 66 +++++++++++ tests/native/runtime_contracts/CMakeLists.txt | 21 ++++ tests/native/runtime_contracts/README.md | 15 +++ .../runtime_contracts/cpu_cast_regression.cpp | 70 ++++++++++++ .../scalar_mul_regression.cpp | 106 ++++++++++++++++++ 7 files changed, 307 insertions(+), 10 deletions(-) create mode 100644 src/native/cpu/ops/cast/validation.h create mode 100644 tests/native/runtime_contracts/CMakeLists.txt create mode 100644 tests/native/runtime_contracts/README.md create mode 100644 tests/native/runtime_contracts/cpu_cast_regression.cpp create mode 100644 tests/native/runtime_contracts/scalar_mul_regression.cpp diff --git a/src/base/mul.h b/src/base/mul.h index 52f3b5e94..5361d1848 100644 --- a/src/base/mul.h +++ b/src/base/mul.h @@ -37,11 +37,22 @@ class Mul : public Operator { } Mul(const Tensor input, const double other, Tensor out) - : output_size_(out.numel()), + : ndim_(out.ndim()), + output_size_(out.numel()), input_type_(input.dtype()), other_type_(input.dtype()), out_type_(out.dtype()), - scalar_mode_(true) {} + input_shape_(input.shape()), + out_shape_(out.shape()), + input_strides_(input.strides()), + out_strides_(out.strides()), + is_input_contiguous_(input.IsContiguous()), + is_out_contiguous_(out.IsContiguous()), + scalar_mode_(true) { + if (input.shape() != out.shape() || input.dtype() != out.dtype()) + throw std::invalid_argument( + "scalar Mul requires matching input/output shape and dtype"); + } virtual void operator()(const Tensor input, const Tensor other, Tensor out) const = 0; diff --git a/src/native/cpu/ops/cast/cast.h b/src/native/cpu/ops/cast/cast.h index 6e570d664..50d24506f 100644 --- a/src/native/cpu/ops/cast/cast.h +++ b/src/native/cpu/ops/cast/cast.h @@ -4,15 +4,23 @@ #include "base/cast.h" #include "common/generic_utils.h" #include "native/cpu/caster_.h" +#include "native/cpu/ops/cast/validation.h" namespace infini::ops { template <> class Operator : public Cast { public: - Operator(const Tensor input, Tensor out) : Cast{input, out} {} + Operator(const Tensor input, Tensor out) + : Cast{cpu_cast_detail::Checked(input, out), out} {} void operator()(const Tensor input, Tensor out) const override { + cpu_cast_detail::Validate(input, out); + if (input.shape() != input_shape_ || out.shape() != out_shape_ || + input.strides() != input_strides_ || out.strides() != out_strides_ || + input.dtype() != input_dtype_ || out.dtype() != out_dtype_) + throw std::invalid_argument("CPU Cast metadata changed after Make"); + if (!output_size_ || input.data() == out.data()) return; DispatchFunc( {input_dtype_, out_dtype_}, [&](auto in_tag, auto out_tag) { @@ -29,17 +37,17 @@ class Operator : public Cast { const auto* in_ptr = static_cast(input.data()); auto* out_ptr = static_cast(out.data()); - auto get_idx = [&](Tensor::Size i, bool is_contig, const auto* shape, - const auto* strides) { - return is_contig ? i : utils::IndexToOffset(i, ndim_, shape, strides); + auto get_idx = [&](Tensor::Size i, bool is_contig, Tensor::Size rank, + const auto* shape, const auto* strides) { + return is_contig ? i : utils::IndexToOffset(i, rank, shape, strides); }; #pragma omp parallel for for (Tensor::Size i = 0; i < output_size_; ++i) { - auto in_idx = get_idx(i, is_input_contiguous_, input_shape_.data(), - input_strides_.data()); - auto out_idx = get_idx(i, is_out_contiguous_, out_shape_.data(), - out_strides_.data()); + auto in_idx = get_idx(i, is_input_contiguous_, input.ndim(), + input_shape_.data(), input_strides_.data()); + auto out_idx = get_idx(i, is_out_contiguous_, out.ndim(), + out_shape_.data(), out_strides_.data()); out_ptr[out_idx] = Caster::template Cast(in_ptr[in_idx]); diff --git a/src/native/cpu/ops/cast/validation.h b/src/native/cpu/ops/cast/validation.h new file mode 100644 index 000000000..f2ee5739d --- /dev/null +++ b/src/native/cpu/ops/cast/validation.h @@ -0,0 +1,66 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#include "tensor.h" + +namespace infini::ops::cpu_cast_detail { +inline std::size_t Span(const Tensor value) { + if (value.shape().size() != value.strides().size()) + throw std::invalid_argument("CPU Cast rank/stride mismatch"); + if (!value.numel()) return 0; + const auto limit = std::numeric_limits::max(); + std::size_t last = 0; + for (std::size_t i = 0; i < value.ndim(); ++i) { + if (value.stride(i) < 0) + throw std::invalid_argument("CPU Cast negative stride"); + const auto extent = value.size(i) - 1; + const auto stride = static_cast(value.stride(i)); + if (extent && stride > (limit - last) / extent) + throw std::invalid_argument("CPU Cast span overflow"); + last += extent * stride; + } + const auto width = value.element_size(); + if (!width || last == limit || last + 1 > limit / width) + throw std::invalid_argument("CPU Cast byte span overflow"); + const auto bytes = (last + 1) * width; + const auto pointer = reinterpret_cast(value.data()); + if (!pointer || pointer % width || + bytes > std::numeric_limits::max() - pointer) + throw std::invalid_argument("CPU Cast invalid storage"); + return bytes; +} +inline void Validate(const Tensor input, const Tensor out) { + if (input.device().type() != Device::Type::kCpu || + input.device() != out.device() || input.numel() != out.numel()) + throw std::invalid_argument( + "CPU Cast requires matching CPU element counts"); + const auto ib = Span(input), ob = Span(out); + if (!ib || !ob) return; + std::vector> dimensions; + for (std::size_t i = 0; i < out.ndim(); ++i) + if (out.size(i) > 1) dimensions.emplace_back(out.stride(i), out.size(i)); + std::sort(dimensions.begin(), dimensions.end()); + std::size_t span = 1; + for (const auto& [stride, size] : dimensions) { + if (stride < span) + throw std::invalid_argument("CPU Cast output elements overlap"); + span += (size - 1) * stride; // Span already checked the complete sum. + } + const auto ip = reinterpret_cast(input.data()); + const auto op = reinterpret_cast(out.data()); + const bool exact = ip == op && input.dtype() == out.dtype() && + input.shape() == out.shape() && + input.strides() == out.strides(); + if (!exact && (ip <= op ? op - ip < ib : ip - op < ob)) + throw std::invalid_argument("CPU Cast input and output storage overlap"); +} +inline Tensor Checked(const Tensor input, const Tensor out) { + Validate(input, out); + return input; +} +} // namespace infini::ops::cpu_cast_detail diff --git a/tests/native/runtime_contracts/CMakeLists.txt b/tests/native/runtime_contracts/CMakeLists.txt new file mode 100644 index 000000000..cfff8f6b3 --- /dev/null +++ b/tests/native/runtime_contracts/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.18) +project(InfiniOpsRuntimeContracts LANGUAGES CXX) +option(WITH_HYGON "Include Hygon scalar Mul checks" OFF) +if(NOT "$ENV{INFINILM_CXX11_ABI}" MATCHES "^[01]$") + message(FATAL_ERROR "Set INFINILM_CXX11_ABI to match the installed SDK") +endif() +foreach(name scalar_mul_regression cpu_cast_regression) + add_executable(${name} ${name}.cpp) + target_compile_features(${name} PRIVATE cxx_std_17) + target_include_directories(${name} PRIVATE "$ENV{INFINI_ROOT}/include") + target_compile_definitions(${name} PRIVATE _GLIBCXX_USE_CXX11_ABI=$ENV{INFINILM_CXX11_ABI}) + target_link_libraries(${name} PRIVATE "$ENV{INFINI_ROOT}/lib/libinfinirt.so" "$ENV{INFINI_ROOT}/lib/libinfiniops.so") +endforeach() +if(WITH_HYGON) + find_package(CUDAToolkit REQUIRED) + target_compile_definitions(scalar_mul_regression PRIVATE WITH_HYGON=1) + target_link_libraries(scalar_mul_regression PRIVATE CUDA::cudart) +endif() +enable_testing() +add_test(NAME scalar_mul_regression COMMAND scalar_mul_regression) +add_test(NAME cpu_cast_regression COMMAND cpu_cast_regression) diff --git a/tests/native/runtime_contracts/README.md b/tests/native/runtime_contracts/README.md new file mode 100644 index 000000000..818c9649e --- /dev/null +++ b/tests/native/runtime_contracts/README.md @@ -0,0 +1,15 @@ +# Scalar multiplication and CPU cast regression checks + +Source the personal SDK environment (`INFINI_ROOT`, `INFINILM_CXX11_ABI`), then: + +```sh +cmake -S tests/native/runtime_contracts -B build/runtime-contracts -DCMAKE_BUILD_TYPE=Release +cmake --build build/runtime-contracts -j4 +ctest --test-dir build/runtime-contracts --output-on-failure +``` + +Add `-DWITH_HYGON=ON -DCUDAToolkit_ROOT=/opt/dtk/cuda/cuda` for Hygon checks. +CPU-only builds do not require a GPU toolkit or GPU allocation. These tests +exercise the installed public Ops API, cached scalar calls, strided output +canaries, exact/partial storage alias rejection and rank-changing CPU Cast. +They are correctness regressions, not performance acceptance benchmarks. diff --git a/tests/native/runtime_contracts/cpu_cast_regression.cpp b/tests/native/runtime_contracts/cpu_cast_regression.cpp new file mode 100644 index 000000000..deb2d59c7 --- /dev/null +++ b/tests/native/runtime_contracts/cpu_cast_regression.cpp @@ -0,0 +1,70 @@ +#include + +#include +#include +#include +#include +using namespace infini::ops; +int main() { + size_t checks = 0; + try { + const Device cpu{Device::Type::kCpu, 0}; + Config config; + config.set_implementation_index(0); + Handle handle; + std::array input{}, output{}; + for (size_t i = 0; i < input.size(); ++i) input[i] = float(i) - 3; + const auto original = input; + auto call = [&](const Tensor& x, const Tensor& y) { + Cast::Call(handle, config, x, y); + }; + Tensor x(input.data(), {2, 3}, DataType::kFloat32, cpu, {5, 1}); + Tensor y(output.data(), {6}, DataType::kFloat32, cpu, {2}); + output.fill(-999); + call(x, y); + for (size_t i = 0; i < output.size(); ++i) { + float expected = + i < 12 && i % 2 == 0 ? input[(i / 2) / 3 * 5 + (i / 2) % 3] : -999; + if (output[i] != expected) + throw std::runtime_error("strided rank-changing Cast mismatch"); + } + ++checks; + call(x, x); + if (input != original) + throw std::runtime_error("in-place identity changed input"); + ++checks; + auto rejected = [&](const Tensor& src, const Tensor& dst) { + const auto before = input, before_out = output; + bool caught = false; + try { + call(src, dst); + } catch (const std::invalid_argument&) { + caught = true; + } + if (!caught || std::memcmp(input.data(), before.data(), sizeof(input)) || + std::memcmp(output.data(), before_out.data(), sizeof(output))) + throw std::runtime_error( + "invalid Cast was not rejected before writing"); + ++checks; + }; + Tensor flat(input.data(), {4}, DataType::kFloat32, cpu); + rejected(flat, Tensor(input.data(), {4}, DataType::kFloat16, cpu)); + rejected(flat, Tensor(input.data() + 1, {4}, DataType::kFloat16, cpu)); + rejected(flat, Tensor(input.data() + 1, {4}, DataType::kFloat32, cpu)); + rejected(flat, Tensor(output.data(), {4}, DataType::kFloat32, cpu, {0})); + rejected(flat, Tensor(output.data(), {3}, DataType::kFloat32, cpu)); + std::array half{}; + call(flat, Tensor(half.data(), {4}, DataType::kFloat16, cpu)); + if (half != std::array{0xc200, 0xc000, 0xbc00, 0}) + throw std::runtime_error("FP16 rounding changed"); + ++checks; + Tensor empty_in(nullptr, {0, 4}, DataType::kFloat32, cpu), + empty_out(nullptr, {0}, DataType::kFloat16, cpu); + call(empty_in, empty_out); + ++checks; + std::cout << "{\"passed\":true,\"checks\":" << checks << "}\n"; + } catch (const std::exception&) { + std::cout << "{\"passed\":false,\"checks\":" << checks << "}\n"; + return 1; + } +} diff --git a/tests/native/runtime_contracts/scalar_mul_regression.cpp b/tests/native/runtime_contracts/scalar_mul_regression.cpp new file mode 100644 index 000000000..13a93876e --- /dev/null +++ b/tests/native/runtime_contracts/scalar_mul_regression.cpp @@ -0,0 +1,106 @@ +#include +#include +#ifdef WITH_HYGON +#include +#endif +#include +#include +#include +#include + +using namespace infini::ops; +#ifdef WITH_HYGON +static void checked(cudaError_t error) { + if (error != cudaSuccess) throw std::runtime_error("GPU operation failed"); +} +struct Buffer { + void* pointer = nullptr; + explicit Buffer(size_t bytes) { + if (bytes) checked(cudaMalloc(&pointer, bytes)); + } + ~Buffer() { + if (pointer) cudaFree(pointer); + } +}; +#endif +int main() { + size_t checks = 0; + try { + std::vector devices{Device::Type::kCpu}; +#ifdef WITH_HYGON + devices.push_back(Device::Type::kHygon); +#endif + for (auto type : devices) { + Device device{type, 0}; + infini::rt::DeviceGuard guard{type, 0, true}; + for (int layout = 0; layout < 4; ++layout) { + Tensor::Shape shape = layout == 0 ? Tensor::Shape{17, 1} + : layout == 1 ? Tensor::Shape{2, 3} + : layout == 2 ? Tensor::Shape{} + : Tensor::Shape{0, 4}; + Tensor::Strides xs = layout == 0 ? Tensor::Strides{1, 1} + : layout == 1 ? Tensor::Strides{5, 1} + : layout == 2 ? Tensor::Strides{} + : Tensor::Strides{4, 1}; + Tensor::Strides ys = layout == 1 ? Tensor::Strides{1, 2} : xs; + for (int seed = 0; seed < 2; ++seed) { + std::vector input(32), output(32, -999.0f), expected = output; + for (size_t i = 0; i < input.size(); ++i) + input[i] = float(int(i) - 9 + seed) / 8; + void* xp = input.data(); + void* yp = output.data(); +#ifdef WITH_HYGON + const bool gpu = type == Device::Type::kHygon; + Buffer xgpu(gpu ? 128 : 0), ygpu(gpu ? 128 : 0); + if (gpu) { + xp = xgpu.pointer; + yp = ygpu.pointer; + } +#endif + for (double alpha : {0.5, -2.0, 0.5}) { + std::fill(output.begin(), output.end(), -999.0f); + expected = output; +#ifdef WITH_HYGON + if (gpu) { + checked( + cudaMemcpy(xp, input.data(), 128, cudaMemcpyHostToDevice)); + checked( + cudaMemcpy(yp, output.data(), 128, cudaMemcpyHostToDevice)); + } +#endif + Tensor x(xp, shape, DataType::kFloat32, device, xs); + Tensor y(yp, shape, DataType::kFloat32, device, ys); + Handle handle; + Config config; + config.set_implementation_index(8); + Mul::Call(handle, config, x, alpha, y); +#ifdef WITH_HYGON + if (gpu) + checked( + cudaMemcpy(output.data(), yp, 128, cudaMemcpyDeviceToHost)); +#endif + const size_t rows = layout == 0 ? 17 + : layout == 1 ? 2 + : layout == 2 ? 1 + : 0; + const size_t cols = layout == 1 ? 3 : 1; + for (size_t r = 0; r < rows; ++r) + for (size_t c = 0; c < cols; ++c) { + const auto ix = layout == 2 ? 0 : r * xs[0] + c * xs[1]; + const auto iy = layout == 2 ? 0 : r * ys[0] + c * ys[1]; + expected[iy] = input[ix] * static_cast(alpha); + } + if (output != expected) + throw std::runtime_error( + "scalar Mul result or output canary mismatch"); + ++checks; + } + } + } + } + std::cout << "{\"passed\":true,\"checks\":" << checks << "}\n"; + } catch (const std::exception&) { + std::cout << "{\"passed\":false,\"checks\":" << checks << "}\n"; + return 1; + } +} From c116490da340a362b5944bebf8ff31dfa74e10e1 Mon Sep 17 00:00:00 2001 From: qin guoyong <305039273@qq.com> Date: Wed, 25 Nov 2026 05:06:46 +0800 Subject: [PATCH 16/18] perf(hygon): accelerate routed and shared W8A8 experts Use packed integer dot products and one wave per output channel on Hygon. Reduce quantization barriers with wave reductions. Preserve the SDK's rounding, scale floor, epilogue order and workspace layout, with the scalar fallback retained for tail or misaligned weight rows. Add public SDK regressions for FP32/BF16 routed/shared experts, changing graphs, invalid routes, grid-stride loops and the safe INT32 dot limit. Each of two complete runs passed 164 strict checks and 112 graph replays. Eight scenarios in five rounds per run improved throughput by at least 23.22% against the repository's retained scalar expert kernels. A prior negative round is retained in the verification record; external SGLang indicator-10 acceptance and full-model/TP8 validation remain incomplete. --- .../ops/deepseek_v4_w8a8_experts/kernel.h | 228 +++++++++++------ .../deepseek_v4_w8a8_experts/kernel_v2.cuh | 149 +++++++++++ .../ops/deepseek_v4_w8a8_experts/math.cuh | 17 ++ tests/native/hygon_experts/CMakeLists.txt | 11 + tests/native/hygon_experts/README.md | 27 ++ tests/native/hygon_experts/check_experts.py | 232 ++++++++++++++++++ tests/native/hygon_experts/expert_probe.cu | 81 ++++++ 7 files changed, 671 insertions(+), 74 deletions(-) create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel_v2.cuh create mode 100644 src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/math.cuh create mode 100644 tests/native/hygon_experts/CMakeLists.txt create mode 100644 tests/native/hygon_experts/README.md create mode 100644 tests/native/hygon_experts/check_experts.py create mode 100644 tests/native/hygon_experts/expert_probe.cu diff --git a/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h index 24a82fa06..b64fbb8b6 100644 --- a/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h +++ b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h @@ -8,6 +8,8 @@ #include #include "base/deepseek_v4_w8a8_experts.h" +#include "kernel_v2.cuh" +#include "math.cuh" #include "native/cuda/hygon/dynamic_int8_quant.cuh" namespace infini::ops { @@ -16,19 +18,10 @@ namespace deepseek_v4_w8a8_detail { using hygon_dynamic_int8::kThreads; using hygon_dynamic_int8::Quantize; -// HCU shared dense projection switches to its assembly epilogue above 16 -// tokens. Routed GEMMs always use the joint-scale epilogue. -static __device__ float ScaledDot(int32_t accumulator, float token_scale, float weight_scale, - bool joint_scale) { - if (!joint_scale) - return __fmul_rn(__fmul_rn(static_cast(accumulator), token_scale), weight_scale); - const float scale = __fmul_rn(token_scale, weight_scale); - return __fmul_rn(static_cast(accumulator), scale); -} - template -__global__ void GateUp(const int8_t* x, const float* input_scale, const int64_t* indices, - const int8_t* w, const float* scale, float* activated, size_t routes, +__global__ void GateUp(const int8_t* x, const float* input_scale, + const int64_t* indices, const int8_t* w, + const float* scale, float* activated, size_t routes, size_t top_k, size_t experts, size_t hidden, size_t inner, float limit, bool routed) { __shared__ int32_t gate_sum[kThreads]; @@ -41,7 +34,8 @@ __global__ void GateUp(const int8_t* x, const float* input_scale, const int64_t* if (lane == 0) activated[index] = 0.0f; continue; } - const size_t gate = static_cast(expert) * (2 * inner) + index % inner; + const size_t gate = + static_cast(expert) * (2 * inner) + index % inner; const size_t up = gate + inner; const auto* row = x + (route / top_k) * hidden; int32_t g = 0, u = 0; @@ -62,25 +56,34 @@ __global__ void GateUp(const int8_t* x, const float* input_scale, const int64_t* if (lane == 0) { using Convert = Caster; const float token_scale = input_scale[route / top_k]; - const auto cast_gate = Convert::template Cast(ScaledDot(gate_sum[0], token_scale, scale[gate], routed || routes / top_k > 16)); - const auto cast_up = Convert::template Cast(ScaledDot(up_sum[0], token_scale, scale[up], routed || routes / top_k > 16)); - float clipped_gate = fminf(Convert::template Cast(cast_gate), limit); + const auto cast_gate = Convert::template Cast( + ScaledDot(gate_sum[0], token_scale, scale[gate], + routed || routes / top_k > 16)); + const auto cast_up = Convert::template Cast(ScaledDot( + up_sum[0], token_scale, scale[up], routed || routes / top_k > 16)); + float clipped_gate = + fminf(Convert::template Cast(cast_gate), limit); if (routed) clipped_gate = fmaxf(clipped_gate, -limit); - const float clipped_up = fminf(fmaxf(Convert::template Cast(cast_up), -limit), limit); + const float clipped_up = + fminf(fmaxf(Convert::template Cast(cast_up), -limit), limit); float gate_activation = clipped_gate / (1.0f + expf(-clipped_gate)); - if (routed) gate_activation = Convert::template Cast(Convert::template Cast(gate_activation)); + if (routed) + gate_activation = Convert::template Cast( + Convert::template Cast(gate_activation)); const float value = gate_activation * clipped_up; - activated[index] = Convert::template Cast(Convert::template Cast(value)); + activated[index] = + Convert::template Cast(Convert::template Cast(value)); } __syncthreads(); } } template -__global__ void Down(const int8_t* activated, const float* activation_scale, const int64_t* indices, - const float* weights, const int8_t* w, const float* scale, - T* out, size_t tokens, size_t top_k, size_t experts, - size_t hidden, size_t inner, bool routed, float routed_scale) { +__global__ void Down(const int8_t* activated, const float* activation_scale, + const int64_t* indices, const float* weights, + const int8_t* w, const float* scale, T* out, size_t tokens, + size_t top_k, size_t experts, size_t hidden, size_t inner, + bool routed, float routed_scale) { __shared__ int32_t sum[kThreads]; const unsigned lane = threadIdx.x; for (size_t index = blockIdx.x; index < tokens * hidden; index += gridDim.x) { @@ -94,7 +97,8 @@ __global__ void Down(const int8_t* activated, const float* activation_scale, con const size_t channel_idx = static_cast(expert) * hidden + channel; int32_t local = 0; for (size_t i = lane; i < inner; i += kThreads) { - local += static_cast(activated[route * inner + i]) * w[channel_idx * inner + i]; + local += static_cast(activated[route * inner + i]) * + w[channel_idx * inner + i]; } sum[lane] = local; __syncthreads(); @@ -104,18 +108,24 @@ __global__ void Down(const int8_t* activated, const float* activation_scale, con } if (lane == 0) { using Convert = Caster; - const float token_scale = activation_scale[route], weight_scale = scale[channel_idx]; + const float token_scale = activation_scale[route], + weight_scale = scale[channel_idx]; if (routed) { - const float weighted = ScaledDot(sum[0], token_scale, weight_scale, true) * weights[route]; - output += Convert::template Cast(Convert::template Cast(weighted)); + const float weighted = + ScaledDot(sum[0], token_scale, weight_scale, true) * + weights[route]; + output += Convert::template Cast( + Convert::template Cast(weighted)); } else { - output += ScaledDot(sum[0], token_scale, weight_scale, tokens > 16) * weights[route]; + output += ScaledDot(sum[0], token_scale, weight_scale, tokens > 16) * + weights[route]; } } __syncthreads(); } if (lane == 0) { - out[index] = Caster::template Cast(output * routed_scale); + out[index] = + Caster::template Cast(output * routed_scale); } } } @@ -126,21 +136,26 @@ template <> class Operator : public DeepseekV4W8a8Experts { public: - static bool supports(const Tensor x, const Tensor weights, const Tensor indices, - const Tensor gate_up, const Tensor gate_up_scale, - const Tensor down, const Tensor down_scale, - double limit, const std::optional routed_scale, const Tensor out) { + static bool supports(const Tensor x, const Tensor weights, + const Tensor indices, const Tensor gate_up, + const Tensor gate_up_scale, const Tensor down, + const Tensor down_scale, double limit, + const std::optional routed_scale, + const Tensor out) { if (x.device().type() != Device::Type::kHygon || x.ndim() != 2 || weights.ndim() != 2 || gate_up.ndim() != 3 || down.ndim() != 3 || out.shape() != x.shape() || out.dtype() != x.dtype() || (x.dtype() != DataType::kBFloat16 && x.dtype() != DataType::kFloat32) || - weights.dtype() != DataType::kFloat32 || indices.dtype() != DataType::kInt64 || + weights.dtype() != DataType::kFloat32 || + indices.dtype() != DataType::kInt64 || weights.shape() != indices.shape() || weights.size(0) != x.size(0) || gate_up.dtype() != DataType::kInt8 || down.dtype() != DataType::kInt8 || - gate_up_scale.dtype() != DataType::kFloat32 || down_scale.dtype() != DataType::kFloat32 || - !std::isfinite(limit) || limit <= 0.0 || limit > std::numeric_limits::max() || - (routed_scale && (!std::isfinite(*routed_scale) || *routed_scale <= 0.0 || - *routed_scale > std::numeric_limits::max()))) + gate_up_scale.dtype() != DataType::kFloat32 || + down_scale.dtype() != DataType::kFloat32 || !std::isfinite(limit) || + limit <= 0.0 || limit > std::numeric_limits::max() || + (routed_scale && + (!std::isfinite(*routed_scale) || *routed_scale <= 0.0 || + *routed_scale > std::numeric_limits::max()))) return false; const auto experts = down.size(0), hidden = x.size(1), inner = down.size(2); @@ -148,59 +163,76 @@ class Operator if (!experts || !hidden || !inner || !x.size(0) || !weights.size(1) || hidden > kMaxDotWidth || inner > kMaxDotWidth || x.size(0) > size_limit / (hidden + sizeof(float)) / 2 || - x.size(0) > size_limit / weights.size(1) / (inner * (sizeof(float) + 1) + sizeof(float)) / 2 || + x.size(0) > size_limit / weights.size(1) / + (inner * (sizeof(float) + 1) + sizeof(float)) / 2 || gate_up.shape() != Tensor::Shape{experts, 2 * inner, hidden} || down.shape() != Tensor::Shape{experts, hidden, inner} || gate_up_scale.shape() != Tensor::Shape{experts, 2 * inner, 1} || down_scale.shape() != Tensor::Shape{experts, hidden, 1}) return false; - for (const auto* t : {&x, &weights, &indices, &gate_up, &gate_up_scale, &down, &down_scale, &out}) { + for (const auto* t : {&x, &weights, &indices, &gate_up, &gate_up_scale, + &down, &down_scale, &out}) { if (t->device() != x.device() || !t->IsContiguous()) return false; } return true; } Operator(const Tensor x, const Tensor weights, const Tensor indices, - const Tensor gate_up, const Tensor gate_up_scale, - const Tensor down, const Tensor down_scale, double limit, const std::optional routed_scale, Tensor out) - : DeepseekV4W8a8Experts(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out) { - if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out)) - throw std::invalid_argument("DeepSeek V4 W8A8 expert input is unsupported"); + const Tensor gate_up, const Tensor gate_up_scale, const Tensor down, + const Tensor down_scale, double limit, + const std::optional routed_scale, Tensor out) + : DeepseekV4W8a8Experts(x, weights, indices, gate_up, gate_up_scale, down, + down_scale, limit, routed_scale, out) { + if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, + limit, routed_scale, out)) + throw std::invalid_argument( + "DeepSeek V4 W8A8 expert input is unsupported"); workspace_bytes_ = workspace_bytes(x, weights, down.size(2)); } - std::size_t workspace_size_in_bytes() const override { return workspace_bytes_; } + std::size_t workspace_size_in_bytes() const override { + return workspace_bytes_; + } bool capture_safe() const override { return true; } void operator()(const Tensor x, const Tensor weights, const Tensor indices, const Tensor gate_up, const Tensor gate_up_scale, - const Tensor down, const Tensor down_scale, - double limit, const std::optional routed_scale, Tensor out) const override { - if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out) || + const Tensor down, const Tensor down_scale, double limit, + const std::optional routed_scale, + Tensor out) const override { + if (!supports(x, weights, indices, gate_up, gate_up_scale, down, down_scale, + limit, routed_scale, out) || workspace_bytes(x, weights, down.size(2)) != workspace_bytes_ || workspace_size_in_bytes_ < workspace_bytes_ || !workspace_) - throw std::invalid_argument("DeepSeek V4 W8A8 expert inputs or workspace changed"); + throw std::invalid_argument( + "DeepSeek V4 W8A8 expert inputs or workspace changed"); if (x.dtype() == DataType::kFloat32) { - launch(x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out); + launch(x, weights, indices, gate_up, gate_up_scale, down, + down_scale, limit, routed_scale, out); } else { launch>( - x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, routed_scale, out); + x, weights, indices, gate_up, gate_up_scale, down, down_scale, limit, + routed_scale, out); } } private: - static constexpr size_t kMaxDotWidth = std::numeric_limits::max() / (127 * 128); + static constexpr size_t kMaxDotWidth = + std::numeric_limits::max() / (127 * 128); size_t workspace_bytes_{}; - static size_t workspace_bytes(const Tensor x, const Tensor weights, size_t inner) { + static size_t workspace_bytes(const Tensor x, const Tensor weights, + size_t inner) { const size_t tokens = x.size(0), routes = weights.numel(); - return (routes * inner + tokens + routes) * sizeof(float) + tokens * x.size(1) + routes * inner; + return (routes * inner + tokens + routes) * sizeof(float) + + tokens * x.size(1) + routes * inner; } template void launch(const Tensor x, const Tensor weights, const Tensor indices, const Tensor gate_up, const Tensor gate_up_scale, - const Tensor down, const Tensor down_scale, double limit, const std::optional routed_scale, Tensor out) const { + const Tensor down, const Tensor down_scale, double limit, + const std::optional routed_scale, Tensor out) const { using namespace deepseek_v4_w8a8_detail; const auto stream = static_cast(stream_); const auto routes = weights.numel(), inner = down.size(2); @@ -211,24 +243,72 @@ class Operator auto* activation_scale = input_scale + tokens; auto* q_input = reinterpret_cast(activation_scale + routes); auto* q_activated = q_input + tokens * hidden; - const auto grid = [](size_t rows) { return static_cast(rows < 65535 ? rows : 65535); }; - Quantize<<>>( - static_cast(x.data()), q_input, input_scale, tokens, hidden); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 input quantization launch failed"); - GateUp<<>>( - q_input, input_scale, ids, static_cast(gate_up.data()), - static_cast(gate_up_scale.data()), activated, routes, weights.size(1), - down.size(0), hidden, inner, static_cast(limit), routed_scale.has_value()); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 gate/up launch failed"); - Quantize<<>>( - activated, q_activated, activation_scale, routes, inner); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 activation quantization launch failed"); - Down<<>>( - q_activated, activation_scale, ids, static_cast(weights.data()), - static_cast(down.data()), static_cast(down_scale.data()), - static_cast(out.data()), tokens, weights.size(1), down.size(0), hidden, inner, - routed_scale.has_value(), static_cast(routed_scale.value_or(1.0))); - if (cudaGetLastError() != cudaSuccess) throw std::runtime_error("DeepSeek V4 down launch failed"); + const auto grid = [](size_t rows) { + return static_cast(rows < 65535 ? rows : 65535); + }; + // Keep the scalar path for packed rows with insufficient alignment. + const bool packed = hidden % 4 == 0 && inner % 4 == 0 && + reinterpret_cast(q_input) % 4 == 0 && + reinterpret_cast(q_activated) % 4 == 0 && + reinterpret_cast(gate_up.data()) % 4 == 0 && + reinterpret_cast(down.data()) % 4 == 0; + if (packed) { + expert_wave::Quant<<>>( + static_cast(x.data()), q_input, input_scale, tokens, + hidden); + } else { + Quantize<<>>( + static_cast(x.data()), q_input, input_scale, tokens, + hidden); + } + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("DeepSeek V4 input quantization launch failed"); + if (packed) { + expert_wave::Gate<<>>( + q_input, input_scale, ids, static_cast(gate_up.data()), + static_cast(gate_up_scale.data()), activated, routes, + weights.size(1), down.size(0), hidden, inner, + static_cast(limit), routed_scale.has_value()); + } else { + GateUp<<>>( + q_input, input_scale, ids, static_cast(gate_up.data()), + static_cast(gate_up_scale.data()), activated, routes, + weights.size(1), down.size(0), hidden, inner, + static_cast(limit), routed_scale.has_value()); + } + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("DeepSeek V4 gate/up launch failed"); + if (packed) { + expert_wave::Quant<<>>( + activated, q_activated, activation_scale, routes, inner); + } else { + Quantize<<>>( + activated, q_activated, activation_scale, routes, inner); + } + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error( + "DeepSeek V4 activation quantization launch failed"); + if (packed) { + expert_wave::Down<<>>( + q_activated, activation_scale, ids, + static_cast(weights.data()), + static_cast(down.data()), + static_cast(down_scale.data()), + static_cast(out.data()), tokens, weights.size(1), down.size(0), + hidden, inner, routed_scale.has_value(), + static_cast(routed_scale.value_or(1.0))); + } else { + Down<<>>( + q_activated, activation_scale, ids, + static_cast(weights.data()), + static_cast(down.data()), + static_cast(down_scale.data()), + static_cast(out.data()), tokens, weights.size(1), down.size(0), + hidden, inner, routed_scale.has_value(), + static_cast(routed_scale.value_or(1.0))); + } + if (cudaGetLastError() != cudaSuccess) + throw std::runtime_error("DeepSeek V4 down launch failed"); } }; diff --git a/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel_v2.cuh b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel_v2.cuh new file mode 100644 index 000000000..42aaa8d1e --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel_v2.cuh @@ -0,0 +1,149 @@ +#pragma once +#include "math.cuh" +#include "native/cuda/hygon/dynamic_int8_quant.cuh" +namespace infini::ops::expert_wave { +__device__ __forceinline__ int DotWord(int a, int b, int sum) { +#if defined(__gfx936__) + return __builtin_amdgcn_sdot4(a, b, sum, false); +#else + for (int j = 0; j < 4; ++j) + sum += + static_cast(a >> (8 * j)) * static_cast(b >> (8 * j)); + return sum; +#endif +} +__device__ __forceinline__ int Reduce(int sum) { + for (int delta = 32; delta > 0; delta >>= 1) + sum += __shfl_down_sync(0xffffffffffffffffull, sum, delta, 64); + return sum; +} +template +__global__ void Quant(const T* input, int8_t* q, float* scales, size_t rows, + size_t width) { + const unsigned lane = threadIdx.x & 63, wave = threadIdx.x / 64; + __shared__ float maxima[4]; + for (size_t row = blockIdx.x; row < rows; row += gridDim.x) { + float maximum = 0.0f; + for (size_t j = threadIdx.x; j < width; j += blockDim.x) + maximum = fmaxf(maximum, + fabsf(Caster::template Cast( + input[row * width + j]))); + for (int delta = 32; delta > 0; delta >>= 1) + maximum = fmaxf( + maximum, __shfl_xor_sync(0xffffffffffffffffull, maximum, delta, 64)); + if (lane == 0) maxima[wave] = maximum; + __syncthreads(); + for (unsigned i = 0; i < blockDim.x / 64; ++i) + maximum = fmaxf(maximum, maxima[i]); + maximum = fmaxf(maximum, 1.0e-10f); + const float scale = maximum / 127.0f, inverse = 127.0f / maximum; + if (threadIdx.x == 0) scales[row] = scale; + for (size_t j = threadIdx.x; j < width; j += blockDim.x) { + const int value = + __float2int_rn(Caster::template Cast( + input[row * width + j]) * + inverse); + q[row * width + j] = static_cast(min(max(value, -127), 127)); + } + __syncthreads(); + } +} +template +__global__ void Gate(const int8_t* x, const float* input_scale, + const int64_t* ids, const int8_t* w, const float* scale, + float* activated, size_t routes, size_t top_k, + size_t experts, size_t hidden, size_t inner, float limit, + bool routed) { + const unsigned lane = threadIdx.x & 63, wave = threadIdx.x / 64; + const size_t first = static_cast(blockIdx.x) * 4 + wave; + for (size_t index = first; index < routes * inner; + index += static_cast(gridDim.x) * 4) { + const size_t route = index / inner; + const auto expert = ids[route]; + if (expert < 0 || static_cast(expert) >= experts) { + if (lane == 0) activated[index] = 0.0f; + continue; + } + const size_t gate = + static_cast(expert) * (2 * inner) + index % inner, + up = gate + inner; + const auto* input = + reinterpret_cast(x + (route / top_k) * hidden); + const auto* gw = reinterpret_cast(w + gate * hidden); + const auto* uw = reinterpret_cast(w + up * hidden); + int g = 0, u = 0; + for (size_t j = lane; j < hidden / 4; j += 64) { + const int value = __ldg(input + j); + g = DotWord(value, __ldg(gw + j), g); + u = DotWord(value, __ldg(uw + j), u); + } + g = Reduce(g); + u = Reduce(u); + if (lane == 0) { + using Convert = Caster; + using deepseek_v4_w8a8_detail::ScaledDot; + const float token_scale = input_scale[route / top_k]; + const auto cast_gate = Convert::template Cast(ScaledDot( + g, token_scale, scale[gate], routed || routes / top_k > 16)); + const auto cast_up = Convert::template Cast( + ScaledDot(u, token_scale, scale[up], routed || routes / top_k > 16)); + float clipped_gate = + fminf(Convert::template Cast(cast_gate), limit); + if (routed) clipped_gate = fmaxf(clipped_gate, -limit); + const float clipped_up = + fminf(fmaxf(Convert::template Cast(cast_up), -limit), limit); + float gate_activation = clipped_gate / (1.0f + expf(-clipped_gate)); + if (routed) + gate_activation = Convert::template Cast( + Convert::template Cast(gate_activation)); + const float value = gate_activation * clipped_up; + activated[index] = + Convert::template Cast(Convert::template Cast(value)); + } + } +} +template +__global__ void Down(const int8_t* activated, const float* activation_scale, + const int64_t* ids, const float* weights, const int8_t* w, + const float* scale, T* out, size_t tokens, size_t top_k, + size_t experts, size_t hidden, size_t inner, bool routed, + float routed_scale) { + const unsigned lane = threadIdx.x & 63, wave = threadIdx.x / 64; + const size_t first = static_cast(blockIdx.x) * 4 + wave; + for (size_t index = first; index < tokens * hidden; + index += static_cast(gridDim.x) * 4) { + const size_t token = index / hidden, channel = index % hidden; + float output = 0.0f; + for (size_t k = 0; k < top_k; ++k) { + const size_t route = token * top_k + k; + const auto expert = ids[route]; + if (expert < 0 || static_cast(expert) >= experts) continue; + const size_t ci = static_cast(expert) * hidden + channel; + const auto* input = + reinterpret_cast(activated + route * inner); + const auto* wp = reinterpret_cast(w + ci * inner); + int sum = 0; + for (size_t j = lane; j < inner / 4; j += 64) + sum = DotWord(__ldg(input + j), __ldg(wp + j), sum); + sum = Reduce(sum); + if (lane == 0) { + using Convert = Caster; + using deepseek_v4_w8a8_detail::ScaledDot; + const float token_scale = activation_scale[route], + weight_scale = scale[ci]; + if (routed) { + const float weighted = + ScaledDot(sum, token_scale, weight_scale, true) * weights[route]; + output += Convert::template Cast( + Convert::template Cast(weighted)); + } else + output += ScaledDot(sum, token_scale, weight_scale, tokens > 16) * + weights[route]; + } + } + if (lane == 0) + out[index] = + Caster::template Cast(output * routed_scale); + } +} +} // namespace infini::ops::expert_wave diff --git a/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/math.cuh b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/math.cuh new file mode 100644 index 000000000..880f8a6ba --- /dev/null +++ b/src/native/cuda/hygon/ops/deepseek_v4_w8a8_experts/math.cuh @@ -0,0 +1,17 @@ +#pragma once +#include + +#include +namespace infini::ops::deepseek_v4_w8a8_detail { +// HCU shared dense projection switches to its assembly epilogue above 16 +// tokens. Routed GEMMs always use the joint-scale epilogue. +static __device__ float ScaledDot(int32_t accumulator, float token_scale, + float weight_scale, bool joint_scale) { + if (!joint_scale) + return __fmul_rn(__fmul_rn(static_cast(accumulator), token_scale), + weight_scale); + const float scale = __fmul_rn(token_scale, weight_scale); + return __fmul_rn(static_cast(accumulator), scale); +} + +} // namespace infini::ops::deepseek_v4_w8a8_detail diff --git a/tests/native/hygon_experts/CMakeLists.txt b/tests/native/hygon_experts/CMakeLists.txt new file mode 100644 index 000000000..dc9d39c32 --- /dev/null +++ b/tests/native/hygon_experts/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.18) +project(HygonExpertChecks LANGUAGES CXX CUDA) +find_package(CUDAToolkit REQUIRED) +if(NOT "$ENV{INFINILM_CXX11_ABI}" MATCHES "^[01]$") + message(FATAL_ERROR "Set INFINILM_CXX11_ABI to match the installed SDK") +endif() +add_library(expert_probe SHARED expert_probe.cu) +target_compile_features(expert_probe PRIVATE cxx_std_17 cuda_std_17) +target_include_directories(expert_probe PRIVATE ../../../src "$ENV{INFINI_ROOT}/include") +target_compile_definitions(expert_probe PRIVATE WITH_HYGON=1 _GLIBCXX_USE_CXX11_ABI=$ENV{INFINILM_CXX11_ABI}) +target_link_libraries(expert_probe PRIVATE CUDA::cudart "$ENV{INFINI_ROOT}/lib/libinfinirt.so" "$ENV{INFINI_ROOT}/lib/libinfiniops.so") diff --git a/tests/native/hygon_experts/README.md b/tests/native/hygon_experts/README.md new file mode 100644 index 000000000..a826e94e4 --- /dev/null +++ b/tests/native/hygon_experts/README.md @@ -0,0 +1,27 @@ +# Hygon W8A8 experts + +These checks call the installed SDK public `DeepseekV4W8a8Experts` interface. +They compare all output and workspace bytes with the retained scalar kernels, +check quantization against a separate CPU oracle and protect workspace guards. +Coverage includes routed/shared experts, FP32/BF16, changing graph inputs, ties, +invalid/duplicate expert IDs, short/tail dimensions, misaligned weight fallback, +the 16/17-token epilogue boundary, grid-stride loops and the safe INT32 dot limit. + +Source the existing SDK environment, including `INFINI_ROOT` and +`INFINILM_CXX11_ABI`, and use the matching Hygon toolchain: + +```sh +cmake -S tests/native/hygon_experts -B build/expert-checks \ + -DCMAKE_CUDA_COMPILER=/opt/dtk/cuda/cuda/bin/nvcc \ + -DCUDAToolkit_ROOT=/opt/dtk/cuda/cuda \ + -DCMAKE_CUDA_ARCHITECTURES=OFF \ + -DCMAKE_CUDA_FLAGS="-arch=gfx936" -DCMAKE_BUILD_TYPE=Release +cmake --build build/expert-checks -j4 +python tests/native/hygon_experts/check_experts.py \ + --library build/expert-checks/libexpert_probe.so --output verification-experts +``` + +Add `--performance` for five alternating timing rounds against the retained +repository scalar implementation. This local baseline is explicitly distinct +from SGLang and the owner's indicator-10 acceptance suite. No acceptance claim +is made by a successful microbenchmark. diff --git a/tests/native/hygon_experts/check_experts.py b/tests/native/hygon_experts/check_experts.py new file mode 100644 index 000000000..0d5ea4229 --- /dev/null +++ b/tests/native/hygon_experts/check_experts.py @@ -0,0 +1,232 @@ +"""Strict Hygon W8A8 regression and optional paired performance screen. + +The baseline retains the pre-optimization scalar GPU kernels. All outputs and +workspace bytes must match. Input quantization is independently checked on CPU, +using direct division with one FP32 rounding instead of reciprocal-multiply. +Performance against this baseline is not external indicator-10 acceptance. +""" +from pathlib import Path +import argparse +import ctypes +import json +import statistics +import torch + +def main(): + parser = argparse.ArgumentParser( + description='Check Hygon W8A8 expert SDK calls against the preserved scalar kernels') + parser.add_argument('--library', required=True) + parser.add_argument('--output', default='verification-experts') + parser.add_argument('--performance', action='store_true') + options = parser.parse_args() + out = Path(options.output) + out.mkdir(parents=True, exist_ok=True) + lib = ctypes.CDLL(options.library) + fn = lib.run + ptr = ctypes.c_void_p + fn.argtypes = [ + ptr] * 9 + [ctypes.c_size_t] * 5 + [ctypes.c_float, + ctypes.c_int, ctypes.c_float, ctypes.c_int, ctypes.c_int, + ptr] + fn.restype = ctypes.c_int + torch.manual_seed(20261125) + checks = 0 + graphs = 0 + quant_checks = 0 + + def inputs(m, h, inner, e, k, dtype): + x = torch.randn((m, h), device='cuda', dtype=dtype) + weights = torch.rand((m, k), device='cuda') + ids = torch.randint(0, e, (m, k), device='cuda', dtype=torch.int64) + g = torch.randint(-128, 128, (e, 2 * inner, h), device='cuda', dtype=torch.int8) + gs = torch.rand((e, 2 * inner, 1), device='cuda') * 0.01 + d = torch.randint(-128, 128, (e, h, inner), device='cuda', dtype=torch.int8) + ds = torch.rand((e, h, 1), device='cuda') * 0.01 + count = (m * k * inner + m + m * k) * 4 + m * h + m * k * inner + storage = [ + torch.full((count + 64,), 97, device='cuda', dtype=torch.uint8) for _ in range(2)] + ws = [z[32:-32] for z in storage] + ys = [torch.empty_like(x) for _ in range(2)] + return (x, weights, ids, g, gs, d, ds, storage, ws, ys) + + def runner(args, mode, routed, limit, scale): + (x, weights, ids, g, gs, d, ds, storage, ws, ys) = args + (m, h) = x.shape + (e, _, inner) = d.shape + k = ids.shape[1] + + def run(): + status = fn( + x.data_ptr(), weights.data_ptr(), ids.data_ptr(), g.data_ptr(), + gs.data_ptr(), d.data_ptr(), ds.data_ptr(), ys[mode].data_ptr(), + ws[mode].data_ptr(), m, h, inner, e, k, limit, routed, + scale, mode, int(x.dtype == torch.bfloat16), torch.cuda.current_stream().cuda_stream) + if status: + raise RuntimeError('expert launch failed ' + str(status)) + return run + + def verify(args, context): + nonlocal checks, quant_checks + (x, weights, ids, g, gs, d, ds, storage, ws, ys) = args + (m, h) = x.shape + inner = d.shape[2] + routes = ids.numel() + torch.cuda.synchronize() + if not torch.equal( + ys[0].view(torch.uint8), ys[1].view(torch.uint8)) or not torch.equal( + ws[0], ws[1]): + (out / 'failure.json').write_text( + json.dumps({'context': context, 'output_equal': torch.equal(ys[0].view(torch.uint8), + ys[1].view(torch.uint8)), 'workspace_equal': torch.equal(ws[0], + ws[1]), 'max_abs': float((ys[0].float() - ys[1].float()).abs().max()), + 'workspace_different_bytes': int((ws[0] != ws[1]).sum())})) + raise RuntimeError('Expert output or intermediate mismatch') + for z in storage: + assert bool((z[:32] == 97).all() and (z[-32:] == 97).all()) + cpu = x.cpu().float() + maximum = cpu.abs().amax(-1, keepdim=True).clamp_min(1e-10) + scale = (maximum.double() / 127).float() + inverse = (torch.full_like(maximum.double(), 127) / maximum.double()).float() + expected = (cpu * inverse).round().clamp(-127, 127).to(torch.int8) + offset = (routes * inner + m + routes) * 4 + actual_q = ws[1][offset:offset + m * h].view(torch.int8).reshape(m, h).cpu() + actual_s = ws[1][routes * inner * 4:(routes * inner + m) * 4].view(torch.float32).cpu() + assert torch.equal( + actual_q, expected) and torch.equal(actual_s.view(torch.uint8), + scale.flatten().view(torch.uint8)) + checks += 1 + quant_checks += 1 + for dtype in [torch.float32, torch.bfloat16]: + for (m, h, inner) in [(1, 256, 64), (3, 1024, 128), (17, 256, 64), (2, 259, 67)]: + for (routed, k, limit) in [(False, 1, 7.0), (True, 6, 1.0), (True, 6, 7.0)]: + args = inputs(m, h, inner, 8, k, dtype) + args[2].view(-1)[0] = -1 + if args[2].numel() > 1: + args[2].view(-1)[1] = 8 + runs = [runner(args, i, routed, limit, 1.5 if routed else 1.0) for i in [0, 1]] + for run in runs: + run() + verify( + args, {'shape': [m, h, inner], 'dtype': str(dtype), + 'routed': routed, 'k': k, 'phase': 'eager'}) + gs = [] + for run in runs: + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph): + run() + gs.append(graph) + for seed in range(3): + args[0].fill_(0.0 if seed == 0 else 1.0 if seed == 1 else -64.0) + args[2].fill_(seed % 8) + for graph in gs: + graph.replay() + verify( + args, {'shape': [m, h, inner], 'dtype': str(dtype), + 'routed': routed, 'k': k, 'phase': 'graph', 'seed': seed}) + graphs += 1 + cases = [] + for dtype in [torch.float32, torch.bfloat16]: + for (m, + h, inner, k) in [(1, 4, 4, 1), (2, 4, 8, 6), (3, 68, + 12, 6), (16, 64, 64, 1), (17, 64, 64, 1), (257, 64, + 256, 6), (257, 1024, 4, 1), (65536, 4, 4, 1), (1, + 132104, 4, 1)]: + for misaligned in [False, True] if m < 100 and h < 100 else [False]: + args = list(inputs(m, h, inner, 1 if k == 1 else 8, k, dtype)) + if misaligned: + for index in [3, 5]: + old = args[index] + storage = torch.empty(old.numel() + 1, device='cuda', dtype=torch.int8) + shift = storage[1:].reshape(old.shape) + shift.copy_(old) + args[index] = shift + if h == 132104: + args[0].fill_(127) + args[3].fill_(-128) + args[5].fill_(127) + elif m < 100: + args[0].zero_() + args[0][:, 0] = 127 + args[0][:, 1] = 0.5 + args[0][:, 2] = -1.5 + args[0][:, 3] = 2.5 + args[2].zero_() + args[1].fill_(0.25) + routed = k > 1 + runs = [runner(args, i, routed, 7.0, 0.25 if routed else 1.0) for i in [0, 1]] + for run in runs: + run() + context = { + 'shape': [m, h, inner], 'dtype': str(dtype), 'misaligned_weights': misaligned, + 'routed': routed} + verify(args, context) + cases.append(context) + if m < 100 and h < 100: + gs = [] + for run in runs: + g = torch.cuda.CUDAGraph() + with torch.cuda.graph(g): + run() + gs.append(g) + for value in [1e-12, -1e-12]: + args[0].fill_(value) + for g in gs: + g.replay() + verify(args, dict(context, tiny_input=value)) + graphs += 1 + (out / 'correctness.json').write_text( + json.dumps({'passed': True, 'checks': checks, 'graph_checks': graphs, + 'independent_input_quant_checks': quant_checks, 'intermediates_bitwise_equal': True, + 'guard_canaries': True})) + if not options.performance: + print(json.dumps({'passed': True, + 'checks': checks, 'graph_checks': graphs, 'edge_cases': len(cases)})) + return + results = [] + for dtype in [torch.float32, torch.bfloat16]: + for (m, + h, inner, e, k, routed) in [(1, 4096, 512, 16, 6, + True), (8, 4096, 512, 16, 6, True), (32, 1024, 256, + 8, 6, True), (17, 1024, 512, 1, 1, False)]: + args = inputs(m, h, inner, e, k, dtype) + runs = [runner(args, i, routed, 7.0, 1.5 if routed else 1.0) for i in [0, 1]] + gs = [] + for run in runs: + for _ in range(10): + run() + torch.cuda.synchronize() + g = torch.cuda.CUDAGraph() + with torch.cuda.graph(g): + run() + gs.append(g) + for graph in gs: + graph.replay() + verify(args, {'shape': [m, h, inner], 'dtype': str(dtype), 'phase': 'performance'}) + rounds = [] + for repeat in range(5): + values = [[], []] + for sample in range(20): + for mode in [0, 1] if (repeat + sample) % 2 == 0 else [1, 0]: + start = torch.cuda.Event(enable_timing=True) + end = torch.cuda.Event(enable_timing=True) + start.record() + gs[mode].replay() + end.record() + end.synchronize() + values[mode].append(start.elapsed_time(end) * 1000) + base = statistics.mean(values[0]) + opt = statistics.mean(values[1]) + rounds.append( + {'baseline_us': base, 'candidate_us': opt, 'throughput_gain': 100 * (base / opt - 1), + 'baseline_samples_us': values[0], 'candidate_samples_us': values[1]}) + results.append( + {'shape': [m, h, inner], 'dtype': str(dtype), 'routed': routed, + 'rounds': rounds, 'min_gain': min((x['throughput_gain'] for x in rounds))}) + (out / 'performance.json').write_text( + json.dumps({'baseline': 'unchanged new repository expert kernels', + 'candidate': 'public SDK wave64 integer-dot path', + 'formal_acceptance': False, 'results': results}, indent=2)) + print(json.dumps({'passed': True, + 'checks': checks, 'graph_checks': graphs, 'performance_cases': len(results)})) +if __name__ == '__main__': + main() diff --git a/tests/native/hygon_experts/expert_probe.cu b/tests/native/hygon_experts/expert_probe.cu new file mode 100644 index 000000000..11638bd9a --- /dev/null +++ b/tests/native/hygon_experts/expert_probe.cu @@ -0,0 +1,81 @@ +#include + +#include "native/cuda/hygon/ops/deepseek_v4_w8a8_experts/kernel.h" +template +int Run(const void* x, const float* weights, const int64_t* ids, + const int8_t* gate, const float* gate_scales, const int8_t* down, + const float* down_scales, void* output, void* workspace, size_t m, + size_t h, size_t inner, size_t experts, size_t topk, float limit, + int routed, float routed_scale, int optimized, cudaStream_t stream) { + using namespace infini::ops; + using namespace deepseek_v4_w8a8_detail; + const size_t routes = m * topk; + auto* activated = static_cast(workspace); + auto* xs = activated + routes * inner; + auto* acts = xs + m; + auto* qx = reinterpret_cast(acts + routes); + auto* qa = qx + m * h; + const auto grid = [](size_t n) { + return static_cast(n < 65535 ? n : 65535); + }; + if (optimized) { + const Device dev{Device::Type::kHygon, 0}; + const DataType dtype = + std::is_same_v ? DataType::kFloat32 : DataType::kBFloat16; + Tensor tx(const_cast(x), {m, h}, dtype, dev), + tw(const_cast(weights), {m, topk}, DataType::kFloat32, dev); + Tensor ti(const_cast(ids), {m, topk}, DataType::kInt64, dev); + Tensor tg(const_cast(gate), {experts, 2 * inner, h}, + DataType::kInt8, dev); + Tensor tgs(const_cast(gate_scales), {experts, 2 * inner, 1}, + DataType::kFloat32, dev); + Tensor td(const_cast(down), {experts, h, inner}, DataType::kInt8, + dev); + Tensor tds(const_cast(down_scales), {experts, h, 1}, + DataType::kFloat32, dev); + Tensor ty(output, {m, h}, dtype, dev); + Handle handle; + handle.set_stream(stream); + handle.set_workspace(workspace); + handle.set_workspace_size_in_bytes( + (routes * inner + m + routes) * sizeof(float) + m * h + routes * inner); + const auto rs = routed ? std::optional(routed_scale) : std::nullopt; + DeepseekV4W8a8Experts::Call( + handle, DeepseekV4W8a8Experts::DefaultConfig(dev.type()), tx, tw, ti, + tg, tgs, td, tds, double(limit), rs, ty); + } else { + Quantize<<>>( + static_cast(x), qx, xs, m, h); + GateUp<<>>( + qx, xs, ids, gate, gate_scales, activated, routes, topk, experts, h, + inner, limit, routed); + Quantize<<>>( + activated, qa, acts, routes, inner); + deepseek_v4_w8a8_detail::Down<<>>( + qa, acts, ids, weights, down, down_scales, static_cast(output), m, + topk, experts, h, inner, routed, routed_scale); + } + return static_cast(cudaGetLastError()); +} +extern "C" int run(const void* x, const float* weights, const int64_t* ids, + const int8_t* gate, const float* gate_scales, + const int8_t* down, const float* down_scales, void* output, + void* workspace, size_t m, size_t h, size_t inner, + size_t experts, size_t topk, float limit, int routed, + float routed_scale, int optimized, int bf16, void* stream) { + try { + if (bf16) + return Run<__nv_bfloat16>(x, weights, ids, gate, gate_scales, down, + down_scales, output, workspace, m, h, inner, + experts, topk, limit, routed, routed_scale, + optimized, static_cast(stream)); + return Run(x, weights, ids, gate, gate_scales, down, down_scales, + output, workspace, m, h, inner, experts, topk, limit, + routed, routed_scale, optimized, + static_cast(stream)); + } catch (const std::invalid_argument&) { + return 101; + } catch (...) { + return 102; + } +} From ed023dc0320075e62e55907013c77114fc471ac6 Mon Sep 17 00:00:00 2001 From: qin guoyong <305039273@qq.com> Date: Wed, 25 Nov 2026 08:26:14 +0800 Subject: [PATCH 17/18] perf(hygon): dispatch large low-precision matmul to BLAS Reuse the existing Hygon GEMM provider for eligible two-dimensional BF16/FP16 matrices. Preserve the generic implementation for small, batched, strided or overlapping cases, and initialize the BLAS handle before graph capture. Check address overlap at execution to respect metadata-based operator caching and rebound storage. Add layout, degenerate leading-dimension, changing-input graph and storage-rebinding regressions using the existing numerical tolerances. Keep HIP FP32 reference matmuls in IEEE mode to avoid PyTorch 2.10 TF32 state conflicts; other backends retain their existing precision setting. Server validation passed 132 Hygon and 57 CPU pytest cases, two batches of 150 public API checks plus 450 graph replays, and 60 native device graphs with 180 changing-input checks. Twelve accelerated scenarios exceeded +12% against the retained scalar implementation in five rounds of both batches. External reference and full indicator-9 acceptance remain incomplete; this is not an end-to-end performance claim. --- src/native/cuda/hygon/ops/matmul/kernel.h | 53 +++++++++++++-- tests/conftest.py | 2 + tests/test_matmul.py | 79 +++++++++++++++++++++++ 3 files changed, 129 insertions(+), 5 deletions(-) diff --git a/src/native/cuda/hygon/ops/matmul/kernel.h b/src/native/cuda/hygon/ops/matmul/kernel.h index 147e29166..1c5b3ca21 100644 --- a/src/native/cuda/hygon/ops/matmul/kernel.h +++ b/src/native/cuda/hygon/ops/matmul/kernel.h @@ -1,21 +1,64 @@ #ifndef INFINI_OPS_HYGON_MATMUL_KERNEL_H_ #define INFINI_OPS_HYGON_MATMUL_KERNEL_H_ - -#include +#include +#include #include "native/cuda/hygon/caster.cuh" +#include "native/cuda/hygon/ops/gemm/cublas.h" #include "native/cuda/hygon/runtime_.h" #include "native/cuda/ops/matmul/kernel.h" namespace infini::ops { - template <> class Operator : public CudaMatmul> { + using Base = CudaMatmul>; + using Fast = Operator; + public: - using CudaMatmul>::CudaMatmul; -}; + Operator(Tensor a, Tensor b, Tensor y) : Base(a, b, y) { + const bool reduced = + y.dtype() == DataType::kFloat16 || y.dtype() == DataType::kBFloat16; + if (reduced && Matrix(a) && Matrix(b) && Matrix(y) && a.size(0) >= 16 && + a.size(1) >= 512 && b.size(1) >= 512 && a.size(1) == b.size(0) && + y.size(0) == a.size(0) && y.size(1) == b.size(1)) { + fast_ = std::make_unique(a, b, y); + } + } + bool capture_safe() const override { return true; } + void operator()(Tensor a, Tensor b, Tensor y) const override { + if (fast_ && !Overlap(a, y) && !Overlap(b, y)) { + fast_->set_stream(stream_); + (*fast_)(a, b, y); + } else { + Base::operator()(a, b, y); + } + } + private: + static bool Matrix(const Tensor& t) { + if (t.shape().size() != 2) return false; + for (int i = 0; i < 2; ++i) + if (!t.size(i) || t.size(i) > INT_MAX || t.stride(i) <= 0 || + t.stride(i) > INT_MAX) + return false; + // The existing BLAS provider selects row-major whenever the last stride + // is one. A degenerate column-major view may have both strides equal to + // one; keep its valid generic path rather than supplying an invalid lda. + if (t.stride(1) == 1) return t.stride(0) >= t.size(1); + return t.stride(0) == 1 && t.stride(1) >= t.size(0); + } + static bool Overlap(const Tensor& a, const Tensor& b) { + const auto bytes = [](const Tensor& t) -> uintptr_t { + return 2 * (1 + (t.size(0) - 1) * t.stride(0) + + (t.size(1) - 1) * t.stride(1)); + }; + const auto ap = reinterpret_cast(a.data()); + const auto bp = reinterpret_cast(b.data()); + return ap <= bp ? bp - ap < bytes(a) : ap - bp < bytes(b); + } + std::unique_ptr fast_; +}; } // namespace infini::ops #endif diff --git a/tests/conftest.py b/tests/conftest.py index cfc2691c1..b247573df 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -48,6 +48,8 @@ def _host_range_profile_compiled(): def pytest_configure(config): torch.backends.fp32_precision = "tf32" + if torch.version.hip: + torch.backends.cuda.matmul.fp32_precision = "ieee" profile_output = config.getoption("--host-range-profile") config._infini_host_range_profile_output = None diff --git a/tests/test_matmul.py b/tests/test_matmul.py index 2a685e437..6e801b60a 100644 --- a/tests/test_matmul.py +++ b/tests/test_matmul.py @@ -10,6 +10,12 @@ "a_shape, b_shape, c_shape, a_strides, b_strides, c_strides", ( ((4, 64), (64, 32), (4, 32), None, None, None), + ((16, 512), (512, 512), (16, 512), None, None, None), + ((17, 513), (513, 513), (17, 513), None, None, None), + ((32, 1024), (1024, 512), (32, 512), None, None, None), + ((16, 512), (512, 512), (16, 512), (1, 16), (1, 512), (1, 16)), + ((16, 512), (512, 512), (16, 512), (1027, 2), (1027, 2), (1027, 2)), + ((1, 257), (257, 65), (1, 65), (1, 1), (1, 257), (1, 1)), ((4, 64), (64, 32), (4, 32), (64, 1), None, None), ((4, 64), (64, 32), (4, 32), None, (1, 64), None), ((4, 64), (64, 32), (4, 32), (64, 1), (1, 64), None), @@ -70,3 +76,76 @@ def _torch_matmul(a, b, c): c.copy_(result) return c + + +@pytest.mark.parametrize("dtype", (torch.float32, torch.float16, torch.bfloat16)) +@pytest.mark.parametrize( + "m, k, n", + ((1, 257, 65), (8, 512, 512), (16, 512, 512), + (17, 513, 513), (32, 1024, 512)), +) +@pytest.mark.parametrize("layout", ("row", "col", "padded", "strided", "unaligned")) +def test_matmul_graph_and_rebound_storage(device, dtype, m, k, n, layout): + if torch.device(device).type != "cuda": + pytest.skip("requires a CUDA-compatible graph backend") + + def allocate(rows, columns): + strides = { + "row": (columns, 1), + "col": (1, rows), + "padded": (columns + 7, 1), + "strided": (columns * 2 + 3, 2), + "unaligned": (columns, 1), + }[layout] + offset = int(layout == "unaligned") + span = 1 + (rows - 1) * strides[0] + (columns - 1) * strides[1] + storage = torch.full( + (span + offset + 8,), 7, dtype=dtype, device=device + ) + view = storage.as_strided((rows, columns), strides, offset) + + return view, storage + + stream = torch.cuda.Stream(device=device) + + with torch.cuda.stream(stream): + for _ in range(2): + a, _ = allocate(m, k) + b, _ = allocate(k, n) + y, storage = allocate(m, n) + a.normal_(0, 0.2) + b.normal_(0, 0.2) + + def run(): + infini.ops.matmul(a, b, y, stream=get_stream(a.device)) + + def check_result(): + reference = torch.matmul(a.float(), b.float()).to(dtype) + torch.testing.assert_close( + y, reference, rtol=1e-2, atol=1e-2 + ) + expected = torch.full_like(storage, 7) + expected.as_strided( + y.shape, y.stride(), y.storage_offset() + ).copy_(y) + assert torch.equal(storage, expected) + + run() + check_result() + graph = torch.cuda.CUDAGraph() + + with torch.cuda.graph(graph, stream=stream): + run() + + for replay in range(3): + if replay == 2: + a.zero_() + b.zero_() + else: + a.normal_(0, 0.2) + b.normal_(0, 0.2) + + graph.replay() + check_result() + + stream.synchronize() From 64d2a84ab5d2e34fb2c0d5202d745b57802063d4 Mon Sep 17 00:00:00 2001 From: qin guoyong <305039273@qq.com> Date: Wed, 25 Nov 2026 14:45:10 +0800 Subject: [PATCH 18/18] perf(hygon): accelerate small-batch low-precision matmul Extend the existing BLAS dispatch from 16 to 8 matrix rows, retaining single-row and unsupported-layout fallbacks and execution-time alias checks. Cover the new boundary with row/column/strided storage, changing-input graph replay and rebound-storage regression cases at the existing tolerances. Server validation: 177 Hygon Matmul, 72 CPU Matmul and 72 Hygon Linear tests passed. Two batches of five rounds passed all 12 added accelerated scenarios versus the prior published implementation; the lowest measured throughput improvement was 172.29%. 84 native device graphs passed 252 changing-input checks. Full metric-9 and model acceptance remain incomplete. --- src/native/cuda/hygon/ops/matmul/kernel.h | 2 +- tests/test_matmul.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/native/cuda/hygon/ops/matmul/kernel.h b/src/native/cuda/hygon/ops/matmul/kernel.h index 1c5b3ca21..40aaad07d 100644 --- a/src/native/cuda/hygon/ops/matmul/kernel.h +++ b/src/native/cuda/hygon/ops/matmul/kernel.h @@ -19,7 +19,7 @@ class Operator Operator(Tensor a, Tensor b, Tensor y) : Base(a, b, y) { const bool reduced = y.dtype() == DataType::kFloat16 || y.dtype() == DataType::kBFloat16; - if (reduced && Matrix(a) && Matrix(b) && Matrix(y) && a.size(0) >= 16 && + if (reduced && Matrix(a) && Matrix(b) && Matrix(y) && a.size(0) >= 8 && a.size(1) >= 512 && b.size(1) >= 512 && a.size(1) == b.size(0) && y.size(0) == a.size(0) && y.size(1) == b.size(1)) { fast_ = std::make_unique(a, b, y); diff --git a/tests/test_matmul.py b/tests/test_matmul.py index 6e801b60a..570481095 100644 --- a/tests/test_matmul.py +++ b/tests/test_matmul.py @@ -10,6 +10,11 @@ "a_shape, b_shape, c_shape, a_strides, b_strides, c_strides", ( ((4, 64), (64, 32), (4, 32), None, None, None), + ((8, 512), (512, 512), (8, 512), None, None, None), + ((9, 513), (513, 513), (9, 513), None, None, None), + ((15, 1024), (1024, 512), (15, 512), None, None, None), + ((8, 512), (512, 512), (8, 512), (1, 8), (1, 512), (1, 8)), + ((8, 512), (512, 512), (8, 512), (1027, 2), (1027, 2), (1027, 2)), ((16, 512), (512, 512), (16, 512), None, None, None), ((17, 513), (513, 513), (17, 513), None, None, None), ((32, 1024), (1024, 512), (32, 512), None, None, None), @@ -81,7 +86,8 @@ def _torch_matmul(a, b, c): @pytest.mark.parametrize("dtype", (torch.float32, torch.float16, torch.bfloat16)) @pytest.mark.parametrize( "m, k, n", - ((1, 257, 65), (8, 512, 512), (16, 512, 512), + ((1, 257, 65), (8, 512, 512), (9, 513, 513), + (15, 1024, 512), (16, 512, 512), (17, 513, 513), (32, 1024, 512)), ) @pytest.mark.parametrize("layout", ("row", "col", "padded", "strided", "unaligned"))