Repository navigation
Studio: memory-map an oversized load on an integrated GPU and read its lookup tables from disk - #16857
Conversation
…s lookup tables from disk
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. Keep them coming! 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". |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
…mapped (unslothai#18448) Fixes unslothai#18001 ## Problem On a Windows Vulkan iGPU with a BIOS carve-out, a paged (memory-mapped) load used only about half of the dedicated GPU memory. The reporter's Ryzen 7 8700G / Radeon 780M has a 16 GiB carve-out; Vulkan reads 23161 MiB free (16 GiB dedicated plus 7.8 GiB shared). Studio launched gemma-4-12B-it-qat UD-Q4_K_XL with ``` --fit on --fit-target 512 ... --load-mode mmap --fit-target 15872 ``` so llama.cpp's fitter could only use 23157 - 15872 = 7285 MiB, put 42 of 49 layers on the GPU and the rest on the CPU, even though the whole model (8306 MiB with context) fits in the carve-out. Task Manager after the load: 7.3 / 16.0 GB dedicated in use. Two things combine. First, the load should never have been paged: in Manual mode with Auto layers Studio launches with `--fit on`, and its host-RAM preflight credits a shared iGPU's memory only when argv forces every layer onto the GPU. Under `--fit on` it credited nothing, priced the whole model as system RAM ("About 7 GB of this model does not fit in GPU memory and would run from system RAM"), and switched the load to mmap. A discrete card is credited in the same situation. Second, the 15872 is the paging headroom from unslothai#16857: a flat `_IGPU_PAGED_FIT_HEADROOM_MIB` (15 GiB) added to the margin whenever an oversized load is switched to mmap on a Windows iGPU. It was measured on a 127 GiB Strix Halo, where it keeps the fit out of the shared memory that also backs the mapped pages. On an iGPU whose carve-out is smaller than that, it takes most of the carve-out away. On this machine almost every load takes that path: with 16 GiB of system RAM left to Windows, a 6 GB model already prices as oversized. ## Change 1. Pricing: when the fitter owns placement (`--fit on`, no layer cap, nothing pinned to the CPU), the preflight now credits a shared Vulkan iGPU with what the fitter can place there: its free reading less the `--fit-target` margin, still bounded by what host RAM can back (the same bound as the every-layer case). On the reporter's readings gemma-4-12B (6.24 GiB) and gpt-oss-20b (11.3 GiB) are no longer priced as oversized: no false warning, no mmap override, plain `--fit-target 512`. A model larger than the pool (21 GiB in the test) is still priced as oversized and paged. 2. Headroom, for loads that are really oversized: For a single Vulkan iGPU on Windows, the headroom is now capped at what the device reads above the free part of its carve-out. The carve-out size is the DirectX `DedicatedVideoMemory` + `DedicatedSystemMemory` value Studio already reads for the carve-out advice; its use is the `GPU Adapter Memory\Dedicated Usage` counter of the same adapter (joined on its LUID): `headroom = min(15360, max(0, raw_free - (carve_out - dedicated_used)))` so the fit still stays out of shared memory, which is what the headroom is for, but can use the whole free carve-out. For the reporter (about 0.3 GB of the carve-out in use before the load): headroom about 7.1 GiB, and the fitter gets about 15.8 GiB, enough for every layer. Unchanged: no carve-out or usage reading (Intel, two adapters, registry or counter failure), a carve-out at its automatic size, a large carve-out with 15 GiB or more of shared memory on top (the Strix Halo case from unslothai#16857), ROCm/CUDA iGPUs, Linux, more than one device, and every case unslothai#16857 already skipped (fitter off, a user `--fit-target`, per-device lists). When the carve-out covers everything the device reads, no headroom pair is added. The per-device margins and the arch-crash retry withdraw the same amount that was added. Note: unslothai#18352 edits the same helper for Windows ROCm APUs (raising the margin there); the two touch different backends and should merge cleanly in either order apart from the `_paged_fit_headroom_flags` signature. ## Testing - New full `load_model` pricing test on the reporter's readings (Vulkan row 22137 MiB, 7843 MiB available of 15992 MiB RAM, Manual mode): gemma-12B and gpt-oss-20b launch unpaged with no warning (both fail on main), a 21 GiB model is still paged. - New full `load_model` headroom test on a simulated 780M (Vulkan row 22137 MiB, 16 GiB carve-out with 300 MiB used, Manual mode with Auto layers, the reporter's path): main emits `--fit-target 15872`, this branch a margin that leaves exactly the free carve-out to the fitter. A half-occupied carve-out keeps a correspondingly larger margin. Controls (automatic 512 MiB carve-out, 96 GiB Strix Halo carve-out, unreadable carve-out, unreadable usage) emit 15872 on both. The headroom helper, the LUID join of the usage counter, the flag, margin and withdrawal math have unit tests; the behaviour tests fail on main. - Real Windows hardware (AMD DevLab, Radeon 8060S / gfx1151, Windows 11, Adrenalin 32.0.32015.7029, the reporter's llama.cpp build b12347-mix-241a0f3, Vulkan), base vs this branch running Studio's own readers: carve-out 337 MiB (automatic), Vulkan free 99437 MiB, Dedicated Usage counter read for that adapter (35.8 MB). Both emit `--fit-target 15872` and llama-server loads a Qwen3-0.6B Q4_K_M with `--fit on --load-mode mmap`. So this host is unchanged, as intended; its carve-out cannot be set to the reporter's 16 GiB, so the 780M case itself is covered by the simulated test only. - `test_llama_cpp_placement.py`, `test_llama_server_args.py`, `test_amd_apu_unified_memory.py`, `test_igpu_carveout_advice.py`, `test_llama_cpp_gpu_oom_respawn.py`, `test_llama_cpp_context_fit.py`: 1306 passed on this branch vs 1297 on main, with the same 16 failing/erroring test IDs on both (local environment: no GPU, missing optional modules).
## Problem When Studio switches an oversized load to mmap on a Windows integrated GPU, the paging path from #16857 can add 15 GiB to llama.cpp's fit reserve. With the default 1 GiB base reserve and 18 GiB free to CUDA, only 2 GiB remains for placement. This explains the CPU-heavy Inkling-2-Mini load in the [RTX Spark N1X report](https://www.reddit.com/r/unsloth/comments/1y3i8p0/). #18448 addressed the equivalent Vulkan case. ## Change Cap the extra paging reserve at 10% of the estimated free CUDA budget for a single Windows integrated CUDA GPU whose device index Studio can match. Use the same calculation on the initial launch and the architecture retry. Studio still uses mmap for oversized loads and honors an explicit `--fit-target`. ## Validation I ran the bundled ARM64 llama-server (b12631) on an RTX Spark N1X with Inkling-2-Mini `UD-Q4_K_XL` at 8K context. I simulated an 18 GiB free budget in the fitter because this laptop reports about 45 GiB free to CUDA. | Effective reserve | Layers reported on CUDA | Model weights on CUDA | | --- | ---: | ---: | | Current 16 GiB | 1 of 25 | 0.46 GiB | | Proposed cap, 2.8 GiB | 25 of 25 | 12.9 GiB | | Manual `--fit-target 1024` | 25 of 25 | 14.9 GiB | Some expert weights stayed on CPU with both lower reserves. A chat request with the manual override returned the expected answer at about 19 tokens/s. The new placement tests cover the CUDA cap, a different device, and a Vulkan index collision. I ran the test cases against the extracted placement helpers and compiled both changed Python files. The full Studio suite is left to CI. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
On an integrated GPU, a GGUF larger than memory fails to load, while Studio's warning says the weights are memory-mapped and paged in from disk. Reported on Discord: on an RTX Spark N1X (Windows),
unsloth/DeepSeek-V4.1-Flash-1017-GGUFUD-IQ1_S(216.97 GiB) failed withunable to allocate CUDA_Host bufferfor 110,595,290,400 bytes, exactly the size of the model's Engram lookup table (per_layer_token_embd.weight).Cause
llama.cpp's default
--load-mode automeans mmap unless a device lacks mmap support, which ggml reports for CUDA, HIP and Vulkan iGPUs (ggml-org/llama.cpp#26081). On the same devices--lazy-mode autoturns off (ggml-org/llama.cpp#28326), so lookup tables that are streamed from disk elsewhere stay resident too.Studio already switches an oversized
noneormlockload to mmap (_page_an_oversized_unmapped_load), but it treated an unset mode as mmap. On an iGPU nothing is set, so nothing changed.Fix
When a load Studio already prices as oversized runs where llama.cpp's
autoresolves tonone(the build listsauto, and a model device lacks mmap support), the override now:--load-mode mmap, including after it drops an explicitnone,--no-mmaporLLAMA_ARG_NO_MMAP;--lazy-mode onif no lazy mode was set and the model has a table over llama.cpp's 4 GiB auto threshold. mmap alone would still read the whole table at load, because prefetch skips only lazy ranges (src/llama-mmap.cpp);--fit-targetby 15 GiB over the launch's margin, unless the fitter is off or the user set a margin. Otherwise the fitter fills the shared memory with the GPU share and the load aborts at warm-up.The load warning gains one sentence saying Unsloth turned on memory mapping, which llama.cpp leaves off by default on this integrated GPU; the lazy table and the fit headroom go to the log only. Both the first launch and the arch-crash retry apply it.
Not changed: loads that fit (they keep
none, the faster load on an iGPU), discrete GPUs, Metal, builds withoutauto, a lazy mode or fit target the user set, and "Don't reserve system RAM" on an iGPU.Limits:
Verification
AMD DevLab Strix Halo machines (128 GB, Vulkan) loading
unsloth/Qwen3.8-Flash-Next-GGUFUD-Q5_K_XL(147.4 GiB, 50.7 GiB of it lookup table) through Studio, then two chat requests:ErrorOutOfDeviceMemoryat loadLoading from the chat picker on Linux, main is killed after 80 s and this PR loads in 73 s. The "after" panel shows the warning text of the final commit, rendered locally over a mocked load status; the load itself is the DevLab run above.
llama-server run directly on the same file, which is why the override sets each flag:
--lazy-mode onloads with or without mmap (34 and 72 s, about 23 tok/s). The 16 GiB fit target is not needed there and cut generation to 13 to 15 tok/s, so the headroom is Windows only.Tests in
test_llama_cpp_placement.pyandtest_llama_server_args.pydriveload_modelon a simulated integrated CUDA SoC with the DeepSeek-V4.1 table and cover each rule and exception above; the behavior tests fail on main. The 19 related backend test files pass (4,202 tests), and every full-suite failure also fails on main or passes when rerun on its own.