Skip to content

[Engine] Inkling 2: a ZHA index over the full-attention layers for long contexts on small cards (opt-in) - #336

Draft
CrownedSilverFox wants to merge 3 commits into
simplexai-labs:mainfrom
CrownedSilverFox:inkling-zha-index
Draft

CrownedSilverFox wants to merge 3 commits into
simplexai-labs:mainfrom
CrownedSilverFox:inkling-zha-index

Conversation

@CrownedSilverFox

Copy link
Copy Markdown

Problem and result

Inkling 2 Mini's 8 full-attention layer runs keep 32,768 bytes a token in VRAM (fp16; 17,408 in Q8_0), so on an 8 GB card the context ends far below 1M tokens: at 1M those runs alone need 32 GiB (17 GiB in Q8_0).

This adds an opt-in index over those runs, after ZHA ("Zyphra Hierarchical Attention", arXiv 2610.42112), one level. With STRATA_INK_ZHA=<reference file> their keys and values go to pinned RAM as 4-bit leaves (9,216 bytes a token for all 8 runs) and VRAM keeps per-chunk summaries (592 bytes a token). A token reads chunk 0, its last 1,024 keys and the far chunks its summaries score best exactly, the rest through the summaries.

On an RTX 4060 8 GB with Inkling-2-Mini-IQ4_NL.gguf (prompt read / tokens written a second):

Prompt main this branch, STRATA_INK_ZHA
32,768 tokens, 200 written 1,319 / 31.1 1,391 / 32.7
129,024 tokens, 200 written 963 / 22.2 1,310 / 30.0
131,497 tokens, needle at 10% 947, found 1,328, found
271,910 tokens, needle at 10% stops: no VRAM for the attention cache 1,243, found
1,024,338 tokens, needle at 10% does not fit 853 (20 min), found

The answers move a little: at 32K, over 2,048 scored positions, mean KL 0.0156 against full attention, the same top-1 token at 97.1%, perplexity 1.5656 against 1.5697.

Without STRATA_INK_ZHA the engine writes the same tokens as main (below).

Related discussion

#324: the scope proposal; a maintainer (VecSzn) answered "go ahead and open the PR once it's ready".

Approach

  • engine/src/poly/inkling2_zha.cu (new): the index's kernels. Prompt calls: chunk scores on the tensor cores (mma.sync), selections for tiles of 64 tokens, the exact chunks flash-style with a cp.async pipeline, the tail on the tensor cores. An answer's token: its own kernels, with a VRAM pool of the chunks it has read (the least recently read leave first), so only new chunks cross PCIe, on a high-priority stream beside the tail.
  • inkling2.cpp/.hpp: the index's slots (leaves, ring, summaries, pool), its work buffers lent from the expert cache while a prompt is read (prompt_begins() / prompt_ends(), two new Engine hooks), the summaries' room grown with the context. A full layer's head under the index is split into two graph recordings around the index for an answer's token (the index decides on the host what it launches).
  • inkling2.cu: ink_qkv skips the cache store when a layer has no cache (the index keeps its keys itself).
  • engine.cpp, separate commit: read_groups() counts the rows an earlier read of the same prompt holds as room. Without it a long prompt's later reads found no room and went a call at a time (seen reading a 1M-token prompt on the 8 GB card). It changes how calls are grouped, not what they compute.
  • Refused at load, with a message: --draft-model, --layer-split, --expert-gpu, cards below sm_75, HIP and Metal (stub entry points there). --batch above 1 reads one sequence, as with --kv q8_0. Three tuning settings: STRATA_INK_ZHA_BUDGET, _MAX, _POOL, in the settings register and docs/SETTINGS.md. Regenerating that page also adds STRATA_INK_FLASH_TOKENS, which main's register already lists and main's page lacks.
  • terminal/data/zha/inkling-2-mini-refs-c64-r4.bin (131 KB, own commit): the reference queries, k-means of the model's queries refined with the paper's local loss. The tools that trained it need a query dump hook that is not in this PR.
  • docs/INKLING_ZHA.md: what it does, what it takes, the measurements.

Validation

Revision: 006ca1f (3 commits on main 36e654b). RTX 4060 8 GB (sm_89), Ryzen 7 5700X, 31 GB RAM, CUDA 13.4, Linux. Model: Inkling-2-Mini-IQ4_NL.gguf (ggml-org). Build: cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CUDA_ARCHITECTURES=89 -DSTRATA_BUILD_POLY=ON -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=ON, then ninja.

