Skip to content

Studio: price Inkling 2 quantized KV by what the bundled llama.cpp runs - #18919

Merged
danielhanchen merged 5 commits into
mainfrom
studio-inkling2-quant-kv-pricing
Nov 22, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-inkling2-quant-kv-pricing

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #18833.

Studio's Inkling 2 memory estimate sent every quantized KV cache to the dense relative-bias fallback, which costs about 99 KiB per context token at ubatch 512. That made q8_0 and q4_0 look far larger than f16. The llama.cpp build Studio ships today really does fall back to dense, but unslothai/llama.cpp#399 (#378 / #386 repinned) runs q8_0 / q4_0 K/V on the banded path and shares one banded mask per graph. This PR prices each cache type by what the installed llama.cpp does.

Changes:

  1. Build probe. LlamaCppBackend.inkling2_banded_support(binary) reports the ggml GPU backend and whether the build runs quantized K/V on the banded path. A How to merge lora adapter with pretrained weights? #399 build is recognised by the q8_0 instantiation of the banded kernel (flash_attn_ext_banded...ggml_type8E) in libggml-cuda or libggml-hip. That is the kernel registration string, so it survives strip and has the same name on every OS. The probe runs at load for Inkling 2 only. The estimate routes resolve it lazily against the installed build, and the result is cached per (path, mtime, size).
  2. Estimate per backend and build:
    • How to merge lora adapter with pretrained weights? #399 build on CUDA: q8_0 / q4_0 price like f16 (the kernel dequantizes in the tile) when every visible card is Turing or newer. The in-tile path is the banded MMA kernel, gated on turing_mma_available (sm_75, by the highest arch the bundle compiled for the card). Pascal / Volta cards and unknown compute capability are charged the f16 K/V copy, about 1 GiB at 256K on Inkling-2-Mini.
    • How to merge lora adapter with pretrained weights? #399 build on HIP, and for q4_1 / q5_0 / q5_1: banded plus the f16 K/V copy the kernel reads.
    • iq4_nl has no f16 converter, so it stays dense.
    • Metal, Vulkan and SYCL have no banded kernel, so every cache type, f16 included, takes the dense estimate.
    • Old builds and unknown builds keep today's dense estimate for quantized caches.
  3. Runtime regrowth. After the first micro-batches the banded compute buffer grows past the reserve llama.cpp sizes at load, on both builds. The estimate is now the larger of the reserve and the regrown size:
    • old builds: reserve 18 bytes per cell per ub token; regrown 15 bytes per cell plus 800 KiB per ub token;
    • How to merge lora adapter with pretrained weights? #399 builds: reserve 4 bytes per cell per ub token (the shared mask sized at load); regrown 2 bytes per cell plus 1200 KiB per ub token. The reserve leads above about 600K cells.

Measurements

Setup: Inkling-2-Mini UD-IQ1_S, B200, -np 4 --kv-unified -fa on -b 2048 --fit off, slot 0 filled to 0.9 n_ctx.

ubatch 1024 (Studio's micro-batch for an all-GPU Inkling 2 load):

build context KV measured peak MiB runtime compute MiB estimate compute, main / PR estimate total, main / PR
new 32K f16 11432 1594 1042 / 1730 10590 / 11278
new 32K q8_0 10582 1594 6706 / 1730 15399 / 10423
new 32K q4_0 10126 1594 6706 / 1730 14943 / 9967
new 128K f16 14676 1768 2770 / 1922 15390 / 14542
new 128K q8_0 12388 1768 25714 / 1922 36039 / 12247
new 128K q4_0 11140 1768 25714 / 1922 34815 / 11023
new 256K f16 19044 2052 5074 / 2178 21790 / 18894
new 256K q8_0 14848 2052 51058 / 2178 63559 / 14679
new 256K q4_0 12600 2052 51058 / 2178 61311 / 12431
old 32K f16 11566 1722 1042 / 1746 10590 / 11294
old 32K q8_0 13588 dense 6706 / 6706 15399 / 15399
old 128K f16 16006 3093 2770 / 3186 15390 / 15806
old 128K q8_0 20022 dense 25714 / 25714 36039 / 36039
old 256K f16 22064 reserve 5074 / 5106 21790 / 21822
old 256K q8_0 30498 dense 51058 / 51058 63559 / 63559

Long context on the #399 build. Slot 0 was filled to only 0.125 n_ctx, so these are the load-time reserves:

ubatch context KV measured peak MiB reserve compute MiB estimate compute, PR
512 512K f16 26172 1078 1322
512 1M f16 43580 2102 2258
1024 1M f16 45784 4206 4562

The 2 bytes per cell rate alone would price the ubatch 512 1M row at 1831 MiB, below the 2102 MiB llama.cpp reserves.

ubatch 512:

build context KV measured peak MiB runtime compute MiB estimate compute, main / PR
new 32K f16 10490 748 498 / 842
new 32K q8_0 9678 748 3354 / 842
new 128K f16 13646 837 1362 / 938
new 128K q8_0 11394 837 12858 / 938
old 32K f16 10560 812 498 / 850
old 128K f16 14278 1497 1362 / 1570

What the tables show:

  • On a How to merge lora adapter with pretrained weights? #399 build q8_0 / q4_0 are now below f16 at every context, as measured.
  • On the shipped build the q8_0 estimate is unchanged. It stays above f16 because that build really allocates the dense bias.
  • main under-priced the f16 runtime compute by up to 680 MiB at 32K. The PR estimate is at or above the measured runtime buffer in every row.

Auto context that Studio's own planner picks for the #18757 case (24122 MiB card, 23857 MiB free, UD-IQ4_XS + mmproj, 4 slots, ngram-mod), main -> PR:

binary f16 q8_0
shipped pins (b12602) 162048 -> 157952 60416 -> 60416
#399 (b12628) 162048 -> 182784 60416 -> 354304

For the #18757 case: at Studio's argv with the free memory capped to 23857 MiB (CUDA MPS limit), the #399 build at Auto 184320 filled slot 0 to 171.7K tokens with no OOM. Runtime compute was 990 MiB, against a PR estimate of about 1040 MiB.

Tests

  • New studio/backend/tests/test_inkling2_banded_quant_kv.py, 60 tests. It covers:
    • old and unknown builds keep the dense q8_0 / q4_0 estimate;
    • on a How to merge lora adapter with pretrained weights? #399 CUDA build q8_0 / q4_0 total below f16 at 32K / 128K / 256K;
    • Metal / Vulkan / SYCL price dense;
    • the HIP f16 copy, the types without an in-tile dequant, and iq4_nl;
    • the runtime buffer table above;
    • the long-context reserve rows above;
    • the in-tile dequant gate by compute capability (Turing+, Pascal, Volta, mixed cards, unknown, a PTX-only bundle);
    • the library probe (new / old CUDA, HIP, Vulkan, Metal, static build, missing binary);
    • lazy resolution of the installed build.
  • test_compute_buffer.py: the existing Inkling 2 banded table is the load-time reserve, so its upper bound now allows the regrowth term.
  • test_inkling2_banded_quant_kv.py, test_compute_buffer.py, test_llama_cpp_gpu_oom_respawn.py and test_cuda_sm_gate.py on this branch: 402 passed. One teardown error in test_mixed_cards_keep_their_own_margins_plus_the_headroom (a CUDA device probe in the fixture on this host) happens on main as well.

Not covered

The Inkling 2 compute estimate sent every quantized KV cache to the dense
relative-bias fallback (about 99 KiB per token at ub 512), so q8_0 and q4_0
showed far more memory than f16 (#18833). That is what the shipped prebuilt
does, but unslothai/llama.cpp#399 runs q8_0 / q4_0 K/V on the banded path and
shares one banded mask per graph.

Read the build instead of the KV type alone: the q8_0 instantiation of the
banded kernel in libggml-cuda / libggml-hip marks a #399 build. On such a build
a quantized cache prices like f16 on CUDA (in-tile dequant) and adds an f16
K/V copy on HIP and for the types without a tile path; iq4_nl stays dense.
Metal, Vulkan and SYCL have no banded kernel, so they take the dense bias for
every cache type. Old or unknown builds keep the dense quantized estimate.

The banded compute buffer also regrows past the load-time reserve once the
prompt runs (Inkling-2-Mini, ub 1024, 4 slots: 1117 -> 1722 MiB at 32K on the
shipped pins, 513 -> 1594 MiB on #399). Price the regrown buffer: on old
builds the larger of the reserve and the regrown size, on #399 builds the
shared-mask rate (2 bytes per cell per ub token) plus the regrowth.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T17:00:56.359363Z c102bef Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e78153a6a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Comment on lines +19188 to +19191
if quantized and not (
build.backend == "cuda" and kv_type in self._INKLING2_BANDED_TILE_TYPES
):
per_tok += self._attention_dequant_width() * 2

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate in-tile dequantization pricing on GPU capability

On pre-Turing NVIDIA GPUs running a #399 build, q8_0/q4_0 attention still reads an f16 copy of K/V, but this condition exempts every CUDA GPU from charging that copy. Detecting the quantized kernel in the library establishes build support, not whether the selected device can execute the in-tile path. For Inkling-2-Mini this undercounts scratch by 4 KiB per context token (1 GiB at 256K), so the Auto planner can select a context that exceeds available VRAM. Check the selected device's compute capability before omitting the copy, and retain the copy charge when capability is unknown.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in c102bef: the in-tile q8_0 / q4_0 dequant is the banded MMA kernel, gated on turing_mma_available (sm_75), so the estimate now charges the f16 K/V copy unless every visible CUDA card is known to be Turing or newer (by the arch the bundle compiled for it); Pascal, Volta and unknown compute capability pay the copy, with regression tests.

On #399 builds the load-time reserve of the shared banded mask grows 4 bytes
per cell per ub token and overtakes the regrown buffer above about 600K cells
(Inkling-2-Mini, 4 slots: 1078 / 2102 MiB at 512K / 1M with ub 512, 4206 MiB
at 1M with ub 1024). The 2 bytes per cell rate alone priced 1M at 1831 MiB.
Take the larger of the reserve and the regrown size, as for older builds.
The banded kernel only dequantizes q8_0 / q4_0 tiles on its MMA path, which
needs turing_mma_available (sm_75, by the highest arch the bundle compiled for
the card). Pascal and Volta take the scalar kernel and read an f16 copy of K/V,
about 1 GiB at 256K on Inkling-2-Mini, so the estimate now charges it unless
every visible card is known to be Turing or newer.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: c102befdd2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit cd318e3 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-inkling2-quant-kv-pricing branch November 22, 2026 19:34
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…growth

On builds before #399, #18919's 18 B/cell load-time reserve rate is a planner
bound that overtakes its 15 B/cell runtime rate at long contexts, as the
measured reserve does (growth 270 -> 140 MiB from 32K to 128K). The --fit on
margin read that surplus as post-prompt growth, so it rose again past ~262K
cells (4332 MiB at 1M, -ub 2048) and a Manual + Auto launch with no -c kept
that maximum free whatever context the fitter picked. Price only the runtime
term against the reserve bound there; every measured row keeps its value.
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 22, 2026
…i#18941)

## Summary

Follow-up to unslothai#18919. When Studio hands an Inkling 2 load to llama.cpp's
fitter (`--fit on`), the fitter measures only the load-time compute
reserve and leaves `--fit-target` free on each device (1024 MiB by
default). The Inkling 2 banded compute buffer grows past that reserve
after the first prompt. At the `-ub 2048` raise Studio applies when MoE
experts sit in RAM, the measured growth is 1.5 to 2.5 GB, more than the
margin, so a session can OOM mid-prompt after a clean load.

This adds the expected growth to the `--fit-target` margin, for Inkling
2 `--fit on` launches only:

- Growth = runtime estimate minus a calibrated lower bound of
llama.cpp's load-time reserve, at the launched micro-batch, K/V type and
context.
- The runtime side is unslothai#18919's banded estimate, unchanged, with a floor
for the margin only, because past about 262K cells at `-ub 2048` the
estimate reads under the measured runtime (524K: 5572 vs 5836 MiB).
- The reserve bound is fitted under every Inkling-2-Mini reading we have
on both builds, for ub 512 to 2048 and 32K to 1M cells. On unslothai#399 it is
the larger of an activation floor and the 4 B per cell shared mask. On
older builds it is the activations plus 16 B per cell.
- Both sides use Mini's activation estimate as a fixed scale, so the
growth follows the micro-batch and context the way unslothai#18919's runtime term
does, not a larger model's activation size.
- With no `-c` (Manual + Auto), the fitter picks the context, so the
largest growth from the `--fit-ctx` floor up to the native length is
used.
- It is added on top of the margin Studio already emits: the budget
slider delta, the small-card trim, the per-device list and the
mid-session OOM headroom. A user's own `--fit-target` still wins.
- It is 0 where there is no banded path: Metal, Vulkan, SYCL, pre-unslothai#399
quantized K/V, or no flash attention.
- Non-Inkling 2 models, including Inkling 1, never reach the new code.

## Measured growth vs emitted

Inkling-2-Mini UD-IQ1_S, CUDA, `-np 4 --kv-unified -fa on`, slot 0
filled to 90% of the context. The compute buffer is read from the load
log and from `memory_breakdown_print` at exit. The ub 2048 rows and the
32K ub 1024 row are `--fit on` launches on the unslothai#399 build b12628; the
other rows are earlier `--fit off` readings. The 32K ub 1024 row
reproduces the earlier `--fit off` reading (1080 vs 1081 MiB).

| build | ub | ctx | K/V | reserve at load | after prompt | measured
growth | emitted growth | pad |
|---|---|---|---|---|---|---|---|---|
| unslothai#399 | 512 | 32K | f16 / q8_0 | 258 | 747 | 489 | 617 | +128 |
| unslothai#399 | 512 | 128K | f16 | 382 | 837 | 455 | 689 | +234 |
| unslothai#399 | 512 | 128K | q8_0 | 541 | 837 | 296 | 689 | +393 |
| unslothai#399 | 512 | 512K | f16 | 1078 | 1077 | 0 | 421 | +422 |
| unslothai#399 | 512 | 1M | f16 | 2102 | 2101 | 0 | 173 | +174 |
| unslothai#399 | 1024 | 32K | f16 | 514 | 1594 | 1080 | 1249 | +169 |
| unslothai#399 | 1024 | 1M | f16 | 4206 | 4205 | 0 | 393 | +394 |
| unslothai#399 | 2048 | 32K | f16 / q8_0 | 1035 | 3582 | 2547 | 2690 | +143 |
| unslothai#399 | 2048 | 128K | f16 / q8_0 | 1443 | 3925 | 2482 | 2866 | +384 |
| unslothai#399 | 2048 | 256K | f16 | 2275 | 4492 | 2217 | 2397 | +180 |
| unslothai#399 | 2048 | 512K | f16 | 4323 | 5836 | 1513 | 1681 | +168 |
| b12602 | 512 | 32K | f16 | 541 | 811 | 270 | 379 | +109 |
| b12602 | 512 | 128K | f16 | 1357 | 1497 | 140 | 323 | +183 |

All values are MiB. The emitted growth is added on top of the existing
margin (1024 MiB by default), so the real headroom is larger than the
pad shown.

## Inkling-2-Small launch matrix

432 simulated launches (sparse real-header GGUFs, 24 / 48 / 80 / 96 /
180 GB and 2x80 GB cards, auto and fixed context, f16 and q8_0, shipped
b12602 and unslothai#399 builds), unslothai#18919 head versus this branch:

- 421 launches go `--fit on`. 315 now carry a larger `--fit-target`. The
other 106 are q8_0 on the shipped build, which runs dense attention and
has no banded growth.
- unslothai#399 build at `-ub 2048`, f16 and q8_0: +2978 to +3250 MiB (margin
4002 to 4274 MiB), 159 launches.
- unslothai#399 build at the default micro-batch: +601 to +689 MiB (margin 1337
to 1713 MiB), 51 launches.
- Shipped build, f16 at `-ub 2048`: +1964 to +2204 MiB, 78 launches.
- Shipped build, f16 at the default micro-batch: +347 to +540 MiB, 27
launches.
- Every launch still loads. Outside `--fit-target`, all 432 argv are
identical.

## Non-Inkling models

384 launches of qwen3moe, llama, inkling, gemma3, deepseek4 and glm4moe
(8 / 24 / 48 GB and mixed cards, 1 to 80 GiB weights, f16 and q8_0, GPU
and Manual + Auto, 276 of them `--fit on`) produce byte-identical argv
on unslothai#18919 head and on this branch.

## Tests

`studio/backend/tests/test_inkling2_fit_target_regrowth.py`:

- for every measured row, emitted growth >= measured growth + 100 MiB,
and the reserve bound <= the measured reserve;
- unslothai#18919's runtime estimate is unchanged;
- the growth does not scale with a larger model's activations;
- an unknown context takes the largest growth;
- no growth without a banded path;
- the launched `--fit-target` with the default margin, the small-card
trim and the per-device list;
- non-Inkling 2 argv is unchanged even with the growth forced to 4096
MiB.

On unslothai#18919 head, 5 of the 49 tests pass (the unchanged cases) and the
rest fail. The neighbouring fit, placement, compute buffer, OOM respawn,
sidecar and Inkling suites give the same results, with the same failing
test IDs, as unslothai#18919 head.

## Not covered

- No real Inkling-2-Small `--fit on` run. The Small numbers come from
the simulated matrix, with the growth calibrated on Inkling-2-Mini.
- Pre-unslothai#399 readings exist only at ub 512.
- unslothai#18919's own runtime estimate reads 264 MiB under the measurement at
524K `-ub 2048`. That affects its `--fit off` decisions and is left to
unslothai#18919.
- A launch that only reaches `--fit on` through a later retry keeps the
margin of its first attempt.

## Test fix

`test_mixed_cards_keep_their_own_margins_plus_the_headroom` (from
unslothai#18920) reached `torch.cuda` through the datacenter and integrated-GPU
probes on a host with visible GPUs, so the CPU-test guard failed it at
teardown. It now fakes the device info the way
`test_auto_fit_target_trim.py` does. On CI runners with no GPU it
already passed.

## Review fixes

- On the currently shipped llama.cpp build (pre-unslothai#399 banded path), the
margin counted unslothai#18919's upper-bound load reserve as post-prompt growth
past about 262K cells, which kept up to 4.7 GB free that the
measurements show is never used (for example 5228 MiB on 12 and 16 GB
cards at 1M context). Growth on that build is now priced from the
runtime term only; every measured row keeps its value.
- The budget delta is floored first (the same 512 MiB floor the emitter
applies), then the regrowth is added on top, for the single margin and
every per-device entry, so a low budget slider can no longer eat the
reserve.
- The mixed-card respawn test also stays off `torch.cuda` when run
alone.

A base vs head launch matrix (516 pairs: 8 to 24 GB cards, 2 x 24 GB and
24 + 8 GB, sparse header-only GGUFs) loads on both sides, and nothing
differs outside `--fit-target`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Inkling kv quant calculation is odd

1 participant