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.

21.9 KB
Raw Blame
"""Dropping the Qwen3-VL visual tower on the safetensors load paths.

Krea-2 and Ideogram 4 condition on the language tower's hidden states; `Qwen3VLModel.forward`
reaches `self.visual` only for `pixel_values`, which text-only encoding never passes, and Ideogram 4
does not go through `forward` at all. Krea-2 and Qwen-Image-2.1 reference images do run the tower,
but a separately loaded one (the `VisionEncoder` submodel) attached to the encoder for that call. The
encoder's own tower is therefore never executed, and dropping it takes 0.39-0.77 GiB off the 4B
encoder depending on the container.

These tests use a real (tiny) `Qwen3VLModel`, so what disappears is the architecture's actual vision
tower rather than a stand-in. MiniMax H3 does run this tower and loads through
`model_loaders/minimax_h3.py`; nothing here is reachable from it.
"""

import json
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock

import pytest
import torch

from invokeai.backend.model_manager.configs.default_settings import MainModelDefaultSettings
from invokeai.backend.model_manager.configs.qwen3_vl_encoder import (
    Qwen3VLEncoder_Checkpoint_Config,
    Qwen3VLEncoder_Qwen3VLEncoder_Config,
)
from invokeai.backend.model_manager.load.model_loaders.krea2 import (
    Qwen3VLEncoderCheckpointLoader,
    Qwen3VLEncoderLoader,
)
from invokeai.backend.model_manager.taxonomy import Qwen3VLVariantType, SubModelType
from invokeai.backend.model_manager.util.qwen3_vl import (
    drop_qwen3vl_unloaded_keys,
    drop_qwen3vl_visual_tower,
    drop_qwen3vl_visual_tower_keys,
)
from invokeai.backend.quantization.int8_convrot import CONVROT_GROUP_SIZE, build_regular_hadamard
from tests.backend.model_manager.load.qwen3vl_gguf_fixture import tiny_qwen3vl_config
from tests.fixtures.safetensors_files import needs_mapping_that_shows_file_writes, tensors_reading_the_file

# --- the key filter -------------------------------------------------------------------------------


@pytest.mark.parametrize(
    "key",
    [
        "visual.blocks.0.attn.qkv.weight",
        "model.visual.blocks.0.attn.qkv.weight",
        "model.visual.pos_embed.weight",
        # A sidecar has to leave with its weight. The sharp case is the int8 branch, which is selected
        # purely by surviving `.comfy_quant` markers: an orphaned visual marker would route the whole
        # checkpoint into int8 handling for a module path the model no longer has.
        "model.visual.blocks.0.attn.qkv.comfy_quant",
        "model.visual.blocks.0.attn.qkv.weight_scale",
        # ComfyUI writes the reversed spelling too, which is why the filter matches on the module
        # path prefix rather than on a list of known suffixes.
        "model.visual.blocks.0.attn.qkv.scale_weight",
    ],
)
def test_visual_tower_keys_are_dropped(key: str) -> None:
    assert drop_qwen3vl_visual_tower_keys({key: "t"}) == {}


@pytest.mark.parametrize(
    "key",
    [
        "model.layers.0.self_attn.q_proj.weight",
        "language_model.layers.0.self_attn.q_proj.weight",
        "model.embed_tokens.weight",
        "model.norm.weight",
        # Not the vision tower: a language-model module that merely contains the substring.
        "model.layers.0.visualization.weight",
    ],
)
def test_language_tower_keys_survive(key: str) -> None:
    assert drop_qwen3vl_visual_tower_keys({key: "t"}) == {key: "t"}


def test_non_string_keys_survive() -> None:
    assert drop_qwen3vl_visual_tower_keys({0: "t"}) == {0: "t"}


@pytest.mark.parametrize(
    "key",
    [
        "model.visual.blocks.0.attn.qkv.weight",
        "lm_head.weight",
        "lm_head.weight_scale",
        # The remap routes these to `language_model.lm_head`, as it does a bare `lm_head`.
        "model.lm_head.weight",
        "model.lm_head.comfy_quant",
        "model.language_model.lm_head.weight",
    ],
)
def test_the_tower_and_every_lm_head_spelling_are_dropped(key: str) -> None:
    assert drop_qwen3vl_unloaded_keys({key: "t", "model.embed_tokens.weight": "e"}) == {
        "model.embed_tokens.weight": "e"
    }


@pytest.mark.parametrize("key", ["model.layers.0.self_attn.q_proj.weight", "model.norm.weight", "lm_head_bias", 0])
def test_what_the_single_file_loader_reads_survives(key) -> None:
    assert drop_qwen3vl_unloaded_keys({key: "t"}) == {key: "t"}


