Repository navigation
Stage the training data: parallel, resumable downloads checked against each file's sha256 - #73
Open
Aadi-Tiwari wants to merge 17 commits into
Open
Aadi-Tiwari wants to merge 17 commits into
Aadi-Tiwari wants to merge 17 commits into
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thenairrbranch, 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>.partand 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.jsonllists 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..partfiles, and queued files never start. Python's thread pool would otherwise run every queued file before the stop took effect.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.--plan_onlyprints 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_gband--max_tokenscap each source on its own.--max_tokensworks for the LLM-jp paths with a published count insources.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, andja/ja_cc/level0tolevel2) refuses a token cap and points to--max_gb. Mix v0 has both, so--mix v0takes--max_gbor--max_files.--to_jsonl DIRturns the staged parquet files into the jsonl.gz lines of{"text", "source"}thatdownload.pywrites andquality.pyreads. K2 subsets go through the verified formatters informatters.py, and only the text column is read.converted.jsonlin 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_jsonretries 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 indownload.pyuses it too.docs/data.mdhas 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.pyhas 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:--rehashfinds 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--to_jsonldrops 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 formatterThe 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:
The size of
ja/ja_wiki/0004.jsonl.gzfrom 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-ossandIFM/Code-Reasoning/code-no-thinking-hard), two at a time:Both matched their published sha256, and
sha256sumagrees withdata/stage-check/manifest.jsonl. I ran it on07ba90e, once more there withmanifest.jsonlmoved away (it hashed both files, downloaded nothing, and wrote both manifest lines again), and again onec5c983anda7b92fd. 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_jsonlwrote 12,786 documents from the math file and 4,425 from the code file (ona7b92fdtoo, where a second run converted nothing and bothconverted.jsonllines matched the manifest), one per row, since every row in both files has text (pyarrow.parquet.read_table(path, columns=["text"])), andexealm.data.qualityreads the output.Notes
download.pyalready 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.README.ja.md.