BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Attention and transformers14 of 23 in topic

404. Implement the original post-norm transformer block

MediumAttention and transformers
Asked atAppleDeepMind

Why it matters Knowing post-norm versus pre-norm explains why early deep transformers needed learning-rate warmup and why GPT-2 onward switched.

The original transformer applied LayerNorm AFTER each residual add. Modern GPTs moved it before the sublayer, and comparing the two is a classic exercise.

Write postnorm_block(x, p). x has shape (T, d). p holds Wq, Wk, Wv, Wo (d, d), W1 (d, f), b1 (f,), W2 (f, d), b2 (d,).
1. LN(z) = (z - mean) / sqrt(var + 1e-5) over the last axis, population variance, no gain or bias.
2. Attn(z): q, k, v = z @ Wq, z @ Wk, z @ Wv; scores = q @ k.T / sqrt(d) with a causal mask; softmax rows; return (weights @ v) @ Wo. Single head.
3. FFN(z) = relu(z @ W1 + b1) @ W2 + b2.
4. h = LN(x + Attn(x)); return LN(h + FFN(h)).

Functiondef postnorm_block(x, p)

Example 1

Input: with every weight and bias zero, x = [[1, 2, 3, 4]]

Output: about [[-1.3416, -0.4472, 0.4472, 1.3416]]

Explanation: Since both sublayers output zero.

Example 2

Input: x = X, p = P

Output: [[0.650539, -1.260457, 1.254172, -0.644254], [0.124402, 1.495701, -1.276426, -0.343677], [0.469462, 1.232589, -0.213915, -1.488136]]

Example 3

Input: x = X[:1], p = P

Output: [[0.650539, -1.260457, 1.254172, -0.644254]]

Constraints

  • x has shape (T, d)
  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

Write LN, Attn and FFN as small helpers, then apply them in the post-norm order.

Hint 2

Population variance: np.var with default ddof.

Hint 3

Causal mask: np.tril of ones.

Hint 4

Normalise AFTER each add.

Common mistakes
  • Mixing up the pre-norm and post-norm order.
  • Using the sample variance in LayerNorm.
Understand the idea

A transformer block has two sublayers, attention and a feed-forward network, each wrapped in a residual add and a LayerNorm. The only question is where the norm goes. Post-norm, used in the 2017 transformer, normalises after the add: out = LN(x + Sublayer(x)). Pre-norm, used by GPT-2 and later, normalises the input to the sublayer: out = x + Sublayer(LN(x)).

Tiny case: if all weights are zero, both sublayers output 0. Post-norm then returns LN(LN(x)). For x = [1,2,3,4]: mean 2.5, variance 1.25, so LN gives about [-1.342, -0.447, 0.447, 1.342]; normalising again changes almost nothing.

Rule here: h = LN(x + Attn(x)); y = LN(h + FFN(h)).

Post-norm keeps every block output at unit scale, but the gradient to early layers must pass through many norms, which made deep post-norm models hard to train without warmup. That is why most current LLMs use pre-norm.

Shapes and data flow
x (T, d) -> q, k, v (T, d) -> scores (T, T) -> attn out (T, d) -> h (T, d) -> FFN hidden (T, f) -> out (T, d)
Topics
Attention and transformers
Companies2
  • Apple a candidate reported this question Apple Machine Learning Engineer Interview Questions | Screna AI
  • DeepMind a candidate reported this question Google DeepMind interview guide (landedjobs/ai-interview-guides)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • GPT decoder: causal self-attention for one headMedium
  • Split and merge attention headsMedium
  • Build the ALiBi attention bias for every headMedium
  • Apply a key padding mask in batched attentionMedium
All Attention and transformers problems
Report
404 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def postnorm_block(x, p):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS