Courseware / Machine Learning / course-021
Building Large Language Models from Scratch: A Comprehensive Guide Inspired by Andrej Karpathy's 6‑Hour Course
Tweet@0xCodezView Source →

🎙 Podcast Version

2-host dialogue — ALEX & SAM discuss this course.

Building Large Language Models from Scratch: A Comprehensive Guide Inspired by Andrej Karpathy's 6‑Hour Course

Overview

This course walks you through the end‑to‑end process of creating a large language model (LLM) comparable to ChatGPT, starting from raw text and ending with a usable inference system. It is based on Andrej Karpathy’s six‑hour live lecture, which covers a deep theoretical dive into LLMs, a hands‑on construction of a GPT‑style model, and practical guidance on how to deploy and use the model effectively. By mastering the material, you gain the ability to build, customize, and operate LLMs without relying on black‑box APIs, opening doors to research, product development, and advanced study that would otherwise require a costly graduate degree. The knowledge is valuable for machine‑learning engineers, AI researchers, and sophisticated hobbyists who want full control over model architecture, training data, and behavior.

Background & Context

Large language models have become the cornerstone of modern natural‑language processing, powering chatbots, code assistants, translation systems, and many other AI‑driven products. Despite their prevalence, most practitioners interact with LLMs only through high‑level APIs, which obscures the underlying mechanics and limits the ability to innovate. Andrej Karpathy, a former Director of AI at Tesla and a founding member of OpenAI, has long advocated for teaching the fundamentals of deep learning by building models from scratch. His six‑hour lecture was released as a free video on X (formerly Twitter) and is positioned as a viable alternative to a $90,000 Stanford LLM master’s program, emphasizing that deep understanding can be achieved through focused, self‑directed study. The lecture’s structure—starting with theory, moving to live coding, and concluding with usage patterns—mirrors the pedagogical approach used in elite university courses but is accessible to anyone with an internet connection and basic programming skills.

Core Concepts

Large Language Models (LLMs)

An LLM is a neural network trained to predict the next token in a sequence of text, where a token can be a word, subword, or character depending on the tokenization scheme. The model’s capacity to capture statistical regularities in language emerges from scaling three dimensions: model size (number of parameters), dataset size (amount of text), and compute (training FLOPs). Modern LLMs such as GPT‑3, GPT‑4, and LLaMA range from hundreds of millions to trillions of parameters and are trained on terabytes of diverse corpora. The emergent abilities—few‑shot learning, chain‑of‑thought reasoning, and tool use—appear only after certain scale thresholds are crossed. Understanding LLMs therefore requires grasping both the architectural details (the transformer block) and the scaling laws that govern performance improvements.

Transformer Architecture

The transformer, introduced in the 2017 paper “Attention Is All You Need,” replaces recurrence and convolution with self‑attention mechanisms that allow each token to attend to every other token in the sequence. A transformer block consists of a multi‑head self‑attention layer followed by a position‑wise feed‑forward network, each wrapped in residual connections and layer normalization. Multi‑head attention enables the model to capture different types of relationships (syntactic, semantic, positional) simultaneously by learning multiple sets of query, key, and value projection matrices. Positional encodings—either sinusoidal or learned—are added to token embeddings to inject order information, since self‑attention is inherently permutation‑invariant. Stacking N identical blocks yields a deep network capable of hierarchical feature extraction, with deeper models capturing longer-range dependencies.

Tokenization and Vocabulary Construction

Before training, raw text must be converted into a sequence of integer IDs that the model can process. Tokenization algorithms such as Byte Pair Encoding (BPE) or WordPiece iteratively merge the most frequent pairs of characters or subwords to build a fixed‑size vocabulary (typically 32k–50k tokens). This subword approach balances the need for a compact vocabulary with the ability to represent rare or unseen words via combinations of known subwords. Special tokens are added for padding (<pad>), sequence start (<bos>), sequence end (<eos>), and unknown (<unk>). Proper tokenization is critical: an overly aggressive split inflates sequence length and computational cost, while too coarse a vocabulary leads to many out‑of‑vocabulary tokens that the model must treat as unknown.

