This is the core exercise and it's less about memorizing a bug list than about actually understanding why each thing is broken.
Start by explaining your systematic debugging process: first verify the model can overfit a tiny batch, then inspect each component (attention, normalization, residuals, masking) against known correct implementations. Prioritize bugs that break gradient flow or numerical stability, and validate fixes with unit tests and loss curves.
Pro tip: Mention that you'd add assertions for shape and value ranges (e.g., attention weights sum to 1, no NaNs) and use gradient checking to catch subtle bugs early. This shows you build safeguards, not just fix symptoms.
Run the model on a small dataset and confirm the failure mode (e.g., loss not decreasing, NaNs). Use a tiny batch to see if the model can overfit, which quickly reveals fundamental bugs.
Review attention mechanism (scaling, masking), layer normalization (epsilon, axis), residual connections, and positional encoding for common mistakes like missing scaling or incorrect masking.
Verify that gradients are flowing to all parameters and that initialization (e.g., Xavier, Kaiming) is appropriate. Look for vanishing/exploding gradients or dead ReLUs.
Write small tests for each component: attention output shapes, mask application, softmax sums to 1, and gradient checks. Compare against a reference implementation if possible.
Apply fixes one at a time, re-running the tiny-batch overfit test after each change. Finally, train on a larger dataset and monitor loss curves to ensure convergence.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Felt more straightforward than the bug hunt once I got there.
Start by outlining the standard Transformer architecture and its language modeling head, then explain how to replace it with a classification head. Discuss the necessary changes to the loss function and prediction process, and address the optional mean-pooling technique. Emphasize trade-offs and design choices.
Pro tip: Highlight that mean-pooling can be beneficial for tasks where all tokens contribute equally, but consider using the [CLS] token or attention pooling for tasks requiring a global representation. Mention that the choice depends on the specific task and dataset.
Recall that a Transformer for language modeling outputs a hidden state for each token and uses a linear layer to project to vocabulary logits. The loss is cross-entropy between predicted and actual next tokens.
Remove the vocabulary projection layer and add a classification head, typically a linear layer mapping the hidden size to the number of classes. This head can be applied to a pooled representation or to each token (for token-level classification).
For sequence classification, aggregate token representations (e.g., mean-pooling, [CLS] token, or max-pooling) to get a fixed-size vector, then pass through the classification head to get class logits. Use cross-entropy loss between logits and true labels.
If mean-pooling is requested, compute the average of all token hidden states (excluding padding) before the final projection. Discuss its simplicity and potential drawbacks, such as losing positional or importance information.
Compare mean-pooling with other pooling methods (e.g., using the [CLS] token, attention pooling, or max-pooling). Mention that the choice can affect performance and should be validated empirically.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
If you've never implemented a KV cache before this will feel chaotic under time pressure.
Start by explaining the role of KV caching in autoregressive decoding and how it avoids redundant computation. Then, walk through the modifications needed in the attention mechanism: concatenating cached keys/values with new ones, adjusting positional indices for cached steps, and ensuring pass-through parameters (like attention mask and past length) are correctly propagated. Finally, discuss potential pitfalls and trade-offs.
Pro tip: Emphasize that positional embeddings must be offset by the cache length to maintain correct positional information, and that the attention mask must account for the cached sequence length to prevent attending to padding or future tokens.
Review the provided KV cache class to understand its interface: methods for storing and retrieving keys/values, and how it tracks sequence length. Identify where to integrate it into the attention layer.
In the attention forward pass, compute new keys and values, then concatenate them with cached ones along the sequence dimension. Use the updated keys/values for attention computation.
When computing positional embeddings for the new tokens, offset the positions by the current cache length. This ensures that each token gets the correct positional index relative to the full sequence.
Ensure that parameters like attention mask, past key/value lengths, and any other relevant arguments are correctly passed through to the attention function and used to shape the attention scores.
Test the implementation with and without caching to ensure numerical equivalence. Discuss memory vs. speed trade-offs and potential issues like cache eviction policies.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.