Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions docs/INKLING_ZHA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# The ZHA index for Inkling 2 Mini (`STRATA_INK_ZHA`)

Back to [Inkling 2 Mini](models/inkling.md)

Inkling 2 Mini's 48 layer runs are 40 that look back 1,024 tokens and 8 that look at the whole context. Those 8
keep 32,768 bytes a token in VRAM (fp16), so a 1M-token context needs 32 GiB for them alone, 17 GiB in Q8_0. With
`STRATA_INK_ZHA` set to a reference file, those 8 runs attend through an index instead: their keys and values go to
pinned RAM in 4 bits, and VRAM keeps a summary of every 64 keys. On an 8 GB card that is what lets a 1M-token prompt
be read and answered.

It is opt-in. Without `STRATA_INK_ZHA` nothing changes: the default path writes the same tokens as before (below,
how that was checked). It runs on an NVIDIA card from Turing on (compute capability 7.5), on one card and one
sequence at a time: with `--batch` above 1 the engine reads one sequence (as with `--kv q8_0`), and the load stops
with a message with `--draft-model`, `--layer-split`, `--expert-gpu`, on AMD and on a Mac.

The method is ZHA ("Zyphra Hierarchical Attention", [arXiv 2610.42112](https://arxiv.org/abs/2610.42112)), one level
of it, with the paper's reference queries trained for this model.

## Turning it on

```
STRATA_INK_ZHA=terminal/data/zha/inkling-2-mini-refs-c64-r4.bin strata-poly --model Inkling-2-Mini-IQ4_NL.gguf --max-context 1048576 ...
```

The reference file is `terminal/data/zha/inkling-2-mini-refs-c64-r4.bin` (131,152 bytes): 4 reference queries for
each KV head of each of the 8 runs, started from k-means of the model's own queries and trained with the paper's
local loss on two long texts. The load prints

```
strata-poly: ZHA index: chunks of 64, 4 references, budget 0.160 (at most 256), a pool of 600 chunks a KV head
```

Three settings tune it; the defaults are the ones measured below.

| Setting | Default | What it does |
| --- | --- | --- |
| `STRATA_INK_ZHA_BUDGET` | 0.16 | the share of a token's far chunks it reads exactly |
| `STRATA_INK_ZHA_MAX` | 256 | at most this many far chunks a token |
| `STRATA_INK_ZHA_POOL` | 600 | an answer's chunks kept in VRAM, a KV head and a layer run; 0 keeps none |

## What a token reads

The keys of a full layer run are cut into chunks of 64. When a chunk is complete, the index summarizes it for each of
4 reference queries of the run's KV head: the keys and the values weighted as that query would weigh them, and the
log of the sum of the weights. A token then reads, for each KV group of 3 heads:

- exactly: chunk 0, its last 1,024 keys, and the far chunks its summaries score best, 16% of them and at most 256
(the three heads of a group choose together; a prompt's tokens choose 64 at a time);
- through the summaries: every other far chunk as one key, the summary of the reference that fits it best.

The exact chunks come from the 4-bit leaves in pinned RAM over the bus. An answer keeps the chunks it read in a pool
in VRAM (600 a KV head), so the next token reads only the ones it has not read yet. The recent keys stay in a ring
in VRAM in Q8_0. A jump in the sequence (a new prompt, a restored conversation) rebuilds the ring and the summaries
from the leaves.

## What it takes

Per token, for the 8 full layer runs (8 KV heads of 128):

| | a token | at 1,048,576 tokens |
| --- | ---: | ---: |
| the leaves, pinned RAM (4-bit keys and values, fp16 scales) | 9,216 B | 9.0 GiB |
| the summaries, VRAM (4 references a chunk, 4-bit) | 592 B | 592 MiB |
| without the index: fp16 / Q8_0 caches, VRAM | 32,768 / 17,408 B | 32 / 17 GiB |

Beside those the index keeps the answer's pool (338 MiB at the default 600), the ring (7 MiB a layer run at a
2,048-token prompt batch) and, while a prompt is read, work buffers it borrows from the expert cache and gives back
at the end of the read. The summaries' room grows with the context, also from the expert cache.

## Measured

| | |
| --- | --- |
| Graphics card | NVIDIA RTX 4060, 8 GB (sm_89), driver 615.78 |
| CPU and RAM | Ryzen 7 5700X (8 cores), 31 GB |
| Built with | CUDA 13.4, Linux |
| Model | `Inkling-2-Mini-IQ4_NL.gguf` (ggml-org) |
| Command | `strata-poly --model ... --tokens @prompt --max-new N --max-context C --prompt-batch 2048 --kv fp8`, with and without `STRATA_INK_ZHA` |

Reading a prompt and answering. The long prompts are one text with a code hidden at 10% of its length and a question
about it at the end; the answer must be the code (`7349-AMBER-2291`).

| Prompt | Without the index | With it |
| --- | --- | --- |
| 32,768 tokens, 200 written | 1,319 tokens/s read, 31.1 written/s | 1,391 tokens/s read, 32.7 written/s |
| 131,497 tokens, the code | 947 tokens/s, found | 1,328 tokens/s, found |
| 129,024 tokens, 200 written | 963 tokens/s read, 22.2 written/s | 1,310 tokens/s read, 30.0 written/s |
| 271,910 tokens, the code | stops: `no VRAM for the attention cache` | 1,243 tokens/s, found |
| 1,024,338 tokens, the code | (the full runs' fp8 caches alone: 16 GiB) | 853 tokens/s (20 min), found |

At 1M the index's leaves took 9.0 GiB of pinned RAM beside the 11.5 GiB of experts the card did not hold.

How far the answers move: over the last 2,048 tokens of a 32,768-token text, each scored one at a time after the
ones before (a scoring build, not part of this change), the index's next-token distributions against the full
attention's: mean KL 0.0156 (p99 0.216), the same most likely token at 97.1% of the positions, perplexity 1.5656
against 1.5697.

The default path: without `STRATA_INK_ZHA` this build and main (36e654b1b) wrote the same 200 tokens after the
32,768-token prompt, with `STRATA_FREEZE_EXPERTS=1` so that both keep the same experts on the card (which experts sit
there changes which code computes them). With `--batch 3 --check-batch 3` both wrote, for each of 3 sequences, the
same 64 tokens as that sequence alone.

## Limits

- The answers move: a token's far chunks it does not read exactly are estimated. At 32K tokens the next-token
distributions differ from the full attention's by a mean KL of 0.0156 (above).
- One sequence (`--batch` is not used); not with the drafter, `--layer-split` or `--expert-gpu`; NVIDIA only.
- The reference file is for Inkling 2 Mini, chunks of 64 and 4 references; the load refuses another shape.
9 changes: 7 additions & 2 deletions docs/SETTINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA
(`terminal/strata-<model>.json`), which the server passes to the engine. The Strata project's Terminal writes it there with
`strata launch --set NAME=value`.

## Switches you can turn on (347)
## Switches you can turn on (351)

Off unless you set them. The last column says where each one is described.

Expand Down Expand Up @@ -127,6 +127,10 @@ Off unless you set them. The last column says where each one is described.
| `STRATA_I8_MT` | number | Intel Arc | [INTEL_INT8_EXPERTS.md](INTEL_INT8_EXPERTS.md) |
| `STRATA_I8_WG` | number | Intel Arc | [INTEL_INT8_EXPERTS.md](INTEL_INT8_EXPERTS.md) |
| `STRATA_INK_FAST_CTRL` | on / off | NVIDIA | - |
| `STRATA_INK_ZHA` | text | NVIDIA and AMD | [INKLING_ZHA.md](INKLING_ZHA.md), [inkling.md](models/inkling.md) |
| `STRATA_INK_ZHA_BUDGET` | number | NVIDIA | [INKLING_ZHA.md](INKLING_ZHA.md) |
| `STRATA_INK_ZHA_MAX` | number | NVIDIA | [INKLING_ZHA.md](INKLING_ZHA.md) |
| `STRATA_INK_ZHA_POOL` | number | NVIDIA | [INKLING_ZHA.md](INKLING_ZHA.md) |
| `STRATA_IO_PF_AHEAD` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) |
| `STRATA_IO_PF_GATE` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md), [release-notes-0.2.0.md](release-notes-0.2.0.md) |
| `STRATA_IO_PF_GATE_LO` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) |
Expand Down Expand Up @@ -365,7 +369,7 @@ Off unless you set them. The last column says where each one is described.
| `STRATA_WDDM_BUDGET` | number | AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md) |
| `STRATA_WDDM_MARGIN_MIB` | number | AMD | [AMD_HIP.md](AMD_HIP.md) |

