Skip to content

13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi

A model trained on contexts of LL tokens has learned what every pair’s angle p θip\,\theta_i looks like for p<Lp < L. Feed it position 2L2L and the slow pairs, which never completed a turn during training, sit at angles it has never seen: its attention degrades and perplexity climbs past the training length.

Concepts from other sections (12)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Real checkpoints ship a fix in config.json. Llama 3.1 has "rope_scaling": {"rope_type": "llama3", "factor": 8.0, ...}, and Qwen2.5 uses yarn. The loader in The Llama-Family Model: Config, Weight Loading, and Download must build exactly the frequencies Hugging Face builds from those fields (its tiny-llama-yarn and tiny-llama-3 fixture models test this), and the TinyStories capstone measures what extension buys at twice the training context. This section computes those frequencies for every scaling rule, and, as an optional final part, builds ALiBi, a position scheme that uses no rotation at all.

You should have Rotary Position Embeddings fresh, since everything here is about what its frequencies do. The default ladder rope_inv_freq and the ALiBi slopes alibi_slopes come from Sequences, geometric series, and frequency ladders in Mathematics. The reference solution is ctxext.py, with rope_inv_freq_scaled, _yarn_ramp, and the optional alibi_bias, against the contract course/contracts/py/tinyllm/modern/ctxext.pyi.

Learning objectives

  • Explain why only the slow RoPE pairs break past the trained context, and why every fix changes frequencies rather than the rotation.
  • Compute position interpolation, NTK-aware, YaRN, and Llama 3 frequencies by hand, including YaRN’s temperature.
  • Implement the scaling rules so they match Hugging Face’s ROPE_INIT_FUNCTIONS to float64 precision.
  • (Optional) Build an ALiBi bias whose decoding rows equal the last rows of prefill.
SymbolMeaningType / shape
rrd_rot, the rotary widthint, even
bbbase (rope_theta)float >1> 1
θi\theta_idefault inverse frequency b−2i/rb^{-2i/r}, i=0..r/2−1i = 0 .. r/2 - 1float64[r/2]
ssfactor: how many times longer the new context isfloat ≥1\ge 1
L0L_0original_max_pos, the trained contextint
λi\lambda_iwavelength 2π/θi2\pi / \theta_i: positions per full turn of pair iifloat
θi′\theta'_ithe scaled inverse frequencyfloat64[r/2]
ttattention scaling, multiplies cos and sinfloat
mhm_hALiBi slope of head hhfloat

Pair ii completes L0/λiL_0 / \lambda_i turns over the trained context. Fast pairs (small λi\lambda_i) turn hundreds of times: every angle was seen, and they encode fine local order. Slow pairs turn less than once: at position 2L02 L_0 they reach angles never seen. The damage is confined to frequencies, so every method below leaves the rotation code untouched and only decides which pairs to slow down and by how much (and sometimes adds a temperature).

The bluntest fix (linear in Hugging Face’s terms) divides every frequency by ss: θi′=θi/s\theta'_i = \theta_i / s. Position sps p then has exactly the angles position pp had. Nothing is new, but neighbouring tokens are now ss times closer in angle on the fast pairs, so the model must be fine-tuned to tell them apart.

The fast pairs never needed help, so a better rule keeps them and slows only the slow ones. NTK-aware scaling (ntk) does it by changing the base instead of the frequencies: b′=b⋅sr/(r−2)b' = b \cdot s^{r / (r - 2)}. Then θ0′=1=θ0\theta'_0 = 1 = \theta_0, and for the last pair i=r/2−1i = r/2 - 1, θi′=b′−(r−2)/r=b−(r−2)/r/s=θi/s\theta'_i = b'^{-(r-2)/r} = b^{-(r-2)/r} / s = \theta_i / s: exactly the factor. Pairs between are slowed by between 1 and ss.

Hugging Face’s dynamic type applies this rule at inference with a factor that grows with the current length, seff=s⋅T/L0−(s−1)s_\text{eff} = s \cdot T / L_0 - (s - 1). At T=2L0T = 2 L_0 and s=2s = 2 that is 3, which is how the golden fixture checks ntk.

NTK still slows the middle pairs by a smoothly varying amount. YaRN (yarn) makes the bands explicit. Pair ii turns L0/λiL_0 / \lambda_i times, and the pair index that turns exactly nn times is

