Skip to content

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

Open
Pfannkuchensack wants to merge 2 commits intoinvoke-ai:mainfrom
Pfannkuchensack:fix/flux2-kohya-transformer-only-lora-detection
Open

fix(model_manager): detect Flux 2 Klein LoRAs in Kohya format with transformer-only keys#8938
Pfannkuchensack wants to merge 2 commits intoinvoke-ai:mainfrom
Pfannkuchensack:fix/flux2-kohya-transformer-only-lora-detection

Conversation

@Pfannkuchensack
Copy link
Collaborator

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
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant