Skip to content

Studio FLUX.3 upsampler: count Vulkan VRAM on Linux ROCm before keeping the server resident - #17159

Merged
oobabooga merged 4 commits into
unslothai:mainfrom
hMelchisedek:pr/studio-upsampler-rocm-vulkan-vram
Nov 20, 2026
Merged

oobabooga merged 4 commits into
unslothai:mainfrom
hMelchisedek:pr/studio-upsampler-rocm-vulkan-vram

Conversation

@hMelchisedek

Copy link
Copy Markdown
Contributor

Summary

On Linux ROCm, the FLUX.3 prompt upsampler never unloads after writing its caption. FLUX.3 klein on stable-diffusion.cpp then fails at once with an out-of-memory error, and so does every later run until the upsampler's keep-alive runs out or Enhance prompt is turned off.

The cause is the free-memory figure. On Linux, hipMemGetInfo (torch.cuda.mem_get_info) counts only compute (KFD) allocations, so Vulkan processes and the desktop are invisible to it. In the ROCm Docker image, both the upsampler's llama-server and sd-server are Vulkan processes. So with the upsampler holding 5.8 GiB, release_if_tight still read the card as 11.8 GiB free and kept the upsampler loaded.

The fix reads the card's free VRAM from DRM sysfs, which the kernel keeps for every process and every API. The upsampler's memory probe then takes the lower of the two figures.

The measurement

torch.cuda.mem_get_info() vs /sys/class/drm/card*/device/mem_info_vram_*, inside unsloth/unsloth-rocm:v0.1.916-beta-studio on an RX 6700 XT (12 GB). The middle line was taken while Studio's bundled Vulkan llama-server held a 3B Q4_K_M model with -ngl 99:

  before: torch mem_get_info free 11802 of 12272 MiB | sysfs used 987 of 12272 MiB -> sysfs free 11284 MiB
llama up: torch mem_get_info free 11802 of 12272 MiB | sysfs used 3525 of 12272 MiB -> sysfs free 8746 MiB
   after: torch mem_get_info free 11802 of 12272 MiB | sysfs used 987 of 12272 MiB -> sysfs free 11284 MiB

Reproduction

FLUX.3 klein Q8_0 (unsloth/flux-3-image-klein-GGUF) on the sd.cpp engine, 1024×1024, 16 steps, guidance 1, Enhance prompt on. Run with the v0.1.916-beta image plus #16815 backported, so the failure carries sd-server's output.

Before: the upsampler starts with free 11806 MiB on cuda and no unloading after the call line follows. The job then fails:

[W] model manager memory on Vulkan0: reported free 3715.92 MB / total 12288.00 MB, ...
[W] model manager cannot make enough memory available on Vulkan0: need 6447.72 MB device / 5935.72 MB budget, available 3715.92 MB device / 10693.08 MB budget

The Images page shows "Image generation failed. The device ran out of memory." Calling POST /images/prompt-upsampler/release (which turning Enhance off also does) and generating again succeeds. So sd-server itself is fine; the resident upsampler is what holds the memory.

After:

flux3.prompt_upsampler: starting llama-server (flux-3-image-klein-pup-UD-Q4_K_XL.gguf, -ngl 99, free 11669 MiB on cuda)
flux3.prompt_upsampler: unloading after the call (free 5833 of 12272 MiB)

Both enhanced 1024×1024 renders then complete (prompt_enhancement: applied).

Changes

  • utils/hardware/hardware.py: new rocm_linux_kernel_free_mib(device=None).
    • It returns the card's free VRAM from mem_info_vram_used/total.
    • It finds the card through the PCI domain, bus and device that get_device_properties reports, matched against _rocm_linux_amdgpu_cards().
    • It returns None off Linux, without a HIP torch, when no card matches, or when the sysfs total differs from torch's by more than ~10%. That last guard is the same one _rocm_system_wide_vram_by_index uses, for APU carve-outs and partitioned cards. It never raises.
  • core/inference/flux3_prompt_upsampler.py: _accelerator_memory_mib uses min(HIP free, kernel free) on CUDA/ROCm devices.
    • The warm-start check, release_if_tight and plan_gpu_layers all read through it, so all three see the Vulkan users now.
    • On CUDA builds and off Linux the helper returns None, so nothing changes there.
  • Tests:
    • tests/test_rocm_kernel_free_vram.py: card selection by PCI address, scope mismatch / no match / broken props → None, Linux + HIP only.
    • test_flux3_prompt_upsampler.py::test_accelerator_memory_counts_what_the_hip_figure_misses.

Testing

  • pytest tests/test_rocm_kernel_free_vram.py tests/test_flux3_prompt_upsampler.py, run in the ROCm Studio venv (torch 2.11.0+rocm7.2) on main and on v0.1.916-beta: 216 passed, 11 failed.
    • The same 11 fail without this change in this ROCm test environment. Among them are the NVIDIA Vulkan-row cases and the CUDA_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES mask cases.
  • scripts/run_ruff_format.py and ruff check on the changed files: clean.
  • End to end on an RX 6700 XT: the before/after reproduction above.

Not changed here

diffusion_memory._cuda_memory, and so trusted_mem_get_info, which diffusers load plans read, have the same blind spot on Linux ROCm. A chat model on Vulkan llama-server, or the desktop, isn't counted when an image model plans its offload. Fixing that there would change every diffusion budget on Linux ROCm, so it's left for a separate change if you want it.


👾 AI Disclaimer: Claude Opus 5.5 has assisted with this Pull Request's description, Commit, and testing, at the prompting of a human.

…ng the server resident

On Linux ROCm, hipMemGetInfo counts only compute (KFD) allocations. A Vulkan
process (the upsampler's own llama-server and sd-server in the ROCm Docker
image) and the desktop are invisible to it: on an RX 6700 XT a 2.5 GiB Vulkan
llama-server left torch.cuda.mem_get_info() at 11802 MiB free while the DRM
sysfs counter rose by 2.5 GiB.

So after an enhanced prompt the upsampler (5.8 GiB) never read as tight and
stayed resident, and FLUX.3 klein on sd.cpp then failed at once: "model
manager cannot make enough memory available on Vulkan0: need 6447.72 MB
device ... available 3715.92 MB". Every later run failed the same way until
the keep-alive ran out or Enhance was turned off (which releases it).

utils.hardware.hardware.rocm_linux_kernel_free_mib reads the card's free VRAM
from DRM sysfs (found by the PCI address torch reports; only trusted when the
sysfs total matches torch's within ~10%), and the upsampler's memory probe
takes the lower of the two. Warm start, release_if_tight and the layer plan
all read through that probe. Off Linux ROCm nothing changes.

Co-Authored-By: Claude Opus 5.5 <[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:21:20.778073Z a39d11d 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: a39d11d931

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

@oobabooga

Copy link
Copy Markdown
Member

Thanks @hMelchisedek. On Linux ROCm, FLUX.3 klein renders with Enhance prompt on failed with an out-of-memory error, because the prompt upsampler never unloaded after writing its caption.

  • release_if_tight unloads it when VRAM is short, and asks torch how much is free.
  • On Linux ROCm that figure comes from hipMemGetInfo, which only counts memory taken through AMD's compute stack (HIP).
  • The upsampler's llama-server and sd-server both run on Vulkan, so their memory doesn't show up, and VRAM never looked short.

The check now also reads the card's free VRAM from DRM sysfs, which counts every process and every API, and uses the lower figure. From the PR's measurement on an RX 6700 XT:

torch free sysfs free
Before loading 11802 MiB 11284 MiB
Vulkan llama-server up 11802 MiB 8746 MiB

I pushed 78e5e37ae5: Radeon wheels from repo.radeon.com leave torch.version.hip unset, so the fix did nothing there. It now also checks for rocm in torch.__version__, like is_rocm_torch.

The MiniMax-H3-Image prompt enhancer uses the same server and gets the fix too. CUDA, Windows and macOS are unchanged.

The 4 new tests fail on main and pass here, and 280 upsampler, enhancer and probe tests pass on the PR merged with main. CI is clear at a39d11d931: 52 checks passed, and the 3 failures are unrelated:

diffusion_memory._cuda_memory and the mask segmenter's unload check still read only the HIP figure on Linux ROCm. That can be fixed separately.

@oobabooga
oobabooga merged commit 951338b into unslothai:main Nov 20, 2026
@hMelchisedek
hMelchisedek deleted the pr/studio-upsampler-rocm-vulkan-vram branch November 24, 2026 09:15
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.

3 participants