Start by outlining the overall architecture and then implement each component in a modular way, testing as you go. Explain the math behind scaled dot-product attention and multi-head attention, and discuss the rationale for design choices like pre-LN vs post-LN. Finally, analyze complexity and contrast with a decoder block.
Pro tip: Mention that you would use torch.einsum for clarity and efficiency in attention computations, and that you would add masking support for decoder self-attention. Also, discuss the trade-offs between pre-LN and post-LN in terms of training stability and performance.
Confirm that the implementation should be from scratch without nn.Transformer or nn.MultiheadAttention. Sketch the encoder block: multi-head attention, feed-forward network, residual connections, and layer normalization.
Write a function that takes queries, keys, values, and optional mask, computes attention scores, scales by sqrt(d_k), applies softmax, and returns weighted values. Explain the scaling rationale.
Project inputs to multiple heads, apply scaled dot-product attention in parallel, concatenate outputs, and apply a final linear projection. Discuss how this allows the model to attend to different representation subspaces.
Combine multi-head attention and a position-wise feed-forward network with residual connections and layer normalization. Specify the order (e.g., pre-LN or post-LN) and explain your choice.
Implement sinusoidal positional encodings and add them to input embeddings. Analyze the O(n^2) complexity of attention and describe how a decoder block differs (masked self-attention, cross-attention).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.