Nearbits ternary-residual compression for Hugging Face models on low-RAM devices.
Project description
Nearbits
Nearbits is a Python package for compressing Hugging Face models with a ternary-residual strategy tuned for the lowest practical RAM footprint.
The default math is a post-training 1.58-bit style path for ordinary linear weights: a grouped ternary base {-1, 0, 1} with blended absmean/RMS scaling, plus a tiny VIP residual slice kept in 4-bit or 8-bit. Protected modules such as embeddings, norms-adjacent outputs, and heads stay in int8 for stability.
New Tech
Nearbits now uses a bitnet158 formula for ordinary projection layers:
W_hat = s_g * T(W_g, tau_g) + R_vip
Where:
W_gis a weight groupT(W_g, tau_g)is ternary quantization to{-1, 0, 1}with thresholdtau_gs_g = alpha * mean(|W_g|) + (1 - alpha) * rms(W_g)is the blended group scaleR_vipis a tiny residual correction made from only the top VIP weights, stored in4-bitor8-bit
Default values in this release:
alpha = 0.7
au_g = 0.9 * mean(|W_g|)
vip_fraction = 0.001
max_vip_fraction = 0.01
vip_bits = 4
activation_bits = 8
This is designed to keep RAM low on arbitrary Hugging Face checkpoints while still producing usable output. It is inspired by BitNet-style ternary math, but this package is still a post-training approximation rather than native BitNet training.
What Nearbits does
- Compresses ordinary
nn.Linearand Hugging FaceConv1Dlayers to abitnet158-style ternary code path plus small VIP residuals - Keeps only protected layers such as embeddings and heads in
int8 - Applies optional per-token activation quantization in
8-bitduring inference-style forward passes - Works directly on existing Hugging Face models without retraining
- Exposes simple chat and prompt benchmarking helpers
Install
Install PyTorch first for your platform, then install Nearbits.
pip install torch
pip install nearbits
For local development from this repo:
pip install torch
pip install -e .
Quick Start
from nearbits import HybridLowBitConfig, chat, compress_hf_model, load_hf_model, model_report
model, tokenizer, model_kind = load_hf_model("Qwen/Qwen2.5-1.5B-Instruct")
print(model_report(model))
config = HybridLowBitConfig(
group_size=128,
bitnet_scale_blend=0.7,
bitnet_threshold_factor=0.9,
vip_fraction=0.001,
max_vip_fraction=0.01,
vip_bits=4,
activation_bits=8,
)
compressed_model = compress_hf_model(model, config)
print(model_report(compressed_model))
reply = chat(compressed_model, tokenizer, "Who are you?")
print(reply)
Google Colab Usage
Use examples/google_collab_hybrid_lowbit.py as the runnable Colab example.
Typical Colab cells:
!pip install torch
!pip install nearbits
from nearbits import HybridLowBitConfig, benchmark_prompts, compress_hf_model, load_hf_model, model_report
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
PROMPTS = [
"hi",
"hello",
"who are you?",
"what is 2 + 2?",
"write one sentence about India.",
"explain model compression in simple words.",
]
model, tokenizer, model_kind = load_hf_model(MODEL_ID)
print("Before:", model_report(model))
compressed_model = compress_hf_model(
model,
HybridLowBitConfig(
vip_fraction=0.001,
max_vip_fraction=0.01,
vip_bits=4,
activation_bits=8,
),
)
print("After:", model_report(compressed_model))
rows = benchmark_prompts(compressed_model, tokenizer, PROMPTS)
for row in rows:
print(row["prompt"])
print(row["response"])
print(row["latency_sec"])
Public API
load_hf_model(model_id, trust_remote_code=False)compress_hf_model(model, config=None)model_size_mb(model)model_report(model)chat(model, tokenizer, prompt, ...)benchmark_prompts(model, tokenizer, prompts, ...)HybridLowBitConfig(...)
Notes
- The public package name is
nearbits - The default ordinary-layer strategy is
bitnet158, not whole-layerint8 - This is a post-training approximation for arbitrary Transformer checkpoints, not native BitNet training
- The strongest RAM reduction comes from the ternary base plus bounded VIP residuals
Publish New Version
Build and upload the new version to PyPI with:
python -m pip install --upgrade build twine
python -m build
python -m twine upload dist/*
If you want to publish only this version's fresh artifacts, clean dist first and then upload:
rmdir /s /q dist
python -m build
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
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 nearbits-0.3.0.tar.gz.
File metadata
- Download URL: nearbits-0.3.0.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e8d69f1cbdedfaeb10e1a62b4747b425999a02595dd5682165a724279e59a4c
|
|
| MD5 |
da8ff3e748d725fb5f047b3b6c12d3f4
|
|
| BLAKE2b-256 |
caa031db67d92a72cfff947addeb1b26d555b4303c608875608fccc462421664
|
File details
Details for the file nearbits-0.3.0-py3-none-any.whl.
File metadata
- Download URL: nearbits-0.3.0-py3-none-any.whl
- Upload date:
- Size: 25.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37df214023569b67a49cb07b5a37393de3d0e0cda62cb5edf8523b417ba1d93c
|
|
| MD5 |
a7427ddba37d277ea9aedff3b8fea449
|
|
| BLAKE2b-256 |
3afd0bc53565776006bd2fbcde21de2a4429d26ae9166c60170da0c6e460615d
|