Can you walk through the forward pass of a sequence through a single decoder-only Transformer block?
Last updated
A decoder block maps a (B, T, d) tensor to the same shape via two pre-norm residual sub-layers:
x = x + Attn(LN(x))thenx = x + FFN(LN(x)).Attention: project to Q, K, V, split into heads (apply RoPE to Q/K), compute
QKᵀ/√d_kwith a causal -inf mask so token t sees only positions ≤ t. Then softmax, weight V, merge heads and applyW_O.The FFN expands each token to about 4d, applies GELU or SwiGLU, and projects back. Pre-norm keeps the residual stream an identity path for stable gradients in deep stacks.
- https://github.com/ombharatiya/AI-Engineer-Interview-Questions/blob/main/02-llm-fundamentals/questions.md
- https://waylandz.com/llm-transformer-book-en/chapter-15-full-forward-pass/
- https://medium.com/@rkumar70900/building-my-first-transformer-decoder-a-journey-from-scratch-to-pytorch-modules-0d30b6eca0c2