DEV Community

Abhishek Banerjee
Abhishek Banerjee

Posted on Originally published at Medium on

Building a Large Language Model from Scratch: A Comprehensive Learning Guide

Architectural Principles, Training Dynamics, and Fine-Tuning Paradigms for Autoregressive Transformers

This guide provides a structured, seven-phase pedagogical framework for developing, pretraining, and fine-tuning a GPT-like Large Language Model (LLM) using PyTorch. Grounded in the methodology established in the Build a Large Language Model (From Scratch) repository, this document outlines the transition from foundational programming to state-of-the-art architectural extensions.

GitHub - rasbt/LLMs-from-scratch: Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

Phase 1: Foundations and Environment Setup

The initial phase focuses on establishing a robust computational environment and mastering the fundamental building blocks of deep learning.

Conceptual Overview

  • Tensors: The multi-dimensional arrays used by PyTorch to represent data and model weights.
  • Automatic Differentiation (Autograd): A core PyTorch feature that automatically calculates gradients, which is essential for training neural networks through backpropagation.
  • Device Execution: The process of shifting computations between a Central Processing Unit (CPU) and a Graphics Processing Unit (GPU) or Metal Performance Shaders (MPS) to accelerate training.

Key Repository Pointers

Self-Verification

  • Can a script identify if a CUDA-enabled GPU or Apple Silicon MPS is available for execution?
  • Are you able to perform matrix multiplication using PyTorch tensors?

Phase 2: Text Ingestion and Data Preparation

Before a model can process language, text must be converted into a numerical format that a computer can manipulate.

Conceptual Overview

  • Tokenization: Breaking raw text into smaller units (tokens) such as words or subwords.
  • Byte Pair Encoding (BPE): A subword tokenisation method that balances vocabulary size and the ability to represent rare words.
  • Embeddings: Numerical vectors that represent tokens in a high-dimensional space, capturing semantic meaning.
  • Positional Embeddings: Vectors added to token embeddings to provide the model with information regarding the position of tokens in a sequence.

Key Repository Pointers

Self-Verification

  • Does the DataLoader correctly implement a sliding window to create input-target pairs for next-token prediction?
  • What is the primary difference between a simple word-level tokeniser and BPE?

Phase 3: Coding the Attention Engine

Attention mechanisms allow the model to focus on different parts of the input sequence when producing an output.

Conceptual Overview

  • Self-Attention: A mechanism where each token in a sequence “attends” to every other token to calculate its context.
  • Query, Key, and Value (Q, K, V): Linear transformations of input embeddings used to calculate attention scores.
  • Causal Masking: A technique used during training to prevent the model from “peeking” at future tokens in the sequence.
  • Multi-Head Attention (MHA): Running multiple attention mechanisms in parallel to allow the model to capture different types of relationships within the data.

Key Repository Pointers

Self-Verification

  • Why is the “scaling” factor (square root of the dimension) necessary in dot-product attention?
  • How does the causal mask ensure the model remains autoregressive?

Phase 4: Assembling the Full GPT Model

This phase involves integrating the attention mechanism into a stackable Transformer block to build the complete architecture.

Conceptual Overview

  • Layer Normalisation: A technique to stabilise the learning process by normalising the inputs to a layer.
  • GELU Activation: The Gaussian Error Linear Unit, a non-linear activation function used in modern Transformers.
  • Feed-Forward Network (FFN): A small neural network applied to each token position independently.
  • Shortcut Connections: Also known as residual connections, these allow gradients to flow through the network more easily by skipping layers.

Key Repository Pointers

Self-Verification

  • What is the role of the final layer normalisation before the output head?
  • How many Transformer blocks are typically used in a small-scale GPT-2 model?

Phase 5: Pretraining and Autoregressive Text Generation

Pretraining involves training the model on vast amounts of unlabeled text to predict the next token.

Conceptual Overview

  • Cross-Entropy Loss: The loss function used to measure the difference between the model’s predicted probability and the actual next token.
  • Autoregressive Generation: Generating text one token at a time, where each new token is appended to the input for the next step.
  • Temperature Scaling: Adjusting the randomness of the output; lower temperature results in more deterministic text.
  • Top-k Sampling: Limiting the model’s choices to the k most likely next tokens.

