Skip to content

Swap shift and scale in Flux2 LoRAs of final_layer.adaLN_modulation.1 - #15276

Merged
sayakpaul merged 2 commits into
huggingface:mainfrom
audenvenn:fix-flux2-lora-final-layer-swap
Nov 13, 2026
Merged

sayakpaul merged 2 commits into
huggingface:mainfrom
audenvenn:fix-flux2-lora-final-layer-swap

Conversation

@audenvenn

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15275

_convert_non_diffusers_flux2_lora_to_diffusers mapped the original key final_layer.adaLN_modulation.1 to norm_out.linear unchanged. The two layers split their output differently:

  • The original final layer splits it into (shift, scale).
  • AdaLayerNormContinuous splits it into (scale, shift).

convert_flux2_transformer_checkpoint_to_diffusers swaps the halves for the weights. This PR does the same for the adapter: it swaps the halves of lora_B with the existing swap_scale_shift helper, because lora_B's rows are the layer's outputs. lora_A stays as it is.

The new test TestFlux2TransformerLoRA.test_lora_bfl_final_layer_modulation checks that the converted adapter's update equals the original update with its halves swapped.

Tests run (macOS 27.2 arm64, torch 2.15.0, Python 3.12):

$ python -m pytest tests/models/transformers/test_models_transformer_flux2.py -k test_lora_bfl_final_layer_modulation
1 passed, 207 deselected        # on main without the fix: 1 failed
$ python -m pytest tests/models/transformers/test_models_transformer_flux2.py -k "LoRA or LoKr"
11 failed, 29 passed, 10 skipped, 158 deselected

The 11 failures are the same with and without this change on this machine:

  • test_hotswapping_compile_on_different_shapes
  • test_hotswapping_compiled_model_linear
  • test_hotswapping_compiled_model_both_linear_and_other
  • test_lora_layerwise_casting_inference
  • test_lora_layerwise_casting_peft_input_autocast

ruff==0.9.10 check and ruff format --check pass on both files.

Self-review notes (the self-review skill's rubric: review-rules.md, code_style.md, testing.md):

  • Blocking: none.
  • Left for review: _convert_non_diffusers_flux2_lokr_to_diffusers maps the same key the same way. For LoKr, swapping the output halves only factors into the Kronecker factors when the first factor has an even number of rows (then you swap that factor's halves). I left it out rather than guess. Should it get that swap, or an error when it does not factor?
  • Dead code: none.
  • Verdict: READY.

Before submitting

Who can review?

@sayakpaul

🤖 Generated with Claude Code

The original final layer orders its modulation (shift, scale) and diffusers'
norm_out (scale, shift). The checkpoint converter swaps the halves; the LoRA
converter now swaps lora_B's halves the same way.

Fixes huggingface#15275

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sayakpaul

Copy link
Copy Markdown
Member

/diffusers-bot pytest tests/models/transformers/test_models_transformer_flux2.py::TestFlux2TransformerLoRA

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ pytest tests/models/transformers/test_models_transformer_flux2.py::TestFlux2TransformerLoRA passed on GPU — view logs.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@sayakpaul
sayakpaul merged commit 2c58e66 into huggingface:main Nov 13, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flux2 LoRA conversion maps final_layer.adaLN_modulation.1 to norm_out.linear without swapping shift and scale

3 participants