[fix][sft] Make nested pretokenized shard order deterministic - #2204
Open
zhang-bofan wants to merge 2 commits into
Open
zhang-bofan wants to merge 2 commits into
zhang-bofan wants to merge 2 commits into
Conversation
Signed-off-by: zhang-bofan <125387643+zhang-bofan@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Code Review
This pull request ensures that data files are collected in a deterministic order by sorting the directory names in os.walk within _collect_data_files. It also adds a corresponding unit test, test_nested_shard_order_is_stable_on_resume, to verify that the shard order remains stable and consistent across different filesystem directory orderings when resuming. There are no review comments, so I have no feedback to provide.
Contributor
|
Signed-off-by: zhang-bofan <125387643+zhang-bofan@users.noreply.github.com>
This branch has not been deployed
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.
What does this PR do?
Give recursively discovered pretokenized SFT shards a deterministic order, so resuming over the same shard tree preserves the dataset index-to-sample mapping.
Why it happens
_collect_data_filessorts filenames but leaves subdirectory traversal in filesystem order. The loader concatenates shards in that order, whileStatefulSequentialSamplercheckpoints only a position. Different directory enumeration can therefore repeat or omit samples after resume.In a local reproduction, identical JSONL shard trees on
/tmpand/dev/shmloaded in ordersb,c,aandc,b,a. After consumingb, uninterrupted iteration returnedc,a, but restoring the dataloader checkpoint against the second tree returnedb,a.Solution
Sort the visible
dirnamesin place beforeos.walkrecurses. This retains the existing hidden-directory filtering and per-directory filename sorting.Test plan and validation
The regression test varies directory enumeration at two nesting levels and checks real
StatefulDataLoadersave/restore withStatefulSequentialSampler, parametrized over Parquet, JSONL, and Arrow.test_checkpoint_loading.py,test_sft_pretokenized.py, andtest_sft_dataloader.pypassed.a,b,con both filesystems and resumes withb,con both.pre-commit run --all-files --config .pre-commit-config.yaml: Ruff, Black, and Gitleaks passed; the patch was staged for the Gitleaks check.Local validation used Python 3.12.14, PyTorch 2.9.0, datasets 5.0.1, and torchdata 0.11.0 in a separate locked environment. The test command was the following, with checkout and validation-environment paths abbreviated:
--noconftestskips the session fixture that starts and stops Ray; these tests exercise the loader and sampler locally. Local runs did not exercise the repository's full dependency combination or GPU/E2E training.CPU test fixture
The checkpoint-loading test constructs
SkyRLTrainBackendwithobject.__new__, so it must initialize_inference_engines_initialized=Falseas the real constructor does. #2065 added the call that reads this flag during checkpoint loading, leaving the test setup incomplete. This PR includes the one-line fixture repair; the optimizer/scheduler assertions remain intact.The initial PR run and the same main baseline failed the same two checkpoint cases. Locally, the checkpoint module changed from 2 failed / 1 passed to 3 passed; the combined relevant suite above passes all 105 tests.
GitHub validation for
9845a83: the training CPU job now passes all three stages: 1705 passed / 16 skipped without vLLM, 206 passed / 1 skipped with vLLM, and 72 passed / 22 skipped for Tinker.The general CPU job also passed (25 and 208 tests), including the engine benchmark step. The remaining failing checks are Anyscale authentication before GPU tests and Vercel deployment authorization.
Compatibility
Nested shard directories gain a deterministic traversal order. This can differ from the order used by earlier versions, so the change does not preserve an old unsorted order when resuming a checkpoint created before the fix.
AI assistance: Codex prepared this change and ran the local reproducer and checks.
Note
Medium Risk
Changes dataset sample order for nested shard layouts versus older runs, so checkpoints from before the fix may not align; resume correctness for new runs is the intended win.
Overview
Fixes non-deterministic nested pretokenized shard ordering by sorting subdirectory names during
os.walkin_collect_data_files, alongside the existing per-directory filename sort. That keeps the concatenated dataset order stable across filesystems so SFT resume withStatefulDataLoader/StatefulSequentialSamplerdoes not repeat or skip samples when the shard tree is reloaded.Adds
test_nested_shard_order_is_stable_on_resume(Parquet, JSONL, Arrow) that flips simulated directory enumeration order and asserts checkpoint restore continues the same sample sequence. Unrelated test tweak: sets_inference_engines_initialized = Falseon a mockedSkyRLTrainBackendin checkpoint-loading tests.Reviewed by Cursor Bugbot for commit 9845a83. Bugbot is set up for automated code reviews on this repo. Configure here.