Skip to content

Stage the training data: parallel, resumable downloads checked against each file's sha256 - #73

Open
Aadi-Tiwari wants to merge 17 commits into
exea-labs:mainfrom
Aadi-Tiwari:aadi/stage-data
Open

Aadi-Tiwari wants to merge 17 commits into
exea-labs:mainfrom
Aadi-Tiwari:aadi/stage-data

Conversation

@Aadi-Tiwari

@Aadi-Tiwari Aadi-Tiwari commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

This adds exealm.data.stage, for the README to-do "Download and stage every DATA.md source: parallel, resumable, checksummed". It downloads the files of an LLM-jp path or a Hugging Face dataset several at a time, continues a cut download from the byte it stopped at, and keeps a file only once it matches the size and sha256 its host publishes. It's the step before packing, so it should help with the "(data) Pack the training data" to-do on the nairr branch, which needs at least 7.5B packed tokens.

What changed

  • exealm/data/stage.py. Both hosts keep their data files in Git LFS, which publishes each file's size and sha256. A file downloads to <name>.part and only gets its final name once both match, so a file under its final name is complete. Running the same command again skips finished files and asks for the rest of partial ones with a byte range. If the server ignores the range, the file starts over instead of getting its first part twice. A file that fails its check is deleted, so the next run fetches it fresh.
  • manifest.jsonl lists every finished file with its source, url, size and sha256. If the plan has a new sha256 for a path the manifest already lists, the file is checked and fetched again even when its size didn't change. The old version stays in place until the new one passes its check, so a failed download doesn't lose it. A file in the folder that the manifest doesn't list with the plan's sha256 (the manifest was deleted, or files were copied in by hand) is hashed before it counts as finished. A manifest line cut short by a kill is skipped instead of stopping every later run.
  • Ctrl-C stops a run cleanly: running files stop after the chunk they're writing and keep their .part files, and queued files never start. Python's thread pool would otherwise run every queued file before the stop took effect.
  • Network errors and 408/425/429/5xx answers are retried, and each try continues from the bytes already written. Python's read(n) returns an empty chunk when a connection drops instead of raising, so a short read now counts as a dropped connection and the partial file is kept. The tests caught this one: at first a cut download looked finished, failed the size check, and deleted the good bytes. A body longer than the published size stops being read at the first chunk past it.
  • LLM-jp sizes and hashes come from GitLab's GraphQL API, 50 files per request. Hugging Face files are pinned to the dataset's commit when the plan is made. --plan_only prints each source's file count and size, and staging stops before it starts if the disk doesn't have room for what's left.
  • --max_files, --max_gb and --max_tokens cap each source on its own. --max_tokens works for the LLM-jp paths with a published count in sources.LLMJP. It turns the count into bytes with that source's own bytes per token, so it's an estimate (LLM-jp counted with its own tokenizer). A source without a published count (all of K2, and ja/ja_cc/level0 to level2) refuses a token cap and points to --max_gb. Mix v0 has both, so --mix v0 takes --max_gb or --max_files.
  • Under a cap, files are tried coarse to fine through the list (first, middle, quarters and so on) and each one that still fits is kept, so a sample covers the whole source. A file is never split, so the plan prints a note when one file is already over the cap.
  • --to_jsonl DIR turns the staged parquet files into the jsonl.gz lines of {"text", "source"} that download.py writes and quality.py reads. K2 subsets go through the verified formatters in formatters.py, and only the text column is read. converted.jsonl in the staging folder records the sha256 of the parquet file each output came from, so a rerun converts only new files, new versions and deleted outputs. A listed file that's missing is skipped with a note. File times can't do this job: on Linux two writes a few milliseconds apart can get the same time, which is how the first version of this check failed in CI.
  • download.get_json retries 429 and 5xx answers, waiting as long as Retry-After asks. GitLab allows 60 API calls a minute without an account, and planning all of mix v0 stopped with a 429 before this. The LLM-jp file listing in download.py uses it too.
  • docs/data.md has a "Staging the full sources" section, and the README to-do says what's built and what's left (running it on the real mix).

Tests

tests/test_stage.py has 40 tests. Most use a local HTTP server that can cut a connection partway, ignore byte ranges, or answer 503 or 416. They check that:

  • a finished file matches, is listed in the manifest once, and a rerun downloads nothing
  • a cut download continues from the bytes it has, both on the next run and on a retry inside one run
  • a server that ignores ranges gets a clean restart, not a doubled first part
  • server errors are retried, a 404 is not, a file that's shorter on the server than published fails its check, and a longer one stops being read once it passes its size
  • wrong bytes or a stale partial file fail the check and leave nothing behind, and the next run repairs it
  • a file the manifest doesn't list, or whose manifest line was cut short, is hashed before it counts, and a wrong one is fetched again
  • --rehash finds a same-size damaged file and fetches it again, and a new version of a file at the same size (a new sha256 in the plan) is fetched again even without --rehash
  • when the new version's download fails, the old file stays and the next run resumes the new one, but a damaged file, or one the manifest doesn't list, is still deleted even when its download fails
  • Ctrl-C stops the running files with their partial files kept, the queued files never start, and the next run resumes both
  • with 4 workers, all 12 files arrive and match, and staging refuses to start when the disk is too small, counting a new version's bytes while the old one is kept
  • both planners read sizes and hashes the right way (GraphQL batches, a pinned Hugging Face commit, non-LFS and non-data files skipped)
  • the caps: spread picks, byte and token caps, the one-file note, and a token cap on a source without a count
  • --to_jsonl drops rows without text, keeps the source names, skips finished files, redoes a deleted output and one made from an older version (also when the new version was finished from an old partial file, or has the same file time as the old output), skips a missing file, and refuses a K2 folder with no verified formatter
  • busy API answers are retried after the wait they ask for
python -m pytest tests/test_stage.py tests/test_formatters.py tests/test_data.py tests/test_files.py tests/test_post_data.py -q

The full suite passes on Windows 11 with CPU torch on the last commit (a7b92fd): 324 passed, 1 skipped (python -m pytest -q, 6 to 12 minutes).

How I tested it

Planning, against the live hosts:

python -m exealm.data.stage --source ja/ja_wiki --source IFM/Pretrain-Behaviors --plan_only
# ja/ja_wiki 6 files 1.87 GB; IFM/Pretrain-Behaviors 4,165 files 8,369.92 GB
python -m exealm.data.stage --source en/en_dolma/pes2o --max_tokens 5 --plan_only
# 7 of 39 files, 7.66 GB (the cap works out to 8.35 GB)
python -m exealm.data.stage --source IFM/Math-Reasoning/math-dialogue --max_gb 10 --plan_only
# 4 files, 8.05 GB
python -m exealm.data.stage --mix v0 --max_files 1 --plan_only
# 30 sources, one file each, 84.39 GB, in 2 min 42 s (it waits out the rate limits)

The size of ja/ja_wiki/0004.jsonl.gz from GraphQL (294,073,138 bytes) matches its LFS pointer.

Downloads, on the two smallest K2 files (24.1 MB and 26.4 MB, from IFM/Math-Reasoning/math-thinking-oss and IFM/Code-Reasoning/code-no-thinking-hard), two at a time:

from exealm.data.stage import hf_items, stage
want = {"IFM/Math-Reasoning/math-thinking-oss": "00231.parquet",
        "IFM/Code-Reasoning/code-no-thinking-hard": "00234.parquet"}
items = [it for src, end in want.items() for it in hf_items(src) if it.path.endswith(end)]
print(stage(items, "data/stage-check", workers=2))   # {'got': 2, 'have': 0, 'failed': []} in about 2 s

Both matched their published sha256, and sha256sum agrees with data/stage-check/manifest.jsonl. I ran it on 07ba90e, once more there with manifest.jsonl moved away (it hashed both files, downloaded nothing, and wrote both manifest lines again), and again on ec5c983 and a7b92fd. To check resuming, I cut the 24.1 MB file back to its first 10,000,000 bytes, renamed it to <name>.part, and ran the same code again. It asked for the rest from byte 10,000,000, the CDN answered 206 (bytes 10000000-24075800/24075801), and the finished file matched.

python -m exealm.data.stage --out data/stage-check --to_jsonl data/stage-check/raw
python -m exealm.data.quality data/stage-check/raw/*.jsonl.gz --out data/stage-check/filtered

--to_jsonl wrote 12,786 documents from the math file and 4,425 from the code file (on a7b92fd too, where a second run converted nothing and both converted.jsonl lines matched the manifest), one per row, since every row in both files has text (pyarrow.parquet.read_table(path, columns=["text"])), and exealm.data.quality reads the output.

Notes

  • I couldn't check LLM-jp downloads live. On Nov 22 every file request to gitlab.llm-jp.nii.ac.jp returned a 500 after about 30 s: the raw URL download.py already uses, the API's raw endpoint, and the LFS object URL, with or without a range. The raw URL still did the same on Nov 23. The API itself answers, so planning works. Resuming against that server is covered by the tests only.
  • Several LLM-jp sources are one big file (en/en_wiki 7.46 GB, en/en_dolma/books 7.79 GB, en/en_finemath/4plus 12.51 GB), so a cap can't take part of them.
  • I didn't touch README.ja.md.

python -m exealm.data.stage downloads the files of an LLM-jp path or a
Hugging Face dataset, several at once. Both hosts keep the files in Git
LFS, which publishes each file's size and sha256. A file is written to
<name>.part and only gets its final name once both match, so a rerun
skips finished files and continues partial ones from the byte they
stopped at. A file that fails the check is deleted, so the next run
fetches it fresh. manifest.jsonl lists every finished file.

LLM-jp sizes and hashes come from GitLab's GraphQL API, 50 files per
request. Hugging Face files are pinned to the dataset's commit when the
plan is made. --plan_only prints each source's file count and size
without downloading anything, and staging stops before it starts if
the disk is too small.

The tests use a local server that can cut a connection, ignore byte
ranges or return a server error. Checked live on two K2 files (24 and
26 MB): both matched their sha256, and one cut back to 10 MB resumed
with a 206 from byte 10,000,000.
--max_gb and --max_tokens cap every source on its own, beside
--max_files. A capped source now keeps files spread evenly through its
sorted list instead of the first ones, so a sample covers the whole
source.

--max_tokens works for LLM-jp paths with a published token count. It
turns the count into bytes with that source's own bytes per token.
LLM-jp counted with its own tokenizer, so this is an estimate. Sources
without a count (K2 and the ja_cc levels) are refused with a pointer
to --max_gb. A file is never split, so when one file is already over
the cap the plan keeps it and prints a note: en/en_wiki is one 7.46 GB
file, so 0.5B of its 5B tokens still means the whole file.
en/en_dolma/pes2o is 30 files of 1.09 GB, then 9 of about 8.7 GB. With
evenly spaced picks, any count past 4 lands on a big file, so a 5B
token cap (8.35 GB) kept 3 files and 3.28 GB. The cap now tries files
in the order the first, the middle, the quarters and so on reach them,
and keeps each one that still fits. The sample stays spread through
the list, and pes2o now gets 7 files and 7.66 GB.
K2 stages as parquet, but quality.py and the rest of the data scripts
read jsonl.gz lines of {"text", "source"}. Running
python -m exealm.data.stage --out data/stage --to_jsonl data/raw
converts every staged parquet file in the manifest into that format.
K2 subsets go through their verified formatters and only the text
column is read; other datasets use download.extract_text. A folder in
a K2 repo with no verified formatter is refused, and finished files
are skipped on a rerun.

Checked on the two staged K2 files: 4,425 and 12,786 documents, the
same as the rows with text in each parquet file, and quality.py reads
the output.
GitLab allows 60 API calls a minute without an account, and planning
every source of mix v0 stopped partway with HTTP 429.
download.get_json now retries 429 and 5xx answers up to six tries. It
waits as long as Retry-After asks, or 5, 10, 20 s and so on (at most
120 s). The LLM-jp file listing in download.py and the GraphQL size
lookup in stage.py both use it.
docs/data.md gets a "Staging the full sources" section and a row in
the scripts table. The sizes in its examples come from --plan_only
runs against the hosts. The README to-do now says what is built and
what is left: running it on the real mix.
A rerun only compared sizes, so a file the host replaced with one of
the same size kept the old bytes, and the manifest kept the old
sha256. stage() now reads the sha256 on each path's last manifest
line. When the plan's sha256 differs, it checks the file, fetches it
again if it does not match, and appends the new line. --to_jsonl
reads the last line for each path.
The docs say a run can be stopped at any point, but the pool still ran
every queued file before Ctrl-C took effect. Now the main thread waits
in half-second steps, and Ctrl-C sets a stop flag. Running files stop
after the chunk they are writing and keep their .part files, and
queued files never start. Reads use read1, which returns what has
arrived, so a stop lands after one network read instead of a full
1 MB block. A test sends Ctrl-C to a run against a slow local server,
and the next run resumes both partial files.

On a real 26 MB K2 file, read1 and read each took 1.0 to 1.4 s over
two runs, so the smaller reads cost nothing noticeable.
A file already at its final name with the right size was counted as
finished and added to manifest.jsonl with the published sha256, even if
its bytes were never checked. That happens when the manifest is deleted
or --out points at files copied in by hand, and a same-size old version
would get listed as the new one. Now any file the manifest does not list
with the plan's sha256 is hashed first and fetched again if it differs.
A run killed while appending to manifest.jsonl left a partial last line,
and every later run and --to_jsonl stopped with a JSON error before doing
anything. Lines that do not parse are now skipped, so that file counts as
unlisted and gets hashed again. The cut line also gets its newline before
the next append, since otherwise the new line is glued onto it and lost.
A server that sends more bytes than the plan says (for example a file that
grew on the host after planning) had its whole body written to the .part
file before the size check failed it, past the disk-space check. Reading
now stops at the first chunk over the size, and the check fails it right
away.
With nothing to download no .part file was ever opened, so the rename to
the final name raised FileNotFoundError and the file was reported as
failed each time. An empty .part is now created first.
When the host had a new version of a file (a new sha256 in the plan),
fetch() deleted the old file before downloading. If that download then
failed, a good file was gone while its manifest line still listed it, and
--to_jsonl stopped on the missing file. The old version now stays under
its name until the new one is checked and replaces it. A damaged copy of
the current version (--rehash) is still deleted first, because its
manifest line would vouch for it on the next run. The disk check now
counts the new version's bytes, since both copies are on disk for a while.
--to_jsonl skipped any output that already existed, so a new version of a
parquet file staged later was never converted and the old documents
stayed in data/raw. It now redoes an output that is older than its
parquet file. A file the manifest lists but that is missing (a damaged
copy deleted after a failed re-fetch) is skipped with a note instead of
stopping the whole run.
docs/data.md said --max_tokens works for every LLM-jp path, but
ja/ja_cc/level0 to level2 have no published token count, so the command
stops on them. Mix v0 includes ja/ja_cc/level0 and K2, so --mix v0 with
--max_tokens always stops; the docs now say to use --max_gb or
--max_files there.
os.replace keeps the .part file's last-write time. When a run stopped
after writing all of a new version but before the rename, and --to_jsonl
ran in between, the next run gave the new version a time older than the
jsonl.gz made from the old one, so --to_jsonl kept the old documents.
The file's time is now set when it is renamed.
@Aadi-Tiwari
Aadi-Tiwari marked this pull request as ready for review November 23, 2026 22:42
Linux CI failed
test_a_new_version_finished_from_an_old_partial_file_is_converted_again.
There the old jsonl.gz and the new parquet file got the same file time,
so --to_jsonl kept the old documents. Linux takes file times from a
clock that ticks every few milliseconds, and FAT32 drives round them to
2 seconds. The new test sets both files to the same time and fails the
same way on Windows.

--to_jsonl now writes converted.jsonl in the staging folder, one line per
output with the sha256 of the parquet file it came from. An output is
skipped only when that matches the manifest, and a deleted output is
made again. The os.utime call from ec5c983 is no longer needed and is
removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant