|
10 | 10 | truncated at the query token's own causal extent. |
11 | 11 | """ |
12 | 12 |
|
| 13 | +import inspect |
13 | 14 | import logging |
14 | 15 | import os |
15 | 16 | from pathlib import Path |
16 | 17 | from types import SimpleNamespace |
| 18 | +from unittest.mock import Mock |
17 | 19 |
|
18 | 20 | import pytest |
19 | 21 | import torch |
|
46 | 48 | pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") |
47 | 49 |
|
48 | 50 | skip_not_sm100 = pytest.mark.skipif( |
49 | | - get_sm_version() not in (100, 103), |
| 51 | + not torch.cuda.is_available() or get_sm_version() not in (100, 103), |
50 | 52 | reason="MiniMax-M3 NVFP4 and fmha_sm100 require SM100/SM103.", |
51 | 53 | ) |
52 | 54 |
|
@@ -307,6 +309,95 @@ def _make_nvfp4_inputs(seq_lens, *, num_kv_heads=1, group=8, decode_query_len=1, |
307 | 309 | ) |
308 | 310 |
|
309 | 311 |
|
| 312 | +@pytest.mark.skipif( |
| 313 | + not torch.cuda.is_available() or get_sm_version() != 107, |
| 314 | + reason="R2 sparse-prefill store requires SM107", |
| 315 | +) |
| 316 | +def test_msa_nvfp4_r2_store_matches_reference(monkeypatch: pytest.MonkeyPatch) -> None: |
| 317 | + require_msa_module() |
| 318 | + # Optional CuTe-DSL dependency: import only after the GPU/platform gate. |
| 319 | + from fmha_sm100 import sparse as msa_sparse |
| 320 | + from src.common import aot_cache |
| 321 | + |
| 322 | + interface = inspect.getmodule(msa_sparse.sparse_atten_nvfp4_kv_func) |
| 323 | + assert interface is not None |
| 324 | + q_len, kv_len, group = 257, 513, 8 |
| 325 | + case = _make_nvfp4_inputs([kv_len], group=group, decode_query_len=q_len, seed=101) |
| 326 | + cu_q = torch.tensor([0, q_len], device="cuda", dtype=torch.int32) |
| 327 | + cu_k = torch.tensor([0, kv_len], device="cuda", dtype=torch.int32) |
| 328 | + row_ptr, q_indices, schedule = msa_sparse.build_k2q_csr( |
| 329 | + case.topk_idx, |
| 330 | + cu_q, |
| 331 | + cu_k, |
| 332 | + PAGE_SIZE, |
| 333 | + total_k=kv_len, |
| 334 | + max_seqlen_k=kv_len, |
| 335 | + max_seqlen_q=q_len, |
| 336 | + total_rows=(kv_len + PAGE_SIZE - 1) // PAGE_SIZE, |
| 337 | + qhead_per_kv=group, |
| 338 | + return_schedule=True, |
| 339 | + ) |
| 340 | + # Fewer blocks than top-k: the helper selects the entire causal prefix. |
| 341 | + # Use independently dequantized K/V, including a partial last page. |
| 342 | + pages = case.block_table[0].long() |
| 343 | + keys = case.k_ref[pages, 0].reshape(-1, HEAD_DIM)[:kv_len] |
| 344 | + values = case.v_ref[pages, 0].reshape(-1, HEAD_DIM)[:kv_len] |
| 345 | + logits = torch.einsum("qhd,kd->qhk", case.q.float(), keys) * HEAD_DIM**-0.5 |
| 346 | + causal_end = kv_len - q_len + torch.arange(q_len, device="cuda") |
| 347 | + invalid = torch.arange(kv_len, device="cuda")[None, :] > causal_end[:, None] |
| 348 | + logits.masked_fill_(invalid[:, None, :], -float("inf")) |
| 349 | + reference = torch.einsum("qhk,kd->qhd", logits.softmax(dim=-1), values) |
| 350 | + reference_lse = logits.logsumexp(dim=-1) |
| 351 | + |
| 352 | + results = [] |
| 353 | + for enabled in (False, True): |
| 354 | + with monkeypatch.context() as patch: |
| 355 | + patch.setenv("TRTLLM_MSA_DISABLE_R2STORE", "0" if enabled else "1") |
| 356 | + patch.setattr(aot_cache, "_r2_store_cache", {}) |
| 357 | + patch.setattr(aot_cache, "_build_fingerprint_cache", {}) |
| 358 | + patch.setattr(aot_cache, "_AOT_DISABLE", True) |
| 359 | + patch.setattr(interface, "_compile_cache", {}) |
| 360 | + # Observe real compilation; a cached binary must not hide either path. |
| 361 | + compile_spy = Mock(spec_set=interface.cute.compile, wraps=interface.cute.compile) |
| 362 | + patch.setattr(interface.cute, "compile", compile_spy) |
| 363 | + out, lse = msa_sparse.sparse_atten_nvfp4_kv_func( |
| 364 | + case.q, |
| 365 | + case.k_paged, |
| 366 | + case.v_paged, |
| 367 | + case.scales["k_block_scale"], |
| 368 | + case.scales["v_block_scale"], |
| 369 | + case.scales["k_global_scale"], |
| 370 | + case.scales["v_global_scale"], |
| 371 | + row_ptr, |
| 372 | + q_indices, |
| 373 | + MSA_REQUIRED_TOPK, |
| 374 | + cu_seqlens_q=cu_q, |
| 375 | + cu_seqlens_k=cu_k, |
| 376 | + max_seqlen_q=q_len, |
| 377 | + max_seqlen_k=kv_len, |
| 378 | + blk_kv=PAGE_SIZE, |
| 379 | + causal=True, |
| 380 | + softmax_scale=HEAD_DIM**-0.5, |
| 381 | + partial_dtype=torch.bfloat16, |
| 382 | + return_softmax_lse=True, |
| 383 | + page_table=case.block_table, |
| 384 | + seqused_k=case.seq_lens, |
| 385 | + schedule=schedule, |
| 386 | + ) |
| 387 | + kernels = [ |
| 388 | + call.args[0] |
| 389 | + for call in compile_spy.call_args_list |
| 390 | + if call.args |
| 391 | + and isinstance(call.args[0], interface.SparseAttentionForwardNvfp4KvSm100) |
| 392 | + ] |
| 393 | + assert kernels and all(kernel.r2_store is enabled for kernel in kernels) |
| 394 | + torch.testing.assert_close(out.float(), reference, rtol=3e-2, atol=3e-2) |
| 395 | + torch.testing.assert_close(lse, reference_lse, rtol=1e-2, atol=1e-2) |
| 396 | + results.append((out.clone(), lse.clone())) |
| 397 | + assert torch.equal(results[0][0].view(torch.int16), results[1][0].view(torch.int16)) |
| 398 | + assert torch.equal(results[0][1].view(torch.int32), results[1][1].view(torch.int32)) |
| 399 | + |
| 400 | + |
310 | 401 | def _run_nvfp4(case, **kwargs): |
311 | 402 | # An E4M3 q is widened in-register, so the output stays at compute dtype. |
312 | 403 | out_dtype = torch.bfloat16 if case.q.dtype == torch.float8_e4m3fn else case.q.dtype |
|
0 commit comments