Skip to content

ggml : add packed int4 convrot mul_mat for CPU, CUDA, HIP and Vulkan - #10

Closed
noctrex wants to merge 1 commit into
leejet:sd.cppfrom
noctrex:convrot-w4-family
Closed

noctrex wants to merge 1 commit into
leejet:sd.cppfrom
noctrex:convrot-w4-family

Conversation

@noctrex

@noctrex noctrex commented Sep 20, 2026 •

Copy link
Copy Markdown

What and why

ComfyUI exports packed int4 convrot checkpoints in two formats, convrot_w4a4 (int4 weights, int4-class activations) and asym_w4a8_int8 (int4 codebook weights, int8 activations). This PR adds direct loading and execution of these checkpoints to the ggml backends: packed nibbles stream to the backend unconverted (roughly half a byte per weight parameter, no load-time reblock, no sidecar cache). The sd.cpp-side loader/graph integration follows as a separate PR once this lands.

The activation pipeline is reused verbatim from the existing int8 tensorwise convrot support: same GGML_OP_QUANTIZE_I8_CONVROT op, same packed scales-in-tail layout, same graph cache. Only the weight side of the MUL_MAT and the epilogue differ per kind:

  • int4 x int8 (convrot_w4a4): signed nibbles sign-extend exactly to int8, so the dot product stays on an exact integer grid. The XOR-8 bias trick moves the sign correction into a per-row activation byte sum (integer-pure, bitwise-identical results).
  • int4 codebook x int8 (asym_w4a8_int8): nibbles index a 16-entry per-module codebook with per-channel scales and per-group F8_E4M3 relative scales. The codebook is requantized to int8 at block start (amax/127, same expression order on all backends), so the group dot is pure dp4a/SDot with one FMA per group.

No new ggml op and no new ggml type: MUL_MAT with src0 = packed I8 weights and a kind selector in op_params slot 3 (slot 3 is zero-initialized, so 0 keeps int8 tensorwise chains unchanged).

Backends

  • CPU: nibble decode + integer/float dot kernels for both kinds.
  • CUDA/HIP: dp4a kernels in ggml-cuda/mmq-w4-convrot.cu. 32-row CTA tiles (weight decode shared by two 16-row tiles), 64-k stages, hoisted shared decode in [slot][output] layout, 4 outputs/thread, double-buffered stages, one barrier per stage. A gfx12 WMMA w4a8 variant is included, compile-verified only, runtime-gated.
  • Vulkan: SDot shaders for both kinds, same tile structure.
  • ggml-hip build: the -mcumode/-mwavefrontsize64 per-file tuning is guarded off for gfx10 targets (ROCm 10.0 clang segfaults in LLVM BranchRelaxation compiling the w4a4 kernel wave64 for gfx1030; gfx10 runs wave32 until the compiler bug is fixed).

The QUANTIZE_I8_CONVROT supports_op/compute_forward gates stay enabled on HIP: packed int4 chains require the activation quantize op on the GPU, and disabling it splits the graph so the mul_mat contract check loses the op marker. Only the plain-int8 mul_mat BLAS route remains CUDA-only here (that is the INT8 HIP/BLAS PR's scope, #6/#9).

Relationship to #6, #7, #8, #9

Deliberate identical-line overlaps so merges are clean in any order: the quantize-region guard flip and the prealloc_qconvrot buffer (#6/#8 territory), and the group-size-64 quantize content (templated CUDA kernels + GROUP_SIZE Vulkan shader variant, #7 territory). No int8 tensorwise behavior changes; the plain-int8 HIP/BLAS enablement stays with #6/#9.

Verification (results; test code stays local)

  • CPU: synthetic fixtures for both kinds against float oracles; W4A4 exact (bitwise), W4A8 within the int8-requant tolerance gate (2e-2 relative, observed residual ~0.7%).
  • HIP (gfx1100, ROCm 10): end to end at 1024x1024, 8 steps - FLUX.2-klein-4b int4 9.56 s sampling, w4a8 12.40 s; z_image-turbo int4 14.38 s (gs=256 regression check); zero unsupported-node fallbacks; images visually equivalent to the int8 baseline.
  • Vulkan (gfx1100): kernel probe (one-hot activation sweep) 32/32 exact on device; fixture gates pass (W4A4 bitwise, W4A8 in gate); end to end klein int4 11.9 s sampling - faster than the int8 baseline (12.3 s) on the same shapes.
  • RDNA2: dp4a kernels are generation-portable (V_DOT4); gfx1030 builds compile clean with the gfx10 wave32 guard.

LLM Disclose: YES, used GLM-5.3 for the grunt work

…lkan

W4A8: weight nibbles index a 16-entry codebook with per-group F8_E4M3
relative scales folded with per-channel scales; the codebook is
requantized to int8 in-CTA so the group dot is pure dp4a/SDot.
W4A4: symmetric int4 weights x int4 activations, integer-pure
accumulation with a byte-sum zero-point correction.
Both use a 32-row CTA tile (weight decode shared by two 16-row tiles)
on CUDA/HIP dp4a and Vulkan SDot; a gfx12 WMMA variant for w4a8 is
included (compile-verified, runtime-gated).
The ggml-hip build guards the -mcumode;-mwavefrontsize64 per-file
tuning behind !CMAKE_HIP_ARCHITECTURES MATCHES gfx10: ROCm 10.0 clang
segfaults (LLVM BranchRelaxation) compiling the w4a4 kernel wave64 for
gfx1030; gfx10 targets keep the compiler default (wave32) until the
compiler bug is fixed and the win is re-benched on RDNA2.
Includes the convrot group-size-64 quantize support (templated CUDA
kernels, GROUP_SIZE Vulkan shader variant) so w4 exports with 64-k
rotation groups run on GPU; that content overlaps the h64 PR
identically. The quantize-region guard flip and the prealloc_qconvrot
buffer likewise overlap the int8 HIP / f32-input PRs (identical
lines, merge clean in any order).
Standalone note: plain int8 tensorwise stays CUDA-only (the INT8
HIP/BLAS and RDNA2 PRs are separate); packed-w4 runs on HIP.
@noctrex

noctrex commented Nov 6, 2026

Copy link
Copy Markdown
Author

Closing as superseded - and first of all, thank you @leejet for porting this upstream and for the credit in #2276. It means a lot to see the format land in stable-diffusion.cpp, and the new backend extension API is a much better home for this work than a ggml-side patch ever was. The follow-up performance work is honestly impressive as well: CUTLASS S8xS4 for W4A4/W4A8 on SM80+ (#2278) and the Vulkan cooperative-matrix kernels (#2280) go well beyond anything this branch attempted.

For the record, the checkpoint contract carried over unchanged: packed I8 weights (low nibble first), 16-entry F32 codebook for asym_w4a8_int8, F32 per-channel scales, F8_E4M3 relative scales per 16 input features, mixed INT4/INT8 checkpoints, H64 and H256 groups, logical input width divisible by 64.

Since the convrot operator implementations have moved out of the ggml submodule into sd.cpp's src/core/{cpu,cuda,vulkan} behind the ggml-ext registry, this ggml-side diff no longer applies, so there is no point keeping the PR open.

Thanks again for absorbing this so thoughtfully. The branch stays on the fork as a historical record.

(Comment drafted with LLM assistance, reviewed by noctrex.)

@noctrex noctrex closed this Nov 6, 2026
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