Long-context training and evaluation

Curriculum learning

Training on long-context tasks from the start is inefficient and unstable. The standard recipe is:

  • Train on standard context (4K or 8K) for ~90% of training steps to teach language, logic, and facts.
  • Run long-context continued pre-training (LCPT / mid-training) on progressively longer sequences (16K → 64K → 128K).
  • During LCPT, the RoPE base bb (in θi=b−2i/d\theta_i = b^{-2i/d}) is often increased (e.g. from 10,000 to 500,000), which lowers the rotation frequencies and stretches wavelengths to accommodate the new lengths.

Short-context dataset balancing

Models trained exclusively on long data "forget" how to handle short tasks, so the data mix during LCPT must remain balanced between short and long documents.

Stabilising logit growth

While extending sequence length at inference tends to flatten attention distributions (higher entropy, as YaRN addresses), during training unconstrained QQ and KK projections can cause attention logits (pre-softmax scores) to drift upward in magnitude:

Logits→∞  ⟹  Softmax→One-Hot\text{Logits} \rightarrow \infty \implies \text{Softmax} \rightarrow \text{One-Hot}

This attention entropy collapse causes gradients to vanish and training to diverge.

Mitigations:

  • QK-Norm: Normalising queries and keys (e.g. via RMSNorm/LayerNorm) before taking their dot product directly bounds attention logit magnitudes; now the standard stabiliser in models such as Gemma 3 and Qwen3.
  • Logit Soft-Capping: Explicitly capping logits with a tanh function: logits=cap⋅tanh⁡(logits/cap)\text{logits} = \text{cap} \cdot \tanh(\text{logits} / \text{cap}). Used in Gemma 2 for both attention and final output logits (Gemma 3 later replaced attention soft-capping with QK-Norm for better FlashAttention kernel compatibility).
  • Z-Loss Regularisation (PaLM): A penalty term on log⁡2∑exp⁡(x)\log^2 \sum \exp(x), primarily applied to final vocabulary output logits to keep the log-partition function bounded and prevent output-layer training instability.

Tags: AI