Build a modern LLM from scratch. Every line commented. Explained like we are five.
Python attention-mechanismdeep-learningeducationalfrom-scratchgptlanguage-modelllamallmmachine-learningnatural-language-processingpythonpytorchtokenisationtransformerstutorial
ignore jupyter checkpoints
raiyanyahya19d086a7ff2b
diff --git a/.gitignore b/.gitignorenew file mode 100644index 0000000..87620ac--- /dev/null+++ b/.gitignore@@ -0,0 +1 @@+.ipynb_checkpoints/diff --git a/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynbdeleted file mode 100644index f8663fb..0000000--- a/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynb+++ /dev/null@@ -1,146 +0,0 @@-{- "cells": [- {- "cell_type": "markdown",- "metadata": {},- "source": [- "# Chapter 2 — Tokenization\n",- "\n",- "Turning words into numbers using Byte Pair Encoding (BPE)."- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Imports"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "from dataclasses import dataclass\n",- "import tiktoken"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Configuration"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "@dataclass\n",- "class TokenizerConfig:\n",- " name: str = \"gpt2\"\n",- " vocab_size: int = 50257"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Tokenizer"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "class SimpleTokenizer:\n",- " def __init__(self, config=None):\n",- " self.config = config or TokenizerConfig()\n",- " self.enc = tiktoken.get_encoding(self.config.name)\n",- " self.eos_token = \"<|endoftext|>\"\n",- " self.eos_token_id = self.enc.encode(\n",- " self.eos_token, allowed_special={self.eos_token}\n",- " )[0]\n",- "\n",- " def encode(self, text):\n",- " return self.enc.encode(text, allowed_special={self.eos_token})\n",- "\n",- " def decode(self, ids):\n",- " return self.enc.decode(ids)\n",- "\n",- " @property\n",- " def vocab_size(self):\n",- " return self.config.vocab_size"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Try it out"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "tokenizer = SimpleTokenizer()\n",- "print(f\"Vocabulary size: {tokenizer.vocab_size:,}\")\n",- "print(f\"EOS token ID: {tokenizer.eos_token_id}\")\n",- "print()\n",- "\n",- "text = \"The cat sat on the mat.\"\n",- "encoded = tokenizer.encode(text)\n",- "decoded = tokenizer.decode(encoded)\n",- "\n",- "print(f\"Original: '{text}'\")\n",- "print(f\"Encoded: {encoded}\")\n",- "print(f\"Decoded: '{decoded}'\")\n",- "print(f\"Roundtrip: {text == decoded}\")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Rare words and emojis"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "rare = tokenizer.encode(\"antidisestablishmentarianism\")\n",- "pieces = [tokenizer.decode([t]) for t in rare]\n",- "print(f\"Pieces: {pieces}\")\n",- "print(f\"Decoded: '{tokenizer.decode(rare)}'\")\n",- "print()\n",- "\n",- "emoji = tokenizer.encode(\"Hello world!\")\n",- "print(f\"Emoji test: {tokenizer.decode(emoji)}\")"- ]- }- ],- "metadata": {- "kernelspec": {- "display_name": "Python 3",- "language": "python",- "name": "python3"- },- "language_info": {- "name": "python",- "version": "3.10.0"- }- },- "nbformat": 4,- "nbformat_minor": 5-}