Passed:

  • Default path unchanged: without STRATA_INK_ZHA, main and this branch wrote the same 200 tokens after a 32,768-token prompt (STRATA_FREEZE_EXPERTS=1 --max-context 34816 --prompt-batch 2048 --kv fp8, outputs compared with cmp), and --batch 3 --check-batch 3 gave "the first 64 of 64 tokens as on its own" for all 3 sequences on both.
  • Needles at 128K, 272K and 1M answered exactly (table above); full attention at 128K too.
  • KL above: --score (a scoring build of main.cpp, not in this PR) over the last 2,048 of 32,768 tokens.
  • poly_inkling2_test (431 checks), poly_inkling2_kvq8_test, poly_ink_flash_test, poly_ctrl_fast_test, poly_sink_attend_test: pass. tools/check --only docs,cmake-cpu: pass (cmake-cpu 156 passed, 4 skipped). tools/settings_register.py --check: no difference.
  • ninja builds every poly target, the ones that compile inkling2.cpp (poly_logits_dump, poly_draft_race_test, poly_layer_split_picks_test, poly_gdn_mma_state_test) with the new file.
  • Refusals: STRATA_INK_ZHA with --batch 2 reads one sequence with a message; a missing file stops the load ("STRATA_INK_ZHA: cannot open ...").

Failed:

  • ninja (all targets): strata-load, strata-overlap, strata-concurrent and pinned_shared_test fail to link (undefined reference to strata::kernels::cpu::expert_layout() from pinned.cu). The same on main 36e654b with the same configuration; not touched here.

Not run:

  • HIP, SYCL and Metal builds (no such machines). HIP gets stub entry points in inkling2_zha.cu, Metal in metal/src/poly/unported.cpp; both refuse STRATA_INK_ZHA at load. Unverified that they compile.
  • Other NVIDIA generations than Ada (sm_75 Turing and sm_80+ code paths are compiled for sm_89 only here).
  • The server with an agent conversation on this branch (it ran on an older branch of mine with the same index).
  • tools/check --only py-tools (no pytest on that machine).

Limits

  • The answers move (mean KL 0.0156 at 32K, above); the index was checked on needles and KL only.
  • One sequence on one NVIDIA card; pinned RAM grows by 9,216 bytes a token (9 GiB at 1M).
  • The reference file is trained for Inkling 2 Mini (chunks of 64, 4 references); the load refuses another shape.
  • The index's own speed work (expert staging, kernels) is not in this PR.

🤖 Generated with Claude Code

CrownedSilverFox and others added 3 commits November 25, 2026 01:39
…s as room

A prompt read in several reads keeps the earlier reads' carried rows mapped until end_read(), and read_groups()
left them out of its room: a later read of a long prompt found no room and went one call at a time.  Seen with
Inkling 2 Mini reading a 1M-token prompt on an 8 GB card.  It changes how a read's calls are grouped, not what
they compute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t-in)

The 8 full-attention layer runs of Inkling 2 Mini keep 32,768 bytes a token in VRAM, which caps an 8 GB card far
below 1M tokens.  With STRATA_INK_ZHA set to a reference file they attend through an index after ZHA (arXiv
2610.42112), one level: their keys and values in pinned RAM as 4-bit leaves, summaries of each 64-key chunk for 4
reference queries in VRAM, a token reading chunk 0, its last 1,024 keys and its best-scored far chunks exactly and
the rest through the summaries.  An answer keeps the chunks it read in a VRAM pool.  The prompt-time buffers are
lent by the expert cache while a prompt is read (Engine::prompt_begins() / prompt_ends()).

NVIDIA from sm_75, one sequence on one card; the load refuses it with --draft-model, --layer-split, --expert-gpu,
on HIP and Metal (stubs there).  Without STRATA_INK_ZHA nothing changes.  Measured on an RTX 4060 8 GB:
docs/INKLING_ZHA.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 reference queries for each KV head of each of the 8 full-attention runs (ZHAR: chunks of 64, 4 references),
started from k-means of the model's own queries and trained with the ZHA paper's local loss.  docs/INKLING_ZHA.md.

Co-Authored-By: Claude Opus 5.5 <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