Skip to content

feat(lora): support PEFT named-adapter LoRAs (e.g. Klein 9B)#9252

Open
Pfannkuchensack wants to merge 2 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/peft-named-adapter-lora-support
Open

feat(lora): support PEFT named-adapter LoRAs (e.g. Klein 9B)#9252
Pfannkuchensack wants to merge 2 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/peft-named-adapter-lora-support

Conversation

@Pfannkuchensack
Copy link
Copy Markdown
Collaborator

Summary

PEFT-format LoRAs with a named adapter encode the adapter name in the weight key (e.g. foo.lora_A.default.weight instead of foo.lora_A.weight). InvokeAI's format detection and conversion only matched the standard PEFT suffix, so these files were classified as Unknown and silently ignored.

Normalize named-adapter keys to the standard PEFT form in both ModelOnDisk.load_state_dict (detection pipeline) and LoRALoader._load_model (conversion pipeline). The pattern is LoRA-specific, so this is a no-op for non-LoRA state dicts. State dicts with multiple distinct adapter names are left untouched to avoid key collisions.

Also widen Flux2-Klein diffusers detection to accept keys without a transformer. or base_model.model. prefix — some trainers (Modelscope / MuseAI Klein 9B finetunes) save at the top level.

Related Issues / Discussions

#9021 and #9249

QA Instructions

Tested with a Klein 9B LoRA from Modelscope (klein-9b-turn2real_v1.5_ep9.safetensors) that uses the PEFT named-adapter format and no key prefix.

  1. Drop a PEFT named-adapter LoRA into your models directory (any base — SD1, SDXL, FLUX, FLUX.2 Klein, Z-Image, Qwen Image).
    • Confirmation that a file is affected: its keys look like …lora_A.default.weight / …lora_B.default.weight rather than …lora_A.weight / …lora_B.weight.
  2. Trigger a model scan / import.
  3. Before this PR: the file is classified as Unknown (no base, no type, no format).
    After this PR: the file is recognized with the correct base, type=LoRA, format=LyCORIS, and (for Flux2) the correct variant (Klein4B / Klein9B).
  4. Use the LoRA in a generation and verify it actually patches the model (no shape errors at load time, visible effect on the output).

Automated coverage:

  • tests/backend/patches/lora_conversions/test_peft_adapter_utils.py — 6 unit tests for the normalization helper (no-op when not needed, strips default, strips custom names, leaves multi-adapter dicts alone, preserves non-LoRA / integer keys).
  • All existing tests/backend/patches/lora_conversions/ tests (108) and tests/backend/model_manager/configs/ tests (41) still pass.

Merge Plan

Standard merge — no schema/migration 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)

PEFT-format LoRAs with a named adapter encode the adapter name in the
weight key (e.g. `foo.lora_A.default.weight` instead of `foo.lora_A.weight`).
InvokeAI's format detection and conversion only matched the standard PEFT
suffix, so these files were classified as Unknown and silently ignored.

Normalize named-adapter keys to the standard PEFT form in both
`ModelOnDisk.load_state_dict` (detection pipeline) and `LoRALoader._load_model`
(conversion pipeline). The pattern is LoRA-specific, so this is a no-op for
non-LoRA state dicts. State dicts with multiple distinct adapter names are
left untouched to avoid key collisions.

Also widen Flux2-Klein diffusers detection to accept keys without a
`transformer.` or `base_model.model.` prefix — some trainers (Modelscope /
MuseAI Klein 9B finetunes) save at the top level.
@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 May 30, 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