Attention and transformers 道练习题
23 道题 · 0 已解决
Attention 让序列中的每个 token 都能观察其他所有 token,并决定哪些 token 对其重要。它的工作原理类似于查找:每个 token 发送一个 query,将其与其它 token 的 keys 进行比较,并对它们的 values 进行加权混合。Transformer 将此机制与 feed-forward layers 和 normalization 堆叠在一起,它是每个现代 LLM 的核心。在这些题目中,你将编写带有 causal mask 的 scaled dot-product attention,在 batch 中应用 key padding masks,拆分并合并 attention heads,生成 sinusoidal position encodings,使用 RoPE 旋转向量并结合 NTK-aware interpolation 进行缩放,构建 ALiBi biases 和 T5 relative position buckets,计算 attention FLOPs,通过 attention 进行反向传播,并运行一个完整的 pre-norm transformer block。从正弦编码和 attention entropy 的 Easy 题目开始。然后编写 masked attention 和 head splitting,最后完成 RoPE、attention backward 和完整的 block。从零开始编写 attention 是最常见的 LLM 面试题之一,而熟练掌握 shapes 和 masks 能在调试实际模型代码时节省大量时间。
简单 6
中等 11
- GPT decoder: causal self-attention for one head
- 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
困难 6
- Rotate a vector by its position (RoPE)Pro
- Run a full pre-norm transformer block forward passPro
- Backpropagate through scaled dot-product attentionPro
- Compute the gradient of a tied input and output embeddingPro
- Convert a GPT block to a Llama-style block (RMSNorm, RoPE, SwiGLU)Pro
- Write multi-head attention that works for self and cross attentionPro