[PR #3168] [CLOSED] [Engine] kv: size the resident ring from the run's depth (opt-in, STRATA_KV_RING_ADAPT) #4488

Closed
opened 2026-11-07 15:36:23 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3168
Author: @xyzzing
Created: 10/19/2026
Status: ❌ Closed

Base: main ← Head: v4-kv-ring-adapt


📝 Commits (1)

  • 07bf6c1 kv: size the resident ring from the run's depth (opt-in, STRATA_KV_RING_ADAPT)

📊 Changes

3 files changed (+29 additions, -0 deletions)

View changed files

📝 include/strata/core/layer.hpp (+3 -0)
📝 src/core/layer.cpp (+15 -0)
📝 src/program/generate.cpp (+11 -0)

📄 Description

Problem and result

The resident K/V ring is one global value (--kv-resident), and because the KV pool is sized by it (the pool is the ring), a ring chosen for depth is paid by every run. A flat raise is a measured loss at shallow depth — on 0.2.0, ring 32,768 → 49,152 at 1K costs −40 % of the MTP path (acceptance 207/207 → 149–156/215) and at 32K −9.1 % with MTP against −1.7 % without: the draft path pays for a ring it cannot use, not attention.

This PR sizes the ring from the run's depth, and only grows it when the ring can cover the whole context:

needed = prompt + max_new + one page
ring   = (needed >= cap) ? cap : 32768     // 32768 is the engine's historical default
cap    = --kv-resident

Opt-in (STRATA_KV_RING_ADAPT), default behaviour unchanged. A shallow run keeps exactly today's ring; a deep one grows into the cap.

  • #2450 — the "keep the K/V in VRAM when it fits" setup rule this extends to the case where it does not quite fit.
  • #2630 — the streamed page pool's eviction policy; ring size is the first-order lever next to it on this card.
  • #3166 — the shallow-depth drafter/ring interaction this avoids by keeping the floor at the default ring.

Approach

  • qsa_kv_ring_adapt(g, cells_needed, cap) in src/core/layer.cpp (+ declaration in layer.hpp): rounds the needed cells up to a page, floors at max(qsa_kv_resident_min(), 32768), and returns the floor unless needed >= cap.
  • The hook in src/program/generate.cpp runs before the session arena is sized and the graphs are captured, because qsa_state_bytes → kv_pool_bytes sizes the pool by slots.
  • The floor is the engine's historical default ring on purpose: with the allocator minimum (20,480) an earlier version measured −7.3 % at 1K.

Validation

Passed (0.2.0-beta base, same binary both arms, interleaved pairs, 256 generated tokens):

  • 32K: control 82.2 → 82.3 tok/s (+0.1 %) — the depth-only rule had lost 9.1 % here; the ring stays at 32,768, which the arm's own log line confirms (KV ring adaptive: 49152 -> 32768).
  • 128K: 70.7 → 81.5 tok/s (+15.3 %), prefill 2,391/2,375 and TTFT 55.1/55.4 s unchanged.
  • Against the live v0.1.45 binary: decode +7.5 % @32K, +9.0 % @64K, +8.6 % @128K (5 pairs at 128K, tight: prefill 2,445–2,459 vs 2,382–2,400; TTFT 53.6–53.9 vs 54.9–55.3 s), prefill −0.9/−1.4/−2.4 %, TTFT +0.7/+1.3/+2.5 %. This is a trade the maintainers may or may not want; it is stated rather than hidden.
  • 1K: −7.7 % with the cap. This is a same-ring comparison — at 1K both arms print 32,768 — and the difference tracks the MTP acceptance state (207 vs 197 accepted). I do not claim it as a gain or a regression.

Failed: none observed.

Not run:

  • CUDA and SYCL builds (no such toolkit on this machine). The patch is backend-neutral core code, but I have not compiled it for those backends.
  • The --serve path: serve keeps one main session plus parked snapshots, so there is no per-request session to size a ring for; per-request sizing there needs per-conversation KV pools, noted as a follow-up, not this patch.

Limits

  • Opt-in and generate-path only.
  • The 32,768 floor is a constant (the engine's default ring), not a configuration knob.
  • Because the pool is the ring, the ring cannot be resized within a session — only chosen when the session's arena is built.
  • The 64K point used a workload derived as the first 65,536 ids of a synthetic 128K prompt; provenance is in the measurement record.

Measurement notes

One process a cell, one-shot greedy, 256 generated tokens; the intervals are the MTP acceptance state (cells moved ±8 %), so only paired medians are used. Raw rows: 32K control 74.0/82.4/…, 128K control 70.7/64.5/…, cap arm 81.5/84.2/… — the full tables are in the campaign records; I can attach a sanitized dump if useful.


🔄 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/3168 **Author:** [@xyzzing](https://github.com/xyzzing) **Created:** 10/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `v4-kv-ring-adapt` --- ### 📝 Commits (1) - [`07bf6c1`](https://github.com/Niko1221/Strata/commit/07bf6c1dba515846493f40cd5f8bd3dd9012e3bf) kv: size the resident ring from the run's depth (opt-in, STRATA_KV_RING_ADAPT) ### 📊 Changes **3 files changed** (+29 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/layer.hpp` (+3 -0) 📝 `src/core/layer.cpp` (+15 -0) 📝 `src/program/generate.cpp` (+11 -0) </details> ### 📄 Description ## Problem and result The resident K/V ring is one global value (`--kv-resident`), and because the KV pool is sized by it (the pool *is* the ring), a ring chosen for depth is paid by every run. A flat raise is a measured loss at shallow depth — on 0.2.0, ring 32,768 → 49,152 at 1K costs **−40 %** of the MTP path (acceptance 207/207 → 149–156/215) and at 32K **−9.1 %** with MTP against **−1.7 %** without: the draft path pays for a ring it cannot use, not attention. This PR sizes the ring from the run's depth, and only grows it when the ring can cover the whole context: ``` needed = prompt + max_new + one page ring = (needed >= cap) ? cap : 32768 // 32768 is the engine's historical default cap = --kv-resident ``` Opt-in (`STRATA_KV_RING_ADAPT`), default behaviour unchanged. A shallow run keeps exactly today's ring; a deep one grows into the cap. ## Related discussion - #2450 — the "keep the K/V in VRAM when it fits" setup rule this extends to the case where it does not quite fit. - #2630 — the streamed page pool's eviction policy; ring size is the first-order lever next to it on this card. - #3166 — the shallow-depth drafter/ring interaction this avoids by keeping the floor at the default ring. ## Approach - `qsa_kv_ring_adapt(g, cells_needed, cap)` in `src/core/layer.cpp` (+ declaration in `layer.hpp`): rounds the needed cells up to a page, floors at `max(qsa_kv_resident_min(), 32768)`, and **returns the floor unless `needed >= cap`**. - The hook in `src/program/generate.cpp` runs **before** the session arena is sized and the graphs are captured, because `qsa_state_bytes` → `kv_pool_bytes` sizes the pool by slots. - The floor is the engine's historical default ring on purpose: with the allocator minimum (20,480) an earlier version measured **−7.3 % at 1K**. ## Validation Passed (0.2.0-beta base, same binary both arms, interleaved pairs, 256 generated tokens): - 32K: control 82.2 → **82.3 tok/s (+0.1 %)** — the depth-only rule had lost 9.1 % here; the ring stays at 32,768, which the arm's own log line confirms (`KV ring adaptive: 49152 -> 32768`). - 128K: 70.7 → **81.5 tok/s (+15.3 %)**, prefill 2,391/2,375 and TTFT 55.1/55.4 s unchanged. - Against the live v0.1.45 binary: decode **+7.5 % @32K, +9.0 % @64K, +8.6 % @128K** (5 pairs at 128K, tight: prefill 2,445–2,459 vs 2,382–2,400; TTFT 53.6–53.9 vs 54.9–55.3 s), prefill −0.9/−1.4/−2.4 %, TTFT +0.7/+1.3/+2.5 %. This is a trade the maintainers may or may not want; it is stated rather than hidden. - 1K: **−7.7 %** with the cap. This is a **same-ring** comparison — at 1K both arms print 32,768 — and the difference tracks the MTP acceptance state (207 vs 197 accepted). I do not claim it as a gain or a regression. Failed: none observed. Not run: - CUDA and SYCL builds (no such toolkit on this machine). The patch is backend-neutral core code, but I have not compiled it for those backends. - The `--serve` path: serve keeps one main session plus parked snapshots, so there is no per-request session to size a ring for; per-request sizing there needs per-conversation KV pools, noted as a follow-up, not this patch. ## Limits - Opt-in and `generate`-path only. - The 32,768 floor is a constant (the engine's default ring), not a configuration knob. - Because the pool is the ring, the ring cannot be resized within a session — only chosen when the session's arena is built. - The 64K point used a workload derived as the first 65,536 ids of a synthetic 128K prompt; provenance is in the measurement record. ## Measurement notes One process a cell, one-shot greedy, 256 generated tokens; the intervals are the MTP acceptance state (cells moved ±8 %), so only paired medians are used. Raw rows: 32K control 74.0/82.4/…, 128K control 70.7/64.5/…, cap arm 81.5/84.2/… — the full tables are in the campaign records; I can attach a sanitized dump if useful. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:36:23 +07:00
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#4488
No description provided.