🤖 AI/ML

vLLM

Virtual Large Language Model

An LLM inference optimization library. Maximizes throughput with PagedAttention.

📖 Detailed Explanation

vLLM is a high-performance LLM inference and serving library developed at UC Berkeley. Since its release in 2023, it has achieved 2 to 24 times higher throughput than previous approaches through an innovative memory management technique called PagedAttention, and has established itself as an industry standard. Because it makes it easy to set up a server compatible with the OpenAI API, it has become an essential tool for building production LLM services.

PagedAttention, the core technology of vLLM, was inspired by virtual memory paging in operating systems. In conventional LLM inference, the KV cache (Key-Value Cache) had to occupy contiguous memory space, so memory was pre-allocated according to sequence length and the remaining space was wasted. PagedAttention divides the KV cache into fixed-size blocks and stores them non-contiguously, reducing memory waste by 60 to 80%.

vLLM batches requests dynamically through Continuous Batching. With traditional static batching, the GPU had to wait until the longest sequence finished, but vLLM immediately removes completed requests and adds new ones. It also supports Tensor Parallelism and Pipeline Parallelism, so it can efficiently serve large models (70B+) in multi-GPU environments.

In practice, vLLM supports most open-source LLMs, including Llama, Mistral, Qwen, and Yi, and can also serve AWQ/GPTQ quantized models. It provides the features needed for production out of the box, such as deployment in Kubernetes environments, Prometheus metrics exposure, and an OpenAI-compatible API. As of 2025, it records a throughput of 2,300 to 2,500 tokens/s for Llama 8B on an H100, showing a throughput difference of more than 20 times compared to Ollama.

💻 Code Example

# vLLM 완전 가이드: 서버 구축, 클라이언트, 벤치마킹
# pip install vllm openai

# ============================================
# 1. vLLM 서버 실행 (터미널에서)
# ============================================
# 기본 실행 (단일 GPU)
# python -m vllm.entrypoints.openai.api_server \
#     --model meta-llama/Llama-3.1-8B-Instruct \
#     --port 8000

# 멀티 GPU (Tensor Parallelism)
# python -m vllm.entrypoints.openai.api_server \
#     --model meta-llama/Llama-3.1-70B-Instruct \
#     --tensor-parallel-size 4 \
#     --port 8000

# 양자화 모델 서빙 (메모리 절약)
# python -m vllm.entrypoints.openai.api_server \
#     --model TheBloke/Llama-2-70B-Chat-AWQ \
#     --quantization awq \
#     --port 8000

# ============================================
# 2. Python에서 vLLM 직접 사용
# ============================================
from vllm import LLM, SamplingParams

def basic_vllm_inference():
    """vLLM으로 오프라인 배치 추론"""
    # 모델 로드
    llm = LLM(
        model="meta-llama/Llama-3.1-8B-Instruct",
        tensor_parallel_size=1,  # GPU 수
        gpu_memory_utilization=0.9,  # GPU 메모리 사용률
        max_model_len=4096  # 최대 컨텍스트 길이
    )

    # 샘플링 파라미터
    sampling_params = SamplingParams(
        temperature=0.7,
        top_p=0.95,
        max_tokens=512,
        stop=["", "[/INST]"]
    )

    # 배치 프롬프트
    prompts = [
        "[INST] Python으로 피보나치 함수를 작성해주세요. [/INST]",
        "[INST] 머신러닝과 딥러닝의 차이를 설명해주세요. [/INST]",
        "[INST] Docker의 장점을 3가지 알려주세요. [/INST]",
    ]

    # 추론 (자동 배칭)
    outputs = llm.generate(prompts, sampling_params)

    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt[:50]}...")
        print(f"Output: {generated_text[:200]}...")
        print("-" * 50)

    return outputs

# ============================================
# 3. OpenAI 호환 클라이언트 사용
# ============================================
from openai import OpenAI

def vllm_openai_client():
    """vLLM 서버를 OpenAI 클라이언트로 호출"""
    # vLLM 서버 주소 (로컬)
    client = OpenAI(
        base_url="http://localhost:8000/v1",
        api_key="dummy"  # vLLM은 API 키 검증 안 함
    )

    # Chat Completion
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[
            {"role": "system", "content": "당신은 유능한 AI 어시스턴트입니다."},
            {"role": "user", "content": "vLLM의 장점을 설명해주세요."}
        ],
        temperature=0.7,
        max_tokens=500
    )

    print(response.choices[0].message.content)
    return response

# ============================================
# 4. 스트리밍 응답
# ============================================
def vllm_streaming():
    """스트리밍으로 토큰 단위 응답 받기"""
    client = OpenAI(
        base_url="http://localhost:8000/v1",
        api_key="dummy"
    )

    stream = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[
            {"role": "user", "content": "Python의 GIL에 대해 설명해주세요."}
        ],
        stream=True,
        max_tokens=300
    )

    full_response = ""
    for chunk in stream:
        if chunk.choices[0].delta.content:
            content = chunk.choices[0].delta.content
            print(content, end="", flush=True)
            full_response += content

    return full_response

# ============================================
# 5. 벤치마킹 및 성능 측정
# ============================================
import time
import asyncio
import aiohttp

async def benchmark_vllm(
    base_url: str = "http://localhost:8000",
    num_requests: int = 100,
    concurrency: int = 10
):
    """vLLM 서버 벤치마킹"""
    prompt = "What is the capital of France? Answer in one word."

    async def single_request(session):
        start = time.time()
        async with session.post(
            f"{base_url}/v1/completions",
            json={
                "model": "meta-llama/Llama-3.1-8B-Instruct",
                "prompt": prompt,
                "max_tokens": 50,
                "temperature": 0
            }
        ) as response:
            result = await response.json()
            latency = time.time() - start
            tokens = result.get("usage", {}).get("completion_tokens", 0)
            return latency, tokens

    # 동시 요청 실행
    async with aiohttp.ClientSession() as session:
        semaphore = asyncio.Semaphore(concurrency)

        async def bounded_request():
            async with semaphore:
                return await single_request(session)

        start_time = time.time()
        results = await asyncio.gather(*[
            bounded_request() for _ in range(num_requests)
        ])
        total_time = time.time() - start_time

    # 결과 분석
    latencies = [r[0] for r in results]
    total_tokens = sum(r[1] for r in results)

    print(f"=== vLLM Benchmark Results ===")
    print(f"Total requests: {num_requests}")
    print(f"Concurrency: {concurrency}")
    print(f"Total time: {total_time:.2f}s")
    print(f"Throughput: {num_requests / total_time:.2f} req/s")
    print(f"Token throughput: {total_tokens / total_time:.2f} tokens/s")
    print(f"Avg latency: {sum(latencies) / len(latencies) * 1000:.2f} ms")
    print(f"P50 latency: {sorted(latencies)[len(latencies)//2] * 1000:.2f} ms")
    print(f"P99 latency: {sorted(latencies)[int(len(latencies)*0.99)] * 1000:.2f} ms")

# ============================================
# 6. 프로덕션 설정 예시 (Docker Compose)
# ============================================
DOCKER_COMPOSE_EXAMPLE = """
# docker-compose.yml
version: '3.8'
services:
  vllm:
    image: vllm/vllm-openai:latest
    ports:
      - "8000:8000"
    volumes:
      - ~/.cache/huggingface:/root/.cache/huggingface
    environment:
      - HUGGING_FACE_HUB_TOKEN=${HF_TOKEN}
    command: >
      --model meta-llama/Llama-3.1-8B-Instruct
      --tensor-parallel-size 1
      --gpu-memory-utilization 0.9
      --max-model-len 8192
      --enable-prefix-caching
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
"""

# 실행 예시
if __name__ == "__main__":
    # 오프라인 추론 (vLLM 직접 사용)
    # basic_vllm_inference()

    # 서버 클라이언트 사용
    # vllm_openai_client()

    # 스트리밍
    # vllm_streaming()

    # 벤치마크 실행
    # asyncio.run(benchmark_vllm())

    print("vLLM 코드 예제 준비 완료!")
    print("서버 실행: python -m vllm.entrypoints.openai.api_server --model ")

📊 Performance & Cost

vLLM Inference Performance Benchmark (as of 2025)

Model GPU Throughput (tokens/s) P99 Latency Notes
Llama 3.1 8B H100 80GB 2,300~2,500 80ms Single GPU
Llama 3.1 70B 4x A100 80GB 400~600 150ms TP=4
Qwen2.5 7B 3x V100 32GB 1,782~2,474 ~100ms TP=2
Gemma3 4B A100 40GB 3,000+ 50ms Small Model
Mistral 7B AWQ RTX 4090 24GB 800~1,200 120ms Quantization

vLLM vs. Competing Solutions Comparison

Solution Peak Throughput P99 Latency Memory Efficiency
vLLM 793 TPS 80ms 60–80% reduction
Ollama 41 TPS 673ms Basic
HuggingFace TGI ~500 TPS ~120ms Flash Attention
TensorRT-LLM ~900 TPS ~70ms NVIDIA-optimized

GPU Requirements Guide

Model Size FP16 VRAM AWQ/GPTQ Recommended GPU
7B ~14GB ~4GB RTX 4090 / A10
13B ~26GB ~7GB A100 40GB
34B ~68GB ~18GB A100 80GB
70B ~140GB ~35GB 2x A100 80GB
405B ~810GB ~100GB 8x H100 80GB
Note: As of vLLM 0.6.0, it achieved a 2.7x throughput improvement and a 5x latency reduction compared to previous versions. On an H100, Llama-3.3-70B FP8 inference is highly energy-efficient at 0.39 joules per token. For the KV cache, an additional 40GB+ of headroom is needed beyond the model's VRAM.

🗣️ Say It Like This in Practice

💬 In a Meeting

"We're reviewing self-hosted serving because LLM API costs are too high." - "If you self-host with vLLM, you can cut costs by more than 90%. Serving Llama 8B on a single H100 can handle more than 2,000 tokens per second, and since it's 100% compatible with the OpenAI API, you only need to change base_url to update your code. There is an upfront GPU cost, but at more than 100,000 requests per month, you'll recoup it within 6 months."

💬 In an Interview

"Why is vLLM's PagedAttention important?" - "In conventional LLM inference, the KV cache had to pre-allocate memory up to the maximum sequence length. Only part of it is actually used, and the rest is wasted. PagedAttention, like the OS's virtual memory, allocates only as much as needed in block units, reducing memory waste by 60 to 80%. This lets the same GPU handle more concurrent requests."

💬 In a Technical Discussion

"vLLM vs. TensorRT-LLM: which one should I use?" - "vLLM is easy to install and supports a wide range of models right away, so it's good for rapid prototyping. TensorRT-LLM is built by NVIDIA and delivers optimal performance on the H100, but it requires a model conversion process and is exclusive to NVIDIA GPUs. If you're just starting out or testing various models, go with vLLM; if you need maximum performance in production, consider TensorRT-LLM."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
GPU Memory Over-allocation

Setting gpu_memory_utilization to 0.95 or higher leaves too little KV cache space and causes OOM. 0.85 to 0.90 is safe, and if you need a long context, explicitly limit max_model_len.

❌
Tensor Parallelism Size Mismatch

tensor_parallel_size must evenly divide the model's number of attention heads. For example, TP=3 is not possible for a model with 32 heads. Also, low inter-GPU communication bandwidth can actually degrade performance, so an NVLink connection is recommended.

✅
The Right Approach

In production, enable system prompt caching with the --enable-prefix-caching option. Monitor Prometheus metrics (/metrics), and prevent cold starts by pre-downloading HuggingFace models (huggingface-cli download). Quantized models (AWQ/GPTQ) can optimize throughput relative to memory.

🔗 Related Terms

📚 Learn More