Skip to content

metal: runtime setter for command-buffer limits - #4562

Closed
aidiffuser wants to merge 1 commit into
ml-explore:mainfrom
aidiffuser:command-buffer-limits-setter
Closed

aidiffuser wants to merge 1 commit into
ml-explore:mainfrom
aidiffuser:command-buffer-limits-setter

Conversation

@aidiffuser

Copy link
Copy Markdown

Adds mx.metal.set_command_buffer_limits(max_ops, max_mb):

  • sets how many operations, and how many MB of distinct input buffers, a Metal command buffer may hold before it is committed;
  • a value <= 0 keeps that limit;
  • returns the previous (max_ops, max_mb);
  • takes effect for the next encoded operation.

C++: mlx::core::metal::set_command_buffer_limits, plus a Device setter next to the existing getter, and a no-op stub for non-Metal builds.

Why

MLX commits a command buffer after max_ops_per_buffer operations or max_mb_per_buffer MB of distinct inputs: 50 / 50 on M3 Ultra, set at startup or through MLX_MAX_OPS_PER_BUFFER / MLX_MAX_MB_PER_BUFFER. Weights count toward the MB budget. A batch-1 decode step of a large mixture-of-experts model therefore commits after nearly every expert matmul, while eval lets at most 10 command buffers be in flight.

On GLM-5.3 (744B, 4-bit), tensor-parallel over two M3 Ultra Mac Studios, Instruments' Metal System Trace shows:

  • ~231 command buffers per decode token, each covering ~0.25 ms of GPU work, so the in-flight window is only ~2.5 ms of lookahead;
  • the GPU busy only 68% of the time at 123k context.

Why a runtime setter and not the environment variables: raising the limits process-wide fixes decode, but in-flight command buffers keep their intermediates alive. A 30k-token prefill then held +88 GB instead of +8 GB. Decode and prefill want different limits, and the environment variables can only set one value for the whole process. With a runtime setter, an application raises the limits around decode steps only.

Measured

Around each decode step only, 150 ops / 5000 MB. Two M3 Ultra Mac Studios, tensor-parallel, same build with the switch off and then on:

model context decode off → on (tok/s)
GLM-5.3 744B 4-bit short 18.7 → 19.3
123k 15.4 → 18.1 (+17.5%)
GLM-5.3-Flash 6-bit short 27.3 → 28.8
32k 23.7 → 25.0
100k 22.3 → 23.8
DeepSeek-V4-Flash short 28.9 → 30.7
32k 27.3 → 28.7
100k 26.2 → 27.7
MiMo-V2.6-Pro short 22.4 → 23.2
33k 21.2 → 22.2
  • Prefill speed and peak memory: unchanged, within 2%.
  • GLM-5.3 at 123k: GPU busy 83 → 89%, and command buffers per 4 s fell from 24,107 to 2,925.
  • Production: it has been running on our cluster since 2026-09-24.

Separately, it may be worth discussing whether resident weights should count toward the MB limit at all. This PR leaves that policy unchanged.

Tests

test_command_buffer_limits in python/tests/test_eval.py checks:

  • the returned previous values;
  • the <= 0 keep-rule;
  • that results are unchanged with 1-op and with 10,000-op command buffers.

Docs: added to docs/src/python/metal.rst.

Co-authored with Claude Opus 5.5

Adds mx.metal.set_command_buffer_limits(max_ops, max_mb), which sets how
many operations, and how many MB of distinct input buffers, a Metal command
buffer may hold before it is committed; a value <= 0 keeps that limit, and
the previous (max_ops, max_mb) is returned.

Weights count toward the MB limit, so batch-1 decode steps of large models
commit after nearly every weight read while at most a few command buffers
may be in flight. Larger limits help decode but hurt prefill, where
in-flight command buffers keep large intermediates alive, so they need to be
switchable at runtime rather than fixed at startup by MLX_MAX_OPS_PER_BUFFER
and MLX_MAX_MB_PER_BUFFER.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcbenz zcbenz added low priority await discussion This pull request makes some major changes that requires the team to have a discussion. and removed low priority labels Sep 27, 2026
@zcbenz

zcbenz commented Oct 7, 2026

Copy link
Copy Markdown
Member

@aidiffuser I'm good with addition of runtime setter, but I'm also wondering MLX_MAX_MB_PER_BUFFER might not be a good way for tuning alone and we might have to introduce a new tunable env. What do you think about #4521 (comment), i.e. requiring a minimum number of ops before checking MLX_MAX_MB_PER_BUFFER.

@aidiffuser

Copy link
Copy Markdown
Author

Thanks! We measured the floor before answering. It's a 5-line change to CommandEncoder::needs_commit, tunable through MLX_MIN_OPS_PER_BUFFER:

static int min_ops = env::get_var("MLX_MIN_OPS_PER_BUFFER", 0);
return (buffer_ops_ > max_ops) ||
    (buffer_ops_ > min_ops && (buffer_sizes_ >> 20) > max_mb);

We compared it with the defaults and with this PR's setter (limits raised around decode steps only).

One M3 Ultra, stock mlx-lm main. Greedy decoding, 256 tokens, the default 2048-token prefill chunks. Generated tokens were identical on every arm. Memory is peak active memory above the weights during a 32k-token prefill.

MiMo-V2.6-Flash (mxfp4 experts, 8-bit rest), decode 43.9 / 37.2 tok/s at short / 32k context with the defaults:

decode short decode 32k prefill memory
defaults (50 / 50) — — 11.3 GB
floor 10 +5.8% +5.0% 14.4 GB
floor 25 +8.0% +6.8% 27.9 GB
MLX_MAX_OPS/MB_PER_BUFFER = 150 / 5000, whole process +8.2% +2.7% 73.6 GB
setter 150 / 5000, decode only +7.9% +2.6% 11.3 GB
setter 50 / 5000, decode only +8.8% +7.2% 11.3 GB

GLM-5.3-Flash (6-bit; stock mlx-lm has no loader for it yet, so this uses ours), decode 21.3 / 19.0 tok/s with the defaults:

decode short decode 32k prefill memory
defaults — — 17.3 GB
floor 10 +2.0% +1.8% 20.1 GB
floor 25 +5.1% +3.6% 24.6 GB
setter 150 / 5000, decode only +7.3% +10.8% 17.3 GB
setter 50 / 5000, decode only +7.1% +7.2% 17.3 GB

Two M3 Ultras, tensor-parallel over JACCL (exo). Same comparison. Memory is the peak above the loaded model during a 32k prefill, averaged over the two machines. The defaults row is the mean of a control run at the start and one at the end.

GLM-5.3-Flash decode short / 32k prefill memory MiMo-V2.6-Pro decode short / 32k prefill memory
defaults 28.7 / 24.5 tok/s 20.9 GB 23.3 / 22.6 tok/s 10.0 GB
floor 10 −0.5% / +0.4% 24.4 GB +3.0% / +2.9% 23.1 GB
floor 25 +4.7% / +3.7% 31.5 GB +4.3% / +4.2% 34.7 GB
setter 150 / 5000, decode only +14.5% / +14.7% 21.1 GB +10.3% / +10.4% 9.7 GB
setter 50 / 5000, decode only +6.8% / +6.1% 20.6 GB +9.0% / +9.1% 9.8 GB

What we take from it:

  • The floor helps decode in most cases, but its prefill memory cost grows with the floor: +3–13 GB per machine at 10 and +7–25 GB at 25, up to 3.5× on MiMo-V2.6-Pro. The MB rule is what keeps prefill command buffers small, and command buffers in flight keep their large intermediates alive. Raising the limits for the whole process shows the extreme: 6.5× the prefill memory.
  • With collectives, the floor matters less. Each all-reduce already ends a command buffer, so floor 10 is within noise on GLM-5.3-Flash.
  • The best decode limits depend on the model and the setup. MiMo-V2.6-Flash on one machine prefers 50 ops; GLM-5.3-Flash and tensor-parallel decoding prefer 150. Raising them around decode only gets the full gain with no prefill cost.

So a small floor, around 10, looks like a reasonable default improvement if that memory cost is acceptable, and the runtime setter is still what lets an application take the larger decode gains. If you want the floor as a tunable, MLX_MIN_OPS_PER_BUFFER (default 0, the current behaviour) can go into this PR or a separate one, whichever you prefer.

Co-authored with Claude Opus 5.5

@zcbenz zcbenz mentioned this pull request Oct 8, 2026
@zcbenz

zcbenz commented Oct 8, 2026

Copy link
Copy Markdown
Member

Thanks a lot for benchmarking! I think the additional memory cost would be unexpected for a lot of users and we should not change the default behavior.

Regarding this PR, I think we should have a general API for changing configurations dynamically, because this is not the first request on making an API for a env var and it is very inconvenient to add new APIs for every configurable switch. I opened #4647, which would cover the use case in this PR.

@zcbenz zcbenz closed this Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

await discussion This pull request makes some major changes that requires the team to have a discussion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants