Overview: what you’re building
You’ll build a single-head causal self-attention module in PyTorch, plus a few small tests to prove it works:
- It will take a batch of token embeddings.
- It will project them into queries, keys, and values.
- It will compute scaled dot-product attention with a causal mask.
- It will return new, context-aware embeddings of the same shape as the input.
We’ll walk through this in four parts, one for each learning point, and end up with one coherent file (e.g. attention.py plus some test code).
A minimal target interface
We’ll aim for something like:
pythonattn = CausalSelfAttention(embed_dim=16)
x = torch.randn(2, 5, 16) # batch=2, time=5, embedding=16
y = attn(x) # y shape: (2, 5, 16)
1 / 6