Skip to main content

Zenbit

TPU sharding and quantization utilities for PyTorch/XLA training. Built as a companion to ZAdapter, but usable standalone for any PyTorch/XLA training setup.

Install

pip install zenbit[tpu]

The [tpu] extra pulls in torch_xla. Without it, quantization utilities (int8, NF4) still work on CPU/GPU for offline testing — only the sharding/tensor-parallel pieces require an actual TPU runtime.

What's inside

  • zenbit.int8 — uniform int8 weight quantization (~50% memory savings vs fp16)
  • zenbit.nf4 — NF4 blockwise quantization with double quantization, QLoRA-style (~75-89% memory savings vs fp32)
  • zenbit.sharding — data-parallel training loop utilities: device detection, dataset sharding, gradient sync, checkpointing, logging
  • zenbit.tensor_parallel — SPMD tensor parallel sharding (Megatron-style column/row parallel linear layers) for models too large to replicate on a single TPU core
  • zenbit.pallas_nf4 (optional, pip install zenbit[pallas]) — genuinely fused NF4 dequant+matmul as a custom Pallas TPU kernel (JAX backend). Weights are streamed compressed into VMEM and dequantized on-chip, never materializing the full-precision weight in HBM — closer to how bitsandbytes fuses dequant+matmul on GPU. Verified on Kaggle TPU (v5e); not currently verified on Google Colab TPU due to a Mosaic module version mismatch in Colab's pinned jax/jaxlib/libtpu combination (unrelated to kernel correctness — the same kernel produces bit-exact output vs the reference implementation on Kaggle).

Quick example

from transformers import AutoModelForCausalLM
from zadapter import inject_adapter
from zenbit import quantize_model_nf4, NF4Config, train_loop, TrainConfig

model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-7B")
model = quantize_model_nf4(model, NF4Config(double_quant=True))
model = inject_adapter(model, r=64)

train_loop(model, train_dataset, loss_fn, TrainConfig(lr=1e-4, num_epochs=3))

For models too large for a single TPU core (30B+), combine with tensor parallel:

from zenbit import setup_spmd_mesh, build_tp_zadapter_model, TPConfig

mesh = setup_spmd_mesh(TPConfig(num_shards=8))
model = build_tp_zadapter_model(model, mesh)

Design notes

  • Why data-parallel by default: adapter parameters are small (~0.5-3% of the model), so gradient sync overhead is minimal. Data parallel keeps communication to one all-reduce per step, avoiding the per-layer communication overhead of full model parallelism.
  • Why tensor parallel is opt-in: it's necessary once the base model no longer fits on a single TPU core's HBM (roughly 30B+ params even with NF4 quantization), but comes with real communication overhead per layer. Use it only when the model size requires it.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zenbit-0.4.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zenbit-0.4.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file zenbit-0.4.0.tar.gz.

File metadata

  • Download URL: zenbit-0.4.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for zenbit-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b85a9270aab8c7b2d654b8fe61e6646d90051f23f9400c9816fc4a7885cfdbbb
MD5 c7adc721de27cb51edccc579bfdf2055
BLAKE2b-256 d333f44ff335d125129d3f29409a6170ccb41b8b0936aa2ec5d02d3918e8d1a5

See more details on using hashes here.

File details

Details for the file zenbit-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: zenbit-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for zenbit-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a8bba53c69f0734c0ff778db00ea427d2def80445844ad474172c319b58a722
MD5 d11dbeaedfbf1bfd15a921d9f61fe063
BLAKE2b-256 e207e57ba44a0059d4ee60eaefecf7f1f91d97c82be8b2461b5c7354c8932626

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.0

2 files

This release

0.4.0 This release

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page