Skip to content

Studio sd.cpp: image render failures carry sd-server's own output - #16815

Merged
danielhanchen merged 9 commits into
mainfrom
fix/moss-image-sd-cpp-small-vram
Nov 7, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
fix/moss-image-sd-cpp-small-vram

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

A Windows user on an 8 GB RTX 3070 could not render MOSS-Image-SFT Q8_0 GGUF. The load succeeded, sampling started, and about 16 seconds later the Images page said only "Image generation failed." The server log held no more than this:

sd-server generation failed: generation_failed: generate_image returned no results

When sd-server fails an image job, the job error is always that generic string. The real cause (a device allocation that failed, the model manager running out of room, an abort) only shows up in sd-server's own output. Studio already appended that output to failed video jobs. For image jobs it was dropped, so neither the user nor the exported log could say what went wrong.

Changes

  • sd_cpp_server.SdCppServer._run_job: a failed image job now includes sd-server's log tail in its message, the same way video jobs already did (_diagnostic_tail: error-marked lines first, then the last few lines, capped at 1500 characters).
  • routes/inference._generate_failure_detail: the Images page still shows only fixed text, never the engine's text.
    • The bare oom needle now matches only as a whole word. A log tail can echo the prompt (sd-server's verbose output prints it), and "a cozy room" is not an out-of-memory failure.
    • stable-diffusion.cpp's cannot make enough memory available now counts as out of memory. That warning can also appear before a render that recovers, but this function only ever sees renders that failed.
  • Side effect, intended: image job failures now reach note_accelerator_failure_from_output with the build's own output, as server deaths and video failures already did. A failed image job that prints, for example, no kernel image is available now counts against that build. Capacity failures stay excluded there, as before. In the same way, the existing one-time CPU-backend restart on a ggml unsupported-op abort and the Vulkan device-lost respawn now also see a failed job's output, not only a dead server's.

No change to any UI flow. A failure that used to say "Image generation failed." now says "Image generation failed. The device ran out of memory. Try a smaller size, fewer steps, or a smaller batch." when the output shows that.

Reproduction: VRAM taken after the load

Same Studio HTTP flow the Images page uses: load unsloth/MOSS-Image-SFT-GGUF Q8_0 with 7.5 GiB of VRAM free, then a second process takes VRAM until only 2 GiB is free, then generate 1024x1024 with 25 steps. Colab G4, the same tier for both arms.

main (04f4f0a) this branch
Images page Image generation failed. Image generation failed. The device ran out of memory. Try a smaller size, fewer steps, or a smaller batch.
server log sd-server generation failed: generation_failed: generate_image returned no results the same, plus the cause (below)
Last output:
[E] MOSS-Image segment 2/43 (moss_image.layers.1) failed during weight preparation --- ggml_runner.cpp:903
[E] diffusion model compute failed --- diffusion_engine.cpp:2825
[E] Diffusion model sampling failed --- diffusion_engine.cpp:3060
[E] sampling for image 1/1 failed after 0.18s --- image.cpp:906
...
[W] model manager cannot make enough memory available on CUDA0: need 1223.37 MB device / 759.59 MB budget, available 1149.19 MB device / 6012.04 MB budget --- model_manager.cpp:2101

The main arm's message matches the reporter's log exactly. I also tried a fix that restarts sd-server so it plans again from the memory that is free now. It did not help in this reproduction: a freshly started server failed on the same layer segment (1223 MB needed, 1187 MB available). With 3 or 4 GiB left, main already renders, because sd.cpp evicts weights on its own. So the restart is not part of this PR.

I also tried restarting with the text encoder on the CPU (--clip-on-cpu) and retrying. That retry ran, but the render still failed, this time on a DiT segment: at 2 GiB free, 1223 MB was needed and 1203 MB was free; at 1.5 GiB free, the prompt encoded on the CPU, and then a DiT segment needed 1711 MB with 1534 MB free. All it added was a restart before the same message, so it was dropped as well. At 1024x1024 these are real shortfalls, and the useful thing Studio can do is say so.

What was checked for MOSS-Image

The reported repos load and render with current main on every path I could reach:

Path Host Result
unsloth/MOSS-Image-SFT-GGUF Q8_0 through Studio HTTP (sd-server, CUDA 13 bundle) A100 with VRAM held down to 7.5 GiB free loads, renders 1024x1024 at 25 steps
same, with free VRAM then squeezed to 3 or 4 GiB A100 renders, main and branch
OpenMOSS-Team/MOSS-Image-SFT pipeline (fetches unsloth/MOSS-Image-SFT-FP8), auto precision A100, 7.5 GiB free int8 + streaming, renders
pinned sd-cli (master-982-0518b34-uaba3a1c and the previous -ud331c66), SFT keys and none L4, free VRAM 7.5 / 6 / 5 / 4 GiB, free RAM 10 / 6 GiB all 13 cells render
pinned Windows CPU build, sd-cli and sd-server with Studio's request shape, moss_uncond_skip=false and none GitHub windows-latest all 5 render
Studio local CPU, server path Linux renders

So #16255's sample args and the pinned build are not the cause. The reporter's failure is the same generic job failure as the reproduction above, and the reproduction shows how a desktop card that also drives the display can land there. Separately, unslothai/stable-diffusion.cpp#50 vendors leejet/stable-diffusion.cpp#2183. That fix makes the same model-manager loop read free memory again before it gives up, which matters when the driver reports freed memory late. It does not change a real shortfall like the one above.

Tests

  • test_sd_cpp_server.py::test_img_gen_failure_carries_the_log_cause: fails on main (no Last output:), passes here. test_img_gen_failure_without_output_keeps_the_job_error: the message stays unchanged when there is no output.
  • test_diffusion_routes.py: an image job OOM from the tail is named; the reproduction's capacity failure is named; a tail that echoes "room / broom / zoomed" is not called out of memory. None of these leak engine text.
  • test_differential.py against 04f4f0a: DISCRIMINATING, 3 tests fixed, 194 unchanged, 0 head failures.
  • Every test_sd_cpp*.py, test_h3_native*.py, test_diffusion_routes.py and test_diffusion_hires.py file passes. Adding test_video_backend.py to the same run gives the same 165 order-dependent failures on main and on this branch; that file passes on its own on both.

Not covered

  • No Windows + NVIDIA host was available, so the report itself was not replayed. Its log will name its cause once it comes from this build.

Screenshots

Images page on Colab A100. unsloth/MOSS-Image-SFT-GGUF Q8_0 was loaded with 7.5 GiB of VRAM free, then a second process took VRAM until 2 GiB was left, then Generate was clicked at 1024x1024 with 25 steps. The left side is main (04f4f0a); the right side is this branch. Both captures come from a real browser.

Images page error toast, before and after

sd-server reports a failed image job only as "generate_image returned no results"; the cause is
in its log. Image job failures now carry that log tail, as video ones already did, so the Images
page can name the failure class (out of memory) and the server log records the cause. The route's
bare "oom" needle matches only as a word, since a tail can echo the prompt, and stable-diffusion.cpp's
"cannot make enough memory available" counts as out of memory.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-07T08:07:23.454015Z 4d8c349 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 danielhanchen self-assigned this Nov 5, 2026
…t-of-memory class

Completed renders print it too, so its presence in a failed render's tail does not say the
failure was memory; the backend's capacity markers exclude it for the same reason.

[skip ci]
… of device memory

sd.cpp plans weight placement and graph segments from the VRAM free when sd-server starts. When
something else takes VRAM after the load, a render fails part way with "cannot make enough memory
available" / "failed during weight preparation" (reported only as "generate_image returned no
results") and every later render on that server fails the same way. Now that the job error
carries that output, restart the server on the same build and flags once per load, so it plans
again from the memory free now, and retry the chunk.

[skip ci]
…ty shortfall as out of memory

On Colab with free VRAM squeezed after the load, main fails a MOSS-Image Q8_0 render only when a
single DiT layer segment no longer fits (need 1223 MB, 1187 MB available); a freshly planned
server fails the same segment, and with 3 or 4 GiB left main already renders. The restart had no
case it fixed, so it goes. The Images page now names the model manager's 'cannot make enough
memory available' as out of memory, only ever read from a render that failed.

[skip ci]
…xt encoder on the CPU and retry once

When something takes VRAM after a MOSS-Image GGUF load, the render fails part way with
"cannot make enough memory available" / "failed during weight preparation" and every later
render on that server fails the same way. Restart the server once per load with --clip-on-cpu
(a device pin is rewritten to te=cpu), so the 8 GB Qwen3.5-9B encoder leaves the card to the DiT,
and retry the chunk. Only after a failed render whose output shows a device memory shortfall.
On Colab with VRAM taken after the load, the retry ran but every render still failed on a DiT
segment (1223 MB needed, 1203 MB free at 2 GiB; 1711 MB needed, 1534 MB free at 1.5 GiB), so it
only added a restart before the same out-of-memory message.

This reverts commit 6c9e5d7.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

The server keeps one rolling log for its lifetime, so a failed image or video job also carried
earlier jobs' error lines. After an out-of-memory render, a later failure for another reason
was reported as out of memory too. Mark the log at submit and attach only lines printed after it.

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 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. Hooray!

Reviewed commit: 4d8c349853

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
danielhanchen added a commit that referenced this pull request Nov 7, 2026
… fp8 to be admitted (#16993)

## Summary

The Windows cross-platform leg fails four tests in
`studio/backend/tests/test_diffusion_routes.py` whenever a PR's
selection includes that file (seen today on #16815, which only changes
sd.cpp error output):

```
FAILED test_diffusion_routes.py::test_a_pipeline_pick_may_pin_a_precision
FAILED test_diffusion_routes.py::test_a_transformer_family_still_reaches_the_scheme_check
FAILED test_diffusion_routes.py::test_a_whole_module_offload_request_admits_a_pipeline_precision[memory0]
FAILED test_diffusion_routes.py::test_a_whole_module_offload_request_admits_a_pipeline_precision[memory1]
RuntimeError: transformer_quant='fp8' could not be used: this process cannot run a torch.compile (no Triton, TORCHDYNAMO_DISABLE, or a family/device that does not compile), ...
```

These tests check that the precision gate admits a pipeline fp8 request.
The gate also refuses fp8 when `compile_eligible()` says the process
cannot compile, because an uncompiled torchao transformer runs far
slower than the bf16 it replaces. That refusal is correct and has its
own test,
`test_a_process_that_cannot_compile_refuses_a_pipeline_precision_up_front`,
which pins `compile_eligible` to `False`. The admitting tests never
pinned it the other way, so they only pass on a host that can actually
compile. The Windows runner has no Triton, so the gate refuses there and
the tests fail.

This is not new: running these tests with `TORCHDYNAMO_DISABLE=1` fails
the same four on main and on main as of 2026-10-01. It only shows up
when a cross-platform Windows leg selects this file.

## Change

`studio/backend/tests/test_diffusion_routes.py`, tests only:
`_pipeline_precision_gate`, `test_a_pipeline_pick_may_pin_a_precision`
and `test_a_transformer_family_still_reaches_the_scheme_check` pin
`compile_eligible` to `True`, next to the `dense_transformer_supported`
pin they already set. Product code is unchanged, and the refusal test
keeps pinning `False`.

## Testing

`tests/test_diffusion_routes.py` in full, CPU torch 2.10:

| | compile available | `TORCHDYNAMO_DISABLE=1` (as on a host without
Triton) |
|---|---|---|
| main | 160 passed | 4 failed (the four above), 156 passed |
| this branch | 160 passed | 160 passed |
@danielhanchen
danielhanchen merged commit 25b53fb into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the fix/moss-image-sd-cpp-small-vram branch November 7, 2026 08:52
oobabooga added a commit that referenced this pull request Nov 20, 2026
…ng the server resident (#17159)

## 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`:

```text
  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:

```text
[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:**

```text
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.*

---------

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

1 participant