Skip to content

inkling2 : query chunking - #32883

Closed
sanmai wants to merge 3 commits into
masterfrom
sanmai/2026-query-chunking-inkling2
Closed

sanmai wants to merge 3 commits into
masterfrom
sanmai/2026-query-chunking-inkling2

Conversation

@sanmai

@sanmai sanmai commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Overview

The goal to reduce memory footprint of work buffers via sub-ubatch attention:

  • Store K/V for the whole ubatch, so each query sees the same cells it would see in one pass
  • Take the first N tokens of the batch, at 1 GB budget per tensor (so 1024 tokens at 262144 ctx for QFN)
  • Build the mask, indexer score, etc
  • Run the attention
  • Free that mask and reuse it for the next chunk
  • Join the outputs, and since chunks attend to the same kv cache the result is same*

*as results are different with different ub

This is an early working prototype, and I mostly focused on Inkling 2 Mini as far as benchmarking, and QFN as far benchmarking and correctness.

Additional information

This expands on #30815. Support for other models is in:

There's a knob to change the chunk size outside of heuristics.

LLAMA_GRAPH_CHUNK=32

All benchmarks are on 5090 with 128 GB DDR4.

Inkling 2 Mini

ctx build ub MoE cache MiB CUDA0 compute MiB warm pp t/s warm tg t/s MoE cache hit rate
10240 master 512 21050 7770 453.3 52.50 96.26%
10240 master 1024 19675 8993 724.6 50.29 94.81%
10240 master 2048 17075 11359 938.5 47.44 92.79%
10240 master 4096 11950 16134 947.0 37.75 83.77%
10240 master 8192 1625 25793 758.6 17.88 33.87%
10240 chunks 512 21050 7770 453.4 52.52 96.04%
10240 chunks 1024 19675 8993 720.5 49.79 94.96%
10240 chunks 2048 19825 8613 946.2 49.82 95.00%
10240 chunks 4096 15825 12220 953.3 44.13 90.61%
10240 chunks 8192 3200 24139 765.0 20.91 49.69%
32768 master 512 18550 9948 443.1 48.54 94.39%
32768 chunks 512 21400 7064 433.7 51.04 96.91%

mean of warm rounds; Inkling-2-Mini-UD-Q8_K_XL, KV q8_0/q5_1, -cmoe --moe-cache-cpu-share 0.95, 9193-token prompt, 1024 decoded

To compare with #32752: compute buffer, GB.

Dry run with llama-fit-params, -fa on, KV q8_0/q8_0, no -cmoe.

ctx ub 512: master -> chunks ub 1024: master -> chunks ub 2048: master -> chunks
32k 3.28 -> 0.77 6.56 -> 1.55 13.13 -> 3.08
90k 8.69 -> 0.96 17.39 -> 1.92 34.78 -> 3.78
200k 19.08 -> 1.90 38.17 -> 3.48 76.36 -> 5.01

Speed, t/s, best warm round. Context 10240, -cmoe, KV q8_0/q5_1, largest MoE cache for 5090.

test ub 512: master -> chunks ub 1024: master -> chunks ub 2048: master -> chunks ub 4096: master -> chunks ub 8192: master -> chunks
pp9193 459.8 -> 461.6 749.8 -> 724.6 936.1 -> invalid 970.3 -> 973.0 758.2 -> 763.6
tg1024 @ d9193 41.15 -> 40.64 42.42 -> 34.13 32.96 -> invalid 30.67 -> 33.43 16.06 -> 18.46

QFN benchmarks

See #32884 for the actual code.

build ub MoE cache MiB CUDA0 compute MiB warm pp best t/s warm tg best t/s decode hit rate
master 4096 17700 5046 1258.7 35.53 83.47%
master 8192 13400 9237 1960.6 32.32 75.73%
chunks 4096 19900 2841 1265.7 34.04 85.34%
chunks 8192 18500 4181 2000.3 35.70 83.75%

Requirements

@sanmai sanmai changed the title inkling2 :: query chunking inkling2 : query chunking Nov 23, 2026
@sanmai sanmai changed the title inkling2 : query chunking inkling2 : query chunking Nov 23, 2026

@pcuenca pcuenca left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I support this as a more general solution than #32752; we could merge for Inkling 2 and then potentially expand.

Tested locally on CUDA and macOS, results and comparisons with #32752 in this comment.

