Skip to content

Studio: fit MiniMax-H3 GGUF to a Vulkan host's GTT pool before rendering - #13531

Merged
danielhanchen merged 20 commits into
mainfrom
studio-h3-vulkan-16gb
Oct 13, 2026
Merged

danielhanchen merged 20 commits into
mainfrom
studio-h3-vulkan-16gb

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

A user on AMD Linux (radv, 16 GB discrete card, 48 GB RAM) got radv/amdgpu: Not enough memory for command submission then ggml_vulkan: device lost loading MiniMax-H3 Q4 through the sd.cpp engine, and could not tell why the engine ran Vulkan when the backend was meant to be ROCm.

Root cause: the Vulkan pinned host buffer, not VRAM or RAM. With --offload-to-cpu, sd.cpp places every module that computes on the GPU in the Vulkan pinned host buffer (params_buffer_type_for -> ggml_backend_dev_host_buffer_type) and keeps it for the whole render (the user's model_manager.cpp:490 line is exactly the "prepared params backend buffers ... on Vulkan_Host" log). On amdgpu that buffer is GTT, which the kernel caps at half of system RAM by default, about 23.4 GiB on a 48 GB host. In the default memory mode H3 Q4 pins 31.35 GiB there (DiT and the Qwen3-VL-32B encoder), which cannot fit. VRAM stays under free memory at every preset because --max-vram segments the graph.

Why Vulkan. The ROCm sd-cli prebuilt ships without its runtime and needs ROCm 7 from the host (libamdhip64.so.7, libhipblas.so.3, librocblas.so.5); Studio's torch bundles its own HIP libraries, so a working Studio says nothing about the host. When they cannot be loaded, Studio falls back to the Vulkan build and records the switch, and every later load diverts silently.

Changes:

  • Load-time fit check (h3_native_memory_fit, amdgpu_gtt_limit_bytes in video_minimax_h3.py, wired in video.py before the device pin). Reads each amdgpu card's GTT size from sysfs (mem_info_gtt_total). In memory mode auto, when the encoder does not fit beside the denoiser in GTT, the encoder runs on the CPU (--clip-on-cpu, which cuts the pinned set to 15.49 GiB for Q4). An explicit memory mode that cannot fit is refused at load with a message naming Low VRAM; if even Low VRAM cannot fit, the message names Q2_K and ttm.pages_limit. A host-RAM floor (swap counted) applies on any accelerator. UNSLOTH_H3_NATIVE_FIT_CHECK=0 turns the checks off.
  • Fallback is explained. The ROCm -> Vulkan fallback log names the missing libraries (or the gfx target / /dev/kfd case) and how to clear the record (DELETE /api/settings/diffusion-accelerator-fallback), and a load diverted by a stored record now says so.
  • Failure text. Native out-of-memory and device-lost failures now point at Low VRAM, 864x480 or a smaller quant instead of "The renderer stopped unexpectedly".

Testing

Repro on this host: Studio's exact Vulkan sd-cli prebuilt on an NVIDIA B200 through the Vulkan driver, an exclusive card with a ballast leaving 15.5 to 15.7 GiB free, Studio's own flag builders, 124 frames, 1 step:

Arm Mode Preset Quant Result VRAM peak Vulkan pinned (GTT on amdgpu)
base auto 1344x768 / 960x544 / 864x480 Q4 renders 15.13 / 15.04 / 14.71 GiB 31.35 GiB
base low_vram same Q4 renders same 15.49 GiB
base auto 864x480 Q2 renders 11.86 GiB 22.21 GiB
head auto (encoder on CPU) 1344x768 / 960x544 / 864x480 Q4 renders 15.13 / 14.45 / 14.71 GiB 15.49 GiB
head balanced any Q4 refused at load, names Auto / Low VRAM
  • New tests/test_h3_native_memory_fit.py (21 tests) and 4 load tests in tests/test_video_backend.py through the real H3 load path.
  • After rebasing on current main: test_h3_native_memory_fit.py, test_sd_cpp*.py, test_video*.py, test_diffusion_autoopt_parity.py give 3322 passed, 97 skipped, 0 failed.

Not proven here: the radv crash itself (no AMD discrete card on this host; NVIDIA has no GTT cap, so the diagnosis rests on the sd.cpp source, the kernel default and the measured pinned totals), Low VRAM on a real radv host, the ROCm path's own pinned limits, and the sd-server render path (same flags, only sd-cli was run). The text encoder on the CPU costs prompt-encode time (43.8 s vs 17 s on this 192-core host). The quant picker still judges fit on the denoiser file alone, so Q4 stays the recommended quant on such a host.

On a Vulkan sd.cpp build every GPU-run module of an offloaded H3 render lives in
the device's pinned host buffer for the whole render (31.35 GiB at Q4_K + Q4_K_M).
On amdgpu/radv that is GTT, capped at half of system RAM by default, so a 16 GB
card on a 48 GB host died with "radv/amdgpu: Not enough memory for command
submission" and a lost device.

- Load: read the amdgpu GTT pool; memory auto moves the text encoder to the CPU
  when that alone makes it fit, an explicit mode that cannot fit is refused
  naming Low VRAM, and a pool too small for either names Q2_K and
  ttm.pages_limit. A host RAM floor refuses loads whose weights cannot fit
  system RAM. UNSLOTH_H3_NATIVE_FIT_CHECK=0 turns both off.
- Failure text: native capacity failures and lost devices now say to use Low
  VRAM, a smaller preset, or a smaller quant instead of "renderer stopped".
- ROCm to Vulkan fallback: the log names the missing ROCm libraries (or the gfx
  target / kfd case) and how to clear the record, and a load diverted by a
  persisted record now says so instead of switching silently.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-13T14:27:05.333475Z e9e1539 New commits
ℹ️ 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[bot]

This comment was marked as resolved.

pre-commit-ci Bot and others added 2 commits October 13, 2026 05:25
…t choice

- Detect the build's accelerator from its own --list-devices when no install
  record exists, so an SD_CLI_PATH / UNSLOTH_SD_CPP_PATH Vulkan build is fit
  too (read from the listing the verdict already took, no extra probe).
- Count swap only as far as the cgroup allows (v2 memory.swap.max minus
  memory.swap.current, v1 memsw headroom minus memory headroom); none when a
  RAM limit is enforced and the swap allowance cannot be read.
- Judge the pool by free GTT (mem_info_gtt_total - mem_info_gtt_used).
- Apply the GTT limit only when the selected Vulkan device is AMD, read from
  the amdgpu card at the torch card's PCI slot (the tightest amdgpu card when
  the slot is unknown); Intel, NVIDIA and other Vulkan devices are not bound.
- Auto no longer commits --clip-on-cpu at load: the fitted flags are kept for
  an offloaded render and a resident render runs without them. Auto refuses at
  load only when even the smallest clip could not render resident; otherwise
  an offloaded render that cannot fit is refused at render time with its own
  message.
- The ROCm runtime check is tri-state: unknown is no longer read as "the
  runtime loads", and Windows gets the HIP SDK DLL / 0xC0000135 explanation.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…eck without Studio's RSS

- cgroup v1 counts RAM+swap together, so the swap this process can use is the
  tightest memsw headroom minus the tightest RAM headroom over every level,
  not the per-level differences (which understated a tight child under a tight
  parent). v2 keeps its per-level minimum: memory.swap.max is swap-only.
- The native H3 host RAM check uses available RAM (cgroup-capped) plus the
  bounded swap allowance. sd-cli / sd-server is a separate process, so Studio's
  own RssAnon / RssShmem is no longer added back; the diffusers guard is
  unchanged.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…d-server before measuring

- h3_native_memory_fit takes RAM and swap separately. Under --offload-to-cpu
  the GPU-run modules' weights are in page-locked host buffers that cannot be
  swapped, so that set plus sd-cli's overhead must fit RAM alone; swap only
  covers the pageable CPU-run modules. Auto moves the text encoder to the CPU
  when that makes the locked set fit RAM (as it already did for GTT); an
  explicit mode is refused naming Low VRAM.
- A replacement load whose fit would refuse or move the encoder first
  releases the committed model's idle sd-server (busy: at its render's end)
  and measures again, so a server in its keepalive window no longer blocks the
  load until the idle timer fires. The old model stays loaded and respawns its
  server on its next render; a load that fits leaves the server alone.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

… outgoing server, and re-fits each offloaded render

- Unreadable /proc/self/cgroup is judged as the namespace root (where the RAM
  budget already reads its limit): a root RAM limit without readable swap
  files yields no swap.
- cgroup v1 swap is the memsw headroom minus the RAM the fit actually budgets
  (min(MemAvailable, cgroup headroom)), passed as ram_budget, rather than
  minus the cgroup RAM headroom. v2 is unchanged.
- Releasing the outgoing model's sd-server reports stopped / pending / none.
  Only a stopped server is re-measured; a pending one (mid-render) keeps the
  reading, and a refusal then says a render is still running and to retry
  after it finishes. The load never waits on the render.
- Auto's offloaded render re-runs the fit on live RAM, swap and GTT readings
  (file reads only; the PCI slot and sizes are resolved at load), so pressure
  since the load is caught and freed memory lifts a load-time refusal. A live
  offloaded sd-server keeps its own flags rather than being measured against
  its own placed weights.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…use, free a resident server before an offloaded re-fit

- RAM for the native fit is capped by the tightest cgroup v1 memsw (RAM+swap)
  headroom, so RAM plus swap never exceeds what the cgroup can charge.
- A swap reading that fails now counts as 0 swap and keeps the RAM check,
  instead of dropping the whole host budget.
- When an amdgpu card reports its GTT total but not a readable
  mem_info_gtt_used, the reading is only an upper bound: Auto trusts only the
  encoder-on-CPU set (and refuses only past total minus the margin), an
  explicit mode that keeps the encoder on the GPU is refused naming Low VRAM.
- An offloaded Auto render with a live resident sd-server releases it before
  the live re-fit (slot.get() would replace it on the signature change). Only
  a stopped server's memory is counted as freed; a busy one keeps the reading
  and a refusal says a render is still running.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…e GTT use, keep the last good device listing

- The live-server shortcut (reuse its flags without a reading) now applies
  only to the server it describes. Before the one-shot sd-cli runs (reference
  video / audio, no server, or a server that could not serve) the render is
  re-fitted with reuse_live_server=False, which releases that server and fits
  the launch on live readings.
- An amdgpu card whose mem_info_gtt_used cannot be read no longer has its
  total treated as usable: every mode that keeps the text encoder on the GPU,
  Auto included, is refused naming Low VRAM; an explicit Low VRAM load still
  runs against the total.
- A transiently unreadable --list-devices re-probe keeps the last successful
  listing; readers already compare (size, mtime_ns), so a replaced binary is
  never answered from it, and the AMD GTT check is no longer skipped.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ding, defer the fit past an in-process teardown

- Every offloaded native render, explicit modes included, keeps its fit
  inputs and is re-fitted on live readings before a process starts (fresh
  sd-cli or a server start). An explicit mode's flags are never altered: it
  runs as chosen or is refused.
- The selected torch card's PCI slot names the GTT card only when the Vulkan
  device pin resolved; otherwise the tightest amdgpu card is read. A slot that
  matches no amdgpu card also falls back to the tightest card instead of
  skipping the check.
- amdgpu_gtt_reading returns free GTT and whether its use was readable from
  one sysfs pass; the two older helpers wrap it.
- When the load replaces an in-process (diffusers) model, whose memory the
  commit's teardown frees before any native process starts, a load-time
  refusal is logged and deferred to the pre-launch live fit instead of
  refusing on a reading that still counts the outgoing model.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…e file cache out of memsw usage

- H3NativeServerSlot.get takes a pre_spawn callback, run under the slot lock
  only when a process is about to start (after any old server has stopped,
  and never on reuse). The render passes the live launch fit
  (_h3_launch_flags), so a server that exited or was released after the
  alive_signature() snapshot is started with freshly fitted flags or refused.
  A refusal propagates instead of disabling the slot.
- cgroup v1 RAM and memsw usage subtract total_inactive_file from
  memory.stat, as cgroup_memory_budgets already does for RAM, in both
  cgroup_v1_memsw_headroom_bytes and cgroup_swap_allowance_bytes.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3f375bf799

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

