Model and Inference EngineeringTraining execution and recoveryStaff
Why can longer training contexts require more computation under the same token budget?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
Two dense-attention training runs process the same total number of valid tokens, one in 4,096-token sequences and one in 32,768-token sequences. Derive the attention-work ratio while holding architecture fixed, explain why total job cost need not rise by that ratio, and compare actual packing, masks, kernel choice and useful long-context quality.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
"A trillion tokens" tells me how many token positions the model trained on. It does not tell me how those positions were arranged. Attention compares positions within a sequence. With dense causal attention, one sequence of length L has roughly L squared query-key work, even though there are only L tokens. Holding the total token count fixed, moving from many 4K sequences to fewer 32K sequences can increase the attention arithmetic per token by roughly 8 times. That is only the attention part, not an 8 times prediction for the whole training job.
Let the total token budget be D and sequence length L. There are about D/L sequences, and dense causal attention performs on the order of L² query-key interactions per sequence. The total attention term therefore grows like D×L under fixed model dimensions. Increasing L by eight multiplies that term by about eight. Linear-in-token projection and feed-forward work still scale with D, so the full run’s ratio depends on how large the attention term was to begin with.
Causal masking roughly halves the relevant pair count compared with a full square, but preserves quadratic growth for long sequences. Actual kernels may compute or skip different masked regions. A memory-efficient attention algorithm can avoid storing all pairwise scores while still evaluating the required interactions. Distinguish arithmetic growth, stored activations and measured wall time before selecting an optimization.
There is another trap in the usual training estimate of about 6 times parameters times tokens. That is a useful rough estimate when the model and sequence regime make parameter matmuls dominant. It hides the length-dependent attention term. FlashAttention avoids materializing the whole attention matrix in high bandwidth memory, but it still computes exact attention. Better memory traffic does not turn dense attention into linear arithmetic.
I would ask for actual sequence-length histograms after packing and truncation, attention implementation, tokens per optimizer step, measured forward and backward time, device utilization and achieved versus estimated FLOPs. Two runs can have the same nominal maximum context but very different length distributions. Padding, attention masks, checkpointing and the shape of the kernel also affect wall time. Measure a short and a long batch on the same hardware before attributing the entire bill to the attention formula.
If the interviewer asks whether we can simply pack shorter documents into 32K blocks, I would ask which attention boundary we want. A block-diagonal mask prevents one document from reading another and changes which token pairs are computed by an implementation that can skip masked regions. A plain causal mask over the full packed block permits cross-document attention. That is the separate correctness problem in Sequence packing doubled training throughput. Why can one document attend to another?. For this run, compare quality at the long-context tasks we actually need, and report both tokens and training FLOPs or device hours. Equal token budgets are not equal experiment budgets.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux