Repository navigation
[staging CI] unslothai/unsloth#13565 - #386
Closed
danielhanchen wants to merge 14 commits into
Closed
danielhanchen wants to merge 14 commits into
danielhanchen wants to merge 14 commits into
Conversation
MLX repos such as orcarouter/Qwen3.8-27B-Uncensored-MLX keep each precision in its own folder (2-bit/, 4-bit/, 6-bit/, 8-bit/). Studio only ever loaded the root copy and gave no way to pick another precision. - List those folders through the existing variants endpoint, tagged variant_format=mlx, with sizes, on-device state and the folder the root duplicates as the default. GGUF listings are unchanged: the MLX lookup only runs when a repo has no GGUF rows. - Carry the picked folder in gguf_variant, fetch only <folder>/* and load it through MLX from disk while the model keeps the repo id as its name. - The chat picker expands such repos into their folders the way it expands a GGUF repo, and names the loaded one MLX instead of GGUF. - A download of the repo root skips the other precision folders (94.7 GB to 16 GB for the repo in the issue), mirrored in the Hub size estimate. Fixes unslothai#13418
So reverting the change fails the tests on behaviour, not on a missing name.
Q4_K_M matched the q<N> alternative, so a stale GGUF variant on a safetensors repo asked the Hub for a config that cannot exist. Also let the Apple Silicon load test reach the Hub for its tiny checkpoint, and keep the duplicate-format gate on one line for the frontend parity guard.
With only 6-bit/ in the cache and the Hub unreachable, the GGUF guard saw a variant and no root config.json and refused the load as an unreadable GGUF repo. A cached <folder>/config.json now counts as a checkpoint, so the folder loads from disk. The Apple Silicon test lays the repo out the same way.
The chat picker only searches Unsloth repos, so a repo such as orcarouter/Qwen3.8-27B-Uncensored-MLX is reached from the Hub page. Its page now lists the precision folders (sizes, on-device state, the one the root duplicates) above the repo card, and Run hands the picked folder to chat, where the load fetches only that folder, the way a hub pick in the picker does. Repos without such folders render exactly as before.
It was recorded as a GGUF load because it carries a variant, which sent a reload and the startup auto-load through the llama.cpp path. A model record now keeps the folder, so Reload brings back the same precision.
An uncached pick from the chat picker or the Hub is staged by the download manager before it loads, and its worker only planned GGUF variants, so a 6-bit/ pick exited with no GGUF shards to fetch. A precision folder is now a plan of its own for the worker, the size probe and the progress reads. Also refuse a precision folder whose config could not be read for any reason other than the Hub saying it does not exist, instead of loading the root, which is another precision; retry a public folder download anonymously when the stored token is rejected; and hide the GGUF update and delete actions on MLX rows, which would act on GGUF files only.
…n load as active Two folders of one architecture and dtype can match in size alone, so the folder marked as the root's copy (and picked by default) is now the one whose weight hashes match the root's. The Hub page treated any loaded model with a variant as GGUF, so a loaded MLX precision folder read as inactive and kept its delete action enabled.
…under a root checkpoint
danielhanchen
pushed a commit
that referenced
this pull request
Nov 20, 2026
…llama.cpp prebuild (unslothai#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 unslothai#18689 is the right one. ## Problem unslothai#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
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.
danielhanchen
added a commit
that referenced
this pull request
Nov 24, 2026
… bytes (builds on unslothai#19148) (unslothai#19249) Builds on unslothai#19148 (includes its commits); merging this merges unslothai#19148. ## Problem unslothai#19148 prices the large compute reserve that unslothai/llama.cpp#386 added in `b12649-mix`. The next llama.cpp prebuild may ship the fix to that reserve. In the fixed build, several slots run the banded kernel over cell positions, and short ubatches run banded over the padded cache. The reserve then drops to almost nothing: | Inkling-2-Mini, 1 slot, 1M context, ub 512 | `b12649-mix` | fixed build | |---|---|---| | f16 K/V | 5866 MiB | 1248 MiB | | q8_0 K/V | 9962 MiB | 1248 MiB | With unslothai#19148 alone, Studio would keep pricing the fixed build with the `b12649-mix` reserve. On the measured points that overestimates the compute buffer by 0.2 to 55 GB, so Studio would place fewer layers on the GPU than fit. This PR prices the fixed build separately, which makes unslothai#19148 safe to merge. ## Fix - Detect the fixed build by two graph input names it adds to `libllama`: `attn_inp_k_pos_swa` (several slots, banded over cell positions) and `attn_inp_rel_shift_swa` (short ubatches over the padded cache). No earlier build has either name. Both survive `strip`. Both must be present. - Price its reserve as the f16 KQ mask over the padded cache (2 bytes per cell per ubatch token, twice with `--swa-full`) plus 160 KiB per ubatch token, for any slot count and K/V type. A layer split adds its mask copies as before. - Keep only the CUDA pool margin (256 KiB per ubatch token) in `--fit-target`, as unslothai#19148 does for `b12649-mix`. The markers identify the build even when its build number cannot be read. - `b12649-mix` and the merge-only rebuild of #386 keep unslothai#19148's pricing. Older builds keep the pricing on `main`. - Split a `--fit off` multi-GPU load by bytes when llama.cpp's split by layer count would overflow a card. Unsloth Dynamic quants keep some layers at higher precision: the last four layers of Inkling-2-Mini UD-Q8_K_XL are about 2 GB each, against 0.6 GB for the rest, so a split by count can put several GB more on the smaller card than it holds. Studio now emits a `--tensor-split` whose layer runs fit each card. When no split fits, it lets `--fit on` place the model with the slots the user asked for, not a count cut only to fit every layer on the GPUs. A load whose count split already fits keeps llama.cpp's own split. Which pricing each build gets (`grep -c` of each string in the built `libllama`): | Build | `inkling2_loop: %zu cache slots exceed` | `attn_inp_k_pos_swa` | `attn_inp_rel_shift_swa` | Pricing | |---|---|---|---|---| | `b12631-mix` | 1 | 0 | 0 | as on `main` (build below 12649) | | `b12649-mix` | 1 | 0 | 0 | unslothai#19148 | | #386 merge-only rebuild | 1 | 0 | 0 | unslothai#19148 | | fixed build (#386 plus the banded-position fix) | 1 | 1 | 1 | this PR | ## Accuracy Compute buffer at load from `llama-server`, Inkling-2-Mini, flash attention, B200, on the fixed build. The table is a sample of 116 points: 1 to 64 slots, ub 256 to 2048, f16 / bf16 / q8_0 / q4_0 / q4_1 / q5_0 / q5_1 / mixed K/V, 4K to 1M context, with and without `--swa-full`, and with a DSpark drafter. | Setup | Measured (MiB) | This PR (MiB) | unslothai#19148 (MiB) | |---|---|---|---| | 4 slots, 32K, ub 512 | 256 | 298 | 3050 | | 4 slots, 256K, ub 512, q5_1 | 480 | 522 | 6506 | | 4 slots, 1M, ub 2048, q8_0 | 4999 | 5156 | 60388 | | 1 slot, 512K, ub 512 | 734 | 773 | 3429 | | 1 slot, 1M, ub 512, q4_0, DSpark | 1248 | 1285 | 10597 | | 1 slot, 415,488, ub 2048, q4_0, `--swa-full` | 4124 | 4292 | 10932 | | 4 slots, 274,688, ub 512, q4_0, `--swa-full` | 758 | 803 | 18577 | | 64 slots, 64K, ub 512 | 352 | 440 | 9496 | Across all 116 points the estimate is 1.01x to 1.25x the measurement (+14 to +184 MiB) and never under it. The highest ratio is 64 slots at 64K, where the flat estimate's output rows lead. After two concurrent 13.5K-token prompts at ub 2048 with 4 slots, the compute buffer grew by 120 MiB past the load reserve. The 512 MiB pool margin in `--fit-target` covers that. ## unslothai#19137 on the fixed build The unslothai#19137 setup with Inkling-2-Mini UD-Q8_K_XL: 2 GPUs, 32K context, 4 slots requested, the Windows reserve floor. Studio's planner ran for each free VRAM pair. The command it produced then ran on two B200s, with `cudaMemGetInfo` capped to that pair. The cap only changes what llama.cpp sees as free memory; it does not stop an allocation. So for every load, the table checks llama.cpp's own per-card memory (weights, cache, compute, unaccounted) against the card. Every load answered two concurrent 13.5K-token prompts. | Free VRAM | `main` | unslothai#19148 | This PR | |---|---|---|---| | 14848 + 11264 MiB | `--fit on`, target 3714: 12.3 GiB of weights on GPU | `--fit on`, target 1536: 16.6 GiB on GPU | same command as unslothai#19148 | | 15360 + 11264 MiB | `--fit on`, target 3714: 12.8 GiB on GPU | `--fit on`, target 1536: 16.6 GiB on GPU | same command as unslothai#19148 (4 slots, `--fit on`, target 1536) | | 16384 + 12288 MiB | all layers on GPU, 4 slots, split by count: 13.5 GB on the 12 GB card, so it fails at load | all layers on GPU, 1 slot, split by count: 13.9 GB on the 12 GB card, so it fails at load | all layers on GPU, 4 slots, `--tensor-split 19.5,5.5`: 15.0 GB and 11.1 GB, under 16 and 12 GB | Decode speed, median of 5 runs of 256 tokens after a warmup, in tok/s. These numbers are noise-dominated: the host CPU was shared with other jobs, and loads with every layer on the GPUs measured anywhere from 91 to 698 tok/s across rounds of the same setup. They do not rank the versions; the placement above is the result. | Free VRAM | `main` | unslothai#19148 | This PR | |---|---|---|---| | 14848 + 11264 MiB | 134 | 155 | same command as unslothai#19148 | | 15360 + 11264 MiB | 155 | 208 | same command as unslothai#19148 | | 16384 + 12288 MiB | 110 (would not load on the real cards) | 184 (would not load on the real cards) | 91 | `main` on the fixed build reproduces the report: 11.6 and 7.9 GB used of the two cards with expert weights on the CPU, as in the Task Manager readings in unslothai#19137. With 16 + 12 GB free, this PR keeps the whole model on the GPUs and loads. `main` and unslothai#19148 plan the same placement but split by layer count, so the 12 GB card cannot hold its share. The 1024 MiB per-device overhead matches what llama.cpp reports as unaccounted per device (0.8 to 1.3 GB), so this PR leaves the multi-GPU margins as they are. ## Boundaries - The byte split shares the cards by the lowest fill they can all hold, in device order. With three or more cards it can leave a middle card lightly used. - `llama-completion` reserves a logit row per ubatch token, so its single-slot buffers are larger than `llama-server`'s at the same settings (819 vs about 252 MiB at 32K, ub 512). Studio launches `llama-server`, which is what this PR prices. - The DSpark drafter's own compute buffer (203 MiB with f16 draft K/V and 663 MiB with q4_0 at 128K) is unchanged by the fix and priced as before. ## Tests - `test_inkling2_banded_quant_kv.py` adds: - the marker detection, including each marker alone, a longer name, an unknown build number and an older build number; - 48 measured points, plus mixed K/V types; - context monotonicity, with no slot or K/V type dependence; - the layer-split mask copies; - the `--fit-target` margin; - the dense fallback with flash attention off or ubatches of 32 or fewer. - `test_byte_balanced_layer_split.py` covers: - an even model keeping llama.cpp's split; - the unslothai#19137 rows split by bytes at 16 + 12 GB, and handed to the fitter at 15 + 11 GB; - each run fitting its card, the positional order over three cards; - the planner path from the layout, and the launch falling back to `--fit on`. - a slot cut whose pin has no layer split that fits: the launch keeps the 4 slots asked with `--fit on`. - The 31 Studio planner suites give the same failing test IDs on `main`, unslothai#19148 and this PR. All 321 come from this host's CUDA probe guard (a CPU test reaching `torch.cuda` on a GPU host without a queried device), not from these changes. --------- Co-authored-by: oobabooga <[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.
Disposable CI run for unslothai#13565. Do not merge; closed after CI.