Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

9.2 KB
Raw Blame
"""Tests for Anima scheduler registry."""

import typing

from diffusers.schedulers.scheduling_utils import SchedulerMixin

from invokeai.backend.flux.schedulers import (
    ANIMA_SCHEDULER_LABELS,
    ANIMA_SCHEDULER_MAP,
    ANIMA_SCHEDULER_NAME_VALUES,
)


def test_anima_scheduler_map_entries_are_class_kwargs_tuples():
    """Every entry must be (SchedulerClass, kwargs_dict)."""
    for name, entry in ANIMA_SCHEDULER_MAP.items():
        assert isinstance(entry, tuple), f"{name} is not a tuple"
        assert len(entry) == 2, f"{name} tuple has wrong arity"
        cls, kwargs = entry
        assert isinstance(cls, type) and issubclass(cls, SchedulerMixin), (
            f"{name} first element is not a SchedulerMixin subclass"
        )
        assert isinstance(kwargs, dict), f"{name} second element is not a dict"


def test_anima_scheduler_map_entries_can_be_constructed():
    """Every entry must construct cleanly by splatting its kwargs."""
    for name, (cls, kwargs) in ANIMA_SCHEDULER_MAP.items():
        scheduler = cls(num_train_timesteps=1000, **kwargs)
        assert isinstance(scheduler, SchedulerMixin), f"{name} did not produce a SchedulerMixin"


def test_anima_scheduler_labels_cover_every_map_key():
    for name in ANIMA_SCHEDULER_MAP.keys():
        assert name in ANIMA_SCHEDULER_LABELS, f"{name} has no label"


def test_anima_scheduler_map_includes_new_dpmpp_entries():
    assert "dpmpp_2m" in ANIMA_SCHEDULER_MAP
    assert "dpmpp_2m_sde" in ANIMA_SCHEDULER_MAP


def test_anima_dpmpp_2m_uses_flow_prediction():
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT

    cls, kwargs = ANIMA_SCHEDULER_MAP["dpmpp_2m"]
    assert kwargs["prediction_type"] == "flow_prediction"
    assert kwargs["use_flow_sigmas"] is True
    assert kwargs["flow_shift"] == ANIMA_SHIFT
    assert kwargs["solver_order"] == 2
    assert "algorithm_type" not in kwargs  # deterministic, default algorithm


def test_anima_dpmpp_2m_sde_uses_sde_algorithm():
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT

    cls, kwargs = ANIMA_SCHEDULER_MAP["dpmpp_2m_sde"]
    assert kwargs["prediction_type"] == "flow_prediction"
    assert kwargs["use_flow_sigmas"] is True
    assert kwargs["flow_shift"] == ANIMA_SHIFT
    assert kwargs["algorithm_type"] == "sde-dpmsolver++"
    assert kwargs["solver_order"] == 2


def test_anima_dpmpp_2m_produces_anima_compatible_sigma_schedule():
    """The DPM++ 2M scheduler, when run through the same dispatch logic as
    anima_denoise._run_diffusion, must produce a sigma schedule equivalent
    to Anima's reference schedule (loglinear_timestep_shift with shift=3.0).

    On diffusers 0.35.1, DPMSolverMultistepScheduler.set_timesteps does not
    accept `sigmas=`, so the runtime falls back to num_inference_steps and
    relies on the scheduler's internal flow_shift=3.0 to compute equivalent
    sigmas. This test verifies that equivalence end-to-end.
    """
    import inspect

    from invokeai.app.invocations.anima.anima_denoise import loglinear_timestep_shift
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT

    num_steps = 10

    # Reference: Anima's own pre-shifted sigma schedule.
    anima_sigmas = [loglinear_timestep_shift(ANIMA_SHIFT, 1.0 - i / num_steps) for i in range(num_steps + 1)]

    cls, kwargs = ANIMA_SCHEDULER_MAP["dpmpp_2m"]
    scheduler = cls(num_train_timesteps=1000, **kwargs)

    # Mirror anima_denoise.py:502-506 dispatch.
    sig = inspect.signature(scheduler.set_timesteps)
    if "sigmas" in sig.parameters:
        scheduler.set_timesteps(sigmas=anima_sigmas, device="cpu")
    else:
        scheduler.set_timesteps(num_inference_steps=num_steps, device="cpu")

    diffusers_sigmas = [float(s) for s in scheduler.sigmas[: len(anima_sigmas)]]
    max_diff = max(abs(a - b) for a, b in zip(anima_sigmas, diffusers_sigmas, strict=True))
    assert max_diff < 1e-3, f"DPM++ 2M sigma schedule diverges from Anima reference (max abs diff = {max_diff:.6f})"


