Skip to content

Studio: keep llama.cpp's --fit inside RAM on a paged Windows ROCm APU - #18352

Open
debojyoti-dev wants to merge 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/apu-fit-target-unbacked-18316
Open

debojyoti-dev wants to merge 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/apu-fit-target-unbacked-18316

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

Fixes #18316

Problem

On a Windows ROCm APU the PAL HIP reading adds most of the WDDM shared heap on top of RAM. The reporter's 64 GB Strix Halo (gfx1151, NO_VMM) reads 110079 MiB free. Studio already sizes its own plan for an APU against system RAM, but for a paged load it only hands llama-server --fit-target 16384, and llama.cpp's fitter subtracts that margin from the HIP reading. The child could therefore fill about 94 GiB, so it offloaded every layer, and on a no-VMM APU every one of those buffers is host RAM. That matches the ~51 GB private bytes in the report.

Change

  • New LlamaCppBackend._rocm_apu_unbacked_free_mib(gpu_ids): for a single Windows ROCm APU, it returns the part of the HIP free reading (torch.cuda.mem_get_info, which is what the child reads) that available RAM plus the BIOS carve-out (_igpu_dedicated_memory_bytes, read from the registry) cannot back. It returns 0 off Windows, for more than one device, for a non-APU, or when a reading is missing.
  • _paged_fit_headroom_flags takes an unbacked_mib and adds it on top of _IGPU_PAGED_FIT_HEADROOM_MIB. _margins_with_paged_headroom and _withdraw_paged_headroom carry the same amount, so per-device margins and the arch-crash retry stay consistent.
  • load_model reads it only when a non-Vulkan load is actually paged, and the retry recomputes it for its own rows.

A user --fit-target (argv or env), fit off, per-device lists, more than one device, RPC, and Vulkan launches are unchanged, as they were before.

For the reporter's numbers, the margin becomes 16384 + (110079 - 54335 - 512) = 71616, so llama.cpp may fill 38463 MiB, which is the RAM left after the existing paging headroom.

Testing

No AMD APU here (Ryzen 5600H + RTX 3050 4 GB, Windows 11), so the two halves were checked separately:

  1. Studio's real load_model launch path (fake binary only to capture argv) with the reporter's hardware readings: a 70.6 GiB GGUF, 54335 MiB available, HIP free 110079 MiB, and a 512 MiB carve-out. Main reproduces the reporter's exact tail --load-mode mmap --lazy-mode on --fit-target 16384. With this change it emits --fit-target 71616.

