bitsandbytes: Efficient k-bit Quantization for PyTorch LLMs

Jul 29, 2025

Introduction

Deploying large language models (LLMs) often requires hardware resources that are prohibitively expensive or unavailable to most developers. The challenge of fitting a multi-billion parameter model into limited GPU VRAM without sacrificing significant accuracy has led to the rise of efficient quantization. bitsandbytes is a PyTorch library that solves this by providing k-bit quantization and optimized optimizers, allowing developers to run massive models on consumer-grade hardware with over 8,000 GitHub stars.

What Is bitsandbytes?

bitsandbytes is a lightweight Python wrapper around hardware accelerator functions that enables accessible large language models via k-bit quantization for PyTorch. It is maintained by the bitsandbytes-foundation and is released under the MIT License, providing a suite of tools to reduce the memory footprint of deep learning models during both inference and training.

The library focuses on reducing the numerical precision of model weights and activations, converting them from standard 16-bit or 32-bit floating-point representations to lower-precision formats like 8-bit integers (INT8) or 4-bit numbers (NF4/FP4). This process allows for a dramatic reduction in VRAM consumption, making it possible to load and fine-tune models that would otherwise require enterprise-grade A100s or H100s.

Why bitsandbytes Matters

Before the widespread adoption of bitsandbytes, running a 7B parameter model in full precision would require nearly 28 GB of VRAM just for the weights. For most developers using consumer GPUs like the RTX 3090 or 4090, this was a significant bottleneck. The library fills this gap by enabling “zero-shot” quantization, where models can be quantized on-the-fly as they are loaded into memory, removing the need for a separate, computationally expensive calibration dataset.

The traction of bitsandbytes is evident in its deep integration with the Hugging Face ecosystem. It is the primary engine behind QLoRA (Quantized Low-Rank Adaptation), which has revolutionized the way the community fine-tunes LLMs. By quantizing the base model to 4-bit and adding small trainable adapters, developers can now fine-tune massive models on a single GPU, democratizing access to state-of-the-art AI.

Investing time in bitsandbytes now is critical because it is the industry standard for memory-efficient PyTorch development. Whether you are deploying a local LLM for privacy or fine-tuning a domain-specific model, bitsandbytes provides the necessary primitives to maximize your existing hardware utilization.

Key Features

  • LLM.int8() Quantization: An 8-bit quantization method that enables LLM inference with half the required memory. It uses vector-wise quantization to handle most features in 8-bit while separately treating outliers with 16-bit matrix multiplication to prevent performance degradation.
  • QLoRA (4-bit Quantization): A memory-saving technique for training that quantizes a model to 4-bit (using NF4 or FP4) and inserts a small set of trainable low-rank adaptation weights. This allows for high-quality fine-tuning on limited hardware.
  • 8-bit Optimizers: Block-wise quantization of optimizer states (like Adam, AdamW, RMSProp) that maintains 32-bit performance while reducing the memory cost of the optimizer by up to 75%.
  • Quantized Linear Layers: Provides Linear8bitLt and Linear4bit layers that act as drop-in replacements for standard torch.nn.Linear layers, simplifying the integration into existing PyTorch architectures.
  • Hardware Accelerator Support: Official support for NVIDIA GPUs (CUDA), CPUs, Intel XPUs, and Intel Gaudi (HPU), with experimental support for AMD ROCm and Apple Silicon.
  • Fast Quantile Estimation: Implements high-performance algorithms for quantile estimation that are up to 100x faster than standard implementations, accelerating the quantization process.

How bitsandbytes Compares

Feature bitsandbytes AutoGPTQ AutoAWQ
Calibration Required No (Zero-Shot) Yes Yes
Inference Speed Moderate Fast Fast
Ease of Setup Very High Moderate Moderate
Training Support Excellent (QLoRA) Limited Limited
Hardware Support NVIDIA, Intel, CPU NVIDIA NVIDIA

The primary differentiator for bitsandbytes is its on-the-fly quantization. While AutoGPTQ and AutoAWQ require a calibration dataset to pre-calculate weights, bitsandbytes allows you to load a model in 4-bit or 8-bit immediately. This makes it the superior choice for rapid prototyping and experimentation.

However, there is a tradeoff in inference speed. Because bitsandbytes often dequantizes weights during the forward pass, it is generally slower than GPTQ or AWQ for pure text generation. For developers who need maximum throughput in production, a common workflow is to fine-tune with bitsandbytes (QLoRA) and then export the model to a GPTQ or AWQ format for deployment.

Getting Started: Installation

bitsandbytes requires Python 3.10+ and PyTorch 2.3+ for optimal performance. It is primarily designed for Linux, though experimental support for other platforms exists.

Installation via PyPI

The most straightforward method is using pip. This will install the pre-compiled wheels for the majority of NVIDIA GPU architectures.

pip install bitsandbytes

Installation for Intel GPUs (XPU)

For users on Intel platforms, a specific version is required to leverage XPU acceleration.

pip install bitsandbytes-intel

Compiling from Source

If you are using a non-standard hardware configuration or need the latest development branch, you can compile from source. This requires the CUDA Toolkit to be installed on your system.

git clone https://github.com/bitsandbytes-foundation/bitsandbytes.git
cd bitsandbytes
python setup.py install

Prerequisites: Ensure your NVIDIA driver is up to date and torch.cuda.is_available() returns True in your Python environment.

How to Use bitsandbytes

The simplest way to use bitsandbytes is through the Hugging Face transformers library. By passing a BitsAndBytesConfig, you can load any supported model in a quantized state without writing custom quantization logic.

For those building custom PyTorch models, the library provides direct access to quantized layers. You can replace a standard torch.nn.Linear layer with bnb.nn.Linear8bitLt or bnb.nn.Linear4bit. When the model is loaded, bitsandbytes intercepts the weights and quantizes them into the requested bit-depth.

If you are training a model, you can replace your standard optimizer (e.g., torch.optim.AdamW) with bnb.optim.AdamW8bit. This reduces the memory overhead of the optimizer states, which often consumes more VRAM than the model weights themselves during training.

Code Examples

Loading a Model in 4-bit (via Transformers)

This is the most common use case for running large models on consumer GPUs.

from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

# Configure 4-bit quantization using NormalFloat4 (NF4)
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True
)

model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-2-7b-hf",
    quantization_config=bnb_config,
    device_map="auto"
)

tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")

Using 8-bit Optimizers for Training

This example shows how to reduce training memory by using an 8-bit version of the AdamW optimizer.

import torch
import bitsandbytes as bnb

model = torch.nn.Linear(1024, 1024).cuda()
optimizer = bnb.optim.AdamW8bit(model.parameters(), lr=1e-4)

# Standard training loop
for x, y in data:
    optimizer.zero_grad()
    output = model(x)
    loss = criterion(output, y)
    loss.backward()
    optimizer.step()

Direct Quantized Linear Layer

This example demonstrates the use of a low-level Linear8bitLt layer for custom architectures.

import torch
import bitsandbytes as bnb

# Create an 8-bit linear layer
linear = bnb.nn.Linear8bitLt(
    in_features=256, 
    out_features=128, 
    has_fp16_weights=False, 
    threshold=6.0
)

# Inputs must be float16 for 8-bit linear layers
input_tensor = torch.randn(1, 256, dtype=torch.float16, device="cuda")
output = linear.to("cuda")(input_tensor)
print(f"Output shape: {output.shape}")

Real-World Use Cases

bitsandbytes is essential for several high-impact scenarios in modern AI development:

  • Local LLM Deployment: A developer can run a 7B or 13B parameter model on a single RTX 3060 (12GB VRAM) by loading it in 4-bit. This allows for private, offline AI assistants without relying on expensive cloud APIs.
  • QLoRA Fine-Tuning: A data scientist can fine-tune a Llama-3 model on a single A100 (40GB) or even a consumer GPU by quantizing the base model to 4-bit and training only the LoRA adapters. This reduces the hardware barrier for domain-specific model adaptation.
  • QLoRA Fine-Tuning: A data scientist can fine-tune a Llama-3 model on a single A100 (40GB) or even a consumer GPU by quantizing the base model to 4-bit and training only the LoRA adapters. This reduces the hardware barrier for domain-specific model adaptation.
  • Memory-Constrained Training: An ML engineer can train larger batch sizes or use longer sequence lengths by replacing standard optimizers with 8-bit optimizers, freeing up VRAM for the model and activations.
  • Cross-Modality Quantization: Because bitsandbytes targets torch.nn.Linear layers, it can be used to quantize non-text models, such as Whisper (speech-to-text) or ViT (vision transformers), reducing their deployment footprint.

Contributing to bitsandbytes

The bitsandbytes-foundation encourages contributions to expand hardware support and optimize CUDA kernels. If you are interested in contributing, you should start by setting up pre-commit hooks to ensure code quality and consistency.

The project follows a standard GitHub flow: report bugs via the Issues tab and submit improvements via Pull Requests. The project also maintains a Code of Conduct to ensure a welcoming and professional environment for all contributors.

To get started with development, you can install the pre-commit hooks using pip install pre-commit and then running pre-commit install in the repository root.

Community and Support

The bitsandbytes community is centered around GitHub. The primary channel for technical support and collaboration is GitHub Discussions, where developers can ask questions, report bugs, and share optimization tips.

The library is also deeply integrated with the Hugging Face ecosystem, meaning most community support for quantization is is found within the Hugging Face forums and the transformers library discussions. Because of its wide adoption, there are extensive third-party tutorials and notebooks available for the community.

Conclusion

bitsandbytes is the definitive tool for making large language models accessible to the broader developer community. By providing a seamless way to load models in 4-bit and 8-bit, it removes the hardware barriers that previously limited AI development to those with enterprise-grade clusters.

While it is not the fastest option for pure inference, its ease of use and the power of QLoRA make it the right choice for anyone experimenting with LLMs, fine-tuning models, or deploying on limited hardware. If you are working with PyTorch and need to reduce your VRAM footprint, bitsandbytes is an essential addition to your toolkit.

Star the repo, try the quickstart, and join the community to start running massive models on your own hardware.

What is bitsandbytes and what problem does it solve?

bitsandbytes is a PyTorch library that provides k-bit quantization and optimized optimizers to reduce the memory footprint of large language models. It solves the problem of high VRAM requirements, allowing developers to run and fine-tune massive models on consumer-grade GPUs.

How do I install bitsandbytes?

The easiest way to install bitsandbytes is via pip using the command pip install bitsandbytes. For Intel GPU users, use pip install bitsandbytes-intel. Ensure you have Python 3.10+ and PyTorch 2.3+ installed.

How does bitsandbytes compare to AutoGPTQ?

bitsandbytes is a “zero-shot” quantizer, meaning it does not require a calibration dataset to load a model in 4-bit or 8-bit. In contrast, AutoGPTQ requires a calibration step to pre-calculate weights. bitsandbytes is generally easier to set up but slower for pure inference compared to GPTQ.

Can I use bitsandbytes for fine-tuning?

Yes, bitsandbytes is the core engine for QLoRA (Quantized Low-Rank Adaptation). By quantizing a base model to 4-bit and training only small adapter layers, you can fine-tune massive models on a single GPU with minimal performance loss.

What hardware is supported by bitsandbytes?

bitsandbytes is officially supported on NVIDIA GPUs (CUDA), CPUs, and Intel XPUs/Gaudi. There is ongoing experimental support for AMD ROCm and Apple Silicon. It is primarily optimized for Linux distributions.

Can I use bitsandbytes for non-LLM models?

bitsandbytes targets torch.nn.Linear layers, so it can be used to quantize any PyTorch model that uses linear layers, including vision transformers (ViT) or speech models like Whisper.

What license does bitsandbytes use?

bitsandbytes is released under the MIT License, which allows for free use, modification, and distribution of the software.