mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3527] [Engine] Metal: release old prompt scratch buffers when the workspace grows #4669
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#4669
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/3527
Author: @Elvis-Luo
Created: 10/21/2026
Status: 🔄 Open
Base:
main← Head:fix/metal-prompt-workspace-growth📝 Commits (5)
093ed30metal: release prompt scratch buffers before workspace growthb9252edMerge current main and exercise prompt growth without caller autorelease pools9f31772Merge current main (0.2.10) into the prompt workspace fixc66bc2ametal: Context::alloc and wrap drain their own autorelease pool, so free() gives the buffer back95b97fftests: the prompt workspace fixture follows main's pc_setup (nexp_, the MTP and fused-input optional buffers)📊 Changes
6 files changed (+332 additions, -42 deletions)
View changed files
📝
metal/CMakeLists.txt(+10 -0)📝
metal/src/engine/prompt_chain.inc(+15 -7)📝
metal/src/engine/window.cpp(+2 -0)📝
metal/src/mtl_rt.mm(+8 -35)➕
metal/tests/buffer_free_test.cpp(+56 -0)➕
metal/tests/test_prompt_workspace.py(+241 -0)📄 Description
Problem and result
On the Metal prompt chain,
Window::pc_setupreplaces its scratch buffers when a larger chunk arrives, and the old ones are never given back. Two separate things keep them alive:PromptBufsis reset without freeing its handles.MTLBuffer, and the engine's threads have no autorelease pool, soContext::freealone does not release the memory.This PR fixes both. On the real model, the same 11 requests end at 13.70 GiB of physical footprint instead of 20.47 GiB, with identical outputs.
main(ffa80d73)95b97ff2)Related discussion
mainhas since added the pools only underSTRATA_DS4_RUNTIME, so the Qwen path still leaks. Neither half works alone: this PR's earlier revisions showed 82 of 82 old buffers retained with only one of them.c66bc2a8), and removes the guard soallocandwrapuse the pool on every path.Approach
pc_setuprecords each scratch allocation once and frees the previous set before replacing it. Same-size and smaller requests keep their buffers and contents.Context::allocandwrapdrain their own autorelease pool.pc_chunkreturns, so replacement only happens between completed chunks.metal/tests/test_prompt_workspace.pyruns the realpc_setupagainst the real allocator, with no caller pool around allocations.Validation
Environment: Apple M4 Max, 128 GB, macOS 27.2 (26B123), Apple clang 21.0.0, metal-cpp 26, ggml from llama.cpp
3cf03257, Unsloth Qwen3.8-Flash-Next UD-Q4_K_XL.Passed:
95b97ff2, full engine target builds.metal/tests/test_prompt_workspace.pyon95b97ff2: every old buffer is deallocated on growth, libraries and capacity are preserved, the layout choice is preserved, replacement buffers work, and the footprint after the final free is 49.0 MiB.buffer_free_testonc66bc2a8: rounds 2 and 3 hold 4.37 GB and return to 0.28 GB after each free, with no growth over 4 rounds.serve/server.pyat--max-context 131072, with the footprint read fromproc_pid_rusageafter each request: the table above. All 11 request outputs and tool calls are identical betweenmainand this PR.Failed:
buffer_free_teston unmodifiedmainreaches 16.96 GB after free at round 3.test_prompt_workspace.py --source-ref origin/mainfails with "growth deallocates every old Metal buffer".mainand this PR: they answerREADY.instead ofREADY. This does not come from this PR, because the outputs are identical on both. A build with the #4141 grid fix on the samemain, without this PR, answersREADYand passes 11 of 11.Not run:
buffer_free_testcommands were not repeated on the final test-only commit95b97ff2. The lifetime test results above are from that commit;buffer_free_testis fromc66bc2a8, and95b97ff2only changes the test fixture.Limits
This frees old scratch buffers when the workspace grows. It does not shrink the high-water workspace, does not change
Context::allocated(), and does not redesign Metal buffer ownership in general.Backports of the same two fixes have also been running on v0.2.1 on this Mac; those runs are not counted as validation of this head.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.