コンテンツにスキップ

LMDeploy - LLM 圧縮とサーブ チートシート

LMDeploy - LLM 圧縮とサーブ チートシート

LMDeploy (InternLM/OpenMMLab エコシステムから) は大規模言語モデルを 圧縮、量子化、サーブ するためのツールキットです。その高性能 TurboMind エンジンは永続的バッチング、ブロック化 KV キャッシュ、最適化 CUDA カーネルを経由して強力なスループットを提供し、PyTorch バックエンドも提供します。4 ビット AWQ 重み量子化と KV キャッシュ量子化をサポートし、OpenAI 互換 API サーバー、そしてビジョン言語モデル (VLM) をサポート。

インストール

方法コマンド
pippip install lmdeploy
CUDA エクストラpip install lmdeploy[all]
Dockerdocker run --gpus all openmmlab/lmdeploy:latest
要件NVIDIA GPU + CUDA
確認lmdeploy --version

クイック推論 (CLI)

# ターミナルでインタラクティブチャット (TurboMind エンジン)
lmdeploy chat internlm/internlm2_5-7b-chat

# バッチ/パイプライン推論で Python (下記)

Python パイプライン

from lmdeploy import pipeline

pipe = pipeline("internlm/internlm2_5-7b-chat")
resp = pipe(["Explain RAG in one sentence."])
print(resp[0].text)
呼び出し説明
pipeline(model)デフォルト (TurboMind) エンジンでモデルをロード
pipe([prompts])バッチ推論
GenerationConfig(...)サンプリングパラメータ (温度, top_p, max_new_tokens)
TurbomindEngineConfig(...)エンジンチューニング (tp, キャッシュ, セッション長)

API をサーブ

# OpenAI 互換サーバーをポート 23333 で
lmdeploy serve api_server internlm/internlm2_5-7b-chat --server-port 23333

# クエリする
curl http://localhost:23333/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"internlm2_5-7b-chat","messages":[{"role":"user","content":"hi"}]}'
コマンド説明
lmdeploy serve api_server MODELOpenAI 互換サーバーを開始
lmdeploy serve gradio MODELGradio ウェブ UI を起動
--server-portAPI ポート
--tp NN GPU 間でテンソル並列度
--session-len N最大コンテキスト長

量子化 (AWQ)

# 4 ビット AWQ 重みのみ量子化
lmdeploy lite auto_awq internlm/internlm2_5-7b-chat \
  --work-dir internlm2_5-7b-chat-4bit

# 量子化モデルをサーブ
lmdeploy serve api_server internlm2_5-7b-chat-4bit --model-format awq
コマンド説明
lmdeploy lite auto_awq MODEL4 ビット AWQ モデルを生成
lmdeploy lite calibrate MODELキャリブレーション ステップ
--model-format awqAWQ 量子化モデルをサーブ
KV キャッシュ量子化--quant-policy 4 または 8 for INT4/INT8 KV キャッシュ

エンジン設定

from lmdeploy import pipeline, TurbomindEngineConfig
pipe = pipeline("internlm/internlm2_5-7b-chat",
    backend_config=TurbomindEngineConfig(
        tp=2, session_len=8192, cache_max_entry_count=0.8, quant_policy=8))
オプションエフェクト
tpテンソル並列 GPU
session_lenコンテキスト長
cache_max_entry_countKV キャッシュ向け VRAM のフラクション
quant_policyKV キャッシュ量子化 (4/8)

ビジョン言語モデル

能力注記
VLM サポートInternVL, LLaVA, Qwen-VL などのモデルをサーブ
同じ APIOpenAI 互換サーバー経由でマルチモーダルメッセージ

LMDeploy vs その他のエンジン

アスペクトLMDeployvLLMAphrodite
エンジンTurboMind + PyTorchPagedAttentionvLLM フォーク
量子化AWQ + KV キャッシュ量子化成長中最も広いフォーマット
VLM サポート強いありあり
最適な用途ハイスループット + AWQ + VLM標準サーブコミュニティクワンタイズフォーマット

リソース