argv

  1. The real llama.cpp b12347 prebuilt (the reporter's build, installed with studio/install_llama_prebuilt.py) on the 3050 with --fit on: the fitter keeps device use under free minus --fit-target (margin 1024: all 29 layers; margin 2900 of 3303 free: a 403 MiB target and 7/29 layers, 387 MiB). So raising the margin by the unbacked part caps what the child puts in "device" memory.

fitter

  1. New tests in tests/test_llama_cpp_placement.py: the unbacked-reading helper with the report's numbers plus every opt-out, the flag and margin math including withdrawal, and a full launch on a paged Windows ROCm APU, both with an unbacked reading and with one RAM can back (which still gets exactly 16384).

tests/test_llama_cpp_placement.py with this change: 604 passed, 8 failed. A git archive of main has the same 8 failures on this machine (missing gguf module, MTP drafter and unmappable-mask cases), and the 4 new tests account for the extra passes. ruff check is clean.

On Windows the PAL HIP reading for an APU adds most of the WDDM shared
heap on top of RAM (110079 MiB free on a 64 GB Strix Halo in unslothai#18316).
Studio already sizes its own plan against system RAM, but the
--fit-target it hands llama-server for a paged load is only a margin,
and llama.cpp's fitter subtracts it from that HIP reading. With 16384
the child could fill about 94 GiB and offloaded every layer, which on a
no-VMM APU is all host RAM.

For a paged launch on a single Windows ROCm APU, the headroom margin now
also covers the part of the HIP free reading that available RAM plus the
BIOS carve-out cannot back, so the fitter plans against the same budget
Studio does. A user --fit-target, a fitter that is off, more than one
device, and Vulkan launches are unchanged. The arch-crash retry
withdraws the same amount it added.

Fixes unslothai#18316

Copy link
Copy Markdown
Contributor

Independent real Windows ROCm APU memory readings relevant to PR #18352, from NH Applied's Radeon 8060S / gfx1151 Strix Halo. This is a read-only hardware/fit-arithmetic comparison, not a full Unsloth load_model validation.

The machine is different from the original reporter's 64 GB APU: ours has 128 GiB installed and a very large GPU reservation. That makes it a useful negative control for a fix which must not mistake Windows-visible RAM for all memory backing the GPU.

Memory reading Real Strix Halo, 19 Nov 2026
Installed DIMM capacity (8 x memory modules) 131,072 MiB
Windows visible RAM (Win32_OperatingSystem) 32,406.816 MiB
Windows free RAM at sample time 4,603.930 MiB
DXGI DedicatedVideoMemory for Radeon 8060S 98,128.703 MiB
DXGI SharedSystemMemory 16,203.408 MiB
hipMemGetInfo total 110,456.531 MiB
hipMemGetInfo free 110,301.598 MiB

The local sample was obtained with a tiny HIP/C++ query (hipGetDeviceCount, hipGetDeviceProperties, hipMemGetInfo) and Windows DXGI CreateDXGIFactory1 / GetDesc1. Zero HIP allocations or GPU kernels, no model loaded, no system/driver changes. The selected HIP DLL was C:\Windows\System32\amdhip64_7.dll (10.0.3683.0); the compiled ROCm SDK header reported HIP_VERSION=70260201 and is not the loaded runtime version.

Two qualification notes for this PR:

  1. Do not clamp against Win32_OperatingSystem.TotalVisibleMemorySize alone. Here that is 31.65 GiB even though DXGI reports about 95.83 GiB dedicated to the GPU. The installed 128 GiB must not be equated with Windows-visible free RAM, nor can HIP's free aperture be simply added to either number.
  2. DXGI adapter enumeration needs identity care. EnumAdapters1 reported two entries both named Radeon 8060S (same PCI vendor/device and same 98,128.703 MiB dedicated capacity, but different LUIDs). HIP reports one device; Windows WMI shows one Radeon controller. Do not sum these DXGI entries into 196 GiB; resolve the adapter identity or fail closed if capacities disagree.

As a diagnostic upper-bound approximation only, Windows free RAM + one DXGI dedicated capacity = 102,732.633 MiB, whereas HIP free is 110,301.598 MiB, a 7,568.965 MiB gap in this snapshot. Existing GPU allocations, mapped pages and exact BIOS/driver accounting can reduce the usable backing further; I am not recommending an actual --fit-target value from this number.

Separately, transcribing the original #18316 report's numbers (HIP free 110,079 MiB; Windows available 54,335 MiB; PR-reported carve-out 512 MiB; existing target 16,384 MiB) independently reproduces the PR's 55,232 MiB unbacked estimate and 71,616 MiB illustrative fit target. That is arithmetic confirmation, not live execution of your patch on the reporter's machine.

For NH Applied Doctor, I added a read-only development diagnostic that distinguishes installed/Windows-visible/free/DXGI/HIP capacity, checks duplicate adapters and compares projected device use against an explicitly optimistic backed budget. 51 dedicated tests, 255 cumulative unit tests, and 19 development CLI regression checks passed. The live Strix data is classified as an aperture warning only, not a reproduced #18316 OOM or a validated PR fix.

If an extra hardware qualification would help, the useful next gate would be running the PR's real LlamaCppBackend._rocm_apu_unbacked_free_mib selection on a machine with a large BIOS carve-out (without loading a large model) and comparing the launch margin to an unpatched control.

Nigel Hutchinson - NH Applied
https://nhapplied.co.uk
[email protected]

@oobabooga

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@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-19T20:56:42.955616Z ff647de 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: ff647de039

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

rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 20, 2026
…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).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants