Repository navigation
fix(adapter): honor LoRA and EoRA scaling (#3975) - #3990
Merged
Merged
Conversation
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.
Fixes #3975
TL;DR
Saved LoRA adapters now honor
lora_alpha,use_rslora,rank_pattern, andalpha_pattern. EoRA exports mirror dynamic ranks into matching alphas, and EoRA covariance accumulation now gives every calibration token uniform weight.1. The problem
Lora.applycomposedx @ lora_A @ lora_Bwithout any PEFT scale.Lora.post_initresolved dynamic rank but never alpha or rsLoRA._eora_savewrote arank_patternwithout the correspondingalpha_pattern, so PEFT divided the global alpha by a smaller module rank. EoRA calibration divided each batch by the fixed batch count and then repeatedly rescaled the running total, geometrically discounting earlier batches.gptqmodel/adapter/adapter.py:203-227lora_alphaanduse_rsloraignoredgptqmodel/adapter/adapter.py:229-342alpha_patternignoredgptqmodel/models/writer.py:1565-1578gptqmodel/eora/eora.py:28-56,gptqmodel/looper/eora_processor.py:170-227flowchart TD A["Lora.post_init"] --> B["_extract_lora clones lora_B"] B --> C["dynamic_rank_override"] C --> D["dynamic_alpha_override"] D --> E{"lora_cfg.use_rslora"} E -- yes --> F["lora_scale = alpha / sqrt(rank)"] E -- no --> G["lora_scale = alpha / rank"] F --> H["lora_B.mul_(lora_scale) once"] G --> H H --> I["Lora.apply"] X["Missing A/B or invalid rank/alpha"] --> Y["fail closed"] style X fill:#fee,stroke:#c00 style Y fill:#fee,stroke:#c002. The fix
The scale is folded once into the private contiguous
lora_Bclone inLora.post_init(gptqmodel/adapter/adapter.py:328-357). This avoids a per-forward multiply and leaves the immutable cached tensors unscaled._eora_savenow writesalpha_pattern = dict(rank_pattern)(gptqmodel/models/writer.py:1565-1579), keeping EoRA's own residual at unit scale.EoRA now accumulates raw
XᵀXplus token counts per device (gptqmodel/eora/eora.py:26-51,gptqmodel/looper/eora_processor.py:167-203)._finalize_eigen_scaling_matrixsums device totals and divides once by total tokens (gptqmodel/looper/eora_processor.py:205-237). The obsolete scaled segment merge path was removed.flowchart TD A["pre_process_fwd_hook"] --> B["eora_process_input"] B --> C["raw sum of X^T X"] B --> D["token_count"] C --> E["_accumulate_eora_contribution"] D --> E E --> F["per-device raw total"] F --> G["_finalize_eigen_scaling_matrix"] G --> H["divide once by total token_count"] H --> I["eora_compute_lora"] X["No activation tokens"] --> Y["RuntimeError"] style X fill:#fee,stroke:#c00 style Y fill:#fee,stroke:#c00The committed reference page is docs/lora-eora-scaling.md, linked from
README.md.3. Tests and measured results
The tests use explicit Torch arithmetic over the original A/B tensors and an independent double-precision uniform covariance. They do not reuse loader or processor intermediates as the expected value.
tests/test_adapter_scaling.py::test_loaded_lora_uses_global_alpha_scaletests/test_adapter_scaling.py::test_loaded_lora_uses_per_module_peft_scaletests/test_adapter_scaling.py::test_eora_dynamic_rank_export_round_trips_at_unit_scaletests/test_eora_merge.py::test_eora_covariance_weights_every_batch_uniformlyFinal regression files on unmodified
origin/main(00163850):Measured before: global-alpha standard/rsLoRA errors were 25.2 and 86.635 absolute; patterned adapter errors were 0.705357 and 1.410714 absolute; covariance differed from the uniform oracle by 1.241275 absolute and 38.7263 relative.
After on the pinned NVIDIA 4090:
After on the pinned AMD R9700:
Related regression files on both environments:
The one skip is the optional PEFT cross-check because PEFT is not installed in these environments.
Measured after: patterned standard/rsLoRA max absolute errors were
2.102e-7and4.204e-7; covariance max absolute error was6.217e-8and relative error was5.340e-8.4. Validation
ruff checkon every changed Python file: passed.git diff --check: passed.ruff check gptqmodel docs/eora tests setup.py: passed after mergingorigin/main(d18e2ba1); GitHubruff-checkworkflows also pass.