vllm.v1.worker.workspace
¶
Classes:
-
WorkspaceManager–Manager for workspace allocation.
Functions:
-
current_workspace_manager–Get the current workspace manager instance.
-
init_workspace_manager–Initialize the workspace manager with a device.
-
is_workspace_manager_initialized–Check if workspace manager has been initialized.
-
lock_workspace–Lock the workspace to prevent growth or new persistent resources.
-
reset_workspace_manager–Reset the workspace manager to uninitialized state.
-
unlock_workspace–Unlock the workspace to allow growth.
-
use_workspace_lane–Select an independent workspace owner for this execution context.
WorkspaceManager
¶
Manager for workspace allocation.
Manages shared scratch and keyed persistent resources per (ubatch, lane).
Can be locked to prevent growth or new resources during execution.
Methods:
-
get_persistent–Get a fixed-shape tensor, optionally zeroed on first allocation only.
-
get_persistent_resource–Return the resource cached for
keyin the current ubatch and lane. -
get_simultaneous–Get multiple workspace tensors simultaneously from a single allocation.
-
is_locked–Check if workspace is locked.
-
lock–Lock the workspace to prevent growth or new persistent resources.
-
unlock–Unlock the workspace to allow growth.
Source code in vllm/v1/worker/workspace.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
_ensure_workspace_size(required_bytes)
¶
Ensure workspace is allocated and large enough, return current workspace.
Parameters:
Returns:
-
Tensor–The current workspace tensor.
Source code in vllm/v1/worker/workspace.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
_workspace_size_bytes(workspace)
staticmethod
¶
get_persistent(key, shape, dtype, *, zero_init=False)
¶
Get a fixed-shape tensor, optionally zeroed on first allocation only.
Source code in vllm/v1/worker/workspace.py
get_persistent_resource(key, factory)
¶
Return the resource cached for key in the current ubatch and lane.
The first request calls factory and stores its result for the lifetime
of this manager. Later requests return the same object without calling
the factory again. Each ubatch/lane pair has a separate cache. Once the
manager is locked, requesting a key missing from that cache raises an
AssertionError.
Keys must identify the resource type and configuration. Callers must not resize or replace tensor storage held by a cached resource. Uses of the same resource must not overlap; include the CUDA stream in the key when streams can execute concurrently.
With torch.compile, call this inside a runtime custom op so resource selection happens during execution.
Source code in vllm/v1/worker/workspace.py
get_simultaneous(*shapes_and_dtypes)
¶
Get multiple workspace tensors simultaneously from a single allocation.
Parameters:
-
(*shapes_and_dtypes¶tuple[tuple[int, ...], dtype], default:()) –One or more (shape, dtype) tuples.
Returns:
Source code in vllm/v1/worker/workspace.py
is_locked()
¶
lock()
¶
Lock the workspace to prevent growth or new persistent resources.
Larger scratch requests and new persistent resources raise an assertion error. Existing resources retain their storage throughout execution.
Source code in vllm/v1/worker/workspace.py
unlock()
¶
Unlock the workspace to allow growth.
This is used during elastic EP scaling when the workspace size needs to grow due to changes in the number of experts.
Source code in vllm/v1/worker/workspace.py
current_workspace_manager()
¶
Get the current workspace manager instance.
Raises:
-
AssertionError–If workspace manager has not been initialized.
Source code in vllm/v1/worker/workspace.py
init_workspace_manager(device, num_ubatches=None, num_lanes=1, alloc_context=nullcontext)
¶
Initialize the workspace manager with a device.
Must be called before using any workspace functions. Typically called from GPUModelRunner.init.
Parameters:
-
(device¶device) –The device to allocate workspace on.
-
(num_ubatches¶int | None, default:None) –Number of workspace ubatch slots. Defaults to 1.
-
(num_lanes¶int, default:1) –Number of independent execution lanes per ubatch. Defaults to 1.
-
(alloc_context¶Callable[[], AbstractContextManager], default:nullcontext) –Factory for the context scratch is allocated in.
Source code in vllm/v1/worker/workspace.py
is_workspace_manager_initialized()
¶
Check if workspace manager has been initialized.
Returns:
-
bool–True if workspace manager is initialized, False otherwise.
lock_workspace()
¶
Lock the workspace to prevent growth or new persistent resources.
Larger scratch requests and new persistent resources raise an AssertionError. All required resources must be initialized in each execution slot during warmup, so their storage remains fixed during execution.
Example
During initialization¶
init_workspace_manager(device) reserve_workspace(shape1, dtype1) reserve_workspace(shape2, dtype2)
Lock after warmup/profiling¶
lock_workspace()
Now all get_workspace calls must fit in pre-allocated size¶
Source code in vllm/v1/worker/workspace.py
reset_workspace_manager()
¶
Reset the workspace manager to uninitialized state.
This is primarily intended for testing purposes to allow tests to reinitialize the workspace manager cleanly.
Source code in vllm/v1/worker/workspace.py
unlock_workspace()
¶
Unlock the workspace to allow growth.
This is used during elastic EP scaling when the workspace size needs to grow due to changes in the number of experts. After scaling operations complete, lock_workspace() should be called again to prevent unexpected allocations.
Source code in vllm/v1/worker/workspace.py
use_workspace_lane(lane)
¶
Select an independent workspace owner for this execution context.