Skip to content
Merged
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
72 changes: 72 additions & 0 deletions tests/v1/spec_decode/test_dynamic_sd_cug.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VllmConfig,
)
from vllm.v1.worker.gpu import cudagraph_utils as gpu_cudagraph_utils
from vllm.v1.worker.utils import get_uniform_decode_token_count

pytestmark = pytest.mark.cpu_test

Expand Down Expand Up @@ -196,6 +197,77 @@ def test_dynamic_sd_non_uniform_batch_falls_back_to_piecewise(monkeypatch):
assert desc.num_active_loras == 0


def test_prompt_chunks_shaped_like_spec_decode_miss_the_full_graph(monkeypatch):
"""A batch holding K+1-token prompt chunks must not replay a decode graph.

Prompt chunks of exactly K + 1 tokens give the batch a spec-decode shape
by coincidence; replaying the FULL graph over them corrupts every request
in the batch. See https://github.com/vllm-project/vllm/issues/49918.
"""

max_spec_tokens = 7
decode_query_len = max_spec_tokens + 1

monkeypatch.setattr(
gpu_cudagraph_utils,
"get_pp_group",
lambda: SimpleNamespace(is_first_rank=True, is_last_rank=True),
)

vllm_config = _create_vllm_config_for_dsd(
max_num_seqs=64,
max_spec_tokens=max_spec_tokens,
use_dynamic_sd=False,
)
manager = gpu_cudagraph_utils.CudaGraphManager(
vllm_config=vllm_config,
device=torch.device("cpu"),
cudagraph_mode=CUDAGraphMode.FULL_AND_PIECEWISE,
decode_query_len=decode_query_len,
)
manager._graphs_captured = True

num_decodes, num_chunks = 6, 2
num_reqs = num_decodes + num_chunks
num_tokens = num_reqs * decode_query_len

# The batch shape is indistinguishable from a full batch of spec decodes.
assert (
gpu_cudagraph_utils.get_uniform_token_count(
num_reqs, num_tokens, decode_query_len
)
== decode_query_len
)

# Two of the requests are decode_query_len tokens into a longer prompt.
uniform_tok_count = get_uniform_decode_token_count(
num_reqs, num_tokens, decode_query_len, has_prefill=True
)
assert uniform_tok_count is None
desc = manager.dispatch(
num_reqs=num_reqs,
num_tokens=num_tokens,
uniform_token_count=uniform_tok_count,
num_active_loras=0,
)
assert desc.cg_mode == CUDAGraphMode.PIECEWISE
assert desc.uniform_token_count is None

# The same shape with every request decoding still gets its FULL graph.
uniform_tok_count = get_uniform_decode_token_count(
num_reqs, num_tokens, decode_query_len, has_prefill=False
)
assert uniform_tok_count == decode_query_len
desc = manager.dispatch(
num_reqs=num_reqs,
num_tokens=num_tokens,
uniform_token_count=uniform_tok_count,
num_active_loras=0,
)
assert desc.cg_mode == CUDAGraphMode.FULL
assert desc.uniform_token_count == decode_query_len


def test_basic_sd_does_not_capture_shorter_full_decode_shapes(monkeypatch):
"""Without DSD, only the max decode query length should get FULL graphs.

Expand Down
151 changes: 149 additions & 2 deletions tests/v1/worker/test_gpu_batch_ordering.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Batch ordering in Model Runner V2 (vllm.v1.worker.gpu).
"""Batch ordering and classification in Model Runner V2 (vllm.v1.worker.gpu).

split_decodes_and_prefills assumes decode -> short_extend -> prefill request
ordering. With spec decode (decode_query_len > 1), a shorter chunked-prefill
tail sorted in front of the uniform decodes misclassifies every decode as a
prefill.

Conversely, a prompt chunk of exactly decode_query_len tokens has a decode
batch's shape, and must not be classified as a uniform decode batch.
"""

import ast
from pathlib import Path
from types import SimpleNamespace
from typing import Any

import numpy as np
import torch

from vllm.v1.attention.backend import CommonAttentionMetadata
from vllm.v1.attention.backends.utils import split_decodes_and_prefills
from vllm.v1.worker.gpu.model_runner import sort_batch_req_ids
from vllm.v1.worker.gpu.model_runner import GPUModelRunner, sort_batch_req_ids
from vllm.v1.worker.utils import get_uniform_decode_token_count


def _make_common_attn_metadata(query_lens: list[int]) -> CommonAttentionMetadata:
Expand All @@ -37,6 +47,72 @@ def _make_common_attn_metadata(query_lens: list[int]) -> CommonAttentionMetadata
)


def _make_runner(
req_states: dict[str, tuple[int, int]], decode_query_len: int = 8
) -> Any:
"""Build a runner stub from {req_id: (num_computed_tokens, prefill_len)}."""
prefill_lens = np.array([s[1] for s in req_states.values()], dtype=np.int32)
num_computed = np.array([s[0] for s in req_states.values()], dtype=np.int32)
runner: Any = GPUModelRunner.__new__(GPUModelRunner)
runner.decode_query_len = decode_query_len
runner.req_states = SimpleNamespace(
req_id_to_index={req_id: i for i, req_id in enumerate(req_states)},
# The runner keeps this as min(num_computed_tokens, prefill_len).
num_computed_prefill_tokens=np.minimum(num_computed, prefill_lens),
prefill_len=SimpleNamespace(np=prefill_lens),
)
return runner


def _uniform_token_count(
req_states: dict[str, tuple[int, int]],
query_len: int,
dummy_run: bool = False,
) -> int | None:
"""Classify via the runner's pre-dispatch gather, as execute_model does."""
scheduler_output = SimpleNamespace(
num_scheduled_tokens={req_id: query_len for req_id in req_states},
total_num_scheduled_tokens=query_len * len(req_states),
)
runner = _make_runner(req_states, decode_query_len=query_len)
_, uniform_tok_count = runner.gather_batch_req_state(scheduler_output, dummy_run)
return uniform_tok_count


def test_spec_decode_batch_is_uniform_decode():
# decode_query_len == 8 (K=7): every request past its prefill.
decodes = {f"d{i}": (16, 16) for i in range(6)}
assert _uniform_token_count(decodes, 8) == 8


def test_prompt_chunk_of_decode_query_len_is_not_uniform_decode():
# Two prompt chunks whose query length coincides with the K+1 spec-decode
# query length must reject the batch (issue #49918).
batch = {f"d{i}": (16, 16) for i in range(6)}
batch.update({f"p{i}": (8, 40) for i in range(2)})
assert _uniform_token_count(batch, 8) is None

# Same collision without spec decoding: 1-token prompt vs 1-token decode.
assert _uniform_token_count({"p0": (0, 1)}, 1) is None

# A fresh prompt of exactly decode_query_len tokens.
assert _uniform_token_count({"p0": (0, 8)}, 8) is None


def test_dummy_batches_stay_uniform_decode():
# Dummy batches have no request state to consult and must stay classified
# by shape alone; rejecting them would make capture and replay disagree.
scheduler_output = SimpleNamespace(
num_scheduled_tokens={f"_dummy_req_{i}": 8 for i in range(4)},
total_num_scheduled_tokens=32,
)
state, uniform_tok_count = _make_runner({}).gather_batch_req_state(
scheduler_output, True
)
assert state is None
assert uniform_tok_count == 8


def test_sort_batch_req_ids_no_spec():
# decode_query_len == 1: plain ascending order (decodes first).
num_tokens_per_req = {"p1": 100, "d1": 1, "p2": 7, "d2": 1}
Expand Down Expand Up @@ -68,3 +144,74 @@ def test_spec_decodes_lead_short_prefill_tail():
)
assert (num_decodes, num_prefills) == (8, 1)
assert (num_decode_tokens, num_prefill_tokens) == (16, 1)


def test_uniform_decode_uses_state_index_not_batch_position():
"""The gather must read each request's own state, not its batch slot.

Batch position and state index diverge as requests come and go; here the
only prefilling request sits at state index 0, so a gather keyed on batch
position would read its row and wrongly reject the two decodes.
"""
runner: Any = GPUModelRunner.__new__(GPUModelRunner)
runner.decode_query_len = 8
# State arrays in state-index order: a prefilling request, then two decodes.
runner.req_states = SimpleNamespace(
req_id_to_index={"prefilling": 0, "decode_a": 1, "decode_b": 2},
num_computed_prefill_tokens=np.array([8, 16, 16], dtype=np.int32),
prefill_len=SimpleNamespace(np=np.array([40, 16, 16], dtype=np.int32)),
)
scheduler_output = SimpleNamespace(
num_scheduled_tokens={"decode_a": 8, "decode_b": 8},
total_num_scheduled_tokens=16,
)

state, uniform_tok_count = runner.gather_batch_req_state(scheduler_output, False)
assert not state.has_prefill
assert uniform_tok_count == 8

# Scheduling the prefilling request alongside them must reject the batch.
scheduler_output.num_scheduled_tokens = {
"decode_a": 8,
"prefilling": 8,
"decode_b": 8,
}
scheduler_output.total_num_scheduled_tokens = 24
state, uniform_tok_count = runner.gather_batch_req_state(scheduler_output, False)
assert state.has_prefill
assert uniform_tok_count is None


def test_uniform_decode_predicate():
# Shape and prefill state must both pass.
assert get_uniform_decode_token_count(2, 16, 8, False) == 8
assert get_uniform_decode_token_count(2, 16, 8, True) is None
# 12 tokens over 2 requests is no shared query length.
assert get_uniform_decode_token_count(2, 12, 8, False) is None


def test_no_speculator_dispatches_on_query_length_alone():
"""No speculator may pick its cudagraph from a shape-only test.

Written over the whole package because speculators are added by copying an
existing one: the shape-only call already reappeared verbatim in a
speculator added after the original call sites were fixed.
"""
import vllm.v1.worker.gpu.spec_decode as spec_decode

shape_only = {"get_uniform_token_count", "is_uniform_query_len"}
root = Path(spec_decode.__file__).parent
offenders = []
for path in sorted(root.rglob("speculator.py")):
for node in ast.walk(ast.parse(path.read_text())):
if not isinstance(node, ast.Call):
continue
func = node.func
name = func.id if isinstance(func, ast.Name) else getattr(func, "attr", "")
if name in shape_only:
offenders.append(f"{path.relative_to(root)}:{node.lineno} {name}")

assert not offenders, (
"these speculators classify a decode batch by query length alone; use "
f"get_uniform_decode_token_count instead: {offenders}"
)
14 changes: 7 additions & 7 deletions vllm/v1/worker/gpu/cudagraph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from vllm.v1.worker.gpu.cp_utils import prepare_dcp_local_seq_lens
from vllm.v1.worker.gpu.input_batch import InputBatch, InputBuffers
from vllm.v1.worker.gpu.model_states.interface import ModelState
from vllm.v1.worker.utils import AttentionGroup
from vllm.v1.worker.utils import AttentionGroup, is_uniform_query_len

logger = init_logger(__name__)

Expand Down Expand Up @@ -90,17 +90,17 @@ def _is_compatible(


def get_uniform_token_count(
num_reqs: int,
num_tokens: int,
max_query_len: int,
num_reqs: int, num_tokens: int, max_query_len: int
) -> int | None:
"""
Return the uniform token count if batch is uniform, else None.
A batch is uniform if all requests have the same number of tokens.

Shape test only, valid for batches known to be decoding by construction
(e.g. dummy runs). Scheduled batches must use
`get_uniform_decode_token_count`.
"""
if (max_query_len == num_tokens // num_reqs) and (
num_tokens == max_query_len * num_reqs
):
if is_uniform_query_len(num_reqs, num_tokens, max_query_len):
return max_query_len
return None

Expand Down
8 changes: 4 additions & 4 deletions vllm/v1/worker/gpu/input_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class InputBatch:
num_computed_prefill_tokens_np: np.ndarray
# [num_reqs] CPU bool array == (num_computed_prefill_tokens_np < prefill_len_np).
is_prefilling_np: np.ndarray
# == np.any(is_prefilling_np)
has_prefill: bool

# [num_reqs] only populated when pipeline parallelism is enabled.
max_seq_len_np: np.ndarray | None
Expand All @@ -101,10 +103,7 @@ class InputBatch:

@classmethod
def make_dummy(
cls,
num_reqs: int,
num_tokens: int,
input_buffers: InputBuffers,
cls, num_reqs: int, num_tokens: int, input_buffers: InputBuffers
) -> "InputBatch":
assert 0 < num_reqs <= num_tokens
device = input_buffers.device
Expand Down Expand Up @@ -175,6 +174,7 @@ def make_dummy(
prefill_len_np=np.zeros(num_reqs, dtype=np.int32),
num_computed_prefill_tokens_np=np.zeros(num_reqs, dtype=np.int32),
is_prefilling_np=np.zeros(num_reqs, dtype=np.bool_),
has_prefill=False,
max_seq_len_np=None,
input_ids=input_ids,
positions=positions,
Expand Down
Loading
Loading