Transformer Architecture In LLMs

Traditional Transformer

The Transformer architecture consists of stacked encoder and decoder layers, each containing two main sub-layers: the multi-head attention mechanism and the position-wise feed-forward network (FFN).

Basics

Multi-Head Attention Mechanism: The attention mechanism allows the model to focus on different parts of the input sequence when generating each element of the output sequence.

Scaled Dot-Product Attention: The core attention mechanism in Transformers is the scaled dot-product attention, which is defined as:

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

Where for a single attention head with input sequence X ∈ R(n×d)ℝ^{(n×d)}:

  • QQ (Query) = XWqXW_qR(n×dk)ℝ^{(n×d_k)}
  • KK (Key) = XWkXW_kR(n×dk)ℝ^{(n×d_k)}
  • VV (Value) = XWvXW_vR(n×dv)ℝ^{(n×d_v)}
  • WqW_q, WkW_kR(d×dk)ℝ^{(d×d_k)} and WvW_vR(d×dv)ℝ^{(d×d_v)} are learnable parameter matrices
  • dkd_k is the dimension of the key vectors
  • nn is the sequence length
  • dd is the model dimension (embedding dimension)

The computation flow is:

  1. Matrix multiplication QKTQK^T produces a matrix ∈ R(n×n)ℝ^{(n×n)}
  2. Division by dk\sqrt{d_k} scales the dot products to have appropriate variance
  3. Softmax normalizes each row to sum to 1, giving an attention weight matrix ∈ R(n×n)ℝ^{(n×n)}
  4. Multiplying by VV gives weighted values ∈ R(n×dv)ℝ^{(n×d_v)}

Multi-Head Attention: The Transformer uses multiple attention heads in parallel, which allows the model to jointly attend to information from different representation subspaces:

MultiHead(X)=Concat(head1,head2,...,headh)WO\text{MultiHead}(X) = \text{Concat}(\text{head}_1, \text{head}_2, ..., \text{head}_h)W^O where headi=Attention(XWiQ,XWiK,XWiV)\text{where head}_i = \text{Attention}(XW^Q_i, XW^K_i, XW^V_i)

With the following dimensions:

  • hh is the number of attention heads (typically 8 in the original paper)
  • WiQW^Q_i, WiKW^K_iR(d×dk)ℝ^{(d×d_k)}, WiVW^V_iR(d×dv)ℝ^{(d×d_v)} where dkd_k = dvd_v = d/hd/h
  • WOW^OR(hdv×d)ℝ^{(hd_v×d)} is the output projection matrix
  • Each headihead_iR(n×dv)ℝ^{(n×d_v)}
  • Concat(head1head_1, …, headhhead_h)R(n×hdv)ℝ^{(n×hd_v)}
  • Final output ∈ R(n×d)ℝ^{(n×d)}

For instance, in the smallest GPT-2 model:

  • Model dimension d = 768
  • Number of heads h = 12
  • Head dimension dkd_k = dvd_v = 64 (768/12)
  • Output projection matrix WOW^OR(768×768)ℝ^{(768×768)}

Position-wise Feed-Forward Network (FFN): After the attention mechanism, each position in the sequence is processed independently through the FFN:

FFN(x)=max(0,xW1+b1)W2+b2\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2

Where for each position x ∈ Rdℝ^d:

  • W1W_1R(d×dff)ℝ^{(d×d_{ff})} is the weight matrix for the first linear transformation
  • b1b_1R(dff)ℝ^{(d_{ff})} is the bias vector for the first linear transformation
  • W2W_2R(dff×d)ℝ^{(d_{ff}×d)} is the weight matrix for the second linear transformation
  • b2b_2Rdℝ^d is the bias vector for the second linear transformation
  • dffd_{ff} is the inner-layer dimensionality (typically 2048 or 4d)

For the entire sequence X ∈ R(n×d)ℝ^{(n×d)}:

  • Input to FFN ∈ R(n×d)ℝ^{(n×d)}
  • After first linear transformation ∈ R(n×dff)ℝ^{(n×d_{ff})}
  • After activation function ∈ R(n×dff)ℝ^{(n×d_{ff})}
  • Output after second linear transformation ∈ R(n×d)ℝ^{(n×d)}

Some implementations use GELU instead of ReLU as the activation function:

FFN(x)=GELU(xW1+b1)W2+b2\text{FFN}(x) = \text{GELU}(xW_1 + b_1)W_2 + b_2

