Upload 2 files
Browse files- README.md +104 -3
- pyproject.toml +29 -0
README.md
CHANGED
|
@@ -1,3 +1,104 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PursuitLoRA
|
| 2 |
+
|
| 3 |
+
LoRA-shaped adapters whose rank-one atoms are chosen by gradient pursuit, for Hugging Face causal LMs.
|
| 4 |
+
|
| 5 |
+
The update to every target matrix is a sum of rank-one atoms `α · u vᵀ`, as in LoRA. Unlike LoRA,
|
| 6 |
+
the rank of each matrix is not fixed in advance. One gradient probe before training gives every
|
| 7 |
+
matrix's negative gradient `−G`. Its leading singular pairs are the candidate atoms, and an atom's
|
| 8 |
+
singular value is its initial descent rate. Atoms are then chosen greedily across **all** matrices
|
| 9 |
+
under a parameter budget. The gradient therefore decides which matrices are adapted and with what
|
| 10 |
+
rank; many matrices get none. Coefficients start at zero, so the model is unchanged at step 0.
|
| 11 |
+
Coefficients and vectors are then trained with AdamW. A merged adapter has zero inference overhead.
|
| 12 |
+
|
| 13 |
+
Two selection rules are available:
|
| 14 |
+
|
| 15 |
+
- `sigma`: largest singular value first (default).
|
| 16 |
+
- `ols`: orthogonal least squares on the linearised loss, which avoids picking atoms whose effects overlap.
|
| 17 |
+
|
| 18 |
+
## Install
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
pip install . # from this directory
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Requires `torch>=2.1`, `transformers>=4.40`, `safetensors` and `huggingface_hub`.
|
| 25 |
+
|
| 26 |
+
## Usage
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
from transformers import AutoModelForCausalLM
|
| 30 |
+
from pursuitlora import PursuitLoRAConfig, get_pursuitlora_model, PursuitLoRAModel
|
| 31 |
+
|
| 32 |
+
model = get_pursuitlora_model(AutoModelForCausalLM.from_pretrained("gpt2"),
|
| 33 |
+
PursuitLoRAConfig(budget=9216))
|
| 34 |
+
model.init_atoms(train_dataloader, n_batches=4) # gradient probe -> atoms, chosen under the budget
|
| 35 |
+
model.print_trainable_parameters() # 8,452
|
| 36 |
+
print(model.describe()) # atoms per matrix
|
| 37 |
+
|
| 38 |
+
# train: AdamW, lr 3e-3, ~10 warm-up steps, weight_decay 0 (or pursuitlora.PursuitLoRATrainer)
|
| 39 |
+
|
| 40 |
+
model.save_pretrained("adapter") # adapter_config.json + adapter_model.safetensors
|
| 41 |
+
model = PursuitLoRAModel.from_pretrained(AutoModelForCausalLM.from_pretrained("gpt2"), "adapter")
|
| 42 |
+
merged = model.merge_and_unload()
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
`model.push_to_hub("user/repo")` uploads the adapter. `PursuitLoRATrainer` is a drop-in
|
| 46 |
+
`transformers.Trainer` whose checkpoints contain only the adapter.
|
| 47 |
+
|
| 48 |
+
| `PursuitLoRAConfig` | default | |
|
| 49 |
+
|---|---|---|
|
| 50 |
+
| `budget` | 9216 | maximum trainable parameters (one atom costs `d_in + d_out + 1`) |
|
| 51 |
+
| `rule` | `"sigma"` | `"sigma"` or `"ols"` |
|
| 52 |
+
| `target_modules` | `"all-linear"` | or name suffixes, e.g. `["q_proj", "v_proj"]` |
|
| 53 |
+
| `exclude_modules` | lm_head, embeddings | |
|
| 54 |
+
| `split_fused_qkv` | True | GPT-2 `c_attn` → q, k, v |
|
| 55 |
+
| `candidates_per_matrix` | 8 | singular pairs considered per matrix |
|
| 56 |
+
| `init_batches` | 4 | probe batches |
|
| 57 |
+
| `ols_batches` | 2 | probe batches whose activations are kept for `ols` |
|
| 58 |
+
| `alpha_scale` | 33.3 | coefficient scaling so that one learning rate fits all parameters |
|
| 59 |
+
|
| 60 |
+
## Results (GPT-2 124M → Bengali news, 300 steps, validation loss, lower is better)
|
| 61 |
+
|
| 62 |
+
Seed 0, identical batches for all methods. The baselines are the runs in the TensorPEFT
|
| 63 |
+
benchmark (`github.com/AngshulMajumdar/TensorPEFT`, `benchmarks/results.json`).
|
| 64 |
+
|
| 65 |
+
| budget | PursuitLoRA `sigma` | PursuitLoRA `ols` | best baseline | LoRA |
|
| 66 |
+
|---|---:|---:|---|---:|
|
| 67 |
+
| ≈1.5k | 1.4842 (1,537) | **1.4812** (1,537) | prompt tuning, 1 token: 1.4928 (768) | – |
|
| 68 |
+
| ≈9k | 1.3693 (8,452) | **1.3684** (8,452) | prompt tuning, 12 tokens: 1.3704 (9,216) | r=1, q,v, last 3 layers: 1.4299 |
|
| 69 |
+
| ≈37k | 1.2732 (36,112) | 1.2633 (35,345) | **LayerNorm tuning: 1.2530** (38,400) | r=1, q,v: 1.3181 |
|
| 70 |
+
| ≈55k | **1.2536** (53,783) | 1.2594 (54,556) | (IA)³, k,v,MLP: 1.2894 (55,296) | – |
|
| 71 |
+
|
| 72 |
+
Parameter counts are in brackets. Base model: 1.7079. Learning rates: PursuitLoRA 3e-3,
|
| 73 |
+
LoRA 1e-3, prompt tuning 3e-2, LayerNorm 1e-3, (IA)³ 3e-3.
|
| 74 |
+
|
| 75 |
+
- PursuitLoRA beats every baseline at ≈1.5k and ≈55k.
|
| 76 |
+
- It is level with prompt tuning at ≈9k (a difference of 0.001–0.002 is within seed noise).
|
| 77 |
+
- LayerNorm tuning is better at ≈37k.
|
| 78 |
+
- It beats LoRA at every budget where LoRA fits.
|
| 79 |
+
|
| 80 |
+
The PursuitLoRA results come from a single seed. The default rule is `sigma`: the two rules are
|
| 81 |
+
within 0.01 of each other, and `ols` keeps per-token activations of every target matrix for
|
| 82 |
+
`ols_batches` batches (peak process memory about 5.5 GB for GPT-2 with 2 × 8 × 128 tokens).
|
| 83 |
+
|
| 84 |
+
## Layout
|
| 85 |
+
|
| 86 |
+
```
|
| 87 |
+
src/pursuitlora/ config, layer discovery, model (probe, selection, save/load, merge), Trainer
|
| 88 |
+
tests/ pytest: GPT-2, Llama, Qwen2, Mistral, Phi, Gemma (CPU, tiny random models)
|
| 89 |
+
examples/ prepare_bengali_data.py, reproduce_gpt2_bengali.py, train_with_trainer.py
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
Reproduce a row:
|
| 93 |
+
|
| 94 |
+
```
|
| 95 |
+
python examples/prepare_bengali_data.py --out data
|
| 96 |
+
python examples/reproduce_gpt2_bengali.py --model gpt2 --data data --budget 9216 --rule sigma
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
Tests: `pip install .[test] && pytest tests`
|
| 100 |
+
|
| 101 |
+
## Notes
|
| 102 |
+
|
| 103 |
+
- Probe memory is about 2× the size of the targeted weights, briefly. For large models, restrict `target_modules`.
|
| 104 |
+
- Quantised (bitsandbytes) bases are not supported in this version.
|
pyproject.toml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["setuptools>=64", "wheel"]
|
| 3 |
+
build-backend = "setuptools.build_meta"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "pursuitlora"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
description = "LoRA-shaped adapters whose rank-one atoms are chosen across all matrices by gradient pursuit"
|
| 9 |
+
readme = "README.md"
|
| 10 |
+
authors = [{ name = "Angshul Majumdar", email = "angshul@iiitd.ac.in" }]
|
| 11 |
+
requires-python = ">=3.9"
|
| 12 |
+
dependencies = [
|
| 13 |
+
"torch>=2.1",
|
| 14 |
+
"transformers>=4.40",
|
| 15 |
+
"safetensors>=0.4",
|
| 16 |
+
"huggingface_hub>=0.20",
|
| 17 |
+
]
|
| 18 |
+
keywords = ["peft", "fine-tuning", "lora", "gradient-pursuit", "transformers", "huggingface"]
|
| 19 |
+
classifiers = [
|
| 20 |
+
"Programming Language :: Python :: 3",
|
| 21 |
+
"Intended Audience :: Science/Research",
|
| 22 |
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
[project.optional-dependencies]
|
| 26 |
+
test = ["pytest", "accelerate"]
|
| 27 |
+
|
| 28 |
+
[tool.setuptools.packages.find]
|
| 29 |
+
where = ["src"]
|