Skip to content

Reuse in-flight Metal buffers on the same stream - #4852

Open
XXXXRT666 wants to merge 1 commit into
ml-explore:mainfrom
XXXXRT666:exp/metal-stream-buffer-reuse
Open

XXXXRT666 wants to merge 1 commit into
ml-explore:mainfrom
XXXXRT666:exp/metal-stream-buffer-reuse

Conversation

@XXXXRT666

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Benchmarking and Testing

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.

Workload Wall time Total active peak (MB) Extra active peak (MB)
Small Transformer encoder, 512 tokens 8.305 → 8.073 56.715 → 12.652 49.500 → 5.438 (−89.0%)
Small LM prefill, 384 tokens 4.617 → 4.576 34.954 → 13.470 28.291 → 6.808 (−75.9%)
Small LM decode, 16 steps 13.786 → 12.920 13.978 → 12.201 3.941 → 2.165 (−45.1%)
Small residual CNN, 160 × 160 1.021 → 1.016 3.245 → 2.635 2.272 → 1.662 (−26.8%)
Small U-Net, 128 × 128 1.215 → 1.178 8.251 → 5.501 7.875 → 5.125 (−34.9%)
Two-layer GRU, 128 steps 14.571 → 12.682 0.678 → 0.566 0.286 → 0.174 (−39.3%)
Qwen3-0.6B prefill, 2,048 tokens 820.45 → 810.14 1,868.88 → 1,404.88 732 → 268 (−63.4%)
Qwen3-0.6B prefill, 4,096 tokens 1,863.32 → 1,860.83 2,112.89 → 1,672.89 976 → 536 (−45.1%)
Qwen3-0.6B prefill, 6,144 tokens 3,338.36 → 3,353.98 2,348.91 → 1,940.91 1,212 → 804 (−33.7%)
Qwen3-0.6B decode, 8 tokens 149.95 → 154.42 1,630.71 → 1,614.20 269.83 → 253.33 (−6.1%)
Parakeet encoder, 33 s 221.03 → 222.37 1,656.07 → 1,341.83 419.58 → 105.06 (−75.0%)
ResNet-50, batch 1 7.903 → 8.028 72.89 → 57.31 23.71 → 8.12 (−65.7%)
ResNet-50, batch 32 175.34 → 169.00 768.41 → 322.53 710.34 → 264.45 (−62.8%)
ViT-L/16, batch 1 59.00 → 58.58 814.81 → 587.38 234.04 → 6.61 (−97.2%)
ViT-L/16, batch 16 745.47 → 766.32 1,222.04 → 671.70 636.97 → 86.64 (−86.4%)
Qwen3-ASR encoder, 33 s 169.03 → 168.82 2,483.54 → 1,841.06 982.18 → 339.70 (−65.4%)
Qwen3-ASR transcription, 33 s 1,700.34 → 1,627.52 2,483.67 → 1,841.07 985.94 → 343.35 (−65.2%)

@nastya236 nastya236 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Comment thread docs/src/python/nn/distributed.rst Resolved
Comment thread docs/src/python/devices_and_streams.rst Resolved
Comment thread docs/src/python/array.rst Resolved
@nastya236 nastya236 added the await response This pull request is waiting for response from the author. label Nov 17, 2026
@XXXXRT666
XXXXRT666 force-pushed the exp/metal-stream-buffer-reuse branch from c5a893b to 9194c5c Compare November 17, 2026 05:23
@XXXXRT666

XXXXRT666 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. Here are the benchmark scripts, the exact build and run commands, and steps to reproduce the results.

mlx-pr-4852-benchmarks.zip

@nastya236 nastya236 added await verification This pull request is non-trivial and requires a human expert to verify its correctness. and removed await response This pull request is waiting for response from the author. labels Nov 17, 2026
XXXXRT666 added a commit to sgl-project/sglang-omni that referenced this pull request Nov 18, 2026
…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
@XXXXRT666
XXXXRT666 force-pushed the exp/metal-stream-buffer-reuse branch from 9194c5c to b558dfe Compare November 24, 2026 18:38

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@XXXXRT666

XXXXRT666 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor Author

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 array::Data for primitive inputs. These references can keep an intermediate’s allocation alive after its last use has been encoded and the graph has dropped its own references. A long chain of operations can accumulate many intermediate allocations until the command buffer completes.

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 array::Data retention.

@XXXXRT666
XXXXRT666 force-pushed the exp/metal-stream-buffer-reuse branch from b558dfe to a39fded Compare December 5, 2026 04:58
@XXXXRT666

Copy link
Copy Markdown
Contributor Author

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.

@zcbenz

zcbenz commented Dec 6, 2026

Copy link
Copy Markdown
Member

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 encoder.malloc(size) (similar to cuda's interface which uses a stream allocator), then the encoder would hold the buffers allocated for current command buffer and decide whether to reuse a held buffer or allocate a new one from the allocator.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

await verification This pull request is non-trivial and requires a human expert to verify its correctness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants