Skip to main content

VPTQ (Vector Post-Training Quantization) is a novel Post-Training Quantization method.

Project description

VPTQ: Extreme Low-bit Vector Post-Training Quantization for Large Language Models

TL;DR

Vector Post-Training Quantization (VPTQ) is a novel Post-Training Quantization method that leverages Vector Quantization to high accuracy on LLMs at an extremely low bit-width (<2-bit). VPTQ can compress 70B, even the 405B model, to 1-2 bits without retraining and maintain high accuracy.

  • Better Accuracy on 1-2 bits, (405B @ <2bit, 70B @ 2bit)
  • Lightweight Quantization Algorithm: only cost ~17 hours to quantize 405B Llama-3.1
  • Agile Quantization Inference: low decode overhead, best throughput, and TTFT

News

  • [2024-11-01] 📦 VPTQ is now available on PyPI! You can install it easily using the command: pip install vptq.
  • [2024-10-28] ✨ VPTQ algorithm early-released at algorithm branch, and checkout the tutorial.
  • [2024-10-22] 🌐 Open source community contributes Meta Llama 3.1 Nemotron 70B models, check how VPTQ counts 'r' on local GPU. We are continuing to work on quantizing the 4-6 bit versions. Please stay tuned!
  • [2024-10-21] 🌐 Open source community contributes Meta Llama 3.1 405B @ 3/4 bits models
  • [2024-10-18] 🌐 Open source community contributes Mistral Large Instruct 2407 (123B) models
  • [2024-10-14] 🚀 Add early ROCm support.
  • [2024-10-06] 🚀 Try VPTQ on Google Colab. VPTQ In Colab
  • [2024-10-05] 🚀 Add free Huggingface Demo: Huggingface Demo
  • [2024-10-04] ✏️ Updated the VPTQ tech report and fixed typos.
  • [2024-09-20] 🌐 Inference code is now open-sourced on GitHub—join us and contribute!
  • [2024-09-20] 🎉 VPTQ paper has been accepted for the main track at EMNLP 2024.

Installation

Dependencies

  • python 3.10+
  • torch >= 2.2.0
  • transformers >= 4.44.0
  • Accelerate >= 0.33.0
  • flash_attn >= 2.5.0
  • latest datasets

Install VPTQ on your machine

recommend For saving your time to build the package, Please install VPTQ from the latest Release directly

pip install vptq

or from

https://github.com/microsoft/VPTQ/releases

build from source

[Not Aavailbe if Release package]

Preparation steps that might be needed: Set up CUDA_HOME and PATH.

Set cuda-12 to your own CUDA version and environment. Run nvcc --version to find out your version, and which nvcc to check your CUDA PATH.

# example
export CUDA_HOME=/usr/local/cuda-12
export PATH=/usr/local/cuda-12/bin/:$PATH  # set dependent on your environment

Will Take several minutes to compile CUDA kernels, please be patient. Current compilation builds on SM 7.0, 7.5, 8.0, 8,6, 9.0 to reduce the compilation time. You can set TORCH_CUDA_ARCH_LIST to your specific architecture.

pip install git+https://github.com/microsoft/VPTQ.git --no-build-isolation

You can configure the required CUDA architectures and the number of nvcc compile threads by setting

TORCH_CUDA_ARCH_LIST=8.0,9.0 NVCC_THREADS=16 pip install -e . --no-build-isolation

to reduce compilation time.

Example: Run Llama 3.1 70b on RTX4090 (24G @ ~2bits) in real time Llama3 1-70b-prompt


VPTQ is an ongoing project. If the open-source community is interested in optimizing and expanding VPTQ, please feel free to submit an issue or DM.


Evaluation

Models from Open Source Community

⚠️ The repository only provides a method of model quantization algorithm.

⚠️ The open-source community VPTQ-community provides models based on the technical report and quantization algorithm.

⚠️ The repository cannot guarantee the performance of those models.

Quick Estimation of Model Bitwidth (Excluding Codebook Overhead):

  • Model Naming Convention: The model's name includes the vector length $v$, codebook (lookup table) size, and residual codebook size. For example, "Meta-Llama-3.1-70B-Instruct-v8-k65536-256-woft" is "Meta-Llama-3.1-70B-Instruct", where:

    • Vector Length: 8
    • Number of Centroids: 65536 (2^16)
    • Number of Residual Centroids: 256 (2^8)
  • Equivalent Bitwidth Calculation:

    • Index: log2(65536) = 16 / 8 = 2 bits
    • Residual Index: log2(256) = 8 / 8 = 1 bit
    • Total Bitwidth: 2 + 1 = 3 bits
  • Model Size Estimation: 70B * 3 bits / 8 bits per Byte = 26.25 GB

  • Note: This estimate does not include the size of the codebook (lookup table), other parameter overheads, and the padding overhead for storing indices. For the detailed calculation method, please refer to Tech Report Appendix C.2.

Model Series Collections (Estimated) Bit per weight
Llama 3.1 Nemotron 70B Instruct HF HF 🤗 4 bits 3 bits 2 bits (1) 2 bits (2) 1.875 bits 1.625 bits 1.5 bits
Llama 3.1 8B Instruct HF 🤗 4 bits 3.5 bits 3 bits 2.3 bits
Llama 3.1 70B Instruct HF 🤗 4 bits 3 bits 2.25 bits 2 bits (1) 2 bits (2) 1.93 bits 1.875 bits 1.75 bits
Llama 3.1 405B Instruct HF 🤗 4 bits 3 bits 2 bits 1.875 bits 1.625 bits 1.5 bits (1) 1.5 bits (2) 1.43 bits 1.375 bits
Mistral Large Instruct 2407 (123B) HF 🤗 4 bits 3 bits 2 bits (1) 2 bits (2) 1.875 bits 1.75 bits 1.625 bits 1.5 bits
Qwen 2.5 7B Instruct HF 🤗 4 bits 3 bits 2 bits (1) 2 bits (2) 2 bits (3)
Qwen 2.5 14B Instruct HF 🤗 4 bits 3 bits 2 bits (1) 2 bits (2) 2 bits (3)
Qwen 2.5 32B Instruct HF 🤗 4 bits 3 bits 2 bits (1) 2 bits (2) 2 bits (3)
Qwen 2.5 72B Instruct HF 🤗 4 bits 3 bits 2.38 bits 2.25 bits (1) 2.25 bits (2) 2 bits (1) 2 bits (2) 1.94 bits
Reproduced from the tech report HF 🤗 Results from the open source community for reference only, please use them responsibly.
Hessian and Inverse Hessian Matrix HF 🤗 Collected from RedPajama-Data-1T-Sample, following Quip#

Language Generation Example

To generate text using the pre-trained model, you can use the following code snippet:

The model VPTQ-community/Meta-Llama-3.1-70B-Instruct-v8-k65536-0-woft (~2 bit) is provided by open source community. The repository cannot guarantee the performance of those models.

python -m vptq --model=VPTQ-community/Meta-Llama-3.1-70B-Instruct-v8-k65536-0-woft --prompt="Explain: Do Not Go Gentle into That Good Night"

Llama3 1-70b-prompt

Terminal Chatbot Example

Launching a chatbot: Note that you must use a chat model for this to work

python -m vptq --model=VPTQ-community/Meta-Llama-3.1-70B-Instruct-v8-k65536-0-woft --chat

Llama3 1-70b-chat

Python API Example

Using the Python API:

import vptq
import transformers
tokenizer = transformers.AutoTokenizer.from_pretrained("VPTQ-community/Meta-Llama-3.1-70B-Instruct-v8-k65536-0-woft")
m = vptq.AutoModelForCausalLM.from_pretrained("VPTQ-community/Meta-Llama-3.1-70B-Instruct-v8-k65536-0-woft", device_map='auto')

inputs = tokenizer("Explain: Do Not Go Gentle into That Good Night", return_tensors="pt").to("cuda")
out = m.generate(**inputs, max_new_tokens=100, pad_token_id=2)
print(tokenizer.decode(out[0], skip_special_tokens=True))

Gradio Web App Example

An environment variable is available to control share link or not. export SHARE_LINK=1

python -m vptq.app

Tech Report

VPTQ_tech_report

Scaling model size significantly challenges the deployment and inference of Large Language Models (LLMs). Due to the redundancy in LLM weights, recent research has focused on pushing weight-only quantization to extremely low-bit (even down to 2 bits). It reduces memory requirements, optimizes storage costs, and decreases memory bandwidth needs during inference. However, due to numerical representation limitations, traditional scalar-based weight quantization struggles to achieve such extreme low-bit. Recent research on Vector Quantization (VQ) for LLMs has demonstrated the potential for extremely low-bit model quantization by compressing vectors into indices using lookup tables.

Read tech report at Tech Report and arXiv Paper

Early Results from Tech Report

VPTQ achieves better accuracy and higher throughput with lower quantization overhead across models of different sizes. The following experimental results are for reference only; VPTQ can achieve better outcomes under reasonable parameters, especially in terms of model accuracy and inference speed.

Model bitwidth W2↓ C4↓ AvgQA↑ tok/s↑ mem(GB) cost/h↓
LLaMA-2 7B 2.02 6.13 8.07 58.2 39.9 2.28 2
2.26 5.95 7.87 59.4 35.7 2.48 3.1
LLaMA-2 13B 2.02 5.32 7.15 62.4 26.9 4.03 3.2
2.18 5.28 7.04 63.1 18.5 4.31 3.6
LLaMA-2 70B 2.07 3.93 5.72 68.6 9.7 19.54 19
2.11 3.92 5.71 68.7 9.7 20.01 19

