Repository navigation
metal: runtime setter for command-buffer limits - #4562
aidiffuser wants to merge 1 commit into
Conversation
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]>
|
@aidiffuser I'm good with addition of runtime setter, but I'm also wondering |
|
Thanks! We measured the floor before answering. It's a 5-line change to 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:
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:
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.
What we take from it:
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, Co-authored with Claude Opus 5.5 |
|
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. |
Adds
mx.metal.set_command_buffer_limits(max_ops, max_mb):<= 0keeps that limit;(max_ops, max_mb);C++:
mlx::core::metal::set_command_buffer_limits, plus aDevicesetter next to the existing getter, and a no-op stub for non-Metal builds.Why
MLX commits a command buffer after
max_ops_per_bufferoperations ormax_mb_per_bufferMB of distinct inputs: 50 / 50 on M3 Ultra, set at startup or throughMLX_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, whileevallets 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:
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:
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_limitsinpython/tests/test_eval.pychecks:<= 0keep-rule;Docs: added to
docs/src/python/metal.rst.Co-authored with Claude Opus 5.5