Repository navigation
[DFlash2] Keep the greedy selector walk in range for all-NaN score rows #41737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kpham-sgl
merged 8 commits into
sgl-project:main
from
modal-projects:rohan/up/dflash2-all-nan-selector-row
Oct 5, 2026
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
15e7bf6
dflash2: greedy selector walk picks the first unbeaten candidate so a…
rchalamala adc74c0
Merge branch 'main' into rohan/up/dflash2-all-nan-selector-row
rodamani 03d0470
test: run DFlash2 selector-walk NaN-row test on CUDA instead of the T…
rodamani 1ddc24e
Merge branch 'rohan/up/dflash2-all-nan-selector-row' of https://git-m…
rodamani b8c2bb4
Merge branch 'main' into rohan/up/dflash2-all-nan-selector-row
rodamani 3da3eab
Merge branch 'main' into rohan/up/dflash2-all-nan-selector-row
rodamani c06d4b7
[DFlash2] Match torch.argmax NaN semantics in greedy selector walk
rodamani 9a4e290
Merge branch 'main' into rohan/up/dflash2-all-nan-selector-row
rodamani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
test/registered/kernels/ops/speculative/test_dflash_selector_walk_nan_row.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import unittest | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.kernels.ops.speculative.dflash import selector_walk_triton | ||
| from sglang.test.ci.ci_register import register_cuda_ci | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
| register_cuda_ci(est_time=10, stage="base-b-kernel-unit", runner_config="1-gpu-large") | ||
|
|
||
|
|
||
| def _walk(scores, greedy): | ||
| batch, slots, top_k, _ = scores.shape | ||
| device = scores.device | ||
| candidate_ids = torch.arange(batch * slots * top_k, device=device).view( | ||
| batch, slots, top_k | ||
| ) | ||
| tokens, q_rows = selector_walk_triton( | ||
| candidate_ids=candidate_ids, | ||
| scores=scores, | ||
| uniforms=torch.full((batch, slots), 0.5, device=device), | ||
| temperatures=torch.ones(batch, device=device), | ||
| greedy_mask=torch.full((batch,), greedy, dtype=torch.bool, device=device), | ||
| ) | ||
| return candidate_ids.cpu(), tokens.cpu(), q_rows.cpu() | ||
|
|
||
|
|
||
| @unittest.skipUnless(torch.cuda.is_available(), "CUDA is required") | ||
| class TestDFlashSelectorWalkNanRow(CustomTestCase): | ||
| def test_all_nan_greedy_row_stays_inside_its_candidate_row(self): | ||
| batch, slots, top_k = 2, 3, 4 | ||
| scores = torch.randn(batch, slots, top_k, top_k, device="cuda") | ||
| scores[0] = float("nan") | ||
| candidate_ids, tokens, q_rows = _walk(scores, greedy=True) | ||
|
|
||
| for slot in range(slots): | ||
| self.assertIn(int(tokens[0, slot]), candidate_ids[0, slot].tolist()) | ||
| self.assertEqual(float(q_rows[0, slot].sum()), 1.0) | ||
|
rodamani marked this conversation as resolved.
Resolved
|
||
| # Matches torch.argmax on the same rows (index 0 for all-NaN). | ||
| self.assertEqual(tokens[0].tolist(), candidate_ids[0, :, 0].tolist()) | ||
|
|
||
| def test_finite_greedy_row_matches_torch_argmax(self): | ||
| batch, slots, top_k = 3, 4, 8 | ||
| scores = torch.randn(batch, slots, top_k, top_k, device="cuda") | ||
| scores[1, 2, :, :] = 0.0 # ties break to the left | ||
| candidate_ids, tokens, q_rows = _walk(scores, greedy=True) | ||
| scores = scores.cpu() | ||
|
|
||
| previous = torch.zeros(batch, dtype=torch.int64) | ||
| for slot in range(slots): | ||
| rows = scores[torch.arange(batch), slot, previous] | ||
| expected = rows.argmax(dim=-1) | ||
| self.assertEqual( | ||
| tokens[:, slot].tolist(), | ||
| candidate_ids[torch.arange(batch), slot, expected].tolist(), | ||
| ) | ||
| torch.testing.assert_close( | ||
| q_rows[:, slot], torch.nn.functional.one_hot(expected, top_k).float() | ||
| ) | ||
| previous = expected | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.