Skip to content

Speed up the Qwen3-VL vision tower with a compiled rotary and a matmul patch embed - #2822

Merged
Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
m4zar:qwen3-vl-vision-speedup
Nov 29, 2026
Merged

Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
m4zar:qwen3-vl-vision-speedup

Conversation

@m4zar

@m4zar m4zar commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 Conv3d patch embedding that can be expressed as a matmul.

apply_rotary_pos_emb_vision runs several elementwise operations twice per layer, once for q and once for k. Adding @mx.compile fuses them into one kernel per call. The output is bit-identical in fp32 and bf16 on both CPU and GPU.

PatchEmbed uses a Conv3d whose 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:

bf16, ms                 720p: main  this PR  change    1080p: main  this PR  change
Vision tower
Holo-3.1-0.8B                  48.4     38.9  -19.8%           156.8    135.6  -13.5%
Holo2-4B                      140.6    118.9  -15.4%           407.3    374.4   -8.1%
Holo-3.1-35B-A3B*             200.0    178.0  -11.0%           808.6    817.9   within noise
Time to first token
Holo-3.1-0.8B                 106.1     92.3  -13.0%           308.9    276.8  -10.4%
Holo2-4B                      454.5    433.4   -4.6%          1118.2   1068.6   -4.4%

* Real config, random weights

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-4 for the 0.8B and 2.0e-4 for 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 against Conv3d with a random bias and checks that mx.conv3d is 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 with MLX_DISABLE_COMPILE.

pytest -k "qwen3_vl or qwen3_5"       35 passed
test_models.py                       590 passed, 1 skipped
test_image_generation_models.py      247 passed
black --check                        ok

mx.compile adds about 1 to 4 ms of tracing on the first call for each new shape and dtype, following the same pattern as rope_utils.py.

The changes also apply to qwen3_5, qwen3_5_moe, qwen4_exp and qwen3_vl_embedding, which inherit this implementation. qwen3_vl_moe and qwen3_omni_moe have separate vision implementations and are unaffected. I can send the same change for qwen3_vl_moe in a follow-up.

…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

Blaizzy commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

LGTM thanks!

@Blaizzy
Blaizzy merged commit 8ece858 into Neywa-Labs:main Nov 29, 2026
11 checks passed
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.

2 participants