Home

Awesome

FlatQuant: Flatness Matters for LLM Quantization

arXiv

This repository contains the PyTorch implementation of FlatQuant: Flatness Matters for LLM Quantization.

FlatQuant leverages Fast and Learnable Affine Transformations tailored for each linear layer to alleviate outliers in LLMs. Additionally, as indicated by the name, it also achieves pretty flat weights and activations that are friendly to quantization. FlatQuant significantly enhances the quantization accuracy under a low-bit quantization setting (i.e., W4A4) while introducing little inference overhead, which may help promote the deployment of W4A4-quantized LLMs.

method

News

Contents

Preparations

Installation

conda create -n flatquant python=3.10 -y
conda activate flatquant
pip install -r requirements.txt && pip install -e . && pip install triton==3.0.0

Note: To run models like LLaMA-3.1 or Qwen-2.5, we use transformers==4.45.0 instead.

Data Preparation

Download datasets in ./datasets.

Calibration set or PPL evaluation

DatasetLocal DirURL
WikiText2./datasets/wikitexthttps://huggingface.co/datasets/wikitext
C4./datasets/allenai/c4https://huggingface.co/datasets/allenai/c4
Pile./datasets/pile-val-backuphttps://huggingface.co/datasets/mit-han-lab/pile-val-backup

Commonsense QA evaluation

DatasetLocal DirURL
ARC-E and ARC-C./datasets/ai2_archttps://huggingface.co/datasets/ai2_arc
HellaSwag./datasets/hellaswaghttps://huggingface.co/datasets/hellaswag
LAMBADA./datasets/lambada_openaihttps://huggingface.co/datasets/EleutherAI/lambada_openai
PIQA./datasets/piqahttps://huggingface.co/datasets/ybisk/piqa
WinoGrande./datasets/winograndehttps://huggingface.co/datasets/winogrande

Model Preparation

Download models in ./modelzoo.

ModelLocal DirURL
LLaMA-2-7B./modelzoo/llama-2/llama-2-7bhttps://huggingface.co/meta-llama/Llama-2-7b
LLaMA-2-13B./modelzoo/llama-2/llama-2-13bhttps://huggingface.co/meta-llama/Llama-2-13b
LLaMA-2-70B./modelzoo/llama-2/llama-2-70bhttps://huggingface.co/meta-llama/Llama-2-70b
LLaMA-3-8B./modelzoo/llama-3/llama-3-8bhttps://huggingface.co/meta-llama/Meta-Llama-3-8B
LLaMA-3-70B./modelzoo/llama-3/llama-3-70bhttps://huggingface.co/meta-llama/Meta-Llama-3-70B

Usage

Calibration

We provide full script to run FlatQuant in ./scripts/. We use LLaMa-3-8B as an example here:

  1. Weight-Activation-KV Cache Quantization
# W4A4KV4
python ./main.py \
    --model ./modelzoo/llama-3/llama-3-8b \
    --w_bits 4 --a_bits 4 \
    --k_bits 4 --k_asym --k_groupsize 128 \
    --v_bits 4 --v_asym --v_groupsize 128 \
    --cali_bsz 4 --epoch 15 --flat_lr 5e-3 \
    --lwc --lac --cali_trans --add_diag \
    --output_dir ./outputs --save_matrix \
    --lm_eval --lm_eval_batch_size 16
  1. Weight-Only Quantization
# W4A16
python ./main.py \
    --model ./modelzoo/llama-3/llama-3-8b \
    --w_bits 4 \
    --cali_bsz 4 --epoch 15 --flat_lr 5e-3 \
    --lwc --lac --cali_trans --add_diag \
    --output_dir ./outputs --exp_name wonly --save_matrix \
    --lm_eval --lm_eval_batch_size 16
  1. Reproduce Evaluation Results of Our Paper

    1) Download the pretrained FlatQuant parameters you want through modelzoo.

    2) Inference with --reload_matrix and --matrix_path PATH_TO_XXX, take LLaMa-3-8B with W4A4KV4 quantization as an example:

python ./main.py \
    --model ./modelzoo/llama-3/llama-3-8b \
    --w_bits 4 --a_bits 4 \
    --k_bits 4 --k_asym --k_groupsize 128 \
    --v_bits 4 --v_asym --v_groupsize 128 \
    --cali_bsz 4 --epoch 15 --flat_lr 5e-3 \
    --lwc --lac --cali_trans --add_diag \
    --output_dir ./outputs --save_matrix \
    --lm_eval --lm_eval_batch_size 16 \
    --reload_matrix --matrix_path PATH_TO_XXX 

