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.

Merge branch 'main' into feat/krea2-lokr-lora-support

Balrog99443bf95cf3bce
diff --cc tests/backend/patches/lora_conversions/test_krea2_lora_conversion_utils.py
index 6e8492c50a,5fc9b4d2d8..7e7fd48b35
--- a/tests/backend/patches/lora_conversions/test_krea2_lora_conversion_utils.py
+++ b/tests/backend/patches/lora_conversions/test_krea2_lora_conversion_utils.py
@@@ -1,17 -1,20 +1,24 @@@
+ import accelerate
import pytest
import torch
+ from diffusers import Krea2Transformer2DModel
+ from invokeai.backend.model_manager.load.model_loaders.krea2 import KREA2_TRANSFORMER_CONFIG
from invokeai.backend.patches.layers.dora_layer import DoRALayer
+from invokeai.backend.patches.layers.lokr_layer import LoKRLayer
from invokeai.backend.patches.layers.lora_layer import LoRALayer
from invokeai.backend.patches.lora_conversions.krea2_lora_constants import (
KREA2_LORA_QWEN3VL_PREFIX,
KREA2_LORA_TRANSFORMER_PREFIX,
)
-from invokeai.backend.patches.lora_conversions.krea2_lora_conversion_utils import lora_model_from_krea2_state_dict
+from invokeai.backend.patches.lora_conversions.krea2_lora_conversion_utils import (
+ is_state_dict_likely_krea2_lora,
+ lora_model_from_krea2_state_dict,
+)
+ from tests.backend.patches.lora_conversions.lora_state_dicts.krea2_lora_kohya_format import (
+ state_dict_keys as krea2_kohya_state_dict_keys,
+ )
+ from tests.backend.patches.lora_conversions.lora_state_dicts.utils import keys_to_mock_state_dict
def test_peft_layer_preserves_explicit_alpha() -> None:
@@@ -212,6 -215,196 +219,201 @@@ def test_single_module_native_krea2_lor
assert set(model.layers) == {f"{KREA2_LORA_TRANSFORMER_PREFIX}{diffusers_module}"}
+ @pytest.mark.parametrize(
+ ("kohya_module", "diffusers_module"),
+ [
+ ("blocks_0_attn_wq", "transformer_blocks.0.attn.to_q"),
+ ("blocks_0_attn_wk", "transformer_blocks.0.attn.to_k"),
+ ("blocks_0_attn_wv", "transformer_blocks.0.attn.to_v"),
+ ("blocks_0_attn_wo", "transformer_blocks.0.attn.to_out.0"),
+ ("blocks_0_attn_gate", "transformer_blocks.0.attn.to_gate"),
+ # Multi-digit block index.
+ ("blocks_27_mlp_down", "transformer_blocks.27.ff.down"),
+ # `layerwise_blocks` / `refiner_blocks` are the native components that themselves contain an
+ # underscore, i.e. the only genuine ambiguity in the flattened form.
+ ("txtfusion_layerwise_blocks_0_attn_wo", "text_fusion.layerwise_blocks.0.attn.to_out.0"),
+ ("txtfusion_refiner_blocks_1_mlp_gate", "text_fusion.refiner_blocks.1.ff.gate"),
+ ("txtfusion_projector", "text_fusion.projector"),
+ ("first", "img_in"),
+ ("tmlp_0", "time_embed.linear_1"),
+ ("tmlp_2", "time_embed.linear_2"),
+ ("tproj_1", "time_mod_proj"),
+ ("txtmlp_1", "txt_in.linear_1"),
+ ("txtmlp_3", "txt_in.linear_2"),
+ ("last_linear", "final_layer.linear"),
+ ],
+ )
+ def test_kohya_flattened_krea2_module_is_remapped(kohya_module: str, diffusers_module: str) -> None:
+ # kohya / LyCORIS flatten the module path and prefix it with `lora_unet_`. Without un-flattening, every key
+ # misses its module and the adapter is a silent no-op ("Failed to find module for LoRA layer key:
+ # lora_transformer-lora_unet_blocks_6_attn_wv").
+ state_dict = {
+ f"lora_unet_{kohya_module}.lora_down.weight": torch.ones(2, 4),
+ f"lora_unet_{kohya_module}.lora_up.weight": torch.ones(4, 2),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ assert set(model.layers) == {f"{KREA2_LORA_TRANSFORMER_PREFIX}{diffusers_module}"}
+
+
+ def test_kohya_krea2_lora_layers_match_the_real_transformer() -> None:
+ # Every layer of a real kohya Krea-2 adapter must land on an actual Linear of Krea2Transformer2DModel, with
+ # in/out features that agree with the LoRA's own down/up shapes. A wrong rename (e.g. ff.gate <-> ff.down,
+ # whose SwiGLU shapes are transposed) is caught here rather than as a runtime warning.
+ state_dict = keys_to_mock_state_dict(krea2_kohya_state_dict_keys)
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ with accelerate.init_empty_weights():
+ transformer = Krea2Transformer2DModel(**KREA2_TRANSFORMER_CONFIG)
+
+ # 4 blocks x 8 Linears (2 transformer, 1 layerwise + 1 refiner text-fusion) + 8 top-level modules.
+ assert len(model.layers) == 40
+ for layer_key, layer in model.layers.items():
+ module_name = layer_key[len(KREA2_LORA_TRANSFORMER_PREFIX) :]
+ submodule = transformer.get_submodule(module_name)
+ assert isinstance(submodule, torch.nn.Linear), f"{module_name} is not a Linear"
+ out_features, in_features = submodule.weight.shape
+ assert layer.down.shape[1] == in_features, f"{module_name} in_features mismatch"
+ assert layer.up.shape[0] == out_features, f"{module_name} out_features mismatch"
+
+
+ def test_kohya_flattened_krea2_layer_preserves_alpha() -> None:
+ # kohya adapters carry an explicit `.alpha`; it must survive the un-flattening intact, otherwise the LoRA
+ # applies at the wrong strength.
+ state_dict = {
+ "lora_unet_blocks_6_attn_wv.lora_down.weight": torch.ones(2, 4),
+ "lora_unet_blocks_6_attn_wv.lora_up.weight": torch.ones(4, 2),
+ "lora_unet_blocks_6_attn_wv.alpha": torch.tensor(2.0),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ layer = model.layers[f"{KREA2_LORA_TRANSFORMER_PREFIX}transformer_blocks.6.attn.to_v"]
+ assert isinstance(layer, LoRALayer)
+ assert layer._alpha == 2.0
+
+
+ def test_kohya_flattened_krea2_keys_tolerate_doubled_separator() -> None:
+ state_dict = {
+ "lora_unet__blocks_6_attn_wv.lora_down.weight": torch.ones(2, 4),
+ "lora_unet__blocks_6_attn_wv.lora_up.weight": torch.ones(4, 2),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ assert set(model.layers) == {f"{KREA2_LORA_TRANSFORMER_PREFIX}transformer_blocks.6.attn.to_v"}
+
+
+ @pytest.mark.parametrize(
+ "flat_module",
+ [
+ # Non-Linear natives: `mod.lin` is folded into the `scale_shift_table` parameter and the norms have no
+ # Linear counterpart, so there is nothing to patch. They must not be renamed into a key that merely
+ # looks applicable.
+ "blocks_6_mod_lin",
+ "blocks_6_prenorm",
+ "blocks_6_attn_qknorm_qnorm",
+ # Not a Krea-2 module layout at all (e.g. a flattened adapter for some other architecture).
+ "double_blocks_0_img_attn_proj",
+ # Sequential positions that hold an activation rather than a Linear. The parsing tree enumerates the
+ # indices it accepts, so these are rejected outright instead of being rewritten to `tmlp.1.*` — a
+ # half-converted key that the native pass no longer recognizes.
+ "tmlp_1",
+ "tproj_0",
+ "txtmlp_2",
+ ],
+ )
+ def test_unrecognized_kohya_flattened_keys_are_left_untouched(flat_module: str) -> None:
+ state_dict = {
+ f"lora_unet_{flat_module}.lora_down.weight": torch.ones(2, 4),
+ f"lora_unet_{flat_module}.lora_up.weight": torch.ones(4, 2),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ # Left verbatim rather than rewritten into a plausible-looking but wrong module path.
+ assert set(model.layers) == {f"{KREA2_LORA_TRANSFORMER_PREFIX}lora_unet_{flat_module}"}
+
+
+ @pytest.mark.parametrize(
- "lycoris_suffixes",
++ ("lycoris_suffixes", "expected_second_layer"),
+ [
- ("lokr_w1", "lokr_w2"),
- ("hada_w1_a", "hada_w1_b", "hada_w2_a", "hada_w2_b"),
- ("diff", "diff_b"),
++ # LoKr is a layout this converter understands, so its module un-flattens like any other and the
++ # adapter actually applies. The other algorithms below have no handler, so they stay verbatim.
++ (("lokr_w1", "lokr_w2"), "transformer_blocks.6.attn.to_q"),
++ (("hada_w1_a", "hada_w1_b", "hada_w2_a", "hada_w2_b"), "lora_unet_blocks_6_attn_wq"),
++ (("diff", "diff_b"), "lora_unet_blocks_6_attn_wq"),
+ # LyCORIS saves an `alpha` per module, so this is the realistic on-disk shape rather than an
+ # edge case — see this repo's own captured fixtures. `.alpha` is a suffix the converter knows,
+ # so deciding per key rewrote it while its siblings stayed verbatim, splitting one module into
+ # two groups and aborting the load on the orphaned `{'alpha'}`.
- ("lokr_w1", "lokr_w2", "alpha"),
- ("hada_w1_a", "hada_w1_b", "hada_w2_a", "hada_w2_b", "alpha"),
++ (("lokr_w1", "lokr_w2", "alpha"), "transformer_blocks.6.attn.to_q"),
++ (("hada_w1_a", "hada_w1_b", "hada_w2_a", "hada_w2_b", "alpha"), "lora_unet_blocks_6_attn_wq"),
+ # `dora_scale` is deliberately not combined with a LyCORIS algorithm here: it would orphan the
+ # same way, but even grouped correctly `any_lora_layer_from_state_dict` tests `dora_scale`
+ # before `lokr_w1`, so a weight-decomposed LoKr dispatches to DoRALayer and dies on a missing
+ # `lora_up.weight`. That precedence is shared code, predates this branch, and is not what the
+ # per-module gate below is about.
+ ],
+ )
-def test_kohya_lycoris_algorithm_keys_do_not_abort_the_load(lycoris_suffixes: tuple[str, ...]) -> None:
++def test_kohya_lycoris_algorithm_keys_do_not_abort_the_load(
++ lycoris_suffixes: tuple[str, ...], expected_second_layer: str
++) -> None:
+ # LyCORIS supports per-module algorithms, so one kohya file can mix ordinary lora_down/up modules with
+ # LoKr/LoHa/full ones. Un-flattening a key whose suffix `_group_by_layer` cannot split back off used to
+ # feed it a dotted path, whose blind `rsplit(".", 2)` fallback then cut inside the module name and fused
+ # two modules into one unsupported layer — aborting the *entire* adapter at generation time.
+ # `alpha` is a scalar on disk and `dora_scale` a per-channel vector; giving them weight-shaped
+ # tensors would fail inside the layer for reasons that have nothing to do with the grouping.
+ shapes = {"alpha": torch.tensor(4.0), "dora_scale": torch.ones(4)}
+ state_dict = {
+ "lora_unet_blocks_0_attn_wv.lora_down.weight": torch.ones(2, 4),
+ "lora_unet_blocks_0_attn_wv.lora_up.weight": torch.ones(4, 2),
+ **{f"lora_unet_blocks_6_attn_wq.{suffix}": shapes.get(suffix, torch.ones(4, 4)) for suffix in lycoris_suffixes},
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
- # The ordinary module still converts, and the LyCORIS one stays verbatim so it degrades to the per-layer
- # "Failed to find module" warning at apply time rather than taking the whole adapter down.
++ # Either way the load completes: a supported algorithm converts onto its real module, an unsupported one
++ # stays verbatim and degrades to the per-layer "Failed to find module" warning at apply time, rather than
++ # taking the whole adapter down.
+ assert set(model.layers) == {
+ f"{KREA2_LORA_TRANSFORMER_PREFIX}transformer_blocks.0.attn.to_v",
- f"{KREA2_LORA_TRANSFORMER_PREFIX}lora_unet_blocks_6_attn_wq",
++ f"{KREA2_LORA_TRANSFORMER_PREFIX}{expected_second_layer}",
+ }
+
+
+ def test_non_string_keys_survive_the_kohya_and_native_passes() -> None:
+ # `.pt`/`.ckpt` sources can carry non-string keys. Once the kohya pass rewrites something, the native pass
+ # runs its substring tests over every key — which raised `TypeError: argument of type 'int' is not
+ # iterable` on an int key rather than leaving it alone.
+ state_dict = {
+ 0: torch.ones(2),
+ "lora_unet_blocks_0_attn_wv.lora_down.weight": torch.ones(2, 4),
+ "lora_unet_blocks_0_attn_wv.lora_up.weight": torch.ones(4, 2),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ assert f"{KREA2_LORA_TRANSFORMER_PREFIX}transformer_blocks.0.attn.to_v" in model.layers
+
+
+ def test_conflicting_kohya_and_native_aliases_raise() -> None:
+ # The flattened and dotted spellings of one logical layer normalize to the same target. Providing both
+ # must raise instead of silently dropping one based on dict ordering.
+ state_dict = {
+ "lora_unet_blocks_0_attn_wq.lora_down.weight": torch.ones(2, 4),
+ "lora_unet_blocks_0_attn_wq.lora_up.weight": torch.ones(4, 2),
+ "blocks.0.attn.wq.lora_down.weight": torch.full((2, 4), 2.0),
+ "blocks.0.attn.wq.lora_up.weight": torch.full((4, 2), 2.0),
+ }
+
+ with pytest.raises(ValueError, match="normalize to the same target"):
+ lora_model_from_krea2_state_dict(state_dict)
+
+
def test_native_transformer_remap_does_not_change_diffusers_text_encoder_blocks() -> None:
state_dict = {
"diffusion_model.blocks.0.attn.wq.lora_A.weight": torch.ones(2, 4),
@@@ -254,79 -447,3 +456,96 @@@ def test_native_krea2_top_level_linear_
f"{KREA2_LORA_TRANSFORMER_PREFIX}{diffusers_module}" for diffusers_module in native_to_diffusers.values()
}
assert expected_keys < set(model.layers)
+
+
+def test_lokr_layer_produces_lokr_layer() -> None:
+ # LyCORIS LoKr adapters (e.g. those produced by ai-toolkit for Krea-2) carry Kronecker factors instead of
+ # a lora_A/lora_B pair. They must survive _group_by_layer intact so any_lora_layer_from_state_dict can
+ # route them to LoKRLayer.
+ state_dict = {
+ "transformer.text_fusion.0.attn.to_q.lokr_w1": torch.ones(2, 2),
+ "transformer.text_fusion.0.attn.to_q.lokr_w2": torch.ones(3, 4),
+ "transformer.text_fusion.0.attn.to_q.alpha": torch.tensor(1.0),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ layer = model.layers[f"{KREA2_LORA_TRANSFORMER_PREFIX}text_fusion.0.attn.to_q"]
+ assert isinstance(layer, LoKRLayer)
+ assert layer._alpha == 1.0
+ # The reconstructed weight is the Kronecker product of the two factors.
+ assert layer.get_weight(torch.empty(6, 8)).shape == (6, 8)
+
+
+def test_factored_lokr_layer_produces_lokr_layer() -> None:
+ # LoKr may factor either Kronecker operand further into an `_a`/`_b` pair. Both spellings must be grouped
+ # onto the same layer.
+ state_dict = {
+ "transformer.text_fusion.0.attn.to_q.lokr_w1_a": torch.ones(2, 1),
+ "transformer.text_fusion.0.attn.to_q.lokr_w1_b": torch.ones(1, 2),
+ "transformer.text_fusion.0.attn.to_q.lokr_w2_a": torch.ones(3, 1),
+ "transformer.text_fusion.0.attn.to_q.lokr_w2_b": torch.ones(1, 4),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ layer = model.layers[f"{KREA2_LORA_TRANSFORMER_PREFIX}text_fusion.0.attn.to_q"]
+ assert isinstance(layer, LoKRLayer)
+ assert layer.w1_a is not None and layer.w1_b is not None
+ assert layer.w2_a is not None and layer.w2_b is not None
+
+
+def test_native_lokr_keys_are_renamed_to_diffusers_layout() -> None:
+ # Native (ComfyUI / ai-toolkit) LoKr keys must go through the same native->diffusers renaming as LoRA
+ # keys: txtfusion -> text_fusion, attn.wq -> attn.to_q, mlp.down -> ff.down.
+ state_dict = {
+ "diffusion_model.txtfusion.layerwise_blocks.0.attn.wq.lokr_w1": torch.ones(2, 2),
+ "diffusion_model.txtfusion.layerwise_blocks.0.attn.wq.lokr_w2": torch.ones(3, 4),
+ "diffusion_model.txtfusion.refiner_blocks.1.mlp.down.lokr_w1": torch.ones(2, 2),
+ "diffusion_model.txtfusion.refiner_blocks.1.mlp.down.lokr_w2": torch.ones(3, 4),
+ }
+
+ model = lora_model_from_krea2_state_dict(state_dict)
+
+ assert set(model.layers) == {
+ f"{KREA2_LORA_TRANSFORMER_PREFIX}text_fusion.layerwise_blocks.0.attn.to_q",
+ f"{KREA2_LORA_TRANSFORMER_PREFIX}text_fusion.refiner_blocks.1.ff.down",
+ }
+ assert all(isinstance(layer, LoKRLayer) for layer in model.layers.values())
+
+
+def test_is_state_dict_likely_krea2_lora_accepts_lokr() -> None:
+ state_dict = {
+ "diffusion_model.txtfusion.layerwise_blocks.0.attn.wq.lokr_w1": torch.ones(2, 2),
+ "diffusion_model.txtfusion.layerwise_blocks.0.attn.wq.lokr_w2": torch.ones(3, 4),
+ }
+
+ assert is_state_dict_likely_krea2_lora(state_dict)
+
+
+def test_is_state_dict_likely_krea2_lora_rejects_lokr_without_krea2_modules() -> None:
+ # The Krea-2 signature modules are still required: a LoKr targeting only generic transformer blocks
+ # belongs to another base (e.g. Qwen-Image) and must not be claimed here.
+ state_dict = {
+ "transformer.transformer_blocks.0.attn.to_q.lokr_w1": torch.ones(2, 2),
+ "transformer.transformer_blocks.0.attn.to_q.lokr_w2": torch.ones(3, 4),
+ }
+
+ assert not is_state_dict_likely_krea2_lora(state_dict)
++
++
++def test_kohya_flattened_lokr_converts_onto_its_real_module() -> None:
++ # A LoKr adapter saved in the kohya flattened layout has to clear both hurdles at once: the un-flattening
++ # pass has to reconstruct the dotted module path, and the grouper has to recognise the `lokr_*` suffixes.
++ # Before LoKr was a known suffix the module was left verbatim and the adapter was a silent no-op.
++ state_dict = {
++ "lora_unet_txtfusion_layerwise_blocks_0_attn_wq.lokr_w1": torch.ones(2, 2),
++ "lora_unet_txtfusion_layerwise_blocks_0_attn_wq.lokr_w2": torch.ones(3, 4),
++ "lora_unet_txtfusion_layerwise_blocks_0_attn_wq.alpha": torch.tensor(4.0),
++ }
++
++ model = lora_model_from_krea2_state_dict(state_dict)
++
++ layer = model.layers[f"{KREA2_LORA_TRANSFORMER_PREFIX}text_fusion.layerwise_blocks.0.attn.to_q"]
++ assert isinstance(layer, LoKRLayer)
++ assert layer.get_weight(torch.empty(6, 8)).shape == (6, 8)