Inference: Cudagraph-aware admission gating in prefill scheduler
What does this PR do ?
Presently, schedule_chunked_prefill and schedule_non_chunked_prefill admit requests based on the available token/request budget only without consideration for whether the resulting batch shape will match any captured CG. When a request pushes the batch into a shape the captured set doesn't cover, the engine silently falls back to eager mode. While Transformer models can absorb extra decodes into prefill slots, for hybrid models, the matcher requires captured_decode_req_count >= real_decode_req_count (strict mode), so we fallback to eager more often.
Now, when cuda_graph_all_prefills is on, _find_cg_chunk_size(max_chunk_tokens) traverses the list of cudagraphs to find the best-fit cudagraph for (active_token_count + chunk_size, num_prefill_requests + 1, num_decode_requests). schedule_chunked_prefill snaps prefill_chunk_length to the largest CG-aligned boundary in the token budget, and if no CG covers the resulting shape, the request is deferred.
This is designed without an eager fallback: if there is no cudagraph, the deferred request waits until the present decode finishes, when the next prefill will be automatically admitted. The worst case of this is max_sequence_length. This avoids the one-off version of forcing an eager step which then continues to trigger eager due to the imbalance, and ensures that we run every step cudagraphed.
#3509 needs to be merged first to avoid unnecessary starvation for hybrid inference. These two PRs together ensure that no steps will run eager.
Issue tracking
For PRs from open-source community contributors:
- New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
- Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.
Linked issue:
Contribution process
Pre-checks
- [ ] I have added relevant unit tests
- [ ] I have added relevant functional tests
- [ ] I have added proper typing to my code Typing guidelines
- [ ] I have added relevant documentation
- [ ] I have run the autoformatter.sh on my PR
Code review
Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
- When your PR is ready, click Ready for Review.
- An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
- Some PRs may jump straight to step 2. This is determined by
.github/CODEOWNERS.
- Some PRs may jump straight to step 2. This is determined by
:warning: Only mark as ready once merge-conflicts are resolved and the CI is passing. Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.
For PRs outside megatron/core, this step is skipped.
Step 3: Approved
Once all required reviewers have approved, the Approved label is applied automatically.
Merge
Any member of mcore-engineers will be able to merge your PR.
For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either [email protected] or [email protected].
This PR has been automatically converted to draft because all PRs must start as drafts.
When you are ready for review, click Ready for Review to begin the review process. This will:
- Add the oncall reviewer (optional reviewer)
- Add required review teams based on your changes
See the contribution guide for more details.
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.
Contributors can view more details about this message here.
The code changes LGTM but can we add some tests which exercise the code path that defers a request due to lack of a CUDA graph match? Basically we should confirm that even if a request is deferred: A) the other scheduled requests still proceed successfully B) the deferred request eventually gets scheduled C) graphs are active for all batches
This pull request requires additional validation before any workflows can run on NVIDIA's runners.
Pull request vetters can view their responsibilities here.
Contributors can view more details about this message here.
/ok to test 4060c40
/claude strict-review
/ok to test 06a9508
/ok to test d940d1a
/ok to test efa61b3
/ok to test 4cd2c33
🔄 Merge queue validation started!
You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/27453419722