Repository navigation
[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
Conversation
|
Thanks @drakarah — opt-in
|
…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
951c491 to
089d5ad
Compare
|
Thanks! All three done, one commit
Builds: CUDA 13.4; HIP with the CI's image ( Could you add |
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):
Again after a reboot, 3 interleaved runs each with
STRATA_QWEN4_DRAFT_HEAD2=0and=1on 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 byEngine::draft()afterdraft_step(); when it returns true the architecture has written the guess and its probability, andlikeliest()overlogits_is skipped.Qwen4::load_drafter(): withSTRATA_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 Nstill limits the rows.Qwen4::draft_step()skips the head's product when the copy is there;Qwen4::pick_guess()runsq4_head2_pick()on the head's inputxn_: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_pickagainst 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).docs/models/qwen4.md.qwen4_kernels.hppand thepick_guess()override are behind#if !defined(STRATA_GPU_METAL), so Metal keeps the engine's defaultpick_guess()(false) and links nothing new;load_drafter()'s copy was already behind it.unsigned shortthrough__ushort_as_half()/__half_as_ushort()(no__halfpointer casts), as the rest ofqwen4.cudoes; the warp shuffles use width 32 like itswarp_sum().Validation
Revision
089d5adcf(one commit: the earlier26d30e2e1and951c4917csquashed, rebased ontomain7cef9524a, plus the Metal guard and the HIP half conversions). The speed measurements are from26d30e2e1(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 (pinned742bc191),--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; ...).q4_head2_score_kernel681 µs,q4_head2_final_kernel21 µs a guess; the old head (native_q6_k_mmvq_kernelover 131,072 rows) 860 µs. Nsight Compute: the score kernel at 82.8% of the card's DRAM throughput, 119 registers, 33% occupancy.26d30e2e1: the copy was made by default and not with--no-draftor--window 1.python3 -m unittest tools.test_settings_register,python3 tools/check_links.py: OK.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), presethip-compile(gfx1100, gfx1201), targetstrata-poly; HIP on Windows 11, ROCm 7.2 SDK (pip_rocm_sdk_devel), gfx1010 (RX 5700 XT), targetstrata-poly, withpoly_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 XTtest_head2_pickpasses the same (19 of 20). There the run first stops atmain's ownq4_moe_combine_gr_writebitwise check (one value 1.19e-07 off on gfx1010, code this PR does not touch), so I ran it withtest_head2_pickcalled first in a local copy. The test now has its own random generator: on the rebasedmainthe 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.mainand this branch without the setting, frozen experts (STRATA_FREEZE_EXPERTS=1 --expert-swaps 0), the same 300 tokens (57be2e24).STRATA_QWEN4_DRAFT_HEAD2=1(the copy made and used) the greedy tokens turn at token 10.mainturns there too with other verify windows: at token 11 with--window 2, at 10 with--no-draft(--draft-vocab 65536and0keep 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:
Not run:
--batchserving.Limits
🤖 Generated with Claude Code
https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW