Home

Awesome

QServe: W4A8KV4 Quantization and System Co-design for Efficient LLM Serving

[Paper] [DeepCompressor Library] [Website]

QServe: Efficient and accurate LLM serving system on GPUs with W4A8KV4 quantization (4-bit weights, 8-bit activations, and 4-bit KV cache). Compared with leading industry solution TensorRT-LLM, QServe achieves 1.2x-1.4x higher throughput when serving Llama-3-8B, and 2.4x-3.5x higher throughput when serving Qwen1.5-72B, on L40S and A100 GPUs. QServe also allows users to achieve A100-level throughput on 3x cheaper L40S GPUs.

QServe is suitable for large-scale synthetic data generation with both LLMs and VLMs. Check out our latest QServe-VLM release!

teaser efficiency

Introduction

Quantization can accelerate large language model (LLM) inference. Going beyond INT8 quantization, the research community is actively exploring even lower precision, such as INT4. Nonetheless, state-of-the-art INT4 quantization techniques only accelerate low-batch, edge LLM inference, failing to deliver performance gains in large-batch, cloud-based LLM serving. We uncover a critical issue: existing INT4 quantization methods suffer from significant runtime overhead (20-90%) when dequantizing either weights or partial sums on GPUs. To address this challenge, we introduce QoQ, a W4A8KV4 quantization algorithm with 4-bit weight, 8-bit activation, and 4-bit KV cache. QoQ stands for quattuor-octo-quattuor, which represents 4-8-4 in Latin. QoQ is implemented by the QServe inference library that achieves measured speedup. The key insight driving QServe is that the efficiency of LLM serving on GPUs is critically influenced by operations on low-throughput CUDA cores. Building upon this insight, in QoQ algorithm, we introduce progressive quantization that can allow low dequantization overhead in W4A8 GEMM. Additionally, we develop SmoothAttention to effectively mitigate the accuracy degradation incurred by 4-bit KV quantization. In the QServe system, we perform compute-aware weight reordering and take advantage of register-level parallelism to reduce dequantization latency. We also make fused attention memory-bound, harnessing the performance gain brought by KV4 quantization. As a result, QServe improves the maximum achievable serving throughput of Llama-3-8B by 1.2× on A100, 1.4× on L40S; and Qwen1.5-72B by 2.4× on A100, 3.5× on L40S, compared to TensorRT-LLM. Remarkably, QServe on L40S GPU can achieve even higher throughput than TensorRT-LLM on A100. Thus, QServe effectively reduces the dollar cost of LLM serving by .

The current release supports:

News

Contents

Installation

  1. Clone this repository and navigate to the corresponding folder:
git clone https://github.com/mit-han-lab/qserve
cd qserve
  1. Install QServe

2.1 LLM setup tutorial

If you hope to serve text-only LLMs, please follow the tutorial below:

conda create -n QServe python=3.10 -y
conda activate QServe
pip install --upgrade pip  # enable PEP 660 support
# This is optional if you prefer to use built-in nvcc
conda install -c nvidia cuda-toolkit -y
pip install -e .
pip install flash-attn --no-build-isolation

We recommend starting an interactive python CLI interface and run

import flash_attn

to check whether FlashAttention-2 is installed successfully. If not, we recommend downloading pre-built wheels from here. Please notice:

2.2 VLM setup tutorial

QServe also supports synthetic caption generation with VILA VLMs. Please follow the tutorial below:

First set up environment for QServe:

conda create -n QServe python=3.10 -y
conda activate QServe
pip install --upgrade pip  # enable PEP 660 support
# This is optional if you prefer to use built-in nvcc
conda install -c nvidia cuda-toolkit -y
pip install -e .

Then install VILA and VILA's dependencies:

cd ~
git clone https://github.com/NVlabs/VILA
cd VILA
pip install https://github.com/Dao-AILab/flash-attention/releases/download/v2.5.8/flash_attn-2.5.8+cu122torch2.3cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
# Install VILA
pip install -e .
pip install -e ".[train]"

Please notice that VILA has a relatively strict dependency on the versions of PyTorch/TorchVision libraries, so it is necessary to make sure the library versions are compatible with VILA.

  1. Compile the CUDA kernels.

Please return to the QServe directory and execute the following commands:

cd kernels
python setup.py install
  1. If you want to clone our model zoo, please make sure that git-lfs is installed.

QServe Model Zoo

We provide pre-quantized checkpoints for multiple model families. For example, for Llama-3-8B model, please run the following commands to download:

# git lfs install  # install git lfs if not already
mkdir -p qserve_checkpoints && cd qserve_checkpoints
git clone https://huggingface.co/mit-han-lab/Llama-3-8B-QServe 

For other models, please refer to the detailed support list for the links to download:

ModelsW4A8-per-channelW4A8-g128
Llama38B/70B8B/70B
Llama3-Instruct8B/70B8B/70B
Llama27B/13B/70B7B/13B/70B
Vicuna7B/13B/30B7B/13B/30B
Mistral7B7B
Yi34B34B
Qwen✅ 72B✅ 72B

For flagship datacenter GPUs such as the A100, it is recommended to use QServe-per-channel, while for inference datacenter GPUs like the L40S, QServe-per-group is the recommended approach.

If you are interested in generating the quantized checkpoints on your own, please follow the instructions in DeepCompressor Library to run QoQ quantization and dump the fake-quantized models. We then provide checkpoint converter to real-quantize and pack the model into QServe format:

python checkpoint_converter.py --model-path <hf-model-path> --quant-path <fake-quant-model-path> --group-size -1 --device cpu
# <fake-quant-model-path> is a directory generated by DeepCompressor, including model.pt and scale.pt

We also provide a script to run the checkpoint converter. The final model will be automatically stored under qserve_checkpoints.

Usage and Examples

We support both offline benchmarking and online generation (in-flight-batching) in QServe.

  1. Offline speed benchmarking (Batched input sequences, fixed context length = 1024 and generation length = 512). We take Llama-3-8B (per-channel quant) as an example here. Please make sure that you have already downloaded the QoQ-quantized QServe model.
export MODEL_PATH=./qserve_checkpoints/Llama-3-8B-QServe # Please set the path accordingly

GLOBAL_BATCH_SIZE=128 \
python qserve_benchmark.py \
  --model $MODEL_PATH \
  --benchmarking \
  --precision w4a8kv4 \
  --group-size -1

If you hope to use larger batch sizes such as 256, you may need to change NUM_GPU_PAGE_BLOCKS to a larger value than the automatically-determined value on A100. For example:

export MODEL_PATH=./qserve_checkpoints/Llama-3-8B-QServe # Please set the path accordingly

GLOBAL_BATCH_SIZE=256 \
NUM_GPU_PAGE_BLOCKS=6400 \
python qserve_benchmark.py \
  --model $MODEL_PATH \
  --benchmarking \
  --precision w4a8kv4 \
  --group-size -1
  1. This is an online demonstration of batched generation, showcasing in-flight batching, paged attention of W4A8KV4 QoQ LLMs. We will randomly sample a set of safety-moderated conversations from the WildChat dataset and process them efficiently through in-flight batching.
export MODEL_PATH=./qserve_checkpoints/Llama-3-8B-Instruct-QServe # Please set the path accordingly

python qserve_e2e_generation.py \
  --model $MODEL_PATH \
  --ifb-mode \
  --precision w4a8kv4 \
  --quant-path $MODEL_PATH \
  --group-size -1
  1. Argument list in QServe

    Below are some frequently used arguments in QServe interface:

  1. One-line scripts:

We also provide sample scripts in QServe.

These scripts are expected to be executed in the QServe project folder (not in the scripts folder). Please note that git-lfs is needed for downloading QServe benchmark config files from huggingface before running the benchmark scripts.

QServe-VLM

QServe now supports large-scale efficient image captioning with the integration of VILA.

Synthetic data generated by VLM is important. It can be used to help improve VLMs themselves (like ShareGPT4V, CapsFusion) or to improve image generation models (like PixArt-alpha, DALL-E-3). Generating image captions with a naive HuggingFace pipeline is not efficient enough, and QServe provides a push-the-button accelerated solution.

cap_example

  1. Data format

Our current implementation assumes that the data is stored in webdataset format. The webdataset format assumes that all images are stored in the following form:

dataset/
├── shard-000000.tar
├── shard-000001.tar
├── shard-000002.tar
├── ...
└── metadata_info.json

The metadata_info.json file should follow the following format:

