Skip to content

Support mixed-rank LoRA adapter metadata - #3004

Open
bghira wants to merge 1 commit into
mainfrom
agent/mixed-rank-lora-metadata
Open

Support mixed-rank LoRA adapter metadata#3004
bghira wants to merge 1 commit into
mainfrom
agent/mixed-rank-lora-metadata

Conversation

@bghira

@bghira bghira commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Summary

Adds mixed-rank LoRA metadata handling so init_lora and validation adapter loading can preserve per-layer rank and alpha values instead of forcing everything through the global rank.

Details

  • Infers rank_pattern and alpha_pattern from LoRA checkpoint tensors during adapter initialization.
  • Synthesizes alpha=rank for mixed-rank checkpoints that do not carry explicit alpha tensors, while preserving explicit alpha tensors when present.
  • Routes vendored pipeline LoRA loading through SimpleTuner's get_peft_kwargs wrapper so layerwise rank/alpha metadata survives Diffusers loader behavior.

Validation

  • .venv/bin/python -m unittest -v -f tests.test_lora_format tests.test_lora_loading_regression

Inspect init_lora checkpoints before constructing PEFT configs so mixed-rank checkpoints can populate rank_pattern and alpha_pattern instead of being forced through the global rank.

Infer missing alpha values from per-module ranks when a checkpoint has mixed ranks but no explicit alpha tensors, and preserve explicit alpha tensors when present. Apply the same inference path for validation adapters and direct LoRA weight loading.

Route vendored pipelines through SimpleTuner's get_peft_kwargs wrapper so Diffusers loader changes do not drop layerwise rank or alpha metadata.
@bghira
bghira marked this pull request as ready for review August 6, 2026 19:44
@bghira
bghira requested a lite review from Copilot August 6, 2026 19:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for mixed-rank LoRA checkpoints by inferring per-module alpha/rank patterns and wiring those into PEFT/diffusers loading paths.

Changes:

  • Add utilities to detect mixed ranks, collect alpha/rank metadata, and synthesize missing alphas.
  • Update adapter/pipeline/model codepaths to apply inferred alpha/rank patterns during LoRA init/load.
  • Add regression + unit tests covering mixed-rank detection and alpha inference behavior.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_lora_loading_regression.py Adds regression test validating mixed-rank init_lora produces PEFT rank/alpha patterns.
tests/test_lora_format.py Adds unit tests for format detection and mixed-rank alpha/rank inference helpers.
simpletuner/helpers/training/lora_format.py Introduces rank/alpha collection + inference helpers and a get_peft_kwargs wrapper.
simpletuner/helpers/training/adapter.py Updates LoRA weight loading to set scaling via alpha and synthesize alphas for mixed ranks.
simpletuner/helpers/models/sd3/pipeline.py Switches to local get_peft_kwargs wrapper import.
simpletuner/helpers/models/longcat_video/pipeline.py Removes get_peft_kwargs from import list.
simpletuner/helpers/models/krea2/lora_pipeline.py Switches to local get_peft_kwargs wrapper import.
simpletuner/helpers/models/ideogram/pipeline.py Switches to local get_peft_kwargs wrapper import.
simpletuner/helpers/models/hidream/pipeline.py Switches to local get_peft_kwargs wrapper import.
simpletuner/helpers/models/flux/pipeline_controlnet.py Removes get_peft_kwargs from import list.
simpletuner/helpers/models/flux/pipeline.py Imports local get_peft_kwargs wrapper via lora_format.
simpletuner/helpers/models/common.py Uses init_lora checkpoint metadata to set PEFT rank/alpha patterns; normalizes inferred alphas.
simpletuner/helpers/models/chroma/pipeline.py Switches to local get_peft_kwargs wrapper import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 172 to +176
elif ".alpha" in k or ".lora_alpha" in k:
explicit_alpha_keys = True
kk = k.replace(".lora_alpha", "").replace(".alpha", "")
if kk in lora_layers:
lora_layers[kk].lora_alpha[loraKey] = v
_set_lora_alpha(lora_layers[kk], loraKey, v)
Comment on lines 155 to +160
for k, v in state_dict.items():
if "lora_A" in k:
kk = k.replace(".lora_A.weight", "")
if kk in lora_layers:
lora_layers[kk].lora_A[loraKey].weight.copy_(v)
loaded_ranks[kk] = int(v.shape[0])
Comment on lines +61 to +62
def _most_common(values: list[Any]) -> Any:
return Counter(values).most_common(1)[0][0]
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.

2 participants