[Refactor]: clean up diffusion scheduler interfaces (#5216)
Signed-off-by: Huang, Zeyu <11222265+fhfuih@users.noreply.github.com> Signed-off-by: Zeyu Huang | 黃澤宇 <11222265+fhfuih@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
31 files changed
+319
-338
No files matched your search
@@ -1,13 +1,15 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from vllm_omni.diffusion.sched.base_scheduler import BaseScheduler, SchedulerInterface
|
||||
from vllm_omni.diffusion.sched.interface import (
|
||||
CachedRequestData,
|
||||
DiffusionRequestState,
|
||||
DiffusionRequestStatus,
|
||||
DiffusionSchedulerOutput,
|
||||
KVPrefetchJob,
|
||||
NewRequestData,
|
||||
SchedulerInterface,
|
||||
SchedulerRequestState,
|
||||
StepBatchSamplingParamsKey,
|
||||
)
|
||||
from vllm_omni.diffusion.sched.request_scheduler import RequestScheduler
|
||||
from vllm_omni.diffusion.sched.step_scheduler import StepScheduler
|
||||
@@ -17,10 +19,13 @@ Scheduler = RequestScheduler
|
||||
__all__ = [
|
||||
"DiffusionRequestStatus",
|
||||
"CachedRequestData",
|
||||
"DiffusionRequestState",
|
||||
"DiffusionSchedulerOutput",
|
||||
"KVPrefetchJob",
|
||||
"NewRequestData",
|
||||
"SchedulerRequestState",
|
||||
"BaseScheduler",
|
||||
"SchedulerInterface",
|
||||
"StepBatchSamplingParamsKey",
|
||||
"RequestScheduler",
|
||||
"StepScheduler",
|
||||
"Scheduler",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from collections import deque
|
||||
from dataclasses import fields
|
||||
|
||||
@@ -12,54 +13,37 @@ from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.sched.interface import (
|
||||
CachedRequestData,
|
||||
DiffusionRequestState,
|
||||
DiffusionRequestStatus,
|
||||
DiffusionSchedulerOutput,
|
||||
KVPrefetchJob,
|
||||
NewRequestData,
|
||||
RequestBatchSamplingParamsKey,
|
||||
SamplingParamsKey,
|
||||
SchedulerInterface,
|
||||
SchedulerRequestState,
|
||||
StepBatchSamplingParamsKey,
|
||||
)
|
||||
from vllm_omni.diffusion.worker.utils import RunnerOutput
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
BatchSamplingParamsKey = StepBatchSamplingParamsKey | RequestBatchSamplingParamsKey
|
||||
|
||||
# LoRA identity is derived from `sampling.lora_request`, not a same-named field
|
||||
# on sampling params, so it must be resolved separately from the bulk lookup.
|
||||
_SAMPLING_PARAMS_KEY_FIELD_NAMES = frozenset(f.name for f in fields(SamplingParamsKey)) - {"lora_int_id"}
|
||||
_REQUEST_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES = frozenset(f.name for f in fields(RequestBatchSamplingParamsKey)) - {
|
||||
_STEP_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES = frozenset(field.name for field in fields(StepBatchSamplingParamsKey)) - {
|
||||
"lora_int_id"
|
||||
}
|
||||
|
||||
|
||||
def get_sampling_params_key(request: OmniDiffusionRequest) -> SamplingParamsKey:
|
||||
"""Build a batch-compatibility key from the request's sampling params."""
|
||||
sampling = request.sampling_params
|
||||
lora_request = getattr(sampling, "lora_request", None)
|
||||
return SamplingParamsKey(
|
||||
lora_int_id=lora_request.lora_int_id if lora_request is not None else None,
|
||||
**{name: getattr(sampling, name) for name in _SAMPLING_PARAMS_KEY_FIELD_NAMES},
|
||||
)
|
||||
|
||||
|
||||
def get_request_batch_sampling_params_key(request: OmniDiffusionRequest) -> RequestBatchSamplingParamsKey:
|
||||
"""Build a request-batch compatibility key from the request's sampling params."""
|
||||
sampling = request.sampling_params
|
||||
lora_request = getattr(sampling, "lora_request", None)
|
||||
key_kwargs = {name: getattr(sampling, name) for name in _REQUEST_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES}
|
||||
key_kwargs["lora_int_id"] = lora_request.lora_int_id if lora_request is not None else None
|
||||
return RequestBatchSamplingParamsKey(**key_kwargs)
|
||||
|
||||
|
||||
class _BaseScheduler(SchedulerInterface):
|
||||
class BaseScheduler(ABC):
|
||||
"""Shared queue/state bookkeeping for diffusion schedulers."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.od_config: OmniDiffusionConfig | None = None
|
||||
self._request_states: dict[str, DiffusionRequestState] = {}
|
||||
self._request_states: dict[str, SchedulerRequestState] = {}
|
||||
self._step_id: int = 0
|
||||
self._waiting: deque[str] = deque()
|
||||
self._running: list[str] = []
|
||||
self._running_sampling_params_key: SamplingParamsKey | RequestBatchSamplingParamsKey | None = None
|
||||
self._running_sampling_params_key: BatchSamplingParamsKey | None = None
|
||||
self._finished_req_ids: set[str] = set()
|
||||
self.max_num_running_reqs: int = 1
|
||||
self._prefetch_enabled: bool = False
|
||||
@@ -129,13 +113,13 @@ class _BaseScheduler(SchedulerInterface):
|
||||
# kv_sender_info (would target the wrong sender under multi-replica) or
|
||||
# one already finished/aborted (would consume its sender buffer for
|
||||
# nothing).
|
||||
kv_prefetch_jobs: dict | None = None
|
||||
kv_prefetch_job: KVPrefetchJob | None = None
|
||||
if self._prefetch_enabled and self._waiting:
|
||||
nxt = self._request_states.get(self._waiting[0])
|
||||
if nxt is not None and not nxt.is_finished():
|
||||
sender_info = getattr(nxt.req, "kv_sender_info", None)
|
||||
if sender_info:
|
||||
kv_prefetch_jobs = {
|
||||
kv_prefetch_job = {
|
||||
"request_id": nxt.request_id,
|
||||
"kv_sender_info": sender_info,
|
||||
}
|
||||
@@ -147,7 +131,7 @@ class _BaseScheduler(SchedulerInterface):
|
||||
finished_req_ids=set(self._finished_req_ids),
|
||||
num_running_reqs=len(self._running),
|
||||
num_waiting_reqs=len(self._waiting),
|
||||
kv_prefetch_jobs=kv_prefetch_jobs,
|
||||
kv_prefetch_job=kv_prefetch_job,
|
||||
)
|
||||
|
||||
# update after schedule
|
||||
@@ -155,6 +139,10 @@ class _BaseScheduler(SchedulerInterface):
|
||||
self._finished_req_ids.clear()
|
||||
return scheduler_output
|
||||
|
||||
@abstractmethod
|
||||
def update_from_output(self, sched_output: DiffusionSchedulerOutput, output: RunnerOutput) -> set[str]:
|
||||
pass
|
||||
|
||||
def has_requests(self) -> bool:
|
||||
return bool(self._waiting or self._running)
|
||||
|
||||
@@ -164,10 +152,10 @@ class _BaseScheduler(SchedulerInterface):
|
||||
def num_running_requests(self) -> int:
|
||||
return len(self._running)
|
||||
|
||||
def get_request_state(self, request_id: str) -> DiffusionRequestState | None:
|
||||
def get_request_state(self, request_id: str) -> SchedulerRequestState | None:
|
||||
return self._request_states.get(request_id)
|
||||
|
||||
def pop_request_state(self, request_id: str) -> DiffusionRequestState | None:
|
||||
def pop_request_state(self, request_id: str) -> SchedulerRequestState | None:
|
||||
self._pop_extra_request_state(request_id)
|
||||
return self._request_states.pop(request_id, None)
|
||||
|
||||
@@ -262,21 +250,21 @@ class _BaseScheduler(SchedulerInterface):
|
||||
def _pop_extra_request_state(self, request_id: str) -> None:
|
||||
"""Remove subclass-owned per-request state before popping request state."""
|
||||
|
||||
def _make_request_state(self, request_id: str, request: OmniDiffusionRequest) -> DiffusionRequestState:
|
||||
return DiffusionRequestState(
|
||||
def _make_request_state(self, request_id: str, request: OmniDiffusionRequest) -> SchedulerRequestState:
|
||||
return SchedulerRequestState(
|
||||
request_id=request_id,
|
||||
req=request,
|
||||
sampling_params_key=self._build_sampling_params_key(request),
|
||||
)
|
||||
|
||||
def _can_schedule_waiting(self, state: DiffusionRequestState) -> bool:
|
||||
def _can_schedule_waiting(self, state: SchedulerRequestState) -> bool:
|
||||
if not self._running:
|
||||
return True
|
||||
|
||||
current_key = self._current_sampling_params_key()
|
||||
return current_key is not None and current_key == state.sampling_params_key
|
||||
|
||||
def _current_sampling_params_key(self) -> SamplingParamsKey | RequestBatchSamplingParamsKey | None:
|
||||
def _current_sampling_params_key(self) -> BatchSamplingParamsKey | None:
|
||||
if self._running_sampling_params_key is not None or not self._running:
|
||||
return self._running_sampling_params_key
|
||||
state = self._request_states.get(self._running[0])
|
||||
@@ -285,5 +273,30 @@ class _BaseScheduler(SchedulerInterface):
|
||||
|
||||
def _build_sampling_params_key(
|
||||
self, request: OmniDiffusionRequest
|
||||
) -> SamplingParamsKey | RequestBatchSamplingParamsKey | None:
|
||||
return get_sampling_params_key(request)
|
||||
) -> StepBatchSamplingParamsKey | RequestBatchSamplingParamsKey: # return type loosened for subclassing
|
||||
"""Build a step-batch compatibility key from sampling parameters."""
|
||||
sampling = request.sampling_params
|
||||
# LoRA identity is optional on sampling params (and on test stubs).
|
||||
lora_request = getattr(sampling, "lora_request", None)
|
||||
return StepBatchSamplingParamsKey(
|
||||
lora_int_id=lora_request.lora_int_id if lora_request is not None else None,
|
||||
**{name: getattr(sampling, name) for name in _STEP_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES},
|
||||
)
|
||||
|
||||
|
||||
class SchedulerInterface(BaseScheduler):
|
||||
"""Deprecated compatibility base for custom scheduler injection.
|
||||
|
||||
Prefer subclassing :class:`BaseScheduler` directly. Subclassing this name
|
||||
still works but emits a :class:`DeprecationWarning`.
|
||||
"""
|
||||
|
||||
def __init_subclass__(cls, **kwargs) -> None:
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"SchedulerInterface is deprecated; subclass BaseScheduler instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
super().__init_subclass__(**kwargs)
|
||||
@@ -4,17 +4,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.worker.utils import RunnerOutput
|
||||
|
||||
|
||||
class DiffusionRequestStatus(enum.IntEnum):
|
||||
"""Request status tracked by diffusion scheduler."""
|
||||
@@ -34,7 +29,7 @@ class DiffusionRequestStatus(enum.IntEnum):
|
||||
|
||||
|
||||
@dataclass(frozen=True, eq=True)
|
||||
class SamplingParamsKey:
|
||||
class StepBatchSamplingParamsKey:
|
||||
"""Denoise step level Batch-compatibility key derived from ``OmniDiffusionSamplingParams``.
|
||||
|
||||
Only requests with the same key can be batched together.
|
||||
@@ -81,33 +76,33 @@ class RequestBatchSamplingParamsKey:
|
||||
"""
|
||||
|
||||
# Spatial / temporal shape.
|
||||
height: object = None
|
||||
width: object = None
|
||||
height: int | None = None
|
||||
width: int | None = None
|
||||
num_frames: int = 1
|
||||
resolution: object = 640
|
||||
fps: object = None
|
||||
frame_rate: object = None
|
||||
boundary_ratio: object = None
|
||||
resolution: int = 640
|
||||
fps: int | None = None
|
||||
frame_rate: float | None = None
|
||||
boundary_ratio: float | None = None
|
||||
|
||||
# CFG / guidance.
|
||||
do_classifier_free_guidance: bool = False
|
||||
guidance_scale: float = 0.0
|
||||
guidance_scale_provided: bool = False
|
||||
guidance_scale_2: object = None
|
||||
guidance_scale_2: float | None = None
|
||||
guidance_rescale: float = 0.0
|
||||
true_cfg_scale: object = None
|
||||
true_cfg_scale: float | None = None
|
||||
cfg_normalize: bool = False
|
||||
strength: object = None
|
||||
strength: float | None = None
|
||||
|
||||
# Scheduling / output shape.
|
||||
num_inference_steps: object = None
|
||||
sigmas: object = None
|
||||
max_sequence_length: object = None
|
||||
num_inference_steps: int | None = None
|
||||
sigmas: list[float] | None = None
|
||||
max_sequence_length: int | None = None
|
||||
num_outputs_per_prompt: int = 1
|
||||
eta: float = 0.0
|
||||
decode_timestep: object = None
|
||||
decode_noise_scale: object = None
|
||||
output_type: object = None
|
||||
decode_timestep: float | list[float] | None = None
|
||||
decode_noise_scale: float | list[float] | None = None
|
||||
output_type: str | None = None
|
||||
|
||||
# Model-specific batch defaults used by request-mode pipelines.
|
||||
layers: int = 4
|
||||
@@ -119,12 +114,12 @@ class RequestBatchSamplingParamsKey:
|
||||
|
||||
|
||||
@dataclass
|
||||
class DiffusionRequestState:
|
||||
class SchedulerRequestState:
|
||||
"""Scheduler-owned state for one queued OmniDiffusionRequest."""
|
||||
|
||||
request_id: str
|
||||
req: OmniDiffusionRequest
|
||||
sampling_params_key: SamplingParamsKey | RequestBatchSamplingParamsKey | None = None
|
||||
sampling_params_key: StepBatchSamplingParamsKey | RequestBatchSamplingParamsKey | None = None
|
||||
status: DiffusionRequestStatus = DiffusionRequestStatus.WAITING
|
||||
error: str | None = None
|
||||
|
||||
@@ -145,7 +140,7 @@ class NewRequestData:
|
||||
req: OmniDiffusionRequest
|
||||
|
||||
@classmethod
|
||||
def from_state(cls, state: DiffusionRequestState) -> NewRequestData:
|
||||
def from_state(cls, state: SchedulerRequestState) -> NewRequestData:
|
||||
return cls(request_id=state.request_id, req=state.req)
|
||||
|
||||
|
||||
@@ -160,10 +155,20 @@ class CachedRequestData:
|
||||
return cls(request_ids=[])
|
||||
|
||||
|
||||
class KVPrefetchJob(TypedDict):
|
||||
"""Descriptor for prefetching the next request's received KV cache."""
|
||||
|
||||
request_id: str
|
||||
kv_sender_info: dict[str, Any]
|
||||
|
||||
|
||||
@dataclass
|
||||
class DiffusionSchedulerOutput:
|
||||
"""Output of a single scheduling cycle."""
|
||||
|
||||
# Stable scheduler-cycle diagnostics for engines, injected schedulers, and
|
||||
# instrumentation; they are intentionally retained even without a direct
|
||||
# production consumer for every field.
|
||||
step_id: int # global step index
|
||||
scheduled_new_reqs: list[NewRequestData]
|
||||
scheduled_cached_reqs: CachedRequestData
|
||||
@@ -171,7 +176,7 @@ class DiffusionSchedulerOutput:
|
||||
num_running_reqs: int
|
||||
num_waiting_reqs: int
|
||||
# next request to background-prefetch KV
|
||||
kv_prefetch_jobs: dict | None = None
|
||||
kv_prefetch_job: KVPrefetchJob | None = None
|
||||
|
||||
@cached_property
|
||||
def scheduled_request_ids(self) -> list[str]:
|
||||
@@ -190,55 +195,3 @@ class DiffusionSchedulerOutput:
|
||||
@property
|
||||
def is_empty(self) -> bool:
|
||||
return self.num_scheduled_reqs == 0
|
||||
|
||||
|
||||
class SchedulerInterface(ABC):
|
||||
"""Abstract lifecycle contract for diffusion schedulers."""
|
||||
|
||||
@abstractmethod
|
||||
def initialize(self, od_config: OmniDiffusionConfig) -> None:
|
||||
"""Initialize or reset scheduler state."""
|
||||
|
||||
@abstractmethod
|
||||
def add_request(self, request: OmniDiffusionRequest) -> str:
|
||||
"""Add a request and return the scheduler-owned request id."""
|
||||
|
||||
@abstractmethod
|
||||
def schedule(self) -> DiffusionSchedulerOutput:
|
||||
"""Run one scheduling cycle."""
|
||||
|
||||
@abstractmethod
|
||||
def update_from_output(self, sched_output: DiffusionSchedulerOutput, output: RunnerOutput) -> set[str]:
|
||||
"""Update scheduler state from executor output."""
|
||||
|
||||
@abstractmethod
|
||||
def get_request_state(self, request_id: str) -> DiffusionRequestState | None:
|
||||
"""Return request state if present."""
|
||||
|
||||
@abstractmethod
|
||||
def has_requests(self) -> bool:
|
||||
"""Return whether the scheduler still owns runnable requests."""
|
||||
|
||||
@abstractmethod
|
||||
def num_waiting_requests(self) -> int:
|
||||
"""Return the number of requests waiting to be scheduled."""
|
||||
|
||||
@abstractmethod
|
||||
def num_running_requests(self) -> int:
|
||||
"""Return the number of requests currently running."""
|
||||
|
||||
@abstractmethod
|
||||
def pop_request_state(self, request_id: str) -> DiffusionRequestState | None:
|
||||
"""Remove and return request state if present."""
|
||||
|
||||
@abstractmethod
|
||||
def preempt_request(self, request_id: str) -> bool:
|
||||
"""Preempt a running request back to waiting."""
|
||||
|
||||
@abstractmethod
|
||||
def finish_requests(self, request_ids: str | list[str], status: DiffusionRequestStatus) -> None:
|
||||
"""Mark one or more requests finished."""
|
||||
|
||||
@abstractmethod
|
||||
def close(self) -> None:
|
||||
"""Release scheduler-owned state."""
|
||||
@@ -3,30 +3,38 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import fields
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.sched.base_scheduler import _BaseScheduler, get_request_batch_sampling_params_key
|
||||
from vllm_omni.diffusion.sched.base_scheduler import BaseScheduler
|
||||
from vllm_omni.diffusion.sched.interface import (
|
||||
DiffusionRequestStatus,
|
||||
DiffusionSchedulerOutput,
|
||||
RequestBatchSamplingParamsKey,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.worker.utils import RunnerOutput
|
||||
|
||||
# LoRA identity is derived from `sampling.lora_request`, not a same-named field
|
||||
# on sampling params, so it must be resolved separately from the bulk lookup.
|
||||
_REQUEST_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES = frozenset(
|
||||
field.name for field in fields(RequestBatchSamplingParamsKey)
|
||||
) - {"lora_int_id"}
|
||||
|
||||
class RequestScheduler(_BaseScheduler):
|
||||
|
||||
class RequestScheduler(BaseScheduler):
|
||||
"""Diffusion scheduler with vLLM-style waiting/running queues."""
|
||||
|
||||
def _build_sampling_params_key(self, request: OmniDiffusionRequest):
|
||||
return get_request_batch_sampling_params_key(request)
|
||||
|
||||
def add_request(self, request: OmniDiffusionRequest) -> str:
|
||||
return super().add_request(request)
|
||||
|
||||
def schedule(self) -> DiffusionSchedulerOutput:
|
||||
return super().schedule()
|
||||
def _build_sampling_params_key(self, request: OmniDiffusionRequest) -> RequestBatchSamplingParamsKey:
|
||||
"""Build a request-batch compatibility key from sampling parameters."""
|
||||
sampling = request.sampling_params
|
||||
# LoRA identity is optional on sampling params (and on test stubs).
|
||||
lora_request = getattr(sampling, "lora_request", None)
|
||||
key_kwargs = {name: getattr(sampling, name) for name in _REQUEST_BATCH_SAMPLING_PARAMS_KEY_FIELD_NAMES}
|
||||
key_kwargs["lora_int_id"] = lora_request.lora_int_id if lora_request is not None else None
|
||||
return RequestBatchSamplingParamsKey(**key_kwargs)
|
||||
|
||||
def update_from_output(self, sched_output: DiffusionSchedulerOutput, output: RunnerOutput) -> set[str]:
|
||||
scheduled_request_ids = sched_output.scheduled_request_ids
|
||||
|
||||
@@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
|
||||
from vllm.logger import init_logger
|
||||
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.sched.base_scheduler import _BaseScheduler
|
||||
from vllm_omni.diffusion.sched.base_scheduler import BaseScheduler
|
||||
from vllm_omni.diffusion.sched.interface import (
|
||||
DiffusionRequestStatus,
|
||||
DiffusionSchedulerOutput,
|
||||
@@ -27,8 +27,8 @@ class _StepProgress:
|
||||
total_steps: int
|
||||
|
||||
|
||||
class StepScheduler(_BaseScheduler):
|
||||
"""Placeholder scheduler that advances a request one denoise step per update."""
|
||||
class StepScheduler(BaseScheduler):
|
||||
"""Scheduler that advances each request by one denoise step per update."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
@@ -62,9 +62,6 @@ class StepScheduler(_BaseScheduler):
|
||||
)
|
||||
return request_id
|
||||
|
||||
def schedule(self) -> DiffusionSchedulerOutput:
|
||||
return super().schedule()
|
||||
|
||||
def update_from_output(self, sched_output: DiffusionSchedulerOutput, output: RunnerOutput) -> set[str]:
|
||||
scheduled_request_ids = sched_output.scheduled_request_ids
|
||||
if not scheduled_request_ids:
|
||||
|
||||
Reference in new issue
Block a user