Skip to main content

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 original LoRA-Pro DeepSpeed integration and is compatible with both plain PyTorch modules and PEFT LoRA layers.

Features

  • Detects LoRA pairs (lora_A, lora_B) on vanilla modules or PEFT ModuleDict layouts automatically.
  • Applies the exact LoRA-Pro closed-form gradient correction before taking an AdamW step.
  • Matches the upstream DeepSpeed implementation bit-for-bit in the provided regression tests.
  • Ships with pytest-based guards, including an integration test that cross-checks against the DeepSpeed implementation on PEFT adapters.

Installation

The package follows a standard src/ layout and publishes Hatch metadata, so you can install it via pip:

pip install lora-pro-adamw

For development (tests, linting, etc.), install the optional extras:

pip install lora-pro-adamw[dev]

Or clone the repository and use Hatch directly:

git clone https://github.com/bamps53/lora-pro-adamw.git
cd lora-pro-adamw
hatch env create
hatch run pytest

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

Running Tests

The project uses pytest. After installing the dev extras:

uv run pytest

This invokes two suites:

  1. A regression test that compares LoRAProAdamW against the DeepSpeed implementation on a minimal toy layer.
  2. An integration test that validates behaviour on an actual PEFT LoRA adapter.

Acknowledgements & Citation

This package reuses the LoRA-Pro mathematics introduced in:

  • Original implementation: 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lora_pro_adamw-0.1.0.tar.gz (77.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lora_pro_adamw-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file lora_pro_adamw-0.1.0.tar.gz.

File metadata

  • Download URL: lora_pro_adamw-0.1.0.tar.gz
  • Upload date:
  • Size: 77.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for lora_pro_adamw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ee987d8f67d0e05a4312ce3b2979d879ff9f8e23e9b64bf7c74b201e542487f
MD5 70f25137e91f3d10014d4c67374d33ea
BLAKE2b-256 5f119ed499b7ebc6cdd1afe16a546b15626d2a172b44ba7c7315d74590140f30

See more details on using hashes here.

File details

Details for the file lora_pro_adamw-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lora_pro_adamw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34aeb74024e621b4baf9bb150a29cad23edc2a3fe752d10afc87a01cca5ce6dc
MD5 c86628c85962967568d7fd87a49057ff
BLAKE2b-256 8efea45878e243af9ff20b877ce1640beb5d3324e68aaa7eedd4f0fd7eb2affc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page