Skip to content

cuda: add TQ2_0 dequant + MMVQ support - #30278

Draft
coolinger2019-wq wants to merge 1 commit into
ggml-org:masterfrom
coolinger2019-wq:p4-tq2-0-cuda
Draft

coolinger2019-wq wants to merge 1 commit into
ggml-org:masterfrom
coolinger2019-wq:p4-tq2-0-cuda

Conversation

@coolinger2019-wq

Copy link
Copy Markdown

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

File Change
ggml/src/ggml-cuda/dequantize.cuh dequantize_tq2_0
ggml/src/ggml-cuda/convert.cu 6 dispatch cases (to_fp16/fp32/bf16 x cont/nc)
ggml/src/ggml-cuda/common.cuh ggml_cuda_type_traits<GGML_TYPE_TQ2_0> (qk=QK_K, qr=1, qi=QK_K/32)
ggml/src/ggml-cuda/ggml-cuda.cu 1 supports_op whitelist entry
ggml/src/ggml-cuda/vecdotq.cuh VDR_TQ2_0_Q8_1_MMVQ, tq2_0_crumbs_to_int8, vec_dot_tq2_0_q8_1
ggml/src/ggml-cuda/mmvq.cu 3 dispatch cases

Layout 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 n sits at byte
32*(n/128) + n%32 with shift 2*((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:

Model Before (CPU fallback) After (this PR) Speedup
BitCPM-CANN-3B (3.61 B) 21.6 t/s 216.1 t/s 10.0x
BitCPM-CANN-8B (8.19 B) 15.0 137.7 9.2x
BitNet-b1.58-2B-4T (2.41 B) 22.9 45.2 2.0x

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

  • PPL unchanged: BitCPM-3B, llama-perplexity on 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.
  • Generation: llama-cli greedy output is coherent and correct.
  • Unit check: tq2_0_crumbs_to_int8's __byte_perm lookup validated against
    a 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_perm selector in tq2_0_crumbs_to_int8 was wrong (x >> 8 instead of
x >> 16), which corrupted only the MMVQ path. Both paths must be tested end to
end; 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

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
@coolinger2019-wq
coolinger2019-wq requested a review from a team as a code owner October 10, 2026 09:31
@ggml-gh-bot

ggml-gh-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown

Hi @coolinger2019-wq, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Oct 10, 2026
@github-actions
github-actions Bot marked this pull request as draft October 10, 2026 10:40
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend and removed draft PR will be changed to draft by github-actions bot labels Oct 10, 2026
@vveerrgg

vveerrgg commented Nov 4, 2026

Copy link
Copy Markdown

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.
Model: Qwen3-0.6B, which I ternarized, saved as F16 and quantized with stock llama-quantize TQ2_0.

  • test-backend-ops -b CUDA0 -p tq2_0: MUL_MAT 48/48. MUL_MAT_ID and GET_ROWS OK.
  • Perplexity (c=512, 8 chunks): GPU and CPU match, 11.58M vs 11.61M. The number is huge because the model is untrained ternary; the point is that the two paths agree.
  • llama-bench tg128: 449 t/s on GPU, vs 152 with the current CPU fallback and 259 for F16 on GPU. pp512: 26.4k vs 29.9k for F16.

Maybe someone else (or an LLM) will come across this and find it of value.

@JohannesGaessler JohannesGaessler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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, ...).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants