mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3600] [CLOSED] [Engine] Metal: Context::free gives the buffer back (alloc and wrap drain their own autorelease pool) #4707
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4707
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3600
Author: @Flo5k5
Created: 10/21/2026
Status: ❌ Closed
Base:
main← Head:fix/metal-free-autorelease📝 Commits (1)
3b41656metal: Context::alloc and wrap drain their own autorelease pool, so free() gives the buffer back📊 Changes
2 files changed (+91 additions, -27 deletions)
View changed files
📝
metal/src/mtl_rt.mm(+35 -27)➕
metal/tests/buffer_free_test.cpp(+56 -0)📄 Description
Problem and result
On the Mac engine,
mtl::Context::freereleases the buffer's handle but the memory stays allocated for the life of the process. Metal keeps an autoreleased reference to every newMTLBuffer, and the engine's threads have no autorelease pool, so that reference is never dropped.Context::runand the other submit paths already wrap their work in@autoreleasepool;allocandwrapdid not.Measured on an Apple M5 Pro: 40 buffers of 100 MB allocated, used by one dispatch and freed, four times over, leave 16.8 GB of process footprint (
task_vm_info.phys_footprint) onmain. With this change: 0.17 GB after each free, and no growth from one round to the next.This is what makes #3527 effective. That PR frees the prompt chain's old buffer set when
pc_setupgrows (prompt_chain.inc:60), but onmainthosefreecalls give nothing back. With Qwen3.8-Flash-Next Q2_0 and an agent client whose first prompt per task is 2,004-2,026 tokens (just under the 2,048-row chunk), each new task grows the set by a few rows and loses about 4 GB.Engine footprint (
vmmap -summary, physical footprint) after fresh conversations of 2,004, 2,017 and 2,600 tokens, then one of 5,279 tokens and its second turn:mainmain+ the #3527 change resolved ontomain(64K context, prompts of 1,963 and 1,973 tokens)main; an external memory watchdog stopped the third prompt (swap +3.8 GB)main+ the #3527 change + this PRRelated discussion
#3527 (the
pc_setupside of the same leak, open). It and this PR are independent: this one makesfreework; #3527 calls it where it was missing. Neither is enough alone for the prompt chain. Its model-free test passes because the test drains autorelease pools itself before checking the buffers' lifetime (which is also why it does not see this). As of011c9e60it no longer compiles againstmain: the extractedpc_setupnow callsT(...)and readshad_on_(Gyro).Approach
metal/src/mtl_rt.mm:Context::alloc,Context::wrap(p, bytes)and each chunk ofContext::wrap(p, bytes, max_span)run inside@autoreleasepool, so the only reference that remains is the one theBufferhandle holds (__bridge_retained), andfreedrops the last one. No behaviour change for buffers that are never freed.metal/tests/buffer_free_test.cpp: the check above, on a thread with no pool (as the engine's are). It does not need a model.Context::freein the engine today: thecoop_k4probe buffer (once, a few KB) andmpemb_inmtp_prefill(at most ~21 MB per growth). The prompt chain gets one with #3527.Validation
Revision:
fix/metal-free-autoreleaseat b19df7bb (main011c9e60+ this change). MacBook Pro, Apple M5 Pro, 64 GB, macOS 27.cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=… -DSTRATA_GGML_DIR=…(llama.cpp 3cf03257),ninja -C build-metal.Passed:
build-metal/buffer_free_test: PASS (9 checks). The same source linked againstmain'slibmtl_rt.a: FAILED (16.85 GB held after the fourth free), as expected.metal/tests: 20 of 20 programs pass (runtime_hello, arena_budget_test, wrap_split_test, coop_layout, dense_mm, expert, fuse, gdn, gdn_prompt, gemv, gr2, gr, mma, moe_mm, pair, prompt, qsa, qsa_prompt, route and sample parity), no FAIL line.--spec 2 --mtp … --prefill auto --max-context 32768 --mmap-experts,STRATA_METAL_STREAM=0,STRATA_METAL_PROMPT_CHUNK=2048), served byserve/server.py, with the #3527 change on top: the footprint table above. TheIOAccelerator (graphics)region stays at 9.7-9.8 GB over the whole sequence (main: 9.7 → 13.5 GB after the second prompt). Each growth of the set takes 192-319 ms.mainagainstmain+ #3527 + this PR: 7 of 7 identical, character for character (5 short prompts, a 6,468-token prompt read in 4 chunks, its second turn).Failed:
Not run:
STRATA_METAL_STREAM=1) as a whole; its buffers are allocated once and are not freed.Limits
Context::allocated()still counts every allocation since start (unchanged; it never subtracted freed bytes).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.