Home

Awesome

<h1 align="center"> <p>LLM-FP4: 4-Bit Floating-Point Quantized Transformers</p> </h1> <h1 align="center"> <img src="./img/fpq.png" width="600"> </h1>

This is the pytorch implementation of our paper LLM-FP4: 4-Bit Floating-Point Quantized Transformers, published in EMNLP 2023 main conference. LLM-FP4 is able to quantize both weights and activations in large language models (LLMs) down to 4-bit floating-point values, in a post-training manner. The methods includes (1) a search-based framework for determining the optimal exponent bias and maximal quantization value; (2) pre-shifted exponent bias, which effectively addresses the challenge of high inter-channel variance in transformers.

Setup

  1. Install dependencies
pip install -r requirements.txt
  1. Model Used:
MODEL_ADDR=huggyllama/llama-7b
MODEL_ADDR=huggyllama/llama-13b

Code Structure

Refer to ./quant_layers/* for FP quantization simulation

Refer to ./utils/quant_calib.py for FP quantization calibration detail

Refer to ./complete_scripts/* for the complete scripts to reproduce the results reported in the paper

Calibration and Evaluation (main.py)

This file contains the code to perform FP-PTQ calibration and evaluation. User can specify different quantization configuration to obtain different quantized model and evalute the quantized model with commonsense reasoning tasks.

Example usage for multiple GPUs:

export CUDA_VISIBLE_DEVICES=0,1
MODEL_ADDR=huggyllama/llama-7b
python main.py --model hf-causal-experimental --model_args pretrained=$MODEL_ADDR,use_accelerate=True \
--tasks arc_challenge,arc_easy,boolq,hellaswag,openbookqa,piqa,winogrande --device cuda --batch_size auto \
--no_cache --num_fewshot 0 --quant_config 'FPQ_config_llama' --qbits 4 4 4 2 2 2 --calib_size 32 --search_round 3 \
--search_intervals 0.01 1.2 100

The search results a.k.a the quantization parameters will be saved under ./search_result after the calibration is done.

Evaluation (main.py)

To evaluate the performance of the quantized model on the commonsense reasoning tasks, first get the path of the quantization parameters and use the following command:

export CUDA_VISIBLE_DEVICES=0,1
MODEL_ADDR=huggyllama/llama-7b
python main.py --model hf-causal-experimental --model_args pretrained=$MODEL_ADDR,use_accelerate=True \
--tasks arc_challenge,arc_easy,boolq,hellaswag,openbookqa,piqa,winogrande --device cuda --batch_size auto \
--no_cache --num_fewshot 0 --quant_config 'FPQ_config_llama' --qbits 4 4 4 2 2 2 --only_eval \
--ptq_param_path "./search_result/FPQ_config_llama/W4A4E4_search_round3_search_intervals(0.01,1.2,100).pt"

PTQ Result

Below is the results in LLaMA-7B and LLaMA-13B with six commonsense reasoning datasets.

Quant Method#Bits (E/W/A)#CalibBoolQPIQAHellaSwagWinoGrandeARC-eARC-cAverage
LLaMA-7B Full-precision16/16/16-75.178.756.969.975.341.966.3
MinMax INT Quant4/4/43254.151.725.649.824.722.938.1
MinMax FP Quant (E2M1)4/4/43247.353.125.750.725.122.437.4
SmoothQuant (Xiao et al., 2022)16/4/451254.162.841.552.650.632.949.1
LLM-QAT (Liu et al., 2023)16/4/4(QAT)63.564.355.652.950.330.252.8
FPQ baseline4/4/43257.456.630.251.137.723.242.7
FPQ4/4/43264.273.547.863.765.933.658.1
Quant Method#Bits (E/W/A)#CalibBoolQPIQAHellaSwagWinoGrandeARC-eARC-cAverage
LLaMA-13B Full-precision16/16/16-77.979.259.972.677.446.468.9
MinMax INT Quant4/4/43254.552.725.551.125.322.138.5
MinMax FP Quant (E2M1)4/4/43245.851.725.549.525.022.836.7
SmoothQuant (Xiao et al., 2022)16/4/451257.661.356.052.649.925.150.4
FPQ baseline4/4/43254.357.735.752.241.125.744.5
FPQ4/4/43271.974.853.366.771.739.963.1

Citing LLM-FP4

If you use LLM-FP4 in your publication, please cite it by using the following BibTeX entry.

@inproceedings{liu-etal-2023-llm,
    title = "{LLM}-{FP}4: 4-Bit Floating-Point Quantized Transformers",
    author = "Liu, Shih-yang  and
      Liu, Zechun  and
      Huang, Xijie  and
      Dong, Pingcheng  and
      Cheng, Kwang-Ting",
    editor = "Bouamor, Houda  and
      Pino, Juan  and
      Bali, Kalika",
    booktitle = "Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing",
    month = dec,
    year = "2023",
    address = "Singapore",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2023.emnlp-main.39",
    pages = "592--605",
    abstract = "We propose LLM-FP4 for quantizing both weights and activations in large language models (LLMs) down to 4-bit floating-point values, in a post-training manner. Existing post-training quantization (PTQ) solutions are primarily integer-based and struggle with bit widths below 8 bits. Compared to integer quantization, floating-point (FP) quantization is more flexible and can better handle long-tail or bell-shaped distributions, and it has emerged as a default choice in many hardware platforms. One characteristic of FP quantization is that its performance largely depends on the choice of exponent bits and clipping range. In this regard, we construct a strong FP-PTQ baseline by searching for the optimal quantization parameters. Furthermore, we observe a high inter-channel variance and low intra-channel variance pattern in activation distributions, which adds activation quantization difficulty. We recognize this pattern to be consistent across a spectrum of transformer models designed for diverse tasks such as LLMs, BERT, and Vision Transformer models. To tackle this, we propose per-channel activation quantization and show that these additional scaling factors can be reparameterized as exponential biases of weights, incurring a negligible cost. Our method, for the first time, can quantize both weights and activations in the LLaMA-13B to only 4-bit and achieves an average score of 63.1 on the common sense zero-shot reasoning tasks, which is only 5.8 lower than the full-precision model, significantly outperforming the previous state-of-the-art by 12.7 points. Code is available at: https://github.com/nbasyl/LLM-FP4.",
}

Acknowledgement

We greatly appreciate the contributions of three remarkable repositories: FP8 Quantization: The Power of the Exponent, PTQ4ViT, Language Model Evaluation Harness. These projects have significantly benefited our work.