Skip to content

vllm.v1.worker.workspace

Classes:

Functions:

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 key in the current ubatch and lane.

  • get_retained –

    Get a scratch view at the end of the workspace that can outlive requests.

  • 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.

_ensure_workspace_size(required_bytes)

Ensure workspace is allocated and large enough, return current workspace.

Parameters:

  • required_bytes

    (int) –

    The number of bytes required.

Returns:

  • Tensor –

    The current workspace tensor.

_workspace_size_bytes(workspace) staticmethod

Get size of workspace in bytes.

get_persistent(key, shape, dtype, *, zero_init=False)

Get a fixed-shape tensor, optionally zeroed on first allocation only.

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 once the manager is locked. 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.

get_retained(key, shape, dtype)

Get a scratch view at the end of the workspace that can outlive requests.

get_simultaneous hands out views from the start of the workspace, so requests smaller than the workspace leave its end alone. This view spans the last bytes of the workspace, and the returned flag says whether they still hold what the previous get_retained left there: that call used the same key and size, no get_simultaneous request since reached into the span, and the workspace was not reallocated in between. What the bytes mean is the caller's to track; only the latest key is remembered.

Parameters:

  • key

    (Hashable) –

    Identifies the caller; another key's call takes the span over.

  • shape

    (tuple[int, ...]) –

    Shape of the view.

  • dtype

    (dtype) –

    Dtype of the view.

Returns:

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:

  • list[Tensor] –

    List of tensor views into the workspace buffer, one per shape/dtype pair.

is_locked()

Check if workspace 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.

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.

current_workspace_manager()

Get the current workspace manager instance.

Raises:

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.

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

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.

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.

use_workspace_lane(lane)

Select an independent workspace owner for this execution context.