Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 138 additions & 91 deletions vllm/model_executor/layers/sparse_attn_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,30 @@ def _get_dcp_warmup_params() -> tuple[int, int, int]:
return dcp_world_size, dcp_rank, cp_kv_cache_interleave_size


def _sync_dcp_warmup() -> None:
"""Finish one-time DCP warmup on every rank before graph warmup proceeds."""
if current_platform.is_cuda():
torch.cuda.synchronize()

try:
from vllm.distributed.parallel_state import get_dcp_group

dcp_group = get_dcp_group()
if int(dcp_group.world_size) <= 1:
return
communicator = getattr(dcp_group, "device_communicator", None)
device_group = getattr(communicator, "device_group", None)
if device_group is not None:
import torch.distributed as dist

dist.barrier(group=device_group)
except Exception:
return
finally:
if current_platform.is_cuda():
torch.cuda.synchronize()


def _prewarm_b12x_dcp_topk_merge(
*,
q_rows: int,
Expand All @@ -439,73 +463,30 @@ def _prewarm_b12x_dcp_topk_merge(
if dcp_world_size <= 1 or topk_tokens <= 0:
return

rows = max(1, int(q_rows))
topk = int(topk_tokens)
world_size = int(dcp_world_size)
candidate_width = world_size * topk
(
candidates,
gathered_candidates,
candidate_indices,
candidate_score_bits,
candidate_lengths,
) = current_workspace_manager().get_simultaneous(
((rows, 2, topk), torch.int32),
((rows * world_size, 2, topk), torch.int32),
((rows, candidate_width), torch.int32),
((rows, candidate_width), torch.int32),
((rows,), torch.int32),
)
if not _use_triton_dcp_remap(candidate_indices):
return

from b12x.attention.indexer.tiled_topk import run_row_topk

from vllm.v1.attention.backends.mla.sparse_utils import (
triton_convert_dcp_local_topk_to_global,
triton_gather_topk_ids_by_position,
)

topk_indices = torch.empty((rows, topk), dtype=torch.int32, device=device)
topk_scores = torch.empty((rows, topk), dtype=torch.float32, device=device)
topk_indices.copy_(
torch.arange(topk, dtype=torch.int32, device=device).expand(rows, topk)
)
topk_scores.zero_()
triton_convert_dcp_local_topk_to_global(
topk_indices,
topk_scores,
dcp_world_size=world_size,
dcp_rank=int(dcp_rank),
cp_kv_cache_interleave_size=int(cp_kv_cache_interleave_size),
)

candidates[:, 0, :].copy_(topk_indices)
candidates[:, 1, :].copy_(topk_scores.view(torch.int32))
gathered_view = gathered_candidates.view(world_size, rows, 2, topk)
for rank in range(world_size):
gathered_view[rank].copy_(candidates)
_unpack_b12x_dcp_gathered_candidates(
gathered_candidates,
candidate_indices,
candidate_score_bits,
dcp_world_size=world_size,
topk_tokens=topk,
)

candidate_lengths.fill_(candidate_width)
run_row_topk(
row_logits=candidate_score_bits.view(torch.float32),
lengths=candidate_lengths,
topk=topk,
output_values=topk_scores,
output_indices=topk_indices,
)
triton_gather_topk_ids_by_position(
candidate_indices,
topk_indices,
topk_indices,
)
# Graph warmup later exercises both small decode rows and prefill chunks.
# Use the real runtime merge path here, including DCP all-gather, so Triton
# and b12x row-topk kernels are loaded before rank-sensitive attention
# collectives begin.
warmup_rows = (1, 2, 4, max(1, int(q_rows)))
seen_rows: set[int] = set()
base_ids = torch.arange(topk, dtype=torch.int32, device=device)
for rows in warmup_rows:
rows = int(rows)
if rows in seen_rows:
continue
seen_rows.add(rows)
topk_indices = base_ids.expand(rows, topk).clone()
topk_scores = torch.zeros((rows, topk), dtype=torch.float32, device=device)
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk,
dcp_world_size=int(dcp_world_size),
dcp_rank=int(dcp_rank),
cp_kv_cache_interleave_size=int(cp_kv_cache_interleave_size),
)
_sync_dcp_warmup()


def _dcp_global_topk_remap(
Expand Down Expand Up @@ -923,6 +904,35 @@ def _merge_b12x_dcp_topk(
)


def _convert_b12x_dcp_local_topk_to_global(
*,
topk_indices: torch.Tensor,
topk_scores: torch.Tensor | None,
dcp_world_size: int,
dcp_rank: int,
cp_kv_cache_interleave_size: int,
) -> None:
if dcp_world_size <= 1 or topk_indices.numel() == 0:
return
if topk_scores is None:
raise RuntimeError(
"B12X sparse indexer DCP requires a topk_scores_buffer for "
"local-to-global candidate conversion."
)

from vllm.v1.attention.backends.mla.sparse_utils import (
triton_convert_dcp_local_topk_to_global,
)

triton_convert_dcp_local_topk_to_global(
topk_indices,
topk_scores,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)


def _prewarm_b12x_paged_indexer_prefill(
*,
q_quant: torch.Tensor,
Expand Down Expand Up @@ -1383,15 +1393,34 @@ def sparse_attn_indexer(
]
if chunk.total_seq_lens <= 0:
topk_indices.fill_(-1)
if dcp_global_topk and not use_b12x_indexer:
_dcp_global_topk_remap(
topk_indices,
None,
topk_tokens,
dcp_rank,
dcp_world_size,
cp_kv_cache_interleave_size,
)
if dcp_global_topk:
if use_b12x_indexer:
if topk_scores_buffer is None:
raise RuntimeError(
"B12X sparse indexer DCP requires topk_scores_buffer."
)
topk_scores = topk_scores_buffer[
chunk.token_start : chunk.token_end, :topk_tokens
]
topk_scores.fill_(-float("inf"))
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk_tokens,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
topk_indices.masked_fill_(topk_scores == -float("inf"), -1)
else:
_dcp_global_topk_remap(
topk_indices,
None,
topk_tokens,
dcp_rank,
dcp_world_size,
cp_kv_cache_interleave_size,
)
continue

if use_b12x_indexer:
Expand Down Expand Up @@ -1431,14 +1460,23 @@ def sparse_attn_indexer(
topk_scores=topk_scores,
shared_page_table=True,
)
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk_tokens,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
if dcp_global_topk:
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk_tokens,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
else:
_convert_b12x_dcp_local_topk_to_global(
topk_indices=topk_indices,
topk_scores=topk_scores,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
topk_indices.masked_fill_(row_has_no_kv[:, None], -1)
continue

Expand Down Expand Up @@ -1574,14 +1612,23 @@ def sparse_attn_indexer(
topk_tokens=topk_tokens,
topk_scores=topk_scores,
)
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk_tokens,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
if dcp_global_topk:
_merge_b12x_dcp_topk(
topk_indices=topk_indices,
topk_scores=topk_scores,
topk_tokens=topk_tokens,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
else:
_convert_b12x_dcp_local_topk_to_global(
topk_indices=topk_indices,
topk_scores=topk_scores,
dcp_world_size=dcp_world_size,
dcp_rank=dcp_rank,
cp_kv_cache_interleave_size=cp_kv_cache_interleave_size,
)
return topk_indices_buffer

schedule_metadata = decode_metadata.schedule_metadata
Expand Down
14 changes: 14 additions & 0 deletions vllm/model_executor/models/deepseek_mtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
GroupShape,
scaled_dequantize,
)
from vllm.model_executor.layers.sparse_attn_indexer import use_b12x_sparse_indexer
from vllm.model_executor.layers.vocab_parallel_embedding import (
ParallelLMHead,
VocabParallelEmbedding,
Expand Down Expand Up @@ -321,8 +322,20 @@ def __init__(self, vllm_config: VllmConfig, prefix: str) -> None:
dtype=torch.int32,
device=self.device,
)
topk_scores_buffer = None
if (
vllm_config.parallel_config.decode_context_parallel_size > 1
and use_b12x_sparse_indexer()
):
topk_scores_buffer = torch.empty(
vllm_config.scheduler_config.max_num_batched_tokens,
topk_tokens,
dtype=torch.float32,
device=self.device,
)
else:
topk_indices_buffer = None
topk_scores_buffer = None

self.shared_head = SharedHead(
config=config, prefix=prefix, quant_config=quant_config
Expand All @@ -332,6 +345,7 @@ def __init__(self, vllm_config: VllmConfig, prefix: str) -> None:
prefix,
config=self.config,
topk_indices_buffer=topk_indices_buffer,
topk_scores_buffer=topk_scores_buffer,
quant_config=quant_config,
layer_idx_override=0,
is_nextn=True,
Expand Down
18 changes: 18 additions & 0 deletions vllm/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CompletionOutput:
output text.
logprobs: The log probabilities of the top probability words at each
position if the logprobs are requested.
sample_logits: Raw logits for generated positions when requested through
``SamplingParams.return_sample_logits``.
finish_reason: The reason why the sequence is finished.
stop_reason: The stop string or token id that caused the completion
to stop, None if the completion finished for some other reason
Expand All @@ -42,6 +44,7 @@ class CompletionOutput:
token_ids: GenericSequence[int]
cumulative_logprob: float | None
logprobs: SampleLogprobs | None
sample_logits: torch.Tensor | None = None
routed_experts: np.ndarray | None = None # [seq_len,layer_num,topk]
finish_reason: str | None = None
stop_reason: int | str | None = None
Expand Down Expand Up @@ -94,6 +97,8 @@ class RequestOutput:
For encoder/decoder models, this is the
decoder input prompt token ids.
prompt_logprobs: The log probabilities to return per prompt token.
prompt_logits: Raw logits for prompt positions when requested through
``SamplingParams.return_prompt_logits``.
outputs: The output sequences of the request.
finished: Whether the whole request is finished.
metrics: Metrics associated with the request.
Expand All @@ -114,6 +119,7 @@ def __init__(
prompt_logprobs: PromptLogprobs | None,
outputs: list[CompletionOutput],
finished: bool,
prompt_logits: torch.Tensor | None = None,
metrics: RequestStateStats | None = None,
lora_request: LoRARequest | None = None,
encoder_prompt: str | None = None,
Expand All @@ -133,6 +139,7 @@ def __init__(
self.prompt = prompt
self.prompt_token_ids = prompt_token_ids
self.prompt_logprobs = prompt_logprobs
self.prompt_logits = prompt_logits
self.outputs = outputs
self.finished = finished
self.metrics = metrics
Expand Down Expand Up @@ -160,6 +167,17 @@ def add(self, next_output: "RequestOutput", aggregate: bool) -> None:
if next_completion.logprobs:
assert completion.logprobs is not None
completion.logprobs.extend(next_completion.logprobs) # type: ignore[arg-type]
if next_completion.sample_logits is not None:
if completion.sample_logits is None:
completion.sample_logits = next_completion.sample_logits
else:
completion.sample_logits = torch.cat(
[
completion.sample_logits,
next_completion.sample_logits,
],
dim=0,
)
completion.cumulative_logprob = (
next_completion.cumulative_logprob
)
Expand Down
Loading
Loading