Repository navigation
Optimize uniform greedy Torch sampling - #7844
Conversation
|
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:
See the contribution guide for more details. |
Signed-off-by: Waterpine <biansonghz@gmail.com>
torch_sampling.py is registered under the request-seed sampling kernel since NVIDIA#7828, so changes to it must update that kernel's determinism test. Replay an all-greedy batch with request seeds under side-stream contention, and check that it returns per-row argmax without advancing the shared RNG. Also update the sample_kernel docstring, which still described no_top_k/no_top_p as ignored. Signed-off-by: Waterpine <biansonghz@gmail.com>
8d85921 to
948ea17
Compare
|
@santhnm2 Rebased onto main (e592c04) and resolved the conflict with #7828 (request-local seeds). Since |
|
@tdene can you please review this MR? |
|
/ok to test 948ea17 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40818044318 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40819569961 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40826842145 |
What does this PR do?
Add a fast path for uniform greedy sampling in TorchSampling, avoiding request bucketing and redundant indexing when all active requests use top_k=1 and top_p=0.0.
This is a follow-up to #7754. The sampling optimization was originally included in that GDP inference PR and was separated at @santhnm2's request so it can be reviewed independently.
The fast path uses per-row argmax, preserves optional gather indices and caller-owned output buffers, and supports speculative token-to-request mappings. Other sampling policies continue through the existing bucketed path.
This PR includes the sampling implementation and its regression test. The GDP kernel and CUDA graph changes remain outside this PR.
Performance
Re-measured after rebasing onto main e592c04 (includes #7828) on one GB300 in the
mcore_ci_devCI image: seeded 48-layer GDP hybrid (800M recipe shape), batch 32, BF16, local CUDA graphs, torch sampling backend, greedy requests. S=4 adds three MTP depths (num_speculative_tokens=3). Synchronized wall time per full decode step; two jobs × two ABBA cycles, 8 processes per arm; speedup is the median of paired ratios.The sampling call alone (32 requests, 131k vocab) is 2.2× faster for the base decode sample, 5.1× for the 128-row speculative verify, and 3.2× per MTP draft sample. Generated tokens matched exactly across all processes and both arms.
Validation
Rebased onto main; the conflict with #7828 in
sample_kernelis resolved by running the greedy shortcut before the request-seed setup. Greedy rows ignore seeds, and the engine already rejects seeded speculative requests. A determinism replay test for the shortcut is added, becausetorch_sampling.pyis now a registered determinism kernel source.On four GB300 GPUs in the CI image, both arms pass the inference sampling, request-seed, text-generation-controller, MTP CUDA-graph, GDP inference, dynamic-engine and determinism-kernel tests, with identical skips.
Issue tracking
For PRs from open-source community contributors:
Linked issue: None; this is a minor performance improvement.
Related PR: #7754 — sampling optimization extracted at the reviewer's request.
Contribution process
Pre-checks
jit_fuser/torch.compile, CUDA extension, TE or external-library dispatch, or a scatter/index accumulation), I have added or updated its bit-exact determinism test and registered it intests/unit_tests/determinism/kernels/manifest.py(guide)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"
.github/CODEOWNERS.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, theFinal Reviewlabel 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
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.