Repository navigation
[5/6][kimi-deterministic] Compute decode logprobs via log_softmax under deterministic inference - #30821
[5/6][kimi-deterministic] Compute decode logprobs via log_softmax under deterministic inference#30821zyzshishui wants to merge 11 commits into
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
b0de7d3 to
7d87bb2
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
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 ( | |||
There was a problem hiding this comment.
[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.
670e96d to
d9f9a16
Compare
ba2f971 to
dc39d99
Compare
dc39d99 to
68f1722
Compare
2b8a0f7 to
3025ae8
Compare
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.
3025ae8 to
7ab0ae3
Compare
Motivation
At
temperature > 0withreturn_logprob, decode logprobs come fromtorch.log(torch.softmax(logits / T))while prefill rescoring usesF.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 viaF.log_softmaxon 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_softmaxonly under deterministic inference withreturn_logprobattemperature > 0.Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ❌ Run #29979964802
Latest PR Test (Extra): ❌ Run #29979964709