← Back to modules

Attention Mechanisms — Advanced

RoPE and ALiBi, long-context extension, KV cache math, MQA/GQA/MLA, prefix caching and PagedAttention, sliding-window and eviction, FlashAttention, and linear attention and SSMs.

Hard30 questions

Sample questions

1

How does Rotary Position Embedding (RoPE) inject position, and why is its effect described as relative?

  • It adds a learned position vector to the token embedding once at the input, so deeper layers infer distance from it
  • It rotates query and key vectors in 2D subspaces by a position-dependent angle, so the dot product depends on relative distance
  • It multiplies the attention logits by a fixed distance penalty per head, so far-apart tokens are down-weighted linearly
  • It appends the absolute integer position as an extra feature dimension that the query and key projections then learn to read

Why

RoPE encodes position by rotating each query and key vector within pairs of dimensions by an angle proportional to the token's absolute position, using the same geometric frequencies as sinusoidal encoding. The key algebraic property is that after rotating a query at position i and a key at position j, their dot product depends only on the difference i minus j, so the attention score is a function of relative distance even though the rotation itself uses absolute position. The learned-input-vector option describes additive learned positional embeddings, a different and now largely superseded method. The distance-penalty option describes ALiBi, which biases logits by distance rather than rotating vectors. The append-integer option is fabricated: RoPE does not add a raw position feature for projections to read; it transforms the existing query and key dimensions by rotation. Because RoPE is applied at every attention layer rather than once at the input, each layer receives fresh position information instead of relying on a signal surviving many residual connections. This combination of relative-distance behavior and cheap element-wise implementation is why RoPE dominates current large models.

2

A RoPE-based model trained at 4K context must serve 128K-token inputs. Which approach directly addresses the position-encoding limitation?

  • Switch the tokenizer to a larger vocabulary so the 128K tokens compress down to fit within the original 4K positions
  • Add more attention heads, since each additional head extends the reachable context window by its own head dimension
  • Disable the causal mask beyond position 4095 so later tokens can attend freely without any position information at all
  • Increase the RoPE base frequency or interpolate positions so rotation wavelengths cover the longer context smoothly

Why

RoPE's rotation wavelengths are set by a base frequency, and a model trained at short context has only seen position gradients within that range, so naively feeding far longer inputs pushes rotations into regimes the model never optimized for. The direct fixes operate on the position encoding itself: position interpolation rescales position indices to fit the trained frequency range, and raising the base frequency stretches the low-frequency wavelengths so the model sees smooth gradients across a much longer window, which is how some models natively support 128K context. The larger-vocabulary option is wrong: better token compression reduces token count somewhat but does not change how positions beyond the trained range are encoded, and it cannot turn 128K tokens into 4K positions. The more-heads option is fabricated, since head count does not extend the position range. The disable-causal-mask option is nonsense and would break autoregression while doing nothing about position encoding. The correct family of solutions, including interpolation and its successors, all rescale RoPE's frequencies to generalize position beyond the training length.

3

How does ALiBi encode position, and what is its main advantage over learned absolute embeddings?

  • It rotates the value vectors by a distance-dependent angle, which lets the model extrapolate to any sequence length
  • It learns one bias vector per position up to the maximum length, then reuses the last vector for any longer inputs
  • It adds a fixed, per-head linear penalty to attention logits based on token distance, and extrapolates beyond training length
  • It concatenates a sinusoidal encoding to each key so distant tokens receive a smaller share of the softmax mass

Why

ALiBi uses no positional embeddings at all; instead it adds a fixed penalty to each attention logit that grows linearly with the distance between the query and key, with a different slope for each head so heads cover a range of receptive fields. Because the bias is a simple linear function of distance with nothing position-specific to learn, ALiBi generalizes naturally to sequences longer than those seen in training, which is its headline advantage over absolute schemes that have a hard length ceiling. The rotate-values option is wrong: ALiBi biases logits and does not rotate any vectors, and rotation of queries and keys is RoPE's mechanism. The learn-one-bias-per-position option describes a learned absolute scheme, exactly what ALiBi avoids. The concatenate-sinusoidal option is wrong because ALiBi neither concatenates encodings nor relies on sinusoids; it simply adds a distance penalty. The per-head slope diversity matters because it lets some heads attend locally for syntax and others globally for long-range dependencies. This cheap, extrapolation-friendly design is why ALiBi remains a valid alternative to RoPE for length generalization.

4

Why is the original sinusoidal positional encoding no longer used in current large language models?

  • It is too computationally expensive, since evaluating sine and cosine per position dominates the cost of each forward pass
  • It leaks future positions into the attention scores, which breaks the causal masking that autoregressive models require
  • It is absolute and fixed to the trained length, so it does not generalize well, and relative methods like RoPE replaced it
  • It requires a separate learned embedding table per layer, which made models with many layers infeasible to train

Why

Sinusoidal encoding is an absolute scheme injected once at the input, so it encodes fixed position vectors that the model optimizes for only within its trained length, and performance degrades beyond that range. Modern models favor relative-position methods such as RoPE and ALiBi that behave in terms of token distance, apply position at every layer, and extend more gracefully to longer contexts, which is why sinusoidal encoding fell out of use. The too-expensive option is wrong: sinusoids are cheap and can be precomputed, so cost was never the reason. The leaks-future-positions option is fabricated, since positional encoding does not bypass the causal mask, which independently blocks future tokens. The per-layer-learned-table option is wrong because sinusoidal encoding is parameter-free and is not a per-layer learned table. The essential reason is generalization and the shift to relative-position formulations, which give stronger and more extensible position signals. Sinusoidal encoding remains valuable conceptually because its frequency structure underlies RoPE's rotations.

5

The KV cache size for a decoder scales with which combination of factors?

  • Two times layers times key-value heads times head dimension times sequence length times bytes-per-element
  • Vocabulary size times embedding dimension, since each cached token stores one full output-projection row per layer
  • The square of the sequence length, because the cache must retain the full attention weight matrix for every layer
  • Number of query heads times the feed-forward hidden size, since the cache mirrors the feed-forward activations

Why

The KV cache stores a key vector and a value vector for every past token, in every layer, for every key-value head, so its size is two (for K and V) times the number of layers times the number of key-value heads times the head dimension times the sequence length, multiplied by the bytes per element. This grows linearly with sequence length and with depth, which is why long contexts are memory-hungry. The vocabulary-times-embedding option is wrong: the cache holds per-token keys and values, not output-projection rows, and vocabulary size is irrelevant to it. The square-of-sequence-length option is wrong because the cache stores keys and values, which are linear in sequence length; it is the transient attention score matrix, not the cache, that is quadratic. The query-heads-times-feed-forward option is fabricated, since the cache mirrors keys and values, not feed-forward activations, and it depends on key-value heads, not query heads. Understanding this formula explains why grouped-query attention, which reduces the key-value head count, is such an effective way to shrink the cache.

Free account

Take the full module

These are the first few of 30 questions. A free account opens the rest as a scored drill.

  • Every question in this module
  • Instant feedback and supporting reading
  • Your score and progress, saved

Free · your email is used for progress only.