Skip to content

Studio: keep the lora_adapter_metadata strength of a rewritten LoRA file - #17088

Merged
danielhanchen merged 5 commits into
mainfrom
studio-lora-nov7-meta
Nov 8, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-lora-nov7-meta

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #16711. For Anima, Krea 2 and MOSS-Image, Studio normalises LoRA files before loading them. A file that needs no rewrite loads by path, and diffusers reads lora_adapter_metadata (lora_alpha, r, alpha_pattern, use_rslora) from its safetensors header.

A file that does need a rewrite loads from a tensor dict instead. Rewrites cover:

  • lora_down / lora_up names
  • MOSS fused qkv
  • Kohya underscore keys
  • a LoRA + LoKR mix

With a dict, diffusers never sees the header, so every module ran at scale 1 instead of lora_alpha / r.

Repro with real PEFT and a tiny real MOSS transformer: both files hold identical tensors and metadata r = 2, lora_alpha = 8.

File keys main this PR
lora_A / lora_B (loads by path) rel. error 1.6e-08 vs the alpha 8 / r 2 delta 1.6e-08 (same path, untouched)
lora_down / lora_up (rewritten) rel. error 0.75 (scale 1.0) 1.6e-08

Same results on peft 0.18.0 and 0.21.2.

Changes

  • load_adapter reads the file's lora_adapter_metadata only when the file is rewritten. It keeps the transformer. keys, the same filtering diffusers applies under that prefix.
  • normalize_lora folds the resulting per-module scale into lora_B, the same way it already folds a tensor .alpha. Diffusers then runs the module at scale 1 and the delta is unchanged.
  • The per-module scale is alpha_pattern / lora_alpha over the real tensor rank, or over sqrt(rank) with use_rslora.
  • A tensor .alpha still wins over the header.
  • The scale is folded before the MOSS qkv split, so patterns naming the fused layer still match.
  • A use_dora header is left as before.
  • A file without metadata, or one that loads by path, takes exactly the same route as before.
  • Kill switch: UNSLOTH_DIFFUSION_LORA_REWRITE_METADATA=0.

Tests

tests/test_diffusion_lora_files.py gains tests for:

  • the real-MOSS strength check, native and renamed
  • the kill switch
  • pattern / rslora / DoRA / .alpha precedence
  • the fused-qkv split
  • header parsing

22 passed, 3 GPU-only tests skipped, on peft 0.18.0, 0.19.1, 0.20.0 and 0.21.2.

GPU (Kaggle T4, torch 2.10 / peft 0.19.1 / diffusers 0.42.1)

Same MOSS check on CUDA, base and head one per T4, 3 reps each. Relative error against the alpha 8 / r 2 delta:

File keys main this PR
native 0.0 0.0
renamed 0.75 0.0

After review, alpha_pattern / rank_pattern lookups use PEFT's own get_pattern_key expression, (.*\.)?(key)$. That includes caret-prefixed keys.

A second review pass found a related gap. PEFT scales the whole lora_B output, so a lora_B.bias kept by the rewrite now carries the folded scale too. This covers both sources: a tensor .alpha, which had the same gap on main, and the metadata. A new test checks both.

Anima / Krea 2 / MOSS-Image files whose keys need rewriting (down/up names, fused
qkv, LoKR mix) load from a dict, so diffusers never saw the lora_adapter_metadata
header and ran every module at scale 1 instead of lora_alpha / r. Fold the
header's scale into lora_B the same way a tensor .alpha already is.
UNSLOTH_DIFFUSION_LORA_REWRITE_METADATA=0 restores scale 1.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T05:08:44.903416Z a1c4bee Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb242e5957

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +70 to +72
if key.startswith("^"):
if re.match(key, stem):
return value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve PEFT's end anchor for caret-prefixed alpha patterns

When a rewritten adapter has a caret-prefixed alpha_pattern, this branch accepts prefix matches, whereas PEFT's get_pattern_key still appends $ to these patterns. For example, ^layers\.0\.proj\.q incorrectly matches layers.0.proj.qkv here, although PEFT would use the default alpha for that module. For a fused MOSS qkv adapter, the incorrect scale is then folded into all three split projections, silently changing its strength. Use PEFT's full matching expression for caret-prefixed keys too.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in c910d6b: the lookup now uses PEFT's own expression, (.*.)?(key)$, for every key, so a caret-prefixed pattern is anchored at both ends. A new test checks layers.0.proj.q and layers.0.proj.qkv against peft.utils.other.get_pattern_key.

…y does

A caret-prefixed alpha_pattern was matched as a prefix, so ^layers.0.proj.q also
named layers.0.proj.qkv; PEFT anchors every key at the end.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c910d6be9b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 145 to 146
if scale != 1.0:
renamed[stem + ".lora_B.weight"] = (b.to(torch.float32) * scale).to(b.dtype)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fold metadata scaling into the LoRA B bias as well

For a rewritten adapter containing a lora_B.bias tensor (lora_up.bias before normalization), this scales only the B weight and leaves its bias unchanged. PEFT applies adapter scaling to the entire lora_B(lora_A(x)) output, including the bias, and diffusers infers lora_bias=True from the retained bias tensor when loading the dictionary. Consequently, metadata with lora_alpha=8 and rank 2 produces 4 * B(A(x)) + bias instead of 4 * (B(A(x)) + bias), silently changing the adapter's output. Apply the same folded scale to the corresponding B bias.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in a1c4bee: the folded scale (tensor .alpha or metadata) now multiplies lora_B.bias as well as lora_B.weight, matching PEFT scaling the whole lora_B output. The tensor .alpha path had the same gap and is covered by the same change; a new test checks both sources.

PEFT applies an adapter's scaling to the whole lora_B output, so a bias kept by
the rewrite has to carry the folded alpha / metadata scale too.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: a1c4beef58

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant