Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: static step skip for video generation (#11748)
Daniel Han5548e92a1c5a
diff --git a/studio/backend/core/inference/diffusion_step_skip.py b/studio/backend/core/inference/diffusion_step_skip.pyindex 446c3e56dd0..d3d58ea4a7a 100644--- a/studio/backend/core/inference/diffusion_step_skip.py+++ b/studio/backend/core/inference/diffusion_step_skip.py@@ -187,14 +187,16 @@ def _timestep_of(kwargs: dict,slot: tuple = ("timestep", None),) -> Any:- """The call's timestep as a one-element float32 tensor on its own device (no host sync)."""+ """The call's timestep as a one-element float32 tensor on its own device (no host sync).++ The max: a per-token ``mask * t`` (Wan2.2 TI2V, LTX) is 0 on conditioned tokens; taylor1 would silently reuse."""name, index = slott = kwargs.get(name)if t is None and index is not None and index < len(args):t = args[index]try:if _is_tensor(t):- return t.detach().reshape(-1)[:1].to(dtype = _torch().float32).clone()+ return t.detach().reshape(-1).float().amax().reshape(1).clone()if isinstance(t, (int, float)) and not isinstance(t, bool):return _torch().tensor([float(t)], dtype = _torch().float32)except Exception: # noqa: BLE001 - no timestep, taylor1 falls back to reuse@@ -294,7 +296,8 @@ class StaticStepSkip:self.history: dict = {}if keep_stats:return self- if self.stats["calls"]:+ if self.stats["calls"] or self.counting:+ # Kept for status past the per-call reset; arming a new generation clears it (no stale counts).self.last_stats = dict(self.stats)self.stats = {"calls": 0, "computed": 0, "skipped": 0}if steps is not None and owner != self.owner:diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex d6937499225..e94de608a26 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -55,13 +55,23 @@ from .diffusion_attention import (from .diffusion_cache import (FBCACHE_MIN_STEPS,TC_AUTO,+ TC_STATIC,apply_step_cache,auto_step_cache_allowed,+ cache_breaks_graph,maybe_toggle_step_cache,normalize_transformer_cache,resolve_auto_step_cache,step_cache_supported,)+from .diffusion_step_skip import (+ install_static_step_skip,+ mark_step_end,+ reset_static_step_skip,+ static_skip_stats,+ static_skip_view,+ uninstall_static_step_skip,+)from .diffusion_device import (DiffusionDeviceTarget,apply_diffusion_device_ordinal,@@ -853,6 +863,7 @@ class _VideoLoadState:speed_optims: tuple = ()backend_flags: Optional[dict] = Noneattention_backend: Optional[str] = None+ # Only fbcache breaks the graph (cache_breaks_graph); static keeps fullgraph and CUDA-graph eligibility.transformer_cache: Optional[str] = None# Auto only: generate() toggles it across FBCACHE_MIN_STEPS; explicit never toggles.cache_auto: bool = False@@ -1291,6 +1302,13 @@ def _denoiser_view(pipe: Any, component: str) -> Any:return pipe if component == "transformer" else _NamedDiTView(pipe, component)+def _is_static_cache_request(value: Optional[str]) -> bool:+ try:+ return normalize_transformer_cache(value) == TC_STATIC+ except ValueError:+ return False++def _views_for(pipe: Any, fam: VideoFamily) -> tuple[Any, ...]:"""The pipe view(s) to pass through the ``getattr(pipe, "transformer")`` helpers sothey cover every denoiser: the real pipe (its ``transformer``), plus a@@ -2017,6 +2035,8 @@ class VideoBackend:hf_token: Optional[str] = None,memory_mode: Optional[str] = None,gpu_ordinal: Optional[int] = None,+ # NAMED so a static step-skip ask can be recorded as declined: sd.cpp runs every step itself.+ transformer_cache: Optional[str] = None,# NAMED, not left to the ``**_`` swallow below: an API-initiated load hands this in through _run_load's kwargs,# and swallowed it meant the four-file bundle, the sizing metadata and the sd-cli install were all fetched by a# load that promised no downloads.@@ -2440,6 +2460,18 @@ class VideoBackend:"flash","sd.cpp diffusion flash attention",),+ **(+ {+ "transformer_cache": (+ transformer_cache,+ "off",+ "static step skip is not supported by the native sd.cpp runtime",+ RESOLVED_UNSUPPORTED,+ )+ }+ if _is_static_cache_request(transformer_cache)+ else {}+ ),}),)@@ -3943,6 +3975,7 @@ class VideoBackend:# Carried, not defaulted: load_pipeline is also reached directly (no _run_load), and dropping it here# would let an offline load fetch the four-file bundle.local_files_only = local_files_only,+ transformer_cache = transformer_cache,)return self.status()@@ -4025,6 +4058,7 @@ class VideoBackend:_base_local_dir = _base_local_dir,# Settled before the pull when the pull acted on it; None when it did not._h3_auto_denoiser_planned = _h3_auto_denoiser_planned,+ transformer_cache = transformer_cache,)transformer_quant_requested = transformer_quant@@ -4542,18 +4576,41 @@ class VideoBackend:default_cache_steps, _ = default_video_generation_params(gguf_filename, repo_id, base)cache_request = resolve_auto_step_cache(effective_speed, default_cache_steps)cache_engaged = None- for view in views:- engaged = apply_step_cache(- view,- mode = cache_request,- threshold = transformer_cache_threshold,- # A quantized transformer's residuals are larger, so both engaged quant and GGUF need the higher FBCache- # threshold.- quant_active = cache_quant_active,- logger = logger,- )- if view is pipe:- cache_engaged = engaged+ static_decline: Optional[str] = None+ if cache_request == TC_STATIC:+ # One denoiser only: a dual-expert MoE hands part of the trajectory to transformer_2, unseen by the history.+ if len(views) > 1 or getattr(pipe, "transformer_2", None) is not None:+ static_decline = (+ "static step skip needs a single denoiser; this family runs two experts "+ "(transformer_2), so it runs uncached"+ )+ logger.warning("video.step_skip: %s", static_decline)+ elif getattr(fam, "has_audio", False):+ # Engaged on LTX-2's joint (video, audio) denoiser it would report static while every call computed.+ static_decline = (+ "static step skip reuses one noise prediction per call; this family's denoiser "+ "returns joint video and audio predictions, so it runs uncached"+ )+ logger.warning("video.step_skip: %s", static_decline)+ else:+ cache_engaged = install_static_step_skip(pipe, logger = logger)+ if cache_engaged is None:+ static_decline = (+ "static step skip is unavailable for this pipeline; it runs uncached"+ )+ else:+ for view in views:+ engaged = apply_step_cache(+ view,+ mode = cache_request,+ threshold = transformer_cache_threshold,+ # Quantized residuals are larger: engaged quant and GGUF need the higher FBCache threshold.+ quant_active = cache_quant_active,+ logger = logger,+ )+ if view is pipe:+ cache_engaged = engaged+ cache_graph_break = cache_breaks_graph(cache_engaged)# Arm only where FBCache can engage: a live toggle drops fullgraph and retries every generation.cache_may_toggle = cache_auto_live and (cache_engaged is not None@@ -4580,7 +4637,7 @@ class VideoBackend:f"{FBCACHE_MIN_STEPS}; re-checked per generation")else:- cache_reason = "requested"+ cache_reason = static_decline or "requested"attention_engaged = None# HunyuanVideo-1.5 only, and once for the whole pipe (the installer fans out over every denoiser DiT itself).# Before apply_attention_backend below, so the requested kernel pins onto the new processors. Held off on@@ -4618,7 +4675,7 @@ class VideoBackend:speed_mode = effective_speed,# An auto cache that could still engage also drops fullgraph (FBCache under a fullgraph-compiled DiT# crashes)- cache_active = cache_engaged is not None or cache_may_toggle,+ cache_active = cache_graph_break or cache_may_toggle,offload_active = plan.offload_policy != "none",cuda_graph_default = False,)@@ -4679,6 +4736,7 @@ class VideoBackend:None if cache_auto else transformer_cache,cache_engaged or "off",cache_reason,+ RESOLVED_UNSUPPORTED if static_decline else None,),"transformer_quant": (transformer_quant_requested,@@ -4857,6 +4915,7 @@ class VideoBackend:_base_local_dir: Optional[str] = None,_h3_auto_denoiser_planned: Optional[str] = None,local_files_only: bool = False,+ transformer_cache: Optional[str] = None,) -> dict[str, Any]:"""Load MiniMax-H3 through its official Modular Diffusers workflow.@@ -5456,7 +5515,16 @@ class VideoBackend:attention_engaged or "native","cuDNN fused attention on NVIDIA when a speed profile is active",),- "transformer_cache": (None, "off", "not supported by this modular workflow"),+ "transformer_cache": (+ (+ transformer_cache,+ "off",+ "static step skip is not supported by this modular workflow",+ RESOLVED_UNSUPPORTED,+ )+ if _is_static_cache_request(transformer_cache)+ else (None, "off", "not supported by this modular workflow")+ ),"cuda_graph": (None,"on" if "cuda_graph" in speed_optims else "off",@@ -5812,6 +5880,13 @@ class VideoBackend:def generate_job_account(self) -> Optional[str]:return self._generate_job_account+ def static_skip_view(self) -> tuple:+ """(owner, ``transformer_cache_stats``) from one read; the owner is never part of status()."""+ state = self._state+ if state is None or state.transformer_cache != TC_STATIC:+ return None, None+ return static_skip_view(state.pipe)+def _run_generate(self,*,@@ -6388,7 +6463,11 @@ class VideoBackend:return_report(ticker.completed())+ static_skip = state.transformer_cache == TC_STATIC+def _on_step(p, step_index, timestep, callback_kwargs):+ if static_skip:+ mark_step_end(pipe)# diffusers calls this at the END of a loop iteration, after scheduler.step, so# the step's latent update is already submitted when the marker goes down.if cancel.is_set():@@ -6451,7 +6530,12 @@ class VideoBackend:+ ("reaches" if toggled else "is below")+ f" {FBCACHE_MIN_STEPS}")- if state.transformer_cache:+ if static_skip:+ # Without a step callback (HunyuanVideo-1.5) steps count per CFG branch from cache_context names.+ reset_static_step_skip(+ pipe, steps, step_signal = has_step_callback, owner = current_account_id()+ )+ elif state.transformer_cache:self._reset_step_cache(pipe)try:with torch.inference_mode(), progress_ctx(), sigma_ctx:@@ -6471,6 +6555,9 @@ class VideoBackend:# must not keep reporting it compiled (a forced-compile quantised load runs ~30x slower eager),# whether this render finished, was cancelled or failed.settle_compile_fallback(state, pipe, logger)+ if static_skip:+ logger.debug("video.step_skip: %s", static_skip_stats(pipe))+ reset_static_step_skip(pipe, None)if fam.modular_workflow:from .video_minimax_h3_vae import settle_h3_vae_fallbacksettle_h3_vae_fallback(state, pipe)@@ -7052,6 +7139,7 @@ class VideoBackend:diffusion_cuda_graph.uninstall_all(getattr(getattr(state, "pipe", None), "_unsloth_cuda_graphs", ()) or ())+ uninstall_static_step_skip(getattr(state, "pipe", None))del statetry:clear_gpu_cache()@@ -7115,6 +7203,7 @@ class VideoBackend:"speed_optims": [],"attention_backend": None,"transformer_cache": None,+ "transformer_cache_stats": None,"transformer_quant": None,"text_encoder_quant": None,"has_audio": False,@@ -7163,6 +7252,9 @@ class VideoBackend:"speed_optims": speed_optims,"attention_backend": state.attention_backend,"transformer_cache": state.transformer_cache,+ "transformer_cache_stats": (+ static_skip_stats(state.pipe) if state.transformer_cache == TC_STATIC else None+ ),"transformer_quant": state.transformer_quant,"text_encoder_quant": state.text_encoder_quant,"has_audio": fam.has_audio,diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex 3c8e85a5825..9f769938bbe 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -4871,19 +4871,25 @@ class VideoLoadRequest(BaseModel):"attention; xformers/aiter are memory-efficient (NVIDIA) / AMD ROCm. An unavailable ""kernel falls back to the default.",)- transformer_cache: Optional[Literal["off", "fbcache"]] = Field(+ transformer_cache: Optional[Literal["off", "fbcache", "static"]] = Field(None,description = "Step caching. fbcache = First-Block-Cache: reuse the transformer tail ""across denoise steps when the first block's residual barely changes. Unset = auto: ""engages only on speed_mode=max with a 20+ step schedule, otherwise uncached. An "- "explicit fbcache engages on every speed tier; incompatible models run uncached.",+ "explicit fbcache engages on every speed tier; incompatible models run uncached. "+ "static = skip denoiser calls on a fixed schedule (first 20% and last 10% of the steps "+ "always run, every other middle step is extrapolated from the last two computed "+ "outputs; 12+ steps only), which keeps compile fullgraph and the CUDA graph. "+ "Single-denoiser video-only families: a two-expert MoE (Wan2.2 A14B), a joint "+ "audio + video denoiser (LTX-2) and the MiniMax-H3 modular workflow run uncached. "+ "Never picked automatically.",)transformer_cache_threshold: Optional[float] = Field(None,ge = 0.0,le = 1.0,description = "FBCache residual threshold (higher = skips more steps = faster, lower "- "quality). null auto-picks the family default.",+ "quality). null auto-picks the family default. Ignored by static.",)transformer_quant: Optional[Literal["auto", "none", "off", "int8", "fp8", "nvfp4", "mxfp8"]] = (Field(@@ -5329,7 +5335,15 @@ class VideoStatusResponse(BaseModel):description = "Attention backend engaged via the diffusers dispatcher (e.g. ""_native_cudnn), or null for the default SDPA",)- transformer_cache: Optional[str] = Field(None, description = "Step cache engaged: fbcache | null")+ transformer_cache: Optional[str] = Field(+ None, description = "Step cache engaged: fbcache | static | null"+ )+ transformer_cache_stats: Optional[dict] = Field(+ None,+ description = "Static step skip only: the schedule (mode, head, tail, every, "+ "planned_skips) and the denoiser call counts (calls, computed, skipped) of the clip in "+ "flight, else of the last one. null for any other step cache.",+ )transformer_quant: Optional[str] = Field(None,description = "Dense transformer quant engaged on a pipeline load: int8 | fp8 | nvfp4 | "diff --git a/studio/backend/routes/video.py b/studio/backend/routes/video.pyindex a96ae605913..b6acd8b2e6a 100644--- a/studio/backend/routes/video.py+++ b/studio/backend/routes/video.py@@ -734,9 +734,20 @@ async def video_status(from core.inference.video import get_video_backendfrom hub.utils.host_paths import redact_host_paths- status_dict = get_video_backend().status()+ backend = get_video_backend()+ status_dict = backend.status()if account_access.resident_hidden("video", status_dict.get("repo_id")):return account_access.hidden_resident_response()+ # Step-skip counters trace a clip as it runs, which generate-progress hides from other accounts:+ # shown only to the account whose clip produced them, from one owner-then-stats read.+ if (+ status_dict.get("transformer_cache_stats") is not None+ and account_access.account_scope() is not None+ ):+ view = getattr(backend, "static_skip_view", None)+ owner, stats = view() if callable(view) else (None, status_dict["transformer_cache_stats"])+ visible = owner is None or owner == current_account_id()+ status_dict = {**status_dict, "transformer_cache_stats": stats if visible else None}# This route answers long after the request that resolved the reference ended, so there is# no handle in context to put back.return redact_host_paths(VideoStatusResponse(**status_dict), via_api_key = via_api_key)diff --git a/studio/backend/tests/test_diffusion_static_skip.py b/studio/backend/tests/test_diffusion_static_skip.pyindex 693a4b51bbd..130b49a5344 100644--- a/studio/backend/tests/test_diffusion_static_skip.py+++ b/studio/backend/tests/test_diffusion_static_skip.py@@ -231,6 +231,38 @@ def test_taylor1_extrapolates_in_timestep():assert torch.allclose(outs[i][0], torch.full((1, 4, 2), 1.0 - i / 25), atol = 1e-6)+def test_per_token_timestep_extrapolates_from_its_max():+ """Wan2.2 TI2V passes ``mask * t`` per token; keyed on the first element (0), taylor1 silently reused."""++ class _TokenDiT:+ def __init__(self):+ self.calls = 0++ def forward(+ self,+ hidden_states = None,+ timestep = None,+ return_dict = True,+ ):+ self.calls += 1+ return (torch.full((1, 4), float(timestep.max())),)++ pipe = _pipe(_TokenDiT())+ knobs = {**ss.static_skip_settings({}), "mode": "taylor1"}+ assert ss.install_static_step_skip(pipe, settings = knobs) == dcache.TC_STATIC+ steps = 25+ ss.reset_static_step_skip(pipe, steps)+ outs = []+ for i in range(steps):+ timestep = torch.full((1, 8), 1.0 - i / steps)+ timestep[0, 0] = 0.0+ outs.append(pipe.transformer.forward(timestep = timestep, return_dict = False))+ assert pipe.transformer.calls == 16+ for i, compute in enumerate(ss.static_schedule(steps)):+ if not compute:+ assert torch.allclose(outs[i][0], torch.full((1, 4), 1.0 - i / steps), atol = 1e-6)++def test_prefix_kv_extract_step_is_never_reused():for mode in ("reuse", "taylor1"):pipe = _installed(_DiT(prefix = 3), mode = mode, head = 0.0)@@ -782,7 +814,7 @@ def test_generate_fbcache_still_bypasses_and_resets(fake_runtime, tmp_path, monkbackend.unload()-def test_image_load_request_accepts_static_and_video_does_not():+def test_image_and_video_load_requests_accept_static():from pydantic import ValidationErrorfrom models.inference import DiffusionLoadRequest, VideoLoadRequest@@ -791,8 +823,12 @@ def test_image_load_request_accepts_static_and_video_does_not():DiffusionLoadRequest(model_path = "org/model", transformer_cache = "static").transformer_cache== "static")+ assert (+ VideoLoadRequest(model_path = "org/model", transformer_cache = "static").transformer_cache+ == "static"+ )with pytest.raises(ValidationError):- VideoLoadRequest(model_path = "org/model", transformer_cache = "static")+ VideoLoadRequest(model_path = "org/model", transformer_cache = "magic")def test_stats_of_the_last_generation_survive_the_post_render_reset():diff --git a/studio/backend/tests/test_video_static_skip.py b/studio/backend/tests/test_video_static_skip.pynew file mode 100644index 00000000000..a66587d732e--- /dev/null+++ b/studio/backend/tests/test_video_static_skip.py@@ -0,0 +1,583 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Video wiring of the static step skip, on test_video_backend's fake runtime with fakes that run a real CFG loop."""++from __future__ import annotations++import contextlib+import sys+import types++import pytest++from core.inference import diffusion_cache as dcache+from core.inference import diffusion_step_skip as ss+from core.inference.video import VideoBackend++from .test_video_backend import ( # noqa: F401 - fake_runtime is a fixture+ _FakeComponentsManager,+ _FakeHV15Pipe,+ _FakeModularPipeline,+ _FakeWanDiT,+ _FakeWanPipeSingle,+ _FakeWanPipelineSingle,+ _detect_load_family,+ fake_runtime,+)++WAN_5B = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"+WAN_A14B = "Wan-AI/Wan2.2-T2V-A14B-Diffusers"+HV15 = "hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-480p_t2v"++# Head 10 and tail 5 compute, the middle 35 alternate: 17 skips per CFG branch.+STEPS = 50+SKIPPED_PER_BRANCH = 17+++class _Tensorish:+ def __init__(self, value):+ self.value = value+ self.shape = (1, 4)++ def detach(self):+ return self++ def clone(self):+ return _Tensorish(self.value)+++class _LoopDiT(_FakeWanDiT):+ def __init__(self) -> None:+ super().__init__()+ self.computed = 0+ self.contexts: list = []++ @contextlib.contextmanager+ def cache_context(self, name, **kwargs):+ self.contexts.append(name)+ yield++ def forward(+ self,+ hidden_states = None,+ timestep = None,+ encoder_hidden_states = None,+ return_dict = True,+ ):+ self.computed += 1+ return (_Tensorish(self.computed),)+++class _LoopWanPipe(_FakeWanPipeSingle):+ """WanPipeline's order: both CFG branches, then ``callback_on_step_end`` for that step."""++ raise_at = None++ def __init__(self) -> None:+ super().__init__()+ self.transformer = _LoopDiT()+ self.components["transformer"] = self.transformer++ def __call__(+ self,+ *,+ prompt = None,+ negative_prompt = None,+ num_inference_steps = None,+ guidance_scale = None,+ width = None,+ height = None,+ num_frames = None,+ generator = None,+ callback_on_step_end = None,+ **kwargs,+ ):+ self.last_kwargs = {"num_inference_steps": num_inference_steps, **kwargs}+ # Spelled out, not factored: the FBCache gate reads this source for a cache_context.+ for i in range(int(num_inference_steps)):+ if self.raise_at is not None and i == self.raise_at:+ raise RuntimeError("denoise failed")+ for name in ("cond", "uncond"):+ with self.transformer.cache_context(name, step_index = i):+ self.transformer.forward(hidden_states = None, timestep = None, return_dict = False)+ if callback_on_step_end is not None:+ callback_on_step_end(self, i, 0, {})+ self.vae.decode(object())+ frames = [[object() for _ in range(int(num_frames or 1))]]+ return types.SimpleNamespace(frames = frames, audio = None)+++class _LoopHV15Pipe(_FakeHV15Pipe):+ def __init__(self) -> None:+ super().__init__()+ self.transformer = _LoopDiT()+ self.components["transformer"] = self.transformer++ def __call__(+ self,+ *,+ num_inference_steps = None,+ num_frames = None,+ **kwargs,+ ):+ self.last_kwargs = {"num_inference_steps": num_inference_steps, **kwargs}+ for _ in range(int(num_inference_steps)):+ for name in ("pred_cond", "pred_uncond"):+ with self.transformer.cache_context(name):+ self.transformer.forward(hidden_states = None, timestep = None, return_dict = False)+ self.scheduler.step()+ self.vae.decode(object())+ frames = [[object() for _ in range(int(num_frames or 1))]]+ return types.SimpleNamespace(frames = frames, audio = None)+++def loop_runtime(fake_runtime, monkeypatch):+ diffusers = sys.modules["diffusers"]+ made: dict = {}++ class _WanLoader:+ @classmethod+ def from_pretrained(cls, repo, **kwargs):+ if "a14b" in str(repo).lower():+ pipe = _FakeWanPipelineSingle.from_pretrained(repo, **kwargs)+ else:+ pipe = _LoopWanPipe()+ made["pipe"] = pipe+ return pipe++ class _HV15Loader:+ @classmethod+ def from_pretrained(cls, repo, **kwargs):+ made["pipe"] = _LoopHV15Pipe()+ return made["pipe"]++ monkeypatch.setattr(diffusers, "WanPipeline", _WanLoader, raising = False)+ monkeypatch.setattr(diffusers, "HunyuanVideo15Pipeline", _HV15Loader, raising = False)+ monkeypatch.setattr(+ sys.modules["torch"], "is_tensor", lambda obj: isinstance(obj, _Tensorish), raising = False+ )+ return made+++def _record_speed(monkeypatch):+ import core.inference.video as video_mod++ seen: list = []++ def fake_speed(pipe, target, **kwargs):+ seen.append(kwargs)+ return {"compiled": True, "cuda_graph": False}++ monkeypatch.setattr(video_mod, "apply_speed_optims", fake_speed)+ return seen++[email protected]("request_cache", ["off", "static"])+def test_static_load_keeps_the_uncached_speed_decisions(loop_runtime, monkeypatch, request_cache):+ seen = _record_speed(monkeypatch)+ backend = VideoBackend()+ status = backend.load_pipeline(+ WAN_5B, model_kind = "pipeline", speed_mode = "default", transformer_cache = request_cache+ )+ assert [kw["cache_active"] for kw in seen] == [False]+ assert status["speed_optims"] == ["compiled"]+ pipe = loop_runtime["pipe"]+ if request_cache == "static":+ assert status["transformer_cache"] == "static"+ entry = status["resolved"]["transformer_cache"]+ assert entry["value"] == "static" and entry["requested"] == "static"+ assert entry["status"] == "applied" and entry["reason"] == "requested"+ assert isinstance(pipe.transformer.__dict__["forward"], ss.StaticStepSkip)+ # Never the FBCache marker, which is what would run a CUDA graph eager.+ assert getattr(pipe.transformer, "_unsloth_step_cache", None) is None+ assert pipe.transformer.cache_config is None+ assert status["transformer_cache_stats"]["mode"] == "taylor1"+ else:+ assert status["transformer_cache"] is None+ assert status["transformer_cache_stats"] is None+ assert "forward" not in pipe.transformer.__dict__+ backend.unload()+++def test_fbcache_load_still_reports_an_active_cache(loop_runtime, monkeypatch):+ seen = _record_speed(monkeypatch)+ backend = VideoBackend()+ status = backend.load_pipeline(+ WAN_5B, model_kind = "pipeline", speed_mode = "default", transformer_cache = "fbcache"+ )+ assert status["transformer_cache"] == "fbcache"+ assert [kw["cache_active"] for kw in seen] == [True]+ assert status["transformer_cache_stats"] is None+ backend.unload()++[email protected]("request_cache", [None, "auto"])+def test_auto_never_installs_static(loop_runtime, monkeypatch, request_cache):+ import core.inference.video as video_mod++ monkeypatch.setattr(+ video_mod, "install_static_step_skip", lambda *a, **k: pytest.fail("auto picked static")+ )+ backend = VideoBackend()+ status = backend.load_pipeline(WAN_5B, model_kind = "pipeline", transformer_cache = request_cache)+ # Auto resolves per speed tier (FBCache on max, else uncached); never to static.+ assert status["transformer_cache"] in (None, "fbcache")+ backend.unload()+++def test_two_expert_moe_declines_static_with_a_reason(loop_runtime, monkeypatch):+ import core.inference.video as video_mod++ seen = _record_speed(monkeypatch)+ monkeypatch.setattr(+ video_mod, "install_static_step_skip", lambda *a, **k: pytest.fail("static on an MoE")+ )+ backend = VideoBackend()+ status = backend.load_pipeline(+ WAN_A14B, model_kind = "pipeline", speed_mode = "default", transformer_cache = "static"+ )+ assert status["transformer_cache"] is None+ entry = status["resolved"]["transformer_cache"]+ assert entry["requested"] == "static" and entry["value"] == "off"+ assert entry["status"] == "unsupported"+ assert "transformer_2" in entry["reason"]+ pipe = loop_runtime["pipe"]+ assert pipe.transformer.cache_config is None and pipe.transformer_2.cache_config is None+ assert all(kw["cache_active"] is False for kw in seen)+ backend.unload()+++def test_joint_audio_video_family_declines_static_with_a_reason(+ fake_runtime, tmp_path, monkeypatch+):+ import core.inference.video as video_mod++ monkeypatch.setattr(+ video_mod, "install_static_step_skip", lambda *a, **k: pytest.fail("static on LTX-2")+ )+ (tmp_path / "model.gguf").write_bytes(b"weights")+ backend = VideoBackend()+ status = backend.load_pipeline(+ str(tmp_path),+ gguf_filename = "model.gguf",+ base_repo = "Lightricks/LTX-2",+ family_override = "ltx-2",+ transformer_cache = "static",+ )+ assert status["transformer_cache"] is None+ entry = status["resolved"]["transformer_cache"]+ assert entry["requested"] == "static" and entry["status"] == "unsupported"+ assert "audio" in entry["reason"]+ backend.unload()+++def test_a_transformer_without_forward_runs_uncached_with_a_reason(fake_runtime):+ backend = VideoBackend()+ status = backend.load_pipeline(WAN_5B, model_kind = "pipeline", transformer_cache = "static")+ assert status["transformer_cache"] is None+ entry = status["resolved"]["transformer_cache"]+ assert entry["status"] == "unsupported" and "unavailable" in entry["reason"]+ backend.unload()+++def _static_backend(loop_runtime):+ backend = VideoBackend()+ backend.load_pipeline(WAN_5B, model_kind = "pipeline", transformer_cache = "static")+ return backend, loop_runtime["pipe"]+++def test_generate_skips_the_scheduled_steps_per_cfg_branch(loop_runtime):+ backend, pipe = _static_backend(loop_runtime)+ backend.generate(prompt = "a sloth", steps = STEPS)+ computed = 2 * (STEPS - SKIPPED_PER_BRANCH)+ assert pipe.transformer.computed == computed+ stats = backend.status()["transformer_cache_stats"]+ assert stats["planned_skips"] == SKIPPED_PER_BRANCH+ assert stats["stats"] == {+ "calls": 2 * STEPS,+ "computed": computed,+ "skipped": 2 * SKIPPED_PER_BRANCH,+ }+ from utils.account_context import current_account_id++ assert backend.static_skip_view()[0] == current_account_id()+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert pipe.transformer.computed == 2 * computed+ backend.generate(prompt = "a sloth", steps = 8)+ assert pipe.transformer.computed == 2 * computed + 16+ backend.unload()+ assert "forward" not in pipe.transformer.__dict__+ assert "cache_context" not in pipe.transformer.__dict__+++def test_generate_arms_with_the_step_callback_and_disarms_after(loop_runtime, monkeypatch):+ import core.inference.video as video_mod++ backend, pipe = _static_backend(loop_runtime)+ armed, marks = [], []+ real_reset, real_mark = video_mod.reset_static_step_skip, video_mod.mark_step_end+ monkeypatch.setattr(+ video_mod,+ "reset_static_step_skip",+ lambda p, steps, **k: armed.append((steps, k.get("step_signal")))+ or real_reset(p, steps, **k),+ )+ monkeypatch.setattr(video_mod, "mark_step_end", lambda p: marks.append(p) or real_mark(p))+ resets = []+ monkeypatch.setattr(+ type(backend), "_reset_step_cache", staticmethod(lambda p: resets.append(p))+ )+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert armed == [(STEPS, True), (None, None)]+ assert len(marks) == STEPS and all(p is pipe for p in marks)+ assert resets == []+ backend.unload()+++def test_a_failed_clip_drops_the_kept_outputs(loop_runtime):+ backend, pipe = _static_backend(loop_runtime)+ layer = pipe.transformer.__dict__["forward"]+ pipe.raise_at = 20+ with pytest.raises(RuntimeError, match = "denoise failed"):+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert layer.history == {} and layer.plan == ()+ pipe.raise_at = None+ before = pipe.transformer.computed+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert pipe.transformer.computed - before == 2 * (STEPS - SKIPPED_PER_BRANCH)+ backend.unload()+++def test_a_new_clip_does_not_report_the_previous_clips_counts(loop_runtime, monkeypatch):+ import core.inference.video as video_mod++ backend, pipe = _static_backend(loop_runtime)+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert backend.status()["transformer_cache_stats"]["stats"]["calls"] == 2 * STEPS+ # Status polled between arming the next clip and its first transformer call (prep).+ seen = []+ real_reset = video_mod.reset_static_step_skip++ def _reset(p, steps, **k):+ done = real_reset(p, steps, **k)+ if steps is not None:+ seen.append(backend.status()["transformer_cache_stats"]["stats"])+ return done++ monkeypatch.setattr(video_mod, "reset_static_step_skip", _reset)+ zeros = {"calls": 0, "computed": 0, "skipped": 0}+ pipe.raise_at = 0+ with pytest.raises(RuntimeError, match = "denoise failed"):+ backend.generate(prompt = "a sloth", steps = STEPS)+ assert seen == [zeros]+ assert backend.status()["transformer_cache_stats"]["stats"] == zeros+ backend.unload()+++def test_hv15_counts_branches_by_context_without_a_step_callback(loop_runtime, monkeypatch):+ import core.inference.video as video_mod++ armed = []+ real_reset = video_mod.reset_static_step_skip+ monkeypatch.setattr(+ video_mod,+ "reset_static_step_skip",+ lambda p, steps, **k: armed.append((steps, k.get("step_signal")))+ or real_reset(p, steps, **k),+ )+ backend = VideoBackend()+ status = backend.load_pipeline(HV15, model_kind = "pipeline", transformer_cache = "static")+ assert status["transformer_cache"] == "static"+ pipe = loop_runtime["pipe"]+ backend.generate(prompt = "a fox", steps = STEPS, num_frames = 9)+ assert "callback_on_step_end" not in pipe.last_kwargs+ assert armed[0] == (STEPS, False)+ assert pipe.transformer.computed == 2 * (STEPS - SKIPPED_PER_BRANCH)+ assert set(pipe.transformer.contexts) == {"pred_cond", "pred_uncond"}+ backend.unload()+++def _load_h3(backend, transformer_cache):+ from core.inference.video import _detect_load_family++ diffusers = sys.modules["diffusers"]+ diffusers.ComponentsManager = _FakeComponentsManager+ diffusers.ModularPipeline = _FakeModularPipeline+ fam = _detect_load_family("MiniMaxAI/MiniMax-H3", None, "minimax-h3")+ return backend._load_h3_modular_pipeline(+ diffusers = diffusers,+ torch = sys.modules["torch"],+ fam = fam,+ repo_id = "MiniMaxAI/MiniMax-H3",+ base = fam.base_repo,+ kind = "pipeline",+ dtype = sys.modules["torch"].bfloat16,+ device = "cpu",+ hf_token = None,+ memory_mode = None,+ _load_token = None,+ _base_local_dir = None,+ transformer_cache = transformer_cache,+ )+++def test_modular_workflow_reports_a_static_ask_as_unsupported(fake_runtime):+ backend = VideoBackend()+ status = _load_h3(backend, "static")+ entry = status["resolved"]["transformer_cache"]+ assert status["transformer_cache"] is None+ assert entry["requested"] == "static" and entry["value"] == "off"+ assert entry["status"] == "unsupported" and "modular" in entry["reason"]+ backend.unload()++[email protected]("request_cache", [None, "off", "fbcache", "bogus"])+def test_modular_workflow_keeps_its_record_for_every_other_ask(fake_runtime, request_cache):+ backend = VideoBackend()+ status = _load_h3(backend, request_cache)+ entry = status["resolved"]["transformer_cache"]+ assert entry["requested"] is None and entry["value"] == "off"+ assert entry["reason"] == "not supported by this modular workflow"+ backend.unload()+++def test_video_api_accepts_static_and_reports_its_stats():+ from pydantic import ValidationError++ from models.inference import VideoLoadRequest, VideoStatusResponse++ assert (+ VideoLoadRequest(model_path = "org/model", transformer_cache = "static").transformer_cache+ == "static"+ )+ with pytest.raises(ValidationError):+ VideoLoadRequest(model_path = "org/model", transformer_cache = "magic")+ stats = {"mode": "taylor1", "every": 2, "stats": {"calls": 100, "computed": 66, "skipped": 34}}+ status = VideoStatusResponse(+ loaded = True, transformer_cache = "static", transformer_cache_stats = stats+ )+ assert status.transformer_cache_stats == stats+ assert VideoStatusResponse(loaded = False).transformer_cache_stats is None+++def test_static_is_a_graph_keeping_mode():+ assert dcache.normalize_transformer_cache("static") == dcache.TC_STATIC+ assert dcache.cache_breaks_graph(dcache.TC_STATIC) is False+ assert dcache.cache_breaks_graph(dcache.TC_FBCACHE) is True+++def _native_h3_load(monkeypatch, tmp_path, transformer_cache):+ from pathlib import Path++ from core.inference import sd_cpp_backend, sd_cpp_engine+ from core.inference import video as video_mod++ class _Info:+ siblings: list = []++ class _Api:+ def __init__(self, **_kwargs):+ pass++ def model_info(self, *_args, **_kwargs):+ return _Info()++ class _Engine:+ def __init__(self, binary):+ self.binary = binary++ def version(self):+ return "stub-version"++ def _download(_repo, wanted, *_args, **_kwargs):+ path = tmp_path / Path(wanted).name+ path.write_bytes(b"x")+ return str(path)++ monkeypatch.setattr("huggingface_hub.HfApi", _Api)+ monkeypatch.setattr(+ video_mod,+ "resolve_diffusion_device_target",+ lambda: types.SimpleNamespace(backend = "cpu", device = "cpu", dtype = None),+ )+ monkeypatch.setattr(sd_cpp_backend, "_install_allowed", lambda: False)+ monkeypatch.setattr(sd_cpp_backend, "ensure_sd_cpp_binary", lambda **_k: "/existing/sd-cli")+ monkeypatch.setattr(sd_cpp_engine, "SdCppEngine", _Engine)+ monkeypatch.setattr("utils.hf_xet_fallback.hf_hub_download_with_xet_fallback", _download)+ import threading++ backend = VideoBackend()+ backend._run_load_h3_native(+ fam = _detect_load_family("leejet/MiniMax-H3-GGUF", None, "minimax-h3"),+ token = None,+ cancel_event = threading.Event(),+ repo_id = "leejet/MiniMax-H3-GGUF",+ gguf_filename = "minimax_h3_fl2va-Q4_K_M.gguf",+ transformer_cache = transformer_cache,+ )+ return backend+++def test_native_h3_reports_a_static_ask_as_unsupported(monkeypatch, tmp_path):+ backend = _native_h3_load(monkeypatch, tmp_path, "static")+ entry = (backend._state.resolved or {})["transformer_cache"]+ assert entry["requested"] == "static" and entry["value"] == "off"+ assert entry["status"] == "unsupported" and "sd.cpp" in entry["reason"]+ assert backend._state.transformer_cache is None+++def test_native_h3_keeps_no_cache_record_for_other_asks(monkeypatch, tmp_path):+ backend = _native_h3_load(monkeypatch, tmp_path, None)+ assert "transformer_cache" not in (backend._state.resolved or {})+++def test_load_pipeline_hands_the_cache_ask_to_the_native_path(monkeypatch):+ backend = VideoBackend()+ calls = []+ monkeypatch.setattr("core.inference.video._ensure_mp4_encoder_available", lambda: None)+ monkeypatch.setattr(backend, "_run_load_h3_native", lambda **kwargs: calls.append(kwargs))+ backend.load_pipeline(+ "leejet/MiniMax-H3-GGUF",+ gguf_filename = "minimax_h3_fl2va-Q4_K_M.gguf",+ family_override = "minimax-h3",+ model_kind = "gguf",+ transformer_cache = "static",+ )+ assert calls and calls[0]["transformer_cache"] == "static"+++ "scope, owner, shown",+ [(None, "b", True), ("scoped", "a", True), ("scoped", "b", False), ("scoped", None, True)],+)+def test_status_route_shows_skip_counts_only_to_their_producer(monkeypatch, scope, owner, shown):+ import asyncio++ from core.inference import video as video_mod+ from hub.services.models import account_access+ from routes import video as routes++ stats = {+ "mode": "taylor1",+ "planned_skips": 17,+ "stats": {"calls": 100, "computed": 66, "skipped": 34},+ }+ # The view's snapshot, not the earlier status() read, is what the caller gets.+ fresh = {**stats, "stats": {"calls": 40, "computed": 26, "skipped": 14}}++ class _Backend:+ def status(self):+ return {"loaded": True, "transformer_cache": "static", "transformer_cache_stats": stats}++ def static_skip_view(self):+ return owner, fresh++ monkeypatch.setattr(video_mod, "get_video_backend", lambda: _Backend())+ monkeypatch.setattr(account_access, "resident_hidden", lambda *a, **k: False)+ monkeypatch.setattr(account_access, "account_scope", lambda: scope)+ monkeypatch.setattr(routes, "current_account_id", lambda: "a")+ body = asyncio.run(routes.video_status(current_subject = "u", via_api_key = False))+ assert body.transformer_cache == "static"+ assert body.transformer_cache_stats == ((fresh if scope else stats) if shown else None)diff --git a/studio/frontend/src/features/video/api.ts b/studio/frontend/src/features/video/api.tsindex ac13cb4efa7..233da57e887 100644--- a/studio/frontend/src/features/video/api.ts+++ b/studio/frontend/src/features/video/api.ts@@ -69,6 +69,12 @@ export interface VideoStatus {speed_optims: string[];attention_backend?: string | null;transformer_cache?: string | null;+ transformer_cache_stats?: {+ mode?: string;+ every?: number;+ planned_skips?: number;+ stats?: { calls?: number; computed?: number; skipped?: number };+ } | null;// Dense DiT precision actually engaged ("int8" | "fp8" | ...) or null for bf16.transformer_quant?: string | null;// Text-encoder quant actually engaged ("fp8" | "fp8_dynamic" | "int8" | "nvfp4") or null for dense bf16.@@ -138,7 +144,7 @@ export interface VideoLoadRequest {| "sage"| "xformers"| "aiter";- transformer_cache?: "off" | "fbcache";+ transformer_cache?: "off" | "fbcache" | "static";transformer_cache_threshold?: number;// Dense DiT precision on full-pipeline loads (omit for the hardware ladder; "none" pins bf16).// GGUF / single-file checkpoints carry their own.diff --git a/studio/frontend/src/features/video/video-page.tsx b/studio/frontend/src/features/video/video-page.tsxindex 110c764fddd..ad68872b877 100644--- a/studio/frontend/src/features/video/video-page.tsx+++ b/studio/frontend/src/features/video/video-page.tsx@@ -1005,7 +1005,7 @@ function VideoGenerator({const [attentionBackend, setAttentionBackend] = useState<"auto" | "native" | "cudnn" | "flash3" | "sage">("auto");- const [transformerCache, setTransformerCache] = useState<"auto" | "off" | "fbcache">("auto");+ const [transformerCache, setTransformerCache] = useState<"auto" | "off" | "fbcache" | "static">("auto");const [transformerQuant, setTransformerQuant] = useState<"auto" | "none" | "fp8" | "int8" | "nvfp4" | "mxfp8">("auto");@@ -3462,7 +3462,7 @@ function VideoGenerator({)}<AdvancedSelectlabel="Step cache"- hint="First-Block-Cache reuses the transformer tail across steps for many-step models (small quality cost). Auto turns it on only on the Max speed tier at 20+ steps, re-checked per clip."+ hint="First-Block-Cache reuses the transformer tail across steps for many-step models (small quality cost). Auto turns it on only on the Max speed tier at 20+ steps, re-checked per clip. Static skip extrapolates every other middle step on a fixed schedule (12+ steps) and keeps the compile and CUDA graph; Wan2.2 A14B, LTX-2 and MiniMax-H3 run uncached. Never picked by Auto."badge={<ResolvedBadge status={status} controlKey="transformer_cache" />}value={transformerCache}onValueChange={(v) => setTransformerCache(v as typeof transformerCache)}@@ -3470,6 +3470,7 @@ function VideoGenerator({["auto", "Auto"],["off", "Off"],["fbcache", "First-Block-Cache"],+ ["static", "Static skip"],]}/><LoadedBuildSummary status={status} />