Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

KV streaming: the draft layer keeps its KV in VRAM when no pinned RAM is left (engine 0.1.6)

Niko12218f279d82a0f9
diff --git a/include/strata/core/layer.hpp b/include/strata/core/layer.hpp
index f2271c35d..1ea792201 100644
--- a/include/strata/core/layer.hpp
+++ b/include/strata/core/layer.hpp
@@ -256,7 +256,8 @@ struct QsaState {
uint64_t qsa_state_bytes(const ModelGeometry& g, int64_t max_cells, bool with_rope = true, int64_t ring_cells = 0);
/// KV streaming: keep `cells` cells of each QSA layer in VRAM and the rest in pinned host memory (0: all in VRAM,
/// the default). Set before sizing and initializing the session; a context that fits in `cells` is not streamed.
-/// Also puts the MTP drafter's K/V in a ring of its window (`ring_cells` of qsa_state_bytes/init).
+/// Also puts the MTP drafter's K/V in a ring of its window (`ring_cells` of qsa_state_bytes/init; -1 forces a fully
+/// resident state).
void qsa_set_kv_resident(int64_t cells);
int64_t qsa_kv_resident();
/// The fewest resident cells a streamed layer may have: one verify window's selections (8 queries x 2,051 cells
diff --git a/setup.py b/setup.py
index e6b024994..66c81b7a1 100644
--- a/setup.py
+++ b/setup.py
@@ -54,7 +54,7 @@ PREBUILT_ASSET = "strata-windows-x64.zip" if WIN else "strata-linux-x64.zip"
# the CUDA libraries the ready-made engine loads (the same CUDA 13.0 it is built with), from NVIDIA's pip packages
CUDA_WHEELS = ["nvidia-cublas==13.0.2.14", "nvidia-cuda-runtime==13.0.96"]
MIN_DRIVER = 580 # CUDA 13.0
-MIN_ENGINE = (0, 1, 5) # KV streaming (--kv-resident), v0.1.5
+MIN_ENGINE = (0, 1, 6) # KV streaming (--kv-resident), v0.1.5; its drafter fallback, v0.1.6
PY_PACKAGES = ["numpy", "jinja2", "regex", "pyyaml", "tqdm", "requests", "cmake", "ninja", "pillow"]
MODELS = {
diff --git a/src/core/layer.cpp b/src/core/layer.cpp
index 29a747a04..c39d195ba 100644
--- a/src/core/layer.cpp
+++ b/src/core/layer.cpp
@@ -492,7 +492,7 @@ KvPlan kv_plan(const QsaShapes& s, int64_t max_cells, int64_t ring_cells) {
p.pages = (max_cells + s.page_size - 1) / s.page_size;
p.slots = p.pages;
p.pooled_rows = max_cells / s.idx_block + 2;
- if (g_kv_resident <= 0) return p;
+ if (g_kv_resident <= 0 || ring_cells < 0) return p; // ring_cells < 0: always fully resident
// A/B only: STRATA_KV_RING_OFF keeps the drafter fully resident, STRATA_KV_MAIN_OFF the main layers
static const bool ring_off = std::getenv("STRATA_KV_RING_OFF") != nullptr;
static const bool main_off = std::getenv("STRATA_KV_MAIN_OFF") != nullptr;
@@ -599,8 +599,11 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Q
uint8_t* h = nullptr;
uint8_t* d = nullptr;
if (cudaHostAlloc((void**) &h, bytes, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess ||
- cudaHostGetDevicePointer((void**) &d, h, 0) != cudaSuccess)
+ cudaHostGetDevicePointer((void**) &d, h, 0) != cudaSuccess) {
+ if (p.mode == 1) std::fprintf(stderr, "strata: KV streaming: cannot pin %.2f GiB of RAM for a layer's KV copy "
+ "(lower the context, or run without --kv-resident)\n", (double) bytes / 1073741824.0);
return 0;
+ }
g_kv_host_bytes += bytes;
Cursor hc{d};
if (g_kv_int8) {
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index d1b74ba35..3c7fce68c 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -158,11 +158,22 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
const strata::kernels::QsaShapes s = shapes_of(g);
const int64_t max_cells = ss.qsa_states[0].max_cells;
// KV streaming: the drafter only reads its last `window` cells, so with streaming on its K/V is a ring of the
- // window (plus the cells a round writes ahead of its queries) over a host copy, refilled on a resume
- const int64_t ring = (window > 0 && window < max_cells) ? window + 4 * (int64_t) max_t + 64 : 0;
- const uint64_t sb = qsa_state_bytes(g, max_cells, false, ring);
+ // window (plus the cells a round writes ahead of its queries) over a host copy, refilled on a resume. The host copy
+ // is pinned after the expert arena has pinned what it could: if it does not fit, the K/V stays whole in VRAM.
+ int64_t ring = (window > 0 && window < max_cells) ? window + 4 * (int64_t) max_t + 64 : 0;
+ uint64_t sb = qsa_state_bytes(g, max_cells, false, ring);
if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }
- if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[0], ring) == 0) { err = "mtp: state init failed"; return false; }
+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[0], ring) == 0) {
+ if (st_.kv_mode == 0) { err = "mtp: state init failed"; return false; }
+ std::fprintf(stderr, "strata mtp: no pinned RAM left for the draft layer's K/V copy; keeping it in VRAM\n");
+ cudaGetLastError();
+ cudaFree(state_arena_);
+ st_ = QsaState{};
+ ring = -1; // fully resident
+ sb = qsa_state_bytes(g, max_cells, false, ring);
+ if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }
+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[0], ring) == 0) { err = "mtp: state init failed"; return false; }
+ }
qsa_state_zero(st_, g, nullptr);
cudaDeviceSynchronize();
vram_ += sb;