C.W.K.
Stream
Lesson 10 of 12 · published

추론 최적화 — Speculative decoding, PagedAttention, continuous batching

~14 min · inference, vllm, speculative

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

모던 서빙 시스템은 프론티어 모델에서 쓸 만한 throughput 짜내려고 네 가지 주요 최적화 결합. 각각이 뭐 하는지 알기는 스택 선택과 성능 디버깅에 필수.

Flash Attention 2/3

Track 4에서 이미 다룸. attention을 타일링해서 (n × n) score 행렬이 HBM에 안 만들어지게. naive 대비 attention throughput 2-4배. FA3는 H100에서 FP8 + warp specialization 지원(~740 TFLOPs/s).

GQA / MQA

아키텍처 단위. query head 가로질러 K, V 공유로 KV cache 감소. Llama 3.3의 8 KV head(vs 64 Q head)가 cache 8배 축소. 더 적은 GPU 메모리에 긴 컨텍스트 수용 가능.

Speculative decoding

작은 draft 모델로 K개 후보 토큰 생성, target(큰) 모델의 단일 forward pass로 검증. 가장 긴 매칭 prefix 수용. 동일 출력 분포에 wall-clock 2-3배 속도. 구현: Llama-cpp, vLLM, MLC-LLM.

PagedAttention (vLLM)

KV cache를 OS virtual-memory 시스템처럼 관리 — 고정 크기 페이지, 논리에서 물리로 매핑하는 block 테이블, 공유 prefix용 copy-on-write. 단편화 낭비를 60-80%에서 ~4%로 감소, GPU당 동시 요청 수 극적으로 증가.

Continuous batching

Iteration 단위 스케줄링 — 매 디코딩 step에서 끝난 시퀀스 제거 + 새 요청 슬롯 채움. GPU idle 시간을 ~40%에서 10% 미만으로 감소. 정적 batching 대비 throughput 5-23배 개선.

기법해결하는 것속도
Flash Attentionattention의 메모리 대역폭2-4배
GQA / MQAKV cache 크기cache 2-8배 감소
Speculative decoding순차 decode 병목wall-clock 2-3배
PagedAttention메모리 단편화GPU당 요청 ~20배
Continuous batchingGPU idle 시간throughput 5-23배

Code

Serving with vLLM (PagedAttention + continuous batching)·python
from vllm import LLM, SamplingParams

llm = LLM(model="meta-llama/Llama-3-8B-Instruct",
          gpu_memory_utilization=0.9,
          max_model_len=8192)

# Speculative decoding (small draft model)
# llm = LLM(model="...", speculative_model="meta-llama/Llama-3.2-1B")

params = SamplingParams(temperature=0.7, top_p=0.9, max_tokens=512)
outputs = llm.generate([prompt for prompt in many_prompts], params)
# vLLM batches them dynamically with continuous batching;
# PagedAttention manages KV cache pages.
# At scale this is 10-20× faster than naive HuggingFace generate.

External links

Exercise

작은 open 모델로 vLLM 설정. throughput(batch 가로지른 tokens/sec) 비교 — (a) HuggingFace의 pipeline(...), (b) model.generate(...) batched 입력, (c) vLLM. 같은 모델, 같은 32 prompt batch 사용. 속도 차이 문서화.

Progress

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

댓글 0

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

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