Skip to content

[Engine] qwen4/metal: 1-slot Metal doorbell in draft_step and BasinKV fallback truncation - #512

Draft
NightRain233 wants to merge 2 commits into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-draft-bell
Draft

NightRain233 wants to merge 2 commits into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-draft-bell

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Draft — Depends on #474.
Commit 1 (5a3b357) rebases #474 cleanly onto main (0794bc5); Commit 2 (f5a04a5) is this PR's actual diff (4 files: engine/src/poly/engine.hpp, engine/src/poly/engine.cpp, engine/src/poly/qwen4.cpp, engine/metal/poly_tests/memory_growth.cpp). Once #474 merges, this PR will be rebased down to Commit 2 and marked ready for review.

Problem & Result

With Metal MTLSharedEvent doorbell (#474) active for the main decode pass, Qwen4's speculative draft_step() (blk.40 MTP 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:

  1. Engine::draft() called sync_st("draft") unconditionally on every chained step i > 0 even when cover() did not grow KV.
  2. Under chained multi-step drafting with BasinKV (--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 beyond pos0 + n.
  3. Engine::metal_bell_serve_range() wrote use_ / recent_ without guarding il >= 0 and 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):

  • Isolated draft_step Metal Doorbell: 27.39 -> 29.17 tok/s (+6.5%), router wait reduced from 18.9 -> 16.5 ms/tok, with identical greedy output.
  • Verified multi-step BasinKV NIAH (--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)

  1. 1-Slot Metal Doorbell in Qwen4::draft_step() (engine/src/poly/qwen4.cpp):
    • When !all_cached() && bell_on(1) && card_moe(1) && L.card != 0 && metal_bell_slots_ >= 1 and all MoE quant types support native_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()), sets draft_bell_pending_ = true, and falls back cleanly to synchronous moe() if metal_bell_ring_layer() returns false.
  2. Submit & Unwind Safety in Engine::draft() (engine/src/poly/engine.hpp, engine/src/poly/engine.cpp):
    • Skips redundant sync_st("draft") when i > 0 && kv_rows_ == kv_before.
    • Adds RAII DraftBellOff guard so any exception during draft_step() / pick_guess() / likeliest() cleanly calls bell_unwind() and resets all doorbell state flags.
    • Submits the single Metal command buffer and calls bell_serve() immediately after if (!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 #467 Qwen4::pick_guess(), which is non-Metal / async-only).
    • Guards metal_bell_serve_range() use_/recent_ updates with il >= 0 and idx < use_.size() / idx < recent_.size().
  3. BasinKV blk.40 Truncation in Qwen4::on_eval_done() (engine/src/poly/qwen4.cpp):
    • Truncates slots_[n_layer_].basin_layer back to pos0 + n when seq_len(bl) > pos0 + n, matching Qwen4::on_after_keep().
  4. Unit Tests (engine/metal/poly_tests/memory_growth.cpp):
    • Added DraftBellEngine covering 5 scenarios: normal 1-slot draft doorbell serving, fallback when metal_bell_ring_layer() declines, exception unwinding via DraftBellOff, il == n_layer_ bounds safety in metal_bell_serve_range(), and chained i > 0 sync 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 5 DraftBellEngine scenarios: 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 — PASS

  • python3 tools/test_dropped_paths.py — PASS

  • python3 tools/settings_register.py --check — PASS

  • Apple M5 Ultra (64-core Apple10, exact rebased commit f5a04a5 on 5a3b357 / origin/main 0794bc5): clean Metal Release build + full ctest -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 PASS

Failed

  • None.

Not Run

  • CUDA / HIP / SYCL builds (all draft-doorbell code is under #if defined(STRATA_GPU_METAL), except the 1-line kv_rows_ == kv_before guard in Engine::draft() and BasinKV truncation in Qwen4::on_eval_done(); non-Metal build will be verified by GitHub Actions CI).

Limits & Default-Path Preservation

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.

1 participant