metal : fix memory leaks due to missing autoreleasepools - #27758
Conversation
|
/bot review |
Automated code reviewI reviewed the diff (verified via Verdict: looks correct, no blocking findingsI confirmed that all objects assigned to struct members inside the new autoreleasepools are +1 retained (Create/new/alloc/init/newSharedEvent rules) and are explicitly released in the corresponding free functions, so they survive the pool drain and no dangling pointers are introduced:
The pattern (wrapping a whole one-shot init body in The Notes for the contributor(point 1) The bulk of the diff is pure re-indentation. Reviewers can use (point 2) Nit (pre-existing, not introduced here): in (point 3) Nit: the two (point 4) AI disclosure in the PR template is present and appropriate; the commit message is clean (no No changes to public API, no new ops/quant types, no security-relevant input parsing touched. The change is Metal-internal, minimal, and consistent with the codebase. This should be a straightforward review. This review was generated automatically by pi coding agent using |
This is a good finding by the bot. Putting that on my list of things to fix in future PRs. |
| [encoder->obj pushDebugGroup:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]]; | ||
| @autoreleasepool { | ||
| [encoder->obj pushDebugGroup:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]]; | ||
| } |
There was a problem hiding this comment.
In this case what is causing the leak - the NSString?
There was a problem hiding this comment.
Yes, here it's the NSString.
|
The test failures are unrelated. (The changes only affect Apple builds.) |
|
Btw, we have this job in the CI that checks for leaks on macos: llama.cpp/.github/workflows/build-apple.yml Lines 67 to 71 in 5e6a37c Not sure why it didn't catch these - any ideas? Can we modify it somehow to be sensitive to those leaks? |
|
@ggerganov Thanks for the pointer to the test!
That said, we can't update the test yet. There's still one |
Overview
Running
llama-cliwithOBJC_DEBUG_MISSING_POOLS=YESon Mac shows various memory leaks in the Metal init path that look like this:This PR adds three
@autoreleasepools to fix these leaks. Apart from that, it's only whitespace changes. (Rungit show --ignore-all-spacelocally to view the diff without whitespace changes.)Additional information
There are still some lines left related to
AGXG16SDevice. I'll check if we can fix those or if those come from inside Metal. I'd love to get this merged in the meantime though.Requirements
@autoreleasepools, verified it manually though and in the process deleted 80% of Opus' code.