Pretraining from Scratch

Pretraining an LLM involves minimizing the cross‑entropy loss between the model’s predicted next‑token distribution and the actual next token in the training corpus. The process requires massive amounts of text, efficient data pipelines, and careful optimization. Training typically uses the AdamW optimizer with a learning rate schedule that includes a linear warm‑up phase followed by cosine decay or inverse square‑root decay. Gradient accumulation allows effective batch sizes larger than what fits in GPU memory. Mixed‑precision training (FP16 or BF16) reduces memory bandwidth and increases throughput. Checkpointing saves model parameters periodically to enable recovery from failures and to evaluate intermediate performance. Throughout pretraining, monitoring perplexity on a held‑out validation set provides a proxy for how well the model captures the statistical structure of the language.

Inference and the Karpathy Method

Once trained, an LLM generates text by sampling from its predictive distribution over the vocabulary for each position, conditioned on the previously generated tokens. Common decoding strategies include greedy selection, top‑k sampling, nucleus (top‑p) sampling, and temperature scaling, which trade off determinism versus diversity. Andrej Karpathy’s “method” emphasizes a disciplined workflow: first, verify that the model can reproduce known benchmarks (e.g., perplexity on WikiText‑103); second, construct simple prompting templates that elicit desired behaviors (e.g., “Q: … A:” for question answering); third, iterate on prompt engineering and sampling hyper‑parameters to align outputs with user intent while minimizing hallucinations. This method treats the model as a programmable tool whose behavior is shaped primarily by the prompt and sampling settings rather than by further fine‑tuning.

Evaluation and Alignment

Beyond perplexity, LLMs are evaluated on downstream tasks such as language modeling (LAMBADA), commonsense reasoning (HellSwag), reading comprehension (RACE), and code generation (HumanEval). Human‑centric metrics—preference scores, truthfulness, and toxicity—are increasingly important for deployment. Alignment techniques such as reinforcement learning from human feedback (RLHF) or supervised fine‑tuning on instruction datasets can steer the model toward helpful and safe behavior, but Karpathy’s approach shows that strong base models combined with thoughtful prompting can already achieve impressive results for many applications.

How It Works / Step‑by‑Step

Step 1: Data Acquisition and Cleaning

Gather a large, diverse text corpus (e.g., Common Crawl, Wikipedia, books, code repositories). Strip HTML, remove boilerplate, deduplicate documents, and optionally filter for language quality using heuristics or a fast classifier. Store the cleaned text in a sharded format (e.g., TFRecord or Arrow) to enable efficient streaming during training.

Step 2: Tokenizer Training

Run a BPE algorithm on a sample of the corpus to learn merge operations. Save the resulting vocabulary and merge files. Apply the tokenizer to the full dataset to convert each document into a list of token IDs. Optionally truncate or pad sequences to a fixed maximum length (e.g., 1024 tokens) and pack multiple short documents into a single training example with separator tokens to improve GPU utilization.

Step 3: Model Initialization

Define the transformer hyper‑parameters: number of layers (N), hidden dimension (d_model), number of attention heads (h), feed‑forward dimension (d_ff), dropout rate, and activation function (usually GELU). Initialize weight matrices using Xavier or Kaiming uniform distribution, and set layer norm biases to zero and gains to one.

Step 4: Training Loop

For each iteration:

  1. Sample a batch of token sequences from the data loader.
  2. Feed the sequences through the transformer to obtain logits for each position.
  3. Shift the target sequence by one position to compute next‑token predictions.
  4. Compute cross‑entropy loss, average over non‑padding tokens.
  5. Backpropagate gradients, apply gradient clipping (e.g., norm = 1.0) to prevent exploding gradients.
  6. Update parameters with AdamW, using the current learning rate from the schedule.
  7. Log loss and perplexity; every N steps, evaluate on a validation set and save a checkpoint.

Step 5: Learning Rate Scheduling

