P2P KV Cache 共享#
P2P(点对点)KV 缓存共享允许多个服务引擎实例之间直接传输缓存,而无需集中式缓存服务器。这种方法提供了高性能的缓存共享,减少了延迟并提高了可扩展性,特别是在分布式推理场景中非常有利。
LMCache 支持通过基于控制器的架构进行 P2P 共享,使用 NIXL(NVIDIA 推理传输库)优化实例之间的数据传输。
前提条件#
配置#
为 P2P 共享设置创建两个配置文件。
实例 1 配置 (example1.yaml):
chunk_size: 256
local_cpu: True
max_local_cpu_size: 100
enable_async_loading: True
# P2P configurations
enable_p2p: True
p2p_host: "localhost"
p2p_init_ports: 8200
p2p_lookup_ports: 8201
transfer_channel: "nixl"
# Controller configurations
enable_controller: True
lmcache_instance_id: "lmcache_instance_1"
controller_pull_url: "localhost:8300"
controller_reply_url: "localhost:8400"
lmcache_worker_ports: 8500
extra_config:
lookup_backoff_time: 0.001
实例 2 配置 (example2.yaml):
chunk_size: 256
local_cpu: True
max_local_cpu_size: 100
enable_async_loading: True
# P2P configurations
enable_p2p: True
p2p_host: "localhost"
p2p_init_ports: 8202
p2p_lookup_ports: 8203
transfer_channel: "nixl"
# Controller configurations
enable_controller: True
lmcache_instance_id: "lmcache_instance_2"
controller_pull_url: "localhost:8300"
controller_reply_url: "localhost:8400"
lmcache_worker_ports: 8501
extra_config:
lookup_backoff_time: 0.001
设置和使用#
步骤 1:启动 LMCache 控制器
PYTHONHASHSEED=123 lmcache_controller --host localhost --port 9000 --monitor-ports '{"pull": 8300, "reply": 8400, "heartbeat": 8082}'
确保在配置文件中将 8300 和 8400 端口设置在 controller_pull_url 和 controller_reply_url 中。9000 端口是控制器主端口,可以任意更改。
启动控制器后,访问 WebUI:
步骤 2:使用 LMCache 工作线程启动 vLLM 引擎
如果网络接口卡支持 RDMA:
export UCX_TLS=rc
如果网络接口卡不支持 RDMA:
export UCX_TLS=tcp
在端口 8010 启动 vLLM 引擎 1:
PYTHONHASHSEED=123 CUDA_VISIBLE_DEVICES=0 LMCACHE_CONFIG_FILE=/path/to/example1.yaml \
vllm serve meta-llama/Meta-Llama-3.1-8B-Instruct \
--gpu-memory-utilization 0.8 \
--port 8010 \
--kv-transfer-config '{"kv_connector":"LMCacheConnectorV1", "kv_role":"kv_both"}'
在端口 8011 启动 vLLM 引擎 2:
PYTHONHASHSEED=123 CUDA_VISIBLE_DEVICES=1 LMCACHE_CONFIG_FILE=/path/to/example2.yaml \
vllm serve meta-llama/Meta-Llama-3.1-8B-Instruct \
--gpu-memory-utilization 0.8 \
--port 8011 \
--kv-transfer-config '{"kv_connector":"LMCacheConnectorV1", "kv_role":"kv_both"}'
步骤 3:测试 P2P 缓存共享
向 vLLM 引擎 1 发送请求以填充缓存:
curl -X POST http://localhost:8010/v1/completions \
-H "Content-Type: application/json" \
-d "{
\"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\",
\"prompt\": \"$(printf 'Explain the significance of KV cache in language models.%.0s' {1..100})\",
\"max_tokens\": 10
}"
将相同的请求发送到 vLLM 引擎 2,以演示从 引擎 1 中检索缓存:
curl -X POST http://localhost:8011/v1/completions \
-H "Content-Type: application/json" \
-d "{
\"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\",
\"prompt\": \"$(printf 'Explain the significance of KV cache in language models.%.0s' {1..100})\",
\"max_tokens\": 10
}"
预期输出#
当第二个请求成功从第一个实例检索缓存时,您应该看到类似于以下的日志:
(EngineCore_DP0 pid=2577584)[2025-09-21 00:00:11,706] LMCache INFO:[0m Established connection to peer_init_url localhost:8200. The peer_lookup_url: localhost:8201 (p2p_backend.py:278:lmcache.v1.storage_backend.p2p_backend)
(EngineCore_DP0 pid=2577584)[2025-09-21 00:00:11,792] LMCache INFO: Retrieved 1002 out of total 1002 out of total 1002 tokens. size: 0.1223 gb, cost 60.3595 ms, throughput: 2.0264 GB/s; (cache_engine.py:496:lmcache.v1.cache_engine)
这些日志表明成功建立了 P2P 连接并高吞吐量地检索了缓存。
步骤 4:基准测试 P2P 缓存共享
向实例 1 发送请求工作负载以填充缓存:
python benchmarks/long_doc_qa/long_doc_qa.py \
--model meta-llama/Meta-Llama-3.1-8B-Instruct \
--num-documents 50 \
--document-length 10000 \
--output-len 100 \
--repeat-count 1 \
--repeat-mode tile \
--port 8010 \
--max-inflight-requests 4
将相同的请求工作负载发送到实例 2,以演示从 实例 1 中检索缓存:
python benchmarks/long_doc_qa/long_doc_qa.py \
--model meta-llama/Meta-Llama-3.1-8B-Instruct \
--num-documents 50 \
--document-length 10000 \
--output-len 100 \
--repeat-count 1 \
--repeat-mode tile \
--port 8011 \
--max-inflight-requests 4
基准测试结果#
第一次实例指标:
Warmup round mean TTFT: 2.286s
Warmup round time: 37.957s
Warmup round prompt count: 50
Warmup round successful prompt count: 50
=== BENCHMARK RESULTS ===
Query round mean TTFT: 2.028s
Query round time: 38.323s
Query round prompt count: 50
Query round successful prompt count: 50
第二个实例指标:
Warmup round mean TTFT: 1.036s
Warmup round time: 13.814s
Warmup round prompt count: 50
Warmup round successful prompt count: 50
=== BENCHMARK RESULTS ===
Query round mean TTFT: 0.490s
Query round time: 7.964s
Query round prompt count: 50
Query round successful prompt count: 50
在这个例子中,long_doc_qa 中的预热轮次指标被使用,因为在一个实例内没有重用现有的 KV Cache 以仅从 P2P 共享中受益。启用 LMCache P2P 共享后,首次令牌时间 (TTFT) 减少了 54.7%,从 2.286 秒降至 1.036 秒,总推理时间减少了 63.6%(37.957 秒 → 13.814 秒)。