perf(training): parallelize SFT dataset rendering with multiprocessing#358
Open
perf(training): parallelize SFT dataset rendering with multiprocessing#358
Conversation
The sft_loop rendering phase tokenizes each example sequentially on a single CPU core, taking ~6 hours for 110K multi-turn examples. Use multiprocessing.Pool with imap to distribute rendering across up to 8 worker processes. Each worker initializes its own tokenizer and renderer to avoid pickling issues. Results stream back to the main process via imap for progress tracking. Falls back to single-threaded rendering for small datasets or single-CPU environments. Made-with: Cursor
…tion After fork(), Python's cyclic GC walks the parent heap in each worker, triggering copy-on-write page faults that duplicate ~37 GiB per worker. With 8 workers this adds ~300 GiB of overhead, causing OOMKill at 14% rendering progress even with a 428 GiB memory limit. Disabling GC in workers keeps shared pages shared, reducing per-worker overhead from ~37 GiB to ~0.5 GiB. Workers are short-lived and don't create reference cycles, so GC is unnecessary. Made-with: Cursor
gc.disable() was insufficient — Python's reference counting still triggers COW page faults when workers access any inherited object. Switching to spawn eliminates COW entirely: each worker starts as a fresh process with no inherited heap, reducing per-worker overhead from ~37 GiB to ~0.8 GiB. Made-with: Cursor
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.
Description
Parallelize the SFT dataset rendering phase in
sft_loop.pyusingmultiprocessing.Pool. The rendering loop tokenizes each example sequentially on a single CPU core, which takes ~6 hours for 110K multi-turn examples. With 8 parallel workers, this drops to ~45 minutes (~6.4x speedup observed).How it works:
_init_render_worker(avoids pickling non-serializable objects)Pool.imapwithchunksize=100streams results back to the main process for progress trackingmin(os.cpu_count(), 8)to respect container CPU limitsMemory consideration: Parallel workers increase peak memory (~2x vs single-threaded) due to per-worker tokenizer copies and IPC deserialization overhead. The orchestrator memory allocation formula in the control plane should account for this (tracked separately).
Type of Change
Testing
Tested on a 12.2 GiB / 110K example multi-turn dataset (qwen3.5-397b):
Checklist
Made with Cursor