Repository navigation
[Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking - #474
Draft
NightRain233 wants to merge 1 commit into
Draft
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
NightRain233
force-pushed
the
feat/metal-shared-event-doorbell
branch
from
December 6, 2026 05:38
fbd684b to
69ef5af
Compare
This was referenced Dec 6, 2026
Merged
Draft
…dog-safe CB chunking
NightRain233
force-pushed
the
feat/metal-shared-event-doorbell
branch
from
December 6, 2026 16:32
69ef5af to
5a3b357
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 and result
On partial-VRAM Apple Silicon Macs (such as 16 GB M4 running Qwen4-35B-A3B
UD-IQ2_XSwith ~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
MTLSharedEventdoorbell for partial-VRAM MoE decode, upgraded with:--defer-rankopt-in cross-layer expert deferral & 16-row batching (up to 16 rows / 128 jobs, ping-pong buffers, Inkling2metal_bell_defer_ok()guard). Default--defer-rank -1is strictly lossless;--defer-rank >= 0is an opt-in approximate overlap knob.< 10%cached-expert floor disabling doorbell when too few experts are resident in VRAM (plusSTRATA_METAL_BELL=0rollback andSTRATA_METAL_BELL_FORCE=1override), preventingkIOGPUCommandBufferCallbackErrorTimeoutand multi-GiB AGX driver leaks when 0 experts are cached.20MoE layers/CB when>= 25%of routed experts are cached, else8;STRATA_METAL_BELL_CHUNKoverride) with zero-bubble lookahead CB pipelining and cleanrec_short_/rec_short_n_fallback.origin/main'sbell_pending_([Engine] synchronizing the compute stream between the doorbell launch andbell_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)--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 GiBduring run ->0.00–0.18 GiBafter exit).--defer-rank 0, 28.02 tok/s (R3) / 30.70 tok/s (R4) withgpu_inuse = 0.18 GiBafter 22 consecutive runs (zero AGX leak).--defer-rank -1, strictly lossless): 99.22% Top-1, 100.00% Top-5,GoldP = 0.7299(vs Full-VRAM baseline0.7326), 6/6 tasks PASS (math trap, logic puzzle, PythonLRUCache, Pythonedit_distance, Zh QA, 16K NIAH) — confirms zero mathematical loss on the default doorbell path.--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).--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 -1default for strict fidelity and reserve--defer-rank 0for high-VRAM-hit >92% workloads).Related discussion
Tracking issue: #472. Cleanly rebased onto
origin/main(0794bc5, including #498, #464, #467, #504) at commit5a3b357.Validation
5a3b357onorigin/main0794bc5, Apple M5 Ultra 64-core Apple10):cmake --build engine/build-metal -j16): PASS.ctest --test-dir engine/build-metal -L poly --output-on-failure: 51/51 (100%) PASS (includespoly_advanced_parity,poly_memory_growthBellChunkEngine,poly_quantized_parityid=-1skip acrossk1/k1g/generic,poly_inkling_parity,poly_qwen4_parity).python3 tools/test_dropped_paths.pyandpython3 tools/settings_register.py --check: PASS.CONTRIBUTING.mdpending maintainerrun-backendsCI.Limits
!all_cached(),cached >= 10%); full-VRAM hosts (all_cached()) continue on the existing zero-sync GPU path unchanged.