Skip to content
AI360Xpert
Beta
Playgrounds

Generative AI

Full Transformer Block

how a single transformer block processes tokens through multi-head attention and a feed-forward network with residual connections

Stage 1 of 6: Embeddings

Stage: Embeddings. Positional encoding on, 2 attention heads. Showing the 3x12 vector matrix for this stage.

  • Token
  • Architectural Block
  • Vector Matrix Cell
  • Attention Weight
Active Tokens3Active Tokens: 3
Attention Heads2Attention Heads: 2
Model Dimension12Model Dimension: 12

Tokens are mapped to continuous vector representations.

  1. Input
  2. Pos Encoding
  3. MHA
  4. Norm 1
  5. FFN
  6. Norm 2

Check your understanding

The Transformer block is the fundamental building block of modern large language models, combining multi-head attention and a feed-forward network with residual connections into a single cohesive pipeline.

The Transformer block is the fundamental building block of modern large language models. It combines several critical layers into a single cohesive pipeline.

First, tokens are converted into embeddings, which are continuous vector representations of discrete words or subwords. Since transformers process all tokens in parallel, they have no inherent concept of order. To fix this, a positional encoding is added to each embedding, injecting a unique signal that depends on the token's position in the sequence.

The core of the block is multi-head attention. Each head projects the input into its own query, key and value vectors through a fixed linear projection, computes a weighted sum of values based on the similarity between queries and keys, and every head's output is then mixed back into one vector by a further output projection. Splitting this into multiple heads allows the model to attend to different types of relationships simultaneously, each in its own subspace.

After attention, a residual connection adds the original input back to the attention output. This helps preserve the original information and allows gradients to flow easily during training, preventing the vanishing gradient problem. The result is then normalized using Layer Normalization, which stabilizes the activations and speeds up convergence.

Next, a feed-forward network (FFN) independently processes the representation of each token. It expands each token's vector to a wider hidden layer, applies a non-linearity, and projects it back down to the original size. While attention mixes information across the sequence, the FFN transforms the information within each token's representation, with no visibility into any other token.

Finally, another residual connection and layer normalization step is applied to the FFN output, completing the Transformer block. This pipeline of Attention -> Add & Norm -> FFN -> Add & Norm can be stacked many times to build deep, powerful models like GPT and BERT.

Notice that nothing in attention or the feed-forward network reads a token's position — attention compares every pair of tokens the same way regardless of where they sit, and the FFN processes each token's vector independently of its neighbours. Positional encoding is the only signal in the whole block that breaks that symmetry: with it removed, permuting the input tokens permutes the output identically, and nothing else changes. Turn it off in the lab and swap two tokens to see the swap propagate through cleanly, with no side effects.

Reference

Residual Connection
\text{LayerNorm}(x + \text{Sublayer}(x))
Sublayer
Multi-Head Attention or Feed-Forward Network
Attention (per head)
\text{softmax}(QK^\top / \sqrt{d_k})V
Multi-Head Attention
W_O \cdot \text{concat}(\text{head}_1, \dots, \text{head}_h)
Layer Norm
(x - \mu) / \sqrt{\sigma^2 + \epsilon}
Feed-Forward
W_2 \cdot \text{ReLU}(W_1 x)

Break it on purpose

Without positional encoding, tokens carry no signal for their position in the sequence, so the block processes them as an unordered set: swapping two tokens’ order changes nothing about how either one is read.