[Model][Feat] Support Minimax-H3 quality grading requests through dynamic loading/unloading with Cache-DiT (#5853)

Signed-off-by: mglyn <1203789601@qq.com>
This commit is contained in:
Mu GuanLin authored and GitHub committed 2026-08-07 09:07:23 +00:00
1 parent e906f41dc8
commit d8c35663b4
28 files changed
+1183 -18

No files matched your search

+6
View File
@@ -20,6 +20,10 @@ from vllm_omni.diffusion.cache.cachedit.model_specific import (
from vllm_omni.diffusion.cache.cachedit.model_specific import (
register_custom_dit_enablers as _register_custom_dit_enablers,
)
from vllm_omni.diffusion.cache.cachedit.runtime import (
CacheDiTRequestSpec,
RequestScopedCacheDiTRuntime,
)
_register_custom_dit_enablers()
@@ -29,6 +33,8 @@ __all__ = [
"CacheDiTAdapterConfig",
"CacheDiTBackend",
"CacheDiTConfig",
"CacheDiTRequestSpec",
"RequestScopedCacheDiTRuntime",
"SensenovaCachedAdapter",
"cache_summary",
"enable_cache_for_dit",
+51 -9
View File
@@ -99,19 +99,37 @@ def _build_cache_context_refresh(
# Cache-DiT has no predefined SCM mask for these small step counts.
scm_supported_steps = num_inference_steps >= 8 or num_inference_steps in (4, 6)
if projected_config.scm_steps_mask_policy is None or not scm_supported_steps:
if projected_config.force_refresh_step_hint is not None:
# Cache-DiT's ``once`` policy clears the hint after it fires.
# Reapply it at every request boundary so a repeated request
# with the same configured policy receives the same behavior.
cache_dit.refresh_context(
transformer,
cache_config=DBCacheConfig().reset(
num_inference_steps=num_inference_steps,
force_refresh_step_hint=projected_config.force_refresh_step_hint,
force_refresh_step_policy=projected_config.force_refresh_step_policy,
),
verbose=verbose,
)
return
cache_dit.refresh_context(transformer, num_inference_steps=num_inference_steps, verbose=verbose)
return
refresh_config = DBCacheConfig().reset(
num_inference_steps=num_inference_steps,
steps_computation_mask=cache_dit.steps_mask(
mask_policy=projected_config.scm_steps_mask_policy,
total_steps=num_inference_steps,
),
steps_computation_policy=projected_config.scm_steps_policy,
)
if projected_config.force_refresh_step_hint is not None:
refresh_config.force_refresh_step_hint = projected_config.force_refresh_step_hint
refresh_config.force_refresh_step_policy = projected_config.force_refresh_step_policy
cache_dit.refresh_context(
transformer,
cache_config=DBCacheConfig().reset(
num_inference_steps=num_inference_steps,
steps_computation_mask=cache_dit.steps_mask(
mask_policy=projected_config.scm_steps_mask_policy,
total_steps=num_inference_steps,
),
steps_computation_policy=projected_config.scm_steps_policy,
),
cache_config=refresh_config,
verbose=verbose,
)
@@ -227,19 +245,23 @@ class CacheDiTBackend(CacheBackend):
super().__init__(config)
self._refresh_funcs: list[RefreshCacheContextFunc] = []
self._cache_targets: list[Any] = []
def enable(self, pipeline: SupportsComponentDiscovery) -> None:
pipeline_name = type(pipeline).__name__
custom_enabler = CUSTOM_DIT_ENABLERS.get(pipeline_name)
self._refresh_funcs = []
self._cache_targets = []
if custom_enabler is not None:
logger.info("Using custom cache-dit enabler for model: %s", pipeline_name)
self._refresh_funcs = [custom_enabler(pipeline, self.config)]
self._cache_targets = [_default_get_pipeline_transformer(pipeline)]
else:
self._refresh_funcs = []
for name in _dit_module_names(pipeline):
get_transformer = _make_pipeline_transformer_getter(name)
block_adapter = _maybe_build_block_adapter(pipeline, get_transformer)
adapter_cls = _maybe_get_cached_adapter_cls(pipeline, get_transformer)
cache_target = get_transformer(pipeline) if block_adapter is None else block_adapter
self._refresh_funcs.append(
enable_cache_for_dit(
pipeline,
@@ -249,12 +271,32 @@ class CacheDiTBackend(CacheBackend):
get_transformer,
)
)
self._cache_targets.append(cache_target)
if not self._refresh_funcs:
raise ValueError(f"Pipeline {pipeline_name} has no declared DiT modules for Cache-DiT")
self.enabled = True
logger.info("Cache-dit enabled successfully on %s", pipeline_name)
def disable(self, pipeline: SupportsComponentDiscovery) -> None:
"""Remove Cache-DiT hooks so later requests use native forwards."""
if not self.enabled:
return
logger.info(
"Disabling cache-dit on %d DiT target(s) for %s",
len(self._cache_targets),
type(pipeline).__name__,
)
try:
for target in self._cache_targets:
cache_dit.disable_cache(target)
finally:
self._refresh_funcs = []
self._cache_targets = []
self.enabled = False
def refresh(self, pipeline: SupportsComponentDiscovery, num_inference_steps: int, verbose: bool = True) -> None:
if not self.enabled or not self._refresh_funcs:
logger.warning("Cache-dit is not enabled. Cannot refresh cache context.")
+94
View File
@@ -0,0 +1,94 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Declarative request-scoped Cache-DiT lifecycle."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from vllm_omni.diffusion.cache.cachedit.backend import CacheDiTBackend
from vllm_omni.diffusion.data import DiffusionCacheConfig
@dataclass(frozen=True)
class CacheDiTRequestSpec:
"""Desired Cache-DiT state for one request.
``installation_key`` identifies configurations that require reinstalling
hooks when switching between them. Step-count changes only require a
context refresh.
"""
installation_key: str
cache_config: DiffusionCacheConfig
num_inference_steps: int
class RequestScopedCacheDiTRuntime:
"""Reconcile declarative request state with installed Cache-DiT hooks."""
def __init__(self, pipeline: Any) -> None:
self._pipeline = pipeline
self._backend: CacheDiTBackend | None = None
self._installation_key: str | None = None
@property
def is_enabled(self) -> bool:
return self._backend is not None and self._backend.is_enabled()
def adopt(self, backend: CacheDiTBackend, *, installation_key: str) -> None:
"""Assume ownership of an already-enabled startup backend."""
if self._backend is not None:
if self._backend is backend and self._installation_key == installation_key:
return
raise RuntimeError(
f"Cache-DiT is already installed with key {self._installation_key!r}; cannot adopt {installation_key!r}"
)
if not backend.is_enabled():
raise ValueError("Cannot adopt a Cache-DiT backend before it is enabled")
self._backend = backend
self._installation_key = installation_key
def prepare(self, spec: CacheDiTRequestSpec | None) -> None:
"""Apply the minimal transition needed before the request denoise.
``None`` disables installed hooks. An unchanged installation key only
refreshes request context; a changed key disables the previous hooks
before installing the requested profile.
"""
desired_key = None if spec is None else spec.installation_key
if self._installation_key != desired_key:
self.disable()
if spec is None:
return
if self._backend is None:
backend = CacheDiTBackend(spec.cache_config)
backend.enable(self._pipeline)
self.adopt(backend, installation_key=spec.installation_key)
assert self._backend is not None
self._backend.refresh(
self._pipeline,
num_inference_steps=spec.num_inference_steps,
)
def disable(self) -> None:
try:
if self._backend is not None:
self._backend.disable(self._pipeline)
finally:
self._backend = None
self._installation_key = None
__all__ = [
"CacheDiTRequestSpec",
"RequestScopedCacheDiTRuntime",
]