Skip to content

Studio sd.cpp: pick the Q8_0 BF16 cuBLAS route from the diffusion GGUF, not its companions (1.57x on A100) - #17098

Merged
danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-q8-cublas-diffusion-gguf
Nov 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-q8-cublas-diffusion-gguf

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

On CUDA, Studio sets GGML_CUDA_QUANT_CUBLAS_MIN_BATCH for the sd.cpp child when the model is Q8_0, so its large matmuls run as BF16 dequant + cuBLAS instead of int8 MMQ (#14983: faster on A100 / Hopper / datacenter Blackwell, and closer to the unquantized render). Since #15054 the check takes the union of the quantized types of the diffusion GGUF and every companion GGUF, so a Q8_0 DiT next to a k-quant encoder gets no env and the whole denoiser stays on MMQ. That pairing is common: the Qwen-Image families (text-to-image, edit, layered) load Qwen2.5-VL Q4_K_M as their encoder, UNSLOTH_SD_CPP_TEXT_ENCODER_QUANT=q4_k_m picks Qwen3-4B Q4_K_M, and a cache-only load falls back to an older cached Q4_K_M encoder.

A100 40 GB, Studio's pinned prebuilt (master-982-0518b34-uaba3a1c, Linux CUDA 13 bundle), Z-Image Turbo Q8_0 DiT + Qwen3-4B Q4_K_M encoder, 1024 px, 8 steps, cfg 1, seed 42, Studio's native argv (--diffusion-fa --diffusion-conv-direct), one warm-up per arm then 3 interleaved reps. Each arm gets the env its own tree computes from the same GGUF files (main: none; this branch: 1024):

sampling s, median [min, max] encoder s VAE s peak VRAM
main (MMQ) 8.02 [8.01, 8.06] 0.71-0.73 0.62 13148 MiB
this branch (BF16 cuBLAS) 5.10 [5.10, 5.30] 0.71 0.62 13148 MiB

1.57x faster sampling. Each arm renders the same pixels in every rep; the two arms differ, as expected when the denoiser changes matmul path.

Why the companion rule can go

#15054 added the union because the env is process-wide: with it set, an encoder's quantized matmuls with at least that many rows also take BF16 cuBLAS. In practice:

Change

  • The load path now decides from gguf_quantized_weight_types(files.diffusion_model) instead of the union, and model_files_quantized_weight_types (its only caller) is removed.
  • Unchanged:
    • the per-GPU table: sm80 / sm90 / sm100 1024, sm120 4096, Ada / consumer Ampere / Orin / older MMQ;
    • every card sd.cpp may pick must agree;
    • a mixed, k-quant, 4-bit, unquantized or unreadable diffusion GGUF keeps MMQ;
    • a user-exported value always wins, 0 included.
  • Once the pin carries the fork's Q8_0-only default (ggml-cuda: Q8_0 weights take dequant-to-BF16 + cuBLAS by default; k-quants stay on MMQ stable-diffusion.cpp#39, patch 0016), Studio can stop setting the env and leave a user value as the only override. The comment at QUANT_CUBLAS_ENV says so.

Cells

Each tree's own sd_cpp_args and load glue on real GGUFs (Z-Image Turbo Q8_0 / Q4_K_M DiT x Qwen3-4B Q4_K_M / Q8_0 / BF16 / safetensors encoder x sm75 / 80 / 86 / 89 / 90 / 100 / 120): 56 cells, 52 identical, 4 moved. The 4 moved cells are all Q8_0 DiT + Q4_K_M encoder on sm80 / sm90 / sm100 (none -> 1024) and sm120 (none -> 4096). L4 (sm89), consumer Ampere and T4 do not move.

Tests

tests/test_sd_cpp_backend.py: test_a_q8_0_diffusion_gguf_with_a_k_quant_companion_keeps_mmq becomes test_a_q8_0_diffusion_gguf_takes_the_route_whatever_its_companions_hold: a k-quant encoder, a Q8_0 encoder with a Q5_K VAE, an unreadable encoder, and a Qwen2.5-VL k-quant encoder with its projector all get 1024, and only the diffusion GGUF is read. On main all 4 cases fail; the existing k-quant / mixed / unknown diffusion GGUF cases still keep MMQ. tests/test_sd_cpp_args.py checks the tensor-table reader and the route on real tiny GGUFs. test_sd_cpp_args.py + test_sd_cpp_backend.py: 291 passed.

Not covered

…F, not its companions

A Q8_0 diffusion GGUF next to a k-quant text encoder (for example Qwen3-4B Q4_K_M) kept the whole
load on int8 MMQ. On an A100 that is 1.6x slower sampling for Z-Image Turbo at 1024 px. The route
is now decided from the diffusion GGUF's tensor types alone; the per-GPU table, the multi-card
agreement rule and a user-set value are unchanged.
@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 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-08T04:26:18.338824Z 6681554 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

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 6681554cc7

ℹ️ 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 that referenced this pull request Nov 8, 2026
…OS legs (#17107)

## Summary

Two test files on main assume a Linux host. Any PR whose cross-platform
run selects them goes red on the leg that is not Linux, whatever the PR
changed. Two current examples:

```
windows-latest (#17067)
FAILED test_diffusion_comfy_block.py::test_a_many_layer_nvfp4_fallback_load_that_fits_is_not_refused
FAILED test_diffusion_comfy_block.py::test_block_dequant_is_checked_for_its_peak_intermediates[nvfp4]
  ... and four more, all:
FileNotFoundError: [Errno 2] No such file or directory: '/proc/self/status'

macos-15 (#17098)
FAILED test_sd_cpp_args.py::test_native_speed_flags
  assert ['--diffusion-fa'] == ['--diffusion-fa', '--diffusion-conv-direct']
FAILED test_sd_cpp_args.py::test_minimax_h3_video_command_has_all_joint_av_components
  assert ['--offload-to-cpu', '--clip-on-cpu'] == ['--diffusion-fa', '--offload-to-cpu']
```

In both files the product code is right and the tests are wrong.

- **`test_diffusion_comfy_block.py`.** The six memory-peak tests already
skip with `needs /proc/self/status RssAnon` when `_rss_anon_bytes()`
returns `None`. But the helper opens `/proc/self/status` unguarded. Off
Linux it raises `FileNotFoundError` instead of returning `None`, so the
tests error out instead of skipping.
- **`test_sd_cpp_args.py`.** On Apple Metal, sd.cpp deliberately drops
`--diffusion-conv-direct` and adds `--clip-on-cpu` (#13369). Most tests
in the file pin `sys.platform` before they build a command. These two do
not, so on a Mac runner they get the Metal command and compare it
against the Linux one.

## Change

Tests only:

- `_rss_anon_bytes()` returns `None` when `/proc/self/status` cannot be
opened, so the existing skips fire on Windows and macOS. On Linux
nothing changes.
- The file's existing autouse fixture in `test_sd_cpp_args.py`, which
already pins the host probes, also pins `sys.platform` to `linux`. Every
test there now builds the same command on any host unless it sets the
platform itself, as the Metal tests already do. That covers the two
tests above and any later one that forgets the pin.

## Testing

| | result |
|---|---|
| `test_sd_cpp_args.py`, `test_sd_cpp_backend.py`,
`test_diffusion_comfy_block.py` on Linux, this branch | 345 passed, 5
skipped |
| main, Windows and macOS legs of #17067 and #17098 | the failures above
|
| this branch, `test_diffusion_comfy_block.py` + `test_sd_cpp_args.py`
on ubuntu-latest | 156 passed, 5 skipped |
| this branch, same files on macos-15 | 150 passed, 11 skipped (the six
RssAnon tests skip) |
| this branch, same files on windows-latest | 150 passed, 11 skipped
(the six RssAnon tests skip) |
@danielhanchen
danielhanchen merged commit ba22bb2 into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-sdcpp-q8-cublas-diffusion-gguf branch November 8, 2026 09:00
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