[PR #3757] [Engine] Metal: batch slots (--parallel) on the Mac engine #4768

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3757
Author: @khaterdev
Created: 10/22/2026
Status: 🔄 Open

Base: main ← Head: metal-batch-slots


📝 Commits (1)

  • d1aeb50 [Engine] Metal: batch slots (--parallel) on the Mac engine

📊 Changes

4 files changed (+290 additions, -4 deletions)

View changed files

📝 docs/BATCHING.md (+5 -0)
📝 docs/MAC.md (+13 -1)
📝 metal/src/engine/window.cpp (+195 -1)
📝 src/program/generate.cpp (+77 -2)

📄 Description

Problem and result

The Mac engine served one request at a time: --batch (the config's "parallel") was not ported, so a second client waited for the first client's whole answer. With two coding-agent sessions on one Mac, a long reply in one left the other stuck until it ended.

After this change "parallel": N (or strata launch --parallel N) runs on the Metal engine. Metal has no co-decode, so the slots time-share the one decode window and the gain is concurrency, not throughput: a request that arrives while another is answering gets its first token after the next batch window instead of after the other's whole answer, and each slot decodes exactly what it would alone. Off unless parallel is set (as on a PC); a Mac that does not set it runs the same code as before (every branch is under STRATA_GPU_METAL and o.batch > 0).

None. Opened directly, as a draft: see "Not run". Independent of #3756 (both touch window.cpp and MAC.md at different places and merge in either order).

Approach

  • metal/src/engine/window.cpp: a SlotStore per slot holds a copy of the window's persistent state (the GDN recurrences and conv histories, the PLE history, each QSA layer's K/V, scales, indexer tail and pooled keys) plus hp_ and the n-gram window. slot_step brings a slot's state into the live buffers if it is not there, runs the window's own run(1 token) + commit(1), and marks it dirty; the state is written back only when something else takes the window (leave_slot() at the solo window_run, window_reset, window_checkpoint_restore and the prompt read), so consecutive steps of one slot copy nothing. The positional buffers are copied up to the conversation's extent plus a margin (cells past it are rewritten before they are read, as commit() already relies on). slot_capture runs a pending lazy fold first (a solo spec window's), so a stored state never depends on the scratch inputs. The dispatch cache is untouched: it binds the live buffers by their stable handles; only the bytes behind them change. batch_fit() once the window is up: as many slots as fit in what cudaMemGetInfo reports free with 1 GiB kept, else batching off, said in the log. STRATA_METAL_BATCH_TRACE=1 prints admissions, restores and steps.
  • src/program/generate.cpp, under STRATA_GPU_METAL: the slot allocation keeps empty SessionState placeholders (the window owns the slots), the slot-session init_slots loop is skipped, copy_to_slot is the window's admission (the live state the solo path just read), copy_from_slot restores the slot's state and, with a turn checkpoint, that checkpoint's running state (conversation_checkpoint_restore, as the CUDA path), and the batch window steps the active slots one after the other instead of run_slot_rows + commit_slots. batch_mtp is off on Metal (one token per slot per window; the solo path keeps its drafts, so a request alone is not put in a slot, as on a PC).
  • Docs: MAC.md (the limit replaced by what it does and costs; the trace line), BATCHING.md (the Mac notes).

Compatibility: no new flag, config key or protocol line; the CUDA and HIP code is unchanged. Sampling in slots: the Mac engine decodes greedily on every path (sample_tokens is a stub on Metal), so slots do too; set_slot_sampling is a no-op there, as before.

Validation

Environment: MacBook Pro M1 Max, 64 GB, macOS 26.5.2; Apple clang 21.0.0, metal-cpp_26, llama.cpp 3cf0325; Qwen3.8-Flash-Next GSQ-RCO Q2_0 with --spec 2 --mtp, 32K context, STRATA_METAL_STREAM=0. Revision d1aeb50c, ninja -C build-metal strata after cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=... -DSTRATA_GGML_DIR=.... The two scripts are the ones BATCHING.md names for the PC engines, unchanged.

Passed:

  • python3 tools/batch_test.py --exe build-metal/strata --config strata-q2_0.json --batch 2 --n 2 --max-new 64 --mt-min "" --extra "--pcie-frac 0 --adapt-every 1000000" -> slot 0: 64 tokens, solo 64: IDENTICAL, slot 1: 64 tokens, solo 64: IDENTICAL; batch: 2 slots, 128 tokens; admissions 1.3s; then 6.8s -> aggregate 15.8 tok/s overall, 17.1 tok/s from the first batch token (the solo runs decoded 64 tokens in 2.8-3.0 s with drafts).
  • python3 tools/batch_interleave_test.py --exe build-metal/strata --config strata-q2_0.json --max-new 100 --long 2000 --yield skip --extra "--pcie-frac 0 --adapt-every 1000000 --no-prefill-borrow" -> exit 0; every comparison IDENTICAL: the three slots (A, B, the 1,983-token C) against solo; A's next turn from its slot; E admitted beside D's read; D read whole; A back on the solo path after 52 slot tokens; F (A's history without the reply's thinking) continued from the slot's turn checkpoint; 7b (F2 after 8 unrelated GENs, from the slot). --yield skip because a Mac reads a prompt in one piece (below).
  • The engine's fit line at start: --batch 3: 3 slots, each a 0.55 GiB copy of the window's state (42.87 GiB free)
  • Two HTTP clients on serve.server with "parallel": 2: a request sent 4 s into another's 220-token answer got its first token after 1.1 s and finished in 1.5 s while the first kept decoding (218 tokens in 11.1 s); each answer equal to the same prompt alone.

Failed:

  • None observed on the submitted revision. (An earlier revision failed the interleave test's turn-checkpoint steps, F and 7b: copy_from_slot ignored the checkpoint and continued from the slot's end state. Fixed: the checkpoint's running state is restored.)

Not run:

  • CUDA, HIP and SYCL builds of src/program/generate.cpp: the changes there are #if defined(STRATA_GPU_METAL) branches and one #if !defined around the existing slot-session init, but I have no PC to build on. That is why this is a draft; a build on one of them would settle it.
  • --yield (BYIELD, a prompt read giving way at a chunk boundary): the Metal prompt path reads a prompt in one call, so it cannot give way; the interleave test ran with --yield skip.
  • Pictures in slots, --batch-mtp (off on Metal), more than 2 slots (3 were used by the interleave test; 8 were not), a context above 32K with slots.

Limits

  • Concurrency, not throughput: the weights are read from the mapped model every step either way. Measured here, two slots together decoded at 15.8-17.1 tok/s against a solo request's ~22 tok/s with drafts (a slot has none).
  • A slot's state copy costs memory by the context: about 0.55 GiB at 32K, 1 GiB at 64K, 1.9 GiB at 128K, 3.6 GiB at 256K (the K/V and pooled keys are copied up to the conversation's extent, the GDN state in full, ~120 MB). At 256K on a 64 GB Mac not two slots fit; the engine says so and runs one at a time.
  • Switching between slots costs the GDN copy twice (~120 MB each way) plus the conversation's K/V; per-slot GDN buffers bound by handle would remove it (the dispatch cache would then need a key per slot). Not done here.
  • A prompt is read in one piece, so slots pause for the read (no BYIELD).

