Skip to content

Optimize uniform greedy Torch sampling - #7844

Merged
janEbert merged 2 commits into
NVIDIA:mainfrom
Waterpine:perf/torch-greedy-sampling
Oct 29, 2026
Merged

janEbert merged 2 commits into
NVIDIA:mainfrom
Waterpine:perf/torch-greedy-sampling

Conversation

@Waterpine

@Waterpine Waterpine commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

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_dev CI 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.

Decode mode main This PR Speedup
S=1 3.717 ms 3.585 ms 1.044×
S=4 6.925 ms 6.058 ms 1.143×

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_kernel is 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, because torch_sampling.py is 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

  • I have added relevant unit tests
  • I have added relevant functional tests
  • If this PR adds or changes a GPU kernel (Triton, 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 in tests/unit_tests/determinism/kernels/manifest.py (guide)
  • 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"

  1. When your PR is ready, click Ready for Review.
  2. 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.

⚠️ 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.

@Waterpine
Waterpine requested review from a team as code owners October 3, 2026 02:44
@copy-pr-bot

copy-pr-bot Bot commented Oct 3, 2026

Copy link
Copy Markdown

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.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft October 3, 2026 02:44
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

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:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@Waterpine
Waterpine marked this pull request as ready for review October 20, 2026 00:38
@svcnvidia-nemo-ci
svcnvidia-nemo-ci requested a review from a team October 20, 2026 00:38
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>
@Waterpine
Waterpine force-pushed the perf/torch-greedy-sampling branch from 8d85921 to 948ea17 Compare October 20, 2026 02:14
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Oct 20, 2026
@Waterpine

Copy link
Copy Markdown
Contributor Author

@santhnm2 Rebased onto main (e592c04) and resolved the conflict with #7828 (request-local seeds). Since torch_sampling.py is now a registered determinism kernel source, I added a bit-exact replay test for the greedy shortcut. Re-measured on GB300 in the mcore_ci_dev image: the full decode step is 1.044× faster at S=1 and 1.143× at S=4, with identical generated tokens (details in the description)

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Oct 20, 2026
@kvareddy
kvareddy requested a review from tdene October 21, 2026 17:45
@kvareddy

Copy link
Copy Markdown
Contributor

@tdene can you please review this MR?

@tdene tdene left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-customer Waiting on the original author to respond label Oct 22, 2026
@cspades

cspades commented Oct 27, 2026

Copy link
Copy Markdown
Member

/ok to test 948ea17

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Approved All necessary approvals have been made label Oct 27, 2026
@janEbert
janEbert enabled auto-merge October 28, 2026 09:14
@janEbert
janEbert added this pull request to the merge queue Oct 28, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-customer Waiting on the original author to respond label Oct 29, 2026
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40818044318

@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40819569961

@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40826842145

Merged via the queue into NVIDIA:main with commit 928fe9d Oct 29, 2026
107 checks passed

This branch was previously deployed

2 inactive deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants