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
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,191 +1,136 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
- **Trajectory controller with orbital memory**: disturbance rejection under task switch
|
| 137 |
-
- **Adaptive thresholds** (μ ± kσ): auto-calibrates across models and tasks
|
| 138 |
-
- **FSM adapter switching under noise**: measurably better performance and lower variance
|
| 139 |
-
|
| 140 |
-
## Computational overhead
|
| 141 |
-
|
| 142 |
-
The controller adds O(1) computation per step: one EMA update, one threshold comparison, one stack operation. No SVD, no matrix decomposition. Negligible relative to the training step.
|
| 143 |
-
|
| 144 |
-
## Control-theoretic framing
|
| 145 |
-
|
| 146 |
-
| Method | Control type | Rank dynamics |
|
| 147 |
-
|-------------------------|-----------------|-----------------------|
|
| 148 |
-
| Standard LoRA | None | rank = constant |
|
| 149 |
-
| AdaLoRA | Open-loop | rank = f(step) |
|
| 150 |
-
| **Unified-LoRA** | **Closed-loop** | rank = f(stress(t)) |
|
| 151 |
-
|
| 152 |
-
Unified-LoRA introduces orbit-aware rank transitions: each capacity increase is tracked and reversed only under confirmed stability, preventing premature compression and oscillatory collapse.
|
| 153 |
-
|
| 154 |
-
## Repository structure
|
| 155 |
-
|
| 156 |
-
```
|
| 157 |
-
controller.py # NestedLoRALinear + OrbitalController
|
| 158 |
-
experiments/
|
| 159 |
-
stress_test_task_switch.py # MRPC → SST-2 stress test (key result)
|
| 160 |
-
stable_task_test.py # Single-task parity test
|
| 161 |
-
docs/
|
| 162 |
-
experimental_results.md # Detailed results and rank traces
|
| 163 |
-
architecture.md # Nested orbital design
|
| 164 |
-
notebooks/ # Experiment notebooks
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
## Open questions
|
| 168 |
-
|
| 169 |
-
- Does nested orbital control scale to 7B+ models? (Tinker validation in progress)
|
| 170 |
-
- What is the minimum shock magnitude that triggers measurable benefit?
|
| 171 |
-
- Does adaptive LR control (black-box analog) show the same pattern on API platforms?
|
| 172 |
-
|
| 173 |
-
## Citation
|
| 174 |
-
|
| 175 |
-
```bibtex
|
| 176 |
-
@software{unified_lora_2025,
|
| 177 |
-
author = {Simona Vargiu},
|
| 178 |
-
title = {Unified-LoRA: Adaptive Fine-Tuning with Nested Orbital Rank Control},
|
| 179 |
-
year = {2025},
|
| 180 |
-
url = {https://github.com/Sva76/Unified-LoRa}
|
| 181 |
}
|
| 182 |
-
```
|
| 183 |
-
|
| 184 |
-
## Contact
|
| 185 |
-
|
| 186 |
-
**Simona Vargiu** (Independent Researcher)
|
| 187 |
-
For collaboration inquiries: simona.vargiu.malta@gmail.com
|
| 188 |
-
|
| 189 |
-
## License
|
| 190 |
-
|
| 191 |
-
Apache License 2.0 — see [LICENSE](LICENSE) for details.
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# Unified LoRA - MRPC Benchmark Example\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"This notebook demonstrates Unified LoRA on the GLUE MRPC task.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"**Expected results:**\n",
|
| 12 |
+
"- Baseline LoRA: F1 ~0.78-0.79\n",
|
| 13 |
+
"- Unified LoRA: F1 ~0.78-0.79\n"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"cell_type": "code",
|
| 18 |
+
"metadata": {},
|
| 19 |
+
"source": [
|
| 20 |
+
"!pip install -q transformers datasets peft evaluate scikit-learn accelerate"
|
| 21 |
+
],
|
| 22 |
+
"outputs": [],
|
| 23 |
+
"execution_count": null
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"source": [
|
| 29 |
+
"import os\n",
|
| 30 |
+
"os.environ['WANDB_DISABLED'] = 'true'\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"import torch\n",
|
| 33 |
+
"from datasets import load_dataset\n",
|
| 34 |
+
"from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments\n",
|
| 35 |
+
"from peft import LoraConfig, get_peft_model\n",
|
| 36 |
+
"from torch.utils.data import DataLoader\n",
|
| 37 |
+
"import evaluate\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"from controller import UnifiedController\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 42 |
+
"print(device)"
|
| 43 |
+
],
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"execution_count": null
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"cell_type": "code",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"source": [
|
| 51 |
+
"dataset = load_dataset('glue','mrpc')['train'].train_test_split(test_size=0.2, seed=42)\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"model_name = 'distilbert-base-uncased'\n",
|
| 54 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"def tokenize(ex):\n",
|
| 57 |
+
" return tokenizer(ex['sentence1'], ex['sentence2'], truncation=True, padding=True)\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"train = dataset['train'].map(tokenize, batched=True).rename_column('label','labels')\n",
|
| 60 |
+
"test = dataset['test'].map(tokenize, batched=True).rename_column('label','labels')\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"metric = evaluate.combine(['accuracy','f1'])\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"def compute_metrics(p):\n",
|
| 65 |
+
" logits, labels = p\n",
|
| 66 |
+
" preds = torch.argmax(torch.tensor(logits), axis=-1)\n",
|
| 67 |
+
" return metric.compute(predictions=preds, references=labels)"
|
| 68 |
+
],
|
| 69 |
+
"outputs": [],
|
| 70 |
+
"execution_count": null
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"metadata": {},
|
| 75 |
+
"source": [
|
| 76 |
+
"# BASELINE\n",
|
| 77 |
+
"model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)\n",
|
| 78 |
+
"model = get_peft_model(model, LoraConfig(r=16, lora_alpha=32, target_modules=['q_lin','v_lin']))\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"trainer = Trainer(\n",
|
| 81 |
+
" model=model,\n",
|
| 82 |
+
" train_dataset=train,\n",
|
| 83 |
+
" eval_dataset=test,\n",
|
| 84 |
+
" args=TrainingArguments(output_dir='./b', num_train_epochs=3, per_device_train_batch_size=16, fp16=True, report_to=None),\n",
|
| 85 |
+
" compute_metrics=compute_metrics\n",
|
| 86 |
+
")\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"trainer.train()\n",
|
| 89 |
+
"base = trainer.evaluate()"
|
| 90 |
+
],
|
| 91 |
+
"outputs": [],
|
| 92 |
+
"execution_count": null
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"cell_type": "code",
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"source": [
|
| 98 |
+
"# UNIFIED\n",
|
| 99 |
+
"ctrl = UnifiedController()\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)\n",
|
| 102 |
+
"model = get_peft_model(model, LoraConfig(r=16, lora_alpha=32, target_modules=['q_lin','v_lin']))\n",
|
| 103 |
+
"model.to(device)\n",
|
| 104 |
+
"\n",
|
| 105 |
+
"loader = DataLoader(train.remove_columns(['sentence1','sentence2','idx']), batch_size=16, shuffle=True)\n",
|
| 106 |
+
"opt = torch.optim.AdamW(model.parameters(), lr=3e-5)\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"model.train()\n",
|
| 109 |
+
"\n",
|
| 110 |
+
"for _ in range(3):\n",
|
| 111 |
+
" for batch in loader:\n",
|
| 112 |
+
" batch = {k:v.to(device) for k,v in batch.items() if k in ['input_ids','attention_mask','labels']}\n",
|
| 113 |
+
" out = model(**batch)\n",
|
| 114 |
+
" lr = ctrl.update(out.loss.item())\n",
|
| 115 |
+
" for g in opt.param_groups: g['lr'] = lr\n",
|
| 116 |
+
" out.loss.backward()\n",
|
| 117 |
+
" opt.step(); opt.zero_grad()\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"model.eval()\n",
|
| 120 |
+
"trainer = Trainer(model=model, eval_dataset=test, args=TrainingArguments(output_dir='./u', per_device_eval_batch_size=16, fp16=True, report_to=None), compute_metrics=compute_metrics)\n",
|
| 121 |
+
"uni = trainer.evaluate()"
|
| 122 |
+
],
|
| 123 |
+
"outputs": [],
|
| 124 |
+
"execution_count": null
|
| 125 |
+
}
|
| 126 |
+
],
|
| 127 |
+
"metadata": {
|
| 128 |
+
"kernelspec": {
|
| 129 |
+
"display_name": "Python 3",
|
| 130 |
+
"language": "python",
|
| 131 |
+
"name": "python3"
|
| 132 |
+
}
|
| 133 |
+
},
|
| 134 |
+
"nbformat": 4,
|
| 135 |
+
"nbformat_minor": 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|