Skip to content

fix(pytorch): make MTP rejection sampling reproducible per request - #5193

Open
caikun-pjlab wants to merge 2 commits into
InternLM:mainfrom
caikun-pjlab:fix/mtp-request-rng
Open

caikun-pjlab wants to merge 2 commits into
InternLM:mainfrom
caikun-pjlab:fix/mtp-request-rng

Conversation

@caikun-pjlab

@caikun-pjlab caikun-pjlab commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

MTP rejection sampling used process-global CUDA RNG for acceptance decisions and recovery sampling. Consequently, a request's seed did not fully control its sampling results, and unrelated requests could affect reproducibility.

Speculative decoding also advanced the sampling offset by one per round, even when a round emitted multiple tokens.

Modification

  • Pass request seeds and offsets through the rejection-sampling interface.
  • Generate acceptance and recovery randomness with Philox, keyed by request seed, token position, and separate RNG purposes.
  • Advance speculative sampling offsets by the number of emitted tokens.
  • Recover only the first rejected position and parallelize recovery across vocabulary blocks.
  • Clamp FP32 uniforms below 1 to prevent infinite exponential noise caused by rounding.
  • Update portable and Ascend adapters and document the reproducibility guarantees.

Tests

Deployment validation used GLM-5.3-Flash on 8 H200 GPUs with DP=8, EP=8, and 5 MTP draft tokens:

  • Identical token sequences across serial retries, retries after unrelated requests, and service restarts.
  • Matching streaming and non-streaming outputs.
  • Different seeds produced different outputs.
  • Full GSM8K evaluation: 97.12% accuracy (1281/1319), with no request errors.

Performance

All nine measured sampler configurations improved over the baseline.

Representative CUDA Graph microbenchmark: batch=32, draft tokens=5, vocabulary=131072.

Sampler latency Before After
P50 195.7 µs 119.6 µs
P99 197.0 µs 120.2 µs
Maximum 199.3 µs 121.1 µs

@RunningLeon RunningLeon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this fix. On H200 with Qwen3.5-4B and MTP3, the same-seed serial retry matched all 2,048 tokens after an unrelated request, unlike on main. I recommend addressing the two inline findings before merging: the CUDA bonus draw still depends on batching, and the new recovery kernel fails to compile for ROCm.

draft_token_ids,
bonus_token_ids,
is_greedy=is_greedy,
random_seeds=expanded_sampling_inputs.random_seeds[::num_tokens],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The rejection path now honors the RNG strides, but the bonus path still passes strided seed and offset views to the CUDA multinomial kernel, which indexes them as contiguous. On H200 with fixed logits and all drafts accepted, 7 of 8 bonus tokens differed between batched and singleton runs, and 8 of 8 differed when the batch was reversed. This also reproduces on main, but it leaves the bonus-token reproducibility guarantee incomplete. Please pass the strides to the multinomial kernel or make the bonus RNG views contiguous, and add a regression with an all-accepted draft and a stochastic bonus.

)
prob = tl.maximum(target_prob - draft_prob, 0.0)
uniform = _rejection_uniform(seed, position, vocab_offset, 2, tl.float32)
score = tl.where(vocab_mask, prob * (-1.0 / libdevice.log1p(-uniform)), float('-inf'))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ROCm also uses CudaOpsBackend. With Triton 3.6.0, this NVIDIA-specific log1p fails to compile for gfx942: Implicit conversion of CUDA __nv_log1pf device function has been dropped. The baseline compiles, and switching only to triton.language.extra.libdevice lets this kernel compile for both gfx942 and sm90. Please use the backend-neutral import and add a ROCm compile check.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants