Repository navigation
Studio: size Inkling 2's dense relative-position bias in the GGUF compute buffer estimate - #18689
Merged
Merged
Conversation
…pute buffer estimate
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. |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
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.
This was referenced Nov 21, 2026
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.  ## 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  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]>
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.
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:The
--fit onretry 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_byteshas a branch forinkling, but not forinkling2, 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_attnthen adds it to the mask before attention, using an i32[n_kv, n_ubatch]distance index. That costsn_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_countis a per-layer array in Inkling 2 GGUFs, and onlyhead_count_kvarrays were parsed, so_n_headswas None.(blocks + 1) * n_embdcopies. It came out at 146 MiB against 187 MiB measured at ub=512.Fix
attention.head_countarrays into a new_n_heads_by_layer, andattn_res.block_sizeinto_attn_res_block_size._n_headsstays unset for array GGUFs, so other architectures keep their existing fallbacks.inkling2branch 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_widthadds the live residual blocks and the depth-mix copies whenattn_res.block_sizeis 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):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 192512with--fit offand 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.pychecks 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.pychecks parsing of the per-layerhead_countandattn_res.block_size. The compute-buffer, KV-estimate and fit suites pass locally.