Skip to content

ggml: TQ3_4S weight type and CPU reference (3.5 bpw) - #418

Merged
TheTom merged 3 commits into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq3_4s-type
Nov 22, 2026
Merged

TheTom merged 3 commits into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/tq3_4s-type

Conversation

@jasstrong

@jasstrong jasstrong commented Nov 11, 2026 •

Copy link
Copy Markdown

Split out of #416 as asked in its review: the TQ3_4S type and its CPU reference, with no GPU kernels. The CUDA/HIP kernels stay in #416, which is stacked on this PR. Rebased onto 50b4aa28b.

The test-backend-ops cases for TQ3_4S are in #416 (the all_types entry and the MUL_MAT / MUL_MAT_ID cases), so nothing in this PR runs the supports_op rejections. The two are meant to merge back to back.

What

GGML_TYPE_TQ3_4S (id 53; 51 and 52 are TURBO5_0 and TURBO6_0) is TQ3_1S with cheaper scales:

  • A block holds 128 values as four TQ3_1S sub-blocks.
  • The fp16 field stores the largest of the eight 16-value half-block scales, and each half-block keeps its own scale as a 6-bit fraction of it: d * q / 63.
  • 2 + 6 + 48 = 56 bytes per 128 values, so 3.5 bpw against TQ3_1S's 4.0.
  • The 3-bit index packing is the TQ3_1S packing.
  • The quantizer fits each 16-value half-block scale on its own, where TQ3_1S searches one grid multiplier for both halves of a block. That lowers the weight error by about 7%.

This PR adds:

  • CPU: quantize, dequantize and a chunked vec_dot against q8_0.
  • llama-quantize: file type TQ3_4S (47). A row whose length is not a multiple of 128 falls back to TQ3_1S.
  • Metal and SYCL: supports_op now returns false for TQ3_4S in MUL_MAT, MUL_MAT_ID and GET_ROWS, so those ops run on the CPU. Both returned true for any src0 type before, so a TQ3_4S file would have aborted at the first decode. CUDA, Vulkan, OpenCL, CANN and WebGPU list their types explicitly and already reject it.
  • AGENTS.md and README: a TQ3_4S row in the fork-only type tables (id 53, backends CPU), the type range 43-53, and a TQ3_4S row in the README backend matrix.
  • llama_ftype_name: names for TQ3_1S, TQ4_1S and TQ3_4S, which printed "unknown, may not work".

Scale storage changed from #416's first version. #416 stored dmax / 63 in the fp16, which underflows for small rows, as found in its review. This version stores dmax. No file in the first format has been published.

Testing

🤖 Generated with Claude Code

jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
TheTom#418 is the type and CPU part split out of this PR, so this branch already
holds its content. The one conflict, the AGENTS.md TQ3_4S sentence, keeps
this branch's wording (CPU and CUDA/HIP kernels) over TheTom#418's (CPU
reference only). No other file changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 21, 2026
superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 21, 2026
The CUDA/HIP half of TheTom#416. The type and its CPU reference are in TheTom#418,
already merged here; this adds the GPU side: convert.cu, dequantize.cuh,
getrows.cu, the CDNA MMQ config entries, the TQ3_4S tile loader and the
MMQ dispatch, plus the generated instance file.
superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 21, 2026
The rest of TheTom#416's CUDA side: the dense decode dispatch, the dp4a and
scalar MoE kernels templated on the block type, the MMQ dispatch and the
test-backend-ops cases. The type and its CPU reference are in TheTom#418.
@TheTom

TheTom commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thanks @jasstrong for carving the type and CPU reference out of #416. This is an ancestor of the #416 head and the codec is byte for byte the same in both, so landing this first is fine.

I read the type wiring against TQ3_1S and every backend either rejects TQ3_4S in supports_op or goes through to_float. The GB10 run in #416 (CUDA 13, sm_121) includes this head: test-turbo-quant and test-quantize-fns pass.

Left before I merge:

Small things:

  • tests/test-turbo-quant.c:36: roundtrip_rel_rmse sits between the block-boundary doc comment and check_chunked_dequant, so that comment now describes the wrong function.
  • src/llama-quant.cpp:438: the comment says a fallback row "keeps the same indices". TQ3_1S runs its own scale search, so the indices can differ. Same layout, per-block scales.
  • ggml/src/ggml-turbo-quant.c:1213: the 7% figure belongs in the description.
  • llama_ftype_name in src/llama-model-loader.cpp has no TQ3_4S case, so a load prints "unknown, may not work". TQ3_1S and TQ4_1S are missing there too, so all three could go in together.

@superbudvar

Copy link
Copy Markdown

#403 has merged, so this branch's base moved again (the fork tip is now 50b4aa28b).

I rebased this branch onto the new base; the result is code-identical to the current head and merges cleanly into the base:

git fetch https://github.com/superbudvar/llama-cpp-turboquant.git pr/tq3_4s-type
git push origin FETCH_HEAD:pr/tq3_4s-type --force-with-lease

head: 8240a50e4

The only conflict was AGENTS.md: #371 rewrote the type table and the knob table, so the TQ3_4S row had to be re-added in the new format (the Backends column, and the type count going from ten to eleven).

The same rebase exists for the rest of the stack: pr/tq3_1s-mmq-amd (b42c60dd5, #413), pr/tq-dp4a-decode (940d1cd2d, #415) and pr/tq3_4s (f64e86ac84, #416), all on my fork.

jas and others added 3 commits November 21, 2026 23:17
A TQ3_4S block holds 128 values as four TQ3_1S sub-blocks. The fp16 field
stores the largest of their eight 16-value half-block scales, and each half
keeps its own scale as a 6-bit fraction of it (d * q / 63), so a block is 56
bytes where four TQ3_1S blocks are 64: 3.5 bpw against 4.0. Storing the
largest scale itself, not a 1/63 step, keeps the fp16 range: rows down to
sigma 1e-6 round-trip at the same error as larger ones. The 3-bit index
packing is the TQ3_1S packing.

The quantizer fits each half-block scale separately, rounds it to the 6-bit
grid and then re-picks the indices against the stored scales.

The type id is 53: 51 and 52 are TURBO5_0 and TURBO6_0.

This commit is the type and the CPU reference: quantize, dequantize and a
chunked vec_dot. Metal and SYCL report MUL_MAT, MUL_MAT_ID and GET_ROWS as
unsupported for it, so a TQ3_4S file runs those on the CPU there instead of
aborting; the other GPU backends already reject unknown types. The CUDA/HIP
kernels follow separately.

Tests: test-quantize-fns covers TQ3_4S at the 3-bit thresholds;
test-turbo-quant adds it to the chunked-dequant check (Test 4) and adds a
small-magnitude round-trip check (Test 5) for TQ3_1S and TQ3_4S.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
LLAMA_FTYPE_MOSTLY_TQ3_4S (47), mapped to GGML_TYPE_TQ3_4S like the TQ3_1S file
type, plus the llama-quantize entry, the loader's file-type guess and the
gguf-py enum. A row whose length is not a multiple of 128 cannot hold whole
super-blocks; it falls back to TQ3_1S, which is the same indices with per-block
scales.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- tests/test-turbo-quant.c: keep the round-trip error sums in double
  without promoting a float operand (-Wdouble-promotion under gcc 13), and
  move roundtrip_rel_rmse above the block-boundary comment, which
  describes check_chunked_dequant.
- src/llama-quant.cpp: the TQ3_1S fallback for partial super-blocks keeps
  the layout, not the indices, since TQ3_1S runs its own scale search.
- ggml-turbo-quant.c: drop the measured figure from the comment.
- llama_ftype_name: name TQ3_1S, TQ4_1S and TQ3_4S, which printed
  "unknown, may not work".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 22, 2026
@jasstrong

Copy link
Copy Markdown
Author

Rebased onto 50b4aa28b, with one follow-up commit (87f70bc):

  • tests/test-turbo-quant.c: the error sums no longer promote a float operand. The file now builds with -Wall -Wextra -Wdouble-promotion -Werror; before, it warned twice. roundtrip_rel_rmse now sits above the block-boundary comment.
  • The llama-quant.cpp comment now says the fallback keeps the layout, not the indices.
  • The 7% figure has moved from the comment to the description.
  • llama_ftype_name names TQ3_1S, TQ4_1S and TQ3_4S.
  • AGENTS.md has a TQ3_4S row (53, CPU) and the range is now 43-53. Base's backend sentence is kept. The README type list and backend matrix have TQ3_4S rows.

The tq3_4s op tests stay in #416, and the description says so, so the two can go in back to back.

@TheTom
TheTom merged commit 09b62b7 into TheTom:feature/turboquant-kv-cache Nov 22, 2026
TheTom added a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 23, 2026
No content change. The branch already holds TheTom#413, TheTom#415 and TheTom#418; this records their merge commits as parents so the pull request merges cleanly.
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.

3 participants