Skip to content

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.

29.2 KB
Raw Blame
# 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"

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)


@pytest.fixture
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


@pytest.mark.parametrize("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)
        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()


@pytest.mark.parametrize("request_cache", [None, "auto"])
def test_default_speed_never_auto_skips_on_wan_5b(loop_runtime, monkeypatch, request_cache):
    monkeypatch.delenv(dcache.ENV_AUTO_STEP_SKIP, raising = False)
    backend = VideoBackend()
    status = backend.load_pipeline(
        WAN_5B, model_kind = "pipeline", speed_mode = "default", transformer_cache = request_cache
    )
    assert status["transformer_cache"] is None
    assert "forward" not in loop_runtime["pipe"].transformer.__dict__
    backend.generate(prompt = "a sloth", steps = STEPS)
    assert loop_runtime["pipe"].transformer.computed == 2 * STEPS
    backend.unload()


@pytest.mark.parametrize("request_cache", [None, "auto"])
def test_auto_installs_the_measured_static_skip_on_wan_5b(loop_runtime, monkeypatch, request_cache):
    monkeypatch.delenv(dcache.ENV_AUTO_STEP_SKIP, raising = False)
    seen = _record_speed(monkeypatch)
    backend = VideoBackend()
    status = backend.load_pipeline(
        WAN_5B, model_kind = "pipeline", speed_mode = "max", transformer_cache = request_cache
    )
    assert status["transformer_cache"] == "static"
    entry = status["resolved"]["transformer_cache"]
    assert entry["value"] == "static" and entry["requested"] is None
    assert "UNSLOTH_DIFFUSION_AUTO_STEP_SKIP" in entry["reason"]
    layer = loop_runtime["pipe"].transformer.__dict__["forward"]
    assert (layer.every, layer.auto, layer.min_steps) == (3, True, 50)
    # Kept graph decisions, and the generate-time FBCache toggle is never armed over it.
    assert [kw["cache_active"] for kw in seen] == [False]
    assert backend._state.cache_auto is False
    backend.generate(prompt = "a sloth", steps = STEPS)
    skipped = len(ss.static_schedule(STEPS, every = 3, min_steps = 50)) - sum(
        ss.static_schedule(STEPS, every = 3, min_steps = 50)
    )
    assert loop_runtime["pipe"].transformer.computed == 2 * (STEPS - skipped)
    backend.unload()


def test_auto_skip_leaves_a_keyframe_clip_unskipped(loop_runtime, monkeypatch):
    monkeypatch.delenv(dcache.ENV_AUTO_STEP_SKIP, raising = False)
    backend = VideoBackend()
    backend.load_pipeline(WAN_5B, model_kind = "pipeline", speed_mode = "max")
    real = VideoBackend._resolve_keyframes

    def with_first_frame(*args, **kwargs):
        first, last, width, height, conditioning = real(*args, **kwargs)
        return object(), last, width, height, conditioning

    monkeypatch.setattr(VideoBackend, "_resolve_keyframes", staticmethod(with_first_frame))

    # A first frame on Wan2.2-TI2V-5B runs its image-to-video class over the loaded pipe (video_i2v);
    # the fake runtime only knows the text-to-video one, so give it a class to swap to.
    class _LoopWanI2V(_LoopWanPipe):
        def __init__(self, **_components):  # the template the swap reads; nothing to set
            pass

    monkeypatch.setattr(
        sys.modules["diffusers"], "WanImageToVideoPipeline", _LoopWanI2V, raising = False
    )
    armed = []
    import core.inference.video as video_mod

    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("compute_all")))
        or real_reset(p, steps, **k),
    )
    try:
        backend.generate(prompt = "a sloth", steps = STEPS)
    except Exception:  # noqa: BLE001 - the fake pipe may not take an image; the arming is what is under test
        pass
    assert armed and armed[0] == (STEPS, True)
    backend.unload()


@pytest.mark.parametrize("request_cache", [None, "auto"])
def test_auto_kill_switch_restores_the_previous_auto(loop_runtime, monkeypatch, request_cache):
    import core.inference.video as video_mod

    monkeypatch.setenv(dcache.ENV_AUTO_STEP_SKIP, "0")
    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)
    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
    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,
    speed_mode = None,
):
    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,
        speed_mode = speed_mode,
    )


class _H3DiT:
    """MiniMax-H3's denoiser: one call per step (guidance-distilled), returning (video velocity, audio velocity)."""

    def __init__(self) -> None:
        self.computed = 0

    def __call__(self, *args, **kwargs):
        # nn.Module.__call__ reads the INSTANCE forward, which is where the skip layer sits.
        return self.forward(*args, **kwargs)

    def forward(
        self,
        hidden_states = None,
        audio_hidden_states = None,
        timestep = None,
        return_dict = True,
    ):
        self.computed += 1
        return _Tensorish(self.computed), _Tensorish(-self.computed)


class _H3LoopPipe:
    """The modular denoise loop: transformer, then components.scheduler.step, once per step; no step callback."""

    def __init__(self, inner) -> None:
        object.__setattr__(self, "_inner", inner)
        object.__setattr__(self, "transformer", _H3DiT())
        object.__setattr__(self, "outputs", [])

    def __getattr__(self, name):
        return getattr(object.__getattribute__(self, "_inner"), name)

    def __setattr__(self, name, value):
        setattr(object.__getattribute__(self, "_inner"), name, value)

    def __call__(
        self,
        *,
        num_inference_steps = None,
        num_frames = None,
        **kwargs,
    ):
        # H3 grid points include the terminal 0
        for _ in range(int(num_inference_steps or 2) - 1):
            self.outputs.append(self.transformer(timestep = None, return_dict = False))
            self.scheduler.step(object(), 0, object())
        return {"videos": [[object() for _ in range(int(num_frames or 1))]], "audio": None}


@pytest.fixture
def h3_runtime(fake_runtime, monkeypatch):
    from .test_video_backend import _FakeModularPipe

    class _Loader:
        @classmethod
        def from_pretrained(cls, path, **kwargs):
            _FakeModularPipeline.instance = _H3LoopPipe(_FakeModularPipe())
            return _FakeModularPipeline.instance

    monkeypatch.setattr(_FakeModularPipeline, "from_pretrained", _Loader.from_pretrained)
    monkeypatch.setattr(
        sys.modules["torch"], "is_tensor", lambda obj: isinstance(obj, _Tensorish), raising = False
    )
    monkeypatch.delenv(dcache.ENV_AUTO_STEP_SKIP, raising = False)
    return _FakeModularPipeline


def _h3_fwd_calls(pipe):
    return pipe.transformer.computed


def test_modular_workflow_engages_an_explicit_static_skip(h3_runtime, monkeypatch):
    monkeypatch.setattr(dcache, "AUTO_STATIC_SKIP", {})
    backend = VideoBackend()
    status = _load_h3(backend, "static")
    entry = status["resolved"]["transformer_cache"]
    assert status["transformer_cache"] == "static"
    assert entry["requested"] == "static" and entry["value"] == "static"
    assert entry["status"] == "applied" and entry["reason"] == "requested"
    pipe = h3_runtime.instance
    assert isinstance(pipe.transformer.__dict__["forward"], ss.StaticStepSkip)
    backend.generate(prompt = "a sloth", steps = 30, num_frames = 124, width = 960, height = 544)
    plan = ss.static_schedule(30)
    assert _h3_fwd_calls(pipe) == sum(plan)
    stats = backend.status()["transformer_cache_stats"]["stats"]
    assert stats == {"calls": 30, "computed": sum(plan), "skipped": 30 - sum(plan)}
    assert all(type(o) is tuple and len(o) == 2 for o in pipe.outputs)
    backend.unload()
    assert "forward" not in pipe.transformer.__dict__


def test_modular_workflow_auto_follows_the_table_and_the_kill_switch(h3_runtime, monkeypatch):
    monkeypatch.setattr(
        dcache, "AUTO_STATIC_SKIP", {"minimaxai/minimax-h3": {"default": 2, "max": 2}}
    )
    backend = VideoBackend()
    status = _load_h3(backend, None)
    assert status["transformer_cache"] is None
    assert "forward" not in h3_runtime.instance.transformer.__dict__
    backend.unload()
    backend = VideoBackend()
    status = _load_h3(backend, None, speed_mode = "max")
    entry = status["resolved"]["transformer_cache"]
    assert status["transformer_cache"] == "static"
    assert entry["requested"] is None and entry["value"] == "static"
    assert "UNSLOTH_DIFFUSION_AUTO_STEP_SKIP" in entry["reason"]
    layer = h3_runtime.instance.transformer.__dict__["forward"]
    assert layer.auto is True and layer.min_steps == dcache.AUTO_STATIC_MIN_STEPS
    backend.generate(prompt = "a sloth", steps = 16, num_frames = 124, width = 960, height = 544)
    assert _h3_fwd_calls(h3_runtime.instance) == 16
    backend.unload()

    monkeypatch.setenv(dcache.ENV_AUTO_STEP_SKIP, "0")
    backend = VideoBackend()
    status = _load_h3(backend, None, speed_mode = "max")
    assert status["transformer_cache"] is None
    assert "forward" not in h3_runtime.instance.transformer.__dict__
    backend.unload()


@pytest.mark.parametrize("request_cache", ["fbcache", "bogus"])
def test_modular_workflow_reports_other_caches_as_unsupported(h3_runtime, request_cache):
    backend = VideoBackend()
    status = _load_h3(backend, request_cache)
    entry = status["resolved"]["transformer_cache"]
    assert status["transformer_cache"] is None
    assert entry["requested"] == request_cache and entry["value"] == "off"
    assert entry["status"] == "unsupported" and "static" in entry["reason"]
    backend.unload()


def test_modular_workflow_off_and_unlisted_auto_run_uncached(h3_runtime, monkeypatch):
    monkeypatch.setattr(dcache, "AUTO_STATIC_SKIP", {})
    for ask in ("off", None):
        backend = VideoBackend()
        status = _load_h3(backend, ask)
        assert status["transformer_cache"] is None
        assert status["resolved"]["transformer_cache"]["value"] == "off"
        assert "forward" not in h3_runtime.instance.transformer.__dict__
        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"


@pytest.mark.parametrize(
    "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},
    }
    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)