Fix Metal resource leak under high concurrency#92
Open
janhilgard wants to merge 1 commit intowaybarrios:mainfrom
Open
Fix Metal resource leak under high concurrency#92janhilgard wants to merge 1 commit intowaybarrios:mainfrom
janhilgard wants to merge 1 commit intowaybarrios:mainfrom
Conversation
Addresses Metal buffer leak where batch.tokens grows via mx.concatenate() each generation step without evaluation, causing computation graph nodes to hold AGXAllocation handles indefinitely. Changes: - Add mx.async_eval(*batch.tokens) after each generation step to eagerly evaluate accumulated token concatenations and release Metal buffers - Make cache clear interval adaptive: scales inversely with active sequence count (min interval 8) to prevent Metal resource handle exhaustion under high-concurrency workloads - Add explicit mx.eval(*tokens) during periodic cache clear to collapse any remaining lazy concatenation chains before clearing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #91 — Metal buffer leak under high concurrency.
Root cause:
batch.tokensgrows viamx.concatenate()each generation step but is never evaluated, so computation graph nodes holdAGXAllocationhandles indefinitely. Under high concurrency this exhausts Metal resource handles.Changes:
mx.async_eval(*batch.tokens)after each generation step to eagerly evaluate accumulated token concatenations and release Metal buffersmx.eval(*tokens)during periodic cache clear to collapse any remaining lazy concatenation chainsHow it works
Fix A — Eager evaluation (line ~202):
Fix B — Adaptive cache clearing (line ~2239):
batch.tokensto collapse lazy chainsTest plan
ioclasscount | grep AGXAllocationstays stable under sustained high-concurrency load🤖 Generated with Claude Code