İçeriğe geç

vLLM Production Mühendisliği: Paged Attention'dan SLA'lara — Modern LLM Sunumunun Anatomisi

vLLM'in matematiksel ve sistemsel anatomi: Paged attention (Kwon vd. 2023) niye RAM'i 5× verimli kullanıyor, continuous batching matematik, KV cache'in iç yapısı, OpenAI-uyumlu API, Türkçe Llama-3 deployment'ı baştan sona. Hardware seçimi (H100 vs A100 vs RTX 4090), Kubernetes setup, autoscaling, SLA garantileri.

Şükrü Yusuf KAYA
85 dakikalık okuma
İleri
vLLM Production Mühendisliği: Paged Attention'dan SLA'lara — Modern LLM Sunumunun Anatomisi
🏗️ vLLM — Modern LLM Sunumunun Mühendislik Harikası
Eylül 2023. UC Berkeley'den Woosuk Kwon ve ekibi (sonradan Anyscale) bir paper yayımladı: 'Efficient Memory Management for LLM Serving with PagedAttention'. Ne yapıyordu? Aynı GPU'da 5× daha çok kullanıcıya hizmet. Sihir? Hayır — işletim sistemi tarihinden ödünç alınan bir fikir: paging. Bilgisayar bilim öğrencilerinin 1960'larda öğrendiği konsept, 60 yıl sonra LLM dünyasını döndürdü. Bugün vLLM modern LLM sunumunun fiili standardı: HuggingFace TGI, Anyscale Ray, AWS SageMaker — hepsi alt katmanda vLLM'i veya benzerini kullanıyor. Bu ders vLLM'in iç matematiğini, üretim ortamında kurulumunu, Türkçe Llama-3 için özel optimizasyonları işliyor. 85 dakika sonra: vLLM'i sadece kullanmıyor, debug edebiliyor ve optimize edebiliyor olacaksın.

Bu Derste Neler Var? (13 Bölüm)#

  1. vLLM'in doğuşu — niye 2023'te?
  2. Paged Attention — bilgisayar mimarisi → LLM
  3. KV Cache'in iç yapısı — neyi sakladığımız
  4. Continuous Batching — gereksiz beklemenin sonu
  5. OpenAI-uyumlu API — drop-in replacement
  6. Hardware seçimi — H100 vs A100 vs RTX 4090
  7. Hızlı kurulum — ilk vLLM server'ı 10 dakikada
  8. Production Türkçe Llama-3 deployment — adım adım
  9. Kubernetes deployment — Helm chart, autoscaling
  10. Monitoring temelleri — Prometheus metrikleri
  11. SLA hesaplama — p50, p95, p99 latency
  12. Türkçe için optimizasyon — tokenizer, batch size
  13. Egzersizler ve troubleshooting

1-4. Paged Attention ve Continuous Batching#

1.1 vLLM doğuşu#

Kwon vd. 2023, Eylül 2023'te paper yayınladı: 'Efficient Memory Management for Large Language Model Serving with PagedAttention' Woosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng, Lianmin Zheng, Cody Hao Yu, Joseph E. Gonzalez, Hao Zhang, Ion Stoica SOSP 2023 (Symposium on Operating Systems Principles)
Dikkat: paper SOSP'ta yayınlandı — operating systems konferansı. AI değil! Çünkü çözüm OS'ten geliyor: paging.

1.2 Çözmek istedikleri problem: KV cache memory israfı#

LLM inference sırasında her token için KV cache tutulur:
  • Attention K (key) matrisleri
  • Attention V (value) matrisleri
Llama-3-8B için cache boyutu:
per_token_kv = 2 (K+V) × 32 layer × 32 head × 128 dim × 2 byte (bf16) = 524 KB / token per_request (8K context) = 4 GB per_request (128K context) = 64 GB
Problem: pre-vLLM (örn. naive PyTorch serving):
  • Request başına maximum cache allocate ediliyor (8K alan)
  • Kullanıcı sadece 500 token üretirse → 7.5K alan boşa harcanmış
  • 100 request × 4 GB = 400 GB cache, gerçekten kullanılan ~%5 = 20 GB
  • Memory waste: %95

1.3 Paged Attention çözümü#

İşletim sistemleri 1960'lardan beri paging kullanıyor: bellek küçük 'page'lere bölünür (4 KB), process'ler ihtiyaç duyduğunda dinamik alır.
vLLM aynısını LLM cache için yapıyor:
  • Cache 'block'lara bölünür (genelde 16 token = 8 KB blok)
  • Her request başlangıçta 1 blok alır, gerektikçe yeni blok
  • Üretim bittiğinde bloklar geri verilir
