Skip to content

Support async in DeepEP #4610

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

Merged
merged 35 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
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
39 changes: 24 additions & 15 deletions python/sglang/srt/layers/moe/ep_moe/token_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
except ImportError:
use_deepep = False

import os
from typing import Optional, Tuple

import torch
Expand Down Expand Up @@ -101,6 +100,7 @@ def __init__(
num_local_experts: int = None,
hidden_size: int = None,
params_dtype: torch.dtype = None,
async_finish: bool = False,
):
self.group = group
self.router_topk = router_topk
Expand All @@ -117,6 +117,7 @@ def __init__(
self.token_probs = None
# Handle used for combine operation
self.handle = None
self.async_finish = async_finish

# `num_max_dispatch_tokens_per_rank` (the actual batch size in the decoding engine) should be less than 256
# https://github.com/deepseek-ai/DeepEP?tab=readme-ov-file#example-use-in-inference-decoding
Expand Down Expand Up @@ -182,7 +183,6 @@ def dispatch(
topk_weights: torch.Tensor,
num_experts: int,
forward_mode: ForwardMode,
previous_event=None,
num_max_dispatch_tokens_per_rank: int = 128,
) -> Tuple[torch.Tensor, torch.Tensor]:
topk_idx = topk_idx.to(torch.int64)
Expand All @@ -195,9 +195,7 @@ def dispatch(
num_recv_tokens_per_expert_list,
handle,
event,
) = self.dispatch_normal(
hidden_states, topk_idx, topk_weights, num_experts, previous_event
)
) = self.dispatch_normal(hidden_states, topk_idx, topk_weights, num_experts)
self.tokens_per_expert = torch.tensor(
num_recv_tokens_per_expert_list,
device=hidden_states.device,
Expand All @@ -213,6 +211,10 @@ def dispatch(
)
)
self.recv_expert_count = recv_expert_count

if self.async_finish:
event.current_stream_wait()

self.handle = handle
self.topk_idx = topk_idx
self.topk_weights = topk_weights
Expand All @@ -235,8 +237,9 @@ def dispatch_normal(
topk_idx: torch.Tensor,
topk_weights: torch.Tensor,
num_experts: int,
previous_event=None,
):
previous_event = Buffer.capture() if self.async_finish else None

(
num_tokens_per_rank,
num_tokens_per_rdma_rank,
Expand All @@ -247,8 +250,8 @@ def dispatch_normal(
topk_idx,
num_experts,
previous_event=previous_event,
async_finish=False,
allocate_on_comm_stream=False,
async_finish=self.async_finish,
allocate_on_comm_stream=previous_event is not None,
)

(
Expand All @@ -267,8 +270,8 @@ def dispatch_normal(
is_token_in_rank=is_token_in_rank,
num_tokens_per_expert=num_tokens_per_expert,
previous_event=previous_event,
async_finish=False,
allocate_on_comm_stream=False,
async_finish=self.async_finish,
allocate_on_comm_stream=(previous_event is not None) and self.async_finish,
)

return (
Expand Down Expand Up @@ -333,7 +336,7 @@ def dispatch_low_latency(
topk_idx,
num_max_dispatch_tokens_per_rank,
num_experts,
async_finish=False,
async_finish=self.async_finish,
return_recv_hook=False, # True for double-batch overlapping, need call hook()
)
)
Expand Down Expand Up @@ -373,16 +376,22 @@ def combine(
hidden_states, event, hook = self.combine_low_latency(
hidden_states, self.topk_idx, self.topk_weights, self.handle
)

if self.async_finish:
event.current_stream_wait()

self.handle = None
return hidden_states

def combine_normal(self, x: torch.Tensor, handle: Tuple, previous_event=None):
def combine_normal(self, x: torch.Tensor, handle: Tuple):
previous_event = Buffer.capture() if self.async_finish else None

combined_x, _, event = self.buffer_normal.combine(
x,
handle,
async_finish=False,
async_finish=self.async_finish,
previous_event=previous_event,
allocate_on_comm_stream=False,
allocate_on_comm_stream=previous_event is not None,
)
return combined_x, event

Expand All @@ -399,7 +408,7 @@ def combine_low_latency(
topk_idx,
topk_weights,
handle,
async_finish=False,
async_finish=self.async_finish,
return_recv_hook=False, # True for double-batch overlapping, need call hook()
)
)
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/deepseek_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def __init__(
num_local_experts=config.n_routed_experts // self.tp_size,
hidden_size=config.hidden_size,
params_dtype=config.torch_dtype,
async_finish=True, # TODO
)

def forward(
Expand Down
Loading