## Switches that go back to earlier behaviour (121)
## Switches that go back to earlier behaviour (122)

These behaviours are on by default. Setting the switch returns the earlier behaviour; the notes in the register say which
version made it the default.
Expand Down Expand Up @@ -404,6 +408,7 @@ version made it the default.
| `STRATA_IGPU_PROMPT_SET` | on / off | NVIDIA and AMD | v0.2.16 | [AMD_HIP.md](AMD_HIP.md), [RADEON_780M.md](RADEON_780M.md), [release-notes-0.2.1.md](release-notes-0.2.1.md) |
| `STRATA_IL_PAD` | number | NVIDIA and AMD | v0.2.16 | - |
| `STRATA_INK_FLASH` | on / off | NVIDIA and AMD | - | - |
| `STRATA_INK_FLASH_TOKENS` | number | NVIDIA and AMD | - | - |
| `STRATA_INK_KERNEL_TABLES` | on / off | NVIDIA | - | [inkling.md](models/inkling.md) |
| `STRATA_INK_MMA75` | on / off | NVIDIA and AMD | - | [inkling.md](models/inkling.md) |
| `STRATA_INK_PROJECT_MIXED` | on / off | NVIDIA | - | - |
Expand Down
8 changes: 8 additions & 0 deletions docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ top-20 probabilities with every expert on the CPU.
Once a token, a card hands the next one the token's residual streams and the loop's inputs, 64.5 KB. The DSpark
drafter (`--draft-model`) runs on one card only.

