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.

27.5 KB
Raw Blame
"""The LTX-2 loaders over toy checkpoints in the official key layout.

The transformer tests build the real ``LTX2VideoTransformer3DModel`` at toy size, write its weights
out under the *official* names (the inverse of the renames the loader applies) and hand that to the
loader: what is pinned is the whole conversion chain landing every tensor, the packed formats staying
packed under renamed paths too, and cache room being reserved before any weight widens -- the
contract the loader-seam driver records as ``order`` (reservations made before each observed step)
and ``dtypes_at_make_room`` (the working dict's dtypes when room was first made).
"""

import json
import logging
from pathlib import Path
from types import SimpleNamespace

import pytest
import safetensors.torch
import torch

from invokeai.backend.ltx2 import component_configs as cc
from invokeai.backend.model_manager.configs.gemma4_encoder import Gemma4Encoder_Gemma4Encoder_LTX2_Config
from invokeai.backend.model_manager.configs.ltx2_duration_head import LTX2DurationHead_Checkpoint_Config
from invokeai.backend.model_manager.configs.main import Main_Checkpoint_LTX2_Config, Main_Diffusers_LTX2_Config
from invokeai.backend.model_manager.load.model_loaders import ltx2
from invokeai.backend.model_manager.load.model_loaders.ltx2 import (
    LTX2CheckpointModel,
    LTX2DurationHeadLoader,
    LTX2FolderModel,
    LTX2Gemma4EncoderModel,
)
from invokeai.backend.model_manager.taxonomy import LTX2VariantType, SubModelType
from invokeai.backend.quantization.int8_convrot import Int8ConvrotLinear
from invokeai.backend.quantization.nvfp4 import NVFP4Linear
from tests.backend.model_manager.load.ltx2_transformer_fixture import (
    TINY_TRANSFORMER,
    official_name,
    tiny_transformer_checkpoint,
)
from tests.fixtures.loader_seams import Seam, prepare
from tests.fixtures.quantized_payloads import comfy_quant_marker, nvfp4_signed_tensors

COMPUTE_DTYPE = torch.bfloat16


