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.

fix(lora): reject MiniMax H3 LoRA layers that resolve to no module (#10364)

Lincoln Stein0ef6a931bcc1
diff --git a/invokeai/backend/patches/lora_conversions/minimax_h3_lora_constants.py b/invokeai/backend/patches/lora_conversions/minimax_h3_lora_constants.py
index b8d18f34e7..9131dfd3a2 100644
--- a/invokeai/backend/patches/lora_conversions/minimax_h3_lora_constants.py
+++ b/invokeai/backend/patches/lora_conversions/minimax_h3_lora_constants.py
@@ -164,6 +164,27 @@ _VALID_LAYER_PARAM_SETS = (
)
_PEFT_PREFIX_STRIP_RE = re.compile(_PEFT_PREFIX_RE)
+# Every released H3 checkpoint has 50 transformer and 2 refiner blocks (the loader derives the same
+# counts from the weights). The probe never sees the base model, so these bound the indices it admits.
+MINIMAX_H3_NUM_BLOCKS = 50
+MINIMAX_H3_NUM_REFINER_BLOCKS = 2
+
+
+def _minimax_h3_target_defect(layer: str) -> str | None:
+ """Why ``layer`` resolves to no H3 Linear, or None. Paths follow the kohya parsing tree's vocabulary."""
+ if not kohya_module_path_is_leaf(layer, _H3_KOHYA_PARSING_TREE):
+ return f"layer {layer!r} targets no MiniMax H3 transformer module"
+ parts = layer.split(".")
+ # Exact match against str(n): ``blocks.00`` or a non-ASCII digit passes the tree's isnumeric() but
+ # becomes ``transformer_blocks.00``, which no ModuleList resolves.
+ for stem, index_pos, count in (
+ ("blocks", 1, MINIMAX_H3_NUM_BLOCKS),
+ ("token_refiner", 2, MINIMAX_H3_NUM_REFINER_BLOCKS),
+ ):
+ if parts[0] == stem and parts[index_pos] not in {str(n) for n in range(count)}:
+ return f"layer {layer!r} has block index {parts[index_pos]!r}; expected 0-{count - 1}"
+ return None
+
def group_minimax_h3_lora_keys(str_keys: Iterable[str]) -> dict[str, dict[str, str]]:
"""Group raw H3 LoRA keys as ``{layer path: {param name: raw key}}``, or raise ``ValueError``.
@@ -173,7 +194,9 @@ def group_minimax_h3_lora_keys(str_keys: Iterable[str]) -> dict[str, dict[str, s
LoRA parameter name (DoRA ``lora_magnitude_vector``, full weights, ...), two raw keys naming
the same tensor (e.g. a layer present both dotted and kohya-flattened), or a layer whose
tensors are not exactly one ``lora_A``/``lora_B`` or ``lora_down``/``lora_up`` pair plus an
- optional ``alpha``. Converting any of these would crash, or silently drop or overwrite tensors.
+ optional ``alpha``, or a layer that names no H3 Linear (foreign ``lora_te_*`` encoder keys,
+ zero-padded or out-of-range block indices). Converting any of these would crash, or silently
+ drop or overwrite tensors.
"""
grouped: dict[str, dict[str, str]] = {}
for raw_key in str_keys:
@@ -194,6 +217,8 @@ def group_minimax_h3_lora_keys(str_keys: Iterable[str]) -> dict[str, dict[str, s
f"layer {layer!r} carries {sorted(layer_params)}; expected exactly lora_A/lora_B or "
"lora_down/lora_up, optionally with alpha"
)
+ if (target_defect := _minimax_h3_target_defect(layer)) is not None:
+ raise ValueError(target_defect)
return grouped
diff --git a/tests/backend/patches/lora_conversions/test_minimax_h3_lora_conversion_utils.py b/tests/backend/patches/lora_conversions/test_minimax_h3_lora_conversion_utils.py
index 4e89994bb2..a965fbe777 100644
--- a/tests/backend/patches/lora_conversions/test_minimax_h3_lora_conversion_utils.py
+++ b/tests/backend/patches/lora_conversions/test_minimax_h3_lora_conversion_utils.py
@@ -296,6 +296,18 @@ def _malformed_cases() -> list[tuple[str, dict[str, torch.Tensor], str]]:
peft_dora = _make_turbo_style_state_dict()
peft_dora["blocks.0.attn.out_proj.lora_magnitude_vector.weight"] = torch.ones(HIDDEN)
+ # Valid H3 adapter plus a complete text-encoder pair, which would map onto no H3 module.
+ with_text_encoder = _make_turbo_style_state_dict()
+ with_text_encoder["lora_te_text_model_encoder_layers_0_mlp_fc1.lora_A.weight"] = torch.zeros(RANK, HIDDEN)
+ with_text_encoder["lora_te_text_model_encoder_layers_0_mlp_fc1.lora_B.weight"] = torch.zeros(HIDDEN, RANK)
+
+ def _with_layer(stem: str, kohya: bool = False) -> dict[str, torch.Tensor]:
+ sd = _make_turbo_style_state_dict()
+ down, up = _lora_pair(HIDDEN, HIDDEN)
+ sd[f"{stem}.attn.out_proj.lora_A.weight"] = down
+ sd[f"{stem}.attn.out_proj.lora_B.weight"] = up
+ return _to_kohya(sd, alpha=RANK) if kohya else sd
+
return [
("kohya-down-only", kohya_down_only, r"\['alpha', 'lora_down.weight'\]"),
("dotted-A-only", dotted_a_only, r"\['lora_A.weight'\]"),
@@ -303,6 +315,16 @@ def _malformed_cases() -> list[tuple[str, dict[str, torch.Tensor], str]]:
("dotted-and-kohya", dotted_and_kohya, "two spellings"),
("mixed-A/B-and-down/up", mixed_spellings, "'lora_A.weight', 'lora_B.weight', 'lora_down.weight'"),
("peft-dora-magnitude", peft_dora, "lora_magnitude_vector"),
+ ("foreign-text-encoder-pair", with_text_encoder, "lora_te_text_model.*targets no MiniMax H3"),
+ ("zero-padded-block", _with_layer("blocks.01"), "'blocks.01.attn.out_proj' has block index '01'"),
+ (
+ "zero-padded-block-kohya",
+ _with_layer("blocks.01", kohya=True),
+ "'blocks.01.attn.out_proj' has block index '01'",
+ ),
+ ("non-ascii-digit-block", _with_layer("blocks.1\u0660"), "has block index"),
+ ("out-of-range-block", _with_layer("blocks.50"), "index '50'; expected 0-49"),
+ ("out-of-range-refiner-block", _with_layer("token_refiner.blocks.2"), "index '2'; expected 0-1"),
]