Merge branch 'main' into patches

Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 17:41:13 +00:00
1122 changed files with 60114 additions and 20956 deletions
+80 -2
View File
@@ -20,9 +20,13 @@ from vllm.distributed.device_communicators import pynccl_allocator
from vllm.v1.kv_cache_interface import KVCacheConfig
from vllm.v1.worker.gpu import cudagraph_utils as gpu_cudagraph_utils
from vllm.v1.worker.gpu.cudagraph_utils import BatchExecutionDescriptor
from vllm.v1.worker.gpu.dp_utils import dispatch_cg_and_sync_dp
from vllm.v1.worker.gpu.input_batch import InputBuffers
from vllm.v1.worker.gpu.pcp_manager import PCPManager
from vllm.v1.worker.gpu.spec_decode.autoregressive.cudagraph_utils import (
from vllm.v1.worker.gpu.spec_decode.target_dependent_ar import (
cudagraph_utils as spec_cudagraph_utils,
)
from vllm.v1.worker.gpu.spec_decode.target_dependent_ar.cudagraph_utils import (
SpeculatorCudaGraphManager,
)
@@ -226,8 +230,10 @@ def test_speculator_capture_preserves_decode_query_bounds(
)
manager = SpeculatorCudaGraphManager.__new__(SpeculatorCudaGraphManager)
manager.max_num_reqs = num_reqs
manager.decode_query_len = uniform_token_count or max_query_len or num_tokens
manager.vllm_config = SimpleNamespace(
parallel_config=SimpleNamespace(data_parallel_size=1)
parallel_config=SimpleNamespace(data_parallel_size=1),
num_speculative_tokens=3,
)
buffers = InputBuffers(num_reqs, num_tokens, torch.device("cpu"))
block_tables = MagicMock()
@@ -488,6 +494,78 @@ def test_dynamic_spec_decode_shared_token_count_stays_reachable(monkeypatch):
), desc
@pytest.mark.parametrize("specialize", [False, True])
def test_speculator_replays_graph_for_runtime_width(monkeypatch, specialize):
"""Equal batch shapes must replay the graph captured for the selected K."""
monkeypatch.setattr(
gpu_cudagraph_utils,
"get_pp_group",
lambda: SimpleNamespace(is_first_rank=True, is_last_rank=True),
)
monkeypatch.setattr(
gpu_cudagraph_utils.current_platform, "get_global_graph_pool", lambda: object()
)
monkeypatch.setattr(gpu_cudagraph_utils, "get_offloader", MagicMock)
monkeypatch.setattr(
spec_cudagraph_utils, "prepare_inputs_to_capture", lambda *a, **kw: ({}, {})
)
forward = MagicMock()
replayed_widths: list[int] = []
def capture(manager, create_forward_fn, progress_bar_desc):
for desc in manager._capture_descs[CUDAGraphMode.FULL]:
create_forward_fn(desc, warmup=False)(CUDAGraphMode.NONE)
k = forward.call_args.kwargs["num_speculative_steps"]
graph = MagicMock()
graph.replay.side_effect = lambda k=k: replayed_widths.append(k)
manager.graphs[desc] = graph
manager._graphs_captured = True
monkeypatch.setattr(gpu_cudagraph_utils.CudaGraphManager, "capture", capture)
manager = spec_cudagraph_utils.SpeculatorCudaGraphManager(
_create_decode_vllm_config(
capture_sizes=[4],
num_speculative_tokens=4,
dynamic_spec_schedule=[(1, 2, 4), (3, 4, 2), (5, 6, 1), (7, 8, 0)],
),
device=torch.device("cpu"),
cudagraph_mode=CUDAGraphMode.FULL_DECODE_ONLY,
decode_query_len=1,
)
manager.capture(
forward,
MagicMock(),
MagicMock(),
MagicMock(),
[],
MagicMock(),
specialize_spec_tokens=specialize,
)
captured_widths = [4, 2] if specialize else [4]
assert [c.kwargs["num_speculative_steps"] for c in forward.call_args_list] == (
captured_widths
)
for k in (2, 4, 2, 3):
desc, _ = dispatch_cg_and_sync_dp(
manager,
num_reqs=3,
num_tokens=3,
uniform_token_count=1,
dp_size=1,
dp_rank=0,
)
desc = manager.specialize_spec_tokens(desc, k)
captured_k = k if specialize else 4
if captured_k in captured_widths:
assert desc.cg_mode == CUDAGraphMode.FULL
assert desc.num_tokens == 4
manager.run_fullgraph(desc)
assert replayed_widths[-1] == captured_k
else:
assert desc.cg_mode == CUDAGraphMode.NONE
@pytest.mark.parametrize(
"decode_query_len,capture_sizes",
[(1, [1, 2, 4, 8]), (2, [2, 4, 8, 16]), (8, [8, 16, 32, 64])],
@@ -245,6 +245,138 @@ class TestFindBudgetGraph:
assert mgr.get_num_graphs_to_capture() == 3
# ---------------------------------------------------------------------------
# _execute_local greedy packing with budget-cliff deferral (no GPU required)
# ---------------------------------------------------------------------------
def _run_packing(
mgr: EncoderCudaGraphManager, specs: list[EncoderItemSpec]
) -> list[tuple[list[int], str, int]]:
"""Run _execute_local with graph replay and postprocess mocked out.
Returns:
(batch_indices, path, token_budget) for every graph replay.
"""
mgr.model = MagicMock()
runs: list[tuple[list[int], str, int]] = []
def fake_run(mm_kwargs, token_budget, path="default", axis_keys=()):
runs.append((list(mm_kwargs["indices"]), path, token_budget))
n_tokens = sum(
specs[i].get_path_output_tokens(path) for i in mm_kwargs["indices"]
)
return torch.zeros(n_tokens, 32)
def fake_postprocess(
graph_outputs,
batch_indices,
per_item_out_tokens,
outputs_by_orig_idx,
clone,
batch_mm_kwargs,
):
for i in batch_indices:
outputs_by_orig_idx[i] = torch.zeros(per_item_out_tokens[i], 32)
mgr.model.postprocess_encoder_output = fake_postprocess
with (
patch.object(mgr, "_get_item_specs", lambda mm_kwargs: specs),
patch.object(
mgr,
"_select_items",
lambda mm_kwargs, indices: ({"indices": indices}, ()),
),
patch.object(mgr, "_run_budget_graph", fake_run),
):
result = mgr._execute_local({})
assert len(result) == len(specs)
return runs
class TestExecuteLocalPacking:
"""Greedy packing defers items that cross a budget cliff."""
@staticmethod
def _spec(tokens: int) -> EncoderItemSpec:
return EncoderItemSpec(input_size=tokens, output_tokens=tokens)
def test_cliff_deferral_splits_batch(self):
# Merging would force 4200 tokens into the 8192 budget; deferring
# the 4000-token item costs 256 + 4096 instead.
mgr = _make_manager_with_budgets([256, 4096, 8192])
runs = _run_packing(mgr, [self._spec(4000), self._spec(200)])
assert runs == [([1], "default", 256), ([0], "default", 4096)]
def test_tie_does_not_defer(self):
# 4096 + 4096 ties the merged 8192 budget; merging wins because it
# saves one replay.
mgr = _make_manager_with_budgets([256, 4096, 8192])
runs = _run_packing(mgr, [self._spec(4000), self._spec(4000)])
assert runs == [([0, 1], "default", 8192)]
def test_deferral_requires_item_outweighing_batch(self):
# The third 1366-token item would save budget if deferred (4096+2048
# < 8192) but is smaller than the current batch (4096), so it merges:
# deferring mid-size items fragments flood packing into extra replays.
mgr = _make_manager_with_budgets([256, 2048, 4096, 8192])
runs = _run_packing(mgr, [self._spec(1366)] * 3)
assert runs == [([0, 1, 2], "default", 8192)]
def test_packing_without_cliff_unchanged(self):
mgr = _make_manager_with_budgets([256, 4096])
runs = _run_packing(mgr, [self._spec(100), self._spec(100)])
assert runs == [([0, 1], "default", 256)]
def test_small_items_split_to_avoid_cliff(self):
# Merging 3+ items of 100 tokens crosses 256 -> 4096; splitting
# into pairs costs 256 + 256 < 4096.
mgr = _make_manager_with_budgets([256, 4096])
runs = _run_packing(mgr, [self._spec(100)] * 4)
assert runs == [([0, 1], "default", 256), ([2, 3], "default", 256)]
def test_max_batch_size_split_unchanged(self):
mgr = _make_manager_with_budgets([1024, 4096])
mgr.max_batch_size = 3
runs = _run_packing(mgr, [self._spec(100)] * 4)
assert runs == [([0, 1, 2], "default", 1024), ([3], "default", 1024)]
def test_oversized_item_still_falls_back_to_eager(self):
mgr = _make_manager_with_budgets([256, 4096])
runs = _run_packing(mgr, [self._spec(100), self._spec(9000)])
assert runs == [([0], "default", 256)]
assert mgr.graph_misses == 1
def test_multi_path_deferral_skips_zero_token_paths(self):
mgr = _make_manager_with_budgets([256, 4096, 8192])
mgr.path_token_budgets = {
"a": [0, 256, 4096, 8192],
"b": [256, 4096, 8192],
}
specs = [
EncoderItemSpec(
input_size=300,
output_tokens=300,
path_output_tokens={"a": 200, "b": 100},
),
EncoderItemSpec(
input_size=4000,
output_tokens=4000,
path_output_tokens={"a": 0, "b": 4000},
),
]
runs = _run_packing(mgr, specs)
# Path b alone triggers deferral (256 + 4096 < 8192); the deferred
# item has zero tokens on path a, so that path is skipped.
assert runs == [
([0], "a", 256),
([0], "b", 256),
([1], "b", 4096),
]
# ---------------------------------------------------------------------------
# get_cumulative_stats
# ---------------------------------------------------------------------------