[Bugfix][MiniMax-M3] Guard sparse decode sentinel indices (#54597)
Signed-off-by: Kyle Liang <kylliang@nvidia.com> Signed-off-by: Kyle Liang <kyleliang-nv@users.noreply.github.com> Co-authored-by: Kyle Liang <kyleliang-nv@users.noreply.github.com> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Kyle Liang
OpenAI Codex
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
972195f4f5
commit
1664c4f444
@@ -2612,6 +2612,103 @@ def test_decode_sparse_attention_correctness(
|
||||
assert error.max().item() < 1.7e-2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kv_layout", ["NHD", "HND"], indirect=True)
|
||||
@pytest.mark.parametrize("capture_graph", [False, True])
|
||||
@pytest.mark.parametrize("num_reqs", [2, 17], ids=["split-k", "single-chunk"])
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_block", [-1, 1_000_000], ids=["sentinel", "out-of-range"]
|
||||
)
|
||||
def test_decode_sparse_attention_ignores_invalid_topk(
|
||||
kv_layout: KVCacheLayout, capture_graph: bool, num_reqs: int, invalid_block: int
|
||||
):
|
||||
"""Unused top-k entries must not be dereferenced as block-table indices."""
|
||||
torch.manual_seed(0)
|
||||
decode_query_len = 4
|
||||
seq_lens = torch.full(
|
||||
(num_reqs,), decode_query_len, device="cuda", dtype=torch.int32
|
||||
)
|
||||
seq_lens[1] = 4096
|
||||
q = torch.randn(
|
||||
seq_lens.numel() * decode_query_len,
|
||||
NUM_Q_HEADS,
|
||||
HEAD_DIM,
|
||||
device="cuda",
|
||||
dtype=DTYPE,
|
||||
)
|
||||
kv_cache = _allocate_main_kv_via_contract(1, kv_layout)
|
||||
|
||||
# All logical blocks alias the single valid page. With a contiguous multi-row
|
||||
# table, row 1's raw pointer offset -1 resolves to the final element of row
|
||||
# 0. Poison that exact location so an unsafe load deterministically forms an
|
||||
# out-of-range KV-cache pointer.
|
||||
clean_block_table = torch.zeros(num_reqs, 32, device="cuda", dtype=torch.int32)
|
||||
block_table = clean_block_table.clone()
|
||||
block_table[0, -1] = 1_000_000
|
||||
topk_idx = torch.full(
|
||||
(NUM_KV_HEADS, q.shape[0], TOPK),
|
||||
-1,
|
||||
device="cuda",
|
||||
dtype=torch.int32,
|
||||
)
|
||||
topk_idx[..., 0] = 0
|
||||
# Request 1 has invalid entries both before and after its valid block.
|
||||
# Two requests produce empty split-K chunks; 17 put the full top-k in one
|
||||
# chunk, exercising neutral updates before and after a nonempty softmax.
|
||||
request_topk = topk_idx[:, decode_query_len : 2 * decode_query_len]
|
||||
request_topk.fill_(invalid_block)
|
||||
request_topk[..., TOPK // 2] = 0
|
||||
|
||||
# A valid-only run isolates sentinel handling from BF16-vs-FP32 rounding.
|
||||
# Include block 0 exactly once; replacing sentinels with 0 would duplicate
|
||||
# its contribution. The general correctness test covers reference accuracy.
|
||||
valid_topk = torch.zeros(
|
||||
(NUM_KV_HEADS, q.shape[0], 1), device="cuda", dtype=torch.int32
|
||||
)
|
||||
expected = torch.empty_like(q)
|
||||
minimax_m3_sparse_attn_decode(
|
||||
q,
|
||||
kv_cache,
|
||||
valid_topk,
|
||||
clean_block_table,
|
||||
seq_lens,
|
||||
NUM_KV_HEADS,
|
||||
SM_SCALE,
|
||||
expected,
|
||||
decode_query_len,
|
||||
)
|
||||
actual = torch.empty_like(q)
|
||||
|
||||
def run_decode() -> None:
|
||||
minimax_m3_sparse_attn_decode(
|
||||
q,
|
||||
kv_cache,
|
||||
topk_idx,
|
||||
block_table,
|
||||
seq_lens,
|
||||
NUM_KV_HEADS,
|
||||
SM_SCALE,
|
||||
actual,
|
||||
decode_query_len,
|
||||
)
|
||||
|
||||
if capture_graph:
|
||||
# An unfixed kernel faults during capture and leaves the CUDA context
|
||||
# unusable. Run negative-control reproductions in a dedicated process.
|
||||
run_decode()
|
||||
current_platform.synchronize()
|
||||
graph = torch.cuda.CUDAGraph()
|
||||
with torch.cuda.graph(graph):
|
||||
run_decode()
|
||||
for _ in range(3):
|
||||
graph.replay()
|
||||
else:
|
||||
run_decode()
|
||||
current_platform.synchronize()
|
||||
|
||||
assert torch.isfinite(actual).all()
|
||||
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
|
||||
|
||||
|
||||
def test_decode_wrong_layout_breaks_parity():
|
||||
"""Negative (AC-3/AC-5): consuming the physical HND buffer as if it were
|
||||
already contiguous-NHD (i.e. skipping the allocator's inverse permute)
|
||||
|
||||
Reference in New Issue
Block a user