c(n)=rln⁡(L0/(2πn))2ln⁡b.c(n) = \frac{r \ln\left(L_0 / (2\pi n)\right)}{2 \ln b}.

With lo=⌊c(βfast)⌋lo = \lfloor c(\beta_\text{fast}) \rfloor and hi=⌈c(βslow)⌉hi = \lceil c(\beta_\text{slow}) \rceil (clamped to [0,r−1][0, r - 1]; defaults βfast=32\beta_\text{fast} = 32, βslow=1\beta_\text{slow} = 1), the ramp ρi=clamp⁡((i−lo)/(hi−lo),0,1)\rho_i = \operatorname{clamp}((i - lo) / (hi - lo), 0, 1) blends

θi′=(1−ρi) θi+ρi θi/s.\theta'_i = (1 - \rho_i)\, \theta_i + \rho_i\, \theta_i / s .

Pairs turning more than 32 times keep θi\theta_i, and pairs turning less than once get θi/s\theta_i / s. There is one more effect to correct. Stretching the slow pairs makes attention scores flatter (more entropy) at long range, so YaRN sharpens them: cos and sin are multiplied by t=0.1ln⁡s+1t = 0.1 \ln s + 1, which multiplies every score by t2t^2.

Llama 3.1 uses wavelength bands directly (llama3), with L0=8192L_0 = 8192, low factor 1, and high factor 4. A pair with λi<L0/4\lambda_i < L_0 / 4 keeps θi\theta_i, a pair with λi>L0\lambda_i > L_0 gets θi/s\theta_i / s, and between them, with σ=(L0/λi−1)/(4−1)\sigma = (L_0 / \lambda_i - 1) / (4 - 1),

θi′=(1−σ) θi/s+σ θi.\theta'_i = (1 - \sigma)\, \theta_i / s + \sigma\, \theta_i .

There is no temperature, and the frequencies stay strictly decreasing in ii.

This part is optional: ALiBi has no call site in the system, since every model the book loads uses RoPE. It is worth knowing because it shows a completely different answer to the same problem.

ALiBi drops rotations entirely. Each head hh adds a bias −mh⋅(i−j)-m_h \cdot (i - j) to the score of query position ii and key position jj: the further back, the lower. The slopes are a geometric sequence (2−8/n,2−16/n,…2^{-8/n}, 2^{-16/n}, \dots for nn heads). With TqT_q queries that are the last of TkT_k keys, query ii sits at key index Tk−Tq+iT_k - T_q + i. Softmax ignores a constant added to a whole row, so BLOOM’s form mh⋅jm_h \cdot j gives the same weights.

Each rule fits in a small example.

Linear and NTK. Take r=4r = 4, b=104b = 10^4, so θ=(1,0.01)\theta = (1, 0.01). Linear with s=2s = 2 gives (0.5,0.005)(0.5, 0.005). NTK with s=2s = 2 gives b′=104⋅24/2=4⋅104b' = 10^4 \cdot 2^{4/2} = 4 \cdot 10^4 and θ′=(1,(4⋅104)−1/2)=(1,0.005)\theta' = (1, (4 \cdot 10^4)^{-1/2}) = (1, 0.005): the fast pair kept, the slow one halved.

YaRN. Take r=8r = 8, b=104b = 10^4, s=4s = 4, L0=2048L_0 = 2048, so θ=(1,0.1,0.01,0.001)\theta = (1, 0.1, 0.01, 0.001). Then L0/2π=325.95L_0 / 2\pi = 325.95; c(32)=8ln⁡(10.186)/18.421=1.008c(32) = 8 \ln(10.186) / 18.421 = 1.008, so lo=1lo = 1; and c(1)=8ln⁡(325.95)/18.421=2.513c(1) = 8 \ln(325.95) / 18.421 = 2.513, so hi=3hi = 3. The ramp is ρ=(0,0,0.5,1)\rho = (0, 0, 0.5, 1), which gives θ′=(1,0.1,0.5⋅0.01+0.5⋅0.0025,0.00025)=(1,0.1,0.00625,0.00025)\theta' = (1, 0.1, 0.5 \cdot 0.01 + 0.5 \cdot 0.0025, 0.00025) = (1, 0.1, 0.00625, 0.00025), and t=0.1ln⁡4+1=1.138629t = 0.1 \ln 4 + 1 = 1.138629.

Llama 3. Use the same θ\theta with s=8s = 8 and L0=1024L_0 = 1024. The wavelengths are (6.28,62.8,628.3,6283)(6.28, 62.8, 628.3, 6283), and the bands are below 256 (keep) and above 1024 (divide). Pairs 0 and 1 keep, pair 3 becomes 0.0001250.000125, and pair 2 blends with σ=(1024/628.3−1)/3=0.209916\sigma = (1024 / 628.3 - 1) / 3 = 0.209916: 0.01⋅(0.790084/8+0.209916)=0.0030870.01 \cdot (0.790084 / 8 + 0.209916) = 0.003087.

ALiBi. With 2 heads (slopes 1/161/16, 1/2561/256) and 2 queries over 3 keys, the distances are (1,0,−1)(1, 0, -1) and (2,1,0)(2, 1, 0), so head 0’s biases are (−0.0625,0,0.0625)(-0.0625, 0, 0.0625) and (−0.125,−0.0625,0)(-0.125, -0.0625, 0).

def rope_inv_freq_scaled(d_rot, base, kind: Literal["default", "linear", "ntk", "yarn", "llama3"], factor=1.0,
original_max_pos=0, beta_fast=32, beta_slow=1, low_freq_factor=1, high_freq_factor=4
) -> tuple[NDArray, float]: ... # (inv_freq float64 [d_rot/2], attention_scaling)
def alibi_bias(n_heads: int, Tq: int, Tk: int) -> NDArray: ... # float32 [n_heads, Tq, Tk]; optional

rope_inv_freq_scaled returns a new inv_freq and an attention scaling, both of which feed rope_cos_sin from the previous section unchanged. alibi_bias returns one bias matrix per head for the caller to add to the scores before the causal mask.

The damage is confined to pairs whose angles leave the trained range, so the fix belongs in the frequencies θi\theta_i, not in the rotation. The output is a new inv_freq (plus a temperature) that rope_cos_sin consumes unchanged. Every method starts from the default ladder θi=b−2i/r\theta_i = b^{-2i/r} and modifies it, so the solution computes theta = rope_inv_freq(d_rot, base) once and branches on kind.

Linear and NTK have closed forms: divide every θi\theta_i by ss, or recompute the ladder with base b⋅sr/(r−2)b \cdot s^{r/(r-2)}. Reusing rope_inv_freq for NTK means the “fastest kept, slowest divided by ss” property follows from the algebra, not from extra code.

YaRN and Llama 3 need per-pair decisions. Vectorise them: compute a per-pair weight in [0,1][0, 1] (YaRN’s ramp ρi\rho_i, Llama 3’s σ\sigma) and blend θi\theta_i with θi/s\theta_i/s. A clamp turns the blend into “keep” and “divide” at the two ends, so three bands need no branching. YaRN’s band edges are pair indices derived from “how many turns over L0L_0”. Inverting L0/λi=nL_0 / \lambda_i = n with λi=2πb2i/r\lambda_i = 2\pi b^{2i/r} gives c(n)c(n), and the edges are rounded outward (floor the low edge, ceil the high) so the blend region never shrinks.

The return type carries the temperature because it belongs to the same config field and must reach rope_cos_sin’s attention_scaling; it is 1 for every kind but YaRN. Finally, configs come from JSON written by people, so every parameter a kind needs is validated for that kind (an original_max_pos only matters to YaRN and Llama 3).

def _yarn_ramp(d_rot, base, original_max_pos, beta_fast, beta_slow):
"""ramp_i in [0, 1] per pair: 0 keeps theta_i, 1 divides it by factor."""
def corr(n_rot):
# the pair index whose wavelength fits n_rot times into the context:
# solve L0 / (2 pi b^(2i/r)) = n_rot for i
return d_rot * math.log(original_max_pos / (n_rot * 2 * math.pi)) / (2 * math.log(base))
# round outward and clamp to valid indices (HF's find_correction_range)
lo = max(math.floor(corr(beta_fast)), 0)
hi = min(math.ceil(corr(beta_slow)), d_rot - 1)
if lo == hi:
hi += 0.001 # avoid 0/0 when both edges land on one pair
i = np.arange(d_rot // 2, dtype=np.float64)
return np.clip((i - lo) / (hi - lo), 0.0, 1.0)

On the worked numbers, corr(32) is 1.008 and corr(1) is 2.513, so lo = 1, hi = 3, and the ramp over i=0..3i = 0..3 is (0,0,0.5,1)(0, 0, 0.5, 1). The factor 2 in the denominator is the “2” in b2i/rb^{2i/r}; dropping it moves both edges by a factor of two.

def rope_inv_freq_scaled(d_rot, base, kind, factor=1.0, original_max_pos=0,
beta_fast=32, beta_slow=1, low_freq_factor=1, high_freq_factor=4):
if kind not in ("default", "linear", "ntk", "yarn", "llama3"):
raise ValueError(f"unknown kind {kind!r}")
# bool is an int subclass: reject True explicitly; d_rot must be even for pairs
if isinstance(d_rot, bool) or int(d_rot) != d_rot or d_rot < 2 or d_rot % 2:
raise ValueError(...)
if not base > 1:
raise ValueError(...)
d_rot = int(d_rot)
theta = rope_inv_freq(d_rot, base) # the trained ladder
if kind == "default":
return theta, 1.0
if not factor >= 1: # factor < 1 would shrink the context
raise ValueError(...)
if kind == "linear":
# position interpolation: position p behaves like p / factor
return theta / factor, 1.0
if kind == "ntk":
# NTK-aware: a larger base; the fastest pair keeps its frequency and
# the slowest (i = r/2 - 1) is divided by exactly factor.
return rope_inv_freq(d_rot, base * factor ** (d_rot / (d_rot - 2))), 1.0
# ... validate original_max_pos >= 1 (only yarn and llama3 need it)

not factor >= 1 rather than factor < 1 also rejects NaN, which compares false to everything.

if kind == "yarn":
ramp = _yarn_ramp(d_rot, base, int(original_max_pos), beta_fast, beta_slow)
keep = 1.0 - ramp # HF's inv_freq_extrapolation_factor
# keep = 1: extrapolate (trained theta); keep = 0: interpolate (theta / s)
inv = (theta / factor) * (1.0 - keep) + theta * keep
# sharpen attention: cos and sin times t, every score times t^2
scale = 1.0 if factor <= 1 else 0.1 * math.log(factor) + 1.0
return inv, scale

The blend is written with Hugging Face’s keep so the expression matches _compute_yarn_parameters term for term; it is (1−ρi)θi+ρiθi/s(1 - \rho_i)\theta_i + \rho_i \theta_i / s. On the example, pair 2 gets 0.5⋅0.0025+0.5⋅0.01=0.006250.5 \cdot 0.0025 + 0.5 \cdot 0.01 = 0.00625 and t=1.138629t = 1.138629.

if not high_freq_factor > low_freq_factor > 0: # swapped factors flip the bands
raise ValueError(...)
L0 = float(original_max_pos)
wavelen = 2 * math.pi / theta
# long wavelengths (slow pairs, > L0 / low) are divided by factor; others kept for now
inv = np.where(wavelen > L0 / low_freq_factor, theta / factor, theta)
# sigma: 0 at wavelength L0 / low, 1 at L0 / high
smooth = (L0 / wavelen - low_freq_factor) / (high_freq_factor - low_freq_factor)
smoothed = (1 - smooth) * inv / factor + smooth * inv
# the medium band is neither short (< L0/high) nor long (> L0/low)
medium = ~(wavelen < L0 / high_freq_factor) & ~(wavelen > L0 / low_freq_factor)
return np.where(medium, smoothed, inv), 1.0

In the medium band inv is still theta, so smoothed is (1−σ)θi/s+σθi(1 - \sigma)\theta_i/s + \sigma\theta_i, the Llama 3 formula. Pair 2 of the example has wavelength 628.3, between 256 and 1024, so σ=0.209916\sigma = 0.209916 and θ2′=0.003087\theta'_2 = 0.003087. Because σ\sigma grows with frequency, the blended values sit between their neighbours and the ladder stays strictly decreasing.

def alibi_bias(n_heads, Tq, Tk):
if min(n_heads, Tq, Tk) < 1 or Tq > Tk:
raise ValueError(...)
slopes = alibi_slopes(n_heads)
# queries are the LAST Tq of Tk positions (prefill: Tq = Tk; decode: Tq = 1)
i = np.arange(Tq)[:, None] + (Tk - Tq) # absolute index of each query
j = np.arange(Tk)[None, :]
dist = (i - j).astype(np.float64)
# further back means lower score; future keys (dist < 0) are masked by the caller
return (-slopes[:, None, None] * dist[None]).astype(np.float32)

The offset Tk - Tq is what makes a one-row decoding bias equal the last row of the prefill bias. The full solution is in ctxext.py.

Everything here is O(r)O(r) work, done once when a model is loaded, so precision rather than speed is what matters: the ladder is float64 and is cast to float32 only inside rope_cos_sin.

Linear interpolation could equally be written as “divide positions by ss”. Changing the frequencies instead keeps one code path for every method and lets the decoder in KV cache and generate pass raw integer positions. The solution also matches Hugging Face’s formulas exactly. Several algebraically equal ways exist to write the YaRN blend or the Llama 3 bands; following Hugging Face’s term order and rounding (floor/ceil of the correction range, the + 0.001 guard) makes the golden values agree to float64 precision. A “cleaner” formula that rounds the range inward moves a pair into the wrong band.

Finally, the scaling is static, not dynamic. Hugging Face’s dynamic type recomputes the NTK base per length, which changes the frequencies under an existing KV cache, so keys cached at the old frequencies no longer match. The solution’s ntk takes one fixed factor, so the table never changes while a model is decoding.

The checks mirror the worked examples and the design. The hand examples for linear, NTK, default, YaRN (ramp and temperature), and Llama 3 (all three bands) must come out exactly, and ten golden cases from Hugging Face’s ROPE_INIT_FUNCTIONS, covering every kind, must match, since rope_scaling configs depend on them. Property tests check the reason each method works: linear interpolation maps the angles at sps p onto the trained angles at pp; NTK keeps the fastest pair, divides the slowest by ss, and is monotone between; YaRN keeps fast pairs, divides slow ones, applies the temperature, and reduces to the default at s=1s = 1; and the Llama 3 bands keep frequencies strictly decreasing so no pairs are reordered. Validation must reject an unknown kind, an odd width, a bad base, a factor below 1, a missing L0L_0, and swapped band factors. For the optional ALiBi part, the hand example must match, the softmax weights must equal BLOOM’s after the causal mask, a decoding row must equal the last row of prefill, and more queries than keys or zero sizes must raise.

test_ctxext.py writes the rules out pair by pair in plain Python floats and compares rope_inv_freq_scaled against them for linear, NTK, YaRN at several sizes, and Llama 3, checks YaRN’s temperature, and checks that bad configs raise. A loop over pairs in scalar floats is a good oracle for vectorised numpy code: it is slow and obvious, so a mistake in a mask or a broadcast in the solution has nowhere to hide.

Warning: YaRN’s ramp is easy to apply backwards. Ramp 0 at low indices (the fast pairs) means keep; if you invert it, fast pairs are interpolated and slow ones extrapolated, and local order blurs. The solution writes keep = 1 - ramp to make the direction explicit.

The pitfalls and how the solution avoids each:

PitfallSymptomHow the solution avoids it
multiplying by the factor in position interpolationthe context shrinks instead of growingtheta / factor in the linear branch
scaling the base by ss, not sr/(r−2)s^{r/(r-2)}the slowest pair is not slowed by exactly ssbase * factor ** (d_rot / (d_rot - 2))
YaRN’s ramp backwardsfast pairs are interpolated, slow ones extrapolated: local order blursramp 0 at low indices (fast pairs) means keep: keep = 1 - ramp
forgetting YaRN’s temperaturelong-range attention too flat; perplexity a little worse everywherereturns 0.1 * log(factor) + 1 as the second value
swapping Llama 3’s low and high bandsthe wrong pairs are slowedlong wavelengths compare against L0 / low_freq_factor, short against L0 / high_freq_factor; swapped factors raise
rounding the correction range inwardone pair lands in the wrong bandfloor for lo, ceil for hi
a missing 2 in the correction dimensionthe bands move by a factor of twocorr divides by 2 * math.log(base)
ALiBi bias growing with distancefar tokens dominate (optional part)the bias is -slope * dist
ALiBi queries treated as the first keysdecoding rows wrong (optional part)query index offset by Tk - Tq

In The Llama-Family Model: Config, Weight Loading, and Download, LlamaConfig.rope_scaling becomes a RopeSpec through rope_inv_freq_scaled, on the way to SmolLM2-135M logits that match Hugging Face. The TinyStories capstone runs the long-context evaluation: bits per byte at twice the training context, with and without rope_scaling = yarn.

  • Chen et al., “Extending Context Window of Large Language Models via Positional Interpolation” (2023).
  • bloc97, “NTK-Aware Scaled RoPE” (2023).
  • Peng et al., “YaRN: Efficient Context Window Extension of Large Language Models” (2023), sections 3.1 to 3.4.
  • Meta, “The Llama 3 Herd of Models” (2024).
  • Press et al., “Train Short, Test Long: Attention with Linear Biases” (2022).
  • Hugging Face ROPE_INIT_FUNCTIONS in transformers/modeling_rope_utils.py: adds dynamic (recomputed per length), longrope (per-pair learned factors), and DeepSeek’s mscale.
  • vLLM YaRNScalingRotaryEmbedding in rotary_embedding.py: precomputed scaled tables with the temperature folded in.
  • Ding et al. 2024, LongRoPE: searched per-pair factors; most of the gain still comes from continued training at the longer length.
  • BLOOM and MPT fuse the ALiBi bias into attention kernels; see FlashAttention’s alibi_slopes argument.
  • Wavelength (λi\lambda_i): positions per full turn of RoPE pair ii, 2π/θi2\pi / \theta_i.
  • Position interpolation: dividing every frequency by the factor so long positions reuse trained angles.
  • NTK-aware scaling: raising the base by sr/(r−2)s^{r/(r-2)} so the fastest pair is kept and the slowest is divided by ss.
  • YaRN: a per-pair ramp between keeping and dividing frequencies, by turns over the trained context, plus a temperature 0.1ln⁡s+10.1 \ln s + 1.
  • Llama 3 scaling: wavelength bands with a smooth blend between keep and divide, no temperature.
  • ALiBi: a per-head bias that falls linearly with query-key distance, used instead of rotations.
  • Every RoPE extension method changes only the frequencies (and sometimes a temperature); the rotation code is unchanged.
  • Position interpolation slows every pair by the factor, so long positions reuse trained angles exactly.
  • NTK-aware scaling raises the base so the fastest pair is untouched and the slowest is slowed by exactly the factor.
  • YaRN and Llama 3 split the pairs into bands by how often they turn over the original context: keep the fast ones, slow the slow ones, blend between; YaRN also sharpens attention by 0.1ln⁡s+10.1 \ln s + 1.
  • ALiBi (optional) uses no rotation at all: a bias that falls linearly with distance, one slope per head.
  1. Why does NTK raise the base by sr/(r−2)s^{r/(r-2)} and not by ss?
    AnswerThe last pair has exponent $-(r-2)/r$. For it to be divided by exactly $s$ we need $b'^{-(r-2)/r} = b^{-(r-2)/r}/s$, so $b' = b \cdot s^{r/(r-2)}$. Pair 0 has exponent 0 and is unchanged whatever the base.
  2. Why is YaRN’s band defined by how many turns a pair makes over L0L_0?
    AnswerA pair that turns many times over the trained context has seen every angle, so extrapolating it is safe and keeps fine local order. A pair that turns less than once has not, so it must be interpolated. Turns per context is exactly the quantity that separates these.
  3. Why must YaRN multiply cos and sin by t=0.1ln⁡s+1t = 0.1 \ln s + 1?
    AnswerSlowing the slow pairs makes far keys look more alike, flattening (raising the entropy of) attention at long range. Scaling $q$ and $k$ by $t$ multiplies every logit by $t^2$, sharpening the softmax back. Without it perplexity is slightly worse everywhere.
  4. What goes wrong if _yarn_ramp rounds lo up and hi down?
    AnswerThe blend region shrinks by up to a pair at each end, so a pair Hugging Face blends is instead kept or fully divided. The frequencies then disagree with the checkpoint's training and with Hugging Face's golden values.
  5. Why does alibi_bias add Tk - Tq to the query index?
    AnswerDuring decoding the $T_q$ new queries are the last positions of a $T_k$-long sequence. Without the offset, query 0 would be treated as key position 0, and the bias would not match the prefill row it is supposed to reproduce.
  6. Why does the solution avoid Hugging Face’s dynamic scaling?
    Answer`dynamic` recomputes the NTK base as the sequence grows, which changes the frequencies under an existing KV cache; keys cached at the old frequencies no longer match the new queries. A fixed factor keeps the table constant while decoding.