Skip to content

cuda sampler: the fallback selects and sorts min(top_k, 1024) candidates (fixes #3712) - #4099

Merged
ashhart merged 4 commits into
mainfrom
lead/issue-3712-sampler-fallback-topk
Nov 20, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/issue-3712-sampler-fallback-topk

Conversation

@ashhart

@ashhart ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

What this changes

In the CUDA sampler's fallback (no window near the max holds the rule's tokens), first_block asked top_keys for 1,024 candidates and sorted all of them even when top_k is small. It now asks for min(top_k, C) and sizes the bitonic sort to that count (at least 32). top_k == 0 or top_k > C keeps the full block.

Tokens and probabilities stay the same: the top_k largest keys with the id tie cut are the same set whichever count top_keys is asked for, the sort order (value desc, id asc) is unchanged, pick() reads only min(n_cand, top_k) of them, and the nucleus path's cut is bounded by the same top-k mark either way.

Checks on this head

  • zig build compile-cuda-tests -Dtarget=aarch64-linux-gnu: passed (compiles the CUDA tests for Linux; no GPU run).
  • Device: tf-cuda-test sample on a GB10 (the flat, tie, peaked and spread rows at top_k 1, 20, 40 and more in zig/tests/cuda/sample_tests.zig) runs on the landing train's GB10 cell before this lands.

@ashhart
ashhart force-pushed the lead/issue-3712-sampler-fallback-topk branch from 133b93c to bd67abc Compare November 20, 2026 16:21
@ashhart
ashhart merged commit 9b9218f into main Nov 20, 2026
3 of 11 checks passed
@ashhart
ashhart deleted the lead/issue-3712-sampler-fallback-topk branch November 20, 2026 16:21
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.

CUDA sampler: direct tf_draw callers can rank and sort 1024 candidates for top_k=20

1 participant