Repository navigation
[DSA] Enable tilelang fp8_e4m3 KV cache on CUDA (raw layout) — verified on sm_121 #36904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4463,9 +4463,18 @@ def _write_mla_kv_buffer( | |
| cache_k_nope: torch.Tensor, | ||
| cache_k_rope: torch.Tensor, | ||
| ) -> None: | ||
| if _is_hip and self.use_dsa and self.dtype == fp8_dtype: | ||
| # HIP FP8 path uses raw MLA KV layout (nope + rope) without per-block scales. | ||
| if ( | ||
| self.use_dsa | ||
| and self.dtype == fp8_dtype | ||
| and not self.dsa_kv_cache_store_fp8 | ||
| ): | ||
| # Raw MLA KV layout (nope + rope cast to fp8) without per-block | ||
| # scales: the HIP DSA kernels and the CUDA TileLang fp8 path. | ||
| # Fuse BF16/FP16 -> FP8 cast with paged KV write. | ||
| if cache_k_rope is None: | ||
| cache_k_rope = cache_k_nope.new_empty( | ||
| (*cache_k_nope.shape[:-1], 0) | ||
| ) | ||
| set_mla_kv_buffer_triton_fp8_quant( | ||
| dst_buffer, | ||
| loc, | ||
|
Comment on lines
4472
to
4474
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When CUDA TileLang FP8 is combined with Useful? React with 👍 / 👎. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Execution test for the CUDA raw-fp8 tilelang DSA sparse path. | ||
|
|
||
| Ported from the probe validated on sm_121 (DGX Spark GB10); requires SM89+ | ||
| for fp8 tensor-core MMA. Two properties, each of which has been observed to | ||
| fail when the path is broken: | ||
| 1. one-hot: a single valid index per token makes softmax exactly 1.0 and | ||
| the fp8 prob-quant exact, so the kernel must return the gathered V row | ||
| nearly bit-exactly (decisive for gather/mask/normalize plumbing); | ||
| 2. spread case vs an fp32 reference computed from the SAME quantized | ||
| inputs must sit inside the analytic fp8-prob-GEMM budget (0.04), and a | ||
| scrambled-index negative control must blow past it (proves the | ||
| comparator discriminates). | ||
| """ | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| tilelang_kernel = pytest.importorskip( | ||
| "sglang.kernels.ops.attention.dsa.tilelang_kernel" | ||
| ) | ||
|
|
||
| requires_fp8_cuda = pytest.mark.skipif( | ||
| not torch.cuda.is_available() | ||
| or torch.cuda.get_device_capability() < (8, 9), | ||
| reason="needs CUDA SM89+ for fp8 tensor-core MMA", | ||
| ) | ||
|
|
||
| S, H, DV, TOPK, POOL = 4, 32, 512, 2112, 32768 # GLM-5.3-Flash NoPE geometry | ||
| SM_SCALE = DV**-0.5 | ||
|
|
||
|
|
||
| def _rel_err(a, b): | ||
| return ((a.float() - b.float()).abs().max() / b.float().abs().max()).item() | ||
|
|
||
|
|
||
| def _make_inputs(seed): | ||
| g = torch.Generator(device="cuda").manual_seed(seed) | ||
| q = torch.randn(S, H, DV, device="cuda", dtype=torch.float32, generator=g) * 0.5 | ||
| kv = torch.randn(POOL, 1, DV, device="cuda", dtype=torch.float32, generator=g) * 0.5 | ||
| idx = torch.randint(1, POOL, (S, 1, TOPK), device="cuda", generator=g).to( | ||
| torch.int32 | ||
| ) | ||
| idx[:, :, -37:] = -1 # exercise the padded-tail mask path | ||
| return q.to(torch.bfloat16), kv.to(torch.bfloat16), idx | ||
|
|
||
|
|
||
| def _ref_attn_fp32(q32, kv32, idx): | ||
| out = torch.empty(S, H, DV, device="cuda", dtype=torch.float32) | ||
| kvf = kv32.squeeze(1) | ||
| for i in range(S): | ||
| ids = idx[i, 0].long() | ||
| m = ids >= 0 | ||
| k = kvf[ids.clamp(min=0)] | ||
| logits = (q32[i] @ k.T) * SM_SCALE | ||
| logits[:, ~m] = float("-inf") | ||
| p = torch.softmax(logits, dim=-1) | ||
| out[i] = p @ k | ||
| return out | ||
|
|
||
|
|
||
| @requires_fp8_cuda | ||
| def test_fp8_one_hot_exact(): | ||
| q, kv, _ = _make_inputs(0) | ||
| kv_fp8 = kv.to(torch.float8_e4m3fn) | ||
| idx_hot = torch.full((S, 1, TOPK), -1, device="cuda", dtype=torch.int32) | ||
| hot = torch.randint(1, POOL, (S,), device="cuda") | ||
| idx_hot[:, 0, 0] = hot.to(torch.int32) | ||
| out = tilelang_kernel.tilelang_sparse_fwd(q, kv_fp8, idx_hot, SM_SCALE, d_v=DV) | ||
| torch.cuda.synchronize() | ||
| expect = kv_fp8.float().squeeze(1)[hot.long()].unsqueeze(1).expand(S, H, DV) | ||
| rel = _rel_err(out.reshape(S, H, DV), expect) | ||
| assert rel < 1e-3, f"one-hot rel err {rel:.6f} exceeds 1e-3" | ||
|
|
||
|
|
||
| @requires_fp8_cuda | ||
| def test_fp8_spread_within_budget_and_negative_control_fails(): | ||
| q, kv, idx = _make_inputs(1) | ||
| kv_fp8 = kv.to(torch.float8_e4m3fn) | ||
| out = tilelang_kernel.tilelang_sparse_fwd(q, kv_fp8, idx, SM_SCALE, d_v=DV) | ||
| torch.cuda.synchronize() | ||
| ref = _ref_attn_fp32(q.to(torch.float8_e4m3fn).float(), kv_fp8.float(), idx) | ||
| rel = _rel_err(out.reshape(S, H, DV), ref) | ||
| assert rel < 0.04, f"spread-case rel err {rel:.5f} exceeds fp8-prob budget" | ||
|
|
||
| idx_bad = idx.clone() | ||
| idx_bad[:, :, : TOPK // 2] = idx[:, :, TOPK // 2 : TOPK].flip(-1)[ | ||
| :, :, : TOPK // 2 | ||
| ] | ||
| out_bad = tilelang_kernel.tilelang_sparse_fwd( | ||
| q, kv_fp8, idx_bad, SM_SCALE, d_v=DV | ||
| ) | ||
| torch.cuda.synchronize() | ||
| rel_bad = _rel_err(out_bad.reshape(S, H, DV), ref) | ||
| assert rel_bad > 0.04, ( | ||
| "scrambled indices still matched the reference — the comparator is " | ||
| f"not discriminating (rel err {rel_bad:.5f})" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
--dcp-size > 1, both DSA forward paths request LSE during decode and target verification via_should_return_dsa_dcp_lse, but CUDA FP8 now enters this partial-and-combine branch and immediately asserts thatreturn_lseis false. Consequently, an otherwise accepted CUDA TileLang FP8 configuration aborts on its first DCP decode or verify step. The FP8 path must return LSE for cross-rank reduction, or argument validation must reject TileLang FP8 with DCP.Useful? React with 👍 / 👎.