Skip to content

[5/6][kimi-deterministic] Compute decode logprobs via log_softmax under deterministic inference - #30821

Closed
zyzshishui wants to merge 11 commits into
sgl-project:mainfrom
zyzshishui:kimi-deterministic/5-logsoftmax-decode-logprobs
Closed

zyzshishui wants to merge 11 commits into
sgl-project:mainfrom
zyzshishui:kimi-deterministic/5-logsoftmax-decode-logprobs

Conversation

@zyzshishui

@zyzshishui zyzshishui commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Stacked series (5/6): stacked on kimi-deterministic/4-dp-attention-sampling-positions (#30820), so the diff below includes 1-4/6. This PR's own changes are the last 2 commits (ending 7ab0ae3). Combined CI: #30822.

Motivation

At temperature > 0 with return_logprob, decode logprobs come from torch.log(torch.softmax(logits / T)) while prefill rescoring uses F.log_softmax; the kernels round differently (~1e-6), breaking bitwise prefill/decode alignment in deterministic mode. (Part 5/6 of the kimi-deterministic series.)

Modifications

Under deterministic inference with return_logprob, compute returned logprobs via F.log_softmax on the temperature-scaled logits; sampling unchanged (the greedy path already did this).

Accuracy Tests

Kimi-K2-Instruct-BF16, 16 GPUs (TP16 / DP8-attention / EP16, fa4 + deep_gemm + deepep normal, radix on), 128 prompts (1k-16k) x 512 tokens, abs tol 0.0: replay, shuffled concurrency-32, bs1 vs bs128, cached-prefix extend vs cold prefill, decode logprobs vs prefill rescoring (65,536 tokens) — all bitwise identical.

The prefill/decode alignment phase failed at temperature 1.0 before this change.

Speed Tests and Profiling

One extra log_softmax only under deterministic inference with return_logprob at temperature > 0.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #29979964802
Latest PR Test (Extra): ❌ Run #29979964709

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@zyzshishui
zyzshishui force-pushed the kimi-deterministic/5-logsoftmax-decode-logprobs branch from b0de7d3 to 7d87bb2 Compare July 10, 2026 22:21
@zyzshishui
zyzshishui marked this pull request as ready for review July 11, 2026 00:41
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@@ -176,6 +176,16 @@ def forward(
# Standard path: do softmax and sample from probs.
logits.div_(sampling_info.temperatures)

if (

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.

[nit] The new branch has no comment explaining why deterministic mode recomputes logprobs via log_softmax after temperature scaling. Nearby RL code documents its matching goal; without a similar note, a later cleanup could re-collapse this to torch.log(probs) and silently re-break prefill/decode bitwise alignment (~1e-6).

Suggestion: Add a one-line comment, e.g. that under deterministic inference returned logprobs must use F.log_softmax (same kernel as prefill rescoring) rather than log(softmax), because the two disagree at ~1e-6 even when mathematically equivalent.

@zyzshishui
zyzshishui force-pushed the kimi-deterministic/5-logsoftmax-decode-logprobs branch 3 times, most recently from ba2f971 to dc39d99 Compare July 19, 2026 20:55
@github-actions github-actions Bot added the dependencies Pull requests that update a dependency file label Jul 19, 2026
@zyzshishui
zyzshishui force-pushed the kimi-deterministic/5-logsoftmax-decode-logprobs branch from dc39d99 to 68f1722 Compare July 20, 2026 22:09
@zyzshishui
zyzshishui force-pushed the kimi-deterministic/5-logsoftmax-decode-logprobs branch 3 times, most recently from 2b8a0f7 to 3025ae8 Compare July 22, 2026 20:23
FA4 could not serve DeepSeek MLA decode: the attention handler hard-coded
MHA_CHUNKED_KV, the absorbed-MLA path asserted FA3-only, and the jit wrapper
dropped the qv argument. Route fa4 through the standard MLA dispatch, plumb
qv through the FA4 varlen/kvcache wrappers, and register fa4 in the
deterministic and radix-compatible backend allowlists. The DeepSeek
deterministic-mode check now uses RADIX_SUPPORTED_DETERMINISTIC_ATTENTION_BACKEND
instead of a stale hard-coded list, which also admits ascend (the error
message already claimed that list).
…hapes

Review follow-up: flash_attn.cute implements the qv argument only on
Blackwell (SM100), so routing fa4 through the MLA dispatch unconditionally
turned the previous MHA-chunked-KV fallback into a hard kernel assert on
other archs. Keep the chunked-KV path off-SM100, reject deterministic fa4
DeepSeek on non-SM100 at startup (deterministic mode requires MLA), and add
a focused FA4 qv test for the DeepSeek absorbed shape (varlen q over paged
KV, MQA/GQA, num_splits=1). Also note in the backend that extend and decode
share the FA4 MLA path.
Review follow-up: flash_attn.cute accepts qv on capability majors 10 and 11
(not 12), while the previous gate admitted only SM100 and the qv test's
file-level skip admitted every arch >= 10 including SM120. Introduce a
single is_sm100_or_sm110_supported predicate shared by the dispatch gate,
the startup validation, and the test skip, and assert the decode-side
flash_attn_with_kvcache wrapper reproduces the varlen path bit-for-bit.
fused_qkv_a_proj_with_mqa picked batch-size-dependent kernels
(dsv3_fused_a_gemm for 1-16 rows, cuBLAS otherwise), breaking batch
invariance. dsv3_fused_a_gemm is a custom kernel outside the deterministic
aten overrides, so gate it off in deterministic mode; the projection then
runs through the batch-invariant aten.mm override at every batch size.
BF16 expert weights could enter the DeepGEMM BF16 grouped-GEMM runner only
under DeepEP low_latency; deepep normal fell back to other kernels. Lift the
low-latency-only restriction and, under deterministic inference, zero-init
the pre-permute scatter/index buffers so uninitialized pad regions cannot
leak nondeterministic values.
Review follow-up: evaluate enable_deterministic_inference once per
pre-permute instead of three times, allocate with torch.zeros instead of
empty-then-zero_, and document what the zero-fill protects: input_tensor
pad slots feeding the grouped GEMM, and scales only for FP8 activations
that share this pre-permute (BF16 dispatch never writes or consumes them).
With DP attention, MLP sync pads positions/seq_lens to the synced batch
size while SamplingBatchInfo covers only the real requests, so seeded
sampling crashes with "Seed and positions must have the same shape".
Slice positions to the sampled row count; real rows precede the padding.
Review follow-up: the non-spec arm of post_forward_mlp_sync_batch restored
batch_size and trimmed logits but left positions/seq_lens/req_pool_indices
padded, so every post-forward consumer (seeded sampling, ngram token-table
updates, prefill-only dummy ids) had to defend against the padding on its
own. Record the real token count at padding time and slice the per-request
tensors back where batch_size is restored, mirroring the draft-decode
branch. This makes the sample()-side positions slice dead; drop it.
TboForwardBatchPreparer.filter_batch allowlist-validates every non-None
ForwardBatch field when building child batches, and the new MLP-sync unpad
field _original_num_tokens was never added to the child dict, so any
TBO + DP-attention server crashed on its first extend batch (caught by the
base-c-test-deepep-4-gpu-h100 suite). Null it alongside its siblings
_original_batch_size/_original_forward_mode — TBO children carry no
MLP-sync restore state; the parent performs the post-forward unpad. Add a
filter_batch regression test with the field set.
At temperature > 0 the standard sampling path derived returned logprobs as
log(softmax(x/T)) while prefill rescoring uses F.log_softmax; the two
kernels disagree by ~1e-6, breaking bitwise prefill/decode logprob
alignment. Under deterministic inference, compute the returned logprobs
with F.log_softmax after temperature scaling.
Review follow-up: without a note here, a later cleanup could re-collapse
this branch into torch.log(probs) and silently re-break the bitwise
prefill/decode logprob alignment the kernel choice exists for.
@zyzshishui
zyzshishui force-pushed the kimi-deterministic/5-logsoftmax-decode-logprobs branch from 3025ae8 to 7ab0ae3 Compare July 23, 2026 04:36
@zyzshishui zyzshishui closed this Jul 24, 2026
@zyzshishui
zyzshishui deleted the kimi-deterministic/5-logsoftmax-decode-logprobs branch August 3, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek dependencies Pull requests that update a dependency file jit-kernel quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants