Repository navigation
fix(kernel): avoid nan in triton sampling softmax when a chunk starts with -inf - #654
Merged
jason-fxz merged 1 commit intoOct 10, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #647.
_sm_partialstarts every column chunk withm = -inf. When the firstBLOCK_SIZEblock of a chunk is all -inf,new_mis still -inf, soexp(x - new_m)andexp(m - new_m)areexp(-inf - -inf)= NaN._sm_finalizeadds that partial to the row sum and the whole row comes out NaN.This shifts by 0 while the running max is still -inf, so an all -inf prefix adds 0. Once the max is finite the shift is
new_mas before, so a row without -inf goes through the same arithmetic._sm_finalizeneeds no change: an all -inf chunk now storespl = 0, and0 * exp(-inf - gm)is 0 as long as the row has a finite logit.flashinfer's
softmaxalready returns finite probabilities for these inputs, so only the Triton path is affected.sample_impluses it when flashinfer is not installed, which is always the case on ROCm. Nothing on main writes -inf into logits yet, so today this is latent; an allowed-token-ids, min_tokens or grammar mask would hit it.The test goes into
tests/kernels/test_triton_fallbacks.py: 3 rows of 151936 logits, two of them masked (the first 16384 columns, or everything except 3 token ids) and one unmasked, with per-row temperatures, againsttorch.softmax.Tested on NVIDIA L4 (sm_89) on Modal, CUDA 13.0, driver 610.57.04 for the main test run and 580.95.05 for this PR's, torch 2.11.0+cu130, Triton 3.6.0, flashinfer 0.6.18.post1, on top of 2d8b9cc. No checkpoint is involved.
pytest tests/kernels/test_triton_fallbacks.pytorch.softmax3.2e-6 over probs > 1e-6 (flashinfer on the same inputs: 4.2e-6)sample_implwith flashinfer turned off, 3 allowed ids, 16 rows x 64 drawsRows without -inf, same process, main's kernels against this PR's, both pinned to the same config:
do_bench_cudagraph, median of 7 interleaved runs with the config main's autotune picks:Not tested: ROCm and sm_120.
A/B script, as run (from the repo root on this branch, with main's
python/freetoken/kernel/triton/sampling.pycopied tocontrol/sampling_main.py)