peft-rs
Comprehensive PEFT (Parameter-Efficient Fine-Tuning) adapter library for Rust.
Overview
peft-rs provides modular implementations of various PEFT methods for fine-tuning large language models efficiently:
- LoRA (Low-Rank Adaptation) - Decomposes weight updates into low-rank matrices
- DoRA (Weight-Decomposed Low-Rank Adaptation) - Magnitude and direction decomposition
- AdaLoRA (Adaptive Low-Rank Adaptation) - Dynamic rank allocation with SVD parameterization
- IAΒ³ (Infused Adapter by Inhibiting and Amplifying) - Learned rescaling vectors
- LoHa (Low-Rank Hadamard Product) - Hadamard product of two low-rank matrices
- LoKr (Low-Rank Kronecker Product) - Kronecker product decomposition
- OFT (Orthogonal Fine-Tuning) - Block-diagonal orthogonal transformations
- BOFT (Butterfly Orthogonal Fine-Tuning) - Butterfly factorization for efficient orthogonal transforms
- VeRA (Vector-based Random Matrix Adaptation) - Ultra-efficient with frozen random matrices
- Prefix Tuning - Prepends trainable vectors to attention keys/values
- Prompt Tuning - Adds learnable soft prompt embeddings
Features
- π¦ Pure Rust implementation using candle
- π Modular adapter design with common traits
- π¦ Easy integration with existing models
- β‘ Optional CUDA acceleration
- π Minimal memory overhead
Installation
Add to your Cargo.toml:
[]
= "0.1"
For CUDA support:
[]
= { = "0.1", = ["cuda"] }
Quick Start
LoRA Example
use ;
use ;
Prompt Tuning Example
use ;
use ;
Saving and Loading Adapters
Adapters can be saved and loaded using safetensors format:
use ;
// Save adapter weights and config
save_adapter_weights?;
save_adapter_config?;
// Load adapter weights and config
let loaded_config = load_adapter_config?;
let mut loaded_layer = new_with_zeros?;
load_adapter_weights?;
Multi-Adapter Support
Manage multiple adapters and switch between them at runtime:
use ;
// Create registry
let mut registry = new;
// Register multiple adapters
let task1_adapter = new_with_zeros?;
let task2_adapter = new_with_zeros?;
registry.register_adapter?;
registry.register_adapter?;
// Switch between adapters
registry.set_active_adapter?;
let output1 = registry.forward?;
registry.set_active_adapter?;
let output2 = registry.forward?;
// Access specific adapters
let task1 = registry.get_adapter?;
Architecture
All adapters implement common traits for consistent usage:
Comparison with Python PEFT
| Feature | peft-rs | HuggingFace PEFT |
|---|---|---|
| LoRA | β | β |
| DoRA | β | β |
| AdaLoRA | β | β |
| IAΒ³ | β | β |
| LoHa | β | β |
| LoKr | β | β |
| OFT | β | β |
| BOFT | β | β |
| VeRA | β | β |
| Prefix Tuning | β | β |
| Prompt Tuning | β | β |
| Weight merging | β | β |
| Weight saving/loading | β | β |
| Multi-adapter support | β | β |
| CUDA support | β | β |
| No Python runtime | β | β |
Contributing
Contributions welcome! See docs/GAP_ANALYSIS.md for planned features and docs/TASK_TRACKER.md for implementation status.
License
MIT Licensed (LICENSE-MIT