GPT-3 › Architectural Details

Michael BrenndoerferUpdated July 21, 202510 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 199GPT-3 › Architectural Details
Reading section 4 of 25 · Architectural Details · 10 minSection 4 of 25 · 10 min

Book contents

Section 4 of 25 · 10 min read

GPT-3 › Architectural DetailsOne section · Sections in this chapter4 / 25 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

GPT-3 uses the same fundamental decoder-only transformer architecture as GPT-2. The core design remains unchanged: a stack of transformer decoder blocks with masked self-attention, each followed by a feed-forward network. This continuity was a deliberate choice. The GPT-3 authors wanted to isolate the effect of scale, not architecture innovation. By keeping the design consistent with GPT-2, any capability differences observed between the models can be attributed to size rather than architectural changes.

The key architectural differences from GPT-2 include:

  • Alternating dense and sparse attention: In some layers, GPT-3 uses sparse attention patterns that attend to every other token, reducing computational cost for long sequences
  • Learned position embeddings: Like GPT-2, positions are represented by learned embeddings rather than sinusoidal functions
  • Pre-layer normalization: Layer normalization is applied before each sublayer (attention and FFN) rather than after, following the Pre-LN configuration that stabilizes training for deep networks

Pre-layer normalization (Pre-LN) deserves particular attention. In the original transformer design, layer normalization was applied after the residual connection, a pattern called Post-LN. For shallow networks, this works fine. But for very deep networks like GPT-3 with 96 layers, Post-LN creates gradient flow problems: the gradients must pass through each normalization operation during backpropagation, and at great depths this can cause gradients to vanish or explode. Pre-LN addresses this by normalizing before the sublayer operation. This keeps the residual connections carry gradients cleanly from output back to input regardless of depth. This seemingly minor change was essential for training stability at 96 layers.

Parameter Count Calculation

When we say GPT-3 has 175 billion parameters, what exactly are we counting? Understanding where these parameters live reveals the model's structure and helps explain why scale matters so much for capability.

A transformer model stores its learned knowledge in weight matrices. Each weight is a single floating-point number that the model adjusts during training. The total count of these numbers determines the model's capacity to represent patterns in language. To understand GPT-3's scale, we need to trace through each component and count its parameters systematically.

The Building Blocks

A GPT-style model consists of several distinct components, each contributing parameters:

  1. Token embeddings: A lookup table that converts each vocabulary token into a dense vector
  2. Position embeddings: A separate lookup table that encodes where each token appears in the sequence
  3. Transformer layers: The repeated blocks that process and transform token representations
  4. Final layer normalization: A normalization layer before the output projection
  5. Output projection: A matrix that converts hidden states back to vocabulary probabilities

Let's formalize this. For a model with vocabulary size VV, maximum sequence length LL, model dimension dd, number of layers NN, and feed-forward dimension dffd_{ff}, the total parameter count is:

Ptotal=Pembed+Ppos+N⋅Player+Pnorm+PoutputP_{\text{total}} = P_{\text{embed}} + P_{\text{pos}} + N \cdot P_{\text{layer}} + P_{\text{norm}} + P_{\text{output}}

where:

  • Pembed=V⋅dP_{\text{embed}} = V \cdot d: token embedding parameters. Each of the VV vocabulary tokens gets its own dd-dimensional vector, requiring V×dV \times d parameters total.
  • Ppos=L⋅dP_{\text{pos}} = L \cdot d: position embedding parameters. Each of the LL possible positions gets a dd-dimensional vector.
  • PlayerP_{\text{layer}}: parameters per transformer layer, which we'll derive below.
  • Pnorm=2dP_{\text{norm}} = 2d: final layer normalization has a learnable scale and bias vector, each of dimension dd.
  • Poutput=d⋅VP_{\text{output}} = d \cdot V: the output projection maps from hidden dimension dd back to vocabulary size VV. This is often weight-tied with the token embeddings (sharing the same matrix), but GPT-3 uses separate weights.

