Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
26 changes: 14 additions & 12 deletions vllm/model_executor/layers/fused_moe/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
QuantizationConfig,
QuantizeMethodBase,
)
from vllm.model_executor.utils import set_weight_attrs
from vllm.model_executor.utils import disable_graph_partition, set_weight_attrs
from vllm.platforms import current_platform
from vllm.platforms.interface import CpuArchEnum
from vllm.utils import cdiv, direct_register_custom_op, has_deep_ep, has_pplx, round_up
Expand Down Expand Up @@ -1900,17 +1900,19 @@ def select_experts(
if use_grouped_topk:
assert topk_group is not None
assert num_expert_group is not None
topk_weights, topk_ids = grouped_topk(
hidden_states=hidden_states,
gating_output=router_logits,
topk=top_k,
renormalize=renormalize,
num_expert_group=num_expert_group,
topk_group=topk_group,
scoring_func=scoring_func,
routed_scaling_factor=routed_scaling_factor,
e_score_correction_bias=e_score_correction_bias,
)

with disable_graph_partition():
topk_weights, topk_ids = grouped_topk(
hidden_states=hidden_states,
gating_output=router_logits,
topk=top_k,
renormalize=renormalize,
num_expert_group=num_expert_group,
topk_group=topk_group,
scoring_func=scoring_func,
routed_scaling_factor=routed_scaling_factor,
e_score_correction_bias=e_score_correction_bias,
)
if indices_type is not None:
topk_ids = topk_ids.to(dtype=indices_type)
elif e_score_correction_bias is not None:
Expand Down
24 changes: 24 additions & 0 deletions vllm/model_executor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Utils for model executor."""

import contextlib
import copy
from typing import Any

Expand Down Expand Up @@ -83,3 +84,26 @@ def get_moe_expert_mapping(
if child_map is not None:
return child_map()
return []


@contextlib.contextmanager
def disable_graph_partition():
"""Context manager to disable inductor graph partition.
This is used to avoid nested cudagraph capture.

Example:
1. We apply torch.compile directly on some ops (e.g., grouped_topk) wrapped
in custom ops. Inductor graph partition applies cudagraph within the custom op.
2. At the same time, we compile the model which uses these custom ops. Inductor
graph partition also wraps each graph partition with CUDAGraph. Some partitions
may include custom ops, which has already been applied cudagraph. This leads to
nested cudagraph which is not supported.

This context manager should be wrapped around torch.compile calls within custom ops
to avoid the nested cudagraph capture."""
old_val = torch._inductor.config.graph_partition
try:
torch._inductor.config.graph_partition = False
yield
finally:
torch._inductor.config.graph_partition = old_val

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This implementation relies on modifying torch._inductor.config.graph_partition, which is an internal, undocumented API of PyTorch's Inductor backend. While this is a clever solution to the nested CUDAGraph problem, it makes the code brittle and susceptible to breaking with future PyTorch updates. It would be beneficial to add a comment here warning about this dependency to aid future maintenance.

Suggested change
old_val = torch._inductor.config.graph_partition
try:
torch._inductor.config.graph_partition = False
yield
finally:
torch._inductor.config.graph_partition = old_val
# NOTE: This relies on an internal PyTorch Inductor API.
# This may break in future PyTorch versions.
old_val = torch._inductor.config.graph_partition
try:
torch._inductor.config.graph_partition = False
yield
finally:
torch._inductor.config.graph_partition = old_val

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config will be BC and tested in pytorch x vllm ci.