Skip to content

[staging CI] unslothai/unsloth#13580 - #378

Closed
danielhanchen wants to merge 4 commits into
upstream-mainfrom
pr-13580-ci
Closed

danielhanchen wants to merge 4 commits into
upstream-mainfrom
pr-13580-ci

Conversation

@danielhanchen

Copy link
Copy Markdown
Collaborator

Disposable CI run for unslothai#13580. Do not merge; closed after CI.

@danielhanchen
danielhanchen deleted the pr-13580-ci branch October 13, 2026 11:08
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…ns (unslothai#18919)

## Summary

Fixes unslothai#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 #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:**
- #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.
- #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;
- #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.
- Old = the shipped Inkling KV pins (b12602 tree, #378 e65c374cb, #386
242a5d8f9).
- New = the #399 pin set (b12628 tree).
- Runtime compute is the CUDA0 compute buffer at exit.
- Estimate totals = weights file + Studio KV + Studio compute + the 320
MiB CUDA context reserve. They are not the planner's full budget, which
also keeps a 3% margin. The 150 to 270 MiB left between them and the
measured process peak is the CUDA context on this card.

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 #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 unslothai#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 unslothai#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 #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

- Real Metal, Vulkan and ROCm hardware. The dense / banded choice there
follows the llama.cpp sources and the #399 cross-platform runs.
- Real pre-Turing NVIDIA hardware. The f16 copy charge there follows the
llama.cpp source.
- A CPU-only build is priced like the shipped build, because no GPU
library carries the marker.
- Inkling 1 is unchanged. #399 only moves a quantized K with an f16 V
there.
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.

1 participant