Key Repository Pointers

Self-Verification

  • How does top-k sampling prevent the model from generating nonsensical or low-probability tokens?
  • Are the model outputs improved significantly after loading pretrained weights compared to random initialisation?

Phase 6: Downstream Fine-Tuning and Preference Alignment

Fine-tuning adapts a pretrained model for specific tasks, such as classification or following instructions.

Conceptual Overview

  • Classification Fine-Tuning: Modifying the model’s output head to predict labels (e.g., “Spam” vs “Not Spam”).
  • Instruction Tuning: Training the model on (instruction, response) pairs to behave as a helpful assistant.
  • Direct Preference Optimization (DPO): Aligning model outputs with human preferences using a dataset of “chosen” vs “rejected” responses.
  • LoRA (Low-Rank Adaptation): A parameter-efficient fine-tuning method that updates only a small subset of weights.

Key Repository Pointers

Self-Verification

  • Why is instruction tuning necessary even if a model is already highly capable of text generation?
  • What are the memory benefits of using LoRA over full-parameter fine-tuning?

Phase 7: SOTA Architectural Extensions and Reasoning Models

The final phase explores advanced modifications found in state-of-the-art (SOTA) models and the development of reasoning capabilities.

Conceptual Overview

  • Grouped-Query Attention (GQA): An attention variant that reduces memory usage by sharing keys and values across heads.
  • KV Caching: Storing previously computed Key and Value vectors to speed up inference.
  • Mixture-of-Experts (MoE): A model architecture that activates only a portion of its parameters for any given input.
  • GRPO (Group Relative Policy Optimization): A reinforcement learning approach used to enhance reasoning.

Key Repository Pointers

End-to-End LLM Pipeline

The shift from task-specific architectures to the autoregressive transformer paradigm marks a fundamental transition in Natural Language Processing (NLP). Modern Large Language Models (LLMs) achieve broad generalization by treating language as a high-dimensional sequence modeling problem. From an architectural standpoint, building these models from first principles rather than relying on opaque, high-level wrappers is a strategic necessity. This “from-scratch” methodology ensures absolute technical control, enabling architects to optimize internal dynamics, troubleshoot convergence issues at scale, and maintain full transparency over the model’s data-handling and reasoning capabilities.

The Development Lifecycle

The path from raw corpus to an aligned assistant is a multi-stage engineering workflow:

  1. Data Ingestion & Tokenization: Converting raw text into a discrete, manageable vocabulary.
  2. Architecture Implementation: Constructing the attention engine and stacking transformer blocks.
  3. Pretraining: Self-supervised learning on massive datasets via Next-Token Prediction to build a “base model.”
  4. Supervised Fine-Tuning (SFT): Specializing the base model for specific tasks or instruction-following.
  5. Preference Alignment: Utilizing datasets (e.g., DPO) to align the model’s outputs with human intent and safety constraints.

Architectural Flowchart

[Raw Text]
      |
      v
[Tokenisation] (BPE / tiktoken - Fallback to character-level bytes)
      |
      v
[Embedding Layer] (Token + Positional Encodings)
      |
      v
[Transformer Blocks] (MHA + FFN + Residuals) <--- (Pretraining Phase)
      |
      v
[Output Heads] (Linear + Softmax / Classification Heads)
      |
      v
[Specialisation] (SFT / LoRA / DPO) <--- (Fine-Tuning & Alignment)
      |
      v
[Final Inference] (Autoregressive Generation with KV Caching)
Enter fullscreen mode Exit fullscreen mode

The systemic nature of LLM development requires precision at the very start of the pipeline, where raw data is first translated into the numerical domain.

Text Ingestion and Embedding Dynamics

High-fidelity data representation is the prerequisite for effective pattern recognition. Because neural networks operate on continuous vectors, the initial translation of discrete text into numerical space must preserve as much information as possible while remaining computationally efficient.

Tokenisation Mechanics

We utilize Byte Pair Encoding (BPE) over character or word-based methods to balance vocabulary size and granularity. Crucially, BPE eliminates the “out-of-vocabulary” (OOV) problem by falling back to character-level bytes for unseen strings. Using libraries like tiktoken allows for efficient vocabulary growth management, ensuring that common words remain single tokens while rare words are decomposed into sub-word units, maximizing the model's linguistic coverage.

The Embedding Layer

The embedding layer maps discrete token IDs to a continuous vector space. This transformation is the first expansion of data into higher dimensions.

Stage
Tensor Shape Description
Mathematical Representation
Input Batch
(batch_size, num_tokens)
(B, T)
Embedding Output
(batch_size, num_tokens, embedding_dim)
(B, T, D)
Enter fullscreen mode Exit fullscreen mode

Positional Encoding

Transformers are permutation-invariant; they lack a built-in sense of sequence order. To inject spatial information, we add positional embeddings to the token embeddings. While absolute embeddings are standard for fixed-length windows, modern architects often evaluate rotary or relative encodings to satisfy the requirements for long-context windows. These static representations provide the necessary coordinates for the attention engine to begin dynamic processing.

The Attention Engine: Causal and Multi-Head Dynamics

The Attention Mechanism is the strategic core of the transformer, enabling the model to assign relative weights to different tokens in a sequence based on context.

Scaled Dot-Product Attention (SDPA)

The computation uses three distinct projections: Query (Q), Key (K), and Value (V). Attention(Q, K, V) = softmax(QK^T / sqrt(d_k))V By scaling the dot product by the square root of the key dimension (d_k), we prevent gradients from vanishing during the softmax operation, ensuring stable training.

Causal Masking

In autoregressive training, a causal mask is applied to the attention scores to prevent the model from “looking ahead” at future tokens. This triangular matrix ensures that the prediction for token n only depends on tokens 1 \dots n.

4x4 Causal Mask Matrix:

[1, 0, 0, 0] (Token 1 attends to: 1)
[1, 1, 0, 0] (Token 2 attends to: 1, 2)
[1, 1, 1, 0] (Token 3 attends to: 1, 2, 3)
[1, 1, 1, 1] (Token 4 attends to: 1, 2, 3, 4)
Enter fullscreen mode Exit fullscreen mode

Multi-Head Attention (MHA)

MHA parallelizes the attention process, allowing the model to attend to different parts of the sequence for different features (e.g., syntax vs. semantics). Note that the “split” into heads occurs after the initial linear projections.

Input (X) 
   |
[Linear Projections: Wq, Wk, Wv]
   |
[Split into N Heads] ----> [Parallel SDPA per head]
   | |
   |<----------------------------|
   v
[Concatenate Heads] ----> [Output Projection Wo] ----> Output
Enter fullscreen mode Exit fullscreen mode

These parallel outputs are then refined through the broader transformer stack.

The Complete Transformer Architecture Stack

The “GPT Block” is a structural assembly where architectural choices like normalization placement and non-linearities dictate training viability.

Normalisation and Activation

  • Layer Normalisation: We utilize Pre-LayerNorm (normalizing before the block) rather than the Post-LayerNorm used in the original Transformer. Pre-LayerNorm addresses the vanishing gradient problem in deep stacks, enabling training stability at significantly higher learning rates.
  • Activation Functions: We employ the Gaussian Error Linear Unit (GELU) over ReLU. GELU’s smoother gradient and non-zero values for negative inputs mitigate the “dying ReLU” problem, where neurons become inactive and stop contributing to the learning process.

Feed-Forward Networks (FFN)

The point-wise FFN operates on each token independently, typically expanding the embedding dimension by a factor of four before contracting it back to the original size: (batch, tokens, D) -> (batch, tokens, 4D) -> (batch, tokens, D)

The GPT Block Assembly

Residual connections (Shortcut Connections) are vital; they allow the original input to bypass the block and be added to the output, facilitating gradient flow during backpropagation.

