Transformers and Attention Questions
The transformer architecture that underlies modern language and multimodal models. Covers self-attention and multi-head attention, positional encoding, encoder/decoder structures, and why transformers scale better than recurrent alternatives. Focuses on the architectural intuition behind contemporary foundation models.
Discuss the roles of residual connections and layer normalization in Transformer blocks. Explain how residuals aid optimization, what problems layer norm addresses, and briefly contrast pre-norm vs post-norm placement and their effects on stability for deep stacks.
Sample Answer
Residual connections and layer normalization are critical in Transformer blocks to enable deep, stable, and efficient training.
Residual connections (skip connections):
- What they do: add the input of a sub-layer (e.g., self-attention or feed-forward) to its output: y = x + Sublayer(x).
- Why helpful: they provide a short path for gradients and signals, mitigating vanishing gradients and enabling deeper networks. They let the model learn residual functions (small corrections) rather than full transformations, which simplifies optimization and speeds convergence.
- Practical effect: faster training, better gradient flow, and improved representational reuse.
Layer normalization:
- What it does: normalizes activations across features for each example, stabilizing activation distributions before/after sub-layers.
- Problems addressed: reduces internal covariate shift, makes training less sensitive to learning rate and initialization, and improves conditioning of the optimization problem.
Pre-norm vs post-norm:
- Post-norm (original Transformer): SublayerNorm after adding residual: x = x + Sublayer(LN(x)); then LN. Tends to work for shallow models but can become unstable for very deep stacks: gradients can explode or training diverges.
- Pre-norm: apply LN before the sublayer and then add residual: x = x + Sublayer(LN(x)). This placement yields more stable gradient norms, smoother training, and better convergence for deep Transformers (hundreds of layers). Pre-norm often allows larger learning rates and removes the need for special warmups or gradient clipping in many setups.
- Trade-off: post-norm sometimes gives slightly better final performance for moderate depths, but pre-norm is preferred for deep/large models because of stability.
In practice: use residuals everywhere, apply layer norm (pre-norm for deep stacks), and combine with appropriate learning-rate schedules and regularization for robust scaling.
Design an attention pattern and algorithm for a 1,000,000 token context that combines local windows, global tokens, and compressed memories to achieve sub-quadratic complexity. Provide the structure, per-token complexity, memory requirements, update rules for compressed memory, and a short analysis of representational trade-offs.
Sample Answer
Requirements & overview:
- Context length N = 1,000,000 tokens. Goal: sub-quadratic attention using sparse pattern that mixes local windows, a small set of global tokens, and compressed memories (summaries) with periodic updates.
Architecture / attention pattern (per layer):
- Local windows: fixed-size sliding window of W tokens (e.g., W=1024). Each token attends to W neighbors (bi-directional or causal).
- Global tokens: G learnable/global special tokens (e.g., G=512) that receive attention from all tokens and can broadcast back. They represent document-wide factors.
- Compressed memories: Partition sequence into B blocks of size S (S >> W, e.g., S=2048–8192). For each block maintain M compressed slots (e.g., M=16) produced by a lightweight encoder (pooling + projection). Tokens attend to their block’s M slots and optionally to nearby-block slots (k-hop).
- Sparse routing: Use LSH or top-k selection for long-range token-to-token queries rarely (optional).
Per-token attention complexity:
- Local: O(W)
- Global: O(G)
- Compressed: O(M * (1 + k)) (own block + k neighbor blocks)
Total per-token: O(W + G + M*(1+k)). With constants (W,G,M small vs N) this is O(1) per token; whole layer O(N).
Memory requirements:
- Raw activations: O(N * d) (unchanged)
- Global token params: O(G * d)
- Compressed memory store: O(B * M * d) where B = N / S. Example: N=1e6, S=4096 -> B≈244, M=16 -> compressed slots ≈ 3904d (tiny vs Nd).
Update rules for compressed memory:
- Compute block summary via lightweight encoder (mean/max pooling of token representations + small transformer or conv) -> produce M slot vectors.
- Online update: when processing a block, compute new slots s_new. Merge with existing slots s_old via gated update:
s_updated = LayerNorm( (1 - α) * s_old + α * s_new ) with learned α per slot or attention-weighted mixing. - Periodic compaction: every T steps optionally run a small compressor (attention over past slots) to reduce slot count and remove redundancy.
Representational trade-offs:
- Pros: Local windows preserve fine-grained context; compressed slots capture mesoscale summaries enabling long-range propagation; global tokens provide cross-document signals. Complexity scales linearly in N.
- Cons: Compression loses token-level detail and may hurt precise long-range dependencies; choice of S, M, G, W is a trade-off between fidelity and compute. Mitigations: adaptive slot sizes, learned routing, and occasional full-resolution attention for critical tokens.
Deployment notes:
- Use block-parallelism, memory-mapped storage for compressed slots, and mixed-precision. Empirically tune W/G/M to task; include sparsity schedules during training to encourage summaries to be informative.
You're adapting a pretrained Transformer encoder for document classification, and input lengths range from a couple of sentences to several thousand tokens. How would you turn the per-token representations into a single vector for the classifier, and what would push you toward one pooling strategy over another as the length variance grows and the input gets noisier?
Sample Answer
There are two separate problems bundled in this scenario: (1) the encoder itself has a fixed maximum sequence length (typically 512 tokens for a BERT-style model), so "several thousand tokens" won't even fit in a single forward pass, and (2) once you have per-token vectors for whatever does fit, you need to turn them into one fixed-size vector for the classifier.
Handling the length problem first: for a document that exceeds the encoder's max length, you generally either truncate (keep the first/last N tokens, simple but throws away content) or split the document into overlapping chunks, run the encoder on each chunk, and aggregate the per-chunk vectors (a hierarchical approach: pool within each chunk, then pool again across chunks). The pooling strategy discussed below applies at both levels, within a chunk and again across chunks if you're doing hierarchical aggregation.
Turning per-token vectors into one vector, the main options:
- CLS token: use the final hidden state of a special token prepended to the input. Simple, and matches how many pretrained encoders (like BERT) were trained, but the pretraining objective that trains CLS (often a next-sentence-style task) was mostly learned on short sequences, so its ability to summarize a multi-thousand-token document isn't guaranteed and should be checked empirically.
- Masked mean pooling: average the token vectors, excluding padding positions using the attention mask. Simple, robust, and a strong default: every token contributes equally, so it doesn't depend on the model having learned a good CLS summary.
- Max pooling: take the element-wise maximum across token vectors. Good at surfacing one strong discriminative signal, but throws away everything else and can be dominated by a single outlier token or noisy embedding.
- Attention (learned) pooling: a small trainable layer computes an importance weight for each token and takes a weighted sum. More parameters and more to tune (the padding mask needs to be applied before the softmax, and it benefits from dropout/temperature tuning to avoid over-concentrating on one token), but it's the only option here that can learn to actually ignore irrelevant tokens rather than treating all tokens equally or relying on one fixed summary token.
What pushes you toward one over another as length variance and noise grow: with short, clean inputs (a couple of sentences), CLS, masked mean, and attention pooling tend to perform similarly, since there's little content to filter and not much difference between averaging everything and learning what to weight. As documents get longer and noisier (e.g., a support-ticket thread with email signatures, quoted replies, and boilerplate mixed in with the actual complaint), masked mean pooling starts diluting the signal: it treats a paragraph of email signature the same as the one sentence that actually describes the problem. That's exactly the situation where attention pooling earns its extra complexity, since it can learn to downweight the boilerplate and concentrate on the informative tokens, so the pooled vector doesn't get watered down as the fraction of noise tokens grows with document length. If the noise instead looks more like a few randomly corrupted tokens rather than long stretches of boilerplate, max pooling's sensitivity to a single strongest activation can become a liability rather than a strength, since one noisy token could dominate the pooled vector.
Practical recommendation: start with masked mean pooling as a fast, robust baseline, and always make sure padding tokens are excluded from the average (a very common source of subtle bugs, since unmasked padding vectors otherwise quietly corrupt the result). Move to learned attention pooling once you observe long, noisy documents actually hurting accuracy, since it's the strategy best suited to filtering signal out of a growing amount of irrelevant content, and validate the switch with an actual accuracy comparison rather than assuming it will help.
You need to fine-tune a pre-trained Transformer on a small labeled dataset (~1k examples). Describe practical strategies to avoid overfitting: layer freezing, adapters/LoRA, learning rates, augmentation, early stopping, and evaluation strategies. Which would you try first and why?
Sample Answer
Short answer: with only about 1,000 labeled examples, the single highest-leverage first move is parameter-efficient fine-tuning, specifically LoRA or adapters, combined with a small learning rate, dropout, and early stopping. Below is why, followed by the fuller toolkit ordered from most to least important at this data size.
- Try first: LoRA or adapters (parameter-efficient fine-tuning)
- LoRA (Low-Rank Adaptation): instead of updating the full pretrained weight matrices (which have millions of parameters and would badly overfit on 1k examples), you freeze the original weights and add a small pair of low-rank trainable matrices alongside each one. Only these new, much smaller matrices get trained.
- Adapters: a similar idea, but instead of modifying the existing weight matrices you insert small new feed-forward layers between the transformer's existing blocks; only these inserted layers are trained, and the pretrained weights stay frozen.
- Why try these first: both cut the number of trainable parameters to a small fraction of the full model (often under 1%), so there is far less capacity to simply memorize 1,000 examples, while the model still benefits from everything it learned during pretraining.
- Layer freezing (a simpler, coarser alternative)
- Freeze the embedding layer and the earlier transformer blocks, which tend to capture general language patterns, and only unfreeze the last few blocks plus the task-specific output head. This is a blunter version of the same idea as LoRA: fewer trainable parameters relative to the amount of training data.
- Learning rate and optimizer settings
- Use a small learning rate (e.g., 1e-5 to 5e-5) on any pretrained weights you do unfreeze, since large updates on a tiny dataset can quickly wipe out useful pretrained knowledge (this is called catastrophic forgetting) or overfit. Use a slightly higher rate only for newly added head/adapter parameters, since those start from a random initialization and have nothing to lose yet.
- Regularization
- Dropout: during training, randomly zero out some neuron activations so the model cannot rely on any single path through the network, which reduces memorization of specific examples.
- Weight decay: a small penalty added to the loss that discourages very large weight values, also reducing overfitting.
- Label smoothing: instead of training the model to predict the correct label with 100% confidence, you soften the target (for example, 90% on the correct class and the remaining 10% spread across the others), which keeps the model from becoming overconfident on the handful of examples it has seen. This is a smaller lever, worth adding but not the first thing to reach for.
- Data augmentation
- Create additional realistic training variants. For text, this can mean back-translation (translate a sentence to another language and back to get a differently-worded version with the same meaning) or synonym replacement. Use augmentation conservatively: on a 1k-example set, overly aggressive changes can accidentally alter the true label.
- Early stopping
- Hold out a validation split from your 1,000 examples and stop training once validation performance stops improving for a few epochs, instead of training for a fixed number of epochs. This directly guards against over-training on a small dataset.
- Evaluation strategy
- A single train/validation split is noisy with only 1k examples. Use k-fold cross-validation (for example, split the data into 5 folds, train on 4 and validate on the 5th, rotate through all 5 splits, and average the results) to get a more reliable read on real performance.
- Also check calibration: whether the model's confidence matches its actual accuracy (for example, among all the times the model says it is 90% confident, is it actually right about 90% of the time?). This helps catch a model that is confidently wrong because it overfit.
- Rarely asked in interviews, but worth a brief mention: testing on out-of-distribution (OOD) examples, meaning inputs that look meaningfully different from anything in the training set, to check the model generalizes rather than just memorizing surface patterns from the 1,000 examples.
Why this ordering: parameter-efficient tuning (LoRA/adapters) gives the biggest reduction in overfitting risk for the least engineering effort, so it is the natural starting point; the rest are complementary layers of defense stacked on top.
Compare locality-sensitive hashing (LSH) attention (e.g., Reformer) with sparse attention patterns and windowed-local attention. Discuss how each reduces memory and compute, their complexities for long sequences, weaknesses (collisions, missing long-range connections), and hardware friendliness.
Sample Answer
High-level summary:
- All three aim to reduce the O(L^2) time/memory of full attention for sequence length L by restricting pairwise comparisons: LSH attention groups similar tokens (approximate nearest neighbors), sparse attention defines fixed sparse patterns, and windowed-local attends only to nearby tokens (local window). Trade-offs center on accuracy of long-range interactions, deterministic coverage, and hardware efficiency.
Complexity and how they reduce cost:
- LSH attention (Reformer): hashes tokens into buckets so attention is computed only within buckets. Average compute roughly O(L log L) for hashing + O(L * b^2) where b is bucket size; memory reduced because only bucket-wise attention matrices are formed. Works well when similar tokens cluster under the hash.
- Sparse attention (e.g., BigBird, Sparse Transformer): defines a mix of local, strided, random, or global patterns so each token attends to k others → O(L·k) time and O(L·k) memory (linear if k is constant).
- Windowed-local attention: pure sliding window of size w gives O(L·w) time and O(L·w) memory; simplest and very memory-efficient for local contexts.
Weaknesses and failure modes:
- LSH collisions/misplacement: hashing is approximate, so unrelated tokens can collide (false positives) or similar tokens can fall in different buckets (false negatives), causing missed dependencies or wasted compute. Requires careful multi-round or overlapping buckets and is sensitive to choice of hash and query/key normalization. Also harder to backprop through discrete bucket assignments (Reformer uses reversible layers and sorting which add complexity).
- Sparse patterns: deterministic sparsity can miss ad-hoc long-range links unless patterns include global/random/global tokens. Designing patterns that capture arbitrary long-range structure can be tricky; theoretical guarantees (like connectivity) depend on pattern choice.
- Windowed-local: cannot capture distant dependencies beyond w unless stacked layers create indirect paths (depth increases effective receptive field). For tasks needing direct long-range attention (e.g., coreference across paragraphs), pure windowing is insufficient.
Hardware friendliness:
- LSH: involves sorting and hashing (irregular memory access, data-dependent control flow). Sorting/permutes can be implemented efficiently but less friendly to dense matrix-multiply optimized kernels; less straightforward batching on GPUs/TPUs.
- Sparse attention: if sparsity pattern is static and regular (blocked, strided), it maps well to sparse/dense-dense kernels and can leverage optimized sparse BMM or block-sparse primitives (TPU/GPUs with block-sparse support). Irregular sparsity hurts performance.
- Windowed-local: most hardware-friendly, since local attention can be implemented as batched small matrix multiplies or conv-like kernels, giving excellent memory locality and parallelism.
Practical guidance:
- Use windowed or block-sparse patterns when hardware efficiency and predictable performance matter and most dependencies are local.
- Use sparse + a few global/random tokens (BigBird style) when you need provable connectivity with near-linear complexity.
- Use LSH when semantic similarity drives sparse interactions and you accept approximation and implementation complexity; combine with overlapping buckets or multiple rounds to reduce missed links.
Unlock Full Question Bank
Get access to all 39 Transformers and Attention interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.