-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
[offloader] v2: Hide weight onloading latency via prefetching #29941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 24 commits
a3dfa47
9bf4097
152af73
67cd6cc
c21063e
f43b577
975e972
0ed3570
b07eb3f
ac5fb49
80e764e
9436a66
21e0813
5bc88d8
719af1b
91c180e
20db2a1
2debd78
20ef9d8
f536bc4
021acc1
0525c42
6506706
55a9934
8478e55
4520428
fcaa9da
79ec7d5
5fe6d7c
08bab8a
0d18ec2
e5d375e
1d3d404
3c3ba9b
96301e3
6c6600d
ff52cd4
2a77385
20696cd
f537f21
18b576e
3df4d98
150dc9f
9f49711
0478a6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Test V2 offloading correctness with DeepSeek V2 model.""" | ||
|
|
||
| from ..utils import compare_two_settings | ||
|
|
||
|
|
||
| def test_v2_offload_deepseek(): | ||
| """Test V2 CPU offloading with DeepSeek-V2-Lite. | ||
|
|
||
| Compares outputs between: | ||
| 1. Baseline (no offloading) | ||
| 2. V2 offloading (group_size=8, num_in_group=2, prefetch_step=1) | ||
|
|
||
| This tests the advanced offloading with prefetching on a MoE model. | ||
| """ | ||
| compare_two_settings( | ||
| "deepseek-ai/DeepSeek-V2-Lite", | ||
| [ | ||
| # V2 offloading configuration | ||
| "--offload-group-size", | ||
| "8", | ||
| "--offload-num-in-group", | ||
| "2", | ||
| "--offload-prefetch-step", | ||
| "1", | ||
| # torch.compile is automatically disabled when V2 offloading is | ||
| # enabled (via enable_if in @support_torch_compile decorator) | ||
| ], | ||
| [], # Baseline: no offloading | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| from vllm.distributed.device_communicators.pynccl_allocator import set_graph_pool_id | ||
| from vllm.forward_context import BatchDescriptor, get_forward_context | ||
| from vllm.logger import init_logger | ||
| from vllm.model_executor.offloader.base import get_offloader | ||
| from vllm.platforms import current_platform | ||
| from vllm.utils.torch_utils import current_stream, weak_ref_tensors | ||
|
|
||
|
|
@@ -265,6 +266,11 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any | None: | |
| set_graph_pool_id(self.graph_pool) | ||
| else: | ||
| set_graph_pool_id(current_platform.graph_pool_handle()) | ||
|
|
||
| # Sync offloader's copy stream before capture. | ||
| # Ensure any pre-capture prefetches from offloader are complete. | ||
| get_offloader().sync_prev_onload() | ||
|
Comment on lines
+270
to
+272
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eellison and/or @BoyuanFeng, could you take a look at this too please? These look reasonable to me but I'm new to this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to validate: the same sync, occurs at runtime, prior to graph replay ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I updated the comment in The
|
||
|
|
||
| # mind-exploding: carefully manage the reference and memory. | ||
| with torch.cuda.graph( | ||
| cudagraph, | ||
|
|
@@ -273,6 +279,11 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any | None: | |
| ): | ||
| # `output` is managed by pytorch's cudagraph pool | ||
| output = self.runnable(*args, **kwargs) | ||
| # Join offloader's copy stream after forward to avoid | ||
| # unjoined stream error. The last layer's start_prefetch | ||
| # forks copy_stream, but wait_prefetch only happens in | ||
| # the next forward pass. | ||
| get_offloader().join_after_forward() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is the CUDAGraph requires side stream with torch.cuda.graph(g):
copy_stream.wait_stream(torch.cuda.current_stream()) # branch the copy_stream from the current_stream
with torch.cuda.stream(copy_stream):
x_cpu.copy_(x_cuda, non_blocking=True)
# any computation
out = y_cuda + 1
torch.cuda.current_stream().wait_stream(copy_stream) # join the copy_stream
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi Boyuan, it's in
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks. for future-proof, could we expose an api to call it in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's asymmetric with the current implementation: events are recorded at per-layer level, and then waited on by the later layers that need the weights. But unfortunately, CUDA graph require events to be joined before capture ends, and this is why we need |
||
| if self.cudagraph_options.weak_ref_output: | ||
| # by converting it to weak ref, | ||
| # the original `output` will immediately be released | ||
|
|
@@ -305,5 +316,8 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any | None: | |
| f"got {new_input_addresses}" | ||
| ) | ||
|
|
||
| # Sync offloader before replay - ensures any external dependencies | ||
| # from pre-capture prefetches are satisfied. | ||
| get_offloader().sync_prev_onload() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: this is runtime overhead on hot path. when offloader is not on, worth making this cheaper ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's no op when offload is not configured. see BaseOffloader and NoopOffloader. |
||
| entry.cudagraph.replay() | ||
| return entry.output | ||
|
minosfuture marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Configuration for model weight offloading.""" | ||
|
|
||
| from pydantic import Field, model_validator | ||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from vllm.config.utils import config | ||
|
|
||
|
|
||
| @config | ||
| @dataclass | ||
|
minosfuture marked this conversation as resolved.
Outdated
|
||
| class OffloadConfig: | ||
| """Configuration for model weight offloading to CPU. | ||
|
|
||
| This controls how model parameters are offloaded to CPU memory to reduce | ||
| GPU memory usage, at the cost of additional CPU-GPU transfers during | ||
| inference. | ||
| """ | ||
|
|
||
| cpu_offload_gb: float = Field(default=0, ge=0) | ||
| """The space in GiB to offload to CPU, per GPU. Default is 0, which means | ||
| no offloading. Intuitively, this argument can be seen as a virtual way to | ||
| increase the GPU memory size. For example, if you have one 24 GB GPU and | ||
| set this to 10, virtually you can think of it as a 34 GB GPU. Then you can | ||
| load a 13B model with BF16 weight, which requires at least 26GB GPU memory. | ||
| Note that this requires fast CPU-GPU interconnect, as part of the model is | ||
| loaded from CPU memory to GPU memory on the fly in each model forward pass. | ||
| This uses UVA (Unified Virtual Addressing) for zero-copy access. | ||
| """ | ||
|
|
||
| cpu_offload_params: set[str] = Field(default_factory=set) | ||
| """The set of parameter name segments to target for CPU offloading. | ||
| Unmatched parameters are not offloaded. If this set is empty, parameters | ||
| are offloaded non-selectively until the memory limit defined by | ||
| `cpu_offload_gb` is reached. | ||
| Examples: | ||
| - For parameter name "mlp.experts.w2_weight": | ||
| - "experts" or "experts.w2_weight" will match. | ||
| - "expert" or "w2" will NOT match (must be exact segments). | ||
| This allows distinguishing parameters like "w2_weight" and "w2_weight_scale". | ||
| """ | ||
|
|
||
| offload_group_size: int = Field(default=0, ge=0) | ||
| """Advanced CPU offloading (V2): Group every N layers together. Offload last | ||
| `offload_num_in_group` layers of each group. Default is 0 (disabled). | ||
| Example: group_size=8, num_in_group=2 offloads layers 6,7,14,15,22,23,... | ||
| Unlike cpu_offload_gb, this uses explicit async prefetching to hide transfer | ||
| latency. | ||
| """ | ||
|
|
||
| offload_num_in_group: int = Field(default=1, ge=1) | ||
| """Advanced CPU offloading (V2): Number of layers to offload per group. | ||
| Must be <= offload_group_size. Default is 1.""" | ||
|
|
||
| offload_prefetch_step: int = Field(default=1, ge=0) | ||
| """Advanced CPU offloading (V2): Number of layers to prefetch ahead. | ||
| Higher values hide more latency but use more GPU memory. Default is 1.""" | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_offload_config(self) -> "OffloadConfig": | ||
| """Validate offload configuration constraints.""" | ||
| if self.offload_group_size > 0: | ||
| if self.offload_num_in_group > self.offload_group_size: | ||
| raise ValueError( | ||
| f"offload_num_in_group ({self.offload_num_in_group}) must be " | ||
| f"<= offload_group_size ({self.offload_group_size})" | ||
| ) | ||
| if self.offload_prefetch_step < 1: | ||
| raise ValueError( | ||
| f"offload_prefetch_step ({self.offload_prefetch_step}) must be " | ||
| f">= 1 when V2 offloading is enabled (offload_group_size > 0)" | ||
| ) | ||
| return self | ||
|
minosfuture marked this conversation as resolved.
|
||
|
|
||
| def compute_hash(self) -> str: | ||
| """ | ||
| Provide a hash that uniquely identifies all the offload configs. | ||
|
|
||
| All fields are included because OffloaderV2 patches module | ||
| forwards and inserts custom ops (wait_prefetch, start_prefetch) | ||
| into the computation graph. Changing any offload setting can | ||
| alter which layers are hooked and how prefetch indices are | ||
| computed, so the compilation cache must distinguish them. | ||
| """ | ||
| # OffloaderV2 (offload_group_size > 0) patches module forwards | ||
| # and inserts custom ops (wait_prefetch, start_prefetch) into the | ||
| # computation graph, so all offload settings must be part of the | ||
| # cache key to avoid stale compilation cache hits. | ||
| from vllm.config.utils import get_hash_factors, hash_factors | ||
|
|
||
| factors = get_hash_factors(self, ignored_factors=set()) | ||
| hash_str = hash_factors(factors) | ||
| return hash_str | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,14 @@ class LLM: | |
| the model weights. This virtually increases the GPU memory space | ||
| you can use to hold the model weights, at the cost of CPU-GPU data | ||
| transfer for every forward pass. | ||
| offload_group_size: Advanced CPU offloading: Group every N layers | ||
| together. Offload last `offload_num_in_group` layers of each group. | ||
| Default is 0 (disabled). | ||
| offload_num_in_group: Advanced CPU offloading: Number of layers to | ||
| offload per group. Default is 1. | ||
| offload_prefetch_step: Advanced CPU offloading: Number of layers to | ||
| prefetch ahead. Higher values hide more latency but use more GPU | ||
| memory. Default is 1. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we are missing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. Thanks! |
||
| enforce_eager: Whether to enforce eager execution. If True, we will | ||
| disable CUDA graph and always execute the model in eager mode. | ||
| If False, we will use CUDA graph and eager execution in hybrid. | ||
|
|
@@ -217,6 +225,9 @@ def __init__( | |
| gpu_memory_utilization: float = 0.9, | ||
| swap_space: float = 4, | ||
| cpu_offload_gb: float = 0, | ||
| offload_group_size: int = 0, | ||
| offload_num_in_group: int = 1, | ||
| offload_prefetch_step: int = 1, | ||
| enforce_eager: bool = False, | ||
| enable_return_routed_experts: bool = False, | ||
| disable_custom_all_reduce: bool = False, | ||
|
|
@@ -326,6 +337,9 @@ def _make_config(value: Any, cls: type[_R]) -> _R: | |
| kv_cache_memory_bytes=kv_cache_memory_bytes, | ||
| swap_space=swap_space, | ||
| cpu_offload_gb=cpu_offload_gb, | ||
| offload_group_size=offload_group_size, | ||
| offload_num_in_group=offload_num_in_group, | ||
| offload_prefetch_step=offload_prefetch_step, | ||
| enforce_eager=enforce_eager, | ||
| enable_return_routed_experts=enable_return_routed_experts, | ||
| disable_custom_all_reduce=disable_custom_all_reduce, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use an fp8 model here to make it faster like RedHatAI/DeepSeek-Coder-V2-Lite-Instruct-FP8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serving "RedHatAI/DeepSeek-Coder-V2-Lite-Instruct-FP8" failed at flashinfer autotuning stage on GB200. 😿
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switched to llama as we support any model now.