fix(oq): prefuse per-expert MoE weights before mlx_vlm sanitize#933
Open
marxo126 wants to merge 1 commit intojundot:mainfrom
Open
fix(oq): prefuse per-expert MoE weights before mlx_vlm sanitize#933marxo126 wants to merge 1 commit intojundot:mainfrom
marxo126 wants to merge 1 commit intojundot:mainfrom
Conversation
FP8 MoE VLM checkpoints (e.g. Qwen3.5-MoE variants) store experts as
`...mlp.experts.{n}.(gate|up|down)_proj.weight` after FP8 dequant, but
`mlx_vlm`'s `Model.sanitize` pops a single fused `experts.gate_up_proj`
and crashes with `KeyError` when it is not present.
Add `_prefuse_moe_experts_for_vlm` that rebuilds the fused layout in
chunks (16 experts per stack) so peak memory stays bounded for models
with hundreds of experts. Called from `_vlm_sanitize` before the
upstream sanitize. No-op on dense models and already-fused checkpoints.
Covers 7 unit tests: round-trip split invariance, already-fused no-op,
dense no-op, partial-layer guard, four `num_experts` config fallbacks,
and 20-expert chunk spillover.
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.
Summary
FP8 MoE VLM checkpoints (e.g.
Huihui-Qwen3.6-35B-A3B-*-FP8,yujiepan/qwen3.5-moe-tiny-random) crash during oQ quantization after the FP8 dequant phase completes:Reproduced on v0.3.6 and v0.3.7rc2. FP8 dequant succeeds (31,738 tensors), then
mlx_vlm's ownModel.sanitizecrashes because it assumes a fusedexperts.gate_up_projtensor while FP8 checkpoints are laid out per-expert (experts.{n}.gate_proj.weight, etc.).Fix
mlx_vlmupstream declined to handle this layout (see Blaizzy/mlx-vlm#815 / #816: "works well with official model weights"). oMLX owns the FP8 dequant → per-expert layout, so the fix belongs in oMLX rather than mlx_vlm.Add
_prefuse_moe_experts_for_vlm(weights, config)inoq.pythat rebuilds the fused layout mlx_vlm expects before callingModel.sanitize:...mlp.experts.{n}.(gate|up|down)_proj.weightkeys via a compiled regex_DiscoveredPlan._STACK_CHUNK) +mx.clear_cache()between chunks → bounded peak memory for 128/256-expert modelsexperts.gate_up_proj([E, 2·I, H]) andexperts.down_proj([E, H, I]) in the layoutmlx_vlm.qwen3_5_moe.sanitizethen splits ataxis=-2and passes throughnum_expertsunset) and already-fused checkpoints — early return, zero scan costnum_expertsfrom four config locations (num_local_experts/num_experts, top-level ortext_config)Call site is a single line in
_vlm_sanitizebeforeModel.sanitize(proxy, weights).Test plan
tests/test_oq.py::TestPrefuseMoeExpertsForVlm— 7 unit tests:mx.split(fused, 2, axis=-2)recovers original gate / up valuesnum_experts)num_expertsconfig key fallbackspytest tests/test_oq.py -m "not slow"→ 133 passed (7 new + 126 existing, no regressions)Scope