Add lesson_05.ipynb
Browse files- lesson_05.ipynb +55 -51
lesson_05.ipynb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
-
"id": "
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
"# e=mc² Lesson 05: Instruction tuning from scratch, turning a story generator into a prompt follower\n",
|
|
@@ -35,7 +35,7 @@
|
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"cell_type": "markdown",
|
| 38 |
-
"id": "
|
| 39 |
"metadata": {},
|
| 40 |
"source": [
|
| 41 |
"## 0. The story: Ella starts taking requests\n",
|
|
@@ -45,18 +45,20 @@
|
|
| 45 |
"teacher and the memory.\n",
|
| 46 |
"\n",
|
| 47 |
"Until now every lesson was \"here is a story, guess the next word\". Momo never had to listen to\n",
|
| 48 |
-
"what anyone *wanted*.
|
|
|
|
| 49 |
"me a story about Zuri being brave in a storm by the watering hole.\" Then she reads a story that\n",
|
| 50 |
"does exactly that. A request and the story that answers it make one **instruction pair**.\n",
|
| 51 |
"\n",
|
| 52 |
"Two problems come with this. First, Momo cannot tell where Ella's request stops and the story\n",
|
| 53 |
-
"begins. So Ella brings two
|
| 54 |
-
"
|
|
|
|
| 55 |
"used. Second, Ella only wants to grade the *story*. Guessing her request word by word is not\n",
|
| 56 |
"Momo's job. So during the request she puts her red pen down: **prompt masking**.\n",
|
| 57 |
"\n",
|
| 58 |
"**Where the picture is not the mechanics.** Momo does not \"understand\" a request. He learns that\n",
|
| 59 |
-
"after the request
|
| 60 |
"the story. Our check measures exactly that copying, by string match, and nothing more. And Ella's\n",
|
| 61 |
"\"red pen down\" is one line of code: the labels for the request tokens are set to -100, a value the\n",
|
| 62 |
"loss function skips.\n",
|
|
@@ -66,9 +68,9 @@
|
|
| 66 |
"|---|---|---|\n",
|
| 67 |
"| **Instruction tuning** | Ella starts every lesson with a request | Fine-tuning on (instruction, response) pairs so the model answers requests |\n",
|
| 68 |
"| **Instruction pair** | A request and the story that answers it | One training example: prompt text plus target text |\n",
|
| 69 |
-
"| **Chat template** | The order Ella always uses:
|
| 70 |
-
"| **Special token** | A
|
| 71 |
-
"| **Resize the embedding** | Two new rows on Momo's meaning map, for the two
|
| 72 |
"| **Prompt masking** | Ella's red pen is down during the request | Setting the request's labels to -100 so they add nothing to the loss |\n",
|
| 73 |
"| **Held-out instruction** | A request Ella never made in a lesson | A test prompt whose character, scene and place never appeared together in training |\n",
|
| 74 |
"| **String match** | Did the story say \"Zuri\" and \"by the watering hole\"? | Checking that exact substrings appear in the generated text |\n",
|
|
@@ -80,7 +82,7 @@
|
|
| 80 |
{
|
| 81 |
"cell_type": "code",
|
| 82 |
"execution_count": null,
|
| 83 |
-
"id": "
|
| 84 |
"metadata": {},
|
| 85 |
"outputs": [],
|
| 86 |
"source": [
|
|
@@ -95,7 +97,7 @@
|
|
| 95 |
{
|
| 96 |
"cell_type": "code",
|
| 97 |
"execution_count": null,
|
| 98 |
-
"id": "
|
| 99 |
"metadata": {},
|
| 100 |
"outputs": [],
|
| 101 |
"source": [
|
|
@@ -112,7 +114,7 @@
|
|
| 112 |
},
|
| 113 |
{
|
| 114 |
"cell_type": "markdown",
|
| 115 |
-
"id": "
|
| 116 |
"metadata": {},
|
| 117 |
"source": [
|
| 118 |
"## 1. Load tiny-storyteller-v2\n",
|
|
@@ -126,7 +128,7 @@
|
|
| 126 |
{
|
| 127 |
"cell_type": "code",
|
| 128 |
"execution_count": null,
|
| 129 |
-
"id": "
|
| 130 |
"metadata": {},
|
| 131 |
"outputs": [],
|
| 132 |
"source": [
|
|
@@ -223,13 +225,15 @@
|
|
| 223 |
{
|
| 224 |
"cell_type": "code",
|
| 225 |
"execution_count": null,
|
| 226 |
-
"id": "
|
| 227 |
"metadata": {},
|
| 228 |
"outputs": [],
|
| 229 |
"source": [
|
| 230 |
"from huggingface_hub import hf_hub_download\n",
|
| 231 |
"from transformers import PreTrainedTokenizerFast\n",
|
| 232 |
"\n",
|
|
|
|
|
|
|
| 233 |
"PREV_REPO = \"singhpratech/emc2-lesson-04-fine-tune\"\n",
|
| 234 |
"PREV_FOLDER = \"tiny-storyteller-v2\"\n",
|
| 235 |
"LOCAL_DIR = \"../04-fine-tune/tiny-storyteller-v2\" # fallback: where Lesson 04 saves it\n",
|
|
@@ -269,7 +273,7 @@
|
|
| 269 |
},
|
| 270 |
{
|
| 271 |
"cell_type": "markdown",
|
| 272 |
-
"id": "
|
| 273 |
"metadata": {},
|
| 274 |
"source": [
|
| 275 |
"## 2. The pairs, and 50 held-out instructions\n",
|
|
@@ -291,7 +295,7 @@
|
|
| 291 |
{
|
| 292 |
"cell_type": "code",
|
| 293 |
"execution_count": null,
|
| 294 |
-
"id": "
|
| 295 |
"metadata": {},
|
| 296 |
"outputs": [],
|
| 297 |
"source": [
|
|
@@ -394,7 +398,7 @@
|
|
| 394 |
{
|
| 395 |
"cell_type": "code",
|
| 396 |
"execution_count": null,
|
| 397 |
-
"id": "
|
| 398 |
"metadata": {},
|
| 399 |
"outputs": [],
|
| 400 |
"source": [
|
|
@@ -497,7 +501,7 @@
|
|
| 497 |
{
|
| 498 |
"cell_type": "code",
|
| 499 |
"execution_count": null,
|
| 500 |
-
"id": "
|
| 501 |
"metadata": {},
|
| 502 |
"outputs": [],
|
| 503 |
"source": [
|
|
@@ -557,13 +561,13 @@
|
|
| 557 |
" got += 1\n",
|
| 558 |
" return out\n",
|
| 559 |
"\n",
|
| 560 |
-
"print(story_with_meta('Zuri', random.Random(7)))"
|
| 561 |
]
|
| 562 |
},
|
| 563 |
{
|
| 564 |
"cell_type": "code",
|
| 565 |
"execution_count": null,
|
| 566 |
-
"id": "
|
| 567 |
"metadata": {},
|
| 568 |
"outputs": [],
|
| 569 |
"source": [
|
|
@@ -606,7 +610,7 @@
|
|
| 606 |
},
|
| 607 |
{
|
| 608 |
"cell_type": "markdown",
|
| 609 |
-
"id": "
|
| 610 |
"metadata": {},
|
| 611 |
"source": [
|
| 612 |
"### The 50 held-out instructions\n",
|
|
@@ -623,7 +627,7 @@
|
|
| 623 |
{
|
| 624 |
"cell_type": "code",
|
| 625 |
"execution_count": null,
|
| 626 |
-
"id": "
|
| 627 |
"metadata": {},
|
| 628 |
"outputs": [],
|
| 629 |
"source": [
|
|
@@ -657,7 +661,7 @@
|
|
| 657 |
},
|
| 658 |
{
|
| 659 |
"cell_type": "markdown",
|
| 660 |
-
"id": "
|
| 661 |
"metadata": {},
|
| 662 |
"source": [
|
| 663 |
"### The leakage check\n",
|
|
@@ -669,7 +673,7 @@
|
|
| 669 |
{
|
| 670 |
"cell_type": "code",
|
| 671 |
"execution_count": null,
|
| 672 |
-
"id": "
|
| 673 |
"metadata": {},
|
| 674 |
"outputs": [],
|
| 675 |
"source": [
|
|
@@ -690,7 +694,7 @@
|
|
| 690 |
},
|
| 691 |
{
|
| 692 |
"cell_type": "markdown",
|
| 693 |
-
"id": "
|
| 694 |
"metadata": {},
|
| 695 |
"source": [
|
| 696 |
"### A second, easier set: 50 familiar requests\n",
|
|
@@ -703,7 +707,7 @@
|
|
| 703 |
{
|
| 704 |
"cell_type": "code",
|
| 705 |
"execution_count": null,
|
| 706 |
-
"id": "
|
| 707 |
"metadata": {},
|
| 708 |
"outputs": [],
|
| 709 |
"source": [
|
|
@@ -724,7 +728,7 @@
|
|
| 724 |
},
|
| 725 |
{
|
| 726 |
"cell_type": "markdown",
|
| 727 |
-
"id": "
|
| 728 |
"metadata": {},
|
| 729 |
"source": [
|
| 730 |
"## 3. The chat template and two new tokens\n",
|
|
@@ -733,12 +737,12 @@
|
|
| 733 |
"\n",
|
| 734 |
"`<|endoftext|>` `<|instruction|>` *the request* `<|story|>` *the story* `<|endoftext|>`\n",
|
| 735 |
"\n",
|
| 736 |
-
"`<|instruction|>` and `<|story|>` are Ella's two
|
| 737 |
"**special tokens**, so the biter never splits them and a request can never accidentally contain\n",
|
| 738 |
"them.\n",
|
| 739 |
"\n",
|
| 740 |
"**Why the embedding must grow.** Momo's meaning map (the embedding table) has exactly one row per\n",
|
| 741 |
-
"token: 4,096 rows. The new
|
| 742 |
"would crash. We add two rows. In this model the scoring tray at the top is the *same* matrix (weight\n",
|
| 743 |
"tying, from Lesson 01), so it grows too. The old rows are copied unchanged; the new rows start at\n",
|
| 744 |
"the average of all the old rows, a neutral spot on the map that training then moves."
|
|
@@ -747,7 +751,7 @@
|
|
| 747 |
{
|
| 748 |
"cell_type": "code",
|
| 749 |
"execution_count": null,
|
| 750 |
-
"id": "
|
| 751 |
"metadata": {},
|
| 752 |
"outputs": [],
|
| 753 |
"source": [
|
|
@@ -781,7 +785,7 @@
|
|
| 781 |
},
|
| 782 |
{
|
| 783 |
"cell_type": "markdown",
|
| 784 |
-
"id": "
|
| 785 |
"metadata": {},
|
| 786 |
"source": [
|
| 787 |
"## 4. Prompt masking\n",
|
|
@@ -790,7 +794,7 @@
|
|
| 790 |
"mask, Ella would also grade the guesses *inside the request*: Momo would be trained to write\n",
|
| 791 |
"requests. With the mask, the labels for those positions are `-100`, and PyTorch's cross-entropy\n",
|
| 792 |
"skips any label equal to -100 (its `ignore_index`). The first graded guess is the first word of the\n",
|
| 793 |
-
"story, made while Momo is looking at the `<|story|>`
|
| 794 |
"\n",
|
| 795 |
"Click between the two modes below. The tokens are the real tokens of one training pair."
|
| 796 |
]
|
|
@@ -798,17 +802,17 @@
|
|
| 798 |
{
|
| 799 |
"cell_type": "code",
|
| 800 |
"execution_count": null,
|
| 801 |
-
"id": "
|
| 802 |
"metadata": {},
|
| 803 |
"outputs": [],
|
| 804 |
"source": [
|
| 805 |
"# Interactive: which tokens are graded, with and without the prompt mask.\n",
|
| 806 |
"from IPython.display import HTML, display\n",
|
| 807 |
"display(HTML(r\"\"\"<div id=\"l5m\" style=\"font-family:-apple-system,Segoe UI,Helvetica,Arial,sans-serif;color:#e8e8e8;background:#1e1e1e;border-radius:12px;padding:16px;max-width:900px;box-sizing:border-box\">\n",
|
| 808 |
-
"<style>#l5m button{background:#333;color:#eee;border:1px solid #777;border-radius:6px;padding:6px 12px;margin:0 6px 6px 0;cursor:pointer}#l5m button.l5m-on{border-color:#ffb347;background:#3a2e1a}#l5m .l5m-t{display:inline-block;margin:2px;padding:3px 5px;border-radius:5px;font-size:12px;font-family:Menlo,Consolas,monospace;border:1px solid #444;white-space:pre}#l5m .l5m-cap{font-size:13px;line-height:1.5;margin-top:8px;background:#262626;border-radius:8px;padding:10px}</style>\n",
|
| 809 |
"<div style=\"font-size:15px;font-weight:600;margin-bottom:6px\">Prompt masking: which tokens is Momo graded on? These are the real tokens of one training pair.</div>\n",
|
| 810 |
"<div><button id=\"l5mOff\">No mask: grade every token</button><button id=\"l5mOn\" class=\"l5m-on\">Mask the prompt</button></div>\n",
|
| 811 |
-
"<div id=\"l5mToks\" style=\"background:#262626;border-radius:8px;padding:8px;line-height:1.9;
|
| 812 |
"<div class=\"l5m-cap\" id=\"l5mCap\"></div>\n",
|
| 813 |
"</div>\n",
|
| 814 |
"<script>\n",
|
|
@@ -824,7 +828,7 @@
|
|
| 824 |
" const bg=counts?(isPrompt?'#4a3a1a':'#1f3d2c'):'#2b2b2b',fg=counts?'#f1eee4':'#777',bd=special?'#ffb347':'#444';\n",
|
| 825 |
" h+='<span class=\"l5m-t\" style=\"background:'+bg+';color:'+fg+';border-color:'+bd+'\" title=\"token '+i+'\">'+esc(t)+'</span>';});\n",
|
| 826 |
" box.innerHTML=h;\n",
|
| 827 |
-
" cap.innerHTML=(masked?'<b>Masked.</b> Grey tokens are Ella\\'s request: their labels are set to -100, so cross-entropy skips them. Momo is graded on <b>'+graded+'</b> of '+(D.tokens.length-1)+' next-token guesses, the story and the end marker, starting with the first word after the <|story|>
|
| 828 |
"bOn.addEventListener('click',function(){draw(true);});bOff.addEventListener('click',function(){draw(false);});draw(true);\n",
|
| 829 |
"})();\n",
|
| 830 |
"</script>\n",
|
|
@@ -833,7 +837,7 @@
|
|
| 833 |
},
|
| 834 |
{
|
| 835 |
"cell_type": "markdown",
|
| 836 |
-
"id": "
|
| 837 |
"metadata": {},
|
| 838 |
"source": [
|
| 839 |
"## 5. The check, before tuning\n",
|
|
@@ -843,14 +847,14 @@
|
|
| 843 |
"character's name, and does it contain the exact place phrase, such as \"by the watering hole\"\n",
|
| 844 |
"(ignoring capital letters)? The score is the share of the 50 where **both** are true.\n",
|
| 845 |
"\n",
|
| 846 |
-
"Before tuning, tiny-storyteller-v2 has no
|
| 847 |
"were the first line of a story, and we score only what it writes after it."
|
| 848 |
]
|
| 849 |
},
|
| 850 |
{
|
| 851 |
"cell_type": "code",
|
| 852 |
"execution_count": null,
|
| 853 |
-
"id": "
|
| 854 |
"metadata": {},
|
| 855 |
"outputs": [],
|
| 856 |
"source": [
|
|
@@ -884,7 +888,7 @@
|
|
| 884 |
},
|
| 885 |
{
|
| 886 |
"cell_type": "markdown",
|
| 887 |
-
"id": "
|
| 888 |
"metadata": {},
|
| 889 |
"source": [
|
| 890 |
"## 6. Instruction tuning: 500 steps\n",
|
|
@@ -901,7 +905,7 @@
|
|
| 901 |
{
|
| 902 |
"cell_type": "code",
|
| 903 |
"execution_count": null,
|
| 904 |
-
"id": "
|
| 905 |
"metadata": {},
|
| 906 |
"outputs": [],
|
| 907 |
"source": [
|
|
@@ -978,7 +982,7 @@
|
|
| 978 |
},
|
| 979 |
{
|
| 980 |
"cell_type": "markdown",
|
| 981 |
-
"id": "
|
| 982 |
"metadata": {},
|
| 983 |
"source": [
|
| 984 |
"## 7. The check, after tuning\n",
|
|
@@ -988,7 +992,7 @@
|
|
| 988 |
{
|
| 989 |
"cell_type": "code",
|
| 990 |
"execution_count": null,
|
| 991 |
-
"id": "
|
| 992 |
"metadata": {},
|
| 993 |
"outputs": [],
|
| 994 |
"source": [
|
|
@@ -1004,7 +1008,7 @@
|
|
| 1004 |
},
|
| 1005 |
{
|
| 1006 |
"cell_type": "markdown",
|
| 1007 |
-
"id": "
|
| 1008 |
"metadata": {},
|
| 1009 |
"source": [
|
| 1010 |
"### A harder test: places from the other world\n",
|
|
@@ -1016,7 +1020,7 @@
|
|
| 1016 |
{
|
| 1017 |
"cell_type": "code",
|
| 1018 |
"execution_count": null,
|
| 1019 |
-
"id": "
|
| 1020 |
"metadata": {},
|
| 1021 |
"outputs": [],
|
| 1022 |
"source": [
|
|
@@ -1038,7 +1042,7 @@
|
|
| 1038 |
{
|
| 1039 |
"cell_type": "code",
|
| 1040 |
"execution_count": null,
|
| 1041 |
-
"id": "
|
| 1042 |
"metadata": {},
|
| 1043 |
"outputs": [],
|
| 1044 |
"source": [
|
|
@@ -1049,7 +1053,7 @@
|
|
| 1049 |
},
|
| 1050 |
{
|
| 1051 |
"cell_type": "markdown",
|
| 1052 |
-
"id": "
|
| 1053 |
"metadata": {},
|
| 1054 |
"source": [
|
| 1055 |
"**What this means, honestly.** (Numbers are from our run on the Mac; yours may differ by a few\n",
|
|
@@ -1069,7 +1073,7 @@
|
|
| 1069 |
},
|
| 1070 |
{
|
| 1071 |
"cell_type": "markdown",
|
| 1072 |
-
"id": "
|
| 1073 |
"metadata": {},
|
| 1074 |
"source": [
|
| 1075 |
"## 8. Save tiny-storyteller-instruct and the pairs\n",
|
|
@@ -1081,7 +1085,7 @@
|
|
| 1081 |
{
|
| 1082 |
"cell_type": "code",
|
| 1083 |
"execution_count": null,
|
| 1084 |
-
"id": "
|
| 1085 |
"metadata": {},
|
| 1086 |
"outputs": [],
|
| 1087 |
"source": [
|
|
@@ -1120,7 +1124,7 @@
|
|
| 1120 |
{
|
| 1121 |
"cell_type": "code",
|
| 1122 |
"execution_count": null,
|
| 1123 |
-
"id": "
|
| 1124 |
"metadata": {},
|
| 1125 |
"outputs": [],
|
| 1126 |
"source": [
|
|
@@ -1140,7 +1144,7 @@
|
|
| 1140 |
},
|
| 1141 |
{
|
| 1142 |
"cell_type": "markdown",
|
| 1143 |
-
"id": "
|
| 1144 |
"metadata": {},
|
| 1145 |
"source": [
|
| 1146 |
"## What we learned\n",
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
+
"id": "8bed029a",
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
"# e=mc² Lesson 05: Instruction tuning from scratch, turning a story generator into a prompt follower\n",
|
|
|
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"cell_type": "markdown",
|
| 38 |
+
"id": "a04d18e9",
|
| 39 |
"metadata": {},
|
| 40 |
"source": [
|
| 41 |
"## 0. The story: Ella starts taking requests\n",
|
|
|
|
| 45 |
"teacher and the memory.\n",
|
| 46 |
"\n",
|
| 47 |
"Until now every lesson was \"here is a story, guess the next word\". Momo never had to listen to\n",
|
| 48 |
+
"what anyone *wanted*. That is **completion** (continue whatever text you are given); tonight's goal is **following**\n",
|
| 49 |
+
"(read a request, then write what it asks for). Tonight Ella changes the lessons. First she says a request out loud: \"Tell\n",
|
| 50 |
"me a story about Zuri being brave in a storm by the watering hole.\" Then she reads a story that\n",
|
| 51 |
"does exactly that. A request and the story that answers it make one **instruction pair**.\n",
|
| 52 |
"\n",
|
| 53 |
"Two problems come with this. First, Momo cannot tell where Ella's request stops and the story\n",
|
| 54 |
+
"begins. So Ella brings two signs (not Lesson 02's query, key and value cards: these are two new signs in\n",
|
| 55 |
+
"the vocabulary): one she holds up before the request, one she holds up when it is Momo's turn.\n",
|
| 56 |
+
"Those signs are **special tokens**, brand-new tokens Momo's biter has never\n",
|
| 57 |
"used. Second, Ella only wants to grade the *story*. Guessing her request word by word is not\n",
|
| 58 |
"Momo's job. So during the request she puts her red pen down: **prompt masking**.\n",
|
| 59 |
"\n",
|
| 60 |
"**Where the picture is not the mechanics.** Momo does not \"understand\" a request. He learns that\n",
|
| 61 |
+
"after the request sign, certain words in the request (a name, a place) are good words to copy into\n",
|
| 62 |
"the story. Our check measures exactly that copying, by string match, and nothing more. And Ella's\n",
|
| 63 |
"\"red pen down\" is one line of code: the labels for the request tokens are set to -100, a value the\n",
|
| 64 |
"loss function skips.\n",
|
|
|
|
| 68 |
"|---|---|---|\n",
|
| 69 |
"| **Instruction tuning** | Ella starts every lesson with a request | Fine-tuning on (instruction, response) pairs so the model answers requests |\n",
|
| 70 |
"| **Instruction pair** | A request and the story that answers it | One training example: prompt text plus target text |\n",
|
| 71 |
+
"| **Chat template** | The order Ella always uses: sign, request, sign, story | A fixed text format that marks who says what |\n",
|
| 72 |
+
"| **Special token** | A sign that is not a word | A new vocabulary entry the tokenizer never splits, used as a marker |\n",
|
| 73 |
+
"| **Resize the embedding** | Two new rows on Momo's meaning map, for the two signs | Growing the embedding (and tied output) matrix from 4,096 to 4,098 rows |\n",
|
| 74 |
"| **Prompt masking** | Ella's red pen is down during the request | Setting the request's labels to -100 so they add nothing to the loss |\n",
|
| 75 |
"| **Held-out instruction** | A request Ella never made in a lesson | A test prompt whose character, scene and place never appeared together in training |\n",
|
| 76 |
"| **String match** | Did the story say \"Zuri\" and \"by the watering hole\"? | Checking that exact substrings appear in the generated text |\n",
|
|
|
|
| 82 |
{
|
| 83 |
"cell_type": "code",
|
| 84 |
"execution_count": null,
|
| 85 |
+
"id": "9871bb57",
|
| 86 |
"metadata": {},
|
| 87 |
"outputs": [],
|
| 88 |
"source": [
|
|
|
|
| 97 |
{
|
| 98 |
"cell_type": "code",
|
| 99 |
"execution_count": null,
|
| 100 |
+
"id": "2cb90c87",
|
| 101 |
"metadata": {},
|
| 102 |
"outputs": [],
|
| 103 |
"source": [
|
|
|
|
| 114 |
},
|
| 115 |
{
|
| 116 |
"cell_type": "markdown",
|
| 117 |
+
"id": "ce4b8e29",
|
| 118 |
"metadata": {},
|
| 119 |
"source": [
|
| 120 |
"## 1. Load tiny-storyteller-v2\n",
|
|
|
|
| 128 |
{
|
| 129 |
"cell_type": "code",
|
| 130 |
"execution_count": null,
|
| 131 |
+
"id": "113d6bc3",
|
| 132 |
"metadata": {},
|
| 133 |
"outputs": [],
|
| 134 |
"source": [
|
|
|
|
| 225 |
{
|
| 226 |
"cell_type": "code",
|
| 227 |
"execution_count": null,
|
| 228 |
+
"id": "6bbb6823",
|
| 229 |
"metadata": {},
|
| 230 |
"outputs": [],
|
| 231 |
"source": [
|
| 232 |
"from huggingface_hub import hf_hub_download\n",
|
| 233 |
"from transformers import PreTrainedTokenizerFast\n",
|
| 234 |
"\n",
|
| 235 |
+
"# If the Lesson 04 repo is still private, log in first with a token that can read it:\n",
|
| 236 |
+
"# from huggingface_hub import login; login()\n",
|
| 237 |
"PREV_REPO = \"singhpratech/emc2-lesson-04-fine-tune\"\n",
|
| 238 |
"PREV_FOLDER = \"tiny-storyteller-v2\"\n",
|
| 239 |
"LOCAL_DIR = \"../04-fine-tune/tiny-storyteller-v2\" # fallback: where Lesson 04 saves it\n",
|
|
|
|
| 273 |
},
|
| 274 |
{
|
| 275 |
"cell_type": "markdown",
|
| 276 |
+
"id": "05a612c9",
|
| 277 |
"metadata": {},
|
| 278 |
"source": [
|
| 279 |
"## 2. The pairs, and 50 held-out instructions\n",
|
|
|
|
| 295 |
{
|
| 296 |
"cell_type": "code",
|
| 297 |
"execution_count": null,
|
| 298 |
+
"id": "e2b25f43",
|
| 299 |
"metadata": {},
|
| 300 |
"outputs": [],
|
| 301 |
"source": [
|
|
|
|
| 398 |
{
|
| 399 |
"cell_type": "code",
|
| 400 |
"execution_count": null,
|
| 401 |
+
"id": "763b08e9",
|
| 402 |
"metadata": {},
|
| 403 |
"outputs": [],
|
| 404 |
"source": [
|
|
|
|
| 501 |
{
|
| 502 |
"cell_type": "code",
|
| 503 |
"execution_count": null,
|
| 504 |
+
"id": "6b6023be",
|
| 505 |
"metadata": {},
|
| 506 |
"outputs": [],
|
| 507 |
"source": [
|
|
|
|
| 561 |
" got += 1\n",
|
| 562 |
" return out\n",
|
| 563 |
"\n",
|
| 564 |
+
"print('example instruction:', story_with_meta('Zuri', random.Random(7))['instruction'])"
|
| 565 |
]
|
| 566 |
},
|
| 567 |
{
|
| 568 |
"cell_type": "code",
|
| 569 |
"execution_count": null,
|
| 570 |
+
"id": "635f98ef",
|
| 571 |
"metadata": {},
|
| 572 |
"outputs": [],
|
| 573 |
"source": [
|
|
|
|
| 610 |
},
|
| 611 |
{
|
| 612 |
"cell_type": "markdown",
|
| 613 |
+
"id": "919af435",
|
| 614 |
"metadata": {},
|
| 615 |
"source": [
|
| 616 |
"### The 50 held-out instructions\n",
|
|
|
|
| 627 |
{
|
| 628 |
"cell_type": "code",
|
| 629 |
"execution_count": null,
|
| 630 |
+
"id": "5ae6e217",
|
| 631 |
"metadata": {},
|
| 632 |
"outputs": [],
|
| 633 |
"source": [
|
|
|
|
| 661 |
},
|
| 662 |
{
|
| 663 |
"cell_type": "markdown",
|
| 664 |
+
"id": "909c2518",
|
| 665 |
"metadata": {},
|
| 666 |
"source": [
|
| 667 |
"### The leakage check\n",
|
|
|
|
| 673 |
{
|
| 674 |
"cell_type": "code",
|
| 675 |
"execution_count": null,
|
| 676 |
+
"id": "54129a4b",
|
| 677 |
"metadata": {},
|
| 678 |
"outputs": [],
|
| 679 |
"source": [
|
|
|
|
| 694 |
},
|
| 695 |
{
|
| 696 |
"cell_type": "markdown",
|
| 697 |
+
"id": "370cd871",
|
| 698 |
"metadata": {},
|
| 699 |
"source": [
|
| 700 |
"### A second, easier set: 50 familiar requests\n",
|
|
|
|
| 707 |
{
|
| 708 |
"cell_type": "code",
|
| 709 |
"execution_count": null,
|
| 710 |
+
"id": "b531f8d8",
|
| 711 |
"metadata": {},
|
| 712 |
"outputs": [],
|
| 713 |
"source": [
|
|
|
|
| 728 |
},
|
| 729 |
{
|
| 730 |
"cell_type": "markdown",
|
| 731 |
+
"id": "4ae028f1",
|
| 732 |
"metadata": {},
|
| 733 |
"source": [
|
| 734 |
"## 3. The chat template and two new tokens\n",
|
|
|
|
| 737 |
"\n",
|
| 738 |
"`<|endoftext|>` `<|instruction|>` *the request* `<|story|>` *the story* `<|endoftext|>`\n",
|
| 739 |
"\n",
|
| 740 |
+
"`<|instruction|>` and `<|story|>` are Ella's two signs. We add them to the tokenizer as\n",
|
| 741 |
"**special tokens**, so the biter never splits them and a request can never accidentally contain\n",
|
| 742 |
"them.\n",
|
| 743 |
"\n",
|
| 744 |
"**Why the embedding must grow.** Momo's meaning map (the embedding table) has exactly one row per\n",
|
| 745 |
+
"token: 4,096 rows. The new signs get ids 4,096 and 4,097, rows that do not exist, so looking them up\n",
|
| 746 |
"would crash. We add two rows. In this model the scoring tray at the top is the *same* matrix (weight\n",
|
| 747 |
"tying, from Lesson 01), so it grows too. The old rows are copied unchanged; the new rows start at\n",
|
| 748 |
"the average of all the old rows, a neutral spot on the map that training then moves."
|
|
|
|
| 751 |
{
|
| 752 |
"cell_type": "code",
|
| 753 |
"execution_count": null,
|
| 754 |
+
"id": "e342cd17",
|
| 755 |
"metadata": {},
|
| 756 |
"outputs": [],
|
| 757 |
"source": [
|
|
|
|
| 785 |
},
|
| 786 |
{
|
| 787 |
"cell_type": "markdown",
|
| 788 |
+
"id": "ac010258",
|
| 789 |
"metadata": {},
|
| 790 |
"source": [
|
| 791 |
"## 4. Prompt masking\n",
|
|
|
|
| 794 |
"mask, Ella would also grade the guesses *inside the request*: Momo would be trained to write\n",
|
| 795 |
"requests. With the mask, the labels for those positions are `-100`, and PyTorch's cross-entropy\n",
|
| 796 |
"skips any label equal to -100 (its `ignore_index`). The first graded guess is the first word of the\n",
|
| 797 |
+
"story, made while Momo is looking at the `<|story|>` sign.\n",
|
| 798 |
"\n",
|
| 799 |
"Click between the two modes below. The tokens are the real tokens of one training pair."
|
| 800 |
]
|
|
|
|
| 802 |
{
|
| 803 |
"cell_type": "code",
|
| 804 |
"execution_count": null,
|
| 805 |
+
"id": "1ad96706",
|
| 806 |
"metadata": {},
|
| 807 |
"outputs": [],
|
| 808 |
"source": [
|
| 809 |
"# Interactive: which tokens are graded, with and without the prompt mask.\n",
|
| 810 |
"from IPython.display import HTML, display\n",
|
| 811 |
"display(HTML(r\"\"\"<div id=\"l5m\" style=\"font-family:-apple-system,Segoe UI,Helvetica,Arial,sans-serif;color:#e8e8e8;background:#1e1e1e;border-radius:12px;padding:16px;max-width:900px;box-sizing:border-box\">\n",
|
| 812 |
+
"<style>#l5m button{background:#333;color:#eee;border:1px solid #777;border-radius:6px;padding:6px 12px;margin:0 6px 6px 0;cursor:pointer}#l5m button.l5m-on{border-color:#ffb347;background:#3a2e1a}#l5m .l5m-t{display:inline-block;margin:2px;padding:3px 5px;border-radius:5px;font-size:12px;font-family:Menlo,Consolas,monospace;border:1px solid #444;white-space:pre}#l5mToks{max-height:260px}@media (max-width:520px){#l5mToks{max-height:560px}}#l5m .l5m-cap{font-size:13px;line-height:1.5;margin-top:8px;background:#262626;border-radius:8px;padding:10px}</style>\n",
|
| 813 |
"<div style=\"font-size:15px;font-weight:600;margin-bottom:6px\">Prompt masking: which tokens is Momo graded on? These are the real tokens of one training pair.</div>\n",
|
| 814 |
"<div><button id=\"l5mOff\">No mask: grade every token</button><button id=\"l5mOn\" class=\"l5m-on\">Mask the prompt</button></div>\n",
|
| 815 |
+
"<div id=\"l5mToks\" style=\"background:#262626;border-radius:8px;padding:8px;line-height:1.9;overflow-y:auto\"></div>\n",
|
| 816 |
"<div class=\"l5m-cap\" id=\"l5mCap\"></div>\n",
|
| 817 |
"</div>\n",
|
| 818 |
"<script>\n",
|
|
|
|
| 828 |
" const bg=counts?(isPrompt?'#4a3a1a':'#1f3d2c'):'#2b2b2b',fg=counts?'#f1eee4':'#777',bd=special?'#ffb347':'#444';\n",
|
| 829 |
" h+='<span class=\"l5m-t\" style=\"background:'+bg+';color:'+fg+';border-color:'+bd+'\" title=\"token '+i+'\">'+esc(t)+'</span>';});\n",
|
| 830 |
" box.innerHTML=h;\n",
|
| 831 |
+
" cap.innerHTML=(masked?'<b>Masked.</b> Grey tokens are Ella\\'s request: their labels are set to -100, so cross-entropy skips them. Momo is graded on <b>'+graded+'</b> of '+(D.tokens.length-1)+' next-token guesses, the story and the end marker, starting with the first word after the <|story|> sign.':'<b>No mask.</b> Momo is also graded on guessing Ella\\'s request word by word. That teaches him to <i>write instructions</i>, which is not his job, and the request\\'s words take a share of every lesson. Graded guesses: <b>'+graded+'</b> of '+(D.tokens.length-1)+'.')+'<br>Orange borders mark the special tokens added in this lesson. Each box is one token; a leading space is shown as a space.';}\n",
|
| 832 |
"bOn.addEventListener('click',function(){draw(true);});bOff.addEventListener('click',function(){draw(false);});draw(true);\n",
|
| 833 |
"})();\n",
|
| 834 |
"</script>\n",
|
|
|
|
| 837 |
},
|
| 838 |
{
|
| 839 |
"cell_type": "markdown",
|
| 840 |
+
"id": "9a63992c",
|
| 841 |
"metadata": {},
|
| 842 |
"source": [
|
| 843 |
"## 5. The check, before tuning\n",
|
|
|
|
| 847 |
"character's name, and does it contain the exact place phrase, such as \"by the watering hole\"\n",
|
| 848 |
"(ignoring capital letters)? The score is the share of the 50 where **both** are true.\n",
|
| 849 |
"\n",
|
| 850 |
+
"Before tuning, tiny-storyteller-v2 has no signs, so it gets the instruction as plain text, as if it\n",
|
| 851 |
"were the first line of a story, and we score only what it writes after it."
|
| 852 |
]
|
| 853 |
},
|
| 854 |
{
|
| 855 |
"cell_type": "code",
|
| 856 |
"execution_count": null,
|
| 857 |
+
"id": "2f2b15d3",
|
| 858 |
"metadata": {},
|
| 859 |
"outputs": [],
|
| 860 |
"source": [
|
|
|
|
| 888 |
},
|
| 889 |
{
|
| 890 |
"cell_type": "markdown",
|
| 891 |
+
"id": "6d0f729a",
|
| 892 |
"metadata": {},
|
| 893 |
"source": [
|
| 894 |
"## 6. Instruction tuning: 500 steps\n",
|
|
|
|
| 905 |
{
|
| 906 |
"cell_type": "code",
|
| 907 |
"execution_count": null,
|
| 908 |
+
"id": "e3535d33",
|
| 909 |
"metadata": {},
|
| 910 |
"outputs": [],
|
| 911 |
"source": [
|
|
|
|
| 982 |
},
|
| 983 |
{
|
| 984 |
"cell_type": "markdown",
|
| 985 |
+
"id": "08f06293",
|
| 986 |
"metadata": {},
|
| 987 |
"source": [
|
| 988 |
"## 7. The check, after tuning\n",
|
|
|
|
| 992 |
{
|
| 993 |
"cell_type": "code",
|
| 994 |
"execution_count": null,
|
| 995 |
+
"id": "f213fb0a",
|
| 996 |
"metadata": {},
|
| 997 |
"outputs": [],
|
| 998 |
"source": [
|
|
|
|
| 1008 |
},
|
| 1009 |
{
|
| 1010 |
"cell_type": "markdown",
|
| 1011 |
+
"id": "74aa84ec",
|
| 1012 |
"metadata": {},
|
| 1013 |
"source": [
|
| 1014 |
"### A harder test: places from the other world\n",
|
|
|
|
| 1020 |
{
|
| 1021 |
"cell_type": "code",
|
| 1022 |
"execution_count": null,
|
| 1023 |
+
"id": "b208c4da",
|
| 1024 |
"metadata": {},
|
| 1025 |
"outputs": [],
|
| 1026 |
"source": [
|
|
|
|
| 1042 |
{
|
| 1043 |
"cell_type": "code",
|
| 1044 |
"execution_count": null,
|
| 1045 |
+
"id": "077499a8",
|
| 1046 |
"metadata": {},
|
| 1047 |
"outputs": [],
|
| 1048 |
"source": [
|
|
|
|
| 1053 |
},
|
| 1054 |
{
|
| 1055 |
"cell_type": "markdown",
|
| 1056 |
+
"id": "a72ed66c",
|
| 1057 |
"metadata": {},
|
| 1058 |
"source": [
|
| 1059 |
"**What this means, honestly.** (Numbers are from our run on the Mac; yours may differ by a few\n",
|
|
|
|
| 1073 |
},
|
| 1074 |
{
|
| 1075 |
"cell_type": "markdown",
|
| 1076 |
+
"id": "608a7ad2",
|
| 1077 |
"metadata": {},
|
| 1078 |
"source": [
|
| 1079 |
"## 8. Save tiny-storyteller-instruct and the pairs\n",
|
|
|
|
| 1085 |
{
|
| 1086 |
"cell_type": "code",
|
| 1087 |
"execution_count": null,
|
| 1088 |
+
"id": "e2c59c4e",
|
| 1089 |
"metadata": {},
|
| 1090 |
"outputs": [],
|
| 1091 |
"source": [
|
|
|
|
| 1124 |
{
|
| 1125 |
"cell_type": "code",
|
| 1126 |
"execution_count": null,
|
| 1127 |
+
"id": "eaeeec53",
|
| 1128 |
"metadata": {},
|
| 1129 |
"outputs": [],
|
| 1130 |
"source": [
|
|
|
|
| 1144 |
},
|
| 1145 |
{
|
| 1146 |
"cell_type": "markdown",
|
| 1147 |
+
"id": "390405df",
|
| 1148 |
"metadata": {},
|
| 1149 |
"source": [
|
| 1150 |
"## What we learned\n",
|