mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3757] [Engine] Metal: batch slots (--parallel) on the Mac engine #4768
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#4768
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/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(orstrata 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 unlessparallelis set (as on a PC); a Mac that does not set it runs the same code as before (every branch is underSTRATA_GPU_METALando.batch > 0).Related discussion
None. Opened directly, as a draft: see "Not run". Independent of #3756 (both touch
window.cppandMAC.mdat different places and merge in either order).Approach
metal/src/engine/window.cpp: aSlotStoreper 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) plushp_and the n-gram window.slot_stepbrings a slot's state into the live buffers if it is not there, runs the window's ownrun(1 token) + commit(1), and marks it dirty; the state is written back only when something else takes the window (leave_slot()at the solowindow_run,window_reset,window_checkpoint_restoreand 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, ascommit()already relies on).slot_captureruns 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 whatcudaMemGetInforeports free with 1 GiB kept, else batching off, said in the log.STRATA_METAL_BATCH_TRACE=1prints admissions, restores and steps.src/program/generate.cpp, underSTRATA_GPU_METAL: the slot allocation keeps emptySessionStateplaceholders (the window owns the slots), the slot-sessioninit_slotsloop is skipped,copy_to_slotis the window's admission (the live state the solo path just read),copy_from_slotrestores 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 ofrun_slot_rows+commit_slots.batch_mtpis 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).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_tokensis a stub on Metal), so slots do too;set_slot_samplingis 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. Revisiond1aeb50c,ninja -C build-metal strataaftercmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=... -DSTRATA_GGML_DIR=.... The two scripts are the onesBATCHING.mdnames 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 skipbecause a Mac reads a prompt in one piece (below).--batch 3: 3 slots, each a 0.55 GiB copy of the window's state (42.87 GiB free)serve.serverwith"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:
copy_from_slotignored the checkpoint and continued from the slot's end state. Fixed: the checkpoint's running state is restored.)Not run:
src/program/generate.cpp: the changes there are#if defined(STRATA_GPU_METAL)branches and one#if !definedaround 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.--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
BYIELD).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.