Skip to content

[Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking - #474

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-shared-event-doorbell
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-shared-event-doorbell

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

On partial-VRAM Apple Silicon Macs (such as 16 GB M4 running Qwen4-35B-A3B UD-IQ2_XS with ~40% of routed experts in VRAM), every MoE layer previously synchronized back to the host via separate command buffers and host routing round-trips, adding heavy per-layer router wait latency.

This PR adds a native Metal MTLSharedEvent doorbell for partial-VRAM MoE decode, upgraded with:

  1. --defer-rank opt-in cross-layer expert deferral & 16-row batching (up to 16 rows / 128 jobs, ping-pong buffers, Inkling2 metal_bell_defer_ok() guard). Default --defer-rank -1 is strictly lossless; --defer-rank >= 0 is an opt-in approximate overlap knob.
  2. macOS IOGPU watchdog / AGX wired-memory leak protection:
    • Automatic < 10% cached-expert floor disabling doorbell when too few experts are resident in VRAM (plus STRATA_METAL_BELL=0 rollback and STRATA_METAL_BELL_FORCE=1 override), preventing kIOGPUCommandBufferCallbackErrorTimeout and multi-GiB AGX driver leaks when 0 experts are cached.
    • Adaptive bounded command-buffer chunking (20 MoE layers/CB when >= 25% of routed experts are cached, else 8; STRATA_METAL_BELL_CHUNK override) with zero-bubble lookahead CB pipelining and clean rec_short_/rec_short_n_ fallback.
  3. Full integration with origin/main's bell_pending_ ([Engine] synchronizing the compute stream between the doorbell launch and bell_serve() aborts the process #419) (metal_sync_memory(), BellOff, bell_unwind()).

Measured Results (Apple M4 16 GB & Apple M5 Ultra 256 GB, Qwen4-35B-A3B-UD-IQ2_XS)

  • Safety (--no-gpu-experts, 0 experts in VRAM on 16 GB M4): cleanly disables doorbell, completes without timeout and zero AGX leak (gpu_inuse = 0.51 GiB during run -> 0.00–0.18 GiB after exit).
  • Speed (16 GB M4, partial-VRAM MTP decode): with opt-in --defer-rank 0, 28.02 tok/s (R3) / 30.70 tok/s (R4) with gpu_inuse = 0.18 GiB after 22 consecutive runs (zero AGX leak).
  • Honest Quality Verification (Apple M5 Ultra, simulated 4,000-expert partial VRAM, cold/low-hit 36%–76% stress test):
    • Default Metal Doorbell (--defer-rank -1, strictly lossless): 99.22% Top-1, 100.00% Top-5, GoldP = 0.7299 (vs Full-VRAM baseline 0.7326), 6/6 tasks PASS (math trap, logic puzzle, Python LRUCache, Python edit_distance, Zh QA, 16K NIAH) — confirms zero mathematical loss on the default doorbell path.
    • Opt-in --defer-rank 4 (defer bottom 4 experts): 92.97% Top-1, 100.00% Top-5, GoldP = 0.7129, 5/6 tasks PASS (logic puzzle slipped at 35% VRAM hit).
    • Opt-in --defer-rank 0 (defer all CPU misses): at ~51% VRAM hit, 79.69% Top-1, 98.44% Top-5, GoldP = 0.6198, 4/6 tasks PASS (math/logic degrade when ~49% of experts are deferred one layer late; keep --defer-rank -1 default for strict fidelity and reserve --defer-rank 0 for high-VRAM-hit >92% workloads).

Related discussion

Tracking issue: #472. Cleanly rebased onto origin/main (0794bc5, including #498, #464, #467, #504) at commit 5a3b357.

Validation

  • Passed (exact rebased commit 5a3b357 on origin/main 0794bc5, Apple M5 Ultra 64-core Apple10):
    • Full Metal Release build (cmake --build engine/build-metal -j16): PASS.
    • ctest --test-dir engine/build-metal -L poly --output-on-failure: 51/51 (100%) PASS (includes poly_advanced_parity, poly_memory_growth BellChunkEngine, poly_quantized_parity id=-1 skip across k1/k1g/generic, poly_inkling_parity, poly_qwen4_parity).
    • python3 tools/test_dropped_paths.py and python3 tools/settings_register.py --check: PASS.
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft per CONTRIBUTING.md pending maintainer run-backends CI.

Limits

  • Metal doorbell applies to partial-VRAM MoE decode (!all_cached(), cached >= 10%); full-VRAM hosts (all_cached()) continue on the existing zero-sync GPU path unchanged.

@NightRain233
NightRain233 force-pushed the feat/metal-shared-event-doorbell branch from 69ef5af to 5a3b357 Compare December 6, 2026 16:32
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