## A context of 1M tokens on a small card

The 8 full-attention layer runs keep 32,768 bytes a token in VRAM, so an 8 GB card runs out long before 1M tokens.
`STRATA_INK_ZHA` (opt-in, NVIDIA) moves their keys and values to pinned RAM and reads them through a ZHA index: on an
RTX 4060 8 GB a 1,024,338-token prompt was read at 853 tokens/s and its question answered, and at 128K the prompt
was read 40% faster than without it. The answers change a little (a mean KL of 0.016 at 32K tokens). More in
[INKLING_ZHA.md](../INKLING_ZHA.md).

## How does it work?

A reply is a list of typed blocks: thinking, text, or a tool call with its arguments as JSON. The server turns
Expand Down
10 changes: 5 additions & 5 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
# Optional model/state check; run by hand with an MTP GGUF and frozen prompt IDs.
add_executable(poly_gdn_mma_state_test src/poly/gdn_mma_state_test.cpp src/poly/engine.cpp src/poly/expert_card.cpp src/poly/glm5next.cpp
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp
src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/cpu_experts.cpp src/poly/kernels.cu
src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/inkling2_zha.cu src/poly/cpu_experts.cpp src/poly/kernels.cu
src/poly/kernels_dsv4.cu src/core/conversation_memory.cpp)
target_include_directories(poly_gdn_mma_state_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories})
target_link_libraries(poly_gdn_mma_state_test PRIVATE strata_poly_mmq strata_kernels strata_spec strata_quant
Expand All @@ -204,7 +204,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA

add_executable(strata-poly src/poly/main.cpp src/poly/engine.cpp src/poly/expert_card.cpp src/poly/glm5next.cpp src/poly/qwen35.cpp
src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp src/poly/flint.cpp
src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/inkling2_zha.cu src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
src/poly/spill.cpp src/core/conversation_memory.cpp src/core/conversation_file.cpp)
target_include_directories(strata-poly PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories})
# the driver's virtual memory calls: the attention caches grow with the conversation; on HIP they are HIP's
Expand All @@ -229,7 +229,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
# poly_draft_race_test MODEL.gguf [RUNS] [LAYER_SPLIT]
add_executable(poly_draft_race_test src/poly/draft_race_test.cpp src/poly/engine.cpp src/poly/expert_card.cpp src/poly/glm5next.cpp
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/inkling2_zha.cu
src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
src/core/conversation_memory.cpp)
target_include_directories(poly_draft_race_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Expand All @@ -241,7 +241,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
# so it is run by hand: poly_layer_split_picks_test MODEL.gguf [LAYER_SPLIT] [TOKENS]
add_executable(poly_layer_split_picks_test src/poly/layer_split_picks_test.cpp src/poly/engine.cpp src/poly/expert_card.cpp src/poly/glm5next.cpp
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/inkling2_zha.cu
src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
src/core/conversation_memory.cpp)
target_include_directories(poly_layer_split_picks_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Expand All @@ -253,7 +253,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
# so it is run by hand: poly_logits_dump MODEL.gguf --tokens ID,ID,... (its usage says the rest)
add_executable(poly_logits_dump src/poly/logits_dump.cpp src/poly/engine.cpp src/poly/expert_card.cpp src/poly/glm5next.cpp
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/inkling2.cpp src/poly/inkling2_pictures.cpp src/poly/inkling2.cu src/poly/inkling2_zha.cu
src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
src/core/conversation_memory.cpp)
target_include_directories(poly_logits_dump PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Expand Down
21 changes: 20 additions & 1 deletion engine/metal/src/poly/unported.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// metal/src/poly/unported.cpp - the entry points of the shared strata-poly host code that have no Metal kernel yet.
// None of them is on a Mac family's default path: llama.cpp's flash attention (which falls back to attend() and
// attend_batch() by its own contract), Gemma 4's opt-in int8 attention caches, Qwen35's opt-in q4_0 cache and cache
// streaming, Clover's block guesses and coreset routing, and the half-sum guard of Flint's MMQ. A call that would
// streaming, Clover's block guesses and coreset routing, the half-sum guard of Flint's MMQ and Inkling 2's opt-in ZHA
// index (STRATA_INK_ZHA, which Inkling2::zha_load() turns down on Metal first). A call that would
// need a kernel says what is missing instead of computing something else.
#include "fattn.hpp"
#include "inkling2_zha.hpp"
#include "mmq.hpp"
#include "strata/kernels/native_mmvq.hpp"
#include "strata/poly/kernels.hpp"
Expand Down Expand Up @@ -84,6 +86,23 @@ void scale_rows(float*, int64_t, int, int, const int32_t*, const float*, bool, v
unported("half-sum guard (Flint)");
}

namespace ink {
void store_q4(const float*, int8_t*, uint16_t*, int, long long, int, long long, void*) { unported("ZHA index (Inkling 2)"); }
void q4_to_q8(const int8_t*, const uint16_t*, long long, int8_t*, uint16_t*, long long, long long, int, int, void*) {
unported("ZHA index (Inkling 2)");
}
void zha_cache_clear(ZhaCache&, int, int, void*) { unported("ZHA index (Inkling 2)"); }
void zha_build(const float*, const ZhaKv&, long long, int, int, int, int, int, int, uint16_t*, uint16_t*, float*, void*,
size_t) {
unported("ZHA index (Inkling 2)");
}
void zha_attend(void*, const Zha&, const float*, const ZhaKv&, const ZhaKv&, long long, const uint16_t*, const uint16_t*,
const float*, int, int, long long, int, int, int, const float*, const float*, int, int, const float*,
const ZhaWork&, float*, void*, ZhaCache*, bool, int) {
unported("ZHA index (Inkling 2)");
}
} // namespace ink

} // namespace strata::poly

namespace strata::kernels {
Expand Down
8 changes: 7 additions & 1 deletion engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,7 @@ bool Engine::mind_card_vram() {
// the cache's least used experts first, and maps them again. The products' work memory (work_map_) is lent from
// the start, so the first prompt takes it here.
void Engine::take_prompt() {
prompt_begins();
if (!prompt_lent_ && !work_lent_) {
if (two_scratch_ && !prompt_scratch_) use_scratch(true);
return;
Expand Down Expand Up @@ -6945,6 +6946,7 @@ void Engine::trim_step_graphs(size_t keep, bool all) {
void Engine::run(const int32_t* tokens, int n) {
if (n < 1 || n > rows_) throw std::logic_error("eval takes 1 to max_rows() tokens");
lock_idle_.store(false, std::memory_order_relaxed);
read_end_ = std::max<int64_t>(read_end_, pos_ + n);
if (n >= kStageRows) take_prompt();
if (pos_ + n > opt_.max_context)
throw std::runtime_error("the context is full (" + std::to_string(opt_.max_context) + " tokens)");
Expand Down Expand Up @@ -7968,6 +7970,7 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks,
if (n < (n_co > 0 ? 0 : 1) || all > read_rows_)
throw std::logic_error("read takes 1 to max_read() tokens, co-reads among them");
lock_idle_.store(false, std::memory_order_relaxed);
read_end_ = pos_ + n;
if (all >= kStageRows) take_prompt();
for (const Image& im : images_)
if (im.at < pos_ + n && im.at + im.n > pos_)
Expand Down Expand Up @@ -8300,8 +8303,11 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function<boo
ck(cudaMemGetInfo(&free_b, &total_b), "cudaMemGetInfo");
if (pool_on_) free_b = vpool_bytes();
const size_t quarter = pool_on_ ? 0 : (size_t(std::max(reserve_mib_, 0)) << 20) / 4;
// what an earlier read of the prompt already holds counts too: its rows stay mapped until end_read(), and
// without them a later read saw no room and went a call at a time (a 1M-token prompt read in 227-call reads)
const size_t room = (free_b > quarter ? free_b - quarter : 0) +
(exp_ && arena_.mapped > kArenaPiece ? arena_.mapped - kArenaPiece : 0);
(exp_ && arena_.mapped > kArenaPiece ? arena_.mapped - kArenaPiece : 0) +
(carry_lent_ ? carry_map_.mapped : 0);
int g = (int) std::min<size_t>((size_t) n_calls, room / (per * sizeof(float)));
float* base = nullptr;
const size_t most = (size_t) ((read_rows_ + rows_ - 1) / rows_) * per * sizeof(float);
Expand Down
Loading
Loading