deepblue
All Posts

Quantization using bitsandbytes

AuthorsBen Shalom Albert
Published
Organizationdeepblue Research
CategoryTechnology, Machine Learning
blue sky and a large cloud illustration

Overview

The article introduces bitsandbytes as a way to reduce model memory use through 8-bit and 4-bit quantization, with an example aimed at Google Colab.

Installation

1pip install transformers accelerate bitsandbytes -qU

The article explains that Transformers provides pretrained model loading, Accelerate helps with hardware-aware placement and loading, and bitsandbytes provides low-bit quantization support.

Import PyTorch:

1import torch

How 8-bit quantization works

The article describes dividing weights into outliers and non-outliers:

  • Outlier values (unusually high or low values) are retained at higher precision, described as 16-bit floating point.
  • Most non-outlier values are represented in 8-bit integer format.
  • The article describes converting quantized non-outlier values back to FP16 for computation.

The goal is to reduce memory consumption while retaining more precision for values that may be especially important.

Load an 8-bit model

1from transformers import AutoModelForCausalLM, BitsAndBytesConfig
2
3quantization_config = BitsAndBytesConfig(load_in_8bit=True)
4
5model_8bit = AutoModelForCausalLM.from_pretrained(
6 "facebook/opt-350m",
7 quantization_config=quantization_config,
8 torch_dtype=torch.float32
9)

The example uses facebook/opt-350m. torch_dtype specifies the dtype for model components that are not quantized. Quantization commonly targets selected layers, while other modules may remain at higher precision.

Inspect memory use

1print(model_8bit.get_memory_footprint())

Summary

The article's central point is that bitsandbytes can reduce model memory use through low-bit quantization. Its closing summary mentions both 8-bit and 4-bit quantization, configuration, offloading, and possible dequantization.

Source coverage note: The accessible article body primarily details the 8-bit example.

Related Posts