休眠模式指南#
概述#
休眠模式 (Sleep Mode) 是一种 API,旨在将模型权重卸载并从 NPU 内存中丢弃 KV 缓存。此功能对于强化学习 (RL) 的训练后 (post-training) 工作负载至关重要,尤其是在 PPO、GRPO 或 DPO 等在线算法中。在训练过程中,策略模型通常会使用 vLLM 等推理引擎进行自回归生成,然后进行优化所需的前向和后向计算。
由于生成和训练阶段可能采用不同的模型并行策略,因此在训练期间释放 vLLM 中存储的 KV 缓存,甚至卸载模型参数变得至关重要。这确保了内存的高效利用,并避免了 NPU 上的资源争用。
入门#
当启用 enable_sleep_mode=True 时,vllm 中内存的管理方式(malloc, free)将遵循特定的内存池。在模型加载和 KV 缓存初始化期间,我们会将内存标记为一个映射:{"weight": data, "kv_cache": data}。
引擎(v0/v1)支持两种休眠级别来管理空闲期间的内存。
级别 1 休眠
操作:卸载模型权重并丢弃 KV 缓存。
内存:模型权重移动到 CPU 内存;KV 缓存被丢弃。
用例:适用于稍后重用同一模型的情况。
注意:确保有足够的 CPU 内存来容纳模型权重。
级别 2 休眠
操作:丢弃模型权重和 KV 缓存。
内存:模型权重和 KV 缓存的内容均被丢弃。
用例:适用于切换到不同模型或更新当前模型的情况。
由于此功能使用了低级别 API AscendCL,因此在使用休眠模式时,您应遵循 安装指南 并从源代码进行构建。如果您使用的是 v0.12.0rc1 及更早版本,请记住设置 export COMPILE_CUSTOM_KERNELS=1。
用法#
以下是使用休眠模式的简单示例。
离线推理
import os import torch from vllm import LLM, SamplingParams from vllm.utils.mem_constants import GiB_bytes os.environ["VLLM_USE_MODELSCOPE"] = "True" os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = "spawn" os.environ["VLLM_ASCEND_ENABLE_NZ"] = "0" if __name__ == "__main__": prompt = "How are you?" free, total = torch.npu.mem_get_info() print(f"Free memory before sleep: {free / 1024 ** 3:.2f} GiB") # record npu memory use baseline in case other process is running used_bytes_baseline = total - free llm = LLM("Qwen/Qwen2.5-0.5B-Instruct", enable_sleep_mode=True) sampling_params = SamplingParams(temperature=0, max_tokens=10) output = llm.generate(prompt, sampling_params) llm.sleep(level=1) free_npu_bytes_after_sleep, total = torch.npu.mem_get_info() print(f"Free memory after sleep: {free_npu_bytes_after_sleep / 1024 ** 3:.2f} GiB") used_bytes = total - free_npu_bytes_after_sleep - used_bytes_baseline # now the memory usage should be less than the model weights # (0.5B model, 1GiB weights) assert used_bytes < 1 * GiB_bytes llm.wake_up() output2 = llm.generate(prompt, sampling_params) # cmp output assert output[0].outputs[0].text == output2[0].outputs[0].text
在线服务
注意
考虑到可能存在恶意访问的风险,请确保您处于开发者模式 (dev-mode) 下,并显式指定开发环境
VLLM_SERVER_DEV_MODE以暴露这些端点(休眠/唤醒)。export VLLM_SERVER_DEV_MODE="1" export VLLM_WORKER_MULTIPROC_METHOD="spawn" export VLLM_USE_MODELSCOPE="True" export VLLM_ASCEND_ENABLE_NZ="0" vllm serve Qwen/Qwen2.5-0.5B-Instruct --enable-sleep-mode # after serving is up, post to these endpoints # sleep level 1 curl -X POST http://127.0.0.1:8000/sleep \ -H "Content-Type: application/json" \ -d '{"level": "1"}' curl -X GET http://127.0.0.1:8000/is_sleeping # sleep level 2 curl -X POST http://127.0.0.1:8000/sleep \ -H "Content-Type: application/json" \ -d '{"level": "2"}' # wake up curl -X POST http://127.0.0.1:8000/wake_up # wake up with tag, tags must be in ["weights", "kv_cache"] curl -X POST "http://127.0.0.1:8000/wake_up?tags=weights" curl -X GET http://127.0.0.1:8000/is_sleeping # after sleep and wake up, the serving is still available curl https://:8000/v1/completions \ -H "Content-Type: application/json" \ -d '{ "model": "Qwen/Qwen2.5-0.5B-Instruct", "prompt": "The future of AI is", "max_tokens": 7, "temperature": 0 }'