Skip to content

fix(mtp): scope speculative command-buffer caps to Qwen targets - #4386

Merged
jundot merged 2 commits into
jundot:mainfrom
junmo-kim:fix/mtp-qwen-command-buffer-caps
Oct 12, 2026
Merged

jundot merged 2 commits into
jundot:mainfrom
junmo-kim:fix/mtp-qwen-command-buffer-caps

Conversation

@junmo-kim

@junmo-kim junmo-kim commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

#3797 raises MLX's command-buffer caps to 200 ops / 512 MB around each Lightning MTP and batched DFlash speculative step on M5 and M3-family GPUs. The context manager sits on the shared speculative path, so the raised caps also apply to targets added later, such as GLM-5.3-Flash, where they slow decode.

This PR keeps the raised caps for the Qwen target types those paths were tuned for (qwen3_5, qwen3_5_moe, qwen3_5_vl, qwen3_6, qwen3_6_moe, qwen4_exp, qwen4_exp_text) and leaves MLX's current caps untouched for other targets. The type is read from the loaded target and its language_model wrapper (model_type, args, config, text_config); drafter metadata is not consulted. Restoration after each step and the prefill caps are unchanged.

There is no env override, following #4396. docs/TESTING.md documents the target selection and the test command.

Validation

Hardware: Mac Studio M5 Ultra, 256 GB, macOS 27.

This PR on MLX 0.32.3. Measured at e6e2fc80 (on main at d1a928dc; the follow-up commit only removes the env override), official mlx/mlx-metal 0.32.3 wheels, native kernels rebuilt from this tree. GLM-5.3-Flash (3.5-bit oQ mixed quantization, MTP draft 3), temperature 0, 256 output tokens. "On" forced the raised caps through the env override that e6e2fc80 still had (removed in the follow-up commit), which matches main on M5; "off" is this PR's default for GLM. Four ABBA blocks, one per code prompt, each with one JSON and one long-context request (two unique prompts each); 48 measured requests after 16 warm-up requests.

Per-block decode change for caps off vs. on, averaged over the 4 blocks:

Workload Input tokens On (tok/s) Off (tok/s) Change Range across blocks
Code 498–505 78.14 80.77 +3.51% +1.37% to +5.59%
JSON 549 85.76 86.95 +1.40% +1.29% to +1.53%
Long context 15,479 / 30,720 67.13 68.90 +2.66% +1.06% to +4.95%

All 64 responses (48 measured, 16 warm-up) had identical token IDs and text between on and off. Peak process footprint was 165.5 GiB with caps on and 165.3 GiB with caps off.

Earlier measurements on MLX 0.32.2. A local build of upstream main at a435a373 (2026-10-05) plus local patches outside this code path, comparing caps on and off on the same build.

GLM-5.3-Flash (3.5-bit oQ mixed quantization, MTP draft 3). "Off" skipped only the set_command_buffer_caps call. Four ABBA/BAAB blocks after warm-up, 256 output tokens, cold prefix cache. Per-block decode change for caps off vs. on, averaged over the 4 blocks:

Workload Input tokens On (tok/s) Off (tok/s) Change Range across blocks
Code 513 84.01 88.28 +5.09% +2.08% to +6.88%
JSON 513 92.84 95.60 +3.00% +1.01% to +5.45%
Long context 30,720 79.93 81.14 +1.51% +0.46% to +2.04%

NLL on 4,608 held-out targets and 36 full-vocabulary logit rows were bit-identical between on and off, and all 82 generated responses were byte-identical. Functional checks passed 9/9 in both arms. Peak process footprint was 165.5 GiB with caps on and 165.4 GiB with caps off.

Qwen3.8-Flash-Next (oQ4 with MTP, qwen4_exp). Caps forced on and off through a local override. Four code prompts, ABBA, temperature 0, 256 output tokens, 16 measured runs. The geometric-mean decode change for on vs. off was +0.32% (per prompt +1.26%, −0.04%, −0.14%, +0.20%), and output token IDs were identical. This PR therefore keeps the existing Qwen default; it does not claim a Qwen speedup.

Tests (M4 Mac mini, GPU detection and the native setter mocked):

python -m pytest -q tests/test_mlx_lm_mtp_patch.py -k "spec_command_buffers or spec_caps"

13 passed. The new cases fail on main, and 3 of them fail if the target check is removed.

Not run: the default suite (python -m pytest) locally, left to CI.

Checklist

  • I read CONTRIBUTING.md.
  • I understand every change in this PR and can explain it in review.
  • For UI or UX changes, I attached before and after screenshots. (No UI changes.)

@junmo-kim junmo-kim closed this Oct 9, 2026
@junmo-kim junmo-kim reopened this Oct 9, 2026
jundot#4396 removed debug env switches from production code. Remove the
OMLX_MTP_COMMAND_BUFFER_CAPS override added here as well, so the raised
caps follow only the target type and the GPU gate.

@jundot jundot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raised caps were only ever measured on Qwen, so keeping them there makes sense. I ran an ABBA on M3 Ultra, and GLM-5.3-Flash came out about the same either way while Gemma 4 didn't lose anything without the raised caps.

Model (Lightning MTP, greedy, 256 tokens) Prompt main PR Change
GLM-5.3-Flash oQ4e ~600 tokens 59.23 tok/s 59.77 tok/s +0.9%
GLM-5.3-Flash oQ4e ~13K tokens 53.66 tok/s 53.95 tok/s +0.5%
Gemma 4 31B oQ4e ~700 tokens 46.98 tok/s 48.56 tok/s +3.4%
Gemma 4 31B oQ4e ~16K tokens 34.75 tok/s 36.25 tok/s +4.3%

Thanks for the careful A/B, LGTM, merging.

@jundot
jundot merged commit d128c11 into jundot:main Oct 12, 2026
1 check passed
jundot added a commit that referenced this pull request Oct 12, 2026
- #4376: drop OMLX_SHARED_SAMPLERS, cache samplers with lru_cache, move tests into test_sampling.py
- #4386: read the target model_type directly
- #4408: drop the decode try/except, keep the older verify tests on their own paths
- #4442: shorten docstrings, move tests into test_turboquant.py
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