Repository navigation
Speed up the Qwen3-VL vision tower with a compiled rotary and a matmul patch embed - #2822
Merged
Merged
Conversation
…l patch embed The rotary embedding was applied eagerly in every vision block, and the patch embedding ran a Conv3d whose kernel equals its stride. Compile apply_rotary_pos_emb_vision, and compute the patch embedding as a matmul over the flattened patch with the permuted weight. This covers qwen3_vl and the models that reuse its vision tower (qwen3_5, qwen3_5_moe, qwen3_vl_embedding, qwen4_exp). Measured on an M5 Max in bf16, vision tower alone, one 720p and one 1080p image: 6% to 19% less time (Holo-3.1-0.8B: -19% at 720p, -18% at 1080p; Holo2-4B: -17% / -8%; Holo-3.1-35B-A3B vision tower: -11% / -6%). Time to first token is 4% to 10% lower on the models tried. The rotary output is bit-identical to the eager version. On GPU the rmse against an fp32 reference is the same before and after, in bf16 and fp16. In fp32 the matmul runs at TF32 precision, like the other linear layers of the tower; the patch embed test relies on conftest.py setting MLX_ENABLE_TF32=0.
Blaizzy
approved these changes
Nov 29, 2026
Collaborator
|
LGTM thanks! |
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.
This cuts Qwen3-VL vision tower time by 8 to 20% for the 0.8B and 4B models in bf16 on an M5 Max, and time to first token by 4 to 13%.
The Qwen3-VL vision tower spends time on separate rotary operations and a
Conv3dpatch embedding that can be expressed as a matmul.apply_rotary_pos_emb_visionruns several elementwise operations twice per layer, once for q and once for k. Adding@mx.compilefuses them into one kernel per call. The output is bit-identical in fp32 and bf16 on both CPU and GPU.PatchEmbeduses aConv3dwhose kernel size equals its stride, so I replaced it with a matmul over flattened patches. The weight is reordered once instead of permuting the input. At 1080p, the Conv3d took 10.5 to 22 ms across model sizes, compared with about 0.6 ms for the matmul.On an M5 Max with an idle GPU, an interleaved A/B in the same process (bf16) gave:
The bf16 vision-tower RMSE against fp32 is unchanged at 0.00220, 0.00257 and 0.00269 for the three model sizes. Full-model answer KL against fp32 is
3.2e-4for the 0.8B and2.0e-4for the 4B. The fp32 patch embedding now uses TF32, like the rest of the tower.Added two tests in
test_models.py. One compares patch embedding againstConv3dwith a random bias and checks thatmx.conv3dis not called. The other checks that rotary compilation traces at most once for repeated calls with the same shape. Both fail without the change. The rotary test is skipped withMLX_DISABLE_COMPILE.mx.compileadds about 1 to 4 ms of tracing on the first call for each new shape and dtype, following the same pattern asrope_utils.py.The changes also apply to
qwen3_5,qwen3_5_moe,qwen4_expandqwen3_vl_embedding, which inherit this implementation.qwen3_vl_moeandqwen3_omni_moehave separate vision implementations and are unaffected. I can send the same change forqwen3_vl_moein a follow-up.