Skip to content

fix(lora): correctly detect flattened layer keys in LayerPatcher#9295

Open
Pfannkuchensack wants to merge 1 commit into
invoke-ai:mainfrom
Pfannkuchensack:fix/flux2-klein-bbox-drag-drop-lora
Open

fix(lora): correctly detect flattened layer keys in LayerPatcher#9295
Pfannkuchensack wants to merge 1 commit into
invoke-ai:mainfrom
Pfannkuchensack:fix/flux2-klein-bbox-drag-drop-lora

Conversation

@Pfannkuchensack

Copy link
Copy Markdown
Collaborator

Summary

The patcher decided whether a patch's layer keys were flattened (legacy underscore-joined) or real dotted module paths by inspecting only the first key. For FLUX.2 Klein diffusers LoRAs whose first converted layer is a dotless top-level module (e.g. context_embedder), the whole patch was misclassified as flattened, causing assert "." not in layer_key to fail on subsequent dotted keys and crashing LoRA application.

Inspect all keys instead: a flattened key never contains a dot, so the patch is flattened only if no key contains one. Add a regression test covering the mixed dotless/dotted key ordering.

Related Issues / Discussions

https://discord.com/channels/1020123559063990373/1149510134058471514/1517709403858669708

QA Instructions

A FLUX.2 Klein diffusers-format LoRA whose first converted layer targets a dotless top-level module (e.g. transformer.context_embedder, transformer.x_embedder, transformer.proj_out) previously crashed on load with:
File ".../invokeai/backend/patches/layer_patcher.py", line 289, in _get_submodule
assert "." not in layer_key

To verify:

  1. Load a FLUX.2 Klein model and apply such a LoRA during denoising — it should now load and apply without the assertion error.
  2. Run the patcher unit tests:
    uv run --extra cuda --extra test python -m pytest tests/backend/patches/test_layer_patcher.py -q
    All tests pass (including the new test_apply_smart_model_patches_mixed_dotted_and_dotless_keys regression test, which fails on the old single-first-key heuristic).
  3. FLUX.1 LoRAs and other formats are unaffected (their keys already contained dots, so flattened detection is unchanged).

Merge Plan

Standard merge — isolated, low-risk change to LoRA patching logic. No DB/schema changes.

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)

The patcher decided whether a patch's layer keys were flattened (legacy
underscore-joined) or real dotted module paths by inspecting only the
first key. For FLUX.2 Klein diffusers LoRAs whose first converted layer
is a dotless top-level module (e.g. `context_embedder`), the whole patch
was misclassified as flattened, causing `assert "." not in layer_key` to
fail on subsequent dotted keys and crashing LoRA application.

Inspect all keys instead: a flattened key never contains a dot, so the
patch is flattened only if no key contains one. Add a regression test
covering the mixed dotless/dotted key ordering.
@github-actions github-actions Bot added python PRs that change python files backend PRs that change backend files python-tests PRs that change python tests labels Jun 20, 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 python-tests PRs that change python tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant