Author line: drop co-author mention
Browse files- tiny_llm_from_scratch.ipynb +67 -67
tiny_llm_from_scratch.ipynb
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
-
"id": "
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
"# Tiny Storyteller: building a language model from scratch\n",
|
| 9 |
"\n",
|
| 10 |
-
"**Author:** singhpratech
|
| 11 |
"\n",
|
| 12 |
"## Purpose\n",
|
| 13 |
"Learn how a language model (an \"LLM\") is really built, by building one from nothing.\n",
|
|
@@ -47,7 +47,7 @@
|
|
| 47 |
},
|
| 48 |
{
|
| 49 |
"cell_type": "markdown",
|
| 50 |
-
"id": "
|
| 51 |
"metadata": {},
|
| 52 |
"source": [
|
| 53 |
"## 0. Before any code: Momo, Ella, and what we are about to build\n",
|
|
@@ -59,7 +59,7 @@
|
|
| 59 |
},
|
| 60 |
{
|
| 61 |
"cell_type": "markdown",
|
| 62 |
-
"id": "
|
| 63 |
"metadata": {},
|
| 64 |
"source": [
|
| 65 |
"### Meet the characters\n",
|
|
@@ -82,7 +82,7 @@
|
|
| 82 |
},
|
| 83 |
{
|
| 84 |
"cell_type": "markdown",
|
| 85 |
-
"id": "
|
| 86 |
"metadata": {},
|
| 87 |
"source": [
|
| 88 |
"### 0.1 What is PyTorch?\n",
|
|
@@ -108,7 +108,7 @@
|
|
| 108 |
},
|
| 109 |
{
|
| 110 |
"cell_type": "markdown",
|
| 111 |
-
"id": "
|
| 112 |
"metadata": {},
|
| 113 |
"source": [
|
| 114 |
"### 0.2 What is a model, really?\n",
|
|
@@ -123,7 +123,7 @@
|
|
| 123 |
},
|
| 124 |
{
|
| 125 |
"cell_type": "markdown",
|
| 126 |
-
"id": "
|
| 127 |
"metadata": {},
|
| 128 |
"source": [
|
| 129 |
"### 0.3 What does a language model actually do?\n",
|
|
@@ -142,7 +142,7 @@
|
|
| 142 |
},
|
| 143 |
{
|
| 144 |
"cell_type": "markdown",
|
| 145 |
-
"id": "
|
| 146 |
"metadata": {},
|
| 147 |
"source": [
|
| 148 |
"### 0.4 What happens when you ask Momo a question?\n",
|
|
@@ -181,7 +181,7 @@
|
|
| 181 |
},
|
| 182 |
{
|
| 183 |
"cell_type": "markdown",
|
| 184 |
-
"id": "
|
| 185 |
"metadata": {},
|
| 186 |
"source": [
|
| 187 |
"### 0.5 How attention looks things up: the story circle\n",
|
|
@@ -213,7 +213,7 @@
|
|
| 213 |
},
|
| 214 |
{
|
| 215 |
"cell_type": "markdown",
|
| 216 |
-
"id": "
|
| 217 |
"metadata": {},
|
| 218 |
"source": [
|
| 219 |
"### 0.6 Ella's shelf: the KV cache\n",
|
|
@@ -240,7 +240,7 @@
|
|
| 240 |
},
|
| 241 |
{
|
| 242 |
"cell_type": "markdown",
|
| 243 |
-
"id": "
|
| 244 |
"metadata": {},
|
| 245 |
"source": [
|
| 246 |
"### 0.7 The strange names, decoded\n",
|
|
@@ -292,7 +292,7 @@
|
|
| 292 |
},
|
| 293 |
{
|
| 294 |
"cell_type": "markdown",
|
| 295 |
-
"id": "
|
| 296 |
"metadata": {},
|
| 297 |
"source": [
|
| 298 |
"### The one-sentence version\n",
|
|
@@ -306,7 +306,7 @@
|
|
| 306 |
},
|
| 307 |
{
|
| 308 |
"cell_type": "markdown",
|
| 309 |
-
"id": "
|
| 310 |
"metadata": {},
|
| 311 |
"source": [
|
| 312 |
"## Live flow diagram\n",
|
|
@@ -323,7 +323,7 @@
|
|
| 323 |
{
|
| 324 |
"cell_type": "code",
|
| 325 |
"execution_count": null,
|
| 326 |
-
"id": "
|
| 327 |
"metadata": {},
|
| 328 |
"outputs": [],
|
| 329 |
"source": [
|
|
@@ -448,7 +448,7 @@
|
|
| 448 |
},
|
| 449 |
{
|
| 450 |
"cell_type": "markdown",
|
| 451 |
-
"id": "
|
| 452 |
"metadata": {},
|
| 453 |
"source": [
|
| 454 |
"### Setup (still Part 0)\n",
|
|
@@ -460,7 +460,7 @@
|
|
| 460 |
{
|
| 461 |
"cell_type": "code",
|
| 462 |
"execution_count": null,
|
| 463 |
-
"id": "
|
| 464 |
"metadata": {},
|
| 465 |
"outputs": [],
|
| 466 |
"source": [
|
|
@@ -475,7 +475,7 @@
|
|
| 475 |
{
|
| 476 |
"cell_type": "code",
|
| 477 |
"execution_count": null,
|
| 478 |
-
"id": "
|
| 479 |
"metadata": {},
|
| 480 |
"outputs": [],
|
| 481 |
"source": [
|
|
@@ -492,7 +492,7 @@
|
|
| 492 |
},
|
| 493 |
{
|
| 494 |
"cell_type": "markdown",
|
| 495 |
-
"id": "
|
| 496 |
"metadata": {},
|
| 497 |
"source": [
|
| 498 |
"## 1. Tensors and gradients: the two building blocks\n",
|
|
@@ -511,7 +511,7 @@
|
|
| 511 |
{
|
| 512 |
"cell_type": "code",
|
| 513 |
"execution_count": null,
|
| 514 |
-
"id": "
|
| 515 |
"metadata": {},
|
| 516 |
"outputs": [],
|
| 517 |
"source": [
|
|
@@ -554,7 +554,7 @@
|
|
| 554 |
},
|
| 555 |
{
|
| 556 |
"cell_type": "markdown",
|
| 557 |
-
"id": "
|
| 558 |
"metadata": {},
|
| 559 |
"source": [
|
| 560 |
"### How matrix multiplication actually happens\n",
|
|
@@ -578,7 +578,7 @@
|
|
| 578 |
{
|
| 579 |
"cell_type": "code",
|
| 580 |
"execution_count": null,
|
| 581 |
-
"id": "
|
| 582 |
"metadata": {},
|
| 583 |
"outputs": [],
|
| 584 |
"source": [
|
|
@@ -635,7 +635,7 @@
|
|
| 635 |
{
|
| 636 |
"cell_type": "code",
|
| 637 |
"execution_count": null,
|
| 638 |
-
"id": "
|
| 639 |
"metadata": {},
|
| 640 |
"outputs": [],
|
| 641 |
"source": [
|
|
@@ -648,7 +648,7 @@
|
|
| 648 |
},
|
| 649 |
{
|
| 650 |
"cell_type": "markdown",
|
| 651 |
-
"id": "
|
| 652 |
"metadata": {},
|
| 653 |
"source": [
|
| 654 |
"### The gradient: which way to turn each dial\n",
|
|
@@ -671,7 +671,7 @@
|
|
| 671 |
{
|
| 672 |
"cell_type": "code",
|
| 673 |
"execution_count": null,
|
| 674 |
-
"id": "
|
| 675 |
"metadata": {},
|
| 676 |
"outputs": [],
|
| 677 |
"source": [
|
|
@@ -711,7 +711,7 @@
|
|
| 711 |
},
|
| 712 |
{
|
| 713 |
"cell_type": "markdown",
|
| 714 |
-
"id": "
|
| 715 |
"metadata": {},
|
| 716 |
"source": [
|
| 717 |
"### Momo in the foggy jungle: thorn bushes, marks, and the slope\n",
|
|
@@ -759,7 +759,7 @@
|
|
| 759 |
{
|
| 760 |
"cell_type": "code",
|
| 761 |
"execution_count": null,
|
| 762 |
-
"id": "
|
| 763 |
"metadata": {},
|
| 764 |
"outputs": [],
|
| 765 |
"source": [
|
|
@@ -870,7 +870,7 @@
|
|
| 870 |
},
|
| 871 |
{
|
| 872 |
"cell_type": "markdown",
|
| 873 |
-
"id": "
|
| 874 |
"metadata": {},
|
| 875 |
"source": [
|
| 876 |
"### How does Momo feel the slope without walking? The blame game\n",
|
|
@@ -906,7 +906,7 @@
|
|
| 906 |
{
|
| 907 |
"cell_type": "code",
|
| 908 |
"execution_count": null,
|
| 909 |
-
"id": "
|
| 910 |
"metadata": {},
|
| 911 |
"outputs": [],
|
| 912 |
"source": [
|
|
@@ -1007,7 +1007,7 @@
|
|
| 1007 |
},
|
| 1008 |
{
|
| 1009 |
"cell_type": "markdown",
|
| 1010 |
-
"id": "
|
| 1011 |
"metadata": {},
|
| 1012 |
"source": [
|
| 1013 |
"Now the blame game in three lines of PyTorch. Three dials, one mistake, `backward()`, and\n",
|
|
@@ -1021,7 +1021,7 @@
|
|
| 1021 |
{
|
| 1022 |
"cell_type": "code",
|
| 1023 |
"execution_count": null,
|
| 1024 |
-
"id": "
|
| 1025 |
"metadata": {},
|
| 1026 |
"outputs": [],
|
| 1027 |
"source": [
|
|
@@ -1039,7 +1039,7 @@
|
|
| 1039 |
},
|
| 1040 |
{
|
| 1041 |
"cell_type": "markdown",
|
| 1042 |
-
"id": "
|
| 1043 |
"metadata": {},
|
| 1044 |
"source": [
|
| 1045 |
"## 2. Data: Ella's bedtime stories (TinyStories)\n",
|
|
@@ -1055,7 +1055,7 @@
|
|
| 1055 |
{
|
| 1056 |
"cell_type": "code",
|
| 1057 |
"execution_count": null,
|
| 1058 |
-
"id": "
|
| 1059 |
"metadata": {},
|
| 1060 |
"outputs": [],
|
| 1061 |
"source": [
|
|
@@ -1073,7 +1073,7 @@
|
|
| 1073 |
},
|
| 1074 |
{
|
| 1075 |
"cell_type": "markdown",
|
| 1076 |
-
"id": "
|
| 1077 |
"metadata": {},
|
| 1078 |
"source": [
|
| 1079 |
"### Ella's own stories about Momo\n",
|
|
@@ -1096,7 +1096,7 @@
|
|
| 1096 |
{
|
| 1097 |
"cell_type": "code",
|
| 1098 |
"execution_count": null,
|
| 1099 |
-
"id": "
|
| 1100 |
"metadata": {},
|
| 1101 |
"outputs": [],
|
| 1102 |
"source": [
|
|
@@ -1207,7 +1207,7 @@
|
|
| 1207 |
},
|
| 1208 |
{
|
| 1209 |
"cell_type": "markdown",
|
| 1210 |
-
"id": "
|
| 1211 |
"metadata": {},
|
| 1212 |
"source": [
|
| 1213 |
"## 3. Momo's first trick: the bigram\n",
|
|
@@ -1233,7 +1233,7 @@
|
|
| 1233 |
{
|
| 1234 |
"cell_type": "code",
|
| 1235 |
"execution_count": null,
|
| 1236 |
-
"id": "
|
| 1237 |
"metadata": {},
|
| 1238 |
"outputs": [],
|
| 1239 |
"source": [
|
|
@@ -1263,7 +1263,7 @@
|
|
| 1263 |
},
|
| 1264 |
{
|
| 1265 |
"cell_type": "markdown",
|
| 1266 |
-
"id": "
|
| 1267 |
"metadata": {},
|
| 1268 |
"source": [
|
| 1269 |
"Now the same thing in code: first the letter vocabulary and the encode/decode pair."
|
|
@@ -1272,7 +1272,7 @@
|
|
| 1272 |
{
|
| 1273 |
"cell_type": "code",
|
| 1274 |
"execution_count": null,
|
| 1275 |
-
"id": "
|
| 1276 |
"metadata": {},
|
| 1277 |
"outputs": [],
|
| 1278 |
"source": [
|
|
@@ -1291,7 +1291,7 @@
|
|
| 1291 |
},
|
| 1292 |
{
|
| 1293 |
"cell_type": "markdown",
|
| 1294 |
-
"id": "
|
| 1295 |
"metadata": {},
|
| 1296 |
"source": [
|
| 1297 |
"Next, Momo's first lessons. Every lesson in this notebook, here and for the big model in\n",
|
|
@@ -1312,7 +1312,7 @@
|
|
| 1312 |
{
|
| 1313 |
"cell_type": "code",
|
| 1314 |
"execution_count": null,
|
| 1315 |
-
"id": "
|
| 1316 |
"metadata": {},
|
| 1317 |
"outputs": [],
|
| 1318 |
"source": [
|
|
@@ -1361,7 +1361,7 @@
|
|
| 1361 |
},
|
| 1362 |
{
|
| 1363 |
"cell_type": "markdown",
|
| 1364 |
-
"id": "
|
| 1365 |
"metadata": {},
|
| 1366 |
"source": [
|
| 1367 |
"Momo learned *something* (the loss dropped, and he produces word-ish shapes with spaces), but\n",
|
|
@@ -1371,7 +1371,7 @@
|
|
| 1371 |
},
|
| 1372 |
{
|
| 1373 |
"cell_type": "markdown",
|
| 1374 |
-
"id": "
|
| 1375 |
"metadata": {},
|
| 1376 |
"source": [
|
| 1377 |
"## 4. A real tokenizer: how Momo decides where to bite\n",
|
|
@@ -1398,7 +1398,7 @@
|
|
| 1398 |
{
|
| 1399 |
"cell_type": "code",
|
| 1400 |
"execution_count": null,
|
| 1401 |
-
"id": "
|
| 1402 |
"metadata": {},
|
| 1403 |
"outputs": [],
|
| 1404 |
"source": [
|
|
@@ -1428,7 +1428,7 @@
|
|
| 1428 |
{
|
| 1429 |
"cell_type": "code",
|
| 1430 |
"execution_count": null,
|
| 1431 |
-
"id": "
|
| 1432 |
"metadata": {},
|
| 1433 |
"outputs": [],
|
| 1434 |
"source": [
|
|
@@ -1455,7 +1455,7 @@
|
|
| 1455 |
},
|
| 1456 |
{
|
| 1457 |
"cell_type": "markdown",
|
| 1458 |
-
"id": "
|
| 1459 |
"metadata": {},
|
| 1460 |
"source": [
|
| 1461 |
"Now every story becomes numbers. We glue all the lesson stories into one long stream of token\n",
|
|
@@ -1466,7 +1466,7 @@
|
|
| 1466 |
{
|
| 1467 |
"cell_type": "code",
|
| 1468 |
"execution_count": null,
|
| 1469 |
-
"id": "
|
| 1470 |
"metadata": {},
|
| 1471 |
"outputs": [],
|
| 1472 |
"source": [
|
|
@@ -1480,7 +1480,7 @@
|
|
| 1480 |
},
|
| 1481 |
{
|
| 1482 |
"cell_type": "markdown",
|
| 1483 |
-
"id": "
|
| 1484 |
"metadata": {},
|
| 1485 |
"source": [
|
| 1486 |
"## 5. Attention, by hand: the story circle in numbers\n",
|
|
@@ -1506,7 +1506,7 @@
|
|
| 1506 |
{
|
| 1507 |
"cell_type": "code",
|
| 1508 |
"execution_count": null,
|
| 1509 |
-
"id": "
|
| 1510 |
"metadata": {},
|
| 1511 |
"outputs": [],
|
| 1512 |
"source": [
|
|
@@ -1531,7 +1531,7 @@
|
|
| 1531 |
},
|
| 1532 |
{
|
| 1533 |
"cell_type": "markdown",
|
| 1534 |
-
"id": "
|
| 1535 |
"metadata": {},
|
| 1536 |
"source": [
|
| 1537 |
"### Ella's shelf (the KV cache), in about a dozen lines\n",
|
|
@@ -1545,7 +1545,7 @@
|
|
| 1545 |
{
|
| 1546 |
"cell_type": "code",
|
| 1547 |
"execution_count": null,
|
| 1548 |
-
"id": "
|
| 1549 |
"metadata": {},
|
| 1550 |
"outputs": [],
|
| 1551 |
"source": [
|
|
@@ -1575,7 +1575,7 @@
|
|
| 1575 |
},
|
| 1576 |
{
|
| 1577 |
"cell_type": "markdown",
|
| 1578 |
-
"id": "
|
| 1579 |
"metadata": {},
|
| 1580 |
"source": [
|
| 1581 |
"## 6. A tiny GPT: the whole of Momo\n",
|
|
@@ -1610,7 +1610,7 @@
|
|
| 1610 |
{
|
| 1611 |
"cell_type": "code",
|
| 1612 |
"execution_count": null,
|
| 1613 |
-
"id": "
|
| 1614 |
"metadata": {},
|
| 1615 |
"outputs": [],
|
| 1616 |
"source": [
|
|
@@ -1654,7 +1654,7 @@
|
|
| 1654 |
{
|
| 1655 |
"cell_type": "code",
|
| 1656 |
"execution_count": null,
|
| 1657 |
-
"id": "
|
| 1658 |
"metadata": {},
|
| 1659 |
"outputs": [],
|
| 1660 |
"source": [
|
|
@@ -1765,7 +1765,7 @@
|
|
| 1765 |
},
|
| 1766 |
{
|
| 1767 |
"cell_type": "markdown",
|
| 1768 |
-
"id": "
|
| 1769 |
"metadata": {},
|
| 1770 |
"source": [
|
| 1771 |
"**Four things in that code we have not talked about.**\n",
|
|
@@ -1783,7 +1783,7 @@
|
|
| 1783 |
},
|
| 1784 |
{
|
| 1785 |
"cell_type": "markdown",
|
| 1786 |
-
"id": "
|
| 1787 |
"metadata": {},
|
| 1788 |
"source": [
|
| 1789 |
"## 7. Training: Ella's bedtime lessons\n",
|
|
@@ -1818,7 +1818,7 @@
|
|
| 1818 |
{
|
| 1819 |
"cell_type": "code",
|
| 1820 |
"execution_count": null,
|
| 1821 |
-
"id": "
|
| 1822 |
"metadata": {},
|
| 1823 |
"outputs": [],
|
| 1824 |
"source": [
|
|
@@ -1861,7 +1861,7 @@
|
|
| 1861 |
{
|
| 1862 |
"cell_type": "code",
|
| 1863 |
"execution_count": null,
|
| 1864 |
-
"id": "
|
| 1865 |
"metadata": {},
|
| 1866 |
"outputs": [],
|
| 1867 |
"source": [
|
|
@@ -1929,7 +1929,7 @@
|
|
| 1929 |
},
|
| 1930 |
{
|
| 1931 |
"cell_type": "markdown",
|
| 1932 |
-
"id": "
|
| 1933 |
"metadata": {},
|
| 1934 |
"source": [
|
| 1935 |
"**How to read the numbers.** The loss is Ella's average \"how surprised were you\" per token.\n",
|
|
@@ -1943,7 +1943,7 @@
|
|
| 1943 |
},
|
| 1944 |
{
|
| 1945 |
"cell_type": "markdown",
|
| 1946 |
-
"id": "
|
| 1947 |
"metadata": {},
|
| 1948 |
"source": [
|
| 1949 |
"## 8. Momo tells a story: the use case\n",
|
|
@@ -1962,7 +1962,7 @@
|
|
| 1962 |
{
|
| 1963 |
"cell_type": "code",
|
| 1964 |
"execution_count": null,
|
| 1965 |
-
"id": "
|
| 1966 |
"metadata": {},
|
| 1967 |
"outputs": [],
|
| 1968 |
"source": [
|
|
@@ -1980,7 +1980,7 @@
|
|
| 1980 |
},
|
| 1981 |
{
|
| 1982 |
"cell_type": "markdown",
|
| 1983 |
-
"id": "
|
| 1984 |
"metadata": {},
|
| 1985 |
"source": [
|
| 1986 |
"**Look closely at what we saw.** The Momo stories are almost word for word the sentences we\n",
|
|
@@ -1994,7 +1994,7 @@
|
|
| 1994 |
},
|
| 1995 |
{
|
| 1996 |
"cell_type": "markdown",
|
| 1997 |
-
"id": "
|
| 1998 |
"metadata": {},
|
| 1999 |
"source": [
|
| 2000 |
"## 9. Put Momo's book in the library: the Hugging Face Hub\n",
|
|
@@ -2019,7 +2019,7 @@
|
|
| 2019 |
{
|
| 2020 |
"cell_type": "code",
|
| 2021 |
"execution_count": null,
|
| 2022 |
-
"id": "
|
| 2023 |
"metadata": {},
|
| 2024 |
"outputs": [],
|
| 2025 |
"source": [
|
|
@@ -2063,7 +2063,7 @@
|
|
| 2063 |
{
|
| 2064 |
"cell_type": "code",
|
| 2065 |
"execution_count": null,
|
| 2066 |
-
"id": "
|
| 2067 |
"metadata": {},
|
| 2068 |
"outputs": [],
|
| 2069 |
"source": [
|
|
@@ -2084,7 +2084,7 @@
|
|
| 2084 |
},
|
| 2085 |
{
|
| 2086 |
"cell_type": "markdown",
|
| 2087 |
-
"id": "
|
| 2088 |
"metadata": {},
|
| 2089 |
"source": [
|
| 2090 |
"## 10. Loading it back later\n",
|
|
@@ -2096,7 +2096,7 @@
|
|
| 2096 |
{
|
| 2097 |
"cell_type": "code",
|
| 2098 |
"execution_count": null,
|
| 2099 |
-
"id": "
|
| 2100 |
"metadata": {},
|
| 2101 |
"outputs": [],
|
| 2102 |
"source": [
|
|
@@ -2119,7 +2119,7 @@
|
|
| 2119 |
},
|
| 2120 |
{
|
| 2121 |
"cell_type": "markdown",
|
| 2122 |
-
"id": "
|
| 2123 |
"metadata": {},
|
| 2124 |
"source": [
|
| 2125 |
"## What we built, in one paragraph\n",
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
+
"id": "d6dbef8d",
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
"# Tiny Storyteller: building a language model from scratch\n",
|
| 9 |
"\n",
|
| 10 |
+
"**Author:** singhpratech. **Runs on:** Google Colab (free T4 GPU), or a Mac with Apple Silicon, or CPU.\n",
|
| 11 |
"\n",
|
| 12 |
"## Purpose\n",
|
| 13 |
"Learn how a language model (an \"LLM\") is really built, by building one from nothing.\n",
|
|
|
|
| 47 |
},
|
| 48 |
{
|
| 49 |
"cell_type": "markdown",
|
| 50 |
+
"id": "52bce3d0",
|
| 51 |
"metadata": {},
|
| 52 |
"source": [
|
| 53 |
"## 0. Before any code: Momo, Ella, and what we are about to build\n",
|
|
|
|
| 59 |
},
|
| 60 |
{
|
| 61 |
"cell_type": "markdown",
|
| 62 |
+
"id": "18badc67",
|
| 63 |
"metadata": {},
|
| 64 |
"source": [
|
| 65 |
"### Meet the characters\n",
|
|
|
|
| 82 |
},
|
| 83 |
{
|
| 84 |
"cell_type": "markdown",
|
| 85 |
+
"id": "c6b124e5",
|
| 86 |
"metadata": {},
|
| 87 |
"source": [
|
| 88 |
"### 0.1 What is PyTorch?\n",
|
|
|
|
| 108 |
},
|
| 109 |
{
|
| 110 |
"cell_type": "markdown",
|
| 111 |
+
"id": "a8557a5e",
|
| 112 |
"metadata": {},
|
| 113 |
"source": [
|
| 114 |
"### 0.2 What is a model, really?\n",
|
|
|
|
| 123 |
},
|
| 124 |
{
|
| 125 |
"cell_type": "markdown",
|
| 126 |
+
"id": "f05878b3",
|
| 127 |
"metadata": {},
|
| 128 |
"source": [
|
| 129 |
"### 0.3 What does a language model actually do?\n",
|
|
|
|
| 142 |
},
|
| 143 |
{
|
| 144 |
"cell_type": "markdown",
|
| 145 |
+
"id": "8774b740",
|
| 146 |
"metadata": {},
|
| 147 |
"source": [
|
| 148 |
"### 0.4 What happens when you ask Momo a question?\n",
|
|
|
|
| 181 |
},
|
| 182 |
{
|
| 183 |
"cell_type": "markdown",
|
| 184 |
+
"id": "5a0ae3aa",
|
| 185 |
"metadata": {},
|
| 186 |
"source": [
|
| 187 |
"### 0.5 How attention looks things up: the story circle\n",
|
|
|
|
| 213 |
},
|
| 214 |
{
|
| 215 |
"cell_type": "markdown",
|
| 216 |
+
"id": "e786fabb",
|
| 217 |
"metadata": {},
|
| 218 |
"source": [
|
| 219 |
"### 0.6 Ella's shelf: the KV cache\n",
|
|
|
|
| 240 |
},
|
| 241 |
{
|
| 242 |
"cell_type": "markdown",
|
| 243 |
+
"id": "b4e0da60",
|
| 244 |
"metadata": {},
|
| 245 |
"source": [
|
| 246 |
"### 0.7 The strange names, decoded\n",
|
|
|
|
| 292 |
},
|
| 293 |
{
|
| 294 |
"cell_type": "markdown",
|
| 295 |
+
"id": "91659f55",
|
| 296 |
"metadata": {},
|
| 297 |
"source": [
|
| 298 |
"### The one-sentence version\n",
|
|
|
|
| 306 |
},
|
| 307 |
{
|
| 308 |
"cell_type": "markdown",
|
| 309 |
+
"id": "eabf2c36",
|
| 310 |
"metadata": {},
|
| 311 |
"source": [
|
| 312 |
"## Live flow diagram\n",
|
|
|
|
| 323 |
{
|
| 324 |
"cell_type": "code",
|
| 325 |
"execution_count": null,
|
| 326 |
+
"id": "1baea7bb",
|
| 327 |
"metadata": {},
|
| 328 |
"outputs": [],
|
| 329 |
"source": [
|
|
|
|
| 448 |
},
|
| 449 |
{
|
| 450 |
"cell_type": "markdown",
|
| 451 |
+
"id": "0c0f4f61",
|
| 452 |
"metadata": {},
|
| 453 |
"source": [
|
| 454 |
"### Setup (still Part 0)\n",
|
|
|
|
| 460 |
{
|
| 461 |
"cell_type": "code",
|
| 462 |
"execution_count": null,
|
| 463 |
+
"id": "478042b4",
|
| 464 |
"metadata": {},
|
| 465 |
"outputs": [],
|
| 466 |
"source": [
|
|
|
|
| 475 |
{
|
| 476 |
"cell_type": "code",
|
| 477 |
"execution_count": null,
|
| 478 |
+
"id": "00b89e95",
|
| 479 |
"metadata": {},
|
| 480 |
"outputs": [],
|
| 481 |
"source": [
|
|
|
|
| 492 |
},
|
| 493 |
{
|
| 494 |
"cell_type": "markdown",
|
| 495 |
+
"id": "33dd01e2",
|
| 496 |
"metadata": {},
|
| 497 |
"source": [
|
| 498 |
"## 1. Tensors and gradients: the two building blocks\n",
|
|
|
|
| 511 |
{
|
| 512 |
"cell_type": "code",
|
| 513 |
"execution_count": null,
|
| 514 |
+
"id": "0b5c0de4",
|
| 515 |
"metadata": {},
|
| 516 |
"outputs": [],
|
| 517 |
"source": [
|
|
|
|
| 554 |
},
|
| 555 |
{
|
| 556 |
"cell_type": "markdown",
|
| 557 |
+
"id": "2dfe6dad",
|
| 558 |
"metadata": {},
|
| 559 |
"source": [
|
| 560 |
"### How matrix multiplication actually happens\n",
|
|
|
|
| 578 |
{
|
| 579 |
"cell_type": "code",
|
| 580 |
"execution_count": null,
|
| 581 |
+
"id": "c94b0555",
|
| 582 |
"metadata": {},
|
| 583 |
"outputs": [],
|
| 584 |
"source": [
|
|
|
|
| 635 |
{
|
| 636 |
"cell_type": "code",
|
| 637 |
"execution_count": null,
|
| 638 |
+
"id": "e39e048b",
|
| 639 |
"metadata": {},
|
| 640 |
"outputs": [],
|
| 641 |
"source": [
|
|
|
|
| 648 |
},
|
| 649 |
{
|
| 650 |
"cell_type": "markdown",
|
| 651 |
+
"id": "3febc443",
|
| 652 |
"metadata": {},
|
| 653 |
"source": [
|
| 654 |
"### The gradient: which way to turn each dial\n",
|
|
|
|
| 671 |
{
|
| 672 |
"cell_type": "code",
|
| 673 |
"execution_count": null,
|
| 674 |
+
"id": "4e72fa2e",
|
| 675 |
"metadata": {},
|
| 676 |
"outputs": [],
|
| 677 |
"source": [
|
|
|
|
| 711 |
},
|
| 712 |
{
|
| 713 |
"cell_type": "markdown",
|
| 714 |
+
"id": "d8c2b93f",
|
| 715 |
"metadata": {},
|
| 716 |
"source": [
|
| 717 |
"### Momo in the foggy jungle: thorn bushes, marks, and the slope\n",
|
|
|
|
| 759 |
{
|
| 760 |
"cell_type": "code",
|
| 761 |
"execution_count": null,
|
| 762 |
+
"id": "2c8b20dd",
|
| 763 |
"metadata": {},
|
| 764 |
"outputs": [],
|
| 765 |
"source": [
|
|
|
|
| 870 |
},
|
| 871 |
{
|
| 872 |
"cell_type": "markdown",
|
| 873 |
+
"id": "ea803726",
|
| 874 |
"metadata": {},
|
| 875 |
"source": [
|
| 876 |
"### How does Momo feel the slope without walking? The blame game\n",
|
|
|
|
| 906 |
{
|
| 907 |
"cell_type": "code",
|
| 908 |
"execution_count": null,
|
| 909 |
+
"id": "dc8e8406",
|
| 910 |
"metadata": {},
|
| 911 |
"outputs": [],
|
| 912 |
"source": [
|
|
|
|
| 1007 |
},
|
| 1008 |
{
|
| 1009 |
"cell_type": "markdown",
|
| 1010 |
+
"id": "fbbc8f63",
|
| 1011 |
"metadata": {},
|
| 1012 |
"source": [
|
| 1013 |
"Now the blame game in three lines of PyTorch. Three dials, one mistake, `backward()`, and\n",
|
|
|
|
| 1021 |
{
|
| 1022 |
"cell_type": "code",
|
| 1023 |
"execution_count": null,
|
| 1024 |
+
"id": "db819628",
|
| 1025 |
"metadata": {},
|
| 1026 |
"outputs": [],
|
| 1027 |
"source": [
|
|
|
|
| 1039 |
},
|
| 1040 |
{
|
| 1041 |
"cell_type": "markdown",
|
| 1042 |
+
"id": "99aa0e23",
|
| 1043 |
"metadata": {},
|
| 1044 |
"source": [
|
| 1045 |
"## 2. Data: Ella's bedtime stories (TinyStories)\n",
|
|
|
|
| 1055 |
{
|
| 1056 |
"cell_type": "code",
|
| 1057 |
"execution_count": null,
|
| 1058 |
+
"id": "fc46895d",
|
| 1059 |
"metadata": {},
|
| 1060 |
"outputs": [],
|
| 1061 |
"source": [
|
|
|
|
| 1073 |
},
|
| 1074 |
{
|
| 1075 |
"cell_type": "markdown",
|
| 1076 |
+
"id": "d1eca3fe",
|
| 1077 |
"metadata": {},
|
| 1078 |
"source": [
|
| 1079 |
"### Ella's own stories about Momo\n",
|
|
|
|
| 1096 |
{
|
| 1097 |
"cell_type": "code",
|
| 1098 |
"execution_count": null,
|
| 1099 |
+
"id": "215e5fa4",
|
| 1100 |
"metadata": {},
|
| 1101 |
"outputs": [],
|
| 1102 |
"source": [
|
|
|
|
| 1207 |
},
|
| 1208 |
{
|
| 1209 |
"cell_type": "markdown",
|
| 1210 |
+
"id": "550278c3",
|
| 1211 |
"metadata": {},
|
| 1212 |
"source": [
|
| 1213 |
"## 3. Momo's first trick: the bigram\n",
|
|
|
|
| 1233 |
{
|
| 1234 |
"cell_type": "code",
|
| 1235 |
"execution_count": null,
|
| 1236 |
+
"id": "0b4022c7",
|
| 1237 |
"metadata": {},
|
| 1238 |
"outputs": [],
|
| 1239 |
"source": [
|
|
|
|
| 1263 |
},
|
| 1264 |
{
|
| 1265 |
"cell_type": "markdown",
|
| 1266 |
+
"id": "fc2334a9",
|
| 1267 |
"metadata": {},
|
| 1268 |
"source": [
|
| 1269 |
"Now the same thing in code: first the letter vocabulary and the encode/decode pair."
|
|
|
|
| 1272 |
{
|
| 1273 |
"cell_type": "code",
|
| 1274 |
"execution_count": null,
|
| 1275 |
+
"id": "0ebe181d",
|
| 1276 |
"metadata": {},
|
| 1277 |
"outputs": [],
|
| 1278 |
"source": [
|
|
|
|
| 1291 |
},
|
| 1292 |
{
|
| 1293 |
"cell_type": "markdown",
|
| 1294 |
+
"id": "b5cc8528",
|
| 1295 |
"metadata": {},
|
| 1296 |
"source": [
|
| 1297 |
"Next, Momo's first lessons. Every lesson in this notebook, here and for the big model in\n",
|
|
|
|
| 1312 |
{
|
| 1313 |
"cell_type": "code",
|
| 1314 |
"execution_count": null,
|
| 1315 |
+
"id": "c1c9d084",
|
| 1316 |
"metadata": {},
|
| 1317 |
"outputs": [],
|
| 1318 |
"source": [
|
|
|
|
| 1361 |
},
|
| 1362 |
{
|
| 1363 |
"cell_type": "markdown",
|
| 1364 |
+
"id": "be9bfbb8",
|
| 1365 |
"metadata": {},
|
| 1366 |
"source": [
|
| 1367 |
"Momo learned *something* (the loss dropped, and he produces word-ish shapes with spaces), but\n",
|
|
|
|
| 1371 |
},
|
| 1372 |
{
|
| 1373 |
"cell_type": "markdown",
|
| 1374 |
+
"id": "2f160514",
|
| 1375 |
"metadata": {},
|
| 1376 |
"source": [
|
| 1377 |
"## 4. A real tokenizer: how Momo decides where to bite\n",
|
|
|
|
| 1398 |
{
|
| 1399 |
"cell_type": "code",
|
| 1400 |
"execution_count": null,
|
| 1401 |
+
"id": "026dd481",
|
| 1402 |
"metadata": {},
|
| 1403 |
"outputs": [],
|
| 1404 |
"source": [
|
|
|
|
| 1428 |
{
|
| 1429 |
"cell_type": "code",
|
| 1430 |
"execution_count": null,
|
| 1431 |
+
"id": "4f80df23",
|
| 1432 |
"metadata": {},
|
| 1433 |
"outputs": [],
|
| 1434 |
"source": [
|
|
|
|
| 1455 |
},
|
| 1456 |
{
|
| 1457 |
"cell_type": "markdown",
|
| 1458 |
+
"id": "48a8b12f",
|
| 1459 |
"metadata": {},
|
| 1460 |
"source": [
|
| 1461 |
"Now every story becomes numbers. We glue all the lesson stories into one long stream of token\n",
|
|
|
|
| 1466 |
{
|
| 1467 |
"cell_type": "code",
|
| 1468 |
"execution_count": null,
|
| 1469 |
+
"id": "d96b25f1",
|
| 1470 |
"metadata": {},
|
| 1471 |
"outputs": [],
|
| 1472 |
"source": [
|
|
|
|
| 1480 |
},
|
| 1481 |
{
|
| 1482 |
"cell_type": "markdown",
|
| 1483 |
+
"id": "2c7ab127",
|
| 1484 |
"metadata": {},
|
| 1485 |
"source": [
|
| 1486 |
"## 5. Attention, by hand: the story circle in numbers\n",
|
|
|
|
| 1506 |
{
|
| 1507 |
"cell_type": "code",
|
| 1508 |
"execution_count": null,
|
| 1509 |
+
"id": "69abace2",
|
| 1510 |
"metadata": {},
|
| 1511 |
"outputs": [],
|
| 1512 |
"source": [
|
|
|
|
| 1531 |
},
|
| 1532 |
{
|
| 1533 |
"cell_type": "markdown",
|
| 1534 |
+
"id": "c24308af",
|
| 1535 |
"metadata": {},
|
| 1536 |
"source": [
|
| 1537 |
"### Ella's shelf (the KV cache), in about a dozen lines\n",
|
|
|
|
| 1545 |
{
|
| 1546 |
"cell_type": "code",
|
| 1547 |
"execution_count": null,
|
| 1548 |
+
"id": "84949030",
|
| 1549 |
"metadata": {},
|
| 1550 |
"outputs": [],
|
| 1551 |
"source": [
|
|
|
|
| 1575 |
},
|
| 1576 |
{
|
| 1577 |
"cell_type": "markdown",
|
| 1578 |
+
"id": "626f6cd8",
|
| 1579 |
"metadata": {},
|
| 1580 |
"source": [
|
| 1581 |
"## 6. A tiny GPT: the whole of Momo\n",
|
|
|
|
| 1610 |
{
|
| 1611 |
"cell_type": "code",
|
| 1612 |
"execution_count": null,
|
| 1613 |
+
"id": "af4363f8",
|
| 1614 |
"metadata": {},
|
| 1615 |
"outputs": [],
|
| 1616 |
"source": [
|
|
|
|
| 1654 |
{
|
| 1655 |
"cell_type": "code",
|
| 1656 |
"execution_count": null,
|
| 1657 |
+
"id": "c54e5e5e",
|
| 1658 |
"metadata": {},
|
| 1659 |
"outputs": [],
|
| 1660 |
"source": [
|
|
|
|
| 1765 |
},
|
| 1766 |
{
|
| 1767 |
"cell_type": "markdown",
|
| 1768 |
+
"id": "246a4e91",
|
| 1769 |
"metadata": {},
|
| 1770 |
"source": [
|
| 1771 |
"**Four things in that code we have not talked about.**\n",
|
|
|
|
| 1783 |
},
|
| 1784 |
{
|
| 1785 |
"cell_type": "markdown",
|
| 1786 |
+
"id": "31bb615e",
|
| 1787 |
"metadata": {},
|
| 1788 |
"source": [
|
| 1789 |
"## 7. Training: Ella's bedtime lessons\n",
|
|
|
|
| 1818 |
{
|
| 1819 |
"cell_type": "code",
|
| 1820 |
"execution_count": null,
|
| 1821 |
+
"id": "0edd20df",
|
| 1822 |
"metadata": {},
|
| 1823 |
"outputs": [],
|
| 1824 |
"source": [
|
|
|
|
| 1861 |
{
|
| 1862 |
"cell_type": "code",
|
| 1863 |
"execution_count": null,
|
| 1864 |
+
"id": "7ed19704",
|
| 1865 |
"metadata": {},
|
| 1866 |
"outputs": [],
|
| 1867 |
"source": [
|
|
|
|
| 1929 |
},
|
| 1930 |
{
|
| 1931 |
"cell_type": "markdown",
|
| 1932 |
+
"id": "ce14f79b",
|
| 1933 |
"metadata": {},
|
| 1934 |
"source": [
|
| 1935 |
"**How to read the numbers.** The loss is Ella's average \"how surprised were you\" per token.\n",
|
|
|
|
| 1943 |
},
|
| 1944 |
{
|
| 1945 |
"cell_type": "markdown",
|
| 1946 |
+
"id": "13ba6e8b",
|
| 1947 |
"metadata": {},
|
| 1948 |
"source": [
|
| 1949 |
"## 8. Momo tells a story: the use case\n",
|
|
|
|
| 1962 |
{
|
| 1963 |
"cell_type": "code",
|
| 1964 |
"execution_count": null,
|
| 1965 |
+
"id": "b8b00a38",
|
| 1966 |
"metadata": {},
|
| 1967 |
"outputs": [],
|
| 1968 |
"source": [
|
|
|
|
| 1980 |
},
|
| 1981 |
{
|
| 1982 |
"cell_type": "markdown",
|
| 1983 |
+
"id": "fc77f782",
|
| 1984 |
"metadata": {},
|
| 1985 |
"source": [
|
| 1986 |
"**Look closely at what we saw.** The Momo stories are almost word for word the sentences we\n",
|
|
|
|
| 1994 |
},
|
| 1995 |
{
|
| 1996 |
"cell_type": "markdown",
|
| 1997 |
+
"id": "24859a73",
|
| 1998 |
"metadata": {},
|
| 1999 |
"source": [
|
| 2000 |
"## 9. Put Momo's book in the library: the Hugging Face Hub\n",
|
|
|
|
| 2019 |
{
|
| 2020 |
"cell_type": "code",
|
| 2021 |
"execution_count": null,
|
| 2022 |
+
"id": "7e55d046",
|
| 2023 |
"metadata": {},
|
| 2024 |
"outputs": [],
|
| 2025 |
"source": [
|
|
|
|
| 2063 |
{
|
| 2064 |
"cell_type": "code",
|
| 2065 |
"execution_count": null,
|
| 2066 |
+
"id": "6cacb9a8",
|
| 2067 |
"metadata": {},
|
| 2068 |
"outputs": [],
|
| 2069 |
"source": [
|
|
|
|
| 2084 |
},
|
| 2085 |
{
|
| 2086 |
"cell_type": "markdown",
|
| 2087 |
+
"id": "77a4c8d7",
|
| 2088 |
"metadata": {},
|
| 2089 |
"source": [
|
| 2090 |
"## 10. Loading it back later\n",
|
|
|
|
| 2096 |
{
|
| 2097 |
"cell_type": "code",
|
| 2098 |
"execution_count": null,
|
| 2099 |
+
"id": "59455da3",
|
| 2100 |
"metadata": {},
|
| 2101 |
"outputs": [],
|
| 2102 |
"source": [
|
|
|
|
| 2119 |
},
|
| 2120 |
{
|
| 2121 |
"cell_type": "markdown",
|
| 2122 |
+
"id": "bef94f79",
|
| 2123 |
"metadata": {},
|
| 2124 |
"source": [
|
| 2125 |
"## What we built, in one paragraph\n",
|