{
  "shardlist": [
    {
      "url": "shard-000000.tar",
      "nsamples": 5046,
      "filesize": 487823360
    },
  ],
  "wids_version": 1
}

It contains the number of samples in each tar and the size of each tar file (in Bytes).

  1. Data and checkpoint preparation

In our tutorial we use cc3m as an example, and we will generate new synthetic captions with W8A8 VILA1.5-13B and our QServe engine.

Download data:

git clone https://huggingface.co/datasets/pixparse/cc3m-wds

Generate info file:

cd data_prepare
python generate_cc_wds_meta.py

You may need to change some paths in this file.

After generating the webdataset metadata, please download the W8A8 VILA1.5-13B model compatible with our QServe engine:

https://huggingface.co/mit-han-lab/VILA1.5-13B-QServe-W8A8
  1. Run QServe to generate captions for all tars in the webdataset:
export dataset_path=<>   # Path to the images (WebDataset format)
export info_path=<>      # Path to the json file (WebDataset format)
export model_path=./qserve_checkpoints/VILA1.5-13B-QServe-W8A8  # Please set the path accordingly

python qserve_vila_caption.py \
      --model $model_path \
      --quant-path $model_path \
      --precision w8a8kv8 \
      --ifb-mode \
      --group-size -1 \
      --max-num-seqs 32 \
      --run-vlm \
      --img-per-seq 1 \
      --omit-prompt \
      --max-new-tokens 300 \
      --data_path $dataset_path \
      --info_path $info_path

The script above runs on a single GPU. We also provide sample scripts for QServe-VILA captioning, in which we apply data parallelism for higher captioning throughputs.

export job_id=0
./scripts/vlm_cap_scripts/run_cap_synth-cc12m_vila13b_8gpus.sh $job_id

The script takes in one argument job_id. It automatically utilizes 8 GPUs to caption tar files with indices 8 * job_id, 8 * job_id + 1, ... 8 * job_id + 7. One can scale up this script easily to multiple nodes, where each node have different job_id. It is recommended to run this script on a slurm cluster management system on a multi-node datacenter.

Sample slurm launch script:

sbatch ./scripts/vlm_cap_scripts/slurm_launch_cap_synth-cc12m_vila13b_8gpus.sh 

Sample image caption generated by QServe:

{
    "000000000": {
        "VILA1.5-13b-qserve-w8a8": "The image captures a serene scene of a flooded landscape. The dominant feature is a river, its waters a murky brown, meandering through the center of the frame. The river is not alone; it is accompanied by a multitude of trees, their leaves a vibrant green, dotting the landscape on both sides. \n\nThe sky above is a canvas of gray, painted with thick, heavy clouds that hint at the possibility of more rain. The perspective of the image is particularly striking, taken from a high vantage point that allows for a sweeping view of the scene below. This bird's-eye view provides a comprehensive look at the river's path and the distribution of the trees.\n\nDespite the flood, there's a certain tranquility to the scene. The river, the trees, and the sky - each element holds its own, yet together they create a harmonious picture of nature's resilience in the face of adversity. There are no texts or human-made objects in the image, just the raw beauty of nature in its most elemental form.</s>"}
    }
}

Results

We evaluate QServe W4A8KV4 quantization on a wide range of mainstream LLMs. QServe consistently outperforms existing W4A4 or W4A8 solutions from the accuracy perspective, while providing State-of-the-Art LLM serving efficiency.

Efficiency Benchmarks

When serving the large language models Llama-3-8B and Qwen1.5-72B on L40S and A100 GPUs, QServe demonstrates superior performance, achieving 1.2x-1.4x higher throughput compared to the leading industry solution, TensorRT-LLM, for Llama-3-8B, and a 2.4x-3.5x higher throughput for Qwen1.5-72B. It is also able to deliver higher throughput and accomodate the same batch size on L40S compared with TensorRT-LLM on A100 for six of eight models benchmarked, effectively saving the dollar cost of LLM serving by around 3x.

Benchmarking setting: the criterion is maximum achieveable throughput on NVIDIA GPUs, and the input context length is 1024 tokens, output generation length is 512 tokens. For all systems that support paged attention, we enable this feature. In-flight batching is turned off in the efficiency benchmarks.

