Skip to content

Studio: keep a Windows iGPU's carve-out usable when a load is memory-mapped - #18448

Merged
danielhanchen merged 7 commits into
mainfrom
studio-igpu-paged-headroom-carveout
Nov 20, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-igpu-paged-headroom-carveout

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Fixes #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 #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 #16857), ROCm/CUDA iGPUs, Linux, more than one device, and every case #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: #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).

@danielhanchen danielhanchen self-assigned this Nov 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 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-20T14:42:51.003831Z 6ad6339 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5daf890c98

ℹ️ 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".

Comment on lines +16230 to +16231
shared_mib = int(rows[0][1]) + _IGPU_HOST_RESERVE_MIB - carve_out / (1024 * 1024)
return float(min(_IGPU_PAGED_FIT_HEADROOM_MIB, max(0, math.ceil(shared_mib))))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for occupied dedicated memory when capping headroom

When another model or application already occupies the iGPU's dedicated memory, this subtracts the total carve-out capacity from a live free-memory reading. _igpu_dedicated_memory_bytes reads the registry's DedicatedVideoMemory and DedicatedSystemMemory capacities, not their remaining free space. For example, a 16 GiB carve-out with 8 GiB occupied and 7 GiB of shared memory free produces a 15 GiB raw-free reading and zero paging headroom, allowing the fitter to allocate nearly 15 GiB despite only 8 GiB remaining in the carve-out. This reintroduces allocations into the shared memory that the paging margin is intended to protect. Base the cap on free dedicated memory, or retain conservative headroom when dedicated usage cannot be determined.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9e8eec4: the cap now subtracts the carve-out's free part (size minus its Dedicated Usage counter for that adapter's LUID), and an unreadable counter keeps the flat 15 GiB headroom.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 9e8eec488b

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a54582c596

ℹ️ 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".

Comment on lines +15712 to +15713
if requested is None or requested == -1:
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor inherited GPU layer limits before crediting iGPU memory

When LLAMA_ARG_N_GPU_LAYERS specifies a fixed count and argv contains no layer override, this returns True without checking the inherited limit. For example, LLAMA_ARG_N_GPU_LAYERS=0 still credits 12,964 MiB on the simulated 780M configuration, although the child places all weights in host RAM. This makes _launch_host_shortfall_message suppress the shortfall warning and the protective mmap override for an 11.3 GiB model with only 7,843 MiB of available RAM, risking an out-of-memory failure. Resolve the environment's layer count when no argv override exists before deciding that the fitter owns placement.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aac7ebc: an inherited LLAMA_ARG_N_GPU_LAYERS count (other than auto/all/-1 or above the block count) now keeps the iGPU pool uncredited, as an argv -ngl cap does.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aac7ebc41a

ℹ️ 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".

Comment on lines +15792 to +15795
if shared_gpu_ids and not every_layer and self._fit_may_place_every_layer(argv, env):
fit_margin = _single_device_fit_margin(argv, env)
if fit_margin is None:
fit_margin = _LLAMA_FIT_TARGET_DEFAULT_MIB

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply each device's fit margin before crediting iGPU memory

For a multi-device launch with per-device --fit-target values, _single_device_fit_margin returns only the first entry, but this value is then applied to every shared GPU. For example, with --device Vulkan0,Vulkan1 --fit-target 512,23000, a discrete Vulkan0 and an iGPU Vulkan1 reporting 22137 MiB usable, this credits 12964 MiB on the reporter's host-memory readings even though Vulkan1's own margin leaves only 161 MiB. _launch_host_shortfall_message can consequently suppress both the RAM warning and the protective mmap override for a model that actually spills into insufficient host RAM. Resolve the margin for each selected device, or conservatively withhold this new credit when a per-device list cannot be mapped.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6ad6339: the credit now charges the largest --fit-target entry (fit_target_margin_in), the same convention the other fit pricing uses, so a per-device list can only shrink it.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 6ad6339006

ℹ️ 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 78ef615 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-igpu-paged-headroom-carveout branch November 20, 2026 15:10
danielhanchen added a commit that referenced this pull request Nov 23, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Incorrect loading into unified memory on Ryzen 8700G & Radeon 780M

1 participant