Ternary QAT for transformers, built for Ternary-Bonsai by Prism-ML
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ternary_qat-0.1.3.tar.gz.
File metadata
- Download URL: ternary_qat-0.1.3.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3a95d9100bfec452f9fa6494eac7fc9186e4957641908e2f324f422a2bcef2f
|
|
| MD5 |
16b6793e02d5c4ccd9f1d8bf6bed022f
|
|
| BLAKE2b-256 |
327f10dce8a08dffbc872a086f1712d33ad814f526fb599fec43233f71eb2dc7
|
Provenance
The following attestation bundles were made for ternary_qat-0.1.3.tar.gz:
Publisher:
publish.yml on electroglyph/ternary_QAT
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ternary_qat-0.1.3.tar.gz -
Subject digest:
b3a95d9100bfec452f9fa6494eac7fc9186e4957641908e2f324f422a2bcef2f - Sigstore transparency entry: 2212107612
- Sigstore integration time:
-
Permalink:
electroglyph/ternary_QAT@bdac0bb392873fbd2bc992f2e36d0be8a1a86e00 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/electroglyph
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bdac0bb392873fbd2bc992f2e36d0be8a1a86e00 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ternary_qat-0.1.3-py3-none-any.whl.
File metadata
- Download URL: ternary_qat-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03cfe64d58dd9ce63d089c217997970f76ac8fa6c02cfd91fe08d630975974c0
|
|
| MD5 |
842bc5cd008beb9f50a0e3079c863b68
|
|
| BLAKE2b-256 |
21c3f79725ac68d62e048d0e6f7c454d5d32b0c0c7c04d54459c61519348804d
|
Provenance
The following attestation bundles were made for ternary_qat-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on electroglyph/ternary_QAT
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ternary_qat-0.1.3-py3-none-any.whl -
Subject digest:
03cfe64d58dd9ce63d089c217997970f76ac8fa6c02cfd91fe08d630975974c0 - Sigstore transparency entry: 2212107637
- Sigstore integration time:
-
Permalink:
electroglyph/ternary_QAT@bdac0bb392873fbd2bc992f2e36d0be8a1a86e00 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/electroglyph
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bdac0bb392873fbd2bc992f2e36d0be8a1a86e00 -
Trigger Event:
push
-
Statement type: