Repository navigation
feat(qwen4-exp): stream MoE routed-expert weights off the wired/phys budget - #3437
alytaphoenix wants to merge 2 commits into
Conversation
…budget Bind the ~46.87 GB of qwen4_exp routed-expert (switch_mlp) tensors as mmap-backed, page-aligned arrays over a side-car artifact next to the checkpoint instead of loading them resident, so Qwen3.8-Flash-Next-MLX-oQ2-MTP fits and serves long context on a 64 GB M4 Pro mini. Components: - omlx/custom_kernels/qwen4_moe_stream/: native _ext (bytesNoCopy MTLBuffer wrap of page-aligned tensors, refcounted deferred munmap), StreamingArtifact manifest index + canonical key parser, weights-list interception, and the repack.py artifact producer (source-dir parameterized via --model-dir / OMLX_QWEN4_MODEL_DIR, no hardcoded home path). - engine/vlm.py: _stream_qwen4_exp_experts_on_load context manager -- patches Module.load_weights POST-sanitize to swap expert values before mx.eval, with expect_all hard-fail, a load-time bit-exactness canary, and failure-path teardown that closes the mapping and skips the canary/success log if the load body raises after the swap. - engine_pool.py: streaming-aware resident-size estimate for admission and unload-time artifact close. - utils/proc_memory.py: name-keyed external-wired provider registry + discount_external_wired measurement hygiene. - process_memory_enforcer.py: external-wired is observability-only in /health, not subtracted from any ceiling (metal_cap Fix A, dynamic ceiling Fix D); real wired pressure is caught live via vm_stat free-bucket depletion. - scheduler.py: wire the provider through the enforcer breakdown. Kill switch: OMLX_QWEN4_MOE_STREAM=0 forces the fully-resident path. Streaming engages only when the artifact exists next to the checkpoint and the native extension is available; otherwise it is a clean resident-load no-op. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FR1CuCS9onnMPzwTTkFU4r
7b47130 to
69974c7
Compare
|
Rebased onto current main (1 real conflict in engine_pool.py: main independently added recurring-attempt tracking + structured decision logging to the prefill-eviction loop since this branch was cut — merged both, applying this feature's |
…t-streaming # Conflicts: # omlx/engine_pool.py # omlx/process_memory_enforcer.py # omlx/scheduler.py # omlx/utils/proc_memory.py
|
Main now has generic MoE expert offload (#2595). It streams non-resident experts straight from the original checkpoint with a configurable resident fraction, and it works with qwen4_exp including Lightning MTP (#3935). I'd rather build on that path than add a second mechanism that needs a ~47 GB repacked side-car and patches |
What
Stream the ~46.87 GB of qwen4_exp routed-expert (
switch_mlp) weights off thewired/phys budget: bind them as mmap-backed, page-aligned arrays over a side-car
artifact next to the checkpoint instead of loading them resident. This lets
Qwen3.8-Flash-Next-MLX-oQ2-MTP load and serve long context on a 64 GB M4 Pro
mini, where the fully-resident path does not fit (backbone ~25.6 GB + experts
~46.9 GB > RAM).
Streaming engages only when the artifact exists next to the checkpoint and
the native extension is available; otherwise it is a clean fully-resident no-op.
Kill switch:
OMLX_QWEN4_MOE_STREAMdefaults to enabled whenever thestreaming artifact is present on disk; set it to
0to force the oldfully-resident path.
This is a measured tradeoff, not an unqualified win
Streaming buys the ability to run this model at all on 64 GB, at a real prefill
cost. Live single-request ladder measured (103,019 real prompt tokens,
929.7 s total):
At the top of the tested range this is roughly ~2x slower prefill than the
project's earlier non-streaming baseline (~223 tok/s-class). This corrects an
earlier design-doc framing that overstated the regression as "~15x slower" —
the real number is ~2x, not an order of magnitude.
Real context ceiling: ~121–122k, not the ~200k+ originally projected
The earlier design doc projected a ~200k+ context ceiling under streaming. That
projection is walked back. Live testing puts the real ceiling at ~121–122k
tokens. At that point real chunked-prefill transient memory (not anything
this feature's own accounting touches) hits the hard watermark and the enforcer
gracefully aborts just that one request while keeping the model loaded —
verified clean, not a crash or an eviction.
Production config (decided, now live on the mini):
max_context_window: 120000, accepting that requests very near the ~121–122k wall may occasionallyget a graceful per-request abort rather than always succeeding.
Open item
All live testing so far is single-request. No concurrent-traffic run under
streaming has been done yet — flagged as an open item, not a proven gap.
How it works
omlx/custom_kernels/qwen4_moe_stream/: native_ext(bytesNoCopy MTLBufferwrap of page-aligned tensors, refcounted deferred
munmap),StreamingArtifactmanifest index + canonical-key parser, weights-list interception, and
repack.py— the artifact producer / format authority (source dirparameterized via
--model-dir/OMLX_QWEN4_MODEL_DIR, no hardcoded path).engine/vlm.py:_stream_qwen4_exp_experts_on_loadcontext manager — patchesModule.load_weightspost-sanitize to swap expert values beforemx.eval,with
expect_allhard-fail, a load-time bit-exactness canary, andfailure-path teardown that closes the mapping and skips the canary/success log
if the load body raises after the swap.
engine_pool.py: streaming-aware resident-size estimate for admission +unload-time artifact close.
utils/proc_memory.py: name-keyed external-wired provider registry +discount_external_wiredmeasurement hygiene.process_memory_enforcer.py: external-wired is observability-only in/health, not subtracted from any ceiling (metal_capper Fix A, dynamicceiling per Fix D); real wired pressure is caught live via
vm_statfree-bucket depletion.
Provenance
This is a focused resubmit targeting
maindirectly, per @jundot's requeston #3283 to split the remaining wanted changes into focused PRs off current
main. Full review history and discussion for this feature live on
alytaphoenix#2 — this PR is the same feature commit, cherry-picked
cleanly onto current
mainas a single logical commit (the earlierdeploy-branch prereq-port commit it used to stack on is not needed —
mainalready independently carries the
engine_pool/deploymentsymbols itdepended on, so it was dropped).
Testing
Full
tests/suite re-run on the rebuilt-onto-mainbranch: 10664 passed,150 skipped, 77 deselected. The only failures (13) are pre-existing and
identical on clean
mainin this environment — an MLX patch-version pin intest_mlx0322_compat/test_sdpa256_attention(env has MLX 0.32.0, thosetests expect 0.32.2); none are in files this feature touches, so zero
regressions vs. the
mainbaseline.The native-extension tests in
tests/test_qwen4_moe_stream.pywere run forreal against the built
_extkernel (not skipped): 17/17 passed,including the bit-exact
load_weightsround-trip, deferred-munmap, andcanonical-key native paths.
🤖 Generated with Claude Code
https://claude.ai/code/session_01FR1CuCS9onnMPzwTTkFU4r