@github-actions github-actions Bot added model Model specific testing Everything test related labels Nov 23, 2026
Keep one chunk when the mask of a full ubatch is in the budget. Otherwise use at most ub/16 tokens per chunk, with a minimum of 32 tokens.
@sanmai

sanmai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@pcuenca I appreciate the feedback! I added a different heuristic that chooses smaller chunk size.

New

ctx ub 512: master -> chunks ub 1024: master -> chunks ub 2048: master -> chunks
32k 3.28 -> 0.77 6.56 -> 1.55 13.13 -> 3.08
90k 8.69 -> 0.96 17.39 -> 1.92 34.78 -> 3.78
200k 19.08 -> 1.90 38.17 -> 3.48 76.36 -> 5.01

Old

ctx ub 512: master -> chunks ub 1024: master -> chunks ub 2048: master -> chunks
32k 3.28 -> 2.28 6.56 -> 2.56 13.13 -> 3.13
90k 8.69 -> 2.44 17.39 -> 2.88 34.78 -> 3.78
200k 19.08 -> 2.72 38.17 -> 3.48 76.36 -> 5.01

abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 24, 2026
…ommit ebc0f11)

Merge --no-ff de la tête de la PR amont ouverte ggml-org#32883 (sanmai, 3
commits importés verbatim) : sub-ubatch attention chunks, Inkling 2 en
chunks, chunks plus petits à contexte court. Aucun conflit.

Décision du propriétaire (2026-11-24) sur mesure : banc r3 [bon] —
Inkling prod 1/49 → 49/49 couches GPU, tg 5,10 → 21,86 tok/s, prefill
+73 % (llama-mix-notes, mesures/2026.md). Arbre de code identique au
candidat mesuré (461b341e0) : build gate couverte.

MIX.md : couche inscrite au moment du merge, premier calque de type
« pr » avec sa règle de retrait ; AGENTS.md : forme de provenance pr:.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ggerganov

Copy link
Copy Markdown
Member

The drawback of this approach is that the graphs will have different number of nodes depending on the batch size. So far we've been trying hard to avoid this, but it seems to be getting unavoidable.

Do we have any alternative ideas?

How much does the chunking hurt the performance (not sure what is "decode hit rate" and how to read the data in the speed table)?

abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 24, 2026
Remplacé par le biais indexé GPU (couche suivante) : bancs r4 et r7 du
2026-11-24, prefill Inkling +61 % de plus que ggml-org#32883 et compute 774 au
lieu de 1051 MiB. Les fichiers mix/ sont conservés ; MIX.md marque la
couche RETIRÉE.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QBjnvFeJ4P7Jp71oC6pzWf
@ggerganov

Copy link
Copy Markdown
Member

After #32740 is adopted, this change will likely not be necessary anymore.

@sanmai

sanmai commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

#32740 is superior indeed.

The decode hit rate is for the MoE cache, so the most gain with this approach are from more experts in VRAM. I did consider other approaches such as index FA; I'll pull down the benchmarks later as #32740 does not apply to QFN but this approach and alternatives still does, to my best understanding.


Normalized to the same MoE cache size, any slowdowns or speedups from chunking are within noise.

model ub chunking MoE cache MiB warm pp t/s warm tg t/s
Inkling 512 no 18550 456.0 51.71
Inkling 512 yes 18550 455.3 50.99
Inkling 2048 no 17075 935.5 47.43
Inkling 2048 yes 17075 939.9 46.90
Inkling 4096 no 11950 940.0 38.49
Inkling 4096 yes 11950 944.0 39.17
Inkling 8192 no 1625 753.3 19.13
Inkling 8192 yes 1625 757.8 18.91
QFN 4096 no 17700 1250.3 41.90
QFN 4096 yes 17700 1230.9 40.08
QFN 8192 no 13400 1903.7 38.14
QFN 8192 yes 13400 1898.4 38.92

@sanmai

sanmai commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

I whipped up a compact bias prototype (4d5fc2a) and it blows the chunking out of the water at 200k context, not even mention short-context workloads.

build compute buffer graph nodes (pp) MoE cache warm pp t/s warm tg t/s
chunks 12471 19020 12500 372.0 33.94
prototype 11327 12322 13625 1058.8 39.51

That's for Inkling 2 Mini UD-Q8_K_XL with RTX 5090.

@sanmai sanmai closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants