[Perf][GLM-5.3-Flash] Shard long-context indexer prefill rows across TP (#54951)
Signed-off-by: Zheng Cai <8370601+zigzagcai@users.noreply.github.com> Signed-off-by: Zheng Cai <caizheng1993@gmail.com> Signed-off-by: Codex <noreply@openai.com> Co-authored-by: root <root@rjob-fb29567b88fec0a9-88c9a4c797eb2ec1-0.caizheng.ailab-sys.svc.pjlab.local> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: zigzagcai <zigzagcai@users.noreply.github.com> Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com>
This commit is contained in:
co-authored by
root
OpenAI Codex
zigzagcai
Claude Code
Wentao Ye
parent
bd5bbce6e4
commit
3e8e1364ff
@@ -7,7 +7,7 @@ import numpy as np
|
||||
import torch
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.config import VllmConfig, get_current_vllm_config
|
||||
from vllm.config import CUDAGraphMode, VllmConfig, get_current_vllm_config
|
||||
from vllm.distributed import get_dcp_group, get_pcp_group
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.warmup.jit_warmup import kernel_launcher, zip_inputs
|
||||
@@ -204,6 +204,64 @@ class PrepareUniformDecodeKernel(
|
||||
)
|
||||
|
||||
|
||||
# Below this many rows per rank, stay replicated (TP4 engages at 64K rows).
|
||||
MIN_TP_SHARD_ROWS_PER_RANK = 16_384
|
||||
|
||||
|
||||
def balanced_prefill_row_shard(
|
||||
seq_lens_cpu: torch.Tensor,
|
||||
query_lens_cpu: torch.Tensor,
|
||||
compress_ratio: int,
|
||||
tp_size: int,
|
||||
) -> list[int] | None:
|
||||
"""Return contiguous TP row counts balanced by indexer MQA work."""
|
||||
num_rows = int(query_lens_cpu.sum())
|
||||
if tp_size < 2 or num_rows < MIN_TP_SHARD_ROWS_PER_RANK * tp_size:
|
||||
return None
|
||||
|
||||
query_lens = query_lens_cpu.to(torch.int64)
|
||||
first_key = torch.repeat_interleave(
|
||||
seq_lens_cpu.to(torch.int64) - query_lens + 1, query_lens
|
||||
)
|
||||
row_in_request = torch.arange(num_rows) - torch.repeat_interleave(
|
||||
torch.cumsum(query_lens, 0) - query_lens, query_lens
|
||||
)
|
||||
cost = torch.cumsum((first_key + row_in_request) // compress_ratio, 0)
|
||||
total = int(cost[-1]) if cost.numel() else 0
|
||||
if total <= 0:
|
||||
return None
|
||||
|
||||
targets = torch.arange(1, tp_size) * total // tp_size
|
||||
bounds = [0, *torch.searchsorted(cost, targets).tolist(), num_rows]
|
||||
# Keep >= 1 row per rank (the floor above leaves ample room); this also
|
||||
# handles repeated costs from pool compression.
|
||||
for i in range(1, tp_size):
|
||||
bounds[i] = max(bounds[i], bounds[i - 1] + 1)
|
||||
for i in range(tp_size - 1, 0, -1):
|
||||
bounds[i] = min(bounds[i], bounds[i + 1] - 1)
|
||||
return [bounds[i + 1] - bounds[i] for i in range(tp_size)]
|
||||
|
||||
|
||||
def tp_prefill_row_sharding_supported(
|
||||
vllm_config: VllmConfig,
|
||||
dcp_world_size: int,
|
||||
use_pcp: bool,
|
||||
tp_size: int,
|
||||
) -> bool:
|
||||
"""Whether replicated prefill rows may be sharded across TP ranks."""
|
||||
cudagraph_mode = vllm_config.compilation_config.cudagraph_mode or CUDAGraphMode.NONE
|
||||
return (
|
||||
current_platform.is_cuda()
|
||||
and dcp_world_size == 1
|
||||
and not use_pcp
|
||||
and tp_size > 1
|
||||
and not envs.VLLM_DISABLE_PYNCCL
|
||||
and not envs.VLLM_USE_NCCL_SYMM_MEM
|
||||
and not envs.VLLM_BATCH_INVARIANT
|
||||
and cudagraph_mode.mixed_mode() != CUDAGraphMode.FULL
|
||||
)
|
||||
|
||||
|
||||
class DeepseekV32IndexerBackend(AttentionBackend):
|
||||
@classmethod
|
||||
def supports_pcp(cls) -> bool:
|
||||
@@ -654,6 +712,8 @@ class BuildPrefillChunkMetadataKernel(
|
||||
class DeepseekV32IndexerPrefillMetadata:
|
||||
chunks: list[DeepseekV32IndexerPrefillChunkMetadata]
|
||||
max_prefill_seq_len: int = -1
|
||||
# Per-TP-rank contiguous row counts, or None for the replicated path.
|
||||
row_shard_sizes: list[int] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -941,6 +1001,7 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
|
||||
scheduler_config = self.vllm_config.scheduler_config
|
||||
parallel_config = self.vllm_config.parallel_config
|
||||
self.dcp_world_size = parallel_config.decode_context_parallel_size
|
||||
self.tp_size = parallel_config.tensor_parallel_size
|
||||
self.dcp_rank = get_dcp_group().rank_in_group if self.dcp_world_size > 1 else 0
|
||||
self.pcp_world_size = parallel_config.prefill_context_parallel_size
|
||||
self.use_pcp = self.pcp_world_size > 1
|
||||
@@ -1512,6 +1573,16 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
|
||||
# Skip when total_seq_lens is 0 (i.e., no compressed token).
|
||||
if metadata is not None:
|
||||
chunks.append(metadata)
|
||||
row_shard_sizes = None
|
||||
if tp_prefill_row_sharding_supported(
|
||||
self.vllm_config, self.dcp_world_size, self.use_pcp, self.tp_size
|
||||
):
|
||||
row_shard_sizes = balanced_prefill_row_shard(
|
||||
seq_lens_cpu[num_decodes:],
|
||||
prefill_query_lens_cpu,
|
||||
self.compress_ratio,
|
||||
self.tp_size,
|
||||
)
|
||||
prefill_metadata = DeepseekV32IndexerPrefillMetadata(
|
||||
chunks,
|
||||
max_prefill_seq_len=(
|
||||
@@ -1519,6 +1590,7 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
|
||||
if num_prefills > 0
|
||||
else 0
|
||||
),
|
||||
row_shard_sizes=row_shard_sizes,
|
||||
)
|
||||
|
||||
decode_metadata = None
|
||||
|
||||
Reference in New Issue
Block a user