Skip to content

Studio sd.cpp: use BF16 cuBLAS for Q8_0 only when every GGUF the process loads is Q8_0 - #15054

Merged
danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-q8-cublas-scope
Oct 24, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-q8-cublas-scope

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #14983. Studio sets GGML_CUDA_QUANT_CUBLAS_MIN_BATCH for the sd.cpp child only when the model is all Q8_0, so large Q8_0 matmuls run as BF16 dequant + cuBLAS while k-quants stay on MMQ. The check read only the diffusion GGUF, but the variable is process-wide:

So a Q8_0 denoiser next to a k-quant text encoder (for example the default Qwen-Image-2.1 encoder) also moved the encoder's matmuls with 1024+ rows (4096 on sm120) off MMQ. That breaks the "k-quants stay on MMQ" rule and adds full-size BF16 dequant scratch for those weights.

Change

  • sd_cpp_args.model_files_quantized_weight_types(files) unions the quantized tensor types of the diffusion model and every companion that sd.cpp loads: VAE, audio VAE, clip_l, clip_g, t5xxl, llm, llm_vision, qwen2vl.
    • Companions that are not GGUF (safetensors) load dense and add nothing.
    • An unreadable companion GGUF returns None, and unknown keeps MMQ, the same as an unreadable diffusion GGUF today.
  • The sd.cpp load path passes this union instead of the diffusion-only types. The existing quant_cublas_env gate is unchanged: the override is still set only when every quantized type is Q8_0, every card sd.cpp may pick agrees, and the user has not set the variable.

Tests

  • test_a_q8_0_diffusion_gguf_with_a_k_quant_companion_keeps_mmq (backend load path) covers:
    • a Q8_0 diffusion GGUF with a Q4_K/Q6_K text encoder: no override
    • a Q8_0 TE with a Q5_K VAE: no override
    • an unreadable TE GGUF: no override
    • a Q8_0 TE with a dense GGUF VAE: override kept at 1024
  • test_gguf_quantized_weight_types_reads_the_tensor_table now writes real Q8_0, Q4_K and F16 GGUFs and checks the union and the resulting env.

On main, all five new cases fail: the k-quant TE cases get {'GGML_CUDA_QUANT_CUBLAS_MIN_BATCH': '1024'}, and the helper does not exist. With this change, tests/test_sd_cpp_args.py and tests/test_sd_cpp_backend.py pass (268 passed).

Notes

  • This rule is conservative. A k-quant TE pinned to CPU with --clip-on-cpu never reaches ggml-cuda, but it still turns the route off. A narrower rule would need the final placement flags, which the fallback ladder can change after the env is chosen.
  • LoRA files (--lora-model-dir) and the upscaler are not inspected. Their behaviour is the same as before this change.

GGML_CUDA_QUANT_CUBLAS_MIN_BATCH is read once per sd.cpp process and the pinned
ggml-cuda applies it to every quantized matmul. Checking only the diffusion GGUF
let a k-quant text encoder or VAE leave MMQ beside a Q8_0 denoiser. Union the
quantized tensor types of the diffusion model, VAE and text encoders; any
k-quant or unreadable companion GGUF keeps MMQ.
@danielhanchen danielhanchen self-assigned this Oct 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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 ✅ Completed 2026-10-24T00:53:13.026644Z f60ccaf 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. Chef's kiss.

Reviewed commit: f60ccaf436

ℹ️ 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 74b6995 into main Oct 24, 2026
41 of 42 checks passed
@danielhanchen
danielhanchen deleted the studio-sdcpp-q8-cublas-scope branch October 24, 2026 02:11
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 8, 2026
…F, not its companions (1.57x on A100) (unslothai#17098)

## 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 (unslothai#14983: faster on A100 / Hopper / datacenter
Blackwell, and closer to the unquantized render). Since unslothai#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

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

- A text prompt is far below 1024 rows, so the encoder keeps MMQ. The
encoder time above is unchanged (0.71 s both arms). On an earlier A100
run of the same Z-Image Q8_0 + Q4_K_M pair (fork master build), the env
render was pixel-identical to the fork's Q8_0-only default from
unslothai/stable-diffusion.cpp#39, which never moves a k-quant. So the
encoder took the same path either way.
- Where an encoder does reach the threshold (a large reference image
through Qwen2.5-VL in an edit), those k-quant matmuls take BF16 cuBLAS.
That was faster on these cards in unslothai#12527 (MiniMax-H3, k-quant DiT: 2.3x
on A100 and B200) and closer to an F32 reference there. The cost is a
BF16 copy of one weight at a time as scratch.

## 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
(unslothai/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

- MiniMax-H3 keeps its own setting (`h3_quant_cublas_env`, 1024 on every
sm80+ card, every type). It was measured faster with the route on L4 too
(unslothai#12527: 54.6 -> 44.3 s/step), so the image table is not applied to it.
- Timed on A100 only. H100 / B200 / RTX PRO 6000 take the same env value
they already got for an all-Q8_0 load (unslothai#14983), now also with a k-quant
encoder.
- The Qwen2.5-VL edit case (encoder rows above the threshold) was not
timed here.
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