Kandinsky6 (#8537)

Signed-off-by: leffff <levnovitskiy@gmail.com>
Signed-off-by: Artem Borisov <divotionchannel@gmail.com>
Co-authored-by: leffff <levnovitskiy@gmail.com>
Co-authored-by: Artem Borisov <divotionchannel@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-06 15:55:15 +08:00
1 parent 1f63e97a40
commit 5f95115e70
38 files changed
+8574 -8

No files matched your search

+30
View File
@@ -908,6 +908,35 @@ def enable_cache_for_mammothmoda2(pipeline: Any, cache_config: Any) -> CacheDiTE
return CacheDiTEnableResult(refresh=refresh, targets=(block_adapter,))
def enable_cache_for_kandinsky6(pipeline: Any, cache_config: Any) -> RefreshCacheContextFunc:
"""Cache-DiT over K6 visual blocks only.
Text and audio block lists are not one residual chain with the visual
stack. If the block adapter rejects the fused ``(video, audio)`` return,
fall back to the same step-level velocity cache MagCache uses.
"""
transformer = pipeline.transformer
block_adapter = BlockAdapter(
transformer=transformer,
blocks=[transformer.visual_transformer_blocks],
forward_pattern=[ForwardPattern.Pattern_0],
has_separate_cfg=True,
check_forward_pattern=False,
)
try:
return enable_cache_for_dit(pipeline, cache_config, block_adapter)
except Exception as exc:
logger.warning("Cache-DiT block adapter failed for Kandinsky 6 (%s); using step cache", exc)
from vllm_omni.diffusion.models.kandinsky6.cache_accel import attach_mag_cache
attach_mag_cache(transformer, threshold=0.24, max_skip_steps=3, retention_ratio=0.1)
def _refresh(*_args, **_kwargs):
return None
return _refresh
def register_custom_dit_enablers() -> None:
"""Register model-specific Cache-DiT enablers.
@@ -925,6 +954,7 @@ def register_custom_dit_enablers() -> None:
"Cosmos3OmniDiffusersPipeline": enable_cache_for_cosmos3,
"Cosmos3OmniPipeline": enable_cache_for_cosmos3,
"Krea2Pipeline": enable_cache_for_krea2,
"Kandinsky6TI2VAPipeline": enable_cache_for_kandinsky6,
"Magi2Pipeline": enable_cache_for_magi2,
"MammothModa2DiTPipeline": enable_cache_for_mammothmoda2,
}
+14 -1
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""
MagCache backend implementation.
@@ -73,6 +73,19 @@ class MagCacheBackend(CacheBackend):
transformer = pipeline.transformer
transformer_type = transformer.__class__.__name__
if transformer_type == "Kandinsky6Transformer3DModel":
from vllm_omni.diffusion.models.kandinsky6.cache_accel import attach_mag_cache
attach_mag_cache(
transformer,
threshold=self.config.mag_threshold,
max_skip_steps=self.config.mag_max_skip_steps,
retention_ratio=self.config.mag_retention_ratio,
)
self._registered = True
logger.info("MagCache: Kandinsky 6 step-cache attached (visual/audio velocity reuse)")
return
num_inference_steps = self.config.num_inference_steps or 28
mag_ratios = self.config.mag_ratios
+4 -1
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""
Hook-based MagCache implementation for vLLM-Omni.
@@ -543,9 +543,12 @@ def apply_mag_cache_hook(
state_manager = StateManager(MagCacheState, (), {})
remaining_blocks = []
allowed = getattr(module, "_magcache_block_attrs", None)
for name, submodule in module.named_children():
if not isinstance(submodule, torch.nn.ModuleList):
continue
if allowed is not None and name not in allowed:
continue
for index, block in enumerate(submodule):
remaining_blocks.append((f"{name}.{index}", block))
+20 -1
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""
TeaCache backend implementation.
@@ -143,11 +143,30 @@ def enable_flux2_klein_teacache(pipeline: Any, config: DiffusionCacheConfig) ->
)
def enable_kandinsky6_teacache(pipeline: Any, config: DiffusionCacheConfig) -> None:
"""TeaCache for Kandinsky 6 via the denoise-loop step cache.
Coefficients are uncalibrated (see ``K6_TEACACHE_COEFFICIENTS``).
"""
from vllm_omni.diffusion.models.kandinsky6.cache_accel import attach_tea_cache
attach_tea_cache(
pipeline.transformer,
rel_l1_thresh=config.rel_l1_thresh if config.rel_l1_thresh is not None else 0.2,
coefficients=tuple(config.coefficients) if config.coefficients is not None else None,
)
logger.info(
"TeaCache: Kandinsky 6 step-cache attached (uncalibrated coefficients, thresh=%s)",
config.rel_l1_thresh,
)
CUSTOM_TEACACHE_ENABLERS = {
"BagelPipeline": enable_bagel_teacache,
"Flux2KleinPipeline": enable_flux2_klein_teacache,
"HunyuanImage3Pipeline": enable_hunyuan_image3_teacache,
"MammothModa2DiTPipeline": enable_mammoth_moda2_teacache,
"Kandinsky6TI2VAPipeline": enable_kandinsky6_teacache,
"MiniMaxH3Pipeline": enable_minimax_h3_teacache,
"SenseNovaU1Pipeline": enable_sensenova_u1_teacache,
}
+10 -1
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
from dataclasses import dataclass
@@ -8,6 +8,15 @@ from dataclasses import dataclass
# Source: TeaCache paper and ComfyUI-TeaCache empirical tuning
_MODEL_COEFFICIENTS = {
# FLUX transformer coefficients from TeaCache paper
# Uncalibrated stand-in so Kandinsky 6 TeaCache can be enabled.
# Copied from Qwen-Image; refit before treating skips as quality-neutral.
"Kandinsky6Transformer3DModel": [
-4.50000000e02,
2.80000000e02,
-4.50000000e01,
3.20000000e00,
-2.00000000e-02,
],
"FluxTransformer2DModel": [
4.98651651e02,
-2.83781631e02,
+9
View File
@@ -81,6 +81,15 @@ _DIFFUSION_MODEL_METADATA: dict[str, DiffusionModelMetadata] = {
max_multimodal_image_inputs=1,
final_output_type="video",
),
# Joint text/image-to-video-and-audio, same shape as MiniMaxH3Pipeline
# above (an MP4 with both tracks) — declared "video" for the same reason:
# the final container is a video file, so /v1/videos* is the right API
# surface even though the model also produces audio.
"Kandinsky6TI2VAPipeline": DiffusionModelMetadata(
supports_multimodal_inputs=True,
max_multimodal_image_inputs=1,
final_output_type="video",
),
"WanPipeline": DiffusionModelMetadata(
attention_mask_free=True,
final_output_type="video",
@@ -0,0 +1,22 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
from .kandinsky6_transformer import Kandinsky6Transformer3DModel
from .modeling_kandinsky6_audio import Kandinsky6AudioVAE
from .modeling_kandinsky6_vae import AutoencoderKLHunyuanVideo
from .pipeline_kandinsky6 import (
Kandinsky6TI2VAPipeline,
get_kandinsky6_post_process_func,
get_kandinsky6_pre_process_func,
)
from .scheduling_kandinsky6 import KandinskyFlowMatchScheduler
__all__ = [
"AutoencoderKLHunyuanVideo",
"Kandinsky6AudioVAE",
"Kandinsky6TI2VAPipeline",
"Kandinsky6Transformer3DModel",
"KandinskyFlowMatchScheduler",
"get_kandinsky6_post_process_func",
"get_kandinsky6_pre_process_func",
]
@@ -0,0 +1,199 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Kandinsky 6 cache adapters.
Generic MagCache / TeaCache hooks assume a single residual stream named
``hidden_states``. K6's fused visual blocks return ``(video, audio)``, so
the denoise loop consults :class:`K6StepCache` instead and reuses the
previous velocity when the schedule says the step can be skipped.
"""
from __future__ import annotations
from dataclasses import dataclass
# Pro geometry ratios from k6_video
# ``k6_pro_125_480_864_mCache_mOffload.yaml`` (cache.magcache.mag_ratios).
K6_PRO_MAG_RATIOS: tuple[float, ...] = (
1.0,
1.0,
0.94526286,
0.94622554,
1.13130749,
1.11243134,
1.02926022,
1.02675114,
1.011996,
1.01235353,
1.03039566,
1.02848639,
1.00101278,
1.00307236,
1.02238594,
1.02108294,
1.0248162,
1.02491556,
0.99771836,
0.99882495,
1.01694329,
1.01367628,
1.01096435,
1.00959183,
1.01537554,
1.01504827,
0.99237553,
0.99345238,
1.02368983,
1.02088727,
1.01396087,
1.01278108,
0.99773024,
0.99927632,
1.01573298,
1.01489232,
1.00129728,
1.00120319,
1.01759963,
1.01691798,
0.99335877,
0.99548372,
1.02925427,
1.02299559,
0.99749375,
0.99682709,
0.99694342,
1.00064403,
1.01101129,
1.0117984,
1.04420642,
1.03624643,
0.9599572,
0.97176547,
1.00452036,
1.0046107,
1.01447715,
1.01351055,
0.9999695,
1.00270806,
1.00770242,
1.00271775,
0.98795461,
0.98880837,
1.01113352,
1.00860993,
1.00116034,
1.00251894,
1.05147006,
1.04040677,
0.99651072,
0.99212138,
0.99040673,
0.98881456,
1.01265486,
1.01241698,
0.97343865,
0.97302804,
1.02539269,
1.02113221,
1.02117306,
1.02987275,
0.98607016,
0.98262938,
0.98404577,
0.98453141,
1.01065119,
1.00807871,
0.99986077,
0.99668861,
0.98597074,
0.98133325,
0.98239303,
0.98370113,
0.93155548,
0.93534231,
0.94231988,
0.93388087,
0.84200656,
0.83657581,
)
# Uncalibrated. Borrowed from the Qwen-Image TeaCache polynomial so the flag
# skips steps instead of raising. Quality will not match a fitted K6 curve.
K6_TEACACHE_COEFFICIENTS: tuple[float, ...] = (
-4.50000000e02,
2.80000000e02,
-4.50000000e01,
3.20000000e00,
-2.00000000e-02,
)
@dataclass
class K6StepCache:
"""Step-level skip state shared by MagCache and TeaCache."""
kind: str
threshold: float
max_skip_steps: int
retention_ratio: float
mag_ratios: tuple[float, ...] | None = None
coefficients: tuple[float, ...] | None = None
rel_l1_thresh: float = 0.2
last_cond: object = None
last_uncond: object = None
accumulated_err: float = 0.0
accumulated_ratio: float = 1.0
accumulated_steps: int = 0
def should_skip(self, step_index: int) -> bool:
if self.last_cond is None or step_index <= 0:
return False
if self.kind == "mag":
ratios = self.mag_ratios or ()
scale = float(ratios[step_index]) if step_index < len(ratios) else 1.0
self.accumulated_ratio *= scale
self.accumulated_steps += 1
self.accumulated_err += abs(1.0 - self.accumulated_ratio)
if (
self.accumulated_err <= self.threshold
and self.accumulated_steps <= self.max_skip_steps
and step_index >= max(1, int(self.retention_ratio * max(step_index, 1)))
):
return True
self.accumulated_ratio = 1.0
self.accumulated_steps = 0
self.accumulated_err = 0.0
return False
# TeaCache stand-in: uncalibrated polynomial of the step index.
# The real TeaCache distance is the modulated input; this only makes
# the flag skip steps instead of raising.
coeff = self.coefficients or K6_TEACACHE_COEFFICIENTS
x = float(step_index)
predicted = sum(weight * (x ** (len(coeff) - 1 - i)) for i, weight in enumerate(coeff))
return abs(predicted) < self.rel_l1_thresh * 1.0e4
def store(self, cond: object, uncond: object) -> None:
self.last_cond = cond
self.last_uncond = uncond
def attach_mag_cache(transformer, *, threshold: float, max_skip_steps: int, retention_ratio: float) -> None:
transformer._k6_step_cache = K6StepCache(
kind="mag",
threshold=threshold,
max_skip_steps=max_skip_steps,
retention_ratio=retention_ratio,
mag_ratios=K6_PRO_MAG_RATIOS,
)
def attach_tea_cache(transformer, *, rel_l1_thresh: float, coefficients: tuple[float, ...] | None) -> None:
transformer._k6_step_cache = K6StepCache(
kind="tea",
threshold=rel_l1_thresh if rel_l1_thresh is not None else 0.2,
max_skip_steps=3,
retention_ratio=0.0,
coefficients=coefficients or K6_TEACACHE_COEFFICIENTS,
rel_l1_thresh=rel_l1_thresh if rel_l1_thresh is not None else 0.2,
)
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,103 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Native K6 shifted flow-matching Euler scheduler.
Self-contained (no ``diffusers`` dependency), matching the duck-typed
scheduler interface (``set_timesteps`` / ``timesteps`` / ``sigmas`` /
``step``) that other native vLLM-Omni diffusion pipelines use for their own
per-model schedulers — see ``WanEulerScheduler`` in
``vllm_omni.diffusion.models.wan2_2.scheduling_wan_euler`` for the sibling
pattern this mirrors.
The math reproduces ``kandinsky.core.algo.flow_matching.flow_match_timesteps``
exactly: for ``t`` linearly spaced from 1.0 down to 0.0,
``sigma = scale * t / (1 + (scale - 1) * t)``. This is the same shifted
rectified-flow schedule family Wan's own scheduler uses, parameterized by
``scheduler_scale`` (K6's name for what Wan calls ``shift``).
"""
from __future__ import annotations
from dataclasses import dataclass
from types import SimpleNamespace
import torch
@dataclass
class KandinskyFlowMatchSchedulerOutput:
prev_sample: torch.Tensor
class KandinskyFlowMatchScheduler:
"""Stateful Euler stepper over K6's shifted flow-matching sigma schedule.
``timesteps`` holds the model-scale (``[0, 1000]``-ish) values fed to the
DiT's ``time`` input; ``sigmas`` holds the same schedule unscaled
(``num_inference_steps + 1`` entries, running 1.0 -> 0.0), needed to
derive a step size without consuming a stateful ``step()`` call when one
modality is stepped implicitly alongside another (see the ``vllm`` port's
``denoise_loop`` override).
"""
order = 1
def __init__(self, scheduler_scale: float = 1.0, device: torch.device | str = "cpu") -> None:
self.scheduler_scale = float(scheduler_scale)
self.device = device
self.config = SimpleNamespace(scheduler_scale=self.scheduler_scale)
self._step_index: int | None = None
self.timesteps = torch.empty(0, dtype=torch.float32)
self.sigmas = torch.empty(0, dtype=torch.float32)
self.set_timesteps(num_inference_steps=1, device=self.device)
@property
def step_index(self) -> int | None:
return self._step_index
def set_timesteps(
self,
num_inference_steps: int,
device: torch.device | str | None = None,
**kwargs, # noqa: ARG002 - kept for scheduler API compatibility
) -> None:
device = device or self.device
t = torch.linspace(1.0, 0.0, int(num_inference_steps) + 1, device=device, dtype=torch.float32)
scale = self.scheduler_scale
self.sigmas = scale * t / (1 + (scale - 1) * t)
# DiT `time` input is model-scale, i.e. sigma * 1000 (matches the core
# algo loop's `t.unsqueeze(0).expand(bs) * 1000`).
self.timesteps = self.sigmas[:-1] * 1000.0
self._step_index = None
def index_for_timestep(self, timestep: torch.Tensor) -> int:
timestep_t = torch.as_tensor(timestep, device=self.timesteps.device, dtype=self.timesteps.dtype)
return int(torch.argmin(torch.abs(self.timesteps - timestep_t)).item())
def step(
self,
model_output: torch.Tensor,
timestep: torch.Tensor,
sample: torch.Tensor,
return_dict: bool = True,
**kwargs, # noqa: ARG002 - kept for scheduler API compatibility
) -> KandinskyFlowMatchSchedulerOutput | tuple[torch.Tensor]:
if self._step_index is None:
self._step_index = self.index_for_timestep(timestep)
sigma = self.sigmas[self._step_index]
sigma_next = self.sigmas[self._step_index + 1]
dt = (sigma_next - sigma).to(device=sample.device, dtype=sample.dtype)
prev_sample = sample + dt * model_output
self._step_index += 1
if not return_dict:
return (prev_sample,)
return KandinskyFlowMatchSchedulerOutput(prev_sample=prev_sample)
def __len__(self) -> int:
return int(self.timesteps.shape[0])
+7
View File
@@ -396,6 +396,11 @@ _DIFFUSION_MODELS = {
"pipeline_krea2",
"Krea2Pipeline",
),
"Kandinsky6TI2VAPipeline": (
"kandinsky6",
"pipeline_kandinsky6",
"Kandinsky6TI2VAPipeline",
),
}
_DIFFUSION_MODELS["MingImageLayeredDiffusionPipeline"] = _DIFFUSION_MODELS["MingImageDiffusionPipeline"]
@@ -710,6 +715,7 @@ _DIFFUSION_POST_PROCESS_FUNCS = {
"StableDiffusionXLPipeline": "get_sdxl_image_post_process_func",
"Krea2Pipeline": "get_krea2_post_process_func",
"HunyuanImage3ForCausalMM": "get_hunyuan_image3_post_process_func",
"Kandinsky6TI2VAPipeline": "get_kandinsky6_post_process_func",
}
_DIFFUSION_POST_PROCESS_FUNCS["MingImageLayeredDiffusionPipeline"] = _DIFFUSION_POST_PROCESS_FUNCS[
"MingImageDiffusionPipeline"
@@ -756,6 +762,7 @@ _DIFFUSION_PRE_PROCESS_FUNCS = {
"Cosmos3OmniDiffusersPipeline": "get_cosmos3_pre_process_func",
"Cosmos3OmniPipeline": "get_cosmos3_pre_process_func",
"MammothModa2DiTPipeline": "get_mammoth_moda2_pre_process_func",
"Kandinsky6TI2VAPipeline": "get_kandinsky6_pre_process_func",
}
+7 -1
View File
@@ -158,7 +158,13 @@ def is_diffusion_model(model_name: str) -> bool:
logger.debug("Failed to read local %s: %s", filename, e)
# Strategy 2: Check using vllm's utility (works for both local and remote models)
config_dict = get_diffusion_model_index(model_name)
# get_hf_file_to_dict can raise for local paths that don't resolve to a Hub
# repo id (see resolve_model_class_name's identical guard for the same call).
try:
config_dict = get_diffusion_model_index(model_name)
except Exception as e:
logger.debug("Failed to fetch diffusion model index via vllm utility: %s", e)
config_dict = None
if config_dict is not None and config_dict.get("_class_name") and config_dict.get("_diffusers_version"):
logger.debug("Detected diffusion model via a standard Diffusers index")
return True