Skip to content

ggml: add PTQ1_0, ternary at group 128 - #29672

Open
bri-prism wants to merge 2 commits into
ggml-org:masterfrom
PrismML-Eng:ptq1_0-base
Open

bri-prism wants to merge 2 commits into
ggml-org:masterfrom
PrismML-Eng:ptq1_0-base

Conversation

@bri-prism

@bri-prism bri-prism commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

PTQ1_0 preserves exactly ternary weights with one scale per 128 elements. It uses the same base-3 digit encoding as TQ1_0. Its block is 24 base-3 payload bytes, two tail bytes and one fp16 scale (28 bytes, 1.75 bits/weight). Existing TQ1_0 remains unchanged. The published PTQ1_0 name and tensor ID 143 are retained pending maintainer agreement; this update does not silently change existing files.

Reconstructed onto upstream 5a4afd80779ba51166b59986e61a0bfbb9e1a4c6, preserving current F8_E4M3 support and the native x86 Q2 implementation. Scope: original 20 production/test files plus two existing Python test files and quantizer documentation. CUDA support stays in #29676.

Validation on the rebased tree (Linux x86-64, CPU only):

  • 42 backend cases pass: MUL_MAT 14, GET_ROWS/CPY/MUL_MAT_ID 28.
  • Quantization suite: zero failures; 8,192 exactly ternary values and three published block-byte fixtures pass.
  • Actual C/Python/scalar encoder byte parity; bitwise dequantization parity; every byte value, nonuniform group scales, rounding ties, invalid widths, and TQ1_0 control. A deliberately corrupted encoder output fails the independent oracle.
  • Seven GGUF tests pass with no skips, including actual llama-quantize --pure PTQ1_0 and COPY on a bounded synthetic matrix. Additional actual CLI cases verify Q4_0 fallback for width 96 and F16 fallback for width 127, including COPY payload preservation. This is not a generatable model test.
  • Changed Python files pass ty; model-name mappings and whitespace checks pass. Official EditorConfig checker was unavailable.

The quantizer documentation now explains the block layout, CLI creation, shape fallback, fp16-scale losslessness conditions and preservation of existing TQ1_0 files. Generic high-precision-to-ternary conversion is not claimed lossless.

Remaining upstream acceptance evidence: a small public model example, PPL and KL against native FP16/BF16 and similar-size quantizations, and matched pure-CPU performance. The previously linked 27B example depends on folded Hadamard support (#29600). Historical numbers against the Prism fork are not a fresh native-baseline quality or performance result for this rebase. No GPU/device validation was run.

AI assistance: the original implementation used Claude Code; this rebase, compatibility tests and update were assisted by Codex. [skip ci] prevents this update from initiating ordinary CI while device testing is not authorized.

@github-actions github-actions Bot added testing Everything test related examples ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language Apple Metal https://en.wikipedia.org/wiki/Metal_(API) conversion labels Sep 29, 2026
@bri-prism
bri-prism marked this pull request as ready for review September 29, 2026 17:23
@bri-prism
bri-prism requested review from a team, CISC and ggerganov as code owners September 29, 2026 17:23

@arthw arthw 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.

@bri-prism
There is no import change in SYCL backend.
I can't review this PR as maintainer.

Thank you!

@Green-Sky

Copy link
Copy Markdown
Collaborator

Since this is just TQ1_0 with 128 instead of 256 values per scale, wouldnt this be named TQ1_X instead?

@bri-prism

Copy link
Copy Markdown
Contributor Author

It is a different layout, not TQ1_0 with a smaller block: 2-bit codes with a per-128 scale, so it round-trips exactly-ternary weights losslessly where TQ1_0's 5-trits-per-byte packing does not (see the lossless.py numbers above). The P prefix follows PQ2_0. Happy to rename if the maintainers prefer another convention.

@Green-Sky

Copy link
Copy Markdown
Collaborator

Can you explain in the op how it works then?

@bri-prism

bri-prism commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Correction to my earlier reply, you were right: the packing is the same as TQ1_0 (5 trits per byte plus a 4-per-byte tail). The only difference is a 128-value block with its own fp16 scale. These models are quantized with one scale per 128 weights, and TQ1_0's single scale per 256 has to merge two of them, which is where the lossless.py error comes from. I've added this to the description. A TQ1_X-style name is fine by me if that fits the naming better.

@bri-prism

Copy link
Copy Markdown
Contributor Author

@Green-Sky Since the packing matches TQ1_0, I'm happy to rename this to TQ1_X before review if you or the maintainers prefer that. Otherwise I'll keep PTQ1_0 to match PQ2_0.

@Green-Sky

Copy link
Copy Markdown
Collaborator

@Green-Sky Since the packing matches TQ1_0, I'm happy to rename this to TQ1_X before review if you or the maintainers prefer that. Otherwise I'll keep PTQ1_0 to match PQ2_0.

I am not the authority on that, but historically it has been like that. I would wait for someone with more say :)

imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
From Brian (bri-prism), PR ggml-org#29672. Preserve type ID 143 for existing GGUF files.

Source: ggml-org#29672
Original-commit: 5e22dd0
Assisted-by: Claude Code
Assisted-by: Codex (porting and rebasing)

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
From Brian (bri-prism), PR ggml-org#29672. Preserve type ID 143 for existing GGUF files.

Source: ggml-org#29672
Original-commit: 5e22dd0
Assisted-by: Claude Code
Assisted-by: Codex (porting and rebasing)

Ported and rebased by Codex.
@nohara-makoto

Copy link
Copy Markdown

Thanks to everyone working on this!

I was looking back at #8151 and noticed that compilade had already considered smaller ternary block sizes, including 64, during the original TQ1_0 development.

With Bonsai 2 now requiring a 128-element block to preserve its learned per-group scales, I wonder if this might be a good opportunity to revisit that discussion.

Would it make sense to consider a common design for a small set of block sizes (such as 64, 128, and 256), or would keeping PTQ1_0 as a separate type be more practical for llama.cpp?

I understand that both approaches have trade-offs, especially regarding compatibility and long-term maintenance.

@ggerganov @CISC — If you have a chance, I'd be interested to hear your thoughts on the preferred direction. Even a brief comment would be appreciated.

I'm just following the development with interest, and I appreciate all the work going into Bonsai 2 support. Thanks!

imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
From Brian (bri-prism), PR ggml-org#29672. Preserve type ID 143 for existing GGUF files.

Source: ggml-org#29672
Original-commit: 5e22dd0
Assisted-by: Claude Code
Assisted-by: Codex (porting and rebasing)
Assisted-by: Claude (Opus 5.5), rebase onto ggml-org master with conflict resolution

Ported and rebased by Codex.
@spydaz

spydaz commented Nov 7, 2026

Copy link
Copy Markdown

i think this is not right !

this does not maintain its first merge ! : the tq_01 works fine in lmstudio and other products which already acess these original ternary quantizes : and this relese would force those previous models to fail :

this is a failure on the part of prisimML whom should just add new quantizes instead of overwriting previous versions :
this truly is the mistake :

personally i have not made the newer version work locally with the newer PQ_01 or the PQ_02 quantizes !
also the documentation should be updated to include the new quantizing options to be added to the cmdline quantize funciton to utlize these quantizes ! they should not be read only ! they should e fully ready for deployment : currenty despite being very useful and the right direction for quantizing models : they need to understadn this lifecycle and methodology ! as without people using the quantizing method to quantize models it become useless ! so the more people using the newer quantize methods the better for the advancement of model qunatizing !

obviousy quantizing methods should not be model specific but cover most models !

we are currenty waiting for the right release and standardized version of this quantizing method !

this shoudl also apply to new quantizing methods : ( also providing full documentations ! )

@spydaz

spydaz commented Nov 7, 2026

Copy link
Copy Markdown

@Green-Sky Since the packing matches TQ1_0, I'm happy to rename this to TQ1_X before review if you or the maintainers prefer that. Otherwise I'll keep PTQ1_0 to match PQ2_0.

yes its important to maintain backwards compatiblity : as well as make sense :
TQ_01 ( this makes sense because its tenary quantize _ 1 )
its no problem to have TQ_01_128, TQ_01_256 to add extra distiction enabling for later addtions and updates .. which is also part of the planning forwards : PTQ1_0 etc seems wrong !

same as turboquantizes need to also have some identifiers such as TB_Q1 Q2 etc but we need to be able to understand these things at a glance : ( by adding the P ( for prisimML ) ) no !! TQ was exactly right and sets the right standard going forwards for others :
as we also want this to become a fully used quantize method : hence accompaning documentation in the docs and tutorials and notebooks etc ! ... Also updating GGUF MY REPO !

@nohara-makoto

Copy link
Copy Markdown

I would really hate to see this PR remain stalled over naming conventions, especially given the potential impact of Ternary Bonsai 2.

Among models that can run within an approximately 8 GB memory budget, Bonsai 2 represents a remarkably interesting level of capability. If it becomes broadly accessible through mainstream llama.cpp and its ecosystem, it could significantly change what is practical on ordinary consumer gaming PCs.

I think getting this support merged upstream is particularly valuable.

Regarding naming, I believe @spydaz's suggestion offers a reasonable direction, while preserving backward compatibility:

Proposed name Group size Purpose
TQ1_0 256 Existing format, unchanged for backward compatibility
TQ_01_256 256 Optional alias for the existing TQ1_0
TQ_01_128 128 Current PTQ1_0 format
TQ_01_64 64 Possible future extension for other QAT models

Of course, the exact spelling could follow whatever convention the llama.cpp maintainers prefer.

The important point is that group size is not merely a storage detail. Bonsai 2 was trained with groups of 128 weights, and converting those weights to a 256-element scale group introduces additional quantization error. Recovering that lost accuracy could require substantial QAT adjustments.

Future ternary models may benefit from different group sizes, so a naming scheme that can accommodate 64, 128, and 256 seems worth considering.

The PR author has already indicated willingness to rename PTQ1_0 if the maintainers prefer another convention. That suggests the remaining issue may be primarily agreeing on a common direction rather than a fundamental technical disagreement.

I am certainly not suggesting that support for all three group sizes needs to be implemented now. Supporting G128 while preserving the existing G256 format would already be an important step.

Could one of the maintainers or contributors familiar with GGUF quantization formats help coordinate a naming decision and a clear path toward merging this PR?

It would be unfortunate for such a promising development to remain pending simply because no common naming convention has been settled.

Thanks again to everyone contributing to this work. I genuinely hope we can get this into upstream llama.cpp and make it available to a much wider community.

@Hi-Angel

Copy link
Copy Markdown

Hi, can you pls rebase?

Wanna try your PTQ1_0 — I was otherwise using Q2_0 but it barely fits on my "16G RAM + AMD iGPU" Archlinux laptop. The -1.3G from PTQ1_0 would've helped a lot.

But on master there have been lots of performance optimizations (including #29772 by you 😊) and your branch is kinda outdated, I'd appreciate if you could rebase 😊

Preserve the published 128-element, 28-byte codec contract on upstream 5a4afd8. Add CPU byte, CLI and GGUF compatibility coverage. Model-level quality and performance acceptance remains pending.

Assisted-by: Claude Code and Codex
@bri-prism
bri-prism requested a review from a team as a code owner November 15, 2026 07:57
Document the existing group-128 packing and CLI option without changing published names or IDs. Verify actual CLI shape fallbacks to Q4_0/F16 and COPY payload preservation.

Assisted-by: Codex
@ggml-org ggml-org deleted a comment Nov 15, 2026
@ggml-org ggml-org deleted a comment Nov 15, 2026
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 15, 2026
@nohara-makoto

Copy link
Copy Markdown

Nice to see this PR rebased and caught up with recent upstream changes!

The expanded test coverage is also great to see. Thanks for keeping this moving and for all the work on compatibility!

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

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) conversion documentation Improvements or additions to documentation examples ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants