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 4 commits into
simplexai-labs:mainfrom
CrownedSilverFox:inkling-zha-index
Draft

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

Conversation

@CrownedSilverFox

@CrownedSilverFox CrownedSilverFox commented Nov 25, 2026 •

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,318 / 31.8 1,400 / 33.8
129,024 tokens, 200 written 963 / 22.4 1,313 / 30.5
131,497 tokens, needle at 10% 946, found 1,330, found
271,910 tokens, needle at 10% stops: no VRAM for the attention cache 1,242, found
1,024,338 tokens, needle at 10% does not fit 849 (20 min), found

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

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.
  • 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.
  • 4th commit: under the index a saved conversation keeps the 4-bit leaves as the full slots' rows (card_row_bytes(), save_card_rows(), load_card_rows(), 9,216 bytes a token), so one brought back after another reads its own keys.

Validation

Revision: 96a1a11 (4 commits on main d62dfac). 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 (630 checks), poly_inkling2_kvq8_test, poly_ink_flash_test, poly_ctrl_fast_test, poly_sink_attend_test: pass. tools/check --only cmake-cpu: pass (157 passed, 4 skipped); tools/check_links.py: all local links resolve. Every STRATA_INK_ZHA* name is in the register.
  • 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.
  • The server's conversation cache (terminal/serve/server.py, --conversation-cache-mib 2048 --prompt-cache 6, 131072 context): a 116,713-token conversation with two needles, parked while a 12,937-token one ran, then brought back ("restored 116713 tokens in 154 ms"): both needles answered exactly, 2.4 s and 1.0 s. Before the 4th commit the needle near its start came back as one string repeated.
  • 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 d62dfac with the same configuration; not touched here.
  • tools/settings_register.py --check (and so tools/check --only docs): main's own STRATA_DS_FUSE, STRATA_IDS_GROUP, STRATA_INK_ATTN_WIN and STRATA_NVFP4_LANES are not in the register, the same on main d62dfac. docs/SETTINGS.md here is main's page with the 4 ZHA rows added (a full --docs run would also change rows of main's own drift).

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).
  • tools/check --only py-tools (no pytest on that machine).

Limits

  • The answers move (mean KL 0.0166 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 4 commits November 25, 2026 22:29
…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>
Under the index card_row_bytes() gave the full slots no rows, so the server's conversation cache saved nothing of
them: a conversation brought back after another one rebuilt its ring and summaries from leaves the other had
overwritten.  A 116,713-token conversation, parked while a 12,937-token one ran, then answered a question about its
first 3,000 characters with one string over and over.  The rows are now a position's 4-bit leaves (9,216 bytes a
token for Inkling 2 Mini's 8 runs), and the summaries are rebuilt from the first restored row's chunk on.  After
it the same question got the right answer, in 2.6 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 25, 2026
…rix cores (WMMA)

On RDNA4 (gfx12) and RDNA3 (gfx11) the prompt's chunk scores and exact keys of simplexai-labs#336's ZHA index
(CrownedSilverFox) now run on v_wmma_f32_16x16x16_f16 instead of the FMA twins: zha_score_wmma_kernel and
zha_exact_wmma_kernel, with the grids, tiles and outputs of zha_score_kernel / zha_exact_kernel, the
products done transposed as in ink_wmma_kernel (S^T = K Q^T, O^T = V^T P^T, a row's softmax in lanes l and
l ^ 16; the gfx11 layout of src/split/src/hip/mma.h). The rounding is the tensor cores': queries, keys,
values and weights one fp16 each, fp32 sums; only the order of the sums differs. The tail stays on its
FMA kernel; other HIP cards, or STRATA_INK_ZHA_WMMA=0, keep the scalar kernels.

An empty asm that pins each 16-row step's accumulator stops the compiler from hoisting all 32 fragment
loads of a tile: 184 VGPRs on gfx12 (255 without), 247 on gfx11 (spills without), no spills, LDS 40 KB.

RX 9070 XT, 32,706-token prompt (rocprofv3): zha_exact 2,919 -> 266 ms, zha_score 477 -> 51 ms; all
kernels under ZHA 24.6 -> 21.6 s (main + port), 15.7 -> 12.7 s (amd-preview-2 + port, full attention
13.7 s). Prompt tok/s, ZHA scalar -> WMMA: 32K 1,675 -> 1,907, 129K 1,295 -> 1,771 (main + port).

poly_zha_kernels_test runs both AMD paths against the CPU reference (all within the same tolerances).
On CUDA inkling2_zha.cu preprocesses to the same tokens as before (sm_75, sm_86, sm_120). gfx11 is
built and checked for spills but not run on a card.

Credit: the ZHA index, its kernels and reference file are CrownedSilverFox's (simplexai-labs#336).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 25, 2026
 + its HIP port with WMMA) into inkling2-amd-preview-3

# Conflicts:
#	docs/SETTINGS.md
#	engine/cmake/strata_poly.cmake
#	engine/src/poly/engine.cpp
#	engine/src/poly/engine.hpp
#	engine/src/poly/inkling2.cpp
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