def test_a_model_without_a_tower_is_refused_rather_than_given_a_dead_attribute(tmp_path: Path) -> None:
    """`setattr` would happily invent `visual` on anything. On the `from_pretrained` paths nothing
    else would notice a renamed tower: it would load, a dead stub would sit beside it, and the
    encoder would keep every byte this is meant to save."""
    with pytest.raises(RuntimeError, match="no Qwen-VL visual tower"):
        drop_qwen3vl_visual_tower(torch.nn.Module())


def test_a_model_without_a_tower_is_tolerated_when_the_caller_says_so() -> None:
    """Ideogram 4 builds whatever its folder's config declares, so a tower-less encoder is a shape it
    may legitimately meet rather than a sign the architecture moved."""
    assert drop_qwen3vl_visual_tower(torch.nn.Module(), required=False) is False


# --- the claim the whole change rests on ----------------------------------------------------------


def test_the_conditioning_is_unchanged_without_the_tower() -> None:
    """The justification for dropping the tower is a property of `Qwen3VLModel.forward` that a
    transformers upgrade can quietly take away. This is the only path that can hold both towers, so
    it is the only place the before/after comparison can be made at all.

    Run through the exact call the Krea-2 invocation makes, including the 3-way expanded mRoPE
    position ids.
    """
    model = _tiny_encoder().eval()
    input_ids = torch.tensor([[1, 2, 3, 4]])
    attention_mask = torch.ones_like(input_ids, dtype=torch.bool)
    position_ids = (attention_mask.long().cumsum(dim=-1) - 1).clamp(min=0).unsqueeze(0).expand(3, -1, -1)

    def encode():
        with torch.no_grad():
            return model(
                input_ids=input_ids,
                attention_mask=attention_mask,
                position_ids=position_ids,
                output_hidden_states=True,
                use_cache=False,
                return_dict=True,
            ).hidden_states

    with_tower = encode()
    drop_qwen3vl_visual_tower(model)
    without_tower = encode()

    assert len(with_tower) == len(without_tower)
    for index, (before, after) in enumerate(zip(with_tower, without_tower, strict=True)):
        assert torch.equal(before, after), f"hidden state {index} changed when the tower was dropped"


# --- the load paths -------------------------------------------------------------------------------


def _tiny_encoder(**config_kwargs):
    """A materialized tiny Qwen3-VL, small enough to save and reload in a test."""
    from transformers import Qwen3VLModel

    torch.manual_seed(0)
    return Qwen3VLModel(tiny_qwen3vl_config(**config_kwargs))


def _assert_tower_gone(model) -> None:
    assert isinstance(model.visual, torch.nn.Identity)
    assert list(model.visual.parameters()) == []
    # The language tower is untouched and complete -- the point is to drop the tower, not to lose
    # weights with it.
    assert [name for name, p in model.named_parameters() if p.is_meta] == []
    assert any(name.startswith("language_model.layers.") for name, _ in model.named_parameters())


def _checkpoint_loader(monkeypatch, te_config) -> Qwen3VLEncoderCheckpointLoader:
    loader = object.__new__(Qwen3VLEncoderCheckpointLoader)
    loader._ram_cache = SimpleNamespace(make_room=MagicMock())
    loader._torch_device = torch.device("cpu")
    loader._logger = MagicMock()
    loader._load_te_config = lambda _config: te_config
    loader._apply_fp8_to_nn_module = lambda *a, **k: None
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.krea2.TorchDevice.choose_torch_device",
        lambda: torch.device("cpu"),
    )
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.krea2.TorchDevice.choose_bfloat16_safe_dtype",
        lambda _device: torch.float32,
    )
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.load_default._device_supports_fp8_storage",
        lambda _device, _logger: False,
    )
    return loader


def _write_checkpoint(path: Path, state_dict: dict[str, torch.Tensor]) -> Qwen3VLEncoder_Checkpoint_Config:
    from safetensors.torch import save_file

    save_file({k: v.contiguous() for k, v in state_dict.items()}, str(path))
    return Qwen3VLEncoder_Checkpoint_Config.model_construct(
        path=str(path), variant=Qwen3VLVariantType.Qwen3VL_4B, name="tiny"
    )


