bitsandbytes: Efficient k-bit Quantization for PyTorch LLMs

Jun 18, 2025

Introduction

Deploying large language models (LLMs) often requires massive amounts of GPU VRAM, making high-end hardware inaccessible to many developers. bitsandbytes is a lightweight PyTorch library that solves this by providing k-bit quantization, allowing multi-billion parameter models to run on consumer-grade GPUs. With over 8.3k GitHub stars, it has become the industry standard for reducing memory footprints without sacrificing significant model accuracy.

What Is bitsandbytes?

bitsandbytes is a lightweight wrapper around CUDA custom functions that enables accessible large language models via k-bit quantization for PyTorch. It provides the software engine necessary to perform efficient low-bit computations, specifically focusing on 8-bit and 4-bit precision for model weights and optimizers.

Maintained by the bitsandbytes-foundation and primarily authored by Tim Dettmers, the library is released under the MIT license. It integrates seamlessly with the Hugging Face Transformers ecosystem, acting as a runtime accelerator rather than a static file format.

Why bitsandbytes Matters

Before bitsandbytes, running a 7B parameter model in full 16-bit precision required approximately 14GB of VRAM just for the weights. For many developers, this meant the barrier to entry for LLM experimentation was a high-cost A100 or H100 GPU. bitsandbytes democratizes access to AI by reducing these requirements by 50% (for 8-bit) or 75% (for 4-bit), enabling models to fit on GPUs with as little as 8GB or 12GB of VRAM.

The library’s significance is further amplified by its role in QLoRA (Quantized Low-Rank Adaptation). By allowing the base model to be quantized to 4-bit while keeping a small set of trainable adapters in higher precision, bitsandbytes enables the fine-tuning of massive models on a single consumer GPU, a feat previously reserved for industrial clusters.

Key Features

  • LLM.int8() Quantization: An 8-bit quantization method that reduces memory usage by 50% without significant performance degradation. It uses vector-wise quantization and separately treats outlier features with 16-bit matrix multiplication to preserve accuracy.
  • QLoRA 4-bit Quantization: A highly aggressive compression technique that reduces memory by ~75%. It utilizes the NF4 (NormalFloat 4) data type, which is mathematically optimized for normally distributed weights, making it ideal for LLM fine-tuning.
  • 8-bit Optimizers: Block-wise quantization of optimizer states (like AdamW) to maintain 32-bit performance while using a fraction of the memory cost, which is critical during the training phase.
  • Multi-Backend Support: While primarily built for NVIDIA CUDA (SM60+), the library now provides official support for CPUs, Intel XPUs, and Intel Gaudi (HPU), with experimental support for AMD ROCm and Apple Silicon.
  • Hugging Face Integration: Direct integration with transformers and accelerate, allowing users to load quantized models using a simple BitsAndBytesConfig object.
  • Mixed-Precision Computation: The library performs on-the-fly de-quantization, allowing weights to be stored in 4-bit or 8-bit but computed in BF16 or FP16 for stability.

How bitsandbytes Compares

Feature bitsandbytes AutoGPTQ AutoAWQ
Quantization Type Runtime / Zero-Shot Post-Training (PTQ) Post-Training (PTQ)
Calibration Data Required No Yes Yes
Inference Speed Slower (Dequantization overhead) Fast Very Fast
Ease of Setup Very High (pip install) Medium Medium
Fine-Tuning Support Excellent (QLoRA) Limited Limited

The primary differentiator for bitsandbytes is its zero-shot quantization. Unlike AutoGPTQ or AutoAWQ, which require a calibration dataset to optimize weights before saving a quantized model to disk, bitsandbytes quantizes the model on-the-fly as it loads into VRAM. This makes it the fastest way to test a new model architecture immediately after its release.

However, there is a tradeoff in performance. Because bitsandbytes must de-quantize weights during the forward pass, it is generally slower for production inference than GPTQ or AWQ. For a typical workflow, developers often use bitsandbytes for rapid prototyping and QLoRA fine-tuning, then export the final model to a GPTQ or AWQ format for high-throughput serving.

Getting Started: Installation

bitsandbytes requires Python >= 3.10 and PyTorch >= 2.4. It is designed to work across multiple hardware backends.

NVIDIA CUDA Installation

The most straightforward method is via PyPI, which bundles precompiled CUDA libraries for versions 11.8 through 13.0.

pip install bitsandbytes

Intel XPU and Gaudi Installation

Official support for Intel accelerators is available via PyPI wheels.

pip install bitsandbytes

AMD ROCm (Preview)

AMD support is currently in preview. You can install via PyPI or compile from source using the ROCm-enabled branch.

git clone --recurse https://github.com/ROCm/bitsandbytes
cd bitsandbytes
git checkout rocm_enabled
pip install -r requirements-dev.txt
cmake -DCOMPUTE_BACKEND=hip -S .
make
pip install .

CPU-Only Installation

bitsandbytes can be installed for CPU-only environments, though quantization functions will be significantly slower.

pip install bitsandbytes

How to Use bitsandbytes

The most common way to use bitsandbytes is through the Hugging Face transformers library. Instead of manually managing quantized layers, you use a BitsAndBytesConfig to define your quantization strategy.

When you load a model with load_in_4bit=True, bitsandbytes intercepts the model loading process, quantizes the weights to NF4, and maps the model to the GPU using accelerate. This allows you to run a model that would normally require 32GB of VRAM on a GPU with only 8GB.

Code Examples

Example 1: Loading a Model in 8-bit (LLM.int8())

This is the simplest way to reduce memory by 50% with minimal accuracy loss.