🔄 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/3757 **Author:** [@khaterdev](https://github.com/khaterdev) **Created:** 10/22/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `metal-batch-slots` --- ### 📝 Commits (1) - [`d1aeb50`](https://github.com/Niko1221/Strata/commit/d1aeb50c79a4a5240de5643c971fce71cc1e9826) [Engine] Metal: batch slots (--parallel) on the Mac engine ### 📊 Changes **4 files changed** (+290 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `docs/BATCHING.md` (+5 -0) 📝 `docs/MAC.md` (+13 -1) 📝 `metal/src/engine/window.cpp` (+195 -1) 📝 `src/program/generate.cpp` (+77 -2) </details> ### 📄 Description ## Problem and result The Mac engine served one request at a time: `--batch` (the config's `"parallel"`) was not ported, so a second client waited for the first client's whole answer. With two coding-agent sessions on one Mac, a long reply in one left the other stuck until it ended. After this change `"parallel": N` (or `strata launch --parallel N`) runs on the Metal engine. Metal has no co-decode, so the slots time-share the one decode window and the gain is concurrency, not throughput: a request that arrives while another is answering gets its first token after the next batch window instead of after the other's whole answer, and each slot decodes exactly what it would alone. Off unless `parallel` is set (as on a PC); a Mac that does not set it runs the same code as before (every branch is under `STRATA_GPU_METAL` and `o.batch > 0`). ## Related discussion None. Opened directly, as a draft: see "Not run". Independent of #3756 (both touch `window.cpp` and `MAC.md` at different places and merge in either order). ## Approach - `metal/src/engine/window.cpp`: a `SlotStore` per slot holds a copy of the window's persistent state (the GDN recurrences and conv histories, the PLE history, each QSA layer's K/V, scales, indexer tail and pooled keys) plus `hp_` and the n-gram window. `slot_step` brings a slot's state into the live buffers if it is not there, runs the window's own `run(1 token) + commit(1)`, and marks it dirty; the state is written back only when something else takes the window (`leave_slot()` at the solo `window_run`, `window_reset`, `window_checkpoint_restore` and the prompt read), so consecutive steps of one slot copy nothing. The positional buffers are copied up to the conversation's extent plus a margin (cells past it are rewritten before they are read, as `commit()` already relies on). `slot_capture` runs a pending lazy fold first (a solo spec window's), so a stored state never depends on the scratch inputs. The dispatch cache is untouched: it binds the live buffers by their stable handles; only the bytes behind them change. `batch_fit()` once the window is up: as many slots as fit in what `cudaMemGetInfo` reports free with 1 GiB kept, else batching off, said in the log. `STRATA_METAL_BATCH_TRACE=1` prints admissions, restores and steps. - `src/program/generate.cpp`, under `STRATA_GPU_METAL`: the slot allocation keeps empty `SessionState` placeholders (the window owns the slots), the slot-session `init_slots` loop is skipped, `copy_to_slot` is the window's admission (the live state the solo path just read), `copy_from_slot` restores the slot's state and, with a turn checkpoint, that checkpoint's running state (`conversation_checkpoint_restore`, as the CUDA path), and the batch window steps the active slots one after the other instead of `run_slot_rows` + `commit_slots`. `batch_mtp` is off on Metal (one token per slot per window; the solo path keeps its drafts, so a request alone is not put in a slot, as on a PC). - Docs: `MAC.md` (the limit replaced by what it does and costs; the trace line), `BATCHING.md` (the Mac notes). Compatibility: no new flag, config key or protocol line; the CUDA and HIP code is unchanged. Sampling in slots: the Mac engine decodes greedily on every path (`sample_tokens` is a stub on Metal), so slots do too; `set_slot_sampling` is a no-op there, as before. ## Validation Environment: MacBook Pro M1 Max, 64 GB, macOS 26.5.2; Apple clang 21.0.0, metal-cpp_26, llama.cpp 3cf0325; Qwen3.8-Flash-Next GSQ-RCO Q2_0 with `--spec 2 --mtp`, 32K context, `STRATA_METAL_STREAM=0`. Revision d1aeb50c, `ninja -C build-metal strata` after `cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=... -DSTRATA_GGML_DIR=...`. The two scripts are the ones `BATCHING.md` names for the PC engines, unchanged. **Passed:** - `python3 tools/batch_test.py --exe build-metal/strata --config strata-q2_0.json --batch 2 --n 2 --max-new 64 --mt-min "" --extra "--pcie-frac 0 --adapt-every 1000000"` -> `slot 0: 64 tokens, solo 64: IDENTICAL`, `slot 1: 64 tokens, solo 64: IDENTICAL`; `batch: 2 slots, 128 tokens; admissions 1.3s; then 6.8s -> aggregate 15.8 tok/s overall, 17.1 tok/s from the first batch token` (the solo runs decoded 64 tokens in 2.8-3.0 s with drafts). - `python3 tools/batch_interleave_test.py --exe build-metal/strata --config strata-q2_0.json --max-new 100 --long 2000 --yield skip --extra "--pcie-frac 0 --adapt-every 1000000 --no-prefill-borrow"` -> exit 0; every comparison IDENTICAL: the three slots (A, B, the 1,983-token C) against solo; A's next turn from its slot; E admitted beside D's read; D read whole; A back on the solo path after 52 slot tokens; F (A's history without the reply's thinking) continued from the slot's turn checkpoint; 7b (F2 after 8 unrelated GENs, from the slot). `--yield skip` because a Mac reads a prompt in one piece (below). - The engine's fit line at start: `--batch 3: 3 slots, each a 0.55 GiB copy of the window's state (42.87 GiB free)` - Two HTTP clients on `serve.server` with `"parallel": 2`: a request sent 4 s into another's 220-token answer got its first token after 1.1 s and finished in 1.5 s while the first kept decoding (218 tokens in 11.1 s); each answer equal to the same prompt alone. **Failed:** - None observed on the submitted revision. (An earlier revision failed the interleave test's turn-checkpoint steps, F and 7b: `copy_from_slot` ignored the checkpoint and continued from the slot's end state. Fixed: the checkpoint's running state is restored.) **Not run:** - CUDA, HIP and SYCL builds of `src/program/generate.cpp`: the changes there are `#if defined(STRATA_GPU_METAL)` branches and one `#if !defined` around the existing slot-session init, but I have no PC to build on. That is why this is a draft; a build on one of them would settle it. - `--yield` (`BYIELD`, a prompt read giving way at a chunk boundary): the Metal prompt path reads a prompt in one call, so it cannot give way; the interleave test ran with `--yield skip`. - Pictures in slots, `--batch-mtp` (off on Metal), more than 2 slots (3 were used by the interleave test; 8 were not), a context above 32K with slots. ## Limits - Concurrency, not throughput: the weights are read from the mapped model every step either way. Measured here, two slots together decoded at 15.8-17.1 tok/s against a solo request's ~22 tok/s with drafts (a slot has none). - A slot's state copy costs memory by the context: about 0.55 GiB at 32K, 1 GiB at 64K, 1.9 GiB at 128K, 3.6 GiB at 256K (the K/V and pooled keys are copied up to the conversation's extent, the GDN state in full, ~120 MB). At 256K on a 64 GB Mac not two slots fit; the engine says so and runs one at a time. - Switching between slots costs the GDN copy twice (~120 MB each way) plus the conversation's K/V; per-slot GDN buffers bound by handle would remove it (the dispatch cache would then need a key per slot). Not done here. - A prompt is read in one piece, so slots pause for the read (no `BYIELD`). --- <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#4768
No description provided.