Skip to content

Studio: size Inkling 2's dense relative-position bias in the GGUF compute buffer estimate - #18689

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
oobabooga:inkling2-compute-buffer
Nov 20, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
oobabooga:inkling2-compute-buffer

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Problem

Loading Inkling-2-Mini (unsloth/Inkling-2-Mini-GGUF, UD-Q4_K_XL) on a 48 GB RTX 6000 Ada fails every time. Studio picks an 800K context and starts llama-server with --fit off, and the server dies allocating its compute buffer:

llama_kv_cache:      CUDA0 KV buffer size = 25000.00 MiB
ggml_backend_cuda_buffer_type_alloc_buffer: allocating 77530.77 MiB on device 0: cudaMalloc failed: out of memory
llama_init_from_model: failed to initialize the context: failed to allocate compute pp buffers

The --fit on retry and the CPU-mmproj retries fail the same way (87676 MiB), and the load ends with "Couldn't allocate memory. Try lowering the context length".

Root cause

The GGUF architecture is inkling2. _compute_buffer_ctx_bytes has a branch for inkling, but not for inkling2, so the model got the generic rate of about 1 KiB per context token (the KQ mask). For the 800K launch Studio estimated a 928 MiB compute buffer, and llama.cpp needed 77531 MiB.

Inkling 2 does not have Inkling's banded flash-attention path. In src/models/inkling2.cpp (b12568) every layer gathers its relative-position bias as a dense f32 [n_kv, n_ubatch, n_head] tensor and permutes it into a second copy. build_attn then adds it to the mask before attention, using an i32 [n_kv, n_ubatch] distance index. That costs n_ubatch * (8 * n_head + 4) bytes per context cell, about 96 KiB at 24 heads and ub=512.

Two smaller gaps hid this:

  • attention.head_count is a per-layer array in Inkling 2 GGUFs, and only head_count_kv arrays were parsed, so _n_heads was None.
  • The ctx-independent estimate missed the block attention residuals: every block's output stays live, and each depth mix holds three (blocks + 1) * n_embd copies. It came out at 146 MiB against 187 MiB measured at ub=512.

Fix

  • Parse attention.head_count arrays into a new _n_heads_by_layer, and attn_res.block_size into _attn_res_block_size. _n_heads stays unset for array GGUFs, so other architectures keep their existing fallbacks.
  • Add an inkling2 branch to _compute_buffer_ctx_bytes: the KQ mask plus two f32 bias copies and the i32 index per cell, sized by the widest full-attention layer. Without flash attention it also adds the f32 scores, as the generic path does. The sliding layers' smaller bias reuses the same allocation, so it adds no extra term. There is no dequant term, because a q8_0 cache measured the same as f16.
  • _compute_activation_width adds the live residual blocks and the depth-mix copies when attn_res.block_size is present.

Verification

Measured the CUDA0 compute buffer from llama-server (bundled b12568, 4 slots, --kv-unified) on an RTX 6000 Ada, compared with Studio's estimate (flat + context terms):

n_ctx ubatch cache flash attn measured (MiB) before (MiB) after (MiB)
65536 512 f16 on 6523 210 6522
131072 512 f16 on 12859 274 12858
262144 512 f16 on 25531 402 25530
800000 512 f16 on 77531 928 77530
65536 256 f16 on 3261 107 3263
65536 1024 f16 on 13049 419 13042
65536 512 q8_0 on 6517 466 6522
32768 512 f16 off 4936 2514 4922
65536 512 f16 off 9672 4882 9658

With experts on CPU (-ot exps=CPU) or a partial offload (-ngl 30), the per-token slope is unchanged (101376 B/token at ub=512). Only a ctx-independent term is added.

End to end, loading the same model on the same machine with no context set, Studio now picks -c 192512 with --fit off and loads on the first attempt. The real compute buffer was 18790 MiB against an estimate of 18798 MiB, GPU 0 ended at 46.9 of 49.1 GB, and a chat completion returned normally.

Tests: test_compute_buffer.py checks the measured table above (within 1%), the per-token rate, the head choice from full-attention layers, the layer-split delta and the residual-block width. test_kv_cache_estimation.py checks parsing of the per-layer head_count and attn_res.block_size. The compute-buffer, KV-estimate and fit suites pass locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T21:42:26.028409Z 0fe3c9d 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 0fe3c9dba6

ℹ️ 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 f0ceab7 into unslothai:main Nov 20, 2026
oobabooga added a commit 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 22, 2026
…r reads as loaded weights (unslothai#18725)

## Problem

When a GGUF load uses llama-server's `--fit on`, the load toast shows
weights loading at 15 to 50 MB/s with a 10 to 15 minute ETA while no
weights are being read. On a load that cannot fit (Inkling-2-Mini at
800K context, unslothai#18689), every retry repeats this, so the failure looks
like slow storage.

![Load progress during the --fit search and the weight load, before and
after](https://raw.githubusercontent.com/oobabooga/unsloth/pr-media/pr/studio-fit-search-progress/fit-phase-moments-1cb2e1e143a9.png)

## Root cause

`LlamaCppBackend.load_progress` reports `max(RssFile, rchar)` of the
llama-server process as bytes loaded. Before loading any tensor, `--fit`
tries dozens of layer placements and re-reads the GGUF header for each
one. `rchar` counts those reads (about 0.45 GB over 20 s for
Inkling-2-Mini), and the frontend turns them into a rate and an ETA.

## Fix

- `_drain_stdout` follows the load stages in llama-server's log: the
`--fit` search, from its first line to its `common_fit_params:` result
line (`llama_params_fit:` on b8864 and older), then the first
`load_tensors: loading model tensors` after it, where `rchar` and
`RssFile` are recorded as a baseline. At verbosity 5 each probe prints
its own `load_tensors` line, so that line only counts outside a search.
- `load_progress` returns `phase: "fit"` with 0 bytes during the search,
and measures both counters from the baseline after it.
- This needs trace verbosity (4), which Studio sets by default. At lower
verbosity the log does not show both ends of the search, so progress is
reported as before.
- The load toast and the inline status show "Finding a GPU layer split
that fits in memory." without a bar, rate or ETA. The inline status now
shows any label that has no percent, so the unknown-size download label
("N GB downloaded") also appears there. The MCP load poller reports the
fit phase instead of the finished download.

## Verification

Inkling-2-Mini UD-Q4_K_XL (18.3 GB), RTX 6000 Ada + RTX 3090, 800K
context, `/api/inference/load-progress` polled every 0.25 s on main and
on this branch. Each load made 6 llama-server attempts, 4 of them with
`--fit on`:

| `--fit` attempt | main: bar during the search | this branch |
|---|---|---|
| 1 | 0.14 to 0.47 GB at 15.0 MB/s over 21.5 s | `phase: "fit"`, 0
bytes, 18.7 s |
| 2 | 0.15 to 0.43 GB at 16.8 MB/s over 16.8 s | `phase: "fit"`, 0
bytes, 20.5 s |
| 3 | 0.08 to 0.50 GB at 22.6 MB/s over 18.4 s | `phase: "fit"`, 0
bytes, 24.9 s |
| 4 | 0.05 to 0.98 GB at 50.3 MB/s over 18.4 s | `phase: "fit"`, 0
bytes, 50.9 s |

After each search, the branch counted the weights up to 17.5 to 17.6 GB.
The same load at `--verbosity 5` (35 probe `load_tensors` lines in one
search) also stayed in `fit` until the real load, and at `--verbosity 2`
it reported as on main.

Unit tests cover the stage parser, including the verbosity 2 and 5 log
sequences and the b8864 markers, and the MCP poller.


## Before / after in the shipped UI

![Load toast during the --fit search and the weight load, main vs this
PR](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/18725/composite_18725-cf3240c6e139.png)

Both trees' frontends, load started from the chat picker with a cached
Qwen3-4B Q4_K_M GGUF. `/api/inference/load-progress` answers what each
tree's own backend returned when a real llama-server trace (Colab L4,
`--fit on --fit-target 20000 -lv 4`, prebuilt b12588) was replayed
through its `_drain_stdout` and `load_progress`, slowed 4x. On main the
search shows 0.2 GB loaded with a rate and ETA (mapped CUDA libraries in
RssFile plus about 30 MB of header re-reads across 5 probes); on this
branch it shows the placement search, then counts the weights from the
first real read.

---------

Co-authored-by: Daniel Han <[email protected]>
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.

2 participants