Skip to content

Commit b2b8820

Browse files
fix(model_manager): prevent Z-Image LoRAs from being misclassified as main models (invoke-ai#8754)
* fix(model_manager): prevent Z-Image LoRAs from being misclassified as main models Z-Image LoRAs containing keys like `diffusion_model.context_refiner.*` were being incorrectly classified as main checkpoint models instead of LoRAs. This happened because the `_has_z_image_keys()` function checked for Z-Image specific keys (like `context_refiner`) without verifying if the file was actually a LoRA. Since main models have higher priority than LoRAs in the classification sort order, the incorrect main model classification would win. The fix adds detection of LoRA-specific weight suffixes (`.lora_down.weight`, `.lora_up.weight`, `.lora_A.weight`, `.lora_B.weight`, `.dora_scale`) and returns False if any are found, ensuring LoRAs are correctly classified. * refactor(mm): simplify _has_z_image_keys with early return Return True directly when a Z-Image key is found instead of using an intermediate variable.
1 parent bb6c544 commit b2b8820

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

  • invokeai/backend/model_manager/configs

invokeai/backend/model_manager/configs/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,43 @@ def _has_main_keys(state_dict: dict[str | int, Any]) -> bool:
114114

115115

116116
def _has_z_image_keys(state_dict: dict[str | int, Any]) -> bool:
117-
"""Check if state dict contains Z-Image S3-DiT transformer keys."""
117+
"""Check if state dict contains Z-Image S3-DiT transformer keys.
118+
119+
This function returns True only for Z-Image main models, not LoRAs.
120+
LoRAs are excluded by checking for LoRA-specific weight suffixes.
121+
"""
118122
# Z-Image specific keys that distinguish it from other models
119123
z_image_specific_keys = {
120124
"cap_embedder", # Caption embedder - unique to Z-Image
121125
"context_refiner", # Context refiner blocks
122126
"cap_pad_token", # Caption padding token
123127
}
124128

129+
# LoRA-specific suffixes - if present, this is a LoRA not a main model
130+
lora_suffixes = (
131+
".lora_down.weight",
132+
".lora_up.weight",
133+
".lora_A.weight",
134+
".lora_B.weight",
135+
".dora_scale",
136+
)
137+
125138
for key in state_dict.keys():
126139
if isinstance(key, int):
127140
continue
141+
142+
# If we find any LoRA-specific keys, this is not a main model
143+
if key.endswith(lora_suffixes):
144+
return False
145+
128146
# Check for Z-Image specific key prefixes
129147
# Handle both direct keys (cap_embedder.0.weight) and
130148
# ComfyUI-style keys (model.diffusion_model.cap_embedder.0.weight)
131149
key_parts = key.split(".")
132150
for part in key_parts:
133151
if part in z_image_specific_keys:
134152
return True
153+
135154
return False
136155

137156

0 commit comments

Comments
 (0)