Repository navigation
Studio GGUF: fuse Q8_0 dequant and cast into one kernel (LTX-2.5 Q8_0 denoise -12%, bit-identical) - #16884
Merged
Conversation
…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.
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. |
Member
Author
|
@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". |
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.
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
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, butGGUFLinear.forward_nativethen 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, becaused.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_castviews the contiguous Q8_0 bytes as int16 (scale) and int8 (codes) before slicing, so nothing is copied out. It computesd * qin the same order as diffusers, then casts.& -1forces the fp16 rounding inside the kernel; in eager mode it does nothing.install_compiled_dequantpatchesGGUFLinear.forward_nativewith 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.uninstall_compiled_dequantreverts the patch, andUNSLOTH_DIFFUSION_GGUF_FUSED_Q8_0=0disables 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:
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.ggufQ8_0 blocks and for random bytes (fp16 overflow, subnormals, NaN). Covered for bf16 / fp16 / fp32 outputs, eager and compiled, withemulate_precision_castsoff and on.torch._dynamo.explaingives 0 graph breaks and 1 graph; 4 layer shapes compile once each (dynamic).With
test_diffusion_gguf_compile.py,test_diffusion_speed.pyandtest_diffusion_autoopt_parity.py, everything passes.Not proven
DIFFUSERS_GGUF_CUDA_KERNELS=1(forward_cuda).