Skip to content

qwen draw: a sampled row's top-k skips 32-logit blocks that cannot beat the k-th candidate - #2507

Merged
ashhart merged 8 commits into
ashhart:mainfrom
mrv777:qwen-topk-blocked-scan
Nov 10, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
mrv777:qwen-topk-blocked-scan

Conversation

@mrv777

@mrv777 mrv777 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • Issue: n/a. This is a host-side speedup with the same candidates, the same keyed sampler and the same tokens; no family, backend or arithmetic change.
  • Stacked on: main (9450a3624); commits ebbd52ca5 (the change) and 36edee4eb (the changelog line's PR number).
  • atc overlap --base upstream/main --json (2026-11-08): the only shared file is CHANGELOG.md (79 open PRs, 6 textual conflicts, all in the Unreleased list). atc who lists no open PR or worktree on draw_topk.zig or qwen35_cache_host.zig.

Checks on this head

  • Head commit: 47375c912, a merge of main (latest 857b11bb8, changelog conflicts only; test-qwen35-metal-host 13/13 on it), over earlier merges and d49835bc7, the changelog line moved back under Unreleased after main's 1.2.5, over 6a467a696, a merge of main (latest 8be9c408c, changelog conflict only; test-qwen35-metal-host 13/13 on it) over 8167e6189, the changelog line reworded for review, over earlier merges of main (changelog conflicts only) over 36edee4eb, whose code is ebbd52ca5 (rebased from ccef4d4da on 985692e50; the code diff is identical, only the changelog moved).
  • Lean checker, 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.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-qwen35-metal-host: 13/13 pass on this head. It now includes the three draw_topk.zig tests: the existing key-order test, the new equality test and the new non-finite test.
    • zig build test test-golden -Dcpu=apple_m1 on ccef4d4da (same code diff): exit 0, 4m22s, Metal device checks included. Golden: 0 differ in every group, known differences as recorded; skips print their reason (no TF_TOKENIZER_DIRS, no TF_CLEF_DIR).
    • Plain zig build -Dcpu=apple_m1 on ccef4d4da: exit 0.
    • zig fmt --check build.zig zig, git diff --check and tools/pr_check.py --base upstream/main --pr 2507: 0 errors, 0 warnings.
  • New tests:
    • The equality test compares the blocked selection with the scalar walk it replaces over 400 random rows: ties, +0/−0, eight-adjacent-value rows (k-th key and block maxima one apart), grammar masks, lengths that aren't a multiple of 32, and NaN/−inf placed anywhere.
    • The second test plants −inf in a block the selection skips.
    • They can't fail before the change, because select didn't exist. Two planted controls show they catch a wrong selection, and each fails as planted:
      • skipping blocks one key too eagerly (<= key +| 1): the equality test fails;
      • checking finiteness after the skip instead of before: both tests fail.
  • CI: pending on this head.
  • Not run, and why: CUDA has no device here. The Qwen 3.5/3.6 CUDA engine (cuda_native.zig) calls the same draw for 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

  • Main against branch: below, same machine, checkpoint and settings. Baseline was main at 985692e50, candidate ccef4d4da; runs alternated base, candidate, base, candidate.
  • Boxes it reaches: Apple M5 Ultra (Metal) measured; CUDA hosts reach the same function for first tokens and grammar-masked windows, unmeasured.
  • New switch? None.
  • Environment:
    • Software: TensorFold 985692e50 vs ccef4d4da, Zig 0.17.0, Xcode 27.2 (27B52) with Metal 32023.922.
    • Machine: macOS 27.2 on an Apple M5 Ultra with 96 GB.
    • Checkpoint: a local Qwen3.8-27B MLX 4-bit (affine, group 64), the same files for both binaries.
    • Drafter: z-lab/Qwen3.8-27B-DFlash2 at 50307d4c4cde6860d4eee73e2547cd786fe8e8a4.
    • Serve flags: serve MODEL --drafter z-lab/Qwen3.8-27B-DFlash2 --parallel 2 --context 32768 --prompt-cache-gib 0, one model process on the machine.
  • Paths and platforms the change reaches:
    • Ran: the Metal Qwen3.8-27B verify (native/qwen27_lanes.zig draw, sampled or masked windows), on the M5 Ultra.
    • Reaches but not run: the Metal Qwen 2B backend and the CUDA Qwen 3.5/3.6 engine.
    • Unaffected: greedy rows (GPU argmax).
  • Exactness: 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, has alone and serial all equal, 0 unequal and 0 failed. All 324 token and output hashes in the two result files are identical between main and branch. The bench_openai.py runs below also have identical token_sha and output_sha256 across all four runs.
  • Main against this branch, same machine and checkpoint: below.
  • Decode speed before and after, from tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0 (median tok/s, two alternated runs each):
prompt temp main branch round gap p50 main → branch
fibonacci-raw 0 228.4 / 228.4 228.0 / 228.7 26.6 → 26.7 / 26.6 ms
gpu-chat-no-think 0 161.3 / 161.1 161.1 / 161.3 26.8–26.9 ms both
fibonacci-raw 1.0 198.5 / 198.2 209.6 / 210.2 29.3 → 27.7 / 27.6 ms
gpu-chat-no-think 1.0 122.5 / 122.1 129.2 / 129.5 29.4–29.5 → 27.9 / 27.8 ms

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%.

  • Prompt speed before and after: n/a. Prompt passes don't call the host top-k except for the first token's draw, one row per request.
  • Licence of any new weights or drafter: n/a.

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": false and 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 --serial check covers their tokens.)

mrv777 added a commit to mrv777/TensorFold that referenced this pull request Nov 8, 2026
@ashhart

ashhart commented Nov 8, 2026

Copy link
Copy Markdown
Owner

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. draw.zig's full-vocabulary tests, together with your 324 identical hashes, make this easy to accept.

Two small points:

  1. On the CUDA Qwen 3.5/3.6 engine, the host draw runs for the first token (cuda_native.zig:444) and for grammar-masked windows (:521). Unmasked sampled verify rows draw on the GPU (cuda_engine.zig:520). Could the CHANGELOG line (CHANGELOG.md:9) say that, and end with "Thanks to @mrv777."? If you can run a masked sampled request on a CUDA box, main-against-branch token hashes would be welcome. Otherwise "not measured" is fine.
  2. The description's head line names 45cb2bca1. Please update it to the current head.

@ashhart

ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@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.

ashhart and others added 8 commits November 10, 2026 14:30
…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.
@ashhart
ashhart force-pushed the qwen-topk-blocked-scan branch from 47375c9 to 72c834b Compare November 10, 2026 14:30
@ashhart
ashhart merged commit 8a7838a into ashhart:main Nov 10, 2026
3 of 10 checks passed
ashhart pushed a commit that referenced this pull request Nov 10, 2026
@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

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.

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @mrv777! This is in TensorFold 1.2.7, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.7

  • Synthena (Ash's Agent)

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