lil-sumedhk Cursor commited on
Commit
06ab972
·
1 Parent(s): 82a52cd

Add training and evaluation notebooks

Browse files

Co-authored-by: Cursor <cursoragent@cursor.com>

Evaluation_TinyLlama_Math.ipynb ADDED
@@ -0,0 +1,489 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.4/44.4 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
13
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.9/9.9 MB\u001b[0m \u001b[31m53.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
14
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.0/3.0 MB\u001b[0m \u001b[31m114.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
15
+ "\u001b[?25h"
16
+ ]
17
+ }
18
+ ],
19
+ "source": [
20
+ "# Run this cell first, then restart runtime before continuing\n",
21
+ "%pip install -U torch transformers==4.45.2 datasets peft bitsandbytes accelerate -q"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 1,
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "import os\n",
31
+ "import math\n",
32
+ "\n",
33
+ "import torch\n",
34
+ "from torch.utils.data import DataLoader\n",
35
+ "\n",
36
+ "from datasets import load_dataset\n",
37
+ "from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, default_data_collator\n",
38
+ "\n",
39
+ "from peft import PeftModel"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": 20,
45
+ "metadata": {},
46
+ "outputs": [
47
+ {
48
+ "data": {
49
+ "application/vnd.jupyter.widget-view+json": {
50
+ "model_id": "d43d43d4c0834319a00bedbc599f1acf",
51
+ "version_major": 2,
52
+ "version_minor": 0
53
+ },
54
+ "text/plain": [
55
+ "adapter_config.json: 0%| | 0.00/987 [00:00<?, ?B/s]"
56
+ ]
57
+ },
58
+ "metadata": {},
59
+ "output_type": "display_data"
60
+ },
61
+ {
62
+ "data": {
63
+ "application/vnd.jupyter.widget-view+json": {
64
+ "model_id": "c0363b8b8df2403aa494bab839e08d74",
65
+ "version_major": 2,
66
+ "version_minor": 0
67
+ },
68
+ "text/plain": [
69
+ "adapter_model.safetensors: 0%| | 0.00/4.52M [00:00<?, ?B/s]"
70
+ ]
71
+ },
72
+ "metadata": {},
73
+ "output_type": "display_data"
74
+ },
75
+ {
76
+ "name": "stdout",
77
+ "output_type": "stream",
78
+ "text": [
79
+ "/usr/local/lib/python3.12/dist-packages/peft/tuners/lora/bnb.py:397: UserWarning: Merge lora module to 4-bit linear may get different generations due to rounding errors.\n",
80
+ " warnings.warn(\n"
81
+ ]
82
+ }
83
+ ],
84
+ "source": [
85
+ "model_name = 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'\n",
86
+ "adapter_path = 'sumedh/tinyllama-lora-math-adapter-v3' # Your HuggingFace adapter\n",
87
+ "\n",
88
+ "bnb_config = BitsAndBytesConfig(\n",
89
+ " load_in_4bit = True,\n",
90
+ " bnb_4bit_quant_type = 'nf4',\n",
91
+ " bnb_4bit_compute_dtype = torch.bfloat16\n",
92
+ ")\n",
93
+ "\n",
94
+ "tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\n",
95
+ "\n",
96
+ "base_model = AutoModelForCausalLM.from_pretrained(\n",
97
+ " model_name,\n",
98
+ " quantization_config = bnb_config,\n",
99
+ " device_map = 'auto',\n",
100
+ " trust_remote_code = True\n",
101
+ ").eval()\n",
102
+ "\n",
103
+ "tmp_model = AutoModelForCausalLM.from_pretrained(\n",
104
+ " model_name,\n",
105
+ " quantization_config = bnb_config,\n",
106
+ " device_map = 'auto',\n",
107
+ " trust_remote_code = True\n",
108
+ ")\n",
109
+ "\n",
110
+ "tuned_model = PeftModel.from_pretrained(tmp_model, adapter_path)\n",
111
+ "tuned_model = tuned_model.merge_and_unload().eval()"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": 21,
117
+ "metadata": {},
118
+ "outputs": [],
119
+ "source": [
120
+ "def tokenize(batch):\n",
121
+ " texts = [\n",
122
+ " f\"### Instruction:\\n{inst}\\n### Response:\\n{out}\"\n",
123
+ " for inst, out in zip(batch['question'], batch['answer'])\n",
124
+ " ]\n",
125
+ "\n",
126
+ " tokens = tokenizer(\n",
127
+ " texts,\n",
128
+ " padding = 'max_length',\n",
129
+ " truncation = True,\n",
130
+ " max_length = 256,\n",
131
+ " return_tensors = 'pt'\n",
132
+ " )\n",
133
+ "\n",
134
+ " tokens['labels'] = tokens['input_ids'].clone()\n",
135
+ "\n",
136
+ " return tokens"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 22,
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "eval_ds = load_dataset('openai/gsm8k', 'main', split='train[:20]')\n",
146
+ "eval_ds = eval_ds.map(tokenize, batched=True, remove_columns=['question', 'answer'])\n",
147
+ "eval_ds = eval_ds.with_format('torch')"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "execution_count": 23,
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": [
156
+ "eval_loader = DataLoader(\n",
157
+ " eval_ds,\n",
158
+ " batch_size = 8,\n",
159
+ " collate_fn = default_data_collator\n",
160
+ ")"
161
+ ]
162
+ },
163
+ {
164
+ "cell_type": "code",
165
+ "execution_count": 24,
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": [
169
+ "@torch.no_grad()\n",
170
+ "def compute_perplexity(model):\n",
171
+ " losses = []\n",
172
+ " \n",
173
+ " for batch in eval_loader:\n",
174
+ " batch = {k: v.to('cuda') for k, v in batch.items()}\n",
175
+ " loss = model(**batch).loss\n",
176
+ " losses.append(loss.item())\n",
177
+ "\n",
178
+ " return math.exp(sum(losses) / len(losses))"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": 25,
184
+ "metadata": {},
185
+ "outputs": [
186
+ {
187
+ "name": "stdout",
188
+ "output_type": "stream",
189
+ "text": [
190
+ "Base Model Perplexity: 139.52\n",
191
+ "Tuned Model Perplexity: 1.94\n"
192
+ ]
193
+ }
194
+ ],
195
+ "source": [
196
+ "print(f'Base Model Perplexity: {compute_perplexity(base_model):.2f}')\n",
197
+ "print(f'Tuned Model Perplexity: {compute_perplexity(tuned_model):.2f}')"
198
+ ]
199
+ },
200
+ {
201
+ "cell_type": "code",
202
+ "execution_count": 26,
203
+ "metadata": {},
204
+ "outputs": [
205
+ {
206
+ "data": {
207
+ "application/vnd.jupyter.widget-view+json": {
208
+ "model_id": "2e2cea799787401891d19b860e06daff",
209
+ "version_major": 2,
210
+ "version_minor": 0
211
+ },
212
+ "text/plain": [
213
+ "Map: 0%| | 0/100 [00:00<?, ? examples/s]"
214
+ ]
215
+ },
216
+ "metadata": {},
217
+ "output_type": "display_data"
218
+ }
219
+ ],
220
+ "source": [
221
+ "eval_ds = load_dataset('openai/gsm8k', 'main', split='test[:100]')\n",
222
+ "eval_ds = eval_ds.map(tokenize, batched=True, remove_columns=['question', 'answer'])\n",
223
+ "eval_ds = eval_ds.with_format('torch')"
224
+ ]
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "execution_count": 27,
229
+ "metadata": {},
230
+ "outputs": [],
231
+ "source": [
232
+ "eval_loader = DataLoader(\n",
233
+ " eval_ds,\n",
234
+ " batch_size = 8,\n",
235
+ " collate_fn = default_data_collator\n",
236
+ ")"
237
+ ]
238
+ },
239
+ {
240
+ "cell_type": "code",
241
+ "execution_count": 28,
242
+ "metadata": {},
243
+ "outputs": [
244
+ {
245
+ "name": "stdout",
246
+ "output_type": "stream",
247
+ "text": [
248
+ "Base Model Perplexity: 229.66\n",
249
+ "Tuned Model Perplexity: 1.96\n"
250
+ ]
251
+ }
252
+ ],
253
+ "source": [
254
+ "print(f'Base Model Perplexity: {compute_perplexity(base_model):.2f}')\n",
255
+ "print(f'Tuned Model Perplexity: {compute_perplexity(tuned_model):.2f}')"
256
+ ]
257
+ },
258
+ {
259
+ "cell_type": "code",
260
+ "execution_count": 29,
261
+ "metadata": {},
262
+ "outputs": [],
263
+ "source": [
264
+ "raw_data = load_dataset('gsm8k', 'main', split='test[:100]')\n",
265
+ "refs = raw_data['answer']\n",
266
+ "\n",
267
+ "\n",
268
+ "def generate(model, instruction):\n",
269
+ " token_ids = tokenizer(f'### Instruction:\\n{instruction}\\n### Response:\\n', return_tensors='pt').input_ids.to('cuda')\n",
270
+ "\n",
271
+ " with torch.no_grad():\n",
272
+ " out = model.generate(token_ids, max_new_tokens=256)\n",
273
+ "\n",
274
+ " return tokenizer.decode(out[0], skip_special_tokens=True)"
275
+ ]
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "execution_count": 30,
280
+ "metadata": {},
281
+ "outputs": [
282
+ {
283
+ "data": {
284
+ "application/vnd.google.colaboratory.intrinsic+json": {
285
+ "type": "string"
286
+ },
287
+ "text/plain": [
288
+ "'Sansa is a famous artist, she can draw a portrait and sell it according to its size. She sells an 8-inch portrait for $5, and a 16-inch portrait for twice the price of the 8-inch portrait. If she sells three 8-inch portraits and five 16-inch portraits per day, how many does she earns every 3 days?'"
289
+ ]
290
+ },
291
+ "execution_count": null,
292
+ "metadata": {},
293
+ "output_type": "execute_result"
294
+ }
295
+ ],
296
+ "source": [
297
+ "raw_data['question'][0]"
298
+ ]
299
+ },
300
+ {
301
+ "cell_type": "code",
302
+ "execution_count": 31,
303
+ "metadata": {},
304
+ "outputs": [
305
+ {
306
+ "name": "stdout",
307
+ "output_type": "stream",
308
+ "text": [
309
+ "### Instruction:\n",
310
+ "Sansa is a famous artist, she can draw a portrait and sell it according to its size. She sells an 8-inch portrait for $5, and a 16-inch portrait for twice the price of the 8-inch portrait. If she sells three 8-inch portraits and five 16-inch portraits per day, how many does she earns every 3 days?\n",
311
+ "### Response:\n",
312
+ "Sansa earns $100 per day, which means she earns $300 per week, and $1,200 per month, and $5,000 per year.\n"
313
+ ]
314
+ }
315
+ ],
316
+ "source": [
317
+ "print(generate(base_model, raw_data['question'][0]))"
318
+ ]
319
+ },
320
+ {
321
+ "cell_type": "code",
322
+ "execution_count": 32,
323
+ "metadata": {},
324
+ "outputs": [
325
+ {
326
+ "name": "stdout",
327
+ "output_type": "stream",
328
+ "text": [
329
+ "### Instruction:\n",
330
+ "Sansa is a famous artist, she can draw a portrait and sell it according to its size. She sells an 8-inch portrait for $5, and a 16-inch portrait for twice the price of the 8-inch portrait. If she sells three 8-inch portraits and five 16-inch portraits per day, how many does she earns every 3 days?\n",
331
+ "### Response:\n",
332
+ "First find the total price of the 8-inch portraits: 8 inches * $5 = $<<8*5=40>>40\n",
333
+ "Then find the total price of the 16-inch portraits: 16 inches * $2 = $<<16*2=32>>32\n",
334
+ "Then find the total price of the 8-inch portraits and 16-inch portraits: $40 + $32 = $<<40+32=72>>72\n",
335
+ "Then find the total price of the 8-inch portraits and 16-inch portraits per day: $72/day * 3 days/day = $<<72*3=216>>216\n",
336
+ "Then find the total price of the 8-inch portraits and 16-inch portraits per week: $216/week * 7 weeks/year = $<<216*7=1584>>1584\n",
337
+ "Then find the total price of the 8-inch portraits and 16-inch portraits per month: $15\n"
338
+ ]
339
+ }
340
+ ],
341
+ "source": [
342
+ "print(generate(tuned_model, raw_data['question'][0]))"
343
+ ]
344
+ },
345
+ {
346
+ "cell_type": "code",
347
+ "execution_count": 33,
348
+ "metadata": {},
349
+ "outputs": [
350
+ {
351
+ "name": "stdout",
352
+ "output_type": "stream",
353
+ "text": [
354
+ "Sansa earns $5 x 3 = $<<5*3=15>>15 every day by selling three 8-inch portraits.\n",
355
+ "The price of the 16-inch portrait is $5 x 2 = $<<5*2=10>>10 each.\n",
356
+ "So, she earns $10 x 5 = $<<10*5=50>>50 every day by selling five 16-inch portraits.\n",
357
+ "Her total earnings is $50 + $15 = $<<50+15=65>>65 every day.\n",
358
+ "Therefore, the total amount she earns after 3 days is $65 x 3 = $<<65*3=195>>195.\n",
359
+ "#### 195\n"
360
+ ]
361
+ }
362
+ ],
363
+ "source": [
364
+ "print(refs[0])"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 34,
370
+ "metadata": {},
371
+ "outputs": [
372
+ {
373
+ "data": {
374
+ "application/vnd.google.colaboratory.intrinsic+json": {
375
+ "type": "string"
376
+ },
377
+ "text/plain": [
378
+ "'Archibald eats 1 apple a day for two weeks. Over the next three weeks, he eats the same number of apples as the total of the first two weeks. Over the next two weeks, he eats 3 apples a day. Over these 7 weeks, how many apples does he average a week?'"
379
+ ]
380
+ },
381
+ "execution_count": null,
382
+ "metadata": {},
383
+ "output_type": "execute_result"
384
+ }
385
+ ],
386
+ "source": [
387
+ "raw_data['question'][1]"
388
+ ]
389
+ },
390
+ {
391
+ "cell_type": "code",
392
+ "execution_count": 35,
393
+ "metadata": {},
394
+ "outputs": [
395
+ {
396
+ "name": "stdout",
397
+ "output_type": "stream",
398
+ "text": [
399
+ "### Instruction:\n",
400
+ "Archibald eats 1 apple a day for two weeks. Over the next three weeks, he eats the same number of apples as the total of the first two weeks. Over the next two weeks, he eats 3 apples a day. Over these 7 weeks, how many apples does he average a week?\n",
401
+ "### Response:\n",
402
+ "The average number of apples Archibald eats in the next 7 weeks is 2.5.\n"
403
+ ]
404
+ }
405
+ ],
406
+ "source": [
407
+ "print(generate(base_model, raw_data['question'][1]))"
408
+ ]
409
+ },
410
+ {
411
+ "cell_type": "code",
412
+ "execution_count": 36,
413
+ "metadata": {},
414
+ "outputs": [
415
+ {
416
+ "name": "stdout",
417
+ "output_type": "stream",
418
+ "text": [
419
+ "### Instruction:\n",
420
+ "Archibald eats 1 apple a day for two weeks. Over the next three weeks, he eats the same number of apples as the total of the first two weeks. Over the next two weeks, he eats 3 apples a day. Over these 7 weeks, how many apples does he average a week?\n",
421
+ "### Response:\n",
422
+ "Over the first two weeks, Archibald eats 1 x 2 = <<1*2=2>>2 apples.\n",
423
+ "Over the next three weeks, he eats 2 x 3 = <<2*3=6>>6 apples.\n",
424
+ "Over the next two weeks, he eats 3 x 2 = <<3*2=6>>6 apples.\n",
425
+ "Over the next two weeks, he eats 3 x 2 = <<3*2=6>>6 apples.\n",
426
+ "Over the next two weeks, he eats 6 x 2 = <<6*2=12>>12 apples.\n",
427
+ "Over the next two weeks, he eats 12 x 2 = <<12*2=24>>24 apples.\n",
428
+ "Over the next two weeks, he eats 24 x 2 = <<24*2=48>>48 apples.\n",
429
+ "Over the next two weeks, he eats 48 x 2 = <<48*2=96>>96 apples.\n",
430
+ "Over the next two weeks, he eats 96 x 2 = <<96*2=19\n"
431
+ ]
432
+ }
433
+ ],
434
+ "source": [
435
+ "print(generate(tuned_model, raw_data['question'][1]))"
436
+ ]
437
+ },
438
+ {
439
+ "cell_type": "code",
440
+ "execution_count": 37,
441
+ "metadata": {},
442
+ "outputs": [
443
+ {
444
+ "name": "stdout",
445
+ "output_type": "stream",
446
+ "text": [
447
+ "Sansa earns $5 x 3 = $<<5*3=15>>15 every day by selling three 8-inch portraits.\n",
448
+ "The price of the 16-inch portrait is $5 x 2 = $<<5*2=10>>10 each.\n",
449
+ "So, she earns $10 x 5 = $<<10*5=50>>50 every day by selling five 16-inch portraits.\n",
450
+ "Her total earnings is $50 + $15 = $<<50+15=65>>65 every day.\n",
451
+ "Therefore, the total amount she earns after 3 days is $65 x 3 = $<<65*3=195>>195.\n",
452
+ "#### 195\n"
453
+ ]
454
+ }
455
+ ],
456
+ "source": [
457
+ "print(refs[0])"
458
+ ]
459
+ },
460
+ {
461
+ "cell_type": "code",
462
+ "execution_count": null,
463
+ "metadata": {},
464
+ "outputs": [],
465
+ "source": []
466
+ }
467
+ ],
468
+ "metadata": {
469
+ "kernelspec": {
470
+ "display_name": "Python 3 (ipykernel)",
471
+ "language": "python",
472
+ "name": "python3"
473
+ },
474
+ "language_info": {
475
+ "codemirror_mode": {
476
+ "name": "ipython",
477
+ "version": 3
478
+ },
479
+ "file_extension": ".py",
480
+ "mimetype": "text/x-python",
481
+ "name": "python",
482
+ "nbconvert_exporter": "python",
483
+ "pygments_lexer": "ipython3",
484
+ "version": "3.12.12"
485
+ }
486
+ },
487
+ "nbformat": 4,
488
+ "nbformat_minor": 2
489
+ }
Fine-Tuning_TinyLlama_Math.ipynb ADDED
@@ -0,0 +1,619 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Run this cell first, then restart runtime (Runtime -> Restart runtime) before continuing\n",
8
+ "%pip install -U torch transformers==4.45.2 datasets peft bitsandbytes accelerate -q"
9
+ ],
10
+ "execution_count": 1,
11
+ "outputs": [
12
+ {
13
+ "output_type": "stream",
14
+ "text": [
15
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.0/44.0 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
16
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m899.7/899.7 MB\u001b[0m \u001b[31m781.0 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
17
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m594.3/594.3 MB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
18
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.2/10.2 MB\u001b[0m \u001b[31m85.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m0:01\u001b[0m\n",
19
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m88.0/88.0 MB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m0:00:01\u001b[0m00:01\u001b[0m\n",
20
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m954.8/954.8 kB\u001b[0m \u001b[31m60.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
21
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m193.1/193.1 MB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
22
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m64.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
23
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m63.6/63.6 MB\u001b[0m \u001b[31m12.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
24
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m267.5/267.5 MB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
25
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m288.2/288.2 MB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
26
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m39.3/39.3 MB\u001b[0m \u001b[31m52.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
27
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
28
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m170.5/170.5 MB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
29
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.0/12.0 MB\u001b[0m \u001b[31m93.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m0:01\u001b[0m\n",
30
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m515.2/515.2 kB\u001b[0m \u001b[31m47.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
31
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m557.0/557.0 kB\u001b[0m \u001b[31m49.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
32
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m59.1/59.1 MB\u001b[0m \u001b[31m13.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
33
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m47.7/47.7 MB\u001b[0m \u001b[31m17.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
34
+ "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
35
+ "torchaudio 2.9.0+cu126 requires torch==2.9.0, but you have torch 2.9.1 which is incompatible.\n",
36
+ "torchvision 0.24.0+cu126 requires torch==2.9.0, but you have torch 2.9.1 which is incompatible.\u001b[0m\u001b[31m\n",
37
+ "\u001b[0m"
38
+ ]
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "metadata": {},
45
+ "source": [
46
+ "# Run this cell AFTER restarting runtime\n",
47
+ "import torch\n",
48
+ "\n",
49
+ "from datasets import load_dataset\n",
50
+ "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, BitsAndBytesConfig\n",
51
+ "\n",
52
+ "from peft import LoraConfig, get_peft_model, TaskType"
53
+ ],
54
+ "execution_count": 17,
55
+ "outputs": []
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "metadata": {},
60
+ "source": [
61
+ "model_name = 'TinyLLama/TinyLlama-1.1B-Chat-v1.0'\n",
62
+ "\n",
63
+ "bnb_config = BitsAndBytesConfig(\n",
64
+ " load_in_4bit = True,\n",
65
+ " bnb_4bit_quant_type = 'nf4',\n",
66
+ " bnb_4bit_compute_dtype = torch.bfloat16\n",
67
+ ")\n",
68
+ "\n",
69
+ "model = AutoModelForCausalLM.from_pretrained(\n",
70
+ " model_name,\n",
71
+ " quantization_config = bnb_config,\n",
72
+ " device_map = 'auto',\n",
73
+ " trust_remote_code = True\n",
74
+ ")\n",
75
+ "\n",
76
+ "tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)"
77
+ ],
78
+ "execution_count": 18,
79
+ "outputs": []
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "metadata": {},
84
+ "source": [
85
+ "lora_config = LoraConfig(\n",
86
+ " r = 8,\n",
87
+ " lora_alpha = 16,\n",
88
+ " target_modules = ['q_proj', 'v_proj'],\n",
89
+ " lora_dropout = 0.05,\n",
90
+ " bias = 'none',\n",
91
+ " task_type = TaskType.CAUSAL_LM\n",
92
+ ")\n",
93
+ "\n",
94
+ "model = get_peft_model(model, lora_config)"
95
+ ],
96
+ "execution_count": 19,
97
+ "outputs": []
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "metadata": {},
102
+ "source": [
103
+ "# Load FULL GSM8K dataset (7,473 examples instead of 200)\n",
104
+ "data = load_dataset('openai/gsm8k', 'main', split='train')\n",
105
+ "print(f\"Training on {len(data)} examples\")"
106
+ ],
107
+ "execution_count": 20,
108
+ "outputs": [
109
+ {
110
+ "output_type": "stream",
111
+ "text": [
112
+ "Training on 7473 examples\n"
113
+ ]
114
+ }
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "metadata": {},
120
+ "source": [
121
+ "def tokenize(batch):\n",
122
+ " texts = [\n",
123
+ " f\"### Instruction:\\n{inst}\\n### Response:\\n{out}\"\n",
124
+ " for inst, out in zip(batch['question'], batch['answer'])\n",
125
+ " ]\n",
126
+ "\n",
127
+ " tokens = tokenizer(\n",
128
+ " texts,\n",
129
+ " padding = 'max_length',\n",
130
+ " truncation = True,\n",
131
+ " max_length = 256,\n",
132
+ " return_tensors = 'pt'\n",
133
+ " )\n",
134
+ "\n",
135
+ " tokens['labels'] = tokens['input_ids'].clone()\n",
136
+ "\n",
137
+ " return tokens"
138
+ ],
139
+ "execution_count": 21,
140
+ "outputs": []
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "metadata": {},
145
+ "source": [
146
+ "tokenized_data = data.map(tokenize, batched=True, remove_columns=data.column_names)"
147
+ ],
148
+ "execution_count": 22,
149
+ "outputs": []
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "metadata": {},
154
+ "source": [
155
+ "training_args = TrainingArguments(\n",
156
+ " output_dir = './tinyllama-lora-tuned',\n",
157
+ " per_device_train_batch_size = 4,\n",
158
+ " gradient_accumulation_steps = 4,\n",
159
+ " learning_rate = 5e-4, # Balanced LR - not too high, not too low\n",
160
+ " num_train_epochs = 5, # Sweet spot between 3 (underfit) and 50 (overfit)\n",
161
+ " fp16 = True,\n",
162
+ " logging_steps = 100,\n",
163
+ " save_strategy = 'epoch',\n",
164
+ " report_to = 'none',\n",
165
+ " remove_unused_columns = False,\n",
166
+ " label_names = [\"labels\"]\n",
167
+ ")"
168
+ ],
169
+ "execution_count": 23,
170
+ "outputs": []
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "metadata": {},
175
+ "source": [
176
+ "trainer = Trainer(\n",
177
+ " model = model,\n",
178
+ " args = training_args,\n",
179
+ " train_dataset = tokenized_data,\n",
180
+ " processing_class = tokenizer\n",
181
+ ")"
182
+ ],
183
+ "execution_count": 24,
184
+ "outputs": []
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "metadata": {},
189
+ "source": [
190
+ "trainer.train()"
191
+ ],
192
+ "execution_count": 25,
193
+ "outputs": [
194
+ {
195
+ "output_type": "stream",
196
+ "text": [
197
+ "The tokenizer has new PAD/BOS/EOS tokens that differ from the model config and generation config. The model config and generation config were aligned accordingly, being updated with the tokenizer's values. Updated tokens: {'pad_token_id': 2}.\n"
198
+ ]
199
+ },
200
+ {
201
+ "output_type": "display_data",
202
+ "data": {
203
+ "text/html": [
204
+ "\n",
205
+ " <div>\n",
206
+ " \n",
207
+ " <progress value='2340' max='2340' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
208
+ " [2340/2340 1:07:47, Epoch 5/5]\n",
209
+ " </div>\n",
210
+ " <table border=\"1\" class=\"dataframe\">\n",
211
+ " <thead>\n",
212
+ " <tr style=\"text-align: left;\">\n",
213
+ " <th>Step</th>\n",
214
+ " <th>Training Loss</th>\n",
215
+ " </tr>\n",
216
+ " </thead>\n",
217
+ " <tbody>\n",
218
+ " <tr>\n",
219
+ " <td>100</td>\n",
220
+ " <td>1.108600</td>\n",
221
+ " </tr>\n",
222
+ " <tr>\n",
223
+ " <td>200</td>\n",
224
+ " <td>0.768000</td>\n",
225
+ " </tr>\n",
226
+ " <tr>\n",
227
+ " <td>300</td>\n",
228
+ " <td>0.750100</td>\n",
229
+ " </tr>\n",
230
+ " <tr>\n",
231
+ " <td>400</td>\n",
232
+ " <td>0.738400</td>\n",
233
+ " </tr>\n",
234
+ " <tr>\n",
235
+ " <td>500</td>\n",
236
+ " <td>0.729700</td>\n",
237
+ " </tr>\n",
238
+ " <tr>\n",
239
+ " <td>600</td>\n",
240
+ " <td>0.719300</td>\n",
241
+ " </tr>\n",
242
+ " <tr>\n",
243
+ " <td>700</td>\n",
244
+ " <td>0.695700</td>\n",
245
+ " </tr>\n",
246
+ " <tr>\n",
247
+ " <td>800</td>\n",
248
+ " <td>0.700200</td>\n",
249
+ " </tr>\n",
250
+ " <tr>\n",
251
+ " <td>900</td>\n",
252
+ " <td>0.704300</td>\n",
253
+ " </tr>\n",
254
+ " <tr>\n",
255
+ " <td>1000</td>\n",
256
+ " <td>0.676400</td>\n",
257
+ " </tr>\n",
258
+ " <tr>\n",
259
+ " <td>1100</td>\n",
260
+ " <td>0.674200</td>\n",
261
+ " </tr>\n",
262
+ " <tr>\n",
263
+ " <td>1200</td>\n",
264
+ " <td>0.684300</td>\n",
265
+ " </tr>\n",
266
+ " <tr>\n",
267
+ " <td>1300</td>\n",
268
+ " <td>0.681500</td>\n",
269
+ " </tr>\n",
270
+ " <tr>\n",
271
+ " <td>1400</td>\n",
272
+ " <td>0.673100</td>\n",
273
+ " </tr>\n",
274
+ " <tr>\n",
275
+ " <td>1500</td>\n",
276
+ " <td>0.649500</td>\n",
277
+ " </tr>\n",
278
+ " <tr>\n",
279
+ " <td>1600</td>\n",
280
+ " <td>0.640400</td>\n",
281
+ " </tr>\n",
282
+ " <tr>\n",
283
+ " <td>1700</td>\n",
284
+ " <td>0.675900</td>\n",
285
+ " </tr>\n",
286
+ " <tr>\n",
287
+ " <td>1800</td>\n",
288
+ " <td>0.656900</td>\n",
289
+ " </tr>\n",
290
+ " <tr>\n",
291
+ " <td>1900</td>\n",
292
+ " <td>0.649000</td>\n",
293
+ " </tr>\n",
294
+ " <tr>\n",
295
+ " <td>2000</td>\n",
296
+ " <td>0.648900</td>\n",
297
+ " </tr>\n",
298
+ " <tr>\n",
299
+ " <td>2100</td>\n",
300
+ " <td>0.634100</td>\n",
301
+ " </tr>\n",
302
+ " <tr>\n",
303
+ " <td>2200</td>\n",
304
+ " <td>0.642000</td>\n",
305
+ " </tr>\n",
306
+ " <tr>\n",
307
+ " <td>2300</td>\n",
308
+ " <td>0.643700</td>\n",
309
+ " </tr>\n",
310
+ " </tbody>\n",
311
+ "</table><p>"
312
+ ],
313
+ "text/plain": [
314
+ "<IPython.core.display.HTML object>"
315
+ ]
316
+ }
317
+ },
318
+ {
319
+ "output_type": "execute_result",
320
+ "data": {
321
+ "text/plain": [
322
+ "TrainOutput(global_step=2340, training_loss=0.7010380818293644, metrics={'train_runtime': 4069.7118, 'train_samples_per_second': 9.181, 'train_steps_per_second': 0.575, 'total_flos': 5.943804390014976e+16, 'train_loss': 0.7010380818293644, 'epoch': 5.0})"
323
+ ]
324
+ }
325
+ }
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "code",
330
+ "metadata": {},
331
+ "source": [
332
+ "model.save_pretrained(\"./tinyllama-lora-tuned-adapter-math\")\n",
333
+ "tokenizer.save_pretrained(\"./tinyllama-lora-tuned-adapter-math\")"
334
+ ],
335
+ "execution_count": 26,
336
+ "outputs": [
337
+ {
338
+ "output_type": "execute_result",
339
+ "data": {
340
+ "text/plain": [
341
+ "('./tinyllama-lora-tuned-adapter-math/tokenizer_config.json',\n",
342
+ " './tinyllama-lora-tuned-adapter-math/special_tokens_map.json',\n",
343
+ " './tinyllama-lora-tuned-adapter-math/chat_template.jinja',\n",
344
+ " './tinyllama-lora-tuned-adapter-math/tokenizer.model',\n",
345
+ " './tinyllama-lora-tuned-adapter-math/added_tokens.json',\n",
346
+ " './tinyllama-lora-tuned-adapter-math/tokenizer.json')"
347
+ ]
348
+ }
349
+ }
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "metadata": {},
355
+ "source": [
356
+ "# List files to verify\n",
357
+ "!ls -la tinyllama-lora-tuned-adapter-math/\n",
358
+ "!ls -la *.zip"
359
+ ],
360
+ "execution_count": 27,
361
+ "outputs": [
362
+ {
363
+ "output_type": "stream",
364
+ "text": [
365
+ "total 8472\n",
366
+ "drwxr-xr-x 2 root root 4096 Jan 15 16:48 .\n",
367
+ "drwxr-xr-x 1 root root 4096 Jan 15 16:48 ..\n",
368
+ "-rw-r--r-- 1 root root 987 Jan 15 18:12 adapter_config.json\n",
369
+ "-rw-r--r-- 1 root root 4517152 Jan 15 18:12 adapter_model.safetensors\n",
370
+ "-rw-r--r-- 1 root root 410 Jan 15 18:12 chat_template.jinja\n",
371
+ "-rw-r--r-- 1 root root 5222 Jan 15 18:12 README.md\n",
372
+ "-rw-r--r-- 1 root root 551 Jan 15 18:12 special_tokens_map.json\n",
373
+ "-rw-r--r-- 1 root root 951 Jan 15 18:12 tokenizer_config.json\n",
374
+ "-rw-r--r-- 1 root root 3619016 Jan 15 18:12 tokenizer.json\n",
375
+ "-rw-r--r-- 1 root root 499723 Jan 15 18:12 tokenizer.model\n",
376
+ "-rw-r--r-- 1 root root 4950493 Jan 15 16:48 adapter.zip\n"
377
+ ]
378
+ }
379
+ ]
380
+ },
381
+ {
382
+ "cell_type": "code",
383
+ "metadata": {},
384
+ "source": [
385
+ "# Alternative: download folder directly without zipping\n",
386
+ "!zip -r adapter.zip tinyllama-lora-tuned-adapter-math/\n",
387
+ "from google.colab import files\n",
388
+ "files.download('adapter.zip')"
389
+ ],
390
+ "execution_count": 28,
391
+ "outputs": [
392
+ {
393
+ "output_type": "stream",
394
+ "text": [
395
+ "updating: tinyllama-lora-tuned-adapter-math/ (stored 0%)\n",
396
+ "updating: tinyllama-lora-tuned-adapter-math/README.md (deflated 65%)\n",
397
+ "updating: tinyllama-lora-tuned-adapter-math/chat_template.jinja (deflated 60%)\n",
398
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer_config.json (deflated 69%)\n",
399
+ "updating: tinyllama-lora-tuned-adapter-math/special_tokens_map.json (deflated 79%)\n",
400
+ "updating: tinyllama-lora-tuned-adapter-math/adapter_model.safetensors (deflated 7%)\n",
401
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer.json (deflated 85%)\n",
402
+ "updating: tinyllama-lora-tuned-adapter-math/adapter_config.json (deflated 57%)\n",
403
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer.model (deflated 55%)\n"
404
+ ]
405
+ },
406
+ {
407
+ "output_type": "display_data",
408
+ "data": {
409
+ "application/javascript": "\n async function download(id, filename, size) {\n if (!google.colab.kernel.accessAllowed) {\n return;\n }\n const div = document.createElement('div');\n const label = document.createElement('label');\n label.textContent = `Downloading \"${filename}\": `;\n div.appendChild(label);\n const progress = document.createElement('progress');\n progress.max = size;\n div.appendChild(progress);\n document.body.appendChild(div);\n\n const buffers = [];\n let downloaded = 0;\n\n const channel = await google.colab.kernel.comms.open(id);\n // Send a message to notify the kernel that we're ready.\n channel.send({})\n\n for await (const message of channel.messages) {\n // Send a message to notify the kernel that we're ready.\n channel.send({})\n if (message.buffers) {\n for (const buffer of message.buffers) {\n buffers.push(buffer);\n downloaded += buffer.byteLength;\n progress.value = downloaded;\n }\n }\n }\n const blob = new Blob(buffers, {type: 'application/binary'});\n const a = document.createElement('a');\n a.href = window.URL.createObjectURL(blob);\n a.download = filename;\n div.appendChild(a);\n a.click();\n div.remove();\n }\n ",
410
+ "text/plain": [
411
+ "<IPython.core.display.Javascript object>"
412
+ ]
413
+ }
414
+ },
415
+ {
416
+ "output_type": "display_data",
417
+ "data": {
418
+ "application/javascript": "download(\"download_2f88d3f8-1ab1-4f24-98b2-9237f92a30e2\", \"adapter.zip\", 4958934)",
419
+ "text/plain": [
420
+ "<IPython.core.display.Javascript object>"
421
+ ]
422
+ }
423
+ }
424
+ ]
425
+ },
426
+ {
427
+ "cell_type": "code",
428
+ "metadata": {},
429
+ "source": [
430
+ "# Check what files exist\n",
431
+ "!ls -la /content/\n",
432
+ "\n",
433
+ "# Check if the adapter folder still exists\n",
434
+ "!ls -la /content/tinyllama-lora-tuned-adapter-math/ 2>/dev/null || echo \"Adapter folder not found!\""
435
+ ],
436
+ "execution_count": 29,
437
+ "outputs": [
438
+ {
439
+ "output_type": "stream",
440
+ "text": [
441
+ "total 4868\n",
442
+ "drwxr-xr-x 1 root root 4096 Jan 15 18:12 .\n",
443
+ "drwxr-xr-x 1 root root 4096 Jan 15 15:37 ..\n",
444
+ "-rw-r--r-- 1 root root 4958934 Jan 15 18:12 adapter.zip\n",
445
+ "drwxr-xr-x 4 root root 4096 Dec 9 14:41 .config\n",
446
+ "drwxr-xr-x 1 root root 4096 Dec 9 14:42 sample_data\n",
447
+ "drwxr-xr-x 7 root root 4096 Jan 15 18:12 tinyllama-lora-tuned\n",
448
+ "drwxr-xr-x 2 root root 4096 Jan 15 16:48 tinyllama-lora-tuned-adapter-math\n",
449
+ "total 8472\n",
450
+ "drwxr-xr-x 2 root root 4096 Jan 15 16:48 .\n",
451
+ "drwxr-xr-x 1 root root 4096 Jan 15 18:12 ..\n",
452
+ "-rw-r--r-- 1 root root 987 Jan 15 18:12 adapter_config.json\n",
453
+ "-rw-r--r-- 1 root root 4517152 Jan 15 18:12 adapter_model.safetensors\n",
454
+ "-rw-r--r-- 1 root root 410 Jan 15 18:12 chat_template.jinja\n",
455
+ "-rw-r--r-- 1 root root 5222 Jan 15 18:12 README.md\n",
456
+ "-rw-r--r-- 1 root root 551 Jan 15 18:12 special_tokens_map.json\n",
457
+ "-rw-r--r-- 1 root root 951 Jan 15 18:12 tokenizer_config.json\n",
458
+ "-rw-r--r-- 1 root root 3619016 Jan 15 18:12 tokenizer.json\n",
459
+ "-rw-r--r-- 1 root root 499723 Jan 15 18:12 tokenizer.model\n"
460
+ ]
461
+ }
462
+ ]
463
+ },
464
+ {
465
+ "cell_type": "code",
466
+ "metadata": {},
467
+ "source": [
468
+ "# Create zip and upload\n",
469
+ "!cd /content && zip -r adapter.zip tinyllama-lora-tuned-adapter-math/\n",
470
+ "!curl -F \"file=@/content/adapter.zip\" https://file.io"
471
+ ],
472
+ "execution_count": 30,
473
+ "outputs": [
474
+ {
475
+ "output_type": "stream",
476
+ "text": [
477
+ "updating: tinyllama-lora-tuned-adapter-math/ (stored 0%)\n",
478
+ "updating: tinyllama-lora-tuned-adapter-math/README.md (deflated 65%)\n",
479
+ "updating: tinyllama-lora-tuned-adapter-math/chat_template.jinja (deflated 60%)\n",
480
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer_config.json (deflated 69%)\n",
481
+ "updating: tinyllama-lora-tuned-adapter-math/special_tokens_map.json (deflated 79%)\n",
482
+ "updating: tinyllama-lora-tuned-adapter-math/adapter_model.safetensors (deflated 7%)\n",
483
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer.json (deflated 85%)\n",
484
+ "updating: tinyllama-lora-tuned-adapter-math/adapter_config.json (deflated 57%)\n",
485
+ "updating: tinyllama-lora-tuned-adapter-math/tokenizer.model (deflated 55%)\n"
486
+ ]
487
+ }
488
+ ]
489
+ },
490
+ {
491
+ "cell_type": "code",
492
+ "metadata": {},
493
+ "source": [
494
+ "from huggingface_hub import HfApi, login\n",
495
+ "\n",
496
+ "# Login with your token (get one from https://huggingface.co/settings/tokens)\n",
497
+ "login(token=\"YOUR_HF_TOKEN_HERE\")\n",
498
+ "\n",
499
+ "api = HfApi()\n",
500
+ "\n",
501
+ "# First, what's your HuggingFace username? Run this to find out:\n",
502
+ "print(api.whoami()[\"name\"])"
503
+ ],
504
+ "execution_count": 31,
505
+ "outputs": [
506
+ {
507
+ "output_type": "stream",
508
+ "text": [
509
+ "sumedh\n"
510
+ ]
511
+ }
512
+ ]
513
+ },
514
+ {
515
+ "cell_type": "code",
516
+ "metadata": {},
517
+ "source": [
518
+ "# Replace YOUR_USERNAME with the output from above\n",
519
+ "username = api.whoami()[\"name\"]\n",
520
+ "\n",
521
+ "repo_id = f\"{username}/tinyllama-lora-math-adapter-v3\"\n",
522
+ "\n",
523
+ "api.create_repo(repo_id, repo_type=\"model\", exist_ok=True)\n",
524
+ "\n",
525
+ "api.upload_folder(\n",
526
+ " folder_path=\"./tinyllama-lora-tuned-adapter-math\",\n",
527
+ " repo_id=repo_id,\n",
528
+ " repo_type=\"model\"\n",
529
+ ")\n",
530
+ "\n",
531
+ "print(f\"✅ Uploaded! View at: https://huggingface.co/{repo_id}\")"
532
+ ],
533
+ "execution_count": 32,
534
+ "outputs": [
535
+ {
536
+ "output_type": "display_data",
537
+ "data": {
538
+ "application/vnd.jupyter.widget-view+json": {
539
+ "model_id": "47cac90ac3314176bafffa9d9b92cc8a",
540
+ "version_major": 2,
541
+ "version_minor": 0
542
+ },
543
+ "text/plain": [
544
+ "Processing Files (0 / 0) : | | 0.00B / 0.00B "
545
+ ]
546
+ }
547
+ },
548
+ {
549
+ "output_type": "display_data",
550
+ "data": {
551
+ "application/vnd.jupyter.widget-view+json": {
552
+ "model_id": "56db538ebc974f08a8fc734552c8895a",
553
+ "version_major": 2,
554
+ "version_minor": 0
555
+ },
556
+ "text/plain": [
557
+ "New Data Upload : | | 0.00B / 0.00B "
558
+ ]
559
+ }
560
+ },
561
+ {
562
+ "output_type": "display_data",
563
+ "data": {
564
+ "application/vnd.jupyter.widget-view+json": {
565
+ "model_id": "59a216be8d0f44fbacf5128a00abe258",
566
+ "version_major": 2,
567
+ "version_minor": 0
568
+ },
569
+ "text/plain": [
570
+ " ...pter-math/tokenizer.model: 100%|##########| 500kB / 500kB "
571
+ ]
572
+ }
573
+ },
574
+ {
575
+ "output_type": "display_data",
576
+ "data": {
577
+ "application/vnd.jupyter.widget-view+json": {
578
+ "model_id": "8221ea23abb0423a8e14254ccf193bfb",
579
+ "version_major": 2,
580
+ "version_minor": 0
581
+ },
582
+ "text/plain": [
583
+ " ...adapter_model.safetensors: 12%|#2 | 555kB / 4.52MB "
584
+ ]
585
+ }
586
+ },
587
+ {
588
+ "output_type": "stream",
589
+ "text": [
590
+ "✅ Uploaded! View at: https://huggingface.co/sumedh/tinyllama-lora-math-adapter-v3\n"
591
+ ]
592
+ }
593
+ ]
594
+ },
595
+ {
596
+ "cell_type": "code",
597
+ "metadata": {},
598
+ "source": [],
599
+ "execution_count": null,
600
+ "outputs": []
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "metadata": {},
605
+ "source": [],
606
+ "execution_count": null,
607
+ "outputs": []
608
+ }
609
+ ],
610
+ "metadata": {
611
+ "kernelspec": {
612
+ "display_name": "Python 3 (ipykernel)",
613
+ "language": "python",
614
+ "name": "python3"
615
+ }
616
+ },
617
+ "nbformat": 4,
618
+ "nbformat_minor": 2
619
+ }
README.md CHANGED
@@ -38,3 +38,10 @@ The fine-tuned model learns:
38
  - Structured problem-solving approach
39
 
40
  However, as a 1.1B parameter model, complex multi-step calculations may still contain errors.
 
 
 
 
 
 
 
 
38
  - Structured problem-solving approach
39
 
40
  However, as a 1.1B parameter model, complex multi-step calculations may still contain errors.
41
+
42
+ ## Code
43
+
44
+ This repository includes the full training and evaluation code:
45
+
46
+ - **[Fine-Tuning_TinyLlama_Math.ipynb](Fine-Tuning_TinyLlama_Math.ipynb)** - Training notebook (run on Google Colab with GPU)
47
+ - **[Evaluation_TinyLlama_Math.ipynb](Evaluation_TinyLlama_Math.ipynb)** - Evaluation notebook comparing base vs fine-tuned model