Fix LoRA scaling: divide alpha by rank (#845)#986
Open
H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
Open
Fix LoRA scaling: divide alpha by rank (#845)#986H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
Conversation
The standard LoRA formulation (Hu et al. 2021) scales the low-rank
update by `alpha / rank`. `LoRaLayer.__call__` was multiplying the
update by raw `alpha` instead, making the effective scaling
rank-times too large for the documented defaults — for example
r=8, alpha=16 gave an effective scaling of 16 instead of the
intended 2.
This affects every adapter trained on the current LoRaLayer and
matches what every other PEFT implementation does, including the
HuggingFace `peft` library and the original Microsoft LoRA repo.
Changes:
* `LoRaLayer.__init__` now stores `self.rank` and `self.scaling
= alpha / rank` for use by both the forward pass and the merge
helper.
* `LoRaLayer.__call__` multiplies the LoRA update by
`self.scaling` instead of `self.alpha`.
* `replace_lora_with_linear` uses `layer.scaling` so the merged
weights match what the trained adapter applies during inference.
* Two regression tests in `test_trainer_utils.py` verify both the
stored attribute and the actual forward pass output.
### Backwards compatibility note
Adapters trained against the previous (broken) scaling will behave
8× weaker after this fix when r=8, alpha=16. Users who want the
old effective scaling can multiply their alpha by rank (e.g. set
alpha=128 to match the old r=8, alpha=16 behaviour). The
recommended action is to retrain with the documented defaults now
that they actually mean what the docs say.
Closes Blaizzy#845
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Apply the standard LoRA scaling factor
alpha / rank(Hu et al. 2021) inLoRaLayer.__call__andreplace_lora_with_linear. Previously the layer multiplied the LoRA update by rawalpha, making the effective scaling rank-times too large for the documented defaults — for exampler=8,alpha=16gave an effective scaling of 16 instead of the intended 2.This matches every other PEFT implementation, including HuggingFace
peftand the original Microsoft LoRA reference.Closes #845.
Changes
LoRaLayer.__init__now storesself.rankandself.scaling = alpha / rank.LoRaLayer.__call__multiplies the update byself.scaling.replace_lora_with_linearuseslayer.scalingso merged weights match what the trained adapter applies during inference.mlx_vlm/tests/test_trainer_utils.py:test_lora_layer_uses_alpha_over_rank_scaling— checks the stored attribute.test_lora_layer_forward_matches_alpha_over_rank— checks the actual forward-pass output against the expectedbase + (alpha/rank) * (x A B)formula with a non-zeroB.Backwards compatibility
r=8,alpha=16. Users who want the old effective scaling can multiply their alpha by rank — e.g. set--lora-alpha 128to recover the oldr=8, alpha=16behaviour. The recommended action is to retrain with the documented defaults now that they actually mean what the docs say.If maintainers prefer to gate this behind a flag for one release I am happy to do that — let me know.
Tests