Repository navigation
Studio sd.cpp: use BF16 cuBLAS for Q8_0 only when every GGUF the process loads is Q8_0 - #15054
Merged
Merged
Conversation
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.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #14983. Studio sets
GGML_CUDA_QUANT_CUBLAS_MIN_BATCHfor 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:scripts/unsloth/ggml-patches/0001-ggml-cuda-h3-speed.patch) reads it once per process and applies it to every quantizedsrc0that has a BF16 converter. Q4_K, Q5_K and Q6_K all have one.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.quant_cublas_envgate 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:test_gguf_quantized_weight_types_reads_the_tensor_tablenow 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.pyandtests/test_sd_cpp_backend.pypass (268 passed).Notes
--clip-on-cpunever 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-model-dir) and the upscaler are not inspected. Their behaviour is the same as before this change.