Skip to main content

quintrit (Python)

Python bindings for the Quintrit SDK: ternary weights {-1, 0, +1} packed at 5 trits per byte (1.60 bits per weight) with SIMD GEMV kernels, a GGUF reader, and a small inference runtime. Built with PyO3 and maturin; numpy in, numpy out.

The package has three levels. Use as many as you need.

Level What it does Entry points
Kernels pack ternary matrices, multiply them with int8 activations pack, unpack, gemv, PackedMatrix
Format inspect GGUF files, read or transcode ternary tensors open, GgufFile, TensorInfo, read_tensor
Runtime load a ternary language model, tokenize, generate, stream Model, Tokenizer, TokenStream

Install

From source (from the repository root; needs Rust 1.98 or newer (edition 2024; the NEON dot-product intrinsics are stable from 1.98) and Python 3.9 or newer):

python -m venv .venv && source .venv/bin/activate      # Windows: .venv\Scripts\activate
pip install maturin numpy pytest
cd rust/crates/quintrit-py
maturin develop --release        # builds the extension and installs it into the active environment
pytest                           # runs the test suite (no model needed)

The wheels are abi3 wheels: one wheel per platform works for every CPython from 3.9 on.

Quick start

Kernels

import numpy as np
import quintrit

weights = np.random.choice([-1, 0, 1], size=(2560, 2560)).astype(np.int8)
packed = quintrit.pack(weights)                      # PackedMatrix, 1.60 bits per weight
print(packed)                                        # PackedMatrix(rows=2560, cols=2560, kernel="quintrit", ...)

x = np.random.randint(-128, 128, size=2560).astype(np.int8)
y = quintrit.gemv(x, packed)                         # int32 array, exact for the full int8 range
assert np.array_equal(y, weights.astype(np.int64) @ x)
  • gemv(activations, packed, *, kernel=None, threads=None, dtype=None, scale=None)
    • activations: 1-D int8 array of length cols, or 2-D (batch, cols); the result has shape (rows,) or (batch, rows).
    • kernel: None (the kernel the matrix was packed for), "auto", "quintrit" or "quintrit_512".
    • threads: worker threads (1 to 256), default is the number of logical CPUs, at most 256.
    • dtype: "int32" (default) or "float32"; scale=... multiplies the result and implies float32.
  • The GIL is released while a kernel runs, so several Python threads can multiply concurrently.
  • pack accepts arrays of any memory layout (C, Fortran, strided views). Inputs must be int8; other dtypes raise DTypeError instead of being converted. Values other than -1, 0, +1 raise TritValueError.
  • Any number of rows and columns works. Rows are padded to the tile height internally (32, or 64 for the AVX-512 kernel) and the padding never shows up in the result.
  • PackedMatrix has rows, cols, shape, kernel, nbytes, memory_bytes, bits_per_weight, unpack(), to_bytes(), PackedMatrix.from_bytes(data), equality and pickle support. to_bytes() stores a 24 byte header (QPKM, version, rows, cols) and the packed weights; it is a convenience format for the Python API and not a model file format.
  • The kernels accumulate in float32, so integer results are exact while their magnitude stays below 2^24, which holds for cols <= 131072 with full-range activations.
  • Memory: the packed weights are kept once; the tiled layout a kernel reads is built on first use and cached per kernel (the layout of the kernel chosen at pack time is built immediately). memory_bytes reports the total.
quintrit.available_kernels()   # ['quintrit'] or ['quintrit', 'quintrit_512']
quintrit.best_kernel()         # what "auto" resolves to
quintrit.cpu_features()        # {'avx2': True, 'avx512f': False, ...}

Format

with quintrit.open("ggml-model-i2_s.gguf") as gguf:
    for t in gguf.tensors():                       # TensorInfo: name, shape, dtype, is_ternary, nbytes, ...
        print(t.name, t.shape, t.dtype)
    trits = gguf.read_trits("blk.0.attn_q.weight")  # int8 array (rows, cols) with values -1/0/+1
    scale = gguf.tensor_scale("blk.0.attn_q.weight")
    packed = gguf.transcode("blk.0.attn_q.weight")  # PackedMatrix, ready for quintrit.gemv
    norm = quintrit.read_tensor(gguf, "blk.0.attn_norm.weight")   # float32 / float16 tensors too
    gguf.get_metadata("llama.context_length")