@pytest.mark.parametrize("prefixed", [False, True], ids=["transformers_layout", "comfyui_layout"])
def test_the_single_file_loader_drops_the_tower(monkeypatch, tmp_path: Path, prefixed: bool) -> None:
    """ComfyUI single-file checkpoints bundle both towers under one ``model.`` prefix; the
    transformers spelling keeps them apart. Both spellings must lose the tower."""
    reference = _tiny_encoder()
    state_dict = dict(reference.state_dict())
    if prefixed:
        state_dict = {f"model.{k}": v for k, v in state_dict.items()}
    config = _write_checkpoint(tmp_path / "qwen3vl_tiny.safetensors", state_dict)

    model = _checkpoint_loader(monkeypatch, tiny_qwen3vl_config())._load_text_encoder(config)

    _assert_tower_gone(model)
    # The language weights are the ones the file carried, not re-initialised ones.
    torch.testing.assert_close(model.language_model.embed_tokens.weight, reference.language_model.embed_tokens.weight)


def test_the_single_file_loader_reserves_only_for_what_it_keeps(monkeypatch, tmp_path: Path) -> None:
    """Asserted on the bytes the loader asks the cache to free, which is the observable contract --
    the cache evicts other resident models to honour it.

    Note this is the loader's own reservation. `ModelLoader._load_and_cache` separately reserves the
    file's on-disk size before `_load_model` runs, and that one still counts the tower; over-
    reserving is safe and pre-existing, but it means this saving is not the whole story.
    """
    reference = _tiny_encoder()
    state_dict = dict(reference.state_dict())
    config = _write_checkpoint(tmp_path / "qwen3vl_tiny.safetensors", state_dict)
    language_bytes = sum(v.nelement() * v.element_size() for k, v in state_dict.items() if not k.startswith("visual."))
    total_bytes = sum(v.nelement() * v.element_size() for v in state_dict.values())

    loader = _checkpoint_loader(monkeypatch, tiny_qwen3vl_config())
    loader._load_text_encoder(config)

    reserved = [call.args[0] for call in loader._ram_cache.make_room.call_args_list]
    assert reserved, "the loader no longer reserves; this test no longer covers what it claims"
    assert max(reserved) <= language_bytes
    # The fixture has to make the difference visible, or the assertion above proves nothing.
    assert language_bytes < total_bytes


def test_the_int8_branch_drops_the_tower_too(monkeypatch, tmp_path: Path) -> None:
    """The drop sits before the int8/dense split, and it has to stay there. Moved into the dense
    branch, an int8-convrot encoder would refuse to load at all: its visual tensors are gone from the
    state dict, so the tower would stay on the meta device and the completeness sweep would reject
    it -- a hard load failure on a supported container, with the dense tests still green.
    """
    from transformers import Qwen3VLModel

    te_config = tiny_qwen3vl_config()
    # `install_int8_convrot_layers` rotates in groups, so the quantized Linear's input width has to be
    # the group size. Everything else stays at the fixture's dimensions.
    te_config.text_config.hidden_size = CONVROT_GROUP_SIZE
    te_config.vision_config.hidden_size = CONVROT_GROUP_SIZE
    te_config.vision_config.out_hidden_size = CONVROT_GROUP_SIZE

    torch.manual_seed(0)
    state_dict = dict(Qwen3VLModel(te_config).state_dict())

    # One language Linear carries the int8-convrot payload; that is all it takes to select the branch.
    layer = "language_model.layers.0.self_attn.q_proj"
    original = state_dict[f"{layer}.weight"].float()
    hadamard = build_regular_hadamard(CONVROT_GROUP_SIZE)
    rows = original.shape[0]
    rotated = (original.view(rows, -1, CONVROT_GROUP_SIZE) @ hadamard.T).view(rows, -1)
    scale = rotated.abs().amax(dim=1, keepdim=True) / 127.0
    marker = json.dumps({"format": "int8_tensorwise", "convrot": True, "convrot_groupsize": CONVROT_GROUP_SIZE})
    state_dict[f"{layer}.weight"] = torch.clamp(torch.round(rotated / scale), -128, 127).to(torch.int8)
    state_dict[f"{layer}.weight_scale"] = scale.to(torch.float32)
    state_dict[f"{layer}.comfy_quant"] = torch.frombuffer(bytearray(marker.encode("utf-8")), dtype=torch.uint8)

    config = _write_checkpoint(tmp_path / "qwen3vl_int8.safetensors", state_dict)

    model = _checkpoint_loader(monkeypatch, te_config)._load_text_encoder(config)

    _assert_tower_gone(model)