More detailed and optional arguments:

Inference Latency

To measure the speedup of FlatQuant and our efficient kernel, run the corresponding benchmark commands provided below:

# Run end-to-end latency benchmark
python ./benchmarks/layer_benchmark.py
# Run kernel latency benchmark
python ./benchmarks/kernel_benchmark.py
# Run linear layer latency benchmark
python ./benchmarks/qlinear_benchmark.py
# Run attention latency benchmark
python ./benchmarks/qattention_benchmark.py

Apply to other models

To apply FlatQuant in your own models, some modifications are required in the forward pass of the model, particularly within the Attention and MLP modules. You can refer to flatquant/model_tools for our implementations of LLaMA2, LLaMA3, LLaMA3.1, and Qwen2.5.

Efficient Kernel

The detailed implementation of our efficient kernel can be found in deploy/kernels/kron_matmul.py and deploy/kernels/block_matmul.py.

Model Zoo

The model zoo is coming soon.

Results

Accuracy Results

Table 1: WikiText-2 perplexity of 4-bit weight & acitvation quantized LLaMA models.

MethodW Quantizer2-7B2-13B2-70B3-8B3-70B
FP16-5.474.883.326.142.86
SmoothQuantRTN83.1235.8826.01210.199.60
OmniQuantRTN14.7412.28---
AffineQuantRTN12.6911.45---
QuaRotRTN8.566.104.1410.6055.44
SpinQuantRTN6.145.443.827.967.58
FlatQuantRTN5.795.123.556.983.78
QUIK-4BGPTQ8.877.786.91--
QuaRotGPTQ6.105.403.798.166.60
SpinQuantGPTQ5.965.243.707.396.21
FlatQuantGPTQ5.785.113.546.903.77

Table 2: C4 perplexity of 4-bit weight & acitvation quantized LLaMA models.

MethodW Quantizer2-7B2-13B2-70B3-8B3-70B
FP16-7.266.735.719.457.17
SmoothQuantRTN77.2743.1934.61187.9316.90
OmniQuantRTN21.4016.24---
AffineQuantRTN15.7613.97---
QuaRotRTN11.868.676.4217.1979.48
SpinQuantRTN9.198.116.2613.4515.39
FlatQuantRTN7.797.095.9111.137.86
QUIK-4BGPTQ-----
QuaRotGPTQ8.327.546.1213.3812.87
SpinQuantGPTQ8.287.486.0712.1912.82
FlatQuantGPTQ7.867.115.9211.217.93

Table 3: Zero-shot QA task results of 4-bit weight & activation quantized LLaMA models.

MethodW Quantizer2-7B2-13B2-70B3-8B3-70B
FP16-69.7972.5577.0573.2379.95
QuaRotRTN57.7366.2573.4761.3435.36
SpinQuantRTN63.5268.5675.0966.9865.66
FlatQuantRTN67.9671.4276.6271.2379.01
QuaRotGPTQ65.0168.9175.6865.7970.45
SpinQuantGPTQ66.2370.9376.0668.7071.66
FlatQuantGPTQ67.4771.6476.5371.3378.58

Latency Results

Table 4: Prefill speedup of LLaMA-2-7B model across different batch sizes on one RTX3090 GPU. We decode 256 tokens after the prefill on a sequence length of 2048.

Batch SizeInt4QuaRotFlatQuant
12.171.972.12
22.211.992.16
42.252.042.21
82.282.052.23
162.322.082.27
322.352.092.28
642.372.112.30

Table 5: Decoding speedup of LLaMA-2-7B model across different batch sizes on one RTX3090 GPU. We decode 256 tokens after the prefill on a sequence length of 2048.

Batch SizeInt4QuaRotFlatQuant
10.810.700.71
20.780.660.69
40.820.740.73
80.970.830.83
161.181.011.05
321.501.381.43
641.831.751.76

Acknowledgements

This project is based on the work of the following projects:

We are grateful for the contributions provided by these projects.

References

If you find FlatQuant helpful, please cite our paper:

@article{sun2024flatquant,
  title={FlatQuant: Flatness Matters for LLM Quantization},
  author={Sun, Yuxuan and Liu, Ruikang and Bai, Haoli and Bao, Han and Zhao, Kang and Li, Yuening and Hu, Jiaxin and Yu, Xianzhi and Hou, Lu and Yuan, Chun and others},
  journal={arXiv preprint arXiv:2410.09426},
  year={2024}
}