Repository navigation
qwen draw: a sampled row's top-k skips 32-logit blocks that cannot beat the k-th candidate - #2507
Conversation
|
Thanks, @mrv777. The skip is safe by construction: blocks run in id order, so a block whose best key is at or below the k-th can only lose, ties included. Non-finite words are still refused before any skip. Two small points:
|
|
@mrv777, thanks, both asks are met: the CHANGELOG line states the CUDA scope with your thanks, and the body names the head. The block skip is exact on reading. A block is skipped only when it's full and its largest key can't beat the k-th, ties still go to the lower id, and non-finite words are checked before the skip. It still applies as is after the draw moved into core. Our own q27 cell is queued, and it lands after that. |
…at the k-th candidate The bounded top-k behind every sampled Qwen row (Metal 27B verify, and the Qwen 3.5/3.6 CUDA engine through the shared draw) walked all 248,320 logits one at a time. A 32-word block whose largest key is at or below the k-th selected key holds no candidate, so the scalar walk over it inserts nothing; it is now skipped with one vector compare. Every word is still checked for non-finite values, including in skipped blocks. The selection is equal to the scalar walk: a host test compares both over 400 random rows (ties, both zeros, masks, tails, NaN and -inf), and a second plants -inf in a skipped block. Planted controls (skip one key too eagerly; check finiteness after the skip) both fail.
47375c9 to
72c834b
Compare
|
Thank you @mrv777. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
|
Thanks @mrv777! This is in TensorFold 1.2.7, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.7
|
What this changes
A sampled Qwen row draws from a bounded top-k (
families/qwen3_5/draw_topk.zig), which walked all 248,320 bf16 logits one at a time on the host for every verify row, while the GPU waits. Now a 32-word block whose largest key is at or below the current k-th key is skipped with one vector compare: the scalar walk over such a block inserts nothing, so the selected candidates, their order and the drawn token are unchanged. Every word is still checked for a non-finite value, skipped blocks included, so a row with NaN or ±inf is refused exactly as before. Greedy rows don't reach this code.9450a3624); commitsebbd52ca5(the change) and36edee4eb(the changelog line's PR number).atc overlap --base upstream/main --json(2026-11-08): the only shared file isCHANGELOG.md(79 open PRs, 6 textual conflicts, all in the Unreleased list).atc wholists no open PR or worktree ondraw_topk.zigorqwen35_cache_host.zig.Checks on this head
47375c912, a merge of main (latest857b11bb8, changelog conflicts only;test-qwen35-metal-host13/13 on it), over earlier merges andd49835bc7, the changelog line moved back under Unreleased after main's 1.2.5, over6a467a696, a merge of main (latest8be9c408c, changelog conflict only;test-qwen35-metal-host13/13 on it) over8167e6189, the changelog line reworded for review, over earlier merges of main (changelog conflicts only) over36edee4eb, whose code isebbd52ca5(rebased fromccef4d4daon985692e50; the code diff is identical, only the changelog moved).python3 tools/zig/lean_check.py, on the base and on this head: 116 and 116, 0 new (lean_new.py). The 116 are inherited.zig build test-qwen35-metal-host: 13/13 pass on this head. It now includes the threedraw_topk.zigtests: the existing key-order test, the new equality test and the new non-finite test.zig build test test-golden -Dcpu=apple_m1onccef4d4da(same code diff): exit 0, 4m22s, Metal device checks included. Golden: 0 differ in every group, known differences as recorded; skips print their reason (noTF_TOKENIZER_DIRS, noTF_CLEF_DIR).zig build -Dcpu=apple_m1onccef4d4da: exit 0.zig fmt --check build.zig zig,git diff --checkandtools/pr_check.py --base upstream/main --pr 2507: 0 errors, 0 warnings.selectdidn't exist. Two planted controls show they catch a wrong selection, and each fails as planted:<= key +| 1): the equality test fails;cuda_native.zig) calls the samedrawfor each first token and for grammar-masked windows, so the same selection applies there, but it is unmeasured. Its unmasked sampled verify rows draw on the GPU (cuda_engine.zig) and don't reach this code. HIP has its own draw and is not touched.Receipt
985692e50, candidateccef4d4da; runs alternated base, candidate, base, candidate.985692e50vsccef4d4da, Zig 0.17.0, Xcode 27.2 (27B52) with Metal 32023.922.z-lab/Qwen3.8-27B-DFlash2at50307d4c4cde6860d4eee73e2547cd786fe8e8a4.serve MODEL --drafter z-lab/Qwen3.8-27B-DFlash2 --parallel 2 --context 32768 --prompt-cache-gib 0, one model process on the machine.native/qwen27_lanes.zigdraw, sampled or masked windows), on the M5 Ultra.tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0,1.0 --levels 1,2 --alone --serial, on each binary. Every cell, greedy and sampled, at 1 and 2 streams, hasaloneandserialall equal, 0 unequal and 0 failed. All 324 token and output hashes in the two result files are identical between main and branch. Thebench_openai.pyruns below also have identicaltoken_shaandoutput_sha256across all four runs.tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0(median tok/s, two alternated runs each):From
bench_concurrent.py, sampled (temperature 1.0) aggregate tok/s, main → branch: code 1 stream 178.0 → 188.4, 2 streams 290.9 → 306.2; chat 1 stream 125.6 → 132.9, 2 streams 191.5 → 201.6. Greedy cells are within ±0.5%.Checklist
The title and description match the head commit, including the receipt and the stack.
New tests fail before the change, pass after it, and skip cleanly without their dependency. (They exercise a function the change adds; the planted controls above show they fail on a wrong selection. They need no device.)
Comments and docstrings are one line. No measurements or history in the source.
No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
Drafted output equals
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Drafted against"draft": falseand concurrent against solo are measured above. Resumed against fresh isn't separately run; the change only selects candidates from a row's logits and touches no state.)No precision traded for speed. If the bits change, the description says which sums change and why. (No sums change: the same keys are compared and the same entries kept.)
Decode and prompt processing are no slower than main at one stream and the widest admitted; with
--drafter, no slower than without it, greedy and sampled. (One and two streams with--drafter, greedy and sampled, measured above; drafts-off sampled steps call the same draw for one row a step, so they can only get faster; the--serialcheck covers their tokens.)