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", dtype="auto") - Notebooks
- Google Colab
- Kaggle
File size: 4,947 Bytes
c567b69 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad 91d7191 2d544ad c567b69 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Orbital LoRA - MRPC Benchmark Example\n",
"\n",
"**Expected:** performance parity with baseline + adaptive behavior\n"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"!pip install -q transformers datasets evaluate scikit-learn accelerate"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import torch\n",
"from datasets import load_dataset\n",
"from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
"from torch.utils.data import DataLoader\n",
"import evaluate\n",
"\n",
"import sys\n",
"sys.path.append('..')\n",
"\n",
"from nested_lora import inject_nested_lora\n",
"from orbital_controller import OrbitalController\n",
"from controller import set_rank\n",
"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"print(device)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"dataset = load_dataset('glue','mrpc')\n",
"tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')\n",
"\n",
"def tok(x):\n",
" return tokenizer(x['sentence1'], x['sentence2'], truncation=True, padding='max_length', max_length=128)\n",
"\n",
"train = dataset['train'].map(tok, batched=True)\n",
"val = dataset['validation'].map(tok, batched=True)\n",
"\n",
"train.set_format(type='torch', columns=['input_ids','attention_mask','label'])\n",
"val.set_format(type='torch', columns=['input_ids','attention_mask','label'])\n",
"\n",
"train_loader = DataLoader(train, batch_size=16, shuffle=True)\n",
"val_loader = DataLoader(val, batch_size=16)\n",
"\n",
"metric = evaluate.load('glue','mrpc')"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"def eval_model(model):\n",
" model.eval()\n",
" preds, labels = [], []\n",
" with torch.no_grad():\n",
" for b in val_loader:\n",
" x=b['input_ids'].to(device)\n",
" m=b['attention_mask'].to(device)\n",
" y=b['label'].to(device)\n",
" p=model(input_ids=x,attention_mask=m).logits.argmax(-1)\n",
" preds.extend(p.cpu().numpy()); labels.extend(y.cpu().numpy())\n",
" return metric.compute(predictions=preds,references=labels)['f1']"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# BASELINE\n",
"model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)\n",
"model = inject_nested_lora(model,16).to(device)\n",
"set_rank(model,16)\n",
"\n",
"opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
"\n",
"for step,b in enumerate(train_loader):\n",
" if step>200: break\n",
" x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
" loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
" loss.backward(); opt.step(); opt.zero_grad()\n",
"\n",
"f1_base = eval_model(model)\n",
"print('Baseline F1:', round(f1_base,3))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# ORBITAL\n",
"model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)\n",
"model = inject_nested_lora(model,16).to(device)\n",
"\n",
"ctrl = OrbitalController(warmup=10, stable_window=6)\n",
"set_rank(model,4)\n",
"\n",
"opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
"\n",
"for step,b in enumerate(train_loader):\n",
" if step>200: break\n",
" x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
" loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
" loss.backward()\n",
"\n",
" r = ctrl.step(loss.item())\n",
" r = max(4,min(16,r))\n",
" set_rank(model,r)\n",
"\n",
" opt.step(); opt.zero_grad()\n",
"\n",
"f1_orb = eval_model(model)\n",
"print('Orbital F1:', round(f1_orb,3))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"print('\\nBaseline:', round(f1_base,3))\n",
"print('Orbital:', round(f1_orb,3))\n",
"print('Delta:', round(f1_orb-f1_base,3))"
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|