Repository navigation
Studio: MiniMax-H3 GGUF uses the BF16 cuBLAS matmul path by default on sm80+ (1.2 to 2.3x per step, closer to an F32 reference) - #12527
Conversation
…on sm80+ The Unsloth sd.cpp fork can run large quantized DiT matmuls as a BF16 cuBLAS GEMM over a dequantized weight instead of int8 MMQ (GGML_CUDA_QUANT_CUBLAS_MIN_BATCH=1024). Studio only set it under speed_mode=max, tied to the sage opt-in. Measured at default speed (no sage) it is faster on every sm80+ card tried (B200, RTX PRO 6000, A100, L4) and closer to an F32-dequant reference than MMQ, so it is now the default for every native H3 GPU load on those cards. - sd_cpp_cuda_compute_capability reads the capability the CUDA build itself reports in its ggml_cuda_init lines for the pinned card (lowest card when none is pinned), reusing the --list-devices listing the accelerator re-check already took, so the load spawns no extra subprocess. - h3_quant_cublas_env: known sm80+ -> set in every speed mode; known older card, ROCm/Vulkan/Metal builds and the CPU fallback -> unchanged; unknown capability keeps the old rule (only the sage opt-in). A user-exported value, 0 included, always wins. - The sage veto (UNSLOTH_H3_SAGE_ATTN=0) no longer drops the matmul path on a known sm80+ card; they are separate levers now.
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 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. |
|
On an sm80+ CUDA card, a native MiniMax-H3 load now hands sd-cli GGML_CUDA_QUANT_CUBLAS_MIN_BATCH=1024 in every speed mode, not just max. CPU-only builds get nothing, and a value the user already exported (0 included) is left alone. This was checked on The pinned build (
All four MP4s hash the same, so the shipped build ran identical kernels on both commits. Two things to look at for that bump. The speed_mode description says "default off -> bit-identical output", but once the pin moves, off and unset H3 renders on sm80+ will also take the BF16 path. The BF16 scratch is also not counted in the H3 VRAM estimate or the --max-vram budget, and 16 GiB cards under streaming weren't tested. The six suites named in the PR pass on Fork CI on |
|
The |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa73ac7a4a
ℹ️ 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".
| if device_name is None: | ||
| return min(caps.values()) |
There was a problem hiding this comment.
Use CUDA0's capability when no device is pinned
On an unpinned mixed-generation host, sd.cpp uses its default CUDA device (ordinal 0), but this returns the minimum capability across every installed card. For example, with an sm89 CUDA0 and an unused sm75 CUDA1, the load runs on CUDA0 while h3_quant_cublas_env sees (7, 5) and disables the new BF16 cuBLAS default, forfeiting the intended speedup. Read caps[0] (or the build's actual default device) instead of the lowest capability.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Leaving this as is. Only an unpinned mixed-generation host is affected, and there the load keeps today's MMQ path, so nothing regresses. Studio pins the selected card whenever one is chosen, and the lowest card is the conservative answer when sd.cpp picks a device itself.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa73ac7a4a
ℹ️ 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".
| h3_quant_cublas = native_device != "cpu" and dict(native_env).get( | ||
| H3_QUANT_CUBLAS_ENV, os.environ.get(H3_QUANT_CUBLAS_ENV, "") | ||
| ).strip() not in ("", "0") |
There was a problem hiding this comment.
Gate the BF16 status on actual CUDA support
When a user exports a nonzero GGML_CUDA_QUANT_CUBLAS_MIN_BATCH on a known pre-sm80 card or a non-CUDA backend, this expression marks BF16 cuBLAS as active and the resolved status reports sd.cpp BF16 cuBLAS matmuls, even though the new capability gate documents that the fork cannot take that route there. This can mislabel benchmark and experiment results; the status should also require a compatible CUDA capability rather than infer execution solely from the inherited environment value.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6393f1d: the status names BF16 cuBLAS only on sm80+ (or an unread capability), since the fork ignores the env below that.
|
@codex review |
|
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". |
…n sm80+ (1.2 to 2.3x per step, closer to an F32 reference) (unslothai#12527) * Studio: MiniMax-H3 GGUF takes the BF16 cuBLAS matmul path by default on sm80+ The Unsloth sd.cpp fork can run large quantized DiT matmuls as a BF16 cuBLAS GEMM over a dequantized weight instead of int8 MMQ (GGML_CUDA_QUANT_CUBLAS_MIN_BATCH=1024). Studio only set it under speed_mode=max, tied to the sage opt-in. Measured at default speed (no sage) it is faster on every sm80+ card tried (B200, RTX PRO 6000, A100, L4) and closer to an F32-dequant reference than MMQ, so it is now the default for every native H3 GPU load on those cards. - sd_cpp_cuda_compute_capability reads the capability the CUDA build itself reports in its ggml_cuda_init lines for the pinned card (lowest card when none is pinned), reusing the --list-devices listing the accelerator re-check already took, so the load spawns no extra subprocess. - h3_quant_cublas_env: known sm80+ -> set in every speed mode; known older card, ROCm/Vulkan/Metal builds and the CPU fallback -> unchanged; unknown capability keeps the old rule (only the sage opt-in). A user-exported value, 0 included, always wins. - The sage veto (UNSLOTH_H3_SAGE_ATTN=0) no longer drops the matmul path on a known sm80+ card; they are separate levers now. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: H3 status names the BF16 cuBLAS route when sd-cli gets it * Studio: tighten H3 BF16 cuBLAS comments * Studio: H3 status reports BF16 cuBLAS only where the fork can take it * Studio: drop a redundant H3 comment --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…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.
Problem
The Unsloth sd.cpp fork can run the large quantized matmuls of the MiniMax-H3 DiT as a BF16 cuBLAS GEMM over a dequantized weight instead of int8 MMQ (
GGML_CUDA_QUANT_CUBLAS_MIN_BATCH=1024). Studio only handed sd-cli that env underspeed_mode=max, bundled with the SageAttention opt-in. Every other GPU load ran MMQ, which on the cards below is both slower and further from a higher-precision reference.Change
sd_cpp_cuda_compute_capability(sd_cpp_backend.py) reads the compute capability the CUDA build itself prints in itsggml_cuda_initlines for the pinned card (CUDA<i>), or the lowest card when nothing is pinned. It reuses the--list-deviceslisting the accelerator re-check already takes under the tree claim, so a load spawns no extra subprocess. ROCm (found N ROCm devices), Vulkan, Metal and CPU builds return None.h3_quant_cublas_env(video_minimax_h3.py) decides the env for a native H3 GPU load:speed_mode=max);0included, always wins and nothing is added.bf16_mma_hardware_available); below it the env could change nothing.UNSLOTH_H3_SAGE_ATTN=0still drops sage, but on a known sm80+ card it no longer drops the matmul path: they are separate levers now.Results
Default speed mode (no sage), 960x544, 124 frames, 4 steps, seed 42, UD-Q3_K_XL DiT + Q4_K_M text encoder, fork build at the head of the speed v3 branch. Base arm = MMQ, which is what Studio runs today at default speed. One session per card, arms alternate in separate server processes (MMQ, BF16, MMQ, BF16), render 0 of each process is the warm-up and is excluded. s/step = warm sampling time / 4. Offload follows Studio's auto rule: resident when free VRAM covers the estimate, else
--offload-to-cpu --max-vram -1(graph-cut streaming).Warm s/step spread inside each arm was under 0.3% on every card (B200 n=4, RTX PRO 6000 n=4, A100 n=2, L4 n=2), far below the deltas. Resident peak grows by about 0.37 GiB (the BF16 copy of the largest weight). Under streaming the peak moves more (about +1.4 GiB on the B200 with 24 GiB left free, within the run-to-run spread on the L4) and stays well inside the budget: no OOM on either the ballasted B200 or the 22.5 GiB L4.
Accuracy
Reference = the same route dequantizing to F32 instead of BF16 (
GGML_CUDA_QUANT_CUBLAS_TYPE=f32), same session, warm render. Per-frame PSNR mean, LPIPS (alex) mean, audio SNR.BF16 vs MMQ directly: B200 38.5 dB / 0.040, RTX PRO 6000 40.6 dB / 0.031, A100 37.5 dB / 0.039, L4 37.8 dB / 0.040. Both arms are deterministic: repeated renders in one process and the two MMQ processes are bit-identical, so every difference above is the matmul path.
Tests
tests/test_video_backend.py: the existing max-mode test now pins the unreadable-capability fallback; new tests cover sm80/86/89/90/100/120 taking the path in every speed mode (sage veto included), sm75/70/61 never taking it, a user value (0,1024,4096) always winning, the pinned card being read on a mixed host (lowest card with no pin), and ROCm, Vulkan, Metal and CPU builds never getting the env.tests/test_video_backend.py,test_sd_cpp_h3_matrix.py(probe count unchanged),test_sd_cpp_backend.py,test_sd_cpp_rocm_vulkan_fallback.py,test_sd_cpp_off_torch_device.py,test_h3_native_resident.py: 1062 passed.Not covered
master-813-bfbef5b-u1d02858) predates the fork change that readsGGML_CUDA_QUANT_CUBLAS_MIN_BATCH, so it ignores the env: this PR has no effect until the pin moves to a fork release that includes it. Nothing breaks in the meantime.