From e006d761a526d0a69555a59f7c10c0c889babd98 Mon Sep 17 00:00:00 2001 From: Mia Ojeda Date: Tue, 29 Sep 2026 21:28:50 -0600 Subject: [PATCH] [Bugfix][CPU][DiffusionGemma] Support narrower canvas w/ sync scheduling (#59107) Signed-off-by: ojeda-e --- .../features/structured_diffusion/README.md | 7 +-- tests/test_sampling_params.py | 9 +-- tests/v1/core/test_scheduler.py | 57 ++++++++++++++++--- vllm/config/vllm.py | 8 ++- vllm/utils/diffusion.py | 10 ---- vllm/v1/core/sched/diffusion_scheduler.py | 27 ++++++--- vllm/v1/engine/input_processor.py | 1 - 7 files changed, 78 insertions(+), 41 deletions(-) diff --git a/examples/features/structured_diffusion/README.md b/examples/features/structured_diffusion/README.md index 37e58dbcdd..88b496bde8 100644 --- a/examples/features/structured_diffusion/README.md +++ b/examples/features/structured_diffusion/README.md @@ -36,10 +36,9 @@ layer when available, otherwise Triton. FlashInfer cannot serve this model (a batch mixes causal prefill with bidirectional denoising), and `--attention-backend FLASHINFER` is rejected. -Per-request canvas widths smaller than the served canvas require async -scheduling. The diffusion async scheduler is selected automatically; no -`--scheduler-cls` argument is needed. Synchronous execution supports full-width -canvases only. +Per-request canvas widths may be smaller than the served canvas with either +synchronous or asynchronous scheduling. Omit `diffusion_canvas_length` to use +the served canvas width. Question types: `noul` (yes/no), `choice` with `options`, `score` with ordered `levels`. Each label must be a single token in the answer template, diff --git a/tests/test_sampling_params.py b/tests/test_sampling_params.py index 98ef59a089..a6e057ad7a 100644 --- a/tests/test_sampling_params.py +++ b/tests/test_sampling_params.py @@ -71,7 +71,6 @@ def _verify_diffusion(params: SamplingParams, canvas_length: int | None = None): params, canvas_length=canvas_length, vocab_size=model_config.get_vocab_size(), - async_scheduling=True, ) @@ -100,7 +99,7 @@ def test_diffusion_extra_args_are_validated_without_a_served_canvas(): {"diffusion_canvas_length": 8}, ], ) -def test_narrow_diffusion_canvas_requires_async_scheduling( +def test_diffusion_canvas_width_is_accepted_with_either_scheduler( async_scheduling, extra_args ): processor = SimpleNamespace( @@ -115,11 +114,7 @@ def test_narrow_diffusion_canvas_requires_async_scheduling( validate_logits_processors_params=lambda params: None, ) params = SamplingParams(extra_args=extra_args) - if not async_scheduling and extra_args.get("diffusion_canvas_length") == 4: - with pytest.raises(VLLMValidationError, match="requires --async-scheduling"): - InputProcessor._validate_params(processor, params, ("generate",)) - else: - InputProcessor._validate_params(processor, params, ("generate",)) + InputProcessor._validate_params(processor, params, ("generate",)) @pytest.mark.parametrize( diff --git a/tests/v1/core/test_scheduler.py b/tests/v1/core/test_scheduler.py index d439a197e9..89a73b675c 100644 --- a/tests/v1/core/test_scheduler.py +++ b/tests/v1/core/test_scheduler.py @@ -42,6 +42,7 @@ from vllm.v1.core.kv_cache_coordinator import HybridKVCacheCoordinator from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash from vllm.v1.core.sched.diffusion_scheduler import ( DiffusionAsyncScheduler, + DiffusionScheduler, diffusion_canvas_width, ) from vllm.v1.core.sched.interface import PauseState @@ -7109,14 +7110,15 @@ def diffusion_model_runner(monkeypatch): monkeypatch.setenv("VLLM_USE_V2_MODEL_RUNNER", "1") -def _diffusion_scheduler(**kwargs) -> DiffusionAsyncScheduler: +def _diffusion_scheduler(async_scheduling: bool = True, **kwargs) -> DiffusionScheduler: + scheduler_cls = DiffusionAsyncScheduler if async_scheduling else DiffusionScheduler scheduler = create_scheduler( - async_scheduling=True, + async_scheduling=async_scheduling, diffusion_canvas_length=8, - scheduler_cls=DiffusionAsyncScheduler, + scheduler_cls=scheduler_cls, **kwargs, ) - assert isinstance(scheduler, DiffusionAsyncScheduler) + assert isinstance(scheduler, scheduler_cls) return scheduler @@ -7127,7 +7129,7 @@ def test_diffusion_scheduler_is_selected_by_default(async_scheduling): async_scheduling=async_scheduling, diffusion_canvas_length=8 ).vllm_config.scheduler_config assert config.get_scheduler_cls() is ( - DiffusionAsyncScheduler if config.async_scheduling else Scheduler + DiffusionAsyncScheduler if config.async_scheduling else DiffusionScheduler ) @@ -7149,10 +7151,13 @@ def test_diffusion_scheduler_narrows_the_canvas_per_request(): @pytest.mark.parametrize("structured", [False, True]) +@pytest.mark.parametrize("async_scheduling", [False, True]) @pytest.mark.usefixtures("diffusion_model_runner") -def test_diffusion_scheduler_trims_full_width_worker_drafts(structured): +def test_diffusion_scheduler_trims_full_width_worker_drafts( + structured, async_scheduling +): """Padded worker drafts must be narrowed before scheduling or grammar validation.""" - scheduler = _diffusion_scheduler() + scheduler = _diffusion_scheduler(async_scheduling=async_scheduling) wide = _diffusion_request("wide", {}) narrow = _diffusion_request("narrow", {"diffusion_canvas_length": 4}) for request in (wide, narrow): @@ -7167,8 +7172,12 @@ def test_diffusion_scheduler_trims_full_width_worker_drafts(structured): ) tokens = list(range(8)) if structured else [-1] * 8 drafts = DraftTokenIds(["wide", "narrow"], [tokens.copy(), tokens.copy()]) - output = scheduler.schedule() - scheduler.update_draft_token_ids_in_output(drafts, output) + if async_scheduling: + output = scheduler.schedule() + scheduler.update_draft_token_ids_in_output(drafts, output) + else: + scheduler.update_draft_token_ids(drafts) + output = scheduler.schedule() assert output.scheduled_spec_decode_tokens == { "wide": tokens, @@ -7180,6 +7189,36 @@ def test_diffusion_scheduler_trims_full_width_worker_drafts(structured): assert narrow.structured_output_request.grammar.seen == [tokens[:4]] +@pytest.mark.parametrize( + "sampled, expected_status", + [ + pytest.param([], RequestStatus.RUNNING, id="denoising"), + pytest.param([1] * 4, RequestStatus.FINISHED_LENGTH_CAPPED, id="final"), + ], +) +@pytest.mark.usefixtures("diffusion_model_runner") +def test_sync_diffusion_step_commits_only_emitted_tokens(sampled, expected_status): + """An empty denoising step reuses the canvas; a completed read emits it.""" + scheduler = _diffusion_scheduler(async_scheduling=False) + request = _diffusion_request( + "read", {"diffusion_canvas_length": 4, "diffusion_read_only": True} + ) + request.max_tokens = request.sampling_params.max_tokens = 4 + scheduler.add_request(request) + _model_output(scheduler, scheduler.schedule(), [[]]) + + scheduler.update_draft_token_ids( + DraftTokenIds([request.request_id], [list(range(8))]) + ) + output = scheduler.schedule() + assert output.num_scheduled_tokens == {request.request_id: 4} + _model_output(scheduler, output, [sampled]) + + assert request.num_computed_tokens == request.num_prompt_tokens + len(sampled) + assert list(request.output_token_ids) == sampled + assert request.status == expected_status + + @pytest.mark.usefixtures("diffusion_model_runner") def test_diffusion_scheduler_defers_a_read_with_every_step_in_flight(): scheduler = _diffusion_scheduler() diff --git a/vllm/config/vllm.py b/vllm/config/vllm.py index 28764e1fcc..a01023e34b 100644 --- a/vllm/config/vllm.py +++ b/vllm/config/vllm.py @@ -2020,11 +2020,15 @@ class VllmConfig: # After the platform hook, which has the last word on async scheduling. if ( self.diffusion_config is not None - and self.scheduler_config.async_scheduling and self.scheduler_config.scheduler_cls is None ): + scheduler_name = ( + "DiffusionAsyncScheduler" + if self.scheduler_config.async_scheduling + else "DiffusionScheduler" + ) self.scheduler_config.scheduler_cls = ( - "vllm.v1.core.sched.diffusion_scheduler.DiffusionAsyncScheduler" + f"vllm.v1.core.sched.diffusion_scheduler.{scheduler_name}" ) self._normalize_piecewise_cudagraph_mode( diff --git a/vllm/utils/diffusion.py b/vllm/utils/diffusion.py index 4980f940d8..f3018c18e7 100644 --- a/vllm/utils/diffusion.py +++ b/vllm/utils/diffusion.py @@ -15,7 +15,6 @@ def validate_diffusion_sampling_params( *, canvas_length: int | None, vocab_size: int, - async_scheduling: bool | None, ) -> None: """Validate request options and normalize one-canvas reads at admission.""" extra = params.extra_args @@ -105,12 +104,3 @@ def validate_diffusion_sampling_params( bool(params.logprob_token_ids), "diffusion_constrained needs logprob_token_ids: they are the allowed set.", ) - - check( - bool(async_scheduling) - or width is None - or canvas_length is None - or width == canvas_length, - "A diffusion_canvas_length smaller than the served canvas " - "requires --async-scheduling.", - ) diff --git a/vllm/v1/core/sched/diffusion_scheduler.py b/vllm/v1/core/sched/diffusion_scheduler.py index 96462aa6ab..9803307037 100644 --- a/vllm/v1/core/sched/diffusion_scheduler.py +++ b/vllm/v1/core/sched/diffusion_scheduler.py @@ -1,15 +1,11 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project -"""Async scheduling for diffusion requests. - -These rules live here so Scheduler.schedule() and AsyncScheduler stay -unchanged. VllmConfig selects this class for a diffusion model under async -scheduling. A sync scheduler creates no output placeholders, which both rules -read. -""" +"""Canvas-width handling and async read deferral for diffusion requests.""" from vllm.v1.core.sched.async_scheduler import AsyncScheduler from vllm.v1.core.sched.output import SchedulerOutput +from vllm.v1.core.sched.scheduler import Scheduler +from vllm.v1.outputs import DraftTokenIds from vllm.v1.request import Request @@ -38,7 +34,22 @@ def _read_in_flight(request: Request, width: int) -> bool: return request.num_output_placeholders >= steps * width -class DiffusionAsyncScheduler(AsyncScheduler): +class DiffusionScheduler(Scheduler): + def update_draft_token_ids(self, draft_token_ids: DraftTokenIds) -> None: + for i, (req_id, token_ids) in enumerate( + zip(draft_token_ids.req_ids, draft_token_ids.draft_token_ids) + ): + request = self.requests.get(req_id) + if request is None: + continue + width = diffusion_canvas_width(request, self.num_spec_tokens) + # Remove padded positions before the base scheduler validates grammar. + if len(token_ids) > width: + draft_token_ids.draft_token_ids[i] = token_ids[:width] + super().update_draft_token_ids(draft_token_ids) + + +class DiffusionAsyncScheduler(DiffusionScheduler, AsyncScheduler): def schedule(self, throttle_prefills: bool = False) -> SchedulerOutput: for request in self.running: width = diffusion_canvas_width(request, self.num_spec_tokens) diff --git a/vllm/v1/engine/input_processor.py b/vllm/v1/engine/input_processor.py index 33d413ba22..3113b90f3e 100644 --- a/vllm/v1/engine/input_processor.py +++ b/vllm/v1/engine/input_processor.py @@ -158,7 +158,6 @@ class InputProcessor: else None ), vocab_size=self.model_config.get_vocab_size(), - async_scheduling=self.vllm_config.scheduler_config.async_scheduling, ) if self.model_config.return_sampling_mask: