
📺 Today’s recommended deep-dive video: https://www.youtube.com/watch?v=quh7z1q7-uc
Coding the Core: A Deep Dive into Building LLMs from Scratch
Most people interact with Large Language Models through a chat interface, but few understand the intricate machinery humming under the hood. Sebastian Raschka breaks down the entire lifecycle of a model—from raw text tokenization to specialized instruction fine-tuning—using hands-on code examples.
Core Question: How can developers move beyond being mere consumers of AI and learn to implement, pre-train, and optimize their own large language models from the ground up?
Highlights
- The mechanics of Byte Pair Encoding (BPE) and data sampling for next-token prediction.
- Implementing the GPT architecture including Transformer blocks and embedding layers.
- Practical pre-training techniques using PyTorch and small-scale datasets for rapid iteration.
- Efficient fine-tuning with LoRA (Low-Rank Adaptation) and evaluating performance with MMLU and AlpacaEval.
⏱️ Reading time: approx. 12 minutes · Saves you about 153 minutes vs. watching.
Want to take notes while watching? Click the image below and let AI Notebook capture the key points for you 👇
The Foundation of Tokens and Data
From Raw Text to Numerical IDs
Before a model can “think,” it must consume data in a format it understands: numbers. The process begins with tokenization, which breaks down raw text into smaller units like words or sub-word components.
Sebastian highlights that modern models prefer Byte Pair Encoding (BPE) because it handles unknown words gracefully by breaking them into sub-parts. Using a library like TickToken, a developer can convert a sentence into a sequence of integer IDs that map directly to a pre-defined vocabulary of over 50,000 unique tokens. This numerical representation is the vital bridge between human language and the vector spaces where neural networks operate.

The Shifting Target Mechanism
Large Language Models are essentially high-powered autocomplete engines trained on the “next-token prediction” task. To facilitate this, the training data must be structured so that for every input sequence, the “target” is the same sequence shifted one position to the right.
This specific alignment ensures that at every step of the sequence, the model is trying to guess what comes next. By using a “stride” equal to the context length, researchers can feed the model vast amounts of text without redundant overlaps, maximizing the efficiency of every training epoch. This structural nuance is what allows the model to learn the statistical relationships between words over millions of iterations.
💡 Digging Deeper
Q: Why use BPE instead of simple word-level tokenization?
A: BPE allows the model to handle “out-of-vocabulary” words by breaking them into smaller, recognizable character chunks, preventing the system from crashing when it hits a new term.
Q: What is the “End of Text” token?
A: It is a special delimiter (like <|endoftext|>) used to signal the boundary between different documents in a massive training set, helping the model learn when one context ends and another begins.
Q: How does the model see the position of words?
A: Since Transformers process all tokens simultaneously, we add “positional embeddings” to the token IDs to provide a sense of order and sequence.
Architecture and the Generation Loop
Building the Transformer Block
The heart of any GPT-like model is the Transformer block, a repeating unit consisting of multi-head attention and feed-forward layers. While the specific number of layers varies—from 12 in smaller models to 36 or more in billions-parameter versions—the underlying logic remains identical across the industry.
Modern architectures like Llama 3 or Phi-3 have introduced minor tweaks, such as replacing standard Layer Norm with RMSNorm or using different activation functions like SwiGLU. However, Sebastian notes that if you understand the basic GPT-2 structure, you possess 80% of the knowledge required to navigate any cutting-edge model. These blocks work in tandem to refine the representation of a token based on the context provided by every other token in the input window.

From Logits to Language
When the model processes a sequence, the final layer outputs “logits,” which are unnormalized scores for every single word in the vocabulary. To turn these raw numbers into a coherent response, we apply a Softmax function to convert them into probabilities.
Generating text is an iterative process: the model predicts the most likely next token, appends it to the original input, and feeds the new, longer sequence back into the architecture. This loop continues until the model hits a maximum token limit or generates a stop sequence. It is a computationally expensive dance, but it is the secret behind the seemingly magical ability of AI to write poetry or code.
💡 Digging Deeper
Q: What are logits exactly?
A: Logits are the raw output values from the last linear layer of the model before they are turned into 0-to-1 probabilities by the Softmax function.
Q: Why do we only look at the “last” token’s output during generation?
A: Because that output represents the model’s prediction for the very next word; previous outputs correspond to words the model has already seen in its input.
Q: Can we load pre-trained weights into a custom-coded architecture?
A: Yes, though it requires a tedious “mapping” process to ensure the weights from a source (like OpenAI) match the specific variable names in your custom PyTorch code.
Training and Efficient Fine-Tuning
The Pre-training Marathon
Pre-training is the most resource-intensive phase, where a model is exposed to trillions of tokens to learn general world knowledge. Using a standard training loop, the model calculates a loss (cross-entropy) based on how far its prediction was from the actual next word in the text.
The optimizer then adjusts the model’s millions of parameters via backpropagation to minimize this loss. While massive models require clusters of GPUs and months of time, Sebastian demonstrates that you can train a tiny model on a single short story in minutes. This exercise is invaluable for debugging the training logic before committing to a large-scale run.

Fine-Tuning with LoRA
Once a model has general knowledge, it needs “instruction fine-tuning” to become a helpful assistant. Instead of updating all billions of parameters, which is slow and memory-heavy, we use Low-Rank Adaptation (LoRA).
LoRA works by freezing the original weights and only training two much smaller matrices that approximate the necessary changes. This “rank-based” shortcut reduces the number of trainable parameters by a factor of 1,000, allowing developers to fine-tune massive models on consumer-grade hardware. It is the gold standard for creating specialized AI agents without the “compute tax” of full fine-tuning.
💡 Digging Deeper
Q: What is the difference between Pre-training and Instruction Fine-tuning?
A: Pre-training teaches the model “how to talk” and general facts; instruction fine-tuning teaches it “how to follow orders” and act as a chatbot.
Q: How much data is needed for instruction fine-tuning?
A: While early datasets had 50,000 examples, recent research shows that 1,000 extremely high-quality, diverse examples can often produce better results.
Q: What is MMLU?
A: It stands for Massive Multitask Language Understanding, a benchmark of 57 subjects across STEM and the humanities used to measure a model’s general knowledge.
Key Takeaways
Building an LLM from scratch is the ultimate “demystification” exercise for any AI engineer. By implementing the architecture, data loaders, and training loops manually, you move past the “black box” mentality and begin to understand why models hallucinate or how they manage to maintain context over long conversations. The transition from a simple GPT-2 clone to a fine-tuned assistant using LoRA illustrates the scalability and flexibility of the Transformer architecture.
Furthermore, the availability of tools like LitGPT allows developers to transition from educational “from-scratch” scripts to production-ready workflows. These libraries provide the optimization and hardware-acceleration needed to handle state-of-the-art models like Phi-3 or Llama 3 while maintaining the transparency of open-source code.
Ultimately, the goal is to bridge the gap between theory and practice. Whether you are training a model on a single public-domain book or fine-tuning a billion-parameter giant for a specific business use case, the fundamental principles of tokenization, attention, and loss minimization remain your North Star.
Q&A
Q1: What is the most important part of the LLM data pipeline?
The “shifted target” structure is critical. It ensures that the model learns to predict the $n+1$ token based on the first $n$ tokens, which is the foundational logic of autoregressive generation.
Q2: Can I run these models on a standard laptop?
Smaller models (like GPT-2 or Phi-3) can run on a CPU, but for training or fine-tuning, a GPU is highly recommended to handle the matrix multiplications efficiently.
Q3: How does LoRA save so much memory?
By approximating weight updates with low-rank matrices, you only need to store and calculate gradients for a tiny fraction (often <1%) of the total parameters, drastically lowering VRAM requirements.
Q4: What is the Alpaca prompt style?
It is a specific template that formats instructions, optional inputs, and responses into a structured string. This helps the model distinguish between the “task” and the “answer” during the fine-tuning process.
Q5: Why does the model sometimes repeat itself during generation?
Repetition often occurs in smaller models or those with poorly tuned “temperature” and “top-k” sampling settings. It suggests the model is stuck in a local probability loop.
Q6: What is a “state dict” in PyTorch?
It is a Python dictionary object that maps each layer of a model to its learnable weights (tensors). It is the standard format for saving and loading model checkpoints.
Q7: Is it possible to evaluate an LLM without human testers?
Yes, researchers use “LLM-as-a-judge” (like AlpacaEval) where a more powerful model (e.g., GPT-4) scores the responses of a smaller model based on specific criteria.
