C.W.K.
Stream
Lesson 03 of 06 · published

Quantization 기초 — Q4, Q8, 진짜 잃는 게 뭐

~16 min · quantization, q4, q8, precision

Level 0Curious
0 XP0/51 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

양자화가 진짜 뭐 하나

모델의 weight 는 floating-point 숫자. fp32 가 숫자 당 4 byte 사용. fp16/bf16 가 2 byte. int8 / Q8 이 1 byte. Q4 가 반 byte. 양자화가 high-precision weight 를 low-precision 근사로 대체, 디스크 크기와 메모리 풋프린트 큰 감소를 위해 모델 품질 약간 trade.

7B 모델에 대해, 차이 dramatic. fp16 에선 weight 만으로 ~14 GB 필요. Q8 에선 ~7 GB. Q4 에선 ~3.5 GB. 같은 모델, 세 다른 메모리 예산.

"Q4" 가 진짜 뭐 의미하나

순진한 해석은 "모든 weight 가 4-bit 정수." 진짜 이야기는 더 미묘. MLX (와 대부분 현대 양자화 schema) 가 weight 를 group 으로 저장. 32 / 64 / 128 weight 의 각 그룹이 scale factor 와 zero point 공유 — 그것들은 더 높은 정밀도 (전형적으로 fp16) 로 저장. 그룹 안에서 개별 weight 가 4 bit 로 양자화. 추론 시간에 weight 가 dequantized = scale * (quantized_int4 - zero_point) 로 재구성.

이걸 affine 양자화 라고 하고 그게 --q-mode affine (기본) 이 주는 거. 새로운 MX 모드 (mxfp4, nvfp4, mxfp8) 는 최신 GPU 의 specialized 하드웨어로 매핑되는 micro-scaling 형식 사용.

Q4 vs Q8 — 뭘 포기하나

  • 디스크와 메모리: Q4 가 (같은 모델에 대해) Q8 의 대략 절반 크기.
  • 추론 속도: Q4 가 가끔 약간 더 빠르고 (메모리 대역폭 덜 필요), 가끔 약간 더 느려 (추가 dequantization 작업). 대략 비슷하다고 다뤄.
  • 품질: Q4 가 대부분 작업에서 작은 양의 perplexity / 벤치마크 점수 잃고, 일부 (long-form reasoning, 저자원 언어) 에서 더 많이. Instruct 모델의 chat 사용 케이스엔, Q4 가 보통 캐주얼 평가에서 Q8 와 구별 안 됨; 벤치마크가 더 미묘한 이야기 말해.
  • 엣지 케이스: 매우 작은 모델 (1B 이하) 이 가끔 큰 모델보다 Q4 에서 더 degrade — 정밀도 손실 흡수할 parameter 적어. 1B 미만엔 Q8 생각, 3B+ 엔 Q4.

들고 다닐 평결

Apple Silicon 의 7B+ instruct 모델엔 Q4 가 기본 — 더 많은 메모리 예산에 fit 되고 품질 손실이 거의 병목 아냐. 정밀도-민감 워크로드 벤치마크 하거나 손실이 중요한 sub-3B 모델 돌릴 때만 Q8 로 step up. 메모리 태울 만하고 그 마지막 한 조각의 품질 신경 쓸 specific 이유 있을 때만 양자화 건너뛰기 (bf16/fp16 사용).

Code

같은 모델을 Q4 와 Q8 로 양자화 — 디스크에서 비교·bash
# Convert at Q4
python -m mlx_lm convert \
  --hf-path meta-llama/Llama-3.2-1B-Instruct \
  --mlx-path ./Llama-3.2-1B-Instruct-Q4 \
  --quantize --q-bits 4 --q-group-size 64

# Convert at Q8
python -m mlx_lm convert \
  --hf-path meta-llama/Llama-3.2-1B-Instruct \
  --mlx-path ./Llama-3.2-1B-Instruct-Q8 \
  --quantize --q-bits 8 --q-group-size 64

# Compare on disk
du -sh ./Llama-3.2-1B-Instruct-Q4 ./Llama-3.2-1B-Instruct-Q8

# Sample (1B Llama variant):
#   ~700 MB for Q4
#   ~1.3 GB for Q8
같은 프롬프트로 품질 눈으로 보기·python
from mlx_lm import load, generate
from mlx_lm.sample_utils import make_sampler
import mlx.core as mx

prompt = "Explain the difference between weather and climate in two sentences."
sampler = make_sampler(temp=0.0)   # greedy for fair comparison

for path in ["./Llama-3.2-1B-Instruct-Q4", "./Llama-3.2-1B-Instruct-Q8"]:
    print(f"--- {path} ---")
    model, tok = load(path)
    mx.random.seed(42)
    print(generate(model, tok, prompt=prompt, max_tokens=80, sampler=sampler, verbose=False))
    print()

# In casual reading the Q4 and Q8 outputs are usually indistinguishable;
# the differences appear in benchmark scores, not in this kind of demo.

External links

Exercise

어떤 작은 base 모델을 세 양자화 레벨로 변환 — Q4 group-size 32, Q4 group-size 128, Q8 group-size 64. 같은 프롬프트를 셋 다 통과 (공정 비교 위해 greedy sampling). 디스크 크기 비교, 출력 읽기. 운동은 — 정성적으로 — Q4 가 얼마나 작아지는지, group size 가 얼마나 중요한지, 그리고 출력을 읽어 구별할 수 있는지 느끼는 것.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.