L40S (48G)Llama-3-8BLlama-2-7BMistral-7BLlama-2-13BLlama-30BYi-34BLlama-2-70BQwen-1.5-72B
TRT-LLM-FP161326444156692OOMOOMOOMOOM
TRT-LLM-W4A161431681145736814831311917
TRT-LLM-W8A8263412712569440123364OOMOOM
Atom-W4A4--2120------------
QuaRot-W4A4--805--413133----15
QServe-W4A8KV4365623943774132750486928659
Throughput Increase*1.39x1.13x1.47x3.02x3.41x2.39x2.40x3.47x
A100 (80G)Llama-3-8BLlama-2-7BMistral-7BLlama-2-13BLlama-30BYi-34BLlama-2-70BQwen-1.5-72B
TRT-LLM-FP1625031549237148880145OOMOOM
TRT-LLM-W4A16237015492403871352569358143
TRT-LLM-W8A8239623342427127736164923553
Atom-W4A4--1160------------
QuaRot-W4A4--1370--289267----68
QServe-W4A8KV43005290829701741749803419340
Throughput Increase*1.20x1.25x1.22x1.36x2.07x1.23x1.17x2.38x

The absolute token generation throughputs of QServe and baseline systems (Unit: tokens/second. -- means unsupported). All experiments were conducted under the same device memory budget. Throughput increase of QServe is calculated with regard to the best baseline in each column. It is recommended to use QServe-per-channel on high-end datacenter GPUs like A100 and QServe-per-group is recommended on inference GPUs like L40S.

Max throughput batch sizes used by QServe:

DeviceLlama-3-8BLlama-2-7BMistral-7BLlama-2-13BLlama-30BYi-34BLlama-2-70BQwen-1.5-72B
L40S128128128753264244
A100256190256128641969632

We recommend direcly setting the NUM_GPU_PAGE_BLOCKS environmental variable to 25 * batch size, since in our benchmarking setting we have a context length of 1024 and generation length of 512, which corresponds to 24 pages (each page contains 64 tokens). We leave some buffer by allocating one more page for each sequence.

Accuracy Evaluation

QServe also maintains high accuracy thanks to the QoQ algorithm provided in our DeepCompressor quantization library.

Below is the WikiText2 perplexity evaluated with 2048 sequence length. The lower is the better.

ModelsPrecisionLlama-3 8BLlama-2 7BLlama-2 13BLlama-2 70BLlama 7BLlama 13BLlama 30BMistral 7BYi 34B
FP166.145.474.883.325.685.094.105.254.60
SmoothQuantW8A86.285.544.953.365.735.134.235.294.69
GPTQ-RW4A16 g1286.565.634.993.435.835.204.225.394.68
AWQW4A16 g1286.545.604.973.415.785.194.215.374.67
QuaRotW4A48.336.195.453.836.345.584.645.77NaN
AtomW4A4 g1287.766.125.313.736.255.524.615.764.97
QoQW4A8KV46.895.755.123.525.935.284.345.454.74
QoQW4A8KV4 g1286.765.705.083.475.895.254.285.424.76

* SmoothQuant is evaluated with per-tensor static KV cache quantization.

Reference

If you find QServe useful or relevant to your research and work, please kindly cite our paper:

@article{lin2024qserve,
  title={QServe: W4A8KV4 Quantization and System Co-design for Efficient LLM Serving},
  author={Lin*, Yujun and Tang*, Haotian and Yang*, Shang and Zhang, Zhekai and Xiao, Guangxuan and Gan, Chuang and Han, Song},
  journal={arXiv preprint arXiv:2405.04532},
  year={2024}
}

Team

The QServe serving library is maintained by the following research team:

Related Projects

The following projects are highly related to QServe. Our group has developed full-stack application-algorithm-system-hardware support for efficient large models, receiving 9k+ GitHub stars and over 1M Huggingface community downloads.

You are also welcome to check out MIT HAN LAB for other exciting projects on Efficient Generative AI!

Acknowledgement

We thank Julien Demouth, Jun Yang, and Dongxu Yang from NVIDIA for the helpful discussions. QServe is also inspired by many open-source libraries, including (but not limited to) TensorRT-LLM, vLLM, vLLM-SmoothQuant, FlashAttention-2, LMDeploy, TorchSparse++, GPTQ, QuaRot and Atom.