[PR #3527] [Engine] Metal: release old prompt scratch buffers when the workspace grows #4669

Open
opened 2026-11-07 15:38:12 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • 093ed30 metal: release prompt scratch buffers before workspace growth
  • b9252ed Merge current main and exercise prompt growth without caller autorelease pools
  • 9f31772 Merge current main (0.2.10) into the prompt workspace fix
  • c66bc2a metal: Context::alloc and wrap drain their own autorelease pool, so free() gives the buffer back
  • 95b97ff tests: 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_setup replaces its scratch buffers when a larger chunk arrives, and the old ones are never given back. Two separate things keep them alive:

  1. PromptBufs is reset without freeing its handles.
  2. Metal autoreleases a reference to each new MTLBuffer, and the engine's threads have no autorelease pool, so Context::free alone 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.

After request main (ffa80d73) this PR (95b97ff2)
135-token prompt 11.09 GiB 11.12 GiB
335 tokens 12.27 GiB 11.48 GiB
585 tokens 13.90 GiB 12.14 GiB
885 tokens 15.84 GiB 12.90 GiB
985 tokens 17.95 GiB 13.31 GiB
4 images, question last 20.19 GiB 13.64 GiB
11 requests done 20.47 GiB 13.70 GiB
  • #3600 by Flo5k5 was the allocator half of this fix. Its author closed it on 2026-11-04, and main has since added the pools only under STRATA_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.
  • This PR therefore carries that commit with its author kept (c66bc2a8), and removes the guard so alloc and wrap use the pool on every path.
  • #4141 is a separate defect in the same prompt path and is not included here. See the note under Failed.

Approach

  • pc_setup records each scratch allocation once and frees the previous set before replacing it. Same-size and smaller requests keep their buffers and contents.
  • Context::alloc and wrap drain their own autorelease pool.
  • Compiled libraries, the expert-layout choice, KV and recurrent state, and model weights are untouched. Prompt submissions finish before pc_chunk returns, so replacement only happens between completed chunks.
  • metal/tests/test_prompt_workspace.py runs the real pc_setup against 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.

cmake -S metal -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DMETAL_CPP_DIR=<metal-cpp-26> -DSTRATA_GGML_DIR=<llama.cpp-3cf03257>
cmake --build build --target strata runtime_hello buffer_free_test
ctest --test-dir build --output-on-failure
build/buffer_free_test

Passed:

  • 95b97ff2, full engine target builds.
  • metal/tests/test_prompt_workspace.py on 95b97ff2: 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_test on c66bc2a8: rounds 2 and 3 hold 4.37 GB and return to 0.28 GB after each free, with no growth over 4 rounds.
  • Real model, served through serve/server.py at --max-context 131072, with the footprint read from proc_pid_rusage after each request: the table above. All 11 request outputs and tool calls are identical between main and this PR.

Failed:

  • Negative controls, as expected: buffer_free_test on unmodified main reaches 16.96 GB after free at round 3. test_prompt_workspace.py --source-ref origin/main fails with "growth deallocates every old Metal buffer".
  • On the real model, 3 of the 11 requests (135, 335 and 985 tokens) fail my exact-text check on both main and this PR: they answer READY. instead of READY. This does not come from this PR, because the outputs are identical on both. A build with the #4141 grid fix on the same main, without this PR, answers READY and passes 11 of 11.

Not run:

  • Other Macs and chips, M5 kernels, DeepSeek on Metal after removing the guard, and CUDA, HIP and SYCL (the change is in the Metal runtime only).
  • Throughput: the machine was shared with another GPU workload during these runs, so this PR makes no speed claim.
  • The ctest and buffer_free_test commands were not repeated on the final test-only commit 95b97ff2. The lifetime test results above are from that commit; buffer_free_test is from c66bc2a8, and 95b97ff2 only 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.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/3527 **Author:** [@Elvis-Luo](https://github.com/Elvis-Luo) **Created:** 10/21/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/metal-prompt-workspace-growth` --- ### 📝 Commits (5) - [`093ed30`](https://github.com/Niko1221/Strata/commit/093ed3077e8f03ae54ffc302ac0b1af37592b38e) metal: release prompt scratch buffers before workspace growth - [`b9252ed`](https://github.com/Niko1221/Strata/commit/b9252edc5368b452a530b32d017e30f8a19e107a) Merge current main and exercise prompt growth without caller autorelease pools - [`9f31772`](https://github.com/Niko1221/Strata/commit/9f31772bc326c077fe20ee5cf9d30604850c505d) Merge current main (0.2.10) into the prompt workspace fix - [`c66bc2a`](https://github.com/Niko1221/Strata/commit/c66bc2a89646f11e446a28f7b27466b56aadc533) metal: Context::alloc and wrap drain their own autorelease pool, so free() gives the buffer back - [`95b97ff`](https://github.com/Niko1221/Strata/commit/95b97ff20613a7553b29bb06654188e4a2826164) tests: 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) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 Description ## Problem and result On the Metal prompt chain, `Window::pc_setup` replaces its scratch buffers when a larger chunk arrives, and the old ones are never given back. Two separate things keep them alive: 1. `PromptBufs` is reset without freeing its handles. 2. Metal autoreleases a reference to each new `MTLBuffer`, and the engine's threads have no autorelease pool, so `Context::free` alone 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. | After request | `main` (`ffa80d73`) | this PR (`95b97ff2`) | | --- | --- | --- | | 135-token prompt | 11.09 GiB | 11.12 GiB | | 335 tokens | 12.27 GiB | 11.48 GiB | | 585 tokens | 13.90 GiB | 12.14 GiB | | 885 tokens | 15.84 GiB | 12.90 GiB | | 985 tokens | 17.95 GiB | 13.31 GiB | | 4 images, question last | 20.19 GiB | 13.64 GiB | | 11 requests done | 20.47 GiB | 13.70 GiB | ## Related discussion - #3600 by Flo5k5 was the allocator half of this fix. Its author closed it on 2026-11-04, and `main` has since added the pools only under `STRATA_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. - This PR therefore carries that commit with its author kept (`c66bc2a8`), and removes the guard so `alloc` and `wrap` use the pool on every path. - #4141 is a separate defect in the same prompt path and is not included here. See the note under **Failed**. ## Approach - `pc_setup` records each scratch allocation once and frees the previous set before replacing it. Same-size and smaller requests keep their buffers and contents. - `Context::alloc` and `wrap` drain their own autorelease pool. - Compiled libraries, the expert-layout choice, KV and recurrent state, and model weights are untouched. Prompt submissions finish before `pc_chunk` returns, so replacement only happens between completed chunks. - `metal/tests/test_prompt_workspace.py` runs the real `pc_setup` against 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. ```sh cmake -S metal -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \ -DMETAL_CPP_DIR=<metal-cpp-26> -DSTRATA_GGML_DIR=<llama.cpp-3cf03257> cmake --build build --target strata runtime_hello buffer_free_test ctest --test-dir build --output-on-failure build/buffer_free_test ``` **Passed:** - `95b97ff2`, full engine target builds. - `metal/tests/test_prompt_workspace.py` on `95b97ff2`: 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_test` on `c66bc2a8`: rounds 2 and 3 hold 4.37 GB and return to 0.28 GB after each free, with no growth over 4 rounds. - Real model, served through `serve/server.py` at `--max-context 131072`, with the footprint read from `proc_pid_rusage` after each request: the table above. All 11 request outputs and tool calls are identical between `main` and this PR. **Failed:** - Negative controls, as expected: `buffer_free_test` on unmodified `main` reaches 16.96 GB after free at round 3. `test_prompt_workspace.py --source-ref origin/main` fails with "growth deallocates every old Metal buffer". - On the real model, 3 of the 11 requests (135, 335 and 985 tokens) fail my exact-text check on both `main` and this PR: they answer `READY.` instead of `READY`. This does not come from this PR, because the outputs are identical on both. A build with the #4141 grid fix on the same `main`, without this PR, answers `READY` and passes 11 of 11. **Not run:** - Other Macs and chips, M5 kernels, DeepSeek on Metal after removing the guard, and CUDA, HIP and SYCL (the change is in the Metal runtime only). - Throughput: the machine was shared with another GPU workload during these runs, so this PR makes no speed claim. - The ctest and `buffer_free_test` commands were not repeated on the final test-only commit `95b97ff2`. The lifetime test results above are from that commit; `buffer_free_test` is from `c66bc2a8`, and `95b97ff2` only 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](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4669
No description provided.