def test_the_bundled_pipeline_encoder_drops_the_tower(monkeypatch, tmp_path: Path) -> None:
    """The path a starter model actually takes. Both loader nodes fall back to the pipeline's own
    encoder when no standalone one is wired up, so without this the headline saving never reaches
    the users who install "Krea-2 Turbo" and press Invoke.
    """
    from invokeai.backend.model_manager.configs.main import Main_Diffusers_Krea2_Config
    from invokeai.backend.model_manager.load.model_loaders.krea2 import Krea2DiffusersModel

    encoder = _tiny_encoder()
    config = Main_Diffusers_Krea2_Config.model_construct(path=str(tmp_path), repo_variant=None)
    loader = object.__new__(Krea2DiffusersModel)
    loader._torch_device = torch.device("cpu")
    loader.get_hf_load_class = lambda _path, _submodel: SimpleNamespace(from_pretrained=MagicMock(return_value=encoder))
    loader._apply_fp8_layerwise_casting = lambda model, _config, _submodel: model
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.krea2.AutoConfig.from_pretrained",
        lambda *_a, **_k: encoder.config,
    )
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.krea2.TorchDevice.choose_torch_device",
        lambda: torch.device("cpu"),
    )
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.krea2.TorchDevice.choose_bfloat16_safe_dtype",
        lambda _device: torch.float32,
    )

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

    _assert_tower_gone(model)


def test_the_folder_loader_drops_the_tower(tmp_path: Path) -> None:
    """The diffusers-directory path loads through `from_pretrained`, which builds the module tree
    itself, so the tower is read and then freed rather than skipped."""
    _tiny_encoder().save_pretrained(tmp_path)
    config = Qwen3VLEncoder_Qwen3VLEncoder_Config.model_construct(
        path=str(tmp_path), variant=Qwen3VLVariantType.Qwen3VL_4B, name="tiny"
    )

    loader = object.__new__(Qwen3VLEncoderLoader)
    loader._logger = MagicMock()

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

    _assert_tower_gone(model)


def _qwen_image_2_1_pipeline(
    monkeypatch,
    tmp_path: Path,
    *,
    conditional_generation: bool,
    saved_dtype: torch.dtype = torch.float32,
    max_shard_size: str | None = None,
):
    """A Qwen-Image-2.1 pipeline folder holding a tiny encoder, saved as `Qwen3VLForConditionalGeneration` (the
    released layout, `model.visual.*`) or as `Qwen3VLModel` (`visual.*`); the loader over it, which loads in
    float32; the saved encoder."""
    from transformers import Qwen3VLForConditionalGeneration

    from invokeai.backend.model_manager.configs.main import Main_Diffusers_QwenImage21_Config
    from invokeai.backend.model_manager.load.model_loaders.qwen_image_2_1 import QwenImage21DiffusersModel

    torch.manual_seed(0)
    saved = Qwen3VLForConditionalGeneration(tiny_qwen3vl_config()) if conditional_generation else _tiny_encoder()
    saved = saved.to(saved_dtype)
    shard_kwargs = {"max_shard_size": max_shard_size} if max_shard_size else {}
    saved.save_pretrained(tmp_path / "text_encoder", **shard_kwargs)
    monkeypatch.setattr(
        "invokeai.backend.model_manager.load.model_loaders.qwen_image_2_1.TorchDevice.choose_bfloat16_safe_dtype",
        lambda _device: torch.float32,
    )
    config = Main_Diffusers_QwenImage21_Config.model_construct(path=str(tmp_path), repo_variant=None)
    loader = object.__new__(QwenImage21DiffusersModel)
    loader._torch_device = torch.device("cpu")
    loader._apply_fp8_layerwise_casting = lambda model, _config, _submodel: model
    encoder = saved.model if conditional_generation else saved
    return loader, config, encoder


def _assert_tower_loaded(tower, saved) -> None:
    from transformers.models.qwen3_vl.modeling_qwen3_vl import Qwen3VLVisionModel

    assert isinstance(tower, Qwen3VLVisionModel)
    expected = saved.visual.state_dict()
    loaded = tower.state_dict()
    assert loaded.keys() == expected.keys()
    for key, value in expected.items():
        assert torch.equal(loaded[key], value), key


@pytest.mark.parametrize("conditional_generation", [True, False], ids=["model.visual", "visual"])
def test_the_qwen_image_2_1_pipeline_loads_the_tower_on_its_own(
    monkeypatch, tmp_path: Path, conditional_generation: bool
) -> None:
    """Text prompts load the encoder without the tower, as every other loader does. Edits load the tower alone,
    from the same folder and with the weights it was saved with, and attach it while they encode."""
    loader, config, saved = _qwen_image_2_1_pipeline(
        monkeypatch, tmp_path, conditional_generation=conditional_generation
    )

    _assert_tower_gone(loader._load_model(config, SubModelType.TextEncoder))
    _assert_tower_loaded(loader._load_model(config, SubModelType.VisionEncoder), saved)


