No history yet

Transformer Mathematical Mechanics

Scaled Dot-Product Attention

The core mechanism of the Transformer is Scaled Dot-Product Attention. It operates on three matrices: Query (Q), Key (K), and Value (V). These matrices are linear projections of the input embeddings. For a given token, its Query vector is matched against the Key vectors of all other tokens in the sequence to produce attention scores. These scores are then used to create a weighted sum of the Value vectors.

Attention(Q,K,V)=softmax(QKTdk)VAttention(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

The Q, K, and V matrices are derived by multiplying the input embedding matrix XX with learned weight matrices WQW_Q, WKW_K, and WVW_V. This allows the model to project the inputs into different representation subspaces, optimizing them for the task of calculating attention.

Q=XWQK=XWKV=XWVQ = XW_Q \quad K = XW_K \quad V = XW_V

The scaling factor, 1dk\frac{1}{\sqrt{d_k}}, is critical. For large values of dkd_k, the dot products QKTQK^T can grow very large in magnitude. This pushes the softmax function into regions where its gradients are extremely small, effectively vanishing and stalling the learning process. Scaling mitigates this by keeping the variance of the dot products at 1, ensuring stable gradients.

Lesson image

Multi-Head Attention

Instead of performing a single attention function, Transformers employ Multi-Head Attention (MHA). This mechanism runs the Scaled Dot-Product Attention process multiple times in parallel. Each parallel run, or "head," uses different, learned linear projections for Q, K, and V. This allows the model to jointly attend to information from different representation subspaces at different positions.

Essentially, each attention head can learn to focus on a different type of relationship between words, such as syntactic dependencies, semantic similarity, or coreference.

The outputs of the individual heads are concatenated and then passed through another learned linear projection to produce the final output. This allows the model to integrate the information captured by the different heads.

Lesson image
MultiHead(Q,K,V)=Concat(head1,...,headh)WOwhere headi=Attention(QWiQ,KWiK,VWiV)\begin{aligned} \text{MultiHead}(Q, K, V) &= \text{Concat}(\text{head}_1, ..., \text{head}_h)W_O \\ \text{where } \text{head}_i &= \text{Attention}(QW_i^Q, KW_i^K, VW_i^V) \end{aligned}

Positional Information and Normalization

The self-attention mechanism is permutation-invariant; it does not inherently understand the order of tokens. To address this, positional information is injected. While the original Transformer used sinusoidal functions, modern architectures often use more advanced techniques.

Rotary Positional Embeddings (RoPE) are a popular choice. Instead of adding positional information to the embeddings, RoPE modifies the Query and Key vectors by applying a rotation matrix whose angle depends on the token's absolute position. When the dot product is taken between a rotated Query and a rotated Key, the result depends only on their relative positions, elegantly encoding relative distance directly into the self-attention mechanism.

Layer Normalization is another key component for stabilizing training. Its placement within the Transformer block has evolved. The original architecture used Post-LayerNorm, applying normalization after the residual connection. However, this can lead to unstable training in very deep models as the output magnitudes can explode.

Modern practice favors Pre-LayerNorm, where normalization is applied to the input of a sub-layer before the main operation (self-attention or the feed-forward network). This keeps the activations well-behaved throughout the network, leading to more stable and faster training, and often eliminates the need for learning rate warm-up.

Lesson image

Let's check your understanding of these core mechanics.

Quiz Questions 1/6

What is the primary role of the scaling factor, 1/dk1/\sqrt{d_k}, in the Scaled Dot-Product Attention mechanism?

Quiz Questions 2/6

How are the Query (Q), Key (K), and Value (V) matrices generated in a Transformer's attention mechanism?

Understanding these mathematical underpinnings is essential for anyone working with or fine-tuning large language models.