Skip to main content

ternary_QAT

Lightweight ternary QAT for Ternary-Bonsai unpacked text models.

Qwen 3.5 is currently being tested.

This is designed from the start to be Unsloth compatible.

Weights are ternarized to {-1, 0, 1} per group of (128/64/user-defined) consecutive weights along the last dim, matching the Bonsai on-disk format (verified bit-exact). Embeddings + all nn.Linear modules (attn, MLP, lm_head) are ternarized; norms stay FP.

Based on Prism-ML's whitepaper: https://github.com/PrismML-Eng/Bonsai-demo/blob/main/ternary-bonsai-8b-whitepaper.pdf

Install

Have your preferred torch version installed first so that this doesn't install the CPU version (which you probably don't want)

pip install ternary_QAT                 # core (torch only)
pip install "ternary_QAT[peft,transformers]"  # + LoRA / model loading

if you use uv:
uv pip install ternary_QAT --torch-backend=auto

Use

Full-finetune

from transformers import AutoModelForCausalLM
from ternary import swap_linear, TernaryConfig

model = AutoModelForCausalLM.from_pretrained("prism-ml/Ternary-Bonsai-1.7B-unpacked")
swap_linear(model, TernaryConfig(group_size=128))
# ... train normally; ternarize fires in every Linear.forward

LoRA (ternary frozen base + FP adapters)

from peft import LoraConfig, get_peft_model
from ternary import swap_linear, TernaryConfig, ternarize_lora_params, reternarize_merged_linears

model = ...  # load model
swap_linear(model, TernaryConfig(group_size=128))
model = get_peft_model(model, LoraConfig(r=128, lora_alpha=128, ...))

# ... train ...

# at save: ternarize adapter, merge, re-ternarize merged linears
ternarize_lora_params(model)
model = model.merge_and_unload()
reternarize_merged_linears(model)
model.save_pretrained("./out")

See examples/ for LoRA, FFT, and Unsloth examples.

Learning rate

Ternary QAT needs 10-50x higher LR than standard fine tuning.

The lowest usable LR I've found so far is around 7e-4, so experiment in that range up to the e-3s, depending on rank and dataset size.

Metadata

Release files for ternary-QAT 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ternary-QAT 0.1.5
File Size Uploaded Provenance Actions
ternary_qat-0.1.5.tar.gz 6.3 kB With provenance Details for ternary_qat-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ternary-QAT 0.1.5
File Interpreter ABI Platform Actions
ternary_qat-0.1.5-py3-none-any.whl Python 3 none any Details for ternary_qat-0.1.5-py3-none-any.whl

Total release size: 13.7 kB

Release files / ternary_qat-0.1.5.tar.gz

Download URL ternary_qat-0.1.5.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
24ea7a2a5c4c4d84c006fcb7eb9c9fb74ba42d19642d6f89e83c7b5593832ae6
BLAKE2b-256 checksum
How to use checksums
9aef3d978e83f412fb884d3970a2de20eb4454523f85abbe7c03c6277c06fc2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / ternary_qat-0.1.5-py3-none-any.whl

Download URL ternary_qat-0.1.5-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a1ad53a245acc36ae2b80c27c823311cab52a7fbcd34965c52617f76636d9a2
BLAKE2b-256 checksum
How to use checksums
c936700ea1637eb2198e3b260b0cc6409cbde2c165f4e04f37f099578fb258f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Security for ternary-QAT 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ternary_qat-0.1.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page