Quantization
Quantization
A technique that lowers the precision of model weights to reduce memory and computation. INT8, INT4, and others.
Quantization
A technique that lowers the precision of model weights to reduce memory and computation. INT8, INT4, and others.
Quantization is an optimization technique that converts the weights and activations of a Deep Learning model to lower precision (number of bits) to reduce memory usage and computation. While typical models use 32-bit floating point (FP32), quantized models can use 16-bit (FP16), 8-bit (INT8), 4-bit (INT4), and even 2-bit.
Quantization technology developed out of the demand for deploying models on mobile and edge devices, and its importance grew explosively in the LLM era of 2023-2024. Loading a 70B-parameter model in FP16 requires 140GB of VRAM, but applying INT4 quantization reduces this to 35GB, making it possible to run on consumer GPUs.
Quantization methods are broadly divided into Post-Training Quantization (PTQ) and Quantization-Aware Training (QAT). PTQ quantizes a trained model directly, while QAT simulates the effects of quantization during training. Recent techniques include GPTQ, AWQ, and GGUF, and in particular, AWQ (Activation-aware Weight Quantization) preserves important weights, maintaining excellent performance even at 4-bit quantization.
As of 2025, quantized models have become the standard in the Hugging Face and llama.cpp ecosystems. Hardware-specific optimization tools such as Apple Silicon's MLX and NVIDIA's TensorRT-LLM also support quantization by default. In practice, you need to choose an appropriate quantization level by considering the trade-off between accuracy and speed/memory.
# 양자화 - bitsandbytes를 활용한 4비트 양자화 예제
# pip install transformers bitsandbytes accelerate
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
# 4비트 양자화 설정 (NF4 + Double Quantization)
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True, # 양자화 파라미터도 양자화
bnb_4bit_quant_type="nf4" # Normal Float 4 (성능 우수)
)
# 모델 로드 (70B 모델도 단일 GPU에서 실행 가능!)
model_name = "meta-llama/Llama-2-7b-hf"
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=quantization_config,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 메모리 사용량 확인
print(f"모델 메모리: {model.get_memory_footprint() / 1e9:.2f} GB")
# FP16: 14GB -> INT4: 3.5GB (4배 감소!)
# 추론 테스트
prompt = "인공지능의 미래는"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=100,
temperature=0.7,
do_sample=True
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
# GPTQ 양자화 모델 직접 로드 (더 빠른 추론)
# from auto_gptq import AutoGPTQForCausalLM
# model = AutoGPTQForCausalLM.from_quantized("TheBloke/Llama-2-7B-GPTQ")
Memory/performance comparison by quantization level (based on Llama-2-70B, January 2025):
| Precision | VRAM Required | Inference speed | Performance Loss |
|---|---|---|---|
| FP32 | 280 GB | Baseline | 0% |
| FP16/BF16 | 140 GB | 1.5x | ~0% |
| INT8 | 70 GB | 2x | 0.5-1% |
| INT4 (GPTQ) | 35 GB | 2.5x | 1-3% |
| INT4 (AWQ) | 35 GB | 2.5x | 0.5-2% |
"To serve a 70B model, you need two A100 80GB GPUs, but if you apply AWQ 4-bit quantization, you can run it on a single A100. By benchmark, the performance loss is under 2%, and inference cost is cut in half."
"The difference between PTQ and QAT in quantization is when it happens relative to training. PTQ is fast because it is post-processing, but it has performance loss, while QAT simulates quantization during training, so accuracy is better. Recently, methods that take the activation distribution into account, like AWQ, are also giving good results in PTQ."
"llama.cpp's GGUF format is optimized for CPU inference, while GPTQ/AWQ are suited to GPU inference. You need to choose the quantization format depending on the deployment environment. On Apple Silicon, MLX quantization is the fastest."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
4-bit quantization is not always the best choice. For tasks where precision matters, such as math problems and coding, INT8 or FP16 may produce better results. Evaluate on your actual task before choosing.
GPTQ, AWQ, and GGUF are not compatible with each other. You need to choose the format that matches the inference engine you use (vLLM, llama.cpp, TGI, etc.).
Always run a Benchmark after quantization. It is a good idea to compare the performance of quantized models on Hugging Face's Open LLM Leaderboard and run A/B tests on real use cases.