Limit reclaim in a v1 memcg whose applicable memory.swappiness is 0 (its own,
else the nearest ancestor that reports one) never swaps, so the swap the H3
native fit may count is 0 there. v2 has no per-cgroup swappiness and is
unchanged.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A reused live sd-server and a resident render (reused or started) place no
new weights in host memory, so no full fit runs for them, but each render
still needs H3_NATIVE_HOST_OVERHEAD_BYTES of RAM for its frames, audio and
CPU buffers. Those paths now check live available RAM (the same budget
reader, swap not counted, the server's resident weights not charged again)
and refuse with a clear message when it does not cover that overhead.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ver before a resident render, one H3 kill switch

- cgroup v1: return no swap when any enforcing level (finite memory or memsw
  limit, the process's own cgroup included, the namespace root never) has
  memory.swappiness 0; a level without a limit no longer decides it.
- A resident Auto render with a live server under other flags (an idle
  offloaded one holding pinned RAM) releases it before the per-render RAM
  check, so get()'s replacement of that server is not counted against the
  render; a busy one keeps the reading and the "still running" hint.
- h3_native_fit_check_enabled() is the single UNSLOTH_H3_NATIVE_FIT_CHECK
  switch: h3_native_memory_fit, h3_native_render_overhead_refusal and both
  render-time paths (_h3_offloaded_render_flags, _h3_launch_flags) honour
  it, so with it off no reading, release or refusal happens and the
  committed flags run as they are.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ot fake takes pre_spawn

- A cgroup v1 container sees its own cgroup as the memory mount root (/proc/self/cgroup reads /), and the
  RAM loop already reads that root's limit, so its memory.swappiness 0 now counts once it enforces a
  memory or memsw limit. An unlimited host root still never enforces, so its value stays irrelevant.
- The H3 server tests' _FakeSlot.get() accepts pre_spawn and starts with the flags the fit returns, as
  H3NativeServerSlot.get does.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@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. Swish!

Reviewed commit: c35b90cbf1

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

- test_video_backend / test_sd_cpp_rocm_vulkan_fallback: an autouse fixture gives the host room, since a native
  H3 load now fits against available RAM and the macos-15 runner has about 3 GiB (91 tests refused there). Tests
  that need a tight host stub usable_host_ram_mib themselves, after the fixture.
- The missing-ROCm-libraries test pins os.name to posix too, so it runs on a Windows runner.
- The unreadable-swap test skips where psutil is not installed.
@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: eea83e06cc

ℹ️ 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 +1433 to +1436
inputs = getattr(runtime, "fit_inputs", None)
if not h3_native_fit_check_enabled():
inputs = None # UNSLOTH_H3_NATIVE_FIT_CHECK=0: no reading, no release, the committed flags as they are
if render_resident or "--offload-to-cpu" not in render_flags or inputs is None:

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 Bypass cached fit decisions when the guard is disabled

If UNSLOTH_H3_NATIVE_FIT_CHECK is changed to 0 after an Auto-mode model was loaded, setting inputs to None still falls through to h3_native_offloaded_render_flags(), which applies the load-time offloaded_flags or raises the cached offload_refusal. Thus the documented kill switch does not actually disable every guard for the loaded runtime; return the committed render_flags directly when the check is disabled.

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.

Not changing this. UNSLOTH_H3_NATIVE_FIT_CHECK is a process environment variable with no UI or API setter, so it cannot change inside a running Studio. Setting it means a restart, and the reload goes through the load path, which already skips the fit when the switch is off, so no offloaded_flags or offload_refusal is cached for the render to apply.

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@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. You're on a roll.

Reviewed commit: eea83e06cc

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

@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: 550d3990eb

ℹ️ 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 +1428 to +1430
slot = _sysfs_pci_slot(device) if want is not None else None
if want is not None and slot != want and not (slot or "").startswith(want + "."):
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Account for every card using the shared GTT pool

On multi-AMD systems such as an APU plus a discrete GPU, pci_slot is normally populated, so this filter discards every other amdgpu card before reading its mem_info_gtt_used. Because the TTM page limit is shared, allocations on those discarded cards reduce the capacity available to the selected card; reporting only selected_total - selected_used can therefore overstate free GTT and allow a render that still exceeds the global pool, resulting in the device-loss failure this guard is intended to prevent. Include all cards' usage when computing the shared remaining budget, while separately applying the selected card's per-device bound.

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 e6dbcb1. TTM's page limit is one pool for every amdgpu device, so amdgpu_gtt_reading now subtracts the GTT used on all amdgpu cards from the pool size: the selected card's mem_info_gtt_total when the slot is known, else the smallest total. That bound is never above the selected card's own total - used, and an unreadable use on any card makes the reading unknown. A test with an APU using 12 GiB beside a discrete card leaves the discrete card 10 GiB of a 24 GiB pool instead of 22.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: e6dbcb18f7

ℹ️ 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 cd8b2ed into main Oct 13, 2026
@danielhanchen
danielhanchen deleted the studio-h3-vulkan-16gb branch October 13, 2026 14:28
danielhanchen added a commit that referenced this pull request Oct 13, 2026
#13612)

- diffusion.py: read pipe.scheduler with getattr on the generate path,
  like the status poll. #13569 made every generate touch it, which broke
  173 test_diffusion_backend tests whose fake pipelines have none.
- test_diffusion_autoopt_parity: skip the two fixture comparisons when
  diffusers is not installed (Backend CI); without it nothing compiles
  and every case differs.
- Format three files from #13610 and #13531.
- Raise the startup budget by the 0.8 KB transfer five merges added.
danielhanchen added a commit that referenced this pull request Oct 18, 2026
…ntegrated AMD GPUs (#13786)

Follow-up to #13531.

MiniMax-H3 GGUF on an integrated AMD GPU (Strix Halo, Strix Point,
Phoenix) loads and passes Studio's GTT fit, then fails every
default-size render on the Vulkan build when GTT is the half-of-RAM
default:

```
[W] model manager cannot make enough memory available on Vulkan0: need 5687.60 MB device / 5175.60 MB budget, available 1356.88 MB device / 11346.88 MB budget
[E] minimax_h3 segment 1/1 (graph) failed during workspace capacity check
```

The UI reports "The renderer stopped unexpectedly".

## Cause

1. Studio passes `--max-vram -1`, which sd.cpp resolves at startup
(`resolve_auto_max_vram_bytes`) from ggml's free memory, before it pins
anything. For an integrated GPU, ggml-vulkan's free memory includes the
GTT that the pinned denoiser and VAEs (15.5 GiB at Q4) are about to
take. With 22.5 GiB free, sd.cpp budgets 21.8 GiB, plans one segment,
stages the whole denoiser, and has 1.4 GB left for a 5.2 GB compute
buffer.
2. #13531's fit checks only the pinned weights against free GTT. On a
UMA card the compute buffer comes from the same pool, and the launch fit
has no render size to estimate it.
3. sd.cpp's capacity failure strings are not in
`_ACCELERATOR_CAPACITY_FAILURE_MARKERS`, so the failure gets no memory
advice.

## Fix

- `sd_cpp_vulkan_device_is_uma` reads ggml-vulkan's `uma: 1` from the
cached `--list-devices` output.
- On a UMA device with a GTT reading, the fit replaces `--max-vram -1`
with `--max-vram -<pinned + 1 GiB>`, so sd.cpp's budget excludes the
weights it pins. An explicit memory mode keeps its placement and only
gets the corrected budget.
- `h3_native_live_fit` takes the render's estimated compute buffer. On a
UMA device, the part the carve-out cannot hold counts against GTT beside
the pinned weights. The render, the sd-server start (`pre_spawn`) and
the load (smallest clip) all pass a size.
- The capacity markers gain sd.cpp's three graph-cut strings, so these
failures get the Low VRAM / 864x480 / smaller quant advice and never
count as accelerator failures.

Unchanged: discrete cards (`uma: 0`), CUDA, ROCm, Windows (no amdgpu
sysfs, so no GTT reading), devices whose listing has no `uma` field,
resident renders, and `UNSLOTH_H3_NATIVE_FIT_CHECK=0`.

## Verification

AMD DevLab Linux runner (gfx1151, RADV, 0.5 GiB carve-out, 62.5 GiB
GTT), Studio's pinned Vulkan prebuilt, Q4_K denoiser and Q4_K_M encoder,
Studio's Auto flags, 124 frames, 2 steps. A Vulkan allocation held the
rest of GTT so that a fixed amount stayed free. The "this PR" column ran
this PR's `h3_native_live_fit` on the runner's live sysfs readings and
passed sd-cli exactly the flags it returned.

| GTT free | Size | main (`--max-vram -1`) | This PR |
|---|---|---|---|
| 22.0 to 25.0 GiB | 864x480 | capacity check fails | |
| 22.5 GiB | 864x480 | capacity check fails | `-17.05`, renders in 387 s
|
| 30.0 GiB | 1344x768 | | `-17.05`, renders in 2268 s |
| 22.5 GiB | 1344x768 | capacity check fails | refused: 26.4 GiB needed,
10.4 GiB of it activations |
| 20.0 GiB | 864x480 | | refused, naming the Q2_K denoiser |
| 62.5 GiB | both | renders | |

Runs:
[main](https://github.com/oobabooga/unsloth/actions/runs/38641651208),
[main, full and 22.5
GiB](https://github.com/oobabooga/unsloth/actions/runs/38631973349),
[this
PR](https://github.com/oobabooga/unsloth/actions/runs/38652906327).

The fit charges only the compute buffer, not sd.cpp's per-segment
working set on top of it (0.3 GiB at 864x480, 1.2 GiB at 1344x768), so
it refused no size that rendered. A render inside that margin fails in
sd.cpp's capacity check and now gets the memory advice.

`test_h3_native_memory_fit.py`, `test_video_backend.py` and
`test_sd_cpp_backend.py`: 740 passed.

Not tested:

- A UMA part with a large carve-out. The reserve still applies; it only
shrinks a budget the carve-out already covers.
- The sd-server render path. Its start uses the same `_h3_launch_flags`,
but only sd-cli was run.
- A reused offloaded sd-server is not re-fitted, as on main, because a
reading would count its own weights. A larger render on it relies on
sd.cpp's capacity check.


## Independent verification on the real target

Each tree's own Studio code (load fit, Auto resident choice, the sd-cli
launch fit, the engine and the failure classifier) ran on gfx1151 Linux
(RADV, 0.5 GiB carve-out, 62.47 GiB GTT), merge base 97ed941 vs head
33599d4, in the same job. Q4_K denoiser, Q4_K_M encoder, 124 frames, 2
steps, Studio's real Auto flags.

GTT held to 22.5 GiB free:

| Size | Base | Head |
|---|---|---|
| 864x480 | `--max-vram -1`, budget 21.80 GiB, fails after 9.7 s in the
workspace capacity check | `--max-vram -17.05`, budget 5.75 GiB, renders
in 386.6 s (864x480, 124 frames, audio) |
| 1344x768 | fails after 8.3 s in the same check | refused before launch
(26.4 GiB needed, 22.5 GiB free) |
| Low VRAM, 864x480 | `--max-vram -1` | same placement, `--max-vram
-17.05` |

Nothing held (healthy host), 3 renders each:

| | Flags | Runs (s) | Median | Peak GTT |
|---|---|---|---|---|
| Base | `--max-vram -1` | 386.2, 374.9, 377.6 | 377.6 | 42.73 GiB |
| Head | `--max-vram -34.02` | 374.6, 377.4, 370.9 | 374.6 | 42.73 GiB |

The 3.0 s difference is inside the 18.9 s run-to-run spread. On Windows
11 gfx1151 (Vulkan prebuilt) the launch flags and the load fit are
byte-identical to base. Across 4,800 enumerated fit cells, only the 40
Vulkan cells with a GTT reading and a UMA device move, and only in their
`--max-vram` value.

## Follow-up: capacity markers

The model manager's "cannot make enough memory available" warning is
printed by renders that complete too (twice in the head render above),
so matching it could relabel an unrelated crash as out of memory and
skip recording a build failure. Commit d107ffe matches only sd.cpp's
failing-segment lines (`failed during workspace capacity check`, `failed
during workspace allocation`, `failed during allocated capacity check`).
The real capacity failure above still gets the memory advice. New test:
`test_a_recovered_capacity_warning_is_not_a_capacity_failure`.

Also not covered: Strix Point and Phoenix, Windows renders under GTT
pressure, and the ROCm sd.cpp build under the same pressure.

---------

Co-authored-by: danielhanchen <[email protected]>
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant