Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
92b1733
FA2 and FlashInfer Full cuda graph support
fhl2000 Jun 25, 2025
58ce477
fix the arch support in CMakeLists.txt to include 8.9
fhl2000 Jun 25, 2025
c2c5fea
Refactors
fhl2000 Jun 25, 2025
1606880
refactors
fhl2000 Jun 25, 2025
806432a
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jun 25, 2025
7c5df45
refactor
fhl2000 Jun 25, 2025
c7a9424
Add check for separate_attention_routine flag
fhl2000 Jun 25, 2025
e8b9296
fix typo error
fhl2000 Jun 26, 2025
94d0b79
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jun 27, 2025
a67c698
refactors and rearchitect cuda graph logic
fhl2000 Jun 28, 2025
da110af
Refactors
fhl2000 Jun 28, 2025
deaf0fe
Delect one commit
fhl2000 Jun 28, 2025
02ca154
Add support for force_no_split_graph
fhl2000 Jun 28, 2025
fa0d25c
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 1, 2025
5108bef
Huge refactors to separete cudagraph logic from vllm compilation
fhl2000 Jul 5, 2025
1c1873d
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 5, 2025
7d4667a
refactors
fhl2000 Jul 5, 2025
fedff47
fix errors
fhl2000 Jul 5, 2025
833ac56
fix small error by lazy import
fhl2000 Jul 5, 2025
d57257d
handle lint-and-deploy errors for cpu execution
fhl2000 Jul 5, 2025
8b7ea7a
remove redundents
fhl2000 Jul 5, 2025
328615d
Clear
fhl2000 Jul 6, 2025
debc682
Big refactors
fhl2000 Jul 9, 2025
cad6c39
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 9, 2025
dc455ee
cleanup
fhl2000 Jul 10, 2025
620a728
fix warmup
fhl2000 Jul 10, 2025
b1e6978
Commit suggestion: Update vllm/config.py
fhl2000 Jul 10, 2025
beee69a
commit suggestion2: Update vllm/config.py
fhl2000 Jul 10, 2025
21b1a8d
fix enforce_eager
fhl2000 Jul 10, 2025
ec79af7
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 10, 2025
210359a
small cleanup for pre-commit
fhl2000 Jul 10, 2025
11263e0
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 11, 2025
9a38a4e
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 12, 2025
699aff3
refactors
fhl2000 Jul 13, 2025
ef3d9d9
resolve yapf conflicts with isort
fhl2000 Jul 13, 2025
658565e
fixes
fhl2000 Jul 13, 2025
15e2b4a
fix global graph pool issue
fhl2000 Jul 13, 2025
4253dbf
fix refactors
fhl2000 Jul 13, 2025
2783e26
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 14, 2025
1b54962
more refactors
fhl2000 Jul 14, 2025
fb2a3c7
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 17, 2025
d6269bd
refactors for and more
fhl2000 Jul 17, 2025
2e1304c
fix pre-commit
fhl2000 Jul 17, 2025
db22ca5
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 18, 2025
72d40e6
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 20, 2025
0c79e53
change cudagraph dispatching logics; runtime style->runtime mode
fhl2000 Jul 21, 2025
75db3a6
pass pre-commit
fhl2000 Jul 21, 2025
0bca4c4
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 23, 2025
9d2f148
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 24, 2025
60bdc61
fix bug when cudagraph_separate_routine==False
fhl2000 Jul 24, 2025
9036bd2
recover FlashInfer from main branch
fhl2000 Jul 24, 2025
89ec3aa
address comments and clean up
fhl2000 Jul 26, 2025
4b991a3
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 26, 2025
614f6ea
clean up
fhl2000 Jul 26, 2025
c049627
fix
fhl2000 Jul 26, 2025
e69e488
add tests; more docs
fhl2000 Jul 27, 2025
835086a
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 27, 2025
534410e
clean up
fhl2000 Jul 27, 2025
618f7c0
small fix
fhl2000 Jul 27, 2025
1b343eb
add more docs
fhl2000 Jul 27, 2025
532f245
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 28, 2025
431a726
simplify the logic
fhl2000 Jul 28, 2025
19faeda
fix CI failures
fhl2000 Jul 29, 2025
348a117
fix CI failures again
fhl2000 Jul 29, 2025
fc5e37a
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 29, 2025
4d9829f
fix pre-commit
fhl2000 Jul 29, 2025
7773608
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 29, 2025
a692bb6
fix CI
fhl2000 Jul 29, 2025
543f264
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 30, 2025
3e5959a
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 31, 2025
aa35551
fix errors;move default initialization of cudagraph_mode to __post_in…
fhl2000 Jul 31, 2025
bad2710
fix a potential bug
fhl2000 Jul 31, 2025
f175c16
Merge branch 'vllm-project:main' into full_cudagraph_FA2_FlashInfer
fhl2000 Jul 31, 2025
9916a75
Merge remote-tracking branch 'origin/main' into pr-20059
LucasWilkinson Jul 31, 2025
81d7561
wip rework cudagraph_mode
LucasWilkinson Aug 1, 2025
0137d84
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 2, 2025
1bfb855
fix and re-enable FlashInfer full cudagraph
fhl2000 Aug 2, 2025
24c40ab
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 2, 2025
95d94f8
fix some CI tests
fhl2000 Aug 2, 2025
e7763ef
fallback
LucasWilkinson Aug 4, 2025
803a185
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 4, 2025
645accf
warn perferred
LucasWilkinson Aug 4, 2025
5029a6a
fix bugs and some refactors;temporarily add FULL_DOUBLE mode
fhl2000 Aug 5, 2025
fef7eee
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 5, 2025
e796196
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 5, 2025
816024e
fix incorrectly infering type from CUDAGraphWrapper
fhl2000 Aug 5, 2025
651f729
fix and refactor cudagraph_mode checkings
fhl2000 Aug 6, 2025
38ddeaf
remove full double
LucasWilkinson Aug 6, 2025
9ca04ed
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 7, 2025
a7adfae
Merge remote-tracking branch 'origin/main' into fhl2000/full_cudagrap…
LucasWilkinson Aug 7, 2025
14e83f5
Merge branch 'fhl2000_full_cudagraph_FA2_FlashInfer_merge' into full_…
LucasWilkinson Aug 7, 2025
9cc6b93
fix
LucasWilkinson Aug 7, 2025
1e97920
fix
LucasWilkinson Aug 7, 2025
25b6242
cleanup
LucasWilkinson Aug 7, 2025
85f20bf
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 8, 2025
766eb7c
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 8, 2025
b0374be
deprecation
LucasWilkinson Aug 8, 2025
a160dd4
migrate flags
LucasWilkinson Aug 8, 2025
c2dc791
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 8, 2025
028f119
cleanup
LucasWilkinson Aug 8, 2025
43db16d
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 9, 2025
2cad036
fix some unit tests
LucasWilkinson Aug 9, 2025
6839e88
more cleanup
LucasWilkinson Aug 9, 2025
04ed99a
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 9, 2025
3f2b279
fix more unit tests
LucasWilkinson Aug 9, 2025
d500150
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
fhl2000 Aug 10, 2025
a56d549
fix is_attention_splitting;fix new mamba_attn cg support
fhl2000 Aug 10, 2025
3499d7b
wip
LucasWilkinson Aug 10, 2025
83d4e7c
stabalize unit test
LucasWilkinson Aug 11, 2025
bf8a51d
cleanup
LucasWilkinson Aug 11, 2025
d1f62e4
unit test fix
LucasWilkinson Aug 11, 2025
7e19ca4
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 11, 2025
c722f2c
refactor
LucasWilkinson Aug 11, 2025
1937615
remove accidentally committed file
LucasWilkinson Aug 11, 2025
ce9cc82
fix XPU tests
LucasWilkinson Aug 11, 2025
f3561f9
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 11, 2025
9d6b189
fix xpu
LucasWilkinson Aug 11, 2025
3c4b532
match HPU cudagraph handling + down grade log
LucasWilkinson Aug 11, 2025
bed9576
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 12, 2025
19f7447
fix xpu
LucasWilkinson Aug 12, 2025
0122313
unit test fixes
LucasWilkinson Aug 12, 2025
3a2041b
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 14, 2025
641b10b
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 14, 2025
974c707
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
LucasWilkinson Aug 14, 2025
3805f3f
Apply suggestions from code review
LucasWilkinson Aug 15, 2025
f2c437a
review comments
LucasWilkinson Aug 15, 2025
1ff41d8
Update vllm/v1/worker/gpu_model_runner.py
LucasWilkinson Aug 15, 2025
af2a38c
Merge remote-tracking branch 'origin/main' into full_cudagraph_FA2_Fl…
fhl2000 Aug 15, 2025
f751e50
Merge branch 'main' into full_cudagraph_FA2_FlashInfer
fhl2000 Aug 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions vllm/compilation/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from vllm.config import CompilationConfig, VllmConfig
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.utils import is_torch_equal_or_newer, resolve_obj_by_qualname
from vllm.utils import is_torch_equal_or_newer

from .compiler_interface import (CompilerInterface, EagerAdaptor,
InductorAdaptor, InductorStandaloneAdaptor)
Expand Down Expand Up @@ -258,6 +258,14 @@ def split_graph(graph: fx.GraphModule,
# we share the global graph pool among all the backends
global_graph_pool = None


def get_global_graph_pool():
global global_graph_pool
if global_graph_pool is None:
global_graph_pool = current_platform.graph_pool_handle()
return global_graph_pool


compilation_start_time = 0.0


Expand Down Expand Up @@ -317,10 +325,9 @@ def call_module(self, target: torch.fx.node.Target,
graph_index=index,
num_graphs=len(self.compile_submod_names),
runtime_shape=None)

piecewise_backend = resolve_obj_by_qualname(
current_platform.get_piecewise_backend_cls())
self.module.__dict__[target] = piecewise_backend(
# Lazy import here to avoid circular import
from .piecewise_backend import PiecewiseBackend
self.module.__dict__[target] = PiecewiseBackend(
submod, self.vllm_config, self.graph_pool, index,
len(self.compile_submod_names), sym_shape_indices,
compiled_graph_for_general_shape, self.vllm_backend)
Expand Down Expand Up @@ -391,9 +398,7 @@ def __init__(
# them, e.g. backbone (default), eagle_head, etc.
self.prefix = prefix or model_tag

global global_graph_pool
if global_graph_pool is None:
global_graph_pool = current_platform.graph_pool_handle()
global_graph_pool = get_global_graph_pool()

# TODO: in the future, if we want to use multiple
# streams, it might not be safe to share a global pool.
Expand Down
72 changes: 0 additions & 72 deletions vllm/compilation/base_piecewise_backend.py

This file was deleted.

57 changes: 57 additions & 0 deletions vllm/compilation/base_static_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

from typing import Any, Callable, Protocol

from vllm.config import VllmConfig


class AbstractStaticGraphWrapper(Protocol):
"""
StaticGraphWrapper interface that allows platforms to wrap a callable
to be captured as a static graph.
"""

def __init__(self, runnable: Callable, vllm_config: VllmConfig,
graph_pool: Any, runtime_style: int, **kwargs):
"""
Initializes the StaticGraphWrapper class with graph capturing and
execution-related configurations.
Args:
runnable (Callable): The callable to be wrapped and captured.
vllm_config (VllmConfig): Global configuration for vLLM.
graph_pool (Any):
Graph memory pool handle, e.g.,
`torch.cuda.graph_pool_handle()`.
runtime_style (Any): The style of the static
graph runtime.
Keyword Args:
kwargs: Additional keyword arguments for platform-specific
configurations.
"""
raise NotImplementedError

def maybe_replace_runnable(self, shape: int, runnable: Any):
"""
Replaces the runnable with a new one for a specific compiled shape.
"""
raise NotImplementedError

def __call__(self, *args, **kwargs) -> Any:
"""
Executes the wrapped callable.
This may involve replaying a captured static graph if the conditions
are met, or running the original callable eagerly and potentially
capturing it.
Args:
*args: Variable length input arguments to be passed into the
callable.
**kwargs: Keyword arguments to be passed into the callable.
Returns:
Any: Output of the executed callable.
"""
raise NotImplementedError
185 changes: 185 additions & 0 deletions vllm/compilation/cuda_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import dataclasses
from contextlib import ExitStack
from typing import Any, Callable, Optional
from unittest.mock import patch

import torch

import vllm.envs as envs
from vllm.compilation.counter import compilation_counter
from vllm.config import CUDAGraphRuntimeStyle, VllmConfig
from vllm.forward_context import get_forward_context
from vllm.logger import init_logger
from vllm.utils import weak_ref_tensors

logger = init_logger(__name__)


@dataclasses.dataclass
class CUDAGraphEntry:
runtime_shape: int
num_finished_warmup: int = 0
runnable: Callable = None # type: ignore
cudagraph: Optional[torch.cuda.CUDAGraph] = None
output: Optional[Any] = None

# for cudagraph debugging, track the input addresses
# during capture, and check if they are the same during replay
input_addresses: Optional[list[int]] = None

usage_type: Optional[str] = None # For debug logging only


class CUDAGraphWrapper:
"""
This class simply wrap a runnable for cudagraph functionality,
taking responsibility of capturing cudagraph and running the replay.
"""

def __init__(self,
runnable: Any,
vllm_config: VllmConfig,
runtime_style: int,
graph_pool: Any = None,
cudagraph_specific_config: Optional[dict[str, Any]] = None):
self.runnable = runnable
self.vllm_config = vllm_config
self.graph_pool = graph_pool
self.runtime_style = runtime_style
self.compilation_config = vllm_config.compilation_config

self.first_run_finished = False
self.is_debugging_mode = envs.VLLM_LOGGING_LEVEL == "DEBUG"

assert self.runtime_style >= CUDAGraphRuntimeStyle.PIECEWISE
if self.graph_pool is None:
# lazy import to avoid triggering some import issues.
from vllm.compilation.backends import get_global_graph_pool
self.graph_pool = get_global_graph_pool()

if cudagraph_specific_config is None:
cudagraph_specific_config = {}
self.debug_capturing = cudagraph_specific_config.get(
"debug_capturing", True)
self.gc_disable = cudagraph_specific_config.get("gc_disable", False)
self.weak_ref_output = cudagraph_specific_config.get(
"weak_ref_output", True)
usage_type = cudagraph_specific_config.get("usage_type")
self.cudagraph_capture_sizes: set[int] = set(
self.compilation_config.cudagraph_capture_sizes)
# the entries for different shapes that we need to capture cudagraph
self.concrete_cudagraph_entries: dict[int, CUDAGraphEntry] = {}

for shape in self.cudagraph_capture_sizes:

self.concrete_cudagraph_entries[shape] = CUDAGraphEntry(
runtime_shape=shape,
runnable=self.runnable,
usage_type=usage_type, # for debug logging only
)

def maybe_replace_runnable(self, shape: int, runnable: Callable):
# this is a hack to replace a general shape runnable with a compiled
# runnable of a specific shape.
if shape not in self.concrete_cudagraph_entries:
return
entry = self.concrete_cudagraph_entries[shape]
assert entry.cudagraph is None, "Cudagraph is already captured"
entry.runnable = runnable

def __call__(self, *args, **kwargs):
forward_context = get_forward_context()
runtime_shape = forward_context.num_tokens
cudagraph_runtime_style = forward_context.cudagraph_runtime_style

if cudagraph_runtime_style == CUDAGraphRuntimeStyle.NONE or\
runtime_shape is None:
# TODO: make sure here is on profile running or eager running
return self.runnable(*args, **kwargs)
if cudagraph_runtime_style != self.runtime_style:
# CUDAGraph runtime style don't match the current
# configuration, so directly call runnable eagerly
# as it's always safe.
return self.runnable(*args, **kwargs)

if runtime_shape not in self.concrete_cudagraph_entries:
# we don't need to do anything for this shape.
return self.runnable(*args, **kwargs)

entry = self.concrete_cudagraph_entries[runtime_shape]

if entry.cudagraph is None:
if entry.num_finished_warmup < self.compilation_config.cudagraph_num_of_warmups: # noqa
entry.num_finished_warmup += 1
if self.debug_capturing:
logger.debug(
"Warming up %s/%s of %s usage for shape %s",
entry.num_finished_warmup,
self.compilation_config.cudagraph_num_of_warmups,
entry.usage_type, entry.runtime_shape)
return entry.runnable(*args, **kwargs)

if self.debug_capturing:
# Since we capture cudagraph for many different shapes and
# capturing is fast, we don't need to log it for every
# shape. We only log it in the debug mode.
logger.debug("Capturing a cudagraph of %s usage for shape %s",
entry.usage_type, entry.runtime_shape)

input_addresses = [
x.data_ptr() for x in args if isinstance(x, torch.Tensor)
]
entry.input_addresses = input_addresses
cudagraph = torch.cuda.CUDAGraph()

with ExitStack() as stack:
if self.gc_disable:
# during every model forward for piecewise cudagraph
# mode, we will capture many pieces of cudagraphs
# (roughly one per layer). running gc again and again
# across layers will make the cudagraph capture very slow.
# therefore, we only run gc for the first graph,
# and disable gc for the rest of the graphs.
stack.enter_context(patch("gc.collect", lambda: None))
stack.enter_context(
patch("torch.cuda.empty_cache", lambda: None))

# mind-exploding: carefully manage the reference and memory.
with torch.cuda.graph(cudagraph, pool=self.graph_pool):
# `output` is managed by pytorch's cudagraph pool
output = entry.runnable(*args, **kwargs)
if self.weak_ref_output:
# by converting it to weak ref,
# the original `output` will immediately be released
# to save memory. It is only safe to do this for
# the last graph, because the output of the last
# graph will not be used by any other cuda graph.
output = weak_ref_tensors(output)

# here we always use weak ref for the output
# to save memory
entry.output = weak_ref_tensors(output)
entry.cudagraph = cudagraph

compilation_counter.num_cudagraph_captured += 1

# important: we need to return the output, rather than
# the weak ref of the output, so that pytorch can correctly
# manage the memory during cuda graph capture
return output

if self.is_debugging_mode:
# check if the input addresses are the same
new_input_addresses = [
x.data_ptr() for x in args if isinstance(x, torch.Tensor)
]
assert new_input_addresses == entry.input_addresses, (
"Input addresses for cudagraphs are different during "
f"replay. Expected {entry.input_addresses}, got "
f"{new_input_addresses}")

entry.cudagraph.replay()
return entry.output
Loading