Module 19 · Part IV · The Modern Architecture

Reading a Model Card

GPT-2, LLaMA, Gemma and Qwen Compared, a Config File Field by Field, and Estimating Parameters, Cache and Compute

By the end you can read a real model config and say what each field implies for memory, speed and quality.

Builds onModule 15·
Check yourself ▸

Before you start, can you already…

  • ☐turn the 2017 block into a 2024 block and justify each change. (Module 15)

If any is shaky, the linked module teaches it.

Open in Colab
2 Hours·Elective·Advanced·0/3

An open model's card and configuration file answer most of what this course has taught you to ask: how wide, how deep, which attention, which position encoding, how big a vocabulary. This module compares the families you have met, then reads a configuration field by field and turns it into parameters, KV cache and compute per token.

19.1 Comparing the Architectures

The table collects the choices of Modules 15 to 17 for four families.

FeatureGPT-2 (2019)LLaMA-3 (2024)Gemma 2 (2024)Qwen2 / Qwen2.5 (2024)
NormalizationLayerNorm, before each sub-layer (Pre-LN)RMSNorm, before each sub-layerRMSNorm before and after each sub-layerRMSNorm, before each sub-layer
Position encodingLearned absolute positionsRoPE ()RoPE (θ=10k\theta = 10\text{k}); Gemma 3: 10k local, 1M globalRoPE (θ=1M\theta = 1\text{M}), YaRN (section 15.2) for long context
ActivationGELUSwiGLUGeGLU (gated GELU)SwiGLU
AttentionMulti-head (MHA)Grouped-query (GQA, 8 KV heads)GQA, local and global layers alternating 1:1; Gemma 3: 5 local to 1 globalGQA
Logit soft-cappingNoneNoneYes: κ=50\kappa = 50 in attention, κ=30\kappa = 30 on output logits (Gemma 3 uses QK-norm instead)None
Vocabulary size50,257 tokens128,256 tokens256,000 tokens152,064 embedding rows
Feed-forwardDense, 2 matricesDense SwiGLU, 3 matricesDense GeGLU, 3 matricesDense SwiGLU; MoE variant: 64 routed + 8 shared experts
Model width 768 (Small)4,096 (8B)3,584 (9B)3,584 (Qwen2.5-7B)

Every row is from the cited papers and model cards, not measured; none of these architectures is in the reference repository.

How transformer designs differ

Compare the design choices of well-known models, from the original 2017 Transformer to GPT-2, LLaMA 3, Gemma 2 and Qwen2.5, a vision-language model, and two specialized models (PP-OCRv6 for reading text in images, Qwen3-TTS for speech). Pick a model on the left to see its details.

Gemma 2

Google DeepMind • Released 2024 • Frontier-LLM

GQA
NORMALIZATIONSandwich norm: RMSNorm both before and after each sub-layer
HOW POSITION IS ENCODEDRoPE with base θ = 10,000
ATTENTIONGQA with local (sliding-window, W = 4,096) and global layers alternating 1:1
FEED-FORWARD ACTIVATIONGeGLU (a gated feed-forward that uses GELU)
VOCABULARY256,000 (SentencePiece)
CONTEXT LENGTH8,192 tokens
A characteristic equationlogitscapped=C⋅tanh⁡(logitsC)\text{logits}_{\text{capped}} = C \cdot \tanh\left(\frac{\text{logits}}{C}\right)
What stands out:
  • Local layers let each token see only the previous 4,096 tokens; global layers see everything
  • Soft-capping, C·tanh(x / C), keeps values below C: C = 50 on attention scores, 30 on output logits
  • Token embeddings are multiplied by √d_model
  • Gemma 3 (2025) changed this: 5 local layers per global one, W = 1,024, and QK-norm in place of soft-capping
Predict, then try

Before opening it, guess which single feature changed the most between GPT-2 and the 2024 families: normalization, position encoding, feed-forward, or attention.

In words. The explorer lists architectures from the original Transformer and GPT-2 to LLaMA 3, Gemma 2, Qwen, a vision-language model, PP-OCR and Qwen3-TTS, and shows the chosen one's normalization, position encoding, activation, attention, vocabulary and loss beside the others.

19.2 A Config File, Field by FieldRun this section

Models on Hugging Face ship a config.json. Here is one for an 8-billion-parameter LLaMA-3-style model:

json
{
  "vocab_size": 128256,
  "hidden_size": 4096,
  "num_hidden_layers": 32,
  "num_attention_heads": 32,
  "num_key_value_heads": 8,
  "intermediate_size": 14336,
  "hidden_act": "silu",
  "max_position_embeddings": 8192,
  "rope_theta": 500000.0,
  "rms_norm_eps": 1e-05,
  "tie_word_embeddings": false,
  "torch_dtype": "bfloat16"
}
fieldmeaningsection
vocab_size∣V∣\lvert V\rvert, rows of the embedding table9.4
hidden_sizedmodeld_{\text{model}}, the width of the residual stream1.5, 7.6
num_hidden_layersthe number of blocks LL7.6
num_attention_headsquery heads hqh_q; the head width is hidden_size / num_attention_heads = 1286.6
num_key_value_headshkvh_{kv}; fewer than hqh_q means grouped-query attention, here 4 query heads per KV head15.5
intermediate_sizedffd_{ff}, the inner width of the feed-forward network7.4, 15.4
hidden_act: siluSwish with ; with three matrices this is SwiGLU15.4
max_position_embeddingsthe context length it was trained for15.2
rope_thetathe RoPE base (10,000 in the original; raised for long context)15.1, 15.2
rms_norm_epsthe inside RMSNorm15.3
tie_word_embeddingswhether the output layer reuses the embedding table7.5
torch_dtypethe format the weights are stored in, 2 bytes per number for bfloat161.7, 24.1

Other fields signal Modules 16 to 18: sliding_window or per-layer attention types for local and global layers, num_experts and num_experts_per_tok for mixture of experts, rope_scaling for context extension, and final_logit_softcapping for Gemma's soft cap.

19.3 Estimating Parameters, Cache, and ComputeRun this section

With d=d = hidden_size, L=L = num_hidden_layers, dh=d/hqd_h = d / h_q and dff=d_{ff} = intermediate_size:

  • Parameters per block. Attention has and WOW^O of size d×dd \times d, and WKW^K and WVW^V of size d×hkvdhd \times h_{kv} d_h, so 2d2+2d hkvdh2d^2 + 2 d\,h_{kv} d_h. A SwiGLU feed-forward network has three matrices, 3 d dff3\,d\,d_{ff}. Norms add a few times dd.
  • Total parameters. LL blocks plus the embedding table ∣V∣d\lvert V\rvert d, plus the same again for the output layer if it is not tied.
  • KV cache per sequence (sections 15.5 and 23.1): 2×L×hkv×dh×(context)×(bytes)2 \times L \times h_{kv} \times d_h \times (\text{context}) \times (\text{bytes}).
  • Compute per token, from section 10.5: about 2×2 \times (parameters) FLOPs in the forward pass, plus the attention scores, about 2×L×d×(context)2 \times L \times d \times (\text{context}) more, which matters only for long contexts.

Worked example. For the configuration above: attention is 2⋅40962+2⋅4096⋅8⋅128=41.92 \cdot 4096^2 + 2 \cdot 4096 \cdot 8 \cdot 128 = 41.9 million per block, SwiGLU is 3⋅4096⋅14,336=176.23 \cdot 4096 \cdot 14{,}336 = 176.2 million, so a block is about 218 million and 32 blocks about 6.98 billion. The untied embedding and output layer add 2⋅128,256⋅4096=1.052 \cdot 128{,}256 \cdot 4096 = 1.05 billion, for a total of about 8.03 billion: the "8B" in the name. At 8,192 tokens in bf16 the KV cache is 2⋅32⋅8⋅128⋅8192⋅22 \cdot 32 \cdot 8 \cdot 128 \cdot 8192 \cdot 2 bytes =1= 1 GiB per sequence.

Figure 19.3
From a config file to parameters, cache and compute

Edit the configuration or load a preset. The readout applies the formulas of this section.

Biases and the small norm weights are ignored. A gated activation (silu) is counted as three feed-forward matrices, otherwise two. KV cache and weights use the bytes of torch_dtype.
Predict, then try

Load the 8B configuration, then change num_key_value_heads from 8 to 32. Before you look, predict which of the three outputs change: parameters, KV cache, or compute per token.

In words. An editable model configuration, with presets, and a readout of head width, parameters per block and in total, KV cache at a chosen context, and forward compute per token.

Exercises

Check

Which configuration field tells you whether a model uses grouped-query attention?

Apply

A configuration has hidden_size 2048, num_attention_heads 16, num_key_value_heads 4. What is the head width, and how many query heads share each key-value head?

Apply

Estimate the KV cache in GiB for one 32,768-token sequence of a model with 28 layers, 4 key-value heads, head width 128, in bf16.

Apply

How many parameters does one SwiGLU feed-forward network have at hidden_size 4096 and intermediate_size 14336, in millions?

Extend

A configuration has num_experts 128 and num_experts_per_tok 8. How do you expect its total parameter count and its compute per token to compare with a dense model of the same width and depth whose feed-forward network is the size of one expert?

Core Academic References

  • Radford et al., OpenAI (2019): Language Models are Unsupervised Multitask Learners (GPT-2). Technical Report.
  • Grattafiori et al. (2024): The Llama 3 Herd of Models. arXiv:2407.21783
  • Gemma Team, Google DeepMind (2024): Gemma 2: Improving Open Language Models at a Practical Size. arXiv:2408.00118
  • Qwen Team, Alibaba (2024): Qwen2.5 Technical Report. arXiv:2412.15115
  • Mitchell et al. (2019): Model Cards for Model Reporting. FAT* 2019. arXiv:1810.03993

Measured labs

These labs test what this module says by running real code. Each card states the idea, what the experiment found, and what it does not prove. The numbers come from experiments/results.json, the output of running experiments/run_all.sh on the tiny-transformers reference code at version 137f7ef. The project checks fail if a measured number in the module text stops matching that file.

Lab 5 — what this repository does and does not implement

What the module says

Modules 16 to 19 compare the Gemma and Qwen model families: alternating local and global attention, RMSNorm before and after each sub-layer, soft-capping of scores, mixture-of-experts routing with shared experts, and capacity limits per expert.

What the experiment found

None of these designs is in the reference repository. What this lab can show is the gap in scale: the reference model has 112,323 parameters and 2 layers, while the models in Module 17 have up to 64 routed experts and vocabularies of 152,064 to 256,000 tokens. The comparison table in 5.4 comes from the published papers, not from measurements.

reference model parameters
112,323
reference model layers
2
reference model attention heads
2
reference vocabulary size
67

single characters; section 19.1 lists 152,064–256,000 for the large models

What this does not prove

The architecture comparisons of Modules 16 to 19 are description. None of their numbers was measured here, and this panel says so rather than suggest otherwise. Treat them as taken from the cited papers and model cards, not from running code.

Run it yourself

bash
cd tiny-transformers && .venv/bin/python experiments/parameter_count.py

The code being tested