Simo76 commited on
Commit
2d544ad
·
1 Parent(s): 121e571

Update mrpc_example.ipynb

Browse files
Files changed (1) hide show
  1. notebooks/mrpc_example.ipynb +133 -146
notebooks/mrpc_example.ipynb CHANGED
@@ -1,148 +1,135 @@
1
  {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "metadata": {},
6
- "source": [
7
- "# Orbital LoRA - MRPC Benchmark Example\n",
8
- "\n",
9
- "Expected: performance parity with baseline + adaptive behavior (no degradation)\n"
10
- ]
11
- },
12
- {
13
- "cell_type": "code",
14
- "source": [
15
- "!pip install -q transformers datasets evaluate scikit-learn accelerate"
16
- ]
17
- },
18
- {
19
- "cell_type": "code",
20
- "source": [
21
- "import os\n",
22
- "os.environ["WANDB_DISABLED"] = "true"\n",
23
- "\n",
24
- "import torch\n",
25
- "from datasets import load_dataset\n",
26
- "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
27
- "from torch.utils.data import DataLoader\n",
28
- "import evaluate\n",
29
- "\n",
30
- "import sys\n",
31
- "sys.path.append('..')\n",
32
- "\n",
33
- "from nested_lora import inject_nested_lora\n",
34
- "from orbital_controller import OrbitalController\n",
35
- "from controller import set_rank\n",
36
- "\n",
37
- "device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n",
38
- "print(device)"
39
- ]
40
- },
41
- {
42
- "cell_type": "code",
43
- "source": [
44
- "dataset = load_dataset("glue", "mrpc")\n",
45
- "tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")\n",
46
- "\n",
47
- "def tok(x):\n",
48
- " return tokenizer(x['sentence1'], x['sentence2'], truncation=True, padding="max_length", max_length=128)\n",
49
- "\n",
50
- "train = dataset['train'].map(tok, batched=True)\n",
51
- "val = dataset['validation'].map(tok, batched=True)\n",
52
- "\n",
53
- "train.set_format(type="torch", columns=["input_ids","attention_mask","label"])\n",
54
- "val.set_format(type="torch", columns=["input_ids","attention_mask","label"])\n",
55
- "\n",
56
- "train_loader = DataLoader(train, batch_size=16, shuffle=True)\n",
57
- "val_loader = DataLoader(val, batch_size=16)\n",
58
- "\n",
59
- "metric = evaluate.load("glue","mrpc")"
60
- ]
61
- },
62
- {
63
- "cell_type": "code",
64
- "source": [
65
- "def eval_model(model):\n",
66
- " model.eval()\n",
67
- " preds, labels = [], []\n",
68
- " with torch.no_grad():\n",
69
- " for b in val_loader:\n",
70
- " x=b['input_ids'].to(device)\n",
71
- " m=b['attention_mask'].to(device)\n",
72
- " y=b['label'].to(device)\n",
73
- " p=model(input_ids=x,attention_mask=m).logits.argmax(-1)\n",
74
- " preds.extend(p.cpu().numpy()); labels.extend(y.cpu().numpy())\n",
75
- " return metric.compute(predictions=preds,references=labels)['f1']"
76
- ]
77
- },
78
- {
79
- "cell_type": "markdown",
80
- "source": ["## Baseline"]
81
- },
82
- {
83
- "cell_type": "code",
84
- "source": [
85
- "model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=2)\n",
86
- "model = inject_nested_lora(model,16).to(device)\n",
87
- "set_rank(model,16)\n",
88
- "\n",
89
- "opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
90
- "\n",
91
- "for step,b in enumerate(train_loader):\n",
92
- " if step>200: break\n",
93
- " x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
94
- " loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
95
- " loss.backward(); opt.step(); opt.zero_grad()\n",
96
- "\n",
97
- "f1_base = eval_model(model)\n",
98
- "print("Baseline F1:", round(f1_base,3))"
99
- ]
100
- },
101
- {
102
- "cell_type": "markdown",
103
- "source": ["## Orbital"]
104
- },
105
- {
106
- "cell_type": "code",
107
- "source": [
108
- "model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=2)\n",
109
- "model = inject_nested_lora(model,16).to(device)\n",
110
- "\n",
111
- "ctrl = OrbitalController(warmup=10, stable_window=6)\n",
112
- "set_rank(model,4)\n",
113
- "\n",
114
- "opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
115
- "\n",
116
- "for step,b in enumerate(train_loader):\n",
117
- " if step>200: break\n",
118
- " x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
119
- " loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
120
- " loss.backward()\n",
121
- "\n",
122
- " r = ctrl.step(loss.item())\n",
123
- " r = max(4,min(16,r))\n",
124
- " set_rank(model,r)\n",
125
- "\n",
126
- " opt.step(); opt.zero_grad()\n",
127
- "\n",
128
- "f1_orb = eval_model(model)\n",
129
- "print("Orbital F1:", round(f1_orb,3))"
130
- ]
131
- },
132
- {
133
- "cell_type": "markdown",
134
- "source": ["## Result"]
135
- },
136
- {
137
- "cell_type": "code",
138
- "source": [
139
- "print("\nBaseline:", round(f1_base,3))\n",
140
- "print("Orbital:", round(f1_orb,3))\n",
141
- "print("Delta:", round(f1_orb-f1_base,3))"
142
- ]
143
- }
144
- ],
145
- "metadata": {},
146
- "nbformat": 4,
147
- "nbformat_minor": 4
148
  }
 