from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

model_id = "meta-llama/Llama-2-7b-hf"

# Configure 8-bit quantization
config = BitsAndBytesConfig(load_in_8bit=True)

model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    quantization_config=config, 
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_id)

# The model is now loaded in 8-bit precision
print("Model loaded in 8-bit!")

Example 2: Loading a Model in 4-bit (NF4) for Inference

This configuration is optimized for maximum memory reduction (75%) and high reasoning capabilities.

from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

model_id = "meta-llama/Llama-3-8B"

# Configure 4-bit quantization
# bnb_4bit_compute_dtype=torch.bfloat16 is recommended for newer GPUs (Ampere+)
config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True
)

model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    quantization_config=config, 
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_id)

# The model is now loaded in 4-bit precision
print("Model loaded in 4-bit!")

Example 3: Using an 8-bit Optimizer for Training

If you are training a model from scratch or fine-tuning, you can replace the standard PyTorch AdamW optimizer with an 8-bit version to save VRAM.

import torch
import bitsandbytes as bnb

# Replace torch.optim.AdamW with bnb.optim.AdamW8bit
optimizer = bnb.optim.AdamW8bit(
    model.parameters(), 
    lr=2e-5
)

# Training loop
# loss.backward()
# optimizer.step()
print("Using 8-bit optimizer to save VRAM during training!")

Real-World Use Cases

  • Consumer GPU LLM Hosting: A developer with an RTX 3060 (12GB VRAM) can use 4-bit quantization to run a Llama-3 8B model, which would otherwise require over 16GB of VRAM in FP16.
  • Low-Resource Fine-Tuning (QLoRA): A researcher can fine-tune a 70B parameter model on a single A100 (80GB) by quantizing the base model to 4-bit and training only the LoRA adapters.
  • Rapid Prototyping: An AI engineer can test a newly released model from Hugging Face immediately without needing to wait for a pre-quantized GPTQ or AWQ version to be uploaded by the community.
  • Edge Deployment: Reducing the memory footprint of a model to fit it into the limited VRAM of an edge device or a smaller cloud instance to reduce hosting costs.

Contributing to bitsandbytes

The bitsandbytes project is open-source and encourages contributions from the community. Because the library involves complex CUDA kernels, contributions typically focus on adding support for new hardware backends (like ROCm or XPU) and optimizing existing kernels.

To contribute, users should first report bugs via GitHub Issues. If you are proficient in CUDA C++, you can submit a pull request to improve the library’s performance or add support for new GPU architectures. The project follows the standard GitHub flow for contributions.

Community and Support

The primary hub for support is the GitHub Discussions tab of the bitsandbytes-foundation repository. Since the library is integrated into the Hugging Face ecosystem, much of the community support also happens within the Hugging Face forums and Discord servers.

The official documentation is hosted on Hugging Face, providing detailed guides on installation and the API reference. For real-time troubleshooting, the GitHub Issues tracker is the most reliable source for finding existing solutions to CUDA version mismatches.

Conclusion

bitsandbytes is an essential tool for anyone working with large language models on limited hardware. By providing a seamless way to integrate 8-bit and 8-bit quantization, it removes the hardware barrier to entry for LLM development. While it is not the fastest option for production serving, its ease of use and the la cornerstone of the QLoRA fine-tuning method.

If you are a developer looking to run larger models on smaller GPUs, or a researcher interested in low-resource fine-tuning, bitsandbytes is the right choice. Star the repo, try the quickstart, and join the community to make LLMs more accessible to all.

What is bitsandbytes and what problem does it solve?

bitsandbytes is a PyTorch library that provides k-bit quantization for LLMs, solving the problem of high GPU VRAM requirements. It allows users to run multi-billion parameter models on consumer-grade GPUs by reducing the memory footprint of weights and optimizers.

How do I install bitsandbytes?

The easiest way to install bitsandbytes is via pip: pip install bitsandbytes. For NVIDIA GPUs, it ensures compatibility with CUDA 11.8 through 13.0. Official support for Intel XPU and Gaudi is also available via the same command.

How does bitsandbytes compare to AutoGPTQ and AutoAWQ?

bitsandbytes uses zero-shot runtime quantization, meaning it doesn’t require calibration data and can be used on any model immediately. In contrast, AutoGPTQ and AutoAWQ are post-training quantization (PTQ) methods that are generally faster for inference but require a pre-quantization step.

Can I use bitsandbytes for fine-tuning?

Yes, bitsandbytes is the core engine behind QLoRA, which allows for the efficient fine-tuning of quantized 4-bit models using low-rank adapters. This makes it possible to fine-tune massive models on a single GPU.

Does bitsandbytes support non-NVIDIA GPUs?

Yes, bitsandbytes now provides official support for CPUs, Intel XPUs, and Intel Gaudi (HPU). There is also experimental support for AMD ROCm and Apple Silicon.

What is the difference between 4-bit and 8-bit quantization in bitsandbytes?

8-bit quantization (LLM.int8()) reduces memory by 50% and is generally more accurate. 4-bit quantization (NF4) is more aggressive, reducing memory by ~75% and is optimized for the weight distribution of LLMs.

What are the system requirements for bitsandbytes?

Minimum requirements include Python >= 3.10 and PyTorch >= 2.4. For NVIDIA GPUs, a Compute Capability of 6.0+ (Pascal or newer) is necessary.

Can I use bitsandbytes with Hugging Face Transformers?

bitsandbytes is natively integrated into the Hugging Face Transformers library. You can load models in 8-bit or 4-bit using the BitsAndBytesConfig class.