睡眠模式

睡眠模式#

概述#

Sleep Mode 是一个旨在从 NPU 内存中卸载模型权重并丢弃 KV cache 的 API。此功能对于强化学习(RL)后训练(Post-training)任务至关重要,特别是在 PPO、GRPO 或 DPO 等在线算法中。在训练过程中,策略模型通常先使用 vLLM 等推理引擎进行自回归生成,随后进行前向和反向传播以进行优化。

由于生成和训练阶段可能采用不同的模型并行策略,因此在训练期间释放 KV cache 甚至卸载存储在 vLLM 中的模型参数变得至关重要。这可以确保内存的高效利用,并避免 NPU 上的资源竞争。

快速上手#

当设置 enable_sleep_mode=True 时,vLLM 管理内存(malloc, free)的方式将在特定的内存池下进行。在模型加载和 KV cache 初始化期间,我们将内存标记为一个映射(Map):{"weight": data, "kv_cache": data}

引擎(v0/v1)支持两种睡眠等级,以便在空闲期间管理内存:

  • 一级睡眠 (Level 1 Sleep)

    • 操作:卸载模型权重并丢弃 KV cache。

    • 内存:模型权重被移动到 CPU 内存;KV cache 被清除。

    • 用例:适用于稍后需要重复使用同一模型的情况。

    • 注意:请确保有足够的 CPU 内存来存放模型权重。

  • 二级睡眠 (Level 2 Sleep)

    • 操作:同时丢弃模型权重和 KV cache。

    • 内存:模型权重和 KV cache 的内容都会被清除。

    • 用例:在切换到不同模型或更新当前模型时最为理想。

由于此功能使用了底层 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,以暴露这些接口端点(sleep/wake up)。

    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 http://localhost: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
        }'