Repository navigation
Conversation
nastya236
left a comment
There was a problem hiding this comment.
Thank you for you contribution. It might take me some time to review the pull request.
DO you mind sharing with me exact command that you use to get the numbers?
c5a893b to
9194c5c
Compare
|
Thanks for taking a look. Here are the benchmark scripts, the exact build and run commands, and steps to reproduce the results. |
…4181) * [MLX] Build MLX Native from a pinned MLX main commit * [MLX] Drop the MLX 0.32.3 bit-parity workarounds * [MLX] Patch the MLX build with ml-explore/mlx#4852 and #4860
9194c5c to
b558dfe
Compare
zcbenz
left a comment
There was a problem hiding this comment.
It is very hard to understand what this PR does by reading the code, can you elaborate what it does and how it reduces the peak memory?
Thanks for the review. The memory saving comes from reusing intermediate storage before a command buffer completes. Currently, completion handlers retain the For allocator owned buffers, this PR tracks outstanding GPU uses separately from array ownership. When the last array reference disappears, the allocator keeps the Metal buffer alive and makes it available for reuse if all outstanding uses belong to one stream. A suitable later allocation on that stream can then reuse the storage. For example, in a chain of matmuls, later outputs can reuse storage from earlier intermediates, reducing the number of buffers allocated at once. This reuse introduces write-after-read dependencies. This PR adds barriers within concurrent encoders and tracks both reader and writer fences across encoders, so a new write waits for earlier accesses(all R&W) to the same buffer and a new read waits for earlier writes. Buffers with outstanding uses on multiple streams are excluded from early reuse, as are allocations for CPU writes. Externally owned buffers keep their existing |
b558dfe to
a39fded
Compare
|
I’ve also integrated this patch into sglang-omni’s native MLX runtime. With this patch, Qwen3-ASR 0.6B’s peak process footprint on long audio dropped from 2.97 GB to 1.98 GB on an M4 Air. |
|
Thanks for the explanations, my understanding is that within the same command buffer when we know kernel_b depends on kernel_a, we can reliably reuses the buffers of kernel_a for kernel_b. On the implementation, I think it is probably not necessary to change the design of allocator. We can change memory allocations to go through command encoder like |
Summary
Track inflight Metal buffer use separately from array ownership. Reuse released storage on the sole stream still using it, with fences for earlier reads and writes and allocator checks for host writes and donation.
Benchmarks
Values are medians over 48 trials per build for small models, 6 for Qwen3 and Parakeet, and 8 for vision and Qwen3-ASR.
Extra active peak memory does not include model footprint.