mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1614] [Engine] shared KV pool for all lanes (--kv-pool-tokens): at 2 x 256k the first token comes 2-5x sooner and lane moves ~10x faster; off = identical to main #3564
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#3564
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/1614
Author: @noon-at-cgn
Created: 10/8/2026
Status: 🔄 Open
Base:
main← Head:pr/kv-pool-141📝 Commits (10+)
8644da2kv: one pinned KV pool shared by the sessions (--kv-pool-tokens)ed157d0kv: a conversation moves between the main session and a slot by swapping pool chunks86863fckv: two comments say what the pool-full paths do5e675e2kv: pool review fixes - drafter host copy, upload order, eviction, 503 pathsf81f46dkv pool: K8V4 halves and the session file's K/V reader carry the chunk table; a session RESTORE reserves its chunksa44e451kv pool: one chunk table across the stages of a layer split64456ebkv pool: the eviction order is a tested function; CPU test for the chunk tables83f072bkv_stream.hpp: std::size_t for_each_piece (a few parity tests include the header before )85caac1serve tests: the KV pool's 503 paths, truncated, the POOL line (serve/test_kv_pool.py)89dbfe9docs: --kv-pool-tokens (BATCHING.md, DETAILS.md, MULTI_GPU.md)📊 Changes
28 files changed (+2008 additions, -168 deletions)
View changed files
📝
CMakeLists.txt(+9 -1)📝
docs/BATCHING.md(+154 -4)📝
docs/DETAILS.md(+20 -0)📝
docs/MULTI_GPU.md(+6 -3)📝
include/strata/core/conversation_cache.hpp(+82 -8)➕
include/strata/core/kv_chunks.hpp(+60 -0)➕
include/strata/core/kv_pool.hpp(+107 -0)➕
include/strata/core/kv_pool_policy.hpp(+51 -0)📝
include/strata/core/layer.hpp(+7 -0)📝
include/strata/core/session.hpp(+7 -2)📝
include/strata/kernels/kv_stream.hpp(+46 -0)📝
serve/server.py(+53 -6)➕
serve/test_kv_pool.py(+256 -0)📝
src/core/conversation_cache_test.cpp(+87 -0)📝
src/core/conversation_snapshot.cpp(+55 -8)📝
src/core/conversation_snapshot_test.cpp(+71 -3)➕
src/core/kv_chunks.cpp(+62 -0)➕
src/core/kv_pool.cpp(+179 -0)📝
src/core/layer.cpp(+43 -34)📝
src/core/mtp.cpp(+5 -0)...and 8 more files
📄 Description
Branch
pr/kv-pool-141, head1e1cd762: 20 commits on upstream main41cf05a0(v0.2.5-6, engine 0.1.50). Opt-in: without--kv-pool-tokensthe output is byte-identical to main (identity gate below). Scope issue: #3351.Problem and result
With KV streaming (
--kv-resident), the main session and every--batchslot pin the K/V of the whole--max-contextin RAM, however short the conversation: 3.09 GiB each at 262,144 cells with--kv int8. A conversation that moves between the main session and a slot is copied, which at 256k takes seconds, and two long lanes push a RAM-limited host to its memory guard.--kv-pool-tokens Npins N cells once. Sessions hold only the 4,096-cell chunks their conversation has reached, and a move between lanes swaps chunk tables instead of copying.Two lanes of about 256k tokens (2 x RTX 3080 20 GB, UD-Q4_K_XL, same binary in both arms, only the flag differs; measured on 0.2.0-beta, engine 0.1.45.3):
moved)copied)MemAvailable)MemAvailable(mean of 3 starts each)At about 133k per lane the move took 0.1-0.25 s with the pool and 0.9-2.0 s without. With the pool a third lane (three conversations of about 104k) fits, every needle answered.
Short context is unchanged: one stream 84.5 vs 81.9 tok/s, two streams 93.7 vs 89.8 (means of three starts each, within the round-to-round spread). The gains are at long context with more than one lane.
On this PR's head (0.2.5) the pool runs the same workload end to end (2 x 256k, numbers under Validation). I did not repeat the no-pool arm on 0.2.5, so the comparison above is the 0.2.0-beta one.
Related discussion
conversation_snapshot.cpp.pick_slot): our slot-choice change is not in this PR.Approach
A session keeps a chunk table, a device copy for the kernels and a host copy for DMA and copies. Unreached chunks point at one spare "trash" chunk. Without the flag the table is the identity layout. A lane reserves chunks as it writes. When the pool is full, an idle slot's cached conversation gives way first, then a new request gets a retryable 503 (
kv_pool_full), and a decoding lane that cannot grow ends withlengthand"truncated": true. It works across a layer split: each card pins its own layers' share and a conversation uses the same chunks on every card. The MTP drafter ring is not pooled.KvHostPools::block,block_host,for_each_piece): append kernels, resolve and stage copies, verify, MTP, prefill, state hash,conversation_kv_copy,conversation_kv_restore_prefix, session files, K8V4 halves. I checked the 967 upstream commits since the last base for new host K/V sites and found none.1e1cd762; see Failed).kv_host_alloc(WSL2 fallback, pin budget).docs/BATCHING.md,docs/DETAILS.md,docs/MULTI_GPU.md.Validation
Head
1e1cd762unless a line says otherwise.ebb61958is the same series before the last two changes (the RoPE gate, and(void) cudaFreeinkv_pool.cpp; that file compiles to identical machine code either way).Passed:
41cf05a0vs this head without--kv-pool-tokens, production config otherwise (2 x RTX 3080 20 GB, layer split 23,parallel 2,--spec 4 --batch-mtp,--max-context 262144 --kv int8 --kv-resident). Greedy (temperature 0, top_k 1, seed 1234), 256 tokens, prompts of 989, 31,280 and 128,209 tokens, same bytes for both. The stock config is not reproducible even main against main (adaptive experts), so both arms ran the deterministic settings fromdocs/DETAILS.md(--adapt-every 100000 --adapt-async 0 --pcie-frac 0) withSTRATA_STATE_HASH=1. Two starts of each: all three outputs byte-identical to main's, the STATE_HASH fields equal, the expert cache at 4893 / 4161 slots like main's. Atebb61958the same held with--spec 2.--kv-pool-tokens 524288, one start):KV pool: 524288 cells shared by the sessions and the stages of the layer split, 6.24 GiB of pinned RAM. Two concurrent 256k prompts, then appended turns: 4/4 needles correct, slot moves 444-842 ms, appended-turn decode 53.0 tok/s aggregate, single-lane questions 43.5 and 50.5 tok/s, nokv_pool_full, hostMemAvailablenever below 17.2 GiB.-DSTRATA_ENABLE_CUDA=ON -DSTRATA_NATIVE_EXPERTS=ON -DSTRATA_MMQ_KQUANTS=ON): rc 0, same warning count as main.CUDA_VISIBLE_DEVICES=-1): kv_chunks_test; conversation_cache_test (4791); conversation_memory_test (31); conversation_file_test (201); conversation_validation_test (972); conversation_transfer_test (1548 host-only); session_save_reclaim_test (119); memory_guard_test (134).ctest -j1atebb61958: 161 of 168 passed, 4 skipped (CPU without AVX-512, ggml without trellis types), 3 failed, all pre-existing (Failed). At1e1cd762the pool-related subset again: 17/17 (conversation_snapshot_test with the pooled variant, kv_chunks, the kv_* parity tests, batch_rows, verify_batch_parity).ebb61958:python -m unittest discover -s serve -t .: 1050 tests, 15 skipped, OK.ebb61958(rocm/dev-ubuntu-24.04:7.2.4, AMD clang 22 / HIP 7.2, flags ofpackaging/build_hip_linux.sh):stratafor gfx1100 and for gfx1151;gfx1201, and all targets with tests on, rc 0 on the branch and on main. The branch adds one test (kv_chunks_test). The only new warning,cudaFreeresult unused inkv_pool.cpp, is fixed on this head.kv_chunks_testandconversation_cache_test(4791) pass on the host.ebb61958(oneAPI DPC++ 2026.1.1, flags ofpackaging/build_sycl_linux.sh):strataand all targets, rc 0, warnings identical to main. The SYCL tree compiles its own migrated copies and does not build the pool: of the touched files onlylayer.hpp,kv_stream.hppandconversation_cache.hppare compiled there, and they build cleanly.Failed:
ebb61958, flag off was not identical to main: the batch slots borrowed the RoPE table without the pool too, so each slot was 0.07 GiB smaller,--expert-cache autocached 43 more experts per card, and greedy output differed. With a VRAM reserve that equalised the expert count the outputs matched, which located the cause.1e1cd762borrows only with the pool; the gate above is from that head.ple_parity(its Q2_0 GGUF is not on this host),ggml_tq_overlay(cmake script "Unknown arguments", same on main),isa_guard_check(iq512x_rowsviolation, same on main's library). This branch does not touchsrc/kernels/cpu,cmakeortests/cmake.metal-linkfails inmetal/src/mtl_rt.mm(MTLGPUFamilyMetal4,MTLCompileOptions.mathMode: macOS 26 API, in main sincead9eb890) on themacos-14runner's SDK. Other PRs on current main fail the same job; this branch does not touchmetal/.Linux / runtime requirements onlyat1e1cd762: 1051 serve tests, 1 failure,test_lifeline.test_the_portable_watcher_ends_the_child_when_the_server_is_killed("the child outlived a killed server (watcher)"). It depends on how the tests are started: on the lm-server box it fails on main41cf05a0too, 5 of 5 runs from an ssh shell (python -m unittest serve.test_lifeline), while the whole serve suite above passed when run from a systemd unit. This branch does not touch the lifeline code or its test.Not run:
1e1cd762(the change sinceebb61958is 3 lines of host C++ ingenerate.cpp, which SYCL does not compile, plus the(void)cast); Metal; Windows and the WSL2cudaHostRegisterpath.1e1cd762(subset and CPU tests only); the no-pool 256k arm on 0.2.5.STRATA_SLOT_CKPT_MOVE=1with the pool;STRATA_SLOT_COPY_DIRECTandborrowend to end; session-fileSAVE/RESTOREwith a pool end to end (unit tests only); pool-full paths under load; more than 3 lanes; a single 512k conversation.All raw outputs (prompts, per-request JSON, engine logs, binaries' sha256) are kept and can be attached.
Limits
--kv-pool-tokens; pass it inargs.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.