main
md 158 lines 7.72 KB
Rendered Raw
1 # Sleep Mode
2
3 vLLM's Sleep Mode allows you to temporarily release most GPU memory used by a model, including model weights and KV cache, without stopping the server or unloading the Docker container. This is especially useful for RLHF, training, or cost-saving scenarios where GPU resources need to be freed between inference workloads.
4
5 Key benefits:
6
7 - **Frees GPU memory**: Offloads model weights to CPU RAM and discards KV cache, releasing up to 90%+ of GPU memory for other tasks.
8 - **Fast resume**: Quickly wake up the engine and resume inference without full model reload.
9 - **API endpoints**: Control sleep/wake_up state via HTTP endpoints or Python API.
10 - **Supports distributed workloads**: Works with tensor parallelism, pipeline parallelism, etc.
11 - **Fine-grained control**: Optionally wake up only model weights or KV cache to avoid OOM during weight updates.
12
13 !!! note
14 This feature is now supported on CUDA and ROCm platform.
15
16 !!! note
17 For more information, see this [Blog Post](https://blog.vllm.ai/2025/10/26/sleep-mode.html).
18
19 ## Sleep levels
20
21 Level 1 sleep will offload the model weights and discard the KV cache. The content of KV cache is forgotten. Level 1 sleep is good for sleeping and waking up the engine to run the same model again. The model weights are backed up in CPU memory. Please make sure there's enough CPU memory to store the model weights. Level 2 sleep will discard both the model weights and the KV cache (while the model's buffers are kept in CPU, like rope scaling tensors). The content of both the model weights and KV cache is forgotten. Level 2 sleep is good for sleeping and waking up the engine to run a different model or update the model, where previous model weights are not needed, e.g. RLHF weight update. Level 2 sleep is also useful when there is not enough CPU memory to back up the model weights, e.g. when a colocated trainer already uses CPU memory for offloading its own state; since nothing is backed up, restore the weights after waking up with `collective_rpc("reload_weights")`.
22
23 ## Usage
24
25 ### Offline inference
26
27 Enable sleep mode by passing `enable_sleep_mode=True` to the `LLM` class.
28
29 ```python
30 from vllm import LLM
31 llm = LLM("Qwen/Qwen3-0.6B", enable_sleep_mode=True)
32 ```
33
34 #### Python API
35
36 ```python
37 # Sleep level 1
38 # Put the engine to sleep (level=1: offload weights to CPU RAM, discard KV cache)
39 llm.sleep(level=1)
40
41 # Wake up the engine (restore weights)
42 llm.wake_up()
43 ```
44
45 ```python
46 # Sleep level 2
47 # Put the engine to sleep (level=2: discard both weights and KV cache)
48 llm.sleep(level=2)
49
50 # Reallocate weights memory only
51 llm.wake_up(tags=["weights"])
52
53 # Load weights in-place
54 llm.collective_rpc("reload_weights")
55
56 # Reallocate KV cache
57 llm.wake_up(tags=["kv_cache"])
58 ```
59
60 #### RLHF weight updates
61
62 During RLHF training, vLLM allows you to selectively wake up only the model weights or the KV cache using the tags argument in wake_up(). This fine-grained control is especially useful when updating model weights: by waking up just the weights (e.g., llm.wake_up(tags=["weights"])), you avoid allocating memory for the KV cache until after the weight update is complete. This approach helps prevent GPU out-of-memory (OOM) errors, particularly with large models, by minimizing peak memory usage during weight synchronization and update operations.
63
64 Use `tags=["weights"]` or `tags=["kv_cache"]` to control which resources are restored, useful for RLHF and weight updates. **Note** that `is_sleeping` will report `true` until all components are awake.
65
66 ```python
67 # Put engine to deep sleep (level=2)
68 llm.sleep(level=2)
69 # ... Get the new weights
70 # Wake up only weights to avoid OOM
71 llm.wake_up(tags=["weights"])
72 # ... Update the weights
73 # wake up KV cache after weights are updated
74 llm.wake_up(tags=["kv_cache"])
75 ```
76
77 #### Retaining frozen weights during RLHF updates
78
79 Set `sleep_preserve_parameter_names` (CLI: `--sleep-preserve-parameter-names`)
80 to runtime parameter-name glob
81 patterns for weights that stay frozen during training. Each pattern must match
82 `model.named_parameters()`; checkpoint names and `requires_grad` are not used.
83
84 Level-2 sleep backs up selected GPU parameters to pageable CPU memory; weights
85 wake-up restores them in place and releases the backups. CPU parameters need no
86 copy. Allow enough host memory for backups. Level-1 behavior is unchanged.
87
88 The trainer must omit these parameters from updates, and reload/post-processing
89 must preserve their values and storage (including avoiding replacement with meta
90 tensors). This option does not filter updates and applies only to the target model.
91
92 #### Release only KV cache memory
93
94 `LLM.release_kv_cache_memory()` discards KV cache physical memory while keeping model weights resident. It requires a completed pause and all executor memory to be resident: full sleep, partial wake-up, and repeated release without restoring memory are rejected. Requests retained with `mode="keep"` are recomputed after wake-up.
95
96 Use the default `cumem` backend with managed allocations: `enable_cumem_allocator=True` on CUDA/ROCm (also enabled by `enable_sleep_mode=True`), or `enable_sleep_mode=True` on XPU. Other backends must implement selective discard; CPU and unmanaged allocations are unsupported.
97
98 ```python
99 llm.sleep(level=0, mode="keep") # Wait for the pause to complete.
100 llm.release_kv_cache_memory()
101 llm.wake_up(tags=["kv_cache"]) # Reallocate KV cache and resume scheduling.
102 ```
103
104 ### Online Serving
105
106 To enable sleep mode in a vLLM server you need to initialize it with the flag `VLLM_SERVER_DEV_MODE=1` and pass `--enable-sleep-mode` to the vLLM server.
107
108 #### Server in development mode
109
110 When using the flag `VLLM_SERVER_DEV_MODE=1` you enable development endpoints, and these endpoints should not be exposed to users.
111
112 ```bash
113 VLLM_SERVER_DEV_MODE=1 vllm serve Qwen/Qwen3-0.6B \
114 --enable-sleep-mode \
115 --port 8000
116 ```
117
118 Below is an example of how to sleep and wake up a model in level 1.
119
120 ```bash
121 curl -X POST 'http://localhost:8000/sleep?level=1'
122 curl -X POST 'http://localhost:8000/wake_up'
123 ```
124
125 And this is an example of how to sleep and wake up a model in level 2.
126
127 ```bash
128 curl -X POST 'http://localhost:8000/sleep?level=2'
129 # Reallocate weights memory only
130 curl -X POST 'http://localhost:8000/wake_up?tags=weights'
131 # Load weights in-place
132 curl -X POST 'http://localhost:8000/collective_rpc' -H 'Content-Type: application/json' -d '{"method":"reload_weights"}'
133 # Reallocate KV cache
134 curl -X POST 'http://localhost:8000/wake_up?tags=kv_cache'
135 ```
136
137 To release only KV cache memory, wait for the level 0 pause to complete before calling the release endpoint. The same resident-memory and backend requirements as the Python API apply.
138
139 ```bash
140 curl -X POST 'http://localhost:8000/sleep?level=0&mode=keep'
141 curl -X POST 'http://localhost:8000/release_kv_cache_memory'
142 curl -X POST 'http://localhost:8000/wake_up?tags=kv_cache'
143 ```
144
145 #### HTTP endpoints
146
147 - `POST /sleep?level=1` — Put the model to sleep (`level=1`).
148 - `POST /release_kv_cache_memory` — Discard KV cache memory after a completed pause, while all executor memory is resident.
149 - `POST /wake_up` — Wake up the model. Supports optional `tags` query parameters for partial wake-up (e.g., `?tags=weights`).
150 - `POST /collective_rpc` — Perform a collective remote procedure call (RPC).
151 - `GET /is_sleeping` — Check if the model is sleeping.
152
153 !!! note
154 These endpoints are only available when passing `VLLM_SERVER_DEV_MODE=1`.
155
156 ## Limitation
157
158 On ROCm, the virtual memory allocation on ROCm is done through chunked memory allocation. You can control the chunk size through `VLLM_ROCM_SLEEP_MEM_CHUNK_SIZE` (in MB). The default value is set at 256MB. The larger the chunk size the faster the performance. However, setting it too large will cause OOM. So if you encounter OOM when using sleep mode. Try reducing the chunk size. It is recommended to define the chunk size as a power of 2.