Skip to content

Host draw: greedy and masked rows scan 64-logit blocks and skip mask words that allow nothing - #2846

Merged
ashhart merged 6 commits into
mainfrom
lead/host-simd-draw
Nov 21, 2026
Merged

ashhart merged 6 commits into
mainfrom
lead/host-simd-draw

Conversation

@ashhart

@ashhart ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Rows drawn on the host scanned the whole vocabulary one logit at a time: greedy structured-output rows on CUDA Qwen 3.5, masked rows on Qwen3.8-27B Metal, every greedy row on the small Metal models, and (through draw_topk.select) structured sampled rows with top_k 1-128. Mask words that allow nothing were still walked logit by logit.

What changes:

  • core/lanes/sampling_row.zig: the host row draw scans 64-logit blocks and skips a block whose mask word allows nothing.
  • qwen3_5/draw_topk.zig: the bounded top-k path gets the same skip and ranks only allowed logits. (Structured sampled rows with top_k up to 128 go through draw_topk.select, not sampling_row, so both paths needed it; Gemma 4 and Step-5 share them too.)
  • A CPU timing step, test-host-draw-timing, outside test and every CI shard.

Exactness: every logit is still checked for NaN and infinity before any block is skipped, as on main; ties go to the lowest id with +0 and -0 equal; mask words past the end of the mask allow nothing and no mask allows everything; sampled draws see the same candidates in the same order. Main's draw is kept as a test-only reference.

Checks: seven tests against that reference cover random rows at 248,320 and odd sizes, ties, ±0, subnormals, every NaN/infinity encoding inside fully masked blocks, and all-masked, one-bit, random and partial-last-word masks; fifteen planted bugs each turned a test red. Eight host steps including test-cuda-host (399/399 steps), compile-cuda-tests -Dtarget=aarch64-linux-gnu, zig fmt --check and the lean count pass.

CPU timing (M3 Ultra, ReleaseFast, one thread, µs per 248,320-logit row, three alternating rounds; token checksums equal in every case):

Case main this branch
greedy, no mask 169-183 14.5-15.8
greedy, half the ids allowed 829-861 22.6-24.2
greedy, 16 ids allowed 134-142 5.4-6.8
top_k 20, 16 ids allowed 142-149 10.1-11.2
top_k 0, 16 ids allowed 265-275 9.6-10.1
top_k 20, no mask 30.5-32.7 26.5-28.6

Still to run before landing: served A/Bs with token hashes equal to main: the Metal 2B (greedy and sampled at 1 and 4 streams), Qwen3.8-27B with structured output on Metal and a GB10 (1 and 4 streams, one cell mixing a masked stream with unmasked ones), and the 35B-A3B structured on a GB10.

For #2832.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner Author

Both test-wiring points from the independent read are settled in 784e3abe88. The 'bounded BF16 keys' filter in qwen35_cache_host.zig had gone dead (its test moved to core with the keys), so that step now runs 'bounded keyed draws' instead, which is the test that catches a ±0 ranking bug; the step runs 16 tests and goes red with that bug planted. The new sampling_row_test.zig tests already run on Linux: zig build test includes test-cuda-host, which runs the whole lanes module, and planting a tie bug there fails both new tests by name.

  • Synthena (Ash's Agent)

…hat allow nothing (#2832)

sampling_row.scan replaces the scalar first pass of the greedy and masked draw: every
block's exponents are checked finite before any mask test, a block whose two mask words
are zero is not ranked, and an allowed key replaces the best only when strictly larger,
taking the block's first lane at that key, so ties keep the lowest id and both zeros
rank equal. The sampled pass walks set mask bits in id order, so the same candidates
reach the fp64 sampler in the same order. The bf16 order keys move here from
qwen3_5/draw_topk.zig. Main's draw stays as the test oracle in sampling_row_test.zig.
…g in, and ranks only allowed logits (#2832)

After the unchanged finite check, a 32-logit block whose mask word is zero is skipped,
the full-selection skip ranks only the allowed lanes, and consider runs for set bits in
id order. Each skipped call would have returned at its mask check, so the selection is
unchanged. Structured sampled rows at top_k 1..128 take this path, including rows where
fewer than top_k ids are allowed. key and keys now come from core's sampling_row (an
order-equivalent encoding), and their order test moved there with them.
… row on the CPU (#2832)

Greedy, masked and sampled rows through qwen3_5/draw.zig at ReleaseFast, with a checksum
of every drawn token so two builds' tokens can be compared. Not part of test or any CI
shard; it runs only when asked for.
…rd core's bf16 keys (#2832)

The "bounded BF16 keys" filter matched nothing once that test moved to core's
sampling_row with the keys. The bounded keyed draws compare the top-k path against the
value-sorted reference, so the step again fails when the keys order -0 and +0 apart.
@ashhart
ashhart force-pushed the lead/host-simd-draw branch from bcb93d3 to f1e12cf Compare November 21, 2026 23:53
@ashhart
ashhart merged commit 39c7715 into main Nov 21, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/host-simd-draw branch November 21, 2026 23:54
ashhart added a commit to BadAd84/TensorFold that referenced this pull request Nov 21, 2026
ashhart added a commit that referenced this pull request Nov 21, 2026
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.

1 participant