diff --git a/docs/INKLING_ZHA.md b/docs/INKLING_ZHA.md new file mode 100644 index 0000000000..b6762b7cde --- /dev/null +++ b/docs/INKLING_ZHA.md @@ -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. diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index bb8fe28061..329b4b1d24 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA (`terminal/strata-.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. @@ -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) | @@ -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. @@ -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 | - | - | diff --git a/docs/models/inkling.md b/docs/models/inkling.md index 8157465201..0935e8c2fd 100644 --- a/docs/models/inkling.md +++ b/docs/models/inkling.md @@ -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 diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 11a4ab043e..17cc693595 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/engine/metal/src/poly/unported.cpp b/engine/metal/src/poly/unported.cpp index 01bdf347a7..3e2f4b6738 100644 --- a/engine/metal/src/poly/unported.cpp +++ b/engine/metal/src/poly/unported.cpp @@ -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" @@ -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 { diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37ed..49986cb561 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -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; @@ -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(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)"); @@ -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_) @@ -8300,8 +8303,11 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function 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) n_calls, room / (per * sizeof(float))); float* base = nullptr; const size_t most = (size_t) ((read_rows_ + rows_ - 1) / rows_) * per * sizeof(float); diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index ab0e274541..c3ede521c9 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -330,6 +330,8 @@ class Engine { /// The prompt's reads are done: the expert cache takes back what Options::stage_read_mib lent the staging /// buffer. window() and step() do it too, if the caller has not. void end_read() { + prompt_ends(); + read_end_ = 0; if (stage_lent_) narrow_stage(); if (carry_lent_) carry_give(); if (prompt_lends_) lend_prompt(); @@ -481,6 +483,9 @@ class Engine { /// through twice: begin_pass() is called before each. virtual void load_layer(int il) = 0; virtual void begin_pass() {} + /// A prompt's calls start (take_prompt()) and end (end_read()): an architecture's own prompt-only VRAM. + virtual void prompt_begins() {} + virtual void prompt_ends() {} /// Throws when the draft block is not one the architecture runs. virtual void check_draft_block() {} /// The weights of what guesses ahead, to VRAM. The engine's own: the draft block's parts beside its layer. @@ -834,6 +839,7 @@ class Engine { bool work_lent_ = false; void take_prompt(); void lend_prompt(); + int64_t read_end_ = 0; ///< where the read or call that takes the prompt's buffers ends (prompt_begins()) /// The scratch in two layouts (scratch_layouts()): one for a call of fewer than kStageRows tokens, which stays, /// and one for a prompt's batch, which is part of prompt_map_ and lent with it. use_scratch(true) gives the /// buffers their places in the prompt's, use_scratch(false) in the other. Recordings are made and replayed diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index c20d374044..905a8a9b3f 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -24,6 +24,17 @@ namespace strata::poly { namespace { constexpr size_t kScoreFloats = size_t(16) << 20; // the attention's scores of the tokens it takes at a time + +double env_num(const char* key, double fallback) { + const char* v = std::getenv(key); + return v && *v ? std::atof(v) : fallback; +} + +// STRATA_INK_ZHA's value: the ZHA index's reference file ("" when unset). +std::string zha_file() { + const char* v = std::getenv("STRATA_INK_ZHA"); + return v ? v : ""; +} const char* const kCtrlName[] = {"q", "k", "v", "ffn"}; // Unsloth's top-level files (architecture "inkling2": unsloth/Inkling-2-Mini-GGUF outside its Mainline_Support folder) @@ -309,6 +320,10 @@ void Inkling2::load_geometry() { } if (!opt_.draft_model.empty()) ds_open(); if (opt_.window == 0) opt_.window = ds_file_ ? ds_block_ : 1; + zha_load(); + // The index keeps one sequence's leaves, ring and summaries, and has not been run beside the drafter. + need(!zha_on_ || !ds_file_, "the ZHA index (STRATA_INK_ZHA) runs without --draft-model"); + one_sequence(!zha_on_, "with the ZHA index (STRATA_INK_ZHA)"); } // --draft-model's DSpark drafter: its shape, checked against what guess_block() runs. @@ -661,7 +676,9 @@ void Inkling2::load_layer(int il) { out_query_ = upload(gg_->find("output_res_query.weight"), "output_res_query.weight", e, 1, kF32); } B.swa = swa_[(size_t) il]; - L.fixed = true; // the launches read the position on the card when they are recorded (head_key(), tail_key()) + // the launches read the position on the card when they are recorded (head_key(), tail_key()); a full layer + // through the ZHA index decides on the host what it launches (chunks built, the ring, a jump): never recorded + L.fixed = !zha_full(B); B.wq = att("attn_q.weight", e, hq); B.wk = att("attn_k.weight", e, kv); B.wv = att("attn_v.weight", e, kv); @@ -764,6 +781,7 @@ void Inkling2::alloc_caches() { keep_ = std::min(window_, ring_); const size_t row = kv_row(); float* st = state_; + if (zha_on_ && zs_.size() != slots_.size()) zs_.assign(slots_.size(), ZSlot{}); for (Slot& s : slots_) { if (!runs(s.il)) continue; s.past = st; @@ -771,6 +789,10 @@ void Inkling2::alloc_caches() { if (blocks_[(size_t) s.il].swa) { s.keys = (uint16_t*) seq_alloc((size_t) ring_ * row, "the attention cache"); s.vals = (uint16_t*) seq_alloc((size_t) ring_ * row, "the attention cache"); + } else if (zha_on_) { // the index's: leaves in RAM, the ring and the summaries in VRAM + const int i = (int) (&s - slots_.data()), core = core_end_ - core_start_; + s.keys = s.vals = nullptr; + zha_alloc(i, s.il, i < n_layer_ ? 0 : 1 + (i - n_layer_) / core); } else { s.keys = (uint16_t*) kv_alloc(row, 1); s.vals = (uint16_t*) kv_alloc(row, 1); @@ -819,7 +841,10 @@ void Inkling2::assign_scratch() { aoraw_ = fbuf(e); fc_ = fbuf(e); // a token's scores of the whole context, and what ink_attend() keeps beside them - score_n_ = std::max(kScoreFloats, ink_attend_floats(n_head_, n_kv_, head_dim_, opt_.max_context)); + // under the index only the window layers' ring goes through ink_attend(): its keys, not the context's (the + // same for both layouts, which share the scores where they are apart) + const int64_t att_keys = zha_on_ ? std::min(opt_.max_context, (int64_t) window_ + rows_) : opt_.max_context; + score_n_ = std::max(kScoreFloats, ink_attend_floats(n_head_, n_kv_, head_dim_, att_keys)); if (scores_apart()) { // sized for the context, not for a call's tokens: 64 MiB that both layouts share if (!scores_) scores_ = (float*) dmalloc(score_n_ * sizeof(float), "the attention's scores"); score_ = scores_; @@ -837,6 +862,7 @@ void Inkling2::assign_scratch() { kvw_halves_ = (size_t) std::min(opt_.max_context, kKvq8WorkRows) * n_kv_ * head_dim_; kvw_ = fbuf(kvw_halves_); // 2 * kvw_halves_ halves } + if (zha_on_) zha_scratch(); if (ds_file_) { // the drafter's: a call's features and mixes, and a block's rows const size_t E = (size_t) n_embd_, hq = (size_t) n_head_ * head_dim_, kv = (size_t) n_kv_ * head_dim_, B = (size_t) ds_block_, V = (size_t) n_vocab_, R = (size_t) ds_rank_; @@ -884,6 +910,12 @@ void Inkling2::assign_scratch() { Inkling2::~Inkling2() { if (side_) cudaStreamSynchronize(cs(side_)); + for (ZSlot& z : zs_) { // the summaries and the leaves are the slots' own + for (void* p : {(void*) z.zk, (void*) z.zv, (void*) z.zb}) + if (p) cudaFree(p); + for (void* p : {(void*) z.kc, (void*) z.ksc, (void*) z.vc, (void*) z.vsc}) + if (p) cudaFreeHost(p); + } for (void* e : {side_go_, side_done_}) if (e) cudaEventDestroy((cudaEvent_t) e); if (side_) cudaStreamDestroy(cs(side_)); @@ -1188,8 +1220,53 @@ void Inkling2::layer_head(Layer& L, int il, int n) { // A call starts at its card's first layer. A card that starts at the loop's first layer comes back to it for // each later pass, so it keeps the pass there: next_layer() leaves it 0 after the last pass. if (il == lo_ && il != core_start_) pass_ = 0; + // A full layer under the index, a token: its launches before the index's and after them as two recordings of + // its own (the engine records only a fixed layer's head, and this one decides on the host what the index runs). + // Not within a prompt's read: its calls' streams are then in read_groups()' rows (carry_at()), which it gives + // back after the read, and a recording made with them would write there at its replays. + const bool home = bl_[0] == home_[0] && bank_ == home_[kBlocks] && bank_out_ == home_[kBlocks + 1]; + if (zha_full(blocks_[(size_t) il]) && n == 1 && !rows_of_ && opt_.graphs && opt_.window <= 1 && !recording_ && + home && !prompt_scratch_) { + if (zgraph_.empty()) zgraph_.assign(slots_.size() * 2, nullptr); + const size_t at = (size_t) slot_of(il, pass_of(il)) * 2; + if (!zgraph_[at]) zgraph_[at] = grab([&] { head_part(L, il, n, 1); }); + ck(cudaGraphLaunch((cudaGraphExec_t) zgraph_[at], cs(st_)), "layer graph"); + zha_attention(il, n); + ink_mul_sigmoid(att_, gate_, (size_t) n_head_ * head_dim_, st_); + if (!zgraph_[at + 1]) zgraph_[at + 1] = grab([&] { head_part(L, il, n, 2); }); + ck(cudaGraphLaunch((cudaGraphExec_t) zgraph_[at + 1], cs(st_)), "layer graph"); + return; + } + head_part(L, il, n, 3); +} + +// Engine::record()'s capture (a template of engine.cpp's): what `work` hands to the stream, as a graph. +void* Inkling2::grab(const std::function& work) { + cudaGraph_t g = nullptr; + ck(cudaStreamBeginCapture(cs(st_), cudaStreamCaptureModeThreadLocal), "cudaStreamBeginCapture"); + recording_ = recorded_ = true; + try { + work(); + } catch (...) { + recording_ = false; + if (cudaStreamEndCapture(cs(st_), &g) == cudaSuccess && g) cudaGraphDestroy(g); + cudaGetLastError(); + throw; + } + recording_ = false; + ck(cudaStreamEndCapture(cs(st_), &g), "cudaStreamEndCapture"); + cudaGraphExec_t exec = nullptr; + const cudaError_t made = cudaGraphInstantiateWithFlags(&exec, g, 0); + cudaGraphDestroy(g); + ck(made, "cudaGraphInstantiate"); + graphs_.push_back(exec); + return exec; +} + +// A layer's head: part 1 its launches up to the keys and values, 2 the ones after the attention, 3 all of them. +void Inkling2::head_part(Layer& L, int il, int n, int part) { Block& B = blocks_[(size_t) il]; - if (ds_file_) { // the drafter's features: the inputs of three layer runs + if ((part & 1) && ds_file_) { // the drafter's features: the inputs of three layer runs const int visit = visit_of(il, pass_of(il)); for (int k = 0; k < 3; ++k) if (visit == ds_visit_[k]) copy_rows(r_, ds_tap_ + (size_t) k * rows_ * n_embd_, n); @@ -1210,7 +1287,7 @@ void Inkling2::layer_head(Layer& L, int il, int n) { #endif // the tokens' embeddings, normed (a picture's rows are not: whole(); step()'s rows have none) - if (il == 0 && (per_row || !image_at(pos_))) { + if ((part & 1) && il == 0 && (per_row || !image_at(pos_))) { rms_norm(r_, fp(tok_norm_), mixf_, n_embd_, n, rms_eps_, st_); copy_rows(mixf_, r_, n); } @@ -1218,7 +1295,7 @@ void Inkling2::layer_head(Layer& L, int il, int n) { // before it as its bank: that pass's later block (if the core has two) and its output. const int core_block = core_start_ / blk_; const int n_bank = (core_end_ - core_start_) / blk_ - 1; - if (il == core_start_ && pass_ > 0) { + if ((part & 1) && il == core_start_ && pass_ > 0) { if (n_bank > 0) copy_rows(bl_[core_block + 1], bank_, n); copy_rows(r_, bank_out_, n); copy_rows(bl_[core_block], r_, n); @@ -1239,7 +1316,7 @@ void Inkling2::layer_head(Layer& L, int il, int n) { const bool looped = pass_of(il) > 0; // a later pass of the core: its inputs take the pass before's messages // a window layer outside a later pass has one input, normed in the mix's launch const bool mix_normed = il > 0 && B.swa && !looped && sum_threads(n_embd_) == 1024; - if (il > 0) { + if ((part & 1) && il > 0) { float* out[3] = {in_[0], in_[1], in_[2]}; ink_mix_norm(streams_of((il - 1) / blk_ + 1, 0), fp(B.attn_query), B.swa ? 1 : 3, out, n_embd_, n, mix_normed ? fp(B.attn_norm) : nullptr, mix_normed ? xin_[0] : nullptr, rms_eps_, st_, @@ -1252,17 +1329,19 @@ void Inkling2::layer_head(Layer& L, int il, int n) { if (looped) { if (n == 1 && core_end_ - core_start_ <= 12) { msg = msgs_.data() + (size_t) (il - core_start_) * kCtrl; - } else { + } else if (part & 1) { InkStates bank; if (n_bank > 0) bank.p[bank.n++] = bank_; bank.p[bank.n++] = bank_out_; ink_mix(bank, fp(B.msg_query), kCtrl, msg_, n_embd_, n, st_); } // Q's last: a window layer's three inputs are one buffer until the messages make them differ - if (!control3(in, msg, B.ctrl, in_, n)) + if ((part & 1) && !control3(in, msg, B.ctrl, in_, n)) for (int j = 2; j >= 0; --j) control(in[j], msg[j], B.ctrl[j], in_[j], n); for (int j = 0; j < 3; ++j) in[j] = in_[j]; } + const long long* at = recording_ ? at_ : nullptr; // a recorded launch: the tokens' positions from what at_ holds + if (part & 1) { const float* x[3]; bool quantized = false; // project()'s inputs quantized by the norms' launch if (B.swa && !looped) { @@ -1327,7 +1406,6 @@ void Inkling2::layer_head(Layer& L, int il, int n) { vd = vdyn_; } const float alpha = B.swa ? 0.f : log_alpha_; - const long long* at = recording_ ? at_ : nullptr; // a recorded launch: the tokens' positions from what at_ holds // one launch for all of it, the same bits (step()'s rows: a launch a row; load_geometry() takes no other heads) if ((n == 1 || per_row) && head_dim_ <= 128 && n_rel_ <= 128) { for (int i = 0; i < each; ++i) { @@ -1423,6 +1501,15 @@ void Inkling2::layer_head(Layer& L, int il, int n) { c.kvq8 = kvq8_; ink_clips(c, st_); } + } // part 1 + // a full layer under the index: its keys and values to the leaves and the ring, its attention through the + // summaries, then the output gate + const bool through_index = zha_full(B); + if (part == 3) { + if (through_index) { + zha_attention(il, n); + ink_mul_sigmoid(att_, gate_, (size_t) n * hq, st_); + } InkAttn a; a.q = q_; a.r = rr_; @@ -1451,7 +1538,8 @@ void Inkling2::layer_head(Layer& L, int il, int n) { // STRATA_KV_INT8 for decode only: a token alone or a window's (guessed tokens checked), not a prompt's rows, // which a chunk of fewer than kStageRows also brings here (a short turn, the end of a long prompt) a.int8_dot = int8_dot_ && (n == 1 || windowed_); - if (rows_launch) { // step()'s rows: each over its own sequence's caches, at its position, as it is alone + if (through_index) { // done above + } else if (rows_launch) { // step()'s rows: each over its own sequence's caches, at its position, as it is alone #if !defined(STRATA_GPU_METAL) InkAttnRows c; InkAttn b = a; @@ -1520,7 +1608,9 @@ void Inkling2::layer_head(Layer& L, int il, int n) { if (n == 1) a.q8 = xqo_; // and the output quantized for wo ink_attend(a, st_); } - lin(B.wo, att_, aoraw_, n, n == 1 && n < kStageRows ? xqo_ : nullptr); + } // part 3 + if (!(part & 2)) return; + lin(B.wo, att_, aoraw_, n, n == 1 && n < kStageRows && !through_index ? xqo_ : nullptr); // the first layer of a block starts a new stream from the attention's output; the one it had joins the mixes if (n == 1 || per_row) { // the convolution and the stream's update in one launch (a launch a row of step()'s) const int mode = il % blk_ != 0 ? 0 : il == core_start_ && looped ? 2 : 1; @@ -1809,7 +1899,7 @@ void Inkling2::load_extra(const uint8_t* src, int64_t pos) { // The full slots' rows of the whole context, keys then values a slot. size_t Inkling2::card_row_bytes() const { size_t n = 0; - for (const Slot& s : slots_) n += blocks_[(size_t) s.il].swa ? 0 : 1; + for (const Slot& s : slots_) n += blocks_[(size_t) s.il].swa || zha_on_ ? 0 : 1; // the index's: in RAM return n * 2 * kv_row(); } @@ -1833,6 +1923,7 @@ void Inkling2::load_card_rows(const uint8_t* src, int64_t from, int64_t count, i if (from < 0 || first < from || n < 0 || first + n > from + count || first + n > opt_.max_context) throw std::logic_error("load_rows: the rows are outside what was saved"); cover(first + n); + for (ZSlot& z : zs_) z.zpos = -1; // the index's ring and summaries again from its leaves ck(cudaStreamSynchronize(cs(st_)), "load rows"); const size_t row = kv_row(); for (const Slot& s : slots_) { @@ -1846,4 +1937,380 @@ void Inkling2::load_card_rows(const uint8_t* src, int64_t from, int64_t count, i } } + +// ---- the ZHA index over the full layers' keys -------------------------------------------------------------------- + +// STRATA_INK_ZHA=FILE: the ZHA index over the full layers (docs/INKLING_ZHA.md). The file: int32 'ZHAR', C, R, +// slots, then each slot's int32 layer, pass and [kv][R][128] floats (its reference queries). Its knobs, defaulting +// to what reached 1M tokens on an 8 GB card: STRATA_INK_ZHA_BUDGET (0.16: the share of a token's far chunks it reads +// exactly), STRATA_INK_ZHA_MAX (256: at most that many), STRATA_INK_ZHA_POOL (600: an answer's chunks kept in VRAM a +// KV head, 0 none). +void Inkling2::zha_load() { + const std::string path = zha_file(); + if (path.empty()) return; +#if defined(STRATA_GPU_METAL) || defined(STRATA_USE_HIP) + throw std::runtime_error("STRATA_INK_ZHA: the ZHA index runs on NVIDIA cards only"); +#else + const auto need = [](bool ok, const char* what) { + if (!ok) throw std::runtime_error(what); + }; + { + int dev = 0, major = 0, minor = 0; + const bool known = cudaGetDevice(&dev) == cudaSuccess && + cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && + cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess; + need(known && major * 10 + minor >= 75, "STRATA_INK_ZHA: the ZHA index needs an NVIDIA card from Turing on"); + } + need(opt_.layer_split.empty() && opt_.expert_gpu < 0, "STRATA_INK_ZHA: the ZHA index runs on one card"); + FILE* f = std::fopen(path.c_str(), "rb"); + if (!f) throw std::runtime_error("STRATA_INK_ZHA: cannot open " + path); + int32_t h[4]; + if (std::fread(h, sizeof h, 1, f) != 1 || h[0] != 0x5A484152) { + std::fclose(f); + throw std::runtime_error("STRATA_INK_ZHA: not a ZHAR file"); + } + zha_.C = h[1]; + zha_.R = h[2]; + if (head_dim_ != 128 || zha_.R != 4 || zha_.C != 64) { + std::fclose(f); + throw std::runtime_error("STRATA_INK_ZHA: heads of 128, chunks of 64 and 4 references"); + } + const size_t per = (size_t) n_kv_ * zha_.R * head_dim_; + zha_refs_.resize(per * (size_t) h[3]); + for (int k = 0; k < h[3]; ++k) { + int32_t id[2]; + if (std::fread(id, sizeof id, 1, f) != 1 || std::fread(zha_refs_.data() + k * per, sizeof(float), per, f) != per) { + std::fclose(f); + throw std::runtime_error("STRATA_INK_ZHA: the file ends early"); + } + zha_ids_.push_back({id[0], id[1]}); + } + std::fclose(f); + zha_.budget = (float) env_num("STRATA_INK_ZHA_BUDGET", 0.16); + zha_.max_sel = (int) env_num("STRATA_INK_ZHA_MAX", 256); + zha_pool_ = (int) env_num("STRATA_INK_ZHA_POOL", 600); + need(zha_.budget > 0.f && zha_.budget <= 1.f && zha_.max_sel >= 1 && zha_pool_ >= 0, + "STRATA_INK_ZHA: _BUDGET in (0, 1], _MAX 1 or more, _POOL 0 or more"); + zring_ = (int64_t) zha_.recent + 2 * zha_.C + std::max(opt_.prompt_batch, 64) + 64; + zha_max_chunks_ = (int) ((opt_.max_context + zha_.C - 1) / zha_.C + 31) / 32 * 32; // words of 32 chunks + zha_on_ = true; + std::fprintf(stderr, "strata-poly: ZHA index: chunks of %d, %d references, budget %.3f (at most %d), a pool of %d " + "chunks a KV head\n", zha_.C, zha_.R, zha_.budget, zha_.max_sel, zha_pool_); +#endif +} + +// A full slot's index: its leaves in pinned RAM (every position's keys and values), the ring of its recent rows, +// its reference queries, its summaries of the whole context and its answer's chunk cache in VRAM. +void Inkling2::zha_alloc(int slot, int il, int pass) { + ZSlot& S = zs_[(size_t) slot]; + if (S.kc) return; + const size_t codes = (size_t) n_kv_ * head_dim_, scales = codes / 32 * sizeof(uint16_t); + const size_t rows = (size_t) opt_.max_context; + const auto host = [&](size_t bytes) { + void* p = nullptr; + ck(cudaHostAlloc(&p, bytes, cudaHostAllocMapped | cudaHostAllocPortable), "the index's leaves (pinned RAM)"); + return p; + }; + S.kc = (int8_t*) host(rows * codes / 2); // 4-bit + S.ksc = (uint16_t*) host(rows * scales); + S.vc = (int8_t*) host(rows * codes / 2); + S.vsc = (uint16_t*) host(rows * scales); + S.rkc = (int8_t*) dmalloc((size_t) zring_ * codes, "the index's ring"); + S.rks = (uint16_t*) dmalloc((size_t) zring_ * scales, "the index's ring"); + S.rvc = (int8_t*) dmalloc((size_t) zring_ * codes, "the index's ring"); + S.rvs = (uint16_t*) dmalloc((size_t) zring_ * scales, "the index's ring"); + if (zha_pool_ > 0) { + ink::ZhaCache& Z = S.zcache; + const size_t keys = (size_t) n_kv_ * zha_pool_ * zha_.C, per_key = head_dim_ / 2; + Z.P = zha_pool_; + Z.pool.row = head_dim_; + Z.pool.headed = 0; + Z.pool.q4 = 1; + Z.pool.kc = (int8_t*) dmalloc(keys * per_key, "the index's chunk cache"); + Z.pool.vc = (int8_t*) dmalloc(keys * per_key, "the index's chunk cache"); + Z.pool.ks = (uint16_t*) dmalloc(keys * head_dim_ / 32 * sizeof(uint16_t), "the index's chunk cache"); + Z.pool.vs = (uint16_t*) dmalloc(keys * head_dim_ / 32 * sizeof(uint16_t), "the index's chunk cache"); + Z.owner = (int*) dmalloc((size_t) n_kv_ * Z.P * sizeof(int), "the index's chunk cache"); + Z.stamp = (int*) dmalloc((size_t) n_kv_ * Z.P * sizeof(int), "the index's chunk cache"); + Z.pmap = (int*) dmalloc((size_t) n_kv_ * zha_max_chunks_ * sizeof(int), "the index's chunk cache"); + Z.aux = (int*) dmalloc((size_t) n_kv_ * (Z.P + 2) * sizeof(int), "the index's chunk cache"); + ink::zha_cache_clear(Z, n_kv_, zha_max_chunks_, st_); + } + size_t k = 0; + while (k < zha_ids_.size() && zha_ids_[k] != std::make_pair(il, pass)) ++k; + if (k == zha_ids_.size()) + throw std::runtime_error("STRATA_INK_ZHA: no references for layer " + std::to_string(il) + " pass " + + std::to_string(pass)); + const size_t refs = (size_t) n_kv_ * zha_.R * head_dim_; + S.zrefs = (float*) dmalloc(refs * sizeof(float), "the index"); + ck(cudaMemcpy(S.zrefs, zha_refs_.data() + k * refs, refs * sizeof(float), cudaMemcpyHostToDevice), "the index"); + if (zcap0_ == 0) { // the summaries' room grows with the context where VRAM can come from the expert cache + zgrow_ = vmm_ && !pool_on_ && !freeze_experts_; + zcap0_ = zgrow_ ? std::min(zha_max_chunks_, 1024) : zha_max_chunks_; // 64K tokens first + } + S.zcap = 0; + S.zbuilt = 0; + S.zpos = -1; + zha_recap(S, zcap0_); +} + +// The slot's summaries in a room of cap chunks a KV head: the built ones copied over (each KV head's rows, then, as +// q8n lays them out, the scales after every head's codes), the old room freed. VRAM the card lacks for the new one +// comes from the expert cache; a smaller room's leftover goes back to it. +void Inkling2::zha_recap(ZSlot& S, int cap) { + if (cap == S.zcap) return; + const size_t R = (size_t) zha_.R, kv = (size_t) n_kv_; + const size_t code = head_dim_ / 2, scale = head_dim_ / 32 * sizeof(uint16_t); // 4-bit codes, fp16 scales + const size_t rows_new = kv * (size_t) cap * R, rows_old = kv * (size_t) S.zcap * R; + const size_t bytes_new = 2 * rows_new * (code + scale) + rows_new * sizeof(float); + const size_t bytes_old = 2 * rows_old * (code + scale) + rows_old * sizeof(float); + ck(cudaStreamSynchronize(cs(st_)), "the index's summaries"); + if (S.zk && exp_ && bytes_new > bytes_old) { + size_t free_b = 0, total_b = 0; + ck(cudaMemGetInfo(&free_b, &total_b), "cudaMemGetInfo"); + const size_t quarter = (size_t(std::max(reserve_mib_, 0)) << 20) / 4; + if (free_b < bytes_new + quarter) { + shrink_cache(bytes_new + quarter - free_b); + held_is_budget(); + } + } + void* p[3] = {}; + const size_t sizes[3] = {rows_new * (code + scale), rows_new * (code + scale), rows_new * sizeof(float)}; + for (int i = 0; i < 3; ++i) + if (cudaMalloc(&p[i], std::max(sizes[i], 256)) != cudaSuccess) { + cudaGetLastError(); + for (int k = 0; k < i; ++k) cudaFree(p[k]); + throw std::runtime_error("no VRAM for the index's summaries (" + std::to_string(cap) + " chunks a head)"); + } + const int keep = std::min(S.zbuilt, std::min(cap, S.zcap)); + if (S.zk && keep > 0) { + const size_t w = (size_t) keep * R; // rows a KV head to copy + uint16_t* const olds[2] = {S.zk, S.zv}; + for (int i = 0; i < 2; ++i) { + uint8_t* d = (uint8_t*) p[i]; + const uint8_t* s = (const uint8_t*) olds[i]; + ck(cudaMemcpy2DAsync(d, (size_t) cap * R * code, s, (size_t) S.zcap * R * code, w * code, kv, + cudaMemcpyDeviceToDevice, cs(st_)), "the index's summaries"); + ck(cudaMemcpy2DAsync(d + rows_new * code, (size_t) cap * R * scale, s + rows_old * code, + (size_t) S.zcap * R * scale, w * scale, kv, cudaMemcpyDeviceToDevice, cs(st_)), + "the index's summaries"); + } + ck(cudaMemcpy2DAsync(p[2], (size_t) cap * R * sizeof(float), S.zb, (size_t) S.zcap * R * sizeof(float), + w * sizeof(float), kv, cudaMemcpyDeviceToDevice, cs(st_)), "the index's summaries"); + ck(cudaStreamSynchronize(cs(st_)), "the index's summaries"); + } + for (void* o : {(void*) S.zk, (void*) S.zv, (void*) S.zb}) + if (o) cudaFree(o); + const bool had = S.zk != nullptr; + S.zk = (uint16_t*) p[0], S.zv = (uint16_t*) p[1], S.zb = (float*) p[2]; + S.zbuilt = keep; + S.zcap = cap; + if (had && exp_ && bytes_old > bytes_new + kArenaPiece) grow_cache(bytes_old - bytes_new, true); +} + +// A prompt's read starts (prompt_begins()): every slot's room for the chunks up to the read's end, now, while the +// expert cache still has VRAM to give: inside a long prompt the cache has lent all of it to the read's buffers. A +// read of max_read() rows is a prompt that goes on past it (its later reads would find no VRAM to grow into): the +// whole room at once. +void Inkling2::zha_room_all(int64_t end) { + int need = (int) ((end + zha_.C - 1) / zha_.C) + 1; + zread_full_ = end - pos_ >= read_rows_; + if (zread_full_) need = zha_max_chunks_; + for (ZSlot& S : zs_) + if (S.zk) zha_room(S, need); +} + +// Room for need chunks: twice the room until it holds them (at most max_chunks). +void Inkling2::zha_room(ZSlot& S, int need) { + if (!zgrow_ || need <= S.zcap) return; + int cap = S.zcap; + while (cap < need && cap < zha_max_chunks_) cap = std::min(zha_max_chunks_, cap * 2); + zha_recap(S, cap); +} + +// The index's work buffers: a prompt call's chunk scores, choices and selections, its staged chunks, an answer's +// parts. With the driver's virtual memory they are lent: mapped while a prompt is read, the expert cache's between. +void Inkling2::zha_scratch() { + if (zw_.S) return; + const int per = n_head_ / n_kv_; + const size_t sub = (size_t) rows_, extent = (size_t) std::max(window_, 1024); + zlend_ = vmm_; + const size_t need = (size_t) per * sub * extent + 65536 + (sub / 64 + 1) * (size_t) zha_max_chunks_ + + sub * per * 9 * (head_dim_ + 2) + (size_t) n_head_ * 128 * (head_dim_ + 2); + zw_.s_floats = std::max(size_t(4) << 20, need + (size_t(1) << 20)); + zfull_[7] = zw_.s_floats * sizeof(float); + if (zlend_) { // lent with the other prompt buffers + vmm_reserve(zmap_[7], zfull_[7]); + zw_.S = (float*) zmap_[7].base; + } else { + zw_.S = (float*) dmalloc(zfull_[7], "the index"); + } + const size_t hs = sub * zha_max_chunks_; + // lent, every KV head's staged chunks are kept across a slot's prompt calls (KEEP, from zkeep_from_ on): a chunk + // crosses the bus once a read's group, not once a call + zw_.heads = zlend_ ? n_kv_ : 1; + zw_.cmap = (int*) dmalloc((size_t) n_kv_ * zha_max_chunks_ * sizeof(int), "the index"); + zw_.count = (int*) dmalloc((size_t) n_kv_ * sizeof(int), "the index"); + zw_.glist = (int*) dmalloc((size_t) n_kv_ * (zha_max_chunks_ + 1) * sizeof(int), "the index"); + const size_t keys = (size_t) zw_.heads * zha_max_chunks_ * zha_.C, per_key = head_dim_ / 2; + zw_.stage.row = head_dim_; + zw_.stage.headed = 0; + zw_.stage.q4 = 1; + zfull_[0] = (size_t) (per + 1) * hs * sizeof(float); + zfull_[1] = zfull_[2] = (size_t) per * hs; + zfull_[3] = zfull_[4] = keys * per_key; + zfull_[5] = zfull_[6] = keys * head_dim_ / 32 * sizeof(uint16_t); + void* p[7]; + for (int i = 0; i < 7; ++i) { + if (zlend_) { // addresses now, VRAM when a prompt starts + vmm_reserve(zmap_[i], zfull_[i]); + p[i] = (void*) zmap_[i].base; + } else { + p[i] = dmalloc(zfull_[i], "the index"); + } + } + zw_.score = (float*) p[0]; + zw_.rstar = (uint8_t*) p[1]; + zw_.sel = (uint8_t*) p[2]; + zw_.stage.kc = (int8_t*) p[3]; + zw_.stage.vc = (int8_t*) p[4]; + zw_.stage.ks = (uint16_t*) p[5]; + zw_.stage.vs = (uint16_t*) p[6]; + if (zlend_) { // an answer's token's own: rows of 4 at most, the same maps and lists + const size_t hd = (size_t) 4 * zha_max_chunks_; + zwd_ = zw_; + zwd_.s_floats = size_t(2) << 20; + zwd_.S = (float*) dmalloc(zwd_.s_floats * sizeof(float), "the index"); + zwd_.score = (float*) dmalloc((size_t) (n_head_ + n_kv_) * hd * sizeof(float), "the index"); + zwd_.rstar = (uint8_t*) dmalloc((size_t) n_head_ * hd, "the index"); + zwd_.sel = (uint8_t*) dmalloc((size_t) n_head_ * hd, "the index"); + zwd_.heads = 1; + } +} + +// The prompt buffers mapped, the expert cache giving up what the card has not free; with `keep` the staging for every +// KV head (KEEP: a deep prompt's chunks cross the bus once a read's group), else for one. +void Inkling2::zha_widen(bool keep) { + const int heads = keep && zw_.heads == n_kv_ ? n_kv_ : 1; + size_t want[8] = {}; + const int nm = zmap_[7].base ? 8 : 7; // S where it is lent + for (int i = 0; i < nm; ++i) + want[i] = i >= 3 && i < 7 && zw_.heads > 1 ? zfull_[i] / zw_.heads * heads : zfull_[i]; + bool enough = zwide_; + for (int i = 0; i < nm && enough; ++i) enough = zmap_[i].mapped >= want[i]; + if (enough) { + zkeep_now_ = std::max(zkeep_now_, heads); + return; + } + ck(cudaStreamSynchronize(cs(st_)), "the index's prompt buffers"); + const size_t grain = pool_on_ ? pg_ : grain_; + const auto whole = [&](size_t x) { return (x + grain - 1) / grain * grain; }; + size_t more = 0; + for (int i = 0; i < nm; ++i) more += whole(want[i]) > zmap_[i].mapped ? whole(want[i]) - zmap_[i].mapped : 0; + size_t free_b = 0, total_b = 0; + 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; + if (exp_ && free_b < more + quarter) { + const size_t had = arena_.mapped; + shrink_cache(more + quarter - free_b); + held_is_budget(); + zcut_ += had - arena_.mapped; + } + int mapped_heads = heads; + try { + for (int i = 0; i < nm; ++i) vmm_map(zmap_[i], want[i], SIZE_MAX, "the index's prompt buffers"); + } catch (const std::runtime_error&) { + if (heads == 1) throw; + // no VRAM for every head's staging: one head's, which an earlier prompt or the first try may have mapped + mapped_heads = 1; + for (int i = 0; i < nm; ++i) + vmm_map(zmap_[i], i >= 3 && i < 7 ? zfull_[i] / zw_.heads : zfull_[i], SIZE_MAX, + "the index's prompt buffers"); + } + zwide_ = true; + zkeep_now_ = mapped_heads; + zw_owner_ = nullptr; // nothing staged survives the buffers' time away +} + +// The prompt is read: the expert cache takes the buffers' VRAM back. +void Inkling2::zha_narrow() { + if (!zwide_) return; + ck(cudaStreamSynchronize(cs(st_)), "the index's prompt buffers"); + for (int i = 0; i < (zmap_[7].base ? 8 : 7); ++i) vmm_unmap(zmap_[i], 0); + zwide_ = false; + zkeep_now_ = 1; + zw_owner_ = nullptr; + if (exp_ && zcut_ > 0) grow_cache(zcut_, true); + zcut_ = 0; +} + + +// A full layer's call under the index: its rows' keys and values (k_, v_) into the leaves and the ring, the chunks +// they complete summarized, the attention into att_ (before the output gate). +void Inkling2::zha_attention(int il, int n) { + const Block& B = blocks_[(size_t) il]; + ZSlot& S = zs_[(size_t) slot_of(il, pass_of(il))]; + const int hk = n_kv_ * head_dim_, C = zha_.C; + ink::store_q4(k_, S.kc, S.ksc, hk, pos_, n, 0, st_); // the leaves: 4-bit + ink::store_q4(v_, S.vc, S.vsc, hk, pos_, n, 0, st_); + { // and the recent rows' ring in VRAM + const int64_t first = pos_ % zring_; + const int head = (int) std::min(n, zring_ - first); + store_q8(k_, S.rkc, S.rks, hk, first, head, st_); + store_q8(v_, S.rvc, S.rvs, hk, first, head, st_); + if (head < n) { + store_q8(k_ + (size_t) head * hk, S.rkc, S.rks, hk, 0, n - head, st_); + store_q8(v_ + (size_t) head * hk, S.rvc, S.rvs, hk, 0, n - head, st_); + } + } + ink::ZhaKv leaves, ring; + leaves.kc = S.kc, leaves.ks = S.ksc, leaves.vc = S.vc, leaves.vs = S.vsc; + leaves.q4 = 1; + ring.kc = S.rkc, ring.ks = S.rks, ring.vc = S.rvc, ring.vs = S.rvs; + leaves.row = ring.row = (long long) hk; + const int64_t n_ring = zring_; + // A jump in the sequence (a new prompt, a restored state, guesses taken back): the ring's rows before the call + // again from the leaves, and the summaries of the chunks before it from the leaves. + if (pos_ != S.zpos) { + const int64_t refill = std::max(0, pos_ - (zring_ - rows_)); + if (refill < pos_) { // 4-bit leaves to the Q8_0 ring + ink::q4_to_q8(S.kc, S.ksc, refill, S.rkc, S.rks, refill, zring_, hk, (int) (pos_ - refill), st_); + ink::q4_to_q8(S.vc, S.vsc, refill, S.rvc, S.rvs, refill, zring_, hk, (int) (pos_ - refill), st_); + } + S.zbuilt = std::min(S.zbuilt, (int) (pos_ / C)); + ink::zha_cache_clear(S.zcache, n_kv_, zha_max_chunks_, st_); + // the read under way's end too: its start (prompt_begins()) gave the room for all of it + const int want = (int) (std::max(pos_ + n, read_end_) / C) + 1; + if (zgrow_ && !zread_full_ && S.zcap > zcap0_ && 4 * want <= S.zcap) { // a short context again + int cap = zcap0_; + while (cap < want) cap *= 2; + zha_recap(S, std::min(cap, zha_max_chunks_)); + } + zha_room(S, (int) (pos_ / C)); + ink::zha_build(S.zrefs, leaves, (int64_t) opt_.max_context + 1, S.zbuilt, (int) (pos_ / C), C, zha_.R, n_kv_, + zha_max_chunks_, S.zk, S.zv, S.zb, st_, zha_q8n(S.zcap)); + S.zbuilt = (int) (pos_ / C); + } + const int done = (int) ((pos_ + n) / C); // the chunks the call completes: from the ring + zha_room(S, done); + ink::zha_build(S.zrefs, ring, n_ring, S.zbuilt, done, C, zha_.R, n_kv_, zha_max_chunks_, S.zk, S.zv, S.zb, st_, + zha_q8n(S.zcap)); + S.zbuilt = std::max(S.zbuilt, done); + S.zpos = pos_ + n; + // an answer's token through the chunk cache takes its own small buffers; anything else the prompt's (a + // prompt's read maps them at its start: take_prompt()) + const bool small = zlend_ && n == 1 && zha_pool_ > 0; + if (zlend_ && !small && !zwide_) zha_widen(false); + ink::ZhaWork wkp = zw_; + if (zlend_) wkp.heads = zkeep_now_; // KEEP where the staging is every head's + const ink::ZhaWork& wk = small ? zwd_ : wkp; + // a prompt call that goes on from this slot's last one keeps what it staged + const bool keep = n >= kStageRows && zw_owner_ == &S && zw_next_ == pos_; + ink::zha_attend(blas_, zha_, q_, leaves, ring, n_ring, S.zk, S.zv, S.zb, S.zbuilt, zha_max_chunks_, pos_, n, n_head_, + n_kv_, rr_, fp(B.rel_bias), B.extent, n_rel_, sink_, wk, att_, st_, &S.zcache, keep, S.zcap); + zw_owner_ = &S; + zw_next_ = n >= kStageRows ? pos_ + n : -1; +} + } // namespace strata::poly diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index c5d7266393..816f93afa3 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -953,6 +953,7 @@ __device__ __forceinline__ void qkv_block(const InkQkv& a, float k_norm, float v if (is_v) rms_head(head, a.dim, a.eps, part); else clip_head(head, nullptr, a.qk_thr, a.dim, 0.0f, 1.0f, p, part); const long long row = a.ring > 0 ? p % a.ring : p; + if (!(is_v ? a.vals : a.keys)) return; // a full layer under the ZHA index keeps its keys itself if (KV != kKvF16) kv_store(head, is_v ? a.vals : a.keys, row, a.n_kv, a.dim, h, part); else if (i < a.dim) ((__half*) (is_v ? a.vals : a.keys))[size_t(row) * channels + size_t(h) * a.dim + i] = __float2half(head[i]); diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 9b3b442939..5628f0f1a8 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -16,6 +16,11 @@ #include "engine.hpp" #include "inkling2_kernels.hpp" +#include "inkling2_zha.hpp" + +#include +#include +#include namespace strata::poly { @@ -238,6 +243,65 @@ class Inkling2 final : public Engine { *ds_ctxf_ = nullptr, *ds_in_ = nullptr, *ds_sp_ = nullptr, *ds_state_ = nullptr, *ds_lat_ = nullptr, *ds_frc_ = nullptr, *ds_approx_ = nullptr, *ds_hscr_ = nullptr; void *ds_yq_ = nullptr, *ds_hq_ = nullptr, *ds_latq_ = nullptr; + // STRATA_INK_ZHA=REFS: the full layers' attention through a ZHA index (arXiv 2610.42112): their keys and values + // in pinned RAM as 4-bit leaves, chunk summaries in VRAM, a ring of the recent rows; each token reads the chunks + // the summaries pick exactly and the others by their summaries (inkling2_zha.cu). The rest of the cache: none. + struct ZSlot { + int8_t *kc = nullptr, *vc = nullptr; ///< the leaves: codes, 4 bits a value, a row a position + uint16_t *ksc = nullptr, *vsc = nullptr; ///< their fp16 scales, a 32 values + int8_t *rkc = nullptr, *rvc = nullptr; ///< the ring of the last zring_ rows, Q8_0 codes, in VRAM + uint16_t *rks = nullptr, *rvs = nullptr; + float* zrefs = nullptr; ///< the reference queries [kv][R][128] + uint16_t *zk = nullptr, *zv = nullptr; ///< the chunks' summaries + float* zb = nullptr; + int zbuilt = 0; ///< the chunks summarized + int zcap = 0; ///< the summaries' room, chunks a KV head (it grows: zha_recap()) + int64_t zpos = -1; ///< the position after the last call's rows (-1: rebuild) + ink::ZhaCache zcache; ///< an answer's chunks in VRAM + }; + std::vector zs_; ///< a slot's, by its index + ink::Zha zha_; + bool zha_on_ = false; + int zha_pool_ = 0, zha_max_chunks_ = 0; + int64_t zring_ = 0; + std::vector zha_refs_; + std::vector> zha_ids_; + ink::ZhaWork zw_; + const void* zw_owner_ = nullptr; + int64_t zw_next_ = -1; + bool zha_full(const Block& B) const { return zha_on_ && !B.swa; } + size_t zha_q8n(int cap) const { + return ink::zha_q8n_make((size_t) n_kv_ * cap * zha_.R, cap, true); // 4-bit summaries + } + bool zgrow_ = false; ///< the summaries grow with the context + int zcap0_ = 0; ///< a slot's first room + void zha_recap(ZSlot& S, int cap); + void zha_room(ZSlot& S, int need); + void zha_room_all(int64_t end); + bool zread_full_ = false; ///< the read under way is max_read()'s: the prompt goes on, the room is whole + void zha_load(); + void zha_alloc(int slot, int il, int pass); + void zha_scratch(); + void zha_attention(int il, int n); + // the prompt buffers lent (with the driver's virtual memory): score, rstar, sel, stage kc, vc, ks, vs, S + Mapped zmap_[8]; // 0-6: zha_scratch()'s p[], 7: S + size_t zfull_[8] = {}; + bool zlend_ = false, zwide_ = false; + int zkeep_now_ = 1; ///< the heads the prompt staging is mapped for now + size_t zcut_ = 0; + ink::ZhaWork zwd_; ///< an answer's token's (rows <= 4) when the prompt's are lent + static constexpr int64_t zkeep_from_ = 131072; ///< KEEP (every KV head's staging kept) for prompts from here on + void zha_widen(bool keep); + void zha_narrow(); + void prompt_begins() override { + if (zgrow_) zha_room_all(std::max(pos_, read_end_)); // before the read takes the VRAM + if (zlend_) zha_widen(std::max(pos_, read_end_) >= zkeep_from_); + } + void prompt_ends() override { if (zlend_) zha_narrow(); } + void head_part(Layer& L, int il, int n, int part); + void* grab(const std::function& work); + std::vector zgraph_; ///< a full slot's two recorded parts of its head (an answer's token) + void* side_ = nullptr; ///< fork()'s stream void *side_go_ = nullptr, *side_done_ = nullptr; ///< its fork and its join diff --git a/engine/src/poly/inkling2_zha.cu b/engine/src/poly/inkling2_zha.cu new file mode 100644 index 0000000000..47a618f4a9 --- /dev/null +++ b/engine/src/poly/inkling2_zha.cu @@ -0,0 +1,2491 @@ +// src/poly/inkling2_zha.cu - the ZHA index of Inkling 2's full layers (STRATA_INK_ZHA, docs/INKLING_ZHA.md): the +// summaries of 64-key chunks, their scores, the chunks a token reads exactly, the tail through the summaries, and the +// answer's chunk cache in VRAM. The method: "ZHA" (Zyphra Hierarchical Attention), arXiv 2610.42112. +// +// The kernels use mma.sync and cp.async: NVIDIA only (compute capability 7.5 and up; the load refuses the index +// elsewhere). A HIP build gets entry points that say so. +#include "inkling2_zha.hpp" + +#include + +#if defined(__HIPCC__) || defined(STRATA_USE_HIP) + +namespace strata::poly::ink { +namespace { +[[noreturn]] void nvidia_only() { throw std::runtime_error("the ZHA index (STRATA_INK_ZHA) runs on NVIDIA cards only"); } +} // namespace +void store_q4(const float*, int8_t*, uint16_t*, int, long long, int, long long, void*) { nvidia_only(); } +void q4_to_q8(const int8_t*, const uint16_t*, long long, int8_t*, uint16_t*, long long, long long, int, int, void*) { + nvidia_only(); +} +void zha_cache_clear(ZhaCache&, int, int, void*) { nvidia_only(); } +void zha_build(const float*, const ZhaKv&, long long, int, int, int, int, int, int, uint16_t*, uint16_t*, float*, void*, + size_t) { + nvidia_only(); +} +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) { + nvidia_only(); +} +} // namespace strata::poly::ink + +#else + +#include +#include +#include + +#include +#include + +namespace strata::poly::ink { +namespace { + +inline cudaStream_t cs(void* s) { return (cudaStream_t) s; } +inline unsigned grid(size_t n, unsigned b) { return (unsigned) ((n + b - 1) / b); } + +__device__ __forceinline__ float warp_sum(float v) { + for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o); + return v; +} + +// ---- ZHA: summaries, chunk scores, the attention through them -------------------------------------------------- +constexpr int kZhaDim = 128; +// an answer's chunk the cache has just taken (zha_assign*): its place in cmap with this bit, until the fill is done +constexpr int kZhaMiss = 1 << 30; + +__device__ __forceinline__ void load4h(const __half* p, float (&x)[4]) { + const uint2 u = *reinterpret_cast(p); + const __half2 a = *reinterpret_cast(&u.x), b = *reinterpret_cast(&u.y); + const float2 fa = __half22float2(a), fb = __half22float2(b); + x[0] = fa.x, x[1] = fa.y, x[2] = fb.x, x[3] = fb.y; +} + +__device__ __forceinline__ void store4h(__half* p, const float (&x)[4]) { + __half2 a = __floats2half2_rn(x[0], x[1]), b = __floats2half2_rn(x[2], x[3]); + uint2 u; + u.x = *reinterpret_cast(&a); + u.y = *reinterpret_cast(&b); + *reinterpret_cast(p) = u; +} + +// Key k's 4 values from `lane` of KV head g (offset g * 128 when the store is a cache's, else 0), and its value's. +__device__ __forceinline__ void zload(const ZhaKv& s, long long k, int g, int lane, float (&kx)[4], float (&vx)[4]) { + const size_t at = (size_t) k * s.row + (s.headed ? (size_t) g * kZhaDim : 0) + 4 * lane; + if (s.q4) { // 4 codes in 2 bytes + const uint16_t a = *reinterpret_cast(s.kc + at / 2), b = *reinterpret_cast(s.vc + at / 2); + const float da = __half2float(__ushort_as_half(s.ks[at / 32])), db = __half2float(__ushort_as_half(s.vs[at / 32])); +#pragma unroll + for (int i = 0; i < 4; ++i) { + kx[i] = (float) ((int) ((a >> (4 * i)) & 15) - 8) * da; + vx[i] = (float) ((int) ((b >> (4 * i)) & 15) - 8) * db; + } + } else if (s.kc) { + const char4 a = *reinterpret_cast(s.kc + at), b = *reinterpret_cast(s.vc + at); + const float da = __half2float(__ushort_as_half(s.ks[at / 32])), db = __half2float(__ushort_as_half(s.vs[at / 32])); + kx[0] = a.x * da, kx[1] = a.y * da, kx[2] = a.z * da, kx[3] = a.w * da; + vx[0] = b.x * db, vx[1] = b.y * db, vx[2] = b.z * db, vx[3] = b.w * db; + } else { + load4h((const __half*) s.k16 + at, kx); + load4h((const __half*) s.v16 + at, vx); + } +} + +__host__ __device__ __forceinline__ size_t zq_rows(size_t q8n) { return q8n & 0xffffffffull; } +// The summaries' chunks a KV head (their room, a slot's own since it grows with the context): q8n's bits 32-61, or +// max_chunks where they are 0 (fp16 summaries, or a caller that gives none) +__host__ __device__ __forceinline__ int zq_cap(size_t q8n, int max_chunks) { + const int c = (int) ((q8n >> 32) & 0x3fffffffull); + return c > 0 ? c : max_chunks; +} +__host__ __device__ __forceinline__ bool zq_4(size_t q8n) { return (q8n & kZhaQ4) != 0; } +__device__ __forceinline__ const uint16_t* zq_scales(const void* codes, size_t q8n) { + return reinterpret_cast(reinterpret_cast(codes) + + zq_rows(q8n) * (zq_4(q8n) ? kZhaDim / 2 : kZhaDim)); +} + +// Summary row `row` (of khat or vhat: fp16, or with q8n its rows' int8 or 4-bit codes then their fp16 scales a 32 +// values). +__device__ __forceinline__ void zsum4(const __half* base, size_t q8n, size_t row, int lane, float (&x)[4]) { + if (q8n && zq_4(q8n)) { + const uint8_t* codes = reinterpret_cast(base); + const uint16_t b2 = *reinterpret_cast(codes + row * (kZhaDim / 2) + 2 * lane); + const float d = __half2float(__ushort_as_half(zq_scales(base, q8n)[row * 4 + lane / 8])); +#pragma unroll + for (int i = 0; i < 4; ++i) x[i] = (float) ((int) ((b2 >> (4 * i)) & 15) - 8) * d; + } else if (q8n) { + const int8_t* codes = reinterpret_cast(base); + const uint16_t* sc = reinterpret_cast(codes + q8n * kZhaDim); + const char4 c = *reinterpret_cast(codes + row * kZhaDim + 4 * lane); + const float d = __half2float(__ushort_as_half(sc[row * 4 + lane / 8])); + x[0] = c.x * d, x[1] = c.y * d, x[2] = c.z * d, x[3] = c.w * d; + } else { + load4h(base + row * kZhaDim + 4 * lane, x); + } +} + +__device__ __forceinline__ uint4 zsum8h(const __half* base, size_t q8n, size_t row, int d) { // 8 values as fp16 + if (!q8n) return *reinterpret_cast(base + row * kZhaDim + d); + if (zq_4(q8n)) { + const uint32_t a4 = *reinterpret_cast(reinterpret_cast(base) + + row * (kZhaDim / 2) + d / 2); + const float s = __half2float(__ushort_as_half(zq_scales(base, q8n)[row * 4 + d / 32])); + uint4 out; + uint32_t* o = &out.x; + for (int e = 0; e < 4; ++e) { + __half2 h = __floats2half2_rn((float) ((int) ((a4 >> (8 * e)) & 15) - 8) * s, + (float) ((int) ((a4 >> (8 * e + 4)) & 15) - 8) * s); + o[e] = *reinterpret_cast(&h); + } + return out; + } + const int8_t* codes = reinterpret_cast(base); + const uint16_t* sc = reinterpret_cast(codes + q8n * kZhaDim); + const uint2 a = *reinterpret_cast(codes + row * kZhaDim + d); + const int8_t* b = reinterpret_cast(&a); + const float s = __half2float(__ushort_as_half(sc[row * 4 + d / 32])); + uint4 out; + uint32_t* o = &out.x; + for (int e = 0; e < 4; ++e) { + __half2 h = __floats2half2_rn(b[2 * e] * s, b[2 * e + 1] * s); + o[e] = *reinterpret_cast(&h); + } + return out; +} + +// A launch block a chunk and KV head, a warp a reference at a time, a lane 4 values: the online softmax of the +// reference's scores over the chunk's keys (from the ring), its keys and values summed with those weights. +__global__ void zha_build_kernel(const float* __restrict__ refs, ZhaKv ring, long long n_ring, int c0, int C, int R, + int max_chunks, __half* __restrict__ khat, __half* __restrict__ vhat, + float* __restrict__ bhat, size_t q8n) { + const int c = c0 + blockIdx.x, g = blockIdx.y, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const float sc = rsqrtf((float) kZhaDim); + for (int r = w; r < R; r += blockDim.x >> 5) { + const float4 qr = *reinterpret_cast(refs + ((size_t) g * R + r) * kZhaDim + 4 * lane); + float m = -INFINITY, l = 0.f, ak[4] = {0.f, 0.f, 0.f, 0.f}, av[4] = {0.f, 0.f, 0.f, 0.f}; + for (int j = 0; j < C; ++j) { + float k[4], v[4]; + zload(ring, ((long long) c * C + j) % n_ring, g, lane, k, v); + const float sv = warp_sum(qr.x * k[0] + qr.y * k[1] + qr.z * k[2] + qr.w * k[3]) * sc; + const float mn = fmaxf(m, sv), f = expf(m - mn), e = expf(sv - mn); + l = l * f + e; +#pragma unroll + for (int i = 0; i < 4; ++i) ak[i] = ak[i] * f + e * k[i], av[i] = av[i] * f + e * v[i]; + m = mn; + } + float kh[4], vh[4]; +#pragma unroll + for (int i = 0; i < 4; ++i) kh[i] = ak[i] / l, vh[i] = av[i] / l; + const float dot = warp_sum(qr.x * kh[0] + qr.y * kh[1] + qr.z * kh[2] + qr.w * kh[3]) * sc; + const size_t row = ((size_t) g * zq_cap(q8n, max_chunks) + c) * R + r, o = row * kZhaDim + 4 * lane; + if (q8n && zq_4(q8n)) { // 4-bit codes, a scale a 32 values (8 lanes) + float ak2 = fmaxf(fmaxf(fabsf(kh[0]), fabsf(kh[1])), fmaxf(fabsf(kh[2]), fabsf(kh[3]))); + float av2 = fmaxf(fmaxf(fabsf(vh[0]), fabsf(vh[1])), fmaxf(fabsf(vh[2]), fabsf(vh[3]))); + for (int x = 1; x < 8; x <<= 1) { + ak2 = fmaxf(ak2, __shfl_xor_sync(0xffffffffu, ak2, x)); + av2 = fmaxf(av2, __shfl_xor_sync(0xffffffffu, av2, x)); + } + const float dk = ak2 / 7.f, dv = av2 / 7.f, ik = dk > 0.f ? 1.f / dk : 0.f, iv = dv > 0.f ? 1.f / dv : 0.f; + uint16_t ck = 0, cv = 0; +#pragma unroll + for (int i = 0; i < 4; ++i) { + ck |= (uint16_t) (min(7, max(-8, (int) lrintf(kh[i] * ik))) + 8) << (4 * i); + cv |= (uint16_t) (min(7, max(-8, (int) lrintf(vh[i] * iv))) + 8) << (4 * i); + } + uint8_t* kc = reinterpret_cast(khat); + uint8_t* vc = reinterpret_cast(vhat); + *reinterpret_cast(kc + row * (kZhaDim / 2) + 2 * lane) = ck; + *reinterpret_cast(vc + row * (kZhaDim / 2) + 2 * lane) = cv; + if ((lane & 7) == 0) { + const_cast(zq_scales(kc, q8n))[row * 4 + lane / 8] = __half_as_ushort(__float2half(dk)); + const_cast(zq_scales(vc, q8n))[row * 4 + lane / 8] = __half_as_ushort(__float2half(dv)); + } + } else if (q8n) { // int8 codes, a scale a 32 values (8 lanes) + float ak2 = fmaxf(fmaxf(fabsf(kh[0]), fabsf(kh[1])), fmaxf(fabsf(kh[2]), fabsf(kh[3]))); + float av2 = fmaxf(fmaxf(fabsf(vh[0]), fabsf(vh[1])), fmaxf(fabsf(vh[2]), fabsf(vh[3]))); + for (int x = 1; x < 8; x <<= 1) { + ak2 = fmaxf(ak2, __shfl_xor_sync(0xffffffffu, ak2, x)); + av2 = fmaxf(av2, __shfl_xor_sync(0xffffffffu, av2, x)); + } + const float dk = ak2 / 127.f, dv = av2 / 127.f, ik = dk > 0.f ? 1.f / dk : 0.f, iv = dv > 0.f ? 1.f / dv : 0.f; + int8_t* kc = reinterpret_cast(khat); + int8_t* vc = reinterpret_cast(vhat); + *reinterpret_cast(kc + o) = make_char4((signed char) lrintf(kh[0] * ik), (signed char) lrintf(kh[1] * ik), + (signed char) lrintf(kh[2] * ik), (signed char) lrintf(kh[3] * ik)); + *reinterpret_cast(vc + o) = make_char4((signed char) lrintf(vh[0] * iv), (signed char) lrintf(vh[1] * iv), + (signed char) lrintf(vh[2] * iv), (signed char) lrintf(vh[3] * iv)); + if ((lane & 7) == 0) { + reinterpret_cast(kc + q8n * kZhaDim)[row * 4 + lane / 8] = __half_as_ushort(__float2half(dk)); + reinterpret_cast(vc + q8n * kZhaDim)[row * 4 + lane / 8] = __half_as_ushort(__float2half(dv)); + } + } else { + store4h(khat + o, kh); + store4h(vhat + o, vh); + } + if (lane == 0) bhat[((size_t) g * zq_cap(q8n, max_chunks) + c) * R + r] = m + logf(l) - dot; + } +} + +// A 32-value block of a row a warp's 8 lanes (4 values each): its largest magnitude / 7 the scale, codes +// round(x / scale) + 8 in [0, 15]; the block's 16 bytes go out in one store (the codes may be in RAM over PCIe). +__global__ void store_q4_kernel(const float* __restrict__ x, int8_t* __restrict__ codes, uint16_t* __restrict__ scales, + int n, long long pos, int rows, long long ring) { + const size_t i = ((size_t) blockIdx.x * blockDim.x + threadIdx.x) / 8, per = n / 32; + const int sub = threadIdx.x & 7; // the lane's 4 values of the block + const bool live = i < (size_t) rows * per; + const int r = live ? (int) (i / per) : 0, b = live ? (int) (i % per) : 0; + float4 v = make_float4(0.f, 0.f, 0.f, 0.f); + if (live) v = *reinterpret_cast(x + (size_t) r * n + 32 * b + 4 * sub); + float amax = fmaxf(fmaxf(fabsf(v.x), fabsf(v.y)), fmaxf(fabsf(v.z), fabsf(v.w))); + for (int o = 4; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o, 8)); + const float d = amax / 7.f, inv = d > 0.f ? 1.f / d : 0.f; + const auto q = [&](float f) { return (uint32_t) min(15, max(0, (int) lrintf(f * inv) + 8)); }; + const uint32_t mine = q(v.x) | (q(v.y) << 4) | (q(v.z) << 8) | (q(v.w) << 12); // 2 bytes + // lanes sub 0..7 hold bytes 2 sub, 2 sub + 1: gather them in lane 0 of the 8 as 4 words + uint32_t w0 = mine | (__shfl_down_sync(0xffffffffu, mine, 1, 8) << 16); + uint32_t w1 = __shfl_down_sync(0xffffffffu, mine, 2, 8) | (__shfl_down_sync(0xffffffffu, mine, 3, 8) << 16); + uint32_t w2 = __shfl_down_sync(0xffffffffu, mine, 4, 8) | (__shfl_down_sync(0xffffffffu, mine, 5, 8) << 16); + uint32_t w3 = __shfl_down_sync(0xffffffffu, mine, 6, 8) | (__shfl_down_sync(0xffffffffu, mine, 7, 8) << 16); + if (!live || sub != 0) return; + long long row = pos + r; + if (ring > 0) row %= ring; + *reinterpret_cast(codes + (size_t) row * (n / 2) + 16 * b) = make_uint4(w0, w1, w2, w3); + scales[(size_t) row * per + b] = __half_as_ushort(__float2half(d)); +} + +// A block of 32 values a thread: 4-bit codes back to values, then to Q8_0 (scale = largest magnitude / 127). +__global__ void q4_to_q8_kernel(const int8_t* __restrict__ sc, const uint16_t* __restrict__ ss, long long from, + int8_t* __restrict__ dc, uint16_t* __restrict__ ds, long long to, long long ring, int n, + int rows) { + const size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x, per = n / 32; + if (i >= (size_t) rows * per) return; + const int r = (int) (i / per), b = (int) (i % per); + const uint8_t* c = reinterpret_cast(sc) + (size_t) (from + r) * (n / 2) + 16 * b; + const float d = __half2float(__ushort_as_half(ss[(size_t) (from + r) * per + b])); + float v[32], amax = 0.f; + for (int j = 0; j < 16; ++j) { + v[2 * j] = (float) ((int) (c[j] & 15) - 8) * d; + v[2 * j + 1] = (float) ((int) (c[j] >> 4) - 8) * d; + } + for (int j = 0; j < 32; ++j) amax = fmaxf(amax, fabsf(v[j])); + const float d8 = amax / 127.f, inv = d8 > 0.f ? 1.f / d8 : 0.f; + const long long row = ring > 0 ? (to + r) % ring : to + r; + int8_t* o = dc + (size_t) row * n + 32 * b; + for (int j = 0; j < 32; ++j) o[j] = (int8_t) lrintf(v[j] * inv); + ds[(size_t) row * per + b] = __half_as_ushort(__float2half(d8)); +} + +__device__ __forceinline__ int zha_far(long long own, int C, int recent, long long* fr) { + *fr = max(1LL, own + 1 - recent) / C * C; // the first recent key: a chunk's first + return max(0, (int) (*fr / C) - 1); // far chunks 1..nf +} + +// The selection's scores of a KV group: for each far chunk, the best of the group's heads' scores less that head's +// best, so a chunk one head needs ranks as high as one another head needs. One block a token. +__global__ void zha_pick_kernel(const float* __restrict__ score, int heads, size_t head_stride, int max_chunks, + long long pos, int C, int recent, float* __restrict__ pick) { + __shared__ float best[8][4]; + const int t = blockIdx.x, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + score += (size_t) blockIdx.y * heads * head_stride; // group blockIdx.y's heads, and its pick + pick += (size_t) blockIdx.y * head_stride; + long long fr; + const int nf = zha_far(pos + t, C, recent, &fr); + float top[8]; + for (int h = 0; h < heads; ++h) { + float m = -INFINITY; + for (int i = threadIdx.x; i < nf; i += blockDim.x) m = fmaxf(m, score[h * head_stride + (size_t) t * max_chunks + 1 + i]); + for (int o = 16; o > 0; o >>= 1) m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, o)); + if (lane == 0) best[h][w] = m; + } + __syncthreads(); + for (int h = 0; h < heads; ++h) top[h] = fmaxf(fmaxf(best[h][0], best[h][1]), fmaxf(best[h][2], best[h][3])); + for (int i = threadIdx.x; i < nf; i += blockDim.x) { + float v = -INFINITY; + for (int h = 0; h < heads; ++h) v = fmaxf(v, score[h * head_stride + (size_t) t * max_chunks + 1 + i] - top[h]); + pick[(size_t) t * max_chunks + 1 + i] = v; + } +} + +// A token's selection: the B best of its far chunks by `score` (bisection on the threshold), a byte each into sel +// (chunk c at c; chunks past the far ones 0). One block of 128 a token. +__global__ void zha_select_kernel(const float* __restrict__ score, int max_chunks, long long pos, int C, int recent, + float budget, int min_chunks, int max_sel, uint8_t* __restrict__ sel, size_t stride) { + __shared__ float part[4], part2[4], lo_s, hi_s; + const int t = blockIdx.x, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + score += blockIdx.y * stride; // selection blockIdx.y's scores and bytes + sel += blockIdx.y * stride; + long long fr; + const int nf = zha_far(pos + t, C, recent, &fr); + const float* s = score + (size_t) t * max_chunks + 1; + uint8_t* out = sel + (size_t) t * max_chunks; + float mn = INFINITY, mx = -INFINITY; + for (int i = threadIdx.x; i < nf; i += blockDim.x) mn = fminf(mn, s[i]), mx = fmaxf(mx, s[i]); + for (int o = 16; o > 0; o >>= 1) { + mn = fminf(mn, __shfl_xor_sync(0xffffffffu, mn, o)); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, o)); + } + if (lane == 0) part[w] = mn, part2[w] = mx; + __syncthreads(); + if (threadIdx.x == 0) { + lo_s = fminf(fminf(part[0], part[1]), fminf(part[2], part[3])) - 1.f; + hi_s = fmaxf(fmaxf(part2[0], part2[1]), fmaxf(part2[2], part2[3])); + } + __syncthreads(); + const int B = nf ? min(nf, max(min_chunks, min(max_sel, (int) lrintf(budget * nf)))) : 0; + for (int it = 0; it < 30 && nf; ++it) { + const float mid = 0.5f * (lo_s + hi_s); + int cnt = 0; + for (int i = threadIdx.x; i < nf; i += blockDim.x) cnt += s[i] > mid; + for (int o = 16; o > 0; o >>= 1) cnt += __shfl_xor_sync(0xffffffffu, cnt, o); + __syncthreads(); + if (lane == 0) part[w] = (float) cnt; + __syncthreads(); + if (threadIdx.x == 0) { + if (part[0] + part[1] + part[2] + part[3] >= B) lo_s = mid; + else hi_s = mid; + } + __syncthreads(); + } + for (int c = threadIdx.x; c < max_chunks; c += blockDim.x) out[c] = c >= 1 && c <= nf && s[c - 1] > lo_s; +} + +// A tile of `tile` tokens chooses together: for each far chunk, the best over the tile's tokens of (its score less the +// token's best), the B best of those (bisection), each token reading the tile's chunks among its own far ones. +// One block of 128 a tile; selection blockIdx.y (scores and bytes `stride` apart). +__global__ void zha_select_tile_kernel(const float* __restrict__ score, int max_chunks, long long pos, int rows, + int tile, int C, int recent, float budget, int min_chunks, int max_sel, + uint8_t* __restrict__ sel, size_t stride, float* __restrict__ comb_buf) { + __shared__ float part[4], part2[4], lo_s, hi_s, best[32]; + const int t0 = blockIdx.x * tile, t1 = min(rows, t0 + tile), lane = threadIdx.x & 31, w = threadIdx.x >> 5; + score += blockIdx.y * stride; + sel += blockIdx.y * stride; + float* comb = comb_buf + ((size_t) blockIdx.y * gridDim.x + blockIdx.x) * max_chunks; + long long fr; + const int nf0 = zha_far(pos + t0, C, recent, &fr), nf1 = zha_far(pos + t1 - 1, C, recent, &fr); + for (int t = t0; t < t1; ++t) { // each token's best far chunk + const int nf = zha_far(pos + t, C, recent, &fr); + float m = -INFINITY; + for (int i = threadIdx.x; i < nf; i += blockDim.x) m = fmaxf(m, score[(size_t) t * max_chunks + 1 + i]); + for (int o = 16; o > 0; o >>= 1) m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, o)); + if (lane == 0) part[w] = m; + __syncthreads(); + if (threadIdx.x == 0) best[t - t0] = fmaxf(fmaxf(part[0], part[1]), fmaxf(part[2], part[3])); + __syncthreads(); + } + float mn = INFINITY, mx = -INFINITY; + for (int i = threadIdx.x; i < nf1; i += blockDim.x) { + float v = -INFINITY; + for (int t = t0; t < t1; ++t) { + const int nf = t == t0 ? nf0 : zha_far(pos + t, C, recent, &fr); + if (i < nf) v = fmaxf(v, score[(size_t) t * max_chunks + 1 + i] - best[t - t0]); + } + comb[i] = v; + mn = fminf(mn, v), mx = fmaxf(mx, v); + } + for (int o = 16; o > 0; o >>= 1) { + mn = fminf(mn, __shfl_xor_sync(0xffffffffu, mn, o)); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, o)); + } + if (lane == 0) part[w] = mn, part2[w] = mx; + __syncthreads(); + if (threadIdx.x == 0) { + lo_s = fminf(fminf(part[0], part[1]), fminf(part[2], part[3])) - 1.f; + hi_s = fmaxf(fmaxf(part2[0], part2[1]), fmaxf(part2[2], part2[3])); + } + __syncthreads(); + const int B = nf1 ? min(nf1, max(min_chunks, min(max_sel, (int) lrintf(budget * nf1)))) : 0; + for (int it = 0; it < 30 && nf1; ++it) { + const float mid = 0.5f * (lo_s + hi_s); + int cnt = 0; + for (int i = threadIdx.x; i < nf1; i += blockDim.x) cnt += comb[i] > mid; + for (int o = 16; o > 0; o >>= 1) cnt += __shfl_xor_sync(0xffffffffu, cnt, o); + __syncthreads(); + if (lane == 0) part[w] = (float) cnt; + __syncthreads(); + if (threadIdx.x == 0) { + if (part[0] + part[1] + part[2] + part[3] >= B) lo_s = mid; + else hi_s = mid; + } + __syncthreads(); + } + for (int t = t0; t < t1; ++t) { + const int nf = zha_far(pos + t, C, recent, &fr); + uint8_t* out = sel + (size_t) t * max_chunks; + for (int c = threadIdx.x; c < max_chunks; c += blockDim.x) out[c] = c >= 1 && c <= nf && comb[c - 1] > lo_s; + } +} + +// The chunks any token of the call (any of `sels` selections) reads, and chunk 0: each not staged yet (cmap < 0) +// gets the next place (base + count) and goes on glist to be copied. A thread a word of 32 chunks: 32 bytes of each +// token's selection. +__global__ void zha_union_kernel(const uint8_t* __restrict__ sel, int sels, size_t sel_stride, int rows, int max_chunks, + int nch, int* __restrict__ cmap, int* __restrict__ count, int base, + int* __restrict__ glist) { + const int wi = blockIdx.x * blockDim.x + threadIdx.x; + if (32 * wi >= max_chunks) return; + if (gridDim.y > 1) { // every group at once: group blockIdx.y's selections, map, count and list + const int g = blockIdx.y; + sel += (size_t) g * sels * sel_stride; + cmap += (size_t) g * max_chunks; + count += g; + base = g * max_chunks; + glist += (size_t) g * (max_chunks + 1); + } + uint4 acc0 = make_uint4(0, 0, 0, 0), acc1 = acc0; + if (32 * wi < nch) + for (int h = 0; h < sels; ++h) + for (int t = 0; t < rows; ++t) { + const uint4* r = reinterpret_cast(sel + h * sel_stride + (size_t) t * max_chunks + 32 * (size_t) wi); + const uint4 a = r[0], b = r[1]; + acc0.x |= a.x, acc0.y |= a.y, acc0.z |= a.z, acc0.w |= a.w; + acc1.x |= b.x, acc1.y |= b.y, acc1.z |= b.z, acc1.w |= b.w; + } + const uint32_t v[8] = {acc0.x, acc0.y, acc0.z, acc0.w, acc1.x, acc1.y, acc1.z, acc1.w}; + for (int k = 0; k < 32; ++k) { + const int c = 32 * wi + k; + const bool any = c < nch && (c == 0 || ((v[k >> 2] >> (8 * (k & 3))) & 0xffu)); + if (!any || cmap[c] >= 0) continue; + cmap[c] = base + atomicAdd(count, 1); + glist[1 + atomicAdd(&glist[0], 1)] = c; + } +} + +// The staged chunks' keys and values of KV head g, from the leaves into the staging buffer (128 values a key). +// A block a chunk, a thread 8 bytes of codes or 4 fp16 values. +__global__ void zha_gather_kernel(ZhaKv leaves, int g, int C, const int* __restrict__ cmap, + const int* __restrict__ glist, ZhaKv stage) { + if ((int) blockIdx.x >= glist[0]) return; + const int c = glist[1 + blockIdx.x]; + const int slot = cmap[c]; + for (int i = threadIdx.x; i < C * (kZhaDim / 4); i += blockDim.x) { + const int j = i / (kZhaDim / 4), d = 4 * (i % (kZhaDim / 4)); + const size_t from = (size_t) ((long long) c * C + j) * leaves.row + (size_t) g * kZhaDim + d; + const size_t to = ((size_t) slot * C + j) * kZhaDim + d; + if (leaves.q4) { // 4 codes in 2 bytes + *reinterpret_cast(const_cast(stage.kc) + to / 2) = *reinterpret_cast(leaves.kc + from / 2); + *reinterpret_cast(const_cast(stage.vc) + to / 2) = *reinterpret_cast(leaves.vc + from / 2); + } else if (leaves.kc) { + *reinterpret_cast(const_cast(stage.kc) + to) = *reinterpret_cast(leaves.kc + from); + *reinterpret_cast(const_cast(stage.vc) + to) = *reinterpret_cast(leaves.vc + from); + } else { + *reinterpret_cast(const_cast(stage.k16) + to) = *reinterpret_cast(leaves.k16 + from); + *reinterpret_cast(const_cast(stage.v16) + to) = *reinterpret_cast(leaves.v16 + from); + } + if (leaves.kc && d % 32 == 0) { + const_cast(stage.ks)[to / 32] = leaves.ks[from / 32]; + const_cast(stage.vs)[to / 32] = leaves.vs[from / 32]; + } + } +} + +// ---- an answer's token through the index (the cache's step, rows 1): scores, selection, exact part, tail ------ + +// Chunk scores from int8 summaries, R 4: a thread a (chunk, reference) row of group blockIdx.y, 16-byte reads, the +// group's heads' dots; the best of a chunk's 4 references (4 lanes) and which one into head * hs + c. +__global__ void __launch_bounds__(128) zha_score_dec_kernel(const float* __restrict__ q, const int8_t* __restrict__ codes, + size_t q8n, const float* __restrict__ bhat, int nch, + int max_chunks, int n_head, int per, + float* __restrict__ score, uint8_t* __restrict__ rstar, + size_t hs) { + __shared__ float qs[4][kZhaDim]; + const int g = blockIdx.y, lane = threadIdx.x & 31; + const float sc = rsqrtf((float) kZhaDim); + for (int i = threadIdx.x; i < per * kZhaDim; i += blockDim.x) + qs[i / kZhaDim][i % kZhaDim] = q[((size_t) g * per + i / kZhaDim) * kZhaDim + i % kZhaDim] * sc; + __syncthreads(); + const int c = blockIdx.x * 32 + (threadIdx.x >> 2), r = threadIdx.x & 3; + const bool live = c < nch; + const size_t row = ((size_t) g * zq_cap(q8n, max_chunks) + (live ? c : 0)) * 4 + r; + const uint16_t* scales = zq_scales(codes, q8n); + const uint2 su = *reinterpret_cast(scales + row * 4); + const uint16_t sv[4] = {(uint16_t) (su.x & 0xffff), (uint16_t) (su.x >> 16), (uint16_t) (su.y & 0xffff), + (uint16_t) (su.y >> 16)}; + float acc[3] = {0.f, 0.f, 0.f}; + if (zq_4(q8n)) { // 4-bit codes: 64 bytes a row, a uint4 a 32-value block + const uint4* cr = reinterpret_cast(codes + row * (kZhaDim / 2)); + uint4 x[4]; +#pragma unroll + for (int i = 0; i < 4; ++i) x[i] = cr[i]; +#pragma unroll + for (int b = 0; b < 4; ++b) { + const uint32_t w4[4] = {x[b].x, x[b].y, x[b].z, x[b].w}; + float part[3] = {0.f, 0.f, 0.f}; +#pragma unroll + for (int k = 0; k < 4; ++k) +#pragma unroll + for (int n = 0; n < 8; ++n) { + const int d0 = 32 * b + 8 * k + n; + const float a0 = (float) ((int) ((w4[k] >> (4 * n)) & 15) - 8); +#pragma unroll + for (int p = 0; p < 3; ++p) + if (p < per) part[p] += a0 * qs[p][d0]; + } + const float d = __half2float(__ushort_as_half(sv[b])); +#pragma unroll + for (int p = 0; p < 3; ++p) acc[p] += part[p] * d; + } + } else { + const uint4* cr = reinterpret_cast(codes + row * kZhaDim); + uint4 x[8]; +#pragma unroll + for (int i = 0; i < 8; ++i) x[i] = cr[i]; +#pragma unroll + for (int b = 0; b < 4; ++b) { // a 32-value block: 2 uint4s + float part[3] = {0.f, 0.f, 0.f}; +#pragma unroll + for (int h2 = 0; h2 < 2; ++h2) { + const uint4 u = x[2 * b + h2]; + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; +#pragma unroll + for (int k = 0; k < 4; ++k) { + const int d0 = 32 * b + 16 * h2 + 4 * k; + const float a0 = (float) (int8_t) (w4[k] & 0xff), a1 = (float) (int8_t) ((w4[k] >> 8) & 0xff), + a2 = (float) (int8_t) ((w4[k] >> 16) & 0xff), a3 = (float) (int8_t) (w4[k] >> 24); +#pragma unroll + for (int p = 0; p < 3; ++p) + if (p < per) + part[p] += a0 * qs[p][d0] + a1 * qs[p][d0 + 1] + a2 * qs[p][d0 + 2] + a3 * qs[p][d0 + 3]; + } + } + const float d = __half2float(__ushort_as_half(sv[b])); +#pragma unroll + for (int p = 0; p < 3; ++p) acc[p] += part[p] * d; + } + } + const float bb = bhat[row]; +#pragma unroll + for (int p = 0; p < 3; ++p) { + if (p >= per) break; + float v = acc[p] + bb; + int arg = r; +#pragma unroll + for (int o = 1; o < 4; o <<= 1) { + const float ov = __shfl_xor_sync(0xffffffffu, v, o); + const int oa = __shfl_xor_sync(0xffffffffu, arg, o); + if (ov > v || (ov == v && oa < arg)) v = ov, arg = oa; + } + if (live && r == 0) { + const size_t at = (size_t) (g * per + p) * hs + c; + score[at] = v; + rstar[at] = (uint8_t) arg; + } + } + (void) lane; +} + +// The block's max (every thread gets it); red: 32 floats of shared memory. +__device__ __forceinline__ float zha_block_max(float v, float* red) { + const int lane = threadIdx.x & 31, w = threadIdx.x >> 5, warps = blockDim.x >> 5; + for (int o = 16; o > 0; o >>= 1) v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, o)); + __syncthreads(); + if (lane == 0) red[w] = v; + __syncthreads(); + v = -INFINITY; + for (int i = 0; i < warps; ++i) v = fmaxf(v, red[i]); + return v; +} + +__device__ __forceinline__ float zha_block_min(float v, float* red) { + return -zha_block_max(-v, red); +} + +// The threshold that keeps the B largest of v[0..n) (shared memory; [lo, hi] their range): two rounds of a 1024-bin +// histogram, the first round's bin that crosses B split again. A block of 1024 threads; every thread gets it. +__device__ float zha_hist_threshold(const float* v, int n, int B, float lo, float hi, int* hist, int* sums, + int* found, int* above) { + if (B >= n || !(hi > lo)) return -INFINITY; + const int tid = threadIdx.x, lane = tid & 31, w = tid >> 5, nt = blockDim.x; + float a = lo, width = (hi - lo) / 1024.f, b_end = INFINITY; + bool top_bin = true; + int need = B; + for (int level = 0; level < 2; ++level) { + for (int k = tid; k < 1024; k += nt) hist[k] = 0; + if (tid == 0) *found = 0, *above = 0; + __syncthreads(); + for (int i = tid; i < n; i += nt) { + const float x = v[i]; + if (x < a || x >= b_end) continue; + atomicAdd(&hist[min(1023, max(0, (int) ((x - a) / width)))], 1); + } + __syncthreads(); + int c = hist[1023 - tid]; // a suffix scan: thread k' holds bins >= 1023 - k' + for (int o = 1; o < 32; o <<= 1) { + const int t2 = __shfl_up_sync(0xffffffffu, c, o); + if (lane >= o) c += t2; + } + if (lane == 31) sums[w] = c; + __syncthreads(); + if (w == 0) { + int s2 = lane < (nt >> 5) ? sums[lane] : 0; + for (int o = 1; o < 32; o <<= 1) { + const int t2 = __shfl_up_sync(0xffffffffu, s2, o); + if (lane >= o) s2 += t2; + } + sums[lane] = s2; + } + __syncthreads(); + const int S = c + (w ? sums[w - 1] : 0), Sabove = S - hist[1023 - tid]; + if (S >= need && Sabove < need) *found = 1023 - tid, *above = Sabove; + __syncthreads(); + const int k = *found; + need -= *above; + a += k * width; + if (!(top_bin && k == 1023)) b_end = a + width; + top_bin = top_bin && k == 1023; + width /= 1024.f; + __syncthreads(); + } + return a; +} + +// zha_select_tile_kernel with a block of 1024 and the tile's picks in shared memory, the threshold by histograms: +// for each far chunk the best over the tile's tokens of (its score less the token's best; with zero_best the scores +// are a group's picks, whose best is 0), the B best of those, each token reading the tile's chunks among its own. +__global__ void __launch_bounds__(1024) zha_select_tile2_kernel(const float* __restrict__ score, int max_chunks, + long long pos, int rows, int tile, int C, int recent, + float budget, int min_chunks, int max_sel, + uint8_t* __restrict__ sel, size_t stride, + bool zero_best) { + extern __shared__ float comb[]; + __shared__ float red[32], best[64]; + __shared__ int nfs[64], hist[1024], sums[32], found, above; + const int t0 = blockIdx.x * tile, t1 = min(rows, t0 + tile), tid = threadIdx.x, lane = tid & 31, w = tid >> 5; + score += blockIdx.y * stride; + sel += blockIdx.y * stride; + long long fr; + for (int t = t0 + tid; t < t1; t += blockDim.x) nfs[t - t0] = zha_far(pos + t, C, recent, &fr); + __syncthreads(); + const int nf1 = nfs[t1 - 1 - t0]; + for (int t = t0 + w; t < t1; t += 32) { // a warp a token: its best far chunk + float m = 0.f; + if (!zero_best) { + m = -INFINITY; + for (int i = lane; i < nfs[t - t0]; i += 32) m = fmaxf(m, score[(size_t) t * max_chunks + 1 + i]); + for (int o = 16; o > 0; o >>= 1) m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, o)); + } + if (lane == 0) best[t - t0] = m; + } + __syncthreads(); + float mn = INFINITY, mx = -INFINITY; + for (int i = tid; i < nf1; i += blockDim.x) { + float v = -INFINITY; + for (int t = t0; t < t1; ++t) + if (i < nfs[t - t0]) v = fmaxf(v, score[(size_t) t * max_chunks + 1 + i] - best[t - t0]); + comb[i] = v; + mn = fminf(mn, v), mx = fmaxf(mx, v); + } + const float lo = zha_block_min(mn, red), hi = zha_block_max(mx, red); + const int B = nf1 ? min(nf1, max(min_chunks, min(max_sel, (int) lrintf(budget * nf1)))) : 0; + const float thr = zha_hist_threshold(comb, nf1, B, lo, hi, hist, sums, &found, &above); + for (int t = t0; t < t1; ++t) { + const int nf = nfs[t - t0]; + uint8_t* out = sel + (size_t) t * max_chunks; + for (int c = tid; c < max_chunks; c += blockDim.x) out[c] = c >= 1 && c <= nf && comb[c - 1] >= thr; + } +} + + +// a float's atomic max (the word starts all ones, a NaN, which every value replaces) +__device__ __forceinline__ void zha_atomic_max(float* at, float v) { + if (__float_as_int(v) >= 0) atomicMax(reinterpret_cast(at), __float_as_int(v)); + else atomicMin(reinterpret_cast(at), __float_as_uint(v)); +} + +// zha_select_tile2_kernel on a group's picks, the picks made here (zha_pick_kernel no more): for each far chunk the best +// over the tile's tokens and the group's heads of (the head's score less the head's best, top: zha_score_kernel's), the +// B best of those, each token reading the tile's chunks among its own. The same bytes as pick + select_tile2. +__global__ void __launch_bounds__(1024) zha_select_tile3_kernel(const float* __restrict__ score, int per, size_t hs, + const float* __restrict__ top, int max_chunks, + long long pos, int rows, int tile, int C, int recent, + float budget, int min_chunks, int max_sel, + uint8_t* __restrict__ sel) { + extern __shared__ float comb[]; + __shared__ float red[32], tp[4][64]; + __shared__ int nfs[64], hist[1024], sums[32], found, above; + const int t0 = blockIdx.x * tile, t1 = min(rows, t0 + tile), tid = threadIdx.x; + long long fr; + for (int t = t0 + tid; t < t1; t += blockDim.x) { + nfs[t - t0] = zha_far(pos + t, C, recent, &fr); + for (int h = 0; h < per; ++h) tp[h][t - t0] = top[(size_t) h * rows + t]; + } + __syncthreads(); + const int nf1 = nfs[t1 - 1 - t0]; + float mn = INFINITY, mx = -INFINITY; + for (int i = tid; i < nf1; i += blockDim.x) { + float v = -INFINITY; + for (int t = t0; t < t1; ++t) + if (i < nfs[t - t0]) { + float pk = -INFINITY; // zha_pick_kernel's value, then less 0 as select_tile2's zero_best + for (int h = 0; h < per; ++h) pk = fmaxf(pk, score[h * hs + (size_t) t * max_chunks + 1 + i] - tp[h][t - t0]); + v = fmaxf(v, pk - 0.f); + } + comb[i] = v; + mn = fminf(mn, v), mx = fmaxf(mx, v); + } + const float lo = zha_block_min(mn, red), hi = zha_block_max(mx, red); + const int B = nf1 ? min(nf1, max(min_chunks, min(max_sel, (int) lrintf(budget * nf1)))) : 0; + const float thr = zha_hist_threshold(comb, nf1, B, lo, hi, hist, sums, &found, &above); + for (int t = t0; t < t1; ++t) { + const int nf = nfs[t - t0]; + uint8_t* out = sel + (size_t) t * max_chunks; + for (int c = tid; c < max_chunks; c += blockDim.x) out[c] = c >= 1 && c <= nf && comb[c - 1] >= thr; + } +} + +// A group's selection of an answer's token (blockIdx.x the group, 1024 threads, the far chunks' pick values in +// dynamic shared memory): each head's best, the pick (best of score less the head's best), then the B best by two +// rounds of a 1024-bin histogram; the bytes into sel + g * hs. +__global__ void __launch_bounds__(1024) zha_pick_select_dec_kernel(const float* __restrict__ score, int per, size_t hs, + int max_chunks, long long pos, int C, int recent, + float budget, int min_chunks, int max_sel, + uint8_t* __restrict__ sel, + const int* __restrict__ pmap, float stick) { + extern __shared__ float pk[]; + __shared__ float red[32]; + __shared__ int hist[1024], sums[32]; + __shared__ int found, above_s; + const int g = blockIdx.x, tid = threadIdx.x, lane = tid & 31, w = tid >> 5, nt = blockDim.x; + score += (size_t) g * per * hs; + sel += (size_t) g * hs; + long long fr; + const int nf = zha_far(pos, C, recent, &fr); + float top[4]; + for (int h = 0; h < per; ++h) { + float m = -INFINITY; + for (int i = tid; i < nf; i += nt) m = fmaxf(m, score[h * hs + 1 + i]); + top[h] = zha_block_max(m, red); + } + float mn = INFINITY, mx = -INFINITY; + if (pmap) pmap += (size_t) g * max_chunks; + for (int i = tid; i < nf; i += nt) { + float v = -INFINITY; + for (int h = 0; h < per; ++h) v = fmaxf(v, score[h * hs + 1 + i] - top[h]); + if (pmap && pmap[1 + i] >= 0) v += stick; // a chunk the cache holds: kept over a close one off the card + pk[i] = v; + mn = fminf(mn, v), mx = fmaxf(mx, v); + } + const float lo = zha_block_min(mn, red), hi = zha_block_max(mx, red); + const int B = nf ? min(nf, max(min_chunks, min(max_sel, (int) lrintf(budget * nf)))) : 0; + float thr = -INFINITY; + if (B < nf && hi > lo) { + float a = lo, width = (hi - lo) / 1024.f; + bool top_bin = true; // the range's upper end open (the largest values clamp into the last bin) + float b_end = INFINITY; + int need = B; + for (int level = 0; level < 2; ++level) { + for (int k = tid; k < 1024; k += nt) hist[k] = 0; + if (tid == 0) found = 0, above_s = 0; + __syncthreads(); + for (int i = tid; i < nf; i += nt) { + const float v = pk[i]; + if (v < a || v >= b_end) continue; + const int bin = min(1023, max(0, (int) ((v - a) / width))); + atomicAdd(&hist[bin], 1); + } + __syncthreads(); + // suffix sums: thread k' = 1023 - k scans hist[k] (an inclusive prefix over k') + int v = tid < 1024 ? hist[1023 - tid] : 0; + for (int o = 1; o < 32; o <<= 1) { + const int t2 = __shfl_up_sync(0xffffffffu, v, o); + if (lane >= o) v += t2; + } + if (lane == 31) sums[w] = v; + __syncthreads(); + if (w == 0) { + int s = lane < (nt >> 5) ? sums[lane] : 0; + for (int o = 1; o < 32; o <<= 1) { + const int t2 = __shfl_up_sync(0xffffffffu, s, o); + if (lane >= o) s += t2; + } + sums[lane] = s; + } + __syncthreads(); + const int S = v + (w ? sums[w - 1] : 0); // the count of bins >= 1023 - tid + const int Sabove = S - (tid < 1024 ? hist[1023 - tid] : 0); + if (tid < 1024 && S >= need && Sabove < need) found = 1023 - tid, above_s = Sabove; + __syncthreads(); + const int k = found; + need -= above_s; + a = a + k * width; + b_end = (top_bin && k == 1023) ? b_end : a + width; + top_bin = top_bin && k == 1023; + width /= 1024.f; + __syncthreads(); + } + thr = a; + } + for (int c = tid; c < max_chunks; c += nt) sel[c] = c >= 1 && c <= nf && pk[c - 1] >= thr; +} + +// The exact part of an answer's token for head blockIdx.x: chunk 0 and the selected far chunks (the group's list, +// from the cache's pool) and the recent ones (from the ring), split over gridDim.y segments; a warp a chunk, a lane a +// key for the scores (64 keys: 2 a lane), a lane 4 dims for the values. Parts (top, sum, values) at seg. +__global__ void __launch_bounds__(128) zha_exact_dec_kernel(const float* __restrict__ q, ZhaKv pool, + const int* __restrict__ cmap, const int* __restrict__ glist, + ZhaKv ring, long long n_ring, int max_chunks, long long pos, + int n_head, int kv, int C, int recent, + const float* __restrict__ rr, const float* __restrict__ table, + int extent, int nrel, float* __restrict__ part, int seg_total) { + __shared__ float qs[kZhaDim], rv[32], wm[4], wl[4], wo[4][kZhaDim]; + const int head = blockIdx.x, seg = blockIdx.y, segs = gridDim.y, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const int g = head / (n_head / kv); + const float sc = rsqrtf((float) kZhaDim); + for (int i = threadIdx.x; i < kZhaDim; i += blockDim.x) qs[i] = q[(size_t) head * kZhaDim + i] * sc; + if ((int) threadIdx.x < nrel) rv[threadIdx.x] = rr[(size_t) head * nrel + threadIdx.x]; + __syncthreads(); + const long long own = pos; + long long fr; + zha_far(own, C, recent, &fr); + const int* gl = glist + (size_t) g * (max_chunks + 1); + const int nl = gl[0]; + const int rc0 = (int) (fr / C), last = (int) (own / C), nr = last - rc0 + 1; + const int items = nl + nr; + const int per_seg = (items + segs - 1) / segs, i0 = seg * per_seg, i1 = min(items, i0 + per_seg); + cmap += (size_t) g * max_chunks; + float m = -INFINITY, l = 0.f, acc[4] = {0.f, 0.f, 0.f, 0.f}; + for (int it = i0 + w; it < i1; it += 4) { + int c; + bool from_ring; + if (it < nl) { + c = gl[1 + it]; + if ((long long) c * C >= fr) continue; // a recent chunk: the ring's + from_ring = false; + } else { + c = rc0 + (it - nl); + from_ring = true; + } + const long long j0 = (long long) c * C, j1 = min(j0 + C, own + 1); + const int slot = from_ring ? 0 : cmap[c] & ~kZhaMiss; + // the scores: lane keys j0 + lane and j0 + 32 + lane + float s2[2]; +#pragma unroll + for (int h2 = 0; h2 < 2; ++h2) { + const long long j = j0 + lane + 32 * h2; + float s = -INFINITY; + if (j < j1) { + float dot = 0.f; + if (from_ring) { + const size_t at = (size_t) (j % n_ring) * ring.row + (size_t) g * kZhaDim; + const uint4* kr = reinterpret_cast(ring.kc + at); + const uint2 su = *reinterpret_cast(ring.ks + at / 32); + const uint16_t sv[4] = {(uint16_t) (su.x & 0xffff), (uint16_t) (su.x >> 16), + (uint16_t) (su.y & 0xffff), (uint16_t) (su.y >> 16)}; +#pragma unroll + for (int b = 0; b < 4; ++b) { + float pt = 0.f; +#pragma unroll + for (int h3 = 0; h3 < 2; ++h3) { + const uint4 u = kr[2 * b + h3]; + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; +#pragma unroll + for (int k = 0; k < 4; ++k) { + const int d0 = 32 * b + 16 * h3 + 4 * k; + pt += (float) (int8_t) (w4[k] & 0xff) * qs[d0] + + (float) (int8_t) ((w4[k] >> 8) & 0xff) * qs[d0 + 1] + + (float) (int8_t) ((w4[k] >> 16) & 0xff) * qs[d0 + 2] + + (float) (int8_t) (w4[k] >> 24) * qs[d0 + 3]; + } + } + dot += pt * __half2float(__ushort_as_half(sv[b])); + } + } else { + const size_t kk = (size_t) slot * C + (size_t) (j - j0); // the pool's key, 128 values + const uint4* kr = reinterpret_cast(pool.kc + kk * (kZhaDim / 2)); + const uint2 su = *reinterpret_cast(pool.ks + kk * 4); + const uint16_t sv[4] = {(uint16_t) (su.x & 0xffff), (uint16_t) (su.x >> 16), + (uint16_t) (su.y & 0xffff), (uint16_t) (su.y >> 16)}; +#pragma unroll + for (int b = 0; b < 4; ++b) { // 32 values: 16 bytes + const uint4 u = kr[b]; + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; + float pt = 0.f; +#pragma unroll + for (int k = 0; k < 4; ++k) { +#pragma unroll + for (int n = 0; n < 8; ++n) { + const int d = 32 * b + 8 * k + n; + pt += (float) ((int) ((w4[k] >> (4 * n)) & 15) - 8) * qs[d]; + } + } + dot += pt * __half2float(__ushort_as_half(sv[b])); + } + } + s = dot; + const long long dd = own - j; + if (dd < extent) { + const float* tt = table + (size_t) dd * nrel; + float bb = 0.f; + for (int k2 = 0; k2 < nrel; ++k2) bb += rv[k2] * tt[k2]; + s += bb; + } + } + s2[h2] = s; + } + float cm = fmaxf(s2[0], s2[1]); + for (int o = 16; o > 0; o >>= 1) cm = fmaxf(cm, __shfl_xor_sync(0xffffffffu, cm, o)); + const float p0 = s2[0] == -INFINITY ? 0.f : expf(s2[0] - cm), p1 = s2[1] == -INFINITY ? 0.f : expf(s2[1] - cm); + const float cz = warp_sum(p0 + p1); + // the values: a lane dims 4 lane .. 4 lane + 3, the chunk's keys in turn + float o4[4] = {0.f, 0.f, 0.f, 0.f}; + const int nk = (int) (j1 - j0); +#pragma unroll 8 + for (int k = 0; k < nk; ++k) { + const float p = __shfl_sync(0xffffffffu, k < 32 ? p0 : p1, k & 31); + float v[4]; + if (from_ring) { + const size_t at = (size_t) ((j0 + k) % n_ring) * ring.row + (size_t) g * kZhaDim + 4 * lane; + const char4 b4 = *reinterpret_cast(ring.vc + at); + const float d = __half2float(__ushort_as_half(ring.vs[at / 32])); + v[0] = b4.x * d, v[1] = b4.y * d, v[2] = b4.z * d, v[3] = b4.w * d; + } else { + const size_t at = ((size_t) slot * C + k) * kZhaDim + 4 * lane; + const uint16_t b2 = *reinterpret_cast(pool.vc + at / 2); + const float d = __half2float(__ushort_as_half(pool.vs[at / 32])); +#pragma unroll + for (int i = 0; i < 4; ++i) v[i] = (float) ((int) ((b2 >> (4 * i)) & 15) - 8) * d; + } +#pragma unroll + for (int i = 0; i < 4; ++i) o4[i] += p * v[i]; + } + // into the warp's running sums + const float nm = fmaxf(m, cm), f = m == -INFINITY ? 0.f : expf(m - nm), e = expf(cm - nm); + l = l * f + cz * e; +#pragma unroll + for (int i = 0; i < 4; ++i) acc[i] = acc[i] * f + o4[i] * e; + m = nm; + } + if (lane == 0) wm[w] = m, wl[w] = l; +#pragma unroll + for (int i = 0; i < 4; ++i) wo[w][4 * lane + i] = acc[i]; + __syncthreads(); + if (w != 0) return; + float M = -INFINITY; + for (int k = 0; k < 4; ++k) M = fmaxf(M, wm[k]); + float Z = 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int k = 0; k < 4; ++k) { + if (wm[k] == -INFINITY) continue; + const float f = expf(wm[k] - M); + Z += wl[k] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += wo[k][4 * lane + i] * f; + } + float* pp = part + ((size_t) head * seg_total + seg) * (kZhaDim + 2); +#pragma unroll + for (int i = 0; i < 4; ++i) pp[4 * lane + i] = o[i]; + if (lane == 0) pp[kZhaDim] = M, pp[kZhaDim + 1] = Z; +} + +// The same for a KV group's 3 heads at once (blockIdx.x the group): a key's and a value's dequantized once for them. +__device__ __forceinline__ void zha_key_ring(const ZhaKv& ring, size_t at, const float (&qs)[3][kZhaDim], float (&dot)[3]) { + const uint4* kr = reinterpret_cast(ring.kc + at); + const uint2 su = *reinterpret_cast(ring.ks + at / 32); + const uint16_t sv[4] = {(uint16_t) (su.x & 0xffff), (uint16_t) (su.x >> 16), (uint16_t) (su.y & 0xffff), + (uint16_t) (su.y >> 16)}; +#pragma unroll + for (int b = 0; b < 4; ++b) { + float pt[3] = {0.f, 0.f, 0.f}; +#pragma unroll + for (int h3 = 0; h3 < 2; ++h3) { + const uint4 u = kr[2 * b + h3]; + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; +#pragma unroll + for (int k = 0; k < 4; ++k) { +#pragma unroll + for (int n = 0; n < 4; ++n) { + const int d = 32 * b + 16 * h3 + 4 * k + n; + const float x = (float) (int8_t) ((w4[k] >> (8 * n)) & 0xff); +#pragma unroll + for (int p = 0; p < 3; ++p) pt[p] += x * qs[p][d]; + } + } + } + const float sc = __half2float(__ushort_as_half(sv[b])); +#pragma unroll + for (int p = 0; p < 3; ++p) dot[p] += pt[p] * sc; + } +} + +__device__ __forceinline__ void zha_key_pool(const ZhaKv& pool, size_t kk, const float (&qs)[3][kZhaDim], float (&dot)[3]) { + const uint4* kr = reinterpret_cast(pool.kc + kk * (kZhaDim / 2)); + const uint2 su = *reinterpret_cast(pool.ks + kk * 4); + const uint16_t sv[4] = {(uint16_t) (su.x & 0xffff), (uint16_t) (su.x >> 16), (uint16_t) (su.y & 0xffff), + (uint16_t) (su.y >> 16)}; +#pragma unroll + for (int b = 0; b < 4; ++b) { + const uint4 u = kr[b]; + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; + float pt[3] = {0.f, 0.f, 0.f}; +#pragma unroll + for (int k = 0; k < 4; ++k) { +#pragma unroll + for (int n = 0; n < 8; ++n) { + const int d = 32 * b + 8 * k + n; + const float x = (float) ((int) ((w4[k] >> (4 * n)) & 15) - 8); +#pragma unroll + for (int p = 0; p < 3; ++p) pt[p] += x * qs[p][d]; + } + } + const float sc = __half2float(__ushort_as_half(sv[b])); +#pragma unroll + for (int p = 0; p < 3; ++p) dot[p] += pt[p] * sc; + } +} + +// zha_exact_dec_kernel for a KV group's 3 heads at once (blockIdx.x the group, blockIdx.y the segment): the selected +// chunks from the cache's pool, chunk 0 and the recent keys from the ring, each key read once for the three. +__global__ void __launch_bounds__(128) zha_exact_dec_group_kernel(const float* __restrict__ q, ZhaKv pool, + const int* __restrict__ cmap, + const int* __restrict__ glist, ZhaKv ring, + long long n_ring, int max_chunks, long long pos, + int n_head, int C, int recent, + const float* __restrict__ rr, + const float* __restrict__ table, int extent, int nrel, + float* __restrict__ part, int seg_total) { + constexpr int PER = 3, KPS = 4; // KPS: keys a step of the values (8 lanes a key, 16 values a lane) + __shared__ float qs[PER][kZhaDim], rv[PER][16], wm[4][PER], wl[4][PER], wo[4][PER][kZhaDim]; + const int g = blockIdx.x, seg = blockIdx.y, segs = gridDim.y, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const float sc = rsqrtf((float) kZhaDim); + for (int i = threadIdx.x; i < PER * kZhaDim; i += blockDim.x) + qs[i / kZhaDim][i % kZhaDim] = q[((size_t) g * PER + i / kZhaDim) * kZhaDim + i % kZhaDim] * sc; + for (int i = threadIdx.x; i < PER * nrel; i += blockDim.x) + rv[i / nrel][i % nrel] = rr[((size_t) g * PER + i / nrel) * nrel + i % nrel]; + __syncthreads(); + const long long own = pos; + long long fr; + zha_far(own, C, recent, &fr); + const int* gl = glist + (size_t) g * (max_chunks + 1); + const int nl = gl[0]; + const int rc0 = (int) (fr / C), last = (int) (own / C), nr = last - rc0 + 1; + const int items = nl + nr; + const int per_seg = (items + segs - 1) / segs, i0 = seg * per_seg, i1 = min(items, i0 + per_seg); + cmap += (size_t) g * max_chunks; + constexpr int LPK = 32 / KPS, DPL = kZhaDim / LPK; // lanes a key, values a lane + const int sub = lane / LPK, dg = lane % LPK; // the values' step: key sub, values [DPL dg, + DPL) + float m[PER], l[PER], acc16[PER][DPL]; +#pragma unroll + for (int p = 0; p < PER; ++p) { + m[p] = -INFINITY, l[p] = 0.f; +#pragma unroll + for (int i = 0; i < DPL; ++i) acc16[p][i] = 0.f; + } + for (int it = i0 + w; it < i1; it += 4) { + int c; + bool from_ring; + if (it < nl) { + c = gl[1 + it]; + if ((long long) c * C >= fr) continue; + from_ring = false; + } else { + c = rc0 + (it - nl); + from_ring = true; + } + const long long j0 = (long long) c * C, j1 = min(j0 + C, own + 1); + const int slot = from_ring ? 0 : cmap[c] & ~kZhaMiss; + float s[2][PER]; +#pragma unroll + for (int h2 = 0; h2 < 2; ++h2) { + const long long j = j0 + lane + 32 * h2; + float dot[PER] = {0.f, 0.f, 0.f}; + if (j < j1) { + if (from_ring) zha_key_ring(ring, (size_t) (j % n_ring) * ring.row + (size_t) g * kZhaDim, qs, dot); + else zha_key_pool(pool, (size_t) slot * C + (size_t) (j - j0), qs, dot); + const long long dd = own - j; + if (dd < extent) { + const float* tt = table + (size_t) dd * nrel; + for (int k2 = 0; k2 < nrel; ++k2) { + const float t = tt[k2]; +#pragma unroll + for (int p = 0; p < PER; ++p) dot[p] += rv[p][k2] * t; + } + } + } +#pragma unroll + for (int p = 0; p < PER; ++p) s[h2][p] = j < j1 ? dot[p] : -INFINITY; + } + float cm[PER], p0[PER], p1[PER], cz[PER]; +#pragma unroll + for (int p = 0; p < PER; ++p) { + float v = fmaxf(s[0][p], s[1][p]); + for (int o = 16; o > 0; o >>= 1) v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, o)); + cm[p] = v; + p0[p] = s[0][p] == -INFINITY ? 0.f : expf(s[0][p] - v); + p1[p] = s[1][p] == -INFINITY ? 0.f : expf(s[1][p] - v); + cz[p] = warp_sum(p0[p] + p1[p]); + } + // the values: 4 keys a step, 8 lanes a key and 16 values a lane (sub, dg), straight into acc16 with the + // chunk's weights under the running largest + float pe0[PER], pe1[PER]; +#pragma unroll + for (int p = 0; p < PER; ++p) { + const float nm = fmaxf(m[p], cm[p]), f = m[p] == -INFINITY ? 0.f : expf(m[p] - nm), e = expf(cm[p] - nm); + l[p] = l[p] * f + cz[p] * e; +#pragma unroll + for (int i = 0; i < DPL; ++i) acc16[p][i] *= f; + pe0[p] = p0[p] * e, pe1[p] = p1[p] * e; + m[p] = nm; + } + const int nk = (int) (j1 - j0); + constexpr int STEPS = 16 / KPS; // the steps whose reads go together: 16 keys + for (int kb = 0; kb < nk; kb += 16) { + uint4 cw[STEPS]; + float vsc[STEPS]; +#pragma unroll + for (int st = 0; st < STEPS; ++st) { + const int kk = kb + KPS * st + sub; + cw[st] = make_uint4(0u, 0u, 0u, 0u), vsc[st] = 0.f; + if (kk < nk) { + if (from_ring) { + const size_t at = (size_t) ((j0 + kk) % n_ring) * ring.row + (size_t) g * kZhaDim + DPL * dg; + if constexpr (DPL == 16) cw[st] = *reinterpret_cast(ring.vc + at); + else { + const uint2 u = *reinterpret_cast(ring.vc + at); + cw[st] = make_uint4(u.x, u.y, 0u, 0u); + } + vsc[st] = __half2float(__ushort_as_half(ring.vs[at / 32])); + } else { + const size_t at = ((size_t) slot * C + kk) * kZhaDim + DPL * dg; + if constexpr (DPL == 16) { + const uint2 u = *reinterpret_cast(pool.vc + at / 2); + cw[st] = make_uint4(u.x, u.y, 0u, 0u); + } else { + cw[st].x = *reinterpret_cast(pool.vc + at / 2); + } + vsc[st] = __half2float(__ushort_as_half(pool.vs[at / 32])); + } + } + } +#pragma unroll + for (int st = 0; st < STEPS; ++st) { + const int k0 = kb + KPS * st, kk = k0 + sub; + float wv[PER]; +#pragma unroll + for (int p = 0; p < PER; ++p) wv[p] = __shfl_sync(0xffffffffu, k0 < 32 ? pe0[p] : pe1[p], (k0 & 31) + sub); + if (kk < nk) { + float v[DPL]; + const uint32_t ww[4] = {cw[st].x, cw[st].y, cw[st].z, cw[st].w}; + if (from_ring) { +#pragma unroll + for (int i = 0; i < DPL; ++i) v[i] = (float) (int8_t) ((ww[i / 4] >> (8 * (i % 4))) & 0xff) * vsc[st]; + } else { +#pragma unroll + for (int i = 0; i < DPL; ++i) v[i] = (float) ((int) ((ww[i / 8] >> (4 * (i % 8))) & 15) - 8) * vsc[st]; + } +#pragma unroll + for (int p = 0; p < PER; ++p) +#pragma unroll + for (int i = 0; i < DPL; ++i) acc16[p][i] += wv[p] * v[i]; + } + } + } + } +#pragma unroll + for (int p = 0; p < PER; ++p) { + if (lane == 0) wm[w][p] = m[p], wl[w][p] = l[p]; +#pragma unroll + for (int i = 0; i < DPL; ++i) { // the step's keys' sums of each value: lanes dg + LPK * sub + float x = acc16[p][i]; +#pragma unroll + for (int o = LPK; o < 32; o <<= 1) x += __shfl_xor_sync(0xffffffffu, x, o); + if (sub == 0) wo[w][p][DPL * dg + i] = x; + } + } + __syncthreads(); + if (w >= PER) return; + const int p = w; // warp p writes head p's part + float M = -INFINITY; + for (int k = 0; k < 4; ++k) M = fmaxf(M, wm[k][p]); + float Z = 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int k = 0; k < 4; ++k) { + if (wm[k][p] == -INFINITY) continue; + const float f = expf(wm[k][p] - M); + Z += wl[k][p] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += wo[k][p][4 * lane + i] * f; + } + float* pp = part + ((size_t) (g * PER + p) * seg_total + seg) * (kZhaDim + 2); +#pragma unroll + for (int i = 0; i < 4; ++i) pp[4 * lane + i] = o[i]; + if (lane == 0) pp[kZhaDim] = M, pp[kZhaDim + 1] = Z; +} + +// An answer's token's tail (blockIdx.x the head, blockIdx.y the segment): each far chunk it does not read exactly as +// one key, the values' summary of the chunk's best reference (4-bit codes) weighted by exp(score). Parts at +// seg_base + seg. +__global__ void __launch_bounds__(128) zha_tail_dec_kernel(const float* __restrict__ score, + const uint8_t* __restrict__ rstar, + const uint8_t* __restrict__ sel, const __half* __restrict__ vhat, + size_t q8n, int max_chunks, long long pos, int n_head, int kv, + int R, int C, int recent, size_t hs, int sel_heads, + float* __restrict__ part, int seg_base, int seg_total) { + __shared__ float red[32], wl[4], wo[4][kZhaDim]; + const int head = blockIdx.x, seg = blockIdx.y, segs = gridDim.y, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const int per = n_head / kv, g = head / per; + score += (size_t) head * hs; + rstar += (size_t) head * hs; + sel += (size_t) (sel_heads == 1 ? g : head) * hs; + long long fr; + const int nf = zha_far(pos, C, recent, &fr); + const int cps = (nf + segs - 1) / segs, c0 = 1 + seg * cps, c1 = min(nf + 1, c0 + cps); // chunks [c0, c1) + float mx = -INFINITY; + for (int c = c0 + threadIdx.x; c < c1; c += blockDim.x) + if (!sel[c]) mx = fmaxf(mx, score[c]); + const float M = zha_block_max(mx, red); + // 4-bit summaries: 4 chunks a step, 8 lanes a chunk's row and 16 values a lane (sub, dg); the 8 steps of a warp's + // 32 chunks read together + const int sub = lane >> 3, dg = lane & 7; + const uint8_t* codes = reinterpret_cast(vhat); + const uint16_t* scl = zq_scales(vhat, q8n); + float z = 0.f, acc16[16]; +#pragma unroll + for (int i = 0; i < 16; ++i) acc16[i] = 0.f; + if (M != -INFINITY) + for (int b0 = c0 + 32 * w; b0 < c1; b0 += 128) { + const int c = b0 + lane; + const bool live = c < c1 && !sel[c]; + const float wt = live ? expf(score[c] - M) : 0.f; + const int r = live ? rstar[c] : 0; + z += wt; + uint2 cw[8]; + float sc[8], wv[8]; +#pragma unroll + for (int st = 0; st < 8; ++st) { + const int jj = 4 * st + sub; + wv[st] = __shfl_sync(0xffffffffu, wt, jj); + const int rj = __shfl_sync(0xffffffffu, r, jj); + const int cj = min(b0 + jj, c1 - 1); + const size_t row = ((size_t) g * zq_cap(q8n, max_chunks) + cj) * R + rj; + cw[st] = *reinterpret_cast(codes + row * (kZhaDim / 2) + 8 * dg); + sc[st] = __half2float(__ushort_as_half(scl[row * 4 + dg / 2])); + } +#pragma unroll + for (int st = 0; st < 8; ++st) { + const float f = wv[st] * sc[st]; + const uint32_t ww[2] = {cw[st].x, cw[st].y}; +#pragma unroll + for (int i = 0; i < 16; ++i) acc16[i] += f * (float) ((int) ((ww[i / 8] >> (4 * (i % 8))) & 15) - 8); + } + } + z = warp_sum(z); + if (lane == 0) wl[w] = z; +#pragma unroll + for (int i = 0; i < 16; ++i) { // the 4 chunks' sums of each value, lanes dg, dg + 8, dg + 16, dg + 24 + float x = acc16[i]; + x += __shfl_xor_sync(0xffffffffu, x, 8); + x += __shfl_xor_sync(0xffffffffu, x, 16); + if (sub == 0) wo[w][16 * dg + i] = x; + } + __syncthreads(); + if (w != 0) return; + float Z = 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int k = 0; k < 4; ++k) { + Z += wl[k]; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += wo[k][4 * lane + i]; + } + float* pp = part + ((size_t) head * seg_total + seg_base + seg) * (kZhaDim + 2); +#pragma unroll + for (int i = 0; i < 4; ++i) pp[4 * lane + i] = o[i]; + if (lane == 0) pp[kZhaDim] = Z > 0.f ? M : -INFINITY, pp[kZhaDim + 1] = Z; +} + +// The parts of zha_attend_kernel's segments for a token's head (blockIdx.x, blockIdx.y + head0), with its sink. +__global__ void zha_join_kernel(const float* __restrict__ part, int segs, int n_head, int head0, + const float* __restrict__ sinks, float* __restrict__ out) { + const int t = blockIdx.x, head = head0 + blockIdx.y, lane = threadIdx.x; + const float* pp = part + ((size_t) t * n_head + head) * segs * (kZhaDim + 2); + const float sink = sinks ? sinks[(size_t) t * n_head + head] : -INFINITY; + float M = sink; + for (int s = 0; s < segs; ++s) M = fmaxf(M, pp[s * (kZhaDim + 2) + kZhaDim]); + float Z = sinks ? expf(sink - M) : 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int s = 0; s < segs; ++s) { + const float* x = pp + s * (kZhaDim + 2); + if (x[kZhaDim] == -INFINITY) continue; + const float f = expf(x[kZhaDim] - M); + Z += x[kZhaDim + 1] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += x[4 * lane + i] * f; + } + float* y = out + ((size_t) t * n_head + head) * kZhaDim + 4 * lane; +#pragma unroll + for (int i = 0; i < 4; ++i) y[i] = o[i] / Z; +} + +// The partials of a group's heads (part [rows][per][segs][130], natural-log tops) joined with each head's sink. +__global__ void zha_join_group_kernel(const float* __restrict__ part, int segs, int per, int n_head, int head0, + const float* __restrict__ sinks, float* __restrict__ out) { + const int t = blockIdx.x, p = blockIdx.y, head = head0 + p, lane = threadIdx.x; + const float* pp = part + ((size_t) t * per + p) * segs * (kZhaDim + 2); + const float sink = sinks ? sinks[(size_t) t * n_head + head] : -INFINITY; + float M = sink; + for (int s = 0; s < segs; ++s) M = fmaxf(M, pp[s * (kZhaDim + 2) + kZhaDim]); + float Z = sinks ? expf(sink - M) : 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int s = 0; s < segs; ++s) { + const float* x = pp + s * (kZhaDim + 2); + if (x[kZhaDim] == -INFINITY) continue; + const float f = expf(x[kZhaDim] - M); + Z += x[kZhaDim + 1] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += x[4 * lane + i] * f; + } + float* y = out + ((size_t) t * n_head + head) * kZhaDim + 4 * lane; +#pragma unroll + for (int i = 0; i < 4; ++i) y[i] = Z > 0.f ? o[i] / Z : 0.f; +} + +// ---- the exact keys of a prompt batch on the tensor cores ---------------------------------------------------- +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 750 +#define ZHA_MMA 0 +#else +#define ZHA_MMA 1 +#endif + +#if ZHA_MMA +__device__ __forceinline__ void zmma(float* c, const uint32_t* a, const uint32_t* b) { +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800 + asm volatile("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};\n" + : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) + : "r"(a[0]), "r"(a[1]), "r"(b[0])); + asm volatile("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};\n" + : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) + : "r"(a[2]), "r"(a[3]), "r"(b[1])); +#else + asm volatile("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, " + "{%0,%1,%2,%3};\n" + : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) + : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b[0]), "r"(b[1])); +#endif +} + +__device__ __forceinline__ void zldm4(uint32_t* r, const __half* p, bool trans) { + const unsigned a = (unsigned) __cvta_generic_to_shared(p); + if (trans) + asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.shared.b16 {%0,%1,%2,%3}, [%4];\n" + : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) : "r"(a)); + else + asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n" + : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) : "r"(a)); +} + +__device__ __forceinline__ uint32_t zh2(float lo, float hi) { + __half2 h = __floats2half2_rn(lo, hi); + return *reinterpret_cast(&h); +} +#endif + +constexpr int kZhaTokens = 64, kZhaKeys = 64, kZhaRow = kZhaDim + 8, kZhaWarps = 12; + +// The exact keys of a prompt batch's tokens for KV group g, flash style: a launch block 64 tokens of the group's 3 +// heads (12 warps: 4 of 16 tokens a head), walking the chunks any of its tokens reads exactly (chunk 0, the +// selected far ones from the staging buffer, the recent ones from the ring) 64 keys at a time; a token masks the +// keys it does not read. bias [per][rows][extent] (natural units) adds the bias by distance. Out: each token's +// head's (top, sum, values) unnormalized, part [rows][per][segs][130] at segment 0. + +// The helpers of zha_exact_kernel (leaves in RAM as 4-bit, the ring Q8_0): a tile's raw bytes, [K codes][K scales] +// [V codes][V scales], 4-bit (64 bytes a key) from the staging buffer or Q8_0 (128) from the ring. +constexpr int kZhaRaw = 2 * (64 * 128 + 64 * 4 * 2); + +__device__ __forceinline__ void zha_cp(void* dst, const void* src, int bytes) { +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 + const unsigned d = (unsigned) __cvta_generic_to_shared(dst); + if (bytes == 16) asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r"(d), "l"(src)); + else asm volatile("cp.async.ca.shared.global [%0], [%1], 8;\n" ::"r"(d), "l"(src)); +#else + if (bytes == 16) *reinterpret_cast(dst) = *reinterpret_cast(src); + else *reinterpret_cast(dst) = *reinterpret_cast(src); +#endif +} + +__device__ __forceinline__ void zha_commit() { +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 + asm volatile("cp.async.commit_group;\n" ::); +#endif +} + +template +__device__ __forceinline__ void zha_wait() { +#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 + asm volatile("cp.async.wait_group %0;\n" ::"n"(N)); +#endif +} + +__device__ __forceinline__ void zha_raw_issue(int c, uint8_t* raw, const ZhaKv& stage, const int* cmap, const ZhaKv& ring, + long long n_ring, int g, int C, int c_ring) { + if (c >= c_ring) { // Q8_0 rows of the ring: 128 code bytes and 4 scales of head g a key + const long long r0 = ((long long) c * C) % n_ring; + const size_t row = (size_t) ring.row; // values a ring row (every KV head's) + uint8_t* kcd = raw; + uint8_t* ksd = raw + 64 * 128; + uint8_t* vcd = raw + 64 * 128 + 64 * 8; + uint8_t* vsd = vcd + 64 * 128; + for (int i = threadIdx.x; i < 64 * 8; i += blockDim.x) { + const int j = i >> 3, part = i & 7; + const size_t at = (size_t) (r0 + j) * row + (size_t) g * kZhaDim + 16 * part; + zha_cp(kcd + j * 128 + 16 * part, ring.kc + at, 16); + zha_cp(vcd + j * 128 + 16 * part, ring.vc + at, 16); + } + for (int j = threadIdx.x; j < 64; j += blockDim.x) { + const size_t at = ((size_t) (r0 + j) * row + (size_t) g * kZhaDim) / 32; + zha_cp(ksd + j * 8, ring.ks + at, 8); + zha_cp(vsd + j * 8, ring.vs + at, 8); + } + } else { // 4-bit chunk of the staging buffer: its keys back to back (64 code bytes, 4 scales a key) + const size_t slot = (size_t) cmap[c]; + const uint8_t* kc = reinterpret_cast(stage.kc) + slot * C * 64; + const uint8_t* vc = reinterpret_cast(stage.vc) + slot * C * 64; + const uint8_t* ks = reinterpret_cast(stage.ks) + slot * C * 8; + const uint8_t* vs = reinterpret_cast(stage.vs) + slot * C * 8; + for (int i = threadIdx.x; i < 256; i += blockDim.x) { + zha_cp(raw + 16 * i, kc + 16 * i, 16); + zha_cp(raw + 64 * 128 + 64 * 8 + 16 * i, vc + 16 * i, 16); + } + for (int i = threadIdx.x; i < 32; i += blockDim.x) { + zha_cp(raw + 64 * 128 + 16 * i, ks + 16 * i, 16); + zha_cp(raw + 64 * 128 + 64 * 8 + 64 * 128 + 16 * i, vs + 16 * i, 16); + } + } +} + +// A tile's raw bytes to the fp16 tiles: 8 values a thread at a time. +__device__ __forceinline__ void zha_raw_dequant(const uint8_t* raw, bool q8, __half (*ks)[kZhaDim + 8], + __half (*vs)[kZhaDim + 8]) { + const uint8_t* kcd = raw; + const uint16_t* ksd = reinterpret_cast(raw + 64 * 128); + const uint8_t* vcd = raw + 64 * 128 + 64 * 8; + const uint16_t* vsd = reinterpret_cast(vcd + 64 * 128); + for (int i = threadIdx.x; i < 64 * (kZhaDim / 8); i += blockDim.x) { + const int r = i / (kZhaDim / 8), d = 8 * (i % (kZhaDim / 8)); + const float dk = __half2float(__ushort_as_half(ksd[r * 4 + d / 32])); + const float dv = __half2float(__ushort_as_half(vsd[r * 4 + d / 32])); + float xk[8], xv[8]; + if (q8) { + const uint2 a = *reinterpret_cast(kcd + r * 128 + d); + const uint2 b = *reinterpret_cast(vcd + r * 128 + d); + const int8_t* pa = reinterpret_cast(&a); + const int8_t* pb = reinterpret_cast(&b); + for (int e = 0; e < 8; ++e) xk[e] = pa[e] * dk, xv[e] = pb[e] * dv; + } else { + const uint32_t a = *reinterpret_cast(kcd + r * 64 + d / 2); + const uint32_t b = *reinterpret_cast(vcd + r * 64 + d / 2); + for (int e = 0; e < 8; ++e) { + xk[e] = (float) ((int) ((a >> (4 * e)) & 15) - 8) * dk; + xv[e] = (float) ((int) ((b >> (4 * e)) & 15) - 8) * dv; + } + } + for (int e = 0; e < 8; e += 2) { + *reinterpret_cast<__half2*>(&ks[r][d + e]) = __floats2half2_rn(xk[e], xk[e + 1]); + *reinterpret_cast<__half2*>(&vs[r][d + e]) = __floats2half2_rn(xv[e], xv[e + 1]); + } + } +} + +__global__ void __launch_bounds__(kZhaWarps * 32) +zha_exact_kernel(const float* __restrict__ q, ZhaKv stage, const int* __restrict__ cmap, ZhaKv ring, long long n_ring, + const uint8_t* __restrict__ sel, int sel_heads, size_t hs, int max_chunks, long long pos, int rows, + int n_head, int g, int per, int C, int recent, const float* __restrict__ bias, int extent, + float* __restrict__ part, int segs, const uint32_t* __restrict__ bits, int words) { +#if ZHA_MMA + __shared__ __align__(16) __half ks[kZhaKeys][kZhaRow]; + __shared__ __align__(16) __half vs[kZhaKeys][kZhaRow]; + __shared__ uint32_t want[32]; // a window of 1024 chunks: a bit each + __shared__ int lst[1024], nlst; // the window's chunks in order + extern __shared__ __align__(16) uint8_t zraw[]; // two stages of a tile's raw bytes (kZhaRaw each) + constexpr float LOG2E = 1.44269504f; + const int t0 = blockIdx.x * kZhaTokens, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const int gid = lane >> 2, tig = lane & 3, p = w % per, head = g * per + p, tw = t0 + 16 * (w / per); + const bool warp_live = w < per * 4; + const int tok[2] = {tw + gid, tw + gid + 8}; + const float sc = rsqrtf((float) kZhaDim) * LOG2E; + uint32_t qa[8][4]; + for (int k = 0; k < 8; ++k) + for (int i = 0; i < 2; ++i) + for (int hi = 0; hi < 2; ++hi) { + float2 x = make_float2(0.f, 0.f); + if (warp_live && tok[i] < rows) + x = *reinterpret_cast(q + ((size_t) tok[i] * n_head + head) * kZhaDim + 16 * k + 8 * hi + 2 * tig); + qa[k][i + 2 * hi] = zh2(x.x * sc, x.y * sc); + } + long long own[2], fr[2]; + const uint8_t* srow[2]; + for (int i = 0; i < 2; ++i) { + own[i] = pos + tok[i]; + zha_far(own[i], C, recent, &fr[i]); + srow[i] = sel + (sel_heads > 1 ? (size_t) p * hs : 0) + (size_t) min(tok[i], rows - 1) * max_chunks; + } + const int t_last = min(t0 + kZhaTokens, rows) - 1; + long long fr_first, fr_last; + zha_far(pos + t0, C, recent, &fr_first); + const int nf_last = zha_far(pos + t_last, C, recent, &fr_last); + const int c_ring = (int) (fr_first / C); // from here the ring holds the chunks + const int c_end = (int) ((pos + t_last) / C) + 1; // past the last key + float o[16][4], m[2] = {-INFINITY, -INFINITY}, l[2] = {0.f, 0.f}; + for (int n = 0; n < 16; ++n) o[n][0] = o[n][1] = o[n][2] = o[n][3] = 0.f; + const auto tile = [&](const int c) { + // a warp's 16 tokens: the chunk only if one of them reads it exactly (the block's other tokens may) + bool ex[2]; + for (int i = 0; i < 2; ++i) + ex[i] = tok[i] < rows && ((long long) c * C >= fr[i] || c == 0 || srow[i][c]); + if (warp_live && __any_sync(0xffffffffu, ex[0] || ex[1])) { + float s[8][4]; + for (int n = 0; n < 8; ++n) { + s[n][0] = s[n][1] = s[n][2] = s[n][3] = 0.f; + for (int k = 0; k < 8; k += 2) { + uint32_t bb[4]; + zldm4(bb, &ks[8 * n + (lane & 7)][16 * k + 8 * (lane >> 3)], false); + zmma(s[n], qa[k], bb); + zmma(s[n], qa[k + 1], bb + 2); + } + } + for (int i = 0; i < 2; ++i) { + const bool row_live = tok[i] < rows; + const bool exact_c = ex[i]; + float mx = -INFINITY; + for (int n = 0; n < 8; ++n) + for (int j = 2 * i; j < 2 * i + 2; ++j) { + const long long key = (long long) c * C + 8 * n + 2 * tig + (j & 1); + if (!row_live || !exact_c || key > own[i]) { + s[n][j] = -INFINITY; + } else { + const long long dd = own[i] - key; + if (dd < extent) s[n][j] += bias[((size_t) p * rows + tok[i]) * extent + dd] * LOG2E; + } + mx = fmaxf(mx, s[n][j]); + } + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 1)); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 2)); + const float now = fmaxf(m[i], mx), base = now == -INFINITY ? 0.f : now; + const float f = exp2f(m[i] - base); + m[i] = now; + l[i] *= f; + for (int n = 0; n < 16; ++n) o[n][2 * i] *= f, o[n][2 * i + 1] *= f; + for (int n = 0; n < 8; ++n) + for (int j = 2 * i; j < 2 * i + 2; ++j) { + s[n][j] = exp2f(s[n][j] - base); + l[i] += s[n][j]; + } + } + for (int k = 0; k < 4; ++k) { + const uint32_t pa[4] = {zh2(s[2 * k][0], s[2 * k][1]), zh2(s[2 * k][2], s[2 * k][3]), + zh2(s[2 * k + 1][0], s[2 * k + 1][1]), zh2(s[2 * k + 1][2], s[2 * k + 1][3])}; + for (int n = 0; n < 16; n += 2) { + uint32_t bb[4]; + zldm4(bb, &vs[16 * k + (lane & 7) + 8 * ((lane >> 3) & 1)][8 * n + 8 * (lane >> 4)], true); + zmma(o[n], pa, bb); + zmma(o[n + 1], pa, bb + 2); + } + } + } + }; + for (int w0 = 0; w0 < c_end; w0 += 1024) { + // the window's chunks any token reads: chunk 0, selected far ones, every recent one + if (threadIdx.x < 32) { // the block's selected far chunks (zha_bits_kernel), chunk 0 and the recent ones + const int wi = (w0 >> 5) + threadIdx.x; + uint32_t x = wi < words ? bits[(size_t) blockIdx.x * words + wi] : 0u; + for (int k = 0; k < 32; ++k) { + const int c = 32 * wi + k; + if (c >= c_end) x &= ~(1u << k); + else if (c == 0 || c >= c_ring) x |= 1u << k; + else if (c > nf_last) x &= ~(1u << k); + } + want[threadIdx.x] = x; + } + __syncthreads(); + // the window's chunks a stage ahead: tile k+1's bytes copy while tile k computes + if (threadIdx.x == 0) { // blockIdx.y of gridDim.y splits takes every gridDim.y-th chunk + int n = 0, k = 0; + for (int b = 0; b < 32; ++b) + for (uint32_t bits = want[b]; bits; bits &= bits - 1, ++k) + if (k % gridDim.y == blockIdx.y) lst[n++] = w0 + 32 * b + __ffs(bits) - 1; + nlst = n; + } + __syncthreads(); + const int n_items = nlst; + if (n_items > 0) zha_raw_issue(lst[0], zraw, stage, cmap, ring, n_ring, g, C, c_ring); + zha_commit(); + for (int it = 0; it < n_items; ++it) { + const int c = lst[it]; + if (it + 1 < n_items) { + zha_raw_issue(lst[it + 1], zraw + ((it + 1) & 1) * kZhaRaw, stage, cmap, ring, n_ring, g, C, c_ring); + zha_commit(); + zha_wait<1>(); + } else { + zha_wait<0>(); + } + __syncthreads(); + zha_raw_dequant(zraw + (it & 1) * kZhaRaw, c >= c_ring, ks, vs); + __syncthreads(); + tile(c); + __syncthreads(); + } + } + if (!warp_live) return; + for (int i = 0; i < 2; ++i) { + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 1); + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 2); + if (tok[i] >= rows) continue; + float* pp = part + (((size_t) tok[i] * per + p) * segs + blockIdx.y) * (kZhaDim + 2); // split y's segment + for (int n = 0; n < 16; ++n) { + pp[8 * n + 2 * tig] = o[n][2 * i]; + pp[8 * n + 2 * tig + 1] = o[n][2 * i + 1]; + } + if (tig == 0) { + pp[kZhaDim] = m[i] == -INFINITY ? -INFINITY : m[i] * 0.69314718f; // log2 units to natural + pp[kZhaDim + 1] = l[i]; + } + } +#else + (void) q, (void) stage, (void) cmap, (void) ring, (void) n_ring, (void) sel, (void) sel_heads, (void) hs, + (void) max_chunks, (void) pos, (void) rows, (void) n_head, (void) g, (void) per, (void) C, (void) recent, + (void) bias, (void) extent, (void) part, (void) segs, (void) bits, (void) words; +#endif +} + +// The chunk scores of a prompt batch for KV group g, fused: a launch block 64 tokens of the group's 3 heads (12 +// warps: 4 of 16 tokens a head) walks the summaries 64 (chunk, reference) rows at a time (64 / R chunks), takes +// q.khat * scale + bhat on the tensor cores and the largest over each chunk's R references in registers; score and +// rstar [per][rows][max_chunks] (the best reference of each). +template +__global__ void __launch_bounds__(kZhaWarps * 32) +zha_score_kernel(const float* __restrict__ q, const __half* __restrict__ khat, const float* __restrict__ bhat, + int nch, int max_chunks, int rows, int n_head, int g, int per, float* __restrict__ score, + uint8_t* __restrict__ rstar, size_t hs, size_t q8n, long long pos = 0, int C = 0, int recent = 0, + float* __restrict__ top = nullptr) { +#if ZHA_MMA + __shared__ __align__(16) __half ks[kZhaKeys][kZhaRow]; + __shared__ float bs[kZhaKeys]; + // top: each (head, token)'s best score over its far chunks 1..nf (zha_pick_kernel's), by atomics at the end + int nft[2] = {0, 0}; + float tmax[2] = {-INFINITY, -INFINITY}; + constexpr int CPT = kZhaKeys / R; // chunks a tile + // a tile's scores and choices of the block's 64 tokens and 3 heads, written out a row of CPT chunks at a time + __shared__ float sbuf[3][kZhaTokens][CPT]; + __shared__ uint8_t rbuf[3][kZhaTokens][CPT]; + const int t0 = blockIdx.x * kZhaTokens, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const int gid = lane >> 2, tig = lane & 3, p = w % per, head = g * per + p, tw = t0 + 16 * (w / per); + const bool warp_live = w < per * 4; + const int tok[2] = {tw + gid, tw + gid + 8}; + const float sc = rsqrtf((float) kZhaDim); + uint32_t qa[8][4]; + for (int k = 0; k < 8; ++k) + for (int i = 0; i < 2; ++i) + for (int hi = 0; hi < 2; ++hi) { + float2 x = make_float2(0.f, 0.f); + if (warp_live && tok[i] < rows) + x = *reinterpret_cast(q + ((size_t) tok[i] * n_head + head) * kZhaDim + 16 * k + 8 * hi + 2 * tig); + qa[k][i + 2 * hi] = zh2(x.x * sc, x.y * sc); + } + const __half* kg = khat + (size_t) g * zq_cap(q8n, max_chunks) * R * kZhaDim; + const float* bg = bhat + (size_t) g * zq_cap(q8n, max_chunks) * R; + // blockIdx.y: a part of the chunks (a launch of a few token blocks fills the card that way) + if (top) + for (int i = 0; i < 2; ++i) { + long long fr; + nft[i] = tok[i] < rows ? zha_far(pos + tok[i], C, recent, &fr) : 0; + } + const int span = ((nch + gridDim.y - 1) / gridDim.y + CPT - 1) / CPT * CPT; + const int cb = blockIdx.y * span, ce = min(nch, cb + span); + for (int c0 = cb; c0 < ce; c0 += CPT) { + const int nr = min(CPT, ce - c0) * R; // the tile's rows + for (int i = threadIdx.x; i < kZhaKeys * (kZhaDim / 8); i += blockDim.x) { + const int r = i / (kZhaDim / 8), d = 8 * (i % (kZhaDim / 8)); + uint4 x = make_uint4(0, 0, 0, 0); + if (r < nr) x = zsum8h(khat, q8n, ((size_t) g * zq_cap(q8n, max_chunks) + c0) * R + r, d); + *reinterpret_cast(&ks[r][d]) = x; + } + for (int i = threadIdx.x; i < kZhaKeys; i += blockDim.x) bs[i] = i < nr ? bg[(size_t) c0 * R + i] : -INFINITY; + __syncthreads(); + if (warp_live) { + float s[8][4]; + for (int n = 0; n < 8; ++n) { + s[n][0] = s[n][1] = s[n][2] = s[n][3] = 0.f; + for (int k = 0; k < 8; k += 2) { + uint32_t bb[4]; + zldm4(bb, &ks[8 * n + (lane & 7)][16 * k + 8 * (lane >> 3)], false); + zmma(s[n], qa[k], bb); + zmma(s[n], qa[k + 1], bb + 2); + } + } + // column 8 n + 2 tig + (j & 1) is (chunk c0 + col / R, reference col % R); a thread's two columns share + // a chunk. R 4: lanes tig 0, 1 hold one chunk of an n-tile, 2, 3 the other; R 8: the 4 lanes one chunk; + // R 16: the 4 lanes of n-tiles n and n + 1 (n even). + for (int i = 0; i < 2; ++i) { + float best[8]; + int arg[8]; + for (int n = 0; n < 8; ++n) { + const int col = 8 * n + 2 * tig; + const float v0 = s[n][2 * i] + bs[col], v1 = s[n][2 * i + 1] + bs[col + 1]; + best[n] = v1 > v0 ? v1 : v0; + arg[n] = v1 > v0 ? col + 1 : col; + const int span = R == 4 ? 2 : 4; // lanes a chunk within the n-tile + for (int o = 1; o < span; o <<= 1) { + const float ob = __shfl_xor_sync(0xffffffffu, best[n], o); + const int oa = __shfl_xor_sync(0xffffffffu, arg[n], o); + if (ob > best[n] || (ob == best[n] && oa < arg[n])) best[n] = ob, arg[n] = oa; + } + } + if constexpr (R == 16) + for (int n = 0; n < 8; n += 2) + if (best[n + 1] > best[n]) best[n] = best[n + 1], arg[n] = arg[n + 1]; + if (!warp_live || tok[i] >= rows) continue; + for (int n = 0; n < 8; ++n) { + bool writer; + if constexpr (R == 4) writer = (tig & 1) == 0; + else if constexpr (R == 8) writer = tig == 0; + else writer = tig == 0 && (n & 1) == 0; + const int c = c0 + arg[n] / R; + if (!writer || c >= ce) continue; + if (c >= 1 && c <= nft[i]) tmax[i] = fmaxf(tmax[i], best[n]); + sbuf[p][tok[i] - t0][c - c0] = best[n]; + rbuf[p][tok[i] - t0][c - c0] = (uint8_t) (arg[n] % R); + } + } + } + __syncthreads(); + // the tile out: a (head, token) row's CPT chunks side by side + const int nc = min(CPT, ce - c0), tn = min(kZhaTokens, rows - t0); + for (int i = threadIdx.x; i < per * kZhaTokens * CPT; i += blockDim.x) { + const int cc = i % CPT, t = (i / CPT) % kZhaTokens, pp = i / (CPT * kZhaTokens); + if (cc >= nc || t >= tn) continue; + const size_t at = (size_t) pp * hs + (size_t) (t0 + t) * max_chunks + c0 + cc; + score[at] = sbuf[pp][t][cc]; + rstar[at] = rbuf[pp][t][cc]; + } + } + if (top && warp_live) // the 4 lanes of a token row, then one atomic a (head, token) and block + for (int i = 0; i < 2; ++i) { + float m = tmax[i]; + m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, 1)); + m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, 2)); + if (tig == 0 && tok[i] < rows && m != -INFINITY) zha_atomic_max(top + (size_t) p * rows + tok[i], m); + } +#else + (void) q, (void) khat, (void) bhat, (void) nch, (void) max_chunks, (void) rows, (void) n_head, (void) g, (void) per, + (void) score, (void) rstar, (void) hs, (void) q8n, (void) pos, (void) C, (void) recent, (void) top; +#endif +} + +// A few tokens' chunk scores from int8 summaries: a thread a chunk of group blockIdx.y and token blockIdx.z, the +// group's heads' queries in shared memory, each head's best tangent and its reference into head * hs. +template +__global__ void zha_score_few_kernel(const float* __restrict__ q, const __half* __restrict__ khat, size_t q8n, + const float* __restrict__ bhat, int nch, int max_chunks, int n_head, int per, + float* __restrict__ score, uint8_t* __restrict__ rstar, size_t hs) { + __shared__ float qs[4][kZhaDim]; + const int g = blockIdx.y, t = blockIdx.z; + const float sc = rsqrtf((float) kZhaDim); + for (int i = threadIdx.x; i < per * kZhaDim; i += blockDim.x) + qs[i / kZhaDim][i % kZhaDim] = q[((size_t) t * n_head + g * per + i / kZhaDim) * kZhaDim + i % kZhaDim] * sc; + __syncthreads(); + const int c = blockIdx.x * blockDim.x + threadIdx.x; + if (c >= nch) return; + const int8_t* codes = reinterpret_cast(khat); + const uint16_t* scales = zq_scales(codes, q8n); + const bool q4 = zq_4(q8n); + float best[4] = {-INFINITY, -INFINITY, -INFINITY, -INFINITY}; + int arg[4] = {0, 0, 0, 0}; + for (int r = 0; r < R; ++r) { + const size_t row = ((size_t) g * zq_cap(q8n, max_chunks) + c) * R + r; + const int8_t* cr = codes + row * (q4 ? kZhaDim / 2 : kZhaDim); + float acc[4] = {0.f, 0.f, 0.f, 0.f}; + for (int b = 0; b < 4; ++b) { // 32-value blocks + const float d = __half2float(__ushort_as_half(scales[row * 4 + b])); + float part[4] = {0.f, 0.f, 0.f, 0.f}; + if (q4) { + const uint4 u = *reinterpret_cast(cr + 16 * b); + const uint32_t w4[4] = {u.x, u.y, u.z, u.w}; + for (int k = 0; k < 4; ++k) + for (int n = 0; n < 8; ++n) { + const float x = (float) ((int) ((w4[k] >> (4 * n)) & 15) - 8); + for (int p = 0; p < per; ++p) part[p] += x * qs[p][32 * b + 8 * k + n]; + } + } else { + for (int k = 0; k < 32; k += 4) { + const char4 x = *reinterpret_cast(cr + 32 * b + k); + for (int p = 0; p < per; ++p) + part[p] += x.x * qs[p][32 * b + k] + x.y * qs[p][32 * b + k + 1] + x.z * qs[p][32 * b + k + 2] + + x.w * qs[p][32 * b + k + 3]; + } + } + for (int p = 0; p < per; ++p) acc[p] += part[p] * d; + } + const float bb = bhat[row]; + for (int p = 0; p < per; ++p) + if (acc[p] + bb > best[p]) best[p] = acc[p] + bb, arg[p] = r; + } + for (int p = 0; p < per; ++p) { + const size_t at = (size_t) (g * per + p) * hs + (size_t) t * max_chunks + c; + score[at] = best[p]; + rstar[at] = (uint8_t) arg[p]; + } +} + +// The tail of a prompt batch on the tensor cores, for KV group g: a launch block 64 tokens of the group's 3 heads +// (12 warps: 4 of 16 tokens a head) walks the value estimates 64 (chunk, reference) rows at a time; a token's weight +// of a row is exp(its score of the chunk) where the reference is its best one and the chunk is a far one it did not +// select, else 0, and the weights times the tile go in as flash attention's P V does. The head's (top, sum, values) +// go to part [rows][per][segs][130] at segment `seg`. +template +__global__ void __launch_bounds__(kZhaWarps * 32) +zha_tail_mma_kernel(const float* __restrict__ score, const uint8_t* __restrict__ sel, const uint8_t* __restrict__ rstar, + const __half* __restrict__ vhat, int max_chunks, long long pos, int rows, int g, int per, int C, + int recent, size_t hs, int sel_heads, float* __restrict__ part, int segs, int seg, size_t q8n) { +#if ZHA_MMA + __shared__ __align__(16) __half vs[kZhaKeys][kZhaRow]; + constexpr int CPT = kZhaKeys / R; + const int t0 = blockIdx.x * kZhaTokens, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + const int gid = lane >> 2, tig = lane & 3, p = w % per, tw = t0 + 16 * (w / per); + const bool warp_live = w < per * 4; + const int tok[2] = {tw + gid, tw + gid + 8}; + int nf[2]; + const float* sc[2]; + const uint8_t* rs[2]; + const uint8_t* se[2]; + for (int i = 0; i < 2; ++i) { + long long fr; + const int t = min(tok[i], rows - 1); + nf[i] = tok[i] < rows ? zha_far(pos + tok[i], C, recent, &fr) : 0; + sc[i] = score + (size_t) p * hs + (size_t) t * max_chunks; + rs[i] = rstar + (size_t) p * hs + (size_t) t * max_chunks; + se[i] = sel + (sel_heads > 1 ? (size_t) p * hs : 0) + (size_t) t * max_chunks; + } + long long frl; + const int nf_last = zha_far(pos + min(t0 + kZhaTokens, rows) - 1, C, recent, &frl); + const __half* vg = vhat + (size_t) g * zq_cap(q8n, max_chunks) * R * kZhaDim; + float o[16][4], m[2] = {-INFINITY, -INFINITY}, l[2] = {0.f, 0.f}; + for (int n = 0; n < 16; ++n) o[n][0] = o[n][1] = o[n][2] = o[n][3] = 0.f; + // blockIdx.y: a part of the far chunks, its sums at segment seg + blockIdx.y + const int span = ((nf_last + 1 + gridDim.y - 1) / gridDim.y + CPT - 1) / CPT * CPT; + const int cb = blockIdx.y * span, ce = min(nf_last + 1, cb + span); + for (int c0 = cb; c0 < ce; c0 += CPT) { + const int nr = min(CPT, ce - c0) * R; + for (int i = threadIdx.x; i < kZhaKeys * (kZhaDim / 8); i += blockDim.x) { + const int r = i / (kZhaDim / 8), d = 8 * (i % (kZhaDim / 8)); + uint4 x = make_uint4(0, 0, 0, 0); + if (r < nr) x = zsum8h(vhat, q8n, ((size_t) g * zq_cap(q8n, max_chunks) + c0) * R + r, d); + *reinterpret_cast(&vs[r][d]) = x; + } + __syncthreads(); + if (warp_live) { + // the weights in log units: a thread's columns 8 n + 2 tig + (0, 1) share a chunk + float s[8][4]; + bool any = false; + for (int n = 0; n < 8; ++n) { + const int col = 8 * n + 2 * tig, c = c0 + col / R; + for (int i = 0; i < 2; ++i) { + float v0 = -INFINITY, v1 = -INFINITY; + if (c >= 1 && c <= nf[i] && !se[i][c]) { + const int r = rs[i][c]; + const float x = sc[i][c]; + if (col % R == r) v0 = x; + if ((col + 1) % R == r) v1 = x; + any = true; + } + s[n][2 * i] = v0, s[n][2 * i + 1] = v1; + } + } + if (__any_sync(0xffffffffu, any)) { + for (int i = 0; i < 2; ++i) { + float mx = -INFINITY; + for (int n = 0; n < 8; ++n) mx = fmaxf(mx, fmaxf(s[n][2 * i], s[n][2 * i + 1])); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 1)); + mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, 2)); + const float now = fmaxf(m[i], mx), base = now == -INFINITY ? 0.f : now; + const float f = expf(m[i] - base); + m[i] = now; + l[i] *= f; + for (int n = 0; n < 16; ++n) o[n][2 * i] *= f, o[n][2 * i + 1] *= f; + for (int n = 0; n < 8; ++n) + for (int j = 2 * i; j < 2 * i + 2; ++j) { + s[n][j] = expf(s[n][j] - base); + l[i] += s[n][j]; + } + } + for (int k = 0; k < 4; ++k) { + const uint32_t pa[4] = {zh2(s[2 * k][0], s[2 * k][1]), zh2(s[2 * k][2], s[2 * k][3]), + zh2(s[2 * k + 1][0], s[2 * k + 1][1]), zh2(s[2 * k + 1][2], s[2 * k + 1][3])}; + for (int n = 0; n < 16; n += 2) { + uint32_t bb[4]; + zldm4(bb, &vs[16 * k + (lane & 7) + 8 * ((lane >> 3) & 1)][8 * n + 8 * (lane >> 4)], true); + zmma(o[n], pa, bb); + zmma(o[n + 1], pa, bb + 2); + } + } + } + } + __syncthreads(); + } + if (!warp_live) return; + for (int i = 0; i < 2; ++i) { + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 1); + l[i] += __shfl_xor_sync(0xffffffffu, l[i], 2); + if (tok[i] >= rows) continue; + float* pp = part + (((size_t) tok[i] * per + p) * segs + seg + blockIdx.y) * (kZhaDim + 2); + for (int n = 0; n < 16; ++n) { + pp[8 * n + 2 * tig] = o[n][2 * i]; + pp[8 * n + 2 * tig + 1] = o[n][2 * i + 1]; + } + if (tig == 0) pp[kZhaDim] = m[i], pp[kZhaDim + 1] = l[i]; + } +#else + (void) score, (void) sel, (void) rstar, (void) vhat, (void) max_chunks, (void) pos, (void) rows, (void) g, (void) per, + (void) C, (void) recent, (void) hs, (void) sel_heads, (void) part, (void) segs, (void) seg, (void) q8n; +#endif +} + +// The cache's part of a step for KV head g: each chunk the step reads (cmap >= 0) the cache holds is stamped and +// mapped (g * P + its place); the others are misses (aux: [hand, count, chunks...]). +__global__ void zha_lookup_kernel(int* __restrict__ cmap, int nch, int g, int P, int max_chunks, int now, + int* __restrict__ stamp, const int* __restrict__ pmap, int* __restrict__ aux) { + if (g < 0) g = blockIdx.y, cmap += (size_t) g * max_chunks; // every group at once + const int c = blockIdx.x * blockDim.x + threadIdx.x; + if (c >= nch || cmap[c] < 0) return; + const int s = pmap[(size_t) g * max_chunks + c]; + if (s >= 0) { + stamp[(size_t) g * P + s] = now; + cmap[c] = g * P + s; + } else { + int* a = aux + (size_t) g * (P + 2); + const int i = atomicAdd(&a[1], 1); + if (i < P) a[2 + i] = c; + } +} + +// One thread: each miss takes the next place past the hand that this step does not read, evicting its chunk. +__global__ void zha_assign_kernel(int* __restrict__ cmap, int g, int P, int max_chunks, int now, int* __restrict__ owner, + int* __restrict__ stamp, int* __restrict__ pmap, int* __restrict__ aux) { + if (g < 0) g = blockIdx.x, cmap += (size_t) g * max_chunks; // a block a group + int* a = aux + (size_t) g * (P + 2); + int* own = owner + (size_t) g * P; + int* st = stamp + (size_t) g * P; + int* pm = pmap + (size_t) g * max_chunks; + const int n = min(a[1], P); + int h = a[0]; + for (int i = 0; i < n; ++i) { + const int c = a[2 + i]; + for (int k = 0; k < P && st[h] == now; ++k) h = (h + 1) % P; + if (own[h] >= 0) pm[own[h]] = -1; + own[h] = c; + pm[c] = h; + st[h] = now; + cmap[c] = (g * P + h) | kZhaMiss; + h = (h + 1) % P; + } + a[0] = h; +} + +// The same, every group at once (a block a group, P <= blockDim): the i-th miss takes the i-th place past the hand +// that this step does not read (a scan of the places in the clock's order), evicting its chunk. +__global__ void __launch_bounds__(1024) zha_assign_par_kernel(int* __restrict__ cmap, int P, int max_chunks, int now, + int* __restrict__ owner, int* __restrict__ stamp, + int* __restrict__ pmap, int* __restrict__ aux) { + __shared__ int sums[32]; + const int g = blockIdx.x, k = threadIdx.x, lane = k & 31, w = k >> 5; + cmap += (size_t) g * max_chunks; + int* a = aux + (size_t) g * (P + 2); + int* own = owner + (size_t) g * P; + int* st = stamp + (size_t) g * P; + int* pm = pmap + (size_t) g * max_chunks; + const int n = min(a[1], P), hand = a[0]; + if (n == 0) return; + const int h = (hand + k) % P; + const bool open = k < P && st[h] != now; + const unsigned m = __ballot_sync(0xffffffffu, open); + if (lane == 0) sums[w] = __popc(m); + __syncthreads(); + if (w == 0) { // the warps' counts, an inclusive scan + int v = lane < (int) (blockDim.x >> 5) ? sums[lane] : 0; + for (int o = 1; o < 32; o <<= 1) { + const int t = __shfl_up_sync(0xffffffffu, v, o); + if (lane >= o) v += t; + } + sums[lane] = v; + } + __syncthreads(); + const int i = __popc(m & ((1u << lane) - 1)) + (w ? sums[w - 1] : 0); + if (!open || i >= n) return; + const int c = a[2 + i]; + if (own[h] >= 0) pm[own[h]] = -1; + own[h] = c; + pm[c] = h; + st[h] = now; + cmap[c] = (g * P + h) | kZhaMiss; + if (i == n - 1) a[0] = (h + 1) % P; +} + +// The same for 4-bit leaves (C 64), 16-byte reads: a block a miss, a thread a quarter row of codes of K and V (64 rows x 4), +// the first 64 also a row's scales; every read of a chunk in flight at once. +__global__ void __launch_bounds__(256) zha_fill_q4_kernel(ZhaKv leaves, const int* __restrict__ cmap, + const int* __restrict__ aux, int P, ZhaKv pool, int max_chunks) { + const int g = blockIdx.y; + cmap += (size_t) g * max_chunks; + const int* a = aux + (size_t) g * (P + 2); + const int misses = min(a[1], P); + const int j = threadIdx.x >> 2, part = threadIdx.x & 3; // row j of the chunk, its 16 bytes `part` + for (int b = blockIdx.x; b < misses; b += gridDim.x) { + const int c = a[2 + b], slot = cmap[c] & ~kZhaMiss; + const size_t from = (size_t) ((long long) c * 64 + j) * leaves.row + (size_t) g * kZhaDim; // in values + const size_t to = ((size_t) slot * 64 + j) * kZhaDim; + const uint4 k = *reinterpret_cast(leaves.kc + from / 2 + 16 * part); + const uint4 v = *reinterpret_cast(leaves.vc + from / 2 + 16 * part); + uint2 ks = make_uint2(0, 0), vs = ks; + if (part == 0) { + ks = *reinterpret_cast(leaves.ks + from / 32); + vs = *reinterpret_cast(leaves.vs + from / 32); + } + *reinterpret_cast(const_cast(pool.kc) + to / 2 + 16 * part) = k; + *reinterpret_cast(const_cast(pool.vc) + to / 2 + 16 * part) = v; + if (part == 0) { + *reinterpret_cast(const_cast(pool.ks) + to / 32) = ks; + *reinterpret_cast(const_cast(pool.vs) + to / 32) = vs; + } + } +} + +__global__ void zha_fill_int_kernel(int* __restrict__ p, size_t n, int v) { + const size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; + if (i < n) p[i] = v; +} + +// An answer step's maps and counts anew, every group's: cmap -1, the staged count, the list's count, the misses'. +__global__ void zha_step_reset_kernel(int* __restrict__ cmap, size_t n, int* __restrict__ count, int* __restrict__ glist, + int max_chunks, int* __restrict__ aux, int P, int groups) { + for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) + cmap[i] = -1; + if (blockIdx.x == 0 && (int) threadIdx.x < groups) { + const int g = threadIdx.x; + count[g] = 0; + glist[(size_t) g * (max_chunks + 1)] = 0; + aux[(size_t) g * (P + 2) + 1] = 0; + } +} + +// For each block of 64 tokens, the far chunks any of its tokens selected (sels selections, hs apart): a bit a +// chunk, words of 32. A thread a word: its 32 bytes of each token's selection. +__global__ void zha_bits_kernel(const uint8_t* __restrict__ sel, int sels, size_t hs, int rows, int max_chunks, + uint32_t* __restrict__ bits, int words, unsigned long long* __restrict__ stats) { + const int blk = blockIdx.x, wi = blockIdx.y * blockDim.x + threadIdx.x; + if (wi >= words) return; + const int t0 = blk * kZhaTokens, t1 = min(rows, t0 + kZhaTokens); + uint4 acc0 = make_uint4(0, 0, 0, 0), acc1 = acc0; + for (int h = 0; h < sels; ++h) + for (int t = t0; t < t1; ++t) { + const uint4* r = reinterpret_cast(sel + h * hs + (size_t) t * max_chunks + 32 * (size_t) wi); + const uint4 a = r[0], b = r[1]; + acc0.x |= a.x, acc0.y |= a.y, acc0.z |= a.z, acc0.w |= a.w; + acc1.x |= b.x, acc1.y |= b.y, acc1.z |= b.z, acc1.w |= b.w; + } + const uint32_t v[8] = {acc0.x, acc0.y, acc0.z, acc0.w, acc1.x, acc1.y, acc1.z, acc1.w}; + uint32_t x = 0; + for (int k = 0; k < 32; ++k) + if ((v[k >> 2] >> (8 * (k & 3))) & 0xffu) x |= 1u << k; + bits[(size_t) blk * words + wi] = x; + if (stats) { // the block's far chunks, and the pairs its tokens selected + atomicAdd(&stats[0], (unsigned long long) __popc(x)); + unsigned long long pairs = 0; + for (int h = 0; h < sels; ++h) + for (int t = t0; t < t1; ++t) { + const uint32_t* r = reinterpret_cast(sel + h * hs + (size_t) t * max_chunks + 32 * (size_t) wi); + for (int k = 0; k < 8; ++k) { + const uint32_t b = r[k]; + pairs += ((b & 0xffu) != 0) + ((b >> 8 & 0xffu) != 0) + ((b >> 16 & 0xffu) != 0) + ((b >> 24) != 0); + } + } + atomicAdd(&stats[1], pairs); + } +} + +// One token's head: chunk 0 and the selected far chunks exactly from the staging buffer, the recent keys from the +// ring (with the bias by distance), the other far chunks as one key each (their score and value estimate), and the +// sink. Four warps take every fourth chunk, a lane 4 values; their sums meet at the end. +__global__ void __launch_bounds__(128) +zha_attend_kernel(const float* __restrict__ q, ZhaKv stage, const int* __restrict__ cmap, ZhaKv ring, long long n_ring, + const float* __restrict__ score, const uint8_t* __restrict__ sel, const uint8_t* __restrict__ rstar, + const __half* __restrict__ vhat, int max_chunks, long long pos, int n_head, int kv, int head, int R, + int C, int recent, int tail, const float* __restrict__ rr, const float* __restrict__ table, int extent, + int nrel, const float* __restrict__ sinks, float* __restrict__ out, size_t head_stride, int sel_heads, + float* __restrict__ part_out, int mode, int seg_base, int seg_total, int part_heads, int all, + size_t q8n) { + __shared__ float rv[32], wm[4], wl[4], wo[4][kZhaDim]; + const int t = blockIdx.x, lane = threadIdx.x & 31, w = threadIdx.x >> 5; + // the group's head blockIdx.y (or with `all`, head blockIdx.y of every group): its scores, value choices and + // (each head's own, or the group's) selection + head += blockIdx.y; + const int g = head / (n_head / kv); + if (all) { + cmap += (size_t) g * max_chunks; + if (sel_heads == 1) sel += (size_t) g * head_stride; + } + score += blockIdx.y * head_stride; + rstar += blockIdx.y * head_stride; + if (sel_heads > 1) sel += blockIdx.y * head_stride; + const int seg = blockIdx.z, segs = gridDim.z; + const long long own = pos + t; + long long fr; + zha_far(own, C, recent, &fr); + const float sc = rsqrtf((float) kZhaDim); + const float4 q4 = *reinterpret_cast(q + ((size_t) t * n_head + head) * kZhaDim + 4 * lane); + if ((int) threadIdx.x < nrel) rv[threadIdx.x] = rr[((size_t) t * n_head + head) * nrel + threadIdx.x]; + __syncthreads(); + const uint8_t* mine = sel + (size_t) t * max_chunks; + float m = -INFINITY, l = 0.f, acc[4] = {0.f, 0.f, 0.f, 0.f}; + const auto take = [&](float s, const float (&v)[4]) { + const float nm = fmaxf(m, s), f = expf(m - nm), e = expf(s - nm); + l = l * f + e; +#pragma unroll + for (int i = 0; i < 4; ++i) acc[i] = acc[i] * f + e * v[i]; + m = nm; + }; + const int last = (int) (own / C); + const int cps = (last + segs) / segs, cb = seg * cps, ce = min(last + 1, cb + cps); // this block's chunks + for (int c = cb + w; c < ce; c += 4) { + const bool recent_c = (long long) c * C >= fr; + if (recent_c || c == 0 || mine[c]) { + if (mode == 2) continue; // the exact keys are another kernel's + const long long j1 = min((long long) (c + 1) * C, own + 1); + for (long long j = (long long) c * C; j < j1; ++j) { + float k[4], v[4]; + if (recent_c) zload(ring, j % n_ring, g, lane, k, v); + else zload(stage, (long long) (cmap[c] & ~kZhaMiss) * C + (j - (long long) c * C), g, lane, k, v); + float s = warp_sum(q4.x * k[0] + q4.y * k[1] + q4.z * k[2] + q4.w * k[3]) * sc; + const long long d = own - j; + if (d < extent) { + const float* tt = table + (size_t) d * nrel; + float b = 0.f; + for (int k2 = 0; k2 < nrel; ++k2) b += rv[k2] * tt[k2]; + s += b; + } + take(s, v); + } + } else if (tail) { + float v[4]; + zsum4(vhat, q8n, ((size_t) g * zq_cap(q8n, max_chunks) + c) * R + rstar[(size_t) t * max_chunks + c], lane, v); + take(score[(size_t) t * max_chunks + c], v); + } + } + if (lane == 0) wm[w] = m, wl[w] = l; +#pragma unroll + for (int i = 0; i < 4; ++i) wo[w][4 * lane + i] = acc[i]; + __syncthreads(); + if (w != 0) return; + if (segs > 1 || mode == 2) { // a part of the head's sums, for the join: (top, sum, values) unnormalized + float M = -INFINITY; + for (int k = 0; k < 4; ++k) M = fmaxf(M, wm[k]); + float Z = 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int k = 0; k < 4; ++k) { + if (wm[k] == -INFINITY) continue; + const float f = expf(wm[k] - M); + Z += wl[k] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += wo[k][4 * lane + i] * f; + } + const int ph = part_heads == n_head ? head : head % part_heads; // a group's buffer: its heads + float* pp = part_out + (((size_t) t * part_heads + ph) * seg_total + seg_base + seg) * (kZhaDim + 2); +#pragma unroll + for (int i = 0; i < 4; ++i) pp[4 * lane + i] = o[i]; + if (lane == 0) pp[kZhaDim] = M, pp[kZhaDim + 1] = Z; + return; + } + const float sink = sinks ? sinks[(size_t) t * n_head + head] : -INFINITY; + float M = sink; + for (int k = 0; k < 4; ++k) M = fmaxf(M, wm[k]); + float Z = sinks ? expf(sink - M) : 0.f, o[4] = {0.f, 0.f, 0.f, 0.f}; + for (int k = 0; k < 4; ++k) { + if (wm[k] == -INFINITY) continue; + const float f = expf(wm[k] - M); + Z += wl[k] * f; +#pragma unroll + for (int i = 0; i < 4; ++i) o[i] += wo[k][4 * lane + i] * f; + } + float* y = out + ((size_t) t * n_head + head) * kZhaDim + 4 * lane; +#pragma unroll + for (int i = 0; i < 4; ++i) y[i] = o[i] / Z; +} + +} // namespace + +void store_q4(const float* x, int8_t* codes, uint16_t* scales, int n, long long pos, int rows, long long ring, + void* stream) { + if (rows <= 0) return; + if (n % 32) throw std::invalid_argument("ink::store_q4: rows of 32-value blocks"); + const size_t blocks = (size_t) rows * (n / 32); // 8 threads a block + store_q4_kernel<<>>(x, codes, scales, n, pos, rows, ring); +} + +void q4_to_q8(const int8_t* src_codes, const uint16_t* src_scales, long long from, int8_t* dst_codes, + uint16_t* dst_scales, long long to, long long ring, int n, int rows, void* stream) { + if (rows <= 0) return; + const size_t blocks = (size_t) rows * (n / 32); + q4_to_q8_kernel<<>>(src_codes, src_scales, from, dst_codes, dst_scales, to, + ring, n, rows); +} + +void zha_cache_clear(ZhaCache& cache, int kv_heads, int max_chunks, void* stream) { + if (cache.P <= 0) return; + const size_t np = (size_t) kv_heads * cache.P, nm = (size_t) kv_heads * max_chunks; + zha_fill_int_kernel<<>>(cache.owner, np, -1); + zha_fill_int_kernel<<>>(cache.stamp, np, -1); + zha_fill_int_kernel<<>>(cache.pmap, nm, -1); + zha_fill_int_kernel<<>>( + cache.aux, (size_t) kv_heads * (cache.P + 2), 0); +} + +void zha_build(const float* refs, const ZhaKv& ring, long long n_ring, int c0, int c1, int C, int R, int kv_heads, + int max_chunks, uint16_t* khat, uint16_t* vhat, float* bhat, void* stream, size_t q8n) { + if (c1 <= c0) return; + if (c1 > zq_cap(q8n, max_chunks)) throw std::logic_error("ink::zha_build: past the summaries' room"); + zha_build_kernel<<>>(refs, ring, n_ring, c0, C, R, max_chunks, + (__half*) khat, (__half*) vhat, bhat, q8n); +} + +void zha_attend(void* blas, const Zha& z, const float* q, const ZhaKv& leaves, const ZhaKv& ring, long long n_ring, + const uint16_t* khat, const uint16_t* vhat, const float* bhat, int built, int max_chunks, long long pos, + int rows, int n_head, int kv_heads, const float* r, const float* table, int extent, int nrel, + const float* sinks, const ZhaWork& wk, float* out, void* stream, ZhaCache* cache, bool keep, int sum_cap) { + if (rows <= 0) return; + if (z.C != kZhaKeys || z.R != 4 || !leaves.q4 || !ring.kc || ring.q4) + throw std::logic_error("ink::zha_attend: chunks of 64, 4 references, 4-bit leaves and a Q8_0 ring"); + // a selection's chunks in shared memory: a score each (zha_pick_select_dec_kernel, zha_select_tile2/3_kernel) + constexpr size_t kSmem = 64 * 1024 + 4096; + constexpr int kTile = 64; // a prompt's tokens choose their chunks together, 64 at a time + constexpr int kSegExact = 16; // an answer's token: the exact keys in 16 segments, + constexpr int kSegTail = 32; // the tail in 32 + constexpr int kFillBlocks = 4; // the cache's misses: a few blocks a group keep the bus busy, the SMs free + constexpr int kExactSplit = 3; // a prompt's exact part in 3 blocks a tile (each every third chunk) + const int cap = sum_cap > 0 ? sum_cap : max_chunks; // the summaries' room a KV head (zq_cap()) + // the staging buffer: a head's chunks at a time (wk.heads 1), or every head's kept across a slot's calls + const bool kept_stage = wk.heads == kv_heads; + const int per = n_head / kv_heads; + // a token at a time with the cache: its chunks from the VRAM it keeps, only the misses over the bus (a KV group's + // heads choose together: one selection a group) + const bool cached = cache && cache->P > 0 && rows == 1 && 1 + std::min(z.max_sel, max_chunks) <= cache->P; + if (!cached && (!kept_stage || !keep)) { + zha_fill_int_kernel<<>>( + wk.cmap, (size_t) wk.heads * max_chunks, -1); + if (cudaMemsetAsync(wk.count, 0, (size_t) wk.heads * sizeof(int), cs(stream)) != cudaSuccess) + throw std::runtime_error("ink::zha_attend: memset"); + } + if (cached) ++cache->now; + const cublasHandle_t h = (cublasHandle_t) blas; + const float one = 1.f, zero = 0.f; + const int nch = std::max(built, 1); + const size_t hs = (size_t) rows * max_chunks; // a head's scores + const size_t q8n = zha_q8n_make((size_t) kv_heads * cap * z.R, cap, true); // the 4-bit summaries + // A few tokens (an answer): every head's chunk scores first, a token's in one launch over the groups, so the + // summaries are read once; and each head's attention in segments of its chunks, joined after. A prompt batch: a + // group's scores at a time, its tokens on the tensor cores. + const bool few = rows <= 4; + const int segs = few ? 32 : 1; + // an answer's token through the cache with its own kernels (groups of 2 or 3 heads, selections that fit kSmem) + const bool dec = cached && per > 1 && per <= 3 && (size_t) max_chunks * sizeof(float) <= kSmem; + if (dec && built > 0) { + zha_score_dec_kernel<<>>( + q, (const int8_t*) khat, q8n, bhat, nch, max_chunks, n_head, per, wk.score, wk.rstar, hs); + } else if (few && built > 0) { // a thread a chunk of a group, its heads' scores at once + zha_score_few_kernel<4><<>>( + q, (const __half*) khat, q8n, bhat, nch, max_chunks, n_head, per, wk.score, wk.rstar, hs); + } + if (cached) { + // every group at once: selections, the chunks they read, the cache's hits and misses, the attention + const int words = max_chunks / 32; + int sels = per; + if (dec) { + static bool attr = false; + if (!attr) { + cudaFuncSetAttribute(zha_pick_select_dec_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, kSmem); + attr = true; + } + // a chunk the cache holds gets 1 added to its pick: an answer's tokens keep reading what VRAM has + zha_pick_select_dec_kernel<<>>( + wk.score, per, hs, max_chunks, pos, z.C, z.recent, z.budget, z.min_chunks, z.max_sel, wk.sel, + cache->pmap, 1.f); + sels = 1; + } else if (per > 1) { + zha_pick_kernel<<>>(wk.score, per, hs, max_chunks, pos, z.C, + z.recent, wk.score + (size_t) n_head * hs); + zha_select_kernel<<>>( + wk.score + (size_t) n_head * hs, max_chunks, pos, z.C, z.recent, z.budget, z.min_chunks, z.max_sel, + wk.sel, hs); + sels = 1; + } else { + zha_select_kernel<<>>(wk.score, max_chunks, pos, z.C, z.recent, + z.budget, z.min_chunks, z.max_sel, wk.sel, hs); + } + zha_step_reset_kernel<<<64, 256, 0, cs(stream)>>>(wk.cmap, (size_t) kv_heads * max_chunks, wk.count, wk.glist, + max_chunks, cache->aux, cache->P, kv_heads); + zha_union_kernel<<>>(wk.sel, sels, hs, rows, max_chunks, + built, wk.cmap, wk.count, 0, wk.glist); + zha_lookup_kernel<<>>( + wk.cmap, built, -1, cache->P, max_chunks, cache->now, cache->stamp, cache->pmap, cache->aux); + if (cache->P <= 1024) + zha_assign_par_kernel<<P + 31) / 32 * 32, 0, cs(stream)>>>( + wk.cmap, cache->P, max_chunks, cache->now, cache->owner, cache->stamp, cache->pmap, cache->aux); + else + zha_assign_kernel<<>>(wk.cmap, -1, cache->P, max_chunks, cache->now, + cache->owner, cache->stamp, cache->pmap, cache->aux); + const auto fill = [&](cudaStream_t s) { + zha_fill_q4_kernel<<>>(leaves, wk.cmap, cache->aux, cache->P, + cache->pool, max_chunks); + }; + if (!dec) { + fill(cs(stream)); + zha_attend_kernel<<>>( + q, cache->pool, wk.cmap, ring, n_ring, wk.score, wk.sel, wk.rstar, (const __half*) vhat, max_chunks, + pos, n_head, kv_heads, 0, z.R, z.C, z.recent, 1, r, table, extent, nrel, sinks, out, hs, sels, wk.S, 1, + 0, segs, n_head, 1, q8n); + zha_join_kernel<<>>(wk.S, segs, n_head, 0, sinks, out); + return; + } + // the misses cross the bus (the fill waits on PCIe) while the tail reads VRAM: on a stream of their own + static cudaStream_t side = nullptr; + static cudaEvent_t ready = nullptr, filled = nullptr; + if (!side) { + int lo = 0, hi = 0; // the fill's stream first: its few blocks wait on the bus, not on the SMs + cudaDeviceGetStreamPriorityRange(&lo, &hi); + if (cudaStreamCreateWithPriority(&side, cudaStreamNonBlocking, hi) != cudaSuccess || + cudaEventCreateWithFlags(&ready, cudaEventDisableTiming) != cudaSuccess || + cudaEventCreateWithFlags(&filled, cudaEventDisableTiming) != cudaSuccess) + throw std::runtime_error("ink::zha_attend: the fill's stream"); + } + cudaEventRecord(ready, cs(stream)); + cudaStreamWaitEvent(side, ready, 0); + fill(side); + cudaEventRecord(filled, side); + // the tail in segments, then (the fill done) the exact part: the selected chunks, chunk 0 and the recent keys, + // all joined with the sinks + constexpr int segs_all = kSegExact + kSegTail; + zha_tail_dec_kernel<<>>( + wk.score, wk.rstar, wk.sel, (const __half*) vhat, q8n, max_chunks, pos, n_head, kv_heads, z.R, z.C, + z.recent, hs, sels, wk.S, kSegExact, segs_all); + cudaStreamWaitEvent(cs(stream), filled, 0); + if (per == 3 && nrel <= 16) // a group's three heads in one launch + zha_exact_dec_group_kernel<<>>( + q, cache->pool, wk.cmap, wk.glist, ring, n_ring, max_chunks, pos, n_head, z.C, z.recent, r, table, + extent, nrel, wk.S, segs_all); + else + zha_exact_dec_kernel<<>>( + q, cache->pool, wk.cmap, wk.glist, ring, n_ring, max_chunks, pos, n_head, kv_heads, z.C, z.recent, r, + table, extent, nrel, wk.S, segs_all); + zha_join_kernel<<>>(wk.S, segs_all, n_head, 0, sinks, out); + return; + } + for (int g = 0; g < kv_heads; ++g) { + float* sc = few ? wk.score + (size_t) g * per * hs : wk.score; // the group's heads' scores, hs apart + uint8_t* rs = few ? wk.rstar + (size_t) g * per * hs : wk.rstar; + float* pick = few ? wk.score + (size_t) n_head * hs : wk.score + (size_t) per * hs; + const bool fused = !few && built > 0; + // a launch of a prompt call's 64-token blocks, cut further by chunks so it fills the card + const int tblocks = (rows + kZhaTokens - 1) / kZhaTokens; + const int split = std::max(1, std::min(8, (72 + tblocks - 1) / tblocks)); + // the scores' launch keeps each (head, token)'s best and zha_select_tile3_kernel picks from the scores + const bool sel3 = fused && per > 1 && per <= 4 && (size_t) max_chunks * sizeof(float) <= kSmem; + float* top = sel3 ? pick : nullptr; // pick's room: per x rows floats + if (sel3 && cudaMemsetAsync(top, 0xFF, (size_t) per * rows * sizeof(float), cs(stream)) != cudaSuccess) + throw std::runtime_error("ink::zha_attend: memset"); + if (fused) // the chunk scores on the tensor cores, the largest reference in registers + zha_score_kernel<4><<>>(q, (const __half*) khat, bhat, nch, max_chunks, rows, n_head, g, per, sc, + rs, hs, q8n, pos, z.C, z.recent, top); + // the selections: the group's one (or each head's, in a group of one); a prompt's in tiles of tokens + int sels = per; + if (sel3) { + static bool attr3 = false; + if (!attr3) { + cudaFuncSetAttribute(zha_select_tile3_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, kSmem); + attr3 = true; + } + const int tiles = (rows + kTile - 1) / kTile; + zha_select_tile3_kernel<<>>( + sc, per, hs, top, max_chunks, pos, rows, kTile, z.C, z.recent, z.budget, z.min_chunks, z.max_sel, + wk.sel); + sels = 1; + } else if (per > 1) { + zha_pick_kernel<<>>(sc, per, hs, max_chunks, pos, z.C, z.recent, pick); + if (few) + zha_select_kernel<<>>(pick, max_chunks, pos, z.C, z.recent, z.budget, + z.min_chunks, z.max_sel, wk.sel, 0); + sels = 1; + } else if (few) { + zha_select_kernel<<>>(sc, max_chunks, pos, z.C, z.recent, z.budget, + z.min_chunks, z.max_sel, wk.sel, hs); + } + if (!few && !sel3) { + const bool picks = sels == 1 && per > 1; + const float* base = picks ? pick : sc; + const int tiles = (rows + kTile - 1) / kTile; + if ((size_t) max_chunks * sizeof(float) <= kSmem) { + static bool attr = false; + if (!attr) { + cudaFuncSetAttribute(zha_select_tile2_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, kSmem); + attr = true; + } + zha_select_tile2_kernel<<>>( + base, max_chunks, pos, rows, kTile, z.C, z.recent, z.budget, z.min_chunks, z.max_sel, wk.sel, hs, + picks); + } else { + float* comb = wk.S + (size_t) per * rows * extent + 65536; // past the bias and the bits + if ((size_t) (comb - wk.S) + (size_t) sels * tiles * max_chunks > wk.s_floats) + throw std::runtime_error("ink::zha_attend: the scratch is too small for tile selections"); + zha_select_tile_kernel<<>>( + base, max_chunks, pos, rows, kTile, z.C, z.recent, z.budget, z.min_chunks, z.max_sel, wk.sel, hs, + comb); + } + } + // the chunks they read, staged: from the leaves in RAM into VRAM of their own + const int hk = kept_stage ? g : 0; // the group's map + int* cmap = wk.cmap + (size_t) hk * max_chunks; + if (!kept_stage && g > 0) { // the head before's places are not this one's + zha_fill_int_kernel<<>>(cmap, (size_t) max_chunks, -1); + if (cudaMemsetAsync(wk.count, 0, sizeof(int), cs(stream)) != cudaSuccess) + throw std::runtime_error("ink::zha_attend: memset"); + } + if (cudaMemsetAsync(wk.glist, 0, sizeof(int), cs(stream)) != cudaSuccess) + throw std::runtime_error("ink::zha_attend: memset"); + zha_union_kernel<<>>(wk.sel, sels, hs, rows, max_chunks, built, + cmap, wk.count + hk, hk * max_chunks, + wk.glist); + zha_gather_kernel<<>>(leaves, g, z.C, cmap, wk.glist, wk.stage); + if (!few) { + // a prompt batch: the exact keys on the tensor cores, the tail on its own, joined with the sinks. In + // wk.S: the group's bias by distance [per][rows][extent], the blocks' bits, the partials. + float* bias = wk.S; + const int words = max_chunks / 32, blocks = (rows + kZhaTokens - 1) / kZhaTokens; + uint32_t* bits = reinterpret_cast(bias + (size_t) per * rows * extent); + float* part = reinterpret_cast(bits + (size_t) blocks * words + 64); + // the parts: the exact keys' at 0 .. kExactSplit - 1, the tail's after (the tensor-core tail, cut by + // chunks) + const int tsplit = std::max(1, std::min(split, nch / (kZhaKeys / 4))), psegs = kExactSplit + tsplit; + if ((size_t) (part - wk.S) + (size_t) rows * per * psegs * (kZhaDim + 2) > wk.s_floats) + throw std::runtime_error("ink::zha_attend: the scratch is too small for a prompt batch"); + for (int pp = 0; pp < per; ++pp) { // bias[pp][t][d] = r[t][head].table[d] + const cublasStatus_t st = cublasSgemm(h, CUBLAS_OP_T, CUBLAS_OP_N, extent, rows, nrel, &one, table, nrel, + r + (size_t) (g * per + pp) * nrel, n_head * nrel, &zero, + bias + (size_t) pp * rows * extent, extent); + if (st != CUBLAS_STATUS_SUCCESS) throw std::runtime_error("ink::zha_attend: bias product failed"); + } + zha_bits_kernel<<>>(wk.sel, sels, hs, rows, max_chunks, + bits, words, nullptr); + static bool attr = false; + if (!attr) { + cudaFuncSetAttribute(zha_exact_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, 2 * kZhaRaw); + attr = true; + } + zha_exact_kernel<<>>( + q, wk.stage, cmap, ring, n_ring, wk.sel, sels, hs, max_chunks, pos, rows, n_head, g, per, z.C, + z.recent, bias, extent, part, psegs, bits, words); + zha_tail_mma_kernel<4><<>>( + sc, wk.sel, rs, (const __half*) vhat, max_chunks, pos, rows, g, per, z.C, z.recent, hs, sels, part, + psegs, kExactSplit, q8n); + zha_join_group_kernel<<>>(part, psegs, per, n_head, g * per, sinks, out); + continue; + } + zha_attend_kernel<<>>( + q, wk.stage, cmap, ring, n_ring, sc, wk.sel, rs, (const __half*) vhat, max_chunks, pos, n_head, kv_heads, + g * per, z.R, z.C, z.recent, 1, r, table, extent, nrel, sinks, out, hs, sels, wk.S, 1, 0, segs, n_head, 0, + q8n); + zha_join_kernel<<>>(wk.S, segs, n_head, g * per, sinks, out); + } +} + +} // namespace strata::poly::ink + +#endif // NVIDIA diff --git a/engine/src/poly/inkling2_zha.hpp b/engine/src/poly/inkling2_zha.hpp new file mode 100644 index 0000000000..01d6ce27a1 --- /dev/null +++ b/engine/src/poly/inkling2_zha.hpp @@ -0,0 +1,97 @@ +// src/poly/inkling2_zha.hpp - the ZHA index of Inkling 2's full layers (inkling2_zha.cu, docs/INKLING_ZHA.md). +#pragma once + +#include +#include + +namespace strata::poly::ink { + +/// ZHA (Zyphra Hierarchical Attention, arXiv 2610.42112) over a full layer's keys, one level: chunks of C keys, +/// each summarized for R reference queries; a token's attention reads key 0's chunk, its last `recent` keys and the +/// chunks its summaries rank best, and (tail) the other chunks as one estimated key each. +// q8n's flag: the summaries as 4-bit codes (two a byte, low nibble first, value (code - 8) * scale) then their fp16 +// scales a 32 values; without it int8 codes then the scales. Bits 32-61: the summaries' chunks a KV head (their +// room, which grows with a slot's context; 0: max_chunks). Bits 0-31: the summaries' rows. +constexpr size_t kZhaQ4 = size_t(1) << 62; +inline size_t zha_q8n_make(size_t rows, int cap, bool q4) { return rows | ((size_t) cap << 32) | (q4 ? kZhaQ4 : 0); } + +/// The index's shape (zha_attend() takes C 64 and R 4 only): a KV group's heads read the same chunks, a prompt's +/// tokens choose theirs 64 at a time, the summaries and the leaves are 4-bit, the ring Q8_0. +struct Zha { + int C = 64, R = 4, recent = 1024; + float budget = 0.16f; ///< the share of a token's far chunks it reads exactly + int min_chunks = 1; + int max_sel = 256; ///< at most this many chunks a selection +}; + +/// Where a full layer's keys and values are: fp16 (k16, v16) or Q8_0 (kc, ks, vc, vs: int8 codes, an fp16 scale a +/// 32-value block). Key k of KV head g is at k * row (+ g * 128 if headed: a cache's rows hold every KV head's). +struct ZhaKv { + const uint16_t* k16 = nullptr; + const uint16_t* v16 = nullptr; + const int8_t* kc = nullptr; + const uint16_t* ks = nullptr; + const int8_t* vc = nullptr; + const uint16_t* vs = nullptr; + long long row = 0; + int headed = 1; + int q4 = 0; ///< kc/vc hold 4-bit codes, two a byte (low nibble first), value = (code - 8) * scale +}; + +/// rows rows of n values (x) as 4-bit codes and an fp16 scale a 32-value block, from row pos of codes / scales +/// (n / 2 bytes and n / 32 fp16 a row); with ring, row p goes to p % ring. +void store_q4(const float* x, int8_t* codes, uint16_t* scales, int n, long long pos, int rows, long long ring, + void* stream); +/// rows rows of n values from 4-bit (src_codes, src_scales, from row `from`) to Q8_0 (dst, from row `to`, % ring). +void q4_to_q8(const int8_t* src_codes, const uint16_t* src_scales, long long from, int8_t* dst_codes, + uint16_t* dst_scales, long long to, long long ring, int n, int rows, void* stream); + +/// ZHA's scratch: S s_floats floats; score (heads a group + 1) * rows * max_chunks +/// floats; rstar and sel heads a group * rows * max_chunks bytes; cmap max_chunks ints, count one; stage: a KV head's +/// keys and values of max_chunks chunks (row 128, not headed), in the leaves' format. +struct ZhaWork { + float* S = nullptr; + size_t s_floats = 0; + float* score = nullptr; + uint8_t* rstar = nullptr; + uint8_t* sel = nullptr; + int* cmap = nullptr; ///< [heads][max_chunks]: where each chunk is staged (heads 1, or kv_heads when kept) + int* count = nullptr; ///< [heads] staged chunks + int* glist = nullptr; ///< [max_chunks + 1]: the chunks a call stages (count first) + int heads = 1; ///< 1: a KV head's chunks at a time; kv_heads: every head's, kept across a slot's calls + ZhaKv stage; +}; + +/// A full layer's slot's chunks kept in VRAM between an answer's tokens: a pool of P chunks a KV head (pool: the +/// leaves' format, row 128, not headed; head g's chunk s at g * P + s), which chunk each holds (owner [kv][P]), when it +/// was last read (stamp [kv][P]), where each chunk is (pmap [kv][max_chunks], -1 if not), the clock hand and the +/// misses of a step ([kv][P + 2]: hand, count, chunks). A step of one token reads its chunks from it. +struct ZhaCache { + ZhaKv pool; + int P = 0; + int* owner = nullptr; + int* stamp = nullptr; + int* pmap = nullptr; + int* aux = nullptr; + int now = 0; +}; +/// Forget every chunk the cache holds (the sequence jumped: the leaves may hold other rows now). +void zha_cache_clear(ZhaCache& cache, int kv_heads, int max_chunks, void* stream); + +/// The summaries of chunks [c0, c1) of a full layer's slot, from its ring (key j at j % n_ring): per KV head g and +/// reference r (refs [kv_heads][R][128]), khat = sum softmax(q_r.k / sqrt 128) k, vhat the values with the same +/// weights (fp16, [kv_heads][max_chunks][R][128]), bhat = LSE - q_r.khat / sqrt 128 ([kv_heads][max_chunks][R]). +void zha_build(const float* refs, const ZhaKv& ring, long long n_ring, int c0, int c1, int C, int R, int kv_heads, + int max_chunks, uint16_t* khat, uint16_t* vhat, float* bhat, void* stream, size_t q8n = 0); + +/// The attention of `rows` tokens from position pos (q [rows][n_head][128], already scaled by tau) over a full +/// layer's slot through its summaries (chunks [0, built)): chunk 0 and the selected far chunks from the leaves (staged +/// into wk.stage a KV head at a time), the recent keys from the ring, the bias by distance (r [rows][n_head][nrel] +/// times table [extent][nrel]) and each token's sink. out [rows][n_head][128]. +void zha_attend(void* blas, const Zha& z, const float* q, const ZhaKv& leaves, const ZhaKv& ring, long long n_ring, + const uint16_t* khat, const uint16_t* vhat, const float* bhat, int built, int max_chunks, long long pos, + int rows, int n_head, int kv_heads, const float* r, const float* table, int extent, int nrel, + const float* sinks, const ZhaWork& wk, float* out, void* stream, ZhaCache* cache = nullptr, + bool keep = false, int sum_cap = 0); // sum_cap: the summaries' chunks a KV head (0: max_chunks) + +} // namespace strata::poly::ink diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a1705..b0500670e2 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -16815,6 +16815,152 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_INK_ZHA", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cpp", + "line": 35, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "string", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/INKLING_ZHA.md:1", + "docs/INKLING_ZHA.md:7", + "docs/INKLING_ZHA.md:11", + "docs/INKLING_ZHA.md:22", + "docs/INKLING_ZHA.md:78", + "docs/INKLING_ZHA.md:98", + "docs/models/inkling.md:337" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "poly", + "note": "Inkling 2 on NVIDIA from Turing on: the path of a ZHA reference file (C 64, R 4) turns on the ZHA index over the 8 full-attention runs: their keys and values in pinned RAM as 4-bit leaves, 64-key chunk summaries in VRAM, a token reading chunk 0, its last 1,024 keys and its best-scored far chunks exactly and the others through their summaries. Off unless set; the answers change (mean KL about 0.016 at 32K). One sequence on one card: refused with --batch above 1, --draft-model, --layer-split, --expert-gpu, on HIP and Metal (engine/src/poly/inkling2.cpp zha_load(), inkling2_zha.cu, docs/INKLING_ZHA.md)", + "retire_after": "", + "retired_in": "" + }, + { + "name": "STRATA_INK_ZHA_BUDGET", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cpp", + "line": 1989, + "backend": "cuda", + "fn": "env_num" + } + ], + "parse": "float", + "default_state": null, + "code_default": "0.16", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/INKLING_ZHA.md:37" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "poly", + "note": "With STRATA_INK_ZHA: the share of a token's far chunks it reads exactly, 0.16 unless set (engine/src/poly/inkling2.cpp zha_load(), inkling2_zha.cu, docs/INKLING_ZHA.md)", + "retire_after": "", + "retired_in": "" + }, + { + "name": "STRATA_INK_ZHA_MAX", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cpp", + "line": 1990, + "backend": "cuda", + "fn": "env_num" + } + ], + "parse": "float", + "default_state": null, + "code_default": "256", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/INKLING_ZHA.md:38" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "poly", + "note": "With STRATA_INK_ZHA: at most this many far chunks a token reads exactly, 256 unless set (engine/src/poly/inkling2.cpp zha_load(), inkling2_zha.cu, docs/INKLING_ZHA.md)", + "retire_after": "", + "retired_in": "" + }, + { + "name": "STRATA_INK_ZHA_POOL", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cpp", + "line": 1991, + "backend": "cuda", + "fn": "env_num" + } + ], + "parse": "float", + "default_state": null, + "code_default": "600", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/INKLING_ZHA.md:39" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "poly", + "note": "With STRATA_INK_ZHA: an answer's chunks kept in VRAM, a KV head and a layer run, 600 unless set; 0 keeps none (every token's chunks over the bus) (engine/src/poly/inkling2.cpp zha_load(), inkling2_zha.cu, docs/INKLING_ZHA.md)", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_IO_PF_AHEAD", "backends": [ diff --git a/terminal/data/zha/inkling-2-mini-refs-c64-r4.bin b/terminal/data/zha/inkling-2-mini-refs-c64-r4.bin new file mode 100644 index 0000000000..ac2f8c2e26 Binary files /dev/null and b/terminal/data/zha/inkling-2-mini-refs-c64-r4.bin differ