gptq

IST-DASLabApache-2.0长期未更新

GPTQ 是 ICLR 2023 论文的官方实现,能在训练后把 GPT 类大模型量化到 3/4 比特,几乎不掉精度,大幅降低显存占用和推理成本。

Code for the ICLR 2023 paper "GPTQ: Accurate Post-training Quantization of Generative Pretrained Transformers".

暂无版本2,394star208fork同步于 2026-11-07 07:11 UTC

怎么上手

由 README 提炼,执行前请确认
python setup_cuda.py install
CUDA_VISIBLE_DEVICES=0 python opt.py facebook/opt-125m c4 --wbits 4
硬件要求按 README 说明

实验在单张 80GB NVIDIA A100 上完成,README 称多数实验在显存小得多的 GPU 上也能跑;3 比特内核仅针对 1×A100 或 2×A6000 上的 OPT-175B 优化,其他 GPU 或小模型性能可能欠佳。

需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

GPTQ 论文官方实现,把 OPT、BLOOM、LLaMa 等大模型训练后量化到 2/3/4 比特

GPTQ 是 ICLR 2023 论文的官方实现,能在训练后把 GPT 类大模型量化到 3/4 比特,几乎不掉精度,大幅降低显存占用和推理成本。

核心能力

  • 实现 GPTQ 逐层训练后量化算法 gptq.py
  • 支持 OPT 与 BLOOM 全系列 2/3/4 比特量化及权重分组
  • 提供 3 比特量化矩阵乘 CUDA 内核并含基准测试
  • 集成 LLaMa 的 act-order 与 true-sequential 量化技巧
  • 在语言生成任务上评测量化模型困惑度与 ZeroShot 表现

容易踩的坑

3 比特内核需先编译 PyTorch CUDA 扩展(测试于 CUDA 11.4);LLaMa 集成需从源码安装 transformers 主分支并额外装 sentencepiece;OPT-175B 需先向 Meta 申请权限并用 metaseq 脚本转成本地 HuggingFace checkpoint。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    GPTQ

    This repository contains the code for the ICLR 2023 paper GPTQ: Accurate Post-training Compression for Generative Pretrained Transformers. The current release includes the following features:

    • An efficient implementation of the GPTQ algorithm: gptq.py
    • Compressing all models from the OPT and BLOOM families to 2/3/4 bits, including weight grouping: opt.py, bloom.py, zeroShot/
    • Evaluating the perplexity of quantized models on several language generation tasks: opt.py, bloom.py
    • Evaluating the performance of quantized models on several ZeroShot tasks: zeroShot/
    • A 3-bit quantized matrix full-precision vector product CUDA kernel: quant_cuda_kernel.cu, quant_cuda.cpp, setup_cuda.py
    • Benchmarking code for individual matrix-vector products and for language generation with quantized models: test_kernel.py, opt.py

    New Features

    Update July 2023:

    • Added --static-groups options which determines all group-grids in advance rather than dynamically during quantization, which has the effect that --act-order does not require any inference changes (that may cause slowdown) when used together with this option.

    Together with the camera ready version of the paper we have added several updates to this repository:

    • Slightly adjusted preprocessing of C4 and PTB for more realistic evaluations (used in our updated results); can be activated via the flag --new-eval.
    • Optimized 3bit kernels, which are considerably faster especially on the A100, e.g. 1.9x -> 3.25x generation speedup for OPT-175B; can be activated via --faster-kernel.
    • A minimal LlaMa integration (for more complete features see the GPTQ-for-LLaMA repository), which demonstrates two new tricks:--act-order (quantizing columns in order of decreasing activation size) and --true-sequential (performing sequential quantization even within a single Transformer block). Those fix GPTQ's strangely bad performance on the 7B model (from 7.15 to 6.09 Wiki2 PPL) and lead to slight improvements on most models/settings in general.

    Here is a summary of LLaMa results:

    Wiki2 PPLFP164bit-RTN4bit-GPTQ3bit-RTN3bit-GPTQ3g128-GPTQ
    LLaMa-7B5.686.296.0925.548.076.61
    LLaMa-13B5.095.535.3611.406.635.62
    LLaMa-30B4.104.544.4514.895.694.80
    LLaMa-65B3.533.923.8410.595.044.17

    Here is a sample command:

    python llama.py LLAMA_HF_FOLDER c4 --wbits 4 --true-sequential --act-order --new-eval
    

    The --act-order heuristic also dramatically improves accuracy on the OPT-66B outlier model: 9.55 to 9.34 and 14.16 to 9.95 PPL on Wiki2 for 4bit and 3bit, respectively.

    Dependencies

    • torch: tested on v1.10.1+cu111
    • transformers: tested on v4.21.2 (the LLaMa integration currently requires a main install from source and sentencepiece)
    • datasets: tested on v1.17.0
    • (to run 3-bit kernels: setup for compiling PyTorch CUDA extensions, see also https://pytorch.org/tutorials/advanced/cpp_extension.html, tested on CUDA 11.4)

    All experiments were run on a single 80GB NVIDIA A100. However, most experiments will work on a GPU with a lot less memory as well.

    Language Generation

    OPT

    # Compute full precision (FP16) results
    CUDA_VISIBLE_DEVICES=0 python opt.py facebook/opt-125m c4
    # Run RTN baseline and compute results
    CUDA_VISIBLE_DEVICES=0 python opt.py facebook/opt-125m c4 --wbits 4 --nearest
    # Run GPTQ and compute results
    CUDA_VISIBLE_DEVICES=0 python opt.py facebook/opt-125m c4 --wbits 4 [--groupsize 1024]
    

    To run other OPT models replace opt-125m with one of: opt-350m, opt-1.3b, opt-2.7b, opt-6.7b, opt-13b, opt-66b. For the 175B-parameter mode, you have to request access from Meta and then convert it to a local HuggingFace checkpoint using their scripts in metaseq. Once you have such a checkpoint, simply pass its path instead of facebook/opt-125m.

    BLOOM

    # Compute full precision (FP16) results
    CUDA_VISIBLE_DEVICES=0 python bloom.py bigscience/bloom-560m c4
    # Run RTN baseline and compute results
    CUDA_VISIBLE_DEVICES=0 python bloom.py bigscience/bloom-560m c4 --wbits 4 --nearest
    # Run GPTQ and compute results
    CUDA_VISIBLE_DEVICES=0 python bloom.py bigscience/bloom-560m c4 --wbits 4 [--groupsize 1024]
    

    To run other BLOOM models replace bloom-560m with one of: bloom-1b1, bloom-1b7, bloom-3b, bloom-7b1, bloom.

    ZeroShot

    See zeroShot/ folder.

    3-bit CUDA Kernels

    # Install kernels
    python setup_cuda.py install
    
    # Benchmark performance for FC2 layer of OPT-175B
    CUDA_VISIBLE_DEVICES=0 python test_kernel.py
    
    # Benchmark language generation with 3-bit OPT-175B:
    # OPT175B denotes the name of the folder with the HuggingFace OPT-175b checkpoint (see above)
    
    # Save compressed model
    CUDA_VISIBLE_DEVICES=0 python opt.py OPT175B c4 --wbits 3 --save opt175-3bit.pt
    # Benchmark generating a 128 token sequence with the saved model
    CUDA_VISIBLE_DEVICES=0 python opt.py OPT175B c4 --load opt175b-3bit.pt --benchmark 128
    # Benchmark FP16 baseline, note that the model will be split across all listed GPUs
    CUDA_VISIBLE_DEVICES=0,1,2,3,4 python opt.py OPT175B c4 --benchmark 128
    

    Please note that our 3-bit kernels are currently only optimized for OPT-175B running on 1xA100 or 2xA6000 and may thus yield suboptimal performance on smaller models or on other GPUs.

    Cite

    If you found this work useful, please consider citing:

    @article{frantar-gptq,
      title={{GPTQ}: Accurate Post-training Compression for Generative Pretrained Transformers}, 
      author={Elias Frantar and Saleh Ashkboos and Torsten Hoefler and Dan Alistarh},
      year={2022},
      journal={arXiv preprint arXiv:2210.17323}
    }