Skip to content

cuda: sample.cu's keyed draws as a shared module (sample.zig); Nemotron draws through it - #654

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jschmied:cuda-sample
Oct 12, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jschmied:cuda-sample

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

The first of the item-4 pieces claimed in #548: sample.cu's keyed draws as a shared module, so a second CUDA family (Kolibri 1, #481) draws by the same rule as Nemotron and the Metal engine without importing Nemotron. It's a move like #558 and #602: same kernels, same launches.

  • cuda/sample.zig:
    • margin, max_candidates and count.
    • Rule and rule(seed, temperature, top_p, min_log, top_k). It takes plain values, since the cuda module doesn't depend on lanes.
    • Draws: resolve from the loaded sample module, draw (with or without a column id map) and logprobRows, each the launch Nemotron's Ops made.
  • families/nemotron/cuda_sampler.zig keeps Sampling, check and the Triton-keyed greedy_draft. rule(s) maps a request onto cuda.sample.rule, and its test is unchanged.
  • families/nemotron/cuda_kernels.zig: Kernels holds one Draws instead of three functions, and Ops.draw and Ops.logprobRows delegate, so no caller changes.

Receipts on GB10 (DGX Spark, sm_121, CUDA 13.0). main (a6aa478) and this branch were each built with nvcc for sm_121:

  • zig build test passes; zig fmt --check and lean_check are clean for the touched files.
  • tf-cuda-test sample: 2,880 draws equal the Metal rule's host references.
  • Nemotron 3.5 Lightning MLX-4bit @ d9d758fb:
    • tensorfold check-kernels: 1,008 of 1,008 projection cases and 384 of 384 forked-MoE cases, both builds.
    • tensorfold run: three prompts, greedy and sampled (0.8 / 20 / 0.95, seed 7; the sampled ones draw through the moved code). All six reply hashes equal main's: 5923b40d570b, d38725819372, ead827be94f5, bb8855c2f084, 068276d3ec75, a6961a8c7589.

@jschmied

Copy link
Copy Markdown
Contributor Author

One more piece I'd like to add here before review, if you agree. The sampler's rule (1/T, top_p, the near window of 20, ln(min_p), top_k, the seed) is currently built in three places:

  • cuda/sample.zig's rule(), from this PR;
  • a copy in tests/cuda/sample_tests.zig;
  • Metal's Forward.sample in families/nemotron/forward.zig, which packs the same values as four floats plus two seed halves.

Proposal: rule(sampling) lives once in core/lanes/gpu_rule.zig, next to the host reference it already matches. Then:

  • the CUDA Rule struct and the Metal cfg are both filled from it;
  • the test drops its copy;
  • a host test pins the layout of both.

The kernels and their arguments stay unchanged, and the receipts would be the same as this PR's: sample against the host references, plus Nemotron reply hashes on both backends (a Mac is available here for the Metal side).

If you'd rather keep this PR CUDA-only, I'll send it as a follow-up after this one lands.

@ashhart
ashhart merged commit 9bcaa68 into ashhart:main Oct 12, 2026
2 checks passed
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. This landed in 1.0.8 as your own commit (ef1211b27), rebased for 1.0.8, which already had #647's prompt_logprobs. That gave tf_logprob_rows a fifth argument, rank, so Ops.logprobRows was the one conflict. In the rebase, Draws.logprobRows takes rank: bool and passes it after k, as the launch did before, and its doc comment names it. Every other line is yours. 9bcaa6840 merges this PR's head (11bbd77be) without changing the tree, so GitHub shows it as merged.

We built both trees with their own kernel sets and compared the tree without your PR (base) against it (tip), with Nemotron 3.5 Lightning MLX 4-bit, on a GB10 (sm_121, CUDA 13.0) and an RTX PRO 6000 (sm_120, CUDA 13.2):

check GB10 RTX PRO 6000
kernel sets, base against tip 30 of 30 fatbins byte-equal 28 of 30 byte-equal; qmm_group and qmmf have identical SASS and PTX and differ only outside them, as two nvcc 13.2 builds of one tree do
tf-cuda-test sample 2,880 draws equal the host references, both builds the same
tensorfold check-kernels lane_gemv 1008/1008, forked MoE 384/384, both builds the same
seeded sampled replies: 4 seeds x 5 settings (top_p 0.8 to 1.0, top_k 0, 5, 20, 50, min_p 0.05, temperature 0.6 to 1.2) x 2 prompts, drafted and "draft": false, plus greedy every token sha equal to base; drafted == plain 42/42 the same
logprob rows (greedy and seeded sampled) and prompt_logprobs rows (300 and 2,100 prompt tokens) byte-equal to base byte-equal to base
every reply in our gate, base against tip 217/217 equal: release prompts drafted and plain, 4 at once, 1 to 8 streams, structured output, resumed turns, the sampled grid, logprobs 133/133 equal: release prompts, 4 at once, the sampled grid, logprobs
decode, A B B A (prose and code, greedy and sampled) tip/base 0.998 to 1.006 0.999 on every cell with both builds on one draft-cost table

Each build times its draft costs the first time it starts. On the RTX PRO 6000 the tip's own timing drafted greedy code in 52 rounds where base took 49, for the same tokens, which read as 5% slower on that one cell; with base's timing it took 49 rounds and matched base's speed.

The four seeds gave four different replies in every setting, so the draws were live, and each build's own replies matched "draft": false token for token.

Yes to core/lanes/gpu_rule.zig, as a follow-up PR on top of this one. One rule for the Metal cfg, the CUDA Rule and the test, with a host test pinning both layouts, is the right next step, and reply hashes on both backends are the receipts we'd look for.

Keeping it a pure move, same kernels and same launches, made this quick to check.

BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…draws module) into the 1.0.8 candidate
@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
ashhart#654 landed above as e45d24291 (the author's commit, rebased onto this candidate). This keeps the PR's own head commit in the history, with no change to the tree.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…draws module) into the 1.0.8 candidate
@jschmied
jschmied deleted the cuda-sample branch October 23, 2026 21:02
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.

2 participants