llm-compressor
repository·main·Indexed 25 days ago
https://github.com/vllm-project/llm-compressorA library for optimizing Large Language Models (LLMs) for efficient deployment with vLLM. It provides quantization algorithms for weights, activations, KV cache, and attention, saving models in the compressed-tensors format. Supported techniques include AutoRound, Activation Aware Quantization (AWQ), and sequential onloading for compressing large models that exceed single GPU memory.
What's inside llm-compressor
- LLM Compressor is a library designed to optimize large language models (LLMs) for deployment with vLLM. It provides tools for applying compression algorithms like quantization and pruning to reduce model size, lower hardware requirements, and improve inference performance (latency and throughput).
Overview of AutoRound Quantization
mainAutoRound is an advanced quantization technique that optimizes rounding values and clipping ranges using three trainable parameters (V, α, and β). It processes decoder layers sequentially using block-wise output reconstruction error as the training objective.
Key benefits include:
- High-accuracy, low-bit quantization: Particularly effective for sub-4-bit (INT2/INT3) where it can achieve 10–20% absolute accuracy improvements over standard PTQ.
- Compatibility: Results are fully compatible with
compressed-tensorsand can be served directly with vLLM. - Versatility: Performs well for large models (≈30B+), small-to-medium LLMs, and emerging formats like
MXFP4andNVFP4.
Use the Sequential Pipeline for model compression
mainThesequentialpipeline is a data pipeline designed for compressing models using specific modifiers. It is primarily used when applying theGPTQModifieror theSparseGPTModifierto a model.DDP Support for Quantization Modifiers
mainThe
llm-compressorframework is designed to be largely DDP-agnostic, but specific components require awareness to function correctly in a distributed environment:- Modifiers: Must be DDP-aware to handle distributed operations.
- Observers: Provide an interface that modifiers use to synchronize activation statistics across processes.
- Saving and Data Preparation: These areas include specific DDP-awareness to prevent common errors (foot-guns) like multiple processes attempting to write the same file simultaneously.
Other components, such as the sequential pipeline, remain DDP-blind and do not require modification.
Understand the Observer lifecycle in llm-compressor
mainAn
Observeris a utility class used during calibration to analyze weight and activation tensors. The process follows a two-phase design:- Observe: Accumulate statistics from tensors using
forward()orupdate_statistics_from_observed(). Common statistics includemin_valsandmax_vals. - Compute: Derive quantization parameters (such as
scale,zero_point, andglobal_scale) from the accumulated statistics usingget_qparams().
This design allows for complex behaviors like Observer Fusion, where multiple observers (e.g., for Q, K, and V projections) can share a single
global_scalecomputed from their combined statistics.- Observe: Accumulate statistics from tensors using
Quantization examples for supported key models
mainLLM Compressor provides tested quantization configurations and recommended parameters for several key models. You can find specific quantization examples, including precision settings (e.g., FP8, NVFP4) and algorithm-specific setups (e.g., HCA, CSA, mHC), in the dedicated model documentation pages for:
- DeepSeek V4: Supports HCA, CSA, and mHC, quantized to FP8 + NVFP4.
- Qwen3.5: Covers vision-language and sparse MoE models.
- Qwen3.6: Specifically covers the Qwen3.6-35B-A3B sparse MoE model.
- Kimi-K2.6: Moonshot AI's multimodal agentic model.
- Gemma 4: Google's multimodal model.
- Llama 4: Meta's Llama 4 Scout multimodal model.
- Mistral Large 3: Mistral's 675B parameter model.
- HY-V3: Tencent's sparse MoE model, quantized to NVFP4 + FP8.
- GLM-5.2: THUDM's mixed dense/MoE model, quantized to NVFP4 + FP8.
Use iMatrix Importance-Weighted Quantization
mainThe
imatrix_mseobserver implements importance-weighted quantization. It uses per-channel activation importance (E[x²]) to weight quantization error during range selection, ensuring channels that carry more signal receive more careful range optimization.When used as a weight observer,
imatrix_msecollects E[x²] per input channel during calibration via forward pre-hooks and applies importance weighting in the MSE grid search using the formula:err = sum(importance * |Q(w) - w|^p).Benefits of using LLM Compressor
mainLLM Compressor provides state-of-the-art quantization and pruning techniques to optimize large language models. Key benefits include:
- Reduced hardware costs: Achieve 50-75% memory reduction, allowing deployment on fewer GPUs.
- Improved inference speed: Lower latency and higher throughput via optimized kernels and specialized hardware (e.g., Tensor Cores).
- Maintained accuracy: Uses advanced algorithms to preserve model quality (e.g., <1% accuracy difference in some studies).
- Broad support: Compatible with standard LLMs, multimodal models, and Mixture of Experts (MoE) architectures.
- Production-ready: Outputs use the
compressed-tensorsformat, ensuring seamless integration with vLLM and Hugging Face.
Get started with LLM Compressor
mainLLM Compressor is a library designed to optimize large language models (LLMs) for deployment. It provides various quantization techniques to balance model quality, performance, and resource efficiency. The typical workflow involves:
- Installation: Setting up the library via
pipor from source. - Compression: Using different algorithms and formats to compress your model.
- Deployment: Using vLLM to run the compressed model for efficient inference.
- Installation: Setting up the library via
Install required transformers version for Qwen3.6
mainTo run quantization examples for the Qwen3.6-35B-A3B sparse MoE model, you must ensure
transformers >= v5is installed. You can upgrade your environment usinguv pip.uv pip install --upgrade transformersInstall llm-compressor
mainTo install
llm-compressorfor using AutoRound quantization, clone the repository and install it in editable mode using pip.git clone https://github.com/vllm-project/llm-compressor.git cd llm-compressor pip install -e .Use the Basic Pipeline for Small Models
mainThe
basicpipeline is faster than thesequentialpipeline but should only be used if the model (plus auxiliary memory like GPTQ hessians) fits entirely within available VRAM. For this pipeline, load the model directly onto GPU devices usingdevice_map="auto"and specifypipeline="basic"when callingoneshot.model = AutoModelForCausalLM.from_pretrained(model_stub, device_map="auto") # model is on devices ... oneshot(model, ..., pipeline="basic")