08 — Architecture

Decoder-Only Models#

GPT · LLaMA · Causal LMMathematical 1 min read
◆ The PatternThe dominant architecture behind GPT, LLaMA, Mistral, and most modern LLMs

Decoder-only models use causal (left-to-right) masking so each token can only attend to itself and previous tokens — never the future. This enables autoregressive generation: predict next token, append, repeat.

Causal mask: M_ij = 0 if j ≤ i, else −∞
Upper triangle set to −∞ before softmax → zeroes out future attention weights.
P(text) = ∏ P(token_t | token_1, ..., token_{t-1})
Autoregressive factorization — the probability of text as a product of conditional probabilities.

Why decoder-only won: (1) simpler than encoder-decoder, (2) scales better with compute, (3) naturally handles both understanding and generation in a single architecture. The "decoder" name comes from the original Transformer paper where this half decoded outputs.

GPT, LLaMA, Mistral, Gemma and most other published LLMs are decoder-only. The encoder-decoder style (T5, BART) is now mainly used for specialized tasks like translation.
Interactive — causal mask & autoregressive generation

Python — causal attention mask#

Pattern bridge: Autoregressive generation — each token conditioned only on the past. In markets, recency bias makes traders decode only from recent history.
Worked example

A decoder-only model scores a sequence as a product of next-token probabilities. Four tokens with probabilities 0.5, 0.3, 0.9 and 0.2 have a log-probability of −3.61: the sequence as a whole has probability 0.027, and long texts become astronomically unlikely. The causal mask lets each token see only what came before — 50% of the full attention pairs for 1,000 tokens.

Where it misleads
  • Low probability is not low quality: long, specific, correct answers are always improbable sequences.
  • Left-to-right generation cannot revise what it already wrote, which is why planning and self-correction need extra steps.
  • Encoder-decoder models remain competitive for some tasks (translation, structured outputs) at smaller scales.
In code
p = np.array([0.5, 0.3, 0.9, 0.2])              # P(token_t | everything before it)
logp = np.log(p).sum()
seq_prob = np.exp(logp)
n = 1_000
pairs_causal, pairs_full = n * (n + 1) // 2, n * n   # attention pairs with and without the causal mask
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.

The same idea with a library
import torch

def causal_mask(seq_len):
    """Lower-triangular mask for causal (left-to-right) attention"""
    mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1)
    return mask.masked_fill(mask == 1, float('-inf'))

# Usage in attention
scores = Q @ K.T / d_k**0.5
scores = scores + causal_mask(seq_len)  # mask future
weights = torch.softmax(scores, dim=-1)
output = weights @ V
Sources
  • A. Radford, K. Narasimhan, T. Salimans & I. Sutskever, “Improving Language Understanding by Generative Pre-Training”, OpenAI, 2018
  • T. Brown et al., “Language Models Are Few-Shot Learners”, NeurIPS, 2020
  • T. Wang et al., “What Language Model Architecture and Pretraining Objective Work Best for Zero-Shot Generalization?”, ICML, 2022

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Transformer Block
Open in the full reader, with the topic sidebar