The problem
Write the block that OLMo 3 stacks 32 or 64 times, exactly as the released implementation computes it, but at toy size. It is the LLaMA (Large Language Model Meta AI) block of section 7 with four changes: QK-norm, a different RoPE (rotary position embedding) layout, grouped-query attention, and the norms moved to the output of each sub-layer.
For x of shape , with query heads, key/value heads and head size , the block does seven things.
The seven steps
- Projections with QK-norm. , , . The norms (root mean square layer normalization) act on the whole projection ( or features), before it is split into heads.
- Heads. Reshape to and , to (section 7: reshape the last axis first, then transpose).
- RoPE on and (the given
apply_rope). - GQA (grouped-query attention). Each key/value head serves consecutive query heads: repeat the key and value heads with
np.repeat(section 7). - Attention with the given boolean
mask(causal, or sliding-window), then merge the heads back to and multiply by . - Reordered norm and first residual. .
- Feed-forward part and second residual. , where the feed-forward part is the SwiGLU (Swish-gated linear unit) MLP (multilayer perceptron) with gate , up projection and down projection , and is the elementwise product.
Example. What changes compared with the block of section 7, with the same input :
- In the LLaMA block: . The norm sees the stream and the attention output is added as it is, with whatever size it has.
- OLMo 3: . The attention reads the raw stream, and its output is normalized before it is added: an attention output of adds about times the gain.
What to implement
olmo3_block(x, p, n_heads, n_kv_heads, mask). p is a dict with:
"Wq"of shape ,"Wk"and"Wv"of shape ,"Wo"of shape ;"q_norm"of shape and"k_norm"of shape : the QK-norm gains;"attn_norm"and"mlp_norm"of shape ;"Wg"and"Wu"of shape ,"Wd"of shape .
The helpers rmsnorm, apply_rope, attention and silu are given.
Requirements
- The norms of steps 6 and 7 go on the output of each sub-layer, not on its input: this is the "reordered norm" of OLMo 2 and 3, the main difference from the LLaMA block.
- No loops over heads or positions.