SmoothQuant
repository·main·Indexed 23 days ago
https://github.com/mit-han-lab/smoothquantA post-training quantization (PTQ) solution for Large Language Models (LLMs) that enables efficient 8-bit weight and 8-bit activation (W8A8) quantization with minimal accuracy loss. It works by smoothing activation outliers to balance quantization difficulty between activations and weights. The library supports models such as OPT, BLOOM, Llama, Falcon, Mistral, and Mixtral, and provides tools for generating activation channel scales, simulated quantization evaluation, and real-INT8 inference using torch-int.
What's inside SmoothQuant
- Pre-computed activation channel scales for OPT and BLOOM models are available on Hugging Face. These scales were generated using 512 random sentences from the Pile validation set. You can use these scales to perform SmoothQuant quantization on these specific model architectures without re-running the calibration process yourself.
Install SmoothQuant
mainTo install SmoothQuant, create a new Conda environment with Python 3.8 and install the required PyTorch, Transformers, and Accelerate dependencies. Finally, run the setup script from the repository root.
conda create -n smoothquant python=3.8 conda activate smoothquant pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113 pip install transformers==4.36.0 accelerate datasets zstandard python setup.py install#!/bin/bash conda create -n smoothquant python=3.8 conda activate smoothquant pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113 pip install transformers==4.36.0 accelerate datasets zstandard python setup.py installSmoothQuant INT8 Quantization Flow
mainThe quantization process for OPT models involves smoothing, quantizing, and exporting. For developers looking to implement or customize this flow, the following files in the repository contain the implementation details:
smoothquant/opt.py: Contains the core quantization flow logic for OPT models.examples/generate_act_scales.py: Demonstrates how to generate activation scales.examples/export_int8_model.py: Demonstrates how to export the final INT8 model.
Install dependencies for SmoothQuant
mainTo run SmoothQuant demonstrations or use the library, ensure the following packages are installed in your environment:
smoothquantPyTorchTransformersAccelerate
Setup SmoothQuant Real-INT8 Inference
mainTo run SmoothQuant real-INT8 inference for PyTorch, you must install the following dependencies:
smoothquanttorch-int(provides CUTLASS INT8 GEMM kernels wrapped as PyTorch modules)PyTorchTransformersAccelerate
Note: For models larger than what can fit on a single A100 GPU (like OPT-30B), it is recommended to use the FasterTransformer implementation of SmoothQuant.
Apply SmoothQuant to a Llama model
mainSmoothQuant improves W8A8 quantization performance by smoothing activation outliers. To use it, follow these steps:
- Load your model in FP16 using
Transformers. - Load pre-computed activation scales (e.n.,
.ptfiles). - Call
smooth_lmwith the model, the scales, and a smoothing factor (e.g.,0.85). - Apply W8A8 quantization using
quantize_llama_like.
This process balances the quantization difficulty between activations and weights, resulting in perplexity closer to the original FP16 model compared to naive W8A8 quantization.
- Load your model in FP16 using
Test SmoothQuant on OPT models using the provided demo
mainTo test the smoothing and quantization process on OPT models, use the Jupyter notebook located at../examples/smoothquant_opt_demo.ipynb. This demo demonstrates how to apply the activation channel scales to the model.Use SmoothQuant INT8 Inference for PyTorch
mainSmoothQuant provides INT8 inference for PyTorch using CUTLASS INT8 GEMM kernels via the
torch-intlibrary.Prerequisite: You must install
torch-intbefore running the inference.For OPT models, you can use the pre-quantized models provided by MIT Han Lab on Hugging Face. The
Int8OPTForCausalLMclass handles the INT8 linear layers and quantization scales.Available OPT model sizes:
125m,1.3B,2.7B,6.7B,13B,30b, and66b.from smoothquant.opt import Int8OPTForCausalLM model = Int8OPTForCausalLM.from_pretrained("mit-han-lab/opt-30b-smoothquant")Load a pre-quantized SmoothQuant OPT model
mainSmoothQuant provides pre-smoothed and quantized OPT models on Hugging Face. You can load these models directly using the
Int8OPTForCausalLMclass fromsmoothquant.opt.Available model sizes for the prefix
mit-han-lab/opt-[MODEL-SIZE]-smoothquantinclude:125m1.3B2.7B6.7B13B30b66b
from smoothquant.opt import Int8OPTForCausalLM # Example loading the 30B quantized model model = Int8OPTForCausalLM.from_pretrained("mit-han-lab/opt-30b-smoothquant")Perform Naive W8A8 Quantization
mainYou can perform a naive W8A8 quantization (without smoothing) usingquantize_opt. Note that for models larger than 6.7B, this typically results in significant accuracy drops due to activation outliers.Apply SmoothQuant to an LLM
mainSmoothQuant improves W8A8 quantization accuracy by smoothing activation outliers and migrating quantization difficulty from activations to weights.
To apply SmoothQuant, use
smooth_lmwith a pre-computed activation channel scales tensor and a smoothing factor (e.g.,0.5). After smoothing, usequantize_optto perform the W8A8 quantization.Note: Activation scales for models like OPT and BLOOM are available in the
../act_scales/directory of the repository.from smoothquant.smooth import smooth_lm from smoothquant.fake_quant import quantize_opt # Load model model = OPTForCausalLM.from_pretrained( "facebook/opt-13b", torch_dtype=torch.float16, device_map="auto" ) # Load pre-computed activation scales act_scales = torch.load("../act_scales/opt-13b.pt") # Apply smoothing and then quantize smooth_lm(model, act_scales, 0.5) model_smoothquant_w8a8 = quantize_opt(model)Evaluate model perplexity with W8A8 simulated quantization
mainYou can evaluate the language modeling perplexity (PPL) of models (OPT, BLOOM, Llama, Falcon, Mistral, Mixtral) using simulated W8A8 quantization via
smoothquant/ppl_eval.py.Arguments:
--model_path: The name or path of the model.--act_scales_path: Path to the pre-generated activation scales file.--smooth: Enables the smoothing transformation.--alpha: The smoothing factor (alpha).--quantize: Enables quantization simulation.
python smoothquant/ppl_eval.py \ --model_path <model_name_or_path> \ --act_scales_path <act_scales_file_path> \ --smooth \ --alpha <alpha> \ --quantize