Positional embedding (sines and cosines)
FFN: ReLU
LayerNorm, post-norm
LayerNorm in front of the block
LlamaQK-normlots of experimentation
Llama 2 came out, everyone started making minor modifications to itThe residual stream operates as a “parallel highway” for information within a Transformer block.
The Main Path (blue arrows): As data moves through the Self-Attention and Feed-Forward layers, it is refined and transformed to learn complex patterns and relationships.
The Residual Stream (yellow arrows)::
This path allows the original, unprocessed input from the previous layer to skip directly to the block’s end, where it is added back to the processed signal.
This mechanism is fundamental for Large Language Models because it ensures that the model preserves previous context and original information as it gets deeper.
Without this direct pathway, essential context could be lost during the heavy processing required at each layer, making it impossible to train deep networks effectively.


smoother gradient flow
⚠️ All modern LLMs push the layer norm outside the residual stream (but before the compuations/multi-head attention)

allow gradients to flow backwards more easily
LayerNorm: normalize the input features (per token) across the hidden dimension
RMSNorm: does not add a bias term unlike layer norm or subtract mean , it only normalize by the square root of the mean square

RMSNorm is faster computationally
💡 This is where architecture interacts with system design.
Think back to arithmetic intensity and the need to keep the GPUs busy
do not move memory back and forth between memory and the compute units
inefficient use of GPU
FLOPS important but runtime is what matters for inference speed
Keep the GPUs fed!
RMSNorm can still matter due to data movement
Bias terms not helpful for transformers
Llama 2 FFN(x) = (xW1 + b1, xW2 + b2, xW3 + b3)
Most implementations now: FFN(x) = sigma(xW1)W2
reasons: memory, efficiency, training stability
get the easy systems win!
drop bias terms: keep the system more arithematically intense
ChinchillaGeLU: Gaussian noise around 0 (used in GPT-2)
Gated ReLU (reGLU): gating is very effective
SwiGLU: swish is x * sigmoid(x)
normal transformer blocks are serial: they compute attention and then FFN/MLP
serial still preferred (depth loss deleterious)
so need a way to represent position
sines and cosines then you can recover relative positions (similar to a Fourier transform)
Absolute embeddings :
Relative embeddings : add a vector to the attention mechanism
Rope embeddings: RoPE (Rotary Position Embedding) :
inner products are invariant to arbitrary rotations
we want embeddings to be position dependent
RoPE intuition : Concept 🧩 🚀

Image from 🎥 Stanford CS336 course
now you can take inner products
what do you in d dimensions? How do you rotate in higher dimensions?
low frequency parts (f1) change slowly, high frequency parts (f2) change quickly
📝 Image from Paper on ROPE

dff = 4 * d_modelrichness of your MLP (multi layer perceptron)
Exceptions: GLU variants
T5: 64 multiplier to keep GPU busyMulti head attention
aspect ratio: deep vs. shallow
aspect ratio = d_model / n_layer
approx. 100 for most models
100 deep for every layer
deep models are hard to parallelize (Tay et al 2021)
expressiveness reasons to go deep
systems reasons to go wide
Larger vocabulary -> larger embedding matrix
z-loss penalizes large logitsPaLMQK norm
QK-norm addresses attention logits. But there’s a second place large logits show up: the final output head, right before the softmax that turns logits into next-token probabilities
this is addressed by z-loss
Here’s the subtle problem: softmax (and therefore cross-entropy) is shift-invariant, exactly as we proved in section 2. That means cross-entropy loss has no preference at all over the overall scale of the logits - it only cares about the differences between them. If the training data is close to linearly separable, gradient descent will happily keep pushing every logit larger and larger forever (larger logits -> more confident softmax -> lower loss), even though the predictions stop meaningfully improving. Nothing in the loss function says “stop growing.”
z-loss adds a small penalty term that directly targets this: it penalizes $\log Z$ (the log of the softmax normalizer, also called the log-partition function) for straying away from zero:
🎉 🥳Here is a funny comic that summarizes the situation
Imagine you’re trying to bake a soufflé, but every time you put it in the oven, it violently explodes.
To fix it, you attach a metal clamp to the dish. Then you add a rubber band around the middle. Then you wrap the top in foil.
After months of research, you step back and realize: you didn’t invent three revolutionary baking techniques. You just put duct tape on the same problem in three slightly different spots.
That’s modern Large Language Model (LLM) training in a nutshell. When AI models learn, their internal numbers often swing wildly out of control (“loss spikes”).
To stop the AI from losing its mind, researchers invent complex-sounding hacks like LayerNorm, QK Norm, and Z-loss.
The dirty secret? They’re all essentially the exact same mathematical trick—normalizing numbers so they don’t blow up—just slapped onto different parts of the network.
Concept 🧩 🚀 AI engineering in a nutshell:

logits = softcap * tanh(logits / softcap)
Gemma uses this


fewer items to move in and out of memory
KV Cache reused across heads
interleave full and sliding window attention
Cohere command A: every 4th layer is full attention
Concept 🧩 🚀 tradeoff between long context attention and performance

