Kernels:
Trusted publisher
Uploaded using `kernel-builder` (batch 1/6).
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- build/torch211-cu128-aarch64-linux/_C.py +194 -0
- build/torch211-cu128-aarch64-linux/__init__.py +849 -0
- build/torch211-cu128-aarch64-linux/_deep_gemm_cuda_47ad41b.abi3.so +3 -0
- build/torch211-cu128-aarch64-linux/_ops.py +9 -0
- build/torch211-cu128-aarch64-linux/deep_gemm/__init__.py +26 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/comm/barrier.cuh +83 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/compile.cuh +18 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/cute_tie.cuh +50 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/epilogue_utils.cuh +27 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/exception.cuh +43 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/math.cuh +153 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/reduction.cuh +44 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/scheduler.cuh +288 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/sm100_utils.cuh +266 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/sm90_utils.cuh +332 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/tma_copy.cuh +92 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/tma_utils.cuh +116 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/types.cuh +43 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/types.hpp +41 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/common/utils.cuh +50 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/sm100_store_cd.cuh +137 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/sm100_store_cd_swap_ab.cuh +144 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/transform.cuh +24 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_bf16_gemm.cuh +437 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_bmk_bnk_mn.cuh +271 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp4_mqa_logits.cuh +457 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp4_paged_mqa_logits.cuh +510 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_fp4_gemm_1d1d.cuh +514 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_fp4_mega_moe.cuh +1380 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_gemm_1d1d.cuh +567 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_mqa_logits.cuh +403 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_paged_mqa_logits.cuh +439 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_tf32_hc_prenorm_gemm.cuh +350 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_bf16_gemm.cuh +388 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_bmk_bnk_mn.cuh +183 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_gemm_1d1d.cuh +346 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_gemm_1d2d.cuh +449 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_mqa_logits.cuh +330 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_paged_mqa_logits.cuh +334 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_tf32_hc_prenorm_gemm.cuh +294 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/smxx_clean_logits.cuh +74 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/smxx_layout.cuh +189 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/layout/mega_moe.cuh +260 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/layout/sym_buffer.cuh +41 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/mma/sm100.cuh +151 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/mma/sm90.cuh +293 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/ld_st.cuh +251 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/tcgen05.cuh +168 -0
- build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/tma.cuh +112 -0
.gitattributes
CHANGED
|
@@ -62,3 +62,4 @@ build/torch211-cu128-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs dif
|
|
| 62 |
build/torch211-cu130-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 63 |
build/torch212-cu130-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 64 |
build/torch212-cu132-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 62 |
build/torch211-cu130-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 63 |
build/torch212-cu130-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 64 |
build/torch212-cu132-x86_64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 65 |
+
build/torch211-cu128-aarch64-linux/_deep_gemm_cuda_47ad41b.abi3.so filter=lfs diff=lfs merge=lfs -text
|
build/torch211-cu128-aarch64-linux/_C.py
ADDED
|
@@ -0,0 +1,194 @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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from ._ops import ops
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def set_num_sms(num_sms: int):
|
| 7 |
+
ops.set_num_sms(num_sms)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_num_sms() -> int:
|
| 11 |
+
return ops.get_num_sms()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def set_tc_util(tc_util: int):
|
| 15 |
+
ops.set_tc_util(tc_util)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_tc_util() -> int:
|
| 19 |
+
return ops.get_tc_util()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def set_ignore_compile_dims(value: bool):
|
| 23 |
+
ops.set_ignore_compile_dims(value)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def set_block_size_multiple_of(value):
|
| 27 |
+
if isinstance(value, tuple):
|
| 28 |
+
block_m, block_n = value
|
| 29 |
+
else:
|
| 30 |
+
block_m = block_n = value
|
| 31 |
+
ops.set_block_size_multiple_of(block_m, block_n)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def set_pdl(enable_pdl: bool):
|
| 35 |
+
ops.set_pdl(enable_pdl)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_pdl() -> bool:
|
| 39 |
+
return ops.get_pdl()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def set_mk_alignment_for_contiguous_layout(value: int):
|
| 43 |
+
ops.set_mk_alignment_for_contiguous_layout(value)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def get_mk_alignment_for_contiguous_layout() -> int:
|
| 47 |
+
return ops.get_mk_alignment_for_contiguous_layout()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_theoretical_mk_alignment_for_contiguous_layout(expected_m=None) -> int:
|
| 51 |
+
return ops.get_theoretical_mk_alignment_for_contiguous_layout(
|
| 52 |
+
0 if expected_m is None else expected_m,
|
| 53 |
+
expected_m is not None,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_tma_aligned_size(mn: int, element_size: int) -> int:
|
| 58 |
+
return ops.get_tma_aligned_size(mn, element_size).item()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_mn_major_tma_aligned_tensor(sf):
|
| 62 |
+
return ops.get_mn_major_tma_aligned_tensor(sf)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def get_mn_major_tma_aligned_packed_ue8m0_tensor(sf):
|
| 66 |
+
return ops.get_mn_major_tma_aligned_packed_ue8m0_tensor(sf)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def get_k_grouped_mn_major_tma_aligned_packed_ue8m0_tensor(
|
| 70 |
+
sf, ks_tensor, ks, gran_k
|
| 71 |
+
):
|
| 72 |
+
ks_int = torch.tensor(ks, dtype=torch.int32, device="cpu")
|
| 73 |
+
return ops.get_k_grouped_mn_major_tma_aligned_packed_ue8m0_tensor(
|
| 74 |
+
sf, ks_tensor, ks_int, gran_k
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def transform_sf_into_required_layout(
|
| 79 |
+
sf,
|
| 80 |
+
mn,
|
| 81 |
+
k,
|
| 82 |
+
recipe,
|
| 83 |
+
num_groups=None,
|
| 84 |
+
is_sfa=None,
|
| 85 |
+
disable_ue8m0_cast=False,
|
| 86 |
+
):
|
| 87 |
+
if len(recipe) == 3:
|
| 88 |
+
r0, r1, r2 = recipe
|
| 89 |
+
recipe_len = 3
|
| 90 |
+
elif len(recipe) == 2:
|
| 91 |
+
r0, r1 = recipe
|
| 92 |
+
r2 = 0
|
| 93 |
+
recipe_len = 2
|
| 94 |
+
else:
|
| 95 |
+
raise ValueError("recipe must have length 2 or 3")
|
| 96 |
+
|
| 97 |
+
return ops.transform_sf_into_required_layout(
|
| 98 |
+
sf,
|
| 99 |
+
mn,
|
| 100 |
+
k,
|
| 101 |
+
r0,
|
| 102 |
+
r1,
|
| 103 |
+
r2,
|
| 104 |
+
recipe_len,
|
| 105 |
+
0 if num_groups is None else num_groups,
|
| 106 |
+
num_groups is not None,
|
| 107 |
+
False if is_sfa is None else is_sfa,
|
| 108 |
+
is_sfa is not None,
|
| 109 |
+
disable_ue8m0_cast,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def get_token_alignment_for_mega_moe() -> int:
|
| 114 |
+
return ops.get_token_alignment_for_mega_moe()
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def get_symm_buffer_size_for_mega_moe(
|
| 118 |
+
num_ranks,
|
| 119 |
+
num_experts,
|
| 120 |
+
num_max_tokens_per_rank,
|
| 121 |
+
num_topk,
|
| 122 |
+
hidden,
|
| 123 |
+
intermediate_hidden,
|
| 124 |
+
use_fp8_dispatch=True,
|
| 125 |
+
activation="swiglu",
|
| 126 |
+
):
|
| 127 |
+
num_bytes = ops.get_symm_buffer_size_for_mega_moe(
|
| 128 |
+
num_ranks,
|
| 129 |
+
num_experts,
|
| 130 |
+
num_max_tokens_per_rank,
|
| 131 |
+
num_topk,
|
| 132 |
+
hidden,
|
| 133 |
+
intermediate_hidden,
|
| 134 |
+
use_fp8_dispatch,
|
| 135 |
+
activation,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
def slice_input_buffers(buffer):
|
| 139 |
+
return tuple(
|
| 140 |
+
ops.get_symm_buffer_views_for_mega_moe(
|
| 141 |
+
buffer,
|
| 142 |
+
num_ranks,
|
| 143 |
+
num_experts,
|
| 144 |
+
num_max_tokens_per_rank,
|
| 145 |
+
num_topk,
|
| 146 |
+
hidden,
|
| 147 |
+
intermediate_hidden,
|
| 148 |
+
use_fp8_dispatch,
|
| 149 |
+
activation,
|
| 150 |
+
)
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
return num_bytes, slice_input_buffers
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def fp8_fp4_mega_moe(
|
| 157 |
+
y,
|
| 158 |
+
l1_weights,
|
| 159 |
+
l2_weights,
|
| 160 |
+
cumulative_local_expert_recv_stats,
|
| 161 |
+
sym_buffer,
|
| 162 |
+
sym_buffer_ptrs,
|
| 163 |
+
rank_idx,
|
| 164 |
+
num_max_tokens_per_rank,
|
| 165 |
+
num_experts,
|
| 166 |
+
num_topk,
|
| 167 |
+
recipe,
|
| 168 |
+
activation,
|
| 169 |
+
activation_clamp,
|
| 170 |
+
fast_math,
|
| 171 |
+
):
|
| 172 |
+
l1_weights_data, l1_weights_sf = l1_weights
|
| 173 |
+
l2_weights_data, l2_weights_sf = l2_weights
|
| 174 |
+
r0, r1, r2 = recipe
|
| 175 |
+
ops.fp8_fp4_mega_moe(
|
| 176 |
+
y,
|
| 177 |
+
l1_weights_data,
|
| 178 |
+
l1_weights_sf,
|
| 179 |
+
l2_weights_data,
|
| 180 |
+
l2_weights_sf,
|
| 181 |
+
cumulative_local_expert_recv_stats,
|
| 182 |
+
sym_buffer,
|
| 183 |
+
sym_buffer_ptrs,
|
| 184 |
+
rank_idx,
|
| 185 |
+
num_max_tokens_per_rank,
|
| 186 |
+
num_experts,
|
| 187 |
+
num_topk,
|
| 188 |
+
r0,
|
| 189 |
+
r1,
|
| 190 |
+
r2,
|
| 191 |
+
activation,
|
| 192 |
+
activation_clamp,
|
| 193 |
+
fast_math,
|
| 194 |
+
)
|
build/torch211-cu128-aarch64-linux/__init__.py
ADDED
|
@@ -0,0 +1,849 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sysconfig
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Avoid holding a CUDA tensor in DeepGEMM's process-lifetime runtime singleton.
|
| 7 |
+
# In packaged/lazy-loaded use, that can outlive PyTorch's CUDA teardown and crash
|
| 8 |
+
# during interpreter shutdown.
|
| 9 |
+
os.environ.setdefault("DG_USE_TEMP_CUBLASLT_WORKSPACE", "1")
|
| 10 |
+
|
| 11 |
+
# Import the compiled extension
|
| 12 |
+
from ._ops import ops as _ops, add_op_namespace_prefix
|
| 13 |
+
from . import utils
|
| 14 |
+
|
| 15 |
+
__version__ = "2.5.0"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# ── Register fake tensor implementations for torch.compile ──────────────────
|
| 19 |
+
# All GEMM ops mutate the output tensor `d` in-place and return void.
|
| 20 |
+
# The fake implementations are no-ops since `d` is pre-allocated by the caller.
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
for _op in [
|
| 24 |
+
"fp8_fp4_gemm_nt",
|
| 25 |
+
"fp8_fp4_gemm_nn",
|
| 26 |
+
"fp8_fp4_gemm_tn",
|
| 27 |
+
"fp8_fp4_gemm_tt",
|
| 28 |
+
"m_grouped_fp8_fp4_gemm_nt_contiguous",
|
| 29 |
+
"m_grouped_fp8_fp4_gemm_nn_contiguous",
|
| 30 |
+
"m_grouped_fp8_fp4_gemm_nt_masked",
|
| 31 |
+
"k_grouped_fp8_gemm_nt_contiguous",
|
| 32 |
+
"k_grouped_fp8_gemm_tn_contiguous",
|
| 33 |
+
"bf16_gemm_nt",
|
| 34 |
+
"bf16_gemm_nn",
|
| 35 |
+
"bf16_gemm_tn",
|
| 36 |
+
"bf16_gemm_tt",
|
| 37 |
+
"m_grouped_bf16_gemm_nt_contiguous",
|
| 38 |
+
"m_grouped_bf16_gemm_nn_contiguous",
|
| 39 |
+
"m_grouped_bf16_gemm_nt_masked",
|
| 40 |
+
"fp8_gemm_nt_skip_head_mid",
|
| 41 |
+
"fp8_fp4_mega_moe",
|
| 42 |
+
]:
|
| 43 |
+
|
| 44 |
+
@torch.library.register_fake(add_op_namespace_prefix(_op))
|
| 45 |
+
def _fake(*args, **kwargs):
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Runtime
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def set_num_sms(num_sms: int):
|
| 53 |
+
ops.set_num_sms(num_sms)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_num_sms() -> int:
|
| 57 |
+
return ops.get_num_sms()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def set_tc_util(tc_util: int):
|
| 61 |
+
ops.set_tc_util(tc_util)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_tc_util() -> int:
|
| 65 |
+
return ops.get_tc_util()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def set_ignore_compile_dims(value: bool):
|
| 69 |
+
ops.set_ignore_compile_dims(value)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def set_block_size_multiple_of(value):
|
| 73 |
+
if isinstance(value, tuple):
|
| 74 |
+
block_m, block_n = value
|
| 75 |
+
else:
|
| 76 |
+
block_m = block_n = value
|
| 77 |
+
ops.set_block_size_multiple_of(block_m, block_n)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def set_pdl(enable_pdl: bool):
|
| 81 |
+
ops.set_pdl(enable_pdl)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def get_pdl() -> bool:
|
| 85 |
+
return ops.get_pdl()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def set_mk_alignment_for_contiguous_layout(alignment: int):
|
| 89 |
+
ops.set_mk_alignment_for_contiguous_layout(alignment)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def get_mk_alignment_for_contiguous_layout() -> int:
|
| 93 |
+
return ops.get_mk_alignment_for_contiguous_layout()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def get_theoretical_mk_alignment_for_contiguous_layout(expected_m=None) -> int:
|
| 97 |
+
return ops.get_theoretical_mk_alignment_for_contiguous_layout(
|
| 98 |
+
0 if expected_m is None else expected_m,
|
| 99 |
+
expected_m is not None,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# Layout utilities
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def get_tma_aligned_size(mn: int, element_size: int) -> int:
|
| 107 |
+
return ops.get_tma_aligned_size(mn, element_size).item()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def get_mn_major_tma_aligned_tensor(sf):
|
| 111 |
+
return ops.get_mn_major_tma_aligned_tensor(sf)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def get_mn_major_tma_aligned_packed_ue8m0_tensor(sf):
|
| 115 |
+
return ops.get_mn_major_tma_aligned_packed_ue8m0_tensor(sf)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def get_k_grouped_mn_major_tma_aligned_packed_ue8m0_tensor(
|
| 119 |
+
sf, ks_tensor, ks, gran_k
|
| 120 |
+
):
|
| 121 |
+
ks_int = torch.tensor(ks, dtype=torch.int32, device="cpu")
|
| 122 |
+
return ops.get_k_grouped_mn_major_tma_aligned_packed_ue8m0_tensor(
|
| 123 |
+
sf, ks_tensor, ks_int, gran_k
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def transform_sf_into_required_layout(
|
| 128 |
+
sf,
|
| 129 |
+
mn,
|
| 130 |
+
k,
|
| 131 |
+
recipe,
|
| 132 |
+
num_groups=None,
|
| 133 |
+
is_sfa=None,
|
| 134 |
+
disable_ue8m0_cast=False,
|
| 135 |
+
):
|
| 136 |
+
if len(recipe) == 3:
|
| 137 |
+
r0, r1, r2 = recipe
|
| 138 |
+
recipe_len = 3
|
| 139 |
+
elif len(recipe) == 2:
|
| 140 |
+
r0, r1 = recipe
|
| 141 |
+
r2 = 0
|
| 142 |
+
recipe_len = 2
|
| 143 |
+
else:
|
| 144 |
+
raise ValueError("recipe must have length 2 or 3")
|
| 145 |
+
has_ng = num_groups is not None
|
| 146 |
+
ng = num_groups if has_ng else 0
|
| 147 |
+
return ops.transform_sf_into_required_layout(
|
| 148 |
+
sf,
|
| 149 |
+
mn,
|
| 150 |
+
k,
|
| 151 |
+
r0,
|
| 152 |
+
r1,
|
| 153 |
+
r2,
|
| 154 |
+
recipe_len,
|
| 155 |
+
ng,
|
| 156 |
+
has_ng,
|
| 157 |
+
False if is_sfa is None else is_sfa,
|
| 158 |
+
is_sfa is not None,
|
| 159 |
+
disable_ue8m0_cast,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# Aliases for contiguous layout alignment
|
| 164 |
+
get_m_alignment_for_contiguous_layout = get_mk_alignment_for_contiguous_layout
|
| 165 |
+
get_k_alignment_for_contiguous_layout = get_mk_alignment_for_contiguous_layout
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# Helper to flatten recipe args
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _flatten_recipe(recipe, recipe_a=None, recipe_b=None):
|
| 172 |
+
has_recipe = recipe is not None
|
| 173 |
+
r0, r1, r2 = recipe if has_recipe else (0, 0, 0)
|
| 174 |
+
has_ra = recipe_a is not None
|
| 175 |
+
ra0, ra1 = recipe_a if has_ra else (0, 0)
|
| 176 |
+
has_rb = recipe_b is not None
|
| 177 |
+
rb0, rb1 = recipe_b if has_rb else (0, 0)
|
| 178 |
+
return r0, r1, r2, has_recipe, ra0, ra1, has_ra, rb0, rb1, has_rb
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# FP8/FP4 GEMM ops
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def fp8_fp4_gemm_nt(
|
| 185 |
+
a,
|
| 186 |
+
b,
|
| 187 |
+
d,
|
| 188 |
+
c=None,
|
| 189 |
+
recipe=None,
|
| 190 |
+
recipe_a=None,
|
| 191 |
+
recipe_b=None,
|
| 192 |
+
compiled_dims="nk",
|
| 193 |
+
disable_ue8m0_cast=False,
|
| 194 |
+
):
|
| 195 |
+
a_data, a_sf = a
|
| 196 |
+
b_data, b_sf = b
|
| 197 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 198 |
+
recipe, recipe_a, recipe_b
|
| 199 |
+
)
|
| 200 |
+
ops.fp8_fp4_gemm_nt(
|
| 201 |
+
a_data,
|
| 202 |
+
a_sf,
|
| 203 |
+
b_data,
|
| 204 |
+
b_sf,
|
| 205 |
+
d,
|
| 206 |
+
c,
|
| 207 |
+
r0,
|
| 208 |
+
r1,
|
| 209 |
+
r2,
|
| 210 |
+
hr,
|
| 211 |
+
ra0,
|
| 212 |
+
ra1,
|
| 213 |
+
hra,
|
| 214 |
+
rb0,
|
| 215 |
+
rb1,
|
| 216 |
+
hrb,
|
| 217 |
+
compiled_dims,
|
| 218 |
+
disable_ue8m0_cast,
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def fp8_fp4_gemm_nn(
|
| 223 |
+
a,
|
| 224 |
+
b,
|
| 225 |
+
d,
|
| 226 |
+
c=None,
|
| 227 |
+
recipe=None,
|
| 228 |
+
recipe_a=None,
|
| 229 |
+
recipe_b=None,
|
| 230 |
+
compiled_dims="nk",
|
| 231 |
+
disable_ue8m0_cast=False,
|
| 232 |
+
):
|
| 233 |
+
a_data, a_sf = a
|
| 234 |
+
b_data, b_sf = b
|
| 235 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 236 |
+
recipe, recipe_a, recipe_b
|
| 237 |
+
)
|
| 238 |
+
ops.fp8_fp4_gemm_nn(
|
| 239 |
+
a_data,
|
| 240 |
+
a_sf,
|
| 241 |
+
b_data,
|
| 242 |
+
b_sf,
|
| 243 |
+
d,
|
| 244 |
+
c,
|
| 245 |
+
r0,
|
| 246 |
+
r1,
|
| 247 |
+
r2,
|
| 248 |
+
hr,
|
| 249 |
+
ra0,
|
| 250 |
+
ra1,
|
| 251 |
+
hra,
|
| 252 |
+
rb0,
|
| 253 |
+
rb1,
|
| 254 |
+
hrb,
|
| 255 |
+
compiled_dims,
|
| 256 |
+
disable_ue8m0_cast,
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
def fp8_fp4_gemm_tn(
|
| 261 |
+
a,
|
| 262 |
+
b,
|
| 263 |
+
d,
|
| 264 |
+
c=None,
|
| 265 |
+
recipe=None,
|
| 266 |
+
recipe_a=None,
|
| 267 |
+
recipe_b=None,
|
| 268 |
+
compiled_dims="mn",
|
| 269 |
+
disable_ue8m0_cast=False,
|
| 270 |
+
):
|
| 271 |
+
a_data, a_sf = a
|
| 272 |
+
b_data, b_sf = b
|
| 273 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 274 |
+
recipe, recipe_a, recipe_b
|
| 275 |
+
)
|
| 276 |
+
ops.fp8_fp4_gemm_tn(
|
| 277 |
+
a_data,
|
| 278 |
+
a_sf,
|
| 279 |
+
b_data,
|
| 280 |
+
b_sf,
|
| 281 |
+
d,
|
| 282 |
+
c,
|
| 283 |
+
r0,
|
| 284 |
+
r1,
|
| 285 |
+
r2,
|
| 286 |
+
hr,
|
| 287 |
+
ra0,
|
| 288 |
+
ra1,
|
| 289 |
+
hra,
|
| 290 |
+
rb0,
|
| 291 |
+
rb1,
|
| 292 |
+
hrb,
|
| 293 |
+
compiled_dims,
|
| 294 |
+
disable_ue8m0_cast,
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def fp8_fp4_gemm_tt(
|
| 299 |
+
a,
|
| 300 |
+
b,
|
| 301 |
+
d,
|
| 302 |
+
c=None,
|
| 303 |
+
recipe=None,
|
| 304 |
+
recipe_a=None,
|
| 305 |
+
recipe_b=None,
|
| 306 |
+
compiled_dims="mn",
|
| 307 |
+
disable_ue8m0_cast=False,
|
| 308 |
+
):
|
| 309 |
+
a_data, a_sf = a
|
| 310 |
+
b_data, b_sf = b
|
| 311 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 312 |
+
recipe, recipe_a, recipe_b
|
| 313 |
+
)
|
| 314 |
+
ops.fp8_fp4_gemm_tt(
|
| 315 |
+
a_data,
|
| 316 |
+
a_sf,
|
| 317 |
+
b_data,
|
| 318 |
+
b_sf,
|
| 319 |
+
d,
|
| 320 |
+
c,
|
| 321 |
+
r0,
|
| 322 |
+
r1,
|
| 323 |
+
r2,
|
| 324 |
+
hr,
|
| 325 |
+
ra0,
|
| 326 |
+
ra1,
|
| 327 |
+
hra,
|
| 328 |
+
rb0,
|
| 329 |
+
rb1,
|
| 330 |
+
hrb,
|
| 331 |
+
compiled_dims,
|
| 332 |
+
disable_ue8m0_cast,
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
# FP8 aliases (same as FP8/FP4)
|
| 337 |
+
fp8_gemm_nt = fp8_fp4_gemm_nt
|
| 338 |
+
fp8_gemm_nn = fp8_fp4_gemm_nn
|
| 339 |
+
fp8_gemm_tn = fp8_fp4_gemm_tn
|
| 340 |
+
fp8_gemm_tt = fp8_fp4_gemm_tt
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
# M-grouped FP8/FP4 GEMM ops
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def m_grouped_fp8_fp4_gemm_nt_contiguous(
|
| 347 |
+
a,
|
| 348 |
+
b,
|
| 349 |
+
d,
|
| 350 |
+
grouped_layout,
|
| 351 |
+
recipe=None,
|
| 352 |
+
recipe_a=None,
|
| 353 |
+
recipe_b=None,
|
| 354 |
+
compiled_dims="nk",
|
| 355 |
+
disable_ue8m0_cast=False,
|
| 356 |
+
use_psum_layout=False,
|
| 357 |
+
expected_m_for_psum_layout=None,
|
| 358 |
+
):
|
| 359 |
+
a_data, a_sf = a
|
| 360 |
+
b_data, b_sf = b
|
| 361 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 362 |
+
recipe, recipe_a, recipe_b
|
| 363 |
+
)
|
| 364 |
+
has_em = expected_m_for_psum_layout is not None
|
| 365 |
+
em = expected_m_for_psum_layout if has_em else 0
|
| 366 |
+
ops.m_grouped_fp8_fp4_gemm_nt_contiguous(
|
| 367 |
+
a_data,
|
| 368 |
+
a_sf,
|
| 369 |
+
b_data,
|
| 370 |
+
b_sf,
|
| 371 |
+
d,
|
| 372 |
+
grouped_layout,
|
| 373 |
+
r0,
|
| 374 |
+
r1,
|
| 375 |
+
r2,
|
| 376 |
+
hr,
|
| 377 |
+
ra0,
|
| 378 |
+
ra1,
|
| 379 |
+
hra,
|
| 380 |
+
rb0,
|
| 381 |
+
rb1,
|
| 382 |
+
hrb,
|
| 383 |
+
compiled_dims,
|
| 384 |
+
disable_ue8m0_cast,
|
| 385 |
+
use_psum_layout,
|
| 386 |
+
em,
|
| 387 |
+
has_em,
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def m_grouped_fp8_fp4_gemm_nn_contiguous(
|
| 392 |
+
a,
|
| 393 |
+
b,
|
| 394 |
+
d,
|
| 395 |
+
grouped_layout,
|
| 396 |
+
recipe=None,
|
| 397 |
+
recipe_a=None,
|
| 398 |
+
recipe_b=None,
|
| 399 |
+
compiled_dims="nk",
|
| 400 |
+
disable_ue8m0_cast=False,
|
| 401 |
+
use_psum_layout=False,
|
| 402 |
+
):
|
| 403 |
+
a_data, a_sf = a
|
| 404 |
+
b_data, b_sf = b
|
| 405 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 406 |
+
recipe, recipe_a, recipe_b
|
| 407 |
+
)
|
| 408 |
+
ops.m_grouped_fp8_fp4_gemm_nn_contiguous(
|
| 409 |
+
a_data,
|
| 410 |
+
a_sf,
|
| 411 |
+
b_data,
|
| 412 |
+
b_sf,
|
| 413 |
+
d,
|
| 414 |
+
grouped_layout,
|
| 415 |
+
r0,
|
| 416 |
+
r1,
|
| 417 |
+
r2,
|
| 418 |
+
hr,
|
| 419 |
+
ra0,
|
| 420 |
+
ra1,
|
| 421 |
+
hra,
|
| 422 |
+
rb0,
|
| 423 |
+
rb1,
|
| 424 |
+
hrb,
|
| 425 |
+
compiled_dims,
|
| 426 |
+
disable_ue8m0_cast,
|
| 427 |
+
use_psum_layout,
|
| 428 |
+
)
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
def m_grouped_fp8_fp4_gemm_nt_masked(
|
| 432 |
+
a,
|
| 433 |
+
b,
|
| 434 |
+
d,
|
| 435 |
+
masked_m,
|
| 436 |
+
expected_m,
|
| 437 |
+
recipe=None,
|
| 438 |
+
recipe_a=None,
|
| 439 |
+
recipe_b=None,
|
| 440 |
+
compiled_dims="nk",
|
| 441 |
+
disable_ue8m0_cast=False,
|
| 442 |
+
):
|
| 443 |
+
a_data, a_sf = a
|
| 444 |
+
b_data, b_sf = b
|
| 445 |
+
r0, r1, r2, hr, ra0, ra1, hra, rb0, rb1, hrb = _flatten_recipe(
|
| 446 |
+
recipe, recipe_a, recipe_b
|
| 447 |
+
)
|
| 448 |
+
ops.m_grouped_fp8_fp4_gemm_nt_masked(
|
| 449 |
+
a_data,
|
| 450 |
+
a_sf,
|
| 451 |
+
b_data,
|
| 452 |
+
b_sf,
|
| 453 |
+
d,
|
| 454 |
+
masked_m,
|
| 455 |
+
expected_m,
|
| 456 |
+
r0,
|
| 457 |
+
r1,
|
| 458 |
+
r2,
|
| 459 |
+
hr,
|
| 460 |
+
ra0,
|
| 461 |
+
ra1,
|
| 462 |
+
hra,
|
| 463 |
+
rb0,
|
| 464 |
+
rb1,
|
| 465 |
+
hrb,
|
| 466 |
+
compiled_dims,
|
| 467 |
+
disable_ue8m0_cast,
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
# M-grouped FP8 aliases
|
| 472 |
+
m_grouped_fp8_gemm_nt_contiguous = m_grouped_fp8_fp4_gemm_nt_contiguous
|
| 473 |
+
m_grouped_fp8_gemm_nn_contiguous = m_grouped_fp8_fp4_gemm_nn_contiguous
|
| 474 |
+
m_grouped_fp8_gemm_nt_masked = m_grouped_fp8_fp4_gemm_nt_masked
|
| 475 |
+
|
| 476 |
+
# Legacy aliases
|
| 477 |
+
fp8_m_grouped_gemm_nt_masked = m_grouped_fp8_fp4_gemm_nt_masked
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
# K-grouped FP8 GEMM ops
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
def k_grouped_fp8_gemm_tn_contiguous(
|
| 484 |
+
a, b, d, ks, ks_tensor, c=None, recipe=(1, 1, 128), compiled_dims="mn"
|
| 485 |
+
):
|
| 486 |
+
a_data, a_sf = a
|
| 487 |
+
b_data, b_sf = b
|
| 488 |
+
r0, r1, r2 = recipe
|
| 489 |
+
ops.k_grouped_fp8_gemm_tn_contiguous(
|
| 490 |
+
a_data, a_sf, b_data, b_sf, d, ks_tensor, c, r0, r1, r2, compiled_dims
|
| 491 |
+
)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
def k_grouped_fp8_gemm_nt_contiguous(
|
| 495 |
+
a, b, d, ks, ks_tensor, c=None, recipe=(1, 1, 128), compiled_dims="mn"
|
| 496 |
+
):
|
| 497 |
+
a_data, a_sf = a
|
| 498 |
+
b_data, b_sf = b
|
| 499 |
+
r0, r1, r2 = recipe
|
| 500 |
+
ops.k_grouped_fp8_gemm_nt_contiguous(
|
| 501 |
+
a_data, a_sf, b_data, b_sf, d, ks_tensor, c, r0, r1, r2, compiled_dims
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
# BF16 GEMM ops
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def bf16_gemm_nt(a, b, d, c=None, compiled_dims="nk"):
|
| 509 |
+
ops.bf16_gemm_nt(a, b, d, c, compiled_dims)
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
def bf16_gemm_nn(a, b, d, c=None, compiled_dims="nk"):
|
| 513 |
+
ops.bf16_gemm_nn(a, b, d, c, compiled_dims)
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
def bf16_gemm_tn(a, b, d, c=None, compiled_dims="mn"):
|
| 517 |
+
ops.bf16_gemm_tn(a, b, d, c, compiled_dims)
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def bf16_gemm_tt(a, b, d, c=None, compiled_dims="mn"):
|
| 521 |
+
ops.bf16_gemm_tt(a, b, d, c, compiled_dims)
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
# M-grouped BF16 GEMM ops
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
def m_grouped_bf16_gemm_nt_contiguous(
|
| 528 |
+
a,
|
| 529 |
+
b,
|
| 530 |
+
d,
|
| 531 |
+
grouped_layout,
|
| 532 |
+
compiled_dims="nk",
|
| 533 |
+
use_psum_layout=False,
|
| 534 |
+
expected_m_for_psum_layout=None,
|
| 535 |
+
):
|
| 536 |
+
has_em = expected_m_for_psum_layout is not None
|
| 537 |
+
em = expected_m_for_psum_layout if has_em else 0
|
| 538 |
+
ops.m_grouped_bf16_gemm_nt_contiguous(
|
| 539 |
+
a, b, d, grouped_layout, compiled_dims, use_psum_layout, em, has_em
|
| 540 |
+
)
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
def m_grouped_bf16_gemm_nn_contiguous(
|
| 544 |
+
a, b, d, grouped_layout, compiled_dims="nk", use_psum_layout=False
|
| 545 |
+
):
|
| 546 |
+
ops.m_grouped_bf16_gemm_nn_contiguous(
|
| 547 |
+
a, b, d, grouped_layout, compiled_dims, use_psum_layout
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
def m_grouped_bf16_gemm_nt_masked(a, b, d, masked_m, expected_m, compiled_dims="nk"):
|
| 552 |
+
ops.m_grouped_bf16_gemm_nt_masked(a, b, d, masked_m, expected_m, compiled_dims)
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
# Legacy alias
|
| 556 |
+
bf16_m_grouped_gemm_nt_masked = m_grouped_bf16_gemm_nt_masked
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
# K-grouped BF16 GEMM ops
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
def k_grouped_bf16_gemm_tn_contiguous(
|
| 563 |
+
a, b, d, ks, ks_tensor, c=None, compiled_dims="mn"
|
| 564 |
+
):
|
| 565 |
+
ops.k_grouped_bf16_gemm_tn_contiguous(a, b, d, ks_tensor, c, compiled_dims)
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
# cuBLASLt GEMM ops
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
def cublaslt_gemm_nt(a, b, d, c=None):
|
| 572 |
+
ops.cublaslt_gemm_nt(a, b, d, c)
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def cublaslt_gemm_nn(a, b, d, c=None):
|
| 576 |
+
ops.cublaslt_gemm_nn(a, b, d, c)
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def cublaslt_gemm_tn(a, b, d, c=None):
|
| 580 |
+
ops.cublaslt_gemm_tn(a, b, d, c)
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
def cublaslt_gemm_tt(a, b, d, c=None):
|
| 584 |
+
ops.cublaslt_gemm_tt(a, b, d, c)
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
# Attention ops
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
def fp8_gemm_nt_skip_head_mid(
|
| 591 |
+
a, b, d, head_splits, recipe=None, compiled_dims="nk", disable_ue8m0_cast=False
|
| 592 |
+
):
|
| 593 |
+
a_data, a_sf = a
|
| 594 |
+
b_data, b_sf = b
|
| 595 |
+
left, mid, right = head_splits
|
| 596 |
+
has_recipe = recipe is not None
|
| 597 |
+
r0, r1, r2 = recipe if has_recipe else (0, 0, 0)
|
| 598 |
+
ops.fp8_gemm_nt_skip_head_mid(
|
| 599 |
+
a_data,
|
| 600 |
+
a_sf,
|
| 601 |
+
b_data,
|
| 602 |
+
b_sf,
|
| 603 |
+
d,
|
| 604 |
+
left,
|
| 605 |
+
mid,
|
| 606 |
+
right,
|
| 607 |
+
r0,
|
| 608 |
+
r1,
|
| 609 |
+
r2,
|
| 610 |
+
has_recipe,
|
| 611 |
+
compiled_dims,
|
| 612 |
+
disable_ue8m0_cast,
|
| 613 |
+
)
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
def fp8_mqa_logits(
|
| 617 |
+
q,
|
| 618 |
+
kv,
|
| 619 |
+
weights,
|
| 620 |
+
cu_seq_len_k_start,
|
| 621 |
+
cu_seq_len_k_end,
|
| 622 |
+
clean_logits=True,
|
| 623 |
+
max_seqlen_k=0,
|
| 624 |
+
):
|
| 625 |
+
kv_data, kv_sf = kv
|
| 626 |
+
return ops.fp8_mqa_logits(
|
| 627 |
+
q,
|
| 628 |
+
kv_data,
|
| 629 |
+
kv_sf,
|
| 630 |
+
weights,
|
| 631 |
+
cu_seq_len_k_start,
|
| 632 |
+
cu_seq_len_k_end,
|
| 633 |
+
clean_logits,
|
| 634 |
+
max_seqlen_k,
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
|
| 638 |
+
def fp8_fp4_mqa_logits(
|
| 639 |
+
q,
|
| 640 |
+
kv,
|
| 641 |
+
weights,
|
| 642 |
+
cu_seq_len_k_start,
|
| 643 |
+
cu_seq_len_k_end,
|
| 644 |
+
clean_logits=True,
|
| 645 |
+
max_seqlen_k=0,
|
| 646 |
+
logits_dtype=torch.float32,
|
| 647 |
+
):
|
| 648 |
+
if isinstance(q, tuple):
|
| 649 |
+
q_data, q_sf = q
|
| 650 |
+
else:
|
| 651 |
+
q_data, q_sf = q, None
|
| 652 |
+
kv_data, kv_sf = kv
|
| 653 |
+
return ops.fp8_fp4_mqa_logits(
|
| 654 |
+
q_data,
|
| 655 |
+
q_sf,
|
| 656 |
+
kv_data,
|
| 657 |
+
kv_sf,
|
| 658 |
+
weights,
|
| 659 |
+
cu_seq_len_k_start,
|
| 660 |
+
cu_seq_len_k_end,
|
| 661 |
+
clean_logits,
|
| 662 |
+
max_seqlen_k,
|
| 663 |
+
logits_dtype,
|
| 664 |
+
)
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
def get_paged_mqa_logits_metadata(context_lens, block_kv, num_sms, indices=None):
|
| 668 |
+
return ops.get_paged_mqa_logits_metadata(context_lens, block_kv, num_sms, indices)
|
| 669 |
+
|
| 670 |
+
|
| 671 |
+
def fp8_paged_mqa_logits(
|
| 672 |
+
q,
|
| 673 |
+
kv_cache,
|
| 674 |
+
weights,
|
| 675 |
+
context_lens,
|
| 676 |
+
block_table,
|
| 677 |
+
schedule_meta,
|
| 678 |
+
max_context_len,
|
| 679 |
+
clean_logits=False,
|
| 680 |
+
indices=None,
|
| 681 |
+
):
|
| 682 |
+
return ops.fp8_paged_mqa_logits(
|
| 683 |
+
q,
|
| 684 |
+
kv_cache,
|
| 685 |
+
weights,
|
| 686 |
+
context_lens,
|
| 687 |
+
block_table,
|
| 688 |
+
schedule_meta,
|
| 689 |
+
max_context_len,
|
| 690 |
+
clean_logits,
|
| 691 |
+
indices,
|
| 692 |
+
)
|
| 693 |
+
|
| 694 |
+
|
| 695 |
+
def fp8_fp4_paged_mqa_logits(
|
| 696 |
+
q,
|
| 697 |
+
kv_cache,
|
| 698 |
+
weights,
|
| 699 |
+
context_lens,
|
| 700 |
+
block_table,
|
| 701 |
+
schedule_meta,
|
| 702 |
+
max_context_len,
|
| 703 |
+
clean_logits=False,
|
| 704 |
+
logits_dtype=torch.float32,
|
| 705 |
+
indices=None,
|
| 706 |
+
):
|
| 707 |
+
if isinstance(q, tuple):
|
| 708 |
+
q_data, q_sf = q
|
| 709 |
+
else:
|
| 710 |
+
q_data, q_sf = q, None
|
| 711 |
+
return ops.fp8_fp4_paged_mqa_logits(
|
| 712 |
+
q_data,
|
| 713 |
+
q_sf,
|
| 714 |
+
kv_cache,
|
| 715 |
+
weights,
|
| 716 |
+
context_lens,
|
| 717 |
+
block_table,
|
| 718 |
+
schedule_meta,
|
| 719 |
+
max_context_len,
|
| 720 |
+
clean_logits,
|
| 721 |
+
logits_dtype,
|
| 722 |
+
indices,
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
# Einsum ops
|
| 727 |
+
|
| 728 |
+
|
| 729 |
+
def einsum(expr, a, b, d, c=None, use_cublaslt=False):
|
| 730 |
+
ops.einsum(expr, a, b, d, c, use_cublaslt)
|
| 731 |
+
|
| 732 |
+
|
| 733 |
+
def fp8_einsum(expr, a, b, d, c=None, recipe=(1, 128, 128)):
|
| 734 |
+
a_data, a_sf = a
|
| 735 |
+
b_data, b_sf = b
|
| 736 |
+
r0, r1, r2 = recipe
|
| 737 |
+
ops.fp8_einsum(expr, a_data, a_sf, b_data, b_sf, d, c, r0, r1, r2)
|
| 738 |
+
|
| 739 |
+
|
| 740 |
+
# Hyperconnection ops
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
def tf32_hc_prenorm_gemm(a, b, d, sqr_sum, num_splits=None):
|
| 744 |
+
has_ns = num_splits is not None
|
| 745 |
+
ns = num_splits if has_ns else 0
|
| 746 |
+
ops.tf32_hc_prenorm_gemm(a, b, d, sqr_sum, ns, has_ns)
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
from .mega import (
|
| 750 |
+
SymmBuffer,
|
| 751 |
+
get_symm_buffer_for_mega_moe,
|
| 752 |
+
transform_weights_for_mega_moe,
|
| 753 |
+
fp8_fp4_mega_moe,
|
| 754 |
+
)
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
# Initialize the C++ runtime
|
| 758 |
+
|
| 759 |
+
|
| 760 |
+
def _find_cuda_home() -> str:
|
| 761 |
+
cuda_home = os.environ.get("CUDA_HOME") or os.environ.get("CUDA_PATH")
|
| 762 |
+
if cuda_home is None:
|
| 763 |
+
try:
|
| 764 |
+
with open(os.devnull, "w") as devnull:
|
| 765 |
+
nvcc = (
|
| 766 |
+
subprocess.check_output(["which", "nvcc"], stderr=devnull)
|
| 767 |
+
.decode()
|
| 768 |
+
.rstrip("\r\n")
|
| 769 |
+
)
|
| 770 |
+
cuda_home = os.path.dirname(os.path.dirname(nvcc))
|
| 771 |
+
except Exception:
|
| 772 |
+
cuda_home = "/usr/local/cuda"
|
| 773 |
+
if not os.path.exists(cuda_home):
|
| 774 |
+
cuda_home = None
|
| 775 |
+
assert cuda_home is not None, "Could not find CUDA installation"
|
| 776 |
+
return cuda_home
|
| 777 |
+
|
| 778 |
+
|
| 779 |
+
# Find the library root for JIT headers
|
| 780 |
+
# In development: use the repo's deep_gemm/ directory
|
| 781 |
+
# In installed wheel: use this package's directory
|
| 782 |
+
_lib_root = os.path.join(
|
| 783 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "deep_gemm"
|
| 784 |
+
)
|
| 785 |
+
if not os.path.isdir(os.path.join(_lib_root, "include")):
|
| 786 |
+
# Fallback: try the parent package
|
| 787 |
+
_lib_root = os.path.dirname(os.path.abspath(__file__))
|
| 788 |
+
|
| 789 |
+
_initialized = False
|
| 790 |
+
|
| 791 |
+
# Set DG_CUTLASS_INCLUDE for JIT kernel compilation (if not already set by user)
|
| 792 |
+
if "DG_CUTLASS_INCLUDE" not in os.environ:
|
| 793 |
+
_include = os.path.join(_lib_root, "include")
|
| 794 |
+
_cutlass_include_candidates = [
|
| 795 |
+
_include, # legacy layout: include/cutlass
|
| 796 |
+
os.path.join(_include, "third-party", "cutlass", "include"), # submodule layout
|
| 797 |
+
]
|
| 798 |
+
for _site_packages in {
|
| 799 |
+
sysconfig.get_paths().get("purelib"),
|
| 800 |
+
sysconfig.get_paths().get("platlib"),
|
| 801 |
+
}:
|
| 802 |
+
if _site_packages:
|
| 803 |
+
_cutlass_include_candidates.append(
|
| 804 |
+
os.path.join(_site_packages, "cutlass_library", "source", "include")
|
| 805 |
+
)
|
| 806 |
+
for _cutlass_include in _cutlass_include_candidates:
|
| 807 |
+
if os.path.isdir(os.path.join(_cutlass_include, "cutlass")):
|
| 808 |
+
os.environ["DG_CUTLASS_INCLUDE"] = _cutlass_include
|
| 809 |
+
break
|
| 810 |
+
else:
|
| 811 |
+
# Fall back to nvidia-cutlass pip package
|
| 812 |
+
try:
|
| 813 |
+
import nvidia.cutlass as _nc
|
| 814 |
+
|
| 815 |
+
os.environ["DG_CUTLASS_INCLUDE"] = os.path.join(
|
| 816 |
+
os.path.dirname(_nc.__file__), "include"
|
| 817 |
+
)
|
| 818 |
+
except ImportError:
|
| 819 |
+
pass
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
def _ensure_initialized():
|
| 823 |
+
global _initialized
|
| 824 |
+
if _initialized:
|
| 825 |
+
return
|
| 826 |
+
_ops.init(_lib_root, _find_cuda_home())
|
| 827 |
+
_initialized = True
|
| 828 |
+
|
| 829 |
+
|
| 830 |
+
class _InitializedOps:
|
| 831 |
+
def __init__(self, raw_ops):
|
| 832 |
+
self._raw_ops = raw_ops
|
| 833 |
+
|
| 834 |
+
def __getattr__(self, name):
|
| 835 |
+
if name != "init":
|
| 836 |
+
_ensure_initialized()
|
| 837 |
+
return getattr(self._raw_ops, name)
|
| 838 |
+
|
| 839 |
+
|
| 840 |
+
ops = _InitializedOps(_ops)
|
| 841 |
+
|
| 842 |
+
|
| 843 |
+
# Try to initialize eagerly, but don't fail if CUDA is not found
|
| 844 |
+
# (e.g., during build-time import checks). init() will be called
|
| 845 |
+
# lazily on first actual kernel use.
|
| 846 |
+
try:
|
| 847 |
+
_ensure_initialized()
|
| 848 |
+
except (AssertionError, RuntimeError):
|
| 849 |
+
pass
|
build/torch211-cu128-aarch64-linux/_deep_gemm_cuda_47ad41b.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95f998b2e8b4689c60d7d629075000f9bd7a9252236473ece61b7390830aa922
|
| 3 |
+
size 1437016
|
build/torch211-cu128-aarch64-linux/_ops.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from . import _deep_gemm_cuda_47ad41b
|
| 3 |
+
ops = torch.ops._deep_gemm_cuda_47ad41b
|
| 4 |
+
|
| 5 |
+
def add_op_namespace_prefix(op_name: str):
|
| 6 |
+
"""
|
| 7 |
+
Prefix op by namespace.
|
| 8 |
+
"""
|
| 9 |
+
return f"_deep_gemm_cuda_47ad41b::{op_name}"
|
build/torch211-cu128-aarch64-linux/deep_gemm/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ctypes
|
| 2 |
+
import importlib.util
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from types import ModuleType
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _import_from_path(file_path: Path) -> ModuleType:
|
| 9 |
+
# We cannot use the module name as-is, after adding it to `sys.modules`,
|
| 10 |
+
# it would also be used for other imports. So, we make a module name that
|
| 11 |
+
# depends on the path for it to be unique using the hex-encoded hash of
|
| 12 |
+
# the path.
|
| 13 |
+
path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
|
| 14 |
+
module_name = path_hash
|
| 15 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 16 |
+
if spec is None:
|
| 17 |
+
raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
|
| 18 |
+
module = importlib.util.module_from_spec(spec)
|
| 19 |
+
if module is None:
|
| 20 |
+
raise ImportError(f"Cannot load module {module_name} from spec")
|
| 21 |
+
sys.modules[module_name] = module
|
| 22 |
+
spec.loader.exec_module(module) # type: ignore
|
| 23 |
+
return module
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/comm/barrier.cuh
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
|
| 5 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 6 |
+
#include <deep_gemm/layout/sym_buffer.cuh>
|
| 7 |
+
#include <deep_gemm/layout/mega_moe.cuh>
|
| 8 |
+
|
| 9 |
+
namespace deep_gemm::comm {
|
| 10 |
+
|
| 11 |
+
CUTLASS_DEVICE void cluster_sync_with_relaxed_arrive() {
|
| 12 |
+
// Perform cluster_sync with `barrier.cluster.arrive.relaxed`
|
| 13 |
+
// This is slightly faster than `cute::cluster_sync` but has weaker memory ordering guarantee
|
| 14 |
+
cute::cluster_arrive_relaxed();
|
| 15 |
+
cute::cluster_wait();
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
template <uint32_t kNumSMs, uint32_t kGridSyncIndex = 0, typename sync_scope_t>
|
| 19 |
+
CUTLASS_DEVICE void grid_sync(const layout::Workspace& workspace,
|
| 20 |
+
const uint32_t& sm_idx, const uint32_t& thread_idx,
|
| 21 |
+
const sync_scope_t& sync_scope) {
|
| 22 |
+
// NOTES: the implementation idea is from `cooperative_groups::this_grid().sync()`
|
| 23 |
+
static constexpr uint32_t kFinishSumTag = 0x80000000u;
|
| 24 |
+
sync_scope();
|
| 25 |
+
if (thread_idx == 0) {
|
| 26 |
+
const auto count_ptr = workspace.get_grid_sync_count_ptr<kGridSyncIndex>();
|
| 27 |
+
const auto old_value = ptx::atomic_add_rel(
|
| 28 |
+
count_ptr, sm_idx == 0 ? (kFinishSumTag - (kNumSMs - 1)) : 1);
|
| 29 |
+
uint32_t new_value;
|
| 30 |
+
do {
|
| 31 |
+
new_value = ptx::ld_acq(count_ptr);
|
| 32 |
+
} while (((new_value ^ old_value) & kFinishSumTag) == 0);
|
| 33 |
+
}
|
| 34 |
+
sync_scope();
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
template <uint32_t kNumRanks, uint32_t kNumSMs, uint32_t kNumThreads, uint32_t kGridSyncIndex, uint32_t kTag, typename sync_scope_t>
|
| 38 |
+
CUTLASS_DEVICE void nvlink_barrier(const layout::Workspace& workspace,
|
| 39 |
+
const layout::SymBuffer<kNumRanks>& sym_buffer,
|
| 40 |
+
const uint32_t& sm_idx, const uint32_t& thread_idx,
|
| 41 |
+
const sync_scope_t& sync_scope,
|
| 42 |
+
const bool& sync_prologue = true,
|
| 43 |
+
const bool& sync_epilogue = true) {
|
| 44 |
+
DG_STATIC_ASSERT(kNumRanks <= kNumThreads, "Insufficient threads");
|
| 45 |
+
|
| 46 |
+
// Grid sync before NVLink signaling
|
| 47 |
+
if (sync_prologue)
|
| 48 |
+
grid_sync<kNumSMs, kGridSyncIndex>(workspace, sm_idx, thread_idx, sync_scope);
|
| 49 |
+
|
| 50 |
+
// NVLink cross-rank barrier, only SM 0 participates
|
| 51 |
+
if (sm_idx == 0) {
|
| 52 |
+
auto* counter_ptr = workspace.get_nvl_barrier_counter_ptr();
|
| 53 |
+
const auto status = (*counter_ptr) & 3;
|
| 54 |
+
const auto signal_phase = status & 1, signal_sign = status >> 1;
|
| 55 |
+
auto* signal_ptr = workspace.get_nvl_barrier_signal_ptr(signal_phase);
|
| 56 |
+
|
| 57 |
+
// Send signals to remote ranks
|
| 58 |
+
if (thread_idx < kNumRanks)
|
| 59 |
+
ptx::red_add_rel_sys(sym_buffer.map(signal_ptr, thread_idx), signal_sign ? -1 : 1);
|
| 60 |
+
sync_scope();
|
| 61 |
+
|
| 62 |
+
// Update status and wait arrival (with 30s timeout, at 2 GHz)
|
| 63 |
+
constexpr int64_t kNumTimeoutCycles = 30ll * 2000000000ll;
|
| 64 |
+
if (thread_idx == 0) {
|
| 65 |
+
ptx::red_add(counter_ptr, 1);
|
| 66 |
+
const int target = signal_sign ? 0 : static_cast<int>(kNumRanks);
|
| 67 |
+
const auto start_clock = clock64();
|
| 68 |
+
while (ptx::ld_acq_sys(signal_ptr) != target) {
|
| 69 |
+
if (clock64() - start_clock >= kNumTimeoutCycles) {
|
| 70 |
+
printf("DeepGEMM NVLink barrier timeout (30s): rank=%d, counter=%d, signal=%d, target=%d, phase=%d, sign=%d, tag=%d\n",
|
| 71 |
+
sym_buffer.rank_idx, *counter_ptr, ptx::ld_acq_sys(signal_ptr), target, signal_phase, signal_sign, kTag);
|
| 72 |
+
DG_DEVICE_ASSERT(false and "NVLink barrier timeout");
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// Grid sync after NVLink completion
|
| 79 |
+
if (sync_epilogue)
|
| 80 |
+
grid_sync<kNumSMs, kGridSyncIndex>(workspace, sm_idx, thread_idx, sync_scope);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
} // namespace deep_gemm::comm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/compile.cuh
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/detail/helper_macros.hpp>
|
| 4 |
+
|
| 5 |
+
#if defined(__NVCC__) or (defined(__clang__) and defined(__CUDA__)) or defined(__CUDACC_RTC__) or defined(__CLION_IDE__)
|
| 6 |
+
#define DG_IN_CUDA_COMPILATION
|
| 7 |
+
#endif
|
| 8 |
+
|
| 9 |
+
#if defined(__NVCC__) || (defined(__clang__) and defined(__CUDA__))
|
| 10 |
+
#define CUTLASS_HOST_DEVICE_NOINLINE __device__ __host__
|
| 11 |
+
#define CUTLASS_DEVICE_NOINLINE __device__
|
| 12 |
+
#elif defined(__CUDACC_RTC__)
|
| 13 |
+
#define CUTLASS_HOST_DEVICE_NOINLINE __device__
|
| 14 |
+
#define CUTLASS_DEVICE_NOINLINE __device__
|
| 15 |
+
#else
|
| 16 |
+
#define CUTLASS_HOST_DEVICE_NOINLINE
|
| 17 |
+
#define CUTLASS_DEVICE_NOINLINE
|
| 18 |
+
#endif
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/cute_tie.cuh
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/int_tuple.hpp>
|
| 4 |
+
|
| 5 |
+
namespace cute {
|
| 6 |
+
|
| 7 |
+
struct ignore_t {
|
| 8 |
+
template <typename T>
|
| 9 |
+
constexpr const ignore_t& operator=(T&&) const noexcept {
|
| 10 |
+
return *this;
|
| 11 |
+
}
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
inline constexpr ignore_t ignore{};
|
| 15 |
+
|
| 16 |
+
} // namespace cute
|
| 17 |
+
|
| 18 |
+
#define CUTE_TIE_CONCAT_IMPL(A, B) A##B
|
| 19 |
+
#define CUTE_TIE_CONCAT(A, B) CUTE_TIE_CONCAT_IMPL(A, B)
|
| 20 |
+
|
| 21 |
+
#define CUTE_TIE_GET_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
|
| 22 |
+
#define CUTE_TIE_COUNT_ARGS(...) \
|
| 23 |
+
CUTE_TIE_GET_NTH_ARG(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
| 24 |
+
|
| 25 |
+
#define CUTE_TIE_OP_DECL(I, TUPLE, VAR) auto VAR = ::cute::get<I>(TUPLE)
|
| 26 |
+
#define CUTE_TIE_OP_ASSIGN(I, TUPLE, VAR) VAR = ::cute::get<I>(TUPLE)
|
| 27 |
+
|
| 28 |
+
#define CUTE_TIE_APPLY_OP_1(OP, T, V1) OP(0, T, V1);
|
| 29 |
+
#define CUTE_TIE_APPLY_OP_2(OP, T, V1, V2) OP(0, T, V1); OP(1, T, V2);
|
| 30 |
+
#define CUTE_TIE_APPLY_OP_3(OP, T, V1, V2, V3) OP(0, T, V1); OP(1, T, V2); OP(2, T, V3);
|
| 31 |
+
#define CUTE_TIE_APPLY_OP_4(OP, T, V1, V2, V3, V4) OP(0, T, V1); OP(1, T, V2); OP(2, T, V3); OP(3, T, V4);
|
| 32 |
+
#define CUTE_TIE_APPLY_OP_5(OP, T, V1, V2, V3, V4, V5) OP(0, T, V1); OP(1, T, V2); OP(2, T, V3); OP(3, T, V4); OP(4, T, V5);
|
| 33 |
+
|
| 34 |
+
#define CUTE_TIE_DECL(TUPLE_EXPR, ...) \
|
| 35 |
+
auto&& CUTE_TIE_CONCAT(cute_tie__temp_tuple_, __LINE__) = (TUPLE_EXPR); \
|
| 36 |
+
CUTE_TIE_CONCAT(CUTE_TIE_APPLY_OP_, CUTE_TIE_COUNT_ARGS(__VA_ARGS__)) ( \
|
| 37 |
+
CUTE_TIE_OP_DECL, \
|
| 38 |
+
CUTE_TIE_CONCAT(cute_tie__temp_tuple_, __LINE__), \
|
| 39 |
+
__VA_ARGS__ \
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
#define CUTE_TIE(TUPLE_EXPR, ...) \
|
| 43 |
+
do { \
|
| 44 |
+
auto&& CUTE_TIE_CONCAT(cute_tie__temp_tuple_, __LINE__) = (TUPLE_EXPR); \
|
| 45 |
+
CUTE_TIE_CONCAT(CUTE_TIE_APPLY_OP_, CUTE_TIE_COUNT_ARGS(__VA_ARGS__)) ( \
|
| 46 |
+
CUTE_TIE_OP_ASSIGN, \
|
| 47 |
+
CUTE_TIE_CONCAT(cute_tie__temp_tuple_, __LINE__), \
|
| 48 |
+
__VA_ARGS__ \
|
| 49 |
+
); \
|
| 50 |
+
} while (0)
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/epilogue_utils.cuh
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <deep_gemm/common/types.hpp>
|
| 4 |
+
#include <deep_gemm/common/utils.cuh>
|
| 5 |
+
|
| 6 |
+
namespace deep_gemm {
|
| 7 |
+
|
| 8 |
+
struct EpilogueIdentity {
|
| 9 |
+
template <uint32_t STORE_BLOCK_N>
|
| 10 |
+
__device__ __forceinline__ static uint32_t apply_index_n(const uint32_t &n_idx) {
|
| 11 |
+
return n_idx;
|
| 12 |
+
}
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
template <uint32_t kLeft, uint32_t kMid, uint32_t kRight>
|
| 16 |
+
struct EpilogueHeadSplits: EpilogueIdentity {
|
| 17 |
+
template <uint32_t STORE_BLOCK_N>
|
| 18 |
+
__device__ __forceinline__ static uint32_t apply_index_n(const uint32_t &n_idx) {
|
| 19 |
+
DG_STATIC_ASSERT(kLeft % STORE_BLOCK_N == 0 and kMid % STORE_BLOCK_N == 0
|
| 20 |
+
and kRight % STORE_BLOCK_N == 0, "Invalid head splits config");
|
| 21 |
+
return n_idx + (n_idx + kRight) / (kLeft + kRight) * kMid;
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
#pragma clang diagnostic pop
|
| 26 |
+
|
| 27 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/exception.cuh
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cuda/std/cstdint>
|
| 4 |
+
#include <deep_gemm/common/compile.cuh>
|
| 5 |
+
|
| 6 |
+
#ifdef __CLION_IDE__
|
| 7 |
+
|
| 8 |
+
CUTLASS_HOST_DEVICE void host_device_printf(const char* format, ...) {
|
| 9 |
+
asm volatile("trap;");
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
#define printf host_device_printf
|
| 13 |
+
#endif
|
| 14 |
+
|
| 15 |
+
#ifndef DG_DEVICE_ASSERT
|
| 16 |
+
#define DG_DEVICE_ASSERT(cond) \
|
| 17 |
+
do { \
|
| 18 |
+
if (not (cond)) { \
|
| 19 |
+
printf("Assertion failed: %s:%d, condition: %s\n", __FILE__, __LINE__, #cond); \
|
| 20 |
+
asm("trap;"); \
|
| 21 |
+
} \
|
| 22 |
+
} while (0)
|
| 23 |
+
#endif
|
| 24 |
+
|
| 25 |
+
#ifndef DG_TRAP_ONLY_DEVICE_ASSERT
|
| 26 |
+
#define DG_TRAP_ONLY_DEVICE_ASSERT(cond) \
|
| 27 |
+
do { \
|
| 28 |
+
if (not (cond)) \
|
| 29 |
+
asm("trap;"); \
|
| 30 |
+
} while (0)
|
| 31 |
+
#endif
|
| 32 |
+
|
| 33 |
+
#ifndef DG_STATIC_ASSERT
|
| 34 |
+
#define DG_STATIC_ASSERT(cond, ...) static_assert(cond, __VA_ARGS__)
|
| 35 |
+
#endif
|
| 36 |
+
|
| 37 |
+
#ifndef DG_UNIFIED_ASSERT
|
| 38 |
+
#ifdef DG_IN_CUDA_COMPILATION
|
| 39 |
+
#define DG_UNIFIED_ASSERT(cond) DG_DEVICE_ASSERT(cond)
|
| 40 |
+
#else
|
| 41 |
+
#define DG_UNIFIED_ASSERT(cond) DG_HOST_ASSERT(cond)
|
| 42 |
+
#endif
|
| 43 |
+
#endif
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/math.cuh
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cuda/std/cstdint>
|
| 4 |
+
#include <deep_gemm/common/compile.cuh>
|
| 5 |
+
#include <deep_gemm/common/exception.cuh>
|
| 6 |
+
|
| 7 |
+
namespace deep_gemm::math {
|
| 8 |
+
|
| 9 |
+
/// Pointer operations
|
| 10 |
+
template <typename dtype_t = void>
|
| 11 |
+
CUTLASS_HOST_DEVICE dtype_t* advance_ptr(void* ptr, const uint64_t num_bytes) {
|
| 12 |
+
return reinterpret_cast<dtype_t*>(static_cast<uint8_t*>(ptr) + num_bytes);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/// Math functions
|
| 16 |
+
template <typename T>
|
| 17 |
+
CUTLASS_HOST_DEVICE T ceil_div(T a, T b) {
|
| 18 |
+
return (a + b - 1) / b;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
template <typename T>
|
| 22 |
+
CUTLASS_HOST_DEVICE constexpr T constexpr_ceil_div(T a, T b) {
|
| 23 |
+
return (a + b - 1) / b;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
template <typename T, bool kDoCeilAlignment = true>
|
| 27 |
+
CUTLASS_HOST_DEVICE T align(T a, T b) {
|
| 28 |
+
return (kDoCeilAlignment ? ceil_div(a, b) : (a / b)) * b;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
template <typename T>
|
| 32 |
+
CUTLASS_HOST_DEVICE constexpr T constexpr_align(T a, T b) {
|
| 33 |
+
return constexpr_ceil_div(a, b) * b;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
template <typename T>
|
| 37 |
+
CUTLASS_HOST_DEVICE constexpr T constexpr_gcd(T a, T b) {
|
| 38 |
+
return b == 0 ? a : constexpr_gcd(b, a % b);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
template <typename T>
|
| 42 |
+
CUTLASS_HOST_DEVICE constexpr T constexpr_min(T a, T b) {
|
| 43 |
+
return a < b ? a : b;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
template <typename T>
|
| 47 |
+
CUTLASS_DEVICE void swap(T& a, T& b) {
|
| 48 |
+
T temp = a;
|
| 49 |
+
a = b;
|
| 50 |
+
b = temp;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
#ifdef DG_IN_CUDA_COMPILATION
|
| 54 |
+
CUTLASS_DEVICE float2 fma2(const float2& a, const float2& b, const float2& c) {
|
| 55 |
+
#if defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)
|
| 56 |
+
return __ffma2_rn(a, b, c);
|
| 57 |
+
#else
|
| 58 |
+
return make_float2(
|
| 59 |
+
__fmaf_rn(a.x, b.x, c.x),
|
| 60 |
+
__fmaf_rn(a.y, b.y, c.y)
|
| 61 |
+
);
|
| 62 |
+
#endif
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
CUTLASS_HOST_DEVICE float fast_rcp(const float& x) {
|
| 66 |
+
#if defined(__CUDA_ARCH__)
|
| 67 |
+
float ret;
|
| 68 |
+
asm volatile("rcp.approx.ftz.f32 %0, %1;" : "=f"(ret) : "f"(x));
|
| 69 |
+
return ret;
|
| 70 |
+
#else
|
| 71 |
+
return 1.0f / x;
|
| 72 |
+
#endif
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/// Casting
|
| 76 |
+
template <typename old_t>
|
| 77 |
+
CUTLASS_DEVICE int cast_into_bf16_and_pack(old_t& x, old_t& y) {
|
| 78 |
+
auto bf16x2 = __float22bfloat162_rn({*reinterpret_cast<float*>(&x), *reinterpret_cast<float*>(&y)});
|
| 79 |
+
return *reinterpret_cast<int*>(&bf16x2);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
CUTLASS_DEVICE float fast_pow2(const int& x) {
|
| 83 |
+
uint32_t bits_x = (x + 127) << 23;
|
| 84 |
+
return *reinterpret_cast<float*>(&bits_x);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
CUTLASS_DEVICE int fast_log2_ceil(float x) {
|
| 88 |
+
const auto bits = *reinterpret_cast<uint32_t*>(&x);
|
| 89 |
+
const auto exp = bits >> 23;
|
| 90 |
+
const auto man = bits & ((1 << 23) - 1);
|
| 91 |
+
return exp - 127 + (man != 0);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
template <bool kUseUE8M0 = true>
|
| 95 |
+
CUTLASS_DEVICE void get_e4m3_sf_and_sf_inv(const float2& amax, float2& sf, float2& sf_inv) {
|
| 96 |
+
DG_STATIC_ASSERT(kUseUE8M0, "Must use UE8M0");
|
| 97 |
+
const float2 finfo_factor = {1.0 / 448.0, 1.0 / 448.0};
|
| 98 |
+
const auto scaled = __fmul2_rn(amax, finfo_factor);
|
| 99 |
+
const auto exp_x = fast_log2_ceil(scaled.x);
|
| 100 |
+
const auto exp_y = fast_log2_ceil(scaled.y);
|
| 101 |
+
sf.x = fast_pow2(exp_x), sf_inv.x = fast_pow2(-exp_x);
|
| 102 |
+
sf.y = fast_pow2(exp_y), sf_inv.y = fast_pow2(-exp_y);
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/// Reduction
|
| 106 |
+
CUTLASS_DEVICE uint32_t warp_inclusive_sum(uint32_t value, const uint32_t& lane_idx) {
|
| 107 |
+
#pragma unroll
|
| 108 |
+
for (uint32_t offset = 1; offset < 32; offset <<= 1) {
|
| 109 |
+
const uint32_t synced = __shfl_up_sync(0xffffffff, value, offset);
|
| 110 |
+
if (lane_idx >= offset)
|
| 111 |
+
value += synced;
|
| 112 |
+
}
|
| 113 |
+
return value;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Operation functors
|
| 117 |
+
template <typename T> struct ReduceSum { CUTLASS_DEVICE T operator()(T a, T b) const { return a + b; } };
|
| 118 |
+
template <typename T> struct ReduceMax { CUTLASS_DEVICE T operator()(T a, T b) const { return a > b ? a : b; } };
|
| 119 |
+
template <typename T> struct ReduceMin { CUTLASS_DEVICE T operator()(T a, T b) const { return a < b ? a : b; } };
|
| 120 |
+
template <typename T> struct ReduceAnd { CUTLASS_DEVICE T operator()(T a, T b) const { return a & b; } };
|
| 121 |
+
template <typename T> struct ReduceOr { CUTLASS_DEVICE T operator()(T a, T b) const { return a | b; } };
|
| 122 |
+
|
| 123 |
+
// Unified reduction function
|
| 124 |
+
template <uint32_t kNumLanesPerGroup, bool kIntergroupReduce, typename T, typename Op>
|
| 125 |
+
CUTLASS_DEVICE T warp_reduce(T value, Op op) {
|
| 126 |
+
DG_STATIC_ASSERT(kNumLanesPerGroup == 32 or kNumLanesPerGroup == 16 or kNumLanesPerGroup == 8 or
|
| 127 |
+
kNumLanesPerGroup == 4 or kNumLanesPerGroup == 2 or kNumLanesPerGroup == 1,
|
| 128 |
+
"Invalid number of lanes");
|
| 129 |
+
constexpr uint32_t mask = 0xffffffff;
|
| 130 |
+
if constexpr (kIntergroupReduce) {
|
| 131 |
+
if constexpr (kNumLanesPerGroup <= 1) value = op(value, __shfl_xor_sync(mask, value, 1));
|
| 132 |
+
if constexpr (kNumLanesPerGroup <= 2) value = op(value, __shfl_xor_sync(mask, value, 2));
|
| 133 |
+
if constexpr (kNumLanesPerGroup <= 4) value = op(value, __shfl_xor_sync(mask, value, 4));
|
| 134 |
+
if constexpr (kNumLanesPerGroup <= 8) value = op(value, __shfl_xor_sync(mask, value, 8));
|
| 135 |
+
if constexpr (kNumLanesPerGroup <= 16) value = op(value, __shfl_xor_sync(mask, value, 16));
|
| 136 |
+
} else {
|
| 137 |
+
if constexpr (kNumLanesPerGroup >= 32) value = op(value, __shfl_xor_sync(mask, value, 16));
|
| 138 |
+
if constexpr (kNumLanesPerGroup >= 16) value = op(value, __shfl_xor_sync(mask, value, 8));
|
| 139 |
+
if constexpr (kNumLanesPerGroup >= 8) value = op(value, __shfl_xor_sync(mask, value, 4));
|
| 140 |
+
if constexpr (kNumLanesPerGroup >= 4) value = op(value, __shfl_xor_sync(mask, value, 2));
|
| 141 |
+
if constexpr (kNumLanesPerGroup >= 2) value = op(value, __shfl_xor_sync(mask, value, 1));
|
| 142 |
+
}
|
| 143 |
+
return value;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// Convenience aliases
|
| 147 |
+
template <uint32_t kNumLanesPerGroup = 32, bool kIntergroupReduce = false, typename T>
|
| 148 |
+
CUTLASS_DEVICE T warp_reduce_sum(T value) {
|
| 149 |
+
return warp_reduce<kNumLanesPerGroup, kIntergroupReduce, T>(value, ReduceSum<T>{});
|
| 150 |
+
}
|
| 151 |
+
#endif
|
| 152 |
+
|
| 153 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/reduction.cuh
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cuda_bf16.h>
|
| 4 |
+
#include <cuda_fp8.h>
|
| 5 |
+
#include <cuda/std/cstdint>
|
| 6 |
+
#include <cuda/std/utility>
|
| 7 |
+
|
| 8 |
+
#include <deep_gemm/common/utils.cuh>
|
| 9 |
+
|
| 10 |
+
// Operation functors
|
| 11 |
+
template <typename T> struct ReduceSum { __device__ T operator()(T a, T b) const { return a + b; } };
|
| 12 |
+
template <typename T> struct ReduceMax { __device__ T operator()(T a, T b) const { return a > b ? a : b; } };
|
| 13 |
+
template <typename T> struct ReduceMin { __device__ T operator()(T a, T b) const { return a < b ? a : b; } };
|
| 14 |
+
template <typename T> struct ReduceAnd { __device__ T operator()(T a, T b) const { return a & b; } };
|
| 15 |
+
template <typename T> struct ReduceOr { __device__ T operator()(T a, T b) const { return a | b; } };
|
| 16 |
+
|
| 17 |
+
// Unified reduction function
|
| 18 |
+
template <int kNumLanesPerGroup, bool kIntergroupReduce, typename T, typename Op>
|
| 19 |
+
__forceinline__ __device__ T warp_reduce(T value, Op op) {
|
| 20 |
+
DG_STATIC_ASSERT(kNumLanesPerGroup == 32 or kNumLanesPerGroup == 16 or kNumLanesPerGroup == 8 or
|
| 21 |
+
kNumLanesPerGroup == 4 or kNumLanesPerGroup == 2 or kNumLanesPerGroup == 1,
|
| 22 |
+
"Invalid number of lanes");
|
| 23 |
+
constexpr uint32_t mask = 0xffffffff;
|
| 24 |
+
if constexpr (kIntergroupReduce) {
|
| 25 |
+
if constexpr (kNumLanesPerGroup <= 1) value = op(value, __shfl_xor_sync(mask, value, 1));
|
| 26 |
+
if constexpr (kNumLanesPerGroup <= 2) value = op(value, __shfl_xor_sync(mask, value, 2));
|
| 27 |
+
if constexpr (kNumLanesPerGroup <= 4) value = op(value, __shfl_xor_sync(mask, value, 4));
|
| 28 |
+
if constexpr (kNumLanesPerGroup <= 8) value = op(value, __shfl_xor_sync(mask, value, 8));
|
| 29 |
+
if constexpr (kNumLanesPerGroup <= 16) value = op(value, __shfl_xor_sync(mask, value, 16));
|
| 30 |
+
} else {
|
| 31 |
+
if constexpr (kNumLanesPerGroup >= 32) value = op(value, __shfl_xor_sync(mask, value, 16));
|
| 32 |
+
if constexpr (kNumLanesPerGroup >= 16) value = op(value, __shfl_xor_sync(mask, value, 8));
|
| 33 |
+
if constexpr (kNumLanesPerGroup >= 8) value = op(value, __shfl_xor_sync(mask, value, 4));
|
| 34 |
+
if constexpr (kNumLanesPerGroup >= 4) value = op(value, __shfl_xor_sync(mask, value, 2));
|
| 35 |
+
if constexpr (kNumLanesPerGroup >= 2) value = op(value, __shfl_xor_sync(mask, value, 1));
|
| 36 |
+
}
|
| 37 |
+
return value;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Convenience aliases
|
| 41 |
+
template <int kNumLanesPerGroup = 32, bool kIntergroupReduce = false, typename T>
|
| 42 |
+
__forceinline__ __device__ T warp_reduce_sum(T value) {
|
| 43 |
+
return warp_reduce<kNumLanesPerGroup, kIntergroupReduce, T>(value, ReduceSum<T>{});
|
| 44 |
+
}
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/scheduler.cuh
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <deep_gemm/common/types.hpp>
|
| 4 |
+
#include <deep_gemm/common/utils.cuh>
|
| 5 |
+
|
| 6 |
+
namespace deep_gemm {
|
| 7 |
+
|
| 8 |
+
enum class IndexType {
|
| 9 |
+
MN,
|
| 10 |
+
K,
|
| 11 |
+
SF_K,
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
template <GemmType kGemmType, uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t kNumSMs, bool kIsMulticastOnA>
|
| 15 |
+
static constexpr uint32_t get_num_1d_blocks_per_group() {
|
| 16 |
+
// Select the best from candidates
|
| 17 |
+
uint32_t num_best_blocks = 0, min_usage = cute::numeric_limits<uint32_t>::max();
|
| 18 |
+
for (const auto& candidate: {8u, 16u}) {
|
| 19 |
+
const auto& usage = kIsMulticastOnA ?
|
| 20 |
+
candidate * BLOCK_N + constexpr_ceil_div(kNumSMs, candidate) * BLOCK_M: // Grouping on N
|
| 21 |
+
candidate * BLOCK_M + constexpr_ceil_div(kNumSMs, candidate) * BLOCK_N; // Grouping on M
|
| 22 |
+
if (usage < min_usage)
|
| 23 |
+
min_usage = usage, num_best_blocks = candidate;
|
| 24 |
+
}
|
| 25 |
+
return num_best_blocks;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
#pragma clang diagnostic push
|
| 29 |
+
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-member-init"
|
| 30 |
+
template <GemmType kGemmType,
|
| 31 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N,
|
| 32 |
+
uint32_t kNumGroups,
|
| 33 |
+
uint32_t kNumMulticast, bool kIsMulticastOnA,
|
| 34 |
+
uint32_t kNumSMs,
|
| 35 |
+
uint32_t SF_K_ALIGNMENT = 512u, // for k-grouped GEMM only: 128 (SM90 float SF) or 512 (SM100 UE8M0 SF)
|
| 36 |
+
uint32_t kNum1DBlocksPerGroup = get_num_1d_blocks_per_group<kGemmType, BLOCK_M, BLOCK_N, kNumSMs, kIsMulticastOnA>()>
|
| 37 |
+
struct Scheduler {
|
| 38 |
+
int current_iter = -1;
|
| 39 |
+
|
| 40 |
+
// Block configs
|
| 41 |
+
uint32_t num_blocks;
|
| 42 |
+
uint32_t num_m_blocks;
|
| 43 |
+
uint32_t num_n_blocks;
|
| 44 |
+
|
| 45 |
+
// For SM90 multicast checks
|
| 46 |
+
uint32_t num_blocks_in_group;
|
| 47 |
+
bool is_peer_cta_alive = true;
|
| 48 |
+
|
| 49 |
+
// For grouped GEMM
|
| 50 |
+
int* grouped_layout;
|
| 51 |
+
uint32_t current_group_idx = 0;
|
| 52 |
+
// Only used for masked layout
|
| 53 |
+
uint32_t current_m_cumsum = 0;
|
| 54 |
+
// Only used for countiguous psum layout
|
| 55 |
+
uint32_t last_psum_m = 0, current_psum_m, current_m_block_cumsum = 0;
|
| 56 |
+
// Only used for k-grouped layout
|
| 57 |
+
uint32_t current_shape_k, current_num_valid_groups = 0, current_k_cumsum = 0, current_sf_k_cumsum = 0;
|
| 58 |
+
uint32_t next_group_idx, next_shape_k;
|
| 59 |
+
|
| 60 |
+
// Only used for k-grouped gemm
|
| 61 |
+
__device__ __forceinline__ void get_next_k_group(uint32_t &group_idx, uint32_t &shape_k) const {
|
| 62 |
+
for (; group_idx < kNumGroups; ++ group_idx) {
|
| 63 |
+
shape_k = __ldg(grouped_layout + group_idx);
|
| 64 |
+
if (shape_k > 0)
|
| 65 |
+
break;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// ReSharper disable once CppPossiblyUninitializedMember
|
| 70 |
+
__device__ __forceinline__ explicit Scheduler(const uint32_t& shape_m, const uint32_t& shape_n, const uint32_t& shape_k,
|
| 71 |
+
int* grouped_layout = nullptr) {
|
| 72 |
+
num_m_blocks = ceil_div(shape_m, BLOCK_M);
|
| 73 |
+
num_n_blocks = ceil_div(shape_n, BLOCK_N);
|
| 74 |
+
current_shape_k = shape_k;
|
| 75 |
+
if constexpr (kGemmType == GemmType::Normal or kGemmType == GemmType::Batched) {
|
| 76 |
+
num_blocks = num_m_blocks * num_n_blocks;
|
| 77 |
+
} else if constexpr (kGemmType == GemmType::MGroupedContiguous) {
|
| 78 |
+
num_blocks = num_m_blocks * num_n_blocks;
|
| 79 |
+
this->grouped_layout = grouped_layout;
|
| 80 |
+
} else if constexpr (kGemmType == GemmType::MGroupedMasked) {
|
| 81 |
+
this->grouped_layout = grouped_layout;
|
| 82 |
+
} else if constexpr (kGemmType == GemmType::MGroupedContiguousWithPsumLayout) {
|
| 83 |
+
this->grouped_layout = grouped_layout;
|
| 84 |
+
current_psum_m = __ldg(grouped_layout);
|
| 85 |
+
num_m_blocks = ceil_div(current_psum_m, BLOCK_M);
|
| 86 |
+
} else if constexpr (kGemmType == GemmType::KGroupedContiguous) {
|
| 87 |
+
this->grouped_layout = grouped_layout;
|
| 88 |
+
get_next_k_group(current_group_idx, current_shape_k);
|
| 89 |
+
next_group_idx = current_group_idx + 1;
|
| 90 |
+
get_next_k_group(next_group_idx, next_shape_k);
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
__device__ __forceinline__ void get_swizzled_block_idx(const uint32_t& block_idx, uint32_t& m_block_idx, uint32_t& n_block_idx) {
|
| 95 |
+
DG_STATIC_ASSERT(kNum1DBlocksPerGroup % kNumMulticast == 0, "Invalid group size");
|
| 96 |
+
|
| 97 |
+
// Swizzle for better L2 usages
|
| 98 |
+
const auto& primary_num_blocks = kIsMulticastOnA ? num_n_blocks : num_m_blocks;
|
| 99 |
+
const auto& secondary_num_blocks = kIsMulticastOnA ? num_m_blocks : num_n_blocks;
|
| 100 |
+
const auto& num_blocks_per_group = secondary_num_blocks * kNum1DBlocksPerGroup;
|
| 101 |
+
const auto& group_idx = block_idx / num_blocks_per_group;
|
| 102 |
+
auto first_block_idx = group_idx * kNum1DBlocksPerGroup;
|
| 103 |
+
auto in_group_idx = block_idx % num_blocks_per_group;
|
| 104 |
+
num_blocks_in_group = min(kNum1DBlocksPerGroup, primary_num_blocks - first_block_idx);
|
| 105 |
+
|
| 106 |
+
// Fix unaligned TMA multicast
|
| 107 |
+
// NOTES: for SM90 only, as SM90 can dynamically disable TMA multicast
|
| 108 |
+
// while SM100 uses 2-CTA, which can not be dynamically disabled
|
| 109 |
+
#if __CUDA_ARCH__ < 1000
|
| 110 |
+
if (kNumMulticast > 1 and num_blocks_in_group % 2 != 0) {
|
| 111 |
+
if (in_group_idx < (num_blocks_in_group ^ 1) * secondary_num_blocks) {
|
| 112 |
+
num_blocks_in_group = num_blocks_in_group ^ 1;
|
| 113 |
+
} else {
|
| 114 |
+
in_group_idx = in_group_idx - (num_blocks_in_group ^ 1) * secondary_num_blocks;
|
| 115 |
+
first_block_idx += num_blocks_in_group ^ 1;
|
| 116 |
+
num_blocks_in_group = 1;
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
#endif
|
| 120 |
+
|
| 121 |
+
// Convert to final M/N block indices
|
| 122 |
+
// `kIsMulticastOnA == true` leads to groups on N
|
| 123 |
+
if constexpr (kIsMulticastOnA) {
|
| 124 |
+
m_block_idx = in_group_idx / num_blocks_in_group;
|
| 125 |
+
n_block_idx = first_block_idx + in_group_idx % num_blocks_in_group;
|
| 126 |
+
} else {
|
| 127 |
+
m_block_idx = first_block_idx + in_group_idx % num_blocks_in_group;
|
| 128 |
+
n_block_idx = in_group_idx / num_blocks_in_group;
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
template <bool kWithGroupOffset, IndexType kIndexType = IndexType::MN>
|
| 133 |
+
__device__ __forceinline__ uint32_t get_global_idx(const uint32_t shape_dim, const uint32_t block_size,
|
| 134 |
+
const uint32_t& block_idx, const uint32_t& m_block_idx = 0) {
|
| 135 |
+
if constexpr (kGemmType == GemmType::Normal) {
|
| 136 |
+
return block_idx * block_size;
|
| 137 |
+
} else if constexpr (kGemmType == GemmType::MGroupedContiguous) {
|
| 138 |
+
const auto offset = kWithGroupOffset ? cute::max(0, __ldg(grouped_layout + m_block_idx * BLOCK_M)) : 0;
|
| 139 |
+
return offset * shape_dim + block_idx * block_size;
|
| 140 |
+
} else if constexpr (kGemmType == GemmType::MGroupedMasked or kGemmType == GemmType::MGroupedContiguousWithPsumLayout) {
|
| 141 |
+
const auto offset = kWithGroupOffset ? current_group_idx : 0;
|
| 142 |
+
return offset * shape_dim + block_idx * block_size;
|
| 143 |
+
} else if constexpr (kGemmType == GemmType::KGroupedContiguous) {
|
| 144 |
+
auto offset = 0;
|
| 145 |
+
if constexpr (kWithGroupOffset) {
|
| 146 |
+
if constexpr (kIndexType == IndexType::MN)
|
| 147 |
+
offset = current_group_idx * shape_dim;
|
| 148 |
+
else if constexpr (kIndexType == IndexType::K)
|
| 149 |
+
offset = current_k_cumsum;
|
| 150 |
+
else if constexpr (kIndexType == IndexType::SF_K)
|
| 151 |
+
offset = current_sf_k_cumsum;
|
| 152 |
+
}
|
| 153 |
+
return offset + block_idx * block_size;
|
| 154 |
+
} else if constexpr (kGemmType == GemmType::Batched) {
|
| 155 |
+
// Ignore kWithGroupOffset, and apply offset for IndexType::SF_K
|
| 156 |
+
const auto offset = kIndexType == IndexType::SF_K ? current_group_idx : 0;
|
| 157 |
+
return offset * shape_dim + block_idx * block_size;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
__device__ __forceinline__ bool get_next_block(uint32_t& m_block_idx, uint32_t& n_block_idx) {
|
| 162 |
+
const auto next_block_idx = (++ current_iter) * kNumSMs + blockIdx.x;
|
| 163 |
+
|
| 164 |
+
if constexpr (kGemmType == GemmType::MGroupedMasked) {
|
| 165 |
+
while (true) {
|
| 166 |
+
// End of the task
|
| 167 |
+
if (current_group_idx == kNumGroups)
|
| 168 |
+
return false;
|
| 169 |
+
|
| 170 |
+
// Within current group
|
| 171 |
+
num_m_blocks = ceil_div(static_cast<uint32_t>(__ldg(grouped_layout + current_group_idx)), BLOCK_M);
|
| 172 |
+
const auto current_m_block_cumsum = current_m_cumsum + num_m_blocks;
|
| 173 |
+
if (next_block_idx < current_m_block_cumsum * num_n_blocks)
|
| 174 |
+
break;
|
| 175 |
+
|
| 176 |
+
// Move to check the next group
|
| 177 |
+
current_group_idx ++, current_m_cumsum = current_m_block_cumsum;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
get_swizzled_block_idx(next_block_idx - current_m_cumsum * num_n_blocks, m_block_idx, n_block_idx);
|
| 181 |
+
} else if constexpr (kGemmType == GemmType::MGroupedContiguousWithPsumLayout) {
|
| 182 |
+
while (true) {
|
| 183 |
+
// Within current group
|
| 184 |
+
if (next_block_idx < (current_m_block_cumsum + num_m_blocks) * num_n_blocks)
|
| 185 |
+
break;
|
| 186 |
+
|
| 187 |
+
// Move to check the next group
|
| 188 |
+
if (++ current_group_idx == kNumGroups)
|
| 189 |
+
return false;
|
| 190 |
+
|
| 191 |
+
// NOTES: `num_m_blocks` varies with the increase of the group index
|
| 192 |
+
last_psum_m = align(current_psum_m, 128u);
|
| 193 |
+
current_psum_m = __ldg(grouped_layout + current_group_idx);
|
| 194 |
+
current_m_block_cumsum += num_m_blocks;
|
| 195 |
+
num_m_blocks = ceil_div(current_psum_m - last_psum_m, BLOCK_M);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
get_swizzled_block_idx(next_block_idx - current_m_block_cumsum * num_n_blocks, m_block_idx, n_block_idx);
|
| 199 |
+
|
| 200 |
+
// NOTES: `last_psum_m` is aligned with 128
|
| 201 |
+
m_block_idx += last_psum_m / BLOCK_M;
|
| 202 |
+
DG_STATIC_ASSERT(128 % BLOCK_M == 0, "Invalid BLOCK_M");
|
| 203 |
+
} else if constexpr (kGemmType == GemmType::KGroupedContiguous) {
|
| 204 |
+
while (true) {
|
| 205 |
+
// End of the task
|
| 206 |
+
if (current_group_idx == kNumGroups)
|
| 207 |
+
return false;
|
| 208 |
+
|
| 209 |
+
// Within current group
|
| 210 |
+
if (next_block_idx < (current_num_valid_groups + 1) * num_m_blocks * num_n_blocks)
|
| 211 |
+
break;
|
| 212 |
+
|
| 213 |
+
// Move to check the next group
|
| 214 |
+
current_k_cumsum += current_shape_k;
|
| 215 |
+
current_sf_k_cumsum += ceil_div(current_shape_k, SF_K_ALIGNMENT);
|
| 216 |
+
current_num_valid_groups ++;
|
| 217 |
+
|
| 218 |
+
current_group_idx = next_group_idx ++;
|
| 219 |
+
current_shape_k = next_shape_k;
|
| 220 |
+
get_next_k_group(next_group_idx, next_shape_k);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
get_swizzled_block_idx(next_block_idx - current_num_valid_groups * num_m_blocks * num_n_blocks, m_block_idx, n_block_idx);
|
| 224 |
+
} else if constexpr (kGemmType == GemmType::Batched) {
|
| 225 |
+
if (next_block_idx >= num_blocks * kNumGroups)
|
| 226 |
+
return false;
|
| 227 |
+
|
| 228 |
+
current_group_idx = next_block_idx / num_blocks;
|
| 229 |
+
const auto& block_idx = next_block_idx - current_group_idx * num_blocks;
|
| 230 |
+
if constexpr (kIsMulticastOnA) {
|
| 231 |
+
m_block_idx = block_idx / num_n_blocks;
|
| 232 |
+
n_block_idx = block_idx % num_n_blocks;
|
| 233 |
+
} else {
|
| 234 |
+
m_block_idx = block_idx % num_m_blocks;
|
| 235 |
+
n_block_idx = block_idx / num_m_blocks;
|
| 236 |
+
}
|
| 237 |
+
} else {
|
| 238 |
+
if (next_block_idx >= num_blocks)
|
| 239 |
+
return false;
|
| 240 |
+
|
| 241 |
+
// For SM90 only
|
| 242 |
+
// NOTES: we don't have to set `is_peer_cta_alive` for masked grouped GEMM, as it must be aligned
|
| 243 |
+
is_peer_cta_alive = num_n_blocks % kNumMulticast == 0 or // Always aligned on N (constant bypass)
|
| 244 |
+
num_m_blocks % kNumMulticast == 0 or // Always aligned on M (constant bypass)
|
| 245 |
+
(next_block_idx ^ 1) < num_blocks; // Peer CTA in bound
|
| 246 |
+
get_swizzled_block_idx(next_block_idx, m_block_idx, n_block_idx);
|
| 247 |
+
}
|
| 248 |
+
return true;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
// For SM90 only
|
| 252 |
+
__device__ __forceinline__ bool is_tma_multicast_valid(const uint32_t& m_block_idx) const {
|
| 253 |
+
if (num_blocks_in_group == 1)
|
| 254 |
+
return false;
|
| 255 |
+
if constexpr (kGemmType == GemmType::Normal or kGemmType == GemmType::MGroupedMasked or
|
| 256 |
+
kGemmType == GemmType::KGroupedContiguous or kGemmType == GemmType::Batched) {
|
| 257 |
+
return true;
|
| 258 |
+
} else {
|
| 259 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::MGroupedContiguous, "Invalid Gemm type");
|
| 260 |
+
if constexpr (kIsMulticastOnA) {
|
| 261 |
+
return true;
|
| 262 |
+
} else {
|
| 263 |
+
const auto& group_idx = __ldg(grouped_layout + m_block_idx * BLOCK_M);
|
| 264 |
+
const auto& peer_group_idx = __ldg(grouped_layout + (m_block_idx ^ 1) * BLOCK_M);
|
| 265 |
+
return group_idx == peer_group_idx;
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
// For SM90 only
|
| 271 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 272 |
+
__device__ __forceinline__ bool is_computation_valid(const uint32_t& m_block_idx, const uint32_t& m_offset) const {
|
| 273 |
+
if constexpr (kGemmType == GemmType::Normal or kGemmType == GemmType::Batched) {
|
| 274 |
+
return true;
|
| 275 |
+
} else if constexpr (kGemmType == GemmType::MGroupedContiguous) {
|
| 276 |
+
return __ldg(grouped_layout + m_offset + m_block_idx * BLOCK_M) >= 0;
|
| 277 |
+
} else if constexpr (kGemmType == GemmType::MGroupedMasked) {
|
| 278 |
+
return m_offset + m_block_idx * BLOCK_M < __ldg(grouped_layout + current_group_idx);
|
| 279 |
+
} else {
|
| 280 |
+
// Unreachable
|
| 281 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(false);
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
};
|
| 285 |
+
|
| 286 |
+
#pragma clang diagnostic pop
|
| 287 |
+
|
| 288 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/sm100_utils.cuh
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/atom/mma_traits_sm100.hpp>
|
| 4 |
+
#include <cute/arch/mma_sm100_umma.hpp>
|
| 5 |
+
#include <cute/arch/tmem_allocator_sm100.hpp>
|
| 6 |
+
#include <cutlass/arch/barrier.h>
|
| 7 |
+
|
| 8 |
+
#include <deep_gemm/common/utils.cuh>
|
| 9 |
+
#include <deep_gemm/common/tma_utils.cuh>
|
| 10 |
+
|
| 11 |
+
namespace deep_gemm::sm100 {
|
| 12 |
+
|
| 13 |
+
__device__ __forceinline__
|
| 14 |
+
cute::UMMA::SmemDescriptor make_smem_desc(cute::UMMA::LayoutType layout, void* smem_ptr,
|
| 15 |
+
uint32_t stride_byte_offset, uint32_t leading_byte_offset) {
|
| 16 |
+
cute::UMMA::SmemDescriptor desc;
|
| 17 |
+
|
| 18 |
+
// Set the version for SM100
|
| 19 |
+
desc.version_ = 1;
|
| 20 |
+
|
| 21 |
+
// Legacy mode
|
| 22 |
+
desc.lbo_mode_ = 0;
|
| 23 |
+
|
| 24 |
+
// Layout
|
| 25 |
+
desc.layout_type_ = static_cast<uint8_t>(layout);
|
| 26 |
+
|
| 27 |
+
// Start address
|
| 28 |
+
const auto uint_ptr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 29 |
+
desc.start_address_ = static_cast<uint16_t>(uint_ptr >> 4);
|
| 30 |
+
|
| 31 |
+
// Base offset
|
| 32 |
+
desc.base_offset_ = 0;
|
| 33 |
+
|
| 34 |
+
// SBO and LBO
|
| 35 |
+
desc.stride_byte_offset_ = stride_byte_offset >> 4;
|
| 36 |
+
desc.leading_byte_offset_ = leading_byte_offset >> 4;
|
| 37 |
+
|
| 38 |
+
return desc;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
__device__ __forceinline__
|
| 42 |
+
cute::UMMA::SmemDescriptor make_sf_desc(void* smem_ptr) {
|
| 43 |
+
// NOTES: the UTCCP layout is K-major by default
|
| 44 |
+
// Atom size: 8 x 128 bits
|
| 45 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 46 |
+
// Since the UTCCP we used is 128b-wide (only 1 atom on K), so LBO can be zero
|
| 47 |
+
return make_smem_desc(cute::UMMA::LayoutType::SWIZZLE_NONE, smem_ptr, 8 * 16, 0);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
__device__ __forceinline__
|
| 51 |
+
void replace_smem_desc_addr(cute::UMMA::SmemDescriptor& desc, const void* smem_ptr) {
|
| 52 |
+
const auto uint_ptr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 53 |
+
desc.start_address_ = static_cast<uint16_t>(uint_ptr >> 4);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ __forceinline__
|
| 57 |
+
static uint32_t get_atom_base(const cute::UMMA::LayoutType& layout_type) {
|
| 58 |
+
return layout_type == cute::UMMA::LayoutType::SWIZZLE_128B_BASE32B ? 32 : 16;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 62 |
+
template <cute::UMMA::Major kMajorMode, uint32_t kSwizzleMode, bool kUseBase32, typename dtype_t>
|
| 63 |
+
constexpr static cute::UMMA::LayoutType to_umma_layout_type() {
|
| 64 |
+
DG_STATIC_ASSERT(kSwizzleMode == 0 or kSwizzleMode == 16 or
|
| 65 |
+
kSwizzleMode == 32 or kSwizzleMode == 64 or
|
| 66 |
+
kSwizzleMode == 128, "Invalid swizzling mode");
|
| 67 |
+
// A special case
|
| 68 |
+
if constexpr ((cute::is_same_v<dtype_t, float> and kMajorMode == cute::UMMA::Major::MN) or kUseBase32) {
|
| 69 |
+
DG_STATIC_ASSERT(kUseBase32, "Invalid swizzling base");
|
| 70 |
+
return cute::UMMA::LayoutType::SWIZZLE_128B_BASE32B;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Normal cases
|
| 74 |
+
if constexpr (kSwizzleMode == 0) return cute::UMMA::LayoutType::SWIZZLE_NONE;
|
| 75 |
+
if constexpr (kSwizzleMode == 16) return cute::UMMA::LayoutType::SWIZZLE_NONE;
|
| 76 |
+
if constexpr (kSwizzleMode == 32) return cute::UMMA::LayoutType::SWIZZLE_32B;
|
| 77 |
+
if constexpr (kSwizzleMode == 64) return cute::UMMA::LayoutType::SWIZZLE_64B;
|
| 78 |
+
if constexpr (kSwizzleMode == 128) return cute::UMMA::LayoutType::SWIZZLE_128B;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 82 |
+
__device__ __forceinline__
|
| 83 |
+
constexpr uint32_t get_umma_desc_stride_k() {
|
| 84 |
+
return kMajorMode == cute::UMMA::Major::K ? 1 : get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 88 |
+
__device__ __forceinline__
|
| 89 |
+
uint32_t advance_umma_desc_lo(const uint32_t& base, const uint32_t& offset, const uint32_t& k_idx) {
|
| 90 |
+
return base + (((offset + k_idx * get_umma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>()) * static_cast<uint32_t>(sizeof(dtype_t))) >> 4u);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, bool kUseBase32 = false, typename dtype_t>
|
| 94 |
+
__device__ __forceinline__
|
| 95 |
+
cute::UMMA::SmemDescriptor make_umma_desc(dtype_t* base_smem_ptr, uint32_t mn_idx, uint32_t k_idx) {
|
| 96 |
+
const uint32_t stride_k = get_umma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 97 |
+
const auto& layout_type = to_umma_layout_type<kMajorMode, kSwizzleMode, kUseBase32, dtype_t>();
|
| 98 |
+
const auto& num_non_contiguous = 128 / get_atom_base(layout_type);
|
| 99 |
+
if constexpr (kMajorMode == cute::UMMA::Major::K) {
|
| 100 |
+
// NOTES: for K-major layout, the swizzle must be the same as `BLOCK_K * sizeof(dtype_t)`
|
| 101 |
+
// also, atom index must be 0, so that each block has exactly one swizzle atom on the K axis
|
| 102 |
+
DG_STATIC_ASSERT(kSwizzleMode == BLOCK_K * sizeof(dtype_t), "Unexpected value");
|
| 103 |
+
|
| 104 |
+
// Atom size: 8 x `kSwizzleMode` (in bytes, on K)
|
| 105 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 106 |
+
// NOTES: on K, there is only 1 atom as asserted previously, so LBO can be 0
|
| 107 |
+
const uint32_t stride_byte_offset = num_non_contiguous * BLOCK_K * sizeof(dtype_t);
|
| 108 |
+
const uint32_t leading_byte_offset = 0;
|
| 109 |
+
return make_smem_desc(layout_type,
|
| 110 |
+
base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k,
|
| 111 |
+
stride_byte_offset, leading_byte_offset);
|
| 112 |
+
} else {
|
| 113 |
+
constexpr uint32_t BLOCK_MN_ATOM = get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 114 |
+
|
| 115 |
+
// Must have no in-atom MN-idx
|
| 116 |
+
// NOTES: no worries for the runtime assert, the `mn_idx` are constants at compilation time
|
| 117 |
+
DG_DEVICE_ASSERT(mn_idx % BLOCK_MN_ATOM == 0);
|
| 118 |
+
DG_STATIC_ASSERT(kSwizzleMode > 0, "Invalid swizzling");
|
| 119 |
+
|
| 120 |
+
// Atom size: `kSwizzleMode` (in bytes, on MN) x 8
|
| 121 |
+
// NOTES: `kSwizzleMode == 16` mean non-swizzling but interleaving
|
| 122 |
+
// {SBO, LBO} means the byte stride between atoms on {K, MN} for swizzling
|
| 123 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K} for non-swizzling
|
| 124 |
+
uint32_t stride_byte_offset = num_non_contiguous * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 125 |
+
uint32_t leading_byte_offset = BLOCK_K * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 126 |
+
if constexpr (kSwizzleMode == 16)
|
| 127 |
+
swap(stride_byte_offset, leading_byte_offset);
|
| 128 |
+
return make_smem_desc(layout_type,
|
| 129 |
+
base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k,
|
| 130 |
+
stride_byte_offset, leading_byte_offset);
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__device__ __forceinline__
|
| 135 |
+
uint64_t make_runtime_instr_desc_with_sf_id(cute::UMMA::InstrDescriptorBlockScaled desc, const uint32_t& sfa_id, const uint32_t& sfb_id) {
|
| 136 |
+
desc.a_sf_id_ = sfa_id, desc.b_sf_id_ = sfb_id;
|
| 137 |
+
return static_cast<uint64_t>(static_cast<uint32_t>(desc)) << 32;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
template <uint32_t kNumCols>
|
| 141 |
+
__device__ constexpr uint32_t get_num_aligned_tmem_cols() {
|
| 142 |
+
DG_STATIC_ASSERT(kNumCols <= 512, "Too many tensor memory columns");
|
| 143 |
+
if (kNumCols <= 32) return 32;
|
| 144 |
+
if (kNumCols <= 64) return 64;
|
| 145 |
+
if (kNumCols <= 128) return 128;
|
| 146 |
+
if (kNumCols <= 256) return 256;
|
| 147 |
+
return 512;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
__device__ __forceinline__ void tcgen05_before_thread_sync() {
|
| 151 |
+
asm volatile("tcgen05.fence::before_thread_sync;");
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
__device__ __forceinline__ void tcgen05_after_thread_sync() {
|
| 155 |
+
asm volatile("tcgen05.fence::after_thread_sync;");
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
__device__ __forceinline__
|
| 159 |
+
void tma_gather4(const void* desc_ptr, cutlass::arch::ClusterTransactionBarrier &mbarrier, void* smem_ptr, int col_idx, int4 row_idxs, uint64_t cache_hint) {
|
| 160 |
+
uint32_t smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 161 |
+
uint32_t mbarrier_addr = cute::cast_smem_ptr_to_uint(&mbarrier);
|
| 162 |
+
asm volatile(
|
| 163 |
+
"cp.async.bulk.tensor.2d.shared::cta.global.tile::gather4.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint [%0], [%1, {%2, %3, %4, %5, %6}], [%7], %8;\n"
|
| 164 |
+
:
|
| 165 |
+
: "r"(smem_addr), "l"(desc_ptr), "r"(col_idx),
|
| 166 |
+
"r"(row_idxs.x), "r"(row_idxs.y), "r"(row_idxs.z), "r"(row_idxs.w),
|
| 167 |
+
"r"(mbarrier_addr), "l"(cache_hint)
|
| 168 |
+
: "memory"
|
| 169 |
+
);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
// UMMA versions with relaxed assertions
|
| 173 |
+
struct SM100_MMA_F16BF16_SS {
|
| 174 |
+
__device__ static void
|
| 175 |
+
fma(uint64_t const& desc_a,
|
| 176 |
+
uint64_t const& desc_b,
|
| 177 |
+
uint32_t const& tmem_c,
|
| 178 |
+
uint32_t const& scale_c,
|
| 179 |
+
uint64_t const& desc) {
|
| 180 |
+
asm volatile(
|
| 181 |
+
"{\n\t"
|
| 182 |
+
".reg .pred p;\n\t"
|
| 183 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 184 |
+
"tcgen05.mma.cta_group::1.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 185 |
+
"}\n"
|
| 186 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 187 |
+
}
|
| 188 |
+
};
|
| 189 |
+
|
| 190 |
+
struct SM100_MMA_F16BF16_2x1SM_SS {
|
| 191 |
+
__device__ static void
|
| 192 |
+
fma(uint64_t const& desc_a,
|
| 193 |
+
uint64_t const& desc_b,
|
| 194 |
+
uint32_t const& tmem_c,
|
| 195 |
+
uint32_t const& scale_c,
|
| 196 |
+
uint64_t const& desc) {
|
| 197 |
+
asm volatile(
|
| 198 |
+
"{\n\t"
|
| 199 |
+
".reg .pred p;\n\t"
|
| 200 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 201 |
+
"tcgen05.mma.cta_group::2.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 202 |
+
"}\n"
|
| 203 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 204 |
+
}
|
| 205 |
+
};
|
| 206 |
+
|
| 207 |
+
struct SM100_MMA_MXF8F6F4_SS {
|
| 208 |
+
__device__ static void
|
| 209 |
+
fma(uint64_t const& desc_a,
|
| 210 |
+
uint64_t const& desc_b,
|
| 211 |
+
uint32_t const& tmem_c,
|
| 212 |
+
uint32_t const& scale_c,
|
| 213 |
+
uint64_t const& desc,
|
| 214 |
+
uint32_t const& tmem_sfa,
|
| 215 |
+
uint32_t const& tmem_sfb) {
|
| 216 |
+
asm volatile(
|
| 217 |
+
"{\n\t"
|
| 218 |
+
".reg .pred p;\n\t"
|
| 219 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 220 |
+
"tcgen05.mma.cta_group::1.kind::mxf8f6f4.block_scale [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 221 |
+
"}\n"
|
| 222 |
+
:
|
| 223 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c),
|
| 224 |
+
"r"(tmem_sfa), "r"(tmem_sfb));
|
| 225 |
+
}
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
struct SM100_MMA_MXF8F6F4_2x1SM_SS {
|
| 229 |
+
__device__ static void
|
| 230 |
+
fma(uint64_t const& desc_a,
|
| 231 |
+
uint64_t const& desc_b,
|
| 232 |
+
uint32_t const& tmem_c,
|
| 233 |
+
uint32_t const& scale_c,
|
| 234 |
+
uint64_t const& desc,
|
| 235 |
+
uint32_t const& tmem_sfa,
|
| 236 |
+
uint32_t const& tmem_sfb) {
|
| 237 |
+
asm volatile(
|
| 238 |
+
"{\n\t"
|
| 239 |
+
".reg .pred p;\n\t"
|
| 240 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 241 |
+
"tcgen05.mma.cta_group::2.kind::mxf8f6f4.block_scale [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 242 |
+
"}\n"
|
| 243 |
+
:
|
| 244 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c),
|
| 245 |
+
"r"(tmem_sfa), "r"(tmem_sfb));
|
| 246 |
+
}
|
| 247 |
+
};
|
| 248 |
+
|
| 249 |
+
struct SM100_MMA_F16BF16_WS_SS {
|
| 250 |
+
__device__ static void
|
| 251 |
+
fma(uint64_t const& desc_a,
|
| 252 |
+
uint64_t const& desc_b,
|
| 253 |
+
uint32_t const& tmem_c,
|
| 254 |
+
uint32_t const& scale_c,
|
| 255 |
+
uint64_t const& desc) {
|
| 256 |
+
asm volatile(
|
| 257 |
+
"{\n\t"
|
| 258 |
+
".reg .pred p;\n\t"
|
| 259 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 260 |
+
"tcgen05.mma.ws.cta_group::1.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 261 |
+
"}\n"
|
| 262 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 263 |
+
}
|
| 264 |
+
};
|
| 265 |
+
|
| 266 |
+
} // namespace `deep_gemm::sm100`
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/sm90_utils.cuh
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 4 |
+
#include <cute/arch/mma_sm90_desc.hpp>
|
| 5 |
+
#include <cute/arch/mma_sm90_gmma.hpp>
|
| 6 |
+
#include <cute/arch/mma_sm90_gmma_ext.hpp>
|
| 7 |
+
#include <cute/arch/mma_sm100_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/utils.cuh>
|
| 10 |
+
#include <deep_gemm/common/sm100_utils.cuh>
|
| 11 |
+
#include <deep_gemm/common/tma_utils.cuh>
|
| 12 |
+
|
| 13 |
+
namespace deep_gemm::sm90 {
|
| 14 |
+
|
| 15 |
+
template <int N_, typename MMA>
|
| 16 |
+
struct FP8MMA {
|
| 17 |
+
|
| 18 |
+
template <size_t ...Idx>
|
| 19 |
+
__forceinline__ __device__ static void call_fma_impl(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 20 |
+
using namespace cute::SM90::GMMA;
|
| 21 |
+
MMA::fma(desc_a, desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
__forceinline__ __device__ static void wgmma(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 25 |
+
call_fma_impl(desc_a, desc_b, d, scale_d, cute::make_index_sequence<N_/2>{});
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
static constexpr int M = 64;
|
| 29 |
+
static constexpr int N = N_;
|
| 30 |
+
static constexpr int K = 32;
|
| 31 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
template <int N>
|
| 35 |
+
struct FP8MMASelector {
|
| 36 |
+
|
| 37 |
+
static constexpr auto select_mma() {
|
| 38 |
+
using namespace cute::SM90::GMMA;
|
| 39 |
+
if constexpr (N == 8) return MMA_64x8x32_F32E4M3E4M3_SS_TN();
|
| 40 |
+
if constexpr (N == 16) return MMA_64x16x32_F32E4M3E4M3_SS_TN();
|
| 41 |
+
if constexpr (N == 24) return MMA_64x24x32_F32E4M3E4M3_SS_TN();
|
| 42 |
+
if constexpr (N == 32) return MMA_64x32x32_F32E4M3E4M3_SS_TN();
|
| 43 |
+
if constexpr (N == 40) return MMA_64x40x32_F32E4M3E4M3_SS_TN();
|
| 44 |
+
if constexpr (N == 48) return MMA_64x48x32_F32E4M3E4M3_SS_TN();
|
| 45 |
+
if constexpr (N == 56) return MMA_64x56x32_F32E4M3E4M3_SS_TN();
|
| 46 |
+
if constexpr (N == 64) return MMA_64x64x32_F32E4M3E4M3_SS_TN();
|
| 47 |
+
if constexpr (N == 72) return MMA_64x72x32_F32E4M3E4M3_SS_TN();
|
| 48 |
+
if constexpr (N == 80) return MMA_64x80x32_F32E4M3E4M3_SS_TN();
|
| 49 |
+
if constexpr (N == 88) return MMA_64x88x32_F32E4M3E4M3_SS_TN();
|
| 50 |
+
if constexpr (N == 96) return MMA_64x96x32_F32E4M3E4M3_SS_TN();
|
| 51 |
+
if constexpr (N == 104) return MMA_64x104x32_F32E4M3E4M3_SS_TN();
|
| 52 |
+
if constexpr (N == 112) return MMA_64x112x32_F32E4M3E4M3_SS_TN();
|
| 53 |
+
if constexpr (N == 120) return MMA_64x120x32_F32E4M3E4M3_SS_TN();
|
| 54 |
+
if constexpr (N == 128) return MMA_64x128x32_F32E4M3E4M3_SS_TN();
|
| 55 |
+
if constexpr (N == 136) return MMA_64x136x32_F32E4M3E4M3_SS_TN();
|
| 56 |
+
if constexpr (N == 144) return MMA_64x144x32_F32E4M3E4M3_SS_TN();
|
| 57 |
+
if constexpr (N == 152) return MMA_64x152x32_F32E4M3E4M3_SS_TN();
|
| 58 |
+
if constexpr (N == 160) return MMA_64x160x32_F32E4M3E4M3_SS_TN();
|
| 59 |
+
if constexpr (N == 168) return MMA_64x168x32_F32E4M3E4M3_SS_TN();
|
| 60 |
+
if constexpr (N == 176) return MMA_64x176x32_F32E4M3E4M3_SS_TN();
|
| 61 |
+
if constexpr (N == 184) return MMA_64x184x32_F32E4M3E4M3_SS_TN();
|
| 62 |
+
if constexpr (N == 192) return MMA_64x192x32_F32E4M3E4M3_SS_TN();
|
| 63 |
+
if constexpr (N == 200) return MMA_64x200x32_F32E4M3E4M3_SS_TN();
|
| 64 |
+
if constexpr (N == 208) return MMA_64x208x32_F32E4M3E4M3_SS_TN();
|
| 65 |
+
if constexpr (N == 216) return MMA_64x216x32_F32E4M3E4M3_SS_TN();
|
| 66 |
+
if constexpr (N == 224) return MMA_64x224x32_F32E4M3E4M3_SS_TN();
|
| 67 |
+
if constexpr (N == 232) return MMA_64x232x32_F32E4M3E4M3_SS_TN();
|
| 68 |
+
if constexpr (N == 240) return MMA_64x240x32_F32E4M3E4M3_SS_TN();
|
| 69 |
+
if constexpr (N == 248) return MMA_64x248x32_F32E4M3E4M3_SS_TN();
|
| 70 |
+
if constexpr (N == 256) return MMA_64x256x32_F32E4M3E4M3_SS_TN();
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
static constexpr auto select_type() {
|
| 74 |
+
return FP8MMA<N, decltype(select_mma())>();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
using type = decltype(select_type());
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
template <int N_, typename MMA>
|
| 81 |
+
struct BF16MMA {
|
| 82 |
+
|
| 83 |
+
template <size_t ...Idx>
|
| 84 |
+
__forceinline__ __device__ static void call_fma_impl(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 85 |
+
using namespace cute::SM90::GMMA;
|
| 86 |
+
MMA::fma(desc_a, desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
__forceinline__ __device__ static void wgmma(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 90 |
+
call_fma_impl(desc_a, desc_b, d, scale_d, cute::make_index_sequence<N_/2>{});
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
static constexpr int M = 64;
|
| 94 |
+
static constexpr int N = N_;
|
| 95 |
+
static constexpr int K = 16;
|
| 96 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
template <cute::UMMA::Major kMajor>
|
| 100 |
+
constexpr cute::SM90::GMMA::Major to_sm90_major() {
|
| 101 |
+
DG_STATIC_ASSERT(kMajor == cute::UMMA::Major::K or kMajor == cute::UMMA::Major::MN, "Invalid major-ness");
|
| 102 |
+
return kMajor == cute::UMMA::Major::K ? cute::SM90::GMMA::Major::K : cute::SM90::GMMA::Major::MN;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
template <int N,
|
| 106 |
+
cute::UMMA::Major kMajorA = cute::UMMA::Major::K,
|
| 107 |
+
cute::UMMA::Major kMajorB = cute::UMMA::Major::K>
|
| 108 |
+
struct BF16MMASelector {
|
| 109 |
+
|
| 110 |
+
static constexpr auto select_mma() {
|
| 111 |
+
using namespace cute::SM90::GMMA;
|
| 112 |
+
constexpr auto kGMMAMajorA = to_sm90_major<kMajorA>();
|
| 113 |
+
constexpr auto kGMMAMajorB = to_sm90_major<kMajorB>();
|
| 114 |
+
if constexpr (N == 8) return MMA_64x8x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 115 |
+
if constexpr (N == 16) return MMA_64x16x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 116 |
+
if constexpr (N == 24) return MMA_64x24x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 117 |
+
if constexpr (N == 32) return MMA_64x32x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 118 |
+
if constexpr (N == 40) return MMA_64x40x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 119 |
+
if constexpr (N == 48) return MMA_64x48x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 120 |
+
if constexpr (N == 56) return MMA_64x56x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 121 |
+
if constexpr (N == 64) return MMA_64x64x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 122 |
+
if constexpr (N == 72) return MMA_64x72x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 123 |
+
if constexpr (N == 80) return MMA_64x80x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 124 |
+
if constexpr (N == 88) return MMA_64x88x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 125 |
+
if constexpr (N == 96) return MMA_64x96x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 126 |
+
if constexpr (N == 104) return MMA_64x104x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 127 |
+
if constexpr (N == 112) return MMA_64x112x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 128 |
+
if constexpr (N == 120) return MMA_64x120x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 129 |
+
if constexpr (N == 128) return MMA_64x128x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 130 |
+
if constexpr (N == 136) return MMA_64x136x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 131 |
+
if constexpr (N == 144) return MMA_64x144x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 132 |
+
if constexpr (N == 152) return MMA_64x152x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 133 |
+
if constexpr (N == 160) return MMA_64x160x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 134 |
+
if constexpr (N == 168) return MMA_64x168x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 135 |
+
if constexpr (N == 176) return MMA_64x176x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 136 |
+
if constexpr (N == 184) return MMA_64x184x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 137 |
+
if constexpr (N == 192) return MMA_64x192x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 138 |
+
if constexpr (N == 200) return MMA_64x200x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 139 |
+
if constexpr (N == 208) return MMA_64x208x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 140 |
+
if constexpr (N == 216) return MMA_64x216x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 141 |
+
if constexpr (N == 224) return MMA_64x224x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 142 |
+
if constexpr (N == 232) return MMA_64x232x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 143 |
+
if constexpr (N == 240) return MMA_64x240x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 144 |
+
if constexpr (N == 248) return MMA_64x248x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 145 |
+
if constexpr (N == 256) return MMA_64x256x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
static constexpr auto select_type() {
|
| 149 |
+
return BF16MMA<N, decltype(select_mma())>();
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
using type = decltype(select_type());
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
template <int N_, typename MMA>
|
| 156 |
+
struct TF32MMARS {
|
| 157 |
+
|
| 158 |
+
template <size_t ...Idx>
|
| 159 |
+
__forceinline__ __device__ static void call_fma_impl(uint32_t* a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 160 |
+
using namespace cute::SM90::GMMA;
|
| 161 |
+
MMA::fma(a[0], a[1], a[2], a[3], desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
__forceinline__ __device__ static void wgmma(float* a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 165 |
+
call_fma_impl(reinterpret_cast<uint32_t*>(a), desc_b, d, scale_d, cute::make_index_sequence<N_/2>{});
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static constexpr int M = 64;
|
| 169 |
+
static constexpr int N = N_;
|
| 170 |
+
static constexpr int K = 8;
|
| 171 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 172 |
+
};
|
| 173 |
+
|
| 174 |
+
template <int N, bool kUseRS = true>
|
| 175 |
+
struct TF32MMASelector {
|
| 176 |
+
|
| 177 |
+
static constexpr auto select_mma() {
|
| 178 |
+
using namespace cute::SM90::GMMA;
|
| 179 |
+
if constexpr (kUseRS) {
|
| 180 |
+
if constexpr (N == 8) return MMA_64x8x8_F32TF32TF32_RS_TN();
|
| 181 |
+
if constexpr (N == 16) return MMA_64x16x8_F32TF32TF32_RS_TN();
|
| 182 |
+
if constexpr (N == 32) return MMA_64x32x8_F32TF32TF32_RS_TN();
|
| 183 |
+
if constexpr (N == 64) return MMA_64x64x8_F32TF32TF32_RS_TN();
|
| 184 |
+
if constexpr (N == 128) return MMA_64x128x8_F32TF32TF32_RS_TN();
|
| 185 |
+
if constexpr (N == 256) return MMA_64x256x8_F32TF32TF32_RS_TN();
|
| 186 |
+
DG_STATIC_ASSERT(N == 8 or N == 16 or N == 32 or N == 64 or N == 128 or N == 256, "Invalid N");
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
static constexpr auto select_type() {
|
| 191 |
+
if constexpr (kUseRS) {
|
| 192 |
+
return TF32MMARS<N, decltype(select_mma())>();
|
| 193 |
+
} else {
|
| 194 |
+
DG_STATIC_ASSERT(kUseRS, "SS mode is not supported for TF32MMASelector for now");
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
using type = decltype(select_type());
|
| 199 |
+
};
|
| 200 |
+
|
| 201 |
+
template <typename dtype_t>
|
| 202 |
+
struct SM90_U32x2_STSM_N {
|
| 203 |
+
__device__ __forceinline__ static void
|
| 204 |
+
copy(dtype_t src_0, dtype_t src_1, void* smem_dst) {
|
| 205 |
+
const uint32_t src[2] = {*reinterpret_cast<uint32_t*>(&src_0), *reinterpret_cast<uint32_t*>(&src_1)};
|
| 206 |
+
asm volatile("stmatrix.sync.aligned.x2.m8n8.shared.b16 [%0], {%1, %2};\n"
|
| 207 |
+
:: "l"(__cvta_generic_to_shared(smem_dst)), "r"(src[0]), "r"(src[1]));
|
| 208 |
+
}
|
| 209 |
+
};
|
| 210 |
+
|
| 211 |
+
struct SM90_U32x2_LDSM_N {
|
| 212 |
+
__device__ __forceinline__ static void
|
| 213 |
+
copy(uint32_t& dst_0, uint32_t& dst_1, void* smem_src) {
|
| 214 |
+
asm volatile("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];\n"
|
| 215 |
+
: "=r"(dst_0), "=r"(dst_1)
|
| 216 |
+
: "l"(__cvta_generic_to_shared(smem_src)));
|
| 217 |
+
}
|
| 218 |
+
};
|
| 219 |
+
|
| 220 |
+
struct SM90_U32x4_LDSM_N {
|
| 221 |
+
__device__ __forceinline__ static void
|
| 222 |
+
copy(uint32_t& dst_0, uint32_t& dst_1, uint32_t& dst_2, uint32_t& dst_3, void* smem_src) {
|
| 223 |
+
asm volatile("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n"
|
| 224 |
+
: "=r"(dst_0), "=r"(dst_1), "=r"(dst_2), "=r"(dst_3)
|
| 225 |
+
: "l"(__cvta_generic_to_shared(smem_src)));
|
| 226 |
+
}
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
__forceinline__ __device__ void warpgroup_arrive() {
|
| 230 |
+
asm volatile("wgmma.fence.sync.aligned;\n" ::: "memory");
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
__forceinline__ __device__ void warpgroup_commit_batch() {
|
| 234 |
+
asm volatile("wgmma.commit_group.sync.aligned;\n" ::: "memory");
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
__forceinline__ __device__ void warpgroup_fence_operand(float& reg) {
|
| 238 |
+
asm volatile("" : "+f"(reg) :: "memory");
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
template <int N>
|
| 242 |
+
__forceinline__ __device__ void warpgroup_wait() {
|
| 243 |
+
DG_STATIC_ASSERT(N >= 0 and N <= 7, "WGMMA wait: N must be in range [0, 7]");
|
| 244 |
+
asm volatile("wgmma.wait_group.sync.aligned %0;\n" :: "n"(N) : "memory");
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
template <class PointerType>
|
| 248 |
+
__device__ cute::GmmaDescriptor make_smem_desc(PointerType smem_ptr, const int& layout_type,
|
| 249 |
+
const int& leading_byte_offset = 0,
|
| 250 |
+
const int& stride_byte_offset = 1024) {
|
| 251 |
+
// NOTES: the default LBO and SBO are for K-major types
|
| 252 |
+
cute::GmmaDescriptor desc;
|
| 253 |
+
const auto& uint_ptr = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr));
|
| 254 |
+
desc.bitfield.start_address_ = uint_ptr >> 4;
|
| 255 |
+
desc.bitfield.layout_type_ = layout_type;
|
| 256 |
+
desc.bitfield.leading_byte_offset_ = leading_byte_offset >> 4;
|
| 257 |
+
desc.bitfield.stride_byte_offset_ = stride_byte_offset >> 4;
|
| 258 |
+
desc.bitfield.base_offset_ = 0;
|
| 259 |
+
return desc;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
template <uint32_t BLOCK_INNER, uint32_t kSwizzleMode, typename dtype_t>
|
| 263 |
+
constexpr uint32_t get_inner_block_atom_size() {
|
| 264 |
+
return kSwizzleMode == 0 ? BLOCK_INNER : kSwizzleMode / sizeof(dtype_t);
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 268 |
+
__device__ __forceinline__
|
| 269 |
+
constexpr uint32_t get_gmma_desc_stride_k() {
|
| 270 |
+
return kMajorMode == cute::UMMA::Major::K ? 1 : get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 274 |
+
template <cute::UMMA::Major kMajorMode, uint32_t kSwizzleMode, typename dtype_t>
|
| 275 |
+
constexpr static cute::SM90::GMMA::LayoutType to_gmma_layout_type() {
|
| 276 |
+
DG_STATIC_ASSERT(kSwizzleMode == 0 or kSwizzleMode == 16 or
|
| 277 |
+
kSwizzleMode == 32 or kSwizzleMode == 64 or
|
| 278 |
+
kSwizzleMode == 128, "Invalid swizzling mode");
|
| 279 |
+
|
| 280 |
+
// Normal cases
|
| 281 |
+
if constexpr (kSwizzleMode == 0) return cute::SM90::GMMA::LayoutType::INTERLEAVE;
|
| 282 |
+
if constexpr (kSwizzleMode == 16) return cute::SM90::GMMA::LayoutType::INTERLEAVE;
|
| 283 |
+
if constexpr (kSwizzleMode == 32) return cute::SM90::GMMA::LayoutType::B32;
|
| 284 |
+
if constexpr (kSwizzleMode == 64) return cute::SM90::GMMA::LayoutType::B64;
|
| 285 |
+
if constexpr (kSwizzleMode == 128) return cute::SM90::GMMA::LayoutType::B128;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, typename dtype_t>
|
| 289 |
+
__device__ __forceinline__
|
| 290 |
+
uint32_t advance_gmma_desc_lo(const uint32_t& base, const uint32_t& mn_idx, const uint32_t& k_idx, const uint32_t& offset = 0) {
|
| 291 |
+
return base + (((offset + mn_idx * BLOCK_K + k_idx * get_gmma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>()) * static_cast<uint32_t>(sizeof(dtype_t))) >> 4u);
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, typename dtype_t>
|
| 295 |
+
__device__ __forceinline__
|
| 296 |
+
cute::GmmaDescriptor make_gmma_desc(dtype_t* base_smem_ptr, uint32_t mn_idx, uint32_t k_idx) {
|
| 297 |
+
const uint32_t stride_k = get_gmma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 298 |
+
const auto& layout_type = to_gmma_layout_type<kMajorMode, kSwizzleMode, dtype_t>();
|
| 299 |
+
constexpr uint32_t num_non_contiguous = 128 / 16;
|
| 300 |
+
if constexpr (kMajorMode == cute::UMMA::Major::K) {
|
| 301 |
+
// NOTES: for K-major layout, the swizzle must be 128B (also, atom index must be 0), as `BLOCK_K` is always 128
|
| 302 |
+
DG_STATIC_ASSERT(kSwizzleMode == BLOCK_K * sizeof(dtype_t), "Unexpected value");
|
| 303 |
+
|
| 304 |
+
// Atom size: 8 x `kSwizzleMode` (in bytes, on K)
|
| 305 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 306 |
+
// NOTES: on K, there is only 1 atom as asserted previously, so LBO can be 0
|
| 307 |
+
const uint32_t stride_byte_offset = num_non_contiguous * BLOCK_K * sizeof(dtype_t);
|
| 308 |
+
const uint32_t leading_byte_offset = 0;
|
| 309 |
+
return make_smem_desc(base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k, static_cast<uint32_t>(layout_type),
|
| 310 |
+
leading_byte_offset, stride_byte_offset);
|
| 311 |
+
} else {
|
| 312 |
+
constexpr uint32_t BLOCK_MN_ATOM = get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 313 |
+
|
| 314 |
+
// Must have no in-atom MN-idx
|
| 315 |
+
// NOTES: no worries for the runtime assert, the `mn_idx` are constants at compilation time
|
| 316 |
+
DG_DEVICE_ASSERT(mn_idx % BLOCK_MN_ATOM == 0);
|
| 317 |
+
DG_STATIC_ASSERT(kSwizzleMode > 0, "Invalid swizzling");
|
| 318 |
+
|
| 319 |
+
// Atom size: `kSwizzleMode` (in bytes, on MN) x 8
|
| 320 |
+
// NOTES: `kSwizzleMode == 16` mean non-swizzling but interleaving
|
| 321 |
+
// {SBO, LBO} means the byte stride between atoms on {K, MN} for swizzling
|
| 322 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K} for non-swizzling
|
| 323 |
+
uint32_t stride_byte_offset = num_non_contiguous * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 324 |
+
uint32_t leading_byte_offset = BLOCK_K * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 325 |
+
if constexpr (kSwizzleMode == 16)
|
| 326 |
+
swap(stride_byte_offset, leading_byte_offset);
|
| 327 |
+
return make_smem_desc(base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k, static_cast<uint32_t>(layout_type),
|
| 328 |
+
leading_byte_offset, stride_byte_offset);
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
} // namespace `deep_gemm::sm90`
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/tma_copy.cuh
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/copy_sm90_tma.hpp>
|
| 4 |
+
#include <cute/arch/copy_sm100_tma.hpp>
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/exception.cuh>
|
| 8 |
+
|
| 9 |
+
namespace deep_gemm::tma {
|
| 10 |
+
|
| 11 |
+
template <uint32_t BLOCK_INNER, uint32_t kSwizzleMode, typename dtype_t>
|
| 12 |
+
constexpr uint32_t get_inner_block_atom_size() {
|
| 13 |
+
return kSwizzleMode == 0 ? BLOCK_INNER : kSwizzleMode / sizeof(dtype_t);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
template <uint32_t BLOCK_INNER, uint32_t BLOCK_OUTER,
|
| 17 |
+
uint32_t kSwizzleMode,
|
| 18 |
+
typename dtype_t, bool kIs3DTMA = false>
|
| 19 |
+
CUTLASS_DEVICE void
|
| 20 |
+
copy(void const* desc_ptr, cutlass::arch::ClusterTransactionBarrier* barrier_ptr,
|
| 21 |
+
dtype_t* smem_ptr, const uint32_t& inner_idx, const uint32_t& outer_idx,
|
| 22 |
+
const uint32_t& num_tma_multicast = 1, const uint32_t& batch_idx = 0) {
|
| 23 |
+
DG_STATIC_ASSERT(static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL) ==
|
| 24 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL), "Invalid cache hint");
|
| 25 |
+
constexpr uint32_t BLOCK_INNER_ATOM = get_inner_block_atom_size<BLOCK_INNER, kSwizzleMode, dtype_t>();
|
| 26 |
+
|
| 27 |
+
if constexpr (not kIs3DTMA) {
|
| 28 |
+
if (num_tma_multicast == 1) {
|
| 29 |
+
#pragma unroll
|
| 30 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 31 |
+
cute::SM90_TMA_LOAD_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 32 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 33 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 34 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 35 |
+
}
|
| 36 |
+
} else {
|
| 37 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000))
|
| 38 |
+
// 2-CTA function will send signals to the leader CTA only
|
| 39 |
+
#pragma unroll
|
| 40 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 41 |
+
cute::SM100_TMA_2SM_LOAD_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 42 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 43 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 44 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 45 |
+
}
|
| 46 |
+
#elif (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900))
|
| 47 |
+
if (cute::block_rank_in_cluster() == 0) {
|
| 48 |
+
#pragma unroll
|
| 49 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 50 |
+
cute::SM90_TMA_LOAD_MULTICAST_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 51 |
+
(1 << num_tma_multicast) - 1, static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL),
|
| 52 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 53 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
#endif
|
| 57 |
+
}
|
| 58 |
+
} else {
|
| 59 |
+
if (num_tma_multicast == 1) {
|
| 60 |
+
#pragma unroll
|
| 61 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 62 |
+
cute::SM90_TMA_LOAD_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 63 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 64 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 65 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 66 |
+
}
|
| 67 |
+
} else {
|
| 68 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000))
|
| 69 |
+
// 2-CTA function will send signals to the leader CTA only
|
| 70 |
+
#pragma unroll
|
| 71 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 72 |
+
cute::SM100_TMA_2SM_LOAD_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 73 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 74 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 75 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 76 |
+
}
|
| 77 |
+
#elif (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900))
|
| 78 |
+
if (cute::block_rank_in_cluster() == 0) {
|
| 79 |
+
#pragma unroll
|
| 80 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 81 |
+
cute::SM90_TMA_LOAD_MULTICAST_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 82 |
+
(1 << num_tma_multicast) - 1, static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL),
|
| 83 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 84 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
#endif
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
} // namespace deep_gemm::tma
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/tma_utils.cuh
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/copy_sm90_tma.hpp>
|
| 4 |
+
#include <cute/arch/copy_sm100_tma.hpp>
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
namespace deep_gemm {
|
| 8 |
+
|
| 9 |
+
template <uint32_t BLOCK_INNER, uint32_t kSwizzleMode, typename dtype_t>
|
| 10 |
+
constexpr uint32_t get_inner_block_atom_size() {
|
| 11 |
+
return kSwizzleMode == 0 ? BLOCK_INNER : kSwizzleMode / sizeof(dtype_t);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
template <uint32_t BLOCK_INNER, uint32_t BLOCK_OUTER,
|
| 15 |
+
uint32_t kSwizzleMode,
|
| 16 |
+
typename dtype_t, bool kIs3DTMA = false>
|
| 17 |
+
__device__ __forceinline__ void
|
| 18 |
+
tma_copy(void const* desc_ptr, cutlass::arch::ClusterTransactionBarrier* barrier_ptr,
|
| 19 |
+
dtype_t* smem_ptr, const uint32_t& inner_idx, const uint32_t& outer_idx,
|
| 20 |
+
const uint32_t& num_tma_multicast = 1, const uint32_t& batch_idx = 0) {
|
| 21 |
+
DG_STATIC_ASSERT(static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL) ==
|
| 22 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL), "Invalid cache hint");
|
| 23 |
+
constexpr uint32_t BLOCK_INNER_ATOM = get_inner_block_atom_size<BLOCK_INNER, kSwizzleMode, dtype_t>();
|
| 24 |
+
|
| 25 |
+
if constexpr (not kIs3DTMA) {
|
| 26 |
+
if (num_tma_multicast == 1) {
|
| 27 |
+
#pragma unroll
|
| 28 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 29 |
+
cute::SM90_TMA_LOAD_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 30 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 31 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 32 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 33 |
+
}
|
| 34 |
+
} else {
|
| 35 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000))
|
| 36 |
+
// 2-CTA function will send signals to the leader CTA only
|
| 37 |
+
#pragma unroll
|
| 38 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 39 |
+
cute::SM100_TMA_2SM_LOAD_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 40 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 41 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 42 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 43 |
+
}
|
| 44 |
+
#elif (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900))
|
| 45 |
+
if (cute::block_rank_in_cluster() == 0) {
|
| 46 |
+
#pragma unroll
|
| 47 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 48 |
+
cute::SM90_TMA_LOAD_MULTICAST_2D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 49 |
+
(1 << num_tma_multicast) - 1, static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL),
|
| 50 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 51 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
#endif
|
| 55 |
+
}
|
| 56 |
+
} else {
|
| 57 |
+
if (num_tma_multicast == 1) {
|
| 58 |
+
#pragma unroll
|
| 59 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 60 |
+
cute::SM90_TMA_LOAD_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 61 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 62 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 63 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 64 |
+
}
|
| 65 |
+
} else {
|
| 66 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000))
|
| 67 |
+
// 2-CTA function will send signals to the leader CTA only
|
| 68 |
+
#pragma unroll
|
| 69 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 70 |
+
cute::SM100_TMA_2SM_LOAD_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 71 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 72 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 73 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 74 |
+
}
|
| 75 |
+
#elif (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900))
|
| 76 |
+
if (cute::block_rank_in_cluster() == 0) {
|
| 77 |
+
#pragma unroll
|
| 78 |
+
for (uint32_t i = 0; i < BLOCK_INNER / BLOCK_INNER_ATOM; ++ i) {
|
| 79 |
+
cute::SM90_TMA_LOAD_MULTICAST_3D::copy(desc_ptr, reinterpret_cast<uint64_t*>(barrier_ptr),
|
| 80 |
+
(1 << num_tma_multicast) - 1, static_cast<uint64_t>(cute::TMA::CacheHintSm90::EVICT_NORMAL),
|
| 81 |
+
smem_ptr + i * BLOCK_OUTER * BLOCK_INNER_ATOM,
|
| 82 |
+
inner_idx + i * BLOCK_INNER_ATOM, outer_idx, batch_idx);
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
#endif
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// Tensormap related
|
| 91 |
+
__device__ __forceinline__ void tensor_map_release_cta() {
|
| 92 |
+
asm volatile ("fence.proxy.tensormap::generic.release.cta;");
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ __forceinline__ void tensor_map_acquire_cta(const cute::TmaDescriptor* gmem_desc_ptr) {
|
| 96 |
+
auto gmem_int_desc = reinterpret_cast<uint64_t>(gmem_desc_ptr);
|
| 97 |
+
asm volatile ("fence.proxy.tensormap::generic.acquire.cta [%0], 128;" :: "l"(gmem_int_desc) : "memory");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__device__ __forceinline__ void tensor_map_replace_global_addr_in_smem(cute::TmaDescriptor* smem_desc, const void* new_addr) {
|
| 101 |
+
auto smem_int_desc = static_cast<uint32_t>(__cvta_generic_to_shared(smem_desc));
|
| 102 |
+
const auto new_int64_addr = reinterpret_cast<uint64_t>(new_addr);
|
| 103 |
+
asm volatile ("tensormap.replace.tile.global_address.shared::cta.b1024.b64 [%0], %1;" :: "r"(smem_int_desc), "l"(new_int64_addr));
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
__device__ __forceinline__ void tensor_map_replace_global_inner_dim_stride_in_smem(cute::TmaDescriptor* smem_desc, const uint32_t& new_dim, const uint64_t& new_stride) {
|
| 107 |
+
auto smem_int_desc = __cvta_generic_to_shared(smem_desc);
|
| 108 |
+
asm volatile ("tensormap.replace.tile.global_dim.shared::cta.b1024.b32 [%0], 0, %1;" :: "l"(smem_int_desc), "r"(new_dim));
|
| 109 |
+
#if ((__CUDACC_VER_MAJOR__ > 12) or ((__CUDACC_VER_MAJOR__ == 12) and (__CUDACC_VER_MINOR__ >= 3)))
|
| 110 |
+
asm volatile("tensormap.replace.tile.global_stride.shared::cta.b1024.b64 [%0], 0, %1;" :: "l"(smem_int_desc), "l"(new_stride));
|
| 111 |
+
#else
|
| 112 |
+
DG_STATIC_ASSERT(false, "Invalid CUDA version");
|
| 113 |
+
#endif
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
} // namespace `deep_gemm`
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/types.cuh
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/mma_sm100_desc.hpp>
|
| 4 |
+
|
| 5 |
+
namespace deep_gemm {
|
| 6 |
+
|
| 7 |
+
enum class MmaKind {
|
| 8 |
+
BF16 = 0,
|
| 9 |
+
MXFP8FP4 = 1,
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
constexpr CUTLASS_HOST_DEVICE int get_element_size(const MmaKind& mma_kind) {
|
| 13 |
+
switch (mma_kind) {
|
| 14 |
+
case MmaKind::BF16: return 2;
|
| 15 |
+
case MmaKind::MXFP8FP4: return 1;
|
| 16 |
+
default: return 0;
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
enum class GemmType {
|
| 21 |
+
Normal = 0,
|
| 22 |
+
MGroupedContiguous = 1,
|
| 23 |
+
MGroupedMasked = 2,
|
| 24 |
+
KGroupedContiguous = 3,
|
| 25 |
+
Batched = 4,
|
| 26 |
+
MGroupedContiguousWithPsumLayout = 5,
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
constexpr CUTLASS_HOST_DEVICE bool is_m_grouped_contiguous(const GemmType& gemm_type) {
|
| 30 |
+
switch (gemm_type) {
|
| 31 |
+
case GemmType::MGroupedContiguous: return true;
|
| 32 |
+
case GemmType::MGroupedContiguousWithPsumLayout: return true;
|
| 33 |
+
default: return false;
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
enum class KernelType {
|
| 38 |
+
Kernel1D1D = 0,
|
| 39 |
+
Kernel1D2D = 1,
|
| 40 |
+
KernelNoSF = 2
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/types.hpp
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
namespace deep_gemm {
|
| 4 |
+
|
| 5 |
+
enum class MmaKind {
|
| 6 |
+
BF16 = 0,
|
| 7 |
+
MXFP8FP4 = 1,
|
| 8 |
+
};
|
| 9 |
+
|
| 10 |
+
constexpr __host__ __device__ int get_element_size(const MmaKind& mma_kind) {
|
| 11 |
+
switch (mma_kind) {
|
| 12 |
+
case MmaKind::BF16: return 2;
|
| 13 |
+
case MmaKind::MXFP8FP4: return 1;
|
| 14 |
+
default: return 0;
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
enum class GemmType {
|
| 19 |
+
Normal = 0,
|
| 20 |
+
MGroupedContiguous = 1,
|
| 21 |
+
MGroupedMasked = 2,
|
| 22 |
+
KGroupedContiguous = 3,
|
| 23 |
+
Batched = 4,
|
| 24 |
+
MGroupedContiguousWithPsumLayout = 5,
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
constexpr __host__ __device__ bool is_m_grouped_contiguous(const GemmType& gemm_type) {
|
| 28 |
+
switch (gemm_type) {
|
| 29 |
+
case GemmType::MGroupedContiguous: return true;
|
| 30 |
+
case GemmType::MGroupedContiguousWithPsumLayout: return true;
|
| 31 |
+
default: return false;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
enum class KernelType {
|
| 36 |
+
Kernel1D1D = 0,
|
| 37 |
+
Kernel1D2D = 1,
|
| 38 |
+
KernelNoSF = 2
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/common/utils.cuh
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cuda/std/cstdint>
|
| 4 |
+
|
| 5 |
+
#include <deep_gemm/common/exception.cuh>
|
| 6 |
+
|
| 7 |
+
namespace deep_gemm::utils {
|
| 8 |
+
|
| 9 |
+
template <typename FuncT>
|
| 10 |
+
struct PatternVisitor {
|
| 11 |
+
FuncT func;
|
| 12 |
+
|
| 13 |
+
CUTLASS_HOST_DEVICE
|
| 14 |
+
explicit PatternVisitor(FuncT&& func): func(std::forward<FuncT>(func)) {}
|
| 15 |
+
|
| 16 |
+
CUTLASS_HOST_DEVICE
|
| 17 |
+
auto operator [](const uint32_t& i) const {
|
| 18 |
+
return func(i);
|
| 19 |
+
}
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
template <uint32_t kNumBytes>
|
| 23 |
+
struct Vectorized {
|
| 24 |
+
static auto zeros() {
|
| 25 |
+
// TODO: add `ulonglong4` for SM100 once `__ldg` support this
|
| 26 |
+
if constexpr (kNumBytes > 0 and kNumBytes % 16 == 0) {
|
| 27 |
+
return make_uint4(0, 0, 0, 0);
|
| 28 |
+
} else if constexpr (kNumBytes > 0 and kNumBytes % 8 == 0) {
|
| 29 |
+
return make_uint2(0, 0);
|
| 30 |
+
} else if constexpr (kNumBytes > 0 and kNumBytes % 4 == 0) {
|
| 31 |
+
return 0;
|
| 32 |
+
} else {
|
| 33 |
+
DG_STATIC_ASSERT(kNumBytes > 0 and kNumBytes % 4 == 0, "Invalid vectorization");
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
using vec_t = decltype(zeros());
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
template <uint32_t kNumCols>
|
| 41 |
+
CUTLASS_DEVICE constexpr uint32_t get_num_aligned_tmem_cols() {
|
| 42 |
+
DG_STATIC_ASSERT(kNumCols <= 512, "Too many tensor memory columns");
|
| 43 |
+
if constexpr (kNumCols <= 32) return 32;
|
| 44 |
+
if constexpr (kNumCols <= 64) return 64;
|
| 45 |
+
if constexpr (kNumCols <= 128) return 128;
|
| 46 |
+
if constexpr (kNumCols <= 256) return 256;
|
| 47 |
+
return 512;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
} // namespace deep_gemm::utils
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/sm100_store_cd.cuh
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/atom/copy_traits_sm100.hpp>
|
| 4 |
+
|
| 5 |
+
#include <deep_gemm/common/math.cuh>
|
| 6 |
+
#include <deep_gemm/common/types.cuh>
|
| 7 |
+
#include <deep_gemm/common/utils.cuh>
|
| 8 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 9 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 10 |
+
|
| 11 |
+
namespace deep_gemm::epilogue {
|
| 12 |
+
|
| 13 |
+
template <uint32_t BLOCK_M, uint32_t BLOCK_N,
|
| 14 |
+
uint32_t STORE_BLOCK_M, uint32_t STORE_BLOCK_N,
|
| 15 |
+
uint32_t kSwizzleCDMode,
|
| 16 |
+
uint32_t kNumTMAStoreStages,
|
| 17 |
+
uint32_t kNumUMMAStoreThreads,
|
| 18 |
+
GemmType kGemmType, bool kWithAccumulation,
|
| 19 |
+
typename cd_dtype_t,
|
| 20 |
+
typename epilogue_type_t,
|
| 21 |
+
typename pattern_cd_t>
|
| 22 |
+
CUTLASS_DEVICE void
|
| 23 |
+
sm100_store_cd(const utils::PatternVisitor<pattern_cd_t>& smem_cd, uint32_t& tma_stage_idx,
|
| 24 |
+
const uint32_t& tmem_base_addr,
|
| 25 |
+
const uint32_t& base_m_idx, const uint32_t& base_n_idx, const uint32_t& batch_idx,
|
| 26 |
+
const uint32_t& epilogue_warp_idx, const uint32_t& lane_idx,
|
| 27 |
+
const cutlass::arch::ClusterTransactionBarrier* tmem_empty_barrier,
|
| 28 |
+
const cute::TmaDescriptor& tensor_map_cd) {
|
| 29 |
+
// TMA checks
|
| 30 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 31 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(cd_dtype_t);
|
| 32 |
+
DG_STATIC_ASSERT(kSwizzleCDMode > 0, "TMA D must be swizzled");
|
| 33 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N % kNumElemsPerBankGroup == 0, "Invalid swizzling");
|
| 34 |
+
DG_STATIC_ASSERT(BLOCK_M % STORE_BLOCK_M == 0, "Invalid block sizes");
|
| 35 |
+
DG_STATIC_ASSERT(BLOCK_N % STORE_BLOCK_N == 0, "Invalid block sizes");
|
| 36 |
+
|
| 37 |
+
// Share store pipeline between blocks
|
| 38 |
+
auto advance_store_pipeline = [&]() {
|
| 39 |
+
tma_stage_idx = (tma_stage_idx + 1) % kNumTMAStoreStages;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
// Iterate over M waves
|
| 43 |
+
constexpr auto kNumMWaves = BLOCK_M / STORE_BLOCK_M;
|
| 44 |
+
#pragma unroll
|
| 45 |
+
for (uint32_t w = 0; w < kNumMWaves; ++ w) {
|
| 46 |
+
// Issue every swizzled atom and pipeline STSM and TMA store
|
| 47 |
+
constexpr uint32_t kNumStores = BLOCK_N / STORE_BLOCK_N;
|
| 48 |
+
#pragma unroll
|
| 49 |
+
for (uint32_t s = 0; s < kNumStores; ++ s, advance_store_pipeline()) {
|
| 50 |
+
auto smem_base_ptr = reinterpret_cast<uint8_t*>(smem_cd[tma_stage_idx]);
|
| 51 |
+
|
| 52 |
+
// Wait shared memory to be released
|
| 53 |
+
if (epilogue_warp_idx == 0)
|
| 54 |
+
cute::tma_store_wait<kNumTMAStoreStages - 1>();
|
| 55 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 56 |
+
|
| 57 |
+
// The pipeline stage
|
| 58 |
+
const auto m_idx = base_m_idx + w * STORE_BLOCK_M;
|
| 59 |
+
const auto n_idx = epilogue_type_t::apply_index_n<STORE_BLOCK_N>(base_n_idx + s * STORE_BLOCK_N);
|
| 60 |
+
|
| 61 |
+
// Store into shared memory
|
| 62 |
+
#pragma unroll
|
| 63 |
+
for (uint32_t i = 0; i < STORE_BLOCK_N / kNumElemsPerBankGroup; ++ i) {
|
| 64 |
+
// Calculate the index of the bank group to be written in the atom
|
| 65 |
+
auto bank_group_index = i + lane_idx * (kSwizzleCDMode / kNumBankGroupBytes);
|
| 66 |
+
|
| 67 |
+
// Reshape the atom in another view and swizzle
|
| 68 |
+
// - original: `(LAYOUT_AD_M, kSwizzleCDMode / kNumBankGroupBytes)`
|
| 69 |
+
// - new: `(LAYOUT_AD_M * kSwizzleCDMode / kNumBankGroupBytes / 8, 8)`
|
| 70 |
+
// NOTES: "8" is the number of bank groups, "16" is the swizzling pattern
|
| 71 |
+
constexpr bool kHasShortcut = (kSwizzleCDMode / kNumBankGroupBytes) == 8;
|
| 72 |
+
auto row = kHasShortcut ? (i / 8 + lane_idx) : (bank_group_index / 8);
|
| 73 |
+
auto col = kHasShortcut ? (i) : (bank_group_index % 8);
|
| 74 |
+
col ^= row % (kSwizzleCDMode / 16);
|
| 75 |
+
|
| 76 |
+
// Source and destination memory address
|
| 77 |
+
uint32_t tmem_addr = tmem_base_addr + // Accumulator offset
|
| 78 |
+
w * BLOCK_N + // Wave offset
|
| 79 |
+
s * STORE_BLOCK_N + i * kNumElemsPerBankGroup; // In-block offset
|
| 80 |
+
auto smem_ptr = smem_base_ptr + // Base pointer
|
| 81 |
+
epilogue_warp_idx * 32 * kSwizzleCDMode + // Warp offset
|
| 82 |
+
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
| 83 |
+
|
| 84 |
+
// Load from tensor memory, store into shared memory
|
| 85 |
+
uint32_t values[kNumElemsPerBankGroup];
|
| 86 |
+
if constexpr (cute::is_same_v<cd_dtype_t, float>) {
|
| 87 |
+
// For FP32 output, read and store
|
| 88 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 4, "Invalid type");
|
| 89 |
+
cute::SM100_TMEM_LOAD_32dp32b4x::copy(tmem_addr,
|
| 90 |
+
values[0], values[1], values[2], values[3]);
|
| 91 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 92 |
+
ptx::st_shared(smem_ptr, values[0], values[1], values[2], values[3]);
|
| 93 |
+
} else {
|
| 94 |
+
// For BF16 output, read, cast and store
|
| 95 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 8 and cute::is_same_v<cd_dtype_t, cutlass::bfloat16_t>, "Invalid type");
|
| 96 |
+
cute::SM100_TMEM_LOAD_32dp32b8x::copy(tmem_addr,
|
| 97 |
+
values[0], values[1], values[2], values[3],
|
| 98 |
+
values[4], values[5], values[6], values[7]);
|
| 99 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 100 |
+
ptx::st_shared(
|
| 101 |
+
smem_ptr,
|
| 102 |
+
math::cast_into_bf16_and_pack(values[0], values[1]),
|
| 103 |
+
math::cast_into_bf16_and_pack(values[2], values[3]),
|
| 104 |
+
math::cast_into_bf16_and_pack(values[4], values[5]),
|
| 105 |
+
math::cast_into_bf16_and_pack(values[6], values[7])
|
| 106 |
+
);
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Notify tensor memory empty (only at the leader CTA) arrival ASAP
|
| 111 |
+
// NOTES: only the last stage needs to do this
|
| 112 |
+
if (w == kNumMWaves - 1 and s == BLOCK_N / STORE_BLOCK_N - 1) {
|
| 113 |
+
ptx::tcgen05_before_thread_sync();
|
| 114 |
+
tmem_empty_barrier->arrive(0u);
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// Synchronize all threads and issue TMA
|
| 118 |
+
cute::tma_store_fence();
|
| 119 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 120 |
+
if (epilogue_warp_idx == 0 and cute::elect_one_sync()) {
|
| 121 |
+
if constexpr (kGemmType == GemmType::Batched) {
|
| 122 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 123 |
+
cute::SM90_TMA_REDUCE_ADD_3D, cute::SM90_TMA_STORE_3D>;
|
| 124 |
+
cute_tma_t::copy(&tensor_map_cd, smem_base_ptr, n_idx, m_idx, batch_idx);
|
| 125 |
+
} else {
|
| 126 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 127 |
+
cute::SM90_TMA_REDUCE_ADD_2D, cute::SM90_TMA_STORE_2D>;
|
| 128 |
+
cute_tma_t::copy(&tensor_map_cd, smem_base_ptr, n_idx, m_idx);
|
| 129 |
+
}
|
| 130 |
+
cute::tma_store_arrive();
|
| 131 |
+
}
|
| 132 |
+
__syncwarp();
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
} // namespace deep_gemm::epilogue
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/sm100_store_cd_swap_ab.cuh
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/atom/copy_traits_sm100.hpp>
|
| 4 |
+
|
| 5 |
+
#include <deep_gemm/common/math.cuh>
|
| 6 |
+
#include <deep_gemm/common/types.cuh>
|
| 7 |
+
#include <deep_gemm/common/utils.cuh>
|
| 8 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 9 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 10 |
+
|
| 11 |
+
namespace deep_gemm::epilogue {
|
| 12 |
+
|
| 13 |
+
template <uint32_t BLOCK_M, uint32_t BLOCK_N,
|
| 14 |
+
uint32_t STORE_BLOCK_M, uint32_t STORE_BLOCK_N,
|
| 15 |
+
uint32_t kSwizzleCDMode,
|
| 16 |
+
uint32_t kNumTMAStoreStages,
|
| 17 |
+
uint32_t kNumUMMAStoreThreads,
|
| 18 |
+
GemmType kGemmType, bool kWithAccumulation,
|
| 19 |
+
typename cd_dtype_t,
|
| 20 |
+
typename epilogue_type_t,
|
| 21 |
+
typename pattern_cd_t>
|
| 22 |
+
CUTLASS_DEVICE void
|
| 23 |
+
sm100_store_cd_swap_ab(const utils::PatternVisitor<pattern_cd_t>& smem_cd, uint32_t& tma_stage_idx,
|
| 24 |
+
const uint32_t& tmem_base_addr,
|
| 25 |
+
const uint32_t& base_m_idx, const uint32_t& base_n_idx, const uint32_t& batch_idx,
|
| 26 |
+
const uint32_t& effective_m,
|
| 27 |
+
const uint32_t& epilogue_warp_idx, const uint32_t& lane_idx,
|
| 28 |
+
const cutlass::arch::ClusterTransactionBarrier* tmem_empty_barrier,
|
| 29 |
+
const cute::TmaDescriptor& tensor_map_cd) {
|
| 30 |
+
// NOTES: The epilogue requires a full warpgroup to read all 128 TMEM rows,
|
| 31 |
+
// implying STORE_BLOCK_N must be 128.
|
| 32 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N == 128, "STORE_BLOCK_N must be 128 to match TMEM rows");
|
| 33 |
+
|
| 34 |
+
// TMA checks
|
| 35 |
+
constexpr uint32_t STORE_BLOCK_N_ATOM = kSwizzleCDMode / sizeof(cd_dtype_t);
|
| 36 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 37 |
+
constexpr uint32_t kNumSwizzleAtomRows = 8;
|
| 38 |
+
DG_STATIC_ASSERT(kSwizzleCDMode == 128, "TMA D must be 128B swizzled");
|
| 39 |
+
DG_STATIC_ASSERT(BLOCK_M % STORE_BLOCK_M == 0, "Invalid block sizes");
|
| 40 |
+
DG_STATIC_ASSERT(BLOCK_N % STORE_BLOCK_N == 0, "Invalid block sizes");
|
| 41 |
+
DG_STATIC_ASSERT(STORE_BLOCK_M % kNumSwizzleAtomRows == 0, "Invalid swizzling");
|
| 42 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N % STORE_BLOCK_N_ATOM == 0, "Invalid swizzling");
|
| 43 |
+
|
| 44 |
+
// Share store pipeline between blocks
|
| 45 |
+
auto advance_store_pipeline = [&]() {
|
| 46 |
+
tma_stage_idx = (tma_stage_idx + 1) % kNumTMAStoreStages;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
// Iterate over M blocks
|
| 50 |
+
const auto num_stores = effective_m / STORE_BLOCK_M;
|
| 51 |
+
for (uint32_t s = 0; s < num_stores; ++ s, advance_store_pipeline()) {
|
| 52 |
+
// Wait shared memory to be released
|
| 53 |
+
if (epilogue_warp_idx == 0)
|
| 54 |
+
cute::tma_store_wait<kNumTMAStoreStages - 1>();
|
| 55 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 56 |
+
|
| 57 |
+
// Store into shared memory
|
| 58 |
+
#pragma unroll
|
| 59 |
+
for (uint32_t i = 0; i < STORE_BLOCK_M / kNumSwizzleAtomRows; ++ i) {
|
| 60 |
+
uint32_t tmem_addr = tmem_base_addr +
|
| 61 |
+
s * STORE_BLOCK_M + // Store stage offset
|
| 62 |
+
i * kNumSwizzleAtomRows; // In-block offset
|
| 63 |
+
uint32_t values[kNumSwizzleAtomRows];
|
| 64 |
+
|
| 65 |
+
// Warps cooperatively write an atomic block to shared memory
|
| 66 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N_ATOM % 32 == 0, "Invalid block sizes");
|
| 67 |
+
constexpr uint32_t kNumWarpsPerAtom = STORE_BLOCK_N_ATOM / 32;
|
| 68 |
+
uint32_t outer_atom_offset = (epilogue_warp_idx / kNumWarpsPerAtom) * STORE_BLOCK_M * kSwizzleCDMode;
|
| 69 |
+
uint32_t inner_atom_offset = i * kNumSwizzleAtomRows * kSwizzleCDMode;
|
| 70 |
+
auto smem_base_ptr = reinterpret_cast<uint8_t*>(smem_cd[tma_stage_idx]) + outer_atom_offset + inner_atom_offset;
|
| 71 |
+
|
| 72 |
+
if constexpr (cute::is_same_v<cd_dtype_t, float>) {
|
| 73 |
+
// NOTES: Swizzling is not required in this case, but used here for consistency with other cases
|
| 74 |
+
cute::SM100_TMEM_LOAD_32dp32b8x::copy(tmem_addr, values[0], values[1], values[2], values[3],
|
| 75 |
+
values[4], values[5], values[6], values[7]);
|
| 76 |
+
uint32_t col = lane_idx / 4;
|
| 77 |
+
|
| 78 |
+
#pragma unroll
|
| 79 |
+
for (uint32_t row = 0; row < kNumSwizzleAtomRows; ++ row) {
|
| 80 |
+
auto smem_ptr = smem_base_ptr + row * (kNumBankGroupBytes * 8)
|
| 81 |
+
+ (col ^ row) * kNumBankGroupBytes
|
| 82 |
+
+ (lane_idx % 4) * sizeof(float);
|
| 83 |
+
ptx::st_shared(reinterpret_cast<uint32_t*>(smem_ptr), values[row]);
|
| 84 |
+
}
|
| 85 |
+
} else {
|
| 86 |
+
// Load from TMEM using `.16x256b` shape to satisfy STSM layout requirements
|
| 87 |
+
// Start from lane index 0
|
| 88 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr,
|
| 89 |
+
values[0], values[1], values[2], values[3]);
|
| 90 |
+
// Start from lane index 16
|
| 91 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr | 0x00100000,
|
| 92 |
+
values[4], values[5], values[6], values[7]);
|
| 93 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 94 |
+
|
| 95 |
+
// Destination shared memory address
|
| 96 |
+
uint32_t row = lane_idx % 8;
|
| 97 |
+
uint32_t col = (epilogue_warp_idx % 2) * 4 + lane_idx / 8;
|
| 98 |
+
auto smem_ptr = smem_base_ptr + row * (kNumBankGroupBytes * 8)
|
| 99 |
+
+ (col ^ row) * kNumBankGroupBytes;
|
| 100 |
+
|
| 101 |
+
// Store matrix with transposition
|
| 102 |
+
ptx::SM90_U32x4_STSM_T<int>::copy(math::cast_into_bf16_and_pack(values[0], values[1]),
|
| 103 |
+
math::cast_into_bf16_and_pack(values[2], values[3]),
|
| 104 |
+
math::cast_into_bf16_and_pack(values[4], values[5]),
|
| 105 |
+
math::cast_into_bf16_and_pack(values[6], values[7]),
|
| 106 |
+
smem_ptr);
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Notify tensor memory empty (only at the leader CTA) arrival ASAP
|
| 111 |
+
// NOTES: only the last stage needs to do this
|
| 112 |
+
if (s == num_stores - 1) {
|
| 113 |
+
ptx::tcgen05_before_thread_sync();
|
| 114 |
+
tmem_empty_barrier->arrive(0u);
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// Synchronize all threads and issue TMA
|
| 118 |
+
cute::tma_store_fence();
|
| 119 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 120 |
+
if (epilogue_warp_idx == 0 and cute::elect_one_sync()) {
|
| 121 |
+
#pragma unroll
|
| 122 |
+
for (uint32_t i = 0; i < STORE_BLOCK_N / STORE_BLOCK_N_ATOM; ++ i) {
|
| 123 |
+
auto smem_ptr = smem_cd[tma_stage_idx] + i * STORE_BLOCK_M * STORE_BLOCK_N_ATOM;
|
| 124 |
+
uint32_t m_idx = base_m_idx + s * STORE_BLOCK_M;
|
| 125 |
+
uint32_t n_idx = epilogue_type_t::apply_index_n<STORE_BLOCK_N_ATOM>(base_n_idx + i * STORE_BLOCK_N_ATOM);
|
| 126 |
+
|
| 127 |
+
// Issue 2D or 3D TMA store
|
| 128 |
+
if constexpr (kGemmType == GemmType::Batched) {
|
| 129 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 130 |
+
cute::SM90_TMA_REDUCE_ADD_3D, cute::SM90_TMA_STORE_3D>;
|
| 131 |
+
cute_tma_t::copy(&tensor_map_cd, smem_ptr, n_idx, m_idx, batch_idx);
|
| 132 |
+
} else {
|
| 133 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 134 |
+
cute::SM90_TMA_REDUCE_ADD_2D, cute::SM90_TMA_STORE_2D>;
|
| 135 |
+
cute_tma_t::copy(&tensor_map_cd, smem_ptr, n_idx, m_idx);
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
cute::tma_store_arrive();
|
| 139 |
+
}
|
| 140 |
+
__syncwarp();
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
} // namespace deep_gemm::epilogue
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/epilogue/transform.cuh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <deep_gemm/common/exception.cuh>
|
| 4 |
+
|
| 5 |
+
namespace deep_gemm::epilogue::transform {
|
| 6 |
+
|
| 7 |
+
struct EpilogueIdentity {
|
| 8 |
+
template <uint32_t STORE_BLOCK_N>
|
| 9 |
+
CUTLASS_DEVICE static uint32_t apply_index_n(const uint32_t& n_idx) {
|
| 10 |
+
return n_idx;
|
| 11 |
+
}
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
template <uint32_t kLeft, uint32_t kMid, uint32_t kRight>
|
| 15 |
+
struct EpilogueHeadSplits: EpilogueIdentity {
|
| 16 |
+
template <uint32_t STORE_BLOCK_N>
|
| 17 |
+
CUTLASS_DEVICE static uint32_t apply_index_n(const uint32_t& n_idx) {
|
| 18 |
+
DG_STATIC_ASSERT(kLeft % STORE_BLOCK_N == 0 and kMid % STORE_BLOCK_N == 0 and
|
| 19 |
+
kRight % STORE_BLOCK_N == 0, "Invalid head splits config");
|
| 20 |
+
return n_idx + (n_idx + kRight) / (kLeft + kRight) * kMid;
|
| 21 |
+
}
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} // namespace deep_gemm::epilogue::transform
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_bf16_gemm.cuh
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#pragma clang diagnostic push
|
| 3 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 4 |
+
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 8 |
+
#include <deep_gemm/common/math.cuh>
|
| 9 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 10 |
+
#include <deep_gemm/epilogue/sm100_store_cd.cuh>
|
| 11 |
+
#include <deep_gemm/epilogue/sm100_store_cd_swap_ab.cuh>
|
| 12 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 13 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 16 |
+
|
| 17 |
+
namespace deep_gemm {
|
| 18 |
+
|
| 19 |
+
template <cute::UMMA::Major kMajorA, cute::UMMA::Major kMajorB,
|
| 20 |
+
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 21 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K_,
|
| 22 |
+
uint32_t kNumGroups,
|
| 23 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleCDMode,
|
| 24 |
+
uint32_t kNumStages_,
|
| 25 |
+
uint32_t kNumNonEpilogueThreads, uint32_t kNumEpilogueThreads,
|
| 26 |
+
uint32_t kNumMulticast, bool kIsMulticastOnA,
|
| 27 |
+
uint32_t kNumSMs,
|
| 28 |
+
bool kSwapAB,
|
| 29 |
+
GemmType kGemmType, bool kWithAccumulation, typename cd_dtype_t,
|
| 30 |
+
uint64_t kTensorCoreUtilControl>
|
| 31 |
+
CUTLASS_GLOBAL void __launch_bounds__(kNumNonEpilogueThreads + kNumEpilogueThreads, 1)
|
| 32 |
+
sm100_bf16_gemm_impl(int* grouped_layout,
|
| 33 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 34 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
| 37 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 38 |
+
// Enlarge `BLOCK_K` for some cases
|
| 39 |
+
// NOTES: this is for reducing the `umma_arrive()` overhead
|
| 40 |
+
constexpr bool kDoMergeStages =
|
| 41 |
+
kNumStages_ >= 8 and kGemmType == GemmType::Normal and
|
| 42 |
+
kMajorA == cute::UMMA::Major::K and kMajorB == cute::UMMA::Major::K;
|
| 43 |
+
// Ensure there are at least `kNumMinStages` stages after merge
|
| 44 |
+
constexpr uint32_t kNumMinStages = 8;
|
| 45 |
+
constexpr uint32_t kNumStagesPerMerge = kDoMergeStages ? kNumStages_ / kNumMinStages : 1;
|
| 46 |
+
constexpr uint32_t BLOCK_K = BLOCK_K_ * kNumStagesPerMerge;
|
| 47 |
+
constexpr uint32_t kNumStages = kNumStages_ / kNumStagesPerMerge;
|
| 48 |
+
|
| 49 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 50 |
+
using Allocator = cute::conditional_t<kNumMulticast == 1, cute::TMEM::Allocator1Sm, cute::TMEM::Allocator2Sm>;
|
| 51 |
+
|
| 52 |
+
// GEMM with accumulation must have FP32 output
|
| 53 |
+
if constexpr (kWithAccumulation)
|
| 54 |
+
DG_STATIC_ASSERT(cute::is_same_v<cd_dtype_t, float>, "Invalid C/D data dtype");
|
| 55 |
+
|
| 56 |
+
// MMA Configs
|
| 57 |
+
constexpr uint32_t LAYOUT_AD_M = 128;
|
| 58 |
+
constexpr uint32_t UMMA_M = LAYOUT_AD_M * kNumMulticast;
|
| 59 |
+
constexpr uint32_t UMMA_N = kSwapAB ? BLOCK_M : BLOCK_N;
|
| 60 |
+
constexpr uint32_t UMMA_K = 16;
|
| 61 |
+
constexpr uint32_t LOAD_BLOCK_M = BLOCK_M / (kIsMulticastOnA ? kNumMulticast: 1);
|
| 62 |
+
constexpr uint32_t LOAD_BLOCK_N = BLOCK_N / (kIsMulticastOnA ? 1 : kNumMulticast);
|
| 63 |
+
DG_STATIC_ASSERT(BLOCK_K_ == 64, "Invalid block K");
|
| 64 |
+
DG_STATIC_ASSERT(kNumMulticast == 1 or kNumMulticast == 2, "Only support 1/2 multicast");
|
| 65 |
+
DG_STATIC_ASSERT((kSwapAB and BLOCK_N == LAYOUT_AD_M) or
|
| 66 |
+
(not kSwapAB and (BLOCK_M == 32 or BLOCK_M == 64 or BLOCK_M == LAYOUT_AD_M)), "Invalid block size");
|
| 67 |
+
|
| 68 |
+
// Epilogue configs
|
| 69 |
+
// Always enable pipeline for better performance
|
| 70 |
+
constexpr uint32_t kNumEpilogueStages = 2;
|
| 71 |
+
constexpr uint32_t kNumTMAStoreStages = 2;
|
| 72 |
+
// NOTES: To maximize epilogue threads utilization, process an entire BLOCK_N
|
| 73 |
+
// per store stage for swap-AB cases, and an entire BLOCK_M for non-swap cases
|
| 74 |
+
constexpr uint32_t STORE_BLOCK_M = kSwapAB ? 16 : cute::min<uint32_t>(BLOCK_M, LAYOUT_AD_M);
|
| 75 |
+
constexpr uint32_t STORE_BLOCK_N = kSwapAB ? BLOCK_N : kSwizzleCDMode / sizeof(cd_dtype_t);
|
| 76 |
+
constexpr uint32_t kNumUMMAStoreThreads = kSwapAB ? kNumEpilogueThreads: STORE_BLOCK_M;
|
| 77 |
+
DG_STATIC_ASSERT(kNumUMMAStoreThreads % 32 == 0, "Invalid store block M");
|
| 78 |
+
|
| 79 |
+
// Share memory sizes
|
| 80 |
+
constexpr uint32_t SMEM_CD_SIZE_PER_STAGE = STORE_BLOCK_M * STORE_BLOCK_N * sizeof(cd_dtype_t);
|
| 81 |
+
constexpr uint32_t SMEM_CD_SIZE = SMEM_CD_SIZE_PER_STAGE * kNumTMAStoreStages;
|
| 82 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = LOAD_BLOCK_M * BLOCK_K * sizeof(cutlass::bfloat16_t);
|
| 83 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = LOAD_BLOCK_N * BLOCK_K * sizeof(cutlass::bfloat16_t);
|
| 84 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % 1024 == 0 and SMEM_A_SIZE_PER_STAGE % 1024 == 0 and SMEM_B_SIZE_PER_STAGE % 1024 == 0,
|
| 85 |
+
"Shared memory of A/B must be aligned to 1024 bytes");
|
| 86 |
+
DG_STATIC_ASSERT(kNumTMAStoreStages >= 1, "Invalid number of TMA stages");
|
| 87 |
+
|
| 88 |
+
// NOTES: Make sure we have enough shared memory for UMMA padding
|
| 89 |
+
static constexpr uint32_t UMMA_A_SIZE_PER_STAGE = math::constexpr_align(LOAD_BLOCK_M, LAYOUT_AD_M) * BLOCK_K * sizeof(nv_bfloat16);
|
| 90 |
+
DG_STATIC_ASSERT(UMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory out of bound for UMMA");
|
| 91 |
+
|
| 92 |
+
// Real tensor memory size and offsets
|
| 93 |
+
constexpr uint32_t kNumAccumTmemCols = kNumEpilogueStages * UMMA_N;
|
| 94 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<kNumAccumTmemCols>();
|
| 95 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 96 |
+
|
| 97 |
+
// Synchronize the cluster before 2-CTA TMEM allocation
|
| 98 |
+
kNumMulticast > 1 ? cute::cluster_sync() : void();
|
| 99 |
+
|
| 100 |
+
// Utils
|
| 101 |
+
bool is_leader_cta = cute::block_rank_in_cluster() == 0;
|
| 102 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 103 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 104 |
+
|
| 105 |
+
// Prefetch TMA descriptors at the very beginning
|
| 106 |
+
if (warp_idx == 0) {
|
| 107 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 108 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 109 |
+
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// Overwrite shape constants if the compiler gives
|
| 113 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 114 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 115 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 116 |
+
|
| 117 |
+
// Align to 1024 bytes for swizzle-128B
|
| 118 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 119 |
+
|
| 120 |
+
// D/A/B shared memory
|
| 121 |
+
auto smem_cd = utils::PatternVisitor([&](const uint32_t& i) {
|
| 122 |
+
return reinterpret_cast<cd_dtype_t*>(smem_buffer + i * SMEM_CD_SIZE_PER_STAGE);
|
| 123 |
+
});
|
| 124 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 125 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 126 |
+
});
|
| 127 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 128 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
// Fill barriers
|
| 132 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_CD_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 133 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 134 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 135 |
+
auto tmem_full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 2 + i); });
|
| 136 |
+
auto tmem_empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 2 + kNumEpilogueStages + i); });
|
| 137 |
+
auto tensor_core_full_barrier = barrier_start_ptr + kNumStages * 3 + kNumEpilogueStages * 2;
|
| 138 |
+
|
| 139 |
+
// Fill the tensor memory pointer
|
| 140 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumStages * 3 + kNumEpilogueStages * 2 + 1);
|
| 141 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 142 |
+
|
| 143 |
+
// Initialize barriers
|
| 144 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 145 |
+
#pragma unroll
|
| 146 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 147 |
+
// Arrive only at the leader CTA
|
| 148 |
+
full_barriers[i]->init(kNumMulticast);
|
| 149 |
+
// Arrive at all CTAs
|
| 150 |
+
empty_barriers[i]->init(1);
|
| 151 |
+
}
|
| 152 |
+
#pragma unroll
|
| 153 |
+
for (uint32_t i = 0; i < kNumEpilogueStages; ++ i) {
|
| 154 |
+
// Arrive at all CTAs
|
| 155 |
+
tmem_full_barriers[i]->init(1);
|
| 156 |
+
// Arrive only at the leader CTA
|
| 157 |
+
tmem_empty_barriers[i]->init(kNumMulticast * kNumUMMAStoreThreads);
|
| 158 |
+
}
|
| 159 |
+
if constexpr (kTensorCoreUtilControl < 100)
|
| 160 |
+
tensor_core_full_barrier->init(1);
|
| 161 |
+
|
| 162 |
+
// Make initialized barrier visible in async proxy
|
| 163 |
+
cutlass::arch::fence_barrier_init();
|
| 164 |
+
} else if (warp_idx == 2) {
|
| 165 |
+
// Allocate tensor memory
|
| 166 |
+
Allocator().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 167 |
+
}
|
| 168 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 169 |
+
|
| 170 |
+
// Wait for primary kernel completion
|
| 171 |
+
cudaGridDependencySynchronize();
|
| 172 |
+
|
| 173 |
+
// Block scheduler
|
| 174 |
+
uint32_t m_block_idx, n_block_idx;
|
| 175 |
+
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumMulticast, kIsMulticastOnA, kNumSMs>(
|
| 176 |
+
shape_m, shape_n, shape_k, grouped_layout);
|
| 177 |
+
|
| 178 |
+
// Pipeline and TMA phases
|
| 179 |
+
uint32_t stage_idx = 0, phase = 0, tensor_core_phase = 0;
|
| 180 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 181 |
+
++ k_block_idx;
|
| 182 |
+
|
| 183 |
+
// Flip phases only if reach the next first stage
|
| 184 |
+
stage_idx = (stage_idx + 1) % kNumStages;
|
| 185 |
+
phase ^= stage_idx == 0;
|
| 186 |
+
};
|
| 187 |
+
|
| 188 |
+
// Dispatch warps into different roles
|
| 189 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 190 |
+
// TMA load warp
|
| 191 |
+
// Persistently schedule over blocks
|
| 192 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 193 |
+
// Use dynamic load block M, when swap-AB is enabled
|
| 194 |
+
const auto load_block_m = kSwapAB ? scheduler.get_aligned_effective_m_in_block(m_block_idx) / kNumMulticast : LOAD_BLOCK_M;
|
| 195 |
+
|
| 196 |
+
// For k-grouped layout, the number of block K is variable
|
| 197 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 198 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 199 |
+
// Wait consumer release
|
| 200 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 201 |
+
|
| 202 |
+
// Compute offsets
|
| 203 |
+
// NOTES: the group is always concatenated with the outer dimension
|
| 204 |
+
uint32_t m_idx = scheduler.template get_global_idx<(kGemmType == GemmType::MGroupedMasked), sched::IndexType::MN> (
|
| 205 |
+
shape_m, BLOCK_M, m_block_idx);
|
| 206 |
+
uint32_t n_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::K), sched::IndexType::MN> (
|
| 207 |
+
shape_n, BLOCK_N, n_block_idx, m_block_idx);
|
| 208 |
+
|
| 209 |
+
// NOTES: `k_idx` is actually the k index default for K-major, while `k_b_idx` may be MN-major
|
| 210 |
+
// And for all m-grouped GEMMs, A must be K-majored
|
| 211 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous or kGemmType == GemmType::Batched or
|
| 212 |
+
kMajorA == cute::UMMA::Major::K, "Invalid major");
|
| 213 |
+
uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 214 |
+
uint32_t k_a_idx = scheduler.template get_global_idx<(kMajorA == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 215 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 216 |
+
uint32_t k_b_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 217 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 218 |
+
|
| 219 |
+
// Add 2 CTA offsets
|
| 220 |
+
if constexpr (kNumMulticast > 1) {
|
| 221 |
+
m_idx += kIsMulticastOnA ? (cute::block_rank_in_cluster() * load_block_m) : 0;
|
| 222 |
+
n_idx += kIsMulticastOnA ? 0 : (cute::block_rank_in_cluster() * LOAD_BLOCK_N);
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
// Issue TMAs
|
| 226 |
+
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
| 227 |
+
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
| 228 |
+
if constexpr (kMajorA == cute::UMMA::Major::K)
|
| 229 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_M, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 230 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_a_idx, m_idx, kNumMulticast, batch_idx);
|
| 231 |
+
if constexpr (kMajorA == cute::UMMA::Major::MN)
|
| 232 |
+
tma::copy<LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 233 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], m_idx, k_a_idx, kNumMulticast, batch_idx);
|
| 234 |
+
if constexpr (kMajorB == cute::UMMA::Major::K)
|
| 235 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_N, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 236 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_b_idx, n_idx, kNumMulticast, batch_idx);
|
| 237 |
+
if constexpr (kMajorB == cute::UMMA::Major::MN)
|
| 238 |
+
tma::copy<LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 239 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], n_idx, k_b_idx, kNumMulticast, batch_idx);
|
| 240 |
+
|
| 241 |
+
// Arrive at full barriers
|
| 242 |
+
constexpr uint32_t kNumArrivalBytes = SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE;
|
| 243 |
+
if (is_leader_cta) {
|
| 244 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(kNumArrivalBytes * kNumMulticast);
|
| 245 |
+
} else {
|
| 246 |
+
full_barriers[stage_idx]->arrive(0u);
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
} else if (warp_idx == 1 and is_leader_cta) {
|
| 251 |
+
// MMA issue warp
|
| 252 |
+
// NOTES: only the leader CTA will do this
|
| 253 |
+
// Make instruction descriptor
|
| 254 |
+
auto instr_desc = kSwapAB ? cute::UMMA::make_instr_desc<cutlass::bfloat16_t, cutlass::bfloat16_t, float,
|
| 255 |
+
UMMA_M, UMMA_N, kMajorB, kMajorA>()
|
| 256 |
+
: cute::UMMA::make_instr_desc<cutlass::bfloat16_t, cutlass::bfloat16_t, float,
|
| 257 |
+
UMMA_M, UMMA_N, kMajorA, kMajorB>();
|
| 258 |
+
|
| 259 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 260 |
+
// Merged stages only happens in NT normal GEMM cases
|
| 261 |
+
constexpr uint32_t BLOCK_ATOM_K = BLOCK_K / kNumStagesPerMerge;
|
| 262 |
+
auto a_desc = mma::sm100::make_umma_desc<kMajorA, LOAD_BLOCK_M, BLOCK_ATOM_K, kSwizzleAMode>(smem_a[0], 0, 0);
|
| 263 |
+
auto b_desc = mma::sm100::make_umma_desc<kMajorB, LOAD_BLOCK_N, BLOCK_ATOM_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 264 |
+
uint32_t a_desc_lo = lane_idx < kNumStages ? a_desc.lo + lane_idx * SMEM_A_SIZE_PER_STAGE / 16 : 0u;
|
| 265 |
+
uint32_t b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 266 |
+
|
| 267 |
+
// Checks for MMA instructions
|
| 268 |
+
// NOTES: CUTLASS does not have such checks except the MMA traits, but we are not using these traits
|
| 269 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 270 |
+
(UMMA_M == 128 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256) or
|
| 271 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 272 |
+
"Invalid MMA instruction shape");
|
| 273 |
+
|
| 274 |
+
// Persistently schedule over blocks
|
| 275 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 276 |
+
// Wait tensor memory empty barrier arrival
|
| 277 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 278 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 279 |
+
tmem_empty_barriers[accum_stage_idx]->wait(accum_phase_idx ^ 1);
|
| 280 |
+
ptx::tcgen05_after_thread_sync();
|
| 281 |
+
|
| 282 |
+
// UMMA and empty barrier arrival alias
|
| 283 |
+
auto umma_arrive = [](const uint64_t* barrier) {
|
| 284 |
+
if constexpr (kNumMulticast == 1) {
|
| 285 |
+
cutlass::arch::umma_arrive(barrier);
|
| 286 |
+
} else {
|
| 287 |
+
constexpr uint16_t kCTAMask = (1 << kNumMulticast) - 1;
|
| 288 |
+
cutlass::arch::umma_arrive_multicast_2x1SM(barrier, kCTAMask);
|
| 289 |
+
}
|
| 290 |
+
};
|
| 291 |
+
auto empty_barrier_arrive = [&](const bool& do_tmem_full_arrive) {
|
| 292 |
+
umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 293 |
+
|
| 294 |
+
// NOTES: the tensor memory accumulator pipeline has nothing to do with multicasting
|
| 295 |
+
if (do_tmem_full_arrive)
|
| 296 |
+
umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barriers[accum_stage_idx]));
|
| 297 |
+
__syncwarp();
|
| 298 |
+
};
|
| 299 |
+
|
| 300 |
+
// Dynamic update of UMMA N based on effective M, when swap-AB is enabled
|
| 301 |
+
if constexpr (kSwapAB) {
|
| 302 |
+
uint32_t umma_n = scheduler.get_aligned_effective_m_in_block(m_block_idx);
|
| 303 |
+
mma::sm100::update_instr_desc_with_umma_n(instr_desc, umma_n);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
// Launch MMAs
|
| 307 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 308 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 309 |
+
// Wait TMA arrival
|
| 310 |
+
full_barriers[stage_idx]->wait(phase);
|
| 311 |
+
ptx::tcgen05_after_thread_sync();
|
| 312 |
+
|
| 313 |
+
// Issue UMMA in the leader CTA
|
| 314 |
+
using mma_t = cute::conditional_t<kNumMulticast == 1, ptx::SM100_MMA_F16BF16_SS, ptx::SM100_MMA_F16BF16_2x1SM_SS>;
|
| 315 |
+
const auto runtime_instr_desc = cute::UMMA::make_runtime_instr_desc(instr_desc);
|
| 316 |
+
const auto a_desc_base_lo = __shfl_sync(0xffffffff, a_desc_lo, static_cast<int>(stage_idx));
|
| 317 |
+
const auto b_desc_base_lo = __shfl_sync(0xffffffff, b_desc_lo, static_cast<int>(stage_idx));
|
| 318 |
+
if (cute::elect_one_sync()) {
|
| 319 |
+
#pragma unroll
|
| 320 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 321 |
+
uint32_t atom_k_idx = k * UMMA_K / BLOCK_ATOM_K;
|
| 322 |
+
a_desc.lo = mma::sm100::advance_umma_desc_lo<kMajorA, LOAD_BLOCK_M, kSwizzleAMode, cutlass::bfloat16_t>(
|
| 323 |
+
a_desc_base_lo, atom_k_idx * LOAD_BLOCK_M * BLOCK_ATOM_K, k * UMMA_K % BLOCK_ATOM_K);
|
| 324 |
+
b_desc.lo = mma::sm100::advance_umma_desc_lo<kMajorB, LOAD_BLOCK_N, kSwizzleBMode, cutlass::bfloat16_t>(
|
| 325 |
+
b_desc_base_lo, atom_k_idx * LOAD_BLOCK_N * BLOCK_ATOM_K, k * UMMA_K % BLOCK_ATOM_K);
|
| 326 |
+
if (kSwapAB) {
|
| 327 |
+
mma_t::fma(b_desc, a_desc, accum_stage_idx * UMMA_N,
|
| 328 |
+
k_block_idx > 0 or k > 0, runtime_instr_desc);
|
| 329 |
+
} else {
|
| 330 |
+
mma_t::fma(a_desc, b_desc, accum_stage_idx * UMMA_N,
|
| 331 |
+
k_block_idx > 0 or k > 0, runtime_instr_desc);
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
}
|
| 335 |
+
__syncwarp();
|
| 336 |
+
|
| 337 |
+
// Commit to the mbarrier object
|
| 338 |
+
// No explicit `tcgen05.fence::before_thread_sync` is needed, as this is implicitly performed by `tcgen05.commit`
|
| 339 |
+
empty_barrier_arrive(k_block_idx == num_total_k_blocks - 1);
|
| 340 |
+
|
| 341 |
+
// Let tensor cores relax for lower possibility of frequency drop
|
| 342 |
+
DG_STATIC_ASSERT(kTensorCoreUtilControl > 0, "Invalid tensor utilization control");
|
| 343 |
+
if constexpr (kTensorCoreUtilControl < 100) {
|
| 344 |
+
// For utilization control
|
| 345 |
+
umma_arrive(reinterpret_cast<uint64_t*>(tensor_core_full_barrier));
|
| 346 |
+
__syncwarp();
|
| 347 |
+
|
| 348 |
+
// Wait for last UMMA to be done
|
| 349 |
+
tensor_core_full_barrier->wait(tensor_core_phase);
|
| 350 |
+
tensor_core_phase ^= 1;
|
| 351 |
+
|
| 352 |
+
// Sleep for certain cycles
|
| 353 |
+
constexpr static uint64_t kNumUMMACycles = (2ull * UMMA_M * UMMA_N * BLOCK_K) / 8192ull;
|
| 354 |
+
constexpr static uint64_t kNumDummyCycles = (100ull - kTensorCoreUtilControl) * kNumUMMACycles / kTensorCoreUtilControl;
|
| 355 |
+
const auto start_clock = clock64();
|
| 356 |
+
if (cute::elect_one_sync())
|
| 357 |
+
while (clock64() - start_clock < kNumDummyCycles) {}
|
| 358 |
+
__syncwarp();
|
| 359 |
+
}
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
// To safely deconstruct barriers, we need another round of waits
|
| 364 |
+
const auto iter_idx = scheduler.current_iter - 1;
|
| 365 |
+
if (kNumMulticast > 1 and iter_idx >= 0) {
|
| 366 |
+
const auto accum_phase_idx = (iter_idx / kNumEpilogueStages) & 1;
|
| 367 |
+
tmem_empty_barriers[iter_idx % kNumEpilogueStages]->wait(accum_phase_idx);
|
| 368 |
+
}
|
| 369 |
+
} else if (warp_idx >= kNumNonEpilogueThreads / 32 and warp_idx < (kNumNonEpilogueThreads + kNumUMMAStoreThreads) / 32) {
|
| 370 |
+
// Epilogue warp groups
|
| 371 |
+
const auto epilogue_warp_idx = warp_idx - (kNumNonEpilogueThreads / 32);
|
| 372 |
+
|
| 373 |
+
// NOTES: tensor memory addresses are simplified, as the hardware will ignore the warp index bits,
|
| 374 |
+
// i.e., no need for `tmem_ptr |= (epilogue_warp_idx * 32) << 16`.
|
| 375 |
+
// NOTES: we also forbid two CTAs to share the same SM and its tensor memory
|
| 376 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 377 |
+
|
| 378 |
+
// Share store pipeline between blocks
|
| 379 |
+
uint32_t tma_stage_idx = 0;
|
| 380 |
+
|
| 381 |
+
// Persistently schedule over blocks
|
| 382 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 383 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 384 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 385 |
+
|
| 386 |
+
// Wait UMMA arrival
|
| 387 |
+
tmem_full_barriers[accum_stage_idx]->wait(accum_phase_idx);
|
| 388 |
+
ptx::tcgen05_after_thread_sync();
|
| 389 |
+
|
| 390 |
+
// Load from tensor memory into registers, and write shared memory with STSM
|
| 391 |
+
const auto tmem_base_addr = accum_stage_idx * UMMA_N;
|
| 392 |
+
const auto base_m_idx = scheduler.template get_global_idx<
|
| 393 |
+
(not is_m_grouped_contiguous(kGemmType)), sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
| 394 |
+
const auto base_n_idx = n_block_idx * BLOCK_N;
|
| 395 |
+
|
| 396 |
+
if constexpr (kSwapAB) {
|
| 397 |
+
const auto effective_m = scheduler.get_aligned_effective_m_in_block(m_block_idx);
|
| 398 |
+
epilogue::sm100_store_cd_swap_ab<BLOCK_M, BLOCK_N, STORE_BLOCK_M, STORE_BLOCK_N,
|
| 399 |
+
kSwizzleCDMode, kNumTMAStoreStages, kNumUMMAStoreThreads,
|
| 400 |
+
kGemmType, kWithAccumulation,
|
| 401 |
+
cd_dtype_t, epilogue::transform::EpilogueIdentity>
|
| 402 |
+
(smem_cd, tma_stage_idx, tmem_base_addr,
|
| 403 |
+
base_m_idx, base_n_idx, scheduler.current_group_idx,
|
| 404 |
+
effective_m,
|
| 405 |
+
epilogue_warp_idx, lane_idx,
|
| 406 |
+
tmem_empty_barriers[accum_stage_idx],
|
| 407 |
+
tensor_map_cd);
|
| 408 |
+
} else {
|
| 409 |
+
epilogue::sm100_store_cd<BLOCK_M, BLOCK_N, STORE_BLOCK_M, STORE_BLOCK_N,
|
| 410 |
+
kSwizzleCDMode, kNumTMAStoreStages, kNumUMMAStoreThreads,
|
| 411 |
+
kGemmType, kWithAccumulation,
|
| 412 |
+
cd_dtype_t, epilogue::transform::EpilogueIdentity>
|
| 413 |
+
(smem_cd, tma_stage_idx, tmem_base_addr,
|
| 414 |
+
base_m_idx, base_n_idx, scheduler.current_group_idx,
|
| 415 |
+
epilogue_warp_idx, lane_idx,
|
| 416 |
+
tmem_empty_barriers[accum_stage_idx],
|
| 417 |
+
tensor_map_cd);
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
// TODO: Remove redundant synchronization
|
| 423 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 424 |
+
|
| 425 |
+
// Deallocate tensor memory
|
| 426 |
+
if (warp_idx == 0)
|
| 427 |
+
Allocator().free(0, kNumTmemCols);
|
| 428 |
+
|
| 429 |
+
#else
|
| 430 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 431 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 432 |
+
#endif
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
}; // namespace deep_gemm
|
| 436 |
+
|
| 437 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_bmk_bnk_mn.cuh
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 4 |
+
#include <cute/util/type_traits.hpp>
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/utils.cuh>
|
| 8 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 9 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 10 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 11 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 12 |
+
|
| 13 |
+
namespace deep_gemm {
|
| 14 |
+
|
| 15 |
+
template <uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 16 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 17 |
+
uint32_t kSplitFactor,
|
| 18 |
+
uint32_t kSwizzleABMode, uint32_t kSwizzleCDMode,
|
| 19 |
+
uint32_t kNumStages, uint32_t kNumThreads>
|
| 20 |
+
CUTLASS_GLOBAL void __launch_bounds__(kNumThreads, 1)
|
| 21 |
+
sm100_bmn_bnk_mn_gemm_impl(uint32_t shape_s,
|
| 22 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 23 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 24 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_d) {
|
| 25 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 26 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 27 |
+
|
| 28 |
+
// Configs
|
| 29 |
+
constexpr uint32_t LAYOUT_AD_M = 128;
|
| 30 |
+
constexpr uint32_t kNumTMAStoreStages = 2;
|
| 31 |
+
|
| 32 |
+
// Utils
|
| 33 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 34 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 35 |
+
DG_STATIC_ASSERT(BLOCK_M == LAYOUT_AD_M and BLOCK_N == 128 and BLOCK_K == 64, "Invalid block size");
|
| 36 |
+
DG_STATIC_ASSERT(kSwizzleABMode == 128 and kSwizzleCDMode == 128, "Invalid swizzle mode");
|
| 37 |
+
|
| 38 |
+
// Align to 1024 bytes for swizzle-128B
|
| 39 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 40 |
+
|
| 41 |
+
// Shared memory sizes
|
| 42 |
+
constexpr uint32_t SMEM_CD_SIZE_PER_STAGE = BLOCK_M * kSwizzleCDMode;
|
| 43 |
+
constexpr uint32_t SMEM_CD_SIZE = SMEM_CD_SIZE_PER_STAGE * kNumTMAStoreStages;
|
| 44 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(cutlass::bfloat16_t);
|
| 45 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(cutlass::bfloat16_t);
|
| 46 |
+
|
| 47 |
+
// Prefetch TMA descriptors at the very beginning
|
| 48 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 49 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 50 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 51 |
+
cute::prefetch_tma_descriptor(&tensor_map_d);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Real tensor memory size and offsets
|
| 55 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<BLOCK_N>();
|
| 56 |
+
|
| 57 |
+
// Fill D/A/B
|
| 58 |
+
auto smem_cd = utils::PatternVisitor([&](const uint32_t& i) {
|
| 59 |
+
return reinterpret_cast<float*>(smem_buffer + (i * SMEM_CD_SIZE_PER_STAGE));
|
| 60 |
+
});
|
| 61 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 62 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + (SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE));
|
| 63 |
+
});
|
| 64 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 65 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + (SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE));
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
// Fill barriers
|
| 69 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_CD_SIZE +
|
| 70 |
+
kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 71 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 72 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 73 |
+
auto tmem_full_barrier = barrier_start_ptr + (kNumStages * 2);
|
| 74 |
+
|
| 75 |
+
// Fill the tensor memory pointer
|
| 76 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumStages * 2 + 1);
|
| 77 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 78 |
+
|
| 79 |
+
// Initialize barriers
|
| 80 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 81 |
+
#pragma unroll
|
| 82 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 83 |
+
full_barriers[i]->init(1);
|
| 84 |
+
empty_barriers[i]->init(1);
|
| 85 |
+
}
|
| 86 |
+
tmem_full_barrier->init(1);
|
| 87 |
+
|
| 88 |
+
// Make initialized barrier visible in async proxy
|
| 89 |
+
cutlass::arch::fence_barrier_init();
|
| 90 |
+
} else if (warp_idx == 2) {
|
| 91 |
+
// Allocate tensor memory
|
| 92 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 93 |
+
}
|
| 94 |
+
__syncthreads();
|
| 95 |
+
|
| 96 |
+
// Block indices
|
| 97 |
+
const uint32_t num_n_blocks = math::ceil_div(SHAPE_N, BLOCK_N);
|
| 98 |
+
const uint32_t num_mn_blocks = num_n_blocks * math::ceil_div(SHAPE_M, BLOCK_M);
|
| 99 |
+
const uint32_t mn_block_idx = blockIdx.x % num_mn_blocks;
|
| 100 |
+
const uint32_t sk_block_idx = blockIdx.x / num_mn_blocks;
|
| 101 |
+
const uint32_t n_block_idx = mn_block_idx % num_n_blocks;
|
| 102 |
+
const uint32_t m_block_idx = mn_block_idx / num_n_blocks;
|
| 103 |
+
const uint32_t num_total_stages = cute::min(kSplitFactor, shape_s * (SHAPE_K / BLOCK_K) - sk_block_idx * kSplitFactor);
|
| 104 |
+
|
| 105 |
+
// Wait for primary kernel completion
|
| 106 |
+
cudaGridDependencySynchronize();
|
| 107 |
+
|
| 108 |
+
if (warp_idx == 0) {
|
| 109 |
+
// TMA load warp
|
| 110 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 111 |
+
const auto& stage_idx = s % kNumStages;
|
| 112 |
+
empty_barriers[stage_idx]->wait(((s / kNumStages) & 1) ^ 1);
|
| 113 |
+
|
| 114 |
+
uint32_t m_idx = BLOCK_M * m_block_idx;
|
| 115 |
+
uint32_t n_idx = BLOCK_N * n_block_idx;
|
| 116 |
+
uint32_t sk_idx = (sk_block_idx * kSplitFactor + s) * BLOCK_K;
|
| 117 |
+
uint32_t k_idx = sk_idx % SHAPE_K;
|
| 118 |
+
uint32_t s_idx = sk_idx / SHAPE_K;
|
| 119 |
+
|
| 120 |
+
// Issue TMAs
|
| 121 |
+
if (cute::elect_one_sync()) {
|
| 122 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleABMode>(&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_idx, m_idx + s_idx * SHAPE_M);
|
| 123 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleABMode>(&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_idx, n_idx + s_idx * SHAPE_N);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
// Arrive at full barriers
|
| 127 |
+
constexpr uint32_t kNumArrivalBytes = SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE;
|
| 128 |
+
if (cute::elect_one_sync())
|
| 129 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(kNumArrivalBytes);
|
| 130 |
+
}
|
| 131 |
+
} else if (warp_idx == 1) {
|
| 132 |
+
// MMA issue warp
|
| 133 |
+
// NOTES: only the leader CTA will do this
|
| 134 |
+
// Make instruction descriptor
|
| 135 |
+
constexpr uint32_t UMMA_M = LAYOUT_AD_M;
|
| 136 |
+
constexpr uint32_t UMMA_N = BLOCK_N;
|
| 137 |
+
constexpr uint32_t UMMA_K = 32 / sizeof(cutlass::bfloat16_t);
|
| 138 |
+
auto instr_desc = cute::UMMA::make_instr_desc<cutlass::bfloat16_t, cutlass::bfloat16_t, float, UMMA_M, UMMA_N, cute::UMMA::Major::K, cute::UMMA::Major::K>();
|
| 139 |
+
|
| 140 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 141 |
+
auto a_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, BLOCK_M, BLOCK_K, kSwizzleABMode>(smem_a[0], 0, 0);
|
| 142 |
+
auto b_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, BLOCK_N, BLOCK_K, kSwizzleABMode>(smem_b[0], 0, 0);
|
| 143 |
+
uint32_t a_desc_lo = lane_idx < kNumStages ? a_desc.lo + lane_idx * SMEM_A_SIZE_PER_STAGE / 16 : 0u;
|
| 144 |
+
uint32_t b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 145 |
+
|
| 146 |
+
// Checks for MMA instructions
|
| 147 |
+
// NOTES: CUTLASS does not have such checks except the MMA traits, but we are not using these traits
|
| 148 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 149 |
+
(UMMA_M == 128 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256) or
|
| 150 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 151 |
+
"Invalid MMA instruction shape");
|
| 152 |
+
|
| 153 |
+
// Wait tensor memory empty barrier arrival
|
| 154 |
+
ptx::tcgen05_after_thread_sync();
|
| 155 |
+
|
| 156 |
+
// Launch MMAs
|
| 157 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 158 |
+
// Wait TMA arrival
|
| 159 |
+
const auto& stage_idx = s % kNumStages;
|
| 160 |
+
full_barriers[stage_idx]->wait((s / kNumStages) & 1);
|
| 161 |
+
ptx::tcgen05_after_thread_sync();
|
| 162 |
+
|
| 163 |
+
// Issue UMMA in the leader CTA
|
| 164 |
+
const auto& runtime_instr_desc = cute::UMMA::make_runtime_instr_desc(instr_desc);
|
| 165 |
+
const auto& a_desc_base_lo = __shfl_sync(0xffffffff, a_desc_lo, stage_idx);
|
| 166 |
+
const auto& b_desc_base_lo = __shfl_sync(0xffffffff, b_desc_lo, stage_idx);
|
| 167 |
+
if (cute::elect_one_sync()) {
|
| 168 |
+
#pragma unroll
|
| 169 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 170 |
+
a_desc.lo = mma::sm100::advance_umma_desc_lo<cute::UMMA::Major::K, BLOCK_M, kSwizzleABMode, cutlass::bfloat16_t>(
|
| 171 |
+
a_desc_base_lo, 0, k * UMMA_K);
|
| 172 |
+
b_desc.lo = mma::sm100::advance_umma_desc_lo<cute::UMMA::Major::K, BLOCK_N, kSwizzleABMode, cutlass::bfloat16_t>(
|
| 173 |
+
b_desc_base_lo, 0, k * UMMA_K);
|
| 174 |
+
ptx::SM100_MMA_F16BF16_SS::fma(a_desc, b_desc, 0, s > 0 or k > 0, runtime_instr_desc);
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// Commit to the mbarrier object
|
| 179 |
+
// No explicit `tcgen05.fence::before_thread_sync` is needed, as this is implicitly performed by `tcgen05.commit`
|
| 180 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 181 |
+
}
|
| 182 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barrier));
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
// NOTES: tensor memory addresses are simplified, as the hardware will ignore the warp index bits,
|
| 186 |
+
// i.e., no need for `tmem_ptr |= (warp_idx * 32) << 16`.
|
| 187 |
+
// NOTES: we also forbid two CTAs to share the same SM and its tensor memory
|
| 188 |
+
if (warp_idx == 2)
|
| 189 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 190 |
+
|
| 191 |
+
// TMA checks
|
| 192 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 193 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(float);
|
| 194 |
+
constexpr uint32_t STORE_BLOCK_N = kSwizzleCDMode / sizeof(float);
|
| 195 |
+
DG_STATIC_ASSERT(kSwizzleCDMode > 0, "TMA D must be swizzled");
|
| 196 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N % kNumElemsPerBankGroup == 0, "Invalid swizzling");
|
| 197 |
+
|
| 198 |
+
// Wait UMMA arrival
|
| 199 |
+
tmem_full_barrier->wait(0);
|
| 200 |
+
ptx::tcgen05_after_thread_sync();
|
| 201 |
+
|
| 202 |
+
// Load from tensor memory into registers, and write shared memory with STSM
|
| 203 |
+
DG_STATIC_ASSERT(BLOCK_N % STORE_BLOCK_N == 0, "Invalid block sizes");
|
| 204 |
+
|
| 205 |
+
// Issue every swizzled atom and pipeline STSM and TMA store
|
| 206 |
+
constexpr uint32_t kNumStores = BLOCK_N / STORE_BLOCK_N;
|
| 207 |
+
#pragma unroll
|
| 208 |
+
for (uint32_t s = 0; s < kNumStores; ++ s) {
|
| 209 |
+
// Wait shared memory to be released
|
| 210 |
+
if (s >= kNumTMAStoreStages) {
|
| 211 |
+
if (warp_idx == 0 and cute::elect_one_sync())
|
| 212 |
+
cute::tma_store_wait<kNumTMAStoreStages - 1>();
|
| 213 |
+
cutlass::arch::NamedBarrier(kNumThreads).sync();
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// The pipeline stage
|
| 217 |
+
const auto tma_stage_idx = s % kNumTMAStoreStages;
|
| 218 |
+
const auto m_idx = m_block_idx * BLOCK_M;
|
| 219 |
+
const auto n_idx = n_block_idx * BLOCK_N + s * STORE_BLOCK_N;
|
| 220 |
+
|
| 221 |
+
// Store into shared memory
|
| 222 |
+
#pragma unroll
|
| 223 |
+
for (uint32_t i = 0; i < STORE_BLOCK_N / kNumElemsPerBankGroup; ++ i) {
|
| 224 |
+
// Calculate the index of the bank group to be written in the atom
|
| 225 |
+
auto bank_group_index = i + lane_idx * (kSwizzleCDMode / kNumBankGroupBytes);
|
| 226 |
+
|
| 227 |
+
// Reshape the atom in another view and swizzle
|
| 228 |
+
// - original: `(LAYOUT_AD_M, kSwizzleCDMode / kNumBankGroupBytes)`
|
| 229 |
+
// - new: `(LAYOUT_AD_M * kSwizzleCDMode / kNumBankGroupBytes / 8, 8)`
|
| 230 |
+
// NOTES: "8" is the number of bank groups, "16" is the swizzling pattern
|
| 231 |
+
constexpr bool kHasShortcut = (kSwizzleCDMode / kNumBankGroupBytes) == 8;
|
| 232 |
+
auto row = kHasShortcut ? (i / 8 + lane_idx) : (bank_group_index / 8);
|
| 233 |
+
auto col = kHasShortcut ? (i) : (bank_group_index % 8);
|
| 234 |
+
col ^= row % (kSwizzleCDMode / 16);
|
| 235 |
+
|
| 236 |
+
// Source and destination memory address
|
| 237 |
+
uint32_t tmem_addr = s * STORE_BLOCK_N + i * kNumElemsPerBankGroup; // In-block offset
|
| 238 |
+
auto smem_ptr = reinterpret_cast<uint8_t*>(smem_cd[tma_stage_idx]) + // Base pointer
|
| 239 |
+
warp_idx * 32 * kSwizzleCDMode + // Warp offset
|
| 240 |
+
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
| 241 |
+
|
| 242 |
+
// Load from tensor memory, store into shared memory
|
| 243 |
+
uint32_t values[kNumElemsPerBankGroup];
|
| 244 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 4, "Invalid type");
|
| 245 |
+
cute::SM100_TMEM_LOAD_32dp32b4x::copy(tmem_addr,
|
| 246 |
+
values[0], values[1], values[2], values[3]);
|
| 247 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 248 |
+
ptx::st_shared(smem_ptr, values[0], values[1], values[2], values[3]);
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
// Synchronize all threads and issue TMA
|
| 252 |
+
cute::tma_store_fence();
|
| 253 |
+
cutlass::arch::NamedBarrier(kNumThreads).sync();
|
| 254 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 255 |
+
cute::SM90_TMA_REDUCE_ADD_2D::copy(&tensor_map_d, smem_cd[tma_stage_idx], n_idx, m_idx);
|
| 256 |
+
cute::tma_store_arrive();
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
// Deallocate tensor memory by warp 1
|
| 261 |
+
// NOTES: warp 0 is doing TMA stores
|
| 262 |
+
if (warp_idx == 1)
|
| 263 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
|
| 264 |
+
|
| 265 |
+
#else
|
| 266 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 267 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 268 |
+
#endif
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
}
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp4_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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<cutlass/arch/barrier.h>
|
| 4 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 5 |
+
|
| 6 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 7 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 10 |
+
#include <deep_gemm/common/utils.cuh>
|
| 11 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 12 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 13 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 15 |
+
|
| 16 |
+
namespace deep_gemm {
|
| 17 |
+
|
| 18 |
+
template <uint32_t kNumHeads, uint32_t kHeadDim,
|
| 19 |
+
bool kIsCompressedLogits,
|
| 20 |
+
uint32_t BLOCK_Q, uint32_t BLOCK_KV,
|
| 21 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 22 |
+
uint32_t kNumSMs,
|
| 23 |
+
uint32_t kNumSpecializedThreads, uint32_t kNumMathThreads,
|
| 24 |
+
typename logits_dtype_t,
|
| 25 |
+
uint32_t kNumMathWarpGroups = kNumMathThreads / 128>
|
| 26 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumSpecializedThreads + kNumMathThreads, 1)
|
| 27 |
+
void sm100_fp4_mqa_logits(const uint32_t seq_len, const uint32_t seq_len_kv,
|
| 28 |
+
const uint32_t max_seqlen_k,
|
| 29 |
+
const uint32_t logits_stride,
|
| 30 |
+
const uint32_t* cu_seq_len_k_start,
|
| 31 |
+
const uint32_t* cu_seq_len_k_end,
|
| 32 |
+
logits_dtype_t* logits,
|
| 33 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 34 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sf_q,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sf_kv,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 38 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 39 |
+
|
| 40 |
+
// Utils
|
| 41 |
+
const auto sm_idx = blockIdx.x;
|
| 42 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 43 |
+
const auto warpgroup_idx = warp_idx / 4;
|
| 44 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 45 |
+
constexpr uint32_t kSpecWarpStart = kNumMathWarpGroups * 4;
|
| 46 |
+
|
| 47 |
+
// Prefetch TMA descriptors
|
| 48 |
+
if (warp_idx == kSpecWarpStart) {
|
| 49 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 50 |
+
cute::prefetch_tma_descriptor(&tensor_map_sf_q);
|
| 51 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 52 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 53 |
+
cute::prefetch_tma_descriptor(&tensor_map_sf_kv);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// UMMA configs
|
| 57 |
+
static constexpr uint32_t kNumTmemStages = 3;
|
| 58 |
+
static constexpr uint32_t kNumUTCCPAlignedElems = 128;
|
| 59 |
+
static constexpr uint32_t UMMA_M = 128;
|
| 60 |
+
static constexpr uint32_t UMMA_N = BLOCK_Q * kNumHeads;
|
| 61 |
+
static constexpr uint32_t UMMA_K = 64;
|
| 62 |
+
static constexpr uint32_t kNumSFQ = math::constexpr_align(BLOCK_Q * kNumHeads, kNumUTCCPAlignedElems);
|
| 63 |
+
static constexpr uint32_t kNumSFKV = math::constexpr_align(BLOCK_KV, kNumUTCCPAlignedElems);
|
| 64 |
+
static constexpr uint32_t kRealNumSFQ = BLOCK_Q * kNumHeads;
|
| 65 |
+
DG_STATIC_ASSERT(kNumSpecializedThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 66 |
+
DG_STATIC_ASSERT(BLOCK_KV == kNumMathWarpGroups * UMMA_M and BLOCK_KV % kNumUTCCPAlignedElems == 0, "Invalid `BLOCK_KV`");
|
| 67 |
+
|
| 68 |
+
// Shared memory configs
|
| 69 |
+
static constexpr uint32_t kSwizzleAlignment = 8 * (kHeadDim / 2);
|
| 70 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * (kHeadDim / 2);
|
| 71 |
+
static constexpr uint32_t SMEM_SF_Q_SIZE_PER_STAGE = kNumSFQ * sizeof(int);
|
| 72 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = BLOCK_KV * (kHeadDim / 2);
|
| 73 |
+
static constexpr uint32_t SMEM_SF_KV_SIZE_PER_STAGE = kNumSFKV * sizeof(int);
|
| 74 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * sizeof(float);
|
| 75 |
+
|
| 76 |
+
// Align to swizzling alignment bytes
|
| 77 |
+
extern __shared__ __align__(kSwizzleAlignment) uint8_t smem_buffer[];
|
| 78 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 79 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 80 |
+
|
| 81 |
+
// Q and KV data on shared memory
|
| 82 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 83 |
+
return smem_buffer + SMEM_Q_SIZE_PER_STAGE * i;
|
| 84 |
+
});
|
| 85 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 86 |
+
return smem_buffer + SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * i;
|
| 87 |
+
});
|
| 88 |
+
const auto smem_sf_ptr = smem_buffer + (SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * kNumKVStages);
|
| 89 |
+
auto smem_sf_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 90 |
+
return reinterpret_cast<uint32_t*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * i);
|
| 91 |
+
});
|
| 92 |
+
auto smem_sf_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 93 |
+
return reinterpret_cast<uint32_t*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * kNumQStages + SMEM_SF_KV_SIZE_PER_STAGE * i);
|
| 94 |
+
});
|
| 95 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 96 |
+
return reinterpret_cast<float*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * kNumQStages + SMEM_SF_KV_SIZE_PER_STAGE * kNumKVStages
|
| 97 |
+
+ SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
// Barriers and TMEM pointer on shared memory
|
| 101 |
+
const auto barrier_ptr = reinterpret_cast<Barrier*>(smem_weights[kNumQStages]);
|
| 102 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + i; });
|
| 103 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages + i; });
|
| 104 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + i; });
|
| 105 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + kNumKVStages + i; });
|
| 106 |
+
const auto tmem_barrier_ptr = barrier_ptr + kNumQStages * 2 + kNumKVStages * 2;
|
| 107 |
+
auto full_tmem_barriers = utils::PatternVisitor([&](const uint32_t& i) { return tmem_barrier_ptr + i; });
|
| 108 |
+
auto empty_tmem_barriers = utils::PatternVisitor([&](const uint32_t& i) { return tmem_barrier_ptr + kNumTmemStages + i; });
|
| 109 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(tmem_barrier_ptr + kNumTmemStages * 2);
|
| 110 |
+
|
| 111 |
+
// Tensor memory configs
|
| 112 |
+
constexpr uint32_t kNumAccumTmemCols = BLOCK_Q * kNumHeads * kNumTmemStages;
|
| 113 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<kNumAccumTmemCols + kNumSFQ / 32 + kNumSFKV / 32>();
|
| 114 |
+
constexpr uint32_t kTmemStartColOfSFQ = kNumAccumTmemCols;
|
| 115 |
+
constexpr uint32_t kTmemStartColOfSFKV = kNumAccumTmemCols + kNumSFQ / 32;
|
| 116 |
+
DG_STATIC_ASSERT(kNumTmemCols <= 512, "Too many tensor memory");
|
| 117 |
+
|
| 118 |
+
// Initialize barriers
|
| 119 |
+
if (warp_idx == kSpecWarpStart + 1 and cute::elect_one_sync()) {
|
| 120 |
+
#pragma unroll
|
| 121 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 122 |
+
full_q_barriers[i]->init(1);
|
| 123 |
+
empty_q_barriers[i]->init(kNumMathThreads + 32);
|
| 124 |
+
}
|
| 125 |
+
#pragma unroll
|
| 126 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 127 |
+
full_kv_barriers[i]->init(1);
|
| 128 |
+
empty_kv_barriers[i]->init(1);
|
| 129 |
+
}
|
| 130 |
+
#pragma unroll
|
| 131 |
+
for (uint32_t i = 0; i < kNumTmemStages; ++i) {
|
| 132 |
+
full_tmem_barriers[i]->init(1);
|
| 133 |
+
empty_tmem_barriers[i]->init(128);
|
| 134 |
+
}
|
| 135 |
+
cutlass::arch::fence_barrier_init();
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// Allocate tensor memory
|
| 139 |
+
if (warp_idx == kSpecWarpStart + 2)
|
| 140 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 141 |
+
__syncthreads();
|
| 142 |
+
|
| 143 |
+
// Scheduler
|
| 144 |
+
const uint32_t num_q_blocks = math::ceil_div(seq_len, BLOCK_Q);
|
| 145 |
+
uint32_t seq_k_start[BLOCK_Q], seq_k_end[BLOCK_Q];
|
| 146 |
+
auto load_schedule = [&](const uint32_t& q_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 147 |
+
uint32_t start = cute::numeric_limits<uint32_t>::max();
|
| 148 |
+
uint32_t end = cute::numeric_limits<uint32_t>::min();
|
| 149 |
+
#pragma unroll
|
| 150 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 151 |
+
const auto row_idx = cute::min(q_idx * BLOCK_Q + i, seq_len - 1);
|
| 152 |
+
seq_k_start[i] = cute::min(cu_seq_len_k_start[row_idx], seq_len_kv);
|
| 153 |
+
seq_k_end[i] = cute::min(cu_seq_len_k_end[row_idx], seq_len_kv);
|
| 154 |
+
start = cute::min(start, seq_k_start[i]);
|
| 155 |
+
end = cute::max(end, seq_k_end[i]);
|
| 156 |
+
}
|
| 157 |
+
// TMA alignment requirements for SF KV
|
| 158 |
+
start = start / 4 * 4;
|
| 159 |
+
return {start, math::ceil_div(end - start, BLOCK_KV)};
|
| 160 |
+
};
|
| 161 |
+
|
| 162 |
+
// Make Q, KV and TMEM pipeline
|
| 163 |
+
auto make_pipeline = [](const uint32_t& num_stages) {
|
| 164 |
+
// Return current stage and phase, and advance pipeline by steps
|
| 165 |
+
return [iter_idx = 0u, num_stages](const uint32_t& step = 1) mutable -> cute::tuple<uint32_t, uint32_t> {
|
| 166 |
+
uint32_t current_idx = iter_idx;
|
| 167 |
+
iter_idx += step;
|
| 168 |
+
return {current_idx % num_stages, (current_idx / num_stages) & 1};
|
| 169 |
+
};
|
| 170 |
+
};
|
| 171 |
+
auto advance_q_pipeline = make_pipeline(kNumQStages);
|
| 172 |
+
auto advance_kv_pipeline = make_pipeline(kNumKVStages);
|
| 173 |
+
auto advance_tmem_pipeline = make_pipeline(kNumTmemStages);
|
| 174 |
+
|
| 175 |
+
// Register reconfigurations
|
| 176 |
+
constexpr uint32_t kNumSpecializedRegisters = 56;
|
| 177 |
+
constexpr uint32_t kNumMathRegisters = 224;
|
| 178 |
+
|
| 179 |
+
// Wait for primary kernel completion
|
| 180 |
+
cudaGridDependencySynchronize();
|
| 181 |
+
|
| 182 |
+
if (warp_idx == kSpecWarpStart) {
|
| 183 |
+
// TMA warp for loading Q
|
| 184 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 185 |
+
|
| 186 |
+
// Enumerate Q blocks
|
| 187 |
+
if (cute::elect_one_sync()) {
|
| 188 |
+
for (uint32_t q_idx = sm_idx; q_idx < num_q_blocks; q_idx += kNumSMs) {
|
| 189 |
+
// Wait Q consumer release
|
| 190 |
+
CUTE_TIE_DECL(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 191 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 192 |
+
|
| 193 |
+
// Issue TMA Q
|
| 194 |
+
cute::SM90_TMA_LOAD_2D::copy(&tensor_map_q, reinterpret_cast<uint64_t*>(full_q_barriers[q_stage_idx]),
|
| 195 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 196 |
+
smem_q[q_stage_idx], 0, q_idx * BLOCK_Q * kNumHeads);
|
| 197 |
+
tma::copy<BLOCK_Q * kNumHeads, 1, 0>(&tensor_map_sf_q, full_q_barriers[q_stage_idx], smem_sf_q[q_stage_idx], 0, q_idx * BLOCK_Q);
|
| 198 |
+
tma::copy<kNumHeads, BLOCK_Q, 0>(&tensor_map_weights, full_q_barriers[q_stage_idx], smem_weights[q_stage_idx], 0, q_idx * BLOCK_Q);
|
| 199 |
+
full_q_barriers[q_stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + kRealNumSFQ * sizeof(int) + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
__syncwarp();
|
| 203 |
+
} else if (warp_idx == kSpecWarpStart + 1) {
|
| 204 |
+
// TMA warp for loading KV cache
|
| 205 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 206 |
+
|
| 207 |
+
if (cute::elect_one_sync()) {
|
| 208 |
+
// Enumerate Q blocks
|
| 209 |
+
for (uint32_t q_idx = sm_idx; q_idx < num_q_blocks; q_idx += kNumSMs) {
|
| 210 |
+
// Load KV block ranges
|
| 211 |
+
CUTE_TIE_DECL(load_schedule(q_idx), kv_start, num_kv_blocks);
|
| 212 |
+
|
| 213 |
+
// Enumerate KV blocks
|
| 214 |
+
for (uint32_t kv_idx = 0; kv_idx < num_kv_blocks; ++ kv_idx) {
|
| 215 |
+
// Wait KV consumer release
|
| 216 |
+
CUTE_TIE_DECL(advance_kv_pipeline(), kv_stage_idx, kv_phase);
|
| 217 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 218 |
+
|
| 219 |
+
// Issue TMA KV
|
| 220 |
+
cute::SM90_TMA_LOAD_2D::copy(&tensor_map_kv, reinterpret_cast<uint64_t*>(full_kv_barriers[kv_stage_idx]),
|
| 221 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 222 |
+
smem_kv[kv_stage_idx], 0, kv_start + kv_idx * BLOCK_KV);
|
| 223 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_sf_kv, full_kv_barriers[kv_stage_idx],
|
| 224 |
+
smem_sf_kv[kv_stage_idx],
|
| 225 |
+
kv_start + kv_idx * BLOCK_KV, 0);
|
| 226 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_SF_KV_SIZE_PER_STAGE);
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
} else if (warp_idx == kSpecWarpStart + 2) {
|
| 231 |
+
// UMMA warp
|
| 232 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 233 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 234 |
+
|
| 235 |
+
// UTCCP transposer
|
| 236 |
+
auto utccp_required_smem_warp_transpose = [&](const uint32_t* smem_ptr) {
|
| 237 |
+
DG_STATIC_ASSERT(kNumUTCCPAlignedElems == 128, "Invalid aligned elements");
|
| 238 |
+
uint32_t values[4];
|
| 239 |
+
#pragma unroll
|
| 240 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 241 |
+
values[i] = ptx::ld_shared(smem_ptr + (i ^ (lane_idx >> 3)) * 32 + lane_idx);
|
| 242 |
+
__syncwarp();
|
| 243 |
+
#pragma unroll
|
| 244 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 245 |
+
ptx::st_shared(smem_ptr + lane_idx * 4 + (i ^ (lane_idx >> 3)), values[i]);
|
| 246 |
+
};
|
| 247 |
+
|
| 248 |
+
// Make UMMA desc
|
| 249 |
+
auto instr_desc = cute::UMMA::make_instr_desc_block_scaled<cutlass::float_e2m1_t, cutlass::float_e2m1_t, float, cutlass::float_ue8m0_t,
|
| 250 |
+
UMMA_M, UMMA_N, cute::UMMA::Major::K, cute::UMMA::Major::K>();
|
| 251 |
+
auto sf_desc = mma::sm100::make_sf_desc(nullptr);
|
| 252 |
+
|
| 253 |
+
// Enumerate Q blocks
|
| 254 |
+
for (uint32_t q_idx = sm_idx; q_idx < num_q_blocks; q_idx += kNumSMs) {
|
| 255 |
+
// Load KV block ranges
|
| 256 |
+
CUTE_TIE_DECL(load_schedule(q_idx), kv_start, num_kv_blocks);
|
| 257 |
+
|
| 258 |
+
// Wait TMA Q arrivals
|
| 259 |
+
CUTE_TIE_DECL(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 260 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 261 |
+
|
| 262 |
+
// Transpose and copy SF Q
|
| 263 |
+
#pragma unroll
|
| 264 |
+
for (uint32_t i = 0; i < kNumSFQ / kNumUTCCPAlignedElems; ++ i) {
|
| 265 |
+
auto smem_ptr = smem_sf_q[q_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 266 |
+
utccp_required_smem_warp_transpose(smem_ptr);
|
| 267 |
+
cutlass::arch::fence_view_async_shared();
|
| 268 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 269 |
+
if (cute::elect_one_sync())
|
| 270 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta::copy(sf_desc, kTmemStartColOfSFQ + i * 4);
|
| 271 |
+
__syncwarp();
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// Enumerate KV blocks
|
| 275 |
+
for (uint32_t kv_idx = 0; kv_idx < num_kv_blocks; ++ kv_idx) {
|
| 276 |
+
// Wait TMA KV arrivals
|
| 277 |
+
CUTE_TIE_DECL(advance_kv_pipeline(), kv_stage_idx, kv_phase);
|
| 278 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 279 |
+
|
| 280 |
+
// Transpose
|
| 281 |
+
#pragma unroll
|
| 282 |
+
for (uint32_t i = 0; i < kNumSFKV / kNumUTCCPAlignedElems; ++ i) {
|
| 283 |
+
auto smem_ptr = smem_sf_kv[kv_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 284 |
+
utccp_required_smem_warp_transpose(smem_ptr);
|
| 285 |
+
cutlass::arch::fence_view_async_shared();
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// UMMA with SF
|
| 289 |
+
if (cute::elect_one_sync()) {
|
| 290 |
+
// Copy SF KV
|
| 291 |
+
#pragma unroll
|
| 292 |
+
for (uint32_t i = 0; i < kNumSFKV / kNumUTCCPAlignedElems; ++ i) {
|
| 293 |
+
auto smem_ptr = smem_sf_kv[kv_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 294 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 295 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta::copy(sf_desc, kTmemStartColOfSFKV + i * 4);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
#pragma unroll
|
| 299 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++ i) {
|
| 300 |
+
// Wait TMEM release
|
| 301 |
+
CUTE_TIE_DECL(advance_tmem_pipeline(), tmem_stage_idx, tmem_phase);
|
| 302 |
+
uint32_t tmem_addr = tmem_stage_idx * UMMA_N;
|
| 303 |
+
|
| 304 |
+
empty_tmem_barriers[tmem_stage_idx]->wait(tmem_phase ^ 1);
|
| 305 |
+
ptx::tcgen05_after_thread_sync();
|
| 306 |
+
|
| 307 |
+
// Issue UMMA with SF
|
| 308 |
+
#pragma unroll
|
| 309 |
+
for (uint32_t k = 0; k < kHeadDim / UMMA_K; ++ k) {
|
| 310 |
+
auto runtime_instr_desc = mma::sm100::make_runtime_instr_desc_with_sf_id(instr_desc, k * 2, k * 2);
|
| 311 |
+
// TODO: generalize umma desc
|
| 312 |
+
DG_STATIC_ASSERT(kHeadDim == 128, "Invalid head dim");
|
| 313 |
+
auto a_desc = mma::sm100::make_smem_desc(
|
| 314 |
+
cute::UMMA::LayoutType::SWIZZLE_64B,
|
| 315 |
+
smem_kv[kv_stage_idx] + i * UMMA_M * (kHeadDim / 2) + k * UMMA_K / 2,
|
| 316 |
+
8 * (kHeadDim / 2), 0);
|
| 317 |
+
auto b_desc = mma::sm100::make_smem_desc(
|
| 318 |
+
cute::UMMA::LayoutType::SWIZZLE_64B,
|
| 319 |
+
smem_q[q_stage_idx] + k * UMMA_K / 2,
|
| 320 |
+
8 * (kHeadDim / 2), 0);
|
| 321 |
+
ptx::SM100_MMA_MXF4_SS::fma(
|
| 322 |
+
a_desc, b_desc, tmem_addr, k, runtime_instr_desc,
|
| 323 |
+
kTmemStartColOfSFKV + i * 4, kTmemStartColOfSFQ);
|
| 324 |
+
}
|
| 325 |
+
// TODO: move this into `deep_gemm/ptx/tcgen05.cuh`
|
| 326 |
+
asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
|
| 327 |
+
::"r"(cute::cast_smem_ptr_to_uint(full_tmem_barriers[tmem_stage_idx])));
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(empty_kv_barriers[kv_stage_idx]));
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// UMMA warp must also arrive on empty_q to prevent running ahead
|
| 334 |
+
// of math warps in the Q pipeline. Without this, UMMA can consume
|
| 335 |
+
// kNumQStages Q blocks before math warps release any, causing a
|
| 336 |
+
// circular dependency: UMMA waits full_q -> TMA_Q waits empty_q
|
| 337 |
+
// -> Math waits full_tmem -> UMMA (already moved on).
|
| 338 |
+
empty_q_barriers[q_stage_idx]->arrive();
|
| 339 |
+
}
|
| 340 |
+
} else if (warp_idx == kSpecWarpStart + 3) {
|
| 341 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 342 |
+
} else if (warp_idx < kSpecWarpStart) {
|
| 343 |
+
// Math warpgroups for reduce
|
| 344 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 345 |
+
|
| 346 |
+
const auto math_warpgroup_idx = warpgroup_idx;
|
| 347 |
+
const auto math_thread_idx = threadIdx.x;
|
| 348 |
+
|
| 349 |
+
// Helper lambda for loading tensor memory
|
| 350 |
+
auto tmem_load = [](auto num_elems_c, const uint32_t& tmem_addr, float* accum) {
|
| 351 |
+
constexpr uint32_t N = decltype(num_elems_c)::value;
|
| 352 |
+
DG_STATIC_ASSERT(N == 32 or N == 64, "Unsupported TMEM load size");
|
| 353 |
+
using Loader = cute::conditional_t<N == 32,
|
| 354 |
+
cute::SM100_TMEM_LOAD_32dp32b32x,
|
| 355 |
+
cute::SM100_TMEM_LOAD_32dp32b64x>;
|
| 356 |
+
[&]<size_t... Is>(cute::index_sequence<Is...>) {
|
| 357 |
+
Loader::copy(tmem_addr, reinterpret_cast<uint32_t*>(accum)[Is]...);
|
| 358 |
+
}(cute::make_index_sequence<N>{});
|
| 359 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 360 |
+
};
|
| 361 |
+
|
| 362 |
+
// Math warpgroups process TMEM stages alternately
|
| 363 |
+
// Advance pipeline to align with the assigned stage
|
| 364 |
+
advance_tmem_pipeline(math_warpgroup_idx);
|
| 365 |
+
|
| 366 |
+
// Local register buffers
|
| 367 |
+
float accum[kNumHeads];
|
| 368 |
+
float weights[BLOCK_Q][kNumHeads];
|
| 369 |
+
|
| 370 |
+
// Enumerate Q blocks
|
| 371 |
+
for (uint32_t q_idx = sm_idx; q_idx < num_q_blocks; q_idx += kNumSMs) {
|
| 372 |
+
// Load KV block ranges
|
| 373 |
+
CUTE_TIE_DECL(load_schedule(q_idx), kv_start, num_kv_blocks);
|
| 374 |
+
|
| 375 |
+
// Wait TMA Q arrivals
|
| 376 |
+
CUTE_TIE_DECL(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 377 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 378 |
+
|
| 379 |
+
// Read weights
|
| 380 |
+
// TODO: optimize bank conflicts
|
| 381 |
+
#pragma unroll
|
| 382 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 383 |
+
#pragma unroll
|
| 384 |
+
for (uint32_t j = 0; j < kNumHeads; ++ j)
|
| 385 |
+
weights[i][j] = ptx::ld_shared(smem_weights[q_stage_idx] + i * kNumHeads + j);
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
// Enumerate KV blocks
|
| 389 |
+
for (uint32_t kv_idx = 0; kv_idx < num_kv_blocks; ++ kv_idx) {
|
| 390 |
+
// Calculate KV offset in advance
|
| 391 |
+
auto kv_offset = kv_start + kv_idx * BLOCK_KV + math_thread_idx;
|
| 392 |
+
|
| 393 |
+
// Advance pipeline by `kNumMathWarpGroups` steps
|
| 394 |
+
// Wait UMMA arrival
|
| 395 |
+
CUTE_TIE_DECL(advance_tmem_pipeline(kNumMathWarpGroups), tmem_stage_idx, tmem_phase);
|
| 396 |
+
full_tmem_barriers[tmem_stage_idx]->wait(tmem_phase);
|
| 397 |
+
ptx::tcgen05_after_thread_sync();
|
| 398 |
+
|
| 399 |
+
// Reduce over the head dim and store
|
| 400 |
+
#pragma unroll
|
| 401 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 402 |
+
// Load accumulator from TMEM
|
| 403 |
+
uint32_t tmem_addr = tmem_stage_idx * UMMA_N + i * kNumHeads;
|
| 404 |
+
tmem_load(cute::Int<kNumHeads / 2>{}, tmem_addr, accum);
|
| 405 |
+
tmem_load(cute::Int<kNumHeads / 2>{}, tmem_addr + kNumHeads / 2, accum + kNumHeads / 2);
|
| 406 |
+
|
| 407 |
+
// Release TMEM empty
|
| 408 |
+
if (i == BLOCK_Q - 1) {
|
| 409 |
+
ptx::tcgen05_before_thread_sync();
|
| 410 |
+
empty_tmem_barriers[tmem_stage_idx]->arrive();
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
// Accumulate weighted ReLU in parallel
|
| 414 |
+
auto sum_0 = make_float2(0, 0);
|
| 415 |
+
auto sum_1 = make_float2(0, 0);
|
| 416 |
+
|
| 417 |
+
const auto transform = [&](const uint32_t& j, const float2& sum) {
|
| 418 |
+
auto a = make_float2(fmaxf(accum[j], 0), fmaxf(accum[j + 1], 0));
|
| 419 |
+
auto b = make_float2(weights[i][j], weights[i][j + 1]);
|
| 420 |
+
return __ffma2_rn(a, b, sum);
|
| 421 |
+
};
|
| 422 |
+
|
| 423 |
+
#pragma unroll
|
| 424 |
+
for (uint32_t j = 0; j < kNumHeads; j += 4) {
|
| 425 |
+
sum_0 = transform(j, sum_0);
|
| 426 |
+
sum_1 = transform(j + 2, sum_1);
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
auto sum = __fadd2_rn(sum_0, sum_1);
|
| 430 |
+
auto result = static_cast<logits_dtype_t>(sum.x + sum.y);
|
| 431 |
+
|
| 432 |
+
// Store into the global memory
|
| 433 |
+
// NOTES: we have redundant writes here, consider more carefully
|
| 434 |
+
// TODO: optimize performance
|
| 435 |
+
const auto q_offset = (q_idx * BLOCK_Q + i) * static_cast<uint64_t>(logits_stride);
|
| 436 |
+
if constexpr (kIsCompressedLogits) {
|
| 437 |
+
if (seq_k_start[i] <= kv_offset and kv_offset < seq_k_end[i])
|
| 438 |
+
logits[q_offset + kv_offset - seq_k_start[i]] = result;
|
| 439 |
+
} else {
|
| 440 |
+
logits[q_offset + kv_offset] = result;
|
| 441 |
+
}
|
| 442 |
+
__syncwarp();
|
| 443 |
+
}
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
// Release last Q empty
|
| 447 |
+
empty_q_barriers[q_stage_idx]->arrive();
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
// Free tensor memory
|
| 451 |
+
cutlass::arch::NamedBarrier(kNumMathThreads, 0).sync();
|
| 452 |
+
if (warp_idx == 0)
|
| 453 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp4_paged_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 5 |
+
|
| 6 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 7 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 10 |
+
#include <deep_gemm/common/math.cuh>
|
| 11 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 12 |
+
#include <deep_gemm/common/utils.cuh>
|
| 13 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 17 |
+
#include <deep_gemm/scheduler/paged_mqa_logits.cuh>
|
| 18 |
+
|
| 19 |
+
namespace deep_gemm {
|
| 20 |
+
|
| 21 |
+
template <uint32_t kNextN, uint32_t kNumHeads,
|
| 22 |
+
uint32_t kHeadDim, uint32_t BLOCK_KV,
|
| 23 |
+
bool kIsContextLens2D, bool kIsVarlen,
|
| 24 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 25 |
+
uint32_t SPLIT_KV,
|
| 26 |
+
uint32_t kNumSpecializedThreads, uint32_t kNumMathThreads,
|
| 27 |
+
typename logits_dtype_t,
|
| 28 |
+
uint32_t kNumMathWarpGroups = kNumMathThreads / 128>
|
| 29 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumSpecializedThreads + kNumMathThreads, 1)
|
| 30 |
+
void sm100_fp4_paged_mqa_logits(const uint32_t batch_size,
|
| 31 |
+
const uint32_t logits_stride, const uint32_t block_table_stride,
|
| 32 |
+
const uint32_t* context_lens, logits_dtype_t* logits,
|
| 33 |
+
const uint32_t* block_table, const uint32_t* indices,
|
| 34 |
+
const uint32_t* schedule_meta,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sf_q,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 38 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sf_kv,
|
| 39 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 40 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 41 |
+
|
| 42 |
+
// Utils
|
| 43 |
+
const auto sm_idx = blockIdx.x;
|
| 44 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 45 |
+
const auto warpgroup_idx = warp_idx / 4;
|
| 46 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 47 |
+
constexpr uint32_t kSpecWarpStart = kNumMathWarpGroups * 4;
|
| 48 |
+
|
| 49 |
+
// Prefetch TMA descriptors
|
| 50 |
+
if (warp_idx == kSpecWarpStart) {
|
| 51 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 52 |
+
cute::prefetch_tma_descriptor(&tensor_map_sf_q);
|
| 53 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 54 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 55 |
+
cute::prefetch_tma_descriptor(&tensor_map_sf_kv);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// For non-varlen odd kNextN >= 3, pad to even using TMA OOB zero-fill.
|
| 59 |
+
static constexpr bool kPadOddN = (not kIsVarlen) and (kNextN % 2 == 1) and (kNextN >= 3);
|
| 60 |
+
static constexpr uint32_t kNextNAtom = (kIsVarlen or kNextN >= 2) ? 2 : 1;
|
| 61 |
+
static constexpr uint32_t kNumNextNAtoms = math::constexpr_ceil_div(kNextN, kNextNAtom);
|
| 62 |
+
|
| 63 |
+
// UMMA configs
|
| 64 |
+
static constexpr uint32_t kNumTmemStages = 3;
|
| 65 |
+
static constexpr uint32_t kNumUTCCPAlignedElems = 128;
|
| 66 |
+
static constexpr uint32_t UMMA_M = 128;
|
| 67 |
+
static constexpr uint32_t UMMA_N = kNextNAtom * kNumHeads;
|
| 68 |
+
static constexpr uint32_t UMMA_K = 64;
|
| 69 |
+
static constexpr uint32_t kNumSFQAtom = math::constexpr_align(kNextNAtom * kNumHeads, kNumUTCCPAlignedElems);
|
| 70 |
+
static constexpr uint32_t kNumSFKV = math::constexpr_align(SPLIT_KV, kNumUTCCPAlignedElems);
|
| 71 |
+
static constexpr uint32_t kRealNumSFQAtom = kNextNAtom * kNumHeads;
|
| 72 |
+
DG_STATIC_ASSERT(kNumSpecializedThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 73 |
+
DG_STATIC_ASSERT(SPLIT_KV == kNumMathWarpGroups * UMMA_M and SPLIT_KV % kNumUTCCPAlignedElems == 0, "Invalid `SPLIT_KV`");
|
| 74 |
+
|
| 75 |
+
// Shared memory configs
|
| 76 |
+
static constexpr uint32_t kSwizzleAlignment = 8 * (kHeadDim / 2);
|
| 77 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = kNextNAtom * kNumHeads * (kHeadDim / 2);
|
| 78 |
+
static constexpr uint32_t SMEM_SF_Q_SIZE_PER_STAGE = kNumSFQAtom * sizeof(int);
|
| 79 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = SPLIT_KV * (kHeadDim / 2);
|
| 80 |
+
static constexpr uint32_t SMEM_SF_KV_SIZE_PER_STAGE = kNumSFKV * sizeof(int);
|
| 81 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = kNextNAtom * kNumHeads * sizeof(float);
|
| 82 |
+
|
| 83 |
+
// Align to swizzling alignment bytes
|
| 84 |
+
extern __shared__ __align__(kSwizzleAlignment) uint8_t smem_buffer[];
|
| 85 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 86 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 87 |
+
|
| 88 |
+
// Q and KV data on shared memory
|
| 89 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 90 |
+
return smem_buffer + SMEM_Q_SIZE_PER_STAGE * i;
|
| 91 |
+
});
|
| 92 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 93 |
+
return smem_buffer + SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * i;
|
| 94 |
+
});
|
| 95 |
+
const auto smem_sf_ptr = smem_buffer + (SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * kNumKVStages);
|
| 96 |
+
auto smem_sf_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 97 |
+
return reinterpret_cast<uint32_t*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * i);
|
| 98 |
+
});
|
| 99 |
+
auto smem_sf_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 100 |
+
return reinterpret_cast<uint32_t*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * kNumQStages + SMEM_SF_KV_SIZE_PER_STAGE * i);
|
| 101 |
+
});
|
| 102 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 103 |
+
return reinterpret_cast<float*>(smem_sf_ptr + SMEM_SF_Q_SIZE_PER_STAGE * kNumQStages + SMEM_SF_KV_SIZE_PER_STAGE * kNumKVStages
|
| 104 |
+
+ SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
// Barriers and TMEM pointer on shared memory
|
| 108 |
+
const auto barrier_ptr = reinterpret_cast<Barrier*>(smem_weights[kNumQStages]);
|
| 109 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + i; });
|
| 110 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages + i; });
|
| 111 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + i; });
|
| 112 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + kNumKVStages + i; });
|
| 113 |
+
const auto tmem_barrier_ptr = barrier_ptr + kNumQStages * 2 + kNumKVStages * 2;
|
| 114 |
+
auto full_tmem_barriers = utils::PatternVisitor([&](const uint32_t& i) { return tmem_barrier_ptr + i; });
|
| 115 |
+
auto empty_tmem_barriers = utils::PatternVisitor([&](const uint32_t& i) { return tmem_barrier_ptr + kNumTmemStages + i; });
|
| 116 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(tmem_barrier_ptr + kNumTmemStages * 2);
|
| 117 |
+
|
| 118 |
+
// Tensor memory configs
|
| 119 |
+
constexpr uint32_t kNumAccumTmemCols = kNextNAtom * kNumHeads * kNumTmemStages;
|
| 120 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<kNumAccumTmemCols + kNumSFQAtom / 32 + kNumSFKV / 32>();
|
| 121 |
+
constexpr uint32_t kTmemStartColOfSFQ = kNumAccumTmemCols;
|
| 122 |
+
constexpr uint32_t kTmemStartColOfSFKV = kNumAccumTmemCols + kNumSFQAtom / 32;
|
| 123 |
+
DG_STATIC_ASSERT(kNumTmemCols <= 512, "Too many tensor memory");
|
| 124 |
+
|
| 125 |
+
// Initialize barriers
|
| 126 |
+
if (warp_idx == kSpecWarpStart and cute::elect_one_sync()) {
|
| 127 |
+
#pragma unroll
|
| 128 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 129 |
+
full_q_barriers[i]->init(1);
|
| 130 |
+
empty_q_barriers[i]->init(kNumMathThreads + 32);
|
| 131 |
+
}
|
| 132 |
+
cutlass::arch::fence_barrier_init();
|
| 133 |
+
}
|
| 134 |
+
if (warp_idx == kSpecWarpStart + 1 and cute::elect_one_sync()) {
|
| 135 |
+
#pragma unroll
|
| 136 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 137 |
+
full_kv_barriers[i]->init(1);
|
| 138 |
+
empty_kv_barriers[i]->init(1);
|
| 139 |
+
}
|
| 140 |
+
cutlass::arch::fence_barrier_init();
|
| 141 |
+
}
|
| 142 |
+
if (warp_idx == kSpecWarpStart + 2) {
|
| 143 |
+
if (cute::elect_one_sync()) {
|
| 144 |
+
#pragma unroll
|
| 145 |
+
for (uint32_t i = 0; i < kNumTmemStages; ++i) {
|
| 146 |
+
full_tmem_barriers[i]->init(1);
|
| 147 |
+
empty_tmem_barriers[i]->init(128);
|
| 148 |
+
}
|
| 149 |
+
cutlass::arch::fence_barrier_init();
|
| 150 |
+
}
|
| 151 |
+
// Allocate tensor memory
|
| 152 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 153 |
+
}
|
| 154 |
+
__syncthreads();
|
| 155 |
+
|
| 156 |
+
// Wait for primary kernel completion
|
| 157 |
+
cudaGridDependencySynchronize();
|
| 158 |
+
|
| 159 |
+
// Scheduler
|
| 160 |
+
constexpr uint32_t kNumBlocksPerSplit = SPLIT_KV / BLOCK_KV;
|
| 161 |
+
using Scheduler = sched::PagedMQALogitsScheduler<kNextN, kIsContextLens2D, kIsVarlen, BLOCK_KV, kNumBlocksPerSplit, kNumNextNAtoms>;
|
| 162 |
+
DG_STATIC_ASSERT(SPLIT_KV == BLOCK_KV * kNumBlocksPerSplit, "Invalid `SPLIT_KV`");
|
| 163 |
+
|
| 164 |
+
// Make Q, KV and TMEM pipeline
|
| 165 |
+
auto make_pipeline = [](const uint32_t& num_stages) {
|
| 166 |
+
// Return current stage and phase, and advance pipeline by steps
|
| 167 |
+
return [iter_idx = 0u, num_stages](const uint32_t& step = 1) mutable -> cute::tuple<uint32_t, uint32_t> {
|
| 168 |
+
uint32_t current_idx = iter_idx;
|
| 169 |
+
iter_idx += step;
|
| 170 |
+
return {current_idx % num_stages, (current_idx / num_stages) & 1};
|
| 171 |
+
};
|
| 172 |
+
};
|
| 173 |
+
auto advance_q_pipeline = make_pipeline(kNumQStages);
|
| 174 |
+
auto advance_kv_pipeline = make_pipeline(kNumKVStages);
|
| 175 |
+
auto advance_tmem_pipeline = make_pipeline(kNumTmemStages);
|
| 176 |
+
|
| 177 |
+
// Register reconfigurations
|
| 178 |
+
constexpr uint32_t kNumSpecializedRegisters = 56;
|
| 179 |
+
constexpr uint32_t kNumMathRegisters = 224;
|
| 180 |
+
|
| 181 |
+
if (warp_idx == kSpecWarpStart) {
|
| 182 |
+
// TMA warp for loading Q
|
| 183 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 184 |
+
|
| 185 |
+
if (cute::elect_one_sync()) {
|
| 186 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 187 |
+
|
| 188 |
+
// Persistently schedule over blocks
|
| 189 |
+
// Initialize outside valid range to indicate no previous task
|
| 190 |
+
uint32_t last_q_atom_idx = batch_size * kNumNextNAtoms;
|
| 191 |
+
uint32_t q_atom_idx, _, __;
|
| 192 |
+
while (scheduler.fetch_next_task(q_atom_idx, _, __)) {
|
| 193 |
+
// Issue TMA Q when (q_idx, atom_idx) changes
|
| 194 |
+
if (q_atom_idx != last_q_atom_idx) {
|
| 195 |
+
// Wait Q consumer release
|
| 196 |
+
CUTE_TIE_DECL(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 197 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 198 |
+
|
| 199 |
+
// Issue TMA Q
|
| 200 |
+
const auto q_token_idx = Scheduler::atom_to_token_idx(q_atom_idx);
|
| 201 |
+
cute::SM90_TMA_LOAD_2D::copy(&tensor_map_q, reinterpret_cast<uint64_t*>(full_q_barriers[q_stage_idx]),
|
| 202 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 203 |
+
smem_q[q_stage_idx], 0, q_token_idx * kNumHeads);
|
| 204 |
+
tma::copy<kNextNAtom * kNumHeads, 1, 0>(&tensor_map_sf_q, full_q_barriers[q_stage_idx], smem_sf_q[q_stage_idx], 0, q_token_idx);
|
| 205 |
+
tma::copy<kNumHeads, kNextNAtom, 0>(&tensor_map_weights, full_q_barriers[q_stage_idx], smem_weights[q_stage_idx], 0, q_token_idx);
|
| 206 |
+
full_q_barriers[q_stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + kRealNumSFQAtom * sizeof(int) + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 207 |
+
}
|
| 208 |
+
last_q_atom_idx = q_atom_idx;
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
__syncwarp();
|
| 212 |
+
} else if (warp_idx == kSpecWarpStart + 1) {
|
| 213 |
+
// TMA warp for loading KV cache
|
| 214 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 215 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 216 |
+
|
| 217 |
+
// Persistently schedule over blocks
|
| 218 |
+
uint32_t kv_block_idx_ptr = 32, kv_block_idx_storage;
|
| 219 |
+
uint32_t last_q_atom_idx = batch_size * kNumNextNAtoms;
|
| 220 |
+
uint32_t q_atom_idx, kv_idx, num_kv;
|
| 221 |
+
while (scheduler.fetch_next_task(q_atom_idx, kv_idx, num_kv)) {
|
| 222 |
+
// Reset block table cache on kv restart
|
| 223 |
+
if (q_atom_idx != last_q_atom_idx)
|
| 224 |
+
kv_block_idx_ptr = 32;
|
| 225 |
+
last_q_atom_idx = q_atom_idx;
|
| 226 |
+
|
| 227 |
+
// Coalesced load of block table
|
| 228 |
+
if (kv_block_idx_ptr == 32) {
|
| 229 |
+
kv_block_idx_ptr = 0;
|
| 230 |
+
const auto block_table_offset = Scheduler::atom_to_block_table_row(q_atom_idx) * static_cast<uint64_t>(block_table_stride);
|
| 231 |
+
kv_block_idx_storage = (kv_idx + lane_idx < num_kv)
|
| 232 |
+
? block_table[block_table_offset + kv_idx + lane_idx] : 0;
|
| 233 |
+
}
|
| 234 |
+
__syncwarp();
|
| 235 |
+
|
| 236 |
+
// Broadcast KV block indices
|
| 237 |
+
int kv_block_idx[kNumBlocksPerSplit];
|
| 238 |
+
#pragma unroll
|
| 239 |
+
for (int i = 0; i < kNumBlocksPerSplit; ++ i)
|
| 240 |
+
kv_block_idx[i] = __shfl_sync(0xffffffff, kv_block_idx_storage, kv_block_idx_ptr + i);
|
| 241 |
+
kv_block_idx_ptr += kNumBlocksPerSplit;
|
| 242 |
+
DG_STATIC_ASSERT(32 % kNumBlocksPerSplit == 0, "Invalid `SPLIT_KV`");
|
| 243 |
+
|
| 244 |
+
// Wait KV consumer release
|
| 245 |
+
CUTE_TIE_DECL(advance_kv_pipeline(), kv_stage_idx, kv_phase);
|
| 246 |
+
|
| 247 |
+
// Issue TMA KV
|
| 248 |
+
if (cute::elect_one_sync()) {
|
| 249 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 250 |
+
#pragma unroll
|
| 251 |
+
for (int i = 0; i < kNumBlocksPerSplit; ++ i) {
|
| 252 |
+
cute::SM90_TMA_LOAD_3D::copy(&tensor_map_kv, reinterpret_cast<uint64_t*>(full_kv_barriers[kv_stage_idx]),
|
| 253 |
+
static_cast<uint64_t>(cute::TMA::CacheHintSm100::EVICT_NORMAL),
|
| 254 |
+
smem_kv[kv_stage_idx] + (BLOCK_KV * kHeadDim / 2) * i,
|
| 255 |
+
0, 0, kv_block_idx[i]);
|
| 256 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_sf_kv, full_kv_barriers[kv_stage_idx],
|
| 257 |
+
smem_sf_kv[kv_stage_idx] + BLOCK_KV * i,
|
| 258 |
+
0, kv_block_idx[i]);
|
| 259 |
+
}
|
| 260 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_SF_KV_SIZE_PER_STAGE);
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
} else if (warp_idx == kSpecWarpStart + 2) {
|
| 264 |
+
// UMMA warp
|
| 265 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 266 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 267 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 268 |
+
|
| 269 |
+
// UTCCP transposer
|
| 270 |
+
auto utccp_required_smem_warp_transpose = [&](const uint32_t* smem_ptr) {
|
| 271 |
+
DG_STATIC_ASSERT(kNumUTCCPAlignedElems == 128, "Invalid aligned elements");
|
| 272 |
+
uint32_t values[4];
|
| 273 |
+
#pragma unroll
|
| 274 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 275 |
+
values[i] = ptx::ld_shared(smem_ptr + (i ^ (lane_idx >> 3)) * 32 + lane_idx);
|
| 276 |
+
__syncwarp();
|
| 277 |
+
#pragma unroll
|
| 278 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 279 |
+
ptx::st_shared(smem_ptr + lane_idx * 4 + (i ^ (lane_idx >> 3)), values[i]);
|
| 280 |
+
};
|
| 281 |
+
|
| 282 |
+
// Make UMMA desc
|
| 283 |
+
auto instr_desc = cute::UMMA::make_instr_desc_block_scaled<cutlass::float_e2m1_t, cutlass::float_e2m1_t, float, cutlass::float_ue8m0_t,
|
| 284 |
+
UMMA_M, UMMA_N, cute::UMMA::Major::K, cute::UMMA::Major::K>();
|
| 285 |
+
auto sf_desc = mma::sm100::make_sf_desc(nullptr);
|
| 286 |
+
|
| 287 |
+
// Persistently schedule over blocks
|
| 288 |
+
uint32_t last_q_atom_idx = batch_size * kNumNextNAtoms;
|
| 289 |
+
uint32_t q_atom_idx, kv_idx, _;
|
| 290 |
+
while (scheduler.fetch_next_task(q_atom_idx, kv_idx, _)) {
|
| 291 |
+
// Wait TMA Q arrivals
|
| 292 |
+
uint32_t q_stage_idx, q_phase;
|
| 293 |
+
if (q_atom_idx != last_q_atom_idx) {
|
| 294 |
+
CUTE_TIE(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 295 |
+
|
| 296 |
+
// Release previous Q empty (UMMA warp must participate to prevent
|
| 297 |
+
// running ahead of math warps in the Q pipeline)
|
| 298 |
+
if (last_q_atom_idx != batch_size * kNumNextNAtoms)
|
| 299 |
+
empty_q_barriers[(q_stage_idx + kNumQStages - 1) % kNumQStages]->arrive();
|
| 300 |
+
|
| 301 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 302 |
+
|
| 303 |
+
// Transpose and copy SF Q
|
| 304 |
+
#pragma unroll
|
| 305 |
+
for (uint32_t i = 0; i < kNumSFQAtom / kNumUTCCPAlignedElems; ++ i) {
|
| 306 |
+
auto smem_ptr = smem_sf_q[q_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 307 |
+
utccp_required_smem_warp_transpose(smem_ptr);
|
| 308 |
+
cutlass::arch::fence_view_async_shared();
|
| 309 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 310 |
+
if (cute::elect_one_sync())
|
| 311 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta::copy(sf_desc, kTmemStartColOfSFQ + i * 4);
|
| 312 |
+
__syncwarp();
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
last_q_atom_idx = q_atom_idx;
|
| 316 |
+
|
| 317 |
+
// Wait TMA KV arrivals
|
| 318 |
+
CUTE_TIE_DECL(advance_kv_pipeline(), kv_stage_idx, kv_phase);
|
| 319 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 320 |
+
|
| 321 |
+
// Transpose
|
| 322 |
+
#pragma unroll
|
| 323 |
+
for (uint32_t i = 0; i < kNumSFKV / kNumUTCCPAlignedElems; ++ i) {
|
| 324 |
+
auto smem_ptr = smem_sf_kv[kv_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 325 |
+
utccp_required_smem_warp_transpose(smem_ptr);
|
| 326 |
+
cutlass::arch::fence_view_async_shared();
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
// UMMA with SF
|
| 330 |
+
if (cute::elect_one_sync()) {
|
| 331 |
+
// Copy SF KV
|
| 332 |
+
#pragma unroll
|
| 333 |
+
for (uint32_t i = 0; i < kNumSFKV / kNumUTCCPAlignedElems; ++ i) {
|
| 334 |
+
auto smem_ptr = smem_sf_kv[kv_stage_idx] + i * kNumUTCCPAlignedElems;
|
| 335 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 336 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta::copy(sf_desc, kTmemStartColOfSFKV + i * 4);
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
#pragma unroll
|
| 340 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++ i) {
|
| 341 |
+
// Wait TMEM release
|
| 342 |
+
CUTE_TIE_DECL(advance_tmem_pipeline(), tmem_stage_idx, tmem_phase);
|
| 343 |
+
uint32_t tmem_addr = tmem_stage_idx * UMMA_N;
|
| 344 |
+
|
| 345 |
+
empty_tmem_barriers[tmem_stage_idx]->wait(tmem_phase ^ 1);
|
| 346 |
+
ptx::tcgen05_after_thread_sync();
|
| 347 |
+
|
| 348 |
+
// Issue UMMA with SF
|
| 349 |
+
#pragma unroll
|
| 350 |
+
for (uint32_t k = 0; k < kHeadDim / UMMA_K; ++ k) {
|
| 351 |
+
auto runtime_instr_desc = mma::sm100::make_runtime_instr_desc_with_sf_id(instr_desc, k * 2, k * 2);
|
| 352 |
+
// TODO: generalize UMMA desc
|
| 353 |
+
DG_STATIC_ASSERT(kHeadDim == 128, "Invalid head dim");
|
| 354 |
+
auto a_desc = mma::sm100::make_smem_desc(
|
| 355 |
+
cute::UMMA::LayoutType::SWIZZLE_64B,
|
| 356 |
+
smem_kv[kv_stage_idx] + i * UMMA_M * (kHeadDim / 2) + k * UMMA_K / 2,
|
| 357 |
+
8 * (kHeadDim / 2), 0);
|
| 358 |
+
auto b_desc = mma::sm100::make_smem_desc(
|
| 359 |
+
cute::UMMA::LayoutType::SWIZZLE_64B,
|
| 360 |
+
smem_q[q_stage_idx] + k * UMMA_K / 2,
|
| 361 |
+
8 * (kHeadDim / 2), 0);
|
| 362 |
+
ptx::SM100_MMA_MXF4_SS::fma(a_desc, b_desc, tmem_addr, k, runtime_instr_desc,
|
| 363 |
+
kTmemStartColOfSFKV + i * 4, kTmemStartColOfSFQ);
|
| 364 |
+
}
|
| 365 |
+
// TODO: move this PTX into headers
|
| 366 |
+
asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
|
| 367 |
+
::"r"(cute::cast_smem_ptr_to_uint(full_tmem_barriers[tmem_stage_idx])));
|
| 368 |
+
}
|
| 369 |
+
}
|
| 370 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(empty_kv_barriers[kv_stage_idx]));
|
| 371 |
+
}
|
| 372 |
+
} else if (warp_idx == kSpecWarpStart + 3) {
|
| 373 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 374 |
+
} else if (warp_idx < kSpecWarpStart) {
|
| 375 |
+
// Math warpgroups for reduce
|
| 376 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 377 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 378 |
+
|
| 379 |
+
const auto math_warpgroup_idx = warpgroup_idx;
|
| 380 |
+
const auto math_thread_idx = warp_idx * 32 + lane_idx;
|
| 381 |
+
|
| 382 |
+
// Helper lambda for loading tensor memory
|
| 383 |
+
auto tmem_load = [](auto num_elems_c, const uint32_t& tmem_addr, float* accum) {
|
| 384 |
+
constexpr int N = decltype(num_elems_c)::value;
|
| 385 |
+
DG_STATIC_ASSERT(N == 32 or N == 64, "Unsupported TMEM load size");
|
| 386 |
+
using Loader = cute::conditional_t<N == 32,
|
| 387 |
+
cute::SM100_TMEM_LOAD_32dp32b32x,
|
| 388 |
+
cute::SM100_TMEM_LOAD_32dp32b64x>;
|
| 389 |
+
[&]<size_t... Is>(cute::index_sequence<Is...>) {
|
| 390 |
+
Loader::copy(tmem_addr, reinterpret_cast<uint32_t*>(accum)[Is]...);
|
| 391 |
+
}(cute::make_index_sequence<N>{});
|
| 392 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 393 |
+
};
|
| 394 |
+
|
| 395 |
+
// Math warpgroups process TMEM stages alternately
|
| 396 |
+
// Advance pipeline to align with the assigned stage
|
| 397 |
+
advance_tmem_pipeline(math_warpgroup_idx);
|
| 398 |
+
|
| 399 |
+
// Local register buffers
|
| 400 |
+
float accum[kNumHeads];
|
| 401 |
+
float weights[kNextNAtom][kNumHeads];
|
| 402 |
+
|
| 403 |
+
// Persistently schedule over blocks
|
| 404 |
+
uint32_t last_q_atom_idx = batch_size * kNumNextNAtoms;
|
| 405 |
+
uint32_t q_atom_idx, kv_idx, _;
|
| 406 |
+
bool is_paired_atom = false;
|
| 407 |
+
while (scheduler.fetch_next_task(q_atom_idx, kv_idx, _)) {
|
| 408 |
+
if (q_atom_idx != last_q_atom_idx) {
|
| 409 |
+
CUTE_TIE_DECL(advance_q_pipeline(), q_stage_idx, q_phase);
|
| 410 |
+
|
| 411 |
+
// Release last Q empty
|
| 412 |
+
if (last_q_atom_idx != batch_size * kNumNextNAtoms)
|
| 413 |
+
empty_q_barriers[(q_stage_idx + kNumQStages - 1) % kNumQStages]->arrive();
|
| 414 |
+
|
| 415 |
+
// Wait TMA Q arrivals
|
| 416 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 417 |
+
|
| 418 |
+
// Read weights
|
| 419 |
+
#pragma unroll
|
| 420 |
+
for (uint32_t i = 0; i < kNextNAtom; ++ i) {
|
| 421 |
+
#pragma unroll
|
| 422 |
+
for (uint32_t j = 0; j < kNumHeads; j += 4) {
|
| 423 |
+
float4 raw = ptx::ld_shared((float4*)(smem_weights[q_stage_idx] + i * kNumHeads + j));
|
| 424 |
+
weights[i][j + 0] = raw.x;
|
| 425 |
+
weights[i][j + 1] = raw.y;
|
| 426 |
+
weights[i][j + 2] = raw.z;
|
| 427 |
+
weights[i][j + 3] = raw.w;
|
| 428 |
+
}
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
// Check if this atom pairs two tokens from the same sequence
|
| 432 |
+
if constexpr (kIsVarlen) {
|
| 433 |
+
is_paired_atom = (scheduler.get_atom_advance(q_atom_idx, batch_size) == 2);
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
last_q_atom_idx = q_atom_idx;
|
| 437 |
+
|
| 438 |
+
// Calculate KV offset in advance
|
| 439 |
+
auto kv_offset = Scheduler::atom_to_token_idx(q_atom_idx) * static_cast<uint64_t>(logits_stride) + kv_idx * BLOCK_KV + math_thread_idx;
|
| 440 |
+
|
| 441 |
+
// Advance pipeline by `kNumMathWarpGroups` steps
|
| 442 |
+
// Wait UMMA arrival
|
| 443 |
+
CUTE_TIE_DECL(advance_tmem_pipeline(kNumMathWarpGroups), tmem_stage_idx, tmem_phase);
|
| 444 |
+
full_tmem_barriers[tmem_stage_idx]->wait(tmem_phase);
|
| 445 |
+
ptx::tcgen05_after_thread_sync();
|
| 446 |
+
|
| 447 |
+
// Reduce over the head dim and store
|
| 448 |
+
const auto reduce_and_store = [&](auto num_iters_c) {
|
| 449 |
+
constexpr uint32_t kNumIters = decltype(num_iters_c)::value;
|
| 450 |
+
|
| 451 |
+
// Only loop over valid iterations
|
| 452 |
+
#pragma unroll
|
| 453 |
+
for (uint32_t i = 0; i < kNumIters; ++ i) {
|
| 454 |
+
// Load accumulator from TMEM
|
| 455 |
+
uint32_t tmem_addr = tmem_stage_idx * UMMA_N + i * kNumHeads;
|
| 456 |
+
tmem_load(cute::Int<kNumHeads / 2>{}, tmem_addr, accum);
|
| 457 |
+
tmem_load(cute::Int<kNumHeads / 2>{}, tmem_addr + kNumHeads / 2, accum + kNumHeads / 2);
|
| 458 |
+
|
| 459 |
+
// Accumulate weighted ReLU in parallel
|
| 460 |
+
auto sum_0 = make_float2(0, 0);
|
| 461 |
+
auto sum_1 = make_float2(0, 0);
|
| 462 |
+
|
| 463 |
+
const auto transform = [&](const uint32_t& j, const float2& sum) {
|
| 464 |
+
auto a = make_float2(fmaxf(accum[j], 0), fmaxf(accum[j + 1], 0));
|
| 465 |
+
auto b = make_float2(weights[i][j], weights[i][j + 1]);
|
| 466 |
+
return __ffma2_rn(a, b, sum);
|
| 467 |
+
};
|
| 468 |
+
|
| 469 |
+
#pragma unroll
|
| 470 |
+
for (uint32_t j = 0; j < kNumHeads; j += 4) {
|
| 471 |
+
sum_0 = transform(j, sum_0);
|
| 472 |
+
sum_1 = transform(j + 2, sum_1);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
auto sum = __fadd2_rn(sum_0, sum_1);
|
| 476 |
+
auto result = static_cast<logits_dtype_t>(sum.x + sum.y);
|
| 477 |
+
|
| 478 |
+
// Store into the global memory
|
| 479 |
+
logits[kv_offset + i * static_cast<uint64_t>(logits_stride)] = result;
|
| 480 |
+
__syncwarp();
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
// Release TMEM empty
|
| 484 |
+
ptx::tcgen05_before_thread_sync();
|
| 485 |
+
empty_tmem_barriers[tmem_stage_idx]->arrive();
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
if constexpr (kIsVarlen) {
|
| 489 |
+
if (is_paired_atom)
|
| 490 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 491 |
+
else
|
| 492 |
+
reduce_and_store(cute::Int<1>{});
|
| 493 |
+
} else if constexpr (kPadOddN) {
|
| 494 |
+
if (q_atom_idx % kNumNextNAtoms == kNumNextNAtoms - 1)
|
| 495 |
+
reduce_and_store(cute::Int<1>{});
|
| 496 |
+
else
|
| 497 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 498 |
+
} else {
|
| 499 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 500 |
+
}
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// Free tensor memory
|
| 504 |
+
cutlass::arch::NamedBarrier(kNumMathThreads, 0).sync();
|
| 505 |
+
if (warp_idx == 0)
|
| 506 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
|
| 507 |
+
}
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#pragma clang diagnostic push
|
| 3 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 4 |
+
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/math.cuh>
|
| 8 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 9 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 10 |
+
#include <deep_gemm/epilogue/sm100_store_cd.cuh>
|
| 11 |
+
#include <deep_gemm/epilogue/sm100_store_cd_swap_ab.cuh>
|
| 12 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 13 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 15 |
+
|
| 16 |
+
namespace deep_gemm {
|
| 17 |
+
|
| 18 |
+
template <cute::UMMA::Major kMajorA, cute::UMMA::Major kMajorB,
|
| 19 |
+
uint32_t kGranKA, uint32_t kGranKB,
|
| 20 |
+
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 21 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 22 |
+
uint32_t kNumGroups,
|
| 23 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleCDMode,
|
| 24 |
+
uint32_t kNumStages,
|
| 25 |
+
uint32_t kNumNonEpilogueThreads, uint32_t kNumEpilogueThreads,
|
| 26 |
+
uint32_t kNumMulticast, bool kIsMulticastOnA,
|
| 27 |
+
uint32_t kNumSMs,
|
| 28 |
+
bool kSwapAB,
|
| 29 |
+
GemmType kGemmType, bool kWithAccumulation,
|
| 30 |
+
typename a_dtype_t, typename b_dtype_t, typename cd_dtype_t,
|
| 31 |
+
typename epilogue_type_t>
|
| 32 |
+
CUTLASS_GLOBAL void __launch_bounds__(kNumNonEpilogueThreads + kNumEpilogueThreads, 1)
|
| 33 |
+
sm100_fp8_fp4_gemm_1d1d_impl(int* grouped_layout,
|
| 34 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfa,
|
| 38 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfb,
|
| 39 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
| 40 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 41 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 42 |
+
using Allocator = cute::conditional_t<kNumMulticast == 1, cute::TMEM::Allocator1Sm, cute::TMEM::Allocator2Sm>;
|
| 43 |
+
|
| 44 |
+
// GEMM with accumulation must have FP32 output
|
| 45 |
+
if constexpr (kWithAccumulation)
|
| 46 |
+
DG_STATIC_ASSERT(cute::is_same_v<cd_dtype_t, float>, "Invalid C/D data dtype");
|
| 47 |
+
|
| 48 |
+
// MMA Configs
|
| 49 |
+
constexpr uint32_t LAYOUT_AD_M = 128;
|
| 50 |
+
constexpr uint32_t UMMA_M = LAYOUT_AD_M * kNumMulticast;
|
| 51 |
+
constexpr uint32_t UMMA_N = kSwapAB ? BLOCK_M : BLOCK_N;
|
| 52 |
+
constexpr uint32_t UMMA_K = 32;
|
| 53 |
+
constexpr uint32_t LOAD_BLOCK_M = BLOCK_M / (kIsMulticastOnA ? kNumMulticast: 1);
|
| 54 |
+
constexpr uint32_t LOAD_BLOCK_N = BLOCK_N / (kIsMulticastOnA ? 1 : kNumMulticast);
|
| 55 |
+
DG_STATIC_ASSERT(BLOCK_K == 128, "Invalid block K");
|
| 56 |
+
DG_STATIC_ASSERT(kNumMulticast == 1 or kNumMulticast == 2, "Only support 1/2 multicast");
|
| 57 |
+
DG_STATIC_ASSERT((kSwapAB and BLOCK_N == LAYOUT_AD_M) or
|
| 58 |
+
(not kSwapAB and (BLOCK_M == 32 or BLOCK_M == 64 or BLOCK_M == LAYOUT_AD_M)), "Invalid block size");
|
| 59 |
+
|
| 60 |
+
// SF configs
|
| 61 |
+
constexpr uint32_t kNumUTCCPAlignedElems = 128;
|
| 62 |
+
constexpr uint32_t SF_BLOCK_M = math::constexpr_align(BLOCK_M, kNumUTCCPAlignedElems);
|
| 63 |
+
constexpr uint32_t SF_BLOCK_N = math::constexpr_align(BLOCK_N, kNumUTCCPAlignedElems);
|
| 64 |
+
constexpr uint32_t kNumSFAStagesPerLoad = kGranKA == 32 ? 1 : 4;
|
| 65 |
+
constexpr uint32_t kNumSFBStagesPerLoad = kGranKB == 32 ? 1 : 4;
|
| 66 |
+
DG_STATIC_ASSERT(kGranKA == 32 or kGranKA == 128, "Invalid granularity K for A");
|
| 67 |
+
DG_STATIC_ASSERT(kGranKB == 32 or kGranKB == 128, "Invalid granularity K for B");
|
| 68 |
+
DG_STATIC_ASSERT((kGemmType != GemmType::KGroupedContiguous) or kGranKA == kGranKB, "K-grouped SF requires kGranKA == kGranKB");
|
| 69 |
+
|
| 70 |
+
// Epilogue configs
|
| 71 |
+
// Always enable pipeline for better performance
|
| 72 |
+
constexpr uint32_t kNumEpilogueStages = 2;
|
| 73 |
+
constexpr uint32_t kNumTMAStoreStages = 2;
|
| 74 |
+
// NOTES: To maximize epilogue threads utilization, process an entire BLOCK_N
|
| 75 |
+
// per store stage for swap-AB cases, and an entire BLOCK_M for non-swap cases
|
| 76 |
+
constexpr uint32_t STORE_BLOCK_M = kSwapAB ? 16 : cute::min<uint32_t>(BLOCK_M, LAYOUT_AD_M);
|
| 77 |
+
constexpr uint32_t STORE_BLOCK_N = kSwapAB ? BLOCK_N : kSwizzleCDMode / sizeof(cd_dtype_t);
|
| 78 |
+
constexpr uint32_t kNumUMMAStoreThreads = kSwapAB ? kNumEpilogueThreads: STORE_BLOCK_M;
|
| 79 |
+
DG_STATIC_ASSERT(kNumUMMAStoreThreads % 32 == 0, "Invalid store block M");
|
| 80 |
+
|
| 81 |
+
// Share memory sizes
|
| 82 |
+
constexpr uint32_t SMEM_CD_SIZE_PER_STAGE = STORE_BLOCK_M * STORE_BLOCK_N * sizeof(cd_dtype_t);
|
| 83 |
+
constexpr uint32_t SMEM_CD_SIZE = SMEM_CD_SIZE_PER_STAGE * kNumTMAStoreStages;
|
| 84 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = LOAD_BLOCK_M * BLOCK_K * sizeof(a_dtype_t);
|
| 85 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = LOAD_BLOCK_N * BLOCK_K * sizeof(b_dtype_t);
|
| 86 |
+
constexpr uint32_t SMEM_SFA_SIZE_PER_STAGE = SF_BLOCK_M * sizeof(uint32_t);
|
| 87 |
+
constexpr uint32_t SMEM_SFB_SIZE_PER_STAGE = SF_BLOCK_N * sizeof(uint32_t);
|
| 88 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % 1024 == 0 and SMEM_A_SIZE_PER_STAGE % 1024 == 0 and SMEM_B_SIZE_PER_STAGE % 1024 == 0,
|
| 89 |
+
"Shared memory of A/B must be aligned to 1024 bytes");
|
| 90 |
+
// NOTES: Make sure we have enough shared memory for UMMA padding
|
| 91 |
+
constexpr uint32_t UMMA_A_SIZE_PER_STAGE = math::constexpr_align(LOAD_BLOCK_M, LAYOUT_AD_M) * BLOCK_K * sizeof(a_dtype_t);
|
| 92 |
+
DG_STATIC_ASSERT(UMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory Out of bound for UMMA");
|
| 93 |
+
|
| 94 |
+
// Tensor memory size and offsets
|
| 95 |
+
constexpr uint32_t kNumAccumTmemCols = UMMA_N * kNumEpilogueStages;
|
| 96 |
+
constexpr uint32_t kNumSFATmemCols = SF_BLOCK_M / 32;
|
| 97 |
+
constexpr uint32_t kNumSFBTmemCols = SF_BLOCK_N / 32;
|
| 98 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<kNumAccumTmemCols + kNumSFATmemCols + kNumSFBTmemCols>();
|
| 99 |
+
constexpr uint32_t kTmemStartColOfSFA = kNumAccumTmemCols;
|
| 100 |
+
constexpr uint32_t kTmemStartColOfSFB = kNumAccumTmemCols + kNumSFATmemCols;
|
| 101 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 102 |
+
|
| 103 |
+
// Synchronize the cluster before 2-CTA TMEM allocation
|
| 104 |
+
kNumMulticast > 1 ? cute::cluster_sync() : void();
|
| 105 |
+
|
| 106 |
+
// Utils
|
| 107 |
+
const bool is_leader_cta = cute::block_rank_in_cluster() == 0;
|
| 108 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 109 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 110 |
+
|
| 111 |
+
// Prefetch TMA descriptors at the very beginning
|
| 112 |
+
if (warp_idx == 0) {
|
| 113 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 114 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 115 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfa);
|
| 116 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfb);
|
| 117 |
+
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Overwrite shape constants if the compiler gives
|
| 121 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 122 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 123 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 124 |
+
const auto shape_sfa_k = math::ceil_div(shape_k, kGranKA * 4);
|
| 125 |
+
const auto shape_sfb_k = math::ceil_div(shape_k, kGranKB * 4);
|
| 126 |
+
|
| 127 |
+
// Align to 1024 bytes for swizzle-128B
|
| 128 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 129 |
+
|
| 130 |
+
// D/A/B shared memory
|
| 131 |
+
auto smem_cd = utils::PatternVisitor([&](const uint32_t& i) {
|
| 132 |
+
return reinterpret_cast<cd_dtype_t*>(smem_buffer + i * SMEM_CD_SIZE_PER_STAGE);
|
| 133 |
+
});
|
| 134 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 135 |
+
return reinterpret_cast<a_dtype_t*>(smem_buffer + SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 136 |
+
});
|
| 137 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 138 |
+
return reinterpret_cast<b_dtype_t*>(smem_buffer + SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
// SFA/SFB shared memory
|
| 142 |
+
auto sf_start_ptr = reinterpret_cast<uint8_t*>(smem_b[kNumStages]);
|
| 143 |
+
auto smem_sfa = utils::PatternVisitor([=](const uint32_t& i) {
|
| 144 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + i * SMEM_SFA_SIZE_PER_STAGE);
|
| 145 |
+
});
|
| 146 |
+
auto smem_sfb = utils::PatternVisitor([=](const uint32_t& i) {
|
| 147 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + kNumStages * SMEM_SFA_SIZE_PER_STAGE + i * SMEM_SFB_SIZE_PER_STAGE);
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
+
// Barriers and tensor memory pointer
|
| 151 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_sfb[kNumStages]);;
|
| 152 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 153 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 154 |
+
auto with_sf_full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 2 + i); });
|
| 155 |
+
auto tmem_full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 3 + i); });
|
| 156 |
+
auto tmem_empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 3 + kNumEpilogueStages + i); });
|
| 157 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumStages * 3 + kNumEpilogueStages * 2);
|
| 158 |
+
|
| 159 |
+
// Initialize barriers
|
| 160 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 161 |
+
#pragma unroll
|
| 162 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 163 |
+
// Arrive at all CTAs
|
| 164 |
+
full_barriers[i]->init(1);
|
| 165 |
+
empty_barriers[i]->init(1);
|
| 166 |
+
// Arrive only at the leader CTA
|
| 167 |
+
with_sf_full_barriers[i]->init(kNumMulticast * 32);
|
| 168 |
+
}
|
| 169 |
+
#pragma unroll
|
| 170 |
+
for (uint32_t i = 0; i < kNumEpilogueStages; ++ i) {
|
| 171 |
+
// Arrive at all CTAs
|
| 172 |
+
tmem_full_barriers[i]->init(1);
|
| 173 |
+
// Arrive only at the leader CTA
|
| 174 |
+
tmem_empty_barriers[i]->init(kNumMulticast * kNumUMMAStoreThreads);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// Make initialized barrier visible in async proxy
|
| 178 |
+
cutlass::arch::fence_barrier_init();
|
| 179 |
+
} else if (warp_idx == 2) {
|
| 180 |
+
// Allocate tensor memory
|
| 181 |
+
Allocator().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 182 |
+
}
|
| 183 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 184 |
+
|
| 185 |
+
// Wait for primary kernel completion
|
| 186 |
+
cudaGridDependencySynchronize();
|
| 187 |
+
|
| 188 |
+
// Block scheduler
|
| 189 |
+
uint32_t m_block_idx, n_block_idx;
|
| 190 |
+
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumMulticast, kIsMulticastOnA, kNumSMs, kGranKA * 4>(
|
| 191 |
+
shape_m, shape_n, shape_k, grouped_layout);
|
| 192 |
+
|
| 193 |
+
// Pipeline and TMA phases
|
| 194 |
+
uint32_t stage_idx = 0, phase = 0;
|
| 195 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 196 |
+
++ k_block_idx;
|
| 197 |
+
|
| 198 |
+
// Flip phases only if reach the next first stage
|
| 199 |
+
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
| 200 |
+
phase ^= stage_idx == 0;
|
| 201 |
+
};
|
| 202 |
+
|
| 203 |
+
// Dispatch warps into different roles
|
| 204 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 205 |
+
// TMA load warp
|
| 206 |
+
// Persistently schedule over blocks
|
| 207 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 208 |
+
// Use dynamic load block M, when swap-AB is enabled
|
| 209 |
+
const auto load_block_m = kSwapAB ? scheduler.get_aligned_effective_m_in_block(m_block_idx) / kNumMulticast : LOAD_BLOCK_M;
|
| 210 |
+
|
| 211 |
+
// For k-grouped layout, the number of block K is variable
|
| 212 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 213 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 214 |
+
// Wait consumer release
|
| 215 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 216 |
+
|
| 217 |
+
// Compute offsets
|
| 218 |
+
// NOTES: the group is always concatenated with the outer dimension
|
| 219 |
+
uint32_t m_idx = scheduler.template get_global_idx<(kGemmType == GemmType::MGroupedMasked), sched::IndexType::MN> (
|
| 220 |
+
shape_m, BLOCK_M, m_block_idx);
|
| 221 |
+
uint32_t n_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::K), sched::IndexType::MN> (
|
| 222 |
+
shape_n, BLOCK_N, n_block_idx, m_block_idx);
|
| 223 |
+
|
| 224 |
+
// NOTES: `k_idx` is actually the k index default for K-major, while `k_b_idx` may be MN-major
|
| 225 |
+
// And for all m-grouped GEMMs, A must be K-majored
|
| 226 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous or kGemmType == GemmType::Batched or
|
| 227 |
+
kMajorA == cute::UMMA::Major::K, "Invalid major");
|
| 228 |
+
uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 229 |
+
uint32_t k_a_idx = scheduler.template get_global_idx<(kMajorA == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 230 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 231 |
+
uint32_t k_b_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 232 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 233 |
+
|
| 234 |
+
// Add 2 CTA offsets
|
| 235 |
+
if constexpr (kNumMulticast > 1) {
|
| 236 |
+
m_idx += kIsMulticastOnA ? (cute::block_rank_in_cluster() * load_block_m) : 0;
|
| 237 |
+
n_idx += kIsMulticastOnA ? 0 : (cute::block_rank_in_cluster() * LOAD_BLOCK_N);
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
// Issue TMAs
|
| 241 |
+
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
| 242 |
+
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
| 243 |
+
if constexpr (kMajorA == cute::UMMA::Major::K)
|
| 244 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t, kIsBatchedMM>(
|
| 245 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_a_idx, m_idx, 1, batch_idx);
|
| 246 |
+
if constexpr (kMajorA == cute::UMMA::Major::MN)
|
| 247 |
+
tma::copy<LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode, a_dtype_t, kIsBatchedMM>(
|
| 248 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], m_idx, k_a_idx, 1, batch_idx);
|
| 249 |
+
if constexpr (kMajorB == cute::UMMA::Major::K)
|
| 250 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t, kIsBatchedMM>(
|
| 251 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_b_idx, n_idx, 1, batch_idx);
|
| 252 |
+
if constexpr (kMajorB == cute::UMMA::Major::MN)
|
| 253 |
+
tma::copy<LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode, b_dtype_t, kIsBatchedMM>(
|
| 254 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], n_idx, k_b_idx, 1, batch_idx);
|
| 255 |
+
auto num_arrival_bytes = SMEM_A_SIZE_PER_STAGE / (std::is_same_v<a_dtype_t, cutlass::float_e4m3_t> ? 1 : 2) +
|
| 256 |
+
SMEM_B_SIZE_PER_STAGE / (std::is_same_v<b_dtype_t, cutlass::float_e4m3_t> ? 1 : 2);
|
| 257 |
+
|
| 258 |
+
// Issue SFA and SFB TMAs at certain stages
|
| 259 |
+
// No swizzling, so one TMA for one SF is enough
|
| 260 |
+
if (k_block_idx % kNumSFAStagesPerLoad == 0) {
|
| 261 |
+
uint32_t sfa_m_idx = m_block_idx * BLOCK_M;
|
| 262 |
+
uint32_t sfa_k_idx = scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), sched::IndexType::SF_K>(
|
| 263 |
+
shape_sfa_k, 1, math::ceil_div(k_idx, BLOCK_K * kNumSFAStagesPerLoad));
|
| 264 |
+
tma::copy<BLOCK_M, 1, 0>(&tensor_map_sfa, full_barriers[stage_idx], smem_sfa[stage_idx], sfa_m_idx, sfa_k_idx);
|
| 265 |
+
num_arrival_bytes += BLOCK_M * sizeof(uint32_t);
|
| 266 |
+
}
|
| 267 |
+
if (k_block_idx % kNumSFBStagesPerLoad == 0) {
|
| 268 |
+
uint32_t sfb_n_idx = n_block_idx * BLOCK_N;
|
| 269 |
+
uint32_t sfb_k_idx = scheduler.template get_global_idx<true, sched::IndexType::SF_K>(
|
| 270 |
+
shape_sfb_k, 1, math::ceil_div(k_idx, BLOCK_K * kNumSFBStagesPerLoad), m_block_idx);
|
| 271 |
+
tma::copy<BLOCK_N, 1, 0>(&tensor_map_sfb, full_barriers[stage_idx], smem_sfb[stage_idx], sfb_n_idx, sfb_k_idx);
|
| 272 |
+
num_arrival_bytes += BLOCK_N * sizeof(uint32_t);
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
// Arrive at full barriers
|
| 276 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(num_arrival_bytes);
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
} else if (warp_idx == 1 and is_leader_cta) {
|
| 280 |
+
// MMA issue warp
|
| 281 |
+
// NOTES: only the leader CTA will do this
|
| 282 |
+
// Make instruction descriptor
|
| 283 |
+
auto instr_desc = kSwapAB ? cute::UMMA::make_instr_desc_block_scaled<b_dtype_t, a_dtype_t, float, cutlass::float_ue8m0_t,
|
| 284 |
+
UMMA_M, UMMA_N, kMajorB, kMajorA>()
|
| 285 |
+
: cute::UMMA::make_instr_desc_block_scaled<a_dtype_t, b_dtype_t, float, cutlass::float_ue8m0_t,
|
| 286 |
+
UMMA_M, UMMA_N, kMajorA, kMajorB>();
|
| 287 |
+
auto sf_desc = mma::sm100::make_sf_desc(nullptr);
|
| 288 |
+
|
| 289 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 290 |
+
auto a_desc = mma::sm100::make_umma_desc<kMajorA, LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode>(smem_a[0], 0, 0);
|
| 291 |
+
auto b_desc = mma::sm100::make_umma_desc<kMajorB, LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 292 |
+
uint32_t a_desc_lo = lane_idx < kNumStages ? a_desc.lo + lane_idx * SMEM_A_SIZE_PER_STAGE / 16 : 0u;
|
| 293 |
+
uint32_t b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 294 |
+
|
| 295 |
+
// Checks for MMA instructions
|
| 296 |
+
// NOTES: CUTLASS does not have such checks except the MMA traits, but we are not using these traits
|
| 297 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 298 |
+
(UMMA_M == 128 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256) or
|
| 299 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 300 |
+
"Invalid MMA instruction shape");
|
| 301 |
+
|
| 302 |
+
// Persistently schedule over blocks
|
| 303 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 304 |
+
// Wait tensor memory empty barrier arrival
|
| 305 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 306 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 307 |
+
tmem_empty_barriers[accum_stage_idx]->wait(accum_phase_idx ^ 1);
|
| 308 |
+
ptx::tcgen05_after_thread_sync();
|
| 309 |
+
|
| 310 |
+
// Empty barrier arrival
|
| 311 |
+
auto empty_barrier_arrive = [&](const bool& do_tmem_full_arrive) {
|
| 312 |
+
auto umma_arrive = [](const uint64_t* barrier) {
|
| 313 |
+
if constexpr (kNumMulticast == 1) {
|
| 314 |
+
cutlass::arch::umma_arrive(barrier);
|
| 315 |
+
} else {
|
| 316 |
+
constexpr uint16_t kCTAMask = (1 << kNumMulticast) - 1;
|
| 317 |
+
cutlass::arch::umma_arrive_multicast_2x1SM(barrier, kCTAMask);
|
| 318 |
+
}
|
| 319 |
+
};
|
| 320 |
+
umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 321 |
+
|
| 322 |
+
// NOTES: the tensor memory accumulator pipeline has nothing to do with multicasting
|
| 323 |
+
if (do_tmem_full_arrive)
|
| 324 |
+
umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barriers[accum_stage_idx]));
|
| 325 |
+
__syncwarp();
|
| 326 |
+
};
|
| 327 |
+
|
| 328 |
+
// Dynamic update of UMMA N based on effective M, when swap-AB is enabled
|
| 329 |
+
if constexpr (kSwapAB) {
|
| 330 |
+
uint32_t umma_n = scheduler.get_aligned_effective_m_in_block(m_block_idx);
|
| 331 |
+
mma::sm100::update_instr_desc_with_umma_n(instr_desc, umma_n);
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
// Launch MMAs
|
| 335 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 336 |
+
#pragma unroll 4
|
| 337 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 338 |
+
// Wait TMA and SF-transpose arrival
|
| 339 |
+
with_sf_full_barriers[stage_idx]->wait(phase);
|
| 340 |
+
ptx::tcgen05_after_thread_sync();
|
| 341 |
+
|
| 342 |
+
const auto a_desc_base_lo = ptx::exchange(a_desc_lo, stage_idx);
|
| 343 |
+
const auto b_desc_base_lo = ptx::exchange(b_desc_lo, stage_idx);
|
| 344 |
+
if (cute::elect_one_sync()) {
|
| 345 |
+
// Do SF copy at certain stages
|
| 346 |
+
// TODO: process shared memory descriptor by addition
|
| 347 |
+
using cute_utccp_t = cute::conditional_t<kNumMulticast == 1,
|
| 348 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta, cute::SM100_UTCCP_4x32dp128bit_2cta>;
|
| 349 |
+
const uint32_t sfa_stage_in_group_idx = k_block_idx % kNumSFAStagesPerLoad;
|
| 350 |
+
if (sfa_stage_in_group_idx == 0) {
|
| 351 |
+
#pragma unroll
|
| 352 |
+
for (uint32_t i = 0; i < SF_BLOCK_M / kNumUTCCPAlignedElems; ++ i) {
|
| 353 |
+
auto smem_ptr = smem_sfa[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 354 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 355 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFA + i * 4);
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
const uint32_t sfb_stage_in_group_idx = k_block_idx % kNumSFBStagesPerLoad;
|
| 359 |
+
if (sfb_stage_in_group_idx == 0) {
|
| 360 |
+
#pragma unroll
|
| 361 |
+
for (uint32_t i = 0; i < SF_BLOCK_N / kNumUTCCPAlignedElems; ++ i) {
|
| 362 |
+
auto smem_ptr = smem_sfb[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 363 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 364 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFB + i * 4);
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
// Issue UMMA
|
| 369 |
+
using mma_t = cute::conditional_t<
|
| 370 |
+
kNumMulticast == 1, ptx::SM100_MMA_MXF8F6F4_SS, ptx::SM100_MMA_MXF8F6F4_2x1SM_SS>;
|
| 371 |
+
#pragma unroll
|
| 372 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 373 |
+
const uint32_t sfa_id = (kGranKA == 32 ? k : sfa_stage_in_group_idx);
|
| 374 |
+
const uint32_t sfb_id = (kGranKB == 32 ? k : sfb_stage_in_group_idx);
|
| 375 |
+
const auto runtime_instr_desc = kSwapAB ?
|
| 376 |
+
mma::sm100::make_runtime_instr_desc_with_sf_id(instr_desc, sfb_id, sfa_id):
|
| 377 |
+
mma::sm100::make_runtime_instr_desc_with_sf_id(instr_desc, sfa_id, sfb_id);
|
| 378 |
+
|
| 379 |
+
a_desc.lo = mma::sm100::advance_umma_desc_lo<kMajorA, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t>(a_desc_base_lo, 0, k * UMMA_K);
|
| 380 |
+
b_desc.lo = mma::sm100::advance_umma_desc_lo<kMajorB, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t>(b_desc_base_lo, 0, k * UMMA_K);
|
| 381 |
+
if constexpr (kSwapAB) {
|
| 382 |
+
mma_t::fma(b_desc, a_desc, accum_stage_idx * UMMA_N,
|
| 383 |
+
k_block_idx > 0 or k > 0, runtime_instr_desc,
|
| 384 |
+
kTmemStartColOfSFB, kTmemStartColOfSFA);
|
| 385 |
+
} else {
|
| 386 |
+
mma_t::fma(a_desc, b_desc, accum_stage_idx * UMMA_N,
|
| 387 |
+
k_block_idx > 0 or k > 0, runtime_instr_desc,
|
| 388 |
+
kTmemStartColOfSFA, kTmemStartColOfSFB);
|
| 389 |
+
}
|
| 390 |
+
}
|
| 391 |
+
}
|
| 392 |
+
__syncwarp();
|
| 393 |
+
|
| 394 |
+
// Commit to the mbarrier object
|
| 395 |
+
// No explicit `tcgen05.fence::before_thread_sync` is needed, as this is implicitly performed by `tcgen05.commit`
|
| 396 |
+
empty_barrier_arrive(k_block_idx == num_total_k_blocks - 1);
|
| 397 |
+
}
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// To safely deconstruct barriers, we need another round of waits
|
| 401 |
+
const auto iter_idx = scheduler.current_iter - 1;
|
| 402 |
+
if (kNumMulticast > 1 and iter_idx >= 0) {
|
| 403 |
+
const auto accum_phase_idx = (iter_idx / kNumEpilogueStages) & 1;
|
| 404 |
+
tmem_empty_barriers[iter_idx % kNumEpilogueStages]->wait(accum_phase_idx);
|
| 405 |
+
}
|
| 406 |
+
} else if (warp_idx == 2) {
|
| 407 |
+
// UTCCP transposer
|
| 408 |
+
auto utccp_required_smem_warp_transpose = [&](const uint32_t* smem_ptr) {
|
| 409 |
+
DG_STATIC_ASSERT(kNumUTCCPAlignedElems == 128, "Invalid aligned elements");
|
| 410 |
+
uint32_t values[4];
|
| 411 |
+
#pragma unroll
|
| 412 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 413 |
+
values[i] = ptx::ld_shared(smem_ptr + (i ^ (lane_idx >> 3)) * 32 + lane_idx);
|
| 414 |
+
__syncwarp();
|
| 415 |
+
#pragma unroll
|
| 416 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 417 |
+
ptx::st_shared(smem_ptr + lane_idx * 4 + (i ^ (lane_idx >> 3)), values[i]);
|
| 418 |
+
};
|
| 419 |
+
|
| 420 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 421 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 422 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 423 |
+
// Wait TMA arrival
|
| 424 |
+
full_barriers[stage_idx]->wait(phase);
|
| 425 |
+
|
| 426 |
+
// Transpose for UTCCP at certain stages
|
| 427 |
+
if (k_block_idx % kNumSFAStagesPerLoad == 0) {
|
| 428 |
+
#pragma unroll
|
| 429 |
+
for (uint32_t i = 0; i < SF_BLOCK_M / kNumUTCCPAlignedElems; ++ i)
|
| 430 |
+
utccp_required_smem_warp_transpose(smem_sfa[stage_idx] + i * kNumUTCCPAlignedElems);
|
| 431 |
+
// TODO: figure out whether the proxy fence is valid for 2-CTA cases
|
| 432 |
+
cutlass::arch::fence_view_async_shared();
|
| 433 |
+
}
|
| 434 |
+
if (k_block_idx % kNumSFBStagesPerLoad == 0) {
|
| 435 |
+
#pragma unroll
|
| 436 |
+
for (uint32_t i = 0; i < SF_BLOCK_N / kNumUTCCPAlignedElems; ++ i)
|
| 437 |
+
utccp_required_smem_warp_transpose(smem_sfb[stage_idx] + i * kNumUTCCPAlignedElems);
|
| 438 |
+
// TODO: figure out whether the proxy fence is valid for 2-CTA cases
|
| 439 |
+
cutlass::arch::fence_view_async_shared();
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
// Arrive
|
| 443 |
+
with_sf_full_barriers[stage_idx]->arrive(0u);
|
| 444 |
+
}
|
| 445 |
+
}
|
| 446 |
+
} else if (warp_idx >= kNumNonEpilogueThreads / 32 and warp_idx < (kNumNonEpilogueThreads + kNumUMMAStoreThreads) / 32) {
|
| 447 |
+
// Epilogue warp groups
|
| 448 |
+
const auto epilogue_warp_idx = warp_idx - (kNumNonEpilogueThreads / 32);
|
| 449 |
+
|
| 450 |
+
// NOTES: tensor memory addresses are simplified, as the hardware will ignore the warp index bits,
|
| 451 |
+
// i.e., no need for `tmem_ptr |= (epilogue_warp_idx * 32) << 16`.
|
| 452 |
+
// NOTES: we also forbid two CTAs to share the same SM and its tensor memory
|
| 453 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 454 |
+
|
| 455 |
+
// Share store pipeline between blocks
|
| 456 |
+
uint32_t tma_stage_idx = 0;
|
| 457 |
+
|
| 458 |
+
// Persistently schedule over blocks
|
| 459 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 460 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 461 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 462 |
+
|
| 463 |
+
// Wait UMMA arrival
|
| 464 |
+
tmem_full_barriers[accum_stage_idx]->wait(accum_phase_idx);
|
| 465 |
+
ptx::tcgen05_after_thread_sync();
|
| 466 |
+
|
| 467 |
+
const auto tmem_base_addr = accum_stage_idx * UMMA_N;
|
| 468 |
+
const auto base_m_idx = scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
| 469 |
+
const auto base_n_idx = n_block_idx * BLOCK_N;
|
| 470 |
+
|
| 471 |
+
if constexpr (kSwapAB) {
|
| 472 |
+
const auto effective_m = scheduler.get_aligned_effective_m_in_block(m_block_idx);
|
| 473 |
+
epilogue::sm100_store_cd_swap_ab<
|
| 474 |
+
BLOCK_M, BLOCK_N, STORE_BLOCK_M, STORE_BLOCK_N,
|
| 475 |
+
kSwizzleCDMode, kNumTMAStoreStages, kNumUMMAStoreThreads,
|
| 476 |
+
kGemmType, kWithAccumulation,
|
| 477 |
+
cd_dtype_t, epilogue_type_t>
|
| 478 |
+
(smem_cd, tma_stage_idx, tmem_base_addr,
|
| 479 |
+
base_m_idx, base_n_idx, scheduler.current_group_idx,
|
| 480 |
+
effective_m,
|
| 481 |
+
epilogue_warp_idx, lane_idx,
|
| 482 |
+
tmem_empty_barriers[accum_stage_idx],
|
| 483 |
+
tensor_map_cd);
|
| 484 |
+
} else {
|
| 485 |
+
epilogue::sm100_store_cd<
|
| 486 |
+
BLOCK_M, BLOCK_N, STORE_BLOCK_M, STORE_BLOCK_N,
|
| 487 |
+
kSwizzleCDMode, kNumTMAStoreStages, kNumUMMAStoreThreads,
|
| 488 |
+
kGemmType, kWithAccumulation,
|
| 489 |
+
cd_dtype_t, epilogue_type_t>
|
| 490 |
+
(smem_cd, tma_stage_idx, tmem_base_addr,
|
| 491 |
+
base_m_idx, base_n_idx, scheduler.current_group_idx,
|
| 492 |
+
epilogue_warp_idx, lane_idx,
|
| 493 |
+
tmem_empty_barriers[accum_stage_idx],
|
| 494 |
+
tensor_map_cd);
|
| 495 |
+
}
|
| 496 |
+
}
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
// TODO: Remove redundant synchronization
|
| 500 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 501 |
+
|
| 502 |
+
// Deallocate tensor memory
|
| 503 |
+
if (warp_idx == 0)
|
| 504 |
+
Allocator().free(0, kNumTmemCols);
|
| 505 |
+
|
| 506 |
+
#else
|
| 507 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 508 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 509 |
+
#endif
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
}; // namespace deep_gemm
|
| 513 |
+
|
| 514 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_fp4_mega_moe.cuh
ADDED
|
@@ -0,0 +1,1380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cstdint>
|
| 4 |
+
#include <cutlass/arch/barrier.h>
|
| 5 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/math.cuh>
|
| 8 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 9 |
+
#include <deep_gemm/common/utils.cuh>
|
| 10 |
+
#include <deep_gemm/comm/barrier.cuh>
|
| 11 |
+
#include <deep_gemm/layout/sym_buffer.cuh>
|
| 12 |
+
#include <deep_gemm/layout/mega_moe.cuh>
|
| 13 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 14 |
+
#include <deep_gemm/scheduler/mega_moe.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/tma.cuh>
|
| 17 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 18 |
+
|
| 19 |
+
namespace deep_gemm {
|
| 20 |
+
|
| 21 |
+
template <
|
| 22 |
+
uint32_t kNumMaxTokensPerRank,
|
| 23 |
+
uint32_t kHidden, uint32_t kIntermediateHidden,
|
| 24 |
+
uint32_t kNumExperts, uint32_t kNumTopk,
|
| 25 |
+
uint32_t kNumExpertsPerWave,
|
| 26 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 27 |
+
uint32_t STORE_BLOCK_M,
|
| 28 |
+
uint32_t SF_BLOCK_M, uint32_t SF_BLOCK_N,
|
| 29 |
+
uint32_t kNumMaxPoolTokens,
|
| 30 |
+
uint32_t kNumPaddedSFPoolTokens,
|
| 31 |
+
uint32_t kNumStages,
|
| 32 |
+
uint32_t kNumDispatchThreads, uint32_t kNumNonEpilogueThreads,
|
| 33 |
+
uint32_t kNumEpilogueThreads,
|
| 34 |
+
uint32_t kNumSMs, uint32_t kNumRanks,
|
| 35 |
+
float kActivationClamp,
|
| 36 |
+
bool kFastMath,
|
| 37 |
+
uint32_t L1_SHAPE_N = kIntermediateHidden * 2,
|
| 38 |
+
uint32_t L1_SHAPE_K = kHidden,
|
| 39 |
+
uint32_t L2_SHAPE_N = kHidden,
|
| 40 |
+
uint32_t L2_SHAPE_K = kIntermediateHidden,
|
| 41 |
+
uint32_t kNumDispatchWarps = kNumDispatchThreads / 32,
|
| 42 |
+
uint32_t kNumMMANonEpilogueWarps = kNumNonEpilogueThreads / 32,
|
| 43 |
+
uint32_t kNumEpilogueWarps = kNumEpilogueThreads / 32,
|
| 44 |
+
uint32_t kNumEpilogueWarpgroups = kNumEpilogueWarps / 4,
|
| 45 |
+
uint32_t kNumThreads = kNumDispatchThreads + kNumNonEpilogueThreads + kNumEpilogueThreads,
|
| 46 |
+
uint32_t kNumTokensPerWarp = 32 / kNumTopk,
|
| 47 |
+
uint32_t kNumExpertsPerRank = kNumExperts / kNumRanks
|
| 48 |
+
>
|
| 49 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumThreads, 1) void
|
| 50 |
+
sm100_fp8_fp4_mega_moe_impl(void* y,
|
| 51 |
+
int* cumulative_local_expert_recv_stats,
|
| 52 |
+
const uint32_t num_tokens,
|
| 53 |
+
const __grid_constant__ layout::SymBuffer<kNumRanks> sym_buffer,
|
| 54 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l1_acts,
|
| 55 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l1_acts_sf,
|
| 56 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l1_weights,
|
| 57 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l1_weights_sf,
|
| 58 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l1_output,
|
| 59 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l2_acts,
|
| 60 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l2_acts_sf,
|
| 61 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l2_weights,
|
| 62 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_l2_weights_sf) {
|
| 63 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 64 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 65 |
+
using Allocator = cute::TMEM::Allocator2Sm;
|
| 66 |
+
|
| 67 |
+
// Template checks
|
| 68 |
+
DG_STATIC_ASSERT(kNumDispatchThreads % 128 == 0, "Invalid number of dispatch threads");
|
| 69 |
+
DG_STATIC_ASSERT(kNumNonEpilogueThreads == 128, "Invalid number of MMA non-epilogue threads");
|
| 70 |
+
DG_STATIC_ASSERT(kNumEpilogueThreads % 128 == 0, "Invalid number of MMA epilogue and combine threads");
|
| 71 |
+
DG_STATIC_ASSERT(kNumExperts % kNumRanks == 0, "Invalid number of experts or ranks");
|
| 72 |
+
|
| 73 |
+
// Thread indices
|
| 74 |
+
const bool is_leader_cta = cute::block_rank_in_cluster() == 0;
|
| 75 |
+
const uint32_t sm_idx = blockIdx.x;
|
| 76 |
+
const uint32_t thread_idx = threadIdx.x;
|
| 77 |
+
const uint32_t warp_idx = cutlass::canonical_warp_idx_sync();
|
| 78 |
+
const uint32_t lane_idx = ptx::get_lane_idx();
|
| 79 |
+
|
| 80 |
+
// Prefetch TMA descriptors at the very beginning
|
| 81 |
+
if (warp_idx == 0) {
|
| 82 |
+
cute::prefetch_tma_descriptor(&tensor_map_l1_acts);
|
| 83 |
+
cute::prefetch_tma_descriptor(&tensor_map_l1_acts_sf);
|
| 84 |
+
cute::prefetch_tma_descriptor(&tensor_map_l1_weights);
|
| 85 |
+
cute::prefetch_tma_descriptor(&tensor_map_l1_weights_sf);
|
| 86 |
+
cute::prefetch_tma_descriptor(&tensor_map_l1_output);
|
| 87 |
+
cute::prefetch_tma_descriptor(&tensor_map_l2_acts);
|
| 88 |
+
cute::prefetch_tma_descriptor(&tensor_map_l2_acts_sf);
|
| 89 |
+
cute::prefetch_tma_descriptor(&tensor_map_l2_weights);
|
| 90 |
+
cute::prefetch_tma_descriptor(&tensor_map_l2_weights_sf);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// Workspaces
|
| 94 |
+
const auto workspace = layout::Workspace(
|
| 95 |
+
sym_buffer.get_base_ptr(), kNumRanks, kNumExperts, kNumMaxTokensPerRank, kNumTopk);
|
| 96 |
+
|
| 97 |
+
// Token and buffer layouts
|
| 98 |
+
constexpr auto fp8_token_layout = layout::Data(kHidden);
|
| 99 |
+
constexpr auto bf16_token_layout = layout::Data(kHidden * sizeof(nv_bfloat16));
|
| 100 |
+
constexpr auto fp8_intermediate_token_layout = layout::Data(kIntermediateHidden);
|
| 101 |
+
constexpr auto fp8_sf_layout = layout::Data(kHidden / 32);
|
| 102 |
+
constexpr auto fp8_intermediate_sf_layout = layout::Data(kIntermediateHidden / 32);
|
| 103 |
+
constexpr auto input_topk_idx_layout = layout::Data(kNumTopk * sizeof(int64_t), false);
|
| 104 |
+
constexpr auto input_topk_weights_layout = layout::Data(kNumTopk * sizeof(float), false);
|
| 105 |
+
constexpr auto l1_topk_weights_layout = layout::Data(sizeof(float), false);
|
| 106 |
+
|
| 107 |
+
// Registered inputs
|
| 108 |
+
const auto input_token_buffer = layout::Buffer(
|
| 109 |
+
fp8_token_layout, 1, kNumMaxTokensPerRank,
|
| 110 |
+
workspace.get_end_ptr());
|
| 111 |
+
const auto input_sf_buffer = layout::Buffer(
|
| 112 |
+
fp8_sf_layout, 1, kNumMaxTokensPerRank,
|
| 113 |
+
input_token_buffer.get_end_ptr());
|
| 114 |
+
const auto input_topk_idx_buffer = layout::Buffer(
|
| 115 |
+
input_topk_idx_layout, 1, kNumMaxTokensPerRank,
|
| 116 |
+
input_sf_buffer.get_end_ptr());
|
| 117 |
+
const auto input_topk_weights_buffer = layout::Buffer(
|
| 118 |
+
input_topk_weights_layout, 1, kNumMaxTokensPerRank,
|
| 119 |
+
input_topk_idx_buffer.get_end_ptr());
|
| 120 |
+
|
| 121 |
+
// SF and its buffer configs
|
| 122 |
+
constexpr uint32_t kGranK = 32;
|
| 123 |
+
constexpr uint32_t kNumUTCCPAlignedElems = 128;
|
| 124 |
+
DG_STATIC_ASSERT(SF_BLOCK_M == math::constexpr_align(BLOCK_M, kNumUTCCPAlignedElems), "Invalid SF_BLOCK_M");
|
| 125 |
+
DG_STATIC_ASSERT(SF_BLOCK_N == BLOCK_N, "No padding is needed for SFB");
|
| 126 |
+
|
| 127 |
+
// UTCCP 4x32 transpose index mapping within each 128-element group
|
| 128 |
+
const auto transform_sf_token_idx = [](const uint32_t& token_idx_in_expert) {
|
| 129 |
+
const uint32_t idx = token_idx_in_expert % BLOCK_M;
|
| 130 |
+
return token_idx_in_expert / BLOCK_M * SF_BLOCK_M +
|
| 131 |
+
(idx & ~127u) + (idx & 31u) * 4 + ((idx >> 5) & 3u);
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
// L1 inputs
|
| 135 |
+
const auto l1_token_buffer = layout::Buffer(
|
| 136 |
+
fp8_token_layout, 1, kNumMaxPoolTokens,
|
| 137 |
+
input_topk_weights_buffer.get_end_ptr());
|
| 138 |
+
const auto l1_sf_buffer = layout::Buffer(
|
| 139 |
+
fp8_sf_layout, 1, kNumPaddedSFPoolTokens,
|
| 140 |
+
l1_token_buffer.get_end_ptr());
|
| 141 |
+
const auto l1_topk_weights_buffer = layout::Buffer(
|
| 142 |
+
l1_topk_weights_layout, 1, kNumMaxPoolTokens,
|
| 143 |
+
l1_sf_buffer.get_end_ptr());
|
| 144 |
+
|
| 145 |
+
// L2 inputs
|
| 146 |
+
const auto l2_token_buffer = layout::Buffer(
|
| 147 |
+
fp8_intermediate_token_layout, 1, kNumMaxPoolTokens,
|
| 148 |
+
l1_topk_weights_buffer.get_end_ptr()
|
| 149 |
+
);
|
| 150 |
+
const auto l2_sf_buffer = layout::Buffer(
|
| 151 |
+
fp8_intermediate_sf_layout, 1, kNumPaddedSFPoolTokens,
|
| 152 |
+
l2_token_buffer.get_end_ptr()
|
| 153 |
+
);
|
| 154 |
+
|
| 155 |
+
// Combine inputs
|
| 156 |
+
const auto combine_token_buffer = layout::Buffer(
|
| 157 |
+
bf16_token_layout, kNumTopk, kNumMaxTokensPerRank,
|
| 158 |
+
l2_sf_buffer.get_end_ptr()
|
| 159 |
+
);
|
| 160 |
+
|
| 161 |
+
// Data types
|
| 162 |
+
// NOTES: activations are FP8 (e4m3), weights are FP4 (e2m1)
|
| 163 |
+
using a_dtype_t = cutlass::float_e4m3_t;
|
| 164 |
+
using b_dtype_t = cutlass::detail::float_e2m1_unpacksmem_t;
|
| 165 |
+
|
| 166 |
+
// MMA configs
|
| 167 |
+
// NOTES: always swap A/B, 2-CTA MMA, and matrices are K-major
|
| 168 |
+
constexpr uint32_t LAYOUT_AD_M = 128;
|
| 169 |
+
constexpr uint32_t UMMA_M = LAYOUT_AD_M * 2;
|
| 170 |
+
constexpr uint32_t UMMA_N = BLOCK_M; // Swap AB
|
| 171 |
+
constexpr uint32_t UMMA_K = 32;
|
| 172 |
+
constexpr uint32_t LOAD_BLOCK_M = BLOCK_M / 2; // Multicast on A
|
| 173 |
+
constexpr uint32_t LOAD_BLOCK_N = BLOCK_N;
|
| 174 |
+
DG_STATIC_ASSERT(BLOCK_M % 16 == 0, "Invalid block M");
|
| 175 |
+
DG_STATIC_ASSERT(BLOCK_N == LAYOUT_AD_M, "Invalid block N");
|
| 176 |
+
DG_STATIC_ASSERT(BLOCK_K == 128, "Invalid block K");
|
| 177 |
+
|
| 178 |
+
// Swizzle configs
|
| 179 |
+
constexpr uint32_t kSwizzleAMode = BLOCK_K * sizeof(a_dtype_t);
|
| 180 |
+
constexpr uint32_t kSwizzleBMode = BLOCK_K * sizeof(b_dtype_t);
|
| 181 |
+
constexpr uint32_t kSwizzleCDMode = 128;
|
| 182 |
+
DG_STATIC_ASSERT(BLOCK_N % kSwizzleCDMode == 0, "Invalid block N");
|
| 183 |
+
|
| 184 |
+
// Epilogue configs
|
| 185 |
+
constexpr uint32_t kNumEpilogueStages = 2;
|
| 186 |
+
constexpr uint32_t kNumTMAStoreStages = 2;
|
| 187 |
+
|
| 188 |
+
// Shared memory
|
| 189 |
+
constexpr uint32_t kSharedMemoryAlignment = 1024;
|
| 190 |
+
extern __shared__ __align__(kSharedMemoryAlignment) uint8_t smem_buffer[];
|
| 191 |
+
|
| 192 |
+
// Shared memory sizes
|
| 193 |
+
// NOTES: FP8 CD output for L1 (2 TMA stages, BLOCK_N/2 post-SwiGLU), BF16 output for L2 (no TMA, a single stage)
|
| 194 |
+
constexpr uint32_t L1_OUT_BLOCK_N = BLOCK_N / 2;
|
| 195 |
+
constexpr uint32_t SMEM_EXPERT_COUNT_SIZE =
|
| 196 |
+
math::constexpr_align<uint32_t>(kNumExperts * sizeof(uint32_t), kSharedMemoryAlignment);
|
| 197 |
+
constexpr uint32_t SMEM_SEND_BUFFER_SIZE =
|
| 198 |
+
math::constexpr_align(fp8_token_layout.get_num_bytes() * kNumDispatchWarps, kSharedMemoryAlignment);
|
| 199 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = LOAD_BLOCK_M * BLOCK_K * sizeof(a_dtype_t);
|
| 200 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = LOAD_BLOCK_N * BLOCK_K * sizeof(b_dtype_t);
|
| 201 |
+
constexpr uint32_t SMEM_SFA_SIZE_PER_STAGE = SF_BLOCK_M * sizeof(uint32_t);
|
| 202 |
+
constexpr uint32_t SMEM_SFB_SIZE_PER_STAGE = SF_BLOCK_N * sizeof(uint32_t);
|
| 203 |
+
constexpr uint32_t SMEM_CD_L1_SIZE =
|
| 204 |
+
kNumEpilogueWarpgroups * STORE_BLOCK_M * L1_OUT_BLOCK_N * sizeof(cutlass::float_e4m3_t) * kNumTMAStoreStages;
|
| 205 |
+
constexpr uint32_t SMEM_CD_L2_SIZE =
|
| 206 |
+
kNumEpilogueWarpgroups * STORE_BLOCK_M * BLOCK_N * sizeof(nv_bfloat16);
|
| 207 |
+
constexpr uint32_t SMEM_CD_SIZE = SMEM_CD_L1_SIZE > SMEM_CD_L2_SIZE ? SMEM_CD_L1_SIZE : SMEM_CD_L2_SIZE;
|
| 208 |
+
constexpr uint32_t SMEM_CD_L1_SIZE_PER_STAGE = SMEM_CD_L1_SIZE / kNumTMAStoreStages;
|
| 209 |
+
constexpr uint32_t SMEM_BEFORE_BARRIER_SIZE =
|
| 210 |
+
SMEM_EXPERT_COUNT_SIZE + SMEM_SEND_BUFFER_SIZE + SMEM_CD_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 211 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % kSharedMemoryAlignment == 0 and
|
| 212 |
+
SMEM_A_SIZE_PER_STAGE % kSharedMemoryAlignment == 0 and
|
| 213 |
+
SMEM_B_SIZE_PER_STAGE % kSharedMemoryAlignment == 0,
|
| 214 |
+
"Shared memory of CD/A/B must be aligned to 1024 bytes");
|
| 215 |
+
|
| 216 |
+
// Tensor memory size
|
| 217 |
+
constexpr uint32_t kNumAccumTmemCols = UMMA_N * kNumEpilogueStages;
|
| 218 |
+
constexpr uint32_t kNumSFATmemCols = SF_BLOCK_M / 32;
|
| 219 |
+
constexpr uint32_t kNumSFBTmemCols = SF_BLOCK_N / 32;
|
| 220 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<kNumAccumTmemCols + kNumSFATmemCols + kNumSFBTmemCols>();
|
| 221 |
+
constexpr uint32_t kTmemStartColOfSFA = kNumAccumTmemCols;
|
| 222 |
+
constexpr uint32_t kTmemStartColOfSFB = kNumAccumTmemCols + kNumSFATmemCols;
|
| 223 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 224 |
+
|
| 225 |
+
// Assign shared memory for dispatch warps
|
| 226 |
+
const auto smem_expert_count = reinterpret_cast<uint32_t*>(smem_buffer);
|
| 227 |
+
const auto smem_send_buffers = layout::Buffer(
|
| 228 |
+
fp8_token_layout, kNumDispatchWarps, 1,
|
| 229 |
+
math::advance_ptr(smem_buffer, SMEM_EXPERT_COUNT_SIZE));
|
| 230 |
+
|
| 231 |
+
// GEMM shared memory: C/D, A, B
|
| 232 |
+
// NOTES: GEMM shared memory starts after the dispatch region, aligned to 1024 bytes
|
| 233 |
+
auto smem_gemm_base = math::advance_ptr(
|
| 234 |
+
smem_buffer, SMEM_EXPERT_COUNT_SIZE + SMEM_SEND_BUFFER_SIZE
|
| 235 |
+
);
|
| 236 |
+
|
| 237 |
+
// D/A/B shared memory
|
| 238 |
+
auto smem_cd = utils::PatternVisitor([=](const uint32_t& i) {
|
| 239 |
+
return math::advance_ptr<uint8_t>(smem_gemm_base, i * SMEM_CD_L1_SIZE_PER_STAGE);
|
| 240 |
+
});
|
| 241 |
+
auto smem_cd_l2 = smem_cd[0];
|
| 242 |
+
auto smem_a = utils::PatternVisitor([=](const uint32_t& i) {
|
| 243 |
+
return math::advance_ptr<a_dtype_t>(smem_gemm_base, SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 244 |
+
});
|
| 245 |
+
auto smem_b = utils::PatternVisitor([=](const uint32_t& i) {
|
| 246 |
+
return math::advance_ptr<b_dtype_t>(smem_gemm_base, SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 247 |
+
});
|
| 248 |
+
|
| 249 |
+
// SF shared memory: SFA and SFB per pipeline stage
|
| 250 |
+
auto sf_start_ptr = math::advance_ptr<uint8_t>(smem_gemm_base,
|
| 251 |
+
SMEM_CD_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 252 |
+
auto smem_sfa = utils::PatternVisitor([=](const uint32_t& i) {
|
| 253 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + i * SMEM_SFA_SIZE_PER_STAGE);
|
| 254 |
+
});
|
| 255 |
+
auto smem_sfb = utils::PatternVisitor([=](const uint32_t& i) {
|
| 256 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + kNumStages * SMEM_SFA_SIZE_PER_STAGE + i * SMEM_SFB_SIZE_PER_STAGE);
|
| 257 |
+
});
|
| 258 |
+
|
| 259 |
+
// Epilogue amax reduction shared memory
|
| 260 |
+
auto smem_amax_reduction = reinterpret_cast<float2*>(smem_sfb[kNumStages]);
|
| 261 |
+
|
| 262 |
+
// Barriers and tensor memory pointer
|
| 263 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_amax_reduction + STORE_BLOCK_M * kNumEpilogueWarps / 2);
|
| 264 |
+
auto dispatch_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 265 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumDispatchWarps + i); });
|
| 266 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumDispatchWarps + kNumStages + i); });
|
| 267 |
+
auto tmem_full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumDispatchWarps + kNumStages * 2 + i); });
|
| 268 |
+
auto tmem_empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumDispatchWarps + kNumStages * 2 + kNumEpilogueStages + i); });
|
| 269 |
+
auto combine_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumDispatchWarps + kNumStages * 2 + kNumEpilogueStages * 2 + i); });
|
| 270 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumDispatchWarps + kNumStages * 2 + kNumEpilogueStages * 2 + kNumEpilogueWarps * 2);
|
| 271 |
+
|
| 272 |
+
// A cluster sync is essential for 2CTA tensor memory allocation
|
| 273 |
+
comm::cluster_sync_with_relaxed_arrive();
|
| 274 |
+
|
| 275 |
+
// Initialization
|
| 276 |
+
if (warp_idx == 0) {
|
| 277 |
+
// Clean shared memory
|
| 278 |
+
if (cute::elect_one_sync())
|
| 279 |
+
ptx::st_shared_bulk(smem_expert_count, kNumExperts * sizeof(uint32_t));
|
| 280 |
+
} else if (warp_idx == 1) {
|
| 281 |
+
// Init m-barriers for dispatch
|
| 282 |
+
#pragma unroll
|
| 283 |
+
for (uint32_t i = lane_idx; i < kNumDispatchWarps; i += 32)
|
| 284 |
+
dispatch_barriers[i]->init(1);
|
| 285 |
+
cutlass::arch::fence_barrier_init();
|
| 286 |
+
} else if (warp_idx == 2) {
|
| 287 |
+
// Init GEMM barriers
|
| 288 |
+
if (cute::elect_one_sync()) {
|
| 289 |
+
#pragma unroll
|
| 290 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 291 |
+
// Arrive at all CTAs
|
| 292 |
+
full_barriers[i]->init(2 * 2);
|
| 293 |
+
empty_barriers[i]->init(1);
|
| 294 |
+
}
|
| 295 |
+
#pragma unroll
|
| 296 |
+
for (uint32_t i = 0; i < kNumEpilogueStages; ++ i) {
|
| 297 |
+
// Arrive at all CTAs
|
| 298 |
+
tmem_full_barriers[i]->init(1);
|
| 299 |
+
// Arrive only at the leader CTA
|
| 300 |
+
tmem_empty_barriers[i]->init(2 * kNumEpilogueThreads);
|
| 301 |
+
}
|
| 302 |
+
#pragma unroll
|
| 303 |
+
for (uint32_t i = 0; i < kNumEpilogueWarps * 2; ++ i)
|
| 304 |
+
combine_barriers[i]->init(1);
|
| 305 |
+
}
|
| 306 |
+
cutlass::arch::fence_barrier_init();
|
| 307 |
+
} else if (warp_idx == 3) {
|
| 308 |
+
// Allocate tensor memory
|
| 309 |
+
Allocator().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 310 |
+
}
|
| 311 |
+
// NOTES: Using `.relaxed` is allowed here since `fence_barrier_init` is `.release.cluster`,
|
| 312 |
+
// and `barrier.cluster.wait.aligned` is by default `.acquire`
|
| 313 |
+
comm::cluster_sync_with_relaxed_arrive();
|
| 314 |
+
|
| 315 |
+
// Task scheduler
|
| 316 |
+
auto scheduler = sched::MegaMoEScheduler<
|
| 317 |
+
BLOCK_M, BLOCK_N, BLOCK_K,
|
| 318 |
+
L1_SHAPE_N, L1_SHAPE_K,
|
| 319 |
+
L2_SHAPE_N, L2_SHAPE_K,
|
| 320 |
+
kNumExpertsPerRank,
|
| 321 |
+
kNumExpertsPerWave,
|
| 322 |
+
kNumSMs, kNumRanks>(workspace);
|
| 323 |
+
|
| 324 |
+
// MMA pipeline and TMA phases
|
| 325 |
+
uint32_t stage_idx = 0, phase = 0;
|
| 326 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 327 |
+
++ k_block_idx;
|
| 328 |
+
|
| 329 |
+
// Flip phases only if reach the next first stage
|
| 330 |
+
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
| 331 |
+
phase ^= stage_idx == 0;
|
| 332 |
+
};
|
| 333 |
+
|
| 334 |
+
// Intra-SM Barrier indices
|
| 335 |
+
constexpr uint32_t kDispatchBarrierIdx = 0;
|
| 336 |
+
constexpr uint32_t kDispatchWithEpilogueBarrierIdx = 1;
|
| 337 |
+
constexpr uint32_t kEpilogueFullBarrierIdx = 2;
|
| 338 |
+
constexpr uint32_t kEpilogueWGBarrierStartIdx = 3;
|
| 339 |
+
|
| 340 |
+
// NVLink barrier tags
|
| 341 |
+
constexpr uint32_t kBeforeDispatchPullBarrierTag = 1;
|
| 342 |
+
constexpr uint32_t kBeforeCombineReduceBarrierTag = 2;
|
| 343 |
+
constexpr uint32_t kAfterWorkspaceCleanBarrierTag = 3;
|
| 344 |
+
|
| 345 |
+
// Adjust registers
|
| 346 |
+
constexpr uint32_t kNumDispatchRegisters = 48;
|
| 347 |
+
constexpr uint32_t kNumNonEpilogueRegisters = 40;
|
| 348 |
+
constexpr uint32_t kNumEpilogueRegisters = 208;
|
| 349 |
+
DG_STATIC_ASSERT(kNumDispatchRegisters * kNumDispatchThreads +
|
| 350 |
+
kNumNonEpilogueRegisters * kNumNonEpilogueThreads +
|
| 351 |
+
kNumEpilogueRegisters * kNumEpilogueThreads <= 64512,
|
| 352 |
+
"Too many registers");
|
| 353 |
+
|
| 354 |
+
// Grid sync index assignments (dispatch and epilogue use separate counters to avoid conflicts)
|
| 355 |
+
constexpr uint32_t kDispatchGridSyncIndex = 0;
|
| 356 |
+
constexpr uint32_t kEpilogueGridSyncIndex = 1;
|
| 357 |
+
|
| 358 |
+
// Different warp roles
|
| 359 |
+
if (warp_idx < kNumDispatchWarps) {
|
| 360 |
+
// Adjust registers
|
| 361 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumDispatchRegisters>();
|
| 362 |
+
|
| 363 |
+
// Dispatch warps
|
| 364 |
+
DG_STATIC_ASSERT(kNumTopk <= 32, "Invalid number of topk");
|
| 365 |
+
constexpr uint32_t kNumActivateLanes = kNumTokensPerWarp * kNumTopk;
|
| 366 |
+
const auto read_topk_idx = [&](const auto& process) {
|
| 367 |
+
// TODO: figure out better unrolling
|
| 368 |
+
// Now, `unroll` is better than `unroll 8`
|
| 369 |
+
#pragma unroll
|
| 370 |
+
for (uint32_t i = (sm_idx * kNumDispatchWarps + warp_idx) * kNumTokensPerWarp;
|
| 371 |
+
i < num_tokens;
|
| 372 |
+
i += kNumSMs * kNumDispatchWarps * kNumTokensPerWarp) {
|
| 373 |
+
// Allocate slots for each token-topk
|
| 374 |
+
int expert_idx = -1;
|
| 375 |
+
if (i + (lane_idx / kNumTopk) < num_tokens and lane_idx < kNumActivateLanes) {
|
| 376 |
+
expert_idx = static_cast<int>(
|
| 377 |
+
__ldg(input_topk_idx_buffer.get_base_ptr<int64_t>() + i * kNumTopk + lane_idx));
|
| 378 |
+
if (expert_idx >= 0)
|
| 379 |
+
process(i * kNumTopk + lane_idx, expert_idx);
|
| 380 |
+
}
|
| 381 |
+
__syncwarp();
|
| 382 |
+
}
|
| 383 |
+
};
|
| 384 |
+
|
| 385 |
+
// Count experts' tokens
|
| 386 |
+
read_topk_idx([&](const uint32_t& token_topk_idx, const int& expert_idx) {
|
| 387 |
+
atomicAdd_block(smem_expert_count + expert_idx, 1);
|
| 388 |
+
});
|
| 389 |
+
ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx);
|
| 390 |
+
|
| 391 |
+
// Get SM offset (~6.5 us)
|
| 392 |
+
#pragma unroll
|
| 393 |
+
for (uint32_t i = thread_idx; i < kNumExperts; i += kNumDispatchThreads) {
|
| 394 |
+
const uint64_t send_value = (1ull << 32) | static_cast<uint64_t>(smem_expert_count[i]);
|
| 395 |
+
smem_expert_count[i] = static_cast<uint32_t>(
|
| 396 |
+
ptx::atomic_add(workspace.get_expert_send_count_ptr(i), send_value));
|
| 397 |
+
}
|
| 398 |
+
ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx);
|
| 399 |
+
|
| 400 |
+
// Write source indices (~2 us with 512 tokens)
|
| 401 |
+
read_topk_idx([&](const uint32_t& token_topk_idx, const int& expert_idx) {
|
| 402 |
+
const auto dst_rank_idx = expert_idx / kNumExpertsPerRank;
|
| 403 |
+
const auto dst_slot_idx = atomicAdd_block(smem_expert_count + expert_idx, 1);
|
| 404 |
+
const auto dst_ptr = workspace.get_src_token_topk_idx_ptr(
|
| 405 |
+
expert_idx % kNumExpertsPerRank, sym_buffer.rank_idx, dst_slot_idx);
|
| 406 |
+
*sym_buffer.map(dst_ptr, dst_rank_idx) = token_topk_idx;
|
| 407 |
+
});
|
| 408 |
+
|
| 409 |
+
// Grid sync
|
| 410 |
+
comm::grid_sync<kNumSMs, kDispatchGridSyncIndex>(
|
| 411 |
+
workspace, sm_idx, thread_idx,
|
| 412 |
+
[=]() { ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx); }
|
| 413 |
+
);
|
| 414 |
+
|
| 415 |
+
// Write expert count
|
| 416 |
+
if (sm_idx == 0) {
|
| 417 |
+
#pragma unroll
|
| 418 |
+
for (uint32_t i = thread_idx; i < kNumExperts; i += kNumDispatchThreads) {
|
| 419 |
+
const auto dst_rank_idx = i / kNumExpertsPerRank;
|
| 420 |
+
const auto dst_local_expert_idx = i % kNumExpertsPerRank;
|
| 421 |
+
const auto expert_status = *workspace.get_expert_send_count_ptr(i);
|
| 422 |
+
*sym_buffer.map(
|
| 423 |
+
workspace.get_expert_recv_count_ptr(sym_buffer.rank_idx, dst_local_expert_idx),
|
| 424 |
+
dst_rank_idx) = expert_status & 0xffffffff;
|
| 425 |
+
ptx::atomic_add_sys(
|
| 426 |
+
sym_buffer.map(workspace.get_expert_recv_count_sum_ptr(dst_local_expert_idx), dst_rank_idx),
|
| 427 |
+
expert_status);
|
| 428 |
+
}
|
| 429 |
+
}
|
| 430 |
+
ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx);
|
| 431 |
+
|
| 432 |
+
// Barrier before pulling
|
| 433 |
+
comm::nvlink_barrier<kNumRanks, kNumSMs, kNumDispatchThreads,
|
| 434 |
+
kDispatchGridSyncIndex, kBeforeDispatchPullBarrierTag>(
|
| 435 |
+
workspace, sym_buffer, sm_idx, thread_idx,
|
| 436 |
+
[=]() { ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx); },
|
| 437 |
+
/* After the grid sync above, there is no more writes by other SMs (except 0) */ false,
|
| 438 |
+
/* After the NVLink barrier, there is a grid sync */ true
|
| 439 |
+
);
|
| 440 |
+
|
| 441 |
+
// Ensure the epilogue barrier cannot run with the pull barrier
|
| 442 |
+
ptx::sync_unaligned(kNumDispatchThreads + kNumEpilogueThreads, kDispatchWithEpilogueBarrierIdx);
|
| 443 |
+
|
| 444 |
+
// Pull token data and SF from remote ranks into local L1 buffer
|
| 445 |
+
uint32_t pull_mbarrier_phase = 0;
|
| 446 |
+
const auto pull_buffer = smem_send_buffers.get_rank_buffer(warp_idx).get_data_buffer(0);
|
| 447 |
+
const auto pull_mbarrier = dispatch_barriers[warp_idx];
|
| 448 |
+
|
| 449 |
+
// Cache expert token counts in registers (same pattern as scheduler)
|
| 450 |
+
scheduler.fetch_expert_recv_count();
|
| 451 |
+
|
| 452 |
+
// Per-rank counts for current expert (re-loaded when expert changes)
|
| 453 |
+
constexpr uint32_t kNumRanksPerLane = math::constexpr_ceil_div(kNumRanks, 32u);
|
| 454 |
+
int current_expert_idx = -1;
|
| 455 |
+
uint32_t stored_rank_count[kNumRanksPerLane] = {};
|
| 456 |
+
uint32_t expert_start_idx = 0, expert_end_idx = 0;
|
| 457 |
+
uint32_t expert_pool_block_offset = 0;
|
| 458 |
+
|
| 459 |
+
constexpr uint32_t kNumGlobalWarps = kNumSMs * kNumDispatchWarps;
|
| 460 |
+
for (uint32_t token_idx = sm_idx * kNumDispatchWarps + warp_idx; ; token_idx += kNumGlobalWarps) {
|
| 461 |
+
// Advance expert until within the range
|
| 462 |
+
int old_expert_idx = current_expert_idx;
|
| 463 |
+
while (token_idx >= expert_end_idx) {
|
| 464 |
+
if (++ current_expert_idx >= kNumExpertsPerRank)
|
| 465 |
+
break;
|
| 466 |
+
|
| 467 |
+
// Update pool block offset for the new expert
|
| 468 |
+
expert_pool_block_offset += math::ceil_div(expert_end_idx - expert_start_idx, BLOCK_M);
|
| 469 |
+
|
| 470 |
+
// Move start and end to the next expert
|
| 471 |
+
expert_start_idx = expert_end_idx;
|
| 472 |
+
expert_end_idx += scheduler.get_num_tokens(current_expert_idx);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
// Finish all tokens
|
| 476 |
+
if (current_expert_idx >= kNumExpertsPerRank)
|
| 477 |
+
break;
|
| 478 |
+
|
| 479 |
+
// Load per-rank counts when expert changes
|
| 480 |
+
if (old_expert_idx != current_expert_idx) {
|
| 481 |
+
old_expert_idx = current_expert_idx;
|
| 482 |
+
#pragma unroll
|
| 483 |
+
for (uint32_t i = 0; i < kNumRanksPerLane; ++ i) {
|
| 484 |
+
const uint32_t j = i * 32 + lane_idx;
|
| 485 |
+
// TODO: this is not coalesced
|
| 486 |
+
stored_rank_count[i] = j < kNumRanks ?
|
| 487 |
+
static_cast<uint32_t>(*workspace.get_expert_recv_count_ptr(j, current_expert_idx)) : 0;
|
| 488 |
+
}
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
// Round-robin rank selection via iterative min-peeling
|
| 492 |
+
uint32_t current_rank_in_expert_idx;
|
| 493 |
+
uint32_t remaining[kNumRanksPerLane];
|
| 494 |
+
#pragma unroll
|
| 495 |
+
for (uint32_t i = 0; i < kNumRanksPerLane; ++ i)
|
| 496 |
+
remaining[i] = stored_rank_count[i];
|
| 497 |
+
uint32_t offset = 0;
|
| 498 |
+
uint32_t token_idx_in_expert = token_idx - expert_start_idx;
|
| 499 |
+
uint32_t slot_idx = token_idx_in_expert;
|
| 500 |
+
uint32_t token_idx_in_rank;
|
| 501 |
+
while (true) {
|
| 502 |
+
// Compute active count and min across all ranks
|
| 503 |
+
// NOTES: reduce within each lane first, then warp-reduce once
|
| 504 |
+
uint32_t num_actives_in_lane = 0;
|
| 505 |
+
uint32_t min_in_lane = 0xffffffff;
|
| 506 |
+
#pragma unroll
|
| 507 |
+
for (uint32_t i = 0; i < kNumRanksPerLane; ++ i) {
|
| 508 |
+
num_actives_in_lane += remaining[i] > 0;
|
| 509 |
+
if (remaining[i] > 0)
|
| 510 |
+
min_in_lane = cute::min(min_in_lane, remaining[i]);
|
| 511 |
+
}
|
| 512 |
+
const uint32_t num_active_ranks = __reduce_add_sync(0xffffffff, num_actives_in_lane);
|
| 513 |
+
const uint32_t length = __reduce_min_sync(0xffffffff, min_in_lane);
|
| 514 |
+
|
| 515 |
+
// Hit in the current round
|
| 516 |
+
const uint32_t num_round_tokens = length * num_active_ranks;
|
| 517 |
+
if (slot_idx < num_round_tokens) {
|
| 518 |
+
const uint32_t slot_idx_in_round = slot_idx % num_active_ranks;
|
| 519 |
+
uint32_t num_seen_ranks = 0;
|
| 520 |
+
current_rank_in_expert_idx = 0;
|
| 521 |
+
#pragma unroll
|
| 522 |
+
for (uint32_t i = 0; i < kNumRanksPerLane; ++ i) {
|
| 523 |
+
const uint32_t mask = __ballot_sync(0xffffffff, remaining[i] > 0);
|
| 524 |
+
const uint32_t num_active_lanes = __popc(mask);
|
| 525 |
+
if (slot_idx_in_round >= num_seen_ranks and slot_idx_in_round < num_seen_ranks + num_active_lanes)
|
| 526 |
+
current_rank_in_expert_idx = i * 32 + __fns(mask, 0, slot_idx_in_round - num_seen_ranks + 1);
|
| 527 |
+
num_seen_ranks += num_active_lanes;
|
| 528 |
+
}
|
| 529 |
+
token_idx_in_rank = offset + (slot_idx / num_active_ranks);
|
| 530 |
+
break;
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
// Move into the next round
|
| 534 |
+
slot_idx -= num_round_tokens;
|
| 535 |
+
offset += length;
|
| 536 |
+
#pragma unroll
|
| 537 |
+
for (uint32_t i = 0; i < kNumRanksPerLane; ++ i)
|
| 538 |
+
remaining[i] -= cute::min(remaining[i], length);
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
// Read source token-topk index (written by remote dispatch via NVLink)
|
| 542 |
+
const uint32_t src_token_topk_idx = *workspace.get_src_token_topk_idx_ptr(
|
| 543 |
+
current_expert_idx, current_rank_in_expert_idx, token_idx_in_rank);
|
| 544 |
+
const uint32_t src_token_idx = src_token_topk_idx / kNumTopk;
|
| 545 |
+
const uint32_t src_topk_idx = src_token_topk_idx % kNumTopk;
|
| 546 |
+
|
| 547 |
+
// TMA load token from remote rank into shared memory
|
| 548 |
+
if (cute::elect_one_sync()) {
|
| 549 |
+
ptx::tma_load_1d(
|
| 550 |
+
pull_buffer.get_base_ptr(),
|
| 551 |
+
sym_buffer.map(input_token_buffer.get_data_buffer(src_token_idx).get_base_ptr(),
|
| 552 |
+
current_rank_in_expert_idx),
|
| 553 |
+
pull_mbarrier, kHidden);
|
| 554 |
+
}
|
| 555 |
+
__syncwarp();
|
| 556 |
+
|
| 557 |
+
// Load and store SF (overlaps with TMA token load)
|
| 558 |
+
constexpr uint32_t kNumSFUint32 = kHidden / 128;
|
| 559 |
+
DG_STATIC_ASSERT(kNumSFUint32 > 0 and kHidden % 128 == 0, "Invalid SF");
|
| 560 |
+
const auto remote_sf_ptr = sym_buffer.map(
|
| 561 |
+
input_sf_buffer.get_data_buffer(src_token_idx).get_base_ptr<uint32_t>(),
|
| 562 |
+
current_rank_in_expert_idx);
|
| 563 |
+
const auto local_sf_ptr = l1_sf_buffer.get_base_ptr<uint32_t>();
|
| 564 |
+
const auto sf_pool_token_idx = expert_pool_block_offset * SF_BLOCK_M +
|
| 565 |
+
transform_sf_token_idx(token_idx_in_expert);
|
| 566 |
+
#pragma unroll
|
| 567 |
+
for (uint32_t i = 0; i < math::constexpr_ceil_div(kNumSFUint32, 32u); ++ i) {
|
| 568 |
+
const uint32_t j = i * 32 + lane_idx;
|
| 569 |
+
if (j < kNumSFUint32)
|
| 570 |
+
local_sf_ptr[j * kNumPaddedSFPoolTokens + sf_pool_token_idx] = remote_sf_ptr[j];
|
| 571 |
+
}
|
| 572 |
+
__syncwarp();
|
| 573 |
+
|
| 574 |
+
// Store weights and token data
|
| 575 |
+
const uint32_t pool_token_idx = expert_pool_block_offset * BLOCK_M + token_idx_in_expert;
|
| 576 |
+
if (cute::elect_one_sync()) {
|
| 577 |
+
// Load weights
|
| 578 |
+
const auto weight = *sym_buffer.map(
|
| 579 |
+
input_topk_weights_buffer.get_base_ptr<float>() + src_token_topk_idx,
|
| 580 |
+
current_rank_in_expert_idx);
|
| 581 |
+
*l1_topk_weights_buffer.get_data_buffer(pool_token_idx).get_base_ptr<float>() = weight;
|
| 582 |
+
|
| 583 |
+
// Wait for TMA token load to complete
|
| 584 |
+
ptx::mbarrier_arrive_and_set_tx(pull_mbarrier, kHidden);
|
| 585 |
+
ptx::mbarrier_wait_and_flip_phase(pull_mbarrier, pull_mbarrier_phase);
|
| 586 |
+
|
| 587 |
+
// Store token to local L1 buffer via TMA
|
| 588 |
+
ptx::tma_store_1d(
|
| 589 |
+
l1_token_buffer.get_data_buffer(pool_token_idx).get_base_ptr(),
|
| 590 |
+
pull_buffer.get_base_ptr(), pull_buffer.get_num_bytes());
|
| 591 |
+
|
| 592 |
+
// Write source metadata for combine write-back
|
| 593 |
+
*workspace.get_token_src_metadata_ptr(pool_token_idx) =
|
| 594 |
+
{current_rank_in_expert_idx, src_token_idx, src_topk_idx};
|
| 595 |
+
|
| 596 |
+
// Wait for token TMA store to complete
|
| 597 |
+
cute::tma_store_arrive();
|
| 598 |
+
ptx::tma_store_wait<0>();
|
| 599 |
+
ptx::red_add_rel(
|
| 600 |
+
workspace.get_l1_arrival_count_ptr(expert_pool_block_offset + token_idx_in_expert / BLOCK_M), 1);
|
| 601 |
+
}
|
| 602 |
+
__syncwarp();
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
// Clean workspace for the next usage, and also do cumulative stats
|
| 606 |
+
// NOTES: it is overlapped with combine reduction epilogue
|
| 607 |
+
ptx::sync_unaligned(kNumDispatchThreads + kNumEpilogueThreads, kDispatchWithEpilogueBarrierIdx);
|
| 608 |
+
|
| 609 |
+
DG_STATIC_ASSERT(kNumSMs > 1, "Invalid SM count");
|
| 610 |
+
if (sm_idx == 0) {
|
| 611 |
+
// SM 0: clear expert send count
|
| 612 |
+
#pragma unroll
|
| 613 |
+
for (uint32_t i = thread_idx; i < kNumExperts; i += kNumDispatchThreads)
|
| 614 |
+
*workspace.get_expert_send_count_ptr(i) = 0;
|
| 615 |
+
} else {
|
| 616 |
+
// Other SMs: clean blocks
|
| 617 |
+
for (uint32_t i = sm_idx - 1; i < kNumExpertsPerRank; i += kNumSMs - 1) {
|
| 618 |
+
// Read expert token count before clearing
|
| 619 |
+
const auto num_recv_tokens = static_cast<uint32_t>(
|
| 620 |
+
*workspace.get_expert_recv_count_sum_ptr(i));
|
| 621 |
+
const auto num_recv_m_blocks = math::ceil_div(num_recv_tokens, BLOCK_M);
|
| 622 |
+
|
| 623 |
+
// Compute expert pool block offset
|
| 624 |
+
expert_pool_block_offset = scheduler.get_pool_block_offset(i);
|
| 625 |
+
|
| 626 |
+
// Wait read count ready
|
| 627 |
+
ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx);
|
| 628 |
+
|
| 629 |
+
// Clean expert token count, and add cumulative results
|
| 630 |
+
DG_STATIC_ASSERT(kNumDispatchWarps >= 2, "Not enough dispatch warps");
|
| 631 |
+
if (warp_idx == 0) {
|
| 632 |
+
*workspace.get_expert_recv_count_sum_ptr(i) = 0;
|
| 633 |
+
} else if (warp_idx == 1) {
|
| 634 |
+
if (cute::elect_one_sync() and cumulative_local_expert_recv_stats != nullptr)
|
| 635 |
+
ptx::red_add(cumulative_local_expert_recv_stats + i, static_cast<int>(num_recv_tokens));
|
| 636 |
+
__syncwarp();
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
// Clean per-rank token count
|
| 640 |
+
for (uint32_t j = thread_idx; j < kNumRanks; j += kNumDispatchThreads)
|
| 641 |
+
*workspace.get_expert_recv_count_ptr(j, i) = 0;
|
| 642 |
+
__syncwarp();
|
| 643 |
+
|
| 644 |
+
// Clean L1 and L2 arrival stuffs
|
| 645 |
+
for (uint32_t j = thread_idx; j < num_recv_m_blocks; j += kNumDispatchThreads) {
|
| 646 |
+
*workspace.get_l1_arrival_count_ptr(expert_pool_block_offset + j) = 0;
|
| 647 |
+
*workspace.get_l2_arrival_mask_ptr(expert_pool_block_offset + j) = 0;
|
| 648 |
+
}
|
| 649 |
+
__syncwarp();
|
| 650 |
+
}
|
| 651 |
+
}
|
| 652 |
+
|
| 653 |
+
// Wait for all ranks to finish cleaning
|
| 654 |
+
comm::nvlink_barrier<kNumRanks, kNumSMs, kNumDispatchThreads,
|
| 655 |
+
kDispatchGridSyncIndex, kAfterWorkspaceCleanBarrierTag>(
|
| 656 |
+
workspace, sym_buffer, sm_idx, thread_idx,
|
| 657 |
+
[=]() { ptx::sync_aligned(kNumDispatchThreads, kDispatchBarrierIdx); },
|
| 658 |
+
/* Before the NVLink barrier, there is a grid sync */ true,
|
| 659 |
+
/* At the end of kernel does not need to sync */ false
|
| 660 |
+
);
|
| 661 |
+
} else if (warp_idx == kNumDispatchWarps) {
|
| 662 |
+
// Adjust registers
|
| 663 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumNonEpilogueRegisters>();
|
| 664 |
+
|
| 665 |
+
// GEMM TMA load warp for tokens with SFA
|
| 666 |
+
scheduler.for_each_block([&](const sched::BlockPhase& block_phase,
|
| 667 |
+
const uint32_t& local_expert_idx,
|
| 668 |
+
const uint32_t& num_k_blocks,
|
| 669 |
+
const uint32_t& m_block_idx, const uint32_t& n_block_idx) {
|
| 670 |
+
const auto tensor_map_a_ptr = block_phase == sched::BlockPhase::Linear2
|
| 671 |
+
? &tensor_map_l2_acts : &tensor_map_l1_acts;
|
| 672 |
+
const auto tensor_map_sfa_ptr = block_phase == sched::BlockPhase::Linear2
|
| 673 |
+
? &tensor_map_l2_acts_sf : &tensor_map_l1_acts_sf;
|
| 674 |
+
|
| 675 |
+
const auto shape_k = block_phase == sched::BlockPhase::Linear2 ? L2_SHAPE_K : L1_SHAPE_K;
|
| 676 |
+
const auto shape_sfa_k = math::ceil_div(shape_k, kGranK * 4u);
|
| 677 |
+
|
| 678 |
+
// Compute pool block offset for this expert
|
| 679 |
+
const uint32_t pool_block_idx = scheduler.get_current_pool_block_offset() + m_block_idx;
|
| 680 |
+
|
| 681 |
+
// Wait the entire token arrival for linear 1
|
| 682 |
+
if (block_phase == sched::BlockPhase::Linear1) {
|
| 683 |
+
const auto ptr = workspace.get_l1_arrival_count_ptr(pool_block_idx);
|
| 684 |
+
const auto expected = scheduler.template get_valid_m<false>();
|
| 685 |
+
while (ptx::ld_acq(ptr) != expected);
|
| 686 |
+
} else {
|
| 687 |
+
// The L1 output's block N is halved into `BLOCK_K / 2`, so we have to wait 2x L1 blocks' arrival
|
| 688 |
+
// NOTES: Originally we wait blocks on-demand to overlap L1 calculation
|
| 689 |
+
// with L2, but this optimization is negative when `num_experts_per_wave`
|
| 690 |
+
// guarantees L1's completion when L2 starts. So we remove it.
|
| 691 |
+
// In the future, if `num_experts_per_wave` is not large enough
|
| 692 |
+
// due to small `num_experts_per_rank`, we may need to add it back or add a switch
|
| 693 |
+
DG_STATIC_ASSERT(BLOCK_K == BLOCK_N, "Invalid block sizes");
|
| 694 |
+
const auto ptr = workspace.get_l2_arrival_mask_ptr(pool_block_idx);
|
| 695 |
+
// NOTES: Equivalent to `(1ull << (2 * num_k_blocks)) - 1`, but split into two shifts
|
| 696 |
+
// to avoid undefined behavior when `num_k_blocks == 32`
|
| 697 |
+
const uint64_t expected = ((1ull << num_k_blocks) << num_k_blocks) - 1;
|
| 698 |
+
while (ptx::ld_acq_gpu(ptr) != expected);
|
| 699 |
+
}
|
| 700 |
+
|
| 701 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_k_blocks; advance_pipeline(k_block_idx)) {
|
| 702 |
+
// Wait consumer release
|
| 703 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 704 |
+
|
| 705 |
+
// Compute token offset from pool block index
|
| 706 |
+
uint32_t m_idx = pool_block_idx * BLOCK_M;
|
| 707 |
+
uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 708 |
+
uint32_t sfa_m_idx = pool_block_idx * SF_BLOCK_M;
|
| 709 |
+
uint32_t sfa_k_idx = k_block_idx;
|
| 710 |
+
|
| 711 |
+
// Add 2 CTA offsets for non-leader CTA
|
| 712 |
+
if (not is_leader_cta)
|
| 713 |
+
m_idx += scheduler.template get_valid_m<true>() / 2;
|
| 714 |
+
|
| 715 |
+
// TMA copy tokens and SFA, then arrive at full barrier
|
| 716 |
+
if (cute::elect_one_sync()) {
|
| 717 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t>(
|
| 718 |
+
tensor_map_a_ptr, full_barriers[stage_idx], smem_a[stage_idx], k_idx, m_idx, 2);
|
| 719 |
+
tma::copy<SF_BLOCK_M, 1, 0>(
|
| 720 |
+
tensor_map_sfa_ptr, full_barriers[stage_idx], smem_sfa[stage_idx], sfa_m_idx, sfa_k_idx, 2);
|
| 721 |
+
if (is_leader_cta) {
|
| 722 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE * 2 + SF_BLOCK_M * sizeof(uint32_t) * 2);
|
| 723 |
+
} else {
|
| 724 |
+
full_barriers[stage_idx]->arrive(0u);
|
| 725 |
+
}
|
| 726 |
+
}
|
| 727 |
+
__syncwarp();
|
| 728 |
+
}
|
| 729 |
+
});
|
| 730 |
+
} else if (warp_idx == kNumDispatchWarps + 1) {
|
| 731 |
+
// Adjust registers
|
| 732 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumNonEpilogueRegisters>();
|
| 733 |
+
|
| 734 |
+
// GEMM TMA load warp for weights with SF
|
| 735 |
+
scheduler.for_each_block([&](const sched::BlockPhase& block_phase,
|
| 736 |
+
const uint32_t& local_expert_idx,
|
| 737 |
+
const uint32_t& num_k_blocks,
|
| 738 |
+
const uint32_t& m_block_idx, const uint32_t& n_block_idx) {
|
| 739 |
+
const auto tensor_map_b_ptr =
|
| 740 |
+
block_phase == sched::BlockPhase::Linear2 ? &tensor_map_l2_weights : &tensor_map_l1_weights;
|
| 741 |
+
const auto tensor_map_sfb_ptr =
|
| 742 |
+
block_phase == sched::BlockPhase::Linear2 ? &tensor_map_l2_weights_sf : &tensor_map_l1_weights_sf;
|
| 743 |
+
|
| 744 |
+
const auto shape_k = block_phase == sched::BlockPhase::Linear2 ? L2_SHAPE_K : L1_SHAPE_K;
|
| 745 |
+
const auto shape_n = block_phase == sched::BlockPhase::Linear2 ? L2_SHAPE_N : L1_SHAPE_N;
|
| 746 |
+
const auto shape_sfb_k = math::ceil_div(shape_k, kGranK * 4u);
|
| 747 |
+
|
| 748 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_k_blocks; advance_pipeline(k_block_idx)) {
|
| 749 |
+
// Wait consumer release
|
| 750 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 751 |
+
|
| 752 |
+
// Compute weight offset
|
| 753 |
+
uint32_t n_idx = local_expert_idx * shape_n + n_block_idx * BLOCK_N;
|
| 754 |
+
uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 755 |
+
uint32_t sfb_n_idx = n_block_idx * BLOCK_N;
|
| 756 |
+
uint32_t sfb_k_idx = local_expert_idx * shape_sfb_k + k_block_idx;
|
| 757 |
+
|
| 758 |
+
// TMA copy weights with SF
|
| 759 |
+
if (cute::elect_one_sync()) {
|
| 760 |
+
tma::copy<BLOCK_K, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t>(
|
| 761 |
+
tensor_map_b_ptr, full_barriers[stage_idx], smem_b[stage_idx], k_idx, n_idx, 2);
|
| 762 |
+
tma::copy<BLOCK_N, 1, 0>(
|
| 763 |
+
tensor_map_sfb_ptr, full_barriers[stage_idx], smem_sfb[stage_idx], sfb_n_idx, sfb_k_idx, 2);
|
| 764 |
+
if (is_leader_cta) {
|
| 765 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(SMEM_B_SIZE_PER_STAGE + BLOCK_N * sizeof(uint32_t) * 2);
|
| 766 |
+
} else {
|
| 767 |
+
full_barriers[stage_idx]->arrive(0u);
|
| 768 |
+
}
|
| 769 |
+
}
|
| 770 |
+
__syncwarp();
|
| 771 |
+
}
|
| 772 |
+
});
|
| 773 |
+
} else if (warp_idx == kNumDispatchWarps + 2) {
|
| 774 |
+
// Adjust registers
|
| 775 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumNonEpilogueRegisters>();
|
| 776 |
+
|
| 777 |
+
// GEMM MMA issue warp (only the leader CTA will run)
|
| 778 |
+
if (is_leader_cta) {
|
| 779 |
+
// Make instruction descriptor with block scaling
|
| 780 |
+
// NOTES: always swap A/B
|
| 781 |
+
auto instr_desc = cute::UMMA::make_instr_desc_block_scaled<
|
| 782 |
+
b_dtype_t, a_dtype_t, float, cutlass::float_ue8m0_t,
|
| 783 |
+
UMMA_M, UMMA_N,
|
| 784 |
+
cute::UMMA::Major::K, cute::UMMA::Major::K
|
| 785 |
+
>();
|
| 786 |
+
auto sf_desc = mma::sm100::make_sf_desc(nullptr);
|
| 787 |
+
|
| 788 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 789 |
+
auto a_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode>(smem_a[0], 0, 0);
|
| 790 |
+
auto b_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 791 |
+
uint32_t a_desc_lo = lane_idx < kNumStages ? a_desc.lo + lane_idx * SMEM_A_SIZE_PER_STAGE / 16 : 0u;
|
| 792 |
+
uint32_t b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 793 |
+
|
| 794 |
+
// Checks for MMA instructions
|
| 795 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 796 |
+
(UMMA_M == 128 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256) or
|
| 797 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 798 |
+
"Invalid MMA instruction shape");
|
| 799 |
+
|
| 800 |
+
// Persistently schedule over blocks
|
| 801 |
+
uint32_t current_iter_idx = 0;
|
| 802 |
+
scheduler.for_each_block([&](const sched::BlockPhase& block_phase,
|
| 803 |
+
const uint32_t& local_expert_idx,
|
| 804 |
+
const uint32_t& num_k_blocks,
|
| 805 |
+
const uint32_t& m_block_idx, const uint32_t& n_block_idx) {
|
| 806 |
+
// Dynamic update of UMMA N based on effective M
|
| 807 |
+
mma::sm100::update_instr_desc_with_umma_n(instr_desc, scheduler.template get_valid_m<true>());
|
| 808 |
+
|
| 809 |
+
// Wait tensor memory empty barrier arrival
|
| 810 |
+
const auto accum_stage_idx = current_iter_idx % kNumEpilogueStages;
|
| 811 |
+
const auto accum_phase = (current_iter_idx ++ / kNumEpilogueStages) & 1;
|
| 812 |
+
tmem_empty_barriers[accum_stage_idx]->wait(accum_phase ^ 1);
|
| 813 |
+
ptx::tcgen05_after_thread_sync();
|
| 814 |
+
|
| 815 |
+
// Empty barrier arrival
|
| 816 |
+
auto empty_barrier_arrive = [&](const bool& do_tmem_full_arrive) {
|
| 817 |
+
auto umma_arrive = [](const uint64_t* barrier) {
|
| 818 |
+
constexpr uint16_t kCTAMask = (1 << 2) - 1;
|
| 819 |
+
cutlass::arch::umma_arrive_multicast_2x1SM(barrier, kCTAMask);
|
| 820 |
+
};
|
| 821 |
+
umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 822 |
+
|
| 823 |
+
// NOTES: the tensor memory accumulator pipeline has nothing to do with multicasting
|
| 824 |
+
if (do_tmem_full_arrive)
|
| 825 |
+
umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barriers[accum_stage_idx]));
|
| 826 |
+
__syncwarp();
|
| 827 |
+
};
|
| 828 |
+
|
| 829 |
+
// Launch MMAs
|
| 830 |
+
#pragma unroll 2
|
| 831 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_k_blocks; advance_pipeline(k_block_idx)) {
|
| 832 |
+
// Wait TMA load completion
|
| 833 |
+
full_barriers[stage_idx]->wait(phase);
|
| 834 |
+
ptx::tcgen05_after_thread_sync();
|
| 835 |
+
|
| 836 |
+
const auto a_desc_base_lo = ptx::exchange(a_desc_lo, stage_idx);
|
| 837 |
+
const auto b_desc_base_lo = ptx::exchange(b_desc_lo, stage_idx);
|
| 838 |
+
if (cute::elect_one_sync()) {
|
| 839 |
+
// UTCCP copy SFA and SFB to TMEM
|
| 840 |
+
using cute_utccp_t = cute::SM100_UTCCP_4x32dp128bit_2cta;
|
| 841 |
+
#pragma unroll
|
| 842 |
+
for (uint32_t i = 0; i < SF_BLOCK_M / kNumUTCCPAlignedElems; ++ i) {
|
| 843 |
+
auto smem_ptr = smem_sfa[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 844 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 845 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFA + i * 4);
|
| 846 |
+
}
|
| 847 |
+
#pragma unroll
|
| 848 |
+
for (uint32_t i = 0; i < SF_BLOCK_N / kNumUTCCPAlignedElems; ++ i) {
|
| 849 |
+
auto smem_ptr = smem_sfb[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 850 |
+
mma::sm100::replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 851 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFB + i * 4);
|
| 852 |
+
}
|
| 853 |
+
|
| 854 |
+
// Issue UMMA
|
| 855 |
+
#pragma unroll
|
| 856 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 857 |
+
const auto runtime_instr_desc =
|
| 858 |
+
mma::sm100::make_runtime_instr_desc_with_sf_id(instr_desc, k, k);
|
| 859 |
+
a_desc.lo = mma::sm100::advance_umma_desc_lo<
|
| 860 |
+
cute::UMMA::Major::K, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t>(a_desc_base_lo, 0, k * UMMA_K);
|
| 861 |
+
b_desc.lo = mma::sm100::advance_umma_desc_lo<
|
| 862 |
+
cute::UMMA::Major::K, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t>(b_desc_base_lo, 0, k * UMMA_K);
|
| 863 |
+
ptx::SM100_MMA_MXF8F6F4_2x1SM_SS::fma(
|
| 864 |
+
b_desc, a_desc, accum_stage_idx * UMMA_N,
|
| 865 |
+
k_block_idx > 0 or k > 0, runtime_instr_desc,
|
| 866 |
+
kTmemStartColOfSFB, kTmemStartColOfSFA);
|
| 867 |
+
}
|
| 868 |
+
}
|
| 869 |
+
__syncwarp();
|
| 870 |
+
|
| 871 |
+
// Commit to the mbarrier object
|
| 872 |
+
// No explicit `tcgen05.fence::before_thread_sync` is needed, as this is implicitly performed by `tcgen05.commit`
|
| 873 |
+
empty_barrier_arrive(k_block_idx == num_k_blocks - 1);
|
| 874 |
+
}
|
| 875 |
+
});
|
| 876 |
+
|
| 877 |
+
// To safely deconstruct barriers, we need another round of waits
|
| 878 |
+
if (current_iter_idx > 0) {
|
| 879 |
+
const auto accum_phase_idx = ((current_iter_idx - 1) / kNumEpilogueStages) & 1;
|
| 880 |
+
tmem_empty_barriers[(current_iter_idx - 1) % kNumEpilogueStages]->wait(accum_phase_idx);
|
| 881 |
+
}
|
| 882 |
+
}
|
| 883 |
+
} else if (warp_idx == kNumDispatchWarps + 3) {
|
| 884 |
+
// Adjust registers
|
| 885 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumNonEpilogueRegisters>();
|
| 886 |
+
|
| 887 |
+
} else if (warp_idx >= kNumDispatchWarps + kNumMMANonEpilogueWarps) {
|
| 888 |
+
// Adjust registers
|
| 889 |
+
cutlass::arch::warpgroup_reg_alloc<kNumEpilogueRegisters>();
|
| 890 |
+
|
| 891 |
+
// NOTES: tensor memory addresses are simplified, as the hardware will ignore the warp index bits,
|
| 892 |
+
// i.e., no need for `tmem_ptr |= (epilogue_warp_idx * 32) << 16`.
|
| 893 |
+
// NOTES: we also forbid two CTAs to share the same SM and its tensor memory
|
| 894 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 895 |
+
|
| 896 |
+
// GEMM epilogue warps
|
| 897 |
+
const auto epilogue_warp_idx = warp_idx - (kNumDispatchWarps + kNumMMANonEpilogueWarps);
|
| 898 |
+
const auto epilogue_wg_idx = epilogue_warp_idx / 4;
|
| 899 |
+
const auto epilogue_thread_idx = epilogue_warp_idx * 32 + lane_idx;
|
| 900 |
+
const auto warp_idx_in_wg = epilogue_warp_idx % 4;
|
| 901 |
+
DG_STATIC_ASSERT((kNumDispatchWarps + kNumMMANonEpilogueWarps) % 4 == 0 and
|
| 902 |
+
kNumEpilogueWarps % 4 == 0, "Invalid epilogue warps");
|
| 903 |
+
|
| 904 |
+
// TODO: support effective block M
|
| 905 |
+
// NOTES:
|
| 906 |
+
// - 2 warpgroups divide the whole BM into BM / 2
|
| 907 |
+
// - 4 warps divide the whole BN into BN / 4
|
| 908 |
+
// - BM / 2 is further divided into stored blocks, i.e. with `STORE_BLOCK_M` size
|
| 909 |
+
// - `STORE_BLOCK_M` in further divided into `ATOM_M`
|
| 910 |
+
constexpr uint32_t WG_BLOCK_M = BLOCK_M / kNumEpilogueWarpgroups;
|
| 911 |
+
constexpr uint32_t ATOM_M = 8;
|
| 912 |
+
constexpr uint32_t kNumBankGroupBytes = 16u;
|
| 913 |
+
constexpr uint32_t kNumAtomsPerStore = STORE_BLOCK_M / ATOM_M;
|
| 914 |
+
DG_STATIC_ASSERT(BLOCK_M % kNumEpilogueWarpgroups == 0, "Invalid block M");
|
| 915 |
+
DG_STATIC_ASSERT(WG_BLOCK_M % STORE_BLOCK_M == 0, "Invalid warpgroup block M");
|
| 916 |
+
DG_STATIC_ASSERT(STORE_BLOCK_M % ATOM_M == 0, "Invalid store block M");
|
| 917 |
+
DG_STATIC_ASSERT(BLOCK_N == 128, "Invalid block N");
|
| 918 |
+
|
| 919 |
+
// Ensure the epilogue barrier cannot run with the pull barrier
|
| 920 |
+
ptx::sync_unaligned(kNumDispatchThreads + kNumEpilogueThreads, kDispatchWithEpilogueBarrierIdx);
|
| 921 |
+
|
| 922 |
+
// Persistently schedule over blocks
|
| 923 |
+
uint32_t current_iter_idx = 0;
|
| 924 |
+
scheduler.for_each_block([&](const sched::BlockPhase& block_phase,
|
| 925 |
+
const uint32_t& local_expert_idx,
|
| 926 |
+
const uint32_t& num_k_blocks,
|
| 927 |
+
const uint32_t& m_block_idx, const uint32_t& n_block_idx) {
|
| 928 |
+
// Wait UMMA arrival
|
| 929 |
+
const auto accum_stage_idx = current_iter_idx % kNumEpilogueStages;
|
| 930 |
+
const auto accum_phase = (current_iter_idx ++ / kNumEpilogueStages) & 1;
|
| 931 |
+
tmem_full_barriers[accum_stage_idx]->wait(accum_phase);
|
| 932 |
+
ptx::tcgen05_after_thread_sync();
|
| 933 |
+
|
| 934 |
+
// Compute offsets
|
| 935 |
+
// NOTES: use shuffle here to let NVCC know warp divergence won't happen
|
| 936 |
+
const uint32_t valid_m = ptx::exchange(scheduler.template get_valid_m<false>(), 0);
|
| 937 |
+
const uint32_t pool_block_idx = scheduler.get_current_pool_block_offset() + m_block_idx;
|
| 938 |
+
uint32_t m_idx = pool_block_idx * BLOCK_M;
|
| 939 |
+
uint32_t n_idx = n_block_idx * BLOCK_N;
|
| 940 |
+
|
| 941 |
+
if (block_phase == sched::BlockPhase::Linear1) {
|
| 942 |
+
// Unified L1 epilogue: SwiGLU in-place using granularity 8 interleaved weights
|
| 943 |
+
// With `SM100_TMEM_LOAD_16dp256b1x`, gate/up pairs are:
|
| 944 |
+
// (values[0], values[2]), (values[1], values[3]),
|
| 945 |
+
// (values[4], values[6]), (values[5], values[7])
|
| 946 |
+
float stored_cached_weight = 0;
|
| 947 |
+
|
| 948 |
+
#pragma unroll
|
| 949 |
+
for (uint32_t s = 0; s < WG_BLOCK_M / STORE_BLOCK_M; ++ s) {
|
| 950 |
+
// Early break if the entire store block is beyond the valid token range
|
| 951 |
+
if (epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M >= valid_m) {
|
| 952 |
+
ptx::tcgen05_before_thread_sync();
|
| 953 |
+
tmem_empty_barriers[accum_stage_idx]->arrive(0u);
|
| 954 |
+
break;
|
| 955 |
+
}
|
| 956 |
+
|
| 957 |
+
// Iterate all atoms in the store block
|
| 958 |
+
float2 swiglu_values[kNumAtomsPerStore * 2];
|
| 959 |
+
float2 amax_values[kNumAtomsPerStore];
|
| 960 |
+
#pragma unroll
|
| 961 |
+
for (uint32_t i = 0; i < kNumAtomsPerStore; ++ i) {
|
| 962 |
+
const uint32_t j = s * kNumAtomsPerStore + i;
|
| 963 |
+
|
| 964 |
+
// Load weights from global into register cache per 32 tokens
|
| 965 |
+
DG_STATIC_ASSERT(32 % ATOM_M == 0, "Invalid block size");
|
| 966 |
+
if ((j * ATOM_M) % 32 == 0 and (WG_BLOCK_M % 32 == 0 or j * ATOM_M + lane_idx < WG_BLOCK_M)) {
|
| 967 |
+
stored_cached_weight = *l1_topk_weights_buffer
|
| 968 |
+
.get_data_buffer(m_idx + epilogue_wg_idx * WG_BLOCK_M + j * ATOM_M + lane_idx)
|
| 969 |
+
.get_base_ptr<float>();
|
| 970 |
+
}
|
| 971 |
+
|
| 972 |
+
// Load weights from register cache
|
| 973 |
+
const float2 weights = {
|
| 974 |
+
ptx::exchange(stored_cached_weight, (j * ATOM_M) % 32 + (lane_idx % 4) * 2 + 0),
|
| 975 |
+
ptx::exchange(stored_cached_weight, (j * ATOM_M) % 32 + (lane_idx % 4) * 2 + 1)
|
| 976 |
+
};
|
| 977 |
+
|
| 978 |
+
// Load from TMEM
|
| 979 |
+
uint32_t tmem_addr = accum_stage_idx * UMMA_N + epilogue_wg_idx * WG_BLOCK_M + j * ATOM_M;
|
| 980 |
+
uint32_t values[ATOM_M];
|
| 981 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr,
|
| 982 |
+
values[0], values[1], values[2], values[3]);
|
| 983 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr | 0x00100000,
|
| 984 |
+
values[4], values[5], values[6], values[7]);
|
| 985 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 986 |
+
|
| 987 |
+
// Signal tensor memory consumed on the last atom
|
| 988 |
+
if (j == WG_BLOCK_M / ATOM_M - 1) {
|
| 989 |
+
ptx::tcgen05_before_thread_sync();
|
| 990 |
+
tmem_empty_barriers[accum_stage_idx]->arrive(0u);
|
| 991 |
+
}
|
| 992 |
+
|
| 993 |
+
// Apply SwiGLU: silu(gate) * up
|
| 994 |
+
// Gate/up pairs: (0, 2), (1, 3), (4, 6), (5, 7)
|
| 995 |
+
auto fp32_values = reinterpret_cast<float*>(values);
|
| 996 |
+
#pragma unroll
|
| 997 |
+
for (uint32_t k = 0; k < 2; ++ k) {
|
| 998 |
+
auto bf16_gate = __float22bfloat162_rn(make_float2(fp32_values[k * 4], fp32_values[k * 4 + 1]));
|
| 999 |
+
auto bf16_up = __float22bfloat162_rn(make_float2(fp32_values[k * 4 + 2], fp32_values[k * 4 + 3]));
|
| 1000 |
+
|
| 1001 |
+
// Clamp
|
| 1002 |
+
if constexpr (kActivationClamp != cute::numeric_limits<float>::infinity()) {
|
| 1003 |
+
bf16_gate = __hmin2(bf16_gate, {kActivationClamp, kActivationClamp});
|
| 1004 |
+
bf16_up = __hmax2(bf16_up, {-kActivationClamp, -kActivationClamp});
|
| 1005 |
+
bf16_up = __hmin2(bf16_up, {kActivationClamp, kActivationClamp});
|
| 1006 |
+
}
|
| 1007 |
+
|
| 1008 |
+
// SwiGLU
|
| 1009 |
+
auto gate = __bfloat1622float2(bf16_gate);
|
| 1010 |
+
auto neg_gate_exp = make_float2(
|
| 1011 |
+
kFastMath ? __expf(-gate.x) : expf(-gate.x),
|
| 1012 |
+
kFastMath ? __expf(-gate.y) : expf(-gate.y));
|
| 1013 |
+
const auto denom = __fadd2_rn({1.0f, 1.0f}, neg_gate_exp);
|
| 1014 |
+
if constexpr (kFastMath) {
|
| 1015 |
+
gate = __fmul2_rn(gate, {math::fast_rcp(denom.x), math::fast_rcp(denom.y)});
|
| 1016 |
+
} else {
|
| 1017 |
+
gate = {gate.x / denom.x, gate.y / denom.y};
|
| 1018 |
+
}
|
| 1019 |
+
const auto up = __bfloat1622float2(bf16_up);
|
| 1020 |
+
swiglu_values[i * 2 + k] = __fmul2_rn(__fmul2_rn(gate, up), weights);
|
| 1021 |
+
}
|
| 1022 |
+
|
| 1023 |
+
// Amax reduction
|
| 1024 |
+
amax_values[i].x = math::warp_reduce<4, true>(
|
| 1025 |
+
cute::max(cute::abs(swiglu_values[i * 2 + 0].x), cute::abs(swiglu_values[i * 2 + 1].x)),
|
| 1026 |
+
math::ReduceMax<float>());
|
| 1027 |
+
amax_values[i].y = math::warp_reduce<4, true>(
|
| 1028 |
+
cute::max(cute::abs(swiglu_values[i * 2 + 0].y), cute::abs(swiglu_values[i * 2 + 1].y)),
|
| 1029 |
+
math::ReduceMax<float>());
|
| 1030 |
+
if (lane_idx < 4)
|
| 1031 |
+
smem_amax_reduction[epilogue_warp_idx * (STORE_BLOCK_M / 2) + i * (ATOM_M / 2) + lane_idx] = amax_values[i];
|
| 1032 |
+
__syncwarp();
|
| 1033 |
+
}
|
| 1034 |
+
|
| 1035 |
+
// Wait shared memory release from previous TMA store
|
| 1036 |
+
// And fence `smem_amax_reduction`
|
| 1037 |
+
const uint32_t tma_stage_idx = s % kNumTMAStoreStages;
|
| 1038 |
+
ptx::tma_store_wait<kNumTMAStoreStages - 1>();
|
| 1039 |
+
ptx::sync_aligned(128, kEpilogueWGBarrierStartIdx + epilogue_wg_idx);
|
| 1040 |
+
|
| 1041 |
+
// Cast to FP8 E4M3 and store into shared memory
|
| 1042 |
+
#pragma unroll
|
| 1043 |
+
for (uint32_t i = 0; i < kNumAtomsPerStore; ++ i) {
|
| 1044 |
+
// Reduce amax
|
| 1045 |
+
const float2 wp_amax =
|
| 1046 |
+
smem_amax_reduction[(epilogue_warp_idx ^ 1) * (STORE_BLOCK_M / 2) + i * (ATOM_M / 2) + lane_idx % 4];
|
| 1047 |
+
amax_values[i].x = cute::max(amax_values[i].x, wp_amax.x);
|
| 1048 |
+
amax_values[i].y = cute::max(amax_values[i].y, wp_amax.y);
|
| 1049 |
+
|
| 1050 |
+
// Calculate SF
|
| 1051 |
+
float2 sf, sf_inv;
|
| 1052 |
+
math::get_e4m3_sf_and_sf_inv(amax_values[i], sf, sf_inv);
|
| 1053 |
+
|
| 1054 |
+
// Cast
|
| 1055 |
+
const float2 upper = __fmul2_rn(swiglu_values[i * 2 + 0], sf_inv);
|
| 1056 |
+
const float2 lower = __fmul2_rn(swiglu_values[i * 2 + 1], sf_inv);
|
| 1057 |
+
const auto fp8x4_values = __nv_fp8x4_e4m3(make_float4(upper.x, upper.y, lower.x, lower.y));
|
| 1058 |
+
|
| 1059 |
+
// STSM
|
| 1060 |
+
uint32_t row = lane_idx;
|
| 1061 |
+
uint32_t col = warp_idx_in_wg;
|
| 1062 |
+
const auto smem_ptr = smem_cd[tma_stage_idx] + epilogue_wg_idx * STORE_BLOCK_M * L1_OUT_BLOCK_N
|
| 1063 |
+
+ i * ATOM_M * L1_OUT_BLOCK_N
|
| 1064 |
+
+ row * L1_OUT_BLOCK_N
|
| 1065 |
+
+ (col ^ (row / 2)) * kNumBankGroupBytes;
|
| 1066 |
+
ptx::SM100_U8x4_STSM_T<__nv_fp8x4_e4m3>::copy(fp8x4_values, smem_ptr);
|
| 1067 |
+
|
| 1068 |
+
// Store SF to `l2_sf_buffer` as UE8M0 (MN-major layout)
|
| 1069 |
+
// Only one warp per pair writes (both hold the same SF after cross-warp reduce)
|
| 1070 |
+
// Each lane < 4 holds SF for 2 rows (sf.x and sf.y)
|
| 1071 |
+
if (warp_idx_in_wg % 2 == 0 and lane_idx < 4) {
|
| 1072 |
+
const uint32_t k_idx = n_block_idx * 2 + warp_idx_in_wg / 2;
|
| 1073 |
+
const uint32_t k_uint_idx = k_idx / 4, byte_idx = k_idx % 4;
|
| 1074 |
+
const uint32_t mn_stride = kNumPaddedSFPoolTokens * sizeof(uint32_t);
|
| 1075 |
+
const auto sf_base_ptr = l2_sf_buffer.get_base_ptr<uint8_t>();
|
| 1076 |
+
// NOTES: consecutive tokens (t, t + 1) are in the same 32-group, so `sf_idx` differs by 4
|
| 1077 |
+
// NOTES: originally there was:
|
| 1078 |
+
// - `const uint32_t token_idx_in_expert = m_block_idx * BLOCK_M + epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M + i * ATOM_M + lane_idx * 2
|
| 1079 |
+
// - `scheduler.get_current_pool_block_offset() * SF_BLOCK_M + transform_sf_token_idx(token_idx_in_expert)`
|
| 1080 |
+
// We find out that
|
| 1081 |
+
// 1. `m_block_idx * BLOCK_M` mod `BLOCK_M` is 0, and `epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M + i * ATOM_M + lane_idx * 2` is always < `BLOCK_M`, so we can put `m_block_idx * BLOCK_M` outside
|
| 1082 |
+
// 2. `lane_idx * 2` controls the lowest 3 bit of `token_idx_in_expert`, and `transform_sf_token_idx` is a bitwise-independent transformation if the input is less than `BLOCK_M`, so we can put `lane_idx * 2` outside
|
| 1083 |
+
// This reduce the number of computation instructions.
|
| 1084 |
+
const uint32_t token_base_idx = epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M + i * ATOM_M;
|
| 1085 |
+
__builtin_assume(token_base_idx < BLOCK_M);
|
| 1086 |
+
const auto sf_pool_token_idx = scheduler.get_current_pool_block_offset() * SF_BLOCK_M
|
| 1087 |
+
+ m_block_idx * SF_BLOCK_M + transform_sf_token_idx(token_base_idx) + (lane_idx * 2) * 4;
|
| 1088 |
+
const auto sf_addr = k_uint_idx * mn_stride + sf_pool_token_idx * static_cast<uint32_t>(sizeof(uint32_t)) + byte_idx;
|
| 1089 |
+
sf_base_ptr[sf_addr] =
|
| 1090 |
+
(*reinterpret_cast<const uint32_t*>(&sf.x) >> 23);
|
| 1091 |
+
sf_base_ptr[sf_addr + 4 * static_cast<uint32_t>(sizeof(uint32_t))] =
|
| 1092 |
+
(*reinterpret_cast<const uint32_t*>(&sf.y) >> 23);
|
| 1093 |
+
}
|
| 1094 |
+
__syncwarp();
|
| 1095 |
+
}
|
| 1096 |
+
ptx::sync_aligned(128, kEpilogueWGBarrierStartIdx + epilogue_wg_idx);
|
| 1097 |
+
|
| 1098 |
+
// Issue TMA store after all atoms in this store block
|
| 1099 |
+
if (warp_idx_in_wg == 0 and cute::elect_one_sync()) {
|
| 1100 |
+
uint32_t out_n_idx = n_block_idx * L1_OUT_BLOCK_N;
|
| 1101 |
+
cute::tma_store_fence();
|
| 1102 |
+
cute::SM90_TMA_STORE_2D::copy(
|
| 1103 |
+
&tensor_map_l1_output,
|
| 1104 |
+
smem_cd[tma_stage_idx] + epilogue_wg_idx * STORE_BLOCK_M * L1_OUT_BLOCK_N,
|
| 1105 |
+
out_n_idx,
|
| 1106 |
+
m_idx + epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M);
|
| 1107 |
+
cute::tma_store_arrive();
|
| 1108 |
+
}
|
| 1109 |
+
__syncwarp();
|
| 1110 |
+
}
|
| 1111 |
+
|
| 1112 |
+
// Notify L2
|
| 1113 |
+
// TODO: less epilogue sync scope
|
| 1114 |
+
ptx::tma_store_wait<0>();
|
| 1115 |
+
ptx::sync_aligned(kNumEpilogueThreads, kEpilogueFullBarrierIdx);
|
| 1116 |
+
if (epilogue_warp_idx == 0 and cute::elect_one_sync()) {
|
| 1117 |
+
DG_STATIC_ASSERT(L2_SHAPE_K <= 64 * L1_OUT_BLOCK_N, "L2 shape K is too large");
|
| 1118 |
+
ptx::red_or_rel_gpu(
|
| 1119 |
+
workspace.get_l2_arrival_mask_ptr(pool_block_idx),
|
| 1120 |
+
1ull << n_block_idx
|
| 1121 |
+
);
|
| 1122 |
+
}
|
| 1123 |
+
__syncwarp();
|
| 1124 |
+
} else {
|
| 1125 |
+
DG_STATIC_ASSERT(STORE_BLOCK_M % 8 == 0, "Invalid store M");
|
| 1126 |
+
constexpr uint32_t kNumRowsPerWarp = STORE_BLOCK_M / 8;
|
| 1127 |
+
|
| 1128 |
+
// L2 BF16 epilogue: write GEMM output to remote combine buffer via NVLink
|
| 1129 |
+
#pragma unroll
|
| 1130 |
+
for (uint32_t s = 0; s < WG_BLOCK_M / STORE_BLOCK_M; ++ s) {
|
| 1131 |
+
// Early break if the entire store block is beyond the valid token range
|
| 1132 |
+
// TODO: check performance
|
| 1133 |
+
if (epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M >= valid_m) {
|
| 1134 |
+
ptx::tcgen05_before_thread_sync();
|
| 1135 |
+
tmem_empty_barriers[accum_stage_idx]->arrive(0u);
|
| 1136 |
+
break;
|
| 1137 |
+
}
|
| 1138 |
+
|
| 1139 |
+
#pragma unroll
|
| 1140 |
+
for (uint32_t i = 0; i < STORE_BLOCK_M / ATOM_M; ++ i) {
|
| 1141 |
+
// Load from TMEM using .16x256b shape to satisfy STSM layout requirements
|
| 1142 |
+
// Start from lane index 0 and 16
|
| 1143 |
+
uint32_t tmem_addr = accum_stage_idx * UMMA_N + epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M + i * ATOM_M;
|
| 1144 |
+
uint32_t values[ATOM_M];
|
| 1145 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr,
|
| 1146 |
+
values[0], values[1], values[2], values[3]);
|
| 1147 |
+
cute::SM100_TMEM_LOAD_16dp256b1x::copy(tmem_addr | 0x00100000,
|
| 1148 |
+
values[4], values[5], values[6], values[7]);
|
| 1149 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 1150 |
+
|
| 1151 |
+
// Wait shared memory release from previous NVLink store
|
| 1152 |
+
// NOTES: skip for the first store block since the prior full barrier already ensures completion
|
| 1153 |
+
if (i == 0 and s > 0)
|
| 1154 |
+
ptx::sync_aligned(128, kEpilogueWGBarrierStartIdx + epilogue_wg_idx);
|
| 1155 |
+
|
| 1156 |
+
// Signal tensor memory consumed
|
| 1157 |
+
if (s == WG_BLOCK_M / STORE_BLOCK_M - 1 and i == STORE_BLOCK_M / ATOM_M - 1) {
|
| 1158 |
+
ptx::tcgen05_before_thread_sync();
|
| 1159 |
+
tmem_empty_barriers[accum_stage_idx]->arrive(0u);
|
| 1160 |
+
}
|
| 1161 |
+
|
| 1162 |
+
// Store into shared memory
|
| 1163 |
+
// NOTES: only use first 16 lanes for address
|
| 1164 |
+
// NOTES: 2 warps share a BF16 swizzle atom
|
| 1165 |
+
uint32_t row = lane_idx % 8;
|
| 1166 |
+
uint32_t col = (epilogue_warp_idx % 2) * 4 + lane_idx / 8;
|
| 1167 |
+
const auto smem_ptr = smem_cd_l2 +
|
| 1168 |
+
epilogue_wg_idx * STORE_BLOCK_M * BLOCK_N * static_cast<uint32_t>(sizeof(nv_bfloat16)) +
|
| 1169 |
+
(warp_idx_in_wg / 2) * STORE_BLOCK_M * kSwizzleCDMode +
|
| 1170 |
+
i * ATOM_M * kSwizzleCDMode +
|
| 1171 |
+
row * (kNumBankGroupBytes * 8) +
|
| 1172 |
+
(col ^ row) * kNumBankGroupBytes;
|
| 1173 |
+
ptx::SM90_U32x4_STSM_T<uint32_t>::copy(
|
| 1174 |
+
math::cast_into_bf16_and_pack(values[0], values[1]),
|
| 1175 |
+
math::cast_into_bf16_and_pack(values[2], values[3]),
|
| 1176 |
+
math::cast_into_bf16_and_pack(values[4], values[5]),
|
| 1177 |
+
math::cast_into_bf16_and_pack(values[6], values[7]),
|
| 1178 |
+
smem_ptr
|
| 1179 |
+
);
|
| 1180 |
+
}
|
| 1181 |
+
|
| 1182 |
+
// Wait shared memory ready
|
| 1183 |
+
ptx::sync_aligned(128, kEpilogueWGBarrierStartIdx + epilogue_wg_idx);
|
| 1184 |
+
|
| 1185 |
+
// Write into remote buffers
|
| 1186 |
+
// One warp per row, now the layout is different from shared memory storing
|
| 1187 |
+
const uint32_t row_in_atom = (warp_idx_in_wg * 2 + lane_idx / 16) % ATOM_M;
|
| 1188 |
+
const uint32_t bank_group_idx = lane_idx % 8;
|
| 1189 |
+
|
| 1190 |
+
#pragma unroll
|
| 1191 |
+
for (uint32_t j = 0; j < kNumRowsPerWarp; ++ j) {
|
| 1192 |
+
const uint32_t row_in_store = j * 8 + warp_idx_in_wg * 2 + lane_idx / 16;
|
| 1193 |
+
const uint32_t m_idx_in_block = epilogue_wg_idx * WG_BLOCK_M + s * STORE_BLOCK_M + row_in_store;
|
| 1194 |
+
|
| 1195 |
+
// Skip padding rows beyond the actual token count for this expert
|
| 1196 |
+
if (m_idx_in_block >= valid_m)
|
| 1197 |
+
break;
|
| 1198 |
+
|
| 1199 |
+
const auto src_metadata = *workspace.get_token_src_metadata_ptr(m_idx + m_idx_in_block);
|
| 1200 |
+
const uint32_t dst_rank_idx = src_metadata.rank_idx;
|
| 1201 |
+
const uint32_t dst_token_idx = src_metadata.token_idx;
|
| 1202 |
+
const uint32_t dst_topk_idx = src_metadata.topk_idx;
|
| 1203 |
+
|
| 1204 |
+
// Read from shared memory
|
| 1205 |
+
const auto smem_ptr = smem_cd_l2 +
|
| 1206 |
+
epilogue_wg_idx * STORE_BLOCK_M * BLOCK_N * static_cast<uint32_t>(sizeof(nv_bfloat16)) +
|
| 1207 |
+
(lane_idx % 16 / 8) * STORE_BLOCK_M * kSwizzleCDMode +
|
| 1208 |
+
row_in_store * kSwizzleCDMode +
|
| 1209 |
+
(bank_group_idx ^ row_in_atom) * kNumBankGroupBytes;
|
| 1210 |
+
const auto packed = ptx::ld_shared(reinterpret_cast<float4*>(smem_ptr));
|
| 1211 |
+
|
| 1212 |
+
// Write into remote
|
| 1213 |
+
const auto dst_token = combine_token_buffer.get_rank_buffer(dst_topk_idx)
|
| 1214 |
+
.get_data_buffer(dst_token_idx);
|
| 1215 |
+
const auto dst_ptr = math::advance_ptr<float4>(
|
| 1216 |
+
dst_token.get_base_ptr(),
|
| 1217 |
+
n_idx * static_cast<uint32_t>(sizeof(nv_bfloat16)) + (lane_idx % 16) * static_cast<uint32_t>(sizeof(float4)));
|
| 1218 |
+
*sym_buffer.map(dst_ptr, dst_rank_idx) = packed;
|
| 1219 |
+
}
|
| 1220 |
+
}
|
| 1221 |
+
|
| 1222 |
+
// Ensure the next epilogue safe to use shared memory
|
| 1223 |
+
ptx::sync_aligned(kNumEpilogueThreads, kEpilogueFullBarrierIdx);
|
| 1224 |
+
}
|
| 1225 |
+
});
|
| 1226 |
+
|
| 1227 |
+
// Deallocate tensor memory
|
| 1228 |
+
// NOTES: must be called by the same logical warp ID on both CTAs
|
| 1229 |
+
if (epilogue_warp_idx == 0)
|
| 1230 |
+
Allocator().free(0, kNumTmemCols);
|
| 1231 |
+
|
| 1232 |
+
// NVLink barrier (grid sync + cross-rank signal + grid sync): ~4 us
|
| 1233 |
+
comm::nvlink_barrier<kNumRanks, kNumSMs, kNumEpilogueThreads,
|
| 1234 |
+
kEpilogueGridSyncIndex, kBeforeCombineReduceBarrierTag>(
|
| 1235 |
+
workspace, sym_buffer, sm_idx, epilogue_thread_idx,
|
| 1236 |
+
[&]() { ptx::sync_aligned(kNumEpilogueThreads, kEpilogueFullBarrierIdx); }
|
| 1237 |
+
);
|
| 1238 |
+
|
| 1239 |
+
// Barrier with dispatch warps, so that they can do clean workspace
|
| 1240 |
+
ptx::sync_unaligned(kNumDispatchThreads + kNumEpilogueThreads, kDispatchWithEpilogueBarrierIdx);
|
| 1241 |
+
|
| 1242 |
+
// Combine: reduce top-k results and write back
|
| 1243 |
+
// NOTES: reuse shared memory from start up to the barriers
|
| 1244 |
+
// 1 token, 1 topk latency: ~3 us
|
| 1245 |
+
constexpr uint32_t kNumHiddenBytes = kHidden * sizeof(nv_bfloat16);
|
| 1246 |
+
constexpr uint32_t kNumElemsPerUint4 = sizeof(uint4) / sizeof(nv_bfloat162);
|
| 1247 |
+
|
| 1248 |
+
// 3 slots of chunk is needed: 2 load stages and 1 store
|
| 1249 |
+
constexpr uint32_t kNumChunkSlots = 3;
|
| 1250 |
+
constexpr uint32_t kNumMaxRegistersForBuffer = 128;
|
| 1251 |
+
|
| 1252 |
+
// NOTES: either 1 or 2 chunks for simplicity
|
| 1253 |
+
// NOTES: Restrict on both smem and register
|
| 1254 |
+
constexpr uint32_t kNumChunks =
|
| 1255 |
+
kNumChunkSlots * kNumEpilogueWarps * kNumHiddenBytes <= SMEM_BEFORE_BARRIER_SIZE and kHidden <= 32 * kNumMaxRegistersForBuffer ? 1 : 2;
|
| 1256 |
+
constexpr uint32_t kNumChunkBytes = kNumHiddenBytes / kNumChunks;
|
| 1257 |
+
constexpr uint32_t kNumChunkUint4 = kNumChunkBytes / sizeof(uint4);
|
| 1258 |
+
constexpr uint32_t kNumUint4PerLane = kNumChunkUint4 / 32;
|
| 1259 |
+
DG_STATIC_ASSERT(kHidden % kNumChunks == 0, "Hidden must be divisible by number of chunks");
|
| 1260 |
+
DG_STATIC_ASSERT(kNumChunkSlots * kNumEpilogueWarps * kNumHiddenBytes / kNumChunks <= SMEM_BEFORE_BARRIER_SIZE, "Hidden is too large");
|
| 1261 |
+
DG_STATIC_ASSERT(kNumChunkBytes % 16 == 0, "Combine chunk must be TMA-aligned (16 bytes)");
|
| 1262 |
+
DG_STATIC_ASSERT(kNumChunkBytes % sizeof(uint4) == 0, "Combine chunk must be divisible by 16 bytes");
|
| 1263 |
+
DG_STATIC_ASSERT(kNumChunkUint4 % 32 == 0, "Combine chunk must be a multiple of 32 16-byte elements (one per lane)");
|
| 1264 |
+
DG_STATIC_ASSERT(kNumTopk <= 32, "Top-k must fit in a single warp");
|
| 1265 |
+
|
| 1266 |
+
// Verify combined shared memory budget at runtime
|
| 1267 |
+
DG_DEVICE_ASSERT(kNumChunkSlots * kNumEpilogueWarps * kNumChunkBytes <= static_cast<uint32_t>(
|
| 1268 |
+
reinterpret_cast<uint8_t*>(barrier_start_ptr) - smem_buffer));
|
| 1269 |
+
|
| 1270 |
+
// Per-warp buffer: 2 stage load buffers + 1 store buffer
|
| 1271 |
+
const auto combine_load_buffer = utils::PatternVisitor([&](const uint32_t& i) {
|
| 1272 |
+
return math::advance_ptr<uint4>(smem_buffer, (epilogue_warp_idx + i * kNumEpilogueWarps) * kNumChunkBytes);
|
| 1273 |
+
});
|
| 1274 |
+
const auto combine_store_buffer = math::advance_ptr<uint4>(smem_buffer, (epilogue_warp_idx + kNumEpilogueWarps * 2) * kNumChunkBytes);
|
| 1275 |
+
|
| 1276 |
+
// Per-warp barriers
|
| 1277 |
+
auto combine_load_barriers = utils::PatternVisitor([&](const uint32_t& i) {
|
| 1278 |
+
return combine_barriers[i + epilogue_warp_idx * 2];
|
| 1279 |
+
});
|
| 1280 |
+
|
| 1281 |
+
// Iterate over all tokens
|
| 1282 |
+
uint32_t combine_phase = 0;
|
| 1283 |
+
uint32_t load_stage_idx = 0;
|
| 1284 |
+
for (uint32_t token_idx = sm_idx * kNumEpilogueWarps + epilogue_warp_idx;
|
| 1285 |
+
token_idx < num_tokens;
|
| 1286 |
+
token_idx += kNumSMs * kNumEpilogueWarps) {
|
| 1287 |
+
// Read top-k slot indices: each lane reads one slot, then broadcast via exchange
|
| 1288 |
+
DG_STATIC_ASSERT(kNumTopk <= 32, "Invalid number of topk");
|
| 1289 |
+
const int stored_topk_slot_idx = lane_idx < kNumTopk ?
|
| 1290 |
+
static_cast<int>(__ldg(input_topk_idx_buffer.get_base_ptr<int64_t>() + token_idx * kNumTopk + lane_idx)) : -1;
|
| 1291 |
+
const uint32_t total_mask = __ballot_sync(0xffffffff, stored_topk_slot_idx >= 0);
|
| 1292 |
+
|
| 1293 |
+
// Iterate all chunks
|
| 1294 |
+
for (uint32_t chunk = 0; chunk < kNumChunks; ++ chunk) {
|
| 1295 |
+
const uint32_t chunk_byte_offset = chunk * kNumChunkBytes;
|
| 1296 |
+
|
| 1297 |
+
// Move mask and load
|
| 1298 |
+
uint32_t mask = total_mask;
|
| 1299 |
+
const auto move_mask_and_load = [&](const uint32_t& i) {
|
| 1300 |
+
if (mask) {
|
| 1301 |
+
// Move
|
| 1302 |
+
const uint32_t slot_idx = __ffs(mask) - 1;
|
| 1303 |
+
mask ^= 1 << slot_idx;
|
| 1304 |
+
|
| 1305 |
+
// Load
|
| 1306 |
+
if (cute::elect_one_sync()) {
|
| 1307 |
+
const auto src_ptr = math::advance_ptr<uint8_t>(
|
| 1308 |
+
combine_token_buffer.get_rank_buffer(slot_idx)
|
| 1309 |
+
.get_data_buffer(token_idx).get_base_ptr(),
|
| 1310 |
+
chunk_byte_offset);
|
| 1311 |
+
ptx::tma_load_1d(combine_load_buffer[i], src_ptr, combine_load_barriers[i], kNumChunkBytes);
|
| 1312 |
+
ptx::mbarrier_arrive_and_set_tx(combine_load_barriers[i], kNumChunkBytes);
|
| 1313 |
+
}
|
| 1314 |
+
__syncwarp();
|
| 1315 |
+
return true;
|
| 1316 |
+
}
|
| 1317 |
+
return false;
|
| 1318 |
+
};
|
| 1319 |
+
|
| 1320 |
+
// Load the first selection
|
| 1321 |
+
bool do_reduce = move_mask_and_load(load_stage_idx);
|
| 1322 |
+
|
| 1323 |
+
// Accumulate all top-k contributions for this chunk in float registers
|
| 1324 |
+
float2 reduced[kNumUint4PerLane * kNumElemsPerUint4] = {};
|
| 1325 |
+
while (do_reduce) {
|
| 1326 |
+
// Prefetch next top-k into the buffer while current is being accumulated
|
| 1327 |
+
do_reduce = move_mask_and_load(load_stage_idx ^ 1);
|
| 1328 |
+
|
| 1329 |
+
// Accumulate
|
| 1330 |
+
combine_load_barriers[load_stage_idx]->wait(combine_phase);
|
| 1331 |
+
#pragma unroll
|
| 1332 |
+
for (uint32_t j = 0; j < kNumUint4PerLane; ++ j) {
|
| 1333 |
+
const auto uint4_values = combine_load_buffer[load_stage_idx][j * 32 + lane_idx];
|
| 1334 |
+
const auto bf16_values = reinterpret_cast<const nv_bfloat162*>(&uint4_values);
|
| 1335 |
+
#pragma unroll
|
| 1336 |
+
for (uint32_t l = 0; l < kNumElemsPerUint4; ++ l)
|
| 1337 |
+
ptx::accumulate(reduced[j * kNumElemsPerUint4 + l], bf16_values[l]);
|
| 1338 |
+
}
|
| 1339 |
+
combine_phase ^= load_stage_idx;
|
| 1340 |
+
load_stage_idx ^= 1;
|
| 1341 |
+
}
|
| 1342 |
+
|
| 1343 |
+
// Cast
|
| 1344 |
+
#pragma unroll
|
| 1345 |
+
for (uint32_t j = 0; j < kNumUint4PerLane; ++ j) {
|
| 1346 |
+
uint4 casted;
|
| 1347 |
+
auto casted_bf16 = reinterpret_cast<nv_bfloat162*>(&casted);
|
| 1348 |
+
#pragma unroll
|
| 1349 |
+
for (uint32_t l = 0; l < kNumElemsPerUint4; ++ l)
|
| 1350 |
+
casted_bf16[l] = __float22bfloat162_rn(reduced[j * kNumElemsPerUint4 + l]);
|
| 1351 |
+
|
| 1352 |
+
// Wait share memory release and write
|
| 1353 |
+
if (j == 0) {
|
| 1354 |
+
ptx::tma_store_wait<0>();
|
| 1355 |
+
__syncwarp();
|
| 1356 |
+
}
|
| 1357 |
+
ptx::st_shared(combine_store_buffer + j * 32 + lane_idx,
|
| 1358 |
+
casted.x, casted.y, casted.z, casted.w);
|
| 1359 |
+
}
|
| 1360 |
+
__syncwarp();
|
| 1361 |
+
|
| 1362 |
+
// TMA store the token chunk
|
| 1363 |
+
if (cute::elect_one_sync()) {
|
| 1364 |
+
cute::tma_store_fence();
|
| 1365 |
+
ptx::tma_store_1d(
|
| 1366 |
+
math::advance_ptr(y, static_cast<uint64_t>(token_idx) * kNumHiddenBytes + chunk_byte_offset),
|
| 1367 |
+
combine_store_buffer, kNumChunkBytes);
|
| 1368 |
+
cute::tma_store_arrive();
|
| 1369 |
+
}
|
| 1370 |
+
__syncwarp();
|
| 1371 |
+
}
|
| 1372 |
+
}
|
| 1373 |
+
}
|
| 1374 |
+
#else
|
| 1375 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 1376 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 1377 |
+
#endif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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#pragma clang diagnostic push
|
| 3 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 4 |
+
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/epilogue_utils.cuh>
|
| 8 |
+
#include <deep_gemm/common/scheduler.cuh>
|
| 9 |
+
#include <deep_gemm/common/utils.cuh>
|
| 10 |
+
#include <deep_gemm/common/sm100_utils.cuh>
|
| 11 |
+
|
| 12 |
+
namespace deep_gemm {
|
| 13 |
+
|
| 14 |
+
using namespace deep_gemm::sm100;
|
| 15 |
+
|
| 16 |
+
template <cute::UMMA::Major kMajorA, cute::UMMA::Major kMajorB,
|
| 17 |
+
uint32_t kGranKA, uint32_t kGranKB,
|
| 18 |
+
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 19 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 20 |
+
uint32_t kNumGroups,
|
| 21 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleCDMode,
|
| 22 |
+
uint32_t kNumStages,
|
| 23 |
+
uint32_t kNumNonEpilogueThreads, uint32_t kNumEpilogueThreads,
|
| 24 |
+
uint32_t kNumMulticast, bool kIsMulticastOnA,
|
| 25 |
+
uint32_t kNumSMs,
|
| 26 |
+
GemmType kGemmType, bool kWithAccumulation,
|
| 27 |
+
typename a_dtype_t, typename b_dtype_t, typename cd_dtype_t,
|
| 28 |
+
typename epilogue_type_t>
|
| 29 |
+
__global__ void __launch_bounds__(kNumNonEpilogueThreads + kNumEpilogueThreads, 1)
|
| 30 |
+
sm100_fp8_gemm_1d1d_impl(int* grouped_layout,
|
| 31 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 32 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 33 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 34 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfa,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfb,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
| 37 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 38 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 39 |
+
using Allocator = cute::conditional_t<kNumMulticast == 1, cute::TMEM::Allocator1Sm, cute::TMEM::Allocator2Sm>;
|
| 40 |
+
|
| 41 |
+
// GEMM with accumulation must have FP32 output
|
| 42 |
+
if constexpr (kWithAccumulation)
|
| 43 |
+
DG_STATIC_ASSERT(cute::is_same_v<cd_dtype_t, float>, "Invalid C/D data dtype");
|
| 44 |
+
|
| 45 |
+
// Configs
|
| 46 |
+
constexpr uint32_t LAYOUT_AD_M = 128;
|
| 47 |
+
constexpr uint32_t WAVE_BLOCK_M = cute::min<uint32_t>(BLOCK_M, LAYOUT_AD_M);
|
| 48 |
+
constexpr uint32_t kNumMWaves = BLOCK_M / WAVE_BLOCK_M;
|
| 49 |
+
constexpr uint32_t kNumTMAStoreStages = 2;
|
| 50 |
+
constexpr uint32_t kNumUTCCPAlignedElems = 128;
|
| 51 |
+
DG_STATIC_ASSERT(BLOCK_K == 128, "Invalid block K");
|
| 52 |
+
DG_STATIC_ASSERT(BLOCK_M % WAVE_BLOCK_M == 0 and 2 % kNumMWaves == 0, "Invalid block M");
|
| 53 |
+
|
| 54 |
+
constexpr uint32_t kNumSFAStagesPerLoad = kGranKA == 32 ? 1 : 4;
|
| 55 |
+
constexpr uint32_t kNumSFBStagesPerLoad = kGranKB == 32 ? 1 : 4;
|
| 56 |
+
DG_STATIC_ASSERT(kGranKA == 32 or kGranKA == 128, "Invalid granularity K for A");
|
| 57 |
+
DG_STATIC_ASSERT(kGranKB == 32 or kGranKB == 128, "Invalid granularity K for B");
|
| 58 |
+
|
| 59 |
+
// Overwrite shape constants if the compiler gives
|
| 60 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 61 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 62 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 63 |
+
const uint32_t shape_sfa_k = ceil_div(shape_k, kGranKA * 4);
|
| 64 |
+
const uint32_t shape_sfb_k = ceil_div(shape_k, kGranKB * 4);
|
| 65 |
+
|
| 66 |
+
// Utils
|
| 67 |
+
bool is_leader_cta = cute::block_rank_in_cluster() == 0;
|
| 68 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 69 |
+
const auto lane_idx = get_lane_idx();
|
| 70 |
+
|
| 71 |
+
// Align to 1024 bytes for swizzle-128B
|
| 72 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 73 |
+
|
| 74 |
+
// 2-CTA MMA
|
| 75 |
+
constexpr uint32_t LOAD_BLOCK_M = BLOCK_M / (kIsMulticastOnA ? kNumMulticast: 1);
|
| 76 |
+
constexpr uint32_t LOAD_BLOCK_N = BLOCK_N / (kIsMulticastOnA ? 1 : kNumMulticast);
|
| 77 |
+
constexpr uint32_t STORE_BLOCK_M = cute::min<uint32_t>(BLOCK_M, LAYOUT_AD_M);
|
| 78 |
+
constexpr uint32_t STORE_BLOCK_N = kSwizzleCDMode / sizeof(cd_dtype_t);
|
| 79 |
+
constexpr uint32_t kNumUMMAStoreThreads = STORE_BLOCK_M;
|
| 80 |
+
DG_STATIC_ASSERT(not kIsMulticastOnA or kNumMulticast == 1, "Invalid multicast");
|
| 81 |
+
DG_STATIC_ASSERT(LOAD_BLOCK_M == BLOCK_M, "Only support tensor memory layout A/D");
|
| 82 |
+
DG_STATIC_ASSERT(kNumMulticast == 1 or kNumMulticast == 2, "Only support 1/2 multicast");
|
| 83 |
+
DG_STATIC_ASSERT(kNumUMMAStoreThreads % 32 == 0, "Invalid store block M");
|
| 84 |
+
|
| 85 |
+
// Share memory sizes
|
| 86 |
+
constexpr uint32_t SMEM_CD_SIZE_PER_STAGE = STORE_BLOCK_M * kSwizzleCDMode;
|
| 87 |
+
constexpr uint32_t SMEM_CD_SIZE = SMEM_CD_SIZE_PER_STAGE * kNumTMAStoreStages;
|
| 88 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = LOAD_BLOCK_M * BLOCK_K * sizeof(a_dtype_t);
|
| 89 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = LOAD_BLOCK_N * BLOCK_K * sizeof(b_dtype_t);
|
| 90 |
+
constexpr uint32_t SF_BLOCK_M = constexpr_align(BLOCK_M, kNumUTCCPAlignedElems);
|
| 91 |
+
constexpr uint32_t SF_BLOCK_N = constexpr_align(BLOCK_N, kNumUTCCPAlignedElems);
|
| 92 |
+
constexpr uint32_t SMEM_SFA_SIZE_PER_STAGE = SF_BLOCK_M * sizeof(uint32_t);
|
| 93 |
+
constexpr uint32_t SMEM_SFB_SIZE_PER_STAGE = SF_BLOCK_N * sizeof(uint32_t);
|
| 94 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % 1024 == 0 and SMEM_A_SIZE_PER_STAGE % 1024 == 0 and SMEM_B_SIZE_PER_STAGE % 1024 == 0,
|
| 95 |
+
"Shared memory of A/B must be aligned to 1024 bytes");
|
| 96 |
+
DG_STATIC_ASSERT(kNumTMAStoreStages >= 1, "Invalid number of TMA stages");
|
| 97 |
+
|
| 98 |
+
// NOTES: Make sure we have enough shared memory for UMMA padding
|
| 99 |
+
static constexpr uint32_t UMMA_A_SIZE_PER_STAGE = constexpr_align(LOAD_BLOCK_M, LAYOUT_AD_M) * BLOCK_K * sizeof(a_dtype_t);
|
| 100 |
+
DG_STATIC_ASSERT(UMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory Out of bound for UMMA");
|
| 101 |
+
|
| 102 |
+
// Automatically deduce the number of epilogue stages (1 or 2), according to the tensor memory size
|
| 103 |
+
// TODO: test cases of `kNumMWaves == 2 and kNumEpilogueStages == 2`
|
| 104 |
+
constexpr uint32_t kNumSFATmemCols = SF_BLOCK_M / 32;
|
| 105 |
+
constexpr uint32_t kNumSFBTmemCols = SF_BLOCK_N / 32;
|
| 106 |
+
constexpr uint32_t kNumEpilogueStages = (2 * kNumMWaves * BLOCK_N + kNumSFATmemCols + kNumSFBTmemCols) > 512 ? 1 : 2;
|
| 107 |
+
|
| 108 |
+
// Real tensor memory size and offsets
|
| 109 |
+
constexpr uint32_t kNumAccumTmemCols = kNumEpilogueStages * kNumMWaves * BLOCK_N;
|
| 110 |
+
constexpr uint32_t kNumTmemCols = get_num_aligned_tmem_cols<kNumAccumTmemCols + kNumSFATmemCols + kNumSFBTmemCols>();
|
| 111 |
+
constexpr uint32_t kTmemStartColOfSFA = kNumAccumTmemCols;
|
| 112 |
+
constexpr uint32_t kTmemStartColOfSFB = kNumAccumTmemCols + kNumSFATmemCols;
|
| 113 |
+
|
| 114 |
+
// Prefetch TMA descriptors at the very beginning
|
| 115 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 116 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 117 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 118 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfa);
|
| 119 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfb);
|
| 120 |
+
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// D/A/B shared memory
|
| 124 |
+
auto smem_cd = PatternVisitor([&](const uint32_t& i) {
|
| 125 |
+
return reinterpret_cast<cd_dtype_t*>(smem_buffer + i * SMEM_CD_SIZE_PER_STAGE);
|
| 126 |
+
});
|
| 127 |
+
auto smem_a = PatternVisitor([&](const uint32_t& i) {
|
| 128 |
+
return reinterpret_cast<a_dtype_t*>(smem_buffer + SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 129 |
+
});
|
| 130 |
+
auto smem_b = PatternVisitor([&](const uint32_t& i) {
|
| 131 |
+
return reinterpret_cast<b_dtype_t*>(smem_buffer + SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
// SFA/SFB shared memory
|
| 135 |
+
auto sf_start_ptr = smem_buffer + SMEM_CD_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 136 |
+
auto smem_sfa = PatternVisitor([=](const uint32_t& i) {
|
| 137 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + i * SMEM_SFA_SIZE_PER_STAGE);
|
| 138 |
+
});
|
| 139 |
+
auto smem_sfb = PatternVisitor([=](const uint32_t& i) {
|
| 140 |
+
return reinterpret_cast<uint32_t*>(sf_start_ptr + kNumStages * SMEM_SFA_SIZE_PER_STAGE + i * SMEM_SFB_SIZE_PER_STAGE);
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
// Fill barriers
|
| 144 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer +
|
| 145 |
+
SMEM_CD_SIZE +
|
| 146 |
+
kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE) +
|
| 147 |
+
kNumStages * (SMEM_SFA_SIZE_PER_STAGE + SMEM_SFB_SIZE_PER_STAGE));
|
| 148 |
+
auto full_barriers = PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 149 |
+
auto empty_barriers = PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 150 |
+
auto with_sf_full_barriers = PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 2 + i); });
|
| 151 |
+
auto tmem_full_barriers = PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 3 + i); });
|
| 152 |
+
auto tmem_empty_barriers = PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 3 + kNumEpilogueStages + i); });
|
| 153 |
+
|
| 154 |
+
// Fill the tensor memory pointer
|
| 155 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumStages * 3 + kNumEpilogueStages * 2);
|
| 156 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 157 |
+
|
| 158 |
+
if (kNumMulticast > 1)
|
| 159 |
+
cute::cluster_sync();
|
| 160 |
+
|
| 161 |
+
// Initialize barriers
|
| 162 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 163 |
+
#pragma unroll
|
| 164 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 165 |
+
// Arrive at all CTAs
|
| 166 |
+
full_barriers[i]->init(1);
|
| 167 |
+
empty_barriers[i]->init(1);
|
| 168 |
+
// Arrive only at the leader CTA
|
| 169 |
+
with_sf_full_barriers[i]->init(kNumMulticast * 32);
|
| 170 |
+
}
|
| 171 |
+
#pragma unroll
|
| 172 |
+
for (uint32_t i = 0; i < kNumEpilogueStages; ++ i) {
|
| 173 |
+
// Arrive at all CTAs
|
| 174 |
+
tmem_full_barriers[i]->init(1);
|
| 175 |
+
// Arrive only at the leader CTA
|
| 176 |
+
tmem_empty_barriers[i]->init(kNumMulticast * kNumUMMAStoreThreads);
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Make initialized barrier visible in async proxy
|
| 180 |
+
cutlass::arch::fence_barrier_init();
|
| 181 |
+
} else if (warp_idx == 2) {
|
| 182 |
+
// Allocate tensor memory
|
| 183 |
+
Allocator().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 184 |
+
}
|
| 185 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 186 |
+
|
| 187 |
+
// Block scheduler
|
| 188 |
+
uint32_t m_block_idx, n_block_idx;
|
| 189 |
+
auto scheduler = Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumMulticast, kIsMulticastOnA, kNumSMs>(shape_m, shape_n, shape_k, grouped_layout);
|
| 190 |
+
|
| 191 |
+
// Pipeline and TMA phases
|
| 192 |
+
uint32_t stage_idx = 0, phase = 0;
|
| 193 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 194 |
+
++ k_block_idx;
|
| 195 |
+
|
| 196 |
+
// Flip phases only if reach the next first stage
|
| 197 |
+
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
| 198 |
+
phase ^= stage_idx == 0;
|
| 199 |
+
};
|
| 200 |
+
|
| 201 |
+
// Dispatch warps into different roles
|
| 202 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 203 |
+
// TMA load warp
|
| 204 |
+
// Persistently schedule over blocks
|
| 205 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 206 |
+
const auto& num_total_k_blocks = ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 207 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 208 |
+
// Wait consumer release
|
| 209 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 210 |
+
|
| 211 |
+
// Compute offsets
|
| 212 |
+
// NOTES: the group is always concatenated with the outer dimension
|
| 213 |
+
uint32_t m_idx = scheduler.template get_global_idx<(kGemmType == GemmType::MGroupedMasked), IndexType::MN> (
|
| 214 |
+
shape_m, BLOCK_M, m_block_idx);
|
| 215 |
+
uint32_t n_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::K), IndexType::MN> (
|
| 216 |
+
shape_n, BLOCK_N, n_block_idx, m_block_idx);
|
| 217 |
+
|
| 218 |
+
// NOTES: `k_idx` is actually the k index default for K-major, while `k_b_idx` may be MN-major
|
| 219 |
+
// And for all m-grouped GEMMs, A must be K-majored
|
| 220 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous or kGemmType == GemmType::Batched or
|
| 221 |
+
kMajorA == cute::UMMA::Major::K, "Invalid major");
|
| 222 |
+
uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 223 |
+
uint32_t k_a_idx = scheduler.template get_global_idx<(kMajorA == cute::UMMA::Major::MN), IndexType::K> (
|
| 224 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 225 |
+
uint32_t k_b_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::MN), IndexType::K> (
|
| 226 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 227 |
+
|
| 228 |
+
// Add 2 CTA offsets
|
| 229 |
+
if constexpr (kNumMulticast > 1) {
|
| 230 |
+
m_idx += kIsMulticastOnA ? (cute::block_rank_in_cluster() * LOAD_BLOCK_M) : 0;
|
| 231 |
+
n_idx += kIsMulticastOnA ? 0 : (cute::block_rank_in_cluster() * LOAD_BLOCK_N);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
// Issue TMAs
|
| 235 |
+
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
| 236 |
+
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
| 237 |
+
if constexpr (kMajorA == cute::UMMA::Major::K)
|
| 238 |
+
tma_copy<BLOCK_K, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t, kIsBatchedMM>(
|
| 239 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_a_idx, m_idx, 1, batch_idx);
|
| 240 |
+
if constexpr (kMajorA == cute::UMMA::Major::MN)
|
| 241 |
+
tma_copy<LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode, a_dtype_t, kIsBatchedMM>(
|
| 242 |
+
&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], m_idx, k_a_idx, 1, batch_idx);
|
| 243 |
+
if constexpr (kMajorB == cute::UMMA::Major::K)
|
| 244 |
+
tma_copy<BLOCK_K, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t, kIsBatchedMM>(
|
| 245 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_b_idx, n_idx, 1, batch_idx);
|
| 246 |
+
if constexpr (kMajorB == cute::UMMA::Major::MN)
|
| 247 |
+
tma_copy<LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode, b_dtype_t, kIsBatchedMM>(
|
| 248 |
+
&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], n_idx, k_b_idx, 1, batch_idx);
|
| 249 |
+
auto num_arrival_bytes = SMEM_A_SIZE_PER_STAGE / (std::is_same_v<a_dtype_t, cutlass::float_e4m3_t> ? 1 : 2) +
|
| 250 |
+
SMEM_B_SIZE_PER_STAGE / (std::is_same_v<b_dtype_t, cutlass::float_e4m3_t> ? 1 : 2);
|
| 251 |
+
|
| 252 |
+
// Issue SFA and SFB TMAs at certain stages
|
| 253 |
+
// No swizzling, so one TMA for one SF is enough
|
| 254 |
+
if (k_block_idx % kNumSFAStagesPerLoad == 0) {
|
| 255 |
+
tma_copy<BLOCK_M, 1, 0>(&tensor_map_sfa, full_barriers[stage_idx], smem_sfa[stage_idx], m_block_idx * BLOCK_M,
|
| 256 |
+
scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), IndexType::SF_K>(shape_sfa_k, 1, ceil_div(k_idx, BLOCK_K * kNumSFAStagesPerLoad)));
|
| 257 |
+
num_arrival_bytes += BLOCK_M * sizeof(uint32_t);
|
| 258 |
+
}
|
| 259 |
+
if (k_block_idx % kNumSFBStagesPerLoad == 0) {
|
| 260 |
+
tma_copy<BLOCK_N, 1, 0>(&tensor_map_sfb, full_barriers[stage_idx], smem_sfb[stage_idx], n_block_idx * BLOCK_N,
|
| 261 |
+
scheduler.template get_global_idx<true, IndexType::SF_K>(shape_sfb_k, 1, ceil_div(k_idx, BLOCK_K * kNumSFBStagesPerLoad), m_block_idx));
|
| 262 |
+
num_arrival_bytes += BLOCK_N * sizeof(uint32_t);
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
// Arrive at full barriers
|
| 266 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(num_arrival_bytes);
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
} else if (warp_idx == 1 and is_leader_cta) {
|
| 270 |
+
// MMA issue warp
|
| 271 |
+
// NOTES: only the leader CTA will do this
|
| 272 |
+
// Make instruction descriptor
|
| 273 |
+
// TODO: refactor `UMMA_M` calculation
|
| 274 |
+
constexpr uint32_t UMMA_M = LAYOUT_AD_M * (kIsMulticastOnA ? 1 : kNumMulticast);
|
| 275 |
+
constexpr uint32_t UMMA_N = BLOCK_N * (kIsMulticastOnA ? kNumMulticast : 1);
|
| 276 |
+
constexpr uint32_t UMMA_K = 32;
|
| 277 |
+
auto instr_desc = cute::UMMA::make_instr_desc_block_scaled<a_dtype_t, b_dtype_t, float, cutlass::float_ue8m0_t,
|
| 278 |
+
UMMA_M, UMMA_N, kMajorA, kMajorB>();
|
| 279 |
+
auto sf_desc = make_sf_desc(nullptr);
|
| 280 |
+
|
| 281 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 282 |
+
auto a_desc = make_umma_desc<kMajorA, LOAD_BLOCK_M, BLOCK_K, kSwizzleAMode>(smem_a[0], 0, 0);
|
| 283 |
+
auto b_desc = make_umma_desc<kMajorB, LOAD_BLOCK_N, BLOCK_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 284 |
+
uint32_t a_desc_lo = lane_idx < kNumStages ? a_desc.lo + lane_idx * SMEM_A_SIZE_PER_STAGE / 16 : 0u;
|
| 285 |
+
uint32_t b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 286 |
+
|
| 287 |
+
// Checks for MMA instructions
|
| 288 |
+
// NOTES: CUTLASS does not have such checks except the MMA traits, but we are not using these traits
|
| 289 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 290 |
+
(UMMA_M == 128 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256) or
|
| 291 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 292 |
+
"Invalid MMA instruction shape");
|
| 293 |
+
|
| 294 |
+
// Persistently schedule over blocks
|
| 295 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 296 |
+
// Wait tensor memory empty barrier arrival
|
| 297 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 298 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 299 |
+
tmem_empty_barriers[accum_stage_idx]->wait(accum_phase_idx ^ 1);
|
| 300 |
+
tcgen05_after_thread_sync();
|
| 301 |
+
|
| 302 |
+
// Empty barrier arrival
|
| 303 |
+
auto empty_barrier_arrive = [&](const bool& do_tmem_full_arrive) {
|
| 304 |
+
auto umma_arrive = [](const uint64_t* barrier) {
|
| 305 |
+
if constexpr (kNumMulticast == 1) {
|
| 306 |
+
cutlass::arch::umma_arrive(barrier);
|
| 307 |
+
} else {
|
| 308 |
+
constexpr uint16_t kCTAMask = (1 << kNumMulticast) - 1;
|
| 309 |
+
cutlass::arch::umma_arrive_multicast_2x1SM(barrier, kCTAMask);
|
| 310 |
+
}
|
| 311 |
+
};
|
| 312 |
+
umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 313 |
+
|
| 314 |
+
// NOTES: the tensor memory accumulator pipeline has nothing to do with multicasting
|
| 315 |
+
if (do_tmem_full_arrive)
|
| 316 |
+
umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barriers[accum_stage_idx]));
|
| 317 |
+
};
|
| 318 |
+
|
| 319 |
+
// Launch MMAs
|
| 320 |
+
const auto& num_total_k_blocks = ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 321 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 322 |
+
// Wait TMA and SF-transpose arrival
|
| 323 |
+
with_sf_full_barriers[stage_idx]->wait(phase);
|
| 324 |
+
tcgen05_after_thread_sync();
|
| 325 |
+
|
| 326 |
+
// Do SF copy at certain stages
|
| 327 |
+
// NOTES: CUTLASS UTCCP's interface does not have `elect_one_sync`, we must do it by ourselves
|
| 328 |
+
// TODO: process shared memory descriptor by addition
|
| 329 |
+
using cute_utccp_t = cute::conditional_t<kNumMulticast == 1,
|
| 330 |
+
cute::SM100_UTCCP_4x32dp128bit_1cta, cute::SM100_UTCCP_4x32dp128bit_2cta>;
|
| 331 |
+
const uint32_t sfa_stage_in_group_idx = k_block_idx % kNumSFAStagesPerLoad;
|
| 332 |
+
if (sfa_stage_in_group_idx == 0 and cute::elect_one_sync()) {
|
| 333 |
+
#pragma unroll
|
| 334 |
+
for (uint32_t i = 0; i < SF_BLOCK_M / kNumUTCCPAlignedElems; ++ i) {
|
| 335 |
+
auto smem_ptr = smem_sfa[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 336 |
+
replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 337 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFA + i * 4);
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
const uint32_t sfb_stage_in_group_idx = k_block_idx % kNumSFBStagesPerLoad;
|
| 341 |
+
if (sfb_stage_in_group_idx == 0 and cute::elect_one_sync()) {
|
| 342 |
+
#pragma unroll
|
| 343 |
+
for (uint32_t i = 0; i < SF_BLOCK_N / kNumUTCCPAlignedElems; ++ i) {
|
| 344 |
+
auto smem_ptr = smem_sfb[stage_idx] + i * kNumUTCCPAlignedElems;
|
| 345 |
+
replace_smem_desc_addr(sf_desc, smem_ptr);
|
| 346 |
+
cute_utccp_t::copy(sf_desc, kTmemStartColOfSFB + i * 4);
|
| 347 |
+
}
|
| 348 |
+
}
|
| 349 |
+
__syncwarp();
|
| 350 |
+
|
| 351 |
+
// Issue UMMA in the leader CTA
|
| 352 |
+
using mma_t = cute::conditional_t<kNumMulticast == 1, SM100_MMA_MXF8F6F4_SS, SM100_MMA_MXF8F6F4_2x1SM_SS>;
|
| 353 |
+
const auto& a_desc_base_lo = __shfl_sync(0xffffffff, a_desc_lo, static_cast<int>(stage_idx));
|
| 354 |
+
const auto& b_desc_base_lo = __shfl_sync(0xffffffff, b_desc_lo, static_cast<int>(stage_idx));
|
| 355 |
+
if (cute::elect_one_sync()) {
|
| 356 |
+
#pragma unroll
|
| 357 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 358 |
+
const uint32_t sfa_id = (kGranKA == 32 ? k : sfa_stage_in_group_idx);
|
| 359 |
+
const uint32_t sfb_id = (kGranKB == 32 ? k : sfb_stage_in_group_idx);
|
| 360 |
+
const auto& runtime_instr_desc = make_runtime_instr_desc_with_sf_id(instr_desc, sfa_id, sfb_id);
|
| 361 |
+
|
| 362 |
+
b_desc.lo = advance_umma_desc_lo<kMajorB, LOAD_BLOCK_N, kSwizzleBMode, b_dtype_t>(b_desc_base_lo, 0, k * UMMA_K);
|
| 363 |
+
#pragma unroll
|
| 364 |
+
for (uint32_t w = 0; w < kNumMWaves; ++ w) {
|
| 365 |
+
DG_STATIC_ASSERT((WAVE_BLOCK_M * BLOCK_K) % 128 == 0, "Invalid swizzling offset");
|
| 366 |
+
a_desc.lo = advance_umma_desc_lo<kMajorA, LOAD_BLOCK_M, kSwizzleAMode, a_dtype_t>(a_desc_base_lo, w * WAVE_BLOCK_M * BLOCK_K, k * UMMA_K);
|
| 367 |
+
mma_t::fma(a_desc, b_desc,
|
| 368 |
+
accum_stage_idx * kNumMWaves * BLOCK_N + w * BLOCK_N,
|
| 369 |
+
k_block_idx > 0 or k > 0,
|
| 370 |
+
runtime_instr_desc,
|
| 371 |
+
kTmemStartColOfSFA + w * (kNumUTCCPAlignedElems / 32),
|
| 372 |
+
kTmemStartColOfSFB);
|
| 373 |
+
}
|
| 374 |
+
}
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
// Commit to the mbarrier object
|
| 378 |
+
// No explicit `tcgen05.fence::before_thread_sync` is needed, as this is implicitly performed by `tcgen05.commit`
|
| 379 |
+
empty_barrier_arrive(k_block_idx == num_total_k_blocks - 1);
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
// To safely deconstruct barriers, we need another round of waits
|
| 384 |
+
const auto& iter_idx = scheduler.current_iter - 1;
|
| 385 |
+
if (kNumMulticast > 1 and iter_idx >= 0) {
|
| 386 |
+
const auto& accum_phase_idx = (iter_idx / kNumEpilogueStages) & 1;
|
| 387 |
+
tmem_empty_barriers[iter_idx % kNumEpilogueStages]->wait(accum_phase_idx);
|
| 388 |
+
}
|
| 389 |
+
} else if (warp_idx == 2) {
|
| 390 |
+
// UTCCP transposer
|
| 391 |
+
auto utccp_required_smem_warp_transpose = [&](const uint32_t* smem_ptr) {
|
| 392 |
+
DG_STATIC_ASSERT(kNumUTCCPAlignedElems == 128, "Invalid aligned elements");
|
| 393 |
+
uint32_t values[4];
|
| 394 |
+
#pragma unroll
|
| 395 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 396 |
+
values[i] = ld_shared(smem_ptr + (i ^ (lane_idx >> 3)) * 32 + lane_idx);
|
| 397 |
+
__syncwarp();
|
| 398 |
+
#pragma unroll
|
| 399 |
+
for (uint32_t i = 0; i < 4; ++ i)
|
| 400 |
+
st_shared(smem_ptr + lane_idx * 4 + (i ^ (lane_idx >> 3)), values[i]);
|
| 401 |
+
};
|
| 402 |
+
|
| 403 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 404 |
+
const auto& num_total_k_blocks = ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 405 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 406 |
+
// Wait TMA arrival
|
| 407 |
+
full_barriers[stage_idx]->wait(phase);
|
| 408 |
+
|
| 409 |
+
// Transpose for UTCCP at certain stages
|
| 410 |
+
if (k_block_idx % kNumSFAStagesPerLoad == 0) {
|
| 411 |
+
#pragma unroll
|
| 412 |
+
for (uint32_t i = 0; i < SF_BLOCK_M / kNumUTCCPAlignedElems; ++ i)
|
| 413 |
+
utccp_required_smem_warp_transpose(smem_sfa[stage_idx] + i * kNumUTCCPAlignedElems);
|
| 414 |
+
// TODO: figure out whether the proxy fence is valid for 2-CTA cases
|
| 415 |
+
cutlass::arch::fence_view_async_shared();
|
| 416 |
+
}
|
| 417 |
+
if (k_block_idx % kNumSFBStagesPerLoad == 0) {
|
| 418 |
+
#pragma unroll
|
| 419 |
+
for (uint32_t i = 0; i < SF_BLOCK_N / kNumUTCCPAlignedElems; ++ i)
|
| 420 |
+
utccp_required_smem_warp_transpose(smem_sfb[stage_idx] + i * kNumUTCCPAlignedElems);
|
| 421 |
+
// TODO: figure out whether the proxy fence is valid for 2-CTA cases
|
| 422 |
+
cutlass::arch::fence_view_async_shared();
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
// Arrive
|
| 426 |
+
with_sf_full_barriers[stage_idx]->arrive(0u);
|
| 427 |
+
}
|
| 428 |
+
}
|
| 429 |
+
} else if (warp_idx >= kNumNonEpilogueThreads / 32 and warp_idx < (kNumNonEpilogueThreads + kNumUMMAStoreThreads) / 32) {
|
| 430 |
+
// Epilogue warp groups
|
| 431 |
+
const auto epilogue_warp_idx = warp_idx - (kNumNonEpilogueThreads / 32);
|
| 432 |
+
|
| 433 |
+
// NOTES: tensor memory addresses are simplified, as the hardware will ignore the warp index bits,
|
| 434 |
+
// i.e., no need for `tmem_ptr |= (epilogue_warp_idx * 32) << 16`.
|
| 435 |
+
// NOTES: we also forbid two CTAs to share the same SM and its tensor memory
|
| 436 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ld_shared(tmem_ptr_in_smem) == 0);
|
| 437 |
+
|
| 438 |
+
// TMA checks
|
| 439 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 440 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(cd_dtype_t);
|
| 441 |
+
DG_STATIC_ASSERT(kSwizzleCDMode > 0, "TMA D must be swizzled");
|
| 442 |
+
DG_STATIC_ASSERT(STORE_BLOCK_N % kNumElemsPerBankGroup == 0, "Invalid swizzling");
|
| 443 |
+
|
| 444 |
+
// Share store pipeline between blocks
|
| 445 |
+
uint32_t tma_stage_idx = 0;
|
| 446 |
+
auto advance_store_pipeline = [&]() {
|
| 447 |
+
tma_stage_idx = (tma_stage_idx + 1) % kNumTMAStoreStages;
|
| 448 |
+
};
|
| 449 |
+
|
| 450 |
+
// Persistently schedule over blocks
|
| 451 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 452 |
+
auto accum_stage_idx = scheduler.current_iter % kNumEpilogueStages;
|
| 453 |
+
auto accum_phase_idx = (scheduler.current_iter / kNumEpilogueStages) & 1;
|
| 454 |
+
|
| 455 |
+
// Wait UMMA arrival
|
| 456 |
+
tmem_full_barriers[accum_stage_idx]->wait(accum_phase_idx);
|
| 457 |
+
tcgen05_after_thread_sync();
|
| 458 |
+
|
| 459 |
+
// Load from tensor memory into registers, and write shared memory with STSM
|
| 460 |
+
DG_STATIC_ASSERT(kNumEpilogueThreads == 128, "Epilogue threads not enough");
|
| 461 |
+
DG_STATIC_ASSERT(BLOCK_N % STORE_BLOCK_N == 0, "Invalid block sizes");
|
| 462 |
+
|
| 463 |
+
// Iterate over M waves
|
| 464 |
+
#pragma unroll
|
| 465 |
+
for (uint32_t w = 0; w < kNumMWaves; ++ w) {
|
| 466 |
+
// Issue every swizzled atom and pipeline STSM and TMA store
|
| 467 |
+
constexpr uint32_t kNumStores = BLOCK_N / STORE_BLOCK_N;
|
| 468 |
+
#pragma unroll
|
| 469 |
+
for (uint32_t s = 0; s < kNumStores; ++ s, advance_store_pipeline()) {
|
| 470 |
+
// Wait shared memory to be released
|
| 471 |
+
if (epilogue_warp_idx == 0)
|
| 472 |
+
cute::tma_store_wait<kNumTMAStoreStages - 1>();
|
| 473 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 474 |
+
|
| 475 |
+
// The pipeline stage
|
| 476 |
+
const auto m_idx = scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), IndexType::MN>(shape_m, BLOCK_M, m_block_idx) + w * WAVE_BLOCK_M;
|
| 477 |
+
const auto n_idx = epilogue_type_t::apply_index_n<STORE_BLOCK_N>(n_block_idx * BLOCK_N + s * STORE_BLOCK_N);
|
| 478 |
+
|
| 479 |
+
// Store into shared memory
|
| 480 |
+
#pragma unroll
|
| 481 |
+
for (uint32_t i = 0; i < STORE_BLOCK_N / kNumElemsPerBankGroup; ++ i) {
|
| 482 |
+
// Calculate the index of the bank group to be written in the atom
|
| 483 |
+
auto bank_group_index = i + lane_idx * (kSwizzleCDMode / kNumBankGroupBytes);
|
| 484 |
+
|
| 485 |
+
// Reshape the atom in another view and swizzle
|
| 486 |
+
// - original: `(LAYOUT_AD_M, kSwizzleCDMode / kNumBankGroupBytes)`
|
| 487 |
+
// - new: `(LAYOUT_AD_M * kSwizzleCDMode / kNumBankGroupBytes / 8, 8)`
|
| 488 |
+
// NOTES: "8" is the number of bank groups, "16" is the swizzling pattern
|
| 489 |
+
constexpr bool kHasShortcut = (kSwizzleCDMode / kNumBankGroupBytes) == 8;
|
| 490 |
+
auto row = kHasShortcut ? (i / 8 + lane_idx) : (bank_group_index / 8);
|
| 491 |
+
auto col = kHasShortcut ? (i) : (bank_group_index % 8);
|
| 492 |
+
col ^= row % (kSwizzleCDMode / 16);
|
| 493 |
+
|
| 494 |
+
// Source and destination memory address
|
| 495 |
+
uint32_t tmem_addr = accum_stage_idx * kNumMWaves * BLOCK_N + // Accumulator offset
|
| 496 |
+
w * BLOCK_N + // Wave offset
|
| 497 |
+
s * STORE_BLOCK_N + i * kNumElemsPerBankGroup; // In-block offset
|
| 498 |
+
auto smem_ptr = reinterpret_cast<uint8_t*>(smem_cd[tma_stage_idx]) + // Base pointer
|
| 499 |
+
epilogue_warp_idx * 32 * kSwizzleCDMode + // Warp offset
|
| 500 |
+
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
| 501 |
+
|
| 502 |
+
// Load from tensor memory, store into shared memory
|
| 503 |
+
uint32_t values[kNumElemsPerBankGroup];
|
| 504 |
+
if constexpr (cute::is_same_v<cd_dtype_t, float>) {
|
| 505 |
+
// For FP32 output, read and store
|
| 506 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 4, "Invalid type");
|
| 507 |
+
cute::SM100_TMEM_LOAD_32dp32b4x::copy(tmem_addr,
|
| 508 |
+
values[0], values[1], values[2], values[3]);
|
| 509 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 510 |
+
st_shared(smem_ptr, values[0], values[1], values[2], values[3]);
|
| 511 |
+
} else {
|
| 512 |
+
// For BF16 output, read, cast and store
|
| 513 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 8 and cute::is_same_v<cd_dtype_t, cutlass::bfloat16_t>, "Invalid type");
|
| 514 |
+
cute::SM100_TMEM_LOAD_32dp32b8x::copy(tmem_addr,
|
| 515 |
+
values[0], values[1], values[2], values[3],
|
| 516 |
+
values[4], values[5], values[6], values[7]);
|
| 517 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 518 |
+
st_shared(smem_ptr,
|
| 519 |
+
cast_into_bf16_and_pack(values[0], values[1]),
|
| 520 |
+
cast_into_bf16_and_pack(values[2], values[3]),
|
| 521 |
+
cast_into_bf16_and_pack(values[4], values[5]),
|
| 522 |
+
cast_into_bf16_and_pack(values[6], values[7]));
|
| 523 |
+
}
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
// Notify tensor memory empty (only at the leader CTA) arrival ASAP
|
| 527 |
+
// NOTES: only the last stage needs to do this
|
| 528 |
+
if (w == kNumMWaves - 1 and s == BLOCK_N / STORE_BLOCK_N - 1) {
|
| 529 |
+
tcgen05_before_thread_sync();
|
| 530 |
+
tmem_empty_barriers[accum_stage_idx]->arrive(0u);
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
// Synchronize all threads and issue TMA
|
| 534 |
+
cute::tma_store_fence();
|
| 535 |
+
cutlass::arch::NamedBarrier::sync(kNumUMMAStoreThreads, 0);
|
| 536 |
+
if (epilogue_warp_idx == 0 and cute::elect_one_sync()) {
|
| 537 |
+
if constexpr (kGemmType == GemmType::Batched) {
|
| 538 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 539 |
+
cute::SM90_TMA_REDUCE_ADD_3D, cute::SM90_TMA_STORE_3D>;
|
| 540 |
+
cute_tma_t::copy(&tensor_map_cd, smem_cd[tma_stage_idx],
|
| 541 |
+
n_idx, m_idx, scheduler.current_group_idx);
|
| 542 |
+
} else {
|
| 543 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 544 |
+
cute::SM90_TMA_REDUCE_ADD_2D, cute::SM90_TMA_STORE_2D>;
|
| 545 |
+
cute_tma_t::copy(&tensor_map_cd, smem_cd[tma_stage_idx], n_idx, m_idx);
|
| 546 |
+
}
|
| 547 |
+
cute::tma_store_arrive();
|
| 548 |
+
}
|
| 549 |
+
}
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
// Deallocate tensor memory
|
| 555 |
+
kNumMulticast > 1 ? cute::cluster_sync() : __syncthreads();
|
| 556 |
+
if (warp_idx == 0)
|
| 557 |
+
Allocator().free(0, kNumTmemCols);
|
| 558 |
+
|
| 559 |
+
#else
|
| 560 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 561 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 562 |
+
#endif
|
| 563 |
+
}
|
| 564 |
+
|
| 565 |
+
}; // namespace deep_gemm
|
| 566 |
+
|
| 567 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 5 |
+
|
| 6 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 7 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 10 |
+
#include <deep_gemm/common/math.cuh>
|
| 11 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 12 |
+
#include <deep_gemm/common/utils.cuh>
|
| 13 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 17 |
+
|
| 18 |
+
namespace deep_gemm {
|
| 19 |
+
|
| 20 |
+
template <uint32_t kNumHeads, uint32_t kHeadDim,
|
| 21 |
+
bool kIsCompressedLogits,
|
| 22 |
+
uint32_t BLOCK_Q, uint32_t BLOCK_KV,
|
| 23 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 24 |
+
uint32_t kNumSMs,
|
| 25 |
+
uint32_t kNumSpecializedThreads, uint32_t kNumMathThreads,
|
| 26 |
+
typename logits_dtype_t,
|
| 27 |
+
uint32_t kNumMathWarpGroups = kNumMathThreads / 128>
|
| 28 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumSpecializedThreads + kNumMathThreads, 1)
|
| 29 |
+
void sm100_fp8_mqa_logits(const uint32_t seq_len, const uint32_t seq_len_kv,
|
| 30 |
+
const uint32_t max_seqlen_k, const uint32_t stride_logits,
|
| 31 |
+
uint32_t* cu_seq_len_k_start,
|
| 32 |
+
uint32_t* cu_seq_len_k_end,
|
| 33 |
+
logits_dtype_t* logits,
|
| 34 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv_scales,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 38 |
+
// TODO: consider TMA multicast
|
| 39 |
+
// Normally, `h (kNumHeads) == 32` and `d (kHeadDim) == 64`
|
| 40 |
+
// For one block, we process `[q_start:q_end, h, d] @ [kv_start:kv_end, d] -> [q_start:q_end, kv_start:kv_end]`
|
| 41 |
+
// Q should be load only at once for a block
|
| 42 |
+
const auto num_q_blocks = math::ceil_div(seq_len, BLOCK_Q);
|
| 43 |
+
|
| 44 |
+
// Types
|
| 45 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 46 |
+
|
| 47 |
+
// Utils
|
| 48 |
+
const auto sm_idx = blockIdx.x;
|
| 49 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 50 |
+
const auto warpgroup_idx = warp_idx / 4;
|
| 51 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 52 |
+
constexpr uint32_t kSpecWarpStart = kNumMathWarpGroups * 4;
|
| 53 |
+
|
| 54 |
+
// Prefetch TMA descriptors
|
| 55 |
+
DG_STATIC_ASSERT(kNumSpecializedThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 56 |
+
if (warp_idx == kSpecWarpStart) {
|
| 57 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 58 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 59 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv_scales);
|
| 60 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Shared memory configs
|
| 64 |
+
// NOTES: weight may be unaligned
|
| 65 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 66 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * sizeof(float);
|
| 67 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = BLOCK_KV * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 68 |
+
static constexpr uint32_t SMEM_KV_SCALE_SIZE_PER_STAGE = BLOCK_KV * sizeof(float);
|
| 69 |
+
static constexpr uint32_t ALIGNED_SMEM_KV_SCALE_SIZE_PER_STAGE = math::constexpr_align(SMEM_KV_SCALE_SIZE_PER_STAGE, 512u);
|
| 70 |
+
|
| 71 |
+
// Align to 512 bytes for swizzle-64B
|
| 72 |
+
extern __shared__ __align__(512) uint8_t smem_buffer[];
|
| 73 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % 512 == 0, "Unaligned TMA swizzling");
|
| 74 |
+
DG_STATIC_ASSERT(SMEM_WEIGHT_SIZE_PER_STAGE % 512 == 0, "Unaligned TMA swizzling");
|
| 75 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % 512 == 0, "Unaligned TMA swizzling");
|
| 76 |
+
|
| 77 |
+
// TMA configs
|
| 78 |
+
constexpr uint32_t kNumTmemCols = BLOCK_Q * kNumHeads * kNumMathWarpGroups;
|
| 79 |
+
DG_STATIC_ASSERT(kNumTmemCols <= 512, "Too many tensor memory");
|
| 80 |
+
|
| 81 |
+
// Data on shared memory
|
| 82 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 83 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer +
|
| 84 |
+
SMEM_Q_SIZE_PER_STAGE * i);
|
| 85 |
+
});
|
| 86 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 87 |
+
return reinterpret_cast<float*>(smem_buffer +
|
| 88 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 89 |
+
});
|
| 90 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 91 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + (
|
| 92 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_WEIGHT_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * i));
|
| 93 |
+
});
|
| 94 |
+
auto smem_kv_scales = utils::PatternVisitor([&](const uint32_t& i) {
|
| 95 |
+
return reinterpret_cast<float*>(smem_buffer +
|
| 96 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_WEIGHT_SIZE_PER_STAGE * kNumQStages +
|
| 97 |
+
SMEM_KV_SIZE_PER_STAGE * kNumKVStages + ALIGNED_SMEM_KV_SCALE_SIZE_PER_STAGE * i);
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
// TMA barriers
|
| 101 |
+
auto barrier_ptr = reinterpret_cast<Barrier*>(smem_kv_scales[kNumKVStages]);
|
| 102 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + i; });
|
| 103 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages + i); });
|
| 104 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + i); });
|
| 105 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + kNumKVStages + i); });
|
| 106 |
+
auto full_umma_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + kNumKVStages * 2 + i); });
|
| 107 |
+
auto empty_umma_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + kNumKVStages * 2 + kNumMathWarpGroups + i); });
|
| 108 |
+
|
| 109 |
+
// Tensor memory allocation
|
| 110 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_ptr + kNumQStages * 2 + kNumKVStages * 2 + kNumMathWarpGroups * 2);
|
| 111 |
+
|
| 112 |
+
// Initialize barriers
|
| 113 |
+
DG_STATIC_ASSERT(kNumSpecializedThreads % 128 == 0 and kNumSpecializedThreads >= 64, "Invalid threads");
|
| 114 |
+
if (warp_idx == kSpecWarpStart and cute::elect_one_sync()) {
|
| 115 |
+
#pragma unroll
|
| 116 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 117 |
+
full_q_barriers[i]->init(1);
|
| 118 |
+
empty_q_barriers[i]->init(kNumMathThreads + 32);
|
| 119 |
+
}
|
| 120 |
+
#pragma unroll
|
| 121 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 122 |
+
full_kv_barriers[i]->init(1);
|
| 123 |
+
empty_kv_barriers[i]->init(kNumMathThreads);
|
| 124 |
+
}
|
| 125 |
+
cutlass::arch::fence_barrier_init();
|
| 126 |
+
}
|
| 127 |
+
if (warp_idx == kSpecWarpStart + 1) {
|
| 128 |
+
if (cute::elect_one_sync()) {
|
| 129 |
+
#pragma unroll
|
| 130 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++ i) {
|
| 131 |
+
full_umma_barriers[i]->init(1);
|
| 132 |
+
empty_umma_barriers[i]->init(128);
|
| 133 |
+
}
|
| 134 |
+
cutlass::arch::fence_barrier_init();
|
| 135 |
+
}
|
| 136 |
+
// Allocate tensor memory
|
| 137 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 138 |
+
}
|
| 139 |
+
__syncthreads();
|
| 140 |
+
|
| 141 |
+
// Register reconfigurations
|
| 142 |
+
constexpr uint32_t kNumSpecializedRegisters = 40;
|
| 143 |
+
constexpr uint32_t kNumMathRegisters = 232;
|
| 144 |
+
|
| 145 |
+
// Block scheduler
|
| 146 |
+
uint32_t block_q_idx = sm_idx, q_iter_idx = 0;
|
| 147 |
+
const auto get_next_block_q_idx = [&]() -> cute::tuple<uint32_t, uint32_t> {
|
| 148 |
+
return {block_q_idx + kNumSMs, q_iter_idx + 1};
|
| 149 |
+
};
|
| 150 |
+
uint32_t seq_k_start[BLOCK_Q], seq_k_end[BLOCK_Q];
|
| 151 |
+
const auto load_schedule = [&](const uint32_t& q_iter_offset = 0) -> cute::tuple<uint32_t, uint32_t, uint32_t, uint32_t> {
|
| 152 |
+
uint32_t start = cute::numeric_limits<uint32_t>::max();
|
| 153 |
+
uint32_t end = cute::numeric_limits<uint32_t>::min();
|
| 154 |
+
|
| 155 |
+
#pragma unroll
|
| 156 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 157 |
+
const auto q_idx = min(block_q_idx * BLOCK_Q + i, seq_len - 1);
|
| 158 |
+
seq_k_start[i] = cu_seq_len_k_start[q_idx];
|
| 159 |
+
seq_k_end[i] = cu_seq_len_k_end[q_idx];
|
| 160 |
+
start = min(start, min(seq_k_start[i], seq_len_kv));
|
| 161 |
+
end = max(end, min(seq_k_end[i], seq_len_kv));
|
| 162 |
+
}
|
| 163 |
+
// TMA alignment requirements for SF KV
|
| 164 |
+
start = start / 4 * 4;
|
| 165 |
+
return {(q_iter_idx + q_iter_offset) % kNumQStages, // Q pipeline stage
|
| 166 |
+
((q_iter_idx + q_iter_offset) / kNumQStages) & 1, // Q pipeline phase
|
| 167 |
+
start, math::ceil_div(end - start, BLOCK_KV)}; // Task info
|
| 168 |
+
};
|
| 169 |
+
|
| 170 |
+
// KV pipeline
|
| 171 |
+
uint32_t num_total_kv_blocks = 0;
|
| 172 |
+
const auto get_kv_pipeline = [&](const uint32_t& kv_block_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 173 |
+
return {
|
| 174 |
+
(num_total_kv_blocks + kv_block_idx) % kNumKVStages, // KV pipeline stage
|
| 175 |
+
((num_total_kv_blocks + kv_block_idx) / kNumKVStages) & 1 // KV pipeline phase
|
| 176 |
+
};
|
| 177 |
+
};
|
| 178 |
+
|
| 179 |
+
// UMMA settings
|
| 180 |
+
// Construct instruction with layout D
|
| 181 |
+
constexpr uint32_t UMMA_M = 128;
|
| 182 |
+
constexpr uint32_t UMMA_K = 32 / sizeof(cutlass::float_e4m3_t);
|
| 183 |
+
constexpr uint32_t UMMA_N = BLOCK_Q * kNumHeads;
|
| 184 |
+
|
| 185 |
+
// Wait for primary kernel completion
|
| 186 |
+
cudaGridDependencySynchronize();
|
| 187 |
+
|
| 188 |
+
if (warp_idx == kSpecWarpStart) {
|
| 189 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 190 |
+
|
| 191 |
+
// Prefetch
|
| 192 |
+
const auto issue_tma_q = [&](const uint32_t& stage_idx, const auto& block_idx) {
|
| 193 |
+
tma::copy<kHeadDim, BLOCK_Q * kNumHeads, kHeadDim>(&tensor_map_q, full_q_barriers[stage_idx], smem_q[stage_idx], 0, block_idx * BLOCK_Q * kNumHeads);
|
| 194 |
+
tma::copy<kNumHeads, BLOCK_Q, 0>(&tensor_map_weights, full_q_barriers[stage_idx], smem_weights[stage_idx], 0, block_idx * BLOCK_Q);
|
| 195 |
+
full_q_barriers[stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 196 |
+
};
|
| 197 |
+
if (cute::elect_one_sync() and block_q_idx < num_q_blocks)
|
| 198 |
+
issue_tma_q(0, block_q_idx);
|
| 199 |
+
|
| 200 |
+
// Only the first lane persistently schedules over blocks
|
| 201 |
+
if (cute::elect_one_sync()) {
|
| 202 |
+
while (block_q_idx < num_q_blocks) {
|
| 203 |
+
CUTE_TIE_DECL(load_schedule(1), q_stage_idx, q_phase, kv_start, num_kv_blocks);
|
| 204 |
+
|
| 205 |
+
// Wait Q consumer release
|
| 206 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 207 |
+
|
| 208 |
+
// Issue TMA Q
|
| 209 |
+
if (const auto& next_block_q_idx = cute::get<0>(get_next_block_q_idx()); next_block_q_idx < num_q_blocks)
|
| 210 |
+
issue_tma_q(q_stage_idx, next_block_q_idx);
|
| 211 |
+
|
| 212 |
+
// Issue TMA KV
|
| 213 |
+
#pragma unroll
|
| 214 |
+
for (uint32_t kv_block_idx = 0; kv_block_idx < num_kv_blocks; ++ kv_block_idx) {
|
| 215 |
+
// Wait consumer release
|
| 216 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_block_idx), kv_stage_idx, kv_phase);
|
| 217 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 218 |
+
|
| 219 |
+
// Issue TMA KV
|
| 220 |
+
tma::copy<kHeadDim, BLOCK_KV, kHeadDim>(&tensor_map_kv, full_kv_barriers[kv_stage_idx],
|
| 221 |
+
smem_kv[kv_stage_idx], 0, kv_start + kv_block_idx * BLOCK_KV);
|
| 222 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_kv_scales, full_kv_barriers[kv_stage_idx],
|
| 223 |
+
smem_kv_scales[kv_stage_idx], kv_start + kv_block_idx * BLOCK_KV, 0);
|
| 224 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_KV_SCALE_SIZE_PER_STAGE);
|
| 225 |
+
}
|
| 226 |
+
num_total_kv_blocks += num_kv_blocks;
|
| 227 |
+
|
| 228 |
+
// Jump to the next block
|
| 229 |
+
CUTE_TIE(get_next_block_q_idx(), block_q_idx, q_iter_idx);
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
} else if (warp_idx == kSpecWarpStart + 1) {
|
| 233 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 234 |
+
|
| 235 |
+
// Require full allocation
|
| 236 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 237 |
+
|
| 238 |
+
// Make UMMA desc
|
| 239 |
+
auto instr_desc = cute::UMMA::make_instr_desc<cutlass::float_e4m3_t, cutlass::float_e4m3_t, float,
|
| 240 |
+
UMMA_M, UMMA_N, cute::UMMA::Major::K, cute::UMMA::Major::K>();
|
| 241 |
+
auto runtime_instr_desc = cute::UMMA::make_runtime_instr_desc(instr_desc);
|
| 242 |
+
|
| 243 |
+
while (block_q_idx < num_q_blocks) {
|
| 244 |
+
CUTE_TIE_DECL(load_schedule(), q_stage_idx, q_phase, kv_start, num_kv_blocks);
|
| 245 |
+
|
| 246 |
+
// Wait TMA Q arrival
|
| 247 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 248 |
+
|
| 249 |
+
// Compute over KV blocks
|
| 250 |
+
#pragma unroll
|
| 251 |
+
for (uint32_t kv_block_idx = 0; kv_block_idx < num_kv_blocks; ++ kv_block_idx) {
|
| 252 |
+
// Compute `[BLOCK_Q * kNumHeads, kHeadDim] @ [BLOCK_KV, kHeadDim] -> [BLOCK_Q, BLOCK_KV]`
|
| 253 |
+
// Wait TMA KV arrival
|
| 254 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_block_idx), kv_stage_idx, kv_phase);
|
| 255 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 256 |
+
|
| 257 |
+
// Issue UMMA
|
| 258 |
+
DG_STATIC_ASSERT(BLOCK_KV == kNumMathThreads, "Invalid block size");
|
| 259 |
+
DG_STATIC_ASSERT(kHeadDim % UMMA_K == 0, "Invalid head dim");
|
| 260 |
+
#pragma unroll
|
| 261 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++ i) {
|
| 262 |
+
empty_umma_barriers[i]->wait(((num_total_kv_blocks + kv_block_idx) & 1) ^ 1);
|
| 263 |
+
ptx::tcgen05_after_thread_sync();
|
| 264 |
+
#pragma unroll
|
| 265 |
+
for (uint32_t k = 0; k < kHeadDim / UMMA_K; ++ k) {
|
| 266 |
+
auto a_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, 0, kHeadDim, kHeadDim>(
|
| 267 |
+
smem_kv[kv_stage_idx], i * UMMA_M, k * UMMA_K);
|
| 268 |
+
auto b_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, 0, kHeadDim, kHeadDim>(
|
| 269 |
+
smem_q[q_stage_idx], 0, k * UMMA_K);
|
| 270 |
+
cute::SM100_MMA_F8F6F4_SS::fma(a_desc, b_desc, i * UMMA_N, k, runtime_instr_desc);
|
| 271 |
+
}
|
| 272 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(full_umma_barriers[i]));
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
num_total_kv_blocks += num_kv_blocks;
|
| 276 |
+
|
| 277 |
+
// UMMA warp must also arrive on empty_q to prevent running ahead
|
| 278 |
+
// of math warps in the Q pipeline
|
| 279 |
+
empty_q_barriers[q_stage_idx]->arrive();
|
| 280 |
+
|
| 281 |
+
// Jump to the next block
|
| 282 |
+
CUTE_TIE(get_next_block_q_idx(), block_q_idx, q_iter_idx);
|
| 283 |
+
}
|
| 284 |
+
} else if (warp_idx == kSpecWarpStart + 2 or warp_idx == kSpecWarpStart + 3) {
|
| 285 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 286 |
+
} else if (warp_idx < kSpecWarpStart) {
|
| 287 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 288 |
+
|
| 289 |
+
// Offsets
|
| 290 |
+
const auto tmem_start = warpgroup_idx * UMMA_N;
|
| 291 |
+
const auto math_thread_idx = warp_idx * 32 + lane_idx;
|
| 292 |
+
|
| 293 |
+
// Helper lambda for loading tensor memory
|
| 294 |
+
auto tmem_load = [](auto num_elems_c, const uint32_t& tmem_addr, float* accum) {
|
| 295 |
+
constexpr int N = decltype(num_elems_c)::value;
|
| 296 |
+
DG_STATIC_ASSERT(N == 32 or N == 64, "Unsupported TMEM load size");
|
| 297 |
+
using Loader = cute::conditional_t<N == 32,
|
| 298 |
+
cute::SM100_TMEM_LOAD_32dp32b32x,
|
| 299 |
+
cute::SM100_TMEM_LOAD_32dp32b64x>;
|
| 300 |
+
[&]<size_t... Is>(cute::index_sequence<Is...>) {
|
| 301 |
+
Loader::copy(tmem_addr, reinterpret_cast<uint32_t*>(accum)[Is]...);
|
| 302 |
+
}(cute::make_index_sequence<N>{});
|
| 303 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 304 |
+
};
|
| 305 |
+
|
| 306 |
+
// Local register buffers
|
| 307 |
+
float weights[BLOCK_Q][kNumHeads];
|
| 308 |
+
|
| 309 |
+
while (block_q_idx < num_q_blocks) {
|
| 310 |
+
CUTE_TIE_DECL(load_schedule(), q_stage_idx, q_phase, kv_start, num_kv_blocks);
|
| 311 |
+
|
| 312 |
+
// Wait TMA Q arrival
|
| 313 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 314 |
+
|
| 315 |
+
// Read weights
|
| 316 |
+
#pragma unroll
|
| 317 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 318 |
+
#pragma unroll
|
| 319 |
+
for (uint32_t j = 0; j < kNumHeads; ++ j)
|
| 320 |
+
weights[i][j] = ptx::ld_shared(smem_weights[q_stage_idx] + i * kNumHeads + j);
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
// Compute over KV blocks
|
| 324 |
+
#pragma unroll
|
| 325 |
+
for (uint32_t kv_block_idx = 0; kv_block_idx < num_kv_blocks; ++ kv_block_idx) {
|
| 326 |
+
// Compute `[BLOCK_Q * kNumHeads, kHeadDim] @ [BLOCK_KV, kHeadDim] -> [BLOCK_Q, BLOCK_KV]`
|
| 327 |
+
// Wait TMA KV arrival
|
| 328 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_block_idx), kv_stage_idx, kv_phase);
|
| 329 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 330 |
+
|
| 331 |
+
// Read per-KV scales
|
| 332 |
+
float scale_kv = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + math_thread_idx);
|
| 333 |
+
|
| 334 |
+
// Wait UMMA arrival
|
| 335 |
+
full_umma_barriers[warpgroup_idx]->wait((num_total_kv_blocks + kv_block_idx) & 1);
|
| 336 |
+
ptx::tcgen05_after_thread_sync();
|
| 337 |
+
|
| 338 |
+
// Release KV empty
|
| 339 |
+
empty_kv_barriers[kv_stage_idx]->arrive();
|
| 340 |
+
|
| 341 |
+
// Reduce over the head dim and store
|
| 342 |
+
const auto kv_offset = kv_start + kv_block_idx * BLOCK_KV + math_thread_idx;
|
| 343 |
+
DG_STATIC_ASSERT(kNumHeads % 8 == 0, "Invalid head");
|
| 344 |
+
|
| 345 |
+
#pragma unroll
|
| 346 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 347 |
+
// Load accumulator from TMEM
|
| 348 |
+
float accum[kNumHeads];
|
| 349 |
+
tmem_load(cute::Int<kNumHeads>{}, tmem_start + i * kNumHeads, accum);
|
| 350 |
+
|
| 351 |
+
// Release TMEM empty
|
| 352 |
+
if (i == BLOCK_Q - 1) {
|
| 353 |
+
ptx::tcgen05_before_thread_sync();
|
| 354 |
+
empty_umma_barriers[warpgroup_idx]->arrive();
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
// Accumulate weighted ReLU in parallel
|
| 358 |
+
auto sum_0 = make_float2(0, 0);
|
| 359 |
+
auto sum_1 = make_float2(0, 0);
|
| 360 |
+
|
| 361 |
+
const auto transform = [&](const uint32_t& j, const float2& sum) {
|
| 362 |
+
auto a = make_float2(fmaxf(accum[j], 0), fmaxf(accum[j + 1], 0));
|
| 363 |
+
auto b = make_float2(weights[i][j], weights[i][j + 1]);
|
| 364 |
+
return __ffma2_rn(a, b, sum);
|
| 365 |
+
};
|
| 366 |
+
|
| 367 |
+
#pragma unroll
|
| 368 |
+
for (uint32_t j = 0; j < kNumHeads; j += 4) {
|
| 369 |
+
sum_0 = transform(j, sum_0);
|
| 370 |
+
sum_1 = transform(j + 2, sum_1);
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
auto sum = __fadd2_rn(sum_0, sum_1);
|
| 374 |
+
auto result = static_cast<logits_dtype_t>(scale_kv * (sum.x + sum.y));
|
| 375 |
+
|
| 376 |
+
// Store into the global memory
|
| 377 |
+
const auto q_offset = (block_q_idx * BLOCK_Q + i) * static_cast<uint64_t>(stride_logits);
|
| 378 |
+
if constexpr (kIsCompressedLogits) {
|
| 379 |
+
if (seq_k_start[i] <= kv_offset and kv_offset < seq_k_end[i])
|
| 380 |
+
logits[q_offset + kv_offset - seq_k_start[i]] = result;
|
| 381 |
+
} else {
|
| 382 |
+
logits[q_offset + kv_offset] = result;
|
| 383 |
+
}
|
| 384 |
+
__syncwarp();
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
num_total_kv_blocks += num_kv_blocks;
|
| 388 |
+
|
| 389 |
+
// Release Q empty
|
| 390 |
+
empty_q_barriers[q_stage_idx]->arrive();
|
| 391 |
+
|
| 392 |
+
// Jump to the next block
|
| 393 |
+
CUTE_TIE(get_next_block_q_idx(), block_q_idx, q_iter_idx);
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
// Free tensor memory
|
| 397 |
+
cutlass::arch::NamedBarrier(kNumMathThreads, 0).sync();
|
| 398 |
+
if (warp_idx == 0)
|
| 399 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
|
| 400 |
+
}
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm100_fp8_paged_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 5 |
+
|
| 6 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 7 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 10 |
+
#include <deep_gemm/common/math.cuh>
|
| 11 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 12 |
+
#include <deep_gemm/common/utils.cuh>
|
| 13 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 17 |
+
#include <deep_gemm/scheduler/paged_mqa_logits.cuh>
|
| 18 |
+
|
| 19 |
+
namespace deep_gemm {
|
| 20 |
+
|
| 21 |
+
template <uint32_t kNextN, uint32_t kNumHeads,
|
| 22 |
+
uint32_t kHeadDim, uint32_t BLOCK_KV,
|
| 23 |
+
bool kIsContextLens2D, bool kIsVarlen,
|
| 24 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 25 |
+
uint32_t SPLIT_KV,
|
| 26 |
+
uint32_t kNumSpecializedThreads, uint32_t kNumMathThreads,
|
| 27 |
+
typename logits_dtype_t,
|
| 28 |
+
uint32_t kNumMathWarpGroups = kNumMathThreads / 128>
|
| 29 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumSpecializedThreads + kNumMathThreads, 1)
|
| 30 |
+
void sm100_fp8_paged_mqa_logits(const uint32_t batch_size,
|
| 31 |
+
const uint32_t logits_stride, const uint32_t block_table_stride,
|
| 32 |
+
const uint32_t* context_lens, logits_dtype_t* logits,
|
| 33 |
+
const uint32_t* block_table, const uint32_t* indices,
|
| 34 |
+
const uint32_t* schedule_meta,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv_scales,
|
| 38 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 39 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 40 |
+
|
| 41 |
+
// Utils
|
| 42 |
+
const auto sm_idx = blockIdx.x;
|
| 43 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 44 |
+
const auto warpgroup_idx = warp_idx / 4;
|
| 45 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 46 |
+
constexpr uint32_t kSpecWarpStart = kNumMathWarpGroups * 4;
|
| 47 |
+
|
| 48 |
+
// Prefetch TMA descriptors
|
| 49 |
+
DG_STATIC_ASSERT(kNumSpecializedThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 50 |
+
if (warp_idx == kSpecWarpStart) {
|
| 51 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 52 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 53 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv_scales);
|
| 54 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// For non-varlen odd kNextN >= 3, pad to even using TMA OOB zero-fill.
|
| 58 |
+
static constexpr bool kPadOddN = (not kIsVarlen) and (kNextN % 2 == 1) and (kNextN >= 3);
|
| 59 |
+
static constexpr uint32_t kNextNAtom = (kIsVarlen or kNextN >= 2) ? 2 : 1;
|
| 60 |
+
static constexpr uint32_t kNumNextNAtoms = math::constexpr_ceil_div(kNextN, kNextNAtom);
|
| 61 |
+
|
| 62 |
+
// Shared memory configs
|
| 63 |
+
static constexpr uint32_t kSwizzleAlignment = kHeadDim * 8;
|
| 64 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = kNextNAtom * kNumHeads * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 65 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = SPLIT_KV * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 66 |
+
static constexpr uint32_t SMEM_KV_SCALE_SIZE_PER_STAGE = SPLIT_KV * sizeof(float);
|
| 67 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = kNextNAtom * kNumHeads * sizeof(float);
|
| 68 |
+
|
| 69 |
+
// Align to swizzling alignment bytes
|
| 70 |
+
extern __shared__ __align__(kSwizzleAlignment) uint8_t smem_buffer[];
|
| 71 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 72 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 73 |
+
|
| 74 |
+
// Q and KV data on shared memory
|
| 75 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 76 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + SMEM_Q_SIZE_PER_STAGE * i);
|
| 77 |
+
});
|
| 78 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 79 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * i);
|
| 80 |
+
});
|
| 81 |
+
constexpr auto smem_offset = SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * kNumKVStages;
|
| 82 |
+
auto smem_kv_scales = utils::PatternVisitor([&](const uint32_t& i) {
|
| 83 |
+
return reinterpret_cast<float*>(smem_buffer + smem_offset + SMEM_KV_SCALE_SIZE_PER_STAGE * i);
|
| 84 |
+
});
|
| 85 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 86 |
+
return reinterpret_cast<float*>(smem_buffer + smem_offset + SMEM_KV_SCALE_SIZE_PER_STAGE * kNumKVStages + SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
// Barriers and TMEM pointer on shared memory
|
| 90 |
+
const auto barrier_ptr = reinterpret_cast<Barrier*>(smem_weights[kNumQStages]);
|
| 91 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + i; });
|
| 92 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages + i; });
|
| 93 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + i; });
|
| 94 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + kNumQStages * 2 + kNumKVStages + i; });
|
| 95 |
+
const auto umma_barrier_ptr = barrier_ptr + kNumQStages * 2 + kNumKVStages * 2;
|
| 96 |
+
auto full_umma_barriers = utils::PatternVisitor([&](const uint32_t& i) { return umma_barrier_ptr + i; });
|
| 97 |
+
auto empty_umma_barriers = utils::PatternVisitor([&](const uint32_t& i) { return umma_barrier_ptr + kNumMathWarpGroups + i; });
|
| 98 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(umma_barrier_ptr + kNumMathWarpGroups * 2);
|
| 99 |
+
|
| 100 |
+
constexpr uint32_t kNumTmemCols = kNextNAtom * kNumHeads * kNumMathWarpGroups;
|
| 101 |
+
DG_STATIC_ASSERT(kNumTmemCols <= 512, "Too many tensor memory");
|
| 102 |
+
|
| 103 |
+
// Initialize barriers
|
| 104 |
+
if (warp_idx == kSpecWarpStart and cute::elect_one_sync()) {
|
| 105 |
+
#pragma unroll
|
| 106 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 107 |
+
full_q_barriers[i]->init(1);
|
| 108 |
+
empty_q_barriers[i]->init(kNumMathThreads + 32);
|
| 109 |
+
}
|
| 110 |
+
#pragma unroll
|
| 111 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 112 |
+
full_kv_barriers[i]->init(1);
|
| 113 |
+
empty_kv_barriers[i]->init(kNumMathThreads);
|
| 114 |
+
}
|
| 115 |
+
cutlass::arch::fence_barrier_init();
|
| 116 |
+
}
|
| 117 |
+
if (warp_idx == kSpecWarpStart + 1) {
|
| 118 |
+
if (cute::elect_one_sync()) {
|
| 119 |
+
#pragma unroll
|
| 120 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++i) {
|
| 121 |
+
full_umma_barriers[i]->init(1);
|
| 122 |
+
empty_umma_barriers[i]->init(128);
|
| 123 |
+
}
|
| 124 |
+
cutlass::arch::fence_barrier_init();
|
| 125 |
+
}
|
| 126 |
+
// Allocate tensor memory
|
| 127 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 128 |
+
}
|
| 129 |
+
__syncthreads();
|
| 130 |
+
|
| 131 |
+
// Register reconfigurations
|
| 132 |
+
constexpr uint32_t kNumSpecializedRegisters = 56;
|
| 133 |
+
constexpr uint32_t kNumMathRegisters = 224;
|
| 134 |
+
|
| 135 |
+
// Wait for primary kernel completion
|
| 136 |
+
cudaGridDependencySynchronize();
|
| 137 |
+
|
| 138 |
+
// Scheduler
|
| 139 |
+
constexpr uint32_t kNumBlocksPerSplit = SPLIT_KV / BLOCK_KV;
|
| 140 |
+
using Scheduler = sched::PagedMQALogitsScheduler<kNextN, kIsContextLens2D, kIsVarlen, BLOCK_KV, kNumBlocksPerSplit, kNumNextNAtoms>;
|
| 141 |
+
DG_STATIC_ASSERT(SPLIT_KV == BLOCK_KV * kNumBlocksPerSplit, "Invalid `SPLIT_KV`");
|
| 142 |
+
|
| 143 |
+
// Q and KV pipeline
|
| 144 |
+
const auto get_q_pipeline = [=](const uint32_t& q_iter_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 145 |
+
return {q_iter_idx % kNumQStages, (q_iter_idx / kNumQStages) & 1}; // Q pipeline stage and phase
|
| 146 |
+
};
|
| 147 |
+
const auto get_kv_pipeline = [=](const uint32_t& kv_iter_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 148 |
+
return {kv_iter_idx % kNumKVStages, (kv_iter_idx / kNumKVStages) & 1}; // KV pipeline stage and phase
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
// UMMA settings
|
| 152 |
+
// Construct instruction with layout D
|
| 153 |
+
constexpr uint32_t UMMA_M = 128;
|
| 154 |
+
constexpr uint32_t UMMA_K = 32 / sizeof(cutlass::float_e4m3_t);
|
| 155 |
+
constexpr uint32_t UMMA_N = kNextNAtom * kNumHeads;
|
| 156 |
+
DG_STATIC_ASSERT(SPLIT_KV == UMMA_M * kNumMathWarpGroups, "Invalid `SPLIT_KV`");
|
| 157 |
+
|
| 158 |
+
if (warp_idx == kSpecWarpStart) {
|
| 159 |
+
// TMA warp for loading data
|
| 160 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 161 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 162 |
+
uint32_t q_iter_idx = 0, kv_iter_idx = 0;
|
| 163 |
+
|
| 164 |
+
const auto issue_tma_q = [&](const uint32_t& stage_idx, const uint32_t& tma_q_atom_idx) {
|
| 165 |
+
if (cute::elect_one_sync()) {
|
| 166 |
+
const auto q_token_idx = Scheduler::atom_to_token_idx(tma_q_atom_idx);
|
| 167 |
+
tma::copy<kHeadDim, kNextNAtom * kNumHeads, kHeadDim>(&tensor_map_q, full_q_barriers[stage_idx], smem_q[stage_idx], 0, q_token_idx * kNumHeads);
|
| 168 |
+
tma::copy<kNextNAtom * kNumHeads, 1, 0>(&tensor_map_weights, full_q_barriers[stage_idx], smem_weights[stage_idx], 0, q_token_idx);
|
| 169 |
+
full_q_barriers[stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 170 |
+
}
|
| 171 |
+
};
|
| 172 |
+
|
| 173 |
+
// Initialize outside valid range to indicate no previous task
|
| 174 |
+
uint32_t q_atom_idx = batch_size * kNumNextNAtoms, kv_idx, num_kv;
|
| 175 |
+
uint32_t next_q_atom_idx, next_kv_idx, next_num_kv;
|
| 176 |
+
bool fetched_next_task;
|
| 177 |
+
|
| 178 |
+
// Prefetch the first Q
|
| 179 |
+
if ((fetched_next_task = scheduler.fetch_next_task(next_q_atom_idx, next_kv_idx, next_num_kv)))
|
| 180 |
+
issue_tma_q(0, next_q_atom_idx), q_iter_idx = 1;
|
| 181 |
+
|
| 182 |
+
uint32_t kv_block_idx_ptr = 32;
|
| 183 |
+
uint32_t kv_block_idx_storage;
|
| 184 |
+
|
| 185 |
+
while (fetched_next_task) {
|
| 186 |
+
// Prefetch next Q when (q, atom) changes
|
| 187 |
+
const auto next_advance = scheduler.get_atom_advance(next_q_atom_idx, batch_size);
|
| 188 |
+
bool prefetch_q = (q_atom_idx != next_q_atom_idx) and scheduler.exist_q_atom_idx(next_q_atom_idx + next_advance);
|
| 189 |
+
|
| 190 |
+
if (q_atom_idx != next_q_atom_idx)
|
| 191 |
+
kv_block_idx_ptr = 32;
|
| 192 |
+
|
| 193 |
+
q_atom_idx = next_q_atom_idx;
|
| 194 |
+
kv_idx = next_kv_idx;
|
| 195 |
+
num_kv = next_num_kv;
|
| 196 |
+
|
| 197 |
+
// Read KV block index
|
| 198 |
+
// TODO(xuzhean): consider -1
|
| 199 |
+
if (kv_block_idx_ptr == 32) {
|
| 200 |
+
kv_block_idx_ptr = 0;
|
| 201 |
+
const auto block_table_offset = Scheduler::atom_to_block_table_row(q_atom_idx) * static_cast<uint64_t>(block_table_stride);
|
| 202 |
+
kv_block_idx_storage = (kv_idx + lane_idx < num_kv)
|
| 203 |
+
? block_table[block_table_offset + kv_idx + lane_idx] : 0;
|
| 204 |
+
}
|
| 205 |
+
__syncwarp();
|
| 206 |
+
DG_STATIC_ASSERT(32 % kNumBlocksPerSplit == 0, "Invalid `UMMA_M`");
|
| 207 |
+
|
| 208 |
+
// Wait Q consumer release and issue TMA Q
|
| 209 |
+
if (prefetch_q) {
|
| 210 |
+
CUTE_TIE_DECL(get_q_pipeline(q_iter_idx ++), q_stage_idx, q_phase);
|
| 211 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 212 |
+
issue_tma_q(q_stage_idx, q_atom_idx + next_advance);
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
uint32_t kv_block_idx[kNumBlocksPerSplit];
|
| 216 |
+
#pragma unroll
|
| 217 |
+
for (uint32_t i = 0; i < kNumBlocksPerSplit; ++ i)
|
| 218 |
+
kv_block_idx[i] = __shfl_sync(0xffffffff, kv_block_idx_storage, kv_block_idx_ptr + i);
|
| 219 |
+
kv_block_idx_ptr += kNumBlocksPerSplit;
|
| 220 |
+
|
| 221 |
+
// Wait KV consumer release
|
| 222 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_iter_idx ++), kv_stage_idx, kv_phase);
|
| 223 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 224 |
+
|
| 225 |
+
if (cute::elect_one_sync()) {
|
| 226 |
+
#pragma unroll
|
| 227 |
+
for (uint32_t i = 0; i < kNumBlocksPerSplit; ++ i) {
|
| 228 |
+
tma::copy<kHeadDim, BLOCK_KV, 0, __nv_fp8_e4m3, true>(&tensor_map_kv, full_kv_barriers[kv_stage_idx],
|
| 229 |
+
smem_kv[kv_stage_idx] + (BLOCK_KV * kHeadDim) * i,
|
| 230 |
+
0, 0, 1, kv_block_idx[i]);
|
| 231 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_kv_scales, full_kv_barriers[kv_stage_idx],
|
| 232 |
+
smem_kv_scales[kv_stage_idx] + BLOCK_KV * i,
|
| 233 |
+
0, kv_block_idx[i]);
|
| 234 |
+
}
|
| 235 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_KV_SCALE_SIZE_PER_STAGE);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
// Fetch next task
|
| 239 |
+
fetched_next_task = scheduler.fetch_next_task(next_q_atom_idx, next_kv_idx, next_num_kv);
|
| 240 |
+
}
|
| 241 |
+
} else if (warp_idx == kSpecWarpStart + 1) {
|
| 242 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 243 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 244 |
+
uint32_t q_iter_idx = 0, kv_iter_idx = 0;
|
| 245 |
+
|
| 246 |
+
// Require full allocation
|
| 247 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(ptx::ld_shared(tmem_ptr_in_smem) == 0);
|
| 248 |
+
|
| 249 |
+
// Make UMMA desc
|
| 250 |
+
auto instr_desc = cute::UMMA::make_instr_desc<cutlass::float_e4m3_t, cutlass::float_e4m3_t, float,
|
| 251 |
+
UMMA_M, UMMA_N, cute::UMMA::Major::K, cute::UMMA::Major::K>();
|
| 252 |
+
auto runtime_instr_desc = cute::UMMA::make_runtime_instr_desc(instr_desc);
|
| 253 |
+
|
| 254 |
+
uint32_t q_atom_idx = batch_size * kNumNextNAtoms, kv_idx;
|
| 255 |
+
uint32_t next_q_atom_idx, next_kv_idx, next_num_kv;
|
| 256 |
+
uint32_t q_stage_idx, q_phase;
|
| 257 |
+
uint32_t umma_phase = 1;
|
| 258 |
+
|
| 259 |
+
while (scheduler.fetch_next_task(next_q_atom_idx, next_kv_idx, next_num_kv)) {
|
| 260 |
+
if (q_atom_idx != next_q_atom_idx) {
|
| 261 |
+
// Release previous Q empty (UMMA warp must participate to prevent
|
| 262 |
+
// running ahead of math warps in the Q pipeline)
|
| 263 |
+
if (q_iter_idx > 0)
|
| 264 |
+
empty_q_barriers[(q_iter_idx - 1) % kNumQStages]->arrive();
|
| 265 |
+
|
| 266 |
+
CUTE_TIE(get_q_pipeline(q_iter_idx ++), q_stage_idx, q_phase);
|
| 267 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
q_atom_idx = next_q_atom_idx;
|
| 271 |
+
kv_idx = next_kv_idx;
|
| 272 |
+
|
| 273 |
+
// Wait KV arrival
|
| 274 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_iter_idx ++), kv_stage_idx, kv_phase);
|
| 275 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 276 |
+
|
| 277 |
+
DG_STATIC_ASSERT(kHeadDim % UMMA_K == 0, "Invalid head dim");
|
| 278 |
+
#pragma unroll
|
| 279 |
+
for (uint32_t i = 0; i < kNumMathWarpGroups; ++ i) {
|
| 280 |
+
empty_umma_barriers[i]->wait(umma_phase);
|
| 281 |
+
ptx::tcgen05_after_thread_sync();
|
| 282 |
+
#pragma unroll
|
| 283 |
+
for (uint32_t k = 0; k < kHeadDim / UMMA_K; ++ k) {
|
| 284 |
+
auto a_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, 0, kHeadDim, kHeadDim>(
|
| 285 |
+
smem_kv[kv_stage_idx], i * UMMA_M, k * UMMA_K);
|
| 286 |
+
auto b_desc = mma::sm100::make_umma_desc<cute::UMMA::Major::K, 0, kHeadDim, kHeadDim>(
|
| 287 |
+
smem_q[q_stage_idx], 0, k * UMMA_K);
|
| 288 |
+
cute::SM100_MMA_F8F6F4_SS::fma(a_desc, b_desc, i * UMMA_N, k, runtime_instr_desc);
|
| 289 |
+
}
|
| 290 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(full_umma_barriers[i]));
|
| 291 |
+
}
|
| 292 |
+
umma_phase ^= 1;
|
| 293 |
+
}
|
| 294 |
+
} else if (warp_idx == kSpecWarpStart + 2 or warp_idx == kSpecWarpStart + 3) {
|
| 295 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumSpecializedRegisters>();
|
| 296 |
+
} else if (warp_idx < kSpecWarpStart) {
|
| 297 |
+
// Math warpgroups for reduce
|
| 298 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 299 |
+
auto scheduler = Scheduler(sm_idx, batch_size, context_lens, schedule_meta, indices);
|
| 300 |
+
uint32_t q_iter_idx = 0, kv_iter_idx = 0;
|
| 301 |
+
|
| 302 |
+
// Offsets
|
| 303 |
+
const auto math_warpgroup_idx = warpgroup_idx;
|
| 304 |
+
const auto tmem_start = math_warpgroup_idx * UMMA_N;
|
| 305 |
+
const auto math_thread_idx = warp_idx * 32 + lane_idx;
|
| 306 |
+
|
| 307 |
+
// Helper lambda for loading tensor memory
|
| 308 |
+
auto tmem_load = [](auto num_elems_c, const uint32_t& tmem_addr, float* accum) {
|
| 309 |
+
constexpr int N = decltype(num_elems_c)::value;
|
| 310 |
+
DG_STATIC_ASSERT(N == 32 or N == 64, "Unsupported TMEM load size");
|
| 311 |
+
using Loader = cute::conditional_t<N == 32,
|
| 312 |
+
cute::SM100_TMEM_LOAD_32dp32b32x,
|
| 313 |
+
cute::SM100_TMEM_LOAD_32dp32b64x>;
|
| 314 |
+
[&]<size_t... Is>(cute::index_sequence<Is...>) {
|
| 315 |
+
Loader::copy(tmem_addr, reinterpret_cast<uint32_t*>(accum)[Is]...);
|
| 316 |
+
}(cute::make_index_sequence<N>{});
|
| 317 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 318 |
+
};
|
| 319 |
+
|
| 320 |
+
// Local register buffers
|
| 321 |
+
float weights[kNextNAtom][kNumHeads];
|
| 322 |
+
|
| 323 |
+
// Initialize outside valid range to indicate no previous task
|
| 324 |
+
uint32_t q_atom_idx = batch_size * kNumNextNAtoms, kv_idx;
|
| 325 |
+
uint32_t next_q_atom_idx, next_kv_idx, next_num_kv;
|
| 326 |
+
uint32_t q_stage_idx, q_phase;
|
| 327 |
+
uint32_t umma_phase = 0;
|
| 328 |
+
bool is_paired_atom = false;
|
| 329 |
+
|
| 330 |
+
while (scheduler.fetch_next_task(next_q_atom_idx, next_kv_idx, next_num_kv)) {
|
| 331 |
+
// Q or atom changes
|
| 332 |
+
if (q_atom_idx != next_q_atom_idx) {
|
| 333 |
+
// Release last Q empty
|
| 334 |
+
if (q_iter_idx > 0)
|
| 335 |
+
empty_q_barriers[(q_iter_idx - 1) % kNumQStages]->arrive();
|
| 336 |
+
|
| 337 |
+
// Wait TMA Q arrival
|
| 338 |
+
CUTE_TIE(get_q_pipeline(q_iter_idx ++), q_stage_idx, q_phase);
|
| 339 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 340 |
+
|
| 341 |
+
// Read weights
|
| 342 |
+
#pragma unroll
|
| 343 |
+
for (uint32_t i = 0; i < kNextNAtom; ++ i) {
|
| 344 |
+
#pragma unroll
|
| 345 |
+
for (uint32_t j = 0; j < kNumHeads; ++ j)
|
| 346 |
+
weights[i][j] = ptx::ld_shared(smem_weights[q_stage_idx] + i * kNumHeads + j);
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
if constexpr (kIsVarlen) {
|
| 350 |
+
is_paired_atom = (scheduler.get_atom_advance(next_q_atom_idx, batch_size) == 2);
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
// Get current task indices
|
| 355 |
+
q_atom_idx = next_q_atom_idx;
|
| 356 |
+
kv_idx = next_kv_idx;
|
| 357 |
+
|
| 358 |
+
// Calculate KV offset in advance
|
| 359 |
+
auto kv_offset = Scheduler::atom_to_token_idx(q_atom_idx) * static_cast<uint64_t>(logits_stride) + kv_idx * BLOCK_KV;
|
| 360 |
+
|
| 361 |
+
// Wait TMA KV arrival
|
| 362 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_iter_idx ++), kv_stage_idx, kv_phase);
|
| 363 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 364 |
+
|
| 365 |
+
// Read per-KV scales
|
| 366 |
+
float scale_kv = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + math_thread_idx);
|
| 367 |
+
|
| 368 |
+
// Wait UMMA arrival
|
| 369 |
+
full_umma_barriers[math_warpgroup_idx]->wait(umma_phase);
|
| 370 |
+
ptx::tcgen05_after_thread_sync();
|
| 371 |
+
umma_phase ^= 1;
|
| 372 |
+
|
| 373 |
+
// Release KV empty
|
| 374 |
+
empty_kv_barriers[kv_stage_idx]->arrive();
|
| 375 |
+
|
| 376 |
+
// Reduce over the head dim and store
|
| 377 |
+
DG_STATIC_ASSERT(kNumHeads % 8 == 0, "Invalid head");
|
| 378 |
+
|
| 379 |
+
const auto reduce_and_store = [&](auto num_iters_c) {
|
| 380 |
+
constexpr uint32_t kNumIters = decltype(num_iters_c)::value;
|
| 381 |
+
float accum[kNumHeads];
|
| 382 |
+
|
| 383 |
+
#pragma unroll
|
| 384 |
+
for (uint32_t i = 0; i < kNumIters; ++ i) {
|
| 385 |
+
// Load accumulator from TMEM
|
| 386 |
+
tmem_load(cute::Int<kNumHeads>{}, tmem_start + i * kNumHeads, accum);
|
| 387 |
+
|
| 388 |
+
// Accumulate weighted ReLU in parallel
|
| 389 |
+
auto sum_0 = make_float2(0, 0);
|
| 390 |
+
auto sum_1 = make_float2(0, 0);
|
| 391 |
+
|
| 392 |
+
const auto transform = [&](const uint32_t& j, const float2& sum) {
|
| 393 |
+
auto a = make_float2(fmaxf(accum[j], 0), fmaxf(accum[j + 1], 0));
|
| 394 |
+
auto b = make_float2(weights[i][j], weights[i][j + 1]);
|
| 395 |
+
return __ffma2_rn(a, b, sum);
|
| 396 |
+
};
|
| 397 |
+
|
| 398 |
+
#pragma unroll
|
| 399 |
+
for (uint32_t j = 0; j < kNumHeads; j += 4) {
|
| 400 |
+
sum_0 = transform(j, sum_0);
|
| 401 |
+
sum_1 = transform(j + 2, sum_1);
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
auto sum = __fadd2_rn(sum_0, sum_1);
|
| 405 |
+
auto result = static_cast<logits_dtype_t>(scale_kv * (sum.x + sum.y));
|
| 406 |
+
|
| 407 |
+
// Store into the global memory
|
| 408 |
+
logits[kv_offset + i * static_cast<uint64_t>(logits_stride) + math_thread_idx] = result;
|
| 409 |
+
__syncwarp();
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
// Release TMEM empty
|
| 413 |
+
ptx::tcgen05_before_thread_sync();
|
| 414 |
+
empty_umma_barriers[math_warpgroup_idx]->arrive();
|
| 415 |
+
};
|
| 416 |
+
|
| 417 |
+
if constexpr (kIsVarlen) {
|
| 418 |
+
if (is_paired_atom)
|
| 419 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 420 |
+
else
|
| 421 |
+
reduce_and_store(cute::Int<1>{});
|
| 422 |
+
} else if constexpr (kPadOddN) {
|
| 423 |
+
if (q_atom_idx % kNumNextNAtoms == kNumNextNAtoms - 1)
|
| 424 |
+
reduce_and_store(cute::Int<1>{});
|
| 425 |
+
else
|
| 426 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 427 |
+
} else {
|
| 428 |
+
reduce_and_store(cute::Int<kNextNAtom>{});
|
| 429 |
+
}
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
// Free tensor memory
|
| 433 |
+
cutlass::arch::NamedBarrier(kNumMathThreads, 0).sync();
|
| 434 |
+
if (warp_idx == 0)
|
| 435 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#pragma clang diagnostic push
|
| 3 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 4 |
+
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 8 |
+
#include <deep_gemm/common/math.cuh>
|
| 9 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 10 |
+
#include <deep_gemm/common/utils.cuh>
|
| 11 |
+
#include <deep_gemm/mma/sm100.cuh>
|
| 12 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 13 |
+
#include <deep_gemm/ptx/tcgen05.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 15 |
+
|
| 16 |
+
namespace deep_gemm {
|
| 17 |
+
|
| 18 |
+
template <uint32_t kSwizzleMode, uint32_t kSwizzleBase = 16>
|
| 19 |
+
CUTLASS_DEVICE
|
| 20 |
+
uint32_t get_swizzled_smem_offset(const uint32_t& offset, const uint32_t& lane_idx) {
|
| 21 |
+
// Calculate the index of the bank group to be written in the atom
|
| 22 |
+
const auto bank_group_idx = offset + lane_idx * (kSwizzleMode / kSwizzleBase);
|
| 23 |
+
|
| 24 |
+
// Reshape the atom in another view and swizzle
|
| 25 |
+
// - original: `(BLOCK_N, kSwizzleMode / kSwizzleBase)`
|
| 26 |
+
// - new: `(BLOCK_N * kSwizzleMode / kSwizzleBase / kNumBankGroups, kNumBankGroups)`
|
| 27 |
+
constexpr uint32_t kNumBankGroups = 128 / kSwizzleBase;
|
| 28 |
+
constexpr bool kHasShortcut = (kSwizzleMode / kSwizzleBase) == kNumBankGroups;
|
| 29 |
+
auto row = kHasShortcut ? (offset / kNumBankGroups + lane_idx) : (bank_group_idx / kNumBankGroups);
|
| 30 |
+
auto col = kHasShortcut ? (offset) : (bank_group_idx % kNumBankGroups);
|
| 31 |
+
col ^= row % (kSwizzleMode / kSwizzleBase);
|
| 32 |
+
|
| 33 |
+
return row * 128 + col * kSwizzleBase;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
template <uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 37 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 38 |
+
uint32_t kNumSplits,
|
| 39 |
+
uint32_t kSwizzleCDMode,
|
| 40 |
+
uint32_t kNumStages,
|
| 41 |
+
uint32_t kNumMMAThreads, uint32_t kNumCastAndReduceThreads>
|
| 42 |
+
CUTLASS_GLOBAL void __launch_bounds__(kNumMMAThreads + kNumCastAndReduceThreads, 1)
|
| 43 |
+
sm100_tf32_hc_prenorm_gemm_impl(const uint32_t shape_m,
|
| 44 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 45 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 46 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_d,
|
| 47 |
+
float* sqr_sum) {
|
| 48 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 1000)) or defined(__CLION_IDE__)
|
| 49 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 50 |
+
|
| 51 |
+
// Configs
|
| 52 |
+
constexpr uint32_t kNumCastStages = 2;
|
| 53 |
+
constexpr uint32_t kSwizzleAMode = cute::min(BLOCK_K * sizeof(nv_bfloat16), 128);
|
| 54 |
+
constexpr uint32_t kSwizzleBMode = cute::min(BLOCK_K * sizeof(float), 128);
|
| 55 |
+
constexpr auto kMajorA = cute::UMMA::Major::K;
|
| 56 |
+
constexpr auto kMajorB = cute::UMMA::Major::K;
|
| 57 |
+
DG_STATIC_ASSERT(kNumCastStages <= kNumStages, "Invalid cast stages");
|
| 58 |
+
DG_STATIC_ASSERT(kSwizzleCDMode / sizeof(float) == BLOCK_N, "Invalid block N");
|
| 59 |
+
DG_STATIC_ASSERT(kNumMMAThreads == 128, "Invalid MMA threads");
|
| 60 |
+
|
| 61 |
+
// Utils
|
| 62 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 63 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 64 |
+
|
| 65 |
+
// Align to 1024 bytes for swizzle-128B
|
| 66 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 67 |
+
|
| 68 |
+
// Share memory sizes
|
| 69 |
+
constexpr uint32_t SMEM_CD_SIZE = BLOCK_M * kSwizzleCDMode;
|
| 70 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(nv_bfloat16);
|
| 71 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(float);
|
| 72 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % 1024 == 0, "Shared memory of A/B must be aligned to 1024 bytes");
|
| 73 |
+
|
| 74 |
+
// Real tensor memory size and offsets
|
| 75 |
+
constexpr uint32_t kNumTmemCols = utils::get_num_aligned_tmem_cols<BLOCK_K * kNumCastStages + BLOCK_N>();
|
| 76 |
+
|
| 77 |
+
// Prefetch TMA descriptors at the very beginning
|
| 78 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 79 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 80 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 81 |
+
cute::prefetch_tma_descriptor(&tensor_map_d);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Data on shared memory (layout as ordered below)
|
| 85 |
+
// Fill D/A/B pointers
|
| 86 |
+
auto smem_cd = reinterpret_cast<float*>(smem_buffer);
|
| 87 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 88 |
+
return reinterpret_cast<nv_bfloat16*>(smem_buffer + (SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE));
|
| 89 |
+
});
|
| 90 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 91 |
+
return reinterpret_cast<float*>(smem_buffer + (SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE));
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
// Fill barriers
|
| 95 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_CD_SIZE +
|
| 96 |
+
kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 97 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 98 |
+
auto full_cast_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 99 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 2 + i); });
|
| 100 |
+
auto empty_cast_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages * 3 + i); });
|
| 101 |
+
auto tmem_full_barrier = barrier_start_ptr + kNumStages * 4;
|
| 102 |
+
|
| 103 |
+
// Fill the tensor memory pointer
|
| 104 |
+
auto tmem_ptr_in_smem = reinterpret_cast<uint32_t*>(barrier_start_ptr + kNumStages * 4 + 1);
|
| 105 |
+
DG_STATIC_ASSERT(32 <= kNumTmemCols and kNumTmemCols <= 512, "Invalid tensor memory columns");
|
| 106 |
+
|
| 107 |
+
// Initialize barriers
|
| 108 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 109 |
+
#pragma unroll
|
| 110 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 111 |
+
full_barriers[i]->init(1);
|
| 112 |
+
full_cast_barriers[i]->init(kNumCastAndReduceThreads);
|
| 113 |
+
empty_barriers[i]->init(1);
|
| 114 |
+
empty_cast_barriers[i]->init(1);
|
| 115 |
+
}
|
| 116 |
+
tmem_full_barrier->init(1);
|
| 117 |
+
|
| 118 |
+
// Make initialized barrier visible in async proxy
|
| 119 |
+
cutlass::arch::fence_barrier_init();
|
| 120 |
+
} else if (warp_idx == 2) {
|
| 121 |
+
// Allocate tensor memory
|
| 122 |
+
cute::TMEM::Allocator1Sm().allocate(kNumTmemCols, tmem_ptr_in_smem);
|
| 123 |
+
}
|
| 124 |
+
__syncthreads();
|
| 125 |
+
|
| 126 |
+
constexpr uint32_t kNumKBlocks = math::constexpr_ceil_div(SHAPE_K, BLOCK_K);
|
| 127 |
+
constexpr uint32_t kNumKBlocksPerSplit = kNumKBlocks / kNumSplits;
|
| 128 |
+
constexpr uint32_t kRemainKBlocks = kNumKBlocks % kNumSplits;
|
| 129 |
+
const uint32_t block_idx = __shfl_sync(0xffffffff, blockIdx.x, 0);
|
| 130 |
+
const uint32_t m_block_idx = block_idx / kNumSplits;
|
| 131 |
+
const uint32_t k_split_idx = block_idx % kNumSplits;
|
| 132 |
+
const uint32_t k_offset = (k_split_idx * kNumKBlocksPerSplit + cute::min(k_split_idx, kRemainKBlocks)) * BLOCK_K;
|
| 133 |
+
const uint32_t m_offset = shape_m * k_split_idx;
|
| 134 |
+
const uint32_t num_total_stages = kNumKBlocksPerSplit + (k_split_idx < kRemainKBlocks);
|
| 135 |
+
|
| 136 |
+
// Wait for primary kernel completion
|
| 137 |
+
cudaGridDependencySynchronize();
|
| 138 |
+
|
| 139 |
+
// Dispatch warps into different roles
|
| 140 |
+
if (warp_idx < kNumMMAThreads / 32) {
|
| 141 |
+
// TMA load warp
|
| 142 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 143 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 144 |
+
// Wait consumer release
|
| 145 |
+
const auto& stage_idx = s % kNumStages;
|
| 146 |
+
empty_barriers[stage_idx]->wait(((s / kNumStages) & 1) ^ 1);
|
| 147 |
+
|
| 148 |
+
// Compute offsets
|
| 149 |
+
uint32_t m_idx = m_block_idx * BLOCK_M;
|
| 150 |
+
uint32_t k_idx = k_offset + s * BLOCK_K;
|
| 151 |
+
|
| 152 |
+
// Issue TMAs
|
| 153 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode>(&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_idx, m_idx);
|
| 154 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode>(&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_idx, 0);
|
| 155 |
+
|
| 156 |
+
// Arrive at full barriers
|
| 157 |
+
constexpr uint32_t kNumArrivalBytes = SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE;
|
| 158 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(kNumArrivalBytes);
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// MMA issue warp
|
| 163 |
+
if (warp_idx == 1) {
|
| 164 |
+
// Make instruction descriptor
|
| 165 |
+
constexpr uint32_t UMMA_M = BLOCK_M;
|
| 166 |
+
constexpr uint32_t UMMA_N = BLOCK_N;
|
| 167 |
+
constexpr uint32_t UMMA_K = 32 / sizeof(float);
|
| 168 |
+
constexpr uint32_t BLOCK_SWIZZLED_BK = kSwizzleBMode / sizeof(float);
|
| 169 |
+
using umma_t = cute::SM100_MMA_TF32_TS<cutlass::tfloat32_t, cutlass::tfloat32_t, float,
|
| 170 |
+
BLOCK_M, BLOCK_N, kMajorA, kMajorB>;
|
| 171 |
+
auto instr_desc = cute::UMMA::make_instr_desc<cutlass::tfloat32_t, cutlass::tfloat32_t, float,
|
| 172 |
+
UMMA_M, UMMA_N, kMajorA, kMajorB>();
|
| 173 |
+
const auto& runtime_instr_desc = cute::UMMA::make_runtime_instr_desc(instr_desc);
|
| 174 |
+
|
| 175 |
+
DG_STATIC_ASSERT(kNumStages <= 32, "Too many stages");
|
| 176 |
+
auto b_desc = mma::sm100::make_umma_desc<kMajorB, BLOCK_N, BLOCK_SWIZZLED_BK, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 177 |
+
const uint32_t& b_desc_lo = lane_idx < kNumStages ? b_desc.lo + lane_idx * SMEM_B_SIZE_PER_STAGE / 16 : 0u;
|
| 178 |
+
|
| 179 |
+
// Checks for MMA instructions
|
| 180 |
+
// NOTES: CUTLASS does not have such checks except the MMA traits, but we are not using these traits
|
| 181 |
+
DG_STATIC_ASSERT((UMMA_M == 64 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 182 |
+
(UMMA_M == 128 and UMMA_N % 8 == 0 and 8 <= UMMA_N and UMMA_N <= 256) or
|
| 183 |
+
(UMMA_M == 256 and UMMA_N % 16 == 0 and 16 <= UMMA_N and UMMA_N <= 256),
|
| 184 |
+
"Invalid MMA instruction shape");
|
| 185 |
+
|
| 186 |
+
// Launch MMAs
|
| 187 |
+
// We can not unroll this part
|
| 188 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 189 |
+
// Wait TMA arrival
|
| 190 |
+
const auto& stage_idx = s % kNumStages;
|
| 191 |
+
const auto& cast_stage_idx = s % kNumCastStages;
|
| 192 |
+
full_cast_barriers[cast_stage_idx]->wait((s / kNumCastStages) & 1);
|
| 193 |
+
ptx::tcgen05_after_thread_sync();
|
| 194 |
+
|
| 195 |
+
// Issue UMMA
|
| 196 |
+
const auto& b_desc_base_lo = __shfl_sync(0xffffffff, b_desc_lo, static_cast<int>(stage_idx));
|
| 197 |
+
#pragma unroll
|
| 198 |
+
for (uint32_t k = 0; k < BLOCK_K / UMMA_K; ++ k) {
|
| 199 |
+
const uint32_t& atom_idx = (k * UMMA_K) / BLOCK_SWIZZLED_BK;
|
| 200 |
+
const uint32_t& in_atom_idx = (k * UMMA_K) % BLOCK_SWIZZLED_BK;
|
| 201 |
+
const uint32_t& offset = atom_idx * BLOCK_N * BLOCK_SWIZZLED_BK;
|
| 202 |
+
b_desc.lo = mma::sm100::advance_umma_desc_lo<kMajorB, BLOCK_N, kSwizzleBMode, float>(b_desc_base_lo, offset, in_atom_idx);
|
| 203 |
+
umma_t::fma(BLOCK_K * cast_stage_idx + k * UMMA_K, b_desc, BLOCK_K * kNumCastStages, s > 0 or k > 0, runtime_instr_desc);
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// Commit
|
| 207 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(empty_cast_barriers[cast_stage_idx]));
|
| 208 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(empty_barriers[stage_idx]));
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// Commit to epilogue threads
|
| 212 |
+
cutlass::arch::umma_arrive(reinterpret_cast<uint64_t*>(tmem_full_barrier));
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// TMA checks
|
| 216 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 217 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(float);
|
| 218 |
+
DG_STATIC_ASSERT(kSwizzleCDMode > 0, "TMA D must be swizzled");
|
| 219 |
+
DG_STATIC_ASSERT(BLOCK_N % kNumElemsPerBankGroup == 0, "Invalid swizzling");
|
| 220 |
+
|
| 221 |
+
// Only support layout F (M = 64) and D (M = 128)
|
| 222 |
+
DG_STATIC_ASSERT(BLOCK_M == 64 or BLOCK_M == 128, "Invalid block M");
|
| 223 |
+
|
| 224 |
+
// Wait UMMA arrival
|
| 225 |
+
tmem_full_barrier->wait(0);
|
| 226 |
+
ptx::tcgen05_after_thread_sync();
|
| 227 |
+
|
| 228 |
+
// Load from tensor memory into registers, and write shared memory with STSM
|
| 229 |
+
DG_STATIC_ASSERT(kNumMMAThreads == 128, "Epilogue threads not enough");
|
| 230 |
+
|
| 231 |
+
// Store into shared memory
|
| 232 |
+
#pragma unroll
|
| 233 |
+
for (uint32_t i = 0; i < BLOCK_N / kNumElemsPerBankGroup; ++ i) {
|
| 234 |
+
// Source and destination memory address
|
| 235 |
+
uint32_t tmem_addr = BLOCK_K * kNumCastStages + i * kNumElemsPerBankGroup;
|
| 236 |
+
auto smem_ptr = reinterpret_cast<uint8_t*>(smem_cd) + // Base pointer
|
| 237 |
+
warp_idx * BLOCK_M / 4 * kSwizzleCDMode + // Warp offset
|
| 238 |
+
get_swizzled_smem_offset<kSwizzleCDMode>(i, lane_idx); // In-atom offset
|
| 239 |
+
|
| 240 |
+
// Load from tensor memory, store into shared memory
|
| 241 |
+
uint32_t values[kNumElemsPerBankGroup];
|
| 242 |
+
DG_STATIC_ASSERT(kNumElemsPerBankGroup == 4, "Invalid type");
|
| 243 |
+
cute::SM100_TMEM_LOAD_32dp32b4x::copy(tmem_addr,
|
| 244 |
+
values[0], values[1], values[2], values[3]);
|
| 245 |
+
cutlass::arch::fence_view_async_tmem_load();
|
| 246 |
+
if (BLOCK_M == 128 or (BLOCK_M == 64 and lane_idx < 16))
|
| 247 |
+
ptx::st_shared(smem_ptr, values[0], values[1], values[2], values[3]);
|
| 248 |
+
if constexpr (BLOCK_M == 64)
|
| 249 |
+
__syncwarp();
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
// Synchronize all threads and issue TMA
|
| 253 |
+
cute::tma_store_fence();
|
| 254 |
+
cutlass::arch::NamedBarrier::sync(kNumMMAThreads, 0);
|
| 255 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 256 |
+
if constexpr (kNumSplits == 1) {
|
| 257 |
+
cute::SM90_TMA_STORE_2D::copy(&tensor_map_d, smem_cd, 0, m_block_idx * BLOCK_M);
|
| 258 |
+
} else {
|
| 259 |
+
cute::SM90_TMA_STORE_3D::copy(&tensor_map_d, smem_cd, 0, m_block_idx * BLOCK_M, k_split_idx);
|
| 260 |
+
}
|
| 261 |
+
cute::tma_store_arrive();
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
// Deallocate tensor memory by warp 1
|
| 265 |
+
// NOTES: warp 0 is waiting TMA store
|
| 266 |
+
if (warp_idx == 1)
|
| 267 |
+
cute::TMEM::Allocator1Sm().free(0, kNumTmemCols);
|
| 268 |
+
} else {
|
| 269 |
+
DG_STATIC_ASSERT(BLOCK_M == 64, "Invalid block M");
|
| 270 |
+
DG_STATIC_ASSERT(kNumCastAndReduceThreads == 128, "Invalid cast-and-reduce threads");
|
| 271 |
+
constexpr uint32_t BLOCK_M_PER_WARP = BLOCK_M / 4;
|
| 272 |
+
const uint32_t sub_warp_idx = warp_idx - kNumMMAThreads / 32;
|
| 273 |
+
|
| 274 |
+
// TODO: make even larger block K
|
| 275 |
+
DG_STATIC_ASSERT(BLOCK_K * sizeof(nv_bfloat16) == kSwizzleAMode, "Invalid block K");
|
| 276 |
+
|
| 277 |
+
// Launch reductions
|
| 278 |
+
float2 sum[2] = {float2{0, 0}, float2{0, 0}};
|
| 279 |
+
#pragma unroll kNumStages
|
| 280 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 281 |
+
// Wait TMA arrival
|
| 282 |
+
const auto& stage_idx = s % kNumStages;
|
| 283 |
+
full_barriers[stage_idx]->wait((s / kNumStages) & 1);
|
| 284 |
+
|
| 285 |
+
// Load from shared memory into tensor memory using movement shape `.16x256b` (shared memory part is 128b)
|
| 286 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 287 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(nv_bfloat16);
|
| 288 |
+
constexpr uint32_t kNumLoads = BLOCK_K / kNumElemsPerBankGroup;
|
| 289 |
+
const auto& smem_base_ptr = reinterpret_cast<uint8_t*>(smem_a[stage_idx]) + // Base pointer
|
| 290 |
+
sub_warp_idx * BLOCK_M_PER_WARP * kSwizzleAMode; // Warp offset
|
| 291 |
+
|
| 292 |
+
// 4 lanes shared a bank group
|
| 293 |
+
uint32_t uint32_values[2][kNumLoads];
|
| 294 |
+
DG_STATIC_ASSERT(kNumLoads % 2 == 0, "Invalid number of loads");
|
| 295 |
+
#pragma unroll
|
| 296 |
+
for (uint32_t i = 0; i < kNumLoads; i += 2) {
|
| 297 |
+
auto smem_ptr = smem_base_ptr + get_swizzled_smem_offset<kSwizzleAMode>(i + lane_idx / 16, lane_idx % 16);
|
| 298 |
+
ptx::SM90_U32x4_LDSM_N::copy(uint32_values[0][i + 0], uint32_values[1][i + 0],
|
| 299 |
+
uint32_values[0][i + 1], uint32_values[1][i + 1],
|
| 300 |
+
smem_ptr);
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
// Wait tensor memory empty
|
| 304 |
+
const auto& cast_stage_idx = s % kNumCastStages;
|
| 305 |
+
empty_cast_barriers[cast_stage_idx]->wait(((s / kNumCastStages) & 1) ^ 1);
|
| 306 |
+
|
| 307 |
+
// Cast, reduce and store into tensor memory
|
| 308 |
+
float2 fp32x2_values[2][kNumLoads];
|
| 309 |
+
const auto& upper_view = reinterpret_cast<uint32_t*>(&fp32x2_values[0]);
|
| 310 |
+
const auto& lower_view = reinterpret_cast<uint32_t*>(&fp32x2_values[1]);
|
| 311 |
+
#pragma unroll
|
| 312 |
+
for (uint32_t i = 0; i < kNumLoads; ++ i) {
|
| 313 |
+
#pragma unroll
|
| 314 |
+
for (uint32_t u = 0; u < 2; ++ u) {
|
| 315 |
+
fp32x2_values[u][i] = __bfloat1622float2(*reinterpret_cast<nv_bfloat162*>(&uint32_values[u][i]));
|
| 316 |
+
sum[u] = __ffma2_rn(fp32x2_values[u][i], fp32x2_values[u][i], sum[u]);
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
// Store upper and lower part at the same time
|
| 320 |
+
const auto idx_0 = i * 2, idx_1 = i * 2 + 1;
|
| 321 |
+
cute::SM100_TMEM_STORE_16dp256b1x::copy(
|
| 322 |
+
upper_view[idx_0], upper_view[idx_1],
|
| 323 |
+
lower_view[idx_0], lower_view[idx_1],
|
| 324 |
+
cast_stage_idx * BLOCK_K + i * 8);
|
| 325 |
+
}
|
| 326 |
+
cutlass::arch::fence_view_async_tmem_store();
|
| 327 |
+
|
| 328 |
+
// Arrive for issuing MMAs
|
| 329 |
+
ptx::tcgen05_before_thread_sync();
|
| 330 |
+
full_cast_barriers[cast_stage_idx]->arrive();
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// Intra-warp reduction and write back
|
| 334 |
+
#pragma unroll
|
| 335 |
+
for (uint32_t u = 0; u < 2; ++ u) {
|
| 336 |
+
const auto reduced_sum = math::warp_reduce_sum<4>(sum[u].x + sum[u].y);
|
| 337 |
+
const auto m_idx = m_block_idx * BLOCK_M + sub_warp_idx * BLOCK_M_PER_WARP + lane_idx / 4 + u * 8;
|
| 338 |
+
if (lane_idx % 4 == 0 and m_idx < shape_m)
|
| 339 |
+
sqr_sum[m_offset + m_idx] = reduced_sum;
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
#else
|
| 343 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 344 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_100f");
|
| 345 |
+
#endif
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
} // namespace deep_gemm
|
| 349 |
+
|
| 350 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_bf16_gemm.cuh
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#pragma clang diagnostic push
|
| 4 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 5 |
+
|
| 6 |
+
#include <cutlass/arch/barrier.h>
|
| 7 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 8 |
+
|
| 9 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 10 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 11 |
+
#include <cute/arch/copy_sm90_tma.hpp>
|
| 12 |
+
#include <cute/arch/mma_sm100_desc.hpp>
|
| 13 |
+
|
| 14 |
+
#include <deep_gemm/common/math.cuh>
|
| 15 |
+
#include <deep_gemm/common/utils.cuh>
|
| 16 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 17 |
+
#include <deep_gemm/common/types.cuh>
|
| 18 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 19 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 20 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 21 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 22 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 23 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 24 |
+
|
| 25 |
+
namespace deep_gemm {
|
| 26 |
+
|
| 27 |
+
template <cute::UMMA::Major kMajorA, cute::UMMA::Major kMajorB,
|
| 28 |
+
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 29 |
+
uint32_t kNumGroups,
|
| 30 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K_,
|
| 31 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleDMode,
|
| 32 |
+
uint32_t kNumStages_,
|
| 33 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
| 34 |
+
uint32_t kNumTMAMulticast, bool kIsTMAMulticastOnA,
|
| 35 |
+
uint32_t kNumSMs,
|
| 36 |
+
GemmType kGemmType, bool kWithAccumulation,
|
| 37 |
+
typename cd_dtype_t>
|
| 38 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1) void
|
| 39 |
+
sm90_bf16_gemm_impl(int* grouped_layout,
|
| 40 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 41 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 42 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 43 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
| 44 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
| 45 |
+
// Enlarge `BLOCK_K` for some cases
|
| 46 |
+
// NOTES: this is for reducing the `warpgroup_wait<0>()` overhead
|
| 47 |
+
constexpr uint32_t kDoMergeStages =
|
| 48 |
+
kNumStages_ >= 10 and
|
| 49 |
+
kGemmType == GemmType::Normal and
|
| 50 |
+
kMajorA == cute::UMMA::Major::K and kMajorB == cute::UMMA::Major::K and
|
| 51 |
+
kNumMathThreads == 128;
|
| 52 |
+
// Ensure there are at least `kNumMinStages` stages after merge
|
| 53 |
+
constexpr uint32_t kNumMinStages = 5;
|
| 54 |
+
constexpr uint32_t kNumStagesPerMerge = kDoMergeStages ? kNumStages_ / kNumMinStages : 1;
|
| 55 |
+
constexpr uint32_t BLOCK_K = BLOCK_K_ * kNumStagesPerMerge;
|
| 56 |
+
constexpr uint32_t kNumStages = kNumStages_ / kNumStagesPerMerge;
|
| 57 |
+
|
| 58 |
+
// Types
|
| 59 |
+
using WGMMA = typename mma::sm90::BF16MMASelector<BLOCK_N, kMajorA, kMajorB>::type;
|
| 60 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 61 |
+
DG_STATIC_ASSERT(BLOCK_M % WGMMA::M == 0 or BLOCK_M < WGMMA::M, "Invalid block size");
|
| 62 |
+
|
| 63 |
+
// Overwrite shape constants if the compiler gives
|
| 64 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 65 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 66 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 67 |
+
|
| 68 |
+
// Shared memory
|
| 69 |
+
static constexpr uint32_t SMEM_D_SIZE = math::constexpr_align(BLOCK_M * BLOCK_N * static_cast<uint32_t>(sizeof(cd_dtype_t)), 1024u);
|
| 70 |
+
static constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(__nv_bfloat16);
|
| 71 |
+
static constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(__nv_bfloat16);
|
| 72 |
+
|
| 73 |
+
// NOTES: Make sure we have enough shared memory for WGMMA padding
|
| 74 |
+
static constexpr uint32_t WGMMA_A_SIZE_PER_STAGE = WGMMA::M * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 75 |
+
DG_STATIC_ASSERT(WGMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory Out of bound for WGMMA");
|
| 76 |
+
|
| 77 |
+
// Configs
|
| 78 |
+
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 79 |
+
const uint32_t lane_idx = ptx::get_lane_idx();
|
| 80 |
+
|
| 81 |
+
// Prefetch TMA descriptors at the very beginning
|
| 82 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 83 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 84 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 85 |
+
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
| 86 |
+
}
|
| 87 |
+
__syncwarp();
|
| 88 |
+
|
| 89 |
+
// Align to 1024 bytes for swizzle-128B
|
| 90 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 91 |
+
DG_STATIC_ASSERT(SMEM_D_SIZE % 1024 == 0 and SMEM_A_SIZE_PER_STAGE % 1024 == 0 and SMEM_B_SIZE_PER_STAGE % 1024 == 0,
|
| 92 |
+
"Shared memory of A/B/D must be aligned to 1024 bytes");
|
| 93 |
+
|
| 94 |
+
// D/A/B shared memory
|
| 95 |
+
auto smem_d = reinterpret_cast<cd_dtype_t*>(smem_buffer);
|
| 96 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 97 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_D_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 98 |
+
});
|
| 99 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 100 |
+
return reinterpret_cast<cutlass::bfloat16_t*>(smem_buffer + SMEM_D_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
// Fill barriers
|
| 104 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_D_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 105 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 106 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 107 |
+
|
| 108 |
+
// Initialize barriers
|
| 109 |
+
if (warp_idx == kNumMathThreads / 32 + 1 and cute::elect_one_sync()) {
|
| 110 |
+
#pragma unroll
|
| 111 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 112 |
+
full_barriers[i]->init(1);
|
| 113 |
+
empty_barriers[i]->init(kNumTMAMulticast * kNumMathThreads / 32);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Make initialized barrier visible in async proxy
|
| 117 |
+
cutlass::arch::fence_barrier_init();
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Synchronize all threads to make barrier visible in normal memory model
|
| 121 |
+
(kNumTMAMulticast > 1) ? cute::cluster_sync() : __syncthreads();
|
| 122 |
+
|
| 123 |
+
// Register reconfigurations
|
| 124 |
+
constexpr uint32_t kNumTMARegisters = 48;
|
| 125 |
+
constexpr uint32_t kNumMathRegisters = kNumMathThreads == 128 ? 248 : 224;
|
| 126 |
+
|
| 127 |
+
// Wait for primary kernel completion
|
| 128 |
+
cudaGridDependencySynchronize();
|
| 129 |
+
|
| 130 |
+
// Block scheduler
|
| 131 |
+
uint32_t m_block_idx, n_block_idx;
|
| 132 |
+
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumTMAMulticast, kIsTMAMulticastOnA, kNumSMs>(shape_m, shape_n, shape_k, grouped_layout);
|
| 133 |
+
|
| 134 |
+
// Pipeline and TMA phases
|
| 135 |
+
uint32_t stage_idx = 0, phase = 0;
|
| 136 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 137 |
+
++ k_block_idx;
|
| 138 |
+
|
| 139 |
+
// Flip phases only if reach the next first stage
|
| 140 |
+
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
| 141 |
+
phase ^= stage_idx == 0;
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
if (warp_idx >= kNumMathThreads / 32) {
|
| 145 |
+
// TMA warp-group for loading data
|
| 146 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 147 |
+
|
| 148 |
+
// NOTES: only one thread (or warp) will be used
|
| 149 |
+
// We use the third warp, as warp 0/1 may be doing WGMMA with `BLOCK_M == 32`
|
| 150 |
+
if (warp_idx == kNumMathThreads / 32 + 2 and cute::elect_one_sync()) {
|
| 151 |
+
DG_STATIC_ASSERT(kNumTMAThreads >= 128, "Need at least 128 threads for TMA warp-group");
|
| 152 |
+
|
| 153 |
+
// Persistently schedule over blocks
|
| 154 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 155 |
+
// Assign TMA multicast number into A and B
|
| 156 |
+
// NOTES: there may be additional odd rows/columns or cases where multicast is not possible.
|
| 157 |
+
const bool is_tma_multicast_valid = scheduler.is_tma_multicast_valid(m_block_idx);
|
| 158 |
+
const uint32_t num_tma_multicast_a = (kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 159 |
+
const uint32_t num_tma_multicast_b = (not kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 160 |
+
DG_STATIC_ASSERT(kNumTMAMulticast <= 2, "Scheduler does not support > 2 TMA multicast");
|
| 161 |
+
|
| 162 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 163 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 164 |
+
// Wait consumer release
|
| 165 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 166 |
+
|
| 167 |
+
constexpr bool kWithGroupOffsetA = kGemmType == GemmType::MGroupedMasked;
|
| 168 |
+
auto& full_barrier = *full_barriers[stage_idx];
|
| 169 |
+
|
| 170 |
+
const auto m_idx = scheduler.template get_global_idx<kWithGroupOffsetA, sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
| 171 |
+
const auto n_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::K), sched::IndexType::MN>(shape_n, BLOCK_N, n_block_idx, m_block_idx);
|
| 172 |
+
|
| 173 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous or kMajorA == cute::UMMA::Major::K, "Invalid major");
|
| 174 |
+
uint32_t k_a_idx = scheduler.template get_global_idx<(kMajorA == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 175 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 176 |
+
uint32_t k_b_idx = scheduler.template get_global_idx<(kMajorB == cute::UMMA::Major::MN), sched::IndexType::K> (
|
| 177 |
+
shape_k, BLOCK_K, k_block_idx, m_block_idx);
|
| 178 |
+
|
| 179 |
+
// Issue TMAs
|
| 180 |
+
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
| 181 |
+
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
| 182 |
+
if constexpr (kMajorA == cute::UMMA::Major::K)
|
| 183 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 184 |
+
&tensor_map_a, &full_barrier, smem_a[stage_idx], k_a_idx, m_idx, num_tma_multicast_a, batch_idx);
|
| 185 |
+
if constexpr (kMajorA == cute::UMMA::Major::MN)
|
| 186 |
+
tma::copy<BLOCK_M, BLOCK_K, kSwizzleAMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 187 |
+
&tensor_map_a, &full_barrier, smem_a[stage_idx], m_idx, k_a_idx, num_tma_multicast_a, batch_idx);
|
| 188 |
+
if constexpr (kMajorB == cute::UMMA::Major::K)
|
| 189 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 190 |
+
&tensor_map_b, &full_barrier, smem_b[stage_idx], k_b_idx, n_idx, num_tma_multicast_b, batch_idx);
|
| 191 |
+
if constexpr (kMajorB == cute::UMMA::Major::MN)
|
| 192 |
+
tma::copy<BLOCK_N, BLOCK_K, kSwizzleBMode, cutlass::bfloat16_t, kIsBatchedMM>(
|
| 193 |
+
&tensor_map_b, &full_barrier, smem_b[stage_idx], n_idx, k_b_idx, num_tma_multicast_b, batch_idx);
|
| 194 |
+
full_barrier.arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
// To safely deconstruct distributed shared barriers, we need another round of empty waits
|
| 199 |
+
if constexpr (kNumTMAMulticast > 1) {
|
| 200 |
+
for (uint32_t i = 0; i < kNumStages; advance_pipeline(i))
|
| 201 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
} else {
|
| 205 |
+
// Math warp-groups for WGMMA
|
| 206 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 207 |
+
|
| 208 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 209 |
+
const auto math_wg_idx = __shfl_sync(0xffffffff, threadIdx.x / 128, 0);
|
| 210 |
+
|
| 211 |
+
// Merged stages only happens in NT normal GEMM cases
|
| 212 |
+
constexpr uint32_t BLOCK_ATOM_K = BLOCK_K / kNumStagesPerMerge;
|
| 213 |
+
auto a_desc = mma::sm90::make_gmma_desc<kMajorA, BLOCK_M, BLOCK_ATOM_K, kSwizzleAMode>(smem_a[0], math_wg_idx * WGMMA::M, 0);
|
| 214 |
+
auto b_desc = mma::sm90::make_gmma_desc<kMajorB, BLOCK_N, BLOCK_ATOM_K, kSwizzleBMode>(smem_b[0], 0, 0);
|
| 215 |
+
const uint32_t a_desc_lo = __shfl_sync(0xffffffff, a_desc.reg32_[0], 0);
|
| 216 |
+
const uint32_t b_desc_lo = __shfl_sync(0xffffffff, b_desc.reg32_[0], 0);
|
| 217 |
+
|
| 218 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 219 |
+
constexpr uint32_t WAVE_BLOCK_M = BLOCK_M <= WGMMA::M ? BLOCK_M : WGMMA::M * 2;
|
| 220 |
+
DG_STATIC_ASSERT(BLOCK_M % WAVE_BLOCK_M == 0, "Invalid block sizes");
|
| 221 |
+
float accum[WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M)] = {0};
|
| 222 |
+
|
| 223 |
+
// Pick threads whose WGMMA results are to be stored in shared memory
|
| 224 |
+
DG_STATIC_ASSERT(BLOCK_M >= 64 or kNumMathThreads == 128, "Only one math warp group for `BLOCK_M < 64`");
|
| 225 |
+
constexpr uint32_t kNumWGMMAStoreThreads = WAVE_BLOCK_M * (128 / WGMMA::M);
|
| 226 |
+
const bool do_wgmma_store = BLOCK_M >= 64 or warp_idx < kNumWGMMAStoreThreads / 32;
|
| 227 |
+
|
| 228 |
+
// Empty barrier arrival
|
| 229 |
+
auto empty_barrier_arrive = [&](uint32_t s) {
|
| 230 |
+
if constexpr (kNumTMAMulticast == 1) {
|
| 231 |
+
lane_idx == 0 ? empty_barriers[s]->arrive() : void();
|
| 232 |
+
} else {
|
| 233 |
+
auto target_cta = scheduler.is_peer_cta_alive ? lane_idx : cute::block_rank_in_cluster();
|
| 234 |
+
lane_idx < kNumTMAMulticast ? empty_barriers[s]->arrive(target_cta) : void();
|
| 235 |
+
}
|
| 236 |
+
};
|
| 237 |
+
|
| 238 |
+
// TODO: remove some useless computation for unaligned Ms
|
| 239 |
+
const auto num_total_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 240 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 241 |
+
const auto a_desc_base_lo = a_desc_lo + stage_idx * (SMEM_A_SIZE_PER_STAGE / 16);
|
| 242 |
+
const auto b_desc_base_lo = b_desc_lo + stage_idx * (SMEM_B_SIZE_PER_STAGE / 16);
|
| 243 |
+
|
| 244 |
+
// Wait TMA arrivals
|
| 245 |
+
full_barriers[stage_idx]->wait(phase);
|
| 246 |
+
|
| 247 |
+
// Commit WGMMA instructions
|
| 248 |
+
#pragma unroll
|
| 249 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M); ++ i)
|
| 250 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 251 |
+
ptx::warpgroup_arrive();
|
| 252 |
+
#pragma unroll
|
| 253 |
+
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
| 254 |
+
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
| 255 |
+
#pragma unroll
|
| 256 |
+
for (uint32_t k = 0; k < BLOCK_K / WGMMA::K; ++ k) {
|
| 257 |
+
const uint32_t atom_k_idx = k * WGMMA::K / BLOCK_ATOM_K;
|
| 258 |
+
a_desc.reg32_[0] = mma::sm90::advance_gmma_desc_lo<kMajorA, BLOCK_M, BLOCK_ATOM_K, kSwizzleAMode, nv_bfloat16>(
|
| 259 |
+
a_desc_base_lo, local_idx * WAVE_BLOCK_M, (k * WGMMA::K) % BLOCK_ATOM_K, atom_k_idx * BLOCK_M * BLOCK_ATOM_K);
|
| 260 |
+
b_desc.reg32_[0] = mma::sm90::advance_gmma_desc_lo<kMajorB, BLOCK_N, BLOCK_ATOM_K, kSwizzleBMode, nv_bfloat16>(
|
| 261 |
+
b_desc_base_lo, 0, (k * WGMMA::K) % BLOCK_ATOM_K, atom_k_idx * BLOCK_N * BLOCK_ATOM_K);
|
| 262 |
+
WGMMA::wgmma(a_desc, b_desc, shifted_accum, 1);
|
| 263 |
+
}
|
| 264 |
+
}
|
| 265 |
+
ptx::warpgroup_commit_batch();
|
| 266 |
+
#pragma unroll
|
| 267 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M); ++ i)
|
| 268 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 269 |
+
ptx::warpgroup_wait<0>();
|
| 270 |
+
|
| 271 |
+
// Notify barrier arrival
|
| 272 |
+
empty_barrier_arrive(stage_idx);
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
// TMA checks
|
| 276 |
+
constexpr uint32_t kNumElemBytes = sizeof(nv_bfloat16);
|
| 277 |
+
constexpr uint32_t TMA_D_BLOCK_N = kSwizzleDMode == 0 ? BLOCK_N : (kSwizzleDMode / kNumElemBytes);
|
| 278 |
+
constexpr uint32_t WGMMA_M_PER_WARP = WGMMA::M / 4;
|
| 279 |
+
DG_STATIC_ASSERT(BLOCK_M % 8 == 0, "Invalid swizzling atom");
|
| 280 |
+
DG_STATIC_ASSERT(BLOCK_N % TMA_D_BLOCK_N == 0 and BLOCK_N / TMA_D_BLOCK_N <= 32,
|
| 281 |
+
"Unaligned TMA store or too many TMA store instructions");
|
| 282 |
+
DG_STATIC_ASSERT(TMA_D_BLOCK_N % 8 == 0, "Invalid TMA block N");
|
| 283 |
+
|
| 284 |
+
// Skip WGMMA store for the unfilled parts
|
| 285 |
+
if (not do_wgmma_store)
|
| 286 |
+
continue;
|
| 287 |
+
|
| 288 |
+
// Wait last TMA store to be finished
|
| 289 |
+
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N)
|
| 290 |
+
cute::tma_store_wait<0>();
|
| 291 |
+
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 0);
|
| 292 |
+
|
| 293 |
+
if constexpr (cute::is_same_v<cd_dtype_t, cutlass::bfloat16_t>) {
|
| 294 |
+
// Write back to shared memory using STSM and issue TMA stores
|
| 295 |
+
DG_STATIC_ASSERT(kSwizzleDMode > 0, "Invalid swizzling type");
|
| 296 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum % 4 == 0, "Invalid STSM x2 vectorization");
|
| 297 |
+
#pragma unroll
|
| 298 |
+
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
| 299 |
+
auto m_offset = local_idx * WAVE_BLOCK_M;
|
| 300 |
+
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
| 301 |
+
#pragma unroll
|
| 302 |
+
for (auto i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 303 |
+
// Swizzle or padding into the correct address
|
| 304 |
+
uint8_t* smem_ptr = nullptr;
|
| 305 |
+
if constexpr (kSwizzleDMode > 0) {
|
| 306 |
+
// Calculate the swizzling atom offset and in-atom offset
|
| 307 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 308 |
+
auto atom_offset = i / (TMA_D_BLOCK_N / 8), in_atom_offset = i % (TMA_D_BLOCK_N / 8);
|
| 309 |
+
|
| 310 |
+
// Calculate the index of the bank group to be written in the atom
|
| 311 |
+
auto bank_group_index = in_atom_offset + lane_idx * (kSwizzleDMode / kNumBankGroupBytes);
|
| 312 |
+
|
| 313 |
+
// Reshape the atom in another view and swizzle
|
| 314 |
+
// - original: `(BLOCK_M, kSwizzleDMode / kNumBankGroupBytes)`
|
| 315 |
+
// - new: `(BLOCK_M * kSwizzleDMode / kNumBankGroupBytes / 8, 8)`
|
| 316 |
+
constexpr bool kHasShortcut = (kSwizzleDMode / kNumBankGroupBytes) == 8;
|
| 317 |
+
auto row = kHasShortcut ? (in_atom_offset / 8 + lane_idx) : (bank_group_index / 8);
|
| 318 |
+
auto col = kHasShortcut ? (in_atom_offset) : (bank_group_index % 8);
|
| 319 |
+
col ^= row % (kSwizzleDMode / 16);
|
| 320 |
+
|
| 321 |
+
// Add back into the base pointer
|
| 322 |
+
// NOTES: think twice before modifying this, as changes may affect the number of instructions
|
| 323 |
+
smem_ptr = reinterpret_cast<uint8_t*>(smem_d) + // Base pointer
|
| 324 |
+
warp_idx * (WGMMA_M_PER_WARP * kSwizzleDMode) + // Warp offset
|
| 325 |
+
m_offset * kSwizzleDMode + // Wave offset
|
| 326 |
+
atom_offset * BLOCK_M * kSwizzleDMode + // Swizzle atom offset (constants)
|
| 327 |
+
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
| 328 |
+
} else {
|
| 329 |
+
// No swizzling
|
| 330 |
+
smem_ptr = reinterpret_cast<uint8_t*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx) * BLOCK_N + i * 8);
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// NOTES: only 16 lanes' addresses are used
|
| 334 |
+
ptx::SM90_U32x2_STSM_N<nv_bfloat162>::copy(
|
| 335 |
+
__float22bfloat162_rn({shifted_accum[i * 4 + 0], shifted_accum[i * 4 + 1]}),
|
| 336 |
+
__float22bfloat162_rn({shifted_accum[i * 4 + 2], shifted_accum[i * 4 + 3]}),
|
| 337 |
+
smem_ptr
|
| 338 |
+
);
|
| 339 |
+
}
|
| 340 |
+
}
|
| 341 |
+
} else {
|
| 342 |
+
// Use `st.shared` if STSM is not available
|
| 343 |
+
#pragma unroll
|
| 344 |
+
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
| 345 |
+
auto m_offset = local_idx * WAVE_BLOCK_M;
|
| 346 |
+
auto shifted_accum = accum + WGMMA::kNumAccum * local_idx;
|
| 347 |
+
auto smem_d_0 = reinterpret_cast<float2*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx / 4 + 0) * BLOCK_N + (lane_idx % 4) * 2);
|
| 348 |
+
auto smem_d_1 = reinterpret_cast<float2*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx / 4 + 8) * BLOCK_N + (lane_idx % 4) * 2);
|
| 349 |
+
#pragma unroll
|
| 350 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 351 |
+
ptx::st_shared(smem_d_0 + i * 4, make_float2(shifted_accum[i * 4 + 0], shifted_accum[i * 4 + 1]));
|
| 352 |
+
ptx::st_shared(smem_d_1 + i * 4, make_float2(shifted_accum[i * 4 + 2], shifted_accum[i * 4 + 3]));
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
+
cute::tma_store_fence();
|
| 357 |
+
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 0);
|
| 358 |
+
|
| 359 |
+
// Use TMA store to write back to global memory
|
| 360 |
+
const auto m_idx = scheduler.template get_global_idx<(not is_m_grouped_contiguous(kGemmType)), sched::IndexType::MN>(shape_m, BLOCK_M, m_block_idx);
|
| 361 |
+
DG_STATIC_ASSERT(kNumWGMMAStoreThreads >= BLOCK_N / TMA_D_BLOCK_N, "Too many TMA blocks");
|
| 362 |
+
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N) {
|
| 363 |
+
auto in_block_n_offset = threadIdx.x * TMA_D_BLOCK_N;
|
| 364 |
+
auto smem_ptr = smem_d + in_block_n_offset * BLOCK_M;
|
| 365 |
+
if constexpr (kGemmType == GemmType::Batched) {
|
| 366 |
+
cute::SM90_TMA_STORE_3D::copy(&tensor_map_cd, smem_ptr,
|
| 367 |
+
n_block_idx * BLOCK_N + in_block_n_offset,
|
| 368 |
+
m_idx, scheduler.current_group_idx);
|
| 369 |
+
} else {
|
| 370 |
+
using cute_tma_t = cute::conditional_t<kWithAccumulation,
|
| 371 |
+
cute::SM90_TMA_REDUCE_ADD_2D, cute::SM90_TMA_STORE_2D>;
|
| 372 |
+
cute_tma_t::copy(&tensor_map_cd, smem_ptr,
|
| 373 |
+
n_block_idx * BLOCK_N + in_block_n_offset, m_idx);
|
| 374 |
+
}
|
| 375 |
+
cute::tma_store_arrive();
|
| 376 |
+
}
|
| 377 |
+
__syncwarp();
|
| 378 |
+
}
|
| 379 |
+
}
|
| 380 |
+
#else
|
| 381 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 382 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
| 383 |
+
#endif
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
}; // namespace deep_gemm
|
| 387 |
+
|
| 388 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_bmk_bnk_mn.cuh
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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#include <cutlass/arch/barrier.h>
|
| 5 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 6 |
+
|
| 7 |
+
#include <deep_gemm/common/math.cuh>
|
| 8 |
+
#include <deep_gemm/common/utils.cuh>
|
| 9 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 10 |
+
#include <deep_gemm/common/types.cuh>
|
| 11 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 12 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 13 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 16 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 17 |
+
|
| 18 |
+
namespace deep_gemm {
|
| 19 |
+
|
| 20 |
+
template <uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 21 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 22 |
+
uint32_t kSplitFactor,
|
| 23 |
+
uint32_t kNumStages,
|
| 24 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads>
|
| 25 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1) void
|
| 26 |
+
sm90_bmn_bnk_mn_gemm_impl(const uint32_t shape_s,
|
| 27 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 28 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 29 |
+
float *d) {
|
| 30 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
| 31 |
+
// Types
|
| 32 |
+
using WGMMA = typename mma::sm90::BF16MMASelector<BLOCK_N>::type;
|
| 33 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 34 |
+
DG_STATIC_ASSERT(BLOCK_M % WGMMA::M == 0, "Invalid block size");
|
| 35 |
+
|
| 36 |
+
// Shared memory
|
| 37 |
+
static constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(__nv_bfloat16);
|
| 38 |
+
static constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(__nv_bfloat16);
|
| 39 |
+
|
| 40 |
+
// Configs
|
| 41 |
+
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 42 |
+
const uint32_t lane_idx = ptx::get_lane_idx();
|
| 43 |
+
DG_STATIC_ASSERT(BLOCK_M == 128, "Invalid block M");
|
| 44 |
+
DG_STATIC_ASSERT(kNumTMAThreads == 128, "Invalid number of TMA threads");
|
| 45 |
+
DG_STATIC_ASSERT(kNumMathThreads == 256, "Invalid number of math threads");
|
| 46 |
+
|
| 47 |
+
// Prefetch TMA descriptors at the very beginning
|
| 48 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 49 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 50 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 51 |
+
}
|
| 52 |
+
__syncwarp();
|
| 53 |
+
|
| 54 |
+
// Align to 1024 bytes for swizzle-128B
|
| 55 |
+
// Fill shared memory pointers
|
| 56 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 57 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 58 |
+
return reinterpret_cast<__nv_bfloat16*>(smem_buffer + (i * SMEM_A_SIZE_PER_STAGE));
|
| 59 |
+
});
|
| 60 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 61 |
+
return reinterpret_cast<__nv_bfloat16*>(smem_buffer + (kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE));
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
// Fill barriers
|
| 65 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 66 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 67 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 68 |
+
|
| 69 |
+
// Initialize barriers
|
| 70 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 71 |
+
#pragma unroll
|
| 72 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 73 |
+
full_barriers[i]->init(1);
|
| 74 |
+
empty_barriers[i]->init(kNumMathThreads);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Make initialized barrier visible in async proxy
|
| 78 |
+
cutlass::arch::fence_barrier_init();
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Synchronize all threads to make barrier visible in normal memory model
|
| 82 |
+
__syncthreads();
|
| 83 |
+
|
| 84 |
+
// Register reconfigurations
|
| 85 |
+
constexpr uint32_t kNumTMARegisters = 40;
|
| 86 |
+
constexpr uint32_t kNumMathRegisters = 232;
|
| 87 |
+
|
| 88 |
+
// Block indices
|
| 89 |
+
const uint32_t num_n_blocks = math::ceil_div(SHAPE_N, BLOCK_N);
|
| 90 |
+
const uint32_t num_mn_blocks = num_n_blocks * math::ceil_div(SHAPE_M, BLOCK_M);
|
| 91 |
+
const uint32_t mn_block_idx = blockIdx.x % num_mn_blocks;
|
| 92 |
+
const uint32_t sk_block_idx = blockIdx.x / num_mn_blocks;
|
| 93 |
+
const uint32_t n_block_idx = mn_block_idx % num_n_blocks;
|
| 94 |
+
const uint32_t m_block_idx = mn_block_idx / num_n_blocks;
|
| 95 |
+
const uint32_t num_total_stages = cute::min(kSplitFactor, shape_s * (SHAPE_K / BLOCK_K) - sk_block_idx * kSplitFactor);
|
| 96 |
+
|
| 97 |
+
// Wait for primary kernel completion
|
| 98 |
+
cudaGridDependencySynchronize();
|
| 99 |
+
|
| 100 |
+
if (warp_idx >= kNumMathThreads / 32) {
|
| 101 |
+
// TMA warp-group for loading data
|
| 102 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 103 |
+
|
| 104 |
+
// NOTES: only one thread (or warp) will be used
|
| 105 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 106 |
+
// Persistently schedule over blocks
|
| 107 |
+
#pragma unroll
|
| 108 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 109 |
+
// Wait consumer release
|
| 110 |
+
const auto stage_idx = s % kNumStages;
|
| 111 |
+
empty_barriers[stage_idx]->wait((s / kNumStages + 1) & 1);
|
| 112 |
+
|
| 113 |
+
auto& full_barrier = *full_barriers[stage_idx];
|
| 114 |
+
const uint32_t sk_idx = (sk_block_idx * kSplitFactor + s) * BLOCK_K;
|
| 115 |
+
const uint32_t k_idx = sk_idx % SHAPE_K;
|
| 116 |
+
const uint32_t s_idx = sk_idx / SHAPE_K;
|
| 117 |
+
|
| 118 |
+
constexpr uint32_t kSwizzle = BLOCK_K * sizeof(nv_bfloat16);
|
| 119 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzle>(
|
| 120 |
+
&tensor_map_a, &full_barrier, smem_a[stage_idx], k_idx, m_block_idx * BLOCK_M + s_idx * SHAPE_M, 1);
|
| 121 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzle>(
|
| 122 |
+
&tensor_map_b, &full_barrier, smem_b[stage_idx], k_idx, n_block_idx * BLOCK_N + s_idx * SHAPE_N, 1);
|
| 123 |
+
full_barrier.arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
} else {
|
| 127 |
+
// Math warp-groups for WGMMA
|
| 128 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 129 |
+
|
| 130 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 131 |
+
const auto math_wg_idx = __shfl_sync(0xffffffff, threadIdx.x / 128, 0);
|
| 132 |
+
float accum[WGMMA::kNumAccum] = {0};
|
| 133 |
+
|
| 134 |
+
// Launch MMAs
|
| 135 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 136 |
+
// Wait TMA arrivals
|
| 137 |
+
const auto stage_idx = s % kNumStages;
|
| 138 |
+
full_barriers[stage_idx]->wait((s / kNumStages) & 1);
|
| 139 |
+
|
| 140 |
+
// Commit WGMMA instructions
|
| 141 |
+
#pragma unroll
|
| 142 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 143 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 144 |
+
ptx::warpgroup_arrive();
|
| 145 |
+
#pragma unroll
|
| 146 |
+
for (uint32_t k = 0; k < BLOCK_K / WGMMA::K; ++ k) {
|
| 147 |
+
auto desc_a = mma::sm90::make_smem_desc(smem_a[stage_idx] + (math_wg_idx * WGMMA::M) * BLOCK_K + k * WGMMA::K, 1);
|
| 148 |
+
auto desc_b = mma::sm90::make_smem_desc(smem_b[stage_idx] + k * WGMMA::K, 1);
|
| 149 |
+
WGMMA::wgmma(desc_a, desc_b, accum, 1);
|
| 150 |
+
}
|
| 151 |
+
ptx::warpgroup_commit_batch();
|
| 152 |
+
#pragma unroll
|
| 153 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 154 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 155 |
+
ptx::warpgroup_wait<0>();
|
| 156 |
+
|
| 157 |
+
// Notify barrier arrival at the last warpgroup wave
|
| 158 |
+
empty_barriers[stage_idx]->arrive();
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
const auto row = m_block_idx * BLOCK_M + warp_idx * 16 + lane_idx / 4;
|
| 162 |
+
const auto col = n_block_idx * BLOCK_N + (lane_idx % 4) * 2;
|
| 163 |
+
#pragma unroll
|
| 164 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 165 |
+
if (col + i * 8 >= SHAPE_N)
|
| 166 |
+
break;
|
| 167 |
+
if (row < SHAPE_M) {
|
| 168 |
+
atomicAdd(reinterpret_cast<float2*>(d + (row + 0) * SHAPE_N + col + i * 8),
|
| 169 |
+
make_float2(accum[i * 4 + 0], accum[i * 4 + 1]));
|
| 170 |
+
}
|
| 171 |
+
if (row + 8 < SHAPE_M) {
|
| 172 |
+
atomicAdd(reinterpret_cast<float2*>(d + (row + 8) * SHAPE_N + col + i * 8),
|
| 173 |
+
make_float2(accum[i * 4 + 2], accum[i * 4 + 3]));
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
#else
|
| 178 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 179 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
| 180 |
+
#endif
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
}; // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_gemm_1d1d.cuh
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#pragma clang diagnostic push
|
| 4 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 5 |
+
|
| 6 |
+
#include <cutlass/arch/barrier.h>
|
| 7 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 8 |
+
|
| 9 |
+
#include <cute/int_tuple.hpp>
|
| 10 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 11 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 12 |
+
#include <cute/arch/copy_sm90_tma.hpp>
|
| 13 |
+
|
| 14 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 15 |
+
#include <deep_gemm/common/math.cuh>
|
| 16 |
+
#include <deep_gemm/common/utils.cuh>
|
| 17 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 18 |
+
#include <deep_gemm/common/types.cuh>
|
| 19 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 20 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 21 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 22 |
+
#include <deep_gemm/ptx/tma.cuh>
|
| 23 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 24 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 25 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 26 |
+
|
| 27 |
+
namespace deep_gemm {
|
| 28 |
+
|
| 29 |
+
template <uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 30 |
+
uint32_t kNumGroups,
|
| 31 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 32 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode,
|
| 33 |
+
uint32_t kNumStages,
|
| 34 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
| 35 |
+
uint32_t kNumTMAMulticast, bool kIsTMAMulticastOnA,
|
| 36 |
+
uint32_t kNumSMs,
|
| 37 |
+
GemmType kGemmType, typename cd_dtype_t>
|
| 38 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1) void
|
| 39 |
+
sm90_fp8_gemm_1d1d_impl(__nv_fp8_e4m3* gmem_a_ptr, __nv_fp8_e4m3* gmem_b_ptr,
|
| 40 |
+
int* grouped_layout,
|
| 41 |
+
cute::TmaDescriptor* tensor_map_buffer,
|
| 42 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 43 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a_base,
|
| 44 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b_base,
|
| 45 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfa,
|
| 46 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfb,
|
| 47 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_cd) {
|
| 48 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
| 49 |
+
// Scaling checks
|
| 50 |
+
DG_STATIC_ASSERT(kNumTMAThreads == 128 and kNumMathThreads % 128 == 0, "Invalid Threads");
|
| 51 |
+
DG_STATIC_ASSERT(BLOCK_K == 128, "Only support per-128-channel FP8 scaling");
|
| 52 |
+
DG_STATIC_ASSERT(cute::is_same_v<cd_dtype_t, float>, "Invalid C/D data dtype");
|
| 53 |
+
DG_STATIC_ASSERT(kGemmType == GemmType::Normal or kGemmType == GemmType::KGroupedContiguous, "Invalid GEMM type");
|
| 54 |
+
|
| 55 |
+
// Types
|
| 56 |
+
using WGMMA = typename mma::sm90::FP8MMASelector<BLOCK_N>::type;
|
| 57 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 58 |
+
DG_STATIC_ASSERT(BLOCK_M % WGMMA::M == 0, "Invalid block size");
|
| 59 |
+
|
| 60 |
+
// Overwrite shape constants if the compiler gives
|
| 61 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 62 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 63 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 64 |
+
|
| 65 |
+
// Shared memory
|
| 66 |
+
static constexpr uint32_t SMEM_TENSOR_MAP_SIZE = (kGemmType == GemmType::KGroupedContiguous ? sizeof(cute::TmaDescriptor) * 2 : 0);
|
| 67 |
+
static constexpr uint32_t SMEM_D_SIZE = BLOCK_M * BLOCK_N * sizeof(float);
|
| 68 |
+
static constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 69 |
+
static constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 70 |
+
static constexpr uint32_t SMEM_SFA_SIZE_PER_STAGE = BLOCK_M * sizeof(float);
|
| 71 |
+
static constexpr uint32_t SMEM_SFB_SIZE_PER_STAGE = BLOCK_N * sizeof(float);
|
| 72 |
+
static constexpr uint32_t ALIGNED_SMEM_SFB_SIZE_PER_STAGE = math::constexpr_align(SMEM_SFB_SIZE_PER_STAGE, 128u);
|
| 73 |
+
DG_STATIC_ASSERT(SMEM_SFA_SIZE_PER_STAGE % 128 == 0, "Invalid TMA alignment");
|
| 74 |
+
|
| 75 |
+
// Configs
|
| 76 |
+
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 77 |
+
const uint32_t lane_idx = threadIdx.x % 32;
|
| 78 |
+
|
| 79 |
+
// Prefetch TMA descriptors at the very beginning
|
| 80 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 81 |
+
cute::prefetch_tma_descriptor(&tensor_map_a_base);
|
| 82 |
+
cute::prefetch_tma_descriptor(&tensor_map_b_base);
|
| 83 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfa);
|
| 84 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfb);
|
| 85 |
+
cute::prefetch_tma_descriptor(&tensor_map_cd);
|
| 86 |
+
}
|
| 87 |
+
__syncwarp();
|
| 88 |
+
|
| 89 |
+
// Align to 1024 bytes for swizzle-128B
|
| 90 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 91 |
+
DG_STATIC_ASSERT(SMEM_D_SIZE % 1024 == 0, "Shared memory of A/B must be aligned to 1024 bytes");
|
| 92 |
+
|
| 93 |
+
// Tensor maps on shared and global memory
|
| 94 |
+
auto smem_tensor_map_a = reinterpret_cast<cute::TmaDescriptor*>(smem_buffer);
|
| 95 |
+
auto smem_tensor_map_b = smem_tensor_map_a + 1;
|
| 96 |
+
auto gmem_tensor_map_a = tensor_map_buffer + blockIdx.x * 2;
|
| 97 |
+
auto gmem_tensor_map_b = gmem_tensor_map_a + 1;
|
| 98 |
+
|
| 99 |
+
// Data on shared memory
|
| 100 |
+
auto smem_d = reinterpret_cast<float*>(smem_buffer + SMEM_TENSOR_MAP_SIZE);
|
| 101 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 102 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + (SMEM_TENSOR_MAP_SIZE + SMEM_D_SIZE + i * SMEM_A_SIZE_PER_STAGE));
|
| 103 |
+
});
|
| 104 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 105 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + (SMEM_TENSOR_MAP_SIZE + SMEM_D_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE));
|
| 106 |
+
});
|
| 107 |
+
constexpr auto SMEM_SF_OFFSET = SMEM_TENSOR_MAP_SIZE + SMEM_D_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 108 |
+
auto smem_sfa = utils::PatternVisitor([&](const uint32_t& i) {
|
| 109 |
+
return reinterpret_cast<float*>(smem_buffer + (SMEM_SF_OFFSET + i * SMEM_SFA_SIZE_PER_STAGE));
|
| 110 |
+
});
|
| 111 |
+
auto smem_sfb = utils::PatternVisitor([&](const uint32_t& i) {
|
| 112 |
+
return reinterpret_cast<float*>(smem_buffer + (SMEM_SF_OFFSET + kNumStages * SMEM_SFA_SIZE_PER_STAGE + i * ALIGNED_SMEM_SFB_SIZE_PER_STAGE));
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
// Barriers on shared memory
|
| 116 |
+
constexpr auto SMEM_BARRIER_OFFSET = SMEM_SF_OFFSET + kNumStages * (SMEM_SFA_SIZE_PER_STAGE + ALIGNED_SMEM_SFB_SIZE_PER_STAGE);
|
| 117 |
+
auto full_barriers = utils::PatternVisitor([&](const uint32_t& i) {
|
| 118 |
+
return reinterpret_cast<Barrier*>(smem_buffer + (SMEM_BARRIER_OFFSET + i * static_cast<uint32_t>(sizeof(Barrier))));
|
| 119 |
+
});
|
| 120 |
+
auto empty_barriers = utils::PatternVisitor([&](const uint32_t& i) {
|
| 121 |
+
return reinterpret_cast<Barrier*>(smem_buffer + (SMEM_BARRIER_OFFSET + (kNumStages + i) * static_cast<uint32_t>(sizeof(Barrier))));
|
| 122 |
+
});
|
| 123 |
+
|
| 124 |
+
if (warp_idx == kNumMathThreads / 32 + 1 and cute::elect_one_sync()) {
|
| 125 |
+
// Load tensormap A/B to shared memory
|
| 126 |
+
if constexpr (kGemmType == GemmType::KGroupedContiguous) {
|
| 127 |
+
*smem_tensor_map_a = tensor_map_a_base;
|
| 128 |
+
*smem_tensor_map_b = tensor_map_b_base;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// Initialize barriers
|
| 132 |
+
// NOTES: we always use `lane_idx` to arrive for the `lane_idx`-th CTA in the cluster,
|
| 133 |
+
// even with TMA multicast disabled, we want to make the behavior aligned
|
| 134 |
+
#pragma unroll
|
| 135 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 136 |
+
full_barriers[i]->init(1);
|
| 137 |
+
empty_barriers[i]->init(kNumTMAMulticast * kNumMathThreads / 32);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// Make initialized barrier visible in async proxy
|
| 141 |
+
cutlass::arch::fence_barrier_init();
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// Synchronize all threads to make barrier visible in normal memory model
|
| 145 |
+
(kNumTMAMulticast > 1) ? cute::cluster_sync() : __syncthreads();
|
| 146 |
+
|
| 147 |
+
// Pipeline unroll control
|
| 148 |
+
constexpr uint32_t kNumPipelineUnrolls = (kGemmType == GemmType::KGroupedContiguous ? 0 : kNumStages);
|
| 149 |
+
|
| 150 |
+
// Register reconfigurations (more math registers are needed with unrolling)
|
| 151 |
+
constexpr uint32_t kNumTMARegisters = (kNumPipelineUnrolls == 0 ? 40 : 24);
|
| 152 |
+
constexpr uint32_t kNumMathRegisters = (kNumPipelineUnrolls == 0 ? 232 : 240);
|
| 153 |
+
|
| 154 |
+
// Wait for primary kernel completion
|
| 155 |
+
cudaGridDependencySynchronize();
|
| 156 |
+
|
| 157 |
+
// Block scheduler
|
| 158 |
+
uint32_t m_block_idx, n_block_idx;
|
| 159 |
+
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumTMAMulticast, kIsTMAMulticastOnA, kNumSMs, 128u>(shape_m, shape_n, shape_k, grouped_layout);
|
| 160 |
+
|
| 161 |
+
// TMA and MMA pipeline
|
| 162 |
+
const auto get_pipeline = [=](const uint32_t& iter_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 163 |
+
return {iter_idx % kNumStages, (iter_idx / kNumStages) & 1}; // Pipeline stage and phase
|
| 164 |
+
};
|
| 165 |
+
uint32_t iter_idx = 0;
|
| 166 |
+
|
| 167 |
+
if (warp_idx >= kNumMathThreads / 32) {
|
| 168 |
+
// TMA warp-group for loading data
|
| 169 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 170 |
+
|
| 171 |
+
// NOTES: only one thread (or warp) will be used
|
| 172 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 173 |
+
uint32_t last_group_idx = kNumGroups;
|
| 174 |
+
|
| 175 |
+
// Persistently schedule over blocks
|
| 176 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 177 |
+
// Assign TMA multicast number into A and B
|
| 178 |
+
// NOTES: there may be additional odd rows/columns or cases where multicast is not possible.
|
| 179 |
+
const bool is_tma_multicast_valid = scheduler.is_tma_multicast_valid(m_block_idx);
|
| 180 |
+
const uint32_t num_tma_multicast_a = (kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 181 |
+
const uint32_t num_tma_multicast_b = (not kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 182 |
+
DG_STATIC_ASSERT(kNumTMAMulticast <= 2, "Scheduler does not support > 2 TMA multicast");
|
| 183 |
+
|
| 184 |
+
const uint32_t num_k_blocks = math::ceil_div(scheduler.current_shape_k, BLOCK_K);
|
| 185 |
+
const uint32_t m_idx = m_block_idx * BLOCK_M;
|
| 186 |
+
const uint32_t n_idx = n_block_idx * BLOCK_N;
|
| 187 |
+
|
| 188 |
+
if (kGemmType == GemmType::KGroupedContiguous && last_group_idx != scheduler.current_group_idx) {
|
| 189 |
+
last_group_idx = scheduler.current_group_idx;
|
| 190 |
+
|
| 191 |
+
// Directly update current tensor map
|
| 192 |
+
const uint64_t current_k_offset = scheduler.current_k_cumsum;
|
| 193 |
+
ptx::tensor_map_replace_global_addr_in_smem(smem_tensor_map_a, gmem_a_ptr + current_k_offset * shape_m);
|
| 194 |
+
ptx::tensor_map_replace_global_addr_in_smem(smem_tensor_map_b, gmem_b_ptr + current_k_offset * shape_n);
|
| 195 |
+
ptx::tensor_map_replace_global_inner_dim_stride_in_smem(smem_tensor_map_a, scheduler.current_shape_k, scheduler.current_shape_k);
|
| 196 |
+
ptx::tensor_map_replace_global_inner_dim_stride_in_smem(smem_tensor_map_b, scheduler.current_shape_k, scheduler.current_shape_k);
|
| 197 |
+
*(gmem_tensor_map_a) = *(smem_tensor_map_a);
|
| 198 |
+
*(gmem_tensor_map_b) = *(smem_tensor_map_b);
|
| 199 |
+
ptx::tensor_map_release_gpu();
|
| 200 |
+
|
| 201 |
+
// Immediately acquire current tensor map
|
| 202 |
+
ptx::tensor_map_acquire_gpu(gmem_tensor_map_a);
|
| 203 |
+
ptx::tensor_map_acquire_gpu(gmem_tensor_map_b);
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
#pragma unroll kNumPipelineUnrolls
|
| 207 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_k_blocks; ++ k_block_idx) {
|
| 208 |
+
// Wait consumer release
|
| 209 |
+
CUTE_TIE_DECL(get_pipeline(iter_idx ++), stage_idx, phase);
|
| 210 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 211 |
+
|
| 212 |
+
// Issue TMA
|
| 213 |
+
auto& full_barrier = *full_barriers[stage_idx];
|
| 214 |
+
const uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 215 |
+
const uint32_t sf_k_idx = scheduler.current_sf_k_cumsum + k_block_idx;
|
| 216 |
+
const auto tensor_map_a_ptr = (kGemmType == GemmType::KGroupedContiguous ? gmem_tensor_map_a : &tensor_map_a_base);
|
| 217 |
+
const auto tensor_map_b_ptr = (kGemmType == GemmType::KGroupedContiguous ? gmem_tensor_map_b : &tensor_map_b_base);
|
| 218 |
+
tma::copy<BLOCK_M, BLOCK_K, 0>(&tensor_map_sfa, &full_barrier, smem_sfa[stage_idx], m_idx, sf_k_idx, num_tma_multicast_a);
|
| 219 |
+
tma::copy<BLOCK_N, BLOCK_K, 0>(&tensor_map_sfb, &full_barrier, smem_sfb[stage_idx], n_idx, sf_k_idx, num_tma_multicast_b);
|
| 220 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode>(tensor_map_a_ptr, &full_barrier, smem_a[stage_idx], k_idx, m_idx, num_tma_multicast_a);
|
| 221 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode>(tensor_map_b_ptr, &full_barrier, smem_b[stage_idx], k_idx, n_idx, num_tma_multicast_b);
|
| 222 |
+
full_barrier.arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE + SMEM_SFA_SIZE_PER_STAGE + SMEM_SFB_SIZE_PER_STAGE);
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
// To safely deconstruct distributed shared barriers, we need another round of empty waits
|
| 227 |
+
if constexpr (kNumTMAMulticast > 1) {
|
| 228 |
+
#pragma unroll
|
| 229 |
+
for (uint32_t s = 0; s < kNumStages; ++ s) {
|
| 230 |
+
CUTE_TIE_DECL(get_pipeline(iter_idx ++), stage_idx, phase);
|
| 231 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
} else {
|
| 236 |
+
// Math warp-groups for WGMMA
|
| 237 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 238 |
+
|
| 239 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 240 |
+
const auto math_wg_idx = __shfl_sync(0xffffffff, threadIdx.x / 128, 0);
|
| 241 |
+
const auto row_idx = lane_idx / 4, col_idx = lane_idx % 4;
|
| 242 |
+
const auto r_0 = warp_idx * 16 + row_idx, r_1 = r_0 + 8;
|
| 243 |
+
|
| 244 |
+
// Persistently schedule over blocks
|
| 245 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 246 |
+
// Accumulation for WGMMA or CUDA promotion
|
| 247 |
+
DG_STATIC_ASSERT(BLOCK_M == WGMMA::M * (BLOCK_M <= 64 ? 1 : 2), "Invalid block sizes");
|
| 248 |
+
const uint32_t current_shape_k = (kGemmType == GemmType::KGroupedContiguous ? scheduler.current_shape_k : shape_k);
|
| 249 |
+
const uint32_t current_group_idx = (kGemmType == GemmType::KGroupedContiguous ? scheduler.current_group_idx : 0);
|
| 250 |
+
const uint32_t num_k_blocks = math::ceil_div(current_shape_k, BLOCK_K);
|
| 251 |
+
float accum[WGMMA::kNumAccum], final_accum[WGMMA::kNumAccum] = {0};
|
| 252 |
+
float2 scales_b[WGMMA::kNumAccum / 4];
|
| 253 |
+
|
| 254 |
+
// Empty barrier arrival
|
| 255 |
+
auto empty_barrier_arrive = [&](uint32_t s) {
|
| 256 |
+
if constexpr (kNumTMAMulticast == 1) {
|
| 257 |
+
lane_idx == 0 ? empty_barriers[s]->arrive() : void();
|
| 258 |
+
} else {
|
| 259 |
+
auto target_cta = scheduler.is_peer_cta_alive ? lane_idx : cute::block_rank_in_cluster();
|
| 260 |
+
lane_idx < kNumTMAMulticast ? empty_barriers[s]->arrive(target_cta) : void();
|
| 261 |
+
}
|
| 262 |
+
};
|
| 263 |
+
|
| 264 |
+
#pragma unroll kNumPipelineUnrolls
|
| 265 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_k_blocks; ++ k_block_idx) {
|
| 266 |
+
// Wait TMA arrivals
|
| 267 |
+
CUTE_TIE_DECL(get_pipeline(iter_idx ++), stage_idx, phase);
|
| 268 |
+
full_barriers[stage_idx]->wait(phase);
|
| 269 |
+
|
| 270 |
+
// Read A scales
|
| 271 |
+
// NOTES: all shared memory read must be prior to `warpgroup_arrive` to avoid next scheduled block polluting the results
|
| 272 |
+
auto scale_a_0 = ptx::ld_shared(smem_sfa[stage_idx] + r_0);
|
| 273 |
+
auto scale_a_1 = ptx::ld_shared(smem_sfa[stage_idx] + r_1);
|
| 274 |
+
|
| 275 |
+
// Read B scales
|
| 276 |
+
#pragma unroll
|
| 277 |
+
for (int i = 0; i < WGMMA::kNumAccum / 4; ++i)
|
| 278 |
+
scales_b[i] = ptx::ld_shared(reinterpret_cast<float2*>(smem_sfb[stage_idx] + i * 8 + col_idx * 2));
|
| 279 |
+
|
| 280 |
+
// Commit WGMMA instructions
|
| 281 |
+
#pragma unroll
|
| 282 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 283 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 284 |
+
ptx::warpgroup_arrive();
|
| 285 |
+
#pragma unroll
|
| 286 |
+
for (uint32_t k = 0; k < BLOCK_K / WGMMA::K; ++ k) {
|
| 287 |
+
auto desc_a = mma::sm90::make_smem_desc(smem_a[stage_idx] + math_wg_idx * WGMMA::M * BLOCK_K + k * WGMMA::K, 1);
|
| 288 |
+
auto desc_b = mma::sm90::make_smem_desc(smem_b[stage_idx] + k * WGMMA::K, 1);
|
| 289 |
+
WGMMA::wgmma(desc_a, desc_b, accum, k);
|
| 290 |
+
}
|
| 291 |
+
ptx::warpgroup_commit_batch();
|
| 292 |
+
#pragma unroll
|
| 293 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 294 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 295 |
+
ptx::warpgroup_wait<0>();
|
| 296 |
+
|
| 297 |
+
// Notify barrier arrival
|
| 298 |
+
empty_barrier_arrive(stage_idx);
|
| 299 |
+
|
| 300 |
+
// Promote with scales
|
| 301 |
+
#pragma unroll
|
| 302 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 303 |
+
const float &scale_b_0 = scales_b[i].x;
|
| 304 |
+
const float &scale_b_1 = scales_b[i].y;
|
| 305 |
+
final_accum[i * 4 + 0] += scale_a_0 * scale_b_0 * accum[i * 4 + 0];
|
| 306 |
+
final_accum[i * 4 + 1] += scale_a_0 * scale_b_1 * accum[i * 4 + 1];
|
| 307 |
+
final_accum[i * 4 + 2] += scale_a_1 * scale_b_0 * accum[i * 4 + 2];
|
| 308 |
+
final_accum[i * 4 + 3] += scale_a_1 * scale_b_1 * accum[i * 4 + 3];
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
// Flush previous stores
|
| 313 |
+
if (warp_idx % 4 == 0 and cute::elect_one_sync())
|
| 314 |
+
cute::tma_store_wait<0>();
|
| 315 |
+
cutlass::arch::NamedBarrier::sync(128, math_wg_idx);
|
| 316 |
+
|
| 317 |
+
// Store to D shared memory
|
| 318 |
+
const auto smem_d_0 = reinterpret_cast<float2*>(smem_d + r_0 * BLOCK_N + col_idx * 2);
|
| 319 |
+
const auto smem_d_1 = reinterpret_cast<float2*>(smem_d + r_1 * BLOCK_N + col_idx * 2);
|
| 320 |
+
#pragma unroll
|
| 321 |
+
for (auto i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 322 |
+
ptx::st_shared(smem_d_0 + i * 4, {final_accum[i * 4 + 0], final_accum[i * 4 + 1]});
|
| 323 |
+
ptx::st_shared(smem_d_1 + i * 4, {final_accum[i * 4 + 2], final_accum[i * 4 + 3]});
|
| 324 |
+
}
|
| 325 |
+
cute::tma_store_fence();
|
| 326 |
+
cutlass::arch::NamedBarrier::sync(128, math_wg_idx);
|
| 327 |
+
|
| 328 |
+
// Use TMA store to write back to global memory
|
| 329 |
+
if (warp_idx % 4 == 0 and cute::elect_one_sync()) {
|
| 330 |
+
cute::SM90_TMA_REDUCE_ADD_2D::copy(
|
| 331 |
+
&tensor_map_cd, smem_d_0, n_block_idx * BLOCK_N,
|
| 332 |
+
current_group_idx * shape_m + m_block_idx * BLOCK_M + r_0);
|
| 333 |
+
cute::tma_store_arrive();
|
| 334 |
+
}
|
| 335 |
+
__syncwarp();
|
| 336 |
+
}
|
| 337 |
+
}
|
| 338 |
+
#else
|
| 339 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 340 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
| 341 |
+
#endif
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
}; // namespace deep_gemm
|
| 345 |
+
|
| 346 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_gemm_1d2d.cuh
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#pragma clang diagnostic push
|
| 4 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 5 |
+
|
| 6 |
+
#include <cutlass/arch/barrier.h>
|
| 7 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 8 |
+
|
| 9 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 10 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 11 |
+
#include <cute/arch/copy_sm90_tma.hpp>
|
| 12 |
+
|
| 13 |
+
#include <deep_gemm/common/math.cuh>
|
| 14 |
+
#include <deep_gemm/common/utils.cuh>
|
| 15 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 16 |
+
#include <deep_gemm/common/types.cuh>
|
| 17 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 18 |
+
#include <deep_gemm/epilogue/transform.cuh>
|
| 19 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 20 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 21 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 22 |
+
#include <deep_gemm/scheduler/gemm.cuh>
|
| 23 |
+
|
| 24 |
+
namespace deep_gemm {
|
| 25 |
+
|
| 26 |
+
template <uint32_t kNumFormerIters, uint32_t kGap, uint32_t kEnd, typename func_t>
|
| 27 |
+
CUTLASS_DEVICE void dispatch_num_former_iters(uint32_t num_former_iters, const func_t& func) {
|
| 28 |
+
if (num_former_iters == kNumFormerIters) {
|
| 29 |
+
func(cute::Int<kNumFormerIters>{});
|
| 30 |
+
return;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
if constexpr (kNumFormerIters + kGap <= kEnd)
|
| 34 |
+
dispatch_num_former_iters<kNumFormerIters + kGap, kGap, kEnd>(num_former_iters, func);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
template <cute::UMMA::Major kMajorSFB,
|
| 38 |
+
uint32_t SHAPE_M, uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 39 |
+
uint32_t kNumGroups,
|
| 40 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 41 |
+
uint32_t kSwizzleAMode, uint32_t kSwizzleBMode, uint32_t kSwizzleDMode,
|
| 42 |
+
uint32_t kNumStages,
|
| 43 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
| 44 |
+
uint32_t kNumTMAMulticast, bool kIsTMAMulticastOnA,
|
| 45 |
+
uint32_t kNumSMs, GemmType kGemmType,
|
| 46 |
+
typename epilogue_type_t>
|
| 47 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1) void
|
| 48 |
+
sm90_fp8_gemm_1d2d_impl(float* sfb, int* grouped_layout,
|
| 49 |
+
uint32_t shape_m, uint32_t shape_n, uint32_t shape_k,
|
| 50 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 51 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 52 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_d,
|
| 53 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_sfa) {
|
| 54 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
| 55 |
+
// Scaling checks
|
| 56 |
+
DG_STATIC_ASSERT(BLOCK_K == 128, "Only support per-128-channel FP8 scaling");
|
| 57 |
+
DG_STATIC_ASSERT(
|
| 58 |
+
math::constexpr_ceil_div(BLOCK_N, BLOCK_K) == 1 or
|
| 59 |
+
(math::constexpr_gcd(BLOCK_N, BLOCK_K) == BLOCK_N - BLOCK_K), "Too much B scales in a single block");
|
| 60 |
+
|
| 61 |
+
// Types
|
| 62 |
+
using WGMMA = typename mma::sm90::FP8MMASelector<BLOCK_N>::type;
|
| 63 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 64 |
+
DG_STATIC_ASSERT(BLOCK_M % WGMMA::M == 0 or BLOCK_M < WGMMA::M, "Invalid block size");
|
| 65 |
+
|
| 66 |
+
// Overwrite shape constants if the compiler gives
|
| 67 |
+
shape_m = SHAPE_M != 0 ? SHAPE_M : shape_m;
|
| 68 |
+
shape_n = SHAPE_N != 0 ? SHAPE_N : shape_n;
|
| 69 |
+
shape_k = SHAPE_K != 0 ? SHAPE_K : shape_k;
|
| 70 |
+
|
| 71 |
+
// Shared memory
|
| 72 |
+
static constexpr bool kMustUseUniformedScaleB = (BLOCK_K % BLOCK_N == 0);
|
| 73 |
+
static constexpr uint32_t SMEM_D_SIZE = math::constexpr_align(BLOCK_M * BLOCK_N * static_cast<uint32_t>(sizeof(__nv_bfloat16)), 1024u);
|
| 74 |
+
static constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 75 |
+
static constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 76 |
+
static constexpr uint32_t SMEM_SFA_SIZE_PER_STAGE = BLOCK_M * sizeof(float);
|
| 77 |
+
static constexpr uint32_t ALIGNED_SMEM_SFA_SIZE_PER_STAGE = math::constexpr_align(SMEM_SFA_SIZE_PER_STAGE, 128u);
|
| 78 |
+
const uint32_t shape_k_scales = math::ceil_div(shape_k, BLOCK_K);
|
| 79 |
+
const uint32_t shape_n_sfb = math::ceil_div(shape_n, BLOCK_K);
|
| 80 |
+
const uint32_t smem_sfb_size = math::align<uint32_t>(shape_k_scales * (kMustUseUniformedScaleB ? 1 : 2) * sizeof(float), sizeof(Barrier));
|
| 81 |
+
|
| 82 |
+
// NOTES: Make sure we have enough shared memory for WGMMA padding
|
| 83 |
+
static constexpr uint32_t WGMMA_A_SIZE_PER_STAGE = WGMMA::M * BLOCK_K * sizeof(__nv_fp8_e4m3);
|
| 84 |
+
DG_STATIC_ASSERT(WGMMA_A_SIZE_PER_STAGE <= SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE * kNumStages, "Memory Out of bound for WGMMA");
|
| 85 |
+
|
| 86 |
+
// Configs
|
| 87 |
+
const uint32_t num_total_k_blocks = math::ceil_div(shape_k, BLOCK_K);
|
| 88 |
+
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 89 |
+
const uint32_t lane_idx = ptx::get_lane_idx();
|
| 90 |
+
|
| 91 |
+
// Prefetch TMA descriptors at the very beginning
|
| 92 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 93 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 94 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 95 |
+
cute::prefetch_tma_descriptor(&tensor_map_sfa);
|
| 96 |
+
cute::prefetch_tma_descriptor(&tensor_map_d);
|
| 97 |
+
}
|
| 98 |
+
__syncwarp();
|
| 99 |
+
|
| 100 |
+
// Align to 1024 bytes for swizzle-128B
|
| 101 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 102 |
+
DG_STATIC_ASSERT(SMEM_D_SIZE % 1024 == 0, "Shared memory of A/B must be aligned to 1024 bytes");
|
| 103 |
+
|
| 104 |
+
// Data on shared memory
|
| 105 |
+
auto smem_d = reinterpret_cast<__nv_bfloat16*>(smem_buffer);
|
| 106 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 107 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + SMEM_D_SIZE + i * SMEM_A_SIZE_PER_STAGE);
|
| 108 |
+
});
|
| 109 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 110 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + SMEM_D_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE);
|
| 111 |
+
});
|
| 112 |
+
constexpr uint32_t SMEM_SF_OFFSET = SMEM_D_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE);
|
| 113 |
+
auto smem_sfa = utils::PatternVisitor([&](const uint32_t& i) {
|
| 114 |
+
return reinterpret_cast<float*>(smem_buffer + SMEM_SF_OFFSET + i * ALIGNED_SMEM_SFA_SIZE_PER_STAGE);
|
| 115 |
+
});
|
| 116 |
+
auto smem_sfb = reinterpret_cast<float*>(smem_buffer + SMEM_SF_OFFSET + kNumStages * ALIGNED_SMEM_SFA_SIZE_PER_STAGE);
|
| 117 |
+
|
| 118 |
+
// Fill barriers
|
| 119 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(reinterpret_cast<uint8_t*>(smem_sfb) + smem_sfb_size);
|
| 120 |
+
auto full_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_start_ptr + i; });
|
| 121 |
+
auto empty_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_start_ptr + kNumStages + i; });
|
| 122 |
+
|
| 123 |
+
// Initialize barriers
|
| 124 |
+
DG_STATIC_ASSERT(kNumTMAMulticast <= 32, "Too many TMA multicast");
|
| 125 |
+
if (warp_idx == kNumMathThreads / 32 + 1 and cute::elect_one_sync()) {
|
| 126 |
+
// NOTES: we always use `lane_idx` to arrive for the `lane_idx`-th CTA in the cluster,
|
| 127 |
+
// even with TMA multicast disabled, we want to make the behavior aligned
|
| 128 |
+
#pragma unroll
|
| 129 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 130 |
+
full_barriers[i]->init(1);
|
| 131 |
+
empty_barriers[i]->init(kNumTMAMulticast * kNumMathThreads / 32);
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// Make initialized barrier visible in async proxy
|
| 135 |
+
cutlass::arch::fence_barrier_init();
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// Synchronize all threads to make barrier visible in normal memory model
|
| 139 |
+
(kNumTMAMulticast > 1) ? cute::cluster_sync() : __syncthreads();
|
| 140 |
+
|
| 141 |
+
// Register reconfigurations
|
| 142 |
+
constexpr uint32_t kNumTMARegisters = 40;
|
| 143 |
+
constexpr uint32_t kNumMathRegisters = kNumMathThreads == 128 ? 248 : 232;
|
| 144 |
+
|
| 145 |
+
// Wait for primary kernel completion
|
| 146 |
+
cudaGridDependencySynchronize();
|
| 147 |
+
|
| 148 |
+
// Block scheduler
|
| 149 |
+
uint32_t m_block_idx, n_block_idx;
|
| 150 |
+
auto scheduler = sched::Scheduler<kGemmType, BLOCK_M, BLOCK_N, kNumGroups, kNumTMAMulticast, kIsTMAMulticastOnA, kNumSMs>(shape_m, shape_n, shape_k, grouped_layout);
|
| 151 |
+
|
| 152 |
+
// Pipeline and TMA phases
|
| 153 |
+
uint32_t stage_idx = 0, phase = 0;
|
| 154 |
+
auto advance_pipeline = [&](uint32_t& k_block_idx) {
|
| 155 |
+
++ k_block_idx;
|
| 156 |
+
|
| 157 |
+
// Flip phases only if reach the next first stage
|
| 158 |
+
stage_idx = stage_idx == kNumStages - 1 ? 0 : stage_idx + 1;
|
| 159 |
+
phase ^= stage_idx == 0;
|
| 160 |
+
};
|
| 161 |
+
|
| 162 |
+
if (warp_idx >= kNumMathThreads / 32) {
|
| 163 |
+
// TMA warp-group for loading data
|
| 164 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 165 |
+
|
| 166 |
+
// NOTES: only one thread (or warp) will be used
|
| 167 |
+
// We use the third warp, as warp 0/1 may be doing WGMMA with `BLOCK_M == 32`
|
| 168 |
+
if (warp_idx == kNumMathThreads / 32 + 2 and cute::elect_one_sync()) {
|
| 169 |
+
// Persistently schedule over blocks
|
| 170 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 171 |
+
// Assign TMA multicast number into A and B
|
| 172 |
+
// NOTES: there may be additional odd rows/columns or cases where multicast is not possible.
|
| 173 |
+
const bool is_tma_multicast_valid = scheduler.is_tma_multicast_valid(m_block_idx);
|
| 174 |
+
const uint32_t num_tma_multicast_a = (kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 175 |
+
const uint32_t num_tma_multicast_b = (not kIsTMAMulticastOnA and is_tma_multicast_valid) ? kNumTMAMulticast : 1;
|
| 176 |
+
DG_STATIC_ASSERT(kNumTMAMulticast <= 2, "Scheduler does not support > 2 TMA multicast");
|
| 177 |
+
|
| 178 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 179 |
+
// Wait consumer release
|
| 180 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 181 |
+
|
| 182 |
+
// Issue TMA A
|
| 183 |
+
constexpr bool kIsBatchedMM = (kGemmType == GemmType::Batched);
|
| 184 |
+
const uint32_t batch_idx = (kIsBatchedMM ? scheduler.current_group_idx : 0);
|
| 185 |
+
|
| 186 |
+
constexpr bool kWithGroupOffsetA = kGemmType == GemmType::MGroupedMasked;
|
| 187 |
+
auto& full_barrier = *full_barriers[stage_idx];
|
| 188 |
+
const uint32_t k_idx = k_block_idx * BLOCK_K;
|
| 189 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode, __nv_fp8_e4m3, kIsBatchedMM>(&tensor_map_a, &full_barrier,
|
| 190 |
+
smem_a[stage_idx], k_idx, scheduler.get_global_idx<kWithGroupOffsetA>(shape_m, BLOCK_M, m_block_idx),
|
| 191 |
+
num_tma_multicast_a, batch_idx);
|
| 192 |
+
tma::copy<BLOCK_M, BLOCK_K, 0>(&tensor_map_sfa, &full_barrier,
|
| 193 |
+
smem_sfa[stage_idx], m_block_idx * BLOCK_M, scheduler.template get_global_idx<kWithGroupOffsetA, sched::IndexType::SF_K>(shape_k_scales, 1, k_block_idx),
|
| 194 |
+
num_tma_multicast_a);
|
| 195 |
+
|
| 196 |
+
// Issue TMA B
|
| 197 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode, __nv_fp8_e4m3, kIsBatchedMM>(&tensor_map_b, &full_barrier,
|
| 198 |
+
smem_b[stage_idx], k_idx, scheduler.get_global_idx<true>(shape_n, BLOCK_N, n_block_idx, m_block_idx),
|
| 199 |
+
num_tma_multicast_b, batch_idx);
|
| 200 |
+
full_barrier.arrive_and_expect_tx(SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE + SMEM_SFA_SIZE_PER_STAGE);
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
// To safely deconstruct distributed shared barriers, we need another round of empty waits
|
| 205 |
+
if constexpr (kNumTMAMulticast > 1) {
|
| 206 |
+
for (uint32_t i = 0; i < kNumStages; advance_pipeline(i))
|
| 207 |
+
empty_barriers[stage_idx]->wait(phase ^ 1);
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
} else {
|
| 211 |
+
// Math warp-groups for WGMMA
|
| 212 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 213 |
+
|
| 214 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 215 |
+
const auto math_wg_idx = __shfl_sync(0xffffffff, threadIdx.x / 128, 0);
|
| 216 |
+
const auto r_0 = warp_idx * 16 + lane_idx / 4, r_1 = r_0 + 8;
|
| 217 |
+
|
| 218 |
+
auto a_desc = mma::sm90::make_smem_desc(smem_a[0] + math_wg_idx * WGMMA::M * BLOCK_K, 1);
|
| 219 |
+
auto b_desc = mma::sm90::make_smem_desc(smem_b[0], 1);
|
| 220 |
+
const uint32_t a_desc_lo = __shfl_sync(0xffffffff, a_desc.reg32_[0], 0);
|
| 221 |
+
const uint32_t b_desc_lo = __shfl_sync(0xffffffff, b_desc.reg32_[0], 0);
|
| 222 |
+
|
| 223 |
+
// Persistently schedule over blocks
|
| 224 |
+
while (scheduler.get_next_block(m_block_idx, n_block_idx)) {
|
| 225 |
+
// Decide the number of scales B to load
|
| 226 |
+
DG_TRAP_ONLY_DEVICE_ASSERT(shape_n % 8 == 0);
|
| 227 |
+
uint32_t num_former_iters = BLOCK_N / 8, num_full_iters = num_former_iters;
|
| 228 |
+
if constexpr (not kMustUseUniformedScaleB) {
|
| 229 |
+
num_former_iters = min(BLOCK_N, BLOCK_K - n_block_idx * BLOCK_N % BLOCK_K) / 8;
|
| 230 |
+
num_full_iters = min(shape_n - n_block_idx * BLOCK_N, BLOCK_N) / 8;
|
| 231 |
+
}
|
| 232 |
+
uint32_t num_sfb = shape_k_scales * (num_former_iters >= num_full_iters ? 1 : 2);
|
| 233 |
+
|
| 234 |
+
// Load B scales with math warp-groups
|
| 235 |
+
// NOTES: except the first warp, we want to overlap loading B scales with TMA stores between tasks
|
| 236 |
+
if (threadIdx.x >= 32) {
|
| 237 |
+
auto previous_group_offset = scheduler.template get_global_idx<true, sched::IndexType::SF_K>(shape_n_sfb * shape_k_scales, 0, 0, m_block_idx);
|
| 238 |
+
const uint32_t stride_n_sfb = kMajorSFB == cute::UMMA::Major::MN ? 1 : shape_k_scales;
|
| 239 |
+
const uint32_t stride_k_sfb = kMajorSFB == cute::UMMA::Major::MN ? shape_n_sfb : 1;
|
| 240 |
+
auto local_sfb = sfb + previous_group_offset + ((n_block_idx * BLOCK_N) / BLOCK_K) * stride_n_sfb;
|
| 241 |
+
|
| 242 |
+
#pragma unroll
|
| 243 |
+
for (uint32_t i = threadIdx.x - 32; i < num_sfb; i += kNumMathThreads - 32)
|
| 244 |
+
ptx::st_shared(smem_sfb + i, i < shape_k_scales ? local_sfb[i * stride_k_sfb] : local_sfb[(i - shape_k_scales) * stride_k_sfb + stride_n_sfb]);
|
| 245 |
+
}
|
| 246 |
+
cutlass::arch::NamedBarrier::sync(kNumMathThreads, 0);
|
| 247 |
+
|
| 248 |
+
// Accumulation for WGMMA or CUDA promotion
|
| 249 |
+
constexpr uint32_t WAVE_BLOCK_M = BLOCK_M <= WGMMA::M ? BLOCK_M : WGMMA::M * 2;
|
| 250 |
+
DG_STATIC_ASSERT(BLOCK_M % WAVE_BLOCK_M == 0, "Invalid block sizes");
|
| 251 |
+
float accum[WGMMA::kNumAccum], final_accum[WGMMA::kNumAccum * (BLOCK_M / WAVE_BLOCK_M)] = {0};
|
| 252 |
+
|
| 253 |
+
// Pick threads whose WGMMA results are to be stored in shared memory
|
| 254 |
+
DG_STATIC_ASSERT(BLOCK_M >= 64 or kNumMathThreads == 128, "Only one math warp group for `BLOCK_M < 64`");
|
| 255 |
+
constexpr uint32_t kNumWGMMAStoreThreads = WAVE_BLOCK_M * (128 / WGMMA::M);
|
| 256 |
+
const bool do_wgmma_store = BLOCK_M >= WGMMA::M or warp_idx < kNumWGMMAStoreThreads / 32;
|
| 257 |
+
|
| 258 |
+
// Empty barrier arrival
|
| 259 |
+
auto empty_barrier_arrive = [&]() {
|
| 260 |
+
if constexpr (kNumTMAMulticast == 1) {
|
| 261 |
+
lane_idx == 0 ? empty_barriers[stage_idx]->arrive() : void();
|
| 262 |
+
} else {
|
| 263 |
+
auto target_cta = scheduler.is_peer_cta_alive ? lane_idx : cute::block_rank_in_cluster();
|
| 264 |
+
lane_idx < kNumTMAMulticast ? empty_barriers[stage_idx]->arrive(target_cta) : void();
|
| 265 |
+
}
|
| 266 |
+
};
|
| 267 |
+
|
| 268 |
+
// Skip useless computations
|
| 269 |
+
if (scheduler.is_computation_valid(m_block_idx, math_wg_idx * WGMMA::M)) {
|
| 270 |
+
// The compiler must know the dynamic variable `num_former_iters`'s real value
|
| 271 |
+
constexpr bool kShouldOptimize = BLOCK_K / math::constexpr_gcd(BLOCK_K, BLOCK_N) <= 4 and not kMustUseUniformedScaleB;
|
| 272 |
+
constexpr uint32_t kGap = math::constexpr_gcd(BLOCK_K, BLOCK_N) / 8;
|
| 273 |
+
constexpr uint32_t kEnd = kShouldOptimize ? BLOCK_K / 8 : 0;
|
| 274 |
+
|
| 275 |
+
// Dispatch `num_former_iters` and launch MMAs
|
| 276 |
+
dispatch_num_former_iters<0, kGap, kEnd>(kShouldOptimize ? num_former_iters : 0, [&](auto _) {
|
| 277 |
+
#pragma unroll 8
|
| 278 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 279 |
+
const auto a_desc_base_lo = a_desc_lo + stage_idx * (SMEM_A_SIZE_PER_STAGE / 16);
|
| 280 |
+
const auto b_desc_base_lo = b_desc_lo + stage_idx * (SMEM_B_SIZE_PER_STAGE / 16);
|
| 281 |
+
|
| 282 |
+
// Read B scales
|
| 283 |
+
float scale_b_0 = ptx::ld_shared(smem_sfb + k_block_idx), scale_b_1;
|
| 284 |
+
// NOTES: even some blocks do not need to read the second row, but we still load one to align with other blocks
|
| 285 |
+
if constexpr (not kMustUseUniformedScaleB)
|
| 286 |
+
scale_b_1 = ptx::ld_shared(smem_sfb + k_block_idx + shape_k_scales);
|
| 287 |
+
|
| 288 |
+
// Wait TMA arrivals
|
| 289 |
+
full_barriers[stage_idx]->wait(phase);
|
| 290 |
+
|
| 291 |
+
// TODO: remove some useless computation for unaligned Ms
|
| 292 |
+
#pragma unroll
|
| 293 |
+
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
| 294 |
+
auto m_offset = local_idx * WAVE_BLOCK_M;
|
| 295 |
+
|
| 296 |
+
// Read A scales
|
| 297 |
+
// NOTES: all shared memory read must be prior to `warpgroup_arrive` to avoid next scheduled block polluting the results
|
| 298 |
+
auto scale_a_0 = do_wgmma_store ? ptx::ld_shared(smem_sfa[stage_idx] + r_0 + m_offset) : 0;
|
| 299 |
+
auto scale_a_1 = do_wgmma_store ? ptx::ld_shared(smem_sfa[stage_idx] + r_1 + m_offset) : 0;
|
| 300 |
+
|
| 301 |
+
// Commit WGMMA instructions
|
| 302 |
+
#pragma unroll
|
| 303 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 304 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 305 |
+
ptx::warpgroup_arrive();
|
| 306 |
+
#pragma unroll
|
| 307 |
+
for (uint32_t k = 0; k < BLOCK_K / WGMMA::K; ++ k) {
|
| 308 |
+
a_desc.reg32_[0] = a_desc_base_lo + (m_offset * BLOCK_K + k * WGMMA::K) / 16;
|
| 309 |
+
b_desc.reg32_[0] = b_desc_base_lo + k * WGMMA::K / 16;
|
| 310 |
+
WGMMA::wgmma(a_desc, b_desc, accum, k);
|
| 311 |
+
}
|
| 312 |
+
ptx::warpgroup_commit_batch();
|
| 313 |
+
#pragma unroll
|
| 314 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 315 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 316 |
+
ptx::warpgroup_wait<0>();
|
| 317 |
+
|
| 318 |
+
// Notify barrier arrival at the last warpgroup wave
|
| 319 |
+
if (local_idx == BLOCK_M / WAVE_BLOCK_M - 1)
|
| 320 |
+
empty_barrier_arrive();
|
| 321 |
+
|
| 322 |
+
// Skip promotion for the unfilled parts
|
| 323 |
+
if (not do_wgmma_store)
|
| 324 |
+
continue;
|
| 325 |
+
|
| 326 |
+
// Promote with scales
|
| 327 |
+
// NOTES: making it as predicates is very important for performance, comparing to two loops
|
| 328 |
+
float scale_0_0 = scale_a_0 * scale_b_0, scale_1_0 = scale_a_1 * scale_b_0;
|
| 329 |
+
float scale_0_1, scale_1_1;
|
| 330 |
+
if constexpr (not kMustUseUniformedScaleB)
|
| 331 |
+
scale_0_1 = scale_a_0 * scale_b_1, scale_1_1 = scale_a_1 * scale_b_1;
|
| 332 |
+
|
| 333 |
+
auto shifted_accum = final_accum + WGMMA::kNumAccum * local_idx;
|
| 334 |
+
#pragma unroll
|
| 335 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 336 |
+
// NOTES: for unrolled `num_former_iters` cases, we expect the compiler to automatically make it a constant
|
| 337 |
+
const bool predicate = kMustUseUniformedScaleB or i < num_former_iters;
|
| 338 |
+
shifted_accum[i * 4 + 0] += (predicate ? scale_0_0 : scale_0_1) * accum[i * 4 + 0];
|
| 339 |
+
shifted_accum[i * 4 + 1] += (predicate ? scale_0_0 : scale_0_1) * accum[i * 4 + 1];
|
| 340 |
+
shifted_accum[i * 4 + 2] += (predicate ? scale_1_0 : scale_1_1) * accum[i * 4 + 2];
|
| 341 |
+
shifted_accum[i * 4 + 3] += (predicate ? scale_1_0 : scale_1_1) * accum[i * 4 + 3];
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
});
|
| 346 |
+
} else {
|
| 347 |
+
#pragma unroll
|
| 348 |
+
for (uint32_t k_block_idx = 0; k_block_idx < num_total_k_blocks; advance_pipeline(k_block_idx)) {
|
| 349 |
+
full_barriers[stage_idx]->wait(phase);
|
| 350 |
+
empty_barrier_arrive();
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
// TMA checks
|
| 355 |
+
constexpr uint32_t kNumElemBytes = sizeof(nv_bfloat16);
|
| 356 |
+
constexpr uint32_t TMA_D_BLOCK_N = kSwizzleDMode == 0 ? BLOCK_N : (kSwizzleDMode / kNumElemBytes);
|
| 357 |
+
constexpr uint32_t WGMMA_M_PER_WARP = WGMMA::M / 4;
|
| 358 |
+
DG_STATIC_ASSERT(BLOCK_M % 8 == 0, "Invalid swizzling atom");
|
| 359 |
+
DG_STATIC_ASSERT(BLOCK_N % TMA_D_BLOCK_N == 0 and BLOCK_N / TMA_D_BLOCK_N <= 32,
|
| 360 |
+
"Unaligned TMA store or too many TMA store instructions");
|
| 361 |
+
DG_STATIC_ASSERT(TMA_D_BLOCK_N % 8 == 0, "Invalid TMA block N");
|
| 362 |
+
|
| 363 |
+
// Skip WGMMA store for the unfilled parts
|
| 364 |
+
if (not do_wgmma_store)
|
| 365 |
+
continue;
|
| 366 |
+
|
| 367 |
+
// Wait last TMA store to be finished
|
| 368 |
+
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N)
|
| 369 |
+
cute::tma_store_wait<0>();
|
| 370 |
+
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 1);
|
| 371 |
+
|
| 372 |
+
// Write back to shared memory using STSM and issue TMA stores
|
| 373 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum % 4 == 0, "Invalid STSM x2 vectorization");
|
| 374 |
+
#pragma unroll
|
| 375 |
+
for (uint32_t local_idx = 0; local_idx < BLOCK_M / WAVE_BLOCK_M; ++ local_idx) {
|
| 376 |
+
auto m_offset = local_idx * WAVE_BLOCK_M;
|
| 377 |
+
auto shifted_accum = final_accum + WGMMA::kNumAccum * local_idx;
|
| 378 |
+
#pragma unroll
|
| 379 |
+
for (auto i = 0; i < WGMMA::kNumAccum / 4; ++ i) {
|
| 380 |
+
// Swizzle or padding into the correct address
|
| 381 |
+
uint8_t* smem_ptr = nullptr;
|
| 382 |
+
if constexpr (kSwizzleDMode > 0) {
|
| 383 |
+
// Calculate the swizzling atom offset and in-atom offset
|
| 384 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 385 |
+
auto atom_offset = i / (TMA_D_BLOCK_N / 8), in_atom_offset = i % (TMA_D_BLOCK_N / 8);
|
| 386 |
+
|
| 387 |
+
// Calculate the index of the bank group to be written in the atom
|
| 388 |
+
auto bank_group_index = in_atom_offset + lane_idx * (kSwizzleDMode / kNumBankGroupBytes);
|
| 389 |
+
|
| 390 |
+
// Reshape the atom in another view and swizzle
|
| 391 |
+
// - original: `(BLOCK_M, kSwizzleDMode / kNumBankGroupBytes)`
|
| 392 |
+
// - new: `(BLOCK_M * kSwizzleDMode / kNumBankGroupBytes / 8, 8)`
|
| 393 |
+
constexpr bool kHasShortcut = (kSwizzleDMode / kNumBankGroupBytes) == 8;
|
| 394 |
+
auto row = kHasShortcut ? (in_atom_offset / 8 + lane_idx) : (bank_group_index / 8);
|
| 395 |
+
auto col = kHasShortcut ? (in_atom_offset) : (bank_group_index % 8);
|
| 396 |
+
col ^= row % (kSwizzleDMode / 16);
|
| 397 |
+
|
| 398 |
+
// Add back into the base pointer
|
| 399 |
+
// NOTES: think twice before modifying this, as changes may affect the number of instructions
|
| 400 |
+
smem_ptr = reinterpret_cast<uint8_t*>(smem_d) + // Base pointer
|
| 401 |
+
warp_idx * (WGMMA_M_PER_WARP * kSwizzleDMode) + // Warp offset
|
| 402 |
+
m_offset * kSwizzleDMode + // Wave offset
|
| 403 |
+
atom_offset * BLOCK_M * kSwizzleDMode + // Swizzle atom offset (constants)
|
| 404 |
+
row * (kNumBankGroupBytes * 8) + col * kNumBankGroupBytes; // In-atom offset
|
| 405 |
+
} else {
|
| 406 |
+
// No swizzling, just padding
|
| 407 |
+
smem_ptr = reinterpret_cast<uint8_t*>(smem_d + (m_offset + warp_idx * WGMMA_M_PER_WARP + lane_idx) * BLOCK_N + i * 8);
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
// NOTES: only 16 lanes' addresses are used
|
| 411 |
+
ptx::SM90_U32x2_STSM_N<nv_bfloat162>::copy(
|
| 412 |
+
__float22bfloat162_rn({shifted_accum[i * 4 + 0], shifted_accum[i * 4 + 1]}),
|
| 413 |
+
__float22bfloat162_rn({shifted_accum[i * 4 + 2], shifted_accum[i * 4 + 3]}),
|
| 414 |
+
smem_ptr
|
| 415 |
+
);
|
| 416 |
+
}
|
| 417 |
+
}
|
| 418 |
+
cute::tma_store_fence();
|
| 419 |
+
cutlass::arch::NamedBarrier::sync(kNumWGMMAStoreThreads, 1);
|
| 420 |
+
|
| 421 |
+
// Use TMA store to write back to global memory
|
| 422 |
+
// TODO: compatible with FP32 output
|
| 423 |
+
constexpr bool kWithGroupOffsetD = kGemmType == GemmType::MGroupedMasked;
|
| 424 |
+
DG_STATIC_ASSERT(kNumWGMMAStoreThreads >= BLOCK_N / TMA_D_BLOCK_N, "Too many TMA blocks");
|
| 425 |
+
if (threadIdx.x < BLOCK_N / TMA_D_BLOCK_N) {
|
| 426 |
+
auto in_block_n_offset = threadIdx.x * TMA_D_BLOCK_N;
|
| 427 |
+
auto smem_ptr = smem_d + in_block_n_offset * BLOCK_M;
|
| 428 |
+
auto n_idx = epilogue_type_t::apply_index_n<TMA_D_BLOCK_N>(n_block_idx * BLOCK_N + in_block_n_offset);
|
| 429 |
+
auto m_idx = scheduler.get_global_idx<kWithGroupOffsetD>(shape_m, BLOCK_M, m_block_idx);
|
| 430 |
+
if constexpr (kGemmType == GemmType::Batched) {
|
| 431 |
+
cute::SM90_TMA_STORE_3D::copy(&tensor_map_d, smem_ptr,
|
| 432 |
+
n_idx, m_idx, scheduler.current_group_idx);
|
| 433 |
+
} else {
|
| 434 |
+
cute::SM90_TMA_STORE_2D::copy(&tensor_map_d, smem_ptr, n_idx, m_idx);
|
| 435 |
+
}
|
| 436 |
+
cute::tma_store_arrive();
|
| 437 |
+
}
|
| 438 |
+
__syncwarp();
|
| 439 |
+
}
|
| 440 |
+
}
|
| 441 |
+
#else
|
| 442 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 443 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
| 444 |
+
#endif
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
}; // namespace deep_gemm
|
| 448 |
+
|
| 449 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 8 |
+
#include <cute/arch/mma_sm90_desc.hpp>
|
| 9 |
+
|
| 10 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 11 |
+
#include <deep_gemm/common/math.cuh>
|
| 12 |
+
#include <deep_gemm/common/utils.cuh>
|
| 13 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 14 |
+
#include <deep_gemm/common/types.cuh>
|
| 15 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 17 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 18 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 19 |
+
|
| 20 |
+
namespace deep_gemm {
|
| 21 |
+
|
| 22 |
+
template <uint32_t kNumHeads, uint32_t kHeadDim,
|
| 23 |
+
bool kIsCompressedLogits,
|
| 24 |
+
uint32_t BLOCK_Q, uint32_t BLOCK_KV,
|
| 25 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 26 |
+
uint32_t kNumSMs,
|
| 27 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
| 28 |
+
typename logits_dtype_t>
|
| 29 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1)
|
| 30 |
+
void sm90_fp8_mqa_logits(const uint32_t seq_len, const uint32_t seq_len_kv,
|
| 31 |
+
const uint32_t max_seqlen_k, const uint32_t stride_logits,
|
| 32 |
+
uint32_t* cu_seq_len_k_start,
|
| 33 |
+
uint32_t* cu_seq_len_k_end,
|
| 34 |
+
logits_dtype_t* logits,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv_scales,
|
| 38 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 39 |
+
// TODO: consider TMA multicast
|
| 40 |
+
// For one block, we process `[q_start:q_end, h, d] @ [kv_start:kv_end, d] -> [q_start:q_end, kv_start:kv_end]`
|
| 41 |
+
// Q should be load only at once for a block
|
| 42 |
+
const auto num_q_blocks = math::ceil_div(seq_len, BLOCK_Q);
|
| 43 |
+
|
| 44 |
+
// Types
|
| 45 |
+
using WGMMA = typename mma::sm90::FP8MMASelector<BLOCK_Q * kNumHeads>::type;
|
| 46 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 47 |
+
|
| 48 |
+
// Prefetch TMA descriptors
|
| 49 |
+
DG_STATIC_ASSERT(kNumTMAThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 50 |
+
if (threadIdx.x / 32 == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 51 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 52 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 53 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv_scales);
|
| 54 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 55 |
+
}
|
| 56 |
+
__syncwarp();
|
| 57 |
+
|
| 58 |
+
// Shared memory configs
|
| 59 |
+
// NOTES: weight may be unaligned
|
| 60 |
+
static constexpr uint32_t kSwizzleAlignment = kHeadDim * 8;
|
| 61 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 62 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = BLOCK_Q * kNumHeads * sizeof(float);
|
| 63 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = BLOCK_KV * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 64 |
+
static constexpr uint32_t SMEM_KV_SCALE_SIZE_PER_STAGE = BLOCK_KV * sizeof(float);
|
| 65 |
+
|
| 66 |
+
// Align to swizzling alignment bytes
|
| 67 |
+
extern __shared__ __align__(kSwizzleAlignment) uint8_t smem_buffer[];
|
| 68 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 69 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 70 |
+
|
| 71 |
+
// Data on shared memory
|
| 72 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 73 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer +
|
| 74 |
+
SMEM_Q_SIZE_PER_STAGE * i);
|
| 75 |
+
});
|
| 76 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 77 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + (
|
| 78 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * i));
|
| 79 |
+
});
|
| 80 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 81 |
+
return reinterpret_cast<float*>(smem_buffer +
|
| 82 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * kNumKVStages + SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 83 |
+
});
|
| 84 |
+
auto smem_kv_scales = utils::PatternVisitor([&](const uint32_t& i) {
|
| 85 |
+
return reinterpret_cast<float*>(smem_buffer +
|
| 86 |
+
SMEM_Q_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SIZE_PER_STAGE * kNumKVStages +
|
| 87 |
+
SMEM_WEIGHT_SIZE_PER_STAGE * kNumQStages + SMEM_KV_SCALE_SIZE_PER_STAGE * i);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
// TMA barriers
|
| 91 |
+
auto barrier_ptr = reinterpret_cast<Barrier*>(smem_kv_scales[kNumKVStages]);
|
| 92 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + i; });
|
| 93 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages + i); });
|
| 94 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + i); });
|
| 95 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return barrier_ptr + (kNumQStages * 2 + kNumKVStages + i); });
|
| 96 |
+
|
| 97 |
+
// Initialize barriers
|
| 98 |
+
const bool is_tma_load_warp = kNumMathThreads <= threadIdx.x and threadIdx.x < kNumMathThreads + 32;
|
| 99 |
+
if (is_tma_load_warp and cute::elect_one_sync()) {
|
| 100 |
+
#pragma unroll
|
| 101 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 102 |
+
full_q_barriers[i]->init(1);
|
| 103 |
+
empty_q_barriers[i]->init(kNumMathThreads);
|
| 104 |
+
}
|
| 105 |
+
#pragma unroll
|
| 106 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 107 |
+
full_kv_barriers[i]->init(1);
|
| 108 |
+
empty_kv_barriers[i]->init(kNumMathThreads);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Make initialized barrier visible in async proxy
|
| 112 |
+
cutlass::arch::fence_barrier_init();
|
| 113 |
+
}
|
| 114 |
+
__syncthreads();
|
| 115 |
+
|
| 116 |
+
// Register reconfigurations
|
| 117 |
+
constexpr uint32_t kNumTMARegisters = 32;
|
| 118 |
+
constexpr uint32_t kNumMathRegisters = 112;
|
| 119 |
+
|
| 120 |
+
// Block scheduler
|
| 121 |
+
const auto sm_idx = blockIdx.x;
|
| 122 |
+
uint32_t block_q_idx = sm_idx, q_iter_idx = 0;
|
| 123 |
+
const auto get_next_block_q_idx = [&]() -> cute::tuple<uint32_t, uint32_t> {
|
| 124 |
+
return {block_q_idx + kNumSMs, q_iter_idx + 1};
|
| 125 |
+
};
|
| 126 |
+
uint32_t seq_k_start[BLOCK_Q], seq_k_end[BLOCK_Q];
|
| 127 |
+
const auto load_schedule = [&](const uint32_t& q_iter_offset = 0) -> cute::tuple<uint32_t, uint32_t, uint32_t, uint32_t> {
|
| 128 |
+
uint32_t start = cute::numeric_limits<uint32_t>::max();
|
| 129 |
+
uint32_t end = cute::numeric_limits<uint32_t>::min();
|
| 130 |
+
|
| 131 |
+
#pragma unroll
|
| 132 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 133 |
+
const auto q_idx = min(block_q_idx * BLOCK_Q + i, seq_len - 1);
|
| 134 |
+
seq_k_start[i] = cu_seq_len_k_start[q_idx];
|
| 135 |
+
seq_k_end[i] = cu_seq_len_k_end[q_idx];
|
| 136 |
+
start = min(start, min(seq_k_start[i], seq_len_kv));
|
| 137 |
+
end = max(end, min(seq_k_end[i], seq_len_kv));
|
| 138 |
+
}
|
| 139 |
+
// TMA alignment requirements for SF KV
|
| 140 |
+
start = start / 4 * 4;
|
| 141 |
+
return {(q_iter_idx + q_iter_offset) % kNumQStages, // Q pipeline stage
|
| 142 |
+
((q_iter_idx + q_iter_offset) / kNumQStages) & 1, // Q pipeline phase
|
| 143 |
+
start, math::ceil_div(end - start, BLOCK_KV)}; // Task info
|
| 144 |
+
};
|
| 145 |
+
|
| 146 |
+
// KV pipeline
|
| 147 |
+
uint32_t num_total_kv_blocks = 0;
|
| 148 |
+
const auto get_kv_pipeline = [&](const uint32_t& kv_block_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 149 |
+
return {
|
| 150 |
+
(num_total_kv_blocks + kv_block_idx) % kNumKVStages, // KV pipeline stage
|
| 151 |
+
((num_total_kv_blocks + kv_block_idx) / kNumKVStages) & 1 // KV pipeline phase
|
| 152 |
+
};
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
// Wait for primary kernel completion
|
| 156 |
+
cudaGridDependencySynchronize();
|
| 157 |
+
|
| 158 |
+
if (threadIdx.x >= kNumMathThreads) {
|
| 159 |
+
// TMA warp-group for loading data
|
| 160 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 161 |
+
|
| 162 |
+
// Only the first warp remains
|
| 163 |
+
if (not is_tma_load_warp)
|
| 164 |
+
return;
|
| 165 |
+
|
| 166 |
+
// Prefetch
|
| 167 |
+
const auto& issue_tma_q = [&](const uint32_t& stage_idx, const auto& block_idx) {
|
| 168 |
+
tma::copy<kHeadDim, BLOCK_Q * kNumHeads, kHeadDim>(&tensor_map_q, full_q_barriers[stage_idx], smem_q[stage_idx], 0, block_idx * BLOCK_Q * kNumHeads);
|
| 169 |
+
tma::copy<kNumHeads, BLOCK_Q, 0>(&tensor_map_weights, full_q_barriers[stage_idx], smem_weights[stage_idx], 0, block_idx * BLOCK_Q);
|
| 170 |
+
full_q_barriers[stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 171 |
+
};
|
| 172 |
+
if (cute::elect_one_sync() and block_q_idx < num_q_blocks)
|
| 173 |
+
issue_tma_q(0, block_q_idx);
|
| 174 |
+
|
| 175 |
+
// Only the first lane persistently schedules over blocks
|
| 176 |
+
if (cute::elect_one_sync()) {
|
| 177 |
+
while (block_q_idx < num_q_blocks) {
|
| 178 |
+
CUTE_TIE_DECL(load_schedule(1), q_stage_idx, q_phase, kv_start, num_kv_blocks);
|
| 179 |
+
|
| 180 |
+
// Wait Q consumer release
|
| 181 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 182 |
+
|
| 183 |
+
// Issue TMA Q
|
| 184 |
+
if (const auto& next_block_q_idx = cute::get<0>(get_next_block_q_idx()); next_block_q_idx < num_q_blocks)
|
| 185 |
+
issue_tma_q(q_stage_idx, next_block_q_idx);
|
| 186 |
+
|
| 187 |
+
// Issue TMA KV
|
| 188 |
+
#pragma unroll
|
| 189 |
+
for (uint32_t kv_block_idx = 0; kv_block_idx < num_kv_blocks; ++ kv_block_idx) {
|
| 190 |
+
// Wait consumer release
|
| 191 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_block_idx), kv_stage_idx, kv_phase);
|
| 192 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 193 |
+
|
| 194 |
+
// Issue TMA KV
|
| 195 |
+
tma::copy<kHeadDim, BLOCK_KV, kHeadDim>(&tensor_map_kv, full_kv_barriers[kv_stage_idx],
|
| 196 |
+
smem_kv[kv_stage_idx], 0, kv_start + kv_block_idx * BLOCK_KV);
|
| 197 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_kv_scales, full_kv_barriers[kv_stage_idx],
|
| 198 |
+
smem_kv_scales[kv_stage_idx], kv_start + kv_block_idx * BLOCK_KV, 0);
|
| 199 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_KV_SCALE_SIZE_PER_STAGE);
|
| 200 |
+
}
|
| 201 |
+
num_total_kv_blocks += num_kv_blocks;
|
| 202 |
+
|
| 203 |
+
// Jump to the next block
|
| 204 |
+
CUTE_TIE(get_next_block_q_idx(), block_q_idx, q_iter_idx);
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
} else {
|
| 208 |
+
// Math warp-groups for WGMMA
|
| 209 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 210 |
+
|
| 211 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 212 |
+
const auto& thread_idx = threadIdx.x % kNumMathThreads;
|
| 213 |
+
const auto& warp_idx = __shfl_sync(0xffffffff, thread_idx / 32, 0);
|
| 214 |
+
const auto& warpgroup_idx = warp_idx / 4;
|
| 215 |
+
const auto& lane_idx = ptx::get_lane_idx();
|
| 216 |
+
float accum[WGMMA::kNumAccum], weights[BLOCK_Q][kNumHeads / 4];
|
| 217 |
+
|
| 218 |
+
const auto& warp_offset = warp_idx * 16;
|
| 219 |
+
const auto& v_0_offset = lane_idx / 4 + 0;
|
| 220 |
+
const auto& v_1_offset = lane_idx / 4 + 8;
|
| 221 |
+
|
| 222 |
+
while (block_q_idx < num_q_blocks) {
|
| 223 |
+
CUTE_TIE_DECL(load_schedule(), q_stage_idx, q_phase, kv_start, num_kv_blocks);
|
| 224 |
+
|
| 225 |
+
// Wait TMA Q arrival
|
| 226 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 227 |
+
|
| 228 |
+
// Read weights
|
| 229 |
+
#pragma unroll
|
| 230 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 231 |
+
#pragma unroll
|
| 232 |
+
for (uint32_t j = 0; j < kNumHeads / 4; ++ j)
|
| 233 |
+
weights[i][j] = ptx::ld_shared(smem_weights[q_stage_idx] + i * kNumHeads + (j / 2) * 8 + (j & 1) + (lane_idx % 4) * 2);
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
// Compute over KV blocks
|
| 237 |
+
#pragma unroll
|
| 238 |
+
for (uint32_t kv_block_idx = 0; kv_block_idx < num_kv_blocks; ++ kv_block_idx) {
|
| 239 |
+
// Compute `[BLOCK_Q * kNumHeads, kHeadDim] @ [BLOCK_KV, kHeadDim] -> [BLOCK_Q, BLOCK_KV]`
|
| 240 |
+
// Wait TMA KV arrival
|
| 241 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_block_idx), kv_stage_idx, kv_phase);
|
| 242 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 243 |
+
|
| 244 |
+
// Read per-KV scales
|
| 245 |
+
float scale_kv_0 = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + warp_offset + v_0_offset);
|
| 246 |
+
float scale_kv_1 = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + warp_offset + v_1_offset);
|
| 247 |
+
|
| 248 |
+
// Issue WGMMA
|
| 249 |
+
DG_STATIC_ASSERT(BLOCK_KV == kNumMathThreads / 2, "Invalid block size");
|
| 250 |
+
DG_STATIC_ASSERT(kHeadDim % WGMMA::K == 0, "Invalid head dim");
|
| 251 |
+
#pragma unroll
|
| 252 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 253 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 254 |
+
ptx::warpgroup_arrive();
|
| 255 |
+
#pragma unroll
|
| 256 |
+
for (uint32_t k = 0; k < kHeadDim / WGMMA::K; ++ k) {
|
| 257 |
+
auto desc_a = mma::sm90::make_smem_desc(
|
| 258 |
+
smem_kv[kv_stage_idx] + (warpgroup_idx * WGMMA::M) * kHeadDim + k * WGMMA::K,
|
| 259 |
+
mma::sm90::to_swizzle_cute_type<kHeadDim>(), 0, kHeadDim * 8);
|
| 260 |
+
auto desc_b = mma::sm90::make_smem_desc(
|
| 261 |
+
smem_q[q_stage_idx] + k * WGMMA::K,
|
| 262 |
+
mma::sm90::to_swizzle_cute_type<kHeadDim>(), 0, kHeadDim * 8);
|
| 263 |
+
WGMMA::wgmma(desc_a, desc_b, accum, k);
|
| 264 |
+
}
|
| 265 |
+
ptx::warpgroup_commit_batch();
|
| 266 |
+
#pragma unroll
|
| 267 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 268 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 269 |
+
ptx::warpgroup_wait<0>();
|
| 270 |
+
|
| 271 |
+
// Release KV empty
|
| 272 |
+
empty_kv_barriers[kv_stage_idx]->arrive();
|
| 273 |
+
|
| 274 |
+
// Reduce over the head dim and store
|
| 275 |
+
const auto& kv_offset = kv_start + kv_block_idx * BLOCK_KV + warp_offset;
|
| 276 |
+
static constexpr uint32_t kNumAccumPerReduce = kNumHeads / 2;
|
| 277 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum % kNumAccumPerReduce == 0, "Invalid accumulation");
|
| 278 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum / kNumAccumPerReduce == BLOCK_Q, "Invalid accumulation");
|
| 279 |
+
DG_STATIC_ASSERT(kNumHeads % 8 == 0, "Invalid head");
|
| 280 |
+
#pragma unroll
|
| 281 |
+
for (uint32_t i = 0; i < BLOCK_Q; ++ i) {
|
| 282 |
+
auto shifted_accum = accum + i * kNumAccumPerReduce;
|
| 283 |
+
const auto transform = [&](const uint32_t& j) {
|
| 284 |
+
return fmaxf(shifted_accum[j], 0) * weights[i][(j / 4) * 2 + (j & 1)];
|
| 285 |
+
};
|
| 286 |
+
|
| 287 |
+
// Intra-thread reduction
|
| 288 |
+
float sum[4] = {transform(0), transform(1), transform(2), transform(3)};
|
| 289 |
+
#pragma unroll
|
| 290 |
+
for (uint32_t j = 1; j < kNumHeads / 8; ++ j) {
|
| 291 |
+
#pragma unroll
|
| 292 |
+
for (uint32_t k = 0; k < 4; k ++)
|
| 293 |
+
sum[k] += transform(j * 4 + k);
|
| 294 |
+
}
|
| 295 |
+
float v_0 = (sum[0] + sum[1]) * scale_kv_0;
|
| 296 |
+
float v_1 = (sum[2] + sum[3]) * scale_kv_1;
|
| 297 |
+
|
| 298 |
+
// Inter-thread reduction
|
| 299 |
+
#pragma unroll
|
| 300 |
+
for (uint32_t j = 0; j < 2; ++ j) {
|
| 301 |
+
const auto& offset = static_cast<int>(1u << j);
|
| 302 |
+
v_0 += __shfl_xor_sync(0xffffffffu, v_0, offset);
|
| 303 |
+
v_1 += __shfl_xor_sync(0xffffffffu, v_1, offset);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
// Store into the global memory
|
| 307 |
+
const auto q_offset = (block_q_idx * BLOCK_Q + i) * static_cast<uint64_t>(stride_logits);
|
| 308 |
+
if constexpr (kIsCompressedLogits) {
|
| 309 |
+
if (seq_k_start[i] <= kv_offset + v_0_offset and kv_offset + v_0_offset < seq_k_end[i])
|
| 310 |
+
logits[q_offset + kv_offset + v_0_offset - seq_k_start[i]] = static_cast<logits_dtype_t>(v_0);
|
| 311 |
+
if (seq_k_start[i] <= kv_offset + v_1_offset and kv_offset + v_1_offset < seq_k_end[i])
|
| 312 |
+
logits[q_offset + kv_offset + v_1_offset - seq_k_start[i]] = static_cast<logits_dtype_t>(v_1);
|
| 313 |
+
} else {
|
| 314 |
+
logits[q_offset + kv_offset + v_0_offset] = static_cast<logits_dtype_t>(v_0);
|
| 315 |
+
logits[q_offset + kv_offset + v_1_offset] = static_cast<logits_dtype_t>(v_1);
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
}
|
| 319 |
+
num_total_kv_blocks += num_kv_blocks;
|
| 320 |
+
|
| 321 |
+
// Release Q empty
|
| 322 |
+
empty_q_barriers[q_stage_idx]->arrive();
|
| 323 |
+
|
| 324 |
+
// Jump to the next block
|
| 325 |
+
CUTE_TIE(get_next_block_q_idx(), block_q_idx, q_iter_idx);
|
| 326 |
+
}
|
| 327 |
+
}
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_fp8_paged_mqa_logits.cuh
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 5 |
+
|
| 6 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 7 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 10 |
+
#include <deep_gemm/common/math.cuh>
|
| 11 |
+
#include <deep_gemm/common/utils.cuh>
|
| 12 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 13 |
+
#include <deep_gemm/common/types.cuh>
|
| 14 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 16 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 17 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 18 |
+
#include <deep_gemm/scheduler/paged_mqa_logits.cuh>
|
| 19 |
+
|
| 20 |
+
namespace deep_gemm {
|
| 21 |
+
|
| 22 |
+
template <uint32_t kNextN, uint32_t kNumHeads,
|
| 23 |
+
uint32_t kHeadDim, uint32_t BLOCK_KV,
|
| 24 |
+
bool kIsContextLens2D, bool kIsVarlen,
|
| 25 |
+
uint32_t kNumQStages, uint32_t kNumKVStages,
|
| 26 |
+
uint32_t SPLIT_KV,
|
| 27 |
+
uint32_t kNumTMAThreads, uint32_t kNumMathThreads,
|
| 28 |
+
typename logits_dtype_t>
|
| 29 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumTMAThreads + kNumMathThreads, 1)
|
| 30 |
+
void sm90_fp8_paged_mqa_logits(const uint32_t batch_size,
|
| 31 |
+
const uint32_t logits_stride, const uint32_t block_table_stride,
|
| 32 |
+
const uint32_t* context_lens, logits_dtype_t* logits,
|
| 33 |
+
const uint32_t* block_table, const uint32_t* indices,
|
| 34 |
+
const uint32_t* schedule_meta,
|
| 35 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_q,
|
| 36 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv,
|
| 37 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_kv_scales,
|
| 38 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_weights) {
|
| 39 |
+
DG_STATIC_ASSERT(not kIsVarlen, "Varlen is not supported for SM90 paged MQA logits");
|
| 40 |
+
|
| 41 |
+
// Types
|
| 42 |
+
using WGMMA = typename mma::sm90::FP8MMASelector<kNextN * kNumHeads>::type;
|
| 43 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 44 |
+
|
| 45 |
+
// NOTES: use `__shfl_sync` to encourage NVCC to use unified registers
|
| 46 |
+
const auto warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 47 |
+
const auto warpgroup_idx = warp_idx / 4;
|
| 48 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 49 |
+
|
| 50 |
+
// Prefetch TMA descriptors
|
| 51 |
+
static constexpr uint32_t kNumMathWarpGroups = kNumMathThreads / 128;
|
| 52 |
+
DG_STATIC_ASSERT(kNumTMAThreads == 128 and kNumMathThreads % 128 == 0, "Invalid threads");
|
| 53 |
+
DG_STATIC_ASSERT(SPLIT_KV == BLOCK_KV * kNumMathWarpGroups, "Invalid `SPLIT_KV`");
|
| 54 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 55 |
+
cute::prefetch_tma_descriptor(&tensor_map_q);
|
| 56 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv);
|
| 57 |
+
cute::prefetch_tma_descriptor(&tensor_map_kv_scales);
|
| 58 |
+
cute::prefetch_tma_descriptor(&tensor_map_weights);
|
| 59 |
+
}
|
| 60 |
+
__syncwarp();
|
| 61 |
+
|
| 62 |
+
// Shared memory configs
|
| 63 |
+
static constexpr uint32_t kSwizzleAlignment = kHeadDim * 8;
|
| 64 |
+
static constexpr uint32_t SMEM_Q_SIZE_PER_STAGE = kNextN * kNumHeads * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 65 |
+
static constexpr uint32_t SMEM_WEIGHT_SIZE_PER_STAGE = kNextN * kNumHeads * sizeof(float);
|
| 66 |
+
static constexpr uint32_t ALIGNED_SMEM_WEIGHT_SIZE_PER_STAGE = math::constexpr_align(SMEM_WEIGHT_SIZE_PER_STAGE, kSwizzleAlignment);
|
| 67 |
+
static constexpr uint32_t SMEM_Q_PIPE_SIZE = kNumQStages * (SMEM_Q_SIZE_PER_STAGE + ALIGNED_SMEM_WEIGHT_SIZE_PER_STAGE) +
|
| 68 |
+
math::constexpr_align(kNumQStages * 8 * 2, kSwizzleAlignment);
|
| 69 |
+
|
| 70 |
+
static constexpr uint32_t SMEM_KV_SIZE_PER_STAGE = BLOCK_KV * kHeadDim * sizeof(__nv_fp8_e4m3);
|
| 71 |
+
static constexpr uint32_t SMEM_KV_SCALE_SIZE_PER_STAGE = BLOCK_KV * sizeof(float);
|
| 72 |
+
static constexpr uint32_t ALIGNED_SMEM_KV_SCALE_SIZE_PER_STAGE = math::constexpr_align(SMEM_KV_SCALE_SIZE_PER_STAGE, kSwizzleAlignment);
|
| 73 |
+
static constexpr uint32_t SMEM_KV_PIPE_SIZE = kNumKVStages * (SMEM_KV_SIZE_PER_STAGE + ALIGNED_SMEM_KV_SCALE_SIZE_PER_STAGE) +
|
| 74 |
+
math::constexpr_align(kNumKVStages * 8 * 2, kSwizzleAlignment);
|
| 75 |
+
|
| 76 |
+
// Align to swizzling alignment bytes
|
| 77 |
+
extern __shared__ __align__(kSwizzleAlignment) uint8_t smem_buffer[];
|
| 78 |
+
DG_STATIC_ASSERT(SMEM_Q_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 79 |
+
DG_STATIC_ASSERT(SMEM_KV_SIZE_PER_STAGE % kSwizzleAlignment == 0, "Unaligned TMA swizzling");
|
| 80 |
+
|
| 81 |
+
// Q data and barriers on shared memory
|
| 82 |
+
auto smem_q = utils::PatternVisitor([&](const uint32_t& i) {
|
| 83 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + SMEM_Q_SIZE_PER_STAGE * i);
|
| 84 |
+
});
|
| 85 |
+
auto smem_weights = utils::PatternVisitor([&](const uint32_t& i) {
|
| 86 |
+
return reinterpret_cast<float*>(smem_buffer + SMEM_Q_SIZE_PER_STAGE * kNumQStages + ALIGNED_SMEM_WEIGHT_SIZE_PER_STAGE * i);
|
| 87 |
+
});
|
| 88 |
+
auto q_barrier_ptr = reinterpret_cast<Barrier*>(smem_weights[kNumQStages]);
|
| 89 |
+
auto full_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return q_barrier_ptr + i; });
|
| 90 |
+
auto empty_q_barriers = utils::PatternVisitor([&](const uint32_t& i) { return q_barrier_ptr + (kNumQStages + i); });
|
| 91 |
+
|
| 92 |
+
// Separate math warpgroups and tma load warps into KV groups
|
| 93 |
+
// Each math warpgroup corresponds to a tma load warp
|
| 94 |
+
const auto kv_group_idx = __shfl_sync(0xffffffff, threadIdx.x >= kNumMathThreads ? (threadIdx.x - kNumMathThreads) / 32 : warpgroup_idx, 0);
|
| 95 |
+
|
| 96 |
+
// Per group KV data and barriers on shared memory
|
| 97 |
+
const auto smem_offset = SMEM_Q_PIPE_SIZE + SMEM_KV_PIPE_SIZE * kv_group_idx;
|
| 98 |
+
auto smem_kv = utils::PatternVisitor([&](const uint32_t& i) {
|
| 99 |
+
return reinterpret_cast<__nv_fp8_e4m3*>(smem_buffer + smem_offset + SMEM_KV_SIZE_PER_STAGE * i);
|
| 100 |
+
});
|
| 101 |
+
auto smem_kv_scales = utils::PatternVisitor([&](const uint32_t& i) {
|
| 102 |
+
return reinterpret_cast<float*>(smem_buffer + smem_offset + SMEM_KV_SIZE_PER_STAGE * kNumKVStages + ALIGNED_SMEM_KV_SCALE_SIZE_PER_STAGE * i);
|
| 103 |
+
});
|
| 104 |
+
auto kv_barrier_ptr = reinterpret_cast<Barrier*>(smem_kv_scales[kNumKVStages]);
|
| 105 |
+
auto full_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return kv_barrier_ptr + i; });
|
| 106 |
+
auto empty_kv_barriers = utils::PatternVisitor([&](const uint32_t& i) { return kv_barrier_ptr + kNumKVStages + i; });
|
| 107 |
+
|
| 108 |
+
// Initialize barriers
|
| 109 |
+
if (warp_idx >= kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 110 |
+
if (kv_group_idx == 0) {
|
| 111 |
+
#pragma unroll
|
| 112 |
+
for (uint32_t i = 0; i < kNumQStages; ++ i) {
|
| 113 |
+
full_q_barriers[i]->init(1);
|
| 114 |
+
empty_q_barriers[i]->init(kNumMathThreads);
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
if (kv_group_idx < kNumMathWarpGroups) {
|
| 118 |
+
#pragma unroll
|
| 119 |
+
for (uint32_t i = 0; i < kNumKVStages; ++ i) {
|
| 120 |
+
full_kv_barriers[i]->init(1);
|
| 121 |
+
empty_kv_barriers[i]->init(128);
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Make initialized barrier visible in async proxy
|
| 126 |
+
cutlass::arch::fence_barrier_init();
|
| 127 |
+
}
|
| 128 |
+
__syncthreads();
|
| 129 |
+
|
| 130 |
+
// Register reconfigurations
|
| 131 |
+
constexpr uint32_t kNumTMARegisters = 64;
|
| 132 |
+
constexpr uint32_t kNumMathRegisters = 104;
|
| 133 |
+
|
| 134 |
+
// Wait for primary kernel completion
|
| 135 |
+
cudaGridDependencySynchronize();
|
| 136 |
+
|
| 137 |
+
// Scheduler
|
| 138 |
+
auto scheduler = sched::PagedMQALogitsScheduler<kNextN, kIsContextLens2D, kIsVarlen, BLOCK_KV, kNumMathWarpGroups, 1>(
|
| 139 |
+
blockIdx.x, batch_size, context_lens, schedule_meta, indices);
|
| 140 |
+
DG_STATIC_ASSERT(SPLIT_KV % BLOCK_KV == 0, "Unaligned SPLIT_KV");
|
| 141 |
+
|
| 142 |
+
// Q and KV pipeline
|
| 143 |
+
const auto get_q_pipeline = [=](const uint32_t& q_iter_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 144 |
+
return {q_iter_idx % kNumQStages, (q_iter_idx / kNumQStages) & 1}; // Q pipeline stage and phase
|
| 145 |
+
};
|
| 146 |
+
const auto get_kv_pipeline = [=](const uint32_t& kv_iter_idx) -> cute::tuple<uint32_t, uint32_t> {
|
| 147 |
+
return {kv_iter_idx % kNumKVStages, (kv_iter_idx / kNumKVStages) & 1}; // KV pipeline stage and phase
|
| 148 |
+
};
|
| 149 |
+
uint32_t q_iter_idx = 0, kv_iter_idx = 0;
|
| 150 |
+
|
| 151 |
+
if (warp_idx >= kNumMathThreads / 32) {
|
| 152 |
+
// TMA warp-group for loading data
|
| 153 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 154 |
+
if (kv_group_idx >= kNumMathWarpGroups)
|
| 155 |
+
return;
|
| 156 |
+
|
| 157 |
+
const auto issue_tma_q = [&](const uint32_t& stage_idx, const uint32_t& q_idx) {
|
| 158 |
+
if (kv_group_idx == 0 and cute::elect_one_sync()) {
|
| 159 |
+
tma::copy<kHeadDim, kNextN * kNumHeads, kHeadDim>(&tensor_map_q, full_q_barriers[stage_idx], smem_q[stage_idx], 0, q_idx * kNextN * kNumHeads);
|
| 160 |
+
tma::copy<kNextN * kNumHeads, 1, 0>(&tensor_map_weights, full_q_barriers[stage_idx], smem_weights[stage_idx], 0, q_idx * kNextN);
|
| 161 |
+
full_q_barriers[stage_idx]->arrive_and_expect_tx(SMEM_Q_SIZE_PER_STAGE + SMEM_WEIGHT_SIZE_PER_STAGE);
|
| 162 |
+
}
|
| 163 |
+
};
|
| 164 |
+
|
| 165 |
+
// Initialize `q_idx` outside `[0, batch_size)` to indicate it was none
|
| 166 |
+
uint32_t q_idx = batch_size, kv_idx, num_kv;
|
| 167 |
+
uint32_t next_q_idx, next_kv_idx, next_num_kv;
|
| 168 |
+
bool fetched_next_task;
|
| 169 |
+
|
| 170 |
+
// Prefetch the first Q
|
| 171 |
+
if ((fetched_next_task = scheduler.fetch_next_task(next_q_idx, next_kv_idx, next_num_kv)))
|
| 172 |
+
issue_tma_q(0, next_q_idx), q_iter_idx = 1;
|
| 173 |
+
|
| 174 |
+
int kv_block_idx_ptr = 32;
|
| 175 |
+
uint32_t kv_block_idx_storage;
|
| 176 |
+
|
| 177 |
+
while (fetched_next_task) {
|
| 178 |
+
// Prefetch next Q when current Q changes
|
| 179 |
+
bool prefetch_q = (q_idx != next_q_idx and scheduler.exist_q_atom_idx(next_q_idx + 1));
|
| 180 |
+
q_idx = next_q_idx;
|
| 181 |
+
kv_idx = next_kv_idx;
|
| 182 |
+
num_kv = next_num_kv;
|
| 183 |
+
|
| 184 |
+
// Wait Q consumer release and issue TMA Q
|
| 185 |
+
if (prefetch_q) {
|
| 186 |
+
CUTE_TIE_DECL(get_q_pipeline(q_iter_idx ++), q_stage_idx, q_phase);
|
| 187 |
+
empty_q_barriers[q_stage_idx]->wait(q_phase ^ 1);
|
| 188 |
+
issue_tma_q(q_stage_idx, q_idx + 1);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
// Read KV block index
|
| 192 |
+
// TODO: deal with `-1`?
|
| 193 |
+
if (kv_idx == 0 or kv_block_idx_ptr == 32) {
|
| 194 |
+
kv_block_idx_ptr = 0;
|
| 195 |
+
kv_block_idx_storage = (kv_idx + kv_group_idx + lane_idx * kNumMathWarpGroups < num_kv ?
|
| 196 |
+
block_table[q_idx * static_cast<uint64_t>(block_table_stride) + (kv_idx + kv_group_idx + lane_idx * kNumMathWarpGroups)] : 0);
|
| 197 |
+
}
|
| 198 |
+
const auto kv_block_idx = __shfl_sync(0xffffffff, kv_block_idx_storage, kv_block_idx_ptr ++);
|
| 199 |
+
|
| 200 |
+
// Wait KV consumer release
|
| 201 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_iter_idx ++), kv_stage_idx, kv_phase);
|
| 202 |
+
empty_kv_barriers[kv_stage_idx]->wait(kv_phase ^ 1);
|
| 203 |
+
|
| 204 |
+
// Issue TMA KV
|
| 205 |
+
if (cute::elect_one_sync()) {
|
| 206 |
+
tma::copy<kHeadDim, BLOCK_KV, 0, __nv_fp8_e4m3, true>(&tensor_map_kv, full_kv_barriers[kv_stage_idx],
|
| 207 |
+
smem_kv[kv_stage_idx], 0, 0, 1, kv_block_idx);
|
| 208 |
+
tma::copy<BLOCK_KV, 1, 0>(&tensor_map_kv_scales, full_kv_barriers[kv_stage_idx],
|
| 209 |
+
smem_kv_scales[kv_stage_idx], 0, kv_block_idx);
|
| 210 |
+
full_kv_barriers[kv_stage_idx]->arrive_and_expect_tx(SMEM_KV_SIZE_PER_STAGE + SMEM_KV_SCALE_SIZE_PER_STAGE);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// Fetch next task
|
| 214 |
+
fetched_next_task = scheduler.fetch_next_task(next_q_idx, next_kv_idx, next_num_kv);
|
| 215 |
+
}
|
| 216 |
+
} else {
|
| 217 |
+
// Math warp-groups for WGMMA
|
| 218 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 219 |
+
|
| 220 |
+
float accum[WGMMA::kNumAccum], weights[kNextN][kNumHeads / 4];
|
| 221 |
+
const auto sub_warp_offset = (warp_idx % 4) * 16;
|
| 222 |
+
const auto v_0_offset = lane_idx / 4 + 0;
|
| 223 |
+
const auto v_1_offset = lane_idx / 4 + 8;
|
| 224 |
+
|
| 225 |
+
// Initialize `q_idx` outside `[0, batch_size)` to indicate it was none
|
| 226 |
+
uint32_t q_idx = batch_size, kv_idx;
|
| 227 |
+
uint32_t next_q_idx, next_kv_idx, next_num_kv;
|
| 228 |
+
uint32_t q_stage_idx, q_phase;
|
| 229 |
+
|
| 230 |
+
while (scheduler.fetch_next_task(next_q_idx, next_kv_idx, next_num_kv)) {
|
| 231 |
+
// Current Q changes
|
| 232 |
+
if (q_idx != next_q_idx) {
|
| 233 |
+
// Release Last Q empty
|
| 234 |
+
if (q_iter_idx > 0)
|
| 235 |
+
empty_q_barriers[(q_iter_idx - 1) % kNumQStages]->arrive();
|
| 236 |
+
|
| 237 |
+
// Wait TMA Q arrival
|
| 238 |
+
CUTE_TIE(get_q_pipeline(q_iter_idx ++), q_stage_idx, q_phase);
|
| 239 |
+
full_q_barriers[q_stage_idx]->wait(q_phase);
|
| 240 |
+
|
| 241 |
+
// Read weights
|
| 242 |
+
#pragma unroll
|
| 243 |
+
for (uint32_t i = 0; i < kNextN; ++ i) {
|
| 244 |
+
#pragma unroll
|
| 245 |
+
for (uint32_t j = 0; j < kNumHeads / 4; ++ j)
|
| 246 |
+
weights[i][j] = ptx::ld_shared(smem_weights[q_stage_idx] + i * kNumHeads + (j / 2) * 8 + (j & 1) + (lane_idx % 4) * 2);
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
// Get current Q and KV index
|
| 251 |
+
q_idx = next_q_idx;
|
| 252 |
+
kv_idx = next_kv_idx;
|
| 253 |
+
|
| 254 |
+
// Calculate KV offset in advance
|
| 255 |
+
auto kv_offset = q_idx * kNextN * static_cast<uint64_t>(logits_stride) + ((kv_idx + kv_group_idx) * BLOCK_KV + sub_warp_offset);
|
| 256 |
+
|
| 257 |
+
// Compute `[kNextN * kNumHeads, kHeadDim] @ [BLOCK_KV, kHeadDim] -> [kNextN, BLOCK_KV]`
|
| 258 |
+
// Wait TMA KV arrival
|
| 259 |
+
CUTE_TIE_DECL(get_kv_pipeline(kv_iter_idx ++), kv_stage_idx, kv_phase);
|
| 260 |
+
full_kv_barriers[kv_stage_idx]->wait(kv_phase);
|
| 261 |
+
|
| 262 |
+
// Issue WGMMA
|
| 263 |
+
DG_STATIC_ASSERT(BLOCK_KV == 64, "Invalid block size");
|
| 264 |
+
DG_STATIC_ASSERT(kHeadDim % WGMMA::K == 0, "Invalid head dim");
|
| 265 |
+
#pragma unroll
|
| 266 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 267 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 268 |
+
ptx::warpgroup_arrive();
|
| 269 |
+
#pragma unroll
|
| 270 |
+
for (uint32_t k = 0; k < kHeadDim / WGMMA::K; ++ k) {
|
| 271 |
+
auto desc_a = mma::sm90::make_smem_desc(
|
| 272 |
+
smem_kv[kv_stage_idx] + k * WGMMA::K,
|
| 273 |
+
mma::sm90::to_swizzle_cute_type<kHeadDim>(), 0, kHeadDim * 8);
|
| 274 |
+
auto desc_b = mma::sm90::make_smem_desc(
|
| 275 |
+
smem_q[q_stage_idx] + k * WGMMA::K,
|
| 276 |
+
mma::sm90::to_swizzle_cute_type<kHeadDim>(), 0, kHeadDim * 8);
|
| 277 |
+
WGMMA::wgmma(desc_a, desc_b, accum, k);
|
| 278 |
+
}
|
| 279 |
+
ptx::warpgroup_commit_batch();
|
| 280 |
+
#pragma unroll
|
| 281 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 282 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 283 |
+
|
| 284 |
+
// Read per-KV scales
|
| 285 |
+
float scale_kv_0 = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + sub_warp_offset + v_0_offset);
|
| 286 |
+
float scale_kv_1 = ptx::ld_shared(smem_kv_scales[kv_stage_idx] + sub_warp_offset + v_1_offset);
|
| 287 |
+
|
| 288 |
+
// Wait WGMMA
|
| 289 |
+
ptx::warpgroup_wait<0>();
|
| 290 |
+
|
| 291 |
+
// Release KV empty
|
| 292 |
+
empty_kv_barriers[kv_stage_idx]->arrive();
|
| 293 |
+
|
| 294 |
+
// Reduce over the head dim and store
|
| 295 |
+
static constexpr uint32_t kNumAccumPerReduce = kNumHeads / 2;
|
| 296 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum % kNumAccumPerReduce == 0, "Invalid accumulation");
|
| 297 |
+
DG_STATIC_ASSERT(WGMMA::kNumAccum / kNumAccumPerReduce == kNextN, "Invalid accumulation");
|
| 298 |
+
DG_STATIC_ASSERT(kNumHeads % 8 == 0, "Invalid head");
|
| 299 |
+
#pragma unroll
|
| 300 |
+
for (uint32_t i = 0; i < kNextN; ++ i) {
|
| 301 |
+
auto shifted_accum = accum + i * kNumAccumPerReduce;
|
| 302 |
+
const auto transform = [&](const uint32_t& j) {
|
| 303 |
+
return fmaxf(shifted_accum[j], 0) * weights[i][(j / 4) * 2 + (j & 1)];
|
| 304 |
+
};
|
| 305 |
+
|
| 306 |
+
// Intra-thread reduction
|
| 307 |
+
float sum[4] = {transform(0), transform(1), transform(2), transform(3)};
|
| 308 |
+
#pragma unroll
|
| 309 |
+
for (uint32_t j = 1; j < kNumHeads / 8; ++ j) {
|
| 310 |
+
#pragma unroll
|
| 311 |
+
for (uint32_t k = 0; k < 4; k ++)
|
| 312 |
+
sum[k] += transform(j * 4 + k);
|
| 313 |
+
}
|
| 314 |
+
float v_0 = (sum[0] + sum[1]) * scale_kv_0;
|
| 315 |
+
float v_1 = (sum[2] + sum[3]) * scale_kv_1;
|
| 316 |
+
|
| 317 |
+
// Inter-thread reduction
|
| 318 |
+
#pragma unroll
|
| 319 |
+
for (uint32_t j = 0; j < 2; ++ j) {
|
| 320 |
+
const auto offset = static_cast<int>(1u << j);
|
| 321 |
+
v_0 += __shfl_xor_sync(0xffffffffu, v_0, offset);
|
| 322 |
+
v_1 += __shfl_xor_sync(0xffffffffu, v_1, offset);
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
// Store into the global memory
|
| 326 |
+
// NOTES: we have redundant writes here, consider more carefully
|
| 327 |
+
logits[kv_offset + i * static_cast<uint64_t>(logits_stride) + v_0_offset] = static_cast<logits_dtype_t>(v_0);
|
| 328 |
+
logits[kv_offset + i * static_cast<uint64_t>(logits_stride) + v_1_offset] = static_cast<logits_dtype_t>(v_1);
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/sm90_tf32_hc_prenorm_gemm.cuh
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#pragma clang diagnostic push
|
| 3 |
+
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
| 4 |
+
|
| 5 |
+
#include <cutlass/arch/barrier.h>
|
| 6 |
+
#include <cutlass/arch/reg_reconfig.h>
|
| 7 |
+
|
| 8 |
+
#include <deep_gemm/common/math.cuh>
|
| 9 |
+
#include <deep_gemm/common/utils.cuh>
|
| 10 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 11 |
+
#include <deep_gemm/common/types.cuh>
|
| 12 |
+
#include <deep_gemm/mma/sm90.cuh>
|
| 13 |
+
#include <deep_gemm/ptx/ld_st.cuh>
|
| 14 |
+
#include <deep_gemm/ptx/utils.cuh>
|
| 15 |
+
#include <deep_gemm/ptx/wgmma.cuh>
|
| 16 |
+
|
| 17 |
+
namespace deep_gemm {
|
| 18 |
+
|
| 19 |
+
template <uint32_t kSwizzleMode, uint32_t kSwizzleBase = 16>
|
| 20 |
+
CUTLASS_DEVICE
|
| 21 |
+
uint32_t get_swizzled_bank_group_idx(const uint32_t& offset, const uint32_t& lane_idx) {
|
| 22 |
+
constexpr uint32_t kGroupsInSwizzleRange = kSwizzleMode / kSwizzleBase;
|
| 23 |
+
|
| 24 |
+
const auto bank_group_idx = offset + lane_idx * kGroupsInSwizzleRange;
|
| 25 |
+
|
| 26 |
+
constexpr uint32_t kNumBankGroups = 128 / kSwizzleBase;
|
| 27 |
+
constexpr bool kHasShortcut = kGroupsInSwizzleRange == kNumBankGroups;
|
| 28 |
+
auto row = kHasShortcut ? (offset / kNumBankGroups + lane_idx) : (bank_group_idx / kNumBankGroups);
|
| 29 |
+
auto col = kHasShortcut ? (offset) : (bank_group_idx % kNumBankGroups);
|
| 30 |
+
col ^= row % kGroupsInSwizzleRange;
|
| 31 |
+
|
| 32 |
+
return (row * kNumBankGroups + col) % kGroupsInSwizzleRange;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
template <uint32_t SHAPE_N, uint32_t SHAPE_K,
|
| 36 |
+
uint32_t BLOCK_M, uint32_t BLOCK_N, uint32_t BLOCK_K,
|
| 37 |
+
uint32_t kNumSplits,
|
| 38 |
+
uint32_t kSwizzleCDMode,
|
| 39 |
+
uint32_t kNumStages,
|
| 40 |
+
uint32_t kNumMathThreads, uint32_t kNumTMAThreads>
|
| 41 |
+
CUTLASS_GLOBAL void __launch_bounds__(kNumMathThreads + kNumTMAThreads, 1)
|
| 42 |
+
sm90_tf32_hc_prenorm_gemm_impl(const uint32_t shape_m,
|
| 43 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_a,
|
| 44 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_b,
|
| 45 |
+
const __grid_constant__ cute::TmaDescriptor tensor_map_d,
|
| 46 |
+
float* sqr_sum) {
|
| 47 |
+
#if (defined(__CUDA_ARCH__) and (__CUDA_ARCH__ >= 900)) or defined(__CLION_IDE__)
|
| 48 |
+
using Barrier = cutlass::arch::ClusterTransactionBarrier;
|
| 49 |
+
|
| 50 |
+
// kSwizzleAMode and kSwizzleBMode must be 128 for now
|
| 51 |
+
constexpr uint32_t kSwizzleAMode = cute::min(BLOCK_K * sizeof(nv_bfloat16), 128);
|
| 52 |
+
constexpr uint32_t kSwizzleBMode = cute::min(BLOCK_K * sizeof(float), 128);
|
| 53 |
+
DG_STATIC_ASSERT(BLOCK_K == 64, "Invalid block K");
|
| 54 |
+
DG_STATIC_ASSERT(kSwizzleAMode == 128, "Invalid swizzle A mode");
|
| 55 |
+
DG_STATIC_ASSERT(kSwizzleBMode == 128, "Invalid swizzle B mode");
|
| 56 |
+
|
| 57 |
+
DG_STATIC_ASSERT(kSwizzleCDMode / sizeof(float) == BLOCK_N, "Invalid block N");
|
| 58 |
+
DG_STATIC_ASSERT(kNumMathThreads == 128, "Invalid MMA threads");
|
| 59 |
+
|
| 60 |
+
// Utils
|
| 61 |
+
const auto warp_idx = cutlass::canonical_warp_idx_sync();
|
| 62 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 63 |
+
|
| 64 |
+
// Align to 1024 bytes for swizzle-128B
|
| 65 |
+
extern __shared__ __align__(1024) uint8_t smem_buffer[];
|
| 66 |
+
|
| 67 |
+
// Share memory sizes
|
| 68 |
+
constexpr uint32_t SMEM_CD_SIZE = BLOCK_M * kSwizzleCDMode;
|
| 69 |
+
constexpr uint32_t SMEM_A_SIZE_PER_STAGE = BLOCK_M * BLOCK_K * sizeof(nv_bfloat16);
|
| 70 |
+
constexpr uint32_t SMEM_B_SIZE_PER_STAGE = BLOCK_N * BLOCK_K * sizeof(float);
|
| 71 |
+
DG_STATIC_ASSERT(SMEM_CD_SIZE % 1024 == 0, "Shared memory of A/B must be aligned to 1024 bytes");
|
| 72 |
+
|
| 73 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 74 |
+
cute::prefetch_tma_descriptor(&tensor_map_a);
|
| 75 |
+
cute::prefetch_tma_descriptor(&tensor_map_b);
|
| 76 |
+
cute::prefetch_tma_descriptor(&tensor_map_d);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// Data on shared memory (layout as ordered below)
|
| 80 |
+
// Fill D/A/B pointers
|
| 81 |
+
auto smem_cd = reinterpret_cast<float*>(smem_buffer);
|
| 82 |
+
auto smem_a = utils::PatternVisitor([&](const uint32_t& i) {
|
| 83 |
+
return reinterpret_cast<nv_bfloat16*>(smem_buffer + (SMEM_CD_SIZE + i * SMEM_A_SIZE_PER_STAGE));
|
| 84 |
+
});
|
| 85 |
+
auto smem_b = utils::PatternVisitor([&](const uint32_t& i) {
|
| 86 |
+
return reinterpret_cast<float*>(smem_buffer + (SMEM_CD_SIZE + kNumStages * SMEM_A_SIZE_PER_STAGE + i * SMEM_B_SIZE_PER_STAGE));
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
// Fill barriers
|
| 90 |
+
auto barrier_start_ptr = reinterpret_cast<Barrier*>(smem_buffer + SMEM_CD_SIZE + kNumStages * (SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE));
|
| 91 |
+
auto full_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (i); });
|
| 92 |
+
auto empty_barriers = utils::PatternVisitor([=](const uint32_t& i) { return barrier_start_ptr + (kNumStages + i); });
|
| 93 |
+
|
| 94 |
+
// Initialize barriers
|
| 95 |
+
if (warp_idx == 1 and cute::elect_one_sync()) {
|
| 96 |
+
#pragma unroll
|
| 97 |
+
for (uint32_t i = 0; i < kNumStages; ++ i) {
|
| 98 |
+
full_barriers[i]->init(1);
|
| 99 |
+
empty_barriers[i]->init(128);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Make initialized barrier visible in async proxy
|
| 103 |
+
cutlass::arch::fence_barrier_init();
|
| 104 |
+
}
|
| 105 |
+
__syncthreads();
|
| 106 |
+
|
| 107 |
+
constexpr uint32_t kNumKBlocks = math::constexpr_ceil_div(SHAPE_K, BLOCK_K);
|
| 108 |
+
constexpr uint32_t kNumKBlocksPerSplit = kNumKBlocks / kNumSplits;
|
| 109 |
+
constexpr uint32_t kRemainKBlocks = kNumKBlocks % kNumSplits;
|
| 110 |
+
const uint32_t block_idx = __shfl_sync(0xffffffff, blockIdx.x, 0);
|
| 111 |
+
const uint32_t m_block_idx = block_idx / kNumSplits;
|
| 112 |
+
const uint32_t k_split_idx = block_idx % kNumSplits;
|
| 113 |
+
const uint32_t k_offset = (k_split_idx * kNumKBlocksPerSplit + cute::min(k_split_idx, kRemainKBlocks)) * BLOCK_K;
|
| 114 |
+
const uint32_t m_offset = shape_m * k_split_idx;
|
| 115 |
+
const uint32_t num_total_stages = kNumKBlocksPerSplit + (k_split_idx < kRemainKBlocks);
|
| 116 |
+
constexpr uint32_t kNumTMARegisters = 40;
|
| 117 |
+
constexpr uint32_t kNumMathRegisters = 256;
|
| 118 |
+
|
| 119 |
+
// Wait for primary kernel completion
|
| 120 |
+
cudaGridDependencySynchronize();
|
| 121 |
+
|
| 122 |
+
// TMA load warp
|
| 123 |
+
if (warp_idx == kNumMathThreads / 32 and cute::elect_one_sync()) {
|
| 124 |
+
cutlass::arch::warpgroup_reg_dealloc<kNumTMARegisters>();
|
| 125 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 126 |
+
// Wait consumer release
|
| 127 |
+
const auto stage_idx = s % kNumStages;
|
| 128 |
+
empty_barriers[stage_idx]->wait(((s / kNumStages) & 1) ^ 1);
|
| 129 |
+
|
| 130 |
+
// Compute offsets
|
| 131 |
+
uint32_t m_idx = m_block_idx * BLOCK_M;
|
| 132 |
+
uint32_t k_idx = k_offset + s * BLOCK_K;
|
| 133 |
+
|
| 134 |
+
// Issue TMAs
|
| 135 |
+
tma::copy<BLOCK_K, BLOCK_M, kSwizzleAMode>(&tensor_map_a, full_barriers[stage_idx], smem_a[stage_idx], k_idx, m_idx);
|
| 136 |
+
tma::copy<BLOCK_K, BLOCK_N, kSwizzleBMode>(&tensor_map_b, full_barriers[stage_idx], smem_b[stage_idx], k_idx, 0);
|
| 137 |
+
|
| 138 |
+
// Arrive at full barriers
|
| 139 |
+
constexpr uint32_t kNumArrivalBytes = SMEM_A_SIZE_PER_STAGE + SMEM_B_SIZE_PER_STAGE;
|
| 140 |
+
full_barriers[stage_idx]->arrive_and_expect_tx(kNumArrivalBytes);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
for (uint32_t s = num_total_stages; s < num_total_stages + kNumStages; ++ s) {
|
| 144 |
+
const auto stage_idx = s % kNumStages;
|
| 145 |
+
empty_barriers[stage_idx]->wait(((s / kNumStages) & 1) ^ 1);
|
| 146 |
+
}
|
| 147 |
+
} else if (warp_idx < kNumMathThreads / 32) {
|
| 148 |
+
cutlass::arch::warpgroup_reg_alloc<kNumMathRegisters>();
|
| 149 |
+
|
| 150 |
+
DG_STATIC_ASSERT(BLOCK_M == 64, "Invalid block M");
|
| 151 |
+
DG_STATIC_ASSERT(BLOCK_K * sizeof(nv_bfloat16) == kSwizzleAMode, "Invalid block K");
|
| 152 |
+
constexpr uint32_t BLOCK_M_PER_WARP = BLOCK_M / 4;
|
| 153 |
+
constexpr uint32_t WGMMA_M = 64;
|
| 154 |
+
constexpr uint32_t WGMMA_N = BLOCK_N;
|
| 155 |
+
constexpr uint32_t WGMMA_K = 8;
|
| 156 |
+
|
| 157 |
+
using WGMMA = typename mma::sm90::TF32MMASelector<WGMMA_N, true>::type;
|
| 158 |
+
float accum[WGMMA::kNumAccum] = {0};
|
| 159 |
+
|
| 160 |
+
constexpr uint32_t kNumBankGroupBytes = 16;
|
| 161 |
+
constexpr uint32_t kNumElemsPerBankGroup = kNumBankGroupBytes / sizeof(nv_bfloat16);
|
| 162 |
+
constexpr uint32_t kNumLoads = BLOCK_K / kNumElemsPerBankGroup;
|
| 163 |
+
float sqr_sum_acc_0 = 0;
|
| 164 |
+
float sqr_sum_acc_1 = 0;
|
| 165 |
+
|
| 166 |
+
#pragma unroll kNumStages < 8 ? kNumStages : kNumStages / 2
|
| 167 |
+
for (uint32_t s = 0; s < num_total_stages; ++ s) {
|
| 168 |
+
// Wait TMA arrival
|
| 169 |
+
const auto& stage_idx = s % kNumStages;
|
| 170 |
+
full_barriers[stage_idx]->wait((s / kNumStages) & 1);
|
| 171 |
+
|
| 172 |
+
constexpr uint32_t kNumRegPerWgmma = WGMMA::M * WGMMA::K / 128;
|
| 173 |
+
constexpr uint32_t kNumWgmmaPerBlockK = BLOCK_K / WGMMA::K;
|
| 174 |
+
|
| 175 |
+
float a[kNumRegPerWgmma * kNumWgmmaPerBlockK];
|
| 176 |
+
// Assume swizzle A mode is 128
|
| 177 |
+
DG_STATIC_ASSERT(kSwizzleAMode == 128, "Invalid swizzle A mode");
|
| 178 |
+
|
| 179 |
+
// Load BF16 A fragment from shared memory into registers, and transpose to FP32
|
| 180 |
+
uint32_t row = warp_idx * 16 + lane_idx / 4;
|
| 181 |
+
#pragma unroll
|
| 182 |
+
for (uint32_t i = 0; i < kNumLoads; ++ i) {
|
| 183 |
+
// Refer to the A layout in https://docs.nvidia.com/cuda/parallel-thread-execution/#wgmma-64n8-a
|
| 184 |
+
uint32_t bank_group_idx = (row ^ i) % 8;
|
| 185 |
+
nv_bfloat16* a_bf16_smem_ptr_upper = smem_a[stage_idx] + row * BLOCK_K + bank_group_idx * kNumElemsPerBankGroup;
|
| 186 |
+
nv_bfloat16* a_bf16_smem_ptr_lower = smem_a[stage_idx] + (row + 8) * BLOCK_K + bank_group_idx * kNumElemsPerBankGroup;
|
| 187 |
+
|
| 188 |
+
uint32_t elem_offset = lane_idx % 4;
|
| 189 |
+
nv_bfloat16 a_bf16[kNumRegPerWgmma];
|
| 190 |
+
a_bf16[0] = a_bf16_smem_ptr_upper[elem_offset];
|
| 191 |
+
a_bf16[2] = a_bf16_smem_ptr_upper[elem_offset + 4];
|
| 192 |
+
a_bf16[1] = a_bf16_smem_ptr_lower[elem_offset];
|
| 193 |
+
a_bf16[3] = a_bf16_smem_ptr_lower[elem_offset + 4];
|
| 194 |
+
|
| 195 |
+
auto a_bf16x2_ptr = reinterpret_cast<nv_bfloat162*>(a_bf16);
|
| 196 |
+
auto a_float2_ptr = reinterpret_cast<float2*>(a);
|
| 197 |
+
float2 a_float2_0 = __bfloat1622float2(a_bf16x2_ptr[0]);
|
| 198 |
+
float2 a_float2_1 = __bfloat1622float2(a_bf16x2_ptr[1]);
|
| 199 |
+
a_float2_ptr[i * 2 + 0] = a_float2_0;
|
| 200 |
+
a_float2_ptr[i * 2 + 1] = a_float2_1;
|
| 201 |
+
sqr_sum_acc_0 += a_float2_0.x * a_float2_0.x + a_float2_1.x * a_float2_1.x;
|
| 202 |
+
sqr_sum_acc_1 += a_float2_0.y * a_float2_0.y + a_float2_1.y * a_float2_1.y;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
ptx::warpgroup_wait<0>();
|
| 206 |
+
if (s > 0)
|
| 207 |
+
empty_barriers[(s - 1) % kNumStages]->arrive();
|
| 208 |
+
|
| 209 |
+
#pragma unroll
|
| 210 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 211 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 212 |
+
ptx::warpgroup_arrive();
|
| 213 |
+
|
| 214 |
+
constexpr int kNumElemsInSwizzleRange = 128 / sizeof(float);
|
| 215 |
+
constexpr uint32_t kNumWgmmaInSwizzleRange = kNumElemsInSwizzleRange / WGMMA::K;
|
| 216 |
+
DG_STATIC_ASSERT(BLOCK_K % kNumElemsInSwizzleRange == 0, "Invalid block K");
|
| 217 |
+
|
| 218 |
+
#pragma unroll
|
| 219 |
+
for (int i = 0; i < BLOCK_K / kNumElemsInSwizzleRange; i++) {
|
| 220 |
+
#pragma unroll
|
| 221 |
+
for (int k = 0; k < kNumElemsInSwizzleRange / WGMMA::K; k++) {
|
| 222 |
+
auto b_desc = mma::sm90::make_smem_desc(
|
| 223 |
+
smem_b[stage_idx] + i * BLOCK_N * kNumElemsInSwizzleRange + k * WGMMA::K, 1);
|
| 224 |
+
WGMMA::wgmma(a + (i * kNumWgmmaInSwizzleRange + k) * kNumRegPerWgmma, b_desc, accum, 1);
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
ptx::warpgroup_commit_batch();
|
| 228 |
+
#pragma unroll
|
| 229 |
+
for (uint32_t i = 0; i < WGMMA::kNumAccum; ++ i)
|
| 230 |
+
ptx::warpgroup_fence_operand(accum[i]);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
const auto& reduced_sum_0 = math::warp_reduce_sum<4>(sqr_sum_acc_0);
|
| 234 |
+
const auto& reduced_sum_1 = math::warp_reduce_sum<4>(sqr_sum_acc_1);
|
| 235 |
+
|
| 236 |
+
const auto& m_idx = m_block_idx * BLOCK_M + (warp_idx * BLOCK_M_PER_WARP + lane_idx / 4);
|
| 237 |
+
if (lane_idx % 4 == 0) {
|
| 238 |
+
if (m_idx < shape_m)
|
| 239 |
+
sqr_sum[m_offset + m_idx] = reduced_sum_0;
|
| 240 |
+
if (m_idx + 8 < shape_m)
|
| 241 |
+
sqr_sum[m_offset + m_idx + 8] = reduced_sum_1;
|
| 242 |
+
}
|
| 243 |
+
ptx::warpgroup_wait<0>();
|
| 244 |
+
empty_barriers[(num_total_stages-1) % kNumStages]->arrive();
|
| 245 |
+
|
| 246 |
+
// Write accum to shared memory
|
| 247 |
+
// Every 2 threads (one pair) will write to the same bank group (16 bytes).
|
| 248 |
+
// Refer to the D layout in https://docs.nvidia.com/cuda/parallel-thread-execution/#wgmma-64n8-d
|
| 249 |
+
uint32_t is_odd_pair = lane_idx / 2 % 2;
|
| 250 |
+
|
| 251 |
+
// Four threads per group; write the data to the same row.
|
| 252 |
+
uint32_t row_idx = lane_idx / 4;
|
| 253 |
+
|
| 254 |
+
// Even/odd index pairs write to the same column, we need to reorder idx:
|
| 255 |
+
// group even pair indices consecutively, and likewise for odd ones.
|
| 256 |
+
uint32_t reordered_pair_idx = is_odd_pair * 8 + row_idx;
|
| 257 |
+
|
| 258 |
+
auto shifted_smem_ptr = reinterpret_cast<uint8_t*>(smem_cd) +
|
| 259 |
+
(warp_idx * BLOCK_M_PER_WARP + row_idx) * kSwizzleCDMode + // Row offset, each warp has 16 rows
|
| 260 |
+
lane_idx % 2 * 8; // One thread of a pair writes 8 bytes
|
| 261 |
+
|
| 262 |
+
#pragma unroll
|
| 263 |
+
for (uint32_t i = 0; i < (kSwizzleCDMode / sizeof(float)) / 4; i += 2) {
|
| 264 |
+
// Get the swizzled bank group index (16 bytes per group)
|
| 265 |
+
uint32_t bank_group_idx = get_swizzled_bank_group_idx<kSwizzleCDMode>(i + is_odd_pair, reordered_pair_idx);
|
| 266 |
+
auto smem_ptr = shifted_smem_ptr + bank_group_idx * kNumBankGroupBytes; // Col offset, 16 bytes per group
|
| 267 |
+
|
| 268 |
+
// 0/1 write to the same row, 2/3 write to another row
|
| 269 |
+
auto values = reinterpret_cast<uint32_t*>(accum + i * 2);
|
| 270 |
+
ptx::st_shared(smem_ptr, values[0], values[1]);
|
| 271 |
+
ptx::st_shared(smem_ptr + 8 * kSwizzleCDMode, values[2], values[3]);
|
| 272 |
+
}
|
| 273 |
+
cute::tma_store_fence();
|
| 274 |
+
cutlass::arch::NamedBarrier::sync(128, 1);
|
| 275 |
+
|
| 276 |
+
// Issue TMA stores
|
| 277 |
+
if (warp_idx == 0 and cute::elect_one_sync()) {
|
| 278 |
+
if constexpr (kNumSplits == 1) {
|
| 279 |
+
cute::SM90_TMA_STORE_2D::copy(&tensor_map_d, smem_cd, 0, m_block_idx * BLOCK_M);
|
| 280 |
+
} else {
|
| 281 |
+
cute::SM90_TMA_STORE_3D::copy(&tensor_map_d, smem_cd, 0, m_block_idx * BLOCK_M, k_split_idx);
|
| 282 |
+
}
|
| 283 |
+
cute::tma_store_arrive();
|
| 284 |
+
}
|
| 285 |
+
}
|
| 286 |
+
#else
|
| 287 |
+
if (blockIdx.x == 0 and threadIdx.x == 0)
|
| 288 |
+
DG_DEVICE_ASSERT(false and "This kernel only support sm_90a");
|
| 289 |
+
#endif
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
} // namespace deep_gemm
|
| 293 |
+
|
| 294 |
+
#pragma clang diagnostic pop
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/smxx_clean_logits.cuh
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 5 |
+
|
| 6 |
+
#include <deep_gemm/common/cute_tie.cuh>
|
| 7 |
+
#include <deep_gemm/common/math.cuh>
|
| 8 |
+
|
| 9 |
+
namespace deep_gemm {
|
| 10 |
+
|
| 11 |
+
template <uint32_t kNextN, uint32_t BLOCK_KV, uint32_t kNumWarps, typename logits_dtype_t>
|
| 12 |
+
CUTLASS_GLOBAL __launch_bounds__(kNumWarps * 32, 1)
|
| 13 |
+
void smxx_clean_logits(const uint32_t seq_len, const uint32_t seq_len_kv, const uint64_t stride_logits,
|
| 14 |
+
const uint32_t* cu_seq_len_k_start, const uint32_t* cu_seq_len_k_end, logits_dtype_t* logits) {
|
| 15 |
+
const uint32_t num_sms = gridDim.x;
|
| 16 |
+
const uint32_t sm_idx = blockIdx.x;
|
| 17 |
+
const uint32_t warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
| 18 |
+
|
| 19 |
+
constexpr uint32_t kAlignment = 16 / sizeof(logits_dtype_t);
|
| 20 |
+
const logits_dtype_t neg_inf = -cute::numeric_limits<logits_dtype_t>::infinity();
|
| 21 |
+
|
| 22 |
+
// Allocate filled `-inf` shared memory
|
| 23 |
+
extern __shared__ __align__(1024) logits_dtype_t smem_buffer[];
|
| 24 |
+
#pragma unroll
|
| 25 |
+
for (uint32_t i = threadIdx.x; i < BLOCK_KV; i += kNumWarps * 32)
|
| 26 |
+
smem_buffer[i] = neg_inf;
|
| 27 |
+
cute::tma_store_fence();
|
| 28 |
+
__syncthreads();
|
| 29 |
+
|
| 30 |
+
// Assign sequence to each warp
|
| 31 |
+
const auto assign_task = [&](const uint32_t& num, const uint32_t& idx,
|
| 32 |
+
const uint32_t& start, const uint32_t& total) -> cute::tuple<uint32_t, uint32_t> {
|
| 33 |
+
const auto per = total / num, rem = total % num;
|
| 34 |
+
return {start + idx * per + cute::min(idx, rem), per + (idx < rem)};
|
| 35 |
+
};
|
| 36 |
+
CUTE_TIE_DECL(assign_task(num_sms, sm_idx, 0, seq_len), sm_seq_start, sm_seq_len);
|
| 37 |
+
CUTE_TIE_DECL(assign_task(kNumWarps, warp_idx, sm_seq_start, sm_seq_len), warp_seq_start, warp_seq_len);
|
| 38 |
+
|
| 39 |
+
// Wait for primary kernel completion
|
| 40 |
+
cudaGridDependencySynchronize();
|
| 41 |
+
|
| 42 |
+
if (cute::elect_one_sync()) {
|
| 43 |
+
for (uint32_t i = warp_seq_start; i < warp_seq_start + warp_seq_len; ++ i) {
|
| 44 |
+
const auto ks = cu_seq_len_k_start == nullptr ? 0 : cu_seq_len_k_start[i / kNextN];
|
| 45 |
+
const auto ke = cu_seq_len_k_end[i / kNextN] - kNextN + i % kNextN + 1;
|
| 46 |
+
const auto aligned_ks = ks / kAlignment * kAlignment, aligned_ke = (ke + kAlignment - 1) / kAlignment * kAlignment;
|
| 47 |
+
|
| 48 |
+
for (uint32_t left = 0; left < seq_len_kv; left += BLOCK_KV) {
|
| 49 |
+
const auto right = cute::min(left + BLOCK_KV, static_cast<uint32_t>(stride_logits));
|
| 50 |
+
if (right <= ks or ke <= left) {
|
| 51 |
+
cute::SM90_BULK_COPY_S2G::copy(smem_buffer, logits + i * stride_logits + left, (right - left) * sizeof(logits_dtype_t));
|
| 52 |
+
} else {
|
| 53 |
+
if (left < aligned_ks)
|
| 54 |
+
cute::SM90_BULK_COPY_S2G::copy(smem_buffer, logits + i * stride_logits + left, (aligned_ks - left) * sizeof(logits_dtype_t));
|
| 55 |
+
if (aligned_ke < right)
|
| 56 |
+
cute::SM90_BULK_COPY_S2G::copy(smem_buffer, logits + i * stride_logits + aligned_ke, (right - aligned_ke) * sizeof(logits_dtype_t));
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
__syncwarp();
|
| 62 |
+
|
| 63 |
+
for (uint32_t i = warp_seq_start; i < warp_seq_start + warp_seq_len; ++ i) {
|
| 64 |
+
const auto ks = cu_seq_len_k_start == nullptr ? 0 : cu_seq_len_k_start[i / kNextN];
|
| 65 |
+
const auto ke = cu_seq_len_k_end[i / kNextN] - kNextN + i % kNextN + 1;
|
| 66 |
+
const auto aligned_ks = ks / kAlignment * kAlignment, aligned_ke = (ke + kAlignment - 1) / kAlignment * kAlignment;
|
| 67 |
+
for (uint32_t j = aligned_ks; j < ks; ++ j)
|
| 68 |
+
logits[i * stride_logits + j] = neg_inf;
|
| 69 |
+
for (uint32_t j = ke; j < aligned_ke; ++ j)
|
| 70 |
+
logits[i * stride_logits + j] = neg_inf;
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
}
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/impls/smxx_layout.cuh
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 7 |
+
|
| 8 |
+
namespace deep_gemm {
|
| 9 |
+
|
| 10 |
+
template <uint32_t kNumThreads, uint32_t BLOCK_MN, uint32_t SF_K,
|
| 11 |
+
uint32_t PADDED_SF_K = SF_K + (1 - (SF_K % 2))>
|
| 12 |
+
CUTLASS_GLOBAL void transpose_fp32(const float* sf, float* out, const uint32_t mn) {
|
| 13 |
+
typedef typename utils::Vectorized<sizeof(float) * SF_K>::vec_t in_vec_t;
|
| 14 |
+
constexpr static uint32_t kNumElemsPerVec = sizeof(in_vec_t) / sizeof(float);
|
| 15 |
+
constexpr static uint32_t SF_VEC_K = SF_K / kNumElemsPerVec;
|
| 16 |
+
|
| 17 |
+
// Shapes and strides
|
| 18 |
+
extern __shared__ float smem_buffer[];
|
| 19 |
+
constexpr auto kNumTMAAlignedElems = static_cast<uint32_t>(16 / sizeof(float));
|
| 20 |
+
const auto in_block_mn = min(BLOCK_MN, mn - blockIdx.x * BLOCK_MN);
|
| 21 |
+
const auto tma_aligned_mn = math::align<uint32_t>(mn, kNumTMAAlignedElems);
|
| 22 |
+
|
| 23 |
+
// Shift into the block
|
| 24 |
+
sf = sf + static_cast<uint64_t>(blockIdx.y) * mn * SF_K;
|
| 25 |
+
out = out + static_cast<uint64_t>(blockIdx.y) * tma_aligned_mn * SF_K;
|
| 26 |
+
const auto& local_sf = reinterpret_cast<const in_vec_t*>(sf + static_cast<uint64_t>(blockIdx.x) * (BLOCK_MN * SF_K));
|
| 27 |
+
|
| 28 |
+
// Wait for primary kernel completion
|
| 29 |
+
cudaGridDependencySynchronize();
|
| 30 |
+
|
| 31 |
+
// Load
|
| 32 |
+
for (uint32_t i = threadIdx.x; i < in_block_mn * SF_VEC_K; i += kNumThreads) {
|
| 33 |
+
auto in_vec = local_sf[i];
|
| 34 |
+
const auto& in_values = reinterpret_cast<float*>(&in_vec);
|
| 35 |
+
|
| 36 |
+
const auto& row = i / SF_VEC_K, col = (i % SF_VEC_K) * kNumElemsPerVec;
|
| 37 |
+
#pragma unroll
|
| 38 |
+
for (uint32_t j = 0; j < kNumElemsPerVec; ++ j)
|
| 39 |
+
smem_buffer[row * PADDED_SF_K + col + j] = in_values[j];
|
| 40 |
+
}
|
| 41 |
+
__syncthreads();
|
| 42 |
+
|
| 43 |
+
// Store
|
| 44 |
+
#pragma unroll
|
| 45 |
+
for (uint32_t i = threadIdx.x; i < in_block_mn * SF_K; i += kNumThreads) {
|
| 46 |
+
const auto& sf_k_idx = i / in_block_mn, mn_idx = i % in_block_mn;
|
| 47 |
+
const auto& global_mn_idx = blockIdx.x * BLOCK_MN + mn_idx;
|
| 48 |
+
out[sf_k_idx * tma_aligned_mn + global_mn_idx] = ptx::ld_shared(smem_buffer + mn_idx * PADDED_SF_K + sf_k_idx);
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// NOTES: the two kernels below always pack the K dimension
|
| 53 |
+
|
| 54 |
+
template <uint32_t kNumThreads, uint32_t BLOCK_MN, uint32_t SF_K>
|
| 55 |
+
CUTLASS_GLOBAL void transpose_and_pack_fp32_into_ue8m0(float* sf, uint32_t* out, const uint32_t mn) {
|
| 56 |
+
extern __shared__ uint32_t smem_buffer[];
|
| 57 |
+
|
| 58 |
+
// Shapes and strides
|
| 59 |
+
constexpr auto kNumPackedSFK = math::constexpr_ceil_div(SF_K, 4u);
|
| 60 |
+
constexpr auto kNumTMAAlignedElems = static_cast<uint32_t>(16 / sizeof(int));
|
| 61 |
+
const auto in_block_mn = min(BLOCK_MN, mn - blockIdx.x * BLOCK_MN);
|
| 62 |
+
const auto tma_aligned_mn = math::align<uint64_t>(mn, kNumTMAAlignedElems);
|
| 63 |
+
|
| 64 |
+
// Shift into the group
|
| 65 |
+
sf = sf + static_cast<uint64_t>(blockIdx.y) * mn * SF_K;
|
| 66 |
+
out = out + static_cast<uint64_t>(blockIdx.y) * tma_aligned_mn * kNumPackedSFK;
|
| 67 |
+
|
| 68 |
+
// Wait for primary kernel completion
|
| 69 |
+
cudaGridDependencySynchronize();
|
| 70 |
+
|
| 71 |
+
// Load FP32 SFs
|
| 72 |
+
DG_STATIC_ASSERT(BLOCK_MN % 4 == 0, "Invalid block size");
|
| 73 |
+
const auto local_sf = reinterpret_cast<uint32_t*>(sf + static_cast<uint64_t>(blockIdx.x) * (BLOCK_MN * SF_K));
|
| 74 |
+
const auto num_values = in_block_mn * SF_K;
|
| 75 |
+
const auto num_uint4 = num_values / 4;
|
| 76 |
+
#pragma unroll
|
| 77 |
+
for (uint32_t i = threadIdx.x; i < num_uint4; i += kNumThreads) {
|
| 78 |
+
const auto& [x, y, z, w] = reinterpret_cast<const uint4*>(local_sf)[i];
|
| 79 |
+
ptx::st_shared(reinterpret_cast<uint4*>(smem_buffer) + i, x, y, z, w);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Fill unaligned values as well
|
| 83 |
+
if (const auto unaligned_idx = num_uint4 * 4 + threadIdx.x; unaligned_idx < num_values)
|
| 84 |
+
ptx::st_shared(smem_buffer + unaligned_idx, local_sf[unaligned_idx]);
|
| 85 |
+
__syncthreads();
|
| 86 |
+
|
| 87 |
+
// Pack into UE8M0 and store
|
| 88 |
+
#pragma unroll
|
| 89 |
+
for (uint32_t i = threadIdx.x; i < (kNumPackedSFK * BLOCK_MN); i += kNumThreads) {
|
| 90 |
+
const auto sf_k_pack_idx = i / BLOCK_MN, mn_idx = i % BLOCK_MN;
|
| 91 |
+
|
| 92 |
+
// Load shared memory
|
| 93 |
+
uint32_t values[4];
|
| 94 |
+
#pragma unroll
|
| 95 |
+
for (uint32_t j = 0; j < 4; ++ j) {
|
| 96 |
+
const auto sf_k_idx = sf_k_pack_idx * 4 + j;
|
| 97 |
+
values[j] = sf_k_idx < SF_K ? ptx::ld_shared(smem_buffer + mn_idx * SF_K + sf_k_idx) : 0;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Pack and store
|
| 101 |
+
uint32_t packed = 0;
|
| 102 |
+
packed |= (values[0] >> 23u);
|
| 103 |
+
packed |= (values[1] >> 15u);
|
| 104 |
+
packed |= (values[2] >> 7u);
|
| 105 |
+
packed |= (values[3] << 1u);
|
| 106 |
+
if (const auto global_mn_idx = blockIdx.x * BLOCK_MN + mn_idx; global_mn_idx < mn)
|
| 107 |
+
out[sf_k_pack_idx * tma_aligned_mn + global_mn_idx] = packed;
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
template <uint32_t kNumGroups, uint32_t kNumThreads,
|
| 112 |
+
uint32_t BLOCK_MN, uint32_t BLOCK_PACKED_SF_K, bool kTransposed = true>
|
| 113 |
+
CUTLASS_GLOBAL void pack_fp32_into_ue8m0(float* sf, uint32_t* out, uint32_t* ks,
|
| 114 |
+
const uint32_t mn, uint32_t sf_k, const uint32_t packed_sf_k,
|
| 115 |
+
const uint32_t gran_k) {
|
| 116 |
+
// Always packing the K dimension
|
| 117 |
+
// NOTES: should also assert `mn % 4 == 0` at launch
|
| 118 |
+
DG_STATIC_ASSERT(kTransposed, "Currently only support transposed SFs (MN-major)");
|
| 119 |
+
DG_STATIC_ASSERT(BLOCK_MN % 4 == 0, "Invalid block sizes");
|
| 120 |
+
DG_STATIC_ASSERT(BLOCK_PACKED_SF_K == kNumThreads / 32, "Invalid block sizes");
|
| 121 |
+
|
| 122 |
+
// Shapes and strides
|
| 123 |
+
const auto in_block_mn = min(BLOCK_MN, mn - blockIdx.x * BLOCK_MN);
|
| 124 |
+
const auto in_block_mn_uint4 = in_block_mn / 4;
|
| 125 |
+
const auto in_block_packed_sf_k = min(BLOCK_PACKED_SF_K, packed_sf_k - blockIdx.y * BLOCK_PACKED_SF_K);
|
| 126 |
+
|
| 127 |
+
// Shift into the right block along MN
|
| 128 |
+
sf += blockIdx.x * BLOCK_MN;
|
| 129 |
+
out += blockIdx.x * BLOCK_MN;
|
| 130 |
+
|
| 131 |
+
// Each warp is responsible for a packed row
|
| 132 |
+
const auto warp_idx = threadIdx.x / 32;
|
| 133 |
+
const auto lane_idx = ptx::get_lane_idx();
|
| 134 |
+
const auto packed_sf_k_idx = static_cast<uint64_t>(blockIdx.y) * BLOCK_PACKED_SF_K + warp_idx;
|
| 135 |
+
if (warp_idx >= in_block_packed_sf_k)
|
| 136 |
+
return;
|
| 137 |
+
|
| 138 |
+
// Wait for primary kernel completion
|
| 139 |
+
cudaGridDependencySynchronize();
|
| 140 |
+
|
| 141 |
+
// Make an offset on the input
|
| 142 |
+
uint32_t input_offset = 0;
|
| 143 |
+
if constexpr (kNumGroups > 1) {
|
| 144 |
+
// Load each group's size
|
| 145 |
+
DG_STATIC_ASSERT(kNumGroups <= 128, "Too many groups");
|
| 146 |
+
uint32_t group_ks[4];
|
| 147 |
+
#pragma unroll
|
| 148 |
+
for (uint32_t i = 0; i < 4; ++ i) {
|
| 149 |
+
const auto group_idx = lane_idx * 4 + i;
|
| 150 |
+
group_ks[i] = group_idx < kNumGroups ? ks[group_idx] : 0;
|
| 151 |
+
}
|
| 152 |
+
__syncwarp();
|
| 153 |
+
|
| 154 |
+
// Make the offset
|
| 155 |
+
sf_k = 0;
|
| 156 |
+
uint32_t sum_packed_sf_k = 0;
|
| 157 |
+
#pragma unroll
|
| 158 |
+
for (uint32_t i = 0; i < kNumGroups; ++ i) {
|
| 159 |
+
const auto sf_k_in_group = __shfl_sync(0xffffffff, group_ks[i % 4] / gran_k, i / 4);
|
| 160 |
+
sf_k += sf_k_in_group;
|
| 161 |
+
sum_packed_sf_k += math::ceil_div(sf_k_in_group, 4u);
|
| 162 |
+
if (packed_sf_k_idx < sum_packed_sf_k)
|
| 163 |
+
break;
|
| 164 |
+
if (const auto remainder = sf_k_in_group % 4; remainder > 0)
|
| 165 |
+
input_offset += 4 - remainder;
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
for (uint32_t mn_idx = ptx::get_lane_idx(); mn_idx < in_block_mn_uint4; mn_idx += 32) {
|
| 170 |
+
// Load
|
| 171 |
+
uint4 values[4];
|
| 172 |
+
#pragma unroll
|
| 173 |
+
for (uint32_t j = 0; j < 4; ++ j) {
|
| 174 |
+
values[j] = make_uint4(0, 0, 0, 0);
|
| 175 |
+
if (const auto sf_k_idx = packed_sf_k_idx * 4 + j - input_offset; sf_k_idx < sf_k)
|
| 176 |
+
values[j] = reinterpret_cast<const uint4*>(sf + sf_k_idx * mn)[mn_idx];
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Pack and store
|
| 180 |
+
uint4 packed;
|
| 181 |
+
packed.x = (values[0].x >> 23u) | (values[1].x >> 15u) | (values[2].x >> 7u) | (values[3].x << 1u);
|
| 182 |
+
packed.y = (values[0].y >> 23u) | (values[1].y >> 15u) | (values[2].y >> 7u) | (values[3].y << 1u);
|
| 183 |
+
packed.z = (values[0].z >> 23u) | (values[1].z >> 15u) | (values[2].z >> 7u) | (values[3].z << 1u);
|
| 184 |
+
packed.w = (values[0].w >> 23u) | (values[1].w >> 15u) | (values[2].w >> 7u) | (values[3].w << 1u);
|
| 185 |
+
reinterpret_cast<uint4*>(out + packed_sf_k_idx * mn)[mn_idx] = packed;
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
} // namespace deep_gemm
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/layout/mega_moe.cuh
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/numeric/math.hpp>
|
| 4 |
+
|
| 5 |
+
#include <deep_gemm/common/math.cuh>
|
| 6 |
+
#include <deep_gemm/common/exception.cuh>
|
| 7 |
+
|
| 8 |
+
namespace deep_gemm::layout {
|
| 9 |
+
|
| 10 |
+
static constexpr int kNumCandidateBlockMs = 7;
|
| 11 |
+
static constexpr int kCandidateBlockM[kNumCandidateBlockMs] = {8, 16, 32, 64, 96, 128, 192};
|
| 12 |
+
static constexpr int kMaxCandidateBlockM = 192;
|
| 13 |
+
static constexpr int kMinCandidateBlockM = 8;
|
| 14 |
+
static constexpr int kLCMCandidateBlockM = 384;
|
| 15 |
+
|
| 16 |
+
// Pool capacity for shared expert token pool: worst-case total tokens + per-expert BLOCK_M alignment padding, among all possible BLOCK_M
|
| 17 |
+
template <typename T>
|
| 18 |
+
CUTLASS_HOST_DEVICE constexpr T get_num_max_pool_tokens(T num_ranks, T num_max_tokens_per_rank, T num_topk,
|
| 19 |
+
T num_experts_per_rank) {
|
| 20 |
+
const auto num_max_recv_tokens = num_ranks * num_max_tokens_per_rank;
|
| 21 |
+
const auto num_max_experts_per_token = math::constexpr_min(num_topk, num_experts_per_rank);
|
| 22 |
+
return math::constexpr_align(
|
| 23 |
+
num_max_recv_tokens * num_max_experts_per_token + num_experts_per_rank * (static_cast<T>(kMaxCandidateBlockM) - 1),
|
| 24 |
+
static_cast<T>(kLCMCandidateBlockM));
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// SF pool capacity: all experts share a contiguous SF region, sized by pool blocks × SF_BLOCK_M
|
| 28 |
+
template <typename T>
|
| 29 |
+
CUTLASS_HOST_DEVICE constexpr T get_num_padded_sf_pool_tokens(T num_max_pool_tokens, T block_m) {
|
| 30 |
+
return (num_max_pool_tokens / block_m) * math::constexpr_align(block_m, static_cast<T>(128));
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// Per-token source metadata for combine write-back
|
| 34 |
+
struct TokenSrcMetadata {
|
| 35 |
+
uint32_t rank_idx;
|
| 36 |
+
uint32_t token_idx;
|
| 37 |
+
uint32_t topk_idx;
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
struct Workspace {
|
| 41 |
+
void* base;
|
| 42 |
+
uint32_t num_ranks, num_experts;
|
| 43 |
+
uint32_t num_experts_per_rank;
|
| 44 |
+
uint32_t num_max_tokens_per_rank;
|
| 45 |
+
uint32_t num_max_recv_tokens_per_expert;
|
| 46 |
+
|
| 47 |
+
// Pool capacity: all local experts share a contiguous token pool
|
| 48 |
+
uint32_t num_max_pool_tokens;
|
| 49 |
+
uint32_t num_max_pool_blocks;
|
| 50 |
+
|
| 51 |
+
// For both grid barrier and NVLink barrier
|
| 52 |
+
static constexpr uint64_t kNumBarrierSignalBytes = 32;
|
| 53 |
+
|
| 54 |
+
CUTLASS_HOST_DEVICE
|
| 55 |
+
Workspace(void* base,
|
| 56 |
+
const uint32_t& num_ranks,
|
| 57 |
+
const uint32_t& num_experts,
|
| 58 |
+
const uint32_t& num_max_tokens_per_rank,
|
| 59 |
+
const uint32_t& num_topk):
|
| 60 |
+
base(base),
|
| 61 |
+
num_ranks(num_ranks), num_experts(num_experts),
|
| 62 |
+
num_max_tokens_per_rank(num_max_tokens_per_rank) {
|
| 63 |
+
num_experts_per_rank = num_experts / num_ranks;
|
| 64 |
+
num_max_recv_tokens_per_expert = num_ranks * num_max_tokens_per_rank;
|
| 65 |
+
num_max_pool_tokens = get_num_max_pool_tokens(num_ranks, num_max_tokens_per_rank, num_topk, num_experts_per_rank);
|
| 66 |
+
num_max_pool_blocks = num_max_pool_tokens / kMinCandidateBlockM;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
CUTLASS_HOST_DEVICE
|
| 70 |
+
uint64_t get_num_bytes() const {
|
| 71 |
+
uint64_t num_bytes = 0;
|
| 72 |
+
|
| 73 |
+
// Barrier
|
| 74 |
+
num_bytes += kNumBarrierSignalBytes;
|
| 75 |
+
|
| 76 |
+
// Expert send/recv count
|
| 77 |
+
num_bytes += num_experts * sizeof(uint64_t) * 2;
|
| 78 |
+
|
| 79 |
+
// Expert recv count sum
|
| 80 |
+
num_bytes += num_experts_per_rank * sizeof(uint64_t);
|
| 81 |
+
|
| 82 |
+
// L1 arrival count (padded to even entry count for `uint64_t` alignment of L2 mask)
|
| 83 |
+
num_bytes += math::align(num_max_pool_blocks, 2u) * sizeof(uint32_t);
|
| 84 |
+
|
| 85 |
+
// L2 block arrival mask
|
| 86 |
+
num_bytes += num_max_pool_blocks * sizeof(uint64_t);
|
| 87 |
+
|
| 88 |
+
// Dispatch pulling source token-topk
|
| 89 |
+
num_bytes += num_experts_per_rank * num_ranks * num_max_recv_tokens_per_expert * sizeof(int);
|
| 90 |
+
|
| 91 |
+
// Combine push source indices
|
| 92 |
+
num_bytes += num_max_pool_tokens * sizeof(TokenSrcMetadata);
|
| 93 |
+
|
| 94 |
+
// Align to TMA descriptor requirements
|
| 95 |
+
num_bytes = math::align<uint64_t>(num_bytes, 16);
|
| 96 |
+
return num_bytes;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
CUTLASS_HOST_DEVICE
|
| 100 |
+
void* get_end_ptr() const {
|
| 101 |
+
return math::advance_ptr(base, get_num_bytes());
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Grid sync counters: `kNumBarrierSignalBytes` layout
|
| 105 |
+
// [ 0..15]: 4 x `uint32_t` grid sync counters
|
| 106 |
+
// [16..20]: `uint32_t` NVLink barrier counter
|
| 107 |
+
// [20..27]: 2 x `int` NVLink barrier signals (phase 0 and 1)
|
| 108 |
+
static constexpr uint32_t kNumMaxGridSyncCounters = 4;
|
| 109 |
+
|
| 110 |
+
template <uint32_t kIndex = 0>
|
| 111 |
+
CUTLASS_DEVICE
|
| 112 |
+
uint32_t* get_grid_sync_count_ptr() const {
|
| 113 |
+
DG_STATIC_ASSERT(kIndex < kNumMaxGridSyncCounters, "Grid sync index out of bounds");
|
| 114 |
+
return static_cast<uint32_t*>(base) + kIndex;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
CUTLASS_DEVICE
|
| 118 |
+
uint32_t* get_nvl_barrier_counter_ptr() const {
|
| 119 |
+
return static_cast<uint32_t*>(base) + kNumMaxGridSyncCounters;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
CUTLASS_DEVICE
|
| 123 |
+
int* get_nvl_barrier_signal_ptr(const uint32_t& phase) const {
|
| 124 |
+
// NOTES: the signal is signed, as we may minus
|
| 125 |
+
return math::advance_ptr<int>(base, (kNumMaxGridSyncCounters + 1) * sizeof(uint32_t) + phase * sizeof(int));
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
CUTLASS_DEVICE
|
| 129 |
+
uint64_t* get_expert_send_count_ptr(const uint32_t& expert_idx = 0) const {
|
| 130 |
+
return math::advance_ptr<uint64_t>(base, kNumBarrierSignalBytes) + expert_idx;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
CUTLASS_DEVICE
|
| 134 |
+
uint64_t* get_expert_recv_count_ptr(
|
| 135 |
+
const uint32_t& rank_idx = 0, const uint32_t& expert_idx = 0) const {
|
| 136 |
+
return get_expert_send_count_ptr(num_experts) + rank_idx * num_experts_per_rank + expert_idx;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
CUTLASS_DEVICE
|
| 140 |
+
uint64_t* get_expert_recv_count_sum_ptr(const uint32_t& expert_idx = 0) const {
|
| 141 |
+
return get_expert_send_count_ptr(num_experts * 2) + expert_idx;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
CUTLASS_DEVICE
|
| 145 |
+
uint32_t* get_l1_arrival_count_ptr(const uint32_t& pool_block_idx = 0) const {
|
| 146 |
+
const auto base = get_expert_recv_count_sum_ptr(num_experts_per_rank);
|
| 147 |
+
return reinterpret_cast<uint32_t*>(base) + pool_block_idx;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
CUTLASS_DEVICE
|
| 151 |
+
uint64_t* get_l2_arrival_mask_ptr(const uint32_t& pool_block_idx = 0) const {
|
| 152 |
+
// Pad L1 entry count to even so that the `l2_arrival_mask` is 8-byte aligned
|
| 153 |
+
const auto base = get_l1_arrival_count_ptr(math::align(num_max_pool_blocks, 2u));
|
| 154 |
+
return reinterpret_cast<uint64_t*>(base) + pool_block_idx;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// For dispatch pulling
|
| 158 |
+
CUTLASS_DEVICE
|
| 159 |
+
uint32_t* get_src_token_topk_idx_ptr(
|
| 160 |
+
const uint32_t& expert_idx = 0, const uint32_t& rank_idx = 0, const uint32_t& token_idx = 0) const {
|
| 161 |
+
const auto base = get_l2_arrival_mask_ptr(num_max_pool_blocks);
|
| 162 |
+
return reinterpret_cast<uint32_t*>(base) +
|
| 163 |
+
expert_idx * (num_ranks * num_max_recv_tokens_per_expert) +
|
| 164 |
+
rank_idx * num_max_recv_tokens_per_expert + token_idx;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
// For combine usages
|
| 168 |
+
CUTLASS_DEVICE
|
| 169 |
+
TokenSrcMetadata* get_token_src_metadata_ptr(const uint32_t& pool_token_idx = 0) const {
|
| 170 |
+
const auto base = reinterpret_cast<TokenSrcMetadata*>(get_src_token_topk_idx_ptr(num_experts_per_rank));
|
| 171 |
+
return base + pool_token_idx;
|
| 172 |
+
}
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
struct Data {
|
| 176 |
+
uint32_t num_bytes;
|
| 177 |
+
bool require_tma_alignment;
|
| 178 |
+
void* base;
|
| 179 |
+
|
| 180 |
+
CUTLASS_HOST_DEVICE
|
| 181 |
+
constexpr explicit Data(
|
| 182 |
+
const uint32_t& num_bytes,
|
| 183 |
+
const bool& require_tma_alignment = true,
|
| 184 |
+
void* base = nullptr) :
|
| 185 |
+
num_bytes(num_bytes), require_tma_alignment(require_tma_alignment), base(base) {
|
| 186 |
+
DG_UNIFIED_ASSERT(num_bytes % 16 == 0 or not require_tma_alignment);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
template <typename dtype_t = uint32_t>
|
| 190 |
+
CUTLASS_HOST_DEVICE constexpr dtype_t get_num_bytes() const {
|
| 191 |
+
return static_cast<dtype_t>(num_bytes);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
template <typename dtype_t = void>
|
| 195 |
+
CUTLASS_HOST_DEVICE dtype_t* get_base_ptr() const {
|
| 196 |
+
return static_cast<dtype_t*>(base);
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
CUTLASS_HOST_DEVICE void set_base_ptr(void* ptr) {
|
| 200 |
+
base = ptr;
|
| 201 |
+
}
|
| 202 |
+
};
|
| 203 |
+
|
| 204 |
+
struct Buffer {
|
| 205 |
+
Data data_layout;
|
| 206 |
+
uint32_t num_ranks;
|
| 207 |
+
uint32_t num_max_tokens_per_rank;
|
| 208 |
+
|
| 209 |
+
void* base;
|
| 210 |
+
|
| 211 |
+
CUTLASS_HOST_DEVICE
|
| 212 |
+
Buffer(const Data& data_layout,
|
| 213 |
+
const uint32_t& num_ranks,
|
| 214 |
+
const uint32_t& max_num_tokens_per_rank,
|
| 215 |
+
void* base = nullptr) :
|
| 216 |
+
data_layout(data_layout),
|
| 217 |
+
num_ranks(num_ranks), num_max_tokens_per_rank(max_num_tokens_per_rank),
|
| 218 |
+
base(base) {}
|
| 219 |
+
|
| 220 |
+
CUTLASS_HOST_DEVICE
|
| 221 |
+
uint64_t get_num_bytes_per_rank() const {
|
| 222 |
+
return num_max_tokens_per_rank * data_layout.get_num_bytes<uint64_t>();
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
CUTLASS_HOST_DEVICE
|
| 226 |
+
uint64_t get_num_bytes() const {
|
| 227 |
+
return get_num_bytes_per_rank() * num_ranks;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
template <typename dtype_t = void>
|
| 231 |
+
CUTLASS_HOST_DEVICE dtype_t* get_base_ptr() const {
|
| 232 |
+
return static_cast<dtype_t*>(base);
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
CUTLASS_HOST_DEVICE
|
| 236 |
+
void* get_end_ptr() const {
|
| 237 |
+
return math::advance_ptr(base, get_num_bytes());
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
CUTLASS_HOST_DEVICE
|
| 241 |
+
Buffer get_rank_buffer(const uint32_t& rank_idx) const {
|
| 242 |
+
return {
|
| 243 |
+
data_layout,
|
| 244 |
+
1, num_max_tokens_per_rank,
|
| 245 |
+
math::advance_ptr(base, get_num_bytes_per_rank() * rank_idx)
|
| 246 |
+
};
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
CUTLASS_HOST_DEVICE
|
| 250 |
+
Data get_data_buffer(const uint32_t& token_idx, const bool& global = false) const {
|
| 251 |
+
DG_DEVICE_ASSERT(num_ranks == 1 or global);
|
| 252 |
+
return Data(
|
| 253 |
+
data_layout.num_bytes,
|
| 254 |
+
data_layout.require_tma_alignment,
|
| 255 |
+
math::advance_ptr(base, data_layout.get_num_bytes<uint64_t>() * token_idx)
|
| 256 |
+
);
|
| 257 |
+
}
|
| 258 |
+
};
|
| 259 |
+
|
| 260 |
+
} // namespace deep_gemm::layout
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/layout/sym_buffer.cuh
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <deep_gemm/common/exception.cuh>
|
| 4 |
+
|
| 5 |
+
namespace deep_gemm::layout {
|
| 6 |
+
|
| 7 |
+
constexpr static uint32_t kNumMaxRanks = 72;
|
| 8 |
+
|
| 9 |
+
template <uint32_t kNumRanks = kNumMaxRanks>
|
| 10 |
+
struct SymBuffer {
|
| 11 |
+
int64_t base;
|
| 12 |
+
int64_t offsets[kNumMaxRanks];
|
| 13 |
+
uint32_t rank_idx;
|
| 14 |
+
|
| 15 |
+
DG_STATIC_ASSERT(kNumRanks <= kNumMaxRanks, "Too many ranks");
|
| 16 |
+
|
| 17 |
+
SymBuffer() = default;
|
| 18 |
+
|
| 19 |
+
template <typename Container>
|
| 20 |
+
explicit SymBuffer(const Container& c, const uint32_t& rank_idx): rank_idx(rank_idx) {
|
| 21 |
+
const auto size = static_cast<uint32_t>(c.size());
|
| 22 |
+
base = c[rank_idx];
|
| 23 |
+
for (uint32_t i = 0; i < kNumMaxRanks; ++ i)
|
| 24 |
+
offsets[i] = i < size ? (c[i] - base) : 0;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
#if defined(__CUDA_ARCH__) or defined(__CLION_IDE__)
|
| 28 |
+
template <typename ptr_t = void*>
|
| 29 |
+
CUTLASS_DEVICE ptr_t get_base_ptr() const {
|
| 30 |
+
return reinterpret_cast<ptr_t>(base);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
template <typename ptr_t>
|
| 34 |
+
CUTLASS_DEVICE ptr_t map(const ptr_t& ptr, const uint32_t& dst_rank_idx) const {
|
| 35 |
+
int64_t mapped_ptr = offsets[dst_rank_idx] + reinterpret_cast<int64_t>(ptr);
|
| 36 |
+
return *reinterpret_cast<ptr_t*>(&mapped_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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/atom/mma_traits_sm100.hpp>
|
| 4 |
+
#include <cute/arch/mma_sm100_umma.hpp>
|
| 5 |
+
|
| 6 |
+
#include <deep_gemm/common/exception.cuh>
|
| 7 |
+
#include <deep_gemm/common/math.cuh>
|
| 8 |
+
#include <deep_gemm/common/tma_copy.cuh>
|
| 9 |
+
|
| 10 |
+
namespace deep_gemm::mma::sm100 {
|
| 11 |
+
|
| 12 |
+
/// Shared memory descriptor
|
| 13 |
+
CUTLASS_DEVICE
|
| 14 |
+
cute::UMMA::SmemDescriptor make_smem_desc(cute::UMMA::LayoutType layout, void* smem_ptr,
|
| 15 |
+
const uint32_t& stride_byte_offset, const uint32_t& leading_byte_offset) {
|
| 16 |
+
cute::UMMA::SmemDescriptor desc;
|
| 17 |
+
|
| 18 |
+
// Set the version for SM100
|
| 19 |
+
desc.version_ = 1;
|
| 20 |
+
|
| 21 |
+
// Legacy mode
|
| 22 |
+
desc.lbo_mode_ = 0;
|
| 23 |
+
|
| 24 |
+
// Layout
|
| 25 |
+
desc.layout_type_ = static_cast<uint8_t>(layout);
|
| 26 |
+
|
| 27 |
+
// Start address
|
| 28 |
+
const auto uint_ptr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 29 |
+
desc.start_address_ = static_cast<uint16_t>(uint_ptr >> 4);
|
| 30 |
+
|
| 31 |
+
// Base offset
|
| 32 |
+
desc.base_offset_ = 0;
|
| 33 |
+
|
| 34 |
+
// SBO and LBO
|
| 35 |
+
desc.stride_byte_offset_ = stride_byte_offset >> 4;
|
| 36 |
+
desc.leading_byte_offset_ = leading_byte_offset >> 4;
|
| 37 |
+
|
| 38 |
+
return desc;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
CUTLASS_DEVICE
|
| 42 |
+
cute::UMMA::SmemDescriptor make_sf_desc(void* smem_ptr) {
|
| 43 |
+
// NOTES: the UTCCP layout is K-major by default
|
| 44 |
+
// Atom size: 8 x 128 bits
|
| 45 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 46 |
+
// Since the UTCCP we used is 128b-wide (only 1 atom on K), so LBO can be zero
|
| 47 |
+
return make_smem_desc(cute::UMMA::LayoutType::SWIZZLE_NONE, smem_ptr, 8 * 16, 0);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
CUTLASS_DEVICE
|
| 51 |
+
void replace_smem_desc_addr(cute::UMMA::SmemDescriptor& desc, const void* smem_ptr) {
|
| 52 |
+
const auto uint_ptr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 53 |
+
desc.start_address_ = static_cast<uint16_t>(uint_ptr >> 4);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
CUTLASS_DEVICE
|
| 57 |
+
static uint32_t get_atom_base(const cute::UMMA::LayoutType& layout_type) {
|
| 58 |
+
return layout_type == cute::UMMA::LayoutType::SWIZZLE_128B_BASE32B ? 32 : 16;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
/// UMMA descriptors
|
| 62 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 63 |
+
template <cute::UMMA::Major kMajorMode, uint32_t kSwizzleMode, bool kUseBase32, typename dtype_t>
|
| 64 |
+
constexpr static cute::UMMA::LayoutType to_umma_layout_type() {
|
| 65 |
+
DG_STATIC_ASSERT(kSwizzleMode == 0 or kSwizzleMode == 16 or
|
| 66 |
+
kSwizzleMode == 32 or kSwizzleMode == 64 or
|
| 67 |
+
kSwizzleMode == 128, "Invalid swizzling mode");
|
| 68 |
+
// A special case
|
| 69 |
+
if constexpr ((cute::is_same_v<dtype_t, float> and kMajorMode == cute::UMMA::Major::MN) or kUseBase32) {
|
| 70 |
+
DG_STATIC_ASSERT(kUseBase32, "Invalid swizzling base");
|
| 71 |
+
return cute::UMMA::LayoutType::SWIZZLE_128B_BASE32B;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// Normal cases
|
| 75 |
+
if constexpr (kSwizzleMode == 0) return cute::UMMA::LayoutType::SWIZZLE_NONE;
|
| 76 |
+
if constexpr (kSwizzleMode == 16) return cute::UMMA::LayoutType::SWIZZLE_NONE;
|
| 77 |
+
if constexpr (kSwizzleMode == 32) return cute::UMMA::LayoutType::SWIZZLE_32B;
|
| 78 |
+
if constexpr (kSwizzleMode == 64) return cute::UMMA::LayoutType::SWIZZLE_64B;
|
| 79 |
+
if constexpr (kSwizzleMode == 128) return cute::UMMA::LayoutType::SWIZZLE_128B;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 83 |
+
CUTLASS_DEVICE
|
| 84 |
+
constexpr uint32_t get_umma_desc_stride_k() {
|
| 85 |
+
return kMajorMode == cute::UMMA::Major::K ? 1 : tma::get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 89 |
+
CUTLASS_DEVICE
|
| 90 |
+
uint32_t advance_umma_desc_lo(const uint32_t& base, const uint32_t& offset, const uint32_t& k_idx) {
|
| 91 |
+
return base + (((offset + k_idx * get_umma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>()) * static_cast<uint32_t>(sizeof(dtype_t))) >> 4u);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, bool kUseBase32 = false, typename dtype_t>
|
| 95 |
+
CUTLASS_DEVICE
|
| 96 |
+
cute::UMMA::SmemDescriptor make_umma_desc(dtype_t* base_smem_ptr, uint32_t mn_idx, uint32_t k_idx) {
|
| 97 |
+
const uint32_t stride_k = get_umma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 98 |
+
const auto layout_type = to_umma_layout_type<kMajorMode, kSwizzleMode, kUseBase32, dtype_t>();
|
| 99 |
+
const auto num_non_contiguous = 128 / get_atom_base(layout_type);
|
| 100 |
+
if constexpr (kMajorMode == cute::UMMA::Major::K) {
|
| 101 |
+
// NOTES: for K-major layout, the swizzle must be the same as `BLOCK_K * sizeof(dtype_t)`
|
| 102 |
+
// also, atom index must be 0, so that each block has exactly one swizzle atom on the K axis
|
| 103 |
+
DG_STATIC_ASSERT(kSwizzleMode == BLOCK_K * sizeof(dtype_t), "Unexpected value");
|
| 104 |
+
|
| 105 |
+
// Atom size: 8 x `kSwizzleMode` (in bytes, on K)
|
| 106 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 107 |
+
// NOTES: on K, there is only 1 atom as asserted previously, so LBO can be 0
|
| 108 |
+
const uint32_t stride_byte_offset = num_non_contiguous * BLOCK_K * sizeof(dtype_t);
|
| 109 |
+
const uint32_t leading_byte_offset = 0;
|
| 110 |
+
return make_smem_desc(layout_type,
|
| 111 |
+
base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k,
|
| 112 |
+
stride_byte_offset, leading_byte_offset);
|
| 113 |
+
} else {
|
| 114 |
+
constexpr uint32_t BLOCK_MN_ATOM = tma::get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 115 |
+
|
| 116 |
+
// Must have no in-atom MN-idx
|
| 117 |
+
// NOTES: no worries for the runtime assert, the `mn_idx` are constants at compilation time
|
| 118 |
+
DG_DEVICE_ASSERT(mn_idx % BLOCK_MN_ATOM == 0);
|
| 119 |
+
DG_STATIC_ASSERT(kSwizzleMode > 0, "Invalid swizzling");
|
| 120 |
+
|
| 121 |
+
// Atom size: `kSwizzleMode` (in bytes, on MN) x 8
|
| 122 |
+
// NOTES: `kSwizzleMode == 16` mean non-swizzling but interleaving
|
| 123 |
+
// {SBO, LBO} means the byte stride between atoms on {K, MN} for swizzling
|
| 124 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K} for non-swizzling
|
| 125 |
+
uint32_t stride_byte_offset = num_non_contiguous * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 126 |
+
uint32_t leading_byte_offset = BLOCK_K * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 127 |
+
if constexpr (kSwizzleMode == 16)
|
| 128 |
+
math::swap(stride_byte_offset, leading_byte_offset);
|
| 129 |
+
return make_smem_desc(layout_type,
|
| 130 |
+
base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k,
|
| 131 |
+
stride_byte_offset, leading_byte_offset);
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
CUTLASS_DEVICE uint64_t make_runtime_instr_desc_with_sf_id(
|
| 136 |
+
cute::UMMA::InstrDescriptorBlockScaled desc, const uint32_t& sfa_id, const uint32_t& sfb_id) {
|
| 137 |
+
desc.a_sf_id_ = sfa_id, desc.b_sf_id_ = sfb_id;
|
| 138 |
+
return static_cast<uint64_t>(static_cast<uint32_t>(desc)) << 32;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
CUTLASS_DEVICE void update_instr_desc_with_umma_n(
|
| 142 |
+
cute::UMMA::InstrDescriptorBlockScaled& desc, const uint32_t& umma_n) {
|
| 143 |
+
desc.n_dim_ = umma_n >> 3;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
CUTLASS_DEVICE void update_instr_desc_with_umma_n(
|
| 147 |
+
cute::UMMA::InstrDescriptor& desc, const uint32_t& umma_n) {
|
| 148 |
+
desc.n_dim_ = umma_n >> 3;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
} // namespace deep_gemm::mma::sm100
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/mma/sm90.cuh
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cute/arch/cluster_sm90.hpp>
|
| 4 |
+
#include <cute/arch/mma_sm90_desc.hpp>
|
| 5 |
+
#include <cute/arch/mma_sm90_gmma.hpp>
|
| 6 |
+
#include <cute/arch/mma_sm90_gmma_ext.hpp>
|
| 7 |
+
#include <cute/arch/mma_sm100_desc.hpp>
|
| 8 |
+
|
| 9 |
+
#include <deep_gemm/common/exception.cuh>
|
| 10 |
+
|
| 11 |
+
namespace deep_gemm::mma::sm90 {
|
| 12 |
+
|
| 13 |
+
/// MMA
|
| 14 |
+
template <int N_, typename MMA>
|
| 15 |
+
struct FP8MMA {
|
| 16 |
+
template <size_t ...Idx>
|
| 17 |
+
CUTLASS_DEVICE static void call_fma_impl(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 18 |
+
using namespace cute::SM90::GMMA;
|
| 19 |
+
MMA::fma(desc_a, desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
CUTLASS_DEVICE static void wgmma(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 23 |
+
call_fma_impl(desc_a, desc_b, d, scale_d, cute::make_index_sequence<N_ / 2>{});
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
static constexpr int M = 64;
|
| 27 |
+
static constexpr int N = N_;
|
| 28 |
+
static constexpr int K = 32;
|
| 29 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
template <int N>
|
| 33 |
+
struct FP8MMASelector {
|
| 34 |
+
static constexpr auto select_mma() {
|
| 35 |
+
using namespace cute::SM90::GMMA;
|
| 36 |
+
if constexpr (N == 8) return MMA_64x8x32_F32E4M3E4M3_SS_TN();
|
| 37 |
+
if constexpr (N == 16) return MMA_64x16x32_F32E4M3E4M3_SS_TN();
|
| 38 |
+
if constexpr (N == 24) return MMA_64x24x32_F32E4M3E4M3_SS_TN();
|
| 39 |
+
if constexpr (N == 32) return MMA_64x32x32_F32E4M3E4M3_SS_TN();
|
| 40 |
+
if constexpr (N == 40) return MMA_64x40x32_F32E4M3E4M3_SS_TN();
|
| 41 |
+
if constexpr (N == 48) return MMA_64x48x32_F32E4M3E4M3_SS_TN();
|
| 42 |
+
if constexpr (N == 56) return MMA_64x56x32_F32E4M3E4M3_SS_TN();
|
| 43 |
+
if constexpr (N == 64) return MMA_64x64x32_F32E4M3E4M3_SS_TN();
|
| 44 |
+
if constexpr (N == 72) return MMA_64x72x32_F32E4M3E4M3_SS_TN();
|
| 45 |
+
if constexpr (N == 80) return MMA_64x80x32_F32E4M3E4M3_SS_TN();
|
| 46 |
+
if constexpr (N == 88) return MMA_64x88x32_F32E4M3E4M3_SS_TN();
|
| 47 |
+
if constexpr (N == 96) return MMA_64x96x32_F32E4M3E4M3_SS_TN();
|
| 48 |
+
if constexpr (N == 104) return MMA_64x104x32_F32E4M3E4M3_SS_TN();
|
| 49 |
+
if constexpr (N == 112) return MMA_64x112x32_F32E4M3E4M3_SS_TN();
|
| 50 |
+
if constexpr (N == 120) return MMA_64x120x32_F32E4M3E4M3_SS_TN();
|
| 51 |
+
if constexpr (N == 128) return MMA_64x128x32_F32E4M3E4M3_SS_TN();
|
| 52 |
+
if constexpr (N == 136) return MMA_64x136x32_F32E4M3E4M3_SS_TN();
|
| 53 |
+
if constexpr (N == 144) return MMA_64x144x32_F32E4M3E4M3_SS_TN();
|
| 54 |
+
if constexpr (N == 152) return MMA_64x152x32_F32E4M3E4M3_SS_TN();
|
| 55 |
+
if constexpr (N == 160) return MMA_64x160x32_F32E4M3E4M3_SS_TN();
|
| 56 |
+
if constexpr (N == 168) return MMA_64x168x32_F32E4M3E4M3_SS_TN();
|
| 57 |
+
if constexpr (N == 176) return MMA_64x176x32_F32E4M3E4M3_SS_TN();
|
| 58 |
+
if constexpr (N == 184) return MMA_64x184x32_F32E4M3E4M3_SS_TN();
|
| 59 |
+
if constexpr (N == 192) return MMA_64x192x32_F32E4M3E4M3_SS_TN();
|
| 60 |
+
if constexpr (N == 200) return MMA_64x200x32_F32E4M3E4M3_SS_TN();
|
| 61 |
+
if constexpr (N == 208) return MMA_64x208x32_F32E4M3E4M3_SS_TN();
|
| 62 |
+
if constexpr (N == 216) return MMA_64x216x32_F32E4M3E4M3_SS_TN();
|
| 63 |
+
if constexpr (N == 224) return MMA_64x224x32_F32E4M3E4M3_SS_TN();
|
| 64 |
+
if constexpr (N == 232) return MMA_64x232x32_F32E4M3E4M3_SS_TN();
|
| 65 |
+
if constexpr (N == 240) return MMA_64x240x32_F32E4M3E4M3_SS_TN();
|
| 66 |
+
if constexpr (N == 248) return MMA_64x248x32_F32E4M3E4M3_SS_TN();
|
| 67 |
+
if constexpr (N == 256) return MMA_64x256x32_F32E4M3E4M3_SS_TN();
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
static constexpr auto select_type() {
|
| 71 |
+
return FP8MMA<N, decltype(select_mma())>();
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
using type = decltype(select_type());
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
template <int N_, typename MMA>
|
| 78 |
+
struct BF16MMA {
|
| 79 |
+
template <size_t ...Idx>
|
| 80 |
+
CUTLASS_DEVICE static void call_fma_impl(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 81 |
+
using namespace cute::SM90::GMMA;
|
| 82 |
+
MMA::fma(desc_a, desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
CUTLASS_DEVICE static void wgmma(uint64_t const& desc_a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 86 |
+
call_fma_impl(desc_a, desc_b, d, scale_d, cute::make_index_sequence<N_/2>{});
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
static constexpr int M = 64;
|
| 90 |
+
static constexpr int N = N_;
|
| 91 |
+
static constexpr int K = 16;
|
| 92 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
template <cute::UMMA::Major kMajor>
|
| 96 |
+
constexpr cute::SM90::GMMA::Major to_sm90_major() {
|
| 97 |
+
DG_STATIC_ASSERT(kMajor == cute::UMMA::Major::K or kMajor == cute::UMMA::Major::MN, "Invalid major-ness");
|
| 98 |
+
return kMajor == cute::UMMA::Major::K ? cute::SM90::GMMA::Major::K : cute::SM90::GMMA::Major::MN;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
template <int N,
|
| 102 |
+
cute::UMMA::Major kMajorA = cute::UMMA::Major::K,
|
| 103 |
+
cute::UMMA::Major kMajorB = cute::UMMA::Major::K>
|
| 104 |
+
struct BF16MMASelector {
|
| 105 |
+
static constexpr auto select_mma() {
|
| 106 |
+
using namespace cute::SM90::GMMA;
|
| 107 |
+
constexpr auto kGMMAMajorA = to_sm90_major<kMajorA>();
|
| 108 |
+
constexpr auto kGMMAMajorB = to_sm90_major<kMajorB>();
|
| 109 |
+
if constexpr (N == 8) return MMA_64x8x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 110 |
+
if constexpr (N == 16) return MMA_64x16x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 111 |
+
if constexpr (N == 24) return MMA_64x24x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 112 |
+
if constexpr (N == 32) return MMA_64x32x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 113 |
+
if constexpr (N == 40) return MMA_64x40x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 114 |
+
if constexpr (N == 48) return MMA_64x48x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 115 |
+
if constexpr (N == 56) return MMA_64x56x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 116 |
+
if constexpr (N == 64) return MMA_64x64x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 117 |
+
if constexpr (N == 72) return MMA_64x72x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 118 |
+
if constexpr (N == 80) return MMA_64x80x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 119 |
+
if constexpr (N == 88) return MMA_64x88x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 120 |
+
if constexpr (N == 96) return MMA_64x96x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 121 |
+
if constexpr (N == 104) return MMA_64x104x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 122 |
+
if constexpr (N == 112) return MMA_64x112x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 123 |
+
if constexpr (N == 120) return MMA_64x120x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 124 |
+
if constexpr (N == 128) return MMA_64x128x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 125 |
+
if constexpr (N == 136) return MMA_64x136x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 126 |
+
if constexpr (N == 144) return MMA_64x144x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 127 |
+
if constexpr (N == 152) return MMA_64x152x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 128 |
+
if constexpr (N == 160) return MMA_64x160x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 129 |
+
if constexpr (N == 168) return MMA_64x168x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 130 |
+
if constexpr (N == 176) return MMA_64x176x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 131 |
+
if constexpr (N == 184) return MMA_64x184x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 132 |
+
if constexpr (N == 192) return MMA_64x192x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 133 |
+
if constexpr (N == 200) return MMA_64x200x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 134 |
+
if constexpr (N == 208) return MMA_64x208x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 135 |
+
if constexpr (N == 216) return MMA_64x216x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 136 |
+
if constexpr (N == 224) return MMA_64x224x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 137 |
+
if constexpr (N == 232) return MMA_64x232x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 138 |
+
if constexpr (N == 240) return MMA_64x240x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 139 |
+
if constexpr (N == 248) return MMA_64x248x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 140 |
+
if constexpr (N == 256) return MMA_64x256x16_F32BF16BF16_SS<kGMMAMajorA, kGMMAMajorB>();
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
static constexpr auto select_type() {
|
| 144 |
+
return BF16MMA<N, decltype(select_mma())>();
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
using type = decltype(select_type());
|
| 148 |
+
};
|
| 149 |
+
|
| 150 |
+
template <int N_, typename MMA>
|
| 151 |
+
struct TF32MMARS {
|
| 152 |
+
template <size_t ...Idx>
|
| 153 |
+
CUTLASS_DEVICE static void call_fma_impl(uint32_t* a, uint64_t const& desc_b, float* d, bool scale_d, cute::index_sequence<Idx...>) {
|
| 154 |
+
using namespace cute::SM90::GMMA;
|
| 155 |
+
MMA::fma(a[0], a[1], a[2], a[3], desc_b, d[Idx]..., (scale_d ? ScaleOut::One : ScaleOut::Zero));
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
CUTLASS_DEVICE static void wgmma(float* a, uint64_t const& desc_b, float* d, bool scale_d) {
|
| 159 |
+
call_fma_impl(reinterpret_cast<uint32_t*>(a), desc_b, d, scale_d, cute::make_index_sequence<N_/2>{});
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
static constexpr int M = 64;
|
| 163 |
+
static constexpr int N = N_;
|
| 164 |
+
static constexpr int K = 8;
|
| 165 |
+
static constexpr int kNumAccum = M * N / 128;
|
| 166 |
+
};
|
| 167 |
+
|
| 168 |
+
template <int N, bool kUseRS = true>
|
| 169 |
+
struct TF32MMASelector {
|
| 170 |
+
static constexpr auto select_mma() {
|
| 171 |
+
using namespace cute::SM90::GMMA;
|
| 172 |
+
if constexpr (kUseRS) {
|
| 173 |
+
if constexpr (N == 8) return MMA_64x8x8_F32TF32TF32_RS_TN();
|
| 174 |
+
if constexpr (N == 16) return MMA_64x16x8_F32TF32TF32_RS_TN();
|
| 175 |
+
if constexpr (N == 32) return MMA_64x32x8_F32TF32TF32_RS_TN();
|
| 176 |
+
if constexpr (N == 64) return MMA_64x64x8_F32TF32TF32_RS_TN();
|
| 177 |
+
if constexpr (N == 128) return MMA_64x128x8_F32TF32TF32_RS_TN();
|
| 178 |
+
if constexpr (N == 256) return MMA_64x256x8_F32TF32TF32_RS_TN();
|
| 179 |
+
DG_STATIC_ASSERT(N == 8 or N == 16 or N == 32 or N == 64 or N == 128 or N == 256, "Invalid N");
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
static constexpr auto select_type() {
|
| 184 |
+
if constexpr (kUseRS) {
|
| 185 |
+
return TF32MMARS<N, decltype(select_mma())>();
|
| 186 |
+
} else {
|
| 187 |
+
DG_STATIC_ASSERT(kUseRS, "SS mode is not supported for TF32MMASelector for now");
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
using type = decltype(select_type());
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
/// Shared memory descriptor
|
| 195 |
+
template <class PointerType>
|
| 196 |
+
CUTLASS_DEVICE cute::GmmaDescriptor
|
| 197 |
+
make_smem_desc(PointerType smem_ptr, const int& layout_type,
|
| 198 |
+
const uint32_t& leading_byte_offset = 0,
|
| 199 |
+
const uint32_t& stride_byte_offset = 1024) {
|
| 200 |
+
// NOTES: the default LBO and SBO are for K-major types
|
| 201 |
+
cute::GmmaDescriptor desc;
|
| 202 |
+
const auto uint_ptr = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr));
|
| 203 |
+
desc.bitfield.start_address_ = uint_ptr >> 4;
|
| 204 |
+
desc.bitfield.layout_type_ = layout_type;
|
| 205 |
+
desc.bitfield.leading_byte_offset_ = leading_byte_offset >> 4;
|
| 206 |
+
desc.bitfield.stride_byte_offset_ = stride_byte_offset >> 4;
|
| 207 |
+
desc.bitfield.base_offset_ = 0;
|
| 208 |
+
return desc;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
template <uint32_t BLOCK_INNER, uint32_t kSwizzleMode, typename dtype_t>
|
| 212 |
+
constexpr uint32_t get_inner_block_atom_size() {
|
| 213 |
+
return kSwizzleMode == 0 ? BLOCK_INNER : kSwizzleMode / sizeof(dtype_t);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t kSwizzleMode, typename dtype_t>
|
| 217 |
+
CUTLASS_DEVICE
|
| 218 |
+
constexpr uint32_t get_gmma_desc_stride_k() {
|
| 219 |
+
return kMajorMode == cute::UMMA::Major::K ? 1 : get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 223 |
+
template <cute::UMMA::Major kMajorMode, uint32_t kSwizzleMode, typename dtype_t>
|
| 224 |
+
constexpr static cute::SM90::GMMA::LayoutType to_gmma_layout_type() {
|
| 225 |
+
DG_STATIC_ASSERT(kSwizzleMode == 0 or kSwizzleMode == 16 or
|
| 226 |
+
kSwizzleMode == 32 or kSwizzleMode == 64 or
|
| 227 |
+
kSwizzleMode == 128, "Invalid swizzling mode");
|
| 228 |
+
|
| 229 |
+
// Normal cases
|
| 230 |
+
if constexpr (kSwizzleMode == 0) return cute::SM90::GMMA::LayoutType::INTERLEAVE;
|
| 231 |
+
if constexpr (kSwizzleMode == 16) return cute::SM90::GMMA::LayoutType::INTERLEAVE;
|
| 232 |
+
if constexpr (kSwizzleMode == 32) return cute::SM90::GMMA::LayoutType::B32;
|
| 233 |
+
if constexpr (kSwizzleMode == 64) return cute::SM90::GMMA::LayoutType::B64;
|
| 234 |
+
if constexpr (kSwizzleMode == 128) return cute::SM90::GMMA::LayoutType::B128;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, typename dtype_t>
|
| 238 |
+
CUTLASS_DEVICE
|
| 239 |
+
uint32_t advance_gmma_desc_lo(const uint32_t& base, const uint32_t& mn_idx, const uint32_t& k_idx, const uint32_t& offset = 0) {
|
| 240 |
+
return base + (((offset + mn_idx * BLOCK_K + k_idx * get_gmma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>()) * static_cast<uint32_t>(sizeof(dtype_t))) >> 4u);
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
template <cute::UMMA::Major kMajorMode, uint32_t BLOCK_MN, uint32_t BLOCK_K, uint32_t kSwizzleMode, typename dtype_t>
|
| 244 |
+
CUTLASS_DEVICE
|
| 245 |
+
cute::GmmaDescriptor make_gmma_desc(dtype_t* base_smem_ptr, uint32_t mn_idx, uint32_t k_idx) {
|
| 246 |
+
const uint32_t stride_k = get_gmma_desc_stride_k<kMajorMode, BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 247 |
+
const auto layout_type = to_gmma_layout_type<kMajorMode, kSwizzleMode, dtype_t>();
|
| 248 |
+
constexpr uint32_t num_non_contiguous = 128 / 16;
|
| 249 |
+
if constexpr (kMajorMode == cute::UMMA::Major::K) {
|
| 250 |
+
// NOTES: for K-major layout, the swizzle must be 128B (also, atom index must be 0), as `BLOCK_K` is always 128
|
| 251 |
+
DG_STATIC_ASSERT(kSwizzleMode == BLOCK_K * sizeof(dtype_t), "Unexpected value");
|
| 252 |
+
|
| 253 |
+
// Atom size: 8 x `kSwizzleMode` (in bytes, on K)
|
| 254 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K}
|
| 255 |
+
// NOTES: on K, there is only 1 atom as asserted previously, so LBO can be 0
|
| 256 |
+
const uint32_t stride_byte_offset = num_non_contiguous * BLOCK_K * sizeof(dtype_t);
|
| 257 |
+
const uint32_t leading_byte_offset = 0;
|
| 258 |
+
return make_smem_desc(base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k, static_cast<uint32_t>(layout_type),
|
| 259 |
+
leading_byte_offset, stride_byte_offset);
|
| 260 |
+
} else {
|
| 261 |
+
constexpr uint32_t BLOCK_MN_ATOM = get_inner_block_atom_size<BLOCK_MN, kSwizzleMode, dtype_t>();
|
| 262 |
+
|
| 263 |
+
// Must have no in-atom MN-idx
|
| 264 |
+
// NOTES: no worries for the runtime assert, the `mn_idx` are constants at compilation time
|
| 265 |
+
DG_DEVICE_ASSERT(mn_idx % BLOCK_MN_ATOM == 0);
|
| 266 |
+
DG_STATIC_ASSERT(kSwizzleMode > 0, "Invalid swizzling");
|
| 267 |
+
|
| 268 |
+
// Atom size: `kSwizzleMode` (in bytes, on MN) x 8
|
| 269 |
+
// NOTES: `kSwizzleMode == 16` mean non-swizzling but interleaving
|
| 270 |
+
// {SBO, LBO} means the byte stride between atoms on {K, MN} for swizzling
|
| 271 |
+
// {SBO, LBO} means the byte stride between atoms on {MN, K} for non-swizzling
|
| 272 |
+
uint32_t stride_byte_offset = num_non_contiguous * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 273 |
+
uint32_t leading_byte_offset = BLOCK_K * BLOCK_MN_ATOM * sizeof(dtype_t);
|
| 274 |
+
if constexpr (kSwizzleMode == 16)
|
| 275 |
+
math::swap(stride_byte_offset, leading_byte_offset);
|
| 276 |
+
return make_smem_desc(base_smem_ptr + mn_idx * BLOCK_K + k_idx * stride_k, static_cast<uint32_t>(layout_type),
|
| 277 |
+
leading_byte_offset, stride_byte_offset);
|
| 278 |
+
}
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
// ReSharper disable once CppNotAllPathsReturnValue
|
| 282 |
+
template <uint32_t kHeadDim>
|
| 283 |
+
static constexpr int to_swizzle_cute_type() {
|
| 284 |
+
DG_STATIC_ASSERT(kHeadDim == 32 or kHeadDim == 64 or kHeadDim == 128, "Invalid swizzling");
|
| 285 |
+
if constexpr (kHeadDim == 32)
|
| 286 |
+
return static_cast<int>(cute::SM90::GMMA::LayoutType::B32);
|
| 287 |
+
if constexpr (kHeadDim == 64)
|
| 288 |
+
return static_cast<int>(cute::SM90::GMMA::LayoutType::B64);
|
| 289 |
+
if constexpr (kHeadDim == 128)
|
| 290 |
+
return static_cast<int>(cute::SM90::GMMA::LayoutType::B128);
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
} // namespace deep_gemm::mma::sm90
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/ld_st.cuh
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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its LD/ST instructions
|
| 9 |
+
#if defined(CUDART_VERSION) and CUDART_VERSION >= 13000
|
| 10 |
+
using longlong4_t = longlong4_32a;
|
| 11 |
+
#define make_longlong4_t make_longlong4_32a
|
| 12 |
+
#else
|
| 13 |
+
struct alignas(32) longlong4_t { long long x, y, z, w; };
|
| 14 |
+
CUTLASS_HOST_DEVICE longlong4_t make_longlong4_t(
|
| 15 |
+
const long long& x, const long long& y, const long long& z, const long long& w) {
|
| 16 |
+
return {x, y, z, w};
|
| 17 |
+
}
|
| 18 |
+
#endif
|
| 19 |
+
|
| 20 |
+
/// LD/ST matrix
|
| 21 |
+
// TODO: remove `struct`
|
| 22 |
+
struct SM90_U32x2_LDSM_N {
|
| 23 |
+
CUTLASS_DEVICE static void
|
| 24 |
+
copy(uint32_t& dst_0, uint32_t& dst_1, void* smem_src) {
|
| 25 |
+
asm volatile("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];\n"
|
| 26 |
+
: "=r"(dst_0), "=r"(dst_1)
|
| 27 |
+
: "l"(__cvta_generic_to_shared(smem_src)));
|
| 28 |
+
}
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
struct SM90_U32x4_LDSM_N {
|
| 32 |
+
CUTLASS_DEVICE static void
|
| 33 |
+
copy(uint32_t& dst_0, uint32_t& dst_1, uint32_t& dst_2, uint32_t& dst_3, void* smem_src) {
|
| 34 |
+
asm volatile("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n"
|
| 35 |
+
: "=r"(dst_0), "=r"(dst_1), "=r"(dst_2), "=r"(dst_3)
|
| 36 |
+
: "l"(__cvta_generic_to_shared(smem_src)));
|
| 37 |
+
}
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
template <typename dtype_t>
|
| 41 |
+
struct SM90_U32x2_STSM_N {
|
| 42 |
+
CUTLASS_DEVICE static void
|
| 43 |
+
copy(dtype_t src_0, dtype_t src_1, void* smem_dst) {
|
| 44 |
+
DG_STATIC_ASSERT(sizeof(dtype_t) == sizeof(uint32_t), "Invalid dtype");
|
| 45 |
+
const uint32_t src[2] = {*reinterpret_cast<uint32_t*>(&src_0), *reinterpret_cast<uint32_t*>(&src_1)};
|
| 46 |
+
asm volatile("stmatrix.sync.aligned.x2.m8n8.shared.b16 [%0], {%1, %2};\n"
|
| 47 |
+
:: "l"(__cvta_generic_to_shared(smem_dst)), "r"(src[0]), "r"(src[1]));
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
template <typename dtype_t>
|
| 52 |
+
struct SM90_U32x4_STSM_T {
|
| 53 |
+
CUTLASS_DEVICE static void
|
| 54 |
+
copy(dtype_t src_0, dtype_t src_1, dtype_t src_2, dtype_t src_3, void* smem_dst) {
|
| 55 |
+
DG_STATIC_ASSERT(sizeof(dtype_t) == sizeof(uint32_t), "Invalid dtype");
|
| 56 |
+
const uint32_t src[4] = {*reinterpret_cast<uint32_t*>(&src_0), *reinterpret_cast<uint32_t*>(&src_1),
|
| 57 |
+
*reinterpret_cast<uint32_t*>(&src_2), *reinterpret_cast<uint32_t*>(&src_3)};
|
| 58 |
+
asm volatile("stmatrix.sync.aligned.x4.m8n8.shared.b16.trans [%0], {%1, %2, %3, %4};\n"
|
| 59 |
+
:: "l"(__cvta_generic_to_shared(smem_dst)),
|
| 60 |
+
"r"(src[0]), "r"(src[1]), "r"(src[2]), "r"(src[3]));
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
template <typename dtype_t>
|
| 65 |
+
struct SM100_U8x4_STSM_T {
|
| 66 |
+
__device__ __forceinline__ static void
|
| 67 |
+
copy(dtype_t src_0, void* smem_dst) {
|
| 68 |
+
DG_STATIC_ASSERT(sizeof(dtype_t) == sizeof(uint32_t), "Invalid dtype");
|
| 69 |
+
const uint32_t src = *reinterpret_cast<uint32_t*>(&src_0);
|
| 70 |
+
asm volatile("stmatrix.sync.aligned.m16n8.x1.trans.shared.b8 [%0], {%1};\n"
|
| 71 |
+
:: "l"(__cvta_generic_to_shared(smem_dst)), "r"(src));
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template <typename dtype_t>
|
| 76 |
+
struct SM100_U8x8_STSM_T {
|
| 77 |
+
__device__ __forceinline__ static void
|
| 78 |
+
copy(dtype_t src_0, dtype_t src_1, void* smem_dst) {
|
| 79 |
+
DG_STATIC_ASSERT(sizeof(dtype_t) == sizeof(uint32_t), "Invalid dtype");
|
| 80 |
+
const uint32_t src[2] = {*reinterpret_cast<uint32_t*>(&src_0), *reinterpret_cast<uint32_t*>(&src_1)};
|
| 81 |
+
asm volatile("stmatrix.sync.aligned.m16n8.x2.trans.shared.b8 [%0], {%1, %2};\n"
|
| 82 |
+
:: "l"(__cvta_generic_to_shared(smem_dst)), "r"(src[0]), "r"(src[1]));
|
| 83 |
+
}
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
/// Shared memory
|
| 87 |
+
CUTLASS_DEVICE uint32_t ld_shared(const uint32_t* ptr) {
|
| 88 |
+
uint32_t ret;
|
| 89 |
+
asm volatile("ld.shared.u32 %0, [%1];" : "=r"(ret) : "l"(__cvta_generic_to_shared(ptr)));
|
| 90 |
+
return ret;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
CUTLASS_DEVICE float2 ld_shared(const float2* ptr) {
|
| 94 |
+
float2 ret;
|
| 95 |
+
asm volatile("ld.shared.v2.f32 {%0, %1}, [%2];" : "=f"(ret.x), "=f"(ret.y) : "l"(__cvta_generic_to_shared(ptr)));
|
| 96 |
+
return ret;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
CUTLASS_DEVICE float4 ld_shared(const float4* ptr) {
|
| 100 |
+
float4 ret;
|
| 101 |
+
asm volatile("ld.shared.v4.f32 {%0, %1, %2, %3}, [%4];" : "=f"(ret.x), "=f"(ret.y), "=f"(ret.z), "=f"(ret.w) : "l"(__cvta_generic_to_shared(ptr)));
|
| 102 |
+
return ret;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
CUTLASS_DEVICE uint4 ld_shared(const uint4* ptr) {
|
| 106 |
+
uint4 ret;
|
| 107 |
+
asm volatile("ld.shared.v4.u32 {%0, %1, %2, %3}, [%4];" : "=r"(ret.x), "=r"(ret.y), "=r"(ret.z), "=r"(ret.w) : "l"(__cvta_generic_to_shared(ptr)));
|
| 108 |
+
return ret;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
CUTLASS_DEVICE float ld_shared(const float* ptr) {
|
| 112 |
+
float ret;
|
| 113 |
+
asm volatile("ld.shared.f32 %0, [%1];" : "=f"(ret) : "l"(__cvta_generic_to_shared(ptr)));
|
| 114 |
+
return ret;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
CUTLASS_DEVICE void st_shared(const float* ptr, float val) {
|
| 118 |
+
asm volatile("st.shared.f32 [%0], %1;" :: "l"(__cvta_generic_to_shared(ptr)), "f"(val));
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
CUTLASS_DEVICE void st_shared(const float2* ptr, float2 val) {
|
| 122 |
+
asm volatile("st.shared.v2.f32 [%0], {%1, %2};" :: "l"(__cvta_generic_to_shared(ptr)), "f"(val.x), "f"(val.y));
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
CUTLASS_DEVICE void st_shared(const uint32_t* ptr, uint32_t val) {
|
| 126 |
+
asm volatile("st.shared.u32 [%0], %1;" :: "l"(__cvta_generic_to_shared(ptr)), "r"(val));
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
CUTLASS_DEVICE void st_shared(const void* ptr, uint32_t x, uint32_t y) {
|
| 130 |
+
asm volatile("st.shared.v2.u32 [%0], {%1, %2};" :: "l"(__cvta_generic_to_shared(ptr)), "r"(x), "r"(y));
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
CUTLASS_DEVICE void st_shared(const void* ptr, uint32_t x, uint32_t y, uint32_t z, uint32_t w) {
|
| 134 |
+
asm volatile("st.shared.v4.u32 [%0], {%1, %2, %3, %4};" :: "l"(__cvta_generic_to_shared(ptr)), "r"(x), "r"(y), "r"(z), "r"(w));
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
CUTLASS_DEVICE void st_shared(const __int128_t* ptr, __int128_t val) {
|
| 138 |
+
asm volatile("st.shared.b128 [%0], %1;" :: "l"(__cvta_generic_to_shared(ptr)), "q"(val));
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
CUTLASS_DEVICE void st_shared_bulk(void* smem_ptr, const uint32_t& num_bytes) {
|
| 142 |
+
// `size` must be 64-bit before PTX ISA 9.0
|
| 143 |
+
asm volatile("st.bulk.weak.shared::cta [%0], %1, 0;" ::
|
| 144 |
+
"l"(__cvta_generic_to_shared(smem_ptr)), "l"(static_cast<uint64_t>(num_bytes)));
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
/// Global memory
|
| 148 |
+
CUTLASS_DEVICE uint64_t ld_volatile(const uint64_t* ptr) {
|
| 149 |
+
uint64_t ret;
|
| 150 |
+
asm volatile("ld.volatile.global.b64 %0, [%1];" : "=l"(ret) : "l"(ptr));
|
| 151 |
+
return ret;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
CUTLASS_DEVICE uint32_t ld_acq(const uint32_t* ptr) {
|
| 155 |
+
uint32_t ret;
|
| 156 |
+
asm volatile("ld.acquire.gpu.global.b32 %0, [%1];" : "=r"(ret) : "l"(ptr));
|
| 157 |
+
return ret;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
CUTLASS_DEVICE uint64_t ld_acq_sys(const uint64_t* ptr) {
|
| 161 |
+
uint64_t ret;
|
| 162 |
+
asm volatile("ld.acquire.sys.global.b64 %0, [%1];" : "=l"(ret) : "l"(ptr));
|
| 163 |
+
return ret;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
CUTLASS_DEVICE void st_relaxed_sys(const uint64_t* ptr, const uint64_t& value) {
|
| 167 |
+
asm volatile("st.L1::no_allocate.relaxed.sys.global.u64 [%0], %1;" :: "l"(ptr), "l"(value));
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
/// Atomics
|
| 171 |
+
CUTLASS_DEVICE uint64_t atomic_add(const uint64_t* ptr, const uint64_t& value) {
|
| 172 |
+
uint64_t ret;
|
| 173 |
+
asm volatile("atom.global.add.u64 %0, [%1], %2;" : "=l"(ret) : "l"(ptr), "l"(value));
|
| 174 |
+
return ret;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
CUTLASS_DEVICE uint64_t atomic_add_sys(const uint64_t* ptr, const uint64_t& value) {
|
| 178 |
+
uint64_t ret;
|
| 179 |
+
asm volatile("atom.sys.global.add.u64 %0, [%1], %2;" : "=l"(ret) : "l"(ptr), "l"(value));
|
| 180 |
+
return ret;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
CUTLASS_DEVICE uint32_t atomic_add_rel(const uint32_t* ptr, const uint32_t& value) {
|
| 184 |
+
uint32_t ret;
|
| 185 |
+
asm volatile("atom.release.gpu.global.add.u32 %0, [%1], %2;" : "=r"(ret) : "l"(ptr), "r"(value));
|
| 186 |
+
return ret;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
CUTLASS_DEVICE void red_add(const int* ptr, const int& value) {
|
| 190 |
+
asm volatile("red.gpu.global.add.s32 [%0], %1;" :: "l"(ptr), "r"(value));
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
CUTLASS_DEVICE void red_add(const uint32_t* ptr, const uint32_t& value) {
|
| 194 |
+
asm volatile("red.gpu.global.add.u32 [%0], %1;" :: "l"(ptr), "r"(value));
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
CUTLASS_DEVICE void red_or_rel_sys(const uint64_t* ptr, const uint64_t& value) {
|
| 198 |
+
asm volatile("red.release.sys.global.or.b64 [%0], %1;" :: "l"(ptr), "l"(value));
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
CUTLASS_DEVICE void red_or_rel_gpu(uint64_t* ptr, const uint64_t& value) {
|
| 202 |
+
asm volatile("red.release.gpu.global.or.b64 [%0], %1;" :: "l"(ptr), "l"(value));
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
CUTLASS_DEVICE void red_add_rel(const uint32_t* ptr, const uint32_t& value) {
|
| 206 |
+
asm volatile("red.release.gpu.global.add.u32 [%0], %1;" :: "l"(ptr), "r"(value));
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
CUTLASS_DEVICE void red_add_rel_sys(const int* ptr, const int& value) {
|
| 210 |
+
asm volatile("red.release.sys.global.add.s32 [%0], %1;" :: "l"(ptr), "r"(value));
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
CUTLASS_DEVICE int ld_acq_sys(const int* ptr) {
|
| 214 |
+
int ret;
|
| 215 |
+
asm volatile("ld.acquire.sys.global.s32 %0, [%1];" : "=r"(ret) : "l"(ptr));
|
| 216 |
+
return ret;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
CUTLASS_DEVICE uint32_t ld_acq_sys(const uint32_t* ptr) {
|
| 220 |
+
uint32_t ret;
|
| 221 |
+
asm volatile("ld.acquire.sys.global.u32 %0, [%1];" : "=r"(ret) : "l"(ptr));
|
| 222 |
+
return ret;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
CUTLASS_DEVICE uint64_t ld_acq_gpu(const uint64_t* ptr) {
|
| 226 |
+
uint64_t ret;
|
| 227 |
+
asm volatile("ld.acquire.gpu.global.u64 %0, [%1];" : "=l"(ret) : "l"(ptr));
|
| 228 |
+
return ret;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
/// Predicated loads
|
| 232 |
+
CUTLASS_DEVICE longlong4_t ld_gez_pred(const longlong4_t* ptr, const int& pred) {
|
| 233 |
+
longlong4_t ret = make_longlong4_t(0, 0, 0, 0);
|
| 234 |
+
asm volatile(
|
| 235 |
+
"{\n\t"
|
| 236 |
+
" .reg .pred p;\n\t"
|
| 237 |
+
" setp.ge.s32 p, %5, 0;\n\t"
|
| 238 |
+
" @p ld.global.L2::256B.v4.s64 {%0, %1, %2, %3}, [%4];\n\t"
|
| 239 |
+
"}"
|
| 240 |
+
: "+l"(ret.x), "+l"(ret.y), "+l"(ret.z), "+l"(ret.w)
|
| 241 |
+
: "l"(ptr), "r"(pred)
|
| 242 |
+
: "memory");
|
| 243 |
+
return ret;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
/// Prefetch
|
| 247 |
+
CUTLASS_DEVICE void prefetch_l1(void *ptr) {
|
| 248 |
+
asm volatile("prefetch.global.L1 [%0];" :: "l"(ptr));
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
} // namespace deep_gemm::ptx
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/tcgen05.cuh
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
namespace deep_gemm::ptx {
|
| 4 |
+
|
| 5 |
+
/// UMMA versions with relaxed assertions
|
| 6 |
+
struct SM100_MMA_F16BF16_SS {
|
| 7 |
+
CUTLASS_DEVICE static void
|
| 8 |
+
fma(uint64_t const& desc_a,
|
| 9 |
+
uint64_t const& desc_b,
|
| 10 |
+
uint32_t const& tmem_c,
|
| 11 |
+
uint32_t const& scale_c,
|
| 12 |
+
uint64_t const& desc) {
|
| 13 |
+
asm volatile(
|
| 14 |
+
"{\n\t"
|
| 15 |
+
".reg .pred p;\n\t"
|
| 16 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 17 |
+
"tcgen05.mma.cta_group::1.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 18 |
+
"}\n"
|
| 19 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 20 |
+
}
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
struct SM100_MMA_F16BF16_2x1SM_SS {
|
| 24 |
+
CUTLASS_DEVICE static void
|
| 25 |
+
fma(uint64_t const& desc_a,
|
| 26 |
+
uint64_t const& desc_b,
|
| 27 |
+
uint32_t const& tmem_c,
|
| 28 |
+
uint32_t const& scale_c,
|
| 29 |
+
uint64_t const& desc) {
|
| 30 |
+
asm volatile(
|
| 31 |
+
"{\n\t"
|
| 32 |
+
".reg .pred p;\n\t"
|
| 33 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 34 |
+
"tcgen05.mma.cta_group::2.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 35 |
+
"}\n"
|
| 36 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 37 |
+
}
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
struct SM100_MMA_MXF8F6F4_SS {
|
| 41 |
+
CUTLASS_DEVICE static void
|
| 42 |
+
fma(uint64_t const& desc_a,
|
| 43 |
+
uint64_t const& desc_b,
|
| 44 |
+
uint32_t const& tmem_c,
|
| 45 |
+
uint32_t const& scale_c,
|
| 46 |
+
uint64_t const& desc,
|
| 47 |
+
uint32_t const& tmem_sfa,
|
| 48 |
+
uint32_t const& tmem_sfb) {
|
| 49 |
+
asm volatile(
|
| 50 |
+
"{\n\t"
|
| 51 |
+
".reg .pred p;\n\t"
|
| 52 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 53 |
+
"tcgen05.mma.cta_group::1.kind::mxf8f6f4.block_scale [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 54 |
+
"}\n"
|
| 55 |
+
:
|
| 56 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c),
|
| 57 |
+
"r"(tmem_sfa), "r"(tmem_sfb));
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
struct SM100_MMA_MXF8F6F4_2x1SM_SS {
|
| 62 |
+
CUTLASS_DEVICE static void
|
| 63 |
+
fma(uint64_t const& desc_a,
|
| 64 |
+
uint64_t const& desc_b,
|
| 65 |
+
uint32_t const& tmem_c,
|
| 66 |
+
uint32_t const& scale_c,
|
| 67 |
+
uint64_t const& desc,
|
| 68 |
+
uint32_t const& tmem_sfa,
|
| 69 |
+
uint32_t const& tmem_sfb) {
|
| 70 |
+
asm volatile(
|
| 71 |
+
"{\n\t"
|
| 72 |
+
".reg .pred p;\n\t"
|
| 73 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 74 |
+
"tcgen05.mma.cta_group::2.kind::mxf8f6f4.block_scale [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 75 |
+
"}\n"
|
| 76 |
+
:
|
| 77 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c),
|
| 78 |
+
"r"(tmem_sfa), "r"(tmem_sfb));
|
| 79 |
+
}
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
struct SM100_MMA_F8F6F4_SS {
|
| 83 |
+
CUTLASS_DEVICE static void
|
| 84 |
+
fma(uint64_t const& desc_a,
|
| 85 |
+
uint64_t const& desc_b,
|
| 86 |
+
uint32_t const& tmem_c,
|
| 87 |
+
uint32_t const& scale_c,
|
| 88 |
+
uint64_t const& desc) {
|
| 89 |
+
asm volatile(
|
| 90 |
+
"{\n\t"
|
| 91 |
+
".reg .pred p;\n\t"
|
| 92 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 93 |
+
"tcgen05.mma.cta_group::1.kind::f8f6f4 [%0], %1, %2, %3, p; \n\t"
|
| 94 |
+
"}\n"
|
| 95 |
+
:
|
| 96 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 97 |
+
}
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
struct SM100_MMA_F8F6F4_2x1SM_SS {
|
| 101 |
+
CUTLASS_DEVICE static void
|
| 102 |
+
fma(uint64_t const& desc_a,
|
| 103 |
+
uint64_t const& desc_b,
|
| 104 |
+
uint32_t const& tmem_c,
|
| 105 |
+
uint32_t const& scale_c,
|
| 106 |
+
uint64_t const& desc) {
|
| 107 |
+
asm volatile(
|
| 108 |
+
"{\n\t"
|
| 109 |
+
".reg .pred p;\n\t"
|
| 110 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 111 |
+
"tcgen05.mma.cta_group::2.kind::f8f6f4 [%0], %1, %2, %3, p; \n\t"
|
| 112 |
+
"}\n"
|
| 113 |
+
:
|
| 114 |
+
: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 115 |
+
}
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
struct SM100_MMA_MXF4_SS {
|
| 119 |
+
CUTLASS_DEVICE static void
|
| 120 |
+
fma(uint64_t const& desc_a,
|
| 121 |
+
uint64_t const& desc_b,
|
| 122 |
+
uint32_t const& tmem_c,
|
| 123 |
+
uint32_t const& scale_c,
|
| 124 |
+
uint64_t const& desc,
|
| 125 |
+
uint32_t const& tmem_sfa,
|
| 126 |
+
uint32_t const& tmem_sfb) {
|
| 127 |
+
asm volatile(
|
| 128 |
+
"{\n\t"
|
| 129 |
+
".reg .pred p;\n\t"
|
| 130 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 131 |
+
#if (__CUDACC_VER_MAJOR__ > 12) || (__CUDACC_VER_MAJOR__ == 12 && __CUDACC_VER_MINOR__ >= 9)
|
| 132 |
+
"tcgen05.mma.cta_group::1.kind::mxf4.block_scale.block32 [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 133 |
+
#else
|
| 134 |
+
"tcgen05.mma.cta_group::1.kind::mxf4.block_scale.scale_vec::2X [%0], %1, %2, %3, [%5], [%6], p; \n\t"
|
| 135 |
+
#endif
|
| 136 |
+
"}\n"
|
| 137 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c),
|
| 138 |
+
"r"(tmem_sfa), "r"(tmem_sfb));
|
| 139 |
+
}
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
+
struct SM100_MMA_F16BF16_WS_SS {
|
| 143 |
+
CUTLASS_DEVICE static void
|
| 144 |
+
fma(uint64_t const& desc_a,
|
| 145 |
+
uint64_t const& desc_b,
|
| 146 |
+
uint32_t const& tmem_c,
|
| 147 |
+
uint32_t const& scale_c,
|
| 148 |
+
uint64_t const& desc) {
|
| 149 |
+
asm volatile(
|
| 150 |
+
"{\n\t"
|
| 151 |
+
".reg .pred p;\n\t"
|
| 152 |
+
"setp.ne.b32 p, %4, 0;\n\t"
|
| 153 |
+
"tcgen05.mma.ws.cta_group::1.kind::f16 [%0], %1, %2, %3, p; \n\t"
|
| 154 |
+
"}\n"
|
| 155 |
+
:: "r"(tmem_c), "l"(desc_a), "l"(desc_b), "r"(static_cast<uint32_t>(desc >> 32)), "r"(scale_c));
|
| 156 |
+
}
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
/// Tensor memory operations
|
| 160 |
+
CUTLASS_DEVICE void tcgen05_before_thread_sync() {
|
| 161 |
+
asm volatile("tcgen05.fence::before_thread_sync;");
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
CUTLASS_DEVICE void tcgen05_after_thread_sync() {
|
| 165 |
+
asm volatile("tcgen05.fence::after_thread_sync;");
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
} // namespace deep_gemm::ptx
|
build/torch211-cu128-aarch64-linux/include/deep_gemm/ptx/tma.cuh
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cutlass/arch/barrier.h>
|
| 4 |
+
#include <cute/arch/copy_sm90_desc.hpp>
|
| 5 |
+
|
| 6 |
+
namespace deep_gemm::ptx {
|
| 7 |
+
|
| 8 |
+
// Tensor-map instructions
|
| 9 |
+
CUTLASS_DEVICE void tensor_map_release_gpu() {
|
| 10 |
+
asm volatile ("fence.proxy.tensormap::generic.release.gpu;" ::: "memory");
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
CUTLASS_DEVICE void tensor_map_acquire_gpu(const cute::TmaDescriptor* gmem_desc_ptr) {
|
| 14 |
+
auto gmem_int_desc = reinterpret_cast<uint64_t>(gmem_desc_ptr);
|
| 15 |
+
asm volatile ("fence.proxy.tensormap::generic.acquire.gpu [%0], 128;" :: "l"(gmem_int_desc) : "memory");
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
CUTLASS_DEVICE void tensor_map_replace_global_addr_in_smem(cute::TmaDescriptor* smem_desc, const void* new_addr) {
|
| 19 |
+
auto smem_int_desc = static_cast<uint32_t>(__cvta_generic_to_shared(smem_desc));
|
| 20 |
+
const auto new_int64_addr = reinterpret_cast<uint64_t>(new_addr);
|
| 21 |
+
asm volatile ("tensormap.replace.tile.global_address.shared::cta.b1024.b64 [%0], %1;" :: "r"(smem_int_desc), "l"(new_int64_addr));
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
CUTLASS_DEVICE void tensor_map_replace_global_inner_dim_stride_in_smem(cute::TmaDescriptor* smem_desc, const uint32_t& new_dim, const uint64_t& new_stride) {
|
| 25 |
+
auto smem_int_desc = __cvta_generic_to_shared(smem_desc);
|
| 26 |
+
asm volatile ("tensormap.replace.tile.global_dim.shared::cta.b1024.b32 [%0], 0, %1;" :: "l"(smem_int_desc), "r"(new_dim));
|
| 27 |
+
#if ((__CUDACC_VER_MAJOR__ > 12) or ((__CUDACC_VER_MAJOR__ == 12) and (__CUDACC_VER_MINOR__ >= 3)))
|
| 28 |
+
asm volatile("tensormap.replace.tile.global_stride.shared::cta.b1024.b64 [%0], 0, %1;" :: "l"(smem_int_desc), "l"(new_stride));
|
| 29 |
+
#else
|
| 30 |
+
DG_STATIC_ASSERT(false, "Invalid CUDA version");
|
| 31 |
+
#endif
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/// TMA instructions
|
| 35 |
+
CUTLASS_DEVICE void mbarrier_arrive(
|
| 36 |
+
cutlass::arch::ClusterTransactionBarrier* ptr) {
|
| 37 |
+
asm volatile("mbarrier.arrive.shared::cta.b64 _, [%0]; \n\t" ::
|
| 38 |
+
"r"(static_cast<uint32_t>(__cvta_generic_to_shared(ptr))));
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
CUTLASS_DEVICE void mbarrier_arrive_and_set_tx(
|
| 42 |
+
cutlass::arch::ClusterTransactionBarrier* ptr, const uint32_t& num_bytes) {
|
| 43 |
+
asm volatile("mbarrier.arrive.expect_tx.shared::cta.b64 _, [%1], %0; \n\t" ::
|
| 44 |
+
"r"(num_bytes), "r"(static_cast<uint32_t>(__cvta_generic_to_shared(ptr))));
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
CUTLASS_DEVICE void mbarrier_wait_and_flip_phase(
|
| 48 |
+
cutlass::arch::ClusterTransactionBarrier* ptr, uint32_t& phase) {
|
| 49 |
+
asm volatile(
|
| 50 |
+
"{\n\t"
|
| 51 |
+
".reg .pred P1; \n\t"
|
| 52 |
+
"LAB_WAIT: \n\t"
|
| 53 |
+
"mbarrier.try_wait.parity.shared::cta.b64 P1, [%0], %1, %2; \n\t"
|
| 54 |
+
"@P1 bra DONE; \n\t"
|
| 55 |
+
"bra LAB_WAIT; \n\t"
|
| 56 |
+
"DONE: \n\t"
|
| 57 |
+
"}" ::
|
| 58 |
+
"r"(static_cast<uint32_t>(__cvta_generic_to_shared(ptr))),
|
| 59 |
+
"r"(phase), "r"(0x989680));
|
| 60 |
+
phase ^= 1;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
CUTLASS_DEVICE void tma_load_1d(
|
| 64 |
+
const void* dst_ptr, const void* src_ptr,
|
| 65 |
+
cutlass::arch::ClusterTransactionBarrier* mbarrier_ptr,
|
| 66 |
+
const uint32_t& num_bytes,
|
| 67 |
+
const cute::TMA::CacheHintSm90& hint = cute::TMA::CacheHintSm90::EVICT_FIRST) {
|
| 68 |
+
// NOTES: normally, the loaded part will be evicted soon
|
| 69 |
+
asm volatile(
|
| 70 |
+
"cp.async.bulk.shared::cluster.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;\n" ::
|
| 71 |
+
"r"(static_cast<uint32_t>(__cvta_generic_to_shared(dst_ptr))),
|
| 72 |
+
"l"(src_ptr),
|
| 73 |
+
"r"(num_bytes),
|
| 74 |
+
"r"(static_cast<uint32_t>(__cvta_generic_to_shared(mbarrier_ptr))),
|
| 75 |
+
"l"(hint)
|
| 76 |
+
: "memory");
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
CUTLASS_DEVICE void tma_store_1d(
|
| 80 |
+
const void* dst_ptr, const void* src_ptr, const uint32_t& num_bytes,
|
| 81 |
+
const cute::TMA::CacheHintSm90& hint = cute::TMA::CacheHintSm90::EVICT_NORMAL) {
|
| 82 |
+
// NOTES: normally, the stored part will be used soon
|
| 83 |
+
asm volatile("cp.async.bulk.global.shared::cta.bulk_group.L2::cache_hint [%0], [%1], %2, %3;\n" ::
|
| 84 |
+
"l"(dst_ptr),
|
| 85 |
+
"r"(static_cast<uint32_t>(__cvta_generic_to_shared(src_ptr))),
|
| 86 |
+
"r"(num_bytes),
|
| 87 |
+
"l"(hint)
|
| 88 |
+
: "memory");
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
template <int kNumRemainingWaits = 0>
|
| 92 |
+
__forceinline__ __device__ void tma_store_wait() {
|
| 93 |
+
// NOTES: this function does not have `.read`
|
| 94 |
+
asm volatile("cp.async.bulk.wait_group %0;" ::"n"(kNumRemainingWaits) : "memory");
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
CUTLASS_DEVICE
|
| 98 |
+
void tma_gather4(const void* desc_ptr, cutlass::arch::ClusterTransactionBarrier& mbarrier,
|
| 99 |
+
void* smem_ptr, const uint32_t& col_idx, const int4& row_idxs, const uint64_t& cache_hint) {
|
| 100 |
+
const auto smem_addr = cute::cast_smem_ptr_to_uint(smem_ptr);
|
| 101 |
+
const auto mbarrier_addr = cute::cast_smem_ptr_to_uint(&mbarrier);
|
| 102 |
+
asm volatile(
|
| 103 |
+
"cp.async.bulk.tensor.2d.shared::cta.global.tile::gather4.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint [%0], [%1, {%2, %3, %4, %5, %6}], [%7], %8;\n"
|
| 104 |
+
:
|
| 105 |
+
: "r"(smem_addr), "l"(desc_ptr), "r"(col_idx),
|
| 106 |
+
"r"(row_idxs.x), "r"(row_idxs.y), "r"(row_idxs.z), "r"(row_idxs.w),
|
| 107 |
+
"r"(mbarrier_addr), "l"(cache_hint)
|
| 108 |
+
: "memory"
|
| 109 |
+
);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
} // namespace deep_gemm::ptx
|