Repository navigation
model : chunk the Inkling 2 relative-bias attention over query rows - #385
Open
danielhanchen wants to merge 5 commits into
Open
danielhanchen wants to merge 5 commits into
danielhanchen wants to merge 5 commits into
Conversation
Inkling 2 builds a dense relative-position bias of n_kv x n_ubatch x n_head elements per attention layer and adds it to the KQ mask. For Inkling-2-Small (48 heads) that passes 2^32 elements from about 175K tokens of context at the default ubatch of 512 (44K at -ub 2048), and the add aborts with GGML_ASSERT(s2 <= UINT32_MAX). The compute buffer also grows with it. When the bias of one call would exceed 2^29 elements for a full cache, attend in equal chunks of query rows (whole 64-row tiles, at most 64 chunks) and concatenate the outputs. The first chunk stores K and V, the distances and the F32 mask are copied to the device once per graph and each chunk views its rows. Below the budget the graph is unchanged.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
oobabooga
added a commit
to unslothai/unsloth
that referenced
this pull request
Nov 20, 2026
…llama.cpp prebuild (#18692) > **Note:** this targets the next llama.cpp prebuild, not the one shipped today (`b12572-mix-8eebad4`). It assumes the pin set on `unslothai/llama.cpp` master, which adds unslothai/llama.cpp#385 (chunked dense bias) and unslothai/llama.cpp#386 (banded flash attention), plus unslothai/llama.cpp#389 at `3b34f4f`. On today's build the dense rate from #18689 is the right one. ## Problem #18689 sized the Inkling 2 compute buffer for the dense relative-position bias (`512 * (8 * n_head + 4 + 2)` bytes per context token at ub=512). The next llama.cpp prebuild replaces that path in the common case: - #386 runs the relative bias through banded flash attention when flash attention is on, the cache is f16/bf16/f32, the head size is 64 or 128, there is one stream and the ubatch is above 32 tokens. Studio's default launch (flash attention, f16 cache, `--kv-unified`, ub=512) takes this path. - #385 chunks the dense bias over query rows once it passes 2^29 elements, for the configurations that still take the dense path. With the dense rate, Studio would overestimate the banded buffer by about 11x (77531 vs 7236 MiB at 800K) and pick a much shorter context than fits. ## Fix In `_compute_buffer_ctx_bytes`, the `inkling2` branch now: - takes the banded path when it applies: 18 bytes per cell and micro-batch token, plus a flat f32 `[ub, ub, n_head]` block; - keeps the dense rate for the rest (quantized cache, flash attention off, ubatch of 32 or less). With #385's chunking that rate is exact up to the first split (32K at ub=512 for 24 heads) and an upper bound after it. ## Verification CUDA0 compute buffer of llama-server from the shipped mix source plus #385, #386 and #389 at `3b34f4f`, on an RTX 6000 Ada, Inkling-2-Mini UD-Q4_K_XL, `--kv-unified`, compared with Studio's estimate: | n_ctx | ubatch | slots | cache | flash attn | measured (MiB) | estimate (MiB) | |---|---|---|---|---|---|---| | 65536 | 512 | 4 | f16 | on | 781 | 786 | | 262144 | 512 | 4 | f16 | on | 2509 | 2514 | | 800000 | 512 | 4 | f16 | on | 7236 | 7241 | | 65536 | 256 | 4 | f16 | on | 387 | 389 | | 65536 | 2048 | 4 | f16 | on | 3350 | 3428 | | 262144 | 2048 | 4 | f16 | on | 10236 | 10340 | | 65536 | 512 | 1 | f16 | on | 672 | 781 | | 65536 | 512 | 4 | q8_0 | on | 4574 | 6522 | | 131072 | 512 | 4 | q8_0 | on | 6175 | 12858 | | 262144 | 512 | 4 | q8_0 | on | 12191 | 25530 | | 32768 | 512 | 4 | f16 | off | 4936 | 4922 | | 65536 | 512 | 4 | f16 | off | 5159 | 9658 | | 131072 | 512 | 4 | f16 | off | 5543 | 19130 | On the banded path the estimate is 0.1% to 2.3% above the measurement with 4 slots, and 16% above with 1. The dense fallback stays at or above the measurement, except at 32K without flash attention, where it is 14 MiB (0.3%) below. Above that it is 1.4x to 3.5x high. It only applies to non-default configurations. Tests: `test_compute_buffer.py` checks the banded table (at or up to 20% above the measurement), the dense fallback staying above the chunked buffer, the two rates, which configurations take the dense path, and the layer-split delta on both paths. `test_compute_buffer.py` and `test_kv_cache_estimation.py` pass locally.
danielhanchen
changed the base branch from
base/upstream-8af68d486
to
base/upstream-29b52d398
November 21, 2026 11:30
burnoutIA
added a commit
to burnoutIA/harn
that referenced
this pull request
Nov 22, 2026
…e texte Moteur sur mesure (engine 'ink2') : - fiche inkling-2-mini-ink2 : checkpoint officiel NVFP4 + MXFP8, empreintes figées ; - installInk2 compile strata-ink2 porté en HIP (burnoutIA/moteurs, commit figé) dans la bulle, puis lance sa façade OpenAI (port 4755, venv tokenizers + jinja2) ; - banc avec et sans drafter, Radeon RDNA4 de 24 Go ou plus sous Linux ; - R9700 : 64,2 tok/s et QI 120 à 128k, contre 39,5 tok/s et QI 115 sous llama.cpp. llama.cpp : - required_pr accepte une liste, y compris des PR de forks (unslothai/llama.cpp#385) : Harn pose leurs diffs figés sur la dernière version ou sur master, puis compile ce mélange isolé, après accord ; - brouillon DSpark du dépôt essayé au banc (--spec-type draft-dspark) ; - cache KV des couches en boucle compté par passage. LoRA de texte sur un modèle installé : profil à part, la base reprise sans copie ; demande à accepter dans la fenêtre quand elle vient de pi. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The query-row chunk of the dense relative bias was balanced over the ubatch and then padded to whole 64-row tiles, so its height depended on n_tokens and could exceed the 2^29 element budget by half. A ubatch shorter than the one reserved at load could build a taller chunk than the reserved graph (Inkling 2 Mini at -c 131072 -ub 512: 340 rows give 192-row chunks, the reserved 512 rows 128-row ones), and the compute buffer grew at runtime. Size the chunk once per cache size: whole 64-row tiles within the budget, at least one tile, and at least n_tokens/64 rows so a layer has at most 64 chunks. The height no longer shrinks as n_tokens grows, so the graph reserved for a full cache and a full ubatch holds the largest chunk any later ubatch can build. When the budget covers a whole ubatch of whole tiles for a full cache (short contexts) the graph is unchanged.
The chunk outputs stayed live until a tree of concatenations at the end of the layer. Each one was allocated while the previous chunk's bias had just been freed, so it split the region the next bias needed and the graph allocator extended the buffer at almost every chunk: Inkling 2 Mini at -c 524288 -ctk q4_0 -ctv q4_0 -kvu -np 2 -ub 2048 reserved 87631 MiB for the 32 chunks of each full-attention layer. Allocate the layer output before the first chunk and copy each chunk's rows into it, so the chunks only hold transient tensors and reuse one region: 26705 MiB for the same graph. The values are the same; a ubatch that is not chunked builds the same graph as before.
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pin on upstream b12549 (
8af68d486a), one commit.Problem
Inkling 2 builds a dense relative-position bias of
n_kv x n_ubatch x n_headelements for every attention layer and adds it to the KQ mask. For Inkling-2-Small (48 heads) that passes 2^32 elements once the context reaches about 175K tokens at the default ubatch of 512 (about 44K at-ub 2048). The add then aborts withGGML_ASSERT(s2 <= UINT32_MAX)in the binary broadcast, and the compute buffer grows with the same product before that point.Change
src/models/inkling2.cpp: if the bias of one attention call would exceed 2^29 elements for a full cache, the query rows are split into equal chunks (whole 64-row tiles, at most 64 chunks). Each chunk gets its own bias and attention call, and the outputs are concatenated. The first chunk stores K and V; the later chunks read them from the cache.src/llama-graph.cpp/.h:build_attntakes the first mask row of the chunk (i_row, default 0). The distances and the F32 mask are copied to the device once per graph, and each chunk takes a view of them.src/llama-kv-cache.cpp/.h:llama_kv_cache_context::get_size(), so the chunking is sized for a full cache and stays the same while the cache fills.Below the budget the graph is the same as before.
Tests
test-llama-archs(CPU): 154/154.test-backend-opsCUDA0 vs CPU (B200, sm_100): FLASH_ATTN_EXT 4430/4430, MUL_MAT_ID 1060/1060, GET_ROWS 228/228, CONCAT 221/221, CPY 285/285, SET_ROWS 179/179.ggml-hip+llama, gfx90a and gfx1151): passes.-c 512 --chunks 8, experts on CPU,-fa on, compared with plain b12549:-b 2048: PPL 3.622135, the same as b12549 run against itself; mean KLD 0.000000, max KLD 0.000054 (the b12549 self-comparison floor), same top token 100%.-b 512: PPL 3.630998, the same as b12549; mean KLD 0.000000, same top token 100%.llama-perplexity -c 51200 -b 2048 -ub 2048 --chunks 1 -fa on(n_kv 51200, above the 43,690 limit at this ubatch):GGML_ASSERT(s2 <= UINT32_MAX)inbinbcast.cu, this branch completes;Update: the compute buffer now covers the dense relative-bias path
While testing #407 we found that Inkling 2 could grow its CUDA compute buffer far past the size reserved at load, which ends in an OOM in the middle of a generation. Two causes combined:
-kvu), a single-sequence ubatch whose cells are not one run, ubatches of 32 tokens or less, and the SWA layers once the positions pass the SWA cache and it wraps.ggml_cpyinto a tensor fromggml_new_tensor_4d. That tensor is a graph leaf, and the allocator keeps leaves for the whole graph, so the bias of every chunk and every dense layer stayed allocated at once and the chunking bounded nothing. The chunk outputs, kept until a final concatenation, also split the region the next chunk's bias reused.On this branch (#385):
On #386 (same code merged, plus the banded reserve):
ggml_castnode instead of a copy into a leaf.Results on a B200, Inkling-2-Mini UD-IQ1_S, CUDA0 compute buffer in MiB, reserved at load and largest at runtime. Each case prefills one sequence to 4352 tokens short of the context, then runs a ubatch split over two sequences (two-slot cases), a 32-token batch, a full ubatch and one token per sequence:
-kvu-kvu-kvu-kvuA single slot at Studio settings (f16 KV,
-np 1) reserves a little more at load:-ub 512-ub 2048The ggml-org#32804 case (
-c 172032 -kvu -npl 2 -npp 80000 -ub 2048) still completes. It reserved 3834 MiB and grew to 17285 MiB at runtime; it now reserves 10673 MiB and stays there.Output is unchanged: wikitext-2 PPL strings are identical to the base at
-c 512and-c 4096, with one sequence per batch and with several in a unified cache, mean KLD 0.000000 and the same top-1 agreement as the base against itself. The logits of a ubatch split over two sequences are bit-identical to the base at 131072 (one chunk) and 524288 (two chunks).test-backend-ops -o FLASH_ATTN_EXT_BANDEDpasses 71/71 on CUDA0, andtest-llama-archspasses for inkling2 and inkling2_loop.The extra reserve nodes are added only to the graph built at load. Every runtime graph has the same node count as before (11035 per decode token at
-ub 1, 10870 for a 512-token ubatch), and a counter on the reserve condition read 0 on every real ubatch: decode, 33 to 2048-token prefill, a ubatch over two sequences that share a position, and llama-server with two slots. llama-bench pp512 and tg128 are within noise of the base. Context shift and--cache-reuseare not available for this model (llama_memory_can_shiftis false), so they cannot reach the dense path.