睡眠模式指南

睡眠模式指南#

概述#

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

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

快速入门#

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

引擎(v0/v1)支持两个睡眠级别来管理空闲期间的内存:

  • 一级睡眠

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

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

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

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

  • 二级睡眠

    • 操作:同时丢弃模型权重和 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_completion_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
    
  • 在线服务:

    备注

    考虑到可能存在恶意访问的风险,请确保您处于开发模式,并显式指定开发环境变量 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_completion_tokens": 7,
            "temperature": 0
        }'