← Coding labs/

Build a Transformer Encoder Block

Implementationhard~45 min
Objective

Fill in the three nn.Module classes (MultiHeadAttention, FeedForward, and TransformerBlock) so the block produces the correct output shape and supports deterministic eval mode.

Background

You're building a transformer from scratch in PyTorch for a deep learning course. The architecture follows the Pre-LN (pre-normalization) variant used in GPT-2 and most modern transformers, where LayerNorm is applied before the sub-layer rather than after. The starter code has the class skeletons, __init__ signatures, and a main() that validates shapes and determinism. Your job is to implement the forward passes and initializations.

Requirements
  1. 1.Implement MultiHeadAttention: project Q/K/V, reshape for heads, compute scaled dot-product attention, concatenate, project output
  2. 2.Implement FeedForward: two linear layers with GELU activation (d_model -> d_ff -> d_model)
  3. 3.Initialize TransformerBlock with attention, FFN, two LayerNorm layers, and dropout
  4. 4.Implement Pre-LN forward: LayerNorm before each sub-layer, residual connections around both
  5. 5.Ensure output shape matches input shape: (batch_size, seq_len, d_model)
Evaluation (100 points)
Multi-head attention implemented
MultiHeadAttention forward should project Q/K/V and reshape for head splitting
20pt
Feed-forward network implemented
FeedForward should have two linear layers with GELU or ReLU activation
20pt
LayerNorm in TransformerBlock
TransformerBlock must include LayerNorm layers for Pre-LN architecture
25pt
Residual connections
Forward pass must add input to sub-layer output (residual/skip connection)
20pt
Dropout in TransformerBlock
Dropout should be applied after sub-layers for regularization
15pt
Hints
Select a file to start editing
Terminal
$
AI Assistant50K tokens left

Ask me about the code, bugs, or concepts.
I'll guide you in plain English, no code output.
Budget: 50K tokens per lab