모델 양자화
양자화(Quantization)는 모델의 가중치와 활성화 값을 더 낮은 정밀도(FP32 -> INT8)로 변환하여 모델 크기를 줄이고 추론 속도를 높이는 기법입니다.import torch
from torch.quantization import quantize_dynamic
import timm
# 모델 로드
model = timm.create_model('resnet50', pretrained=True, num_classes=5)
model.eval()
# Dynamic Quantization (CPU 전용)
quantized_model = quantize_dynamic(
model,
{torch.nn.Linear, torch.nn.Conv2d}, # 양자화 대상 레이어
dtype=torch.qint8,
)
# 모델 크기 비교
import os
torch.save(model.state_dict(), 'fp32_model.pth')
torch.save(quantized_model.state_dict(), 'int8_model.pth')
fp32_size = os.path.getsize('fp32_model.pth') / (1024 * 1024)
int8_size = os.path.getsize('int8_model.pth') / (1024 * 1024)
print(f"FP32 크기: {fp32_size:.1f} MB")
print(f"INT8 크기: {int8_size:.1f} MB")
print(f"압축률: {fp32_size / int8_size:.1f}x")
from torch.quantization import prepare, convert, get_default_qconfig
# 양자화 설정
model.qconfig = get_default_qconfig('x86') # CPU용
prepared = prepare(model)
# 캘리브레이션: 대표 데이터로 통계 수집
with torch.no_grad():
for images, _ in calibration_loader: # 100~1000개 이미지
prepared(images)
# 양자화 적용
quantized = convert(prepared)
from onnxruntime.quantization import quantize_dynamic, QuantType
# ONNX 모델 Dynamic 양자화
quantize_dynamic(
model_input='model.onnx',
model_output='model_int8.onnx',
weight_type=QuantType.QInt8,
)
정밀도별 성능 비교
| 정밀도 | 비트 수 | 모델 크기 | 추론 속도 | 정확도 |
|---|---|---|---|---|
| FP32 | 32 | 기준 | 기준 | 기준 |
| FP16 | 16 | 50% | 2x | 거의 동일 |
| INT8 | 8 | 25% | 3~4x | 약간 감소 |
| INT4 | 4 | 12.5% | 4~8x | 감소 |
FP16과 INT8 중 어떤 것을 선택하나요?
FP16과 INT8 중 어떤 것을 선택하나요?
FP16은 정확도 손실이 거의 없고 설정이 간단하여 첫 번째 시도로 적합합니다. INT8은 추가 속도 향상이 필요할 때 선택하며, 캘리브레이션 데이터와 정확도 검증이 필요합니다.
양자화로 학습도 할 수 있나요?
양자화로 학습도 할 수 있나요?
QAT는 양자화 효과를 시뮬레이션하면서 학습하므로, PTQ보다 정확도 손실이 적습니다. 다만 학습 시간이 추가로 소요되며, 구현 복잡도가 높아집니다.

