Repository navigation
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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=1is read once, at the end ofInkCudaBackend::init, and only acts when--kv-offload offis in effect and no host K/V tail exists. At that point:InkCudaRunner::prealloc_global_kv_fullfrees 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.InkDSpark::prealloc_ctx_fulldoes 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.[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,
initreturnsSTRATA_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 testKV_FIRSThere; 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, ownerinkling2/cuda) anddocs/SETTINGS.mdwas regenerated from it. I added only the new entry by hand: a fulltools/settings_register.pyrun 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:
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:
Passed:
finish_reason: length)KV_PREALLOC=1, DSparkKV_PREALLOC=1, DFlash 2The 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=1vs 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):
KV_PREALLOC=1: 38.2 / 45.2 / 51.5 / 41.0 / 21.8The 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 131072fp16 with DSpark. The planner auto-sizes to 112,256, the K/V fits, the DSpark context does not:The engine exits with code 1. Under the Control the runtime reports
failed server_exited. TheERR 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 otheriniterrors show the same; this change does not touch that path.Build:
ninja -C build allpasses. No warnings in the changed files (the 11 in the log are inink_hybrid.cuhandink_moe_fp4_sm120.cu).Settings register:
python3 tools/settings_register.py --checkpasses, andpython3 tools/test_settings_register.pyruns 8 tests, OK.Merge:
git merge-tree --write-tree origin/main HEADagainst 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:
main. The commit merges cleanly, but I built and tested it on v0.2.23 only.engine/src/inkling2/hiphas its own sources and includes onlyink_iq_grids.cuhfrom the CUDA folder. The generatedSETTINGS.mdrow says "NVIDIA and AMD" because the register classifies the folder that way.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.all_sf4_vram). The guard that skips it is from reading the code; this card does not use that path.prealloc_global_kv_fullreturns false whenm.ls_activeis set, so with the variable on, start-up refuses with the message above.Limits
--kv-offload off. Withautooron, the variable has no effect and nothing is logged.🤖 Generated with Claude Code