Long documents make a training batch smaller than it looks
A long-context training batch is usually made of a few long documents, cut into pieces. Pieces of the same document are alike and usually push the model in much the same direction, so each training batch gains less than its size suggests. To measure how much less, I cut real documents into 512-token chunks and ran them through open models. For code training, chunks from one code repository were worth only 20–28% as much as the same amount of ordinary web text.
Correction, 17 Nov 2026. Measured with every chunk inside its document's full causal context, as in real long-context training, the 16K variance ratio is about 1.3 (Qwen2.5-1.5B and deepseek-coder-1.3b, on documents first created in 2026), below the preregistered hurdle of 1.5. The standalone-chunk measurement used here overstates the excess R − 1 about fivefold on the same documents (exploratory), so the claim that a code repository's chunks are worth 20–28% of the same amount of web text no longer stands. See version 7 of the report and its data. This page is kept unchanged below as the record of version 6.
- Chunks from the same code repository or arXiv paper have correlated gradients, and the correlation is still there even at the longest distance I measured (16K tokens).
- Take 32 chunks from one code repo and you get 3.5–4.9× fewer independent samples than from web text. For arXiv it's around 2.3×, and books only 1.3–1.6×.
- In effective samples, 1M tokens of whole code repos ≈ 204–285K tokens of web text. That’s a sample count, not a noise measurement, because a single code chunk is noisier or quieter than a web chunk to begin with.
- What I measured is a proxy. Every chunk was run as its own 512-token sequence, while real 16K training lets each chunk attend to the ones before it, and that changes its gradient. I did not measure that case.
- The effect is there at every Pythia-410M checkpoint from 0.7% of training to the end, at the final checkpoints of Pythia-1.4B and OLMo-2-1B, and in all four parameter groups.
Two ways to fill a batch
Pretraining usually packs many short web documents into each sequence. Long-context training does something different: it fills a 16K or 64K sequence with one long document. That could be a book, a code repository glued together file by file, or a paper.
Both batches have the same number of tokens, so on paper they are the same batch size. But the 32 chunks of one repository are not 32 independent samples. They share variable names, style, topic and imports. If their gradients point the same way, averaging them cancels less noise than averaging 32 unrelated chunks would.
The idea in one formula
Statisticians who run surveys already have a word for this: the design effect. Interview whole households instead of random people and your answers will be noisier than the headcount suggests. For an average of m chunks, the variance is
σ² is how noisy one chunk’s gradient is, and ρ is how much two chunks’ noise moves together. Independent chunks give R = 1. R = 4 means your batch is as noisy as one with a quarter of the samples. In McCandlish et al.’s terms, R is how much Bsimple goes up when you trade shuffled chunks for whole documents.
How I measured it
I published the plan before computing a single gradient. One thing in this post isn’t in it: the comparison with packed web in Result 3. I added that after I’d seen some of the data (more under What went wrong). The setup:
| models | Pythia-410M (4 checkpoints), Pythia-1.4B (2), OLMo-2-1B (2) |
|---|---|
| corpora | code repositories (The Stack, concatenated by repo), arXiv papers, PG-19 books, and packed FineWeb as the control |
| sample | 16 documents per corpus, the same 16 for every model; from each, the span from token 2,048 to 18,432, cut into 32 chunks of 512 tokens |
| per cell | 512 chunk gradients and their inner products (a Gram matrix) |
| coverage | 32 planned cells: 24 complete, 5 partial, 3 missing; every final checkpoint is complete |
| hardware | CPU only, 4-core cloud machines; no GPU |
Running chunks on their own keeps attention out of the measurement. It also means this is not the gradient of a real 16K training sequence, where each chunk sees everything before it. The numbers describe training batches of 512-token chunks grouped by document, a proxy for long-context training.
How the gradients are compressed and compared: see Method on GitHub.
Result 1: the correlation reaches far
Two chunks from the same repo, almost 16K tokens apart, still correlate at about 0.1 (0.14 on Pythia-1.4B, 0.09 on OLMo-2). ArXiv drops from 0.13 for neighbouring chunks to 0.07–0.08 at 2K and 0.04–0.06 at the far end. Books stay low, around 0.02–0.03 past 2K. Web text does what packing predicts: a little correlation between neighbours, where one page runs across two chunks, and nothing past about 4K.
A 16K block has 496 chunk pairs and R adds them all up, so even 0.1 matters. These are averages, and single pairs vary a lot.
Result 2: R grows with block length
At 16K tokens, a block of code is 4.7–6.9× noisier than the same number of independent code chunks. ArXiv comes in at 3.0–3.3, books at 1.75–2.2.
Result 3: against the control
Packed web isn’t at 1 either. Short pages spill across chunk boundaries, which puts it at R = 1.3–1.4. So I divide every corpus by packed web, with the same model and checkpoint: Rrel = Rcorpus / Rweb.
Rrel compares how much each layout cuts the number of effective samples. It doesn’t compare raw noise. Code and web chunks differ in how noisy they are and in their mean gradient, and Rrel ignores both.
| corpus | Pythia-1.4B | OLMo-2-1B | raw R(16K) |
|---|---|---|---|
| code repositories | 4.91 [2.61, 7.78] | 3.51 [2.37, 5.23] | 6.91 / 4.73 |
| arXiv papers | 2.36 [1.91, 2.96] | 2.23 [1.78, 2.92] | 3.32 / 3.01 |
| PG-19 books | 1.56 [1.26, 1.98] | 1.30 [1.07, 1.67] | 2.20 / 1.75 |
| packed web | 1 | 1 | 1.41 / 1.35 |
The two models disagree on the exact size but agree on the order: code, then arXiv, then books. The intervals come from resampling the 16 documents with replacement, so they reflect only the documents I drew. Books sit near 1.5, the threshold the plan set in advance for raw R and the addendum later applied to Rrel.
What it means for your batch size
1 / Rrel gives an effective batch size: the tokens of packed web that hold as many effective independent samples.
| 1M tokens per step of… | as many effective samples as (Pythia-1.4B) | (OLMo-2-1B) |
|---|---|---|
| whole code repositories | 204K web tokens [129K, 382K] | 285K web tokens [191K, 421K] |
| whole arXiv papers | 424K [337K, 523K] | 448K [342K, 562K] |
| whole books | 639K [504K, 793K] | 769K [597K, 932K] |
The brackets are the 95% intervals carried over from Rrel; the ranges in the text are the two models' point estimates. This matters because long-context data mixes lean heavily on code repositories. ProLong's long-document data, for instance, is mostly repositories and books. If you size a whole-document batch by its token count, as you would packed data, it may hold several times fewer independent samples than that count suggests.
The table counts samples, for 512-token chunks grouped by document. It does not say what a 16K training step sees, and it gives no learning-rate rule: Bsimple ignores curvature, and a noisier gradient does not by itself mean a worse model.
More checks
The ordering holds at every Pythia-410M checkpoint and in every parameter group of Pythia-1.4B. Per-repository results, the check of the gradient compression and the full tables are in the full report.
An interactive version of this post, with a batch-size calculator and every curve, is at severinvisionary.github.io/doc-batch-gradients.
Try it yourself
Pick a corpus, a model and the tokens per step you train with. Then flip through the curves behind the numbers above.
What went wrong, and what I changed
The control failed. The plan required packed web to come in below 1.2, and at the final checkpoints it came in at 1.26–1.41, because of the short-range overlap described above. Under the preregistered rules that makes the verdict inconclusive, and it stays inconclusive.
I then wrote an addendum that judges each corpus against packed web, the Rrel above. That rule is post hoc. When I wrote it I had seen all of the Pythia-410M results and part of the 1B data, including half of the Pythia-1.4B web control (R(16K) = 1.59 on 8 documents). Under the new rule none of the plan's stopping criteria fire.
What this does not show
Long-context training can cost some short-context quality, depending on the data mix; ProLong, for example, reports both the losses and mixes that avoid them. A noisier gradient is one possible reason. I measured how big the extra noise is in the proxy.
A training experiment to test this is described in the full report.
Related work
- Everett and Qiu (arXiv 2609.04577, Appendix I) guess that tokens in the same sequence are more correlated, and suggest testing it at matched tokens.
- Bsimple is from McCandlish et al. (arXiv 1812.06162).
- Thomas (arXiv 2607.05872) shuffles documents too, but to study the gradient spectrum. Within-document correlation turned out to be a small part of that. Distance isn't looked at.
- The critical-batch-size paper of Zhang et al. (arXiv 2410.21676) finds context length barely matters, but that's packed data, up to 4K.
- Dataset Decomposition (Pouransari et al., arXiv 2405.13226) and ProLong (Gao et al., arXiv 2410.02660) are about which long data to train on and in what order. Neither measures gradients.
I couldn't find anyone who measured this before. If you know of a paper, email me.
Data and code
Everything is in doc-batch-gradients: the probe,
the analysis, the Gram matrices for all 57 shards, and the full report. Every number on this page can be
recomputed on a laptop in about 20 minutes. The plan, the deviations and the addendum are in docs/.
How to cite
This post, the code and the analysis report are archived on Zenodo as doi:10.5281/zenodo.23268861. The Gram matrices are too large for that deposit; they stay in the GitHub repository at tag v2.0, and the deposit lists their SHA-256 hashes.
@techreport{yang2026longdocbatch,
author = {Yang, Hanyu},
title = {Long documents make a training batch smaller than it looks: measuring within-document gradient correlation},
year = {2026},
institution = {Zenodo},
doi = {10.5281/zenodo.23268861},
url = {https://doi.org/10.5281/zenodo.23268861}
}