[Bugfix][DCP] Materialize prefill keys on non-owner ranks (#54908)
Signed-off-by: 云挚 <ningyunxiao.nyx@antgroup.com> Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
560ef78bfe
commit
19c018ec05
@@ -234,7 +234,7 @@ def test_fused_norm_rope(num_tokens: int, index_interleave: bool, mla_dtype: str
|
|||||||
assert (topk == -1).all(), "topk buffer not cleared on indexer layer"
|
assert (topk == -1).all(), "topk buffer not cleared on indexer layer"
|
||||||
|
|
||||||
|
|
||||||
def test_fused_norm_rope_normalizes_query_without_local_cache_slots():
|
def test_fused_norm_rope_materializes_qk_without_local_cache_slots():
|
||||||
"""DCP non-owner ranks still need valid query shards for query AllGather."""
|
"""DCP non-owner ranks still need valid query shards for query AllGather."""
|
||||||
torch.manual_seed(7)
|
torch.manual_seed(7)
|
||||||
dev = "cuda"
|
dev = "cuda"
|
||||||
@@ -259,6 +259,8 @@ def test_fused_norm_rope_normalizes_query_without_local_cache_slots():
|
|||||||
idx_cache = torch.zeros(1, max_pos, idx_row, device=dev, dtype=torch.uint8)
|
idx_cache = torch.zeros(1, max_pos, idx_row, device=dev, dtype=torch.uint8)
|
||||||
no_local_slots = torch.full((num_tokens,), -1, device=dev, dtype=torch.int64)
|
no_local_slots = torch.full((num_tokens,), -1, device=dev, dtype=torch.int64)
|
||||||
topk = torch.full((num_tokens, 2048), 7, device=dev, dtype=torch.int32)
|
topk = torch.full((num_tokens, 2048), 7, device=dev, dtype=torch.int32)
|
||||||
|
kv_out = torch.empty_like(kv_c)
|
||||||
|
kpe_out = torch.empty_like(k_pe)
|
||||||
|
|
||||||
q_out = K.fused_norm_rope(
|
q_out = K.fused_norm_rope(
|
||||||
pos,
|
pos,
|
||||||
@@ -283,9 +285,17 @@ def test_fused_norm_rope_normalizes_query_without_local_cache_slots():
|
|||||||
mla_k_scale=None,
|
mla_k_scale=None,
|
||||||
has_indexer=True,
|
has_indexer=True,
|
||||||
index_rope_interleave=True,
|
index_rope_interleave=True,
|
||||||
|
kv_c_out=kv_out,
|
||||||
|
k_pe_out=kpe_out,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_bf16(q_out, rms_norm(q_c, qw), "q_c rmsnorm without local cache slots")
|
assert_bf16(q_out, rms_norm(q_c, qw), "q_c rmsnorm without local cache slots")
|
||||||
|
assert_bf16(kv_out, rms_norm(kv_c, kvw), "kv_c rmsnorm without local slots")
|
||||||
|
assert_bf16(
|
||||||
|
kpe_out,
|
||||||
|
rope(k_pe.float(), pos, cos_sin, interleave=True),
|
||||||
|
"k_pe RoPE without local slots",
|
||||||
|
)
|
||||||
assert not mla_cache.any(), "non-owner rank wrote the MLA KV cache"
|
assert not mla_cache.any(), "non-owner rank wrote the MLA KV cache"
|
||||||
assert not idx_cache.any(), "non-owner rank wrote the indexer KV cache"
|
assert not idx_cache.any(), "non-owner rank wrote the indexer KV cache"
|
||||||
assert (topk == -1).all(), "topk buffer not cleared on non-owner rank"
|
assert (topk == -1).all(), "topk buffer not cleared on non-owner rank"
|
||||||
|
|||||||
@@ -198,9 +198,13 @@ def _fused_norm_rope_kernel(
|
|||||||
if slot_mapping_ptr is None:
|
if slot_mapping_ptr is None:
|
||||||
if kv_out_ptr is None and kpe_out_ptr is None and index_k_out_ptr is None:
|
if kv_out_ptr is None and kpe_out_ptr is None and index_k_out_ptr is None:
|
||||||
return
|
return
|
||||||
elif tl.load(slot_mapping_ptr + tok_idx) < 0:
|
elif tl.load(slot_mapping_ptr + tok_idx) < 0 and (
|
||||||
# Padding, or (under DCP) a token whose KV slot another rank owns:
|
pid != 1 or (kv_out_ptr is None and kpe_out_ptr is None)
|
||||||
# skip the K-side norms and cache writes.
|
):
|
||||||
|
# Padding, or (under DCP) a token whose KV slot another rank owns.
|
||||||
|
# Dense prefill still consumes the materialized normalized/rotated K
|
||||||
|
# rows on every rank, so pid 1 must produce them even when this rank
|
||||||
|
# must not write the owner-local cache slot.
|
||||||
return
|
return
|
||||||
|
|
||||||
if pid == 1:
|
if pid == 1:
|
||||||
@@ -250,6 +254,8 @@ def _fused_norm_rope_kernel(
|
|||||||
return
|
return
|
||||||
|
|
||||||
slot_idx = tl.load(slot_mapping_ptr + tok_idx)
|
slot_idx = tl.load(slot_mapping_ptr + tok_idx)
|
||||||
|
if slot_idx < 0:
|
||||||
|
return
|
||||||
mla_block_size = MLA_CACHE_BLOCK_SIZE
|
mla_block_size = MLA_CACHE_BLOCK_SIZE
|
||||||
mla_block_idx = slot_idx // mla_block_size
|
mla_block_idx = slot_idx // mla_block_size
|
||||||
mla_block_off = slot_idx % mla_block_size
|
mla_block_off = slot_idx % mla_block_size
|
||||||
|
|||||||
Reference in New Issue
Block a user