Understand the transformer architecture that powers GPT, BERT, and every modern LLM. Covers self-attention, positional encoding, multi-head attention, feedforward layers, and the patterns that make transformers the foundation of modern AI.
This reference explains the core mechanics of transformer architecture as implemented in standard deep learning frameworks and training pipelines, focusing on how to configure, deploy, and debug models that use attention mechanisms for sequence modeling.
The fundamental building block of transformers is multi-head self-attention. The attention mechanism computes a weighted sum over input sequences where weights are determined by query, key, and value vectors:
def scaled_dot_product_attention(Q, K, V, mask=None):
d_k = Q.shape[-1]
scores = torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
attention_weights = torch.softmax(scores, dim=-1)
return torch.matmul(attention_weights, V)
Attention matrices must be square and dimensionally consistent. A mismatch between query and key dimensions causes silent failures in forward passes, with no explicit error message. The model continues to train but produces incorrect outputs. When debugging attention failures, check that d_model is divisible by num_heads.
Each transformer layer includes layer normalization applied before attention and feed-forward components:
class TransformerLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff):
super().__init__()
self.attention = MultiHeadAttention(d_model, num_heads)
self.norm1 = nn.LayerNorm(d_model)
self.ffn = FeedForward(d_model, d_ff)
self.norm2 = nn.LayerNorm(d_model)
Positional encodings must match sequence length. If input sequences exceed the maximum sequence length used during training, attention weights become invalid, producing NaN outputs in subsequent layers.
Training parameters directly affect transformer behavior. The learning rate schedule is critical:
optimizer:
name: AdamW
params:
lr: 5e-5
betas: [0.9, 0.98]
eps: 1e-9
weight_decay: 0.01
scheduler:
name: LinearWarmupDecay
warmup_steps: 4000
decay_steps: 100000
The weight_decay parameter in AdamW is essential for preventing overfitting. Without it, models may train successfully but fail to generalize.
Models are typically saved using PyTorch's native torch.save() and torch.load() functions:
# Saving
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'epoch': epoch,
'loss': loss
}, 'transformer_checkpoint.pth')
# Loading
checkpoint = torch.load('transformer_checkpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
Incorrect state dict loading can silently overwrite model weights. Always verify that the loaded checkpoint matches the expected model architecture.
Transformer models can exceed GPU memory limits during training. The gradient_checkpointing technique reduces memory usage by recomputing intermediate values during backpropagation:
model.gradient_checkpointing_enable()
When gradient checkpointing is enabled, gradients may not flow through all attention layers correctly. In such cases, compute gradients manually using hooks or debug with torch.autograd.gradcheck.
Attention mechanisms can silently fail due to incorrect tensor shapes. Common failure modes include:
A common error occurs when attention mask shape does not match expected input dimensions. The model continues training but produces meaningless outputs.
Transformers process batches of sequences with variable lengths by padding to the maximum sequence length in each batch:
from torch.nn.utils.rnn import pad_sequence
# Example padding logic
padded_sequences = pad_sequence(sequences, batch_first=True)
Padding tokens must be masked out during attention computation. Failing to do so causes attention weights to include irrelevant information from padded positions.
Each head in multi-head attention operates independently on projected query, key, and value tensors:
def multi_head_attention(Q, K, V, num_heads, d_model):
d_k = d_model // num_heads
Q = Q.view(batch_size, seq_len, num_heads, d_k).transpose(1, 2)
K = K.view(batch_size, seq_len, num_heads, d_k).transpose(1, 2)
V = V.view(batch_size, seq_len, num_heads, d_k).transpose(1, 2)
attention_output = scaled_dot_product_attention(Q, K, V)
output = attention_output.transpose(1, 2).contiguous().view(batch_size, seq_len, d_model)
return output
Incorrect splitting of d_model into num_heads components results in incompatible tensor shapes for matrix multiplication. This failure often manifests as a runtime error or incorrect gradient computation.
Cross-entropy loss with label smoothing is commonly used:
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
Label smoothing helps prevent overconfidence in predictions. If the loss function does not match the training data format, model convergence may be poor or fail entirely.
Distributed training requires careful handling of attention computations across workers:
# Example distributed setup
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
model = DDP(model, device_ids=[local_rank])
Attention computation in distributed settings must ensure consistent communication patterns. Failures in synchronization can lead to inconsistent attention scores across workers.
Memory usage scales quadratically with sequence length. Techniques include:
torch.cuda.amp)Mixed precision training requires explicit handling of attention computation steps to maintain numerical stability.
Transformer architectures typically support maximum sequence lengths between 512 and 2048 tokens. Exceeding this limit causes either runtime errors or incorrect attention weights.
Attention mechanisms do not inherently handle variable-length sequences beyond their configured maximum. Padding must be explicitly managed in the model's forward pass.
Before deploying models, validate that attention layers produce expected outputs:
# Validate attention output shapes
with torch.no_grad():
attention_output = model.attention_layer(input_tensor)
assert attention_output.shape == input_tensor.shape
Failure to validate attention outputs can result in silent degradation of model performance.
Critical configuration parameters include:
d_model: Embedding dimension, must be divisible by num_headsnum_heads: Number of attention heads, typically 8 or 16d_ff: Feed-forward network dimensiondropout: Applied to attention weights and intermediate activationsIncorrect values for these parameters cause training instability or incorrect model behavior. For example, setting d_model to 512 with num_heads=7 produces incompatible tensor shapes.
Common failure modes include:
Use gradient checking tools and explicit validation of attention outputs to detect these issues early.
This page was rewritten on 15 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy