Fix VRAM leak in overlap scheduling with structured output (#20640)#20697
Conversation
…ct#20640) Release delay_sample_func closure and vocab_mask/next_token_logits GPU tensors after sampling completes. These references were kept alive by result_queue and batch_record_buf until the next iteration, causing steady VRAM growth when grammar-based decoding is used with overlap scheduling (extra_buffer) enabled. - Set sampling_info.vocab_mask = None after applying it to logits - Set batch_result.delay_sample_func = None after execution - Set logits_output.next_token_logits = None after sampling
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical VRAM leak that manifested under specific conditions involving overlap scheduling and structured output. By strategically clearing references to large GPU tensors as soon as they are no longer needed, the change ensures efficient memory management and prevents out-of-memory errors, thereby enhancing the stability and performance of the system in these advanced usage scenarios. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a VRAM leak occurring with overlap scheduling and structured output. The fix correctly releases references to large GPU tensors as soon as they are no longer needed. The changes in scheduler.py and model_runner.py are clear, well-commented, and directly target the source of the leak. The implementation appears correct and should resolve the memory issue without side effects.
|
I tested it on the Qwen3.5-122B-A10B model and it works great. |
|
Hi, @hnyls2002 , this is my first PR in sglang. It has been waiting for review for a while, so I was wondering whether I may have missed any steps or done anything incorrectly. If there's anything I should fix, please let me know. If you have time, could you help review it? Thanks! |
33c04f6 to
68b3a0d
Compare
hnyls2002
left a comment
There was a problem hiding this comment.
This change seems to be reasonable. But why does the VRAM grow steadily instead of releasing just one iteration late?
In |
That closure won't hold the reference forever; the tensor will just delay releasing for one or two rounds. |
…d release on finish Address review feedback from DarkSharpness: - Simplify filter_batch grammar filtering logic (3 cases: empty, slice, clear) - Remove grammars=None from copy_for_forward (breaks grammar in overlap mode) - Remove vocab_mask cleanup from filter_batch (handled by sgl-project#20697 in _preprocess_logits) - Fix comment on req.grammar=None (grammar is CPU-only, not GPU) - Keep req.grammar=None for releasing CPU resources on request finish Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Motivation
Fix VRAM leak when
extra_buffer(overlap scheduling) is enabled together with structured JSON output (grammar-based decoding). Fixes #20640When overlap scheduling is active and grammar is used,
tp_worker.pywraps sampling into adelay_sample_funcclosure. This closure capturesforward_batch(holdingsampling_info.vocab_mask) andlogits_output(holdingnext_token_logits). Sincebatch_resultis kept alive byresult_queueandbatch_record_bufuntil the next scheduler iteration, these large GPU tensors are never freed in time. Each iteration allocates a newvocab_mask, so VRAM grows steadily until OOM.The leak only triggers when both conditions are met:
extra_bufferenabled → overlap scheduling →delay_sample_funcclosure createdvocab_maskallocated every iterationModifications
Release GPU tensor references as soon as they are no longer needed:
model_runner.py: Setsampling_info.vocab_mask = Noneimmediately afterapply_logits_bias()— the mask has already been applied to logits and is not needed downstream.scheduler.py: Setbatch_result.delay_sample_func = Noneafter the closure executes — breaks the closure's reference toforward_batchandlogits_output.scheduler.py: Setlogits_output.next_token_logits = Noneafter sampling — the raw logits are not used bycopy_to_cpu()(which only copiesnext_token_idsand logprob fields).Accuracy Tests
Benchmarking and Profiling
Verification
Tested with a reproduction script. After the fix,
torch.cuda.memory_allocated()remains stable across iterations, confirming the leak is resolved.Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci