mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
fix(inkling cuda): growing the global K/V never ends the engine; the host tail is the whole rest of the context (#6905)
#6905 (RTX 5060 Ti 16 GB, SQ-10, 64K, vision): another program took VRAM after the start, the K/V could not grow in the card, the engine spilled a host tail
sized to just the request that hit the limit, and the next, longer request ended the engine ("VRAM exhausted growing elastic global KV cache to 40961 tokens").
- ensure_global_kv_cap maps the WHOLE rest of the context to pinned host RAM when the card cannot grow (ink_kv_host_tail_full); only when that does not fit in
host memory it falls back to the request's own size (ink_kv_host_tail_need).
- When the K/V cannot grow at all (K/V offload off, no host RAM), ink_serve ends only the requests whose positions are past the K/V's capacity (reason
"length") and goes on; every other forward failure is still fatal.
- Planner: on CUDA with K/V over 1 GB and offload auto, the K/V stays in the card only with 0.5 GB to spare (KV_CARD_HEADROOM_GB); the reporter's plan
(SQ-10, 15.9 GiB, 64K, vision: 0.4 GB to spare) now keeps all experts in the card and puts the K/V on the host.
- test_no_debugger_children: setup.py has one more listed process (vm_stat, added by 7a1467e21 without the count).
RTX 4090, SQ-10, max context 131072, a hog takes all but 1100 MiB after the engine is ready, requests of 20K / 45K / 60K / 100K tokens:
before: 20K answered, 45K "ERR fatal GPU error: VRAM exhausted growing elastic global KV cache to 36383 tokens", engine gone (spill 20224 + 12544 host)
after: all four answered, then a short one again (spill 18432 + 112640 host = 131072)
Tests: ink_kv_grow_test (host tail), test_inkling_kv_headroom, 158 planner/launch tests on p2006.
This commit is contained in:
1 parent
751d55647f
commit
479b9bcf5e
9 files changed
+94
-4
No files matched your search
@@ -37,6 +37,12 @@ inline bool ink_kv_grow_fits(size_t vfree, size_t floor_b, int n_glob, int slots
|
||||
return vfree > floor_b + ink_kv_grow_peak_bytes(n_glob, slots, old_cap, new_cap, per_pos);
|
||||
}
|
||||
|
||||
// Host tail (positions) to map when the card cannot grow the K/V any further and the rest of the K/V goes to pinned host RAM: the WHOLE rest of the
|
||||
// context first, so the K/V never has to grow again (a spill sized to just the request that hit the limit made the next, longer request end the
|
||||
// engine: "VRAM exhausted growing elastic global KV cache to 40961 tokens", #6905), and only when that does not fit in host memory, just the request.
|
||||
inline int ink_kv_host_tail_full(int kv_dev_ctx, int max_ctx) { return std::max(0, max_ctx - kv_dev_ctx); }
|
||||
inline int ink_kv_host_tail_need(int kv_dev_ctx, int new_cap) { return std::max(0, new_cap - kv_dev_ctx); }
|
||||
|
||||
// The capacity to grow to: the full step when it fits, else the tight one (just the request, 256-aligned), else 0 (no room:
|
||||
// the caller spills the tail to mapped host memory or refuses).
|
||||
inline int ink_kv_grow_target(size_t vfree, size_t floor_b, int n_glob, int slots, int old_cap, int needed_pos,
|
||||
|
||||
@@ -84,6 +84,8 @@ public:
|
||||
|
||||
int effective_max_ctx() const override { return initialized_ ? eff_max_ctx_ : 0; }
|
||||
int effective_chunk() const override { return initialized_ ? eff_chunk_run_ : 0; }
|
||||
bool last_failure_is_kv_growth() const override { return kv_grow_failed_; }
|
||||
int kv_capacity_tokens() const override { return (initialized_ && runner_) ? runner_->kv_alloc_ctx : 0; }
|
||||
|
||||
bool dspark_ready() const override { return initialized_ && dspark_ && dspark_->ready; }
|
||||
|
||||
@@ -586,6 +588,7 @@ public:
|
||||
}
|
||||
const int N = batch.n_tokens;
|
||||
if (N <= 0) return true;
|
||||
kv_grow_failed_ = false;
|
||||
// Defensive KV position bounds check + elastic global KV growth (fixes #6590B CUDA error 700 & CA1JEeAx!)
|
||||
int max_pos_req = 0;
|
||||
for (int i = 0; i < N; ++i) {
|
||||
@@ -597,6 +600,7 @@ public:
|
||||
}
|
||||
if (!runner_->ensure_global_kv_cap(max_pos_req + 1)) {
|
||||
err = "VRAM exhausted growing elastic global KV cache to " + std::to_string(max_pos_req + 1) + " tokens";
|
||||
kv_grow_failed_ = true;
|
||||
return false;
|
||||
}
|
||||
if (batch.capture_taps && dspark_ && dspark_->ready) {
|
||||
@@ -968,6 +972,7 @@ private:
|
||||
int gpu_id_ = 0;
|
||||
int max_slots_ = 1;
|
||||
int eff_max_ctx_ = 4096;
|
||||
bool kv_grow_failed_ = false; // the last forward_batch ended only because the global K/V could not grow
|
||||
int eff_chunk_run_ = 0; // the prefill chunk the runner really got (after auto / halve-on-fail)
|
||||
int dspark_slot_ = -1;
|
||||
KvDtype kv_dtype_ = KvDtype::FP16;
|
||||
|
||||
@@ -2289,8 +2289,13 @@ struct InkCudaRunner {
|
||||
if (tight_cap <= old_cap ||
|
||||
!strata::core::ink_kv_grow_fits(vf, floor_b, n_glob_s, max_slots, old_cap, tight_cap, per_pos)) {
|
||||
// VRAM cannot grow global KV further: seamlessly spill tail [kv_dev_ctx..new_cap-1] into mapped pinned host RAM!
|
||||
// The whole rest of the context first, so the K/V never has to grow again (a tail sized to just this request made the next, longer
|
||||
// request end the engine, #6905); only when that does not fit in host memory, just what this request needs.
|
||||
if (kv_offload_mode >= 0 && kv_host_ctx == 0) {
|
||||
const int hcap = new_cap - kv_dev_ctx;
|
||||
const int tails[2] = {strata::core::ink_kv_host_tail_full(kv_dev_ctx, max_ctx), strata::core::ink_kv_host_tail_need(kv_dev_ctx, new_cap)};
|
||||
for (int ti = 0; ti < 2; ++ti) {
|
||||
const int hcap = tails[ti];
|
||||
if (ti == 1 && hcap == tails[0]) break;
|
||||
if (hcap > 0) {
|
||||
if (m.ls_active) {
|
||||
for (int ss = 0; ss < m.n_stages; ++ss) {
|
||||
@@ -2339,6 +2344,7 @@ struct InkCudaRunner {
|
||||
}
|
||||
for (int v = 0; v < kInkVisits; ++v) if (!kv_desc[v].is_swa) free_host_kv_visit(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,10 @@ public:
|
||||
virtual BackendStats stats() const = 0;
|
||||
virtual int effective_max_ctx() const { return 0; }
|
||||
virtual int effective_chunk() const { return 0; } // the prefill chunk the backend really runs with (0: opts.chunk)
|
||||
// The last forward_batch failed only because the global K/V could not grow (no room in the card, none to spill to): the engine is fine, the
|
||||
// requests whose positions reach kv_capacity_tokens() are the ones to end (#6905); every other failure is fatal.
|
||||
virtual bool last_failure_is_kv_growth() const { return false; }
|
||||
virtual int kv_capacity_tokens() const { return 0; }
|
||||
|
||||
// Load Section 1 trunk + DSpark + 3x loop-weighted Section 4 experts into device/host memory
|
||||
virtual bool init(const InkModelFile& model, const BackendOpts& opts, std::string& err) = 0;
|
||||
|
||||
@@ -939,6 +939,21 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend&
|
||||
logits_buf.resize(static_cast<size_t>(std::max(1, n_want)) * V);
|
||||
std::string ferr;
|
||||
if (!backend.forward_batch(batch, n_want > 0 ? logits_buf.data() : nullptr, nullptr, ferr)) {
|
||||
// The global K/V could not grow (no room in the card, none to spill to): the engine is fine. End the requests whose positions
|
||||
// reach past what the K/V holds (reason "length": the context this card can serve), keep every other request, and go on (#6905).
|
||||
if (backend.last_failure_is_kv_growth()) {
|
||||
const int cap = backend.kv_capacity_tokens();
|
||||
bool ended = false;
|
||||
for (int i = 0; i < batch.n_tokens; ++i) {
|
||||
const int s = batch.slot_ids[i];
|
||||
if (batch.pos[i] >= cap && s >= 0 && s < P && reqs[s].active) {
|
||||
std::fprintf(stderr, "[ink-serve] slot %d ended at %d tokens: %s\n", s, batch.pos[i], ferr.c_str());
|
||||
finish_req(reqs[s], "length");
|
||||
ended = true;
|
||||
}
|
||||
}
|
||||
if (ended) continue;
|
||||
}
|
||||
proto("ERR fatal GPU error: %s", ferr.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -82,6 +82,17 @@ int main() {
|
||||
CHECK(ink_kv_grow_target(kFloor, kFloor, n_glob, 1, 8192, 8193, max_ctx, kPerPos) == 0);
|
||||
CHECK(ink_kv_grow_target(0, kFloor, n_glob, 1, 8192, 8193, max_ctx, kPerPos) == 0);
|
||||
|
||||
// #6905: when the card cannot grow the K/V and the rest goes to host RAM, the host tail is the WHOLE rest of the context (so no later, longer request
|
||||
// needs to grow it again), and only if that does not fit in host memory just what the request needs
|
||||
CHECK(ink_kv_host_tail_full(40960, 65536) == 24576); // the reporter: the card stopped at 40960 of a 64K context
|
||||
CHECK(ink_kv_host_tail_need(40960, 49152) == 8192);
|
||||
CHECK(ink_kv_host_tail_full(40960, 65536) > ink_kv_host_tail_need(40960, 41984));
|
||||
CHECK(ink_kv_host_tail_full(65536, 65536) == 0); // nothing left to map
|
||||
CHECK(ink_kv_host_tail_full(70000, 65536) == 0); // never negative
|
||||
CHECK(ink_kv_host_tail_need(8192, 8192) == 0);
|
||||
// a K/V that grew to its host tail serves every position up to max_ctx: dev + full tail == max_ctx
|
||||
for (int dev : {256, 8192, 32768, 40960, 65471}) CHECK(dev + ink_kv_host_tail_full(dev, 65536) == 65536);
|
||||
|
||||
if (failed) {
|
||||
std::fprintf(stderr, "ink_kv_grow_test: %d failed\n", failed);
|
||||
return 1;
|
||||
|
||||
@@ -31,6 +31,7 @@ OS_RESERVE_GB = 4.50 # system RAM reserve on discrete or UMA hosts
|
||||
# Measured (RTX 3060 12 GB, A4000 16 GB, 4090 24 GB; strata-inkling memory ledger): after the experts, the K/V, the workspaces and the
|
||||
# drafter are in, 0.4-0.5 GiB of the card was left; the margin makes that about 0.7-0.8 GiB, and a card that drives a screen keeps more.
|
||||
VRAM_SAFETY_GB = 0.30 # taken off the card's free memory before anything is placed
|
||||
KV_CARD_HEADROOM_GB = 0.50 # CUDA: the K/V stays in the card only with this much to spare; with less, the program that takes the memory next (browser, video) ends the growth (#6905)
|
||||
DESKTOP_RESERVE_GB = 0.80 # more, when the card drives a display: the desktop (browser, video) grows after the plan is made
|
||||
FREE_RAM_MARGIN_GB = 1.50 # free RAM that stays free beside the experts held in RAM
|
||||
UMA_RESERVE_GB = 5.00 # unified memory OS + driver reserve
|
||||
@@ -330,7 +331,10 @@ def compute(build: str, vram_gib: float, ram_gib: float, ctx: int | None = None,
|
||||
full_need = dense_total + info["bodies_gb"] + non_expert_gb(
|
||||
ctx, parallel, vision=vision_in_vram, dspark=dspark, fp8_kv=fp8_kv, hybrid=False, host_offload_global_kv=False, backend=backend_low
|
||||
)
|
||||
if vram_gb > 0.0 and effective_offload != "on" and full_need <= vram_gb:
|
||||
# CUDA: a K/V that fills the card to within a few hundred MB grows into whatever the desktop takes next; with that little to spare the K/V goes to host RAM
|
||||
# (the experts stay all in the card, the engine keeps the window of every slot there) when the RAM can hold it (#6905: a 16 GB card, 64K, vision, 0.4 GB to spare)
|
||||
kv_headroom = KV_CARD_HEADROOM_GB if (backend_low == "cuda" and effective_offload == "auto" and glob_kv > 1.0) else 0.0
|
||||
if vram_gb > 0.0 and effective_offload != "on" and full_need + kv_headroom <= vram_gb:
|
||||
return {
|
||||
"vram_gb": round(info["bodies_gb"], 2),
|
||||
"ram_gb": 0.0,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
"""#6905: the Inkling planner keeps the global K/V in a CUDA card only with room to spare."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
TERMINAL = Path(__file__).resolve().parents[2]
|
||||
if str(TERMINAL) not in sys.path:
|
||||
sys.path.insert(0, str(TERMINAL))
|
||||
|
||||
from strata_control import inkling_fit as F
|
||||
|
||||
|
||||
class KvHeadroomOnCuda(unittest.TestCase):
|
||||
"""#6905: RTX 5060 Ti 16 GB, SQ-10, 64K, vision: the plan kept the K/V in the card with 0.4 GB to spare, the browser took the next gigabyte, and growing the K/V ended the engine."""
|
||||
|
||||
def test_the_reporters_card_puts_the_k_v_on_the_host_not_in_a_card_with_no_room(self):
|
||||
p = F.compute("SQ-10", 15.9, 128, 64000, vision=True, backend="cuda")
|
||||
self.assertEqual(p["kv_offload"], "on")
|
||||
self.assertTrue(p["full_vram"]) # the experts all stay in the card
|
||||
self.assertGreater(p["host_kv_gb"], 1.0)
|
||||
|
||||
def test_a_card_with_room_keeps_the_k_v_in_the_card(self):
|
||||
p = F.compute("SQ-10", 24.0, 128, 64000, vision=True, backend="cuda")
|
||||
self.assertEqual(p["kv_offload"], "off")
|
||||
|
||||
def test_other_backends_and_an_explicit_choice_are_not_touched(self):
|
||||
# HIP / SYCL have no host K/V; --kv-offload off is the person's choice
|
||||
self.assertEqual(F.compute("SQ-10", 15.9, 128, 64000, vision=True, backend="hip")["kv_offload"], "off")
|
||||
self.assertEqual(F.compute("SQ-10", 15.9, 128, 64000, vision=True, backend="cuda", kv_offload="off")["kv_offload"], "off")
|
||||
|
||||
def test_a_small_k_v_keeps_no_headroom_rule(self):
|
||||
# 8K context: the K/V is far under 1 GB, nothing to move
|
||||
self.assertEqual(F.compute("SQ-10", 15.9, 128, 8192, vision=False, backend="cuda")["kv_offload"], "off")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -166,8 +166,8 @@ STRATA_PROGRAMS = {
|
||||
"strata_cli/launch/runner.py": 2, # setup.py and the other children (_env)
|
||||
"strata_cli/launch/params.py": 2, # engine --help (inherits)
|
||||
"strata_cli/cli.py": 2, # setup.py, the revise command
|
||||
"setup.py": 20, # builds, engine probes, the helpers it runs (main sets it); the update's git calls (P6: symbolic-ref) and the
|
||||
# re-run of the pulled setup.py (it inherits main's environment, Q6)
|
||||
"setup.py": 21, # builds, engine probes, the helpers it runs (main sets it); the update's git calls (P6: symbolic-ref) and the
|
||||
# re-run of the pulled setup.py (it inherits main's environment, Q6); vm_stat (macOS free memory, read-only, 7a1467e21)
|
||||
}
|
||||
OTHER_PROGRAMS = {
|
||||
"serve/oomd_pacer.py": 1, # systemctl (read-only: is systemd-oomd active, its limit)
|
||||
|
||||
Reference in new issue
Block a user