LoRA-Pro gradient-corrected AdamW optimizer for PyTorch and PEFT
Project description
LoRA-Pro AdamW
A lightweight Python package that ships a standalone implementation of the LoRA-Pro gradient correction for low-rank adapters together with an AdamW optimizer wrapper. The implementation is a clean-room port of the official LoRA-Pro reference optimizer and is compatible with both plain PyTorch modules and PEFT LoRA layers.
Features
- Detects LoRA pairs (
lora_A,lora_B) on vanilla modules or PEFTModuleDictlayouts automatically. - Applies the exact LoRA-Pro closed-form gradient correction before taking an AdamW step.
- Supports both the original "full" mode (mathematically identical to the official LoRA-Pro release) and the new "efficient" mode with a vectorized Sylvester solver.
- Matches the upstream LoRA-Pro reference implementation bit-for-bit in the provided regression tests.
- Ships with pytest-based guards, including an integration test that cross-checks against the official reference implementation on PEFT adapters.
Installation
pip install lora-pro-adamw
Quick Start
import torch
from peft import LoraConfig, TaskType, get_peft_model
from lora_pro import LoRAProAdamW
class Toy(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(32, 24, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear(x)
config = LoraConfig(
task_type=TaskType.FEATURE_EXTRACTION,
target_modules=["linear"],
r=8,
lora_alpha=16,
lora_dropout=0.0,
bias="none",
)
model = get_peft_model(Toy(), config)
# LoRAProAdamW wraps AdamW and performs the LoRA-Pro gradient correction internally.
# Unlike a vanilla torch.optim optimizer, instantiate it with the *model*, not parameter groups.
optimizer = LoRAProAdamW(model, lr=1e-3, weight_decay=0.0)
inputs = torch.randn(4, 32)
target = torch.randn(4, 24)
loss = torch.nn.functional.mse_loss(model(inputs), target)
loss.backward()
optimizer.step()
optimizer.zero_grad()
The optimizer automatically discovers LoRA parameters on the PEFT-wrapped modules and applies the LoRA-Pro gradient correction before delegating to AdamW. By default, the optimizer runs in "full" mode, reproducing the official LoRA-Pro implementation exactly. To trade a tiny amount of numerical difference for significantly lower solver cost, switch to the efficient solver mode:
optimizer = LoRAProAdamW(
model,
lr=1e-3,
weight_decay=0.0,
lorapro_mode="efficient", # use vec-trick Sylvester solver
efficient_solver="vec", # or "eig" for the eigendecomposition variant
)
State dicts include the chosen mode and solver so checkpoints remain compatible with your selected configuration.
Acknowledgements & Citation
This package reuses the LoRA-Pro mathematics introduced in the official reference implementation:
- Reference optimizer: mrflogs/LoRA-Pro
- Paper: Zhengbo Wang, Jian Liang, Ran He, Zilei Wang, Tieniu Tan. LoRA-Pro: Are Low-Rank Adapters Properly Optimized? ICLR 2025. [OpenReview]
Please cite the paper above when using this optimizer in your research.
License
Released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lora_pro_adamw-0.2.1.tar.gz.
File metadata
- Download URL: lora_pro_adamw-0.2.1.tar.gz
- Upload date:
- Size: 81.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47f28dc6de25b53dabaf954729cc0863c3afe2f137d6d37cac0866454ccfd190
|
|
| MD5 |
42640ab285f47b314697d928220c1321
|
|
| BLAKE2b-256 |
37f2ecf9c0101453683f91a48c203661006dbb97705a533e9229dca27774a9b6
|
File details
Details for the file lora_pro_adamw-0.2.1-py3-none-any.whl.
File metadata
- Download URL: lora_pro_adamw-0.2.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d01f8f8121de117b7666cb3fe04d0327962b87ab3ba7a66364ed95a623b5437a
|
|
| MD5 |
646ad3144abd26e7c8d892921dff5e18
|
|
| BLAKE2b-256 |
18a92ab1b33dfd33bf24e85e0d6eed6cc6f1a4fc8a1a5cef71eded2e86859cbd
|