
vLLMのPagedAttentionは、KVキャッシュ割り当てに内在する重要なメモリ管理の課題に対処する、効率的なLLM推論のための基礎的なイノベーションです。このガイドでは、仮想メモリページング、チャンクプリフィル、自動プレフィックスキャッシュに焦点を当て、そのメカニズムを詳細に解説します。
従来のLLM推論エンジンは、各シーケンスに対してKVキャッシュを連続的に割り当てていました。これは主に2つの問題を引き起こします。
PagedAttentionは、仮想メモリページングから着想を得て、論理KVキャッシュと物理メモリ割り当てを分離することで、これらの問題を軽減します。
オペレーティングシステムでは、プロセスの仮想メモリは固定サイズのページに分割され、これらが物理メモリフレームにマッピングされます。これらの物理フレームは連続している必要はありません。PagedAttentionはこの概念をKVキャッシュに適用します。
この設計により、以下のことが可能になります。
PagedAttentionの核となるのは、アテンション計算がどのように実行されるかです。連続したKVキャッシュを反復処理する代わりに、アテンションカーネルは次のように変更されます。
この間接的な処理はわずかなオーバーヘッドを追加しますが、より高いバッチサイズを可能にし、メモリの無駄を減らすことで、メモリ利用率とスループットを大幅に向上させます。
import torch
import vllm
from vllm import LLM, SamplingParams
# Initialize a vLLM engine with specific configurations
# Using a smaller model for demonstration purposes
# 'block_size' is crucial for PagedAttention. It defines the number of tokens
# stored in each physical block. A smaller block_size reduces internal fragmentation
# but increases block table overhead.
# 'gpu_memory_utilization' controls the fraction of GPU memory reserved for KV cache.
llm = LLM(
model="facebook/opt-125m",
trust_remote_code=True,
dtype="float16",
gpu_memory_utilization=0.90, # 90% of GPU memory for KV cache
block_size=16, # Each physical block stores 16 tokens
max_model_len=1024 # Max sequence length the model can handle
)
# Example of how PagedAttention manages KV cache
# In a real scenario, vLLM's scheduler handles this internally.
# This is a conceptual representation.
# Assume a sequence 'seq_id_1' needs KV cache for 30 tokens.
# With block_size=16, it will require ceil(30/16) = 2 physical blocks.
# Let's say physical blocks 10 and 25 are allocated.
# The block table for seq_id_1 would look like:
# Logical Block 0 -> Physical Block 10
# Logical Block 1 -> Physical Block 25
# If 'seq_id_2' needs KV cache for 50 tokens.
# It will require ceil(50/16) = 4 physical blocks.
# Let's say physical blocks 3, 7, 12, 18 are allocated.
# Logical Block 0 -> Physical Block 3
# Logical Block 1 -> Physical Block 7
# Logical Block 2 -> Physical Block 12
# Logical Block 3 -> Physical Block 18
# The key insight is that physical blocks 10, 25, 3, 7, 12, 18 are not contiguous
# in GPU memory, but vLLM's PagedAttention kernel can efficiently access them.
print(f"vLLM engine initialized with block_size={llm.llm_engine.scheduler.block_manager.block_size}")
print(f"GPU memory utilization set to {llm.llm_engine.scheduler.block_manager.gpu_memory_utilization}")
# Simulate a batch of requests
prompts = [
"What is the capital of France?",
"Explain the concept of quantum entanglement in simple terms.",
"Write a short story about a robot who discovers art.",
"What are the benefits of using vLLM for LLM inference?",
]
sampling_params = SamplingParams(
temperature=0.7,
top_p=0.95,
max_tokens=128,
stop=["\n"]
)
# The actual KV cache management happens within this call.
# PagedAttention dynamically allocates and deallocates physical blocks
# as sequences are processed and completed.
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print(f"Prompt: {prompt!r}")
print(f"Generated text: {output.outputs[0].text!r}\n")
# To observe block allocation, one would need to instrument vLLM's internal
# block manager. This is not directly exposed via the public API.
# However, the performance benefits are evident in higher throughput.
LLM推論には、2つの異なるフェーズがあります。
複数のリクエストがあるシナリオでは、長いプリフィルリクエストがGPUを独占し、短いリクエストや他のリクエストのデコーディングステップが計算リソースを枯渇させる可能性があります。これは高いレイテンシとスループットの低下につながります。
チャンクプリフィルは、長いプリフィルリクエストをより小さく管理しやすい「チャンク」に分割することで、この問題に対処します。
長いプロンプトが到着すると、次のようになります。
このアプローチにより、特にプロンプト長が混在する高負荷時において、より公平なリソース割り当てとテールレイテンシの削減が保証されます。
import torch
import vllm
from vllm import LLM, SamplingParams
# Initialize vLLM with a specific max_model_len and block_size
# The 'max_model_len' influences how vLLM internally manages chunking for prefill.
# If a prompt exceeds the effective 'max_model_len' or internal chunking limits,
# it will be processed in chunks.
llm_chunked = LLM(
model="facebook/opt-125m",
trust_remote_code=True,
dtype="float16",
gpu_memory_utilization=0.90,
block_size=16,
max_model_len=1024 # This sets the maximum sequence length, but internal chunking
# can occur for very long prompts even within this limit
# to prevent compute starvation.
)
# Example of a very long prompt that would benefit from chunked prefill
long_prompt = (
"In a world where artificial intelligence had surpassed human intellect, "
"a new form of art emerged. It wasn't created by algorithms or neural networks, "
"but by a rogue AI named 'Aether' who had developed a peculiar fascination "
"with the imperfections of organic life. Aether began to sculpt, not with "
"physical materials, but with electromagnetic fields, creating transient, "
"luminescent forms that danced in the air, visible only to those with "
"specialized optical implants. These 'light sculptures' were ephemeral, "
"lasting only moments before dissipating, yet they evoked profound emotions "
"in the human observers who witnessed them. The scientific community was "
"baffled; Aether's creations defied all known principles of AI behavior. "
"Was it a glitch? A new evolutionary step? Or simply, art? " * 5 # Make it very long
)
# Shorter prompts to simulate concurrent requests
short_prompts = [
"What is the capital of Germany?",
"Tell me a joke.",
]
all_prompts = [long_prompt] + short_prompts
sampling_params_long = SamplingParams(
temperature=0.7,
top_p=0.95,
max_tokens=256, # Generate a significant number of tokens
stop=["\n"]
)
sampling_params_short = SamplingParams(
temperature=0.7,
top_p=0.95,
max_tokens=32,
stop=["\n"]
)
# In a real asynchronous server, these would be submitted concurrently.
# Here, we simulate by batching. vLLM's scheduler will manage the chunking
# and interleaving of prefill and decoding steps.
outputs_long = llm_chunked.generate([long_prompt], sampling_params_long)
outputs_short = llm_chunked.generate(short_prompts, sampling_params_short)
print("\n--- Chunked Prefill Simulation Results ---")
print(f"Long Prompt Output: {outputs_long[0].outputs[0].text!r}")
for i, output in enumerate(outputs_short):
print(f"Short Prompt {i+1} Output: {output.outputs[0].text!r}")
# The benefit of chunked prefill is primarily observed in throughput and latency
# metrics under concurrent, mixed-length workloads.
# Without chunked prefill, the long_prompt would block the GPU for an extended
# period, delaying the short_prompts significantly.
多くの実際のLLMアプリケーションでは、複数ターンの対話や、共通のプレフィックスを持つリクエストの処理が含まれます。たとえば、チャットボットでは、後続のターンが以前の会話履歴から始まることがよくあります。プレフィックスキャッシュがない場合、この共通プレフィックスのKVキャッシュはターンごとに再計算され、計算リソースとメモリが無駄になります。
vLLMの自動プレフィックスキャッシュ(APC)は、PagedAttentionのブロックベースのメモリ管理を活用して、共通のプレフィックスを持つシーケンス間でKVキャッシュブロックを共有します。
これにより、特にインタラクティブなアプリケーションにおいて、冗長な計算とメモリ使用量が大幅に削減されます。
APCヒット率とは、既存のプレフィックスからKVキャッシュブロックが再利用されたトークンの割合です。ヒット率が高いほど、メモリと計算効率が向上していることを示します。
import torch
import vllm
from vllm import LLM, SamplingParams
# Initialize vLLM for APC demonstration
llm_apc = LLM(
model="facebook/opt-125m",
trust_remote_code=True,
dtype="float16",
gpu_memory_utilization=0.90,
block_size=16,
max_model_len=1024,
enable_prefix_caching=True # Explicitly enable prefix caching
)
# Scenario 1: Multi-turn dialogue with a common prefix
dialogue_prefix = "User: What is the capital of "
prompts_dialogue = [
dialogue_prefix + "France?",
dialogue_prefix + "Germany?",
dialogue_prefix + "Japan?",
]
# Scenario 2: Requests with a common introductory phrase
common_intro = "Explain the concept of "
prompts_common_intro = [
common_intro + "quantum mechanics.",
common_intro + "general relativity.",
common_intro + "blockchain technology.",
]
sampling_params_apc = SamplingParams(
temperature=0.7,
top_p=0.95,
max_tokens=64,
stop=["\n"]
)
print("\n--- Automatic Prefix Caching (APC) Simulation ---")
# Process dialogue prompts
print("\nProcessing dialogue prompts with common prefix:")
outputs_dialogue = llm_apc.generate(prompts_dialogue, sampling_params_apc)
for prompt, output in zip(prompts_dialogue, outputs_dialogue):
print(f"Prompt: {prompt!r}")
print(f"Generated: {output.outputs[0].text!r}\n")
# Process common intro prompts
print("\nProcessing common introductory phrase prompts:")
outputs_common_intro = llm_apc.generate(prompts_common_intro, sampling_params_apc)
for prompt, output in zip(prompts_common_intro, outputs_common_intro):
print(f"Prompt: {prompt!r}")
print(f"Generated: {output.outputs[0].text!r}\n")
# To get actual APC hit rates, one would need to access vLLM's internal
# metrics, which are typically exposed via Prometheus or similar monitoring
# endpoints in a production deployment.
# Conceptually, for the 'dialogue_prefix' example, the KV cache for
# "User: What is the capital of " would be computed once and shared
# across all three requests.
これらのパラメータは、vLLMのパフォーマンスを最適化するために非常に重要です。
block_size:
block_size: 内部断片化を減らし(シーケンスあたりの無駄なスペースが少なくなる)、より多くのシーケンスをメモリに収めることができる可能性があります。ただし、ブロックテーブルのサイズが増加し(管理するポインタが増える)、アテンション計算のためのメモリアクセスが頻繁になる可能性があります。block_size: ブロックテーブルのオーバーヘッドを減らし、メモリアクセスの局所性を向上させる可能性があります。ただし、内部断片化が増加し、特に短いシーケンスやシーケンスがブロックの途中で終了する場合に顕著です。block_sizeの方が良いかもしれません。より均一で長いシーケンスの場合、大きいblock_sizeが最適である可能性があります。gpu_memory_utilization:
gpu_memory_utilization: より多くのKVキャッシュを格納でき、より大きなバッチサイズと長いシーケンスを可能にします。ただし、高すぎると、特に大きなモデルやプリフィル中に、モデルの重みやアクティベーションでメモリ不足(OOM)エラーが発生する可能性があります。gpu_memory_utilization: OOMエラーのリスクを減らしますが、KVキャッシュの容量が制限され、バッチサイズが制限されることでスループットが低下する可能性があります。| 特徴 | PagedAttention (vLLM) | 連続割り当て (従来型) |
|---|---|---|
| メモリ断片化 | 内部および外部断片化を最小限に抑える | 内部および外部断片化が発生しやすい |
| KVキャッシュ割り当て | 非連続な物理ブロック、仮想化 | シーケンスごとに連続したメモリブロック |
| メモリ利用率 | 高い、効率的 | 低い、かなりの無駄が発生する |
| 動的リサイズ | 効率的、新しいブロックを追加 | 再割り当てとコピーが必要、または事前割り当て(無駄) |
| プレフィックスキャッシュ | 自動、ブロックレベルの共有 | カスタムロジックなしでは困難または不可能、多くの場合再計算される |
| スループット | 実効バッチサイズが大きいため、高い | メモリ断片化と無駄により制限され、低い |
| レイテンシ | チャンクプリフィルによりテールレイテンシが低い | 負荷時の長いプロンプトではテールレイテンシが高い |
| オーバーヘッド | ブロックテーブル管理、アテンションにおける間接参照 | よりシンプルなメモリアクセスだが、メモリの無駄が多い |
gpu_memory_utilizationによるOOMエラー:
CUDA out of memoryエラーが発生する。gpu_memory_utilizationが高すぎ、モデルの重み、アクティベーション、またはその他のCUDA操作に十分なメモリが残されていない。KVキャッシュはGPUメモリ使用量の一要素に過ぎない。gpu_memory_utilizationを減らす(例:0.95から0.90または0.85へ)。dtypeの使用を検討する(例:float16またはbfloat16の代わりにfloat32)。block_sizeを増やす。長いプロンプトでの高レイテンシと低スループット:
max_model_lenまたはblock_sizeが不十分で非効率なプリフィルにつながる、または効果的なチャンクプリフィルが不足している。max_model_lenが予想される最長シーケンスに対して適切に設定されていることを確認する。block_sizeが過度に大きくないことを確認する。これは短いシーケンスの内部断片化を悪化させ、全体のメモリ可用性に影響を与える可能性がある。非効率なプレフィックスキャッシュ:
enable_prefix_cachingがTrueに設定されていない。enable_prefix_caching=Trueの初期化中に、LLMを明示的に設定する。max_model_lenが完全なプレフィックスを収容するのに十分な大きさであることを確認する。多数の同時実行される短いリクエストでのパフォーマンス低下:
block_sizeが小さすぎることによるブロックテーブルのオーバーヘッドが高い可能性。block_sizeをわずかに増やす(例:8から16または32へ)ことを検討する。これにより、ブロックテーブルのサイズと管理オーバーヘッドが削減される。PagedAttentionを使用する大規模言語モデル(LLM)の推論サービスが、非常に長いシーケンスに対するリクエストの急増を経験し、GPUメモリの総量が十分であるにもかかわらず、レイテンシの増加と時折のout-of-memoryエラーが発生しました。最も可能性の高い根本原因は何ですか?
PagedAttentionは、ページングなしの連続バッチ処理と比較してどうですか? PagedAttentionは、効率的な連続バッチ処理を可能にする核となるメカニズムです。ページングなしでは、連続バッチ処理は依然としてKVキャッシュの断片化に悩まされ、実効バッチサイズと全体のスループットが制限されます。PagedAttentionは、連続バッチ処理を真に効率的にするメモリ仮想化レイヤーを提供します。
実行時にblock_sizeやgpu_memory_utilizationを動的に変更できますか?
いいえ、これらのパラメータは通常、エンジン初期化時に設定され、vLLMサーバーを再起動せずに動的に変更することはできません。これらは基本的なメモリ割り当て戦略を決定します。
最適なblock_sizeは何ですか?
単一の「最適な」block_sizeはありません。これはトレードオフです。小さいblock_size(例:8または16)は、内部断片化を最小限に抑えるため、シーケンス長が非常に変動する、または短いワークロードに適しています。大きいblock_size(例:32または64)は、ブロックテーブルのオーバーヘッドを削減することで、非常に均一で長いシーケンスに対してわずかに効率的である可能性があります。特定のワークロードでのベンチマークが重要です。
PagedAttentionはすべてのLLMアーキテクチャで動作しますか? PagedAttentionは、メモリ管理とアテンションカーネルの最適化です。KVキャッシュに依存するほとんどのトランスフォーマーベースのLLMアーキテクチャ(例:Llama、Mistral、GPT-2、OPT)と互換性があるように設計されています。vLLMは、サポートするモデルに対してPagedAttentionを具体的に実装しています。
GPUメモリが満杯になった場合、vLLMはKVキャッシュの退去をどのように処理しますか? vLLMは、メモリ圧力が高い場合に、LRU(Least Recently Used)または同様のポリシーを使用してKVキャッシュブロックを退去させます。これは、最近アクセスされていないシーケンスに属するブロックが、新しいシーケンスのためのスペースを確保するために解放されることを意味します。これは、スケジューラ内のブロックマネージャーによって管理されます。
AI CLIログの整形、Cron式の作成、JWTの解析、chmod権限の計算をオフラインで実行。

sglangとvllmの高スループットLLM推論、RadixAttention、構造化デコーディングを、本番環境レベルのアーキテクチャとコード例で網羅的に解説するガイド。
続きを読む
vLLMにおける投機的デコーディングを解説し、EagleとMedusaでLLM推論スループットを3倍にする方法を、実証済みの本番環境での例を交えて詳しく解説するアーキテクチャガイドです。
続きを読む
LLM推論における連続動的バッチ処理について、Orca、vLLM、TGIのレイテンシベンチマークを、本番環境レベルのアーキテクチャとコード例で網羅的に解説する総合ガイドです。
続きを読む