Repository navigation
[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
Draft
CrownedSilverFox wants to merge 3 commits into
CrownedSilverFox wants to merge 3 commits into
Conversation
…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>
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
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):
STRATA_INK_ZHAno VRAM for the attention cacheThe 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_ZHAthe 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 newEnginehooks), 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_qkvskips 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.--draft-model,--layer-split,--expert-gpu, cards below sm_75, HIP and Metal (stub entry points there).--batchabove 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 addsSTRATA_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.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, thenninja.Passed:
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 withcmp), and--batch 3 --check-batch 3gave "the first 64 of 64 tokens as on its own" for all 3 sequences on both.--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.ninjabuilds 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.STRATA_INK_ZHAwith--batch 2reads 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:
inkling2_zha.cu, Metal inmetal/src/poly/unported.cpp; both refuseSTRATA_INK_ZHAat load. Unverified that they compile.tools/check --only py-tools(no pytest on that machine).Limits
🤖 Generated with Claude Code