Attention and transformers practice problems
21 problems · 0 solved
Attention lets every token in a sequence look at every other token and decide which ones matter for it. It works like a lookup: each token sends a query, compares it with the keys of other tokens, and takes a weighted mix of their values. The transformer stacks this with feed-forward layers and normalization, and it is the core of every modern LLM. In these problems you will write scaled dot-product attention with a causal mask, apply key padding masks in a batch, split and merge attention heads, generate sinusoidal position encodings, rotate vectors with RoPE and scale it with NTK-aware interpolation, build ALiBi biases and T5 relative position buckets, count attention FLOPs, backpropagate through attention, and run a full pre-norm transformer block. Start with the Easy problems on sinusoidal encodings and attention entropy. Then write masked attention and head splitting, and finish with RoPE, attention backward and the full block. Writing attention from scratch is one of the most common LLM interview questions, and knowing the shapes and masks well saves hours when you debug real model code.
Easy 5
Medium 11
- Write scaled dot-product attention with a causal mask
- Split and merge attention heads
- Build the ALiBi attention bias for every head
- Apply a key padding mask in batched attention
- Bucket relative positions like T5
- Scale RoPE frequencies with NTK-aware interpolation
- Apply a causal mask when queries are fewer than keys
- Split a fused QKV checkpoint weight into separate projections
- Implement the original post-norm transformer block
- Add QK-norm to multi-head causal attention
- Count the parameters of a GPT-2 style model