Skip to content

[Engine] strata-inkling CUDA: opt-in full-size VRAM K/V at start-up (STRATA_INK_KV_PREALLOC=1) - #7644

Open
mstrent wants to merge 1 commit into
Niko1221:mainfrom
mstrent:inkling-kv-prealloc
Open

mstrent wants to merge 1 commit into
Niko1221:mainfrom
mstrent:inkling-kv-prealloc

Conversation

@mstrent

@mstrent mstrent commented Dec 2, 2026

Copy link
Copy Markdown

Problem and result

With --kv-offload off, strata-inkling (CUDA) grows the global K/V in steps while a long prompt is read. On a card with little headroom, a growth step can be refused well short of --max-context, and the request ends with no output (#6770, item 2).

On a 16 GB T4 with 0.2.23 (engine 0.1.70), fp16 K/V and --max-context 92160, an 89,100-token prompt stops at 83,456 tokens:

[ink-serve] slot 0 ended at 83456 tokens: VRAM exhausted growing elastic global KV cache to 83968 tokens

This PR adds an opt-in switch, STRATA_INK_KV_PREALLOC=1. With it, the engine allocates the whole global K/V, and the DSpark context cache, in VRAM at start-up, so nothing grows at runtime. The same request completes: a 1,047-token answer at 16.8 tok/s, then a correct image answer in the same conversation. If the whole context does not fit, start-up fails with a message that says so, instead of a request failing later.

Without the variable no new code runs.

Related discussion

#6770. Item 2 there describes the gap, and I asked twice (in the report and in the 0.2.21 retest) whether you would take an opt-in pre-allocation as a PR or would rather close the gap in the planner. There is no answer yet, so the scope is not agreed. I am opening this because another user (an RTX 4090 with engine 0.1.71, DSpark K/V growth refused at 99,328 tokens with 1,244 MiB free) asked for the code in that thread. If you prefer the planner route, close this and I will keep the patch local.

The 0.2.21 fixes (1e800fd for the growth budget, 479b9bc / #6905 for the host tail) are already in; this change is only about the remaining gap.

Approach

STRATA_INK_KV_PREALLOC=1 is read once, at the end of InkCudaBackend::init, and only acts when --kv-offload off is in effect and no host K/V tail exists. At that point:

  1. InkCudaRunner::prealloc_global_kv_full frees the global (non-SWA) K/V visits, which are still empty, and allocates them once at the effective max_ctx for every slot (fp16, fp8 or k8v4 layout, with scales), updates the device descriptors and drops the cached CUDA graphs.
  2. InkDSpark::prealloc_ctx_full does the same for DSpark's global context cache. It does nothing for DFlash 2 (fixed-size rings), and it leaves the COMPACT drafter's mapped-host context alone.
  3. One start-up line reports the result, for example
    [InkCUDA] KV prealloc: global KV 92160 of 92160 tokens, drafter DSpark, DSpark ctx 92160 (fused pack=1), 452 MiB VRAM free.
    If either allocation fails, init returns STRATA_INK_KV_PREALLOC=1 could not pre-allocate N tokens of VRAM-only K/V ...; lower --max-context.

Why the last start-up step, and not earlier: DSpark builds its fused FP16 pack only when enough VRAM is free at its init. In an earlier local version that allocated the caches before DSpark, the pack was silently skipped, decode was 10 to 25 % slower and greedy outputs changed. Doing it last keeps every VRAM-dependent start-up choice as it is today.

Pre-allocation uses the max_ctx the planner chose (after Auto-sizing). It does not raise that number.

STRATA_INK_KV_FIRST (already in 0.2.23) is related but different, from reading the code: it reserves the K/V before the experts are placed, takes that VRAM from the experts, and only applies with one slot on cards up to 20 GiB whose experts do not all fit. This switch runs after the experts are placed, works with any slot count, and changes nothing else. I did not test KV_FIRST here; on this card all 3,072 experts fit in VRAM, so it would not apply.

Compatibility: the variable is new and off by default. The settings register has the entry (status opt-in, owner inkling2/cuda) and docs/SETTINGS.md was regenerated from it. I added only the new entry by hand: a full tools/settings_register.py run rewrote about 13,000 lines of unrelated fields on this checkout.

Validation

Machine: Tesla T4 16 GB (sm_75, driver 615.71.09), Ubuntu 26.04.1, CUDA 13.1. Served by the Strata 0.2.23 Control (0.1.70). Model Inkling-2-Mini SQ-10, all 3,072 experts in VRAM (--vram-experts-gb 8.60), vision on (read on the CPU), --parallel 1, --kv fp16, --kv-offload off, --max-context 92160, auto prefill chunk (512).

Engines compared:

  • release: the 0.1.70 package from the 0.2.23 release.
  • branch: this commit on tag v0.2.23 (4560d03), built with
    cmake -S engine/src/inkling2 -B build -G Ninja -DSTRATA_ENABLE_CUDA=ON -DSTRATA_INK_CUDA_ARCHS=75 -DCMAKE_BUILD_TYPE=Release && ninja -C build all, run with the release package's CUDA libraries.

Tests:

  • Greedy: 5 fixed prompts at temperature 0, including a 5K-token one; compared by hash of the answer.
  • Fill: one long prompt with up to 1,500 answer tokens, then an image follow-up (a red/blue picture) in the same conversation.

Passed:

Engine, drafter Start-up Fill prompt Fill result Image follow-up Peak VRAM
release, DSpark elastic (grows while reading) 89,100 tokens ends at 83,456, 0 tokens (finish_reason: length) 0 tokens 15,505 MiB
branch, no variable, DSpark same as release 89,100 same: ends at 83,456, 0 tokens 0 tokens 15,353 MiB
branch, KV_PREALLOC=1, DSpark 92,160 of 92,160, DSpark ctx 92,160, fused pack=1, 452 MiB free 89,100 completes: read at 368.7 tok/s, 1,047 tokens at 16.8 tok/s correct ("Left: red; right: blue.") 15,617 MiB
release, DFlash 2 Auto-sizing 92,160 -> 89,920 86,163 completes: grows to 89,920, read at 367.4 tok/s, 760 tokens at 16.9 tok/s correct 15,073 MiB
branch, KV_PREALLOC=1, DFlash 2 Auto-sizing 92,160 -> 89,920, then 89,920 of 89,920, drafter DFlash 2, 892 MiB free 86,163 completes: read at 367.0 tok/s, 454 tokens at 16.8 tok/s correct 15,167 MiB (flat)

The DFlash 2 runs use a shorter prompt because the planner caps that start at 89,920 tokens, and 89,100 + 1,500 is rejected up front with HTTP 400 on both engines.

  • Default path unchanged: branch without the variable vs release, DSpark: all 5 greedy answers byte-identical, and the fill fails at the same token.

  • Same outputs with the switch on: branch with KV_PREALLOC=1 vs release, DSpark: all 5 byte-identical. With DFlash 2: all 5 byte-identical to the release with DFlash 2.

  • Decode on the greedy set (tok/s; one run each; prose / code / math / list / 5K):

    • release: 39.5 / 46.5 / 53.2 / 42.1 / 22.1
    • branch, no variable: 38.7 / 45.7 / 52.0 / 41.3 / 21.9
    • branch, KV_PREALLOC=1: 38.2 / 45.2 / 51.5 / 41.0 / 21.8

    The no-variable run runs the same code as the release and is up to 2.3 % lower, so I read these as run-to-run spread. They are single runs, not a speed claim.

  • Too large refuses to start: --max-context 131072 fp16 with DSpark. The planner auto-sizes to 112,256, the K/V fits, the DSpark context does not:

    [InkCUDA] KV prealloc: global KV 112256 of 112256 tokens, drafter DSpark, DSpark ctx 8192 (fused pack=1), 172 MiB VRAM free
    ERR init: STRATA_INK_KV_PREALLOC=1 could not pre-allocate 112256 tokens of VRAM-only K/V (DSpark context cache failed); lower --max-context
    

    The engine exits with code 1. Under the Control the runtime reports failed server_exited. The ERR init: line comes from running the same command directly: Control 0.1.70 did not copy it into its server or engine log. I did not check whether other init errors show the same; this change does not touch that path.

  • Build: ninja -C build all passes. No warnings in the changed files (the 11 in the log are in ink_hybrid.cuh and ink_moe_fp4_sm120.cu).

  • Settings register: python3 tools/settings_register.py --check passes, and python3 tools/test_settings_register.py runs 8 tests, OK.

  • Merge: git merge-tree --write-tree origin/main HEAD against main 8db8753 reports no conflicts.

Logs, raw JSON and VRAM traces of every run are kept and can be attached.

Context, not validation of this commit: earlier versions of the same patch (on 0.1.66, 0.1.67 and 0.1.69) ran as my daily Inkling service on this T4 from 2026-11-23 to 2026-11-29, last with 2 slots x 81,920 tokens of fp8 K/V and DSpark.

Failed:

None observed.

Not run:

  • Building or running on main. The commit merges cleanly, but I built and tested it on v0.2.23 only.
  • HIP, SYCL and Metal. The changed files are only compiled into the CUDA engine: engine/src/inkling2/hip has its own sources and includes only ink_iq_grids.cuh from the CUDA folder. The generated SETTINGS.md row says "NVIDIA and AMD" because the register classifies the folder that way.
  • Windows / WDDM. No machine. Someone in [Engine] strata-inkling CUDA: elastic global KV stops growing at about half of max_ctx, then the request fails (503) #6770 offered to test on an RTX 4090 under Windows.
  • More than one slot, fp8 and k8v4 on this commit. They allocate through the same code (max_slots * cap, the fp8 / k8v4 sizes and scales of the existing growth path), and the 0.1.69 version ran 2 slots with fp8, but none of these ran on this build.
  • The COMPACT drafter (mapped-host DSpark context, all_sf4_vram). The guard that skips it is from reading the code; this card does not use that path.
  • Multi-GPU layer split. prealloc_global_kv_full returns false when m.ls_active is set, so with the variable on, start-up refuses with the message above.

Limits

  • Opt-in, CUDA only, and only with --kv-offload off. With auto or on, the variable has no effect and nothing is logged.
  • It does not raise the planner's max_ctx. With DFlash 2 on this card the release already grows to the planner's 89,920, so the switch only moves the allocation to start-up there. The 4090 report in [Engine] strata-inkling CUDA: elastic global KV stops growing at about half of max_ctx, then the request fails (503) #6770 (DFlash 2 capped at 87,040 with 1,658 MiB free) is a planner cap that this change does not lift.
  • It fails at start-up rather than falling back. A start-up that would run today with DSpark disabled or a shorter K/V refuses to start with the variable on. That is deliberate: the switch is for people who pick their context by hand and want to know at start-up.
  • The VRAM it holds is held from start-up, even when only short prompts arrive.
  • Not supported with layer split (start-up refuses).

🤖 Generated with Claude Code

…REALLOC=1)

With --kv-offload off the global K/V grows in steps at runtime, and on a
card with little headroom a step can be refused well short of
--max-context (Niko1221#6770 item 2: a T4 with --max-context 92160 stops at
86,528 tokens and the request ends with no output).

STRATA_INK_KV_PREALLOC=1 (only with --kv-offload off) frees the still-empty
global K/V and the DSpark global context cache as the last start-up step,
after DSpark has built its fused pack, and allocates them once at the full
max_ctx. Nothing grows at runtime. Start-up fails with a clear message if
the whole context does not fit. DFlash 2 keeps its fixed-size rings.

The default path is unchanged: without the variable no code runs.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant