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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(lora): identify LTX-2 LoRAs saved with a bare base_model.model. prefix (#10341)
Alexander Eichhorn4b2aefe6c733
diff --git a/invokeai/backend/patches/lora_conversions/ltx2_lora_constants.py b/invokeai/backend/patches/lora_conversions/ltx2_lora_constants.pyindex a437e5d5b7..856331b686 100644--- a/invokeai/backend/patches/lora_conversions/ltx2_lora_constants.py+++ b/invokeai/backend/patches/lora_conversions/ltx2_lora_constants.py@@ -29,7 +29,7 @@ LTX2_LORA_TRANSFORMER_PREFIX = "lora_transformer-"# would also match nested prefixes like ``diffusion_model.transformer.blocks...``, which the# probe would then admit but the converter would map onto nonexistent module paths (the LoRA# would silently apply zero layers).-_PEFT_PREFIX_RE = r"^(?:(?:model\.diffusion_model|diffusion_model|transformer|base_model\.model\.transformer)\.)?"+_PEFT_PREFIX_RE = r"^(?:(?:model\.diffusion_model|diffusion_model|transformer|base_model\.model(?:\.transformer)?)\.)?"# Submodules unique to LTX-2 among the supported architectures, in the checkpoint's native# naming. All of them exist only because LTX-2 is a *dual-stream* video+audio transformer:@@ -41,6 +41,11 @@ _PEFT_PREFIX_RE = r"^(?:(?:model\.diffusion_model|diffusion_model|transformer|ba# - ``patchify_proj`` alone — earlier LTX-Video releases use it too, so it identifies the# family but not this architecture.# Requiring an audio-side or cross-modal marker is what makes this exclusive.+#+# A LoRA without such a marker (video stream only) is deliberately not identified as LTX-2, even though the+# video stream is 4096 wide: LTX-Video 13B (``LTXVideoTransformer3DModel``: 48 blocks, 32 heads of 128, 4096-wide+# text context) names and shapes ``transformer_blocks.N.attn1/attn2.to_q/to_k/to_v/to_out.0`` identically, as do+# Cosmos-Predict1 7B LoRAs, so no shape separates them and a wrong claim would apply silently to the wrong model._LTX2_BLOCK_RE = re.compile(_PEFT_PREFIX_RE + r"transformer_blocks\.\d+\.(?:audio_attn[12]|audio_to_video_attn|video_to_audio_attn|audio_ff)\.")diff --git a/invokeai/backend/patches/lora_conversions/ltx2_lora_conversion_utils.py b/invokeai/backend/patches/lora_conversions/ltx2_lora_conversion_utils.pyindex 53f59305ed..f81b24c97e 100644--- a/invokeai/backend/patches/lora_conversions/ltx2_lora_conversion_utils.py+++ b/invokeai/backend/patches/lora_conversions/ltx2_lora_conversion_utils.py@@ -41,6 +41,7 @@ from invokeai.backend.patches.model_patch_raw import ModelPatchRaw# official paths. Ordered longest-first so the nested PEFT prefix is not left with a tail._PEFT_PREFIXES_TO_STRIP = ("base_model.model.transformer.",+ "base_model.model.",# Comfy packages some releases under this; diffusers' own rename table strips it too, so the# probe admits it and this keeps the two in step."model.diffusion_model.",diff --git a/tests/backend/model_manager/configs/test_anima_diffusers_lora_config.py b/tests/backend/model_manager/configs/test_anima_diffusers_lora_config.pyindex 12643e34c9..84ec4d48c1 100644--- a/tests/backend/model_manager/configs/test_anima_diffusers_lora_config.py+++ b/tests/backend/model_manager/configs/test_anima_diffusers_lora_config.py@@ -74,7 +74,8 @@ FOREIGN_TRANSFORMERS = {"pixart-sigma": (1152, 1152, 1152),"sana-600m": (1152, 1152, 1152),"sana-1600m": (2240, 2240, 2240),- # Self-attention is as wide as Anima's; only the text context tells them apart.+ # LTX-Video 2B: self-attention is as wide as Anima's; only the text context tells them apart. (The 13B is 4096+ # wide, like LTX-2's video stream; see test_ltx2_lora_probe_independence.py.)"ltx-video": (2048, 2048, 2048),}diff --git a/tests/backend/model_manager/configs/test_ltx2_lora_probe_independence.py b/tests/backend/model_manager/configs/test_ltx2_lora_probe_independence.pyindex b47f90cdad..5e78830043 100644--- a/tests/backend/model_manager/configs/test_ltx2_lora_probe_independence.py+++ b/tests/backend/model_manager/configs/test_ltx2_lora_probe_independence.py@@ -21,7 +21,7 @@ import torchfrom invokeai.backend.model_manager.configs.base import Config_Basefrom invokeai.backend.model_manager.configs.identification_utils import NotAMatchError-from invokeai.backend.model_manager.configs.lora import LoRA_LyCORIS_Anima_Config, LoRA_LyCORIS_LTX2_Config+from invokeai.backend.model_manager.configs.lora import LoRA_LyCORIS_LTX2_Configfrom invokeai.backend.model_manager.taxonomy import BaseModelType@@ -141,7 +141,10 @@ def _anima_kohya_keys() -> dict[str, torch.Tensor]:return sd[email protected]("prefix", ["", "diffusion_model.", "transformer.", "base_model.model.transformer."])+ALL_PREFIXES = ["", "diffusion_model.", "transformer.", "base_model.model.", "base_model.model.transformer."]++[email protected]("prefix", ALL_PREFIXES)def test_an_ltx2_lora_identifies_as_ltx2(prefix: str) -> None:matched, result = _probe(LoRA_LyCORIS_LTX2_Config, _ltx2_distilled_keys(prefix))@@ -149,7 +152,7 @@ def test_an_ltx2_lora_identifies_as_ltx2(prefix: str) -> None:assert result.base is BaseModelType.LTX2[email protected]("prefix", ["", "diffusion_model.", "transformer.", "base_model.model.transformer."])[email protected]("prefix", ALL_PREFIXES)def test_an_ltx2_lora_is_claimed_by_the_ltx2_probe_alone(prefix: str) -> None:"""Every LoRA probe, whatever the prefix. Unprefixed or under ``transformer.`` its keys startwith ``transformer_blocks.`` as Qwen Image's do; as wide as Qwen-Image-2.1 (4096), it names@@ -177,13 +180,20 @@ def test_another_familys_lora_is_rejected_by_the_ltx2_probe(name: str, keys) ->assert not matched[email protected]("prefix", ["", "transformer.", "base_model.model.", "base_model.model.transformer."])-def test_a_video_only_ltx2_lora_is_rejected_by_the_anima_probe(prefix: str) -> None:- """Without the audio tower nothing in the key names tells it from Anima's diffusers layout; the 4096-wide- attention does."""- video_tower = {k: v for k, v in _ltx2_distilled_keys(prefix).items() if ".attn1." in k}+ "prefix", ["", "diffusion_model.", "transformer.", "base_model.model.", "base_model.model.transformer."]+)+def test_a_video_stream_only_lora_is_claimed_by_no_ltx2_probe(prefix: str) -> None:+ """LTX-Video 13B (`LTXVideoTransformer3DModel`: 48 blocks, 32 heads of 128, 4096-wide text context) names and+ shapes its attention exactly as LTX-2's video stream does, so without an audio-side key the file cannot be told+ from an LTX-2 one and must not be claimed as LTX-2."""+ layers = {"attn1.to_q": 4096, "attn1.to_out.0": 4096, "attn2.to_q": 4096, "attn2.to_k": 4096, "attn2.to_v": 4096}+ sd: dict[str, torch.Tensor] = {}+ for name, width in layers.items():+ sd[f"{prefix}transformer_blocks.0.{name}.lora_A.weight"] = _z(8, width)+ sd[f"{prefix}transformer_blocks.0.{name}.lora_B.weight"] = _z(width, 8)- matched, _ = _probe(LoRA_LyCORIS_Anima_Config, video_tower)+ matched, _ = _probe(LoRA_LyCORIS_LTX2_Config, sd)assert not matcheddiff --git a/tests/backend/patches/lora_conversions/test_ltx2_lora_conversion.py b/tests/backend/patches/lora_conversions/test_ltx2_lora_conversion.pyindex 37c90dc22e..dc6111632a 100644--- a/tests/backend/patches/lora_conversions/test_ltx2_lora_conversion.py+++ b/tests/backend/patches/lora_conversions/test_ltx2_lora_conversion.py@@ -144,11 +144,17 @@ def test_the_official_names_the_2_0_map_misses_are_still_renamed() -> None:assert "proj_in" in renamed[email protected]("prefix", ["", "diffusion_model.", "transformer.", "base_model.model.transformer."])+ "prefix",+ ["", "diffusion_model.", "transformer.", "base_model.model.", "base_model.model.transformer."],+)def test_the_peft_prefixes_seen_in_the_wild_all_strip(prefix: str) -> None:patch = lora_model_from_ltx2_state_dict(_published_state_dict(prefix=prefix))+ bare = lora_model_from_ltx2_state_dict(_published_state_dict(prefix=""))assert len(patch.layers) == len(PUBLISHED_LAYER_PATHS)+ # Stripped, not carried through: a left-over prefix would resolve onto no module.+ assert patch.layers.keys() == bare.layers.keys()def test_an_unknown_layer_keeps_its_own_name_so_the_patcher_can_report_it() -> None: