Skip to content

[Engine] qwen4: opt-in STRATA_QWEN4_DRAFT_HEAD2=1: the MTP block's guesses from a 2-bit copy of the whole output matrix - #467

Open
drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/qwen4-draft-head2
Open

drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/qwen4-draft-head2

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

Qwen4's MTP draft block picks its guess from the output matrix's product over the first 131,072 rows (guess_vocab()), half of the 248,320: the whole matrix (Q6_K, 417 MB) took twice as long. A token past row 131,072 can never be guessed, and many languages live there. With Qwen4's tokenizer, of the tokens of a short paragraph: Arabic 83%, Russian 66%, Korean 57%, Japanese 44%, French 19%, German 18%, Dutch 13%, Chinese 7%; English (the README) 0.6%. On an RTX 4060 Laptop a Russian answer had 46% of its guesses right where an English one has about 71%, and --draft-vocab 0 (all rows) made the Russian answer 15% faster at twice the head's cost.

Now the guess comes from a 2-bit copy of the whole matrix (152 MiB): every row is scored from the copy, the rows near the best are scored again exactly from the Q6_K rows, and the best of those is the guess. The copy is scored in 681 µs where the 131,072 Q6_K rows took 860 µs. Measured on the laptop below (500 tokens, interleaved runs):

Answer in Guesses from the first 131,072 rows From the 2-bit copy, all rows Change
Russian (4 runs) 69.29 ± 2.05 tokens/s 84.78 ± 1.27 +22%
Japanese (4 runs) 76.25 ± 2.16 81.83 ± 3.32 +7%
Chinese (4 runs) 81.28 ± 2.45 84.27 ± 0.50 +4%
English (6 runs) 86.01 ± 2.55 85.54 ± 2.62 within the spread

Again after a reboot, 3 interleaved runs each with STRATA_QWEN4_DRAFT_HEAD2=0 and =1 on the same build: French 83.11 ± 1.70 against 89.12 ± 2.22 (+7%), Dutch 77.05 ± 1.63 against 82.55 ± 0.79 (+7%), Russian 70.94 ± 1.21 against 84.29 ± 0.67 (+19%), English 86.66 ± 3.87 against 86.20 ± 2.69 (the same).

It is opt-in because English gains nothing for the 152 MiB of VRAM (93 cached experts on this card): it is for answers in languages whose tokens lie past row 131,072.

Related discussion

none (no issue). The screening idea is the one Inkling 2's DSpark/DFlash2 heads use (dc5468a08), written for Qwen4's 2,048-wide Q6_K matrix instead of generalizing those kernels, which are fixed to 2,304 and to Q8_0/Q4_K/Q5_K.

Approach

  • Engine::pick_guess(Likeliest*) (virtual, false): called by Engine::draft() after draft_step(); when it returns true the architecture has written the guess and its probability, and likeliest() over logits_ is skipped.
  • Qwen4::load_drafter(): with STRATA_QWEN4_DRAFT_HEAD2=1, where the output matrix is Q6_K with rows of 2,048 and the MTP block drafts (opt_.draft, window not 1, no --draft-model), packs the copy (q4_head2_pack(), 640 bytes a row: a 2-bit level a value, (l - 1.5) * s, an FP16 scale a 32 values chosen by least squared error and stretched to keep the row's length) and logs it. --draft-vocab N still limits the rows.
  • Qwen4::draft_step() skips the head's product when the copy is there; Qwen4::pick_guess() runs q4_head2_pick() on the head's input xn_:
    • q4_head2_score_kernel: a warp a row, each lane's 64 input values in registers, four rows' loads at once; each block of 256 rows keeps its 4 best and its softmax partial (max, sum of exp).
    • q4_head2_final_kernel (one block): the candidates within 5 of the best copy score (at most 64, the window halved until they fit), scored again from the Q6_K rows with the FP32 input; the best (the lower id of a tie) is the guess, its probability the softmax's with the other rows at their copy scores.
  • poly_qwen4_test: test_head2_pick against a host FP64 product of a random 5,000 x 2,048 Q6_K matrix (not a whole number of blocks): the pick is the exact best in 20 of 20 trials; the probability within 0.05 where the input leans to one row, as a model's output does, and within 0.25 for a random input (uniform random rows are a 2-bit copy's worst case).
  • Settings register (opt-in) and a sentence in docs/models/qwen4.md.
  • Metal: the kernels' declarations in qwen4_kernels.hpp and the pick_guess() override are behind #if !defined(STRATA_GPU_METAL), so Metal keeps the engine's default pick_guess() (false) and links nothing new; load_drafter()'s copy was already behind it.
  • HIP: the copy's FP16 scales are read and written as unsigned short through __ushort_as_half() / __half_as_ushort() (no __half pointer casts), as the rest of qwen4.cu does; the warp shuffles use width 32 like its warp_sum().
  • Kept out: DFlash2 (its own head), a Metal kernel, the verify step's head (it must be exact), a copy for other output types.

Validation

Revision 089d5adcf (one commit: the earlier 26d30e2e1 and 951c4917c squashed, rebased onto main 7cef9524a, plus the Metal guard and the HIP half conversions). The speed measurements are from 26d30e2e1 (on by default then). RTX 4060 Laptop GPU 8 GB (sm_89), i9-12900H, 30 GB RAM, Linux, CUDA 13.4; Qwen4-35B-A3B UD-IQ4_XS (pinned 742bc191), --kv q8_0 --max-context 8192, default MTP. The table's runs were on a build with my other Qwen4 changes (PR #440's reader and my other Qwen4 PRs: the PLE read-ahead, the PLE read depth, the row-table kernel, --stage-next and the VRAM reserve); this branch alone builds and runs (Russian 78.6 against 66.0 tokens/s in one run each, the mapped PLE table making it noisier).

Passed:

  • poly_qwen4_test (built with -DSTRATA_BUILD_TESTS=ON): all checks, PASS head2_pick (20 of 20 the exact best; ...).
  • Against the exact head over all rows in the engine (a debug build that computed both): the same pick in 198 of 200 English and 246 of 250 Russian guesses, probabilities 0.009 apart on average; the old head over 131,072 rows agreed with the full one in 198 of 200 English and only 160 of 250 Russian guesses.
  • Nsight Systems: q4_head2_score_kernel 681 µs, q4_head2_final_kernel 21 µs a guess; the old head (native_q6_k_mmvq_kernel over 131,072 rows) 860 µs. Nsight Compute: the score kernel at 82.8% of the card's DRAM throughput, 119 registers, 33% occupancy.
  • On 26d30e2e1: the copy was made by default and not with --no-draft or --window 1.
  • python3 -m unittest tools.test_settings_register, python3 tools/check_links.py: OK.
  • Builds of 089d5adcf: CUDA 13.4 (strata-poly, poly_qwen4_test, sm_89); HIP with ROCm 7.2.4 (rocm/dev-ubuntu-24.04:7.2.4-complete, the CI's image), preset hip-compile (gfx1100, gfx1201), target strata-poly; HIP on Windows 11, ROCm 7.2 SDK (pip _rocm_sdk_devel), gfx1010 (RX 5700 XT), target strata-poly, with poly_qwen4_test.
  • poly_qwen4_test: all checks on the RTX 4060 (test_head2_pick: 19 of 20 trials the exact best, every leaning one exact). On the RX 5700 XT test_head2_pick passes the same (19 of 20). There the run first stops at main's own q4_moe_combine_gr_write bitwise check (one value 1.19e-07 off on gfx1010, code this PR does not touch), so I ran it with test_head2_pick called first in a local copy. The test now has its own random generator: on the rebased main the shared one gave it other inputs, and a random-input trial, the copy's worst case, picked the second-best row. That trial failed, though the test allows 2 such misses at its end; random trials now count against those 2, and leaning ones must still be exact.
  • Default path: main and this branch without the setting, frozen experts (STRATA_FREEZE_EXPERTS=1 --expert-swaps 0), the same 300 tokens (57be2e24).
  • With STRATA_QWEN4_DRAFT_HEAD2=1 (the copy made and used) the greedy tokens turn at token 10. main turns there too with other verify windows: at token 11 with --window 2, at 10 with --no-draft (--draft-vocab 65536 and 0 keep it). Other guesses give other verify windows, whose sums differ in the last bits, so a near tie can go the other way. An earlier version of this text said a guess never changes the answer; that was wrong.

Failed:

  • Eight rows' loads at once (instead of four) spilled registers: 876 µs. Not in this commit.

Not run:

  • Metal build (no Mac here). I checked by reading the code that no call into the new kernels is left outside the guards.
  • Running on an AMD card (HIP was compiled, not run).
  • Arabic and Korean answers (the token counts above suggest gains like Russian's), long answers, sampling (temperature) rather than greedy.
  • DFlash2, --batch serving.
  • Reproduction by anyone else; the whole test suite.

Limits

  • 152 MiB of VRAM when on: on this card 93 fewer cached experts (2,506 instead of 2,599). English decode is unchanged within the runs' spread.
  • The probability given to the guess policy is approximate for the rows outside the exact ones; on the model's own matrix it was 0.009 off on average.
  • One card, one model file; greedy answers.

🤖 Generated with Claude Code

https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW

@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks @drakarah — opt-in STRATA_QWEN4_DRAFT_HEAD2=1 is a neat idea! To pass all backends cleanly before merging:

  1. Guard the CUDA/HIP-only path cleanly against Metal (#if !defined(STRATA_GPU_METAL)).
  2. Use HIP-compatible half conversion (__ushort_as_half / existing compat helper) so ROCm/HIP builds cleanly alongside CUDA.
  3. Rebase onto current main (which now includes your #462, #463, #468, #470, #486, #487, #488!).

…esses from a 2-bit copy of the whole output matrix

The draft block's head was the output matrix's product over its first 131,072 rows (guess_vocab()), so a token
past them could not be guessed: about half the tokens of Russian text and 40-80% of Korean, Arabic or Japanese
are, and there a window's guesses were right much less often.  The whole matrix (248,320 rows, Q6_K) took twice as
long as those rows.

With STRATA_QWEN4_DRAFT_HEAD2=1, where the output matrix is Q6_K with rows of 2,048 (CUDA, HIP), load_drafter()
packs a 2-bit copy of it (a level (l - 1.5) * s a value, an FP16 scale s a 32 values, 640 bytes a row, 152 MiB), and
the guess is picked from it (Engine::pick_guess(), a new hook after draft_step(); draft_step() then skips the head's
product): q4_head2_score_kernel scores every row from the copy (a warp a row, the input in registers, four rows'
loads at once) and keeps each block's 4 best and its softmax partials; q4_head2_final_kernel scores the rows within 5
of the best copy score (at most 64) again from the Q6_K rows, and the best of them is the guess, its probability the
softmax's with the other rows at their copy scores.  Only the guesses change; a wrong guess costs a window place.
Other guesses make other verify windows, and their sums can differ in the last bits, so a greedy answer can turn
where two tokens are close, as it does with --window: with frozen experts main's 300 tokens turn at token 11 with
--window 2 and at 10 with --no-draft, and with this setting at 10.  Off by default: the copy costs 152 MiB of VRAM, which English text gets nothing for.

Metal: the kernels' declarations and the pick_guess() override are behind !STRATA_GPU_METAL (the engine's default
pick_guess() stays).  HIP: the copy's FP16 scales are read and written as unsigned short through __ushort_as_half()
and __half_as_ushort().  poly_qwen4_test: test_head2_pick against a host FP64 product, with its own random generator.

RTX 4060 Laptop 8 GB, UD-IQ4_XS, --kv q8_0, 500 tokens, interleaved runs (on by default then): Russian 84.78 +- 1.27
tokens/s against 69.29 +- 2.05 (4 runs, +22%), Japanese 81.83 +- 3.32 against 76.25 +- 2.16 (+7%), Chinese 84.27 +-
0.50 against 81.28 +- 2.45 (+4%), English 85.54 +- 2.62 against 86.01 +- 2.55 (6 runs, the same within the spread).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
@drakarah
drakarah force-pushed the feat/qwen4-draft-head2 branch from 951c491 to 089d5ad Compare December 6, 2026 09:29
@drakarah

drakarah commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! All three done, one commit 089d5adcf on current main:

  1. Metal: the kernels' declarations in qwen4_kernels.hpp and the pick_guess() override are behind #if !defined(STRATA_GPU_METAL); Metal keeps the engine's default pick_guess().
  2. HIP: the copy's FP16 scales are read and written as unsigned short through __ushort_as_half() / __half_as_ushort(), with no __half pointer casts.
  3. Rebased.

Builds: CUDA 13.4; HIP with the CI's image (rocm/dev-ubuntu-24.04:7.2.4-complete, hip-compile, gfx1100/gfx1201); HIP on Windows (ROCm 7.2, gfx1010), where test_head2_pick passes on an RX 5700 XT. Two corrections in the description: the test now has its own random generator (on the rebased main a random-input trial picked the second-best row, which its own 2-miss allowance permits), and a guess can change a greedy answer at a near tie through the verify windows' shapes, as --window 2 does on main. The default path gives main's tokens.

Could you add run-backends so the backend jobs (including Metal) run? I can't set labels here.

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