Quantization
Quantization
Converts model weights to lower precision. Reduces size and speeds up inference.
Quantization
Converts model weights to lower precision. Reduces size and speeds up inference.
Quantization is a technique that converts a neural network's weights and activations from high precision (FP32/FP16) to low precision (INT8/INT4). This can reduce model size by 2-8x and improve inference speed by 1.5-4x. In the era of LLMs in particular, it has become an essential technology for running large models of 70B or more on consumer GPUs.
Quantization is broadly divided into two approaches. PTQ (Post-Training Quantization) is a method that quantizes an already trained model without additional training, and GPTQ, AWQ, and GGUF are representative examples. It is fast and simple, but quality degradation can occur at extremely low precision (4-bit or below). QAT (Quantization-Aware Training) simulates quantization during the training process so that the model adapts to a low-precision environment. Quality is better, but additional training is required.
The core of modern quantization techniques is to "preserve important weights." GPTQ uses Hessian information to preferentially preserve weights that are sensitive to the loss function. AWQ (Activation-aware Weight Quantization) identifies and protects important channels based on the magnitude of activation values. NF4 (NormalFloat 4-bit) optimizes the quantization bins by exploiting the fact that weights follow a normal distribution. Thanks to these techniques, models can now be compressed with only a 1-2% performance degradation relative to the original, even with 4-bit quantization.
In practice, quantized models are distributed in various formats. GGUF (GPT-Generated Unified Format) is the format used by llama.cpp; it is optimized for CPU inference and supports various quantization levels such as Q4_K_M and Q5_K_S. GPTQ is optimized for GPU inference and is widely used on Hugging Face. AWQ offers faster speed and similar quality compared to GPTQ. For mobile/edge deployment, it is used together with optimized runtimes such as TensorRT-LLM and ONNX Runtime.
This is an example of loading an LLM and running inference using various quantization techniques:
# 1. bitsandbytes 4-bit μμν (QLoRAμ©)
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
import torch
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4", # NormalFloat4
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True # μΆκ° μμΆ
)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config,
device_map="auto"
)
# μλ³Έ 28GB -> μμν ν ~4GB
# 2. GPTQ μμν λͺ¨λΈ λ‘λ (GPU μΆλ‘ )
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Llama-2-7B-GPTQ",
device_map="auto",
trust_remote_code=True
)
# 3. AWQ μμν λͺ¨λΈ (λ λΉ λ₯Έ μΆλ‘ )
from awq import AutoAWQForCausalLM
model = AutoAWQForCausalLM.from_pretrained(
"TheBloke/Llama-2-7B-AWQ",
device_map="auto"
)
# 4. llama.cpp GGUF λͺ¨λΈ (CPU/μ μ¬μ GPU)
from llama_cpp import Llama
llm = Llama(
model_path="./llama-2-7b.Q4_K_M.gguf",
n_ctx=4096, # 컨ν
μ€νΈ κΈΈμ΄
n_threads=8, # CPU μ€λ λ
n_gpu_layers=35 # GPU μ€νλ‘λ λ μ΄μ΄ μ
)
output = llm(
"AIμ λ―Έλλ",
max_tokens=100,
temperature=0.7
)
print(output["choices"][0]["text"])
# 5. μ§μ GPTQ μμν μν
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
gptq_config = GPTQConfig(
bits=4,
dataset="c4", # μΊλ¦¬λΈλ μ΄μ
λ°μ΄ν°μ
tokenizer=tokenizer,
group_size=128
)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=gptq_config,
device_map="auto"
)
model.save_pretrained("./llama-2-7b-gptq")
PyTorch native quantization (for non-LLM models):
import torch
from torch.quantization import quantize_dynamic
# λμ μμν (μΆλ‘ μ νμ±ν μμν)
model_fp32 = MyModel()
model_int8 = quantize_dynamic(
model_fp32,
{torch.nn.Linear, torch.nn.LSTM}, # μμν λμ λ μ΄μ΄
dtype=torch.qint8
)
# μ μ μμν (μΊλ¦¬λΈλ μ΄μ
νμ)
model.qconfig = torch.quantization.get_default_qconfig('fbgemm')
torch.quantization.prepare(model, inplace=True)
# ... μΊλ¦¬λΈλ μ΄μ
λ°μ΄ν°λ‘ forward pass ...
torch.quantization.convert(model, inplace=True)
"To serve Llama 70B, we need two A100s, but with AWQ 4-bit quantization we can run it on a single A100. The performance degradation is about 1.5% on MMLU. It cuts infrastructure costs by 50%."
"Choosing a quantization method depends on the deployment environment. For GPU servers, GPTQ or AWQ is fast, and for Mac or CPU environments, GGUF Q4_K_M offers a good quality-speed trade-off. If extreme compression is needed, 2-bit is also possible, but the quality degradation is significant."
"AWQ is faster than GPTQ because it uses per-channel scaling instead of group-wise quantization, which makes matrix operations more efficient. However, GPTQ has slightly better perplexity and a more mature ecosystem, so the choice depends on the situation."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free