def _shard_the_tower_and_leave_a_stray_file(tmp_path: Path) -> None:
    """The released encoder is sharded; check the tower spans several shards, and leave a stray safetensors file
    beside them (a leftover variant) that the index does not name, holding zeros under the tower's keys."""
    from safetensors import safe_open
    from safetensors.torch import save_file

    folder = tmp_path / "text_encoder"
    weight_map = json.loads((folder / "model.safetensors.index.json").read_text())["weight_map"]
    tower_keys = [key for key in weight_map if key.startswith("model.visual.")]
    assert len({weight_map[key] for key in tower_keys}) > 1
    first_shard = folder / weight_map[tower_keys[0]]
    with safe_open(first_shard, framework="pt") as f:
        stray = {key: torch.zeros_like(f.get_tensor(key)) for key in f.keys() if key.startswith("model.visual.")}
    save_file(stray, str(folder / "model.fp16.safetensors"))


def test_a_sharded_tower_loads_from_the_shards_its_index_names(monkeypatch, tmp_path: Path) -> None:
    loader, config, saved = _qwen_image_2_1_pipeline(
        monkeypatch, tmp_path, conditional_generation=True, max_shard_size="40KB"
    )
    _shard_the_tower_and_leave_a_stray_file(tmp_path)

    _assert_tower_loaded(loader._load_model(config, SubModelType.VisionEncoder), saved)


def test_the_tower_reserves_its_own_size_at_the_dtype_it_loads_in(monkeypatch, tmp_path: Path) -> None:
    """The cache makes room for this size before the load: the encoder folder's would evict ~17 GB for ~1 GB.
    Saved in bf16, sharded, and loaded in float32: four bytes per element, counted once, from the index's shards."""
    loader, config, saved = _qwen_image_2_1_pipeline(
        monkeypatch, tmp_path, conditional_generation=True, saved_dtype=torch.bfloat16, max_shard_size="40KB"
    )
    _shard_the_tower_and_leave_a_stray_file(tmp_path)

    elements = sum(v.nelement() for v in saved.visual.state_dict().values())
    assert loader.get_size_fs(config, tmp_path, SubModelType.VisionEncoder) == 4 * elements


@pytest.mark.parametrize("cpu_dtype", [torch.float32, torch.bfloat16])
def test_cpu_only_tower_reservation_matches_frozen_load_dtype(
    monkeypatch, tmp_path: Path, cpu_dtype: torch.dtype
) -> None:
    loader, config, saved = _qwen_image_2_1_pipeline(monkeypatch, tmp_path, conditional_generation=True)
    config.default_settings = MainModelDefaultSettings(cpu_only=True)
    loader._torch_device = torch.device("cuda:0")
    other_dtype = torch.bfloat16 if cpu_dtype == torch.float32 else torch.float32
    monkeypatch.setattr("invokeai.backend.util.devices.TorchDevice.choose_torch_device", lambda: torch.device("cuda:0"))
    monkeypatch.setattr(
        "invokeai.backend.util.devices.TorchDevice.choose_bfloat16_safe_dtype",
        lambda device: cpu_dtype if device.type == "cpu" else other_dtype,
    )

    # Cache-key lookup freezes the decision before the reservation and construction.
    loader._decision(config, SubModelType.VisionEncoder)
    reserved = loader.get_size_fs(config, tmp_path, SubModelType.VisionEncoder)
    monkeypatch.setattr("invokeai.backend.util.devices.TorchDevice.choose_bfloat16_safe_dtype", lambda _d: other_dtype)
    tower = loader._load_model(config, SubModelType.VisionEncoder)

    assert all(t.dtype == cpu_dtype for t in tower.state_dict().values())
    assert reserved == sum(t.numel() * t.element_size() for t in tower.state_dict().values())
    assert reserved == sum(t.numel() for t in saved.visual.state_dict().values()) * cpu_dtype.itemsize


@needs_mapping_that_shows_file_writes
def test_the_tower_does_not_keep_the_text_encoders_shards_mapped(monkeypatch, tmp_path: Path) -> None:
    """Saved in the dtype it loads in, the tower's cast is a no-op, so each tensor read from a shard would stay a view
    of it and keep the encoder's whole shard mapped for as long as the tower is cached."""
    loader, config, _ = _qwen_image_2_1_pipeline(
        monkeypatch, tmp_path, conditional_generation=True, max_shard_size="40KB"
    )
    tower = loader._load_model(config, SubModelType.VisionEncoder)

    shards = sorted((tmp_path / "text_encoder").glob("*.safetensors"))

    assert len(shards) > 1
    assert [name for shard in shards for name in tensors_reading_the_file(tower, shard)] == []