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. Includes a fused backward pass (custom VJP, dL/dX computed without materializing the dequantized weight — dL/dW is not computed, matching a frozen quantized base weight), double quantization for the scale factors, tiling-size helpers (suggest_tile_sizes), and an optional Flax nn.Module drop-in layer (zenbit.pallas_nf4.flax_layer.NF4DenseFused, pip install zenbit[pallas-flax]). Verified on Kaggle TPU (v5e), forward and backward both bit-exact (max_diff=0.0) vs reference; 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).

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.5.0.tar.gz (26.1 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.5.0-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for zenbit-0.5.0.tar.gz
Algorithm Hash digest
SHA256 1eb09d527e3b12218b1a464a78025ff1181ac50d1d31cec2c091e6542fd9d3e0
MD5 81e6bb64c69871f5fd09cbce678f9c58
BLAKE2b-256 73f6148c168029c06e68dc2ca687f755e868fc6162926c4ceef3b77c7e9ff15f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zenbit-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 29.3 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83ac6286f62c8b65bb980f12c3886ac26c8b49e5165b803f888b6458b9598178
MD5 0f3ce263f36b54881bbf12242cf9c122
BLAKE2b-256 db57555c8d0a6ea5c5ff0b749edfce0f16c29f40058067a0794e42647de55bc2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 files

0.4.0

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