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 cross-layer expert deferral & 16-row batching (up to 16 rows / 128 jobs, ping-pong buffers, Inkling2 metal_bell_defer_ok() guard).
  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 (submitting chunk c + 1 before serving chunk c's last slot) and clean rec_short_/rec_off_ OOM fallback to run_direct().
  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 on Apple M4 16 GB (Qwen4-35B-A3B-UD-IQ2_XS, integrated R3 stack bfd5d1b):

  • --no-gpu-experts (0 experts in VRAM): cleanly disables doorbell, completes 8 tokens (7.04 tok/s, 11.7 s), zero timeout and zero AGX leak (gpu_inuse = 0.51 GiB).
  • Partial-VRAM MTP decode with --defer-rank 0: 28.02 tok/s (router wait cut to 17.0 ms/tok, gpu_inuse = 0.18 GiB after 22 consecutive runs — zero AGX leak).

Related discussion

Tracking issue: #472. Rebased onto origin/main (7cef952, v0.2.20-poly.11) and integrated cleanly with #419 (bell_pending_).

Approach

  • Adds MTLSharedEvent host/GPU signaling primitives in engine/metal/include/strata/gpu/metal.hpp and engine/metal/src/gpu/runtime.cpp, plus metal_bell_ring / metal_bell_add kernels in advanced.{metal,cpp} and zero-bandwidth negative expert-id skip in quantized.metal / basic.metal / inkling.metal.
  • Keeps non-Metal CUDA/HIP/SYCL paths untouched (#if defined(STRATA_GPU_METAL) balanced across all shared files).
  • Registers STRATA_METAL_BELL, STRATA_METAL_BELL_CHUNK, STRATA_METAL_BELL_FORCE, and updates STRATA_POLY_BELL in terminal/data/settings-register.json + docs/SETTINGS.md.

Validation

  • Passed (Apple M4 16 GB, macOS, commit 69ef5af on origin/main 7cef952):
    • Metal Release configure + build (strata-poly, poly_advanced_parity, poly_quantized_parity, poly_memory_growth, poly_runtime_safety, poly_basic_parity, poly_qwen4_parity, poly_inkling_parity, poly_tiny_model_fixture): 0 errors, 0 warnings.
    • ctest --test-dir engine/build-metal -R '^(poly_advanced_parity|poly_quantized_parity|poly_memory_growth|poly_runtime_safety|poly_basic_parity|poly_qwen4_parity|poly_balanced_qwen35moe_protocol|poly_metal_defaults)$' --output-on-failure: 14/14 passed (includes BellChunkEngine 3-chunk & 1-slot-chunk parity for defer_rank=-1 and defer_rank=1, rec_short_ fallback & recovery, and bell_pending_ exception-unwind test).
    • ctest --test-dir engine/build-metal -R '^poly_inkling_parity$' --output-on-failure: 1/1 passed.
    • python3 tools/check --only docs: passed (6/6).
    • python3 -m unittest tools.test_settings_register: passed (8/8).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft pending cross-backend CI (backends.yml / check.yml).

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.
  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via CI before marking Ready for Review.

@NightRain233
NightRain233 force-pushed the feat/metal-shared-event-doorbell branch from fbd684b to 69ef5af Compare December 6, 2026 05:38
@NightRain233 NightRain233 changed the title [Engine] metal: add MTLSharedEvent doorbell for partial-VRAM MoE decode [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking Dec 6, 2026
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