# HQQ

> Calibration-free quantization for LLMs and vision models down to 1 bit

- Category: Models & Inference / Quantization & Compression
- Language: Python
- Repository: https://github.com/dropbox/hqq
- Canonical page: https://ai-tldr.dev/tools/hqq/

## In short

HQQ (Half-Quadratic Quantization) is a method for shrinking the weights of large models so they take up less memory. It sits in the Quantization & Compression category of the Models & Inference tools and is open-source, written in Python. To get started: install HQQ.

## Overview

HQQ (Half-Quadratic Quantization) is a method for shrinking the weights of large models so they take up less memory. It compresses weights to lower bit widths (8, 4, 3, 2, or even 1 bit) and, unlike many other quantization methods, it does not need any calibration dataset to do so.

Because it skips calibration, HQQ can quantize even very large models in a few minutes. It works on any model type, including LLMs and vision models, and integrates with HuggingFace Transformers as well as its own library for saving and loading quantized checkpoints.

It fits in the quantization and compression part of an inference stack: you quantize a model once, then run it with less VRAM. The dequantization step is a plain linear operation, so it stays compatible with torch.compile and optimized CUDA/Triton inference kernels.

## What it does

- Quantizes to 8, 4, 3, 2, or 1 bits with no calibration data required
- Works on any model, including LLMs and vision models
- Integrated into HuggingFace Transformers via HqqConfig, with save_pretrained / from_pretrained support
- Linear dequantization step compatible with optimized CUDA/Triton kernels
- Aims for full torch.compile compatibility to speed up training and inference
- Compatible with PEFT training; HQQ+ adds trainable low-rank adapters for better quality at low bits

## Getting started

Make sure you have a PyTorch 2 build that matches your CUDA version, then install HQQ and quantize a model. The README recommends starting with nbits=4, group_size=64, axis=1 for a good balance of quality, VRAM, and speed.

1. Install HQQ

   Install the latest stable release from PyPI, or the latest updates straight from GitHub (recommended in the README).

```bash
# latest stable version
pip install hqq

# latest updates - recommended
pip install git+https://github.com/dropbox/hqq.git
```

2. Quantize a linear layer

   Replace a torch.nn.Linear layer with an HQQLinear configured by a BaseQuantizeConfig.

```python
from hqq.core.quantize import *

quant_config = BaseQuantizeConfig(nbits=4, group_size=64)

hqq_layer = HQQLinear(your_linear_layer,
                      quant_config=quant_config,
                      compute_dtype=torch.float16,
                      device='cuda',
                      initialize=True,
                      del_orig=True)

y = hqq_layer(x)  # forward-pass
```

3. Quantize a Transformers model

   Pass an HqqConfig as quantization_config when loading a model from HuggingFace; all linear layers use the same config.

```python
from transformers import AutoModelForCausalLM, HqqConfig

quant_config = HqqConfig(nbits=4, group_size=64)

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="cuda",
    quantization_config=quant_config,
)
```

## When to use it

- Fit a large LLM into limited GPU VRAM by quantizing its weights to 4 bits or lower
- Quickly quantize very large models without preparing a calibration dataset
- Compress vision or language models for cheaper inference while staying torch.compile compatible
- Run low-bit quantized models through HuggingFace Transformers with standard save/load workflows
