Repository navigation
ggml: TQ3_4S weight type and CPU reference (3.5 bpw) - #418
Conversation
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]>
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.
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.
|
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 Left before I merge:
Small things:
|
|
#403 has merged, so this branch's base moved again (the fork tip is now I rebased this branch onto the new base; the result is code-identical to the current head and merges cleanly into the base: head: The only conflict was The same rebase exists for the rest of the stack: |
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]>
bcb4dd5 to
87f70bc
Compare
|
Rebased onto
The tq3_4s op tests stay in #416, and the description says so, so the two can go in back to back. |
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.
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-opscases for TQ3_4S are in #416 (theall_typesentry and theMUL_MAT/MUL_MAT_IDcases), so nothing in this PR runs thesupports_oprejections. 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:d * q / 63.This PR adds:
TQ3_4S(47). A row whose length is not a multiple of 128 falls back to TQ3_1S.supports_opnow 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.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 / 63in the fp16, which underflows for small rows, as found in its review. This version storesdmax. No file in the first format has been published.Testing
test-turbo-quant: TQ3_4S is added to Test 4 (chunked dequant == whole-row dequant at k = 128..4096). The new Test 5 round-trips Gaussian rows at sigma 1e-2..1e-6 for TQ3_1S and TQ3_4S:test-quantize-fns: TQ3_4S passes at the 3-bit thresholds.-Wdouble-promotion:tests/test-turbo-quant.cbuilds clean with-Wall -Wextra -Wdouble-promotion -Werror(two warnings before the fix). On the rebased head: builds without warnings on its own (MI210).test-turbo-quantandtest-quantize-fnspass on all three cards at ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415) #416's head, which contains this branch.Mixed-type file: quantizing SmolLM2-360M-Instruct (F16) with
llama-quantize ... TQ3_4Sexercises the fallback. Its hidden size is 960 and its FFN width 2560. The 32ffn_downtensors (row 2560) become TQ3_4S, and the 224 tensors with row 960 (attention, gate, up) fall back to TQ3_1S. Wikitext-2 perplexity, 16 x 512:The mixed file loads and runs on Metal, which aborted before this change. This branch and ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415) #416 quantize it byte-identically.
Same-source comparison: Qwen3-8B BF16 quantized to TQ3_1S and to TQ3_4S with this storage. Wikitext-2, 16 x 512, run on a 7900 XTX with ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415) #416's kernels:
Flash-Next with all 144 expert tensors requantized to TQ3_4S gives the same NMSE against BF16 to 6 digits as ggml: add TQ3_4S, 3.5 bpw TQ3 with 128-value super-blocks (stacked on #403, #413, #415) #416's first storage.
🤖 Generated with Claude Code