mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3564] [Engine] Metal: working session cache, and the 12 GiB its arena was reserving for nothing #4692
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#4692
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/3564
Author: @drumblund
Created: 10/21/2026
Status: 🔄 Open
Base:
main← Head:metal-cache-only📝 Commits (4)
980b72cHost memory: read the available RAM on macOS3b6018aSessions: a QsaState names its own K/V page granule62fad48Metal: publish the decode window's K/V into the session's QsaState descriptors9613f82Metal: leave the QSA K/V pools and the rope table to the backend📊 Changes
6 files changed (+164 additions, -13 deletions)
View changed files
📝
include/strata/core/layer.hpp(+6 -0)📝
metal/src/engine/window.cpp(+89 -4)📝
src/core/conversation_memory.cpp(+17 -0)📝
src/core/conversation_snapshot.cpp(+10 -3)📝
src/core/layer.cpp(+22 -5)📝
src/core/mtp.cpp(+20 -1)📄 Description
Problem and result
Three things were broken on macOS/Metal, and they compound: the session cache could not be used at all.
1. The memory guard could not read the RAM.
sample_host_memory()had a Windows branch and a/procbranch, nothing else. On a Mac it returned nothing, so
conversation_memory_admit()said no and both--conversation-cache-mib(RAM parking) and session save/restore were refused outright:2. The Metal backend never published its K/V. Parking and session files read
QsaStatedescriptorscarved from an arena the Metal decode window never writes; its K/V lives in
Window::kq_/vq_/ksc_/vsc_.The descriptor's running-state sizes did not even match, so a SAVE died before writing a byte:
Had the sizes matched, the failure would have been worse than loud: the arena memory exists and reads
fine, it is simply all zeros, so the file would have saved successfully and restored into nothing.
3. 12 GiB of VRAM was reserved for a copy nothing used. The arena carved a full fp16 K/V pool per QSA
layer (1 GiB × 12 at 512K) plus a 128 MiB rope table that is already a no-op on this backend.
After, on the same machine (M5 Max, 128 GB, 512K context, IQ3_S):
the available RAM cannot be readlimit 128.0 GiB, available 75.3 GiBinvalid checkpoint running-state payloadcached=26,987of 26,992VRAM free with everything loadedThe answer after a restore matches the one before it character for character, and matches the value
recomputed independently from the generated corpus (so it is the K/V being read, not a re-prefill
landing on the same greedy output).
Related discussion
none — no issue was opened for this. Every behaviour change is behind
#if defined(STRATA_GPU_METAL)ora field that defaults to the current value, so CUDA/SYCL/HIP are byte-for-byte unchanged.
Approach
A backend says what it owns, instead of the snapshot code assuming.
QsaState::page_size(0 = theartifact's granule, so every existing backend is byte-for-byte unchanged) lets a backend say its pools are
cell-major. The Metal window sets it to 1, and at a granule of one cell
[page][kv_head][page_size][head_dim]and
[cell][kv_head][head_dim]are the same bytes in the same order — so parking, session files and thestate hash read the real K/V through the existing code path, with no second copy of any of it. That is the
whole trick;
Window::bind_sessionpublishes the pointers once, atwindow_init, before the first request.Two running-state fields were not in the shared format. The indexer tail was saved as four rows per
layer where the arena keeps three (
idx_block - 1— the cell that completes a block is consumed by thepool instead of being kept, exactly as
src/kernels/cuda/qsa.cunotes), andblock_poswas clearedbecause this backend derives it from the cell count and keeps no copy; it is reconstructed from the same
count. The draft layer's descriptor was never initialised at all on this backend, so
bind_session'ssibling binds the MTP window's own buffers, and
MtpDrafter::loadbrings the window up (idempotent) so aRESTORE that arrives before the first prefill has a descriptor to size the file's draft part against.
The pools are skipped at compile time, not at run time. With the descriptors published, the arena's
copy is a second set that nothing writes and nothing reads;
qsa_pools_in_arenais aconstexprfalse onMetal only, applied to
qsa_state_bytes()andqsa_state_init()in pairs so offsets stay aligned.Compatibility: CUDA/SYCL/HIP builds keep
page_size = 0, so their layout, sizes and file format areunchanged. Session files written on Metal declare
page_size = 1and a CUDA engine refuses them withincompatible K/V geometryrather than misreading them.Validation
Passed:
Environment for all of the below: macOS 26.5.2, M5 Max 128 GB, Xcode 26.6, engine
0.1.44.1-metal,IQ3_S 79 GB,
cmake --build build-metal(incremental, ~5 s per rebuild).the available RAM cannot be read; ... session save/restore are refused,after
limit 128.0 GiB (source: meminfo), available 60.4 → 75.3 GiB.saved 291,371,100 B in 525 ms,restored 3,617 tokens in 77 ms,cached=3,548, output identical.parked 27,023 tokens in 120.7 ms,saved 651,131,596 B in 223.3 ms,restored 27,174 tokens in 146.0 ms (read+check 115.1),cached=26,987, output identical and equal to the value recomputed from the corpus.VRAM free with everything loaded: 30,227 → 42,643 MiB (+12,416), matching the computed131,072 × 2 × 4 × 256 × 2 × 2 B × 12 layers + 128 MiBto the MiB.main(9de04cd4):cmake -S metal -B build-check-metal -DMETAL_CPP_DIR=…then
ninja strata→strata 0.1.46-metal, 4,542,424 B, withbind_session/mtp_kv_statepresent(
nm). This is the Metal project only; the engine sources that this PR touches are the ones compiled.Failed: none observed.
Not run:
#if defined(STRATA_GPU_METAL)andpage_sizedefaults to 0, but I could not compile those targets.strata_kernels_cputarget on arm64 (a cleancmake -S .stops at-mavx512f…on
arm64-apple-darwin). Unrelated to this PR -metal/is its own CMake project and is what was built.0.1.46-metalbinary: the measurements below are from thesame four commits built into
0.1.44.1-metal, the engine this machine serves from. Upstream's onlyMetal change between the two is
qsa_prompt.metal(an M1 Max thread-group fix).Limits
page_size = 1; a CUDA enginerefuses them (
incompatible K/V geometry). That is the intended behaviour, not a bug to fix here.idx_pooled's moving spare row is not written by this backend. Metal scores fromidx_dead; therow may therefore hold a stale value in a Metal file. Neither backend's scoring reads it, but a future
caller that does would need it.
process, unrelated to the restore (a second identical request is 0.5 s). Worth knowing before reading a
wall-clock number as the restore cost.
--conversation-cache-mibis a ceiling; a 280K-token conversationis ~4.5 GB (
236 MB + tokens × 15.3 KB, measured at two sizes). On a memory-tight machine, lower theceiling before anything else.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.