Repository navigation
Studio: keep the lora_adapter_metadata strength of a rewritten LoRA file - #17088
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if key.startswith("^"): | ||
| if re.match(key, stem): | ||
| return value |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if scale != 1.0: | ||
| renamed[stem + ".lora_B.weight"] = (b.to(torch.float32) * scale).to(b.dtype) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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_upnamesqkvWith 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.lora_A/lora_B(loads by path)lora_down/lora_up(rewritten)Same results on peft 0.18.0 and 0.21.2.
Changes
load_adapterreads the file'slora_adapter_metadataonly when the file is rewritten. It keeps thetransformer.keys, the same filtering diffusers applies under that prefix.normalize_lorafolds the resulting per-module scale intolora_B, the same way it already folds a tensor.alpha. Diffusers then runs the module at scale 1 and the delta is unchanged.alpha_pattern/lora_alphaover the real tensor rank, or oversqrt(rank)withuse_rslora..alphastill wins over the header.use_doraheader is left as before.UNSLOTH_DIFFUSION_LORA_REWRITE_METADATA=0.Tests
tests/test_diffusion_lora_files.pygains tests for:.alphaprecedence22 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:
After review,
alpha_pattern/rank_patternlookups use PEFT's ownget_pattern_keyexpression,(.*\.)?(key)$. That includes caret-prefixed keys.A second review pass found a related gap. PEFT scales the whole
lora_Boutput, so alora_B.biaskept 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.