[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
@@ -37,7 +37,7 @@ if TYPE_CHECKING:
|
||||
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
||||
|
||||
from vllm_omni.diffusion.worker.input_batch import InputBatch
|
||||
from vllm_omni.diffusion.worker.utils import DiffusionRequestState
|
||||
from vllm_omni.diffusion.worker.utils import StepRequestState
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -279,9 +279,9 @@ class HeliosPipeline(
|
||||
|
||||
def prepare_encode(
|
||||
self,
|
||||
state: DiffusionRequestState,
|
||||
state: StepRequestState,
|
||||
**kwargs: Any,
|
||||
) -> DiffusionRequestState:
|
||||
) -> StepRequestState:
|
||||
"""Initialize Helios request state for chunk-wise step execution."""
|
||||
del kwargs
|
||||
# Wrap the single request in a DiffusionRequestBatch so the batch
|
||||
@@ -530,7 +530,7 @@ class HeliosPipeline(
|
||||
self._prepare_next_chunk(state)
|
||||
return state
|
||||
|
||||
def _prepare_next_chunk(self, state: DiffusionRequestState) -> None:
|
||||
def _prepare_next_chunk(self, state: StepRequestState) -> None:
|
||||
extra = state.extra
|
||||
k = state.chunk_index
|
||||
is_first_chunk = k == 0
|
||||
@@ -604,7 +604,7 @@ class HeliosPipeline(
|
||||
state.step_index = 0
|
||||
self._num_timesteps = state.chunk_num_steps
|
||||
|
||||
def _prepare_stage2_chunk(self, state: DiffusionRequestState) -> None:
|
||||
def _prepare_stage2_chunk(self, state: StepRequestState) -> None:
|
||||
extra = state.extra
|
||||
batch_size, num_channel, num_frames_lat, height, width = state.latents.shape
|
||||
latents_flat = state.latents.permute(0, 2, 1, 3, 4).reshape(
|
||||
@@ -634,7 +634,7 @@ class HeliosPipeline(
|
||||
return num_steps * 2
|
||||
return num_steps
|
||||
|
||||
def _set_stage2_timesteps(self, state: DiffusionRequestState) -> None:
|
||||
def _set_stage2_timesteps(self, state: StepRequestState) -> None:
|
||||
extra = state.extra
|
||||
patch_size = self.transformer.config.patch_size
|
||||
image_seq_len = (state.latents.shape[-1] * state.latents.shape[-2] * state.latents.shape[-3]) // (
|
||||
@@ -655,7 +655,7 @@ class HeliosPipeline(
|
||||
def denoise_step(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
states: Sequence[DiffusionRequestState],
|
||||
states: Sequence[StepRequestState],
|
||||
**kwargs: Any,
|
||||
) -> torch.Tensor | None:
|
||||
del kwargs
|
||||
@@ -668,7 +668,7 @@ class HeliosPipeline(
|
||||
|
||||
def _denoise_stage1_step(
|
||||
self,
|
||||
state: DiffusionRequestState,
|
||||
state: StepRequestState,
|
||||
latents: torch.Tensor,
|
||||
timesteps: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
@@ -727,7 +727,7 @@ class HeliosPipeline(
|
||||
cfg_normalize=False,
|
||||
)
|
||||
|
||||
def _denoise_stage2_step(self, state: DiffusionRequestState) -> torch.Tensor:
|
||||
def _denoise_stage2_step(self, state: StepRequestState) -> torch.Tensor:
|
||||
extra = state.extra
|
||||
latents = state.latents
|
||||
assert latents is not None
|
||||
@@ -773,7 +773,7 @@ class HeliosPipeline(
|
||||
|
||||
def step_scheduler(
|
||||
self,
|
||||
state: DiffusionRequestState,
|
||||
state: StepRequestState,
|
||||
noise_pred: torch.Tensor,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
@@ -800,7 +800,7 @@ class HeliosPipeline(
|
||||
state.step_in_chunk += 1
|
||||
state.step_index = state.step_in_chunk
|
||||
|
||||
def _step_scheduler_stage2(self, state: DiffusionRequestState, noise_pred: torch.Tensor) -> None:
|
||||
def _step_scheduler_stage2(self, state: StepRequestState, noise_pred: torch.Tensor) -> None:
|
||||
extra = state.extra
|
||||
t = state.current_timestep
|
||||
assert t is not None and state.latents is not None
|
||||
@@ -866,7 +866,7 @@ class HeliosPipeline(
|
||||
|
||||
def post_decode(
|
||||
self,
|
||||
state: DiffusionRequestState,
|
||||
state: StepRequestState,
|
||||
**kwargs: Any,
|
||||
) -> DiffusionOutput:
|
||||
del kwargs
|
||||
|
||||
@@ -54,7 +54,7 @@ from .system_prompt import get_system_prompt
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.worker.input_batch import InputBatch
|
||||
from vllm_omni.diffusion.worker.utils import DiffusionRequestState
|
||||
from vllm_omni.diffusion.worker.utils import StepRequestState
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -486,7 +486,7 @@ class HunyuanImage3Pipeline(
|
||||
self._pipeline = HunyuanImage3Text2ImagePipeline(model=self, scheduler=self.scheduler, vae=self.vae)
|
||||
return self._pipeline
|
||||
|
||||
def _validate_step_request(self, state: "DiffusionRequestState") -> None:
|
||||
def _validate_step_request(self, state: "StepRequestState") -> None:
|
||||
prompt = state.prompt
|
||||
sampling = state.sampling
|
||||
if prompt is None:
|
||||
@@ -584,7 +584,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _extract_step_prompt_inputs(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
) -> tuple[list[str], list[str | None], str | None, list[list[JointImageInfo]] | None, str]:
|
||||
sampling = state.sampling
|
||||
return self._extract_prompt_inputs(
|
||||
@@ -610,7 +610,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _restore_injected_ar_kv(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
) -> None:
|
||||
@@ -755,7 +755,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _prompt_kv_prefix_lens(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
) -> list[int] | None:
|
||||
@@ -774,7 +774,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _merge_step_model_inputs(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
first_step: bool,
|
||||
@@ -828,7 +828,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _restore_prompt_kv_cache(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
) -> None:
|
||||
@@ -855,7 +855,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _capture_prompt_kv_cache(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
) -> None:
|
||||
@@ -1838,9 +1838,9 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def prepare_encode(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
**kwargs: Any,
|
||||
) -> "DiffusionRequestState":
|
||||
) -> "StepRequestState":
|
||||
del kwargs
|
||||
self._validate_step_request(state)
|
||||
pipe = self.pipeline
|
||||
@@ -1955,7 +1955,7 @@ class HunyuanImage3Pipeline(
|
||||
}
|
||||
return state
|
||||
|
||||
def _step_group_key(self, state: "DiffusionRequestState") -> tuple[Any, ...]:
|
||||
def _step_group_key(self, state: "StepRequestState") -> tuple[Any, ...]:
|
||||
if _STEP_CFG_FACTOR not in state.extra:
|
||||
raise ValueError(f"Missing Hunyuan CFG factor for request {state.request_id}.")
|
||||
if _STEP_MODEL_KWARGS not in state.extra:
|
||||
@@ -1989,16 +1989,16 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _split_step_groups(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
) -> list[list["DiffusionRequestState"]]:
|
||||
grouped: dict[tuple[Any, ...], list[DiffusionRequestState]] = {}
|
||||
states: list["StepRequestState"],
|
||||
) -> list[list["StepRequestState"]]:
|
||||
grouped: dict[tuple[Any, ...], list[StepRequestState]] = {}
|
||||
for state in states:
|
||||
grouped.setdefault(self._step_group_key(state), []).append(state)
|
||||
return list(grouped.values())
|
||||
|
||||
@staticmethod
|
||||
def _step_row_order(
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
cfg_factor: int,
|
||||
) -> tuple[list[int], list[int]]:
|
||||
row_state_indexes = [state_idx for _ in range(cfg_factor) for state_idx in range(len(states))]
|
||||
@@ -2006,7 +2006,7 @@ class HunyuanImage3Pipeline(
|
||||
return row_state_indexes, row_branches
|
||||
|
||||
@staticmethod
|
||||
def _validate_step_group_states(states: list["DiffusionRequestState"]) -> tuple[bool, int]:
|
||||
def _validate_step_group_states(states: list["StepRequestState"]) -> tuple[bool, int]:
|
||||
if not states:
|
||||
raise ValueError("HunyuanImage3 denoise_step received an empty group.")
|
||||
|
||||
@@ -2043,7 +2043,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def _split_merged_kwargs_to_states(
|
||||
self,
|
||||
states: list["DiffusionRequestState"],
|
||||
states: list["StepRequestState"],
|
||||
merged_kwargs: dict[str, Any],
|
||||
row_state_indexes: list[int],
|
||||
row_branches: list[int],
|
||||
@@ -2094,7 +2094,7 @@ class HunyuanImage3Pipeline(
|
||||
state.extra[_STEP_MODEL_KWARGS] = next_kwargs
|
||||
state.extra[_STEP_INPUT_IDS] = None
|
||||
|
||||
def _denoise_step_group(self, states: list["DiffusionRequestState"]) -> torch.Tensor:
|
||||
def _denoise_step_group(self, states: list["StepRequestState"]) -> torch.Tensor:
|
||||
first_step, cfg_factor = self._validate_step_group_states(states)
|
||||
row_state_indexes, row_branches = self._step_row_order(states, cfg_factor)
|
||||
latents = torch.cat([state.latents for state in states], dim=0)
|
||||
@@ -2179,7 +2179,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def step_scheduler(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
noise_pred: torch.Tensor,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
@@ -2200,7 +2200,7 @@ class HunyuanImage3Pipeline(
|
||||
|
||||
def post_decode(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
**kwargs: Any,
|
||||
) -> DiffusionOutput:
|
||||
output_type = kwargs.get("output_type", "pil")
|
||||
|
||||
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
|
||||
|
||||
from vllm_omni.diffusion.data import DiffusionOutput
|
||||
from vllm_omni.diffusion.worker.input_batch import InputBatch
|
||||
from vllm_omni.diffusion.worker.utils import DiffusionRequestState
|
||||
from vllm_omni.diffusion.worker.utils import StepRequestState
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
@@ -54,7 +54,7 @@ class SupportsStepExecution(Protocol):
|
||||
|
||||
supports_step_execution: ClassVar[bool] = True
|
||||
|
||||
def prepare_encode(self, state: DiffusionRequestState, **kwargs: Any) -> DiffusionRequestState:
|
||||
def prepare_encode(self, state: StepRequestState, **kwargs: Any) -> StepRequestState:
|
||||
"""Prepare request-level inputs and return initialized state."""
|
||||
...
|
||||
|
||||
@@ -62,11 +62,11 @@ class SupportsStepExecution(Protocol):
|
||||
"""Run one denoise forward on the runner-assembled batch."""
|
||||
...
|
||||
|
||||
def step_scheduler(self, state: DiffusionRequestState, noise_pred: torch.Tensor, **kwargs: Any) -> None:
|
||||
def step_scheduler(self, state: StepRequestState, noise_pred: torch.Tensor, **kwargs: Any) -> None:
|
||||
"""Run one scheduler step."""
|
||||
...
|
||||
|
||||
def post_decode(self, state: DiffusionRequestState, **kwargs: Any) -> DiffusionOutput:
|
||||
def post_decode(self, state: StepRequestState, **kwargs: Any) -> DiffusionOutput:
|
||||
"""Decode output after denoise loop or at a partial chunk boundary."""
|
||||
...
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ from vllm_omni.diffusion.worker.request_batch import DiffusionRequestBatch, spli
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.worker.input_batch import InputBatch
|
||||
from vllm_omni.diffusion.worker.utils import DiffusionRequestState
|
||||
from vllm_omni.diffusion.worker.utils import StepRequestState
|
||||
|
||||
from vllm_omni.model_executor.model_loader.weight_utils import (
|
||||
download_weights_from_hf_specific,
|
||||
@@ -770,9 +770,9 @@ class QwenImagePipeline(
|
||||
|
||||
def prepare_encode(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
**kwargs: Any,
|
||||
) -> "DiffusionRequestState":
|
||||
) -> "StepRequestState":
|
||||
"""Populate *state* with encoded prompts, latents, timesteps, and CFG config."""
|
||||
sampling = state.sampling
|
||||
prompt, negative_prompt = self._extract_prompts([state.prompt] if state.prompt is not None else [])
|
||||
@@ -959,7 +959,7 @@ class QwenImagePipeline(
|
||||
|
||||
def step_scheduler(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
noise_pred: torch.Tensor,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
@@ -980,7 +980,7 @@ class QwenImagePipeline(
|
||||
|
||||
def post_decode(
|
||||
self,
|
||||
state: "DiffusionRequestState",
|
||||
state: "StepRequestState",
|
||||
**kwargs: Any,
|
||||
) -> DiffusionOutput:
|
||||
"""Decode final latents from *state*."""
|
||||
|
||||
Reference in new issue
Block a user