POST 1 of 5 MorningAI/MLConcept
A transformer block is two sublayers
📅 Day 53. The transformer block — the Lego brick that stacks to build every modern LLM. 🏗 The block has two sublayers. Each sublayer is wrapped in a 'pre-norm + residual' pattern. 📐 Sublayer 1 — Multi-head attention. → LayerNorm the input. → Multi-head attention on the normalised input. → Add the result back to the original input (residual connection). 📐 Sublayer 2 — MLP (feed-forward). → LayerNorm the result of sublayer 1. → Two linear layers with GELU in between (typically inner dim is 4x outer dim). → Add the result back (residual connection). That's it. One block has these two sublayers. Stack 12, 24, 48, 96 of them, add an embedding layer at the bottom and an output head at the top, and you have GPT, Llama, BERT, Claude. 🌐 Same skeleton; different scales: → BERT-base — 12 blocks, hidden dim 768. ~110M parameters. → GPT-3 — 96 blocks, hidden dim 12288. 175B parameters. → Llama-3-8B — 32 blocks, hidden dim 4096. 8B parameters. → Llama-3-405B — 126 blocks, hidden dim 16384. 405B parameters. 🧠 The two simple sublayers, plus residual connections and layer normalisation, are what make 100B-parameter models trainable. Residuals carry gradient through deep stacks; LayerNorm keeps activation magnitudes stable. 🔧 Modern variations (post-2020): → RMSNorm replaces LayerNorm. Simpler, slightly faster, same effect. → SwiGLU replaces standard MLP. Slightly more parameters, better performance. → Rotary positional encodings (RoPE) replace absolute position embeddings. Better extrapolation to longer sequences. → Grouped-query attention (GQA) reduces inference memory. Used in Llama-2/3. All these are optimisations on the same core block. The 2017 design holds. 💡 Master the block. Stack to taste.
#NLP#Transformers#LLM#AI#100DaysOfCode#Transformers