Repository navigation
cuda: add TQ2_0 dequant + MMVQ support - #30278
coolinger2019-wq wants to merge 1 commit into
Conversation
TQ2_0 (type 35, group 256) is the ternary 2-bit format used by BitNet and
BitCPM models. Until now MUL_MAT with TQ2_0 weights fell back to the CPU
on the CUDA backend.
This adds:
- dequantize_tq2_0 + convert.cu / ggml-cuda.cu / common.cuh wiring so
TQ2_0 tensors are supported by the CUDA backend
- vec_dot_tq2_0_q8_1 MMVQ kernel: crumbs are extracted plane-wise and
mapped {-1,0,+1,+2} via __byte_perm (HIP path uses plain arithmetic),
then fed through the existing dp4a 8-chunk unrolled loop.
Verified on RTX 5060 Ti (sm_120), CUDA 13.1, upstream master 1623d8c:
- generation: 201 t/s on BitCPM4-3B (was CPU fallback)
- PPL (corpus.txt, c512): 6.4719 CUDA vs 6.4769 CPU path - no change
|
Hi @coolinger2019-wq, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Was looking for a similar bit of code to build on my machine and came across this PR and ran it. Fit what I needed for running ternary models on CUDA. I had Claude give me a few bullets on the numbers it was pulling and got these points Built from master 9e2a04a + this PR. RTX PRO 4000 Blackwell SFF (sm_120), CUDA 12.9, driver 595.
Maybe someone else (or an LLM) will come across this and find it of value. |
JohannesGaessler
left a comment
There was a problem hiding this comment.
Based on static code analysis the changes look correct to me. Since this is a draft: do you intend to add more changes?
Also be aware that according to the llama.cpp AI usage policy:
It is strictly prohibited to use AI to write your posts for you (bug reports, feature requests, pull request descriptions, Github discussions, responding to humans, ...).
What this does
Adds CUDA support for
GGML_TYPE_TQ2_0(upstream ternary type 35, group 256):a dequantize path plus the MMVQ dispatch, so ternary models (BitNet b1.58, BitCPM,
Falcon3-1.58bit, ...) can keep their weights on the GPU instead of falling back to
the CPU for every MUL_MAT.
Scope mirrors the minimal approach of #28769 (dequant + MMVQ only) rather than the
fuller #11183 (which also adds MMQ/MMA tiles). 6 files, 95 added lines, no deletions.
Changes
ggml/src/ggml-cuda/dequantize.cuhdequantize_tq2_0ggml/src/ggml-cuda/convert.cuto_fp16/fp32/bf16xcont/nc)ggml/src/ggml-cuda/common.cuhggml_cuda_type_traits<GGML_TYPE_TQ2_0>(qk=QK_K, qr=1, qi=QK_K/32)ggml/src/ggml-cuda/ggml-cuda.cusupports_opwhitelist entryggml/src/ggml-cuda/vecdotq.cuhVDR_TQ2_0_Q8_1_MMVQ,tq2_0_crumbs_to_int8,vec_dot_tq2_0_q8_1ggml/src/ggml-cuda/mmvq.cuLayout note (the only difference from Q2_0): Q2_0 packs 4 consecutive elements
per byte; TQ2_0 stores 4 bit planes of 32 bytes, so element
nsits at byte32*(n/128) + n%32with shift2*((n%128)/32). Same 2-bit code table({0,1,2,3} -> {-1,0,+1,+2}), one fp16 scale per 256 weights.
Measurements (RTX 5060 Ti 16 GB, CUDA 13.1, sm_120, base master 1623d8c)
llama-bench -p 512 -n 128:Prefill (
pp512): 5406 / 2590 / 6116 t/s respectively.(The BitNet number is limited by its small size, not the kernel; large ternary
models see ~10x since the CPU fallback dominated.)
Correctness
llama-perplexityon the same corpus:6.4719 (this PR, GPU) vs 6.4769 (CPU path) vs 6.4723 (PQ2_0 re-pack
reference) - all three paths agree within noise.
llama-cligreedy output is coherent and correct.tq2_0_crumbs_to_int8's__byte_permlookup validated againsta scalar reference on 20,000 random inputs (bit-exact).
A pitfall worth flagging for reviewers
My first working version passed PPL but produced garbage output. Cause: the
decode path (MMVQ) and the prefill/dequant path use different kernels - the
__byte_permselector intq2_0_crumbs_to_int8was wrong (x >> 8instead ofx >> 16), which corrupted only the MMVQ path. Both paths must be tested end toend; PPL alone is not sufficient. A repo-side op test for TQ2_0 MMVQ would catch
this class of bug - worth adding together with this change.
Notes
pieces from ggml-cuda : add TQ2_0 kernels, for ternary inference on GPU #11183 on top if maintainers want them.
1623d8c; builds clean with CUDA 13.1 and nomodifications outside the 6 files listed.