양자화 (Quantization)
양자화 (Quantization)
양자화(Quantization)는 모델의 정밀도를 조금 포기하는 대신 메모리 사용량을 줄이는 기법이에요. 이렇게 하면 메모리 용량이 작은 기기에서도 큰 모델을 실행할 수 있어요.
팁
양자화를 처음 시작할 때는 LLM Compressor를 살펴보는 걸 추천해요. LLM Compressor는 vLLM에 배포할 모델을 최적화하기 위한 라이브러리로, FP8, INT8, INT4 등 여러 양자화 형식을 지원해요.
vLLM이 지원하는 양자화 형식은 다음과 같아요.
- AutoAWQ
- BitsAndBytes
- GPTQModel
- Intel Neural Compressor
- LLM Compressor
- FP8 W8A8
- INT4 W4A16
- INT8 W4A8
- INT8 W8A8
- NVIDIA Model Optimizer
- Online Quantization
- AMD Quark
- Quantized KV Cache
- TorchAO
- FP8 ViT Encoder Attention
지원 하드웨어
아래 표는 vLLM에서 각 양자화 구현이 어떤 하드웨어 플랫폼과 호환되는지를 보여줘요.
| 구현 | Volta | Turing | Ampere | Ada | Hopper | AMD GPU | Intel GPU | x86 CPU | Arm CPU |
|---|---|---|---|---|---|---|---|---|---|
| AWQ | ❌ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ✅︎ | ✅︎ | ❌ |
| GPTQ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ✅︎ | ✅︎ | ❌ |
| llm-compressor INT8 (W8A8) | ❌ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ❌ | ✅︎ | ✅︎ |
| llm-compressor INT8 (W4A8) | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅︎ |
| llm-compressor FP8 (W8A8) | ❌ | ❌ | ❌ | ✅︎ | ✅︎ | ✅︎ | ❌ | ❌ | ❌ |
| bitsandbytes | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ❌ | ❌ | ❌ |
| DeepSpeedFP | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ❌ | ❌ | ❌ |
| GGUF | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ✅︎ | ❌ | ❌ | ❌ |
Intel Gaudi의 양자화 지원은 모두 vLLM-Gaudi로 이전됐어요.
* Turing은 Marlin MXFP4를 지원하지 않아요.
참고
Google TPU의 양자화 지원에 대한 정보는 TPU-Inference Recommended Models and Features 문서를 참고해 주세요.
참고
이 호환성 표는 vLLM이 계속 발전하고 하드웨어 플랫폼과 양자화 방식에 대한 지원을 확장함에 따라 바뀔 수 있어요.
트리 밖 양자화 플러그인 (Out-of-Tree Quantization Plugins)
vLLM은 @register_quantization_config 데코레이터를 사용해 트리 밖(out-of-tree) 커스텀 양자화 방식을 등록하는 것을 지원해요. 이렇게 하면 vLLM 코드베이스를 수정하지 않고도 자신만의 양자화 방식을 구현해서 쓸 수 있어요.
커스텀 양자화 방식 등록하기
커스텀 양자화 방식을 등록하려면 QuantizationConfig를 상속하는 클래스를 만들고 @register_quantization_config로 데코레이팅하면 돼요. get_quant_method는 레이어 타입에 따라 적절한 quantize 메서드로 분기해요.
import torch
from vllm.model_executor.layers.quantization import (
register_quantization_config,
)
from vllm.model_executor.layers.quantization.base_config import (
QuantizationConfig,
QuantizeMethodBase,
)
from vllm.model_executor.layers.linear import LinearBase
@register_quantization_config("my_quant")
class MyQuantConfig(QuantizationConfig):
"""Custom quantization config."""
def get_name(self) -> str:
return "my_quant"
def get_supported_act_dtypes(self) -> list:
return [torch.float16, torch.bfloat16]
@classmethod
def get_min_capability(cls) -> int:
# Minimum GPU compute capability, -1 for no restriction
return -1
@staticmethod
...
self, layer: torch.nn.Module, prefix: str
) -> QuantizeMethodBase | None:
# Dispatch based on layer type
# NOTE: you only need to implement methods you care about
if isinstance(layer, LinearBase):
return MyQuantLinearMethod()
elif isinstance(layer, FusedMoE):
...
필수 QuantizationConfig 메서드
커스텀 QuantizationConfig 서브클래스는 다음 추상 메서드를 반드시 구현해야 해요.
| 메서드 | 설명 |
|---|---|
| get_name() | 양자화 방식의 이름을 반환해요 |
| get_supported_act_dtypes() | 지원하는 활성화(activation) dtype 목록을 반환해요 (예: torch.float16) |
| get_config_filenames() | 모델 디렉터리에서 찾을 config 파일 이름 목록을 반환해요 |
| from_config(config) | 모델의 양자화 config dict에서 config를 만드는 클래스 메서드예요 |
| get_quant_method(layer, prefix) | 주어진 레이어에 대한 양자화 메서드를 반환하거나, 건너뛰려면 None을 반환해요 |
양자화된 Linear 메서드 구현하기
Linear 레이어의 경우 get_quant_method에서 QuantizeMethodBase 서브클래스를 반환해요. 시작점으로 UnquantizedLinearMethod를 확장할 수 있어요.
from vllm.model_executor.layers.linear import UnquantizedLinearMethod
class MyQuantLinearMethod(UnquantizedLinearMethod):
"""Custom quantization method for linear layers."""
양자화된 MoE 메서드 구현하기
MoE(Mixture of Experts) 모델의 경우 get_quant_method에서 FusedMoEMethodBase 서브클래스를 반환해요. MoE 양자화를 건너뛰고 싶다면 UnquantizedFusedMoEMethod를 사용할 수 있어요.
class MyQuantMoEMethod(FusedMoEMethodBase):
"""Custom quantization method for MoE layers."""
...
layer: torch.nn.Module,
router: "FusedMoERouter",
x: torch.Tensor,
router_logits: torch.Tensor,
) -> torch.Tensor:
# Apply MoE computation with quantized weights
...
def get_fused_moe_quant_config(
self, layer: torch.nn.Module
) -> FusedMoEQuantConfig | None:
...
참고로 vllm/model_executor/layers/quantization/fp8.py에 있는 Fp8MoEMethod 같은 기존 구현을 참고하면 좋아요.
플러그인 사용하기
등록이 끝나면 커스텀 양자화 방식을 vLLM에서 이렇게 사용할 수 있어요.
# Register your quantization method (import the module containing your config)
import my_quant_plugin
from vllm import LLM
# Use the custom quantization method
llm = LLM(model="your-model", quantization="my_quant")
플러그인 시스템에 대한 더 자세한 내용은 Plugin System 문서를 참고해 주세요.