Repository navigation
[Engine] qwen4/metal: 1-slot Metal doorbell in draft_step and BasinKV fallback truncation - #512
Draft
NightRain233 wants to merge 2 commits into
Draft
NightRain233 wants to merge 2 commits into
NightRain233 wants to merge 2 commits into
Conversation
…dog-safe CB chunking
… fallback truncation
NightRain233
force-pushed
the
feat/qwen4-metal-draft-bell
branch
from
December 6, 2026 16:56
1be083d to
f5a04a5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem & Result
With Metal
MTLSharedEventdoorbell (#474) active for the main decode pass, Qwen4's speculativedraft_step()(blk.40MTP MoE layer) still fell back to synchronous CPU routing whenever experts were partially offloaded (!all_cached()), forcing a full GPU-to-CPU router stall on every draft token. In addition:Engine::draft()calledsync_st("draft")unconditionally on every chained stepi > 0even whencover()did not grow KV.--basinkv, [Engine] metal: port opt-in BasinKV sparse KV offload (--basinkv) #500), if a draft step rolled back / fell through to normal evaluation (on_eval_done()),blk.40's BasinKV slot could retain stale extra tokens beyondpos0 + n.Engine::metal_bell_serve_range()wroteuse_/recent_without guardingil >= 0and vector bounds when serving the draft layer (il == n_layer_).Measured result (16 GB Apple M4, Qwen4-35B-A3B
UD-IQ2_XS, 192 tok,--ple-direct, 4,000 fixed GPU experts,--window 2 --defer-rank 0):draft_stepMetal Doorbell:27.39 -> 29.17 tok/s(+6.5%), router wait reduced from18.9 -> 16.5 ms/tok, with identical greedy output.--basinkv --window 4 --defer-rank 0, 16K prompt / 128K cap): exact needle recall'AMBER-4921'with zero stale-row corruption on fallback.Approach (Commit 2:
f5a04a5)Qwen4::draft_step()(engine/src/poly/qwen4.cpp):!all_cached() && bell_on(1) && card_moe(1) && L.card != 0 && metal_bell_slots_ >= 1and all MoE quant types supportnative_mmvq_ids, encodes a 1-slot Metal doorbell pass (bell_begin_pass(st_, 1),metal_bell_ring_layer(..., defer_cur=false),moe_shared(),moe_card()), setsdraft_bell_pending_ = true, and falls back cleanly to synchronousmoe()ifmetal_bell_ring_layer()returnsfalse.Engine::draft()(engine/src/poly/engine.hpp,engine/src/poly/engine.cpp):sync_st("draft")wheni > 0 && kv_rows_ == kv_before.DraftBellOffguard so any exception duringdraft_step()/pick_guess()/likeliest()cleanly callsbell_unwind()and resets all doorbell state flags.bell_serve()immediately afterif (!pick_guess(h_guess_)) likeliest(logits_, draft_rows(), h_guess_, st_);(verified deadlock-free with [Engine] qwen4: opt-in STRATA_QWEN4_DRAFT_HEAD2=1: the MTP block's guesses from a 2-bit copy of the whole output matrix #467Qwen4::pick_guess(), which is non-Metal / async-only).metal_bell_serve_range()use_/recent_updates withil >= 0andidx < use_.size()/idx < recent_.size().blk.40Truncation inQwen4::on_eval_done()(engine/src/poly/qwen4.cpp):slots_[n_layer_].basin_layerback topos0 + nwhenseq_len(bl) > pos0 + n, matchingQwen4::on_after_keep().engine/metal/poly_tests/memory_growth.cpp):DraftBellEnginecovering 5 scenarios: normal 1-slot draft doorbell serving, fallback whenmetal_bell_ring_layer()declines, exception unwinding viaDraftBellOff,il == n_layer_bounds safety inmetal_bell_serve_range(), and chainedi > 0sync skipping.Validation
Passed (on this exact PR branch
f5a04a5, Apple M4 macOS)cmake --build engine/build-metal --target strata-poly poly_memory_growth poly_advanced_parity poly_qwen4_parity poly_quantized_parity -j8— PASS (0 errors)./engine/build-metal/poly_memory_growth— PASS (BellChunkEngine+ all 5DraftBellEnginescenarios:OK)./engine/build-metal/poly_advanced_parity— PASS (Metal doorbell: 0 errors,Metal doorbell defer: 0 errors, 0 mismatches)./engine/build-metal/poly_qwen4_parity— PASS (0 mismatches)./engine/build-metal/poly_quantized_parity— PASS (0 mismatches)git diff --check && git diff --cached --check— PASSpython3 tools/test_dropped_paths.py— PASSpython3 tools/settings_register.py --check— PASSApple M5 Ultra (64-core Apple10, exact rebased commit
f5a04a5on5a3b357/origin/main0794bc5): clean Metal Release build + fullctest -L poly --output-on-failure— 51/51 (100%) PASS (54.91s).Passed (on combined Round 4 branch
4707dd5, Apple M5 Ultra 64-core Apple10)ctest --test-dir engine/build-metal -L poly --output-on-failure— 53/53 PASSFailed
Not Run
#if defined(STRATA_GPU_METAL), except the 1-linekv_rows_ == kv_beforeguard inEngine::draft()and BasinKV truncation inQwen4::on_eval_done(); non-Metal build will be verified by GitHub Actions CI).Limits & Default-Path Preservation
all_cached()is true (full-VRAM machines),STRATA_METAL_BELL=0, orSTRATA_POLY_BELL=0,draft_bellis false and the existing path is untouched.