mirror of
https://github.com/mw00/project-maya.git
synced 2026-12-03 21:08:26 +07:00
[PR #86] [MERGED] GLM KV streaming: the DSA latent cache in RAM, a window of it in VRAM (--kv-streaming on) #325
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
mw00/project-maya#325
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/mw00/project-maya/pull/86
Author: @needmorevram
Created: 10/10/2026
Status: ✅ Merged
Merged: 10/13/2026
Merged by: @mw00
Base:
main← Head:glm-kv-stream📝 Commits (5)
4082581GLM KV streaming: each DSA layer's latent cache lives in pinned RAM and a window of it in VRAM (--kv-streaming on|off, --kv-resident N, default 32768 positions a layer), so the VRAM the rest took holds expertsf81fa6dKV streaming: the pinning error names --kv-streaming off (the config's switch) instead of --kv-resident0a7413eKV streaming on main's new paths: the DFlash2 verify window and several conversations at once (--parallel) - each conversation has its own host copy and residency maps (swapped with its state arena, its arena laid out with the VRAM slots as load_pack lays the first); the token path, the verify window and the rows share two hooks (where dsa_prep writes, which cells the attention reads); the prompt path's staging copy is sized once, for the first conversation; docs/SETTINGS.mdde2075dKV streaming: a prompt's stage holds the positions up to its end, not the whole context4178b3edocs: where KV streaming pays off - long contexts and several conversations on 1-3 GPUs📊 Changes
22 files changed (+989 additions, -78 deletions)
View changed files
📝
CMakeLists.txt(+7 -0)📝
docs/SETTINGS.md(+1 -0)📝
include/strata/core/glm_model.hpp(+34 -4)📝
include/strata/kernels/glm_batch.hpp(+3 -0)📝
include/strata/kernels/glm_fast.hpp(+3 -0)➕
include/strata/kernels/glm_kv_stream.hpp(+43 -0)📝
maya.py(+9 -2)📝
serve/server.py(+1 -0)📝
serve/web/app.js(+6 -2)📝
src/core/glm_fast_path.cu(+30 -18)📝
src/core/glm_fast_state.hpp(+41 -0)📝
src/core/glm_model.cu(+155 -5)📝
src/core/glm_multi.cu(+14 -2)📝
src/core/glm_prefill.cu(+109 -15)📝
src/core/glm_rows.cu(+7 -3)📝
src/core/glm_window.cu(+8 -6)📝
src/kernels/cuda/glm_batch.cu(+19 -10)📝
src/kernels/cuda/glm_fast.cu(+13 -4)➕
src/kernels/cuda/glm_kv_stream.cu(+143 -0)➕
src/kernels/glm_kv_stream_parity.cpp(+279 -0)...and 2 more files
📄 Description
KV streaming for the GLM engine, as Strata's: each DSA layer's latent cache lives in pinned RAM and only a window of it (32768 positions a layer by default) stays in VRAM, so the VRAM the rest of the cache took holds experts instead - and the prompt path can borrow it.
Where it pays off
On 1-3 GPUs, where most of Maya-L's experts live in RAM, at long contexts and with several conversations at once. The cache costs 1,152 bytes a position and DSA layer in VRAM without streaming (11 DSA layers: 1.6 GB at 128K, 6.6 GB at 512K, 13.3 GB at 1M); with it, 128 bytes (the indexer's pools) plus the fixed window.
This PC: RTX 3090 (GPU 0) + RTX 5060 Tis, PCIe 3.0 x8 each, 2x Xeon Gold 6152, 183 GB RAM. Maya-L with the DFlash2 drafter (
--dflash),--prefill auto, the engine's own split; one start with--kv-streaming offand one withoneach, the same build, the same request sequence (3 short questions with 256-token answers, 8K / 8K / 8K-continued / 32K prompts with 128, 100K+ prompts with 256).--prefill 32768(the setup's tip with 96+ GB RAM)--prefill autooff), decode +4%--parallel 4--parallel 4Why decode moves less than the prompts: on this PC the CPU lane computes 60-85% of a token's experts (40 threads, ~0.2 ms an expert), and Maya-L's routing is nearly flat (the VRAM hit rate follows the share of experts held), so 5-10 GB more VRAM is 4-8% more of 138 GB of experts. The prompt path is different: it borrows its chunk from the expert pool, and without streaming a long context leaves the pool too little to lend (one GPU at 512K: 768-token chunks, each streaming every expert). Three GPUs share one CPU pool and a pipelined window, where the freed VRAM shows in decode too. At 128K with one conversation it changes little (below).
How it works
Rebased on main
f4d13e1(v1.1.4 + #145).glm_kv_resolve(one block on the device, no host round trip) makes every 4-position block it names resident: hits get a reference bit, misses take a victim slot found by a clock sweep that never takes a slot this token uses, and a copy kernel reads the missed blocks from the host copy over PCIe. It also rewrites the selection to slot rows, so the attention kernels are unchanged.dsa_prepkernels write the host copy always and the slot only when its block is resident, so slots never go stale (rejected drafts, snapshot restores and conversation slots need no invalidation).STRATA_GLM_KV_PREFETCH=0: on the compute stream).reset(on the fast path's stream, as #133's) and a loaded slot evict every block. The layer split's search prices the smaller state and the staging copy. The NextN draft block streams like the trunk's DSA layers.--parallel: the token path, the verify window and the per-conversation rows share two hooks (wheredsa_prepwrites; which cells the attention reads - the window resolves each token's blocks right before that token's attention). Every conversation of--parallelhas its own host copy and residency maps, swapped with its state arena.Settings
"--kv-streaming", "on"into every config;"off"there turns it off, and a setup again keeps what the config says.--kv-resident Nsets the window (at least 20480;STRATA_GLM_KV_RESIDENTfrom the environment). A context of N positions or fewer is not streamed.--kv int8; each--parallelconversation has its own.docs/SETTINGS.md: the row now says where it pays off (the numbers above).Testing
glm_kv_stream_parity(new, in ctest): a synthetic sequence written by the engine's own decode and prompt writers into a resident cache and into a streamed one with too few slots (constant evictions). 686 attention calls compare bitwise equal for FP16 and INT8; the residency map and the slot contents are checked against the host copy throughout. A deliberately broken remap fails it (1,087 failures).glm|kv_stream14/14; Python serve 156 (3 skipped), tools 250.f4d13e1. The rebased head repeated the one-GPU 512Konstart up to the 100K prompt: the same layout (1,176 slots, 13.1 GB of experts in VRAM), prompts 384 / 423 / 702 / 834 tok/s against 383 / 426 / 706 / 836 before, decode 17.3 against 16.9 tok/s over the same requests.--parallelwith streaming (the engine'sSTRATA_GLM_MULTI_BENCH, two conversations with 40,000-token prompts, a 20,480-position window so both stream): sequence 0's 128 tokens are the same alone and with the other one decoding beside it, on one GPU (the rows path) and on two (pipelined); with both on the same prompt, sequence 1 - its own host copy and maps - writes sequence 0's tokens exactly (128 of 128).STRATA_GLM_PROMOTE=0, CPU lane off,--prefill 4096, a fixed split): decode with streaming gives the same tokens as without it, FP16 and INT8, speculative decoding included; with the prefetch on and off, every answer is token-identical after 8K, 32K and 100K prompts and after a conversation is loaded back from disk. Answers after long prompts fork from the non-streamed engine after a few dozen tokens; a control without streaming that only changes which experts fit in VRAM (STRATA_GLM_RESERVE_MB) forks the same way - the prompt path's placement sensitivity, not the streaming./api/contextreports the streamed cache.At 128K with one conversation
--dflash,STRATA_GLM_PCIE_SHARE=0.00): slots 10,531 -> 10,591, decode 44.14 -> 43.28 tok/s (-1.9%, the spread between starts), draft acceptance 44.9% / 44.6%, prompts within ~2%.Remaining
kv_stream.cualready uses on HIP; an AMD build and a short run are still needed.cudaHostAlloced (pinned, mapped) as Strata's is; under WSL the driver pins only about 1 GB, so there a context above 32K fails to start with streaming on (the error names--kv-streaming off)."--kv-streaming", "on"until the setup runs again.--contextor the dashboard's slider. Offering them in the setup is a separate decision (RAM: ~12 KB a token).STRATA_GLM_SPEC_CANCEL(#145, opt-in) was not run with streaming: a cancelled pass's skippeddsa_prepleaves a host row the redo writes, and its resolve sees the previous selection's cells - harmless by design, untested.dump_state) write a streamed layer's VRAM slots, not its cache in position order.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.