Skip to content

fix(adapter): honor LoRA and EoRA scaling (#3975) - #3990

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-3975
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-3975

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3975

TL;DR

Saved LoRA adapters now honor lora_alpha, use_rslora, rank_pattern, and alpha_pattern. EoRA exports mirror dynamic ranks into matching alphas, and EoRA covariance accumulation now gives every calibration token uniform weight.

1. The problem

Lora.apply composed x @ lora_A @ lora_B without any PEFT scale. Lora.post_init resolved dynamic rank but never alpha or rsLoRA. _eora_save wrote a rank_pattern without the corresponding alpha_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.

Old mechanism Old location Failure
Unscaled LoRA update gptqmodel/adapter/adapter.py:203-227 lora_alpha and use_rslora ignored
Rank-only pattern resolution gptqmodel/adapter/adapter.py:229-342 alpha_pattern ignored
Rank pattern without alpha pattern gptqmodel/models/writer.py:1565-1578 Dynamic rank 4 in a rank-16 EoRA export applied at scale 4
Fixed-count normalization plus running rescale gptqmodel/eora/eora.py:28-56, gptqmodel/looper/eora_processor.py:170-227 Earlier batches decayed exponentially
flowchart 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:#c00
Loading

2. The fix

The scale is folded once into the private contiguous lora_B clone in Lora.post_init (gptqmodel/adapter/adapter.py:328-357). This avoids a per-forward multiply and leaves the immutable cached tensors unscaled. _eora_save now writes alpha_pattern = dict(rank_pattern) (gptqmodel/models/writer.py:1565-1579), keeping EoRA's own residual at unit scale.

EoRA now accumulates raw XᵀX plus token counts per device (gptqmodel/eora/eora.py:26-51, gptqmodel/looper/eora_processor.py:167-203). _finalize_eigen_scaling_matrix sums 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:#c00
Loading

The 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.

Finding Regression
Global alpha and rsLoRA tests/test_adapter_scaling.py::test_loaded_lora_uses_global_alpha_scale
Per-module rank/alpha tests/test_adapter_scaling.py::test_loaded_lora_uses_per_module_peft_scale
Dynamic-rank EoRA round trip tests/test_adapter_scaling.py::test_eora_dynamic_rank_export_round_trips_at_unit_scale
Uniform covariance tests/test_eora_merge.py::test_eora_covariance_weights_every_batch_uniformly

Final regression files on unmodified origin/main (00163850):

CUDA_VISIBLE_DEVICES=GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_adapter_scaling.py tests/test_eora_merge.py -q
7 failed, 1 skipped

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:

CUDA_VISIBLE_DEVICES=GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e HIP_VISIBLE_DEVICES=1 PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_adapter_scaling.py tests/test_eora_merge.py tests/test_adapter_config.py tests/test_adapter_case_sensitive.py -q
23 passed, 1 skipped

After on the pinned AMD R9700:

HIP_VISIBLE_DEVICES=1 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_adapter_scaling.py tests/test_eora_merge.py tests/test_adapter_config.py tests/test_adapter_case_sensitive.py -q
23 passed, 1 skipped

Related regression files on both environments:

pytest tests/test_eora.py tests/test_eora_weight_lifecycle.py tests/test_processor_continuation.py tests/kernels/test_lora_cuda_autograd.py -q
15 passed, 1 skipped

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-7 and 4.204e-7; covariance max absolute error was 6.217e-8 and relative error was 5.340e-8.

4. Validation

  • ruff check on every changed Python file: passed.
  • git diff --check: passed.
  • Required repository-wide ruff check gptqmodel docs/eora tests setup.py: passed after merging origin/main (d18e2ba1); GitHub ruff-check workflows also pass.

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.

[BUG] LoRA/EoRA numerics: lora_alpha/rslora scaling ignored, dynamic-rank EoRA export over-scales in PEFT, EoRA covariance geometrically weighted

1 participant