The FFN can be viewed as two convolutions with kernel size 1 and is applied identically to each position, but with different parameters from layer to layer

Layer Normalization and Residual Connections: Each sub-layer (attention and FFN) in the Transformer includes a residual connection followed by layer normalization:

X=LayerNorm(X+MultiHead(X))X' = \text{LayerNorm}(X + \text{MultiHead}(X)) Z=LayerNorm(X+FFN(X))Z = \text{LayerNorm}(X' + \text{FFN}(X'))

Where X, X’, Z ∈ R(n×d)ℝ^{(n×d)}

Complete Transformer Layer: A single Transformer layer processes an input X ∈ R(n×d)ℝ^{(n×d)} through:

  1. Multi-head attention: X → X’ ∈ R(n×d)ℝ^{(n×d)}
  2. Layer normalization and residual connection: X, X’ → X” ∈ R(n×d)ℝ^{(n×d)}
  3. Feed-forward network: X” → X''' ∈ R(n×d)ℝ^{(n×d)}
  4. Layer normalization and residual connection: X”, X''' → output ∈ R(n×d)ℝ^{(n×d)}

Stacked Encoder-Decoder

The Transformer architecture consists of a stack of encoder layers and a stack of decoder layers working in tandem to transform an input sequence into an output sequence. This design enables the model to capture complex patterns and dependencies across sequences of varying lengths.

Encoder Stack

The encoder transforms an input sequence into a continuous representation that captures its semantic content. This representation is then used by the decoder to generate the output sequence.

Input Processing: For an input sequence of tokens w1,w2,,wnw_1, w_2, \ldots, w_n, we first convert each token to an embedding vector. If we denote the embedding function as EE, the initial representation becomes:

X0=[E(w1)E(w2)E(wn)]Rn×dX^0 = \begin{bmatrix} E(w_1) \\ E(w_2) \\ \vdots \\ E(w_n) \end{bmatrix} \in \mathbb{R}^{n \times d}

Since Transformers don’t have a built-in notion of token order, we add positional encodings to these embeddings. The positional encoding for position pos and dimension i is defined as:

PE(pos,2i)=sin(pos100002i/d)PE_{(pos,2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right) PE(pos,2i+1)=cos(pos100002i/d)PE_{(pos,2i+1)} = \cos\left(\frac{pos}{10000^{2i/d}}\right)

These positional encodings are added to the token embeddings to form the actual input to the encoder:

Xpos0=E(wpos)+PEposRdX^{0} _{pos} = E(w _{pos}) + PE _{pos} \in \mathbb{R}^{d}

The complete input matrix becomes X0Rn×dX^0 \in \mathbb{R}^{n \times d} where each row incorporates both token and positional information.

Encoder Layer Computation

Each encoder layer l (where l{1,2,,Lenc}l \in \{1, 2, \ldots, L_{enc}\}) processes its input Xl1X^{l-1} through two main sub-layers: multi-head self-attention and a feed-forward network. The mathematical formulation for the l-th encoder layer is:

Xattl=MultiHead(Xl1,Xl1,Xl1)X^{l}_{\text{att}} = \text{MultiHead}(X^{l-1}, X^{l-1}, X^{l-1})

Xmidl=LayerNorm(Xl1+Xattl)X^{l}_{\text{mid}}=\text{LayerNorm}(X^{l-1} + X^{l} _{\text{att}})

Xffnl=FFN(Xmidl)X^{l}_{\text{ffn}} = \text{FFN}(X^{l} _{\text{mid}})

Xl=LayerNorm(Xmidl+Xffnl)X^{l} = \text{LayerNorm}(X^{l} _{\text{mid}} + X^{l} _{\text{ffn}})

The multi-head attention in the encoder allows each position to attend to all positions in the previous layer. For the ll-th layer, this computation involves using the output of the previous layer Xl1X^{l-1} as the queries, keys, and values for the self-attention mechanism.

Stacked Encoder

The encoder comprises LencL_{enc} identical layers stacked on top of each other. Each layer processes the output of the previous layer, creating increasingly abstract representations of the input sequence. The final output of the encoder stack, XLencRn×dX^{L_{enc}} \in \mathbb{R}^{n \times d}, serves as the key and value for the cross-attention mechanism in the decoder.

The transformation through the encoder stack can be represented recursively as:

Xl=EncoderLayerl(Xl1)X^{l} = \text{EncoderLayer}_l(X^{l-1})

Where X0X^0 is the input embedding plus positional encoding, and XLencX^{L_{enc}} is the final encoder output.

Decoder Stack

The decoder generates the output sequence element by element, using both the encoder’s output and the previously generated elements.

Decoder Input and Masking

For auto-regressive generation, the decoder takes as input the previously generated sequence. During training, this is the target sequence shifted right by one position and prepended with a start token. Like the encoder, these tokens are embedded and combined with positional encodings:

Y0=[E(y0)+PE0E(y1)+PE1E(ym1)+PEm1]Rm×dY^0 = \begin{bmatrix} E(y_0) + PE_0 \\ E(y_1) + PE_1 \\ \vdots \\ E(y_{m-1}) + PE_{m-1} \end{bmatrix} \in \mathbb{R}^{m \times d}

Where y0y_0 is the start token, and mm is the length of the output sequence.

Decoder Layer Computation

Each decoder layer ll (where l{1,2,,Ldec}l \in \{1, 2, \ldots, L_{dec}\}) consists of three sub-layers: masked multi-head self-attention, cross-attention with the encoder output, and a feed-forward network. The mathematical formulation for the ll-th decoder layer is:

Yself-attl=MaskedMultiHead(Yl1,Yl1,Yl1)Y^{l} _{\text{self-att}} = \text{MaskedMultiHead}(Y^{l-1}, Y^{l-1}, Y^{l-1})

Ymid1l=LayerNorm(Yl1+Yself-attl)Y^{l} _{\text{mid1}} = \text{LayerNorm}(Y^{l-1} + Y^{l} _{\text{self-att}})

Ycross-attl=MultiHead(Ymid1l,XLenc,XLenc)Y^{l} _{\text{cross-att}} = \text{MultiHead}(Y^{l} _{\text{mid1}}, X^{L _{enc}}, X^{L _{enc}})

Ymid2l=LayerNorm(Ymid1l+Ycross-attl)Y^{l} _{\text{mid2}} = \text{LayerNorm}(Y^{l} _{\text{mid1}} + Y^{l} _{\text{cross-att}})

Yffnl=FFN(Ymid2l)Y^{l} _{\text{ffn}} = \text{FFN}(Y^{l} _{\text{mid2}})

Yl=LayerNorm(Ymid2l+Yffnl)Y^{l} = \text{LayerNorm}(Y^{l} _{\text{mid2}} + Y^{l} _{\text{ffn}})

The masked multi-head attention employs a lower triangular mask to ensure that when predicting the token at position ii, the model can only use information from positions <i< i. This is implemented by applying a mask MM to the attention weights:

Mij={0if ijotherwiseM_{ij} = \begin{cases} 0 & \text{if } i \geq j \\ -\infty & \text{otherwise} \end{cases}

The attention scores then become:

MaskedAttention(Q,K,V)=softmax(QKT+Mdk)V\text{MaskedAttention}(Q, K, V) = \text{softmax}\left(\frac{QK^T + M}{\sqrt{d_k}}\right)V

The cross-attention mechanism allows the decoder to focus on relevant parts of the input sequence. In this sub-layer, the queries come from the decoder’s previous sub-layer, while the keys and values come from the encoder’s output.

Similar to the encoder, the decoder consists of LdecL_{dec} identical layers stacked on top of each other. The transformation through the decoder stack can be represented recursively as:

Yl=DecoderLayerl(Yl1,XLenc)Y^{l} = \text{DecoderLayer} _l(Y^{l-1}, X^{L _{enc}})

Where Y0Y^0 is the shifted target sequence embedding plus positional encoding, and YLdecRm×dY^{L_{dec}} \in \mathbb{R}^{m \times d} is the final decoder output.

Final Output Layer

The final decoder output is transformed into probabilities over the vocabulary through a linear projection followed by a softmax function:

P(yiy<i,X)=softmax(YiLdecWout+bout)P(y_i | y_{<i}, X) = \text{softmax}(Y^{L_{dec}} _i W _{\text{out}} + b _{\text{out}})

Where WoutRd×VW_{\text{out}} \in \mathbb{R}^{d \times |V|} and boutRVb_{\text{out}} \in \mathbb{R}^{|V|} are the output projection parameters, and V|V| is the vocabulary size.

Mixture of Experts (MoE)

The Mixture of Experts (MoE) architecture enhances transformer models by introducing conditional computation, where specialized subnetworks (“experts”) are dynamically selected per token. This approach maintains computational efficiency while scaling model capacity.

Core Components and Mathematics

Base Transformer Recap: In a standard transformer layer, the feed-forward network (FFN) processes all tokens identically. For an input token xRdx \in \mathbb{R}^d, the FFN is:

FFN(x)=W2GELU(W1x+b1)+b2\text{FFN}(x) = W_2 \cdot \text{GELU}(W_1 x + b_1) + b_2

where W1Rd×4dW_1 \in \mathbb{R}^{d \times 4d}, W2R4d×dW_2 \in \mathbb{R}^{4d \times d}, b1R4db_1 \in \mathbb{R}^{4d}, and b2Rdb_2 \in \mathbb{R}^d.

MoE Layer: The MoE layer replaces the FFN with EE experts and a router. For a token xiRdx_i \in \mathbb{R}^d:

  1. Router: Computes routing probabilities over experts:

    g(xi)=softmax(xiWr)REg(x_i) = \text{softmax}(x_i W_r) \in \mathbb{R}^E

    where WrRd×EW_r \in \mathbb{R}^{d \times E} is the router’s weight matrix. The top-kk experts (typically k=1k=1 or 22) are selected based on these probabilities.

  2. Experts: Each expert jj is an FFN:

    Expertj(xi)=W2,jGELU(W1,jxi+b1,j)+b2,j\text{Expert} _j(x_i) = W _{2,j} \cdot \text{GELU}(W _ {1,j} x_i + b _{1,j}) + b _{2,j}

    where W1,jRd×mW_{1,j} \in \mathbb{R}^{d \times m}, W2,jRm×dW_{2,j} \in \mathbb{R}^{m \times d}, and mm is the hidden dimension (often m=4dm=4d). Experts share the same architecture but have independent parameters.

  3. Combination: The final output is a weighted sum of the selected experts:

    MoE(xi)=jTig(xi)jExpertj(xi)\text{MoE}(x_i) = \sum_{j \in \mathcal{T}_i} g(x_i)_j \cdot \text{Expert}_j(x_i)

    where Ti\mathcal{T}_i is the set of top-kk expert indices for token xix_i.

Load Balancing and Capacity

Expert Capacity: To prevent overloading individual experts, each processes at most CC tokens per batch:

C=αBnEC = \left\lceil \frac{\alpha \cdot B \cdot n}{E} \right\rceil

where BB is the batch size, nn is the sequence length, and α1\alpha \geq 1 is a buffer factor.

Load Balancing Loss: A regularization term ensures uniform expert utilization:

Lbalance=λEj=1Efjpj\mathcal{L} _{\text{balance}} = \lambda \cdot E \cdot \sum _{j=1}^E f_j \cdot p_j

where fjf_j is the fraction of tokens routed to expert jj, pjp_j is the mean routing probability for expert jj, and λ\lambda is a hyperparameter (typically 0.010.01).

Sparse Computation

Activation Sparsity: Only kk experts per token are activated. For a batch of BnB \cdot n tokens, the MoE layer computes:

  • Router logits: BnEB \cdot n \cdot E operations
  • Expert computations: Bnk(2dm+m+d)B \cdot n \cdot k \cdot (2dm + m + d) operations

This contrasts with a dense FFN’s Bn(2dm+m+d)B \cdot n \cdot (2dm + m + d) operations. MoE scales model size (via EE) without linearly increasing computation.

Advanced Variants

Switch Transformer: Uses k=1k=1 for simplicity. The routing equation reduces to:

j=argmaxj(xiWr)jj^* = \arg\max_j (x_i W_r)_j

and only Expertj\text{Expert}_{j^*} processes xix_i.

Expert Choice Routing: Experts select tokens instead of tokens selecting experts. For expert jj:

Sj=top-C tokens by xiWrj\mathcal{S}_j = \text{top-}C \text{ tokens by } x_i W_r^j

where WrjRdW_r^j \in \mathbb{R}^d is the expert-specific routing vector.

Hierarchical MoE: Organizes experts into groups. A token is first routed to a group, then to an expert within the group, reducing the effective routing dimension.

Implementation Challenges

Distributed Training: Experts are sharded across devices. Tokens are routed via all-to-all communication, which introduces overhead proportional to EE.

Memory Footprint: Storing EE experts increases memory use by E×E \times compared to a dense layer. Techniques like expert parameter offloading or quantization mitigate this.

Convergence Stability: The interation between router gradients and expert training requires careful tuning of optimizer settings (e.g., higher learning rates for routers).