The formula captures the intuition that larger vocabularies, longer sequences, and higher dimensions all increase parameter count. But the dominant term is N⋅PlayerN \cdot P_{\text{layer}}: the transformer layers, repeated NN times.

Inside a Transformer Layer

Each transformer layer consists of two sublayers, each followed by layer normalization:

  1. Multi-head self-attention: Allows tokens to gather information from other positions
  2. Feed-forward network (FFN): Applies the same transformation to each position independently

The attention sublayer requires four projection matrices: Query (WQ\mathbf{W}_Q), Key (WK\mathbf{W}_K), Value (WV\mathbf{W}_V), and Output (WO\mathbf{W}_O). Each matrix has shape d×dd \times d, contributing d2d^2 parameters. With four matrices, attention adds 4d24d^2 parameters.

The feed-forward network consists of two linear transformations. The first expands from dimension dd to dffd_{ff} (typically 4d4d), and the second contracts back from dffd_{ff} to dd. This requires d×dff+dff×d=2⋅d⋅dffd \times d_{ff} + d_{ff} \times d = 2 \cdot d \cdot d_{ff} parameters.

Each layer also has two layer normalizations (one before attention, one before FFN in the Pre-LN configuration), each with scale and bias vectors of dimension dd. This adds 4d4d parameters per layer.

Putting it together:

Player=Pattn+Pffn+Player_normsP_{\text{layer}} = P_{\text{attn}} + P_{\text{ffn}} + P_{\text{layer\_norms}}

where:

  • Pattn=4d2P_{\text{attn}} = 4d^2: the four attention projection matrices (WQ\mathbf{W}_Q, WK\mathbf{W}_K, WV\mathbf{W}_V, WO\mathbf{W}_O)
  • Pffn=2⋅d⋅dffP_{\text{ffn}} = 2 \cdot d \cdot d_{ff}: the two feed-forward linear layers. With dff=4dd_{ff} = 4d, this becomes 8d28d^2.
  • Player_norms=4dP_{\text{layer\_norms}} = 4d: scale and bias for two layer normalizations

Notice that the per-layer count scales with d2d^2. This quadratic dependence explains why increasing the hidden dimension has such a dramatic effect on model size: doubling dd quadruples the parameters in each layer. It also explains why GPT-3's hidden dimension of 12,288 is so consequential: compared to GPT-2's 1,600, this is roughly an 8x increase in dd, which translates to a 64x increase in parameters per layer from the quadratic terms alone.

A Worked Example: GPT-3 175B

Let's apply these formulas to GPT-3's actual configuration:

  • Vocabulary size: V=50257V = 50257 tokens (the GPT-2 tokenizer vocabulary)
  • Maximum sequence length: L=2048L = 2048 tokens
  • Model dimension: d=12288d = 12288
  • Number of layers: N=96N = 96
  • Feed-forward dimension: dff=4×12288=49152d_{ff} = 4 \times 12288 = 49152

First, we calculate the per-layer parameters:

Pattn=4×122882=4×150,994,944=603,979,776Pffn=2×12288×49152=1,207,959,552Player_norms=4×12288=49,152Player=603,979,776+1,207,959,552+49,152≈1.81 billion\begin{aligned} P_{\text{attn}} &= 4 \times 12288^2 = 4 \times 150{,}994{,}944 = 603{,}979{,}776 \\ P_{\text{ffn}} &= 2 \times 12288 \times 49152 = 1{,}207{,}959{,}552 \\ P_{\text{layer\_norms}} &= 4 \times 12288 = 49{,}152 \\ P_{\text{layer}} &= 603{,}979{,}776 + 1{,}207{,}959{,}552 + 49{,}152 \approx 1.81 \text{ billion} \end{aligned}

Each layer contributes about 1.81 billion parameters. With 96 layers, the transformer stack alone accounts for approximately 174 billion parameters. The remaining 1+ billion come from embeddings and the output projection.

This breakdown reveals an important pattern: the transformer layers dominate the parameter count. Increasing the number of layers or the hidden dimension has a much larger effect than expanding the vocabulary or context length. If you doubled the vocabulary to 100,514 tokens, the additional parameters would be less than 1.5 billion, barely 1% of the total. But if you doubled the hidden dimension to 24,576, you would quadruple the per-layer parameters and nearly quadruple the entire model. In practice, this means that the "knowledge" of a GPT-3-scale model lives overwhelmingly in its attention and FFN matrices, not in the embedding lookup tables.

Implementation

Let's implement this calculation to verify our formulas and explore how parameters distribute across components:

In[7]:
Code
def compute_gpt3_parameters(
    vocab_size=50257,
    n_layers=96,
    d_model=12288,
    n_heads=96,
    d_ff=None,
    max_seq_len=2048,
):
    """
    Calculate parameter count for a GPT-3 style model.

    The feed-forward dimension is 4x the model dimension by default.
    """
    if d_ff is None:
        d_ff = 4 * d_model

    # Token embeddings
    token_embedding_params = vocab_size * d_model

    # Position embeddings
    position_embedding_params = max_seq_len * d_model

    # Per-layer parameters
    # Attention: Q, K, V, O projections
    attention_params_per_layer = 4 * d_model * d_model

    # FFN: two linear layers
    ffn_params_per_layer = 2 * d_model * d_ff

    # Layer norms (2 per layer, each has scale and bias)
    layernorm_params_per_layer = 4 * d_model

    params_per_layer = (
        attention_params_per_layer
        + ffn_params_per_layer
        + layernorm_params_per_layer
    )

    total_layer_params = n_layers * params_per_layer

    # Final layer norm
    final_norm_params = 2 * d_model

    # Output projection (weight-tied with embedding or separate)
    # GPT-3 uses separate output weights
    output_params = d_model * vocab_size

    total = (
        token_embedding_params
        + position_embedding_params
        + total_layer_params
        + final_norm_params
        + output_params
    )

    return {
        "token_embedding": token_embedding_params,
        "position_embedding": position_embedding_params,
        "layers": total_layer_params,
        "final_norm": final_norm_params,
        "output": output_params,
        "total": total,
    }
Out[8]:
Console
GPT-3 175B Parameter Breakdown
=============================================
Token embeddings:        0.62B
Position embeddings:     0.03B
Transformer layers:    173.95B
Final layer norm:        0.02M
Output projection:       0.62B
---------------------------------------------
Total:                 175.21B

The output confirms our manual calculation. The vast majority of parameters, over 173 billion, reside in the 96 transformer layers. This makes sense: each layer contributes about 1.8 billion parameters through its attention projections and feed-forward network, and with 96 layers, these add up quickly.

The embedding layers tell an interesting story. Despite handling a vocabulary of 50,257 tokens, the token embeddings account for only about 0.62 billion parameters, less than 1% of the total. The position embeddings are even smaller at 0.025 billion (25 million) parameters. The output projection mirrors the token embedding in size but is kept separate in GPT-3 rather than being weight-tied.

This distribution has practical implications. If you want a larger model, adding layers or increasing the hidden dimension is far more effective than expanding the vocabulary or context length. Conversely, if you want a smaller model, the layers are where you need to cut.

The visualization below makes this distribution strikingly clear:

Out[9]:
Visualization
Pie chart showing GPT-3 parameter distribution with transformer layers as the largest segment.
Parameter distribution in GPT-3 175B. The transformer layers dominate, containing over 99% of all parameters. Embedding and output projections together account for less than 2%, illustrating how the model's representational capacity lives almost entirely in the processing layers rather than the input/output interfaces.

The pie chart reveals just how lopsided the distribution is: the transformer layers are so dominant that the embeddings and output projection are barely visible. This concentration of parameters in the processing layers, rather than the input/output interfaces, reflects where the model's computational "thinking" happens. The embeddings convert tokens to vectors and back, but the layers are where the model transforms and reasons about those representations. The pattern also has an important implication for interpretability: if you want to understand what a model knows or how it reasons, you need to study the attention and FFN matrices in the processing layers, not the embedding tables.