Skip to content

fix(model_manager): detect Flux 2 Klein LoRAs in Kohya format with transformer-only keys - #8938

Merged
lstein merged 4 commits into
invoke-ai:mainfrom
Pfannkuchensack:fix/flux2-kohya-transformer-only-lora-detection
Mar 7, 2026
Merged

fix(model_manager): detect Flux 2 Klein LoRAs in Kohya format with transformer-only keys#8938
lstein merged 4 commits into
invoke-ai:mainfrom
Pfannkuchensack:fix/flux2-kohya-transformer-only-lora-detection

Conversation

@Pfannkuchensack

Copy link
Copy Markdown
Member

Summary

LoRAs trained with musubi-tuner (and potentially other trainers) that only target transformer blocks (double_blocks/single_blocks) without embedding layers (txt_in/vector_in/context_embedder) were incorrectly classified as Flux 1.

The existing Kohya-format detection in _is_flux2_lora_state_dict and _get_flux2_lora_variant only checked for embedding layer keys (lora_unet_txt_in.*, lora_unet_vector_in.*, lora_unet_context_embedder.*). LoRAs that exclusively target transformer blocks (e.g. lora_unet_double_blocks_0_img_attn_proj.*) fell through all checks and defaulted to Flux 1.

This PR adds fallback detection for Kohya-format LoRAs using transformer block tensor dimensions:

  • _is_flux2_lora_state_dict: Check img_attn_proj hidden_size (lora_down.weight shape[1]). If hidden_size ≠ 3072 → Flux 2. If hidden_size = 3072 (ambiguous between Flux 1 and Klein 4B), check MLP ratio via img_mlp_0 ffn_dim — Klein 4B uses mlp_ratio=6 (ffn_dim=18432) vs Flux 1 mlp_ratio=4 (ffn_dim=12288).
  • _get_flux2_lora_variant: Same img_attn_proj hidden_size check to determine Klein 4B (3072) vs Klein 9B (4096).

Both functions also handle LoKR variants of these keys.

QA Instructions

Tested with simulated state dicts matching the tensor shapes from a real musubi-tuner Flux 2 Klein 9B LoRA:

State dict _is_flux2_lora_state_dict _get_flux2_lora_variant
Klein 9B (hidden_size=4096) True Klein9B
Klein 4B (hidden_size=3072, mlp_ratio=6) True Klein4B
Flux 1 (hidden_size=3072, mlp_ratio=4) False

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • ❗Changes to a redux slice have a corresponding migration
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

…ansformer-only keys

LoRAs trained with musubi-tuner (and potentially other trainers) that
only target transformer blocks (double_blocks/single_blocks) without
embedding layers (txt_in/vector_in/context_embedder) were incorrectly
classified as Flux 1. Add fallback detection using attention projection
hidden_size and MLP ratio from transformer block tensors
@github-actions github-actions Bot added python PRs that change python files backend PRs that change backend files labels Mar 2, 2026
@lstein lstein self-assigned this Mar 7, 2026
@lstein lstein added the v6.12.0 Intended for 6.12.0 release label Mar 7, 2026

@lstein lstein left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add a test for this format in model_identification/stripped_models?

@lstein
lstein self-requested a review March 7, 2026 01:37

@lstein lstein left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. No examples of this format easily available, so will go ahead and accept.

@lstein
lstein enabled auto-merge (squash) March 7, 2026 01:43
@lstein
lstein merged commit 274d9b3 into invoke-ai:main Mar 7, 2026
13 checks passed
@Pfannkuchensack
Pfannkuchensack deleted the fix/flux2-kohya-transformer-only-lora-detection branch March 7, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files python PRs that change python files v6.12.0 Intended for 6.12.0 release

Projects

Status: 6.12.x

Development

Successfully merging this pull request may close these issues.

2 participants