Skip to content

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

Merged
danielhanchen merged 8 commits into
mainfrom
studio/h3-gguf-bf16-default
Oct 3, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
studio/h3-gguf-bf16-default

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

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 under speed_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 its ggml_cuda_init lines for the pinned card (CUDA<i>), or the lowest card when nothing is pinned. It reuses the --list-devices listing 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:
    • known sm80+ card: set in every speed mode (default, off, max);
    • known older card (sm75 and below), non-CUDA builds and the CPU fallback: launch unchanged;
    • capability unreadable: the previous rule (only the sage opt-in under speed_mode=max);
    • a value the user exported, 0 included, always wins and nothing is added.
  • The floor is sm80 because that is where the fork itself takes the route (bf16_mma_hardware_available); below it the env could change nothing.
  • UNSLOTH_H3_SAGE_ATTN=0 still 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).

GPU (arch) placement MMQ s/step BF16 s/step speedup MMQ render s BF16 render s peak VRAM MMQ / BF16
B200 (sm100) resident 7.15 3.16 2.26x 34.3 18.1 35.4 / 35.8 GiB
B200, 24 GiB left free streamed 7.28 3.25 2.24x 35.8 20.3 14.8-15.2 / 16.6 GiB
RTX PRO 6000 Blackwell (sm120) resident 6.01 4.67 1.29x 30.6 25.1 35.2 / 35.6 GiB
A100 40GB (sm80) resident 17.42 7.48 2.33x 81.4 41.5 35.1 / 35.4 GiB
L4 24GB (sm89) streamed 54.64 44.28 1.23x 255.8 214.1 15.2-16.5 / 15.9-16.8 GiB
T4 (sm75), 480x272x25 streamed route not taken: video and audio bit-identical between arms

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.

GPU MMQ vs F32 ref BF16 vs F32 ref
B200 37.91 dB / 0.044 / 13.7 dB 44.44 dB / 0.017 / 23.1 dB
RTX PRO 6000 40.19 dB / 0.032 / 15.6 dB 43.66 dB / 0.019 / 23.3 dB
A100 35.99 dB / 0.044 / 16.1 dB 41.70 dB / 0.021 / 23.0 dB

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.
  • Other H3 suites: the same 39 failures on the merge base and on this branch (identical test IDs; bitsandbytes CUDA setup and diffusers-path tests on this host), none touched by this change.

Not covered

  • sm86 (RTX 30xx, A10) and sm90 (H100) were not measured; they get the path because they sit inside the sm80+ range the fork serves and both neighbours (A100, L4) measured faster.
  • Windows, WSL and ROCm hosts were not run; ROCm/Vulkan/Metal builds are excluded by construction and covered by unit tests only.
  • No F32 reference on the L4 (BF16 vs MMQ only there), and the T4 run used a smaller workload (480x272, 25 frames) to keep it short, since it only has to show the route is not taken; its timings are a single warm render and not a result.
  • Measured with sd-server built from the fork's speed v3 head. The prebuilt Studio pins today (master-813-bfbef5b-u1d02858) predates the fork change that reads GGML_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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 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-03T13:59:29.843570Z 6393f1d Manual request
🔒 Security Review ⚠️ Failed 2026-10-02T11:16:40.117141Z e869629 PR opened
ℹ️ 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 Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review 🔄 Running since 2026-10-02T11:16:36.872034Z e869629 PR opened
ℹ️ 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.

@Etherll Etherll self-assigned this Oct 2, 2026
@Etherll

Etherll commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

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 c19e855 by driving the real load path against the sd-cli build Studio pins, on a B200.

The pinned build (master-813-bfbef5b-u1d02858) does not contain the switch yet. It only exists in the perf/h3-gguf-speed-v3 patches, so for now the env is ignored: a real 480x272, 25-frame, 4-step UD-Q3_K_XL render produced a byte-identical MP4 on 2eaf5b4 and on c19e855, at the same ~23 s. The speedup arrives with a pin bump.

Run Load Generate
base 2eaf5b4, run 1 81.9 s (cold model cache) 23.0 s
head c19e855, run 1 6.9 s 46.6 s (other GPU jobs on the host)
base 2eaf5b4, run 2 6.8 s 23.2 s
head c19e855, run 2 6.8 s 23.0 s

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 c19e855 (1067 passed).

Fork CI on c19e855: 33 checks pass, and the failures don't come from this PR. The Python 3.9 compatibility ratchet flags studio/backend/requirements/engines/engine_compat.py, which #11491 added on main, and plain main 08716f0 fails it the same way. Nine MLX tests in the unsloth-zoo suite (tests/test_mlx_generate_metal.py at zoo b0f78b5) fail because the zoo's own simulation stub has no mlx.nn.Sequential. This PR doesn't touch any of the code those tests import.

@danielhanchen

Copy link
Copy Markdown
Member Author

The Core zoo failures (tests/test_mlx_generate_metal.py NAX scope tests: set_dtype / mlx-shim: 'mlx.nn.Sequential') are unsloth-zoo main's, not this PR's: unslothai/unsloth-zoo#1541's new tests run against the mlx simulation when the whole zoo suite is collected. unslothai/unsloth-zoo#1547 gates them on real mlx; a re-run after it lands should clear them.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +481 to +482
if device_name is None:
return min(caps.values())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread studio/backend/core/inference/video.py Outdated
Comment on lines +3579 to +3581
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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6393f1d: the status names BF16 cuBLAS only on sm80+ (or an unread capability), since the fork ignores the env below that.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 6393f1d85e

ℹ️ 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 e640be3 into main Oct 3, 2026
@danielhanchen
danielhanchen deleted the studio/h3-gguf-bf16-default branch October 3, 2026 17:29
JoshuaL3000 pushed a commit to JoshuaL3000/unsloth that referenced this pull request Oct 4, 2026
…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>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants