Long-context training mixture design
Measure sequence, token, and compute mixtures separately while controlling packing, supervision, and benchmark leakage.
On this page
The question for today
What you’ll understand
- Calculate how sequence sampling differs from token and attention-work shares.
- Preserve intended independence and loss masks when packing examples.
- Separate training, adaptation, and held-out long-context evidence.
A dataset can contain few long examples yet spend most tokens and attention work on them. Reporting only the percentage of long sequences can therefore hide what the model actually trained on and what the experiment cost. LongAlign studies mixed short and long instruction data and packing choices; Position Interpolation supplies another concrete adaptation setting. Their recipes motivate measurement, not a universal mixture ratio for every model and workload.
Declare the unit being sampled and weighted
A training mixture can be specified by documents, sequences, input tokens, supervised target tokens, or optimizer updates. These are different units. A long input with a short supervised answer may consume substantial compute while contributing relatively few unmasked loss terms. State whether batches sample examples uniformly or target token proportions, and how losses are normalized. Otherwise an apparent benefit from a long-data mixture can come from changing the effective objective or training exposure rather than improving long-distance information use.
Character and word lengths are not interchangeable with model-token lengths, especially across languages. Measure with the deployed tokenizer and record truncation. A long document cut into short pieces no longer supplies the same long-range dependencies, while concatenating unrelated short pieces produces a long tensor without necessarily creating useful coherent context. Include tasks that demand distant relationships when that is the intended capability. Mere exposure to high position indices does not establish that the model learned to integrate several separated facts.
Packing and length schedules change the experiment
Packing can reduce padding waste by placing several examples in one tensor, but attention masks and position policies must preserve the intended example boundaries. If unrelated examples are meant to be independent, accidental cross-example attention can expose targets or create spurious dependencies. Loss masking must distinguish instructions, context, and supervised answers according to the objective. LongAlign discusses packing with attention isolation and loss weighting; those details are part of the training method rather than an incidental throughput optimization.
For full dense attention, pairwise interaction work grows roughly quadratically with sequence length before accounting for projections, feed-forward layers, and implementation effects. Thus long examples can dominate the attention component even when their token share is modest. Flash-style kernels can avoid materializing the full score matrix and change IO behavior, but do not make the mathematical full-attention pair count linear. Measure actual training time and memory under the chosen architecture instead of turning a pair-count proxy into a hardware benchmark.
Keep evaluation sources outside adaptation
Split source documents, near duplicates, generated templates, and answer-bearing records before forming long sequences. A document split after overlapping windows can place almost identical evidence into both training and evaluation. Synthetic needles also need fresh values and held-out generation patterns; repeatedly testing the same template can reward memorized format behavior. Record corpus versions, deduplication rules, mixture weights, lengths, and adaptation tokens so a later result can distinguish data effects from a positional or architecture change.
The probability lab illustrates only a declared sequence-sampling assumption. Reset, set long-sequence probability to 0.2 and trial count to 10, predict the expected long count, then Step: two. Compare a fresh Reset with both probability at 0.2 and trials at 30 before Step: six. It displays exact binomial masses under independent equal-probability draws, not actual batches, token exposure, quadratic attention work, or data leakage. Use the separate accounting equations to translate a specified mixture into those other units.
Work it through
A small sequence share dominates attention work
A hypothetical dataset has 80 sequences of 1,024 tokens and 20 sequences of 16,384 tokens. Assume no padding or truncation, and equal-width full-attention work proportional only to squared length. Compute long-sequence, token, and attention-proxy shares.
Long examples are 20 of 100 sequences, so their sequence share is 20%. That is only the example-count unit.
Short examples contain 80 × 1,024 = 81,920 tokens; long examples contain 20 × 16,384 = 327,680. Long examples provide 327,680/409,600 = 80% of input tokens.
The long-to-short attention-proxy ratio is (20/80) × (16,384/1,024)² = 64. Thus long examples account for 64/65, approximately 98.46%, of that proxy, not necessarily of total training time.
A mixture percentage is interpretable only after its counting unit and computational assumptions are named.
The living notebook
Explore the model
Interactive lab · probability
Sequence sampling is one mixture unit
Reset, set p = 0.2 and n = 10, predict expected long sequences, then Step: two. Compare a fresh Reset with both p = 0.2 and n = 30 before Step: six. This independent-binomial model does not construct training batches, count tokens, simulate attention, or inspect leakage.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forSequence probability alone does not determine token, target-loss, or compute exposure.
Pause & reason
Check your understanding
Your turn
Try it yourself
A mixture contains 75 sequences of 2,048 tokens and 25 of 8,192 tokens, without padding or truncation. Compute the long token share and equal-width full-attention squared-length share.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The length ratio is four. Use that ratio once for tokens and twice for the attention proxy.
Compare with the solution
The long token share is (25 × 4)/(75 + 25 × 4) = 4/7, approximately 57.14%. Its attention-proxy share is (25 × 16)/(75 + 25 × 16) = 16/19, approximately 84.21%. The sequence share is only 25%. Neither proxy supplies the target-token share or measured runtime, and packing or sparse attention can change the applicable accounting.
Keep the idea
Take it with you
- Mixture claims need explicit sequence, input-token, supervised-target, and compute units.
- Packing masks and loss normalization are part of the learning contract.
- Source-level splits and fresh templates are necessary controls for long-context evaluation.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.