The reasoning methods never required reading the transformer. This module is the map of the particular network they call: Qwen3 at 0.6B parameters, in the implementation shipped with the course code. It is a decoder-only transformer. The pieces that differ from a 2019 GPT-2 block are the ones worth naming, because they show up in the shapes of the cache and in the tokenizer behavior from Module 2.

The 0.6B configuration

Vocabulary
Layers
Embedding width
Query heads
Key-value groups
Head dimension
Feed-forward width
RoPE base
Context length
Dtypebfloat16
QK normon

The context field is the maximum position the cache and the rotary table will accept. The model was trained with a shorter context, tokens. Treat as a hard ceiling in this code, not as a promise of training-time quality out to that length.

The forward pass is: token embedding, blocks, a final RMSNorm, a linear map to vocabulary logits. The output projection is its own matrix.

One block

input attention FFN next block
Figure 9.1. Each box is wrapped in a residual. Normalization sits on the branch, before attention and before the feed-forward.

Figure 9.1 is pre-norm. If enters the attention branch,

and the feed-forward is the same pattern with a second RMSNorm. The shortcut is what makes a stack of blocks trainable. The reasoning algorithms do not touch this identity. They only read logits and, during GRPO or distillation, send gradients back through it.

RMSNorm

Definition 9.1 (RMSNorm).

For a vector and a learned scale ,

with . There is no mean subtraction and no bias.

Definition 9.1 replaces LayerNorm in this model. Dropping the mean is the whole change. The same operation is also applied to queries and keys inside attention (QK norm), with the scale learned per head dimension, before the rotary rotation. QK norm keeps the dot products that become attention scores from drifting in magnitude. It is on for this configuration.

The code computes the square mean in float32 even when the activations are bfloat16, then casts back. That is a numerical detail, not a different definition.

The feed-forward

The inner map is a gated linear unit, not a two-layer MLP.

Definition 9.2 (SwiGLU feed-forward).

With and ,

where .

Definition 9.2 has three matrices. is the gate, is the up-projection, and maps back down to the residual width. For this model and . There are no biases on these linears.

A third matrix looks like “more parameters than GPT-2’s two-layer MLP.” Compare at equal parameter count, not at equal . The extra matrix is paid for by the choice of width.

Grouped-query attention

Sixteen query heads and eight key-value groups means each key head is shared by two query heads. The head dimension is , so queries are and keys and values are per token.

Definition 9.3 (Grouped-query attention).

Split the queries into groups of heads. Each group attends to its own key and value heads. Scores inside a head are

where is on allowed positions and on future positions.

Definition 9.3 is why the KV cache from Definition 2.2 stores eight key heads and eight value heads per layer, not sixteen. Sharing keys is the memory saving. The attention math per query head is otherwise ordinary scaled dot-product attention.

The mask is the piece the cache changes. On a full sequence it is a triangular matrix. On a single new token it is a row that allows every cached position.

Rotary positions

Positions are not added as a learned vector. They rotate queries and keys. This repository uses the split-halves layout: the head vector is cut into a first half and a second half, and those halves are mixed by and . The interleaved even-odd layout used in the original RoPE paper encodes the same relative positions up to the order of coordinates. Mixing the two layouts when you load weights is a shape-compatible and silently wrong model.

The angles depend on the token index and on a set of inverse frequencies with . A cached decode must pass the absolute index of the new token. Restarting the index at zero rotates the new token as if it were the first token of the prompt.

What you can ignore while reading the earlier modules

Nothing in modules 1–8 changes RMSNorm, the gating, or the rotary frequencies. Those modules treat the network as a map from ids to logits, plus a cache. Come back here when a shape error in the cache, a tokenizer id above , or a mismatched RoPE layout is the bug in front of you. Larger Qwen3 sizes, including mixture-of-experts variants, use the same block with different widths. The loading path for those checkpoints is appendix D of the repository, not a different architecture.

Code

The readable implementation is qwen3.py. The notebook that walks the same file is chC_main.ipynb. RoPE was introduced by Su et al., 2021.