Text Classification
Transformers
lora
fine-tuning
adaptive
research
nested-lora
synaptic-plasticity
rank-adaptation
Instructions to use Simo76/Unified-LoRA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Simo76/Unified-LoRA with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="Simo76/Unified-LoRA")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Simo76/Unified-LoRA", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Revise README for adaptive LoRA findings and clarity
Browse filesUpdated README to reflect key findings and improvements in adaptive LoRA fine-tuning with FSM-driven adapter switching. Enhanced clarity on performance under noisy training conditions and revised project description.
README.md
CHANGED
|
@@ -1,54 +1,90 @@
|
|
| 1 |
-
|
| 2 |
# Unified-LoRA
|
| 3 |
|
| 4 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
-
##
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
| CoLA (MCC) | 0.474 Β± 0.001 | **0.478 Β± 0.011** | 0.477 Β± 0.021 | 7.0 |
|
| 26 |
-
| RTE (Acc) | **0.560 Β± 0.014** | 0.560 Β± 0.018 | 0.543 Β± 0.010 | 11.7 |
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
| Mode | Acc | F1 | Rank |
|
| 33 |
|------|-----|-----|------|
|
| 34 |
| r=8 | 0.876 Β± 0.008 | 0.913 Β± 0.004 | 8 |
|
| 35 |
| r=16 | 0.875 Β± 0.004 | 0.913 Β± 0.002 | 16 |
|
| 36 |
| r=32 | 0.883 Β± 0.012 | 0.918 Β± 0.008 | 32 |
|
| 37 |
-
| Adaptive | 0.870 Β± 0.014 | 0.911 Β± 0.008 | 10.8 |
|
| 38 |
-
|
| 39 |
-
**Finding:** Rank doesn't matter at 3B either. Gap between r=8 and r=32 is 0.5%.
|
| 40 |
-
|
| 41 |
-
### FSM Stability β Qwen2.5-3B + LoRA, MRPC, 3 seeds, A100
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|------|-----|--------|--------|
|
| 45 |
-
| Baseline (no protection) | **0.916 Β± 0.001** | | 330 |
|
| 46 |
-
| FSM Ο(t) | 0.907 Β± 0.005 | | 306 |
|
| 47 |
-
| Cosine scheduler | 0.898 Β± 0.001 | | 335 |
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
```
|
| 54 |
[250] Mode=1 Ο=0.333 (stable)
|
|
@@ -59,89 +95,42 @@ Two systems:
|
|
| 59 |
[700] Mode=1 Ο=0.333 (baseline restored)
|
| 60 |
```
|
| 61 |
|
| 62 |
-
**Finding:** The FSM mechanism works β it detects shock and recovers. But this was a single run with induced instability, not a multi-seed validation against alternatives.
|
| 63 |
-
|
| 64 |
-
## What was tested and didn't help
|
| 65 |
-
|
| 66 |
-
Tested rigorously and documented honestly:
|
| 67 |
-
|
| 68 |
-
- **Adaptive rank per-layer** (gradient EMA): rank adapts but doesn't improve results
|
| 69 |
-
- **Fluid dynamics metrics** (shock, vorticity, swirl): too conservative
|
| 70 |
-
- **Budget redistribution** across layers: winner-takes-all problem
|
| 71 |
-
- **Adaptive gradient clipping** via swirl: inconsistent
|
| 72 |
-
- **Vincolo integration** (StabilityController + rank): zero shock events on stable training
|
| 73 |
-
- **Predictive signals** (trend + acceleration): no improvement
|
| 74 |
-
- **FSM Ο(t) on natural training**: either no instability to handle, or instability doesn't hurt results
|
| 75 |
-
- **Stress testing** (high LR + label noise): training collapsed before FSM could act
|
| 76 |
-
|
| 77 |
## What was learned
|
| 78 |
|
| 79 |
-
1. **LoRA rank doesn't matter on classification tasks** from 67M to 3B
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
4. **Single-seed results are misleading.** Several configurations showed positive results on single seeds that disappeared on multi-seed evaluation.
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
#
|
|
|
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
layer0.q: 7.9 layer0.v: 8.8
|
| 96 |
-
layer1.q: 7.8 layer1.v: 7.9
|
| 97 |
-
layer2.q: 7.9 layer2.v: 8.5
|
| 98 |
-
layer3.q: 8.8 layer3.v: 11.3 β deep v_proj needs more
|
| 99 |
-
layer4.q: 10.3 layer4.v: 12.9
|
| 100 |
-
layer5.q: 7.6 layer5.v: 11.3
|
| 101 |
```
|
| 102 |
|
| 103 |
## Open questions
|
| 104 |
|
| 105 |
-
- Does
|
| 106 |
-
-
|
| 107 |
-
-
|
| 108 |
-
|
| 109 |
-
## Quick start
|
| 110 |
-
|
| 111 |
-
```python
|
| 112 |
-
from unified_lora import inject_lora, get_lora_modules, setup_trainable
|
| 113 |
-
|
| 114 |
-
model = inject_lora(model, target_modules=["q_proj", "v_proj"])
|
| 115 |
-
model = setup_trainable(model)
|
| 116 |
-
|
| 117 |
-
# In training loop:
|
| 118 |
-
for m in get_lora_modules(model):
|
| 119 |
-
m.update_rank() # adaptive rank (works mechanically, no performance benefit found)
|
| 120 |
-
```
|
| 121 |
-
|
| 122 |
-
## Reproduce
|
| 123 |
-
|
| 124 |
-
```bash
|
| 125 |
-
pip install transformers datasets evaluate accelerate scikit-learn bitsandbytes
|
| 126 |
-
|
| 127 |
-
# DistilBERT multi-seed validation (~20 min, T4)
|
| 128 |
-
python validation_complete.py
|
| 129 |
-
|
| 130 |
-
# Qwen 3B scale test (~40 min, A100)
|
| 131 |
-
python scale_test.py
|
| 132 |
-
|
| 133 |
-
# Qwen 3B stability test (~40 min, A100)
|
| 134 |
-
python stability_test.py
|
| 135 |
-
```
|
| 136 |
|
| 137 |
## Repository structure
|
| 138 |
|
| 139 |
```
|
| 140 |
-
unified_lora.py # Adaptive rank controller
|
| 141 |
-
benchmark.py #
|
| 142 |
-
validation_complete.py # Multi-seed
|
| 143 |
-
|
| 144 |
-
stability_test.py # FSM vs Baseline vs Cosine (A100)
|
| 145 |
controller.py # FSM Ο(t) controller
|
| 146 |
Archive/ # Earlier experimental results
|
| 147 |
docs/ # Additional documentation
|
|
@@ -153,7 +142,7 @@ notebooks/ # Experiment notebooks
|
|
| 153 |
```
|
| 154 |
@software{unified_lora_2025,
|
| 155 |
author = {Simona Vargiu},
|
| 156 |
-
title = {Unified-LoRA:
|
| 157 |
year = {2025},
|
| 158 |
url = {https://github.com/Sva76/Unified-LoRa}
|
| 159 |
}
|
|
|
|
|
|
|
| 1 |
# Unified-LoRA
|
| 2 |
|
| 3 |
+
**Adaptive LoRA fine-tuning with FSM-driven adapter switching.**
|
| 4 |
+
|
| 5 |
+
An exploration of adaptive LoRA fine-tuning that discovered a specific use case: under noisy training conditions, an FSM controller that switches between adapters of different rank based on training stress significantly outperforms fixed-rank LoRA.
|
| 6 |
+
|
| 7 |
+
## Key finding
|
| 8 |
+
|
| 9 |
+
Under noisy conditions (label noise), the FSM adapter switching controller provides measurably better performance and lower variance than any fixed-rank baseline.
|
| 10 |
+
|
| 11 |
+
**5 seeds, DistilBERT + LoRA, MRPC, 50% label noise:**
|
| 12 |
+
|
| 13 |
+
| Method | Mean F1 | Std | Per-seed F1 |
|
| 14 |
+
|--------|---------|-----|-------------|
|
| 15 |
+
| r=4 fixed | 0.410 | 0.323 | [0.62, 0.61, 0.04, 0.01, 0.78] |
|
| 16 |
+
| r=16 fixed | 0.439 | 0.234 | [0.73, 0.55, 0.31, 0.06, 0.55] |
|
| 17 |
+
| **FSM switching** | **0.622** | **0.174** | [0.66, 0.29, 0.70, 0.65, 0.81] |
|
| 18 |
+
| Random switching | 0.275 | 0.283 | [0.13, 0.08, 0.35, 0.01, 0.79] |
|
| 19 |
+
|
| 20 |
+
**Why this matters:**
|
| 21 |
+
- FSM has the highest mean F1 (+18 points over best fixed rank)
|
| 22 |
+
- FSM has the lowest variance (most robust across seeds)
|
| 23 |
+
- Random switching is worst β proving the intelligence of the switching matters, not just having multiple adapters
|
| 24 |
+
- Fixed ranks collapse on bad seeds (r4 β 0.007, r16 β 0.055); FSM never drops below 0.294
|
| 25 |
+
|
| 26 |
+
## How it works
|
| 27 |
+
|
| 28 |
+
The FSM controller monitors training loss and switches between three LoRA adapters (r=4, r=8, r=16) based on a stress signal Ο(t):
|
| 29 |
+
|
| 30 |
+
```
|
| 31 |
+
Ο(t) = f(loss_EMA, instability, progress)
|
| 32 |
+
|
| 33 |
+
Ο < ΞΈβ β Mode 0: use r=4 adapter (low stress, light capacity)
|
| 34 |
+
Ο < ΞΈβ β Mode 1: use r=8 adapter (moderate stress)
|
| 35 |
+
Ο β₯ ΞΈβ β Mode 2: use r=16 adapter (high stress, full capacity)
|
| 36 |
+
```
|
| 37 |
|
| 38 |
+
Under normal training, the controller stays in low-rank mode (efficient). When noise or instability hits, it switches to higher rank (resilient). When stress passes, it returns to low rank.
|
| 39 |
|
| 40 |
+
## Where it works and where it doesn't
|
| 41 |
|
| 42 |
+
### Works: noisy/unstable training
|
| 43 |
+
- Label noise, data corruption, adversarial batches
|
| 44 |
+
- The controller acts as a resilience mechanism
|
| 45 |
+
- Degrades less than fixed rank under stress
|
| 46 |
|
| 47 |
+
### Doesn't work: clean training
|
| 48 |
+
- On standard GLUE tasks without noise, r=8 β r=16 β r=32
|
| 49 |
+
- The rank choice doesn't matter, so the controller has no problem to solve
|
| 50 |
+
- Tested on DistilBERT (67M), TinyLlama (1.1B), Qwen2.5-3B β same conclusion
|
| 51 |
|
| 52 |
+
### Doesn't work: rank adaptation without switching
|
| 53 |
+
- Per-layer gradient EMA rank controller was tested extensively
|
| 54 |
+
- Multi-seed validation showed no benefit over fixed rank on clean data
|
| 55 |
+
- Higher variance than fixed-rank baselines
|
| 56 |
|
| 57 |
+
## Full experimental history
|
| 58 |
|
| 59 |
+
This project tested many approaches. In the interest of scientific honesty:
|
| 60 |
|
| 61 |
+
**Tested and didn't help on clean data:**
|
| 62 |
+
- Adaptive rank per-layer (gradient EMA) β no performance benefit
|
| 63 |
+
- Fluid dynamics metrics (shock, vorticity, swirl) β too conservative
|
| 64 |
+
- Budget redistribution across layers β winner-takes-all problem
|
| 65 |
+
- Adaptive gradient clipping β inconsistent
|
| 66 |
+
- Vincolo StabilityController integration β zero shock events on stable training
|
| 67 |
+
- FSM with LR control only (no adapter switching) β loses to cosine scheduler
|
| 68 |
|
| 69 |
+
**What works:**
|
| 70 |
+
- FSM with adapter switching under noisy conditions (this finding)
|
| 71 |
+
- FSM stress-recovery cycle validated on Tinker with Llama-3.2-1B
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
## Scale test results (clean data)
|
| 74 |
|
| 75 |
+
Qwen2.5-3B, 4-bit, MRPC, 3 seeds, A100:
|
| 76 |
|
| 77 |
| Mode | Acc | F1 | Rank |
|
| 78 |
|------|-----|-----|------|
|
| 79 |
| r=8 | 0.876 Β± 0.008 | 0.913 Β± 0.004 | 8 |
|
| 80 |
| r=16 | 0.875 Β± 0.004 | 0.913 Β± 0.002 | 16 |
|
| 81 |
| r=32 | 0.883 Β± 0.012 | 0.918 Β± 0.008 | 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
Rank doesn't matter at 3B on classification. Gap r=8 vs r=32: 0.5%.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
## FSM on Tinker (Llama-3.2-1B)
|
| 86 |
|
| 87 |
+
Demonstrated full stress β recovery cycle with manually induced shock:
|
| 88 |
|
| 89 |
```
|
| 90 |
[250] Mode=1 Ο=0.333 (stable)
|
|
|
|
| 95 |
[700] Mode=1 Ο=0.333 (baseline restored)
|
| 96 |
```
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
## What was learned
|
| 99 |
|
| 100 |
+
1. **LoRA rank doesn't matter on clean classification tasks** from 67M to 3B
|
| 101 |
+
2. **Under noise, adaptive switching beats fixed rank** β the FSM provides resilience
|
| 102 |
+
3. **Switching intelligence matters** β random switching is worst
|
| 103 |
+
4. **Single-seed results are misleading** β always use multi-seed
|
| 104 |
+
5. **The simplest baseline wins on clean data** β complexity only pays under stress
|
| 105 |
|
| 106 |
+
## Reproduce
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
```bash
|
| 109 |
+
pip install transformers datasets evaluate accelerate scikit-learn peft
|
| 110 |
|
| 111 |
+
# Clean data benchmark
|
| 112 |
+
python benchmark.py
|
| 113 |
|
| 114 |
+
# Multi-seed validation
|
| 115 |
+
python validation_complete.py
|
| 116 |
|
| 117 |
+
# Noisy training FSM test (the key finding)
|
| 118 |
+
python fsm_noise_test.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
```
|
| 120 |
|
| 121 |
## Open questions
|
| 122 |
|
| 123 |
+
- Does FSM adapter switching help at 7B+ scale under noise?
|
| 124 |
+
- What noise levels trigger the benefit? (tested at 50%, untested at 5-20%)
|
| 125 |
+
- Does it help on generation/instruction tasks with naturally noisy data?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
## Repository structure
|
| 128 |
|
| 129 |
```
|
| 130 |
+
unified_lora.py # Adaptive rank controller module
|
| 131 |
+
benchmark.py # Clean data benchmark
|
| 132 |
+
validation_complete.py # Multi-seed clean data validation
|
| 133 |
+
fsm_noise_test.py # FSM adapter switching under noise (key result)
|
|
|
|
| 134 |
controller.py # FSM Ο(t) controller
|
| 135 |
Archive/ # Earlier experimental results
|
| 136 |
docs/ # Additional documentation
|
|
|
|
| 142 |
```
|
| 143 |
@software{unified_lora_2025,
|
| 144 |
author = {Simona Vargiu},
|
| 145 |
+
title = {Unified-LoRA: Adaptive LoRA Fine-tuning with FSM Adapter Switching},
|
| 146 |
year = {2025},
|
| 147 |
url = {https://github.com/Sva76/Unified-LoRa}
|
| 148 |
}
|