Implement a warm‑up phase of, e.g., 2,000 steps where the learning rate linearly increases from 0 to the peak value (e.g., 6e‑4). After warm‑up, decay the learning rate using a cosine schedule: lr = lr_max 0.5 (1 + cos(π * step / total_steps)). This schedule has been shown to stabilize training of large transformers.

Step 6: Mixed‑Precision and Gradient Accumulation

Enable automatic mixed precision (AMP) so that forward passes run in FP16 while weight updates accumulate in FP32, halving memory usage. If the desired batch size exceeds GPU memory, accumulate gradients over multiple micro‑batches before performing an optimizer step.

Step 7: Checkpointing and Recovery

Save model weights, optimizer state, scheduler state, and a random seed snapshot every few thousand iterations. To resume training, load the checkpoint and restore the RNG states to ensure bit‑wise reproducibility.

Step 8: Inference Pipeline

Load the final checkpoint, set the model to evaluation mode, and disable dropout. For generation:

  • Encode the prompt with the tokenizer to obtain input IDs.
  • Iteratively: feed the current sequence through the model, retrieve logits for the last position, apply temperature scaling, optionally apply top‑k or top‑p filtering, sample a token ID, append it to the sequence, and stop when an end‑of‑token is generated or a max length is reached.
  • Decode the token IDs back to text using the tokenizer’s inverse mapping.

Step 9: Prompt Engineering (Karpathy Method)

Design prompt templates that clearly separate instruction from context. For zero‑shot QA, use “Question: {q}\nAnswer:”. For few‑shot learning, prepend exemplars: “Q: {q1}\nA: {a1}\nQ: {q2}\nA: {a2}\n…\nQ: {q_test}\nA:”. Experiment with temperature (0.2–0.8) and top‑p (0.9) to balance creativity and fidelity. Evaluate generations with automated metrics (BLEU, ROUGE) and human preference studies to refine prompts.

Real-World Examples & Use Cases

Example 1: Building a Mini‑GPT for Code Autocompletion

A developer collects 10 GB of permissively licensed source code from GitHub, trains a 12512‑layer transformer (≈30 M parameters) using the steps above, and obtains a model that predicts the next token in a code file. At inference time, the model is integrated into an IDE plugin: as the programmer types, the last 128 tokens are fed to the model, which returns the top‑5 token continuations displayed as a dropdown. The model achieves a 22 % reduction in keystrokes for common boilerplate patterns compared to a rule‑based completer.

Example 2: Domain‑Specific Medical Chatbot

A research team curates a corpus of de‑identified clinical notes and PubMed abstracts (≈5 GB). They train a medium‑sized LLM (250 M parameters) from scratch, then use the Karpathy prompting method with a template: “Patient: {symptoms}\nDoctor:”. By adjusting temperature to 0.3 and top‑p to 0.9, the model generates plausible, concise medical advice that aligns with clinician‑reviewed answers in 78 % of cases on a held‑out set of patient queries. The system is deployed as a triage assistant in a telemedicine platform, reducing nurse workload by 15 %.

Example 3: Educational Tool for Language Learning

An educator assembles a multilingual corpus of simple sentences in English, Spanish, and French (≈2 GB). After training a small encoder‑decoder transformer (≈50 M parameters) from scratch, they create a prompting scheme: “Translate to French: {english_sentence}”. The model achieves a BLEU score of 31.2 on the WMT14 test set, comparable to phrase‑based baselines, while offering the flexibility to handle idiomatic phrases and user‑generated content without additional rule engineering.