Sonuç:
  • Memory waste %95'ten %4'e iner
  • Aynı GPU'da 5× daha çok concurrent request
  • Kapasite 100 → 500 request

1.4 KV cache matematiği — sayısal#

Llama-3-8B, H100 80GB:
  • Model weights: 16 GB (bf16)
  • Aktivasyonlar: ~5 GB
  • KV cache için kalan: ~60 GB
Pre-vLLM: 8K context × 4 GB/request = 15 concurrent users vLLM: dinamik allocation, gerçek kullanıma göre = 75-100 concurrent users

2.1 Continuous Batching#

Klasik batching (PyTorch eager): batch'teki tüm request'ler bitene kadar GPU çalışıyor. En yavaş kullanıcı bekletilirken diğerleri 'idle'.
Örnek: 32 request batch'i. 30'u 50 token üretiyor (~1sn), 2'si 1000 token üretiyor (~20sn). Klasik batching: 32 request birlikte 20sn bekler.
Continuous batching (vLLM): her token döngüsünde batch yeniden kompoze edilir. Biten request çıkar, yeni gelen alınır. GPU her zaman dolu.
T=0: [A, B, C, D, E] (5 request batch'te) T=1: A bitti → [B, C, D, E, F] (yeni F geldi) T=2: C bitti → [B, D, E, F, G] ...
GPU utilization %30 → %85-95 çıkar.
bash
# vLLM Production Quickstart
# 10 dakikada Türkçe Llama-3 sunumu
 
# 1. Kurulum
pip install vllm
 
# 2. Türkçe DPO model'ini başlat (Modül 15.6 capstone)
vllm serve sukruyusufkaya/llama-3-8b-tr-dpo-instruct \
--tensor-parallel-size 1 \
--gpu-memory-utilization 0.9 \
--max-model-len 8192 \
--quantization awq \
--dtype bfloat16 \
--port 8000
 
# 3. Health check
curl http://localhost:8000/health
 
# 4. OpenAI-uyumlu API kullan (Python)
cat > test.py <<'EOF'
from openai import OpenAI
 
client = OpenAI(
base_url='http://localhost:8000/v1',
api_key='dummy', # vLLM auth gerek yok
)
 
response = client.chat.completions.create(
model='sukruyusufkaya/llama-3-8b-tr-dpo-instruct',
messages=[
{'role': 'system', 'content': 'Sen yardımsever bir Türk asistansın.'},
{'role': 'user', 'content': 'İstanbul Boğazı hakkında 3 ilginç bilgi paylaş.'},
],
max_tokens=500,
temperature=0.7,
stream=True, # streaming destekleniyor
)
 
for chunk in response:
print(chunk.choices[0].delta.content or '', end='', flush=True)
EOF
 
python test.py
 
# 5. Production monitoring endpoint'i
curl http://localhost:8000/metrics
# Prometheus formatında metrikler döner
 
vLLM Production Quickstart — Türkçe Llama-3 Self-Host

6, 8, 11. Hardware + Türkçe + SLA#

6.1 Hardware seçimi#

Llama-3-8B Türkçe DPO için:
NVIDIA H100 80GB (mid-range production):
  • Cloud: $2.5-4/saat
  • Throughput (vLLM): 3,000-4,000 token/sn
  • Concurrent: 75-100 request
  • Latency p50: 50-100ms TTFB, 0.8-1.2sn full response
  • Önerilen — orta-büyük production
NVIDIA A100 80GB (low-cost production):
  • Cloud: $1.5-2.5/saat
  • Throughput: 2,000-2,500 token/sn (H100'ün %60-70'i)
  • Concurrent: 50-75 request
  • Latency p50: 80-120ms TTFB
  • Önerilen — bütçe sınırlı production
NVIDIA RTX 4090 24GB (hobby/küçük):
  • Tek seferlik satın alma: $1,600-2,000
  • Throughput: 800-1,200 token/sn
  • Concurrent: 15-25 request
  • Latency p50: 100-150ms TTFB
  • Önerilen — küçük TR SaaS, MVP, geliştirme
RTX 4090 limit: 24GB VRAM. Llama-3-8B bf16 (16GB) + KV cache (~8GB) sığar. Llama-3-70B sığmaz. Quantization (AWQ 4-bit) ile Llama-3-70B 4090'a sığabilir ama tek concurrent user.

8.1 Production Türkçe Llama-3 deployment (Kubernetes)#

# vllm-llama-tr.yaml apiVersion: apps/v1 kind: Deployment metadata: name: llama-tr-vllm namespace: production spec: replicas: 2 selector: matchLabels: app: llama-tr-vllm template: metadata: labels: app: llama-tr-vllm spec: containers: - name: vllm image: vllm/vllm-openai:v0.6.0 args: - --model - sukruyusufkaya/llama-3-8b-tr-dpo-instruct - --tensor-parallel-size - '1' - --gpu-memory-utilization - '0.9' - --max-model-len - '8192' - --quantization - 'awq' - --port - '8000' ports: - containerPort: 8000 resources: limits: nvidia.com/gpu: 1 memory: 32Gi cpu: 8 requests: nvidia.com/gpu: 1 memory: 16Gi cpu: 4 livenessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 120 periodSeconds: 30 readinessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 60 periodSeconds: 10 --- apiVersion: v1 kind: Service metadata: name: llama-tr-svc spec: selector: app: llama-tr-vllm ports: - port: 80 targetPort: 8000 type: ClusterIP --- apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: llama-tr-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: llama-tr-vllm minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70

11.1 SLA hesaplama#

Production SLA'ları için 3 metrik:
Latency (p50, p95, p99):
  • p50 (medyan): ortalama kullanıcı deneyimi
  • p95: %95 kullanıcı bu kadar bekliyor
  • p99: 'worst case' deneyim
Llama-3-8B Türkçe vLLM (300 token output):
  • p50: 1.0 sn
  • p95: 1.8 sn
  • p99: 3.2 sn
Throughput: token/saniye (aggregate)
  • 1× H100, vLLM: 3,000-4,000 token/sn
  • 100 concurrent x 30-40 token/sn/user → 3,000-4,000 total
Uptime: % zaman sistem yanıt veriyor
  • Single GPU: ~%99 (donanım hatası nadir ama mümkün)
  • Multi-replica (2+ pod): %99.9
  • Multi-region: %99.99
Türkçe SaaS için pratik SLA:
  • p95 latency < 2 sn
  • Uptime %99.5+
  • Error rate < %0.1
  • Throughput hedef: 100K conv/gün → ortalama 1.2 token/sn aggregate (yedek kapasite ile 10×)
✅ Ders 16.2 Özeti — vLLM Production
vLLM (Kwon 2023) modern LLM sunumunun fiili standardı. Paged Attention = OS'ten paging fikrinin LLM'e taşınması. KV cache memory waste %95'ten %4'e iner. Continuous Batching = batch dinamik yeniden komposizyonu, GPU utilization %30→%85+. OpenAI-uyumlu API = drop-in replacement, mevcut SDK'lar değişmeden çalışır. Hardware: TR SaaS için H100 ideal (3K-4K token/sn, 75-100 user), A100 bütçe-sıkı (%60-70 perf), RTX 4090 MVP/hobby. Kubernetes + HPA ile autoscaling. SLA: p95 <2sn, uptime %99.5+ TR SaaS için. Sonraki ders: quantization — modeli küçültüp daha hızlı/ucuz koş.

Sonraki Ders: Quantization#

Ders 16.3'te modelin boyutu küçültülür, hız artar, maliyet düşer. INT8 vs INT4 vs FP8 quantization, GPTQ vs AWQ vs GGUF format, Llama-3 4-bit Türkçe model üretimi. Niye ve nasıl model 16GB'tan 4GB'a iner, kalite kaybı ne kadar, RTX 4090'da Llama-3-70B çalıştırma.

Sık Sorulan Sorular

Her ikisi de paged attention + continuous batching kullanan modern LLM serving framework'leri. Farklar: **vLLM**: - Doğum: UC Berkeley 2023, açık kaynak - Hızlı geliştirme, en son araştırma özellikleri çabuk gelir - Ekosistem: Anyscale, Together AI, Modal hepsi vLLM tabanlı - Daha esnek, daha çok hyperparameter **TGI (Text Generation Inference)**: - Doğum: HuggingFace 2022 (vLLM'den önce) - Daha stable, production-tested - HuggingFace ekosistemine sıkı entegre - Daha az hyperparameter (basit) **Performans**: 2024 sonunda benzer (vLLM marjinal hızlı bazı senaryolarda). **TR SaaS için tavsiye**: **vLLM** — daha geniş community, daha hızlı feature gelişimi, daha aktif geliştirme. TGI 2022-2023'te baskın'dı, 2024'te yer değiştirdi.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

İlgili İçerikler