def _tiny_geometry(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(cc, "LTX2_5_TRANSFORMER_CONFIG", TINY_TRANSFORMER)


def _transformer_seam() -> Seam:
    return Seam(
        loader=LTX2CheckpointModel,
        module=ltx2,
        load_file_host=safetensors.torch,
        compute_dtype=COMPUTE_DTYPE,
        patches_device=True,
    )


def _checkpoint_config(
    tmp_path: Path,
    sd: dict[str, torch.Tensor],
    name: str = "ltx-2.5-22b-dev_diffusion_model.safetensors",
    metadata: dict[str, str] | None = None,
):
    """The config for a checkpoint written to disk for real: the header readers open the path
    themselves, so a served state dict alone would leave them reading an empty file."""
    path = tmp_path / name
    safetensors.torch.save_file(sd, path, metadata=metadata)
    return Main_Checkpoint_LTX2_Config.model_construct(
        path=str(path), variant=LTX2VariantType.Dev, generation="2.5", fp8_storage=None
    )


OBSERVED = ("cast_state_dict", "install_int8_convrot_layers", "install_nvfp4_layers")

# Layers whose official and diffusers spellings differ: what the side channel has to follow.
INT8_LAYER = "proj_in"  # official: patchify_proj
NVFP4_LAYER = "time_embed.emb.timestep_embedder.linear_1"  # official: adaln_single.…


def _int8_checkpoint() -> tuple[dict[str, torch.Tensor], torch.Tensor]:
    sd = tiny_transformer_checkpoint()
    key = official_name(f"{INT8_LAYER}.weight")
    dense = sd[key]
    scale = dense.abs().amax(dim=1, keepdim=True) / 127.0
    sd[key] = torch.round(dense / scale).clamp(-127, 127).to(torch.int8)
    sd[official_name(f"{INT8_LAYER}.weight_scale")] = scale.float()
    sd[official_name(f"{INT8_LAYER}.comfy_quant")] = comfy_quant_marker({"format": "int8_tensorwise", "convrot": False})
    return sd, dense


def _room_was_made_before(run, step: str) -> bool:
    return any(name == step and rooms > 0 for name, rooms in run.order)


def test_a_dense_bf16_file_loads_every_tensor_reserving_before_the_cast(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint()
    run = prepare(
        _transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry, observe=OBSERVED
    )
    model = run.load(_checkpoint_config(tmp_path, sd))

    assert not any(p.is_meta for p in model.parameters())
    assert {p.dtype for p in model.parameters()} == {COMPUTE_DTYPE}
    assert model.config.use_keyframes_abs_pos_embedding is True
    assert run.casting_calls == 1, "a dense file is what the FP8 Storage policy applies to"
    assert _room_was_made_before(run, "cast_state_dict")
    # The working dict still held the on-disk dtype when room was made: reserved for what will
    # exist, not for what was already made.
    assert set(run.dtypes_at_make_room.values()) == {torch.float32}


def test_an_int8_convrot_layer_stays_packed_under_its_renamed_path(monkeypatch, tmp_path) -> None:
    sd, dense = _int8_checkpoint()
    run = prepare(
        _transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry, observe=OBSERVED
    )
    model = run.load(_checkpoint_config(tmp_path, sd))

    layer = model.get_submodule(INT8_LAYER)
    assert isinstance(layer, Int8ConvrotLinear)
    assert layer.weight.dtype == torch.int8
    torch.testing.assert_close(
        layer._dequantized_weight(torch.device("cpu"), torch.float32), dense, atol=0.05, rtol=0.05
    )
    assert run.casting_calls == 0, "packed formats are never re-encoded by the FP8 Storage pass"
    # The shared int8 helper makes the reservation itself, before its own split and cast: what is
    # observable from outside is that the working dict still held the int8 codes when it did.
    assert run.reserved and torch.int8 in run.dtypes_at_make_room.values()


def test_a_header_named_nvfp4_layer_stays_packed_under_its_renamed_path(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint()
    official = official_name(f"{NVFP4_LAYER}.weight")
    shape = tuple(sd.pop(official).shape)
    assert shape[0] % 128 == 0 and shape[1] % 64 == 0, "the toy layer must fit nvfp4's tiles"
    module_path = official[len("model.diffusion_model.") : -len(".weight")]
    tensors, expected = nvfp4_signed_tensors(module_path, torch.randint(0, 2, shape, dtype=torch.bool))
    sd.update({f"model.diffusion_model.{k}": v for k, v in tensors.items()})
    metadata = {"_quantization_metadata": json.dumps({"layers": {module_path: {"format": "nvfp4"}}})}
    run = prepare(
        _transformer_seam(), monkeypatch, state_dict=sd, metadata=metadata, geometry=_tiny_geometry, observe=OBSERVED
    )
    model = run.load(_checkpoint_config(tmp_path, sd, metadata=metadata))

    layer = model.get_submodule(NVFP4_LAYER)
    assert isinstance(layer, NVFP4Linear)
    torch.testing.assert_close(
        layer._dequantized_weight(torch.device("cpu"), torch.float32), expected.float(), atol=0.0, rtol=0.0
    )
    assert not any(p.is_meta for p in model.parameters())
    assert _room_was_made_before(run, "install_nvfp4_layers")


def test_a_file_without_the_keyframe_embedding_builds_the_model_without_it(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint(keyframe_embedding=False)
    run = prepare(_transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry)
    model = run.load(_checkpoint_config(tmp_path, sd))
    assert model.config.use_keyframes_abs_pos_embedding is False
    assert getattr(model, "keyframes_abs_pos_embedding", None) is None


def test_a_bare_key_file_loads_the_same_as_a_prefixed_one(monkeypatch, tmp_path) -> None:
    """Identification accepts both spellings, so the loader has to as well."""
    sd = tiny_transformer_checkpoint(prefix="")
    run = prepare(_transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry)
    model = run.load(_checkpoint_config(tmp_path, sd))
    assert not any(p.is_meta for p in model.parameters())


def test_bundled_component_tensors_are_ignored_not_refused(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint()
    sd["vae.encoder.conv_in.conv.weight"] = torch.zeros(4, 4)
    sd["model.diffusion_model.video_embeddings_connector.learnable_registers"] = torch.zeros(4, 4)
    run = prepare(_transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry)
    model = run.load(_checkpoint_config(tmp_path, sd))
    assert not any(p.is_meta for p in model.parameters())
    assert any("ignoring 2 tensor" in str(call) for call in run.loader._logger.info.call_args_list)


def test_a_header_declared_unsupported_format_is_refused_before_the_tensor_read(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint()
    header = {"_quantization_metadata": json.dumps({"layers": {"x": {"format": "fp8_scaled"}}})}
    config = _checkpoint_config(tmp_path, sd, metadata=header)
    run = prepare(_transformer_seam(), monkeypatch, state_dict=sd, metadata=header, geometry=_tiny_geometry)
    monkeypatch.setattr(safetensors.torch, "load_file", lambda _p: pytest.fail("the tensors were read"))
    with pytest.raises(ValueError, match="fp8_scaled"):
        run.load(config)
    assert run.reserved == []


def test_undeclared_float8_weights_are_refused_by_name(monkeypatch, tmp_path) -> None:
    sd = tiny_transformer_checkpoint()
    key = official_name("transformer_blocks.0.attn1.to_v.weight")
    sd[key] = sd[key].to(torch.float8_e4m3fn)
    run = prepare(_transformer_seam(), monkeypatch, state_dict=sd, metadata={}, geometry=_tiny_geometry)
    with pytest.raises(ValueError, match="float8"):
        run.load(_checkpoint_config(tmp_path, sd))
    assert run.reserved == []


# --- component folder ----------------------------------------------------------------------------


def _folder_loader(reserved: list[int] | None = None) -> LTX2FolderModel:
    loader = object.__new__(LTX2FolderModel)
    loader._ram_cache = SimpleNamespace(make_room=(reserved.append if reserved is not None else lambda _n: None))
    loader._logger = SimpleNamespace(info=lambda *_a, **_k: None)
    loader._torch_device = torch.device("cpu")
    return loader


def _cpu(monkeypatch) -> None:
    monkeypatch.setattr(ltx2.TorchDevice, "choose_torch_device", staticmethod(lambda: torch.device("cpu")))
    monkeypatch.setattr(ltx2.TorchDevice, "choose_bfloat16_safe_dtype", staticmethod(lambda _d: torch.float32))


def test_a_components_only_folder_refuses_the_transformer_with_the_next_step(tmp_path) -> None:
    config = Main_Diffusers_LTX2_Config.model_construct(
        path=str(tmp_path),
        components={"video_vae": "v.safetensors", "audio_vae": "a.safetensors"},
        components_only=True,
    )
    with pytest.raises(ValueError, match="components-only"):
        _folder_loader()._load_model(config, SubModelType.Transformer)


def test_a_missing_component_names_what_to_install(tmp_path) -> None:
    config = Main_Diffusers_LTX2_Config.model_construct(path=str(tmp_path), components={"video_vae": "v.safetensors"})
    with pytest.raises(ValueError, match="vocoder"):
        _folder_loader()._load_model(config, SubModelType.Vocoder)


def test_a_component_file_deleted_after_install_is_named_not_tracebacked(tmp_path) -> None:
    config = Main_Diffusers_LTX2_Config.model_construct(path=str(tmp_path), components={"vocoder": "gone.safetensors"})
    with pytest.raises(ValueError, match="gone.safetensors"):
        _folder_loader()._load_model(config, SubModelType.Vocoder)


def test_the_text_encoder_is_not_the_folder_s_to_serve(tmp_path) -> None:
    config = Main_Diffusers_LTX2_Config.model_construct(path=str(tmp_path), components={})
    with pytest.raises(ValueError, match="Gemma-4"):
        _folder_loader()._load_model(config, SubModelType.TextEncoder)


@pytest.mark.parametrize(
    ("tensors", "match"),
    [
        ({"encoder.conv_in.conv.weight": torch.zeros(2, 2, dtype=torch.float8_e4m3fn)}, "float8"),
        (
            {
                "encoder.conv_in.conv.weight": torch.zeros(2, 2, dtype=torch.int8),
                "encoder.conv_in.conv.weight_scale": torch.ones(2, 1),
            },
            "int8",
        ),
    ],
)
def test_a_quantized_component_file_is_refused_before_any_room_is_made(monkeypatch, tmp_path, tensors, match) -> None:
    safetensors.torch.save_file(tensors, tmp_path / "v.safetensors")
    config = Main_Diffusers_LTX2_Config.model_construct(path=str(tmp_path), components={"video_vae": "v.safetensors"})
    _cpu(monkeypatch)
    reserved: list[int] = []
    with pytest.raises(ValueError, match=match):
        _folder_loader(reserved)._load_model(config, SubModelType.VAE)
    assert reserved == []


def test_the_folder_sizes_the_files_a_submodel_is_read_from(tmp_path) -> None:
    """`_load_and_cache` reserves `get_size_fs` before the read; a folder has no `<submodel>/` dir
    to size, so the read would otherwise land on an unreserved cache."""
    for name, size in (
        ("p.safetensors", 100),
        ("vc.safetensors", 200),
        ("ac.safetensors", 300),
        ("v.safetensors", 1000),
    ):
        (tmp_path / name).write_bytes(b"\0" * size)
    config = Main_Diffusers_LTX2_Config.model_construct(
        path=str(tmp_path),
        components={
            "text_projection": "p.safetensors",
            "video_connector": "vc.safetensors",
            "audio_connector": "ac.safetensors",
            "video_vae": "v.safetensors",
        },
    )
    loader = _folder_loader()
    assert loader.get_size_fs(config, tmp_path, SubModelType.Connectors) == 600
    assert loader.get_size_fs(config, tmp_path, SubModelType.VAE) == 1000
    assert loader.get_size_fs(config, tmp_path, SubModelType.Vocoder) == 0


def test_the_connectors_are_read_out_of_a_bundled_transformer_file_by_key(monkeypatch, tmp_path) -> None:
    """The official transformer files carry the two connectors beside the transformer; only the
    connector tensors are read, and the strict connector load never sees the rest."""
    from diffusers.pipelines.ltx2.connectors import LTX2TextConnectors

    tiny = {
        **cc.LTX2_5_CONNECTORS_CONFIG,
        "caption_channels": 4,
        "text_proj_in_factor": 2,
        "video_connector_num_attention_heads": 2,
        "video_connector_attention_head_dim": 4,
        "video_connector_num_layers": 1,
        "video_connector_num_learnable_registers": 2,
        "audio_connector_num_attention_heads": 2,
        "audio_connector_attention_head_dim": 2,
        "audio_connector_num_layers": 1,
        "audio_connector_num_learnable_registers": 2,
        "video_hidden_dim": 8,
        "audio_hidden_dim": 4,
    }
    monkeypatch.setattr(cc, "LTX2_5_CONNECTORS_CONFIG", tiny)
    torch.manual_seed(0)
    reference = LTX2TextConnectors(**tiny)
    official: dict[str, torch.Tensor] = {}
    for key, value in reference.state_dict().items():
        if key.startswith("video_text_proj_in."):
            official["text_embedding_projection.video_aggregate_embed." + key[len("video_text_proj_in.") :]] = value
        elif key.startswith("audio_text_proj_in."):
            official["text_embedding_projection.audio_aggregate_embed." + key[len("audio_text_proj_in.") :]] = value
        else:
            branch, rest = key.split(".", 1)
            rest = (
                rest.replace("transformer_blocks.", "transformer_1d_blocks.")
                .replace("norm_q", "q_norm")
                .replace("norm_k", "k_norm")
            )
            official[f"model.diffusion_model.{branch.replace('_connector', '_embeddings_connector')}.{rest}"] = value
    bundle = {**official, "model.diffusion_model.transformer_blocks.0.attn1.to_q.weight": torch.zeros(4, 4)}
    safetensors.torch.save_file(bundle, tmp_path / "bundle.safetensors")
    config = Main_Diffusers_LTX2_Config.model_construct(
        path=str(tmp_path),
        components={
            "text_projection": "bundle.safetensors",
            "video_connector": "bundle.safetensors",
            "audio_connector": "bundle.safetensors",
        },
    )
    _cpu(monkeypatch)
    model = _folder_loader()._load_model(config, SubModelType.Connectors)
    torch.testing.assert_close(model.video_connector.learnable_registers, reference.video_connector.learnable_registers)


# --- Gemma-4 text tower --------------------------------------------------------------------------


def _tiny_gemma4_config():
    from transformers import Gemma4TextConfig

    return Gemma4TextConfig(
        hidden_size=32,
        num_hidden_layers=2,
        num_attention_heads=2,
        num_key_value_heads=1,
        head_dim=16,
        intermediate_size=64,
        vocab_size=128,
        layer_types=["sliding_attention", "full_attention"],
        sliding_window=8,
        hidden_size_per_layer_input=0,
        vocab_size_per_layer_input=128,
        tie_word_embeddings=True,
    )


@pytest.fixture(params=["model.", "model.language_model."])
def gemma_folder(request, tmp_path: Path) -> SimpleNamespace:
    """The tower under either of the two prefixes the releases use, beside the tensors that are not
    the tower's (a vision tower, the tied head, a with-proj projection) and one int8 layer."""
    from transformers.models.gemma4.modeling_gemma4 import Gemma4TextModel

    torch.manual_seed(0)
    config = _tiny_gemma4_config()
    model = Gemma4TextModel(config)
    config_dict = {
        **config.to_dict(),
        "architectures": ["Gemma4UnifiedForCausalLM"],
        "model_type": "gemma4_unified_text",
    }
    (tmp_path / "config.json").write_text(json.dumps(config_dict))
    prefix = request.param
    sd = {f"{prefix}{k}": v.detach().clone() for k, v in model.state_dict().items()}
    key = f"{prefix}layers.0.mlp.down_proj.weight"
    dense = sd[key]
    scale = dense.abs().amax(dim=1, keepdim=True) / 127.0
    sd[key] = torch.round(dense / scale).clamp(-127, 127).to(torch.int8)
    sd[f"{prefix}layers.0.mlp.down_proj.weight_scale"] = scale.float()
    sd[f"{prefix}layers.0.mlp.down_proj.comfy_quant"] = comfy_quant_marker(
        {"format": "int8_tensorwise", "convrot": False}
    )
    sd["hf_asset__video_aggregate_embed.weight"] = torch.zeros(4, 4)
    sd["model.vision_tower.patch_embedding.weight"] = torch.zeros(4, 4)
    sd["lm_head.weight"] = torch.zeros(128, 32)
    safetensors.torch.save_file(sd, tmp_path / "te.safetensors")
    return SimpleNamespace(root=tmp_path, reference=model)


def test_the_text_tower_loads_from_the_file_with_its_int8_layer_packed(monkeypatch, gemma_folder) -> None:
    _cpu(monkeypatch)
    loader = object.__new__(LTX2Gemma4EncoderModel)
    reserved: list[int] = []
    loader._ram_cache = SimpleNamespace(make_room=reserved.append)
    loader._logger = SimpleNamespace(info=lambda *_a, **_k: None)
    config = Gemma4Encoder_Gemma4Encoder_LTX2_Config.model_construct(
        path=str(gemma_folder.root), subfolder="", weight_file="te.safetensors"
    )

    model = loader._load_model(config, SubModelType.TextEncoder)

    assert isinstance(model.layers[0].mlp.down_proj, Int8ConvrotLinear)
    assert not any(p.is_meta for p in model.parameters())
    assert reserved, "cache room is reserved for the tower"
    ids = torch.tensor([[2, 5, 9, 1]])
    with torch.no_grad():
        out = model(input_ids=ids, output_hidden_states=True)
        ref = gemma_folder.reference(input_ids=ids, output_hidden_states=True)
    assert len(out.hidden_states) == 3, "every layer's hidden state, plus the embedding output"
    torch.testing.assert_close(out.hidden_states[-1], ref.hidden_states[-1], atol=0.2, rtol=0.05)


def test_the_tokenizer_comes_from_the_same_folder(monkeypatch, tmp_path) -> None:
    calls: list[Path] = []
    import transformers

    monkeypatch.setattr(
        transformers, "AutoTokenizer", SimpleNamespace(from_pretrained=lambda p, **_k: calls.append(p) or "tok")
    )
    loader = object.__new__(LTX2Gemma4EncoderModel)
    config = Gemma4Encoder_Gemma4Encoder_LTX2_Config.model_construct(
        path=str(tmp_path), subfolder="gemma4-12b-ltx-v1", weight_file="te.safetensors"
    )
    assert loader._load_model(config, SubModelType.Tokenizer) == "tok"
    assert calls == [tmp_path / "gemma4-12b-ltx-v1"]


@pytest.fixture
def gemma4_tokenizer_folder(tmp_path: Path) -> Path:
    """A folder that trips the Mistral-regex heuristic the released Gemma-4 encoder trips.

    Real transformers, no weights: the release's ``model_type`` and no ``transformers_version`` so
    the heuristic cannot short-circuit, and a vocabulary over 100k with a
    pre-tokenizer, which is what gates that heuristic at all. Written here rather than mocked so the
    warnings are the ones transformers actually emits: a reworded message or a renamed kwarg has to
    fail a test instead of silently returning the advice.
    """
    from tokenizers import Tokenizer, models, pre_tokenizers

    root = tmp_path / "gemma4-12b-ltx-v1"
    root.mkdir()
    tokenizer = Tokenizer(models.WordLevel(vocab={f"t{i}": i for i in range(100_002)}, unk_token="t0"))
    tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()
    tokenizer.save(str(root / "tokenizer.json"))
    (root / "tokenizer_config.json").write_text(
        json.dumps({"tokenizer_class": "PreTrainedTokenizerFast", "unk_token": "t0"})
    )
    (root / "config.json").write_text(
        json.dumps({"model_type": "gemma4_unified_text", "architectures": ["Gemma4UnifiedForCausalLM"]})
    )
    return root


@pytest.fixture
def transformers_warnings():
    """Every warning transformers emits, with its verbosity pinned so the capture cannot go empty."""
    import transformers

    logger = logging.getLogger("transformers")
    captured: list[str] = []

    class Capture(logging.Handler):
        def emit(self, record: logging.LogRecord) -> None:
            if record.levelno >= logging.WARNING:
                captured.append(record.getMessage())

    handler = Capture()
    previous = transformers.utils.logging.get_verbosity()
    transformers.utils.logging.set_verbosity_warning()
    logger.addHandler(handler)
    try:
        yield captured
    finally:
        logger.removeHandler(handler)
        transformers.utils.logging.set_verbosity(previous)


def test_the_release_s_mistral_regex_advice_is_declined(gemma4_tokenizer_folder, transformers_warnings):
    """The advice would mis-tokenize every prompt if taken. Loading the same folder directly is the
    baseline: it fires there, so an empty capture after the loader means it was answered, not that it
    never happens."""
    from transformers import AutoTokenizer

    direct = AutoTokenizer.from_pretrained(gemma4_tokenizer_folder, local_files_only=True)
    baseline = list(transformers_warnings)
    assert sum("fix_mistral_regex=True" in message for message in baseline) == 1

    transformers_warnings.clear()
    loader = object.__new__(LTX2Gemma4EncoderModel)
    config = Gemma4Encoder_Gemma4Encoder_LTX2_Config.model_construct(
        path=str(gemma4_tokenizer_folder.parent), subfolder="gemma4-12b-ltx-v1", weight_file="te.safetensors"
    )
    tokenizer = loader._load_model(config, SubModelType.Tokenizer)

    assert transformers_warnings == []
    # Declining the advice is not the same as taking it: the pre-tokenizer is the released one.
    assert str(tokenizer._tokenizer.pre_tokenizer) == str(direct._tokenizer.pre_tokenizer)


def test_the_encoder_sizes_its_weight_file_one_directory_down(tmp_path) -> None:
    nested = tmp_path / "gemma4-12b-ltx-v1"
    nested.mkdir()
    (nested / "te.safetensors").write_bytes(b"\0" * 5000)
    (nested / "tokenizer.json").write_bytes(b"\0" * 40)
    config = Gemma4Encoder_Gemma4Encoder_LTX2_Config.model_construct(
        path=str(tmp_path), subfolder="gemma4-12b-ltx-v1", weight_file="te.safetensors"
    )
    loader = object.__new__(LTX2Gemma4EncoderModel)
    assert loader.get_size_fs(config, tmp_path, SubModelType.TextEncoder) == 5000
    assert loader.get_size_fs(config, tmp_path, SubModelType.Tokenizer) == 40


def test_an_nvfp4_layer_missing_its_global_scale_is_refused_before_the_cache_is_evicted(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    """The degraded half-state, at this seam rather than at the detector.

    A packed uint8 weight with a block-scale grid and no `weight_scale_2` is what a guard keyed on
    `weight_scale_2` -- the key the decode keys on -- lets straight through. `find_nvfp4_layers`
    refuses it and `test_nvfp4.py` pins that; what only a seam can answer is whether this loader
    reaches the detector before it asks the cache for room. Here a key conversion stands between the
    pop and the reservation, and nothing but their order keeps them that way round.
    """
    sd = tiny_transformer_checkpoint()
    target = official_name(f"{NVFP4_LAYER}.weight")[: -len(".weight")]
    rows, packed = sd[f"{target}.weight"].shape[0], sd[f"{target}.weight"].shape[1] // 2
    sd[f"{target}.weight"] = torch.zeros(rows, packed, dtype=torch.uint8)
    sd[f"{target}.weight_scale"] = torch.zeros(rows, packed // 8).to(torch.float8_e4m3fn)
    _tiny_geometry(monkeypatch)
    run = prepare(_transformer_seam(), monkeypatch)

    with pytest.raises(ValueError, match="with a weight_scale but no weight_scale_2"):
        run.load(_checkpoint_config(tmp_path, sd))

    assert run.reserved == []


def _duration_head_loader(reserved: list[int]) -> LTX2DurationHeadLoader:
    loader = object.__new__(LTX2DurationHeadLoader)
    loader._ram_cache = SimpleNamespace(make_room=reserved.append)
    loader._logger = SimpleNamespace(info=lambda *_a, **_k: None, warning=lambda *_a, **_k: None)
    return loader


def test_the_duration_head_loads_widened_and_predicts_on_the_grid(monkeypatch, tmp_path) -> None:
    """A published (bf16) head loads in fp32 and answers through the exact calls the duration node makes.

    The node calls `forward` and `predict_num_frames` by keyword; driving an unstubbed head here is
    what catches diffusers renaming either, which the node's own tests (with a pinned regression)
    cannot.
    """
    from diffusers.pipelines.ltx2.duration_head import LTX2DurationHead

    torch.manual_seed(0)
    reference = LTX2DurationHead().eval()
    published = {key: value.to(torch.bfloat16).contiguous() for key, value in reference.state_dict().items()}
    safetensors.torch.save_file(published, tmp_path / "head.safetensors")
    _cpu(monkeypatch)
    reserved: list[int] = []
    config = LTX2DurationHead_Checkpoint_Config.model_construct(path=str(tmp_path / "head.safetensors"))

    head = _duration_head_loader(reserved)._load_model(config)

    assert all(p.dtype == torch.float32 and not p.is_meta for p in head.parameters())
    assert reserved, "cache room is reserved before the widening cast"
    video, audio = torch.randn(1, 16, 4096), torch.randn(1, 16, 2048, dtype=torch.bfloat16)
    reference.load_state_dict({key: value.float() for key, value in published.items()})
    with torch.no_grad():
        seconds = head(video_tokens=video, audio_tokens=audio)
        torch.testing.assert_close(seconds, reference(video_tokens=video, audio_tokens=audio))
        frames = head.predict_num_frames(
            video_tokens=video,
            audio_tokens=audio,
            frame_rate=24.0,
            temporal_compression_ratio=8,
            min_seconds=1.0,
            max_seconds=20.0,
        )
    assert (frames - 1) % 8 == 0 and 24 <= frames <= 481


def test_a_quantized_duration_head_is_refused_by_name(monkeypatch, tmp_path) -> None:
    """Same keys, float8 weights: upcast without their scales it would load and predict nonsense."""
    from diffusers.pipelines.ltx2.duration_head import LTX2DurationHead

    sd = {key: value.contiguous() for key, value in LTX2DurationHead().state_dict().items()}
    sd["video_input_proj.weight"] = sd["video_input_proj.weight"].to(torch.float8_e4m3fn)
    safetensors.torch.save_file(sd, tmp_path / "head.safetensors")
    _cpu(monkeypatch)
    reserved: list[int] = []
    config = LTX2DurationHead_Checkpoint_Config.model_construct(path=str(tmp_path / "head.safetensors"))

    with pytest.raises(ValueError, match="float8"):
        _duration_head_loader(reserved)._load_model(config)
    assert reserved == []