Key Insights & Takeaways

  • Understanding the transformer’s self‑attention mechanism is essential for grasping how LLMs capture long‑range dependencies without recurrence.
  • Tokenization choice directly impacts model size, training speed, and ability to represent rare words; BPE offers a practical balance for most languages.
  • Pretraining from scratch requires careful orchestration of data pipelines, optimizer settings, and learning‑rate schedules to achieve stable convergence.
  • Mixed‑precision training and gradient accumulation make it feasible to train large models on modest hardware budgets.
  • Checkpointing not only guards against failures but also enables analysis of intermediate model states for ablation studies.
  • The Karpathy method shows that strong prompting and sampling strategies can elicit high‑quality behavior from a base model without costly fine‑tuning.
  • Evaluation should combine perplexity (a proxy for language modeling ability) with task‑specific metrics and human judgments to assess real‑world usefulness.
  • Building your own LLM provides full control over data licensing, model architecture, and deployment constraints, which is critical for research reproducibility and commercial applications.
  • The skills acquired—data engineering, model implementation, training loop debugging, and prompt design—transfer to other deep‑learning domains such as vision transformers and multimodal models.

Common Pitfalls / What to Watch Out For

  • Insufficient Data Quality: Training on noisy or biased text leads to models that reproduce harmful stereotypes or factual errors; always audit and filter your corpus.
  • Mis‑scaled Learning Rate: Too high a rate causes divergence; too low results in painfully slow convergence. Use learning‑rate finders or follow established schedules (warm‑up + cosine decay).
  • Overlooking Gradient Clipping: Unclipped gradients can explode in deep transformers, producing NaN losses; clip at a sensible norm (e.g., 1.0).
  • Neglecting Validation Perplexity: Relying solely on training loss masks overfitting; monitor validation perplexity each checkpoint to detect early signs of memorization.
  • Inadequate Batch Size via Gradient Accumulation Mistakes: Incorrectly scaling the loss when accumulating gradients leads to under‑ or over‑updating weights; divide the loss by the number of accumulation steps.
  • Improper Tokenizer Mismatch: Using a different tokenizer at inference than at training yields garbled outputs; always save and reload the exact tokenizer files.
  • Overreliance on Greedy Decoding: Greedy selection often produces repetitive, low‑diversity text; experiment with temperature, top‑k, or top‑p sampling for richer outputs.
  • Hardware Mismatch: Attempting to fit a model larger than GPU memory without proper activation checkpointing or model parallelism will cause OOM errors; use tools like DeepSpeed or FSDP when scaling beyond a single GPU.
  • Prompt Drift: Small changes in prompt wording can cause large shifts in model behavior; keep a version‑controlled prompt library and evaluate systematically.

Review Questions

  1. Explain how multi‑head self‑attention enables a transformer to capture different types of linguistic relationships, and describe the role of query, key, and value projections in this process.
  2. Outline the complete training loop for an LLM from data loading to optimizer step, highlighting where learning‑rate scheduling, gradient clipping, and mixed‑precision come into play.
  3. Suppose you have trained a 120‑million‑parameter GPT‑style model and want to deploy it as a question‑answering chatbot. Design a prompting strategy, select appropriate sampling hyper‑parameters, and describe how you would evaluate whether the model’s answers are both accurate and safe.

Further Learning

  • Read the original “Attention Is All You Need” paper (Vaswani et al., 2017) and the follow‑up “Training Language Models to Follow Instructions with Human Feedback” (Ouyang et al., 2022) to understand the evolution from base LLMs to aligned models.
  • Study the GPT‑2 and GPT‑3 technical reports from OpenAI for details on scaling laws, model architecture variations, and training infrastructure.
  • Explore the Hugging Face Transformers library source code to see practical implementations of tokenization, model classes, and training utilities.
  • Enroll in the full‑scale “LLM University” course by Andrej Karpathy (available on his website) for deeper dives into reinforcement learning from human feedback, model parallelism, and evaluation benchmarks.
  • Investigate efficient training frameworks such as DeepSpeed, Megatron‑LM, and FSDP that enable training of hundred‑billion‑parameter models on GPU clusters.
  • Experiment with replication projects like MiniGPT‑4 or NanoGPT to solidify your understanding by building and training small transformers from scratch on a single GPU.
  • Keep up‑to‑date with recent LLM releases (LLaMA 2, Mistral, Gemini) by reading their model cards and attempting to fine‑tune them on domain‑specific data for specialized applications.
← Previous
Next →