0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vLLM クイックスタートガイド

Posted at

vLLM は、LLM(大規模言語モデル)の高速推論と高スループットを実現する推論エンジンです。特に attention の KV キャッシュをページ方式で効率管理する「PagedAttention」により、複数リクエストの並列処理性能を大幅に向上させます ([GitHub][1])。
Hugging Face Transformers 等に比べ、GPU メモリの無駄が極めて小さく、高性能なバッチ処理を自動で行うのが特長です ([Medium][2], [GitHub][1])。


前提条件(Prerequisites)

  • Linux 環境
  • Python 3.9 ‑ 3.12
  • NVIDIA GPU(CUDA ドライバ対応) または 他のサポートデバイス ([vLLM][3])

1. インストール(Installation)

推奨環境管理ツール:uv

# Python 仮想環境を作成
uv venv --python 3.12 --seed
source .venv/bin/activate

# vLLM インストール(バックエンド自動選択)
uv pip install vllm --torch-backend=auto
  • --torch-backend=auto により CUDA のバージョンを自動判定してくれます ([vLLM][3])。
  • conda 等でも同様に仮想環境を構築可能です。

2. オフラインバッチ推論(Offline Batched Inference)

サンプルコード(Python)

from vllm import LLM, SamplingParams

prompts = [
    "Hello, my name is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

llm = LLM(model="facebook/opt-125m")
outputs = llm.generate(prompts, sampling_params)

for out in outputs:
    print(f"Prompt: {out.prompt!r}{out.outputs[0].text!r}")
  • LLM クラスが推論エンジンの核です。
  • SamplingParams による柔軟なデコーディング制御(温度・トップ p など)
  • generate メソッドは内部でキューイング&連続バッチ処理を行い、高スループットを実現します ([vLLM][3])。

3. OpenAI 互換 API サーバー(オンラインサービス)

サーバー起動

vllm serve facebook/opt-125m
# 必要に応じてホスト・ポート指定
vllm serve facebook/opt-125m --host 0.0.0.0 --port 8000
  • デフォルトで http://localhost:8000 に OpenAI API 互換サーバーが立ち上がります ([vLLM][3])。
  • --generation-config vllm を使うと、Hugging Face の generation_config.json を無効化できます。

Chat & Completion エンドポイントの例

Completions API

curl http://localhost:8000/v1/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "facebook/opt-125m",
    "prompt": "San Francisco is a",
    "max_tokens": 7,
    "temperature": 0
  }'

Chat API(会話形式)

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen/Qwen2.5‑1.5B‑Instruct",
    "messages": [
      {"role":"system","content":"You are a helpful assistant."},
      {"role":"user","content":"Tell me a joke."}
    ]
  }'
  • 既存の OpenAI Python クライアントへの切り替えは、API ベース URL を変更するだけで対応可能です ([vLLM][3], [apidog][4])。

4. PagedAttention の仕組み(核心技術)

  • Attention の KV キャッシュはトークン単位でページに分割し、動的に配置・参照されます。
  • メモリ断片化を抑制し、メモリ効率を極限まで高める構造です ([Medium][2], [GitHub][1])。

5. 応用・チューニングの視点

  • 🔧 モデル量子化支援:GPTQ, AWQ, INT8, FP8 などを自動で活用可能 ([GitHub][1])
  • 🧠 分散推論対応:テンソルパラレル/パイプラインパラレル実装可能
  • ⚡ 推論性能改善の鍵:SamplingParams の調整や CUDA バックエンド変更(FlashAttention, FlashInfer, XFormers など)も有効 ([GitHub][1])

6. ベストプラクティス(オペレーショナル観点)

項目 推奨設定または注意点
仮想環境 uvconda で vllm 環境を分離
モデル取得 Hugging Face Hub またはローカルパス(環境変数 VLLM_USE_MODELSCOPE
サンプリング設定 モデル依存の generation_config.json を確認/必要なら無効化
API セキュリティ 本番では --api-key または環境変数 VLLM_API_KEY の設定を推奨
モニタリング GPU 使用率、レイテンシ、バッチサイズを継続監視

✨ 総まとめ

  • vLLM は大量リクエスト/バッチ推論向けに最適設計された高性能 LLM エンジン
  • GPU メモリ管理効率を最大化する PagedAttention を核に、従来比で数倍以上のスループットを実現
  • 簡単な CLI 操作と Python API から、オフライン推論と OpenAI API 互換サーバーの両方で活用可能
  • 高度なモデル管理やデプロイングを自前で完結でき、オンプレ/クラウド運用に柔軟対応
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?