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, loggingzenbit.tensor_parallel— SPMD tensor parallel sharding (Megatron-style column/row parallel linear layers) for models too large to replicate on a single TPU corezenbit.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 howbitsandbytesfuses 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 Flaxnn.Moduledrop-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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eb09d527e3b12218b1a464a78025ff1181ac50d1d31cec2c091e6542fd9d3e0
|
|
| MD5 |
81e6bb64c69871f5fd09cbce678f9c58
|
|
| BLAKE2b-256 |
73f6148c168029c06e68dc2ca687f755e868fc6162926c4ceef3b77c7e9ff15f
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83ac6286f62c8b65bb980f12c3886ac26c8b49e5165b803f888b6458b9598178
|
|
| MD5 |
0f3ce263f36b54881bbf12242cf9c122
|
|
| BLAKE2b-256 |
db57555c8d0a6ea5c5ff0b749edfce0f16c29f40058067a0794e42647de55bc2
|