[perf, training_utils] fix: Run gc regularly in trainer/sft_trainer.py to avoid spikes in step time.#5549
Open
sheilaliuxl wants to merge 2 commits intoverl-project:mainfrom
Open
[perf, training_utils] fix: Run gc regularly in trainer/sft_trainer.py to avoid spikes in step time.#5549sheilaliuxl wants to merge 2 commits intoverl-project:mainfrom
sheilaliuxl wants to merge 2 commits intoverl-project:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to manually control garbage collection during training to stabilize step times. A gc_freq configuration is added to disable automatic GC and periodically run gc.collect(). While the core idea is sound and the performance benefits are clear from the description, the implementation has a critical flaw: it doesn't guarantee that the garbage collector will be re-enabled if the training loop exits unexpectedly or completes normally without hitting the final step condition. This can lead to memory leaks. I've added comments with suggestions to use a try...finally block to make the GC management robust.
…n stats for training data.
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?
Run gc regularly in trainer/sft_trainer.py to avoid spikes in step time.
Checklist Before Starting
[{modules}] {type}: {description}(This will be checked by the CI){modules}includefsdp,megatron,veomni,sglang,vllm,rollout,trainer,ci,training_utils,recipe,hardware,deployment,ray,worker,single_controller,misc,perf,model,algo,env,tool,ckpt,doc,data,cfg,reward,fully_async,one_step_off,like[megatron, fsdp, doc]{type}is infeat,fix,refactor,chore,test[BREAKING]to the beginning of the title.[BREAKING][fsdp, megatron] feat: dynamic batchingTest
I: Step Time
It decreases training step time consistently with
gc_freq=1(Dropping first10steps and last3steps):ip-10-4-145-27, to reduce inconsistency across different nodesMediantime:0.8652 ==> 0.7247 (-16.2%)Avgtime:0.8828 ==> 0.7324 (-17.0%)Stdtime also decreases0.0696 ==> 0.0362 (-48.0%)or improvement of1.92xII: MFU
MFUincreases slightly on theworkeras well, fromgc_freq = -1 to 1:avgMFU increases0.2221 ==> 0.2297 (+3.4%):2026-03-13 17:34:52,918 [tb.py:264] INFO - [243/256] : (avg (std), med) = ( 0.2297 [+ 0.0076] ( 0.0020), 0.2301) # train/mfustdMFU decreases00093 ==> 0.0020 (-78.5%)or improvement of4.7x1.
Step Time2. All Metrics
data/train_seq_len*metrics are exactly the sametrain/time(s)decrease andtrain/mfuincreasesAPI and Usage Example
Add
trainer.gc_freq=1to training config:Design & Code Changes
N.A.
Checklist Before Submitting
Important
Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.
pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=alwaysci-requestchannel in theverlSlack workspace. (If not accessible, please try the Feishu group (飞书群).)recipesubmodule, please also update the reference to the submodule commit viagit submodule update --remoteorcd recipe && git pull origin main.