Supported ternary tensor types: I2_S, TQ1_0, TQ2_0. GGUF stores dimensions innermost first; TensorInfo.shape is in numpy order ((rows, cols)), TensorInfo.ggml_shape is the stored order. The file is memory-mapped; close() (or leaving the with block) unmaps it. GGUF is the only file format that is read.

Runtime

model = quintrit.Model.load("models/Falcon3-1B-Instruct-1.58bit", threads=8)   # file or directory
print(model.config["num_hidden_layers"], model.context_length, model.kernel)

prompt = "<|user|>\nWhat is ternary computing?\n<|assistant|>\n"
text = model.generate(prompt, max_tokens=64)             # greedy (temperature=0): deterministic

for piece in model.stream(prompt, max_tokens=64, temperature=0.8, top_p=0.9, seed=1):
    print(piece, end="", flush=True)

ids = model.tokenizer.encode(prompt)
logits = model.logits(ids)                               # float32 array (vocab_size,)
model.chat([{"role": "user", "content": "Hello"}], max_tokens=32)
  • generate and stream take max_tokens, temperature (0 = greedy), top_k, top_p, seed and stop (a string or a list of strings; the text ends before the first match). Ctrl-C interrupts a running generation.
  • A model has one context. Every generate / stream call starts from an empty context; starting a new one (or calling reset() / logits()) invalidates a stream that is still open, and its next step raises StaleStreamError.
  • TokenStream exposes text, token_ids, n_tokens, finish_reason (length, eos, stop, context, closed) and close(). Text pieces never split a multi-byte character; a piece is held back until the character is complete.
  • Loading a model is silent. Model.load(path, verbose=True) prints progress lines to the standard output of the process (they come from the Rust runtime and bypass Python's sys.stdout, so contextlib.redirect_stdout does not capture them).
  • Use kernel="quintrit" or kernel="quintrit_512" in Model.load to force a kernel; all kernels give identical logits.
  • NVIDIA GPU. A package built with the cargo feature cuda (maturin develop --release --features cuda; needs the CUDA Toolkit 13.x to build) runs I2_S and block-scaled Q2_0 models on a GPU of compute capability 8.0 or newer: Model.load(path, device="cuda") ("cuda:1" for the second GPU, context=2048 to bound the context window). Logits and text are bit-identical to the CPU. quintrit.cuda_built() tells whether the package has GPU support and quintrit.cuda_devices() lists the GPUs. threads and kernel do not apply on a GPU; errors from the GPU raise quintrit.DeviceError. See the GPU guide.

Errors

All errors derive from quintrit.QuintritError; where a builtin is the natural parent they inherit from it too, so except ValueError and except TypeError keep working.

Class Also a Raised for
ShapeError ValueError wrong array rank, empty array, activation length mismatch
DTypeError TypeError wrong dtype or object type (for example a float array)
InvalidArgumentError ValueError thread count, sampling parameters, token ids
TritValueError ValueError trits outside {-1, 0, +1}
KernelError, UnsupportedKernelError ValueError unknown kernel name; kernel the CPU cannot run
SerializationError ValueError malformed PackedMatrix.from_bytes data
FormatError, TensorNotFoundError ValueError, KeyError bad or unsupported file content, unknown tensor
QuintritIOError, QuintritFileNotFoundError OSError, FileNotFoundError file access
ModelLoadError, TokenizerError, GenerationError RuntimeError runtime failures
StaleStreamError, ContextOverflowError GenerationError invalidated stream; prompt longer than the context
InternalError RuntimeError an unexpected failure inside the native code (a bug)

Inputs are validated before any kernel runs, and a panic in the native code is caught at the boundary and raised as InternalError instead of aborting the interpreter. The extension is therefore built with panic = "unwind" (the SDK's own release profile uses abort).

Supported platforms

Platform Kernels Test status
Windows x86-64 quintrit (AVX2), quintrit_512 (AVX-512 BW + VBMI) the test suite passes on an AVX2 host (the AVX-512 path needs an AVX-512 VBMI CPU)
Linux x86-64 quintrit (AVX2), quintrit_512 same sources; run pytest on the target machine to confirm
Linux aarch64 quintrit (NEON) same sources; run pytest on the target machine to confirm
macOS arm64 / x86-64 quintrit (NEON / AVX2) same sources; run pytest on the target machine to confirm

The CPU features are detected at run time: one wheel contains every kernel and picks the best one the machine can run. On a CPU without AVX2 or NEON the quintrit kernel falls back to portable scalar code.

Building wheels

pip install maturin
cd rust/crates/quintrit-py

# Linux, x86-64 or aarch64 (build on the matching machine or container; manylinux compliant)
maturin build --release --manylinux 2_28 --out dist
#   or with the official image:  docker run --rm -v "$PWD/../..":/io ghcr.io/pyo3/maturin build --release \
#       -m rust/crates/quintrit-py/Cargo.toml --out rust/crates/quintrit-py/dist

# Windows x86-64 (MSVC toolchain)
maturin build --release --out dist

# macOS: one wheel for both architectures
rustup target add aarch64-apple-darwin x86_64-apple-darwin
maturin build --release --target universal2-apple-darwin --out dist

pip install dist/quintrit-*.whl

The build needs the sibling crates (quintrit-core, quintrit-format, quintrit-runtime) next to this directory, so build from a checkout of the SDK. The crate is a standalone Cargo package with its own Cargo.lock; it is not part of the SDK's Cargo workspace, so cargo test of the SDK never needs Python.

Tests

pytest                                        # default run: no model, no network
QUINTRIT_TEST_MODEL=/path/to/model.gguf pytest tests/test_model_real.py -s   # optional, real model

The default run checks the kernels against brute-force numpy ground truth for many shapes (including row and column remainders and extreme activation values), memory layouts, error paths, GGUF parsing on synthetic files, and the whole runtime (loading, tokenizing, generating, streaming, stop sequences) on a tiny random model that the tests write themselves. The real-model test is skipped unless QUINTRIT_TEST_MODEL is set.

Examples

Standalone examples matching the C and C++ examples are also in examples/python/.

Type information

The package ships py.typed and a stub file for the compiled module (python/quintrit/_native.pyi).

License

Apache-2.0 (see LICENSE)

Metadata

Release files for quintrit 0.2.0

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

Source distribution (sdist)

Source distribution for quintrit 0.2.0
File Size Uploaded Provenance Actions
quintrit-0.2.0.tar.gz 493.8 kB With provenance Details for quintrit-0.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for quintrit 0.2.0
File Interpreter ABI Platform Actions
quintrit-0.2.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for quintrit-0.2.0-cp39-abi3-win_amd64.whl
quintrit-0.2.0-cp39-abi3-manylinux_2_28_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64 Details for quintrit-0.2.0-cp39-abi3-manylinux_2_28_x86_64.whl
quintrit-0.2.0-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details for quintrit-0.2.0-cp39-abi3-manylinux_2_28_aarch64.whl

Total release size: 5.6 MB

Release files / quintrit-0.2.0.tar.gz

Download URL quintrit-0.2.0.tar.gz
Size 493.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bb006111665a4915ba27bc1b9b4273d35ed6350603864f7a2e161b228acd8aba
BLAKE2b-256 checksum
How to use checksums
9262402526af03af177947ccb5979f1469a6a52acefe794e1ce1c130ee7afeac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / quintrit-0.2.0-cp39-abi3-win_amd64.whl

Download URL quintrit-0.2.0-cp39-abi3-win_amd64.whl
Size 1.7 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
3684d608a9277763914e869dd1ebbb561e8d3e315ef20c5e6a221130e3e357b5
BLAKE2b-256 checksum
How to use checksums
2f1d3f5346a5d419bf11e08b96d63aca4844562486edbef9f280ba80b6723e05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / quintrit-0.2.0-cp39-abi3-manylinux_2_28_x86_64.whl

Download URL quintrit-0.2.0-cp39-abi3-manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
fd8f95f70d3a7020d85dbd602b68398eef81a5e788ee7285172f992ee2e65ad7
BLAKE2b-256 checksum
How to use checksums
baebe192b70b855f4e99fcc81d49f736b627878a7b0f06a6ff5489de0ddcac12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / quintrit-0.2.0-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL quintrit-0.2.0-cp39-abi3-manylinux_2_28_aarch64.whl
Size 1.6 MB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
6709d5588040a6bb7e5cdca0ffaf9f84fde389489816dd9328449a49b9485d4f
BLAKE2b-256 checksum
How to use checksums
78ce2d6a67cdb1d7a22b44aea65d0c2e70169f803005ad1abc115ec289c82b82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Security for quintrit 0.2.0

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 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for quintrit-0.2.0.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 Nov 24, 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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

4 release files Full provenance

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