Skip to main content

Hybrid low-bit compression for Hugging Face models on low-RAM devices.

Project description

NearBit

NearBit is a Python package for compressing Hugging Face models with a single strategy: hybrid_lowbit.

The package keeps high-impact parts in int8, compresses the bulk of linear layers to binary or ternary, rescues outlier weights in int8, and targets the lowest practical RAM footprint without retraining.

What NearBit does

  • Compresses nn.Linear layers with binary or ternary storage plus group-wise scales
  • Keeps sensitive linear layers in int8
  • Compresses nn.Embedding tables to int8
  • Works directly on existing Hugging Face models
  • Exposes simple chat and prompt benchmarking helpers

Install

Install PyTorch first for your platform, then install NearBit.

pip install torch
pip install nearbit

For local development from this repo:

pip install torch
pip install -e .

Quick Start

from nearbit 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,
    binary_outlier_fraction=0.002,
    ternary_outlier_fraction=0.001,
    binary_nmse_threshold=0.045,
    ternary_nmse_threshold=0.02,
)

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 nearbit
from nearbit 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())
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

  • NearBit is centered on one executable path: hybrid_lowbit
  • There is no public int8 fallback API in the package surface
  • Quality depends on the model architecture and compression thresholds
  • The strongest RAM reduction comes from the hybrid path, not from pure int8

Project details


Download files

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

Source Distribution

nearbits-0.2.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

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

nearbits-0.2.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file nearbits-0.2.0.tar.gz.

File metadata

  • Download URL: nearbits-0.2.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for nearbits-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2aec99ec32b9d862162097bbcadd03557a4df9a892edf84da1638ac494a052fe
MD5 1f308df37143dbf9d3e277b0748fae9d
BLAKE2b-256 d18159e066b9529e6a2e922868addf5d333608ffda8b2625352a130c6e45dd6b

See more details on using hashes here.

File details

Details for the file nearbits-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nearbits-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for nearbits-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fb9f3359d742a40b83545e83c1c9369afb2a7235278cc3ab28f63c14af7c738
MD5 f06ff56e09bf4e8f5a77a2c220d50b8d
BLAKE2b-256 adbf17c7b0f233baa397d16d651aaceee523f594e3025845ec88f701bb88548b

See more details on using hashes here.

Supported by

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