Road Map

  • Merge the quantization algorithm into the public repository.
  • Release on Pypi
  • Improve the implementation of the inference kernel (e.g., CUDA, ROCm, Triton) and apply kernel fusion by combining dequantization (lookup) and Linear (GEMM) to enhance inference performance.
  • Support VLM models @YangWang92
  • Contribute VPTQ to Huggingface Transformers
  • Contribute VPTQ to vLLM, LLM Compressor
  • Contribute VPTQ to llama.cpp/exllama.
  • TBC

Project main members:

  • Yifei Liu (@lyf-00)
  • Jicheng Wen (@wejoncy)
  • Yang Wang (@YangWang92)

Acknowledgement

  • We thank for James Hensman for his crucial insights into the error analysis related to Vector Quantization (VQ), and his comments on LLMs evaluation are invaluable to this research.
  • We are deeply grateful for the inspiration provided by the papers QUIP, QUIP#, GPTVQ, AQLM, WoodFisher, GPTQ, and OBC.

Publication

EMNLP 2024 Main

@inproceedings{
  vptq,
  title={VPTQ: Extreme Low-bit Vector Post-Training Quantization for Large Language Models},
  author={Yifei Liu and
          Jicheng Wen and
          Yang Wang and
          Shengyu Ye and
          Li Lyna Zhang and
          Ting Cao and
          Cheng Li and
          Mao Yang},
  booktitle={The 2024 Conference on Empirical Methods in Natural Language Processing},
  year={2024},
}

Star History

Star History Chart


Limitation of VPTQ

  • ⚠️ VPTQ should only be used for research and experimental purposes. Further testing and validation are needed before you use it.
  • ⚠️ The repository only provides a method of model quantization algorithm. The open-source community may provide models based on the technical report and quantization algorithm by themselves, but the repository cannot guarantee the performance of those models.
  • ⚠️ VPTQ is not capable of testing all potential applications and domains, and VPTQ cannot guarantee the accuracy and effectiveness of VPTQ across other tasks or scenarios.
  • ⚠️ Our tests are all based on English texts; other languages are not included in the current testing.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

vptq-0.0.4-cp312-cp312-manylinux1_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.12

vptq-0.0.4-cp311-cp311-manylinux1_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.11

vptq-0.0.4-cp310-cp310-manylinux1_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.10

vptq-0.0.4-cp39-cp39-manylinux1_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.9

vptq-0.0.4-cp38-cp38-manylinux1_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.8

File details

Details for the file vptq-0.0.4-cp312-cp312-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for vptq-0.0.4-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bf5e80758cefad8b81f41821e475e33c6afd159fe6ba26c6bedfe13c4f8e1079
MD5 cc19e95581d785a5b8013d38586b158f
BLAKE2b-256 ed9abeb9f8a1f5af7b77956e7d51b7520c620976784610c13038ea6bf25010c2

See more details on using hashes here.

File details

Details for the file vptq-0.0.4-cp311-cp311-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for vptq-0.0.4-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9c3d0d0c49922d06b9f85a8e6aeed9dc6824177b4db737eb00803868234c02b2
MD5 c92ecb6a3c57b1c00f19144f9263fcda
BLAKE2b-256 1b3cf3e3caf3670e0c2ee3278166d92172a0fc686db140b92f318ca367252ac1

See more details on using hashes here.

File details

Details for the file vptq-0.0.4-cp310-cp310-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for vptq-0.0.4-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f8f35a5d2f38c235d56bd7a2860232d735cedfc36c56a89b7d1853316bdd0f4a
MD5 4cdd9a381a58a02f24c8bc65ac3502a1
BLAKE2b-256 ce5b596e0ddd381a2746c1dc7fe89e2792e06895230b8771361de4d06d05c878

See more details on using hashes here.

File details

Details for the file vptq-0.0.4-cp39-cp39-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for vptq-0.0.4-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 046c9bcda83d3e388d82eb0baba1c387088c6a1f89c549d9642e6fe7d6691fd0
MD5 f2fd14794cf7a6259a96fe887ed16103
BLAKE2b-256 13ae3dabd4f3c91f0dc312ae8c49c5483060671133e6b9258c0821c11ba8b7d7

See more details on using hashes here.

File details

Details for the file vptq-0.0.4-cp38-cp38-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for vptq-0.0.4-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 290f4062e7c21ec65c35166b088174a6539aecb63d76a95f94121235a7f87269
MD5 66ae360a1436a016a84a598867befa56
BLAKE2b-256 36b2b4187c6e212f2db3871297211cd906c9fe4b980d11154e6b9d12222e6a9b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page