Input (x)
  |-------------------------------------------|
  | |
  |-----> [LayerNorm] --> [MHA] --> [Addition (+)]
                                              |
  |-------------------------------------------|
  |
  |-----> [LayerNorm] --> [FFN] --> [Addition (+)]
                                              |
                                              v
                                      Next Block / Head
Enter fullscreen mode Exit fullscreen mode

Stacking these blocks facilitates the emergence of complex language capabilities through hierarchical feature extraction.

Pretraining Dynamics and Generative Inference

Pretraining transforms the model from a random state into a statistically proficient engine through self-supervised learning on massive unlabelled corpora.

The Objective Function

The model utilizes Cross-Entropy Loss for Next-Token Prediction. It is penalized when the probability distribution it generates deviates from the ground-truth next token in the training set.

Weight Loading and Memory Footprint

Loading pretrained weights (e.g., GPT-2 or Llama-3) requires careful VRAM planning.

Parameter Count
Approx. Memory (FP16/BF16)
Use Case
124M
~0.5 GB
Educational/Edge
1.5B
~3.0 GB
Specialized Tasks
7B
~14–28 GB
General Purpose LLM
Enter fullscreen mode Exit fullscreen mode

Decoding and Efficiency

  • KV Caching: To speed up inference, we cache the Key and Value tensors of previously processed tokens. This avoids the O(N²) cost of recomputing the full attention matrix for every newly generated token in the autoregressive loop.
  • Sampling: Temperature sampling (adjusting distribution sharpness) and Top-k sampling (limiting choices to the top k tokens) are used to balance coherence and creativity.

These foundational base models are later specialized into assistant models via fine-tuning.

Downstream Fine-Tuning and Preference Alignment

Fine-tuning pivots the general-purpose base model toward specific business value, such as specialized classification or instruction following.

Supervised Fine-Tuning (SFT) & PEFT

While SFT updates all parameters, Low-Rank Adaptation (LoRA) is a parameter-efficient alternative. LoRA freezes the main weights and injects two low-rank matrices (A and B) into the layers.

LoRA Adapter Path:

Input (x)
     / \
[Frozen W] [A -> B] (Rank r << d)
     \ /
      Sum (+)
         |
       Output
Enter fullscreen mode Exit fullscreen mode

Alignment via DPO

Direct Preference Optimization (DPO) is a high-efficiency alternative to RLHF. By using a preference dataset (pairs of “Chosen” and “Rejected” responses), DPO optimizes the model directly without requiring a separate reward model or complex reinforcement learning loops, ensuring the output aligns with human intent.

Advanced Architectural Extensions and Reasoning

The research frontier focuses on scaling reasoning capabilities while reducing computational bottlenecks.

Efficiency and Sparse Architectures

Architectures like Mixture-of-Experts (MoE) activate only a subset of parameters per token, drastically reducing FLOPs during inference.

Feature
Dense Model
Sparse (MoE) Model
Routing Mechanism
Active Params
100%
~10–20%
Top-k Gating
FLOPs
High
Low
Learned Router
Enter fullscreen mode Exit fullscreen mode

Extensions like Grouped-Query Attention (GQA) further optimize KV cache memory usage by sharing keys and values across multiple query heads.

Reasoning Models and GRPO

Modern reasoning models move toward “Inference-time scaling.” A key breakthrough is Group Relative Policy Optimization (GRPO). Unlike traditional RL, GRPO removes the need for a separate critic model by calculating a “Relative” score based on the group mean of multiple completions for the same prompt. This significantly reduces VRAM overhead during the alignment phase and empowers the model to engage in self-refinement and multi-step logic.

Final Synthesis

The “LLMs-from-scratch” methodology provides a comprehensive framework for innovation. By mastering every layer from the byte-level fallback of BPE to the group-relative score of GRPO architects can build the next generation of transparent, efficient, and highly specialized AI systems.

Need High-Impact Technical Content for Your Engineering Team?

I partner with developer-tooling startups, SaaS platforms, and engineering teams to translate complex infrastructure, agentic systems, and backend architecture into publication-grade technical writing.

Whether you need deep-dive architecture essays, hands-on developer tutorials, or technical counter-narratives:

Top comments (0)