def test_anima_dpmpp_2m_with_denoising_start_honors_clipped_schedule():
    """DPM++ img2img: the set_begin_index path must start at the correct sigma.

    When DPMSolverMultistepScheduler doesn't accept sigmas=, anima_denoise falls back to
    set_timesteps(num_inference_steps=full_steps) + set_begin_index(start_idx).  The
    effective first sigma must match the clipped Anima reference schedule within 1e-3.
    """
    import inspect

    from invokeai.app.invocations.anima.anima_denoise import loglinear_timestep_shift
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT

    num_steps = 30
    denoising_start = 0.5
    start_idx = int(denoising_start * num_steps)  # mirrors anima_denoise clipping math

    full_sigmas = [loglinear_timestep_shift(ANIMA_SHIFT, 1.0 - i / num_steps) for i in range(num_steps + 1)]
    expected_first_sigma = full_sigmas[start_idx]

    cls, kwargs = ANIMA_SCHEDULER_MAP["dpmpp_2m"]
    scheduler = cls(num_train_timesteps=1000, **kwargs)
    sig = inspect.signature(scheduler.set_timesteps)

    if "sigmas" in sig.parameters:
        # Future diffusers: sigmas= supported, clipped schedule passed directly.
        scheduler.set_timesteps(sigmas=full_sigmas[start_idx:], device="cpu")
        actual_first_sigma = float(scheduler.sigmas[0])
    else:
        # Current diffusers: use set_begin_index on the full schedule.
        scheduler.set_timesteps(num_inference_steps=num_steps, device="cpu")
        scheduler.set_begin_index(start_idx)
        actual_first_sigma = float(scheduler.sigmas[start_idx])

    assert abs(actual_first_sigma - expected_first_sigma) < 1e-3, (
        f"DPM++ first sigma with denoising_start=0.5: got {actual_first_sigma:.6f}, expected {expected_first_sigma:.6f}"
    )


def test_anima_set_begin_index_path_step_count_with_denoising_end():
    """set_begin_index fallback must honour denoising_end, not just denoising_start.

    Regression test: the old formula (len(timesteps) - begin_index) ignored denoising_end
    and ran past it. For steps=30, denoising_start=0.2, denoising_end=0.8 the correct
    step count is 18, not 24.
    """
    import inspect

    from invokeai.app.invocations.anima.anima_denoise import loglinear_timestep_shift
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT

    num_steps = 30
    denoising_start = 0.2
    denoising_end = 0.8

    # Reference step count from the Euler path (clipped sigmas).
    full_sigmas = [loglinear_timestep_shift(ANIMA_SHIFT, 1.0 - i / num_steps) for i in range(num_steps + 1)]
    total_sigmas = len(full_sigmas)
    start_idx = int(denoising_start * (total_sigmas - 1))
    end_idx = int(denoising_end * (total_sigmas - 1)) + 1
    expected_steps = (end_idx - start_idx) - 1  # 18

    cls, kwargs = ANIMA_SCHEDULER_MAP["dpmpp_2m"]
    scheduler = cls(num_train_timesteps=1000, **kwargs)
    sig = inspect.signature(scheduler.set_timesteps)

    scheduler_begin_index = int(denoising_start * num_steps)
    if "sigmas" in sig.parameters:
        clipped = full_sigmas[start_idx:end_idx]
        scheduler.set_timesteps(sigmas=clipped, device="cpu")
        num_scheduler_steps = len(scheduler.timesteps)
    else:
        scheduler.set_timesteps(num_inference_steps=num_steps, device="cpu")
        scheduler.set_begin_index(scheduler_begin_index)
        num_scheduler_steps = int(denoising_end * num_steps) - scheduler_begin_index

    assert num_scheduler_steps == expected_steps, (
        f"DPM++ scheduler step count with denoising_start={denoising_start}, "
        f"denoising_end={denoising_end}: got {num_scheduler_steps}, expected {expected_steps}"
    )


def test_anima_literal_covers_every_map_key():
    """Catch the silent failure mode where a new entry lands in the map but
    the Literal isn't updated — Pydantic validation would still accept it
    via runtime introspection but type-check tooling would not."""
    literal_values = set(typing.get_args(ANIMA_SCHEDULER_NAME_VALUES))
    for name in ANIMA_SCHEDULER_MAP:
        assert name in literal_values, f"{name} is in the map but missing from the Literal"


def test_anima_scheduler_literal_includes_er_sde():
    """er_sde must appear in the literal type, have a label, and be
    registered in ANIMA_SCHEDULER_MAP for dispatch through the universal
    scheduler path."""
    literal_args = typing.get_args(ANIMA_SCHEDULER_NAME_VALUES)
    assert "er_sde" in literal_args
    assert "er_sde" in ANIMA_SCHEDULER_LABELS
    assert ANIMA_SCHEDULER_LABELS["er_sde"] == "ER-SDE"
    assert "er_sde" in ANIMA_SCHEDULER_MAP


def test_anima_scheduler_map_er_sde_entry():
    """ANIMA_SCHEDULER_MAP['er_sde'] must map to ERSDEScheduler with rectified-flow kwargs.

    This is the wiring that lets Anima dispatch er_sde through the universal scheduler
    path (replacing the legacy elif is_er_sde: branch in anima_denoise.py).
    """
    from invokeai.backend.flux.schedulers import ANIMA_SHIFT
    from invokeai.backend.rectified_flow.er_sde_scheduler import ERSDEScheduler

    assert "er_sde" in ANIMA_SCHEDULER_MAP, "er_sde must be in ANIMA_SCHEDULER_MAP"
    cls, kwargs = ANIMA_SCHEDULER_MAP["er_sde"]
    assert cls is ERSDEScheduler
    assert kwargs["use_flow_sigmas"] is True
    assert kwargs["prediction_type"] == "flow_prediction"
    assert kwargs["solver_order"] == 3
    assert kwargs["stochastic"] is True
    assert kwargs["flow_shift"] == ANIMA_SHIFT