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): preserve FLUX PEFT alpha values (#10058)
Guishi Wangb44a4eee3b8b
diff --git a/docs/src/content/docs/Users Guide/03.Models/03.model-format-support.mdx b/docs/src/content/docs/Users Guide/03.Models/03.model-format-support.mdxindex 6a4d39ff81..97693f0ab3 100644--- a/docs/src/content/docs/Users Guide/03.Models/03.model-format-support.mdx+++ b/docs/src/content/docs/Users Guide/03.Models/03.model-format-support.mdx@@ -146,6 +146,9 @@ themselves.| LTX-2, MiniMax H3 | ✓ (plain low-rank; LoKR, LoHA and DoRA are refused) | ✗ | || SD 3, CogView4, Ideogram 4, ERNIE-Image, Qwen-Image-2.1, MOSS-Image | ✗ | ✗ | |+FLUX.1 and FLUX.2 also accept ComfyUI/OneTrainer PEFT LoRAs whose module names start with `diffusion_model.`.+Per-layer `.alpha` values in these `diffusion_model.` files set the LoRA scale.+For SD 1.x / 2.x and FLUX.2, LyCORIS LoKr files may store either Kronecker operand whole or as afactorized `_a` / `_b` pair. FLUX.2 also accepts LoHA files in the same BFL and kohya layouts.diff --git a/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-1.mdx b/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-1.mdxindex 4a6e54db6f..a9fd7d12f9 100644--- a/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-1.mdx+++ b/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-1.mdx@@ -141,7 +141,9 @@ recommended.FLUX.1 LoRAs can be applied from the Generate tab or with the **Apply LoRA - FLUX** and **Apply LoRACollection - FLUX** nodes. They patch the transformer and, where the LoRA includes text-encoder layers, the-CLIP and T5 encoders.+CLIP and T5 encoders. Supported layouts include LyCORIS, Diffusers/PEFT, OMI and ComfyUI/OneTrainer PEFT files+whose module names start with `diffusion_model.`. Per-layer `.alpha` values in these `diffusion_model.` files set+the LoRA scale.## PiD super-resolution decodediff --git a/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-2.mdx b/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-2.mdxindex 636867af75..e57920c75f 100644--- a/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-2.mdx+++ b/docs/src/content/docs/Users Guide/03.Models/Local Models/flux-2.mdx@@ -186,7 +186,9 @@ happens.FLUX.2 LoRAs are labelled with the variant they were trained for, and the Generate tab only offers LoRAsthat match the selected model's variant. LoRAs patch the transformer and, where they include text-encoderlayers, the Qwen3 or Mistral encoder. In workflows use **Apply LoRA - Flux2 Klein** or **Apply LoRA --FLUX.2 [dev]** (and their *Collection* versions).+FLUX.2 [dev]** (and their *Collection* versions). Supported layouts include LyCORIS, Diffusers/PEFT and+ComfyUI/OneTrainer PEFT files whose module names start with `diffusion_model.`. Per-layer `.alpha` values in+these `diffusion_model.` files set the LoRA scale.LyCORIS LoKr and LoHA files are supported in BFL and kohya layouts; each LoKr operand may be stored wholeor as a factorized `_a` / `_b` pair.diff --git a/invokeai/backend/patches/lora_conversions/flux_diffusers_lora_conversion_utils.py b/invokeai/backend/patches/lora_conversions/flux_diffusers_lora_conversion_utils.pyindex 31e59409d6..578c20cce1 100644--- a/invokeai/backend/patches/lora_conversions/flux_diffusers_lora_conversion_utils.py+++ b/invokeai/backend/patches/lora_conversions/flux_diffusers_lora_conversion_utils.py@@ -399,13 +399,17 @@ def _group_by_layer_mixed_format(state_dict: Dict[str, torch.Tensor]) -> dict[stdef _group_by_layer(state_dict: Dict[str, torch.Tensor]) -> dict[str, dict[str, torch.Tensor]]:- """Groups the keys in the state dict by layer."""+ """Groups LoRA weights and optional alpha values by layer."""layer_dict: dict[str, dict[str, torch.Tensor]] = {}for key in state_dict:- # Split the 'lora_A.weight' or 'lora_B.weight' suffix from the layer name.- parts = key.rsplit(".", maxsplit=2)- layer_name = parts[0]- key_name = ".".join(parts[1:])+ if key.endswith(".alpha"):+ layer_name = key.removesuffix(".alpha")+ key_name = "alpha"+ else:+ # Split a two-part weight suffix such as 'lora_A.weight' from the layer name.+ parts = key.rsplit(".", maxsplit=2)+ layer_name = parts[0]+ key_name = ".".join(parts[1:])if layer_name not in layer_dict:layer_dict[layer_name] = {}layer_dict[layer_name][key_name] = state_dict[key]diff --git a/tests/backend/patches/lora_conversions/test_flux_aitoolkit_lora_conversion_utils.py b/tests/backend/patches/lora_conversions/test_flux_aitoolkit_lora_conversion_utils.pyindex 648f17438b..9e95f9fee1 100644--- a/tests/backend/patches/lora_conversions/test_flux_aitoolkit_lora_conversion_utils.py+++ b/tests/backend/patches/lora_conversions/test_flux_aitoolkit_lora_conversion_utils.py@@ -1,5 +1,6 @@import accelerateimport pytest+import torchfrom invokeai.backend.flux.model import Fluxfrom invokeai.backend.flux.util import get_flux_transformers_params@@ -64,3 +65,25 @@ def test_lora_model_from_flux_aitoolkit_state_dict():state_dict = keys_to_mock_state_dict(flux_aitoolkit_state_dict_keys)assert lora_model_from_flux_aitoolkit_state_dict(state_dict)+++def test_lora_model_from_flux_aitoolkit_state_dict_groups_alpha_with_block_and_top_level_layers():+ rank = 4+ alpha = 2.0+ state_dict: dict[str, torch.Tensor] = {}+ for layer_name in ("double_blocks.0.img_attn.proj", "img_in"):+ prefix = f"diffusion_model.{layer_name}"+ state_dict[f"{prefix}.lora_A.weight"] = torch.ones(rank, 8)+ state_dict[f"{prefix}.lora_B.weight"] = torch.ones(8, rank)+ state_dict[f"{prefix}.alpha"] = torch.tensor(alpha)++ assert is_state_dict_likely_in_flux_aitoolkit_format(state_dict)++ model = lora_model_from_flux_aitoolkit_state_dict(state_dict)++ expected_keys = {+ "lora_transformer-double_blocks.0.img_attn.proj",+ "lora_transformer-img_in",+ }+ assert set(model.layers) == expected_keys+ assert all(layer.scale() == pytest.approx(alpha / rank) for layer in model.layers.values())