1
  {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Orbital LoRA - MRPC Benchmark Example\n",
8
+ "\n",
9
+ "**Expected:** performance parity with baseline + adaptive behavior\n"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "source": [
15
+ "!pip install -q transformers datasets evaluate scikit-learn accelerate"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "source": [
21
+ "import torch\n",
22
+ "from datasets import load_dataset\n",
23
+ "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
24
+ "from torch.utils.data import DataLoader\n",
25
+ "import evaluate\n",
26
+ "\n",
27
+ "import sys\n",
28
+ "sys.path.append('..')\n",
29
+ "\n",
30
+ "from nested_lora import inject_nested_lora\n",
31
+ "from orbital_controller import OrbitalController\n",
32
+ "from controller import set_rank\n",
33
+ "\n",
34
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
35
+ "print(device)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "source": [
41
+ "dataset = load_dataset('glue','mrpc')\n",
42
+ "tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')\n",
43
+ "\n",
44
+ "def tok(x):\n",
45
+ " return tokenizer(x['sentence1'], x['sentence2'], truncation=True, padding='max_length', max_length=128)\n",
46
+ "\n",
47
+ "train = dataset['train'].map(tok, batched=True)\n",
48
+ "val = dataset['validation'].map(tok, batched=True)\n",
49
+ "\n",
50
+ "train.set_format(type='torch', columns=['input_ids','attention_mask','label'])\n",
51
+ "val.set_format(type='torch', columns=['input_ids','attention_mask','label'])\n",
52
+ "\n",
53
+ "train_loader = DataLoader(train, batch_size=16, shuffle=True)\n",
54
+ "val_loader = DataLoader(val, batch_size=16)\n",
55
+ "\n",
56
+ "metric = evaluate.load('glue','mrpc')"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "source": [
62
+ "def eval_model(model):\n",
63
+ " model.eval()\n",
64
+ " preds, labels = [], []\n",
65
+ " with torch.no_grad():\n",
66
+ " for b in val_loader:\n",
67
+ " x=b['input_ids'].to(device)\n",
68
+ " m=b['attention_mask'].to(device)\n",
69
+ " y=b['label'].to(device)\n",
70
+ " p=model(input_ids=x,attention_mask=m).logits.argmax(-1)\n",
71
+ " preds.extend(p.cpu().numpy()); labels.extend(y.cpu().numpy())\n",
72
+ " return metric.compute(predictions=preds,references=labels)['f1']"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "source": [
78
+ "# BASELINE\n",
79
+ "model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)\n",
80
+ "model = inject_nested_lora(model,16).to(device)\n",
81
+ "set_rank(model,16)\n",
82
+ "\n",
83
+ "opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
84
+ "\n",
85
+ "for step,b in enumerate(train_loader):\n",
86
+ " if step>200: break\n",
87
+ " x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
88
+ " loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
89
+ " loss.backward(); opt.step(); opt.zero_grad()\n",
90
+ "\n",
91
+ "f1_base = eval_model(model)\n",
92
+ "print('Baseline F1:', round(f1_base,3))"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "source": [
98
+ "# ORBITAL\n",
99
+ "model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)\n",
100
+ "model = inject_nested_lora(model,16).to(device)\n",
101
+ "\n",
102
+ "ctrl = OrbitalController(warmup=10, stable_window=6)\n",
103
+ "set_rank(model,4)\n",
104
+ "\n",
105
+ "opt = torch.optim.AdamW(model.parameters(), lr=5e-5)\n",
106
+ "\n",
107
+ "for step,b in enumerate(train_loader):\n",
108
+ " if step>200: break\n",
109
+ " x=b['input_ids'].to(device); m=b['attention_mask'].to(device); y=b['label'].to(device)\n",
110
+ " loss=model(input_ids=x,attention_mask=m,labels=y).loss\n",
111
+ " loss.backward()\n",
112
+ "\n",
113
+ " r = ctrl.step(loss.item())\n",
114
+ " r = max(4,min(16,r))\n",
115
+ " set_rank(model,r)\n",
116
+ "\n",
117
+ " opt.step(); opt.zero_grad()\n",
118
+ "\n",
119
+ "f1_orb = eval_model(model)\n",
120
+ "print('Orbital F1:', round(f1_orb,3))"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "print('\\nBaseline:', round(f1_base,3))\n",
127
+ "print('Orbital:', round(f1_orb,3))\n",
128
+ "print('Delta:', round(f1_orb-f1_base,3))"
129
+ ]
130
+ }
131
+ ],
132
+ "metadata": {},
133
+ "nbformat": 4,
134
+ "nbformat_minor": 4
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  }