File size: 38,165 Bytes
fb24f54 b74a784 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 b74a784 f747ce5 fb24f54 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 b74a784 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 f747ce5 fb24f54 b74a784 fb24f54 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 f747ce5 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 b74a784 fb24f54 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Learn GPT from scratch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"if not os.path.isfile(\"./datasets/corpora/shakespeare.txt\"):\n",
" !wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt -O datasets/corpora/shakespeare.txt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"with open(\"datasets/corpora/shakespeare.txt\", 'r', encoding='utf-8') as f:\n",
" text = f.read()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Putting hyperparameters at the top because I learned this the hard way\n",
"# 64 * NUM_HEADS\n",
"EMBEDDING_NDIM=256\n",
"VOCAB_SIZE=128\n",
"BATCH_SIZE=64\n",
"# \"Context window\"\n",
"BLOCK_SIZE=256"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tokenization and dataset creation"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#%pip install torch pandas numpy tensorboard gradio"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"from torch.optim import lr_scheduler\n",
"import torch.nn.functional as F\n",
"from torch.utils.data import Dataset, DataLoader, TensorDataset, random_split\n",
"import pandas as pd\n",
"import numpy as np\n",
"import math\n",
"\n",
"torch.manual_seed(1337)\n",
"# Set device to CUDA if available, otherwise use CPU\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Simple dumb ASCII character-level \"encoding\" since all training data is ASCII\n",
"def encode_text(text):\n",
" return([ord(t) for t in text])\n",
"\n",
"def decode_text(indices):\n",
" return([chr(x) for x in indices])\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1115394 chars of data\n"
]
}
],
"source": [
"# Tensorify data, put it in dataset\n",
"data = torch.tensor(encode_text(text), dtype=torch.int32)\n",
"\n",
"test_split_idx = int(0.8 * len(data))\n",
"val_split_idx = int(0.9 * len(data))\n",
"train_data = data[:test_split_idx]\n",
"test_data = data[test_split_idx:val_split_idx]\n",
"val_data = data[val_split_idx:]\n",
"print(f\"{len(data)} chars of data\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We have to make a custom PyTorch dataset class to automatically generate the \"context\" windows at load time. This allows us to avoid keeping these windows around in memory when not in use:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"class TextDataset(Dataset):\n",
" def __init__(self, data_tensor, context_size):\n",
" self.data_tensor = data_tensor\n",
" self.context_size = context_size\n",
" \n",
" def __len__(self):\n",
" return len(self.data_tensor) - self.context_size\n",
"\n",
" def __getitem__(self, index):\n",
" x = self.data_tensor[index:index + self.context_size]\n",
" y = self.data_tensor[index + 1:index + self.context_size + 1]\n",
" \n",
" return x, y"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attention is all you need (注目こそが必要なすべて)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class MultiheadAttention(nn.Module):\n",
" def __init__(self, embed_dim, num_heads, dropout=0.0, bias=True, device=None, dtype=None):\n",
" super(MultiheadAttention, self).__init__()\n",
"\n",
" # Save variables\n",
" self.embed_dim = embed_dim\n",
" self.num_heads = num_heads\n",
" self.d_k = embed_dim // num_heads\n",
"\n",
" self.Q = nn.Linear(embed_dim, embed_dim, bias=False)\n",
" self.K = nn.Linear(embed_dim, embed_dim, bias=False)\n",
" self.V = nn.Linear(embed_dim, embed_dim, bias=False)\n",
"\n",
" self.dropout = nn.Dropout(dropout)\n",
" self.out_proj = nn.Linear(embed_dim, embed_dim, bias=False)\n",
"\n",
" def forward(self, query, key, value, attn_mask=None):\n",
" batch_size = query.size(0)\n",
"\n",
" # Apply linear layers\n",
" q = self.Q(query) # [B, C, E]\n",
" k = self.K(key) # [B, C, E]\n",
" v = self.V(value) # [B, C, E]\n",
"\n",
" # Mutate dimensions so the attention matmul can get rid of the inner d_k\n",
" q = q.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) # [batch_size, num_heads, C, d_k]\n",
" k = k.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) # [batch_size, num_heads, C, d_k]\n",
" v = v.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) # [batch_size, num_heads, C, d_k]\n",
" \n",
" # Get raw attention scores\n",
" scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k) # [B, num_heads, C, C]\n",
"\n",
" # Apply mask, if necessary\n",
" if attn_mask is not None:\n",
" \"\"\"\n",
" MAY BE WORTH DEBUGGING\n",
"\n",
" if key_padding_mask.dim() == 3:\n",
" # If the mask is 3D, add an extra dimension for the num_heads\n",
" key_padding_mask = key_padding_mask.unsqueeze(1) # [batch_size, 1, seq_len, seq_len]\n",
" else:\n",
" # If the mask is 2D, add dimensions for the num_heads and the 'query' sequence length\n",
" key_padding_mask = key_padding_mask.unsqueeze(1).unsqueeze(2) # [batch_size, 1, 1, seq_len]\n",
" \"\"\"\n",
" # Apply the mask to attention scores\n",
" scores = scores.masked_fill(attn_mask, float('-inf'))\n",
"\n",
" # Scale by sqrt(k)\n",
" attn = F.softmax(scores, dim=-1)\n",
" attn = self.dropout(attn)\n",
" out = attn @ v # [B, num_heads, C, d_k]\n",
"\n",
" # Concat and project\n",
" # Swap C and num_heads, force memory to coalesce, then fuse back num_heads and d_k together\n",
" out = out.transpose(1, 2).contiguous().view(batch_size, -1, self.embed_dim)\n",
" # Project: give attention \"time to think\". Maybe this should be part of a different module but whatever\n",
" out = self.out_proj(out)\n",
" return((out, None))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"class FeedForward(nn.Module):\n",
" def __init__(self, embed_dim, dropout):\n",
" super().__init__()\n",
" self.net = nn.Sequential(\n",
" nn.Linear(embed_dim, 4 * embed_dim),\n",
" nn.GELU(),\n",
" nn.Linear(4 * embed_dim, embed_dim),\n",
" nn.Dropout(dropout),\n",
" )\n",
"\n",
" def forward(self, x):\n",
" return(self.net(x))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"class Block(nn.Module):\n",
" \"\"\"Self-attention\"\"\"\n",
" def __init__(self, embed_dim, num_heads, mask, dropout=0.2):\n",
" super(Block, self).__init__() \n",
" self.register_buffer(\"mask\", mask)\n",
" self.head = MultiheadAttention(embed_dim=embed_dim, num_heads=num_heads, dropout=dropout)\n",
" #self.head = nn.MultiheadAttention(embed_dim=embed_dim, num_heads=num_heads, dropout=dropout, batch_first=True)\n",
" self.ffwd = FeedForward(embed_dim=embed_dim, dropout=dropout)\n",
" self.ln1 = nn.LayerNorm(embed_dim)\n",
" self.ln2 = nn.LayerNorm(embed_dim)\n",
"\n",
" def forward(self, x):\n",
" # Residual connections\n",
" x = self.ln1(x)\n",
" attn_output, _ = self.head(x, x, x, attn_mask=self.mask) \n",
" x = x + attn_output\n",
" out = x + self.ffwd(self.ln2(x))\n",
" return out\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"class GPT(nn.Module):\n",
" def __init__(self, embedding_dim, vocab_size, context_size):\n",
" super(GPT, self).__init__()\n",
"\n",
" self.embedding_dim = embedding_dim\n",
" self.output_dim = vocab_size\n",
" self.context_size = context_size\n",
"\n",
" NUM_HEADS=4\n",
" NUM_LAYERS=4\n",
" \n",
" # Initialize layers\n",
" self.tok_embed = nn.Embedding(vocab_size, embedding_dim)\n",
" self.pos_embed = nn.Embedding(context_size, embedding_dim)\n",
"\n",
" mask = torch.tril(torch.ones(self.context_size, self.context_size)).bool()\n",
" mask = ~mask\n",
" self.register_buffer(\"mask\", mask)\n",
"\n",
" self.blocks = nn.Sequential(\n",
" *[Block(embed_dim=embedding_dim, num_heads=NUM_HEADS, mask=mask, dropout=0.2) for _ in range(NUM_LAYERS)]\n",
" )\n",
"\n",
" self.ln_f = nn.LayerNorm(self.embedding_dim)\n",
" # Final feed-forward layer from embeddings\n",
" self.ffwd = nn.Linear(embedding_dim, out_features=vocab_size, bias=False)\n",
"\n",
" def forward(self, x):\n",
" tok_embed = self.tok_embed(x)\n",
" pos_embed = self.pos_embed(\n",
" torch.arange(0, self.context_size, device=\"cuda\")\n",
" )\n",
" x = tok_embed + pos_embed\n",
"\n",
" x = self.blocks(x)\n",
" x = self.ln_f(x)\n",
"\n",
" logits = self.ffwd(x)\n",
" return(logits)\n",
" \n",
" def infer(self, x):\n",
" with torch.no_grad():\n",
" res = self.forward(x)\n",
" return(res)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Training"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def compute_loss(model, criterion, x, y):\n",
" logits = model(x)\n",
" B,C,V = logits.shape\n",
" logits = logits.view(B*C, V)\n",
" y = y.view(B*C)\n",
" loss = F.cross_entropy(logits, y.long())\n",
" return loss"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"LR=3e-4\n",
"\n",
"train_dataset = TextDataset(train_data, BLOCK_SIZE)\n",
"test_dataset = TextDataset(test_data, BLOCK_SIZE)\n",
"\n",
"# Janky training code\n",
"model = GPT(\n",
" embedding_dim=EMBEDDING_NDIM, \n",
" vocab_size=VOCAB_SIZE,\n",
" context_size=BLOCK_SIZE,\n",
" )\n",
"\n",
"model = model.to(device)\n",
"optimizer = optim.AdamW(model.parameters(), lr=LR)\n",
"#scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1000, gamma=0.1)\n",
"criterion = F.cross_entropy\n",
"\n",
"global_step = 0"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 0; loss: 4.62758731842041\n",
"Step 100; loss: 2.5372843742370605\n",
"Step 200; loss: 2.486722946166992\n",
"Step 300; loss: 2.3916263580322266\n",
"Step 400; loss: 2.269087314605713\n",
"Step 500; loss: 2.1484358310699463\n",
"Step 600; loss: 2.057586193084717\n",
"Step 700; loss: 1.9845455884933472\n",
"Step 800; loss: 1.910020351409912\n",
"Step 900; loss: 1.8550803661346436\n",
"Step 1000; loss: 1.8193731307983398\n",
"Step 1100; loss: 1.767741322517395\n",
"Step 1200; loss: 1.7612113952636719\n",
"Step 1300; loss: 1.7009034156799316\n",
"Step 1400; loss: 1.6827564239501953\n",
"Step 1500; loss: 1.6604313850402832\n",
"Step 1600; loss: 1.633068323135376\n",
"Step 1700; loss: 1.6335963010787964\n",
"Step 1800; loss: 1.6095472574234009\n",
"Step 1900; loss: 1.6086715459823608\n",
"Step 2000; loss: 1.5876469612121582\n",
"Step 2100; loss: 1.5713247060775757\n",
"Step 2200; loss: 1.5546257495880127\n",
"Step 2300; loss: 1.5589814186096191\n",
"Step 2400; loss: 1.5507397651672363\n",
"Step 2500; loss: 1.5470337867736816\n",
"Step 2600; loss: 1.547551155090332\n",
"Step 2700; loss: 1.5338884592056274\n",
"Step 2800; loss: 1.5179914236068726\n",
"Step 2900; loss: 1.5240544080734253\n",
"Step 3000; loss: 1.5162924528121948\n",
"Step 3100; loss: 1.5197933912277222\n",
"Step 3200; loss: 1.5107413530349731\n",
"Step 3300; loss: 1.5017006397247314\n",
"Step 3400; loss: 1.4874128103256226\n",
"Step 3500; loss: 1.4917751550674438\n",
"Step 3600; loss: 1.5251762866973877\n",
"Step 3700; loss: 1.4957225322723389\n",
"Step 3800; loss: 1.507473111152649\n",
"Step 3900; loss: 1.4815101623535156\n",
"Step 4000; loss: 1.4824676513671875\n",
"Step 4100; loss: 1.4799575805664062\n",
"Step 4200; loss: 1.4820805788040161\n",
"Step 4300; loss: 1.4852553606033325\n",
"Step 4400; loss: 1.469815731048584\n",
"Step 4500; loss: 1.4853312969207764\n",
"Step 4600; loss: 1.4830256700515747\n",
"Step 4700; loss: 1.468559741973877\n",
"Step 4800; loss: 1.4680243730545044\n",
"Step 4900; loss: 1.464580774307251\n"
]
}
],
"source": [
"from torch.utils.tensorboard import SummaryWriter\n",
"\n",
"EPOCHS = 1\n",
"STEPS = 5000\n",
"VAL_INTERVAL = 100\n",
"\n",
"model.train()\n",
"\n",
"train_dataloader = DataLoader(\n",
" train_dataset, \n",
" batch_size=BATCH_SIZE, \n",
" shuffle=True, \n",
" num_workers=4\n",
")\n",
"\n",
"test_dataloader = DataLoader(test_dataset, batch_size=512, num_workers=4, shuffle=True)\n",
"\n",
"writer = SummaryWriter()\n",
"\n",
"step = 0\n",
"\n",
"for epoch in range(EPOCHS):\n",
" for data, target in train_dataloader:\n",
" data = data.to(device)\n",
" target = target.to(device)\n",
"\n",
" loss = compute_loss(model, criterion, data, target)\n",
"\n",
" # Backward pass\n",
" optimizer.zero_grad()\n",
" loss.backward()\n",
" optimizer.step()\n",
" #scheduler.step()\n",
"\n",
" writer.add_scalar(\"Loss/train\", loss.cpu().detach().numpy(), global_step)\n",
" global_step += 1\n",
"\n",
" # TODO!!! WTF???\n",
" if step % VAL_INTERVAL == 0:\n",
" total_loss = 0\n",
" total_samples = 0\n",
"\n",
" with torch.no_grad():\n",
" model.eval()\n",
" for x, y in test_dataloader:\n",
" x = x.to(device)\n",
" y = y.to(device)\n",
"\n",
" batch_loss = compute_loss(model, criterion, x, y)\n",
" total_loss += batch_loss.item() * 512\n",
" total_samples += 512\n",
" if total_samples > 10:\n",
" break\n",
"\n",
" model.train()\n",
" average_loss = total_loss / total_samples\n",
"\n",
" print(f\"Step {step}; loss: {average_loss}\")\n",
" writer.add_scalar(\"Loss/val\", average_loss, global_step)\n",
"\n",
"\n",
" step += 1\n",
" if step >= STEPS:\n",
" break\n",
"\n",
"writer.close()\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"PATH = \"checkpoints/model.pt\""
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Store\n",
"torch.save({\n",
" 'steps': step,\n",
" 'model_state_dict': model.state_dict(),\n",
" 'optimizer_state_dict': optimizer.state_dict(),\n",
"}, PATH)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"checkpoint = torch.load(PATH)\n",
"model.load_state_dict(checkpoint['model_state_dict'])\n",
"optimizer.load_state_dict(checkpoint['optimizer_state_dict'])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we test for overfitting:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import gc\n",
"gc.collect()"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.7774584962397206\n"
]
}
],
"source": [
"model.eval()\n",
"total_loss = 0.0\n",
"total_samples = 0\n",
"\n",
"val_dataset = TextDataset(val_data, BLOCK_SIZE)\n",
"val_dataloader = DataLoader(val_dataset, batch_size=512, num_workers=4)\n",
"with torch.no_grad():\n",
" for x, y in val_dataloader:\n",
" x = x.to(device)\n",
" y = y.to(device)\n",
"\n",
" batch_loss = compute_loss(model, criterion, x, y)\n",
" total_loss += batch_loss.item() * x.size(0)\n",
" total_samples += x.size(0)\n",
" if total_samples > 100000:\n",
" break\n",
"\n",
" average_loss = total_loss / total_samples\n",
" print(average_loss)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3286528"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
"num_params"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we generate. NOTE: seeds shorter than 256 chars have nonsense until you reach the context window. I think it's because Karpathy jammed the whole Shakespeare into one file with no act/scene breaks and both he and I didn't split it, so there's only one padding that the model sees, ever. TODO: fix this in the data loading step"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Shepherd:\n",
"What should be gone to your deeds to prince\n",
"That let us away the house of Lancasters.\n",
"\n",
"Second Murderer:\n",
"Alas, if I be not the fire rogued man\n",
"That hath dead by the fault of the good stars;\n",
"And the business of some many that be sounded.\n",
"\n",
"AUTOLYCUS:\n",
"My soul are but not so dishonours.\n",
"\n",
"BRUTUS:\n",
"I will do not be point, but it is a soul,\n",
"I would I see my heart fair state,\n",
"That he is in the grief and be contented\n",
"That you may made the father, the day doth nothing\n",
"And lie at like in his world moved me at him.\n",
"\n",
"First Senator:\n",
"The rotten far the points of our honour.\n",
"\n",
"AUTOLYCUS:\n",
"The king hath been you think the duke of the foes,\n",
"And had so long and confessal abroad,\n",
"And said in the light save of men so meeting\n",
"May not shall do be as we as here.\n",
"\n",
"Second Servingman:\n",
"You are grace as the child and fight again,\n",
"If you do not shall not be much of the thing;\n",
"One but so the creature of the charge is were\n",
"Than the point of your several motion\n",
"May seem as her like a pride and as would all grave\n",
"The which of the blood"
]
},
{
"data": {
"text/plain": [
"'\\nPlot histograms of the gradient values during training. If you notice a significant number of gradients are near zero (vanishing gradients) or very large values (exploding gradients), it could be a problem. TensorBoard is a useful tool for visualizing these histograms.\\n\\nShepherd:\\nWhat should be gone to your deeds to prince\\nThat let us away the house of Lancasters.\\n\\nSecond Murderer:\\nAlas, if I be not the fire rogued man\\nThat hath dead by the fault of the good stars;\\nAnd the business of some many that be sounded.\\n\\nAUTOLYCUS:\\nMy soul are but not so dishonours.\\n\\nBRUTUS:\\nI will do not be point, but it is a soul,\\nI would I see my heart fair state,\\nThat he is in the grief and be contented\\nThat you may made the father, the day doth nothing\\nAnd lie at like in his world moved me at him.\\n\\nFirst Senator:\\nThe rotten far the points of our honour.\\n\\nAUTOLYCUS:\\nThe king hath been you think the duke of the foes,\\nAnd had so long and confessal abroad,\\nAnd said in the light save of men so meeting\\nMay not shall do be as we as here.\\n\\nSecond Servingman:\\nYou are grace as the child and fight again,\\nIf you do not shall not be much of the thing;\\nOne but so the creature of the charge is were\\nThan the point of your several motion\\nMay seem as her like a pride and as would all grave\\nThe which of the blood'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"seed = \"\"\"\n",
"Plot histograms of the gradient values during training. If you notice a significant number of gradients are near zero (vanishing gradients) or very large values (exploding gradients), it could be a problem. TensorBoard is a useful tool for visualizing these histograms.\n",
"\"\"\"\n",
"GEN_LENGTH=1024\n",
"\n",
"def generate(prompt, gen_length, temp=1, top_k=10, top_p=None):\n",
" g_cuda = torch.Generator(device=device)\n",
" contexts = torch.tensor(encode_text(prompt), dtype=torch.int32).to(device)\n",
"\n",
" model.eval()\n",
" for i in range(GEN_LENGTH):\n",
" transform = nn.LogSoftmax(1)\n",
" x = contexts[-BLOCK_SIZE:]\n",
" if x.size(0) < BLOCK_SIZE:\n",
" x = F.pad(x, (BLOCK_SIZE - x.size(0), 0), \"constant\", 0).unsqueeze(0) # B*T\n",
" else:\n",
" x = x.unsqueeze(0)\n",
"\n",
" preds = model.infer(x)\n",
" preds = preds.squeeze(0)\n",
" preds = preds / temp\n",
" probs = F.softmax(preds, dim=-1)\n",
"\n",
" if top_p is not None:\n",
" # Apply top-p\n",
" sorted_probs, sorted_indices = torch.sort(probs[-1, :], descending=True)\n",
" cumulative_probs = torch.cumsum(sorted_probs, dim=-1)\n",
" # find cutoff\n",
" idx_top_p = (cumulative_probs < top_p).sum().item()\n",
" top_probs = sorted_probs[:idx_top_p]\n",
" top_indices = sorted_indices[:idx_top_p]\n",
" # Null case\n",
" if top_probs.size(0) == 0:\n",
" top_probs = sorted_probs[:1]\n",
" top_indices = sorted_indices[:1]\n",
" \n",
" next_char = torch.multinomial(top_probs, num_samples=1, generator=g_cuda)\n",
" next_char = top_indices[next_char]\n",
" elif top_k is not None:\n",
" top_k_probs, top_k_indices = torch.topk(probs[-1, :], k=top_k)\n",
" next_char = torch.multinomial(top_k_probs, num_samples=1, generator=g_cuda)\n",
" next_char = top_k_indices[next_char]\n",
" else:\n",
" next_char = torch.multinomial(probs, num_samples=1, generator=g_cuda)\n",
"\n",
"\n",
" contexts = torch.cat((contexts, next_char), dim=0)\n",
" print(decode_text(next_char.cpu().numpy())[-1], end=\"\")\n",
" \n",
" return(\"\".join(decode_text(contexts.cpu().numpy())))\n",
"\n",
"\"\".join(generate(seed, GEN_LENGTH,temp=0.8,top_p=0.9))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Gradio WebUI"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7866\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"If you do I am sure be your father's limbs,\n",
"Which is not drouble Paulina,\n",
"Where I come to me too much be to thus\n",
"As in a mind sorten but here hoped--followed as hung\n",
"bard at the time own that that he is death;\n",
"The stir weors shall be a new good groans.\n",
"\n",
"OXFORD:\n",
"And I say him to deed your city,\n",
"Thy heart advantage by mine at their world,\n",
"And be glood much show with upon the suddenly,\n",
"And in this free can company from here faults.\n",
"\n",
"YORK:\n",
"I bring thee, now I am again: it is a Montague.\n",
"\n",
"BRUTUS:\n",
"We while you not be the fault of the life.\n",
"\n",
"KING RICHARD III:\n",
"Come, sir, do I: in put I with so,\n",
"The rest redies with this ballad or suppher.\n",
"\n",
"Third Servingman:\n",
"Now, well do with kindness of power the that\n",
"ence of the ambassage; but to shall here,\n",
"But saving as the wounds both drawn dead.\n",
"\n",
"CLARENCE:\n",
"Peace, sir, he adieu.\n",
"\n",
"MISTRESS OVERDONE:\n",
"Cheep, comes thou lovest which I do well;\n",
"Now now as that protectors, in this parliament--\n",
"That nature i' the mark of her with old her,\n",
"And so here doth in circle and call'd the wind,\n",
"If you do I am sure be your father's limbs,\n",
"Which is not drouble Paulina,\n",
"Where I come to me too much be to thus\n",
"As in a mind sorten but here hoped--followed as hung\n",
"bard at the time own that that he is death;\n",
"The stir weors shall be a new good groans.\n",
"\n",
"OXFORD:\n",
"And I say him to deed your city,\n",
"Thy heart advantage by mine at their world,\n",
"And be glood much show with upon the suddenly,\n",
"And in this free can company from here faults.\n",
"\n",
"YORK:\n",
"I bring thee, now I am again: it is a Montague.\n",
"\n",
"BRUTUS:\n",
"We while you not be the fault of the life.\n",
"\n",
"KING RICHARD III:\n",
"Come, sir, do I: in put I with so,\n",
"The rest redies with this ballad or suppher.\n",
"\n",
"Third Servingman:\n",
"Now, well do with kindness of power the that\n",
"ence of the ambassage; but to shall here,\n",
"But saving as the wounds both drawn dead.\n",
"\n",
"CLARENCE:\n",
"Peace, sir, he adieu.\n",
"\n",
"MISTRESS OVERDONE:\n",
"Cheep, comes thou lovest which I do well;\n",
"Now now as that protectors, in this parliament--\n",
"That nature i' the mark of her with old her,\n",
"And so here doth in circle and call'd the wind,\n",
"If you do I am sure be your father's limbs,\n",
"Which is not drouble Paulina,\n",
"Where I come to me too much be to thus\n",
"As in a mind sorten but here hoped--followed as hung\n",
"bard at the time own that that he is death;\n",
"The stir weors shall be a new good groans.\n",
"\n",
"OXFORD:\n",
"And I say him to deed your city,\n",
"Thy heart advantage by mine at their world,\n",
"And be glood much show with upon the suddenly,\n",
"And in this free can company from here faults.\n",
"\n",
"YORK:\n",
"I bring thee, now I am again: it is a Montague.\n",
"\n",
"BRUTUS:\n",
"We while you not be the fault of the life.\n",
"\n",
"KING RICHARD III:\n",
"Come, sir, do I: in put I with so,\n",
"The rest redies with this ballad or suppher.\n",
"\n",
"Third Servingman:\n",
"Now, well do with kindness of power the that\n",
"ence of the ambassage; but to shall here,\n",
"But saving as the wounds both drawn dead.\n",
"\n",
"CLARENCE:\n",
"Peace, sir, he adieu.\n",
"\n",
"MISTRESS OVERDONE:\n",
"Cheep, comes thou lovest which I do well;\n",
"Now now as that protectors, in this parliament--\n",
"That nature i' the mark of her with old her,\n",
"And so here doth in circle and call'd the wind,\n",
"If you do I am sure be your father's limbs,\n",
"Which is not drouble Paulina,\n",
"Where I come to me too much be to thus\n",
"As in a mind sorten but here hoped--followed as hung\n",
"bard at the time own that that he is death;\n",
"The stir weors shall be a new good groans.\n",
"\n",
"OXFORD:\n",
"And I say him to deed your city,\n",
"Thy heart advantage by mine at their world,\n",
"And be glood much show with upon the suddenly,\n",
"And in this free can company from here faults.\n",
"\n",
"YORK:\n",
"I bring thee, now I am again: it is a Montague.\n",
"\n",
"BRUTUS:\n",
"We while you not be the fault of the life.\n",
"\n",
"KING RICHARD III:\n",
"Come, sir, do I: in put I with so,\n",
"The rest redies with this ballad or suppher.\n",
"\n",
"Third Servingman:\n",
"Now, well do with kindness of power the that\n",
"ence of the ambassage; but to shall here,\n",
"But saving as the wounds both drawn dead.\n",
"\n",
"CLARENCE:\n",
"Peace, sir, he adieu.\n",
"\n",
"MISTRESS OVERDONE:\n",
"Cheep, comes thou lovest which I do well;\n",
"Now now as that protectors, in this parliament--\n",
"That nature i' the mark of her with old her,\n",
"And so here doth in circle and call'd the wind,\n",
"If you do I am sure be your father's limbs,\n",
"Which is not drouble Paulina,\n",
"Where I come to me too much be to thus\n",
"As in a mind sorten but here hoped--followed as hung\n",
"bard at the time own that that he is death;\n",
"The stir weors shall be a new good groans.\n",
"\n",
"OXFORD:\n",
"And I say him to deed your city,\n",
"Thy heart advantage by mine at their world,\n",
"And be glood much show with upon the suddenly,\n",
"And in this free can company from here faults.\n",
"\n",
"YORK:\n",
"I bring thee, now I am again: it is a Montague.\n",
"\n",
"BRUTUS:\n",
"We while you not be the fault of the life.\n",
"\n",
"KING RICHARD III:\n",
"Come, sir, do I: in put I with so,\n",
"The rest redies with this ballad or suppher.\n",
"\n",
"Third Servingman:\n",
"Now, well do with kindness of power the that\n",
"ence of the ambassage; but to shall here,\n",
"But saving as the wounds both drawn dead.\n",
"\n",
"CLARENCE:\n",
"Peace, sir, he adieu.\n",
"\n",
"MISTRESS OVERDONE:\n",
"Cheep, comes thou lovest which I do well;\n",
"Now now as that protectors, in this parliament--\n",
"That nature i' the mark of her with old her,\n",
"And so here doth in circle and call'd the wind,\n",
"If you do I have some that to be blow.\n",
"\n",
"QUEEN ELIZABETH:\n",
"But shall we will not a man; for all one,\n",
"That you will not be but a fault and house,\n",
"I cannot strive at once from your father's son.\n",
"\n",
"GLOUCESTER:\n",
"\n",
"CLARENCE:\n",
"That is the field to be put in his name.\n",
"\n",
"KING RICHARD III:\n",
"Ay, sir.\n",
"\n",
"QUEEN ELIZABETH:\n",
"How farewell.\n",
"\n",
"POLIXENES:\n",
"She hath stand and better me for you.\n",
"\n",
"QUEEN ELIZABETH:\n",
"I will be patient me.\n",
"\n",
"CLARENCE:\n",
"The been are the lawful strength, and he made\n",
"That me was upon the which he comes with a holy face.\n",
"\n",
"BUCKINGHAM:\n",
"The noble and of my tent.\n",
"\n",
"GLOUCESTER:\n",
"My gracious lord, he did soul the news,\n",
"Which he had of pretty with the fair traitor\n",
"Than a service hath some said for his prince.\n",
"\n",
"QUEEN MARGARET:\n",
"Stay the sad is mine execution,\n",
"Which can slain with you have some so death;\n",
"For I have sent done the many of the city\n",
"That you more so much hold me speak.\n",
"\n",
"GLOUCESTER:\n",
"\n",
"GLOUCESTER:\n",
"So doth fair lord, that be straight in his wit\n",
"The matter, the city distance.\n",
"\n",
"LEONTES:\n",
"The brother about the fire--thou sha\n",
"If you do I am sure be so princely false;\n",
"And the prince may do me for the house.\n",
"\n",
"GLOUCESTER:\n",
"Why, what wilt me, when we strike a mine eyes;\n",
"Of he doth deficers, in my fair deeds,\n",
"That thou wert from me hence his pardon lives.\n",
"\n",
"LORD FITZWATER:\n",
"Say, I will be ground to some of my body;\n",
"And what thou wilt part so supposed with him.\n",
"\n",
"CLARENCE:\n",
"You might be breathe in me?\n",
"\n",
"CAMILLO:\n",
"Stay now the first is no more resolved\n",
"With all the lawful and shed the gates word.\n",
"\n",
"CLARENCE:\n",
"Thou that, when my fair that I would not for heaven,\n",
"That fortune but not like her for the mind,\n",
"When I do be my tongues and her love.\n",
"\n",
"ANGELO:\n",
"She shall we not have done by his house.\n",
"\n",
"HENRY BOLINGBROKE:\n",
"Have you no more by my fortune should as heard.\n",
"\n",
"QUEEN ELIZABETH:\n",
"O, bring forth thy brother Gloucester,\n",
"But both an earthly of love from spoke me.\n",
"\n",
"QUEEN ELIZABETH:\n",
"Thou art thou as you shalt be dish'd in their head.\n",
"\n",
"Second Murderer:\n",
"The prince and my execution and heard it.\n",
"\n",
"KING RICHARD III:\n",
"Say, well I am a house in my heart;\n",
"But I am"
]
}
],
"source": [
"import gradio as gr\n",
"\n",
"demo = gr.Interface(\n",
" fn=generate, \n",
" inputs=[\n",
" gr.Textbox(lines=2, placeholder=\"Prompt here...\"),\n",
" gr.Number(value=256),\n",
" gr.Number(value=0.8),\n",
" gr.Slider(maximum=128,value=10),\n",
" gr.Slider(maximum=1,value=1)\n",
" ], \n",
" outputs=\"text\",\n",
" title=\"Shakespeare-GPT\",\n",
" description=\"Putting theater kids out of their nonexistent jobs since 2023\"\n",
")\n",
"\n",
"demo.launch()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|