Skip to content

Studio GGUF: fuse Q8_0 dequant and cast into one kernel (LTX-2.5 Q8_0 denoise -12%, bit-identical) - #16884

Merged
danielhanchen merged 1 commit into
mainfrom
studio-gguf-q8-dequant-per-forward
Nov 6, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-gguf-q8-dequant-per-forward

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

GGUF Q8_0 denoisers (LTX-2.5 Q8_0 is the measured case) spent a large part of every forward dequantizing weights. Studio compiles diffusers' dequantize_gguf_tensor, but GGUFLinear.forward_native then casts the fp16 result to the compute dtype in a separate eager copy. For Q8_0 the compiled dequant itself also splits into three kernels, because d.view(float16) on a non-contiguous byte slice cannot fuse. That is 4 kernels per Q8_0 Linear per forward instead of 1.

This PR dequantizes and casts Q8_0 weights in one compiled kernel. The dequantized weights are bit-identical to the stock path, so frames do not change.

What / why / evidence

What. diffusion_gguf_compile.py:

  • _q8_0_blocks_dequant_cast views the contiguous Q8_0 bytes as int16 (scale) and int8 (codes) before slicing, so nothing is copied out. It computes d * q in the same order as diffusers, then casts.
  • fp16 rounding is kept. If inductor fused the product and the cast directly, it would round the fp32 product straight to bf16 and skip the fp16 rounding step. On random blocks about 25% of words then differ. An int16 bitcast plus & -1 forces the fp16 rounding inside the kernel; in eager mode it does nothing.
  • Install. install_compiled_dequant patches GGUFLinear.forward_native with the same gating as the compiled dequant. Only 2-D Q8_0 weights with a compute dtype take the fused path. Every other quant type (Q4_K and the rest) takes the stock forward unchanged.
  • Off switch. uninstall_compiled_dequant reverts the patch, and UNSLOTH_DIFFUSION_GGUF_FUSED_Q8_0=0 disables it.

Why. On the LTX-2.5 Q8_0 profile (G4, resident), the denoise window spent copy_cast 1.89 s + elementwise 0.79 s + triton 0.60 s per 5 forwards in weight dequant, next to 3.3 s of bf16 GEMMs.

Evidence. LTX-2.5 Q8_0 GGUF, 1280x704x121, two-stage distilled, whole card (RTX PRO 6000 Blackwell, Colab G4). Base = main b75a4cd, head = this branch, one VM, arm order S F F S, 2 new prompts per arm:

base S1 / S2 head F1 / F2
denoise s / clip (median) 11.98 / 11.97 10.58 / 10.57
step s (phase median) 0.616 / 0.616 0.488 / 0.485
steady s / clip 13.36 / 13.39 11.96 / 11.96
frames md5 (v0, v1) e91bd8c6b0, fdeea50dc9 e91bd8c6b0, fdeea50dc9
  • Speed. Denoise -11.7% and steady clip -10.5%. A/A spread is 0.1% on base and 0.0% on head.
  • Frames. Bit-identical between base and head: every frame matches, PSNR 99 dB, LPIPS 0.
  • Per-Linear microbench (A100, 5 interleaved repeats). Weight dequant + cast for 4096x4096: 0.345 ms to 0.124 ms. For 16384x4096: 1.270 ms to 0.205 ms. The head forward at M=3520 is within 3-10% of a plain bf16 GEMM.

Tests

New studio/backend/tests/test_diffusion_gguf_fused_q8_0.py. On base 15 of its 17 tests fail; on head all 17 pass.

  • Bit identity: words match diffusers' eager dequant + cast for real gguf Q8_0 blocks and for random bytes (fp16 overflow, subnormals, NaN). Covered for bf16 / fp16 / fp32 outputs, eager and compiled, with emulate_precision_casts off and on.
  • The bitcast is needed: a naive fusion is shown to differ.
  • Forward: the patched forward matches the stock forward bit for bit and never calls the stock dequant.
  • Other quant types: Q4_K still takes the stock forward.
  • Compile: torch._dynamo.explain gives 0 graph breaks and 1 graph; 4 layer shapes compile once each (dynamic).
  • Switches: uninstall and the env kill switch both work.

With test_diffusion_gguf_compile.py, test_diffusion_speed.py and test_diffusion_autoopt_parity.py, everything passes.

Not proven

  • Torch versions: only torch 2.12 (CPU) and 2.13 / 2.14 (GPU) were exercised. Not tested on the torch 2.6 floor.
  • Untouched paths: the max-tier block-compile path (the patch is not installed there) and DIFFUSERS_GGUF_CUDA_KERNELS=1 (forward_cuda).
  • Other quant types: Q4_K and the other K-quants keep the stock compiled dequant. That path already differs from eager in its fp16 rounding, so fusing them is a separate decision.
  • Other cards: only the G4 (Blackwell) and A100 were measured.

…ed kernel

GGUFLinear.forward_native ran the compiled dequantize_gguf_tensor (three Triton
kernels for Q8_0: a scale-slice copy and two mul kernels, fp16 out) and then an
eager .to(compute_dtype) copy, per linear per forward. Q8_0 weights now go
through one compiled kernel that reads the packed bytes and writes the
compute-dtype weight. An int16 bitcast keeps the fp16 rounding of scale * code
inside the fused kernel, so the weight is bit-identical to the stock path with
or without inductor's emulate_precision_casts. Other quant types keep the stock
forward. UNSLOTH_DIFFUSION_GGUF_FUSED_Q8_0=0 disables.
@danielhanchen danielhanchen self-assigned this Nov 6, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 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-11-06T07:54:44.618185Z 512d47d 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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 512d47d310

ℹ️ 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 a0197ca into main Nov 6, 2026
@danielhanchen
danielhanchen deleted the studio-gguf-q8-dequant-per-forward branch November 6, 2026 09:58
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 6, 2026
…i#16911)

Follow-up to unslothai#16884.

unslothai#16884 replaced `GGUFLinear.forward_native` for Q8_0 weights with one
compiled dequant+cast kernel. On torch 2.6 that kernel is lowered
wrongly by inductor: on an A100 with torch 2.6.0+cu124 (Triton 3.2.0),
24421 of 24576 weight elements differ from the stock dequant for real
`gguf`-quantized Q8_0 blocks, in bf16, fp16 and fp32 alike, with and
without `emulate_precision_casts`. Nothing raises, so the compile guard
never falls back and a Q8_0 GGUF render on torch 2.6 runs with corrupted
weights. On CPU, torch 2.6 cannot build the kernel at all
(`CppCompileError`, a float `&` int16 in the generated C++).

Studio still keeps existing installs on torch 2.4 to 2.14 (install.sh
preservation path, the `torch>=2.4` ROCm and gfx906 routes), so torch
2.6 is reachable.

This PR installs the fused forward only from torch 2.7. Below that,
every Q8_0 layer keeps the stock forward over the compiled dequant,
which is what ran before unslothai#16884. Nothing changes on torch 2.7 and later.

## Torch sweep (A100, same script, real Q8_0 blocks and random-byte
blocks, 12 cells per version)

| torch | Triton | fused == stock (cells) | stock compiled dequant ==
eager (cells) |
|---|---|---|---|
| 2.6.0+cu124 | 3.2.0 | 0 / 12 | 12 / 12 |
| 2.7.1+cu126 | 3.3.1 | 12 / 12 | 12 / 12 |
| 2.8.0+cu128 | 3.4.0 | 12 / 12 | 12 / 12 |
| 2.9.1+cu128 | 3.5.1 | 12 / 12 | 12 / 12 |
| 2.10.0+cu128 | 3.6.0 | 12 / 12 | 12 / 12 |
| 2.11.0+cu130 | 3.6.0 | 12 / 12 | 12 / 12 |
| 2.15.0+cu132 | 3.9.0 | 12 / 12 | 12 / 12 |

No graph breaks and one dynamic graph across four layer shapes on every
version. On gfx1151 (torch 2.11.0+rocm7.13.0) the fused kernel is also
bit-identical on real Q8_0 blocks, so ROCm on 2.7+ keeps it.

## Tests

- `test_fused_q8_0_torch_floor`: the version parse, including ROCm and
dev tags.
- `test_torch_below_the_floor_keeps_the_stock_forward`: with
`torch.__version__` set to 2.6.0 the dequant compile still installs, the
fused forward does not. Fails with the gate removed.
- The compile-dependent tests skip below the floor instead of failing.

`pytest studio/backend/tests/test_diffusion_gguf_fused_q8_0.py
studio/backend/tests/test_diffusion_gguf_compile.py`: 40 passed on torch
2.12.
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