Redis#
概述#
Redis 是一个内存中的键值存储,是 LMCache 中支持的远程 KV Cache 卸载选项之一。其他一些远程后端包括 Mooncake、Valkey 和 InfiniStore。本指南将主要集中于单节点 Redis,但也会向您展示如何设置 Redis Sentinel 和 LMCache 服务器。
配置 LMCache Redis 卸载的两种方式:#
1. 环境变量:
# 256 Tokens per KV Chunk
export LMCACHE_CHUNK_SIZE=256
# Redis host
export LMCACHE_REMOTE_URL="redis://your-redis-host:6379"
# Redis Sentinel hosts (for high availability)
# export LMCACHE_REMOTE_URL="redis-sentinel://localhost:26379,localhost:26380,localhost:26381"
# LMCache Server host
# export LMCACHE_REMOTE_URL="lm://localhost:65432"
# How to serialize and deserialize KV cache on remote transmission
export LMCACHE_REMOTE_SERDE="naive" # "naive" (default) or "cachegen"
2. 配置文件:
通过 LMCACHE_CONFIG_FILE=your-lmcache-config.yaml 传入
示例 config.yaml:
# 256 Tokens per KV Chunk
chunk_size: 256
# Redis host
remote_url: "redis://your-redis-host:6379"
# Redis Sentinel hosts (for high availability)
# remote_url: "redis-sentinel://localhost:26379,localhost:26380,localhost:26381"
# LMCache Server host
# remote_url: "lm://localhost:65432"
# How to serialize and deserialize KV cache on remote transmission
remote_serde: "naive" # "naive" (default) or "cachegen"
远程存储说明:#
LMCache 的后端遵循自然内存层次结构,优先考虑 CPU RAM 卸载,然后是本地存储卸载,最后是远程卸载。
为了让 LMCache 知道如何创建连接器以连接远程后端,您必须在 remote_url 中指定一个连接器类型,后面跟着一个或多个主机:端口对(具体取决于使用的连接器类型)。如果 remote_url 设置为 None,LMCache 将不会使用任何远程存储。
remote_url 的示例:
remote_url: "redis://your-redis-host:6379"
remote_url: "redis-sentinel://localhost:26379,localhost:26380,localhost:26381"
remote_url: "lm://localhost:65432"
remote_url: "infinistore://127.0.0.1:12345"
remote_url: "mooncakestore://127.0.0.1:50051"
远程存储示例#
前提条件:
一台至少配备一个 GPU 的机器。您可以根据您的显存调整 vllm 实例的最大模型长度。
安装了 vllm 和 LMCache (安装指南)
Hugging Face 访问
meta-llama/Meta-Llama-3.1-8B-Instruct
export HF_TOKEN=your_hugging_face_token
第 0 步. 为此示例设置一个目录:
mkdir lmcache-redis-offload-example
cd lmcache-redis-offload-example
步骤 1. 启动 Redis 服务器:
# Ubuntu / Debian Installation
sudo apt-get install redis
redis-server # starts the server on default port 6379
检查 Redis 是否正在运行:
redis-cli ping
预期响应:
PONG
可选:设置哨兵:
要通过 Redis 实现高可用性,您可以配置 Redis 哨兵来监控主节点,并在需要时自动切换到副本。
步骤 1a. 启动 Redis 副本:
redis-server --port 6380 --replicaof 127.0.0.1 6379
步骤 1b. 创建 Sentinel 配置文件:
创建三个文件:sentinel-26379.conf、sentinel-26380.conf 和 sentinel-26381.conf,内容如下:
port 26379 # Use 26380 and 26381 in other files respectively
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
步骤 1c. 启动每个 Sentinel:
redis-server sentinel-26379.conf --sentinel
redis-server sentinel-26380.conf --sentinel
redis-server sentinel-26381.conf --sentinel
步骤 1d. 确保哨兵正在跟踪主节点:
redis-cli -p 26379 sentinel master mymaster
redis-cli -p 26380 sentinel master mymaster
redis-cli -p 26381 sentinel master mymaster
步骤 1e. 验证一切是否正常运行:
ps aux | grep redis
您应该看到类似这样的内容(不包括注释):
# Master (read-write)
user 60816 0.1 0.0 69804 11132 ? Sl 04:11 0:00 redis-server *:6379
# Replica (read-only mirror of 6379)
user 60903 0.1 0.0 80048 10928 ? Sl 04:12 0:00 redis-server *:6380
# Sentinels (monitor the master and hold quorums to decide when to failover)
user 61301 0.1 0.0 67244 10944 ? Sl 04:14 0:00 redis-server *:26379 [sentinel]
user 61382 0.1 0.0 67244 10944 ? Sl 04:14 0:00 redis-server *:26380 [sentinel]
user 61462 0.1 0.0 67244 10944 ? Sl 04:15 0:00 redis-server *:26381 [sentinel]
替代方案:启动 LMCache 服务器:
lmcache_server CLI 入口点启动一个远程 LMCache 服务器,并附带 lmcache 包。
lmcache_server <host> <port> <device>
lmcache_server localhost 65432
目前,唯一支持的设备是“cpu”(这是默认值,因此您无需指定它)。
步骤 2. 启动一个启用远程卸载的 vLLM 服务器:
创建一个名为 redis-offload.yaml 的 lmcache 配置文件。
# disabling CPU RAM offload not recommended (on by default) but
# if you want to confirm that the remote backend works by itself
# local_cpu: false
chunk_size: 256
remote_url: "redis://localhost:6379"
remote_serde: "naive"
如果您不想使用配置文件,请取消注释前面三个环境变量,然后注释掉下面的 LMCACHE_CONFIG_FILE:
# disabling CPU RAM offload not recommended (on by default) but
# if you want to confirm that the remote backend works by itself
# LMCACHE_LOCAL_CPU=False \
# LMCACHE_CHUNK_SIZE=256 \
# LMCACHE_REMOTE_URL="redis://localhost:6379" \
# LMCACHE_REMOTE_SERDE="naive"
LMCACHE_CONFIG_FILE="redis-offload.yaml" \
vllm serve \
meta-llama/Llama-3.1-8B-Instruct \
--max-model-len 16384 \
--kv-transfer-config \
'{"kv_connector":"LMCacheConnectorV1", "kv_role":"kv_both"}'
可选:哨兵
创建一个名为 redis-sentinel-offload.yaml 的 lmcache 配置文件
chunk_size: 256
remote_url: "redis-sentinel://localhost:26379,localhost:26380,localhost:26381"
remote_serde: "naive"
如果您不想使用配置文件,请取消注释前面三个环境变量,然后注释掉下面的 LMCACHE_CONFIG_FILE:
# LMCACHE_CHUNK_SIZE=256 \
# LMCACHE_REMOTE_URL="redis-sentinel://localhost:26379,localhost:26380,localhost:26381" \
# LMCACHE_REMOTE_SERDE="naive"
LMCACHE_CONFIG_FILE="redis-sentinel-offload.yaml" \
vllm serve \
meta-llama/Llama-3.1-8B-Instruct \
--max-model-len 16384 \
--kv-transfer-config \
'{"kv_connector":"LMCacheConnectorV1", "kv_role":"kv_both"}'
替代方案:LMCache 服务器
创建一个名为 lmcache-server-offload.yaml 的 lmcache 配置文件
chunk_size: 256
remote_url: "lm://localhost:65432"
remote_serde: "naive"
如果您不想使用配置文件,请取消注释前面三个环境变量,然后注释掉下面的 LMCACHE_CONFIG_FILE:
# LMCACHE_CHUNK_SIZE=256 \
# LMCACHE_REMOTE_URL="lm://localhost:65432" \
# LMCACHE_REMOTE_SERDE="naive"
LMCACHE_CONFIG_FILE="lmcache-server-offload.yaml" \
vllm serve \
meta-llama/Llama-3.1-8B-Instruct \
--max-model-len 16384 \
--kv-transfer-config \
'{"kv_connector":"LMCacheConnectorV1", "kv_role":"kv_both"}'
步骤 3. 在 Redis 中查看和管理 LMCache 条目:
如果您想体验通过卸载和 KV Cache 重用来加速 TTFT 的速度,可以随意使用与 CPU RAM 和 Local Storage 中相同的 query-twice.py 脚本和 man-bash.txt 长上下文。
在这里,我们将演示如何在 Redis 中查找和修改 LMCache KV 块条目。
请注意,官方的 LMCache 方法来实现此 Redis 特定功能以查看和修改 LMCache KV 块可在 LMCache Controller 中找到。
这次让我们先用 curl 预热/填充 LMCache:
curl -X 'POST' \
'http://127.0.0.1:8000/v1/chat/completions' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"model": "meta-llama/Llama-3.1-8B-Instruct",
"messages": [
{"role": "system", "content": "You are a helpful AI coding assistant."},
{"role": "user", "content": "Write a segment tree implementation in python"}
],
"max_tokens": 150
}'
LMCache 使用结构化键格式在 Redis 中存储数据。每个键包含以下以分隔格式的信息:
model_name@world_size@worker_id@chunk_hash
model_name: 语言模型的名称
world_size: 分布式部署中工作节点的总数
worker_id: 创建此缓存条目的工作线程 ID,范围为 [0, world_size - 1]
chunk_hash: 令牌块的哈希值(基于 SHA-256)
例如,一个典型的键可能看起来像:
vllm@meta-llama/Llama-3.1-8B-Instruct@1@0@a1b2c3d4e5f6...
使用 redis-cli 查看 LMCache 数据
要检查和管理 Redis 中的 LMCache 条目:
redis-cli -h localhost -p 6379
可选:如果您使用的是哨兵,首先找到主端口:
redis-cli -p 26379 sentinel get-master-addr-by-name mymaster
redis-cli -h localhost -p <master-port>
列出 LMCache 键:
注意(从键的后缀中)每个 LMCache KV 块有两个条目:kv_bytes 和 metadata
# Show all keys
localhost:6379> KEYS *
1) "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
2) "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]"
# Show keys for a specific model
localhost:6379> KEYS *Llama-3.1-8B-Instruct*
1) "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
2) "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]"
删除 LMCache 条目:
localhost:6379> DEL *
删除特定的 LMCache 条目:
localhost:6379> DEL "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
localhost:6379> KEYS *
1) "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]"
检查键是否存在:
localhost:6379> EXISTS "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
查看键的内存使用情况:
请注意,kv_bytes 条目正是持有 KV 块的内容,并且比 metadata 条目大得多。
localhost:6379> MEMORY USAGE "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]"
(integer) 198
localhost:6379> MEMORY USAGE "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
(integer) 7340200
删除特定键:
# Delete a single key
localhost:6379> DEL "vllm@meta-llama/Llama-3.1-8B-Instruct@1@[email protected]_bytes"
# Delete all keys matching a pattern
redis-cli -h localhost -p 6379 --scan --pattern "vllm@meta-llama/Llama-3.1-8B-Instruct*" \
| xargs redis-cli -h localhost -p 6379 DEL
实时监控 Redis:
localhost:6379> MONITOR
获取 LMCache 的 Redis 统计信息:
# Get memory stats
localhost:6379> INFO memory
# Get statistics about operations
localhost:6379> INFO stats
本教程使用 redis-cli 直接查看远程后端并操作 KV 块。
请再次参考 LMCache Controller 以获取在您的 LMCache 实例中控制和路由 KV Cache 的官方 LMCache 方法。
步骤 4. 清理:
redis-cli shutdown
# Optional:
# Shut down the Redis replica (if started)
redis-cli -p 6380 shutdown
# Shut down all Redis Sentinels (if started)
redis-cli -p 26379 shutdown
redis-cli -p 26380 shutdown
redis-cli -p 26381 shutdown
# (Optional) Remove temporary files or configs
rm -f sentinel-26379.conf sentinel-26380.conf sentinel-26381.conf
# Confirm no Redis processes are still running
ps aux | grep redis