Skip to content

Commit a854cc6

Browse files
committed
DeepEP LL dispatch FP4
Signed-off-by: Yilin Zhang <[email protected]>
1 parent 428e340 commit a854cc6

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

cpp/tensorrt_llm/deep_ep/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(DEEP_EP_COMMIT 7b15af835942675df041eca2dcb9930b880287e1)
1+
set(DEEP_EP_COMMIT edf3ea2b086a393d3163bf2773eab69d9191cc01)
22
set(NVSHMEM_URL_HASH
33
SHA256=eb2c8fb3b7084c2db86bd9fd905387909f1dfd483e7b45f7b3c3d5fcf5374b5a)
44

tensorrt_llm/_torch/modules/fused_moe/deep_ep_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ def low_latency_dispatch(self, hidden_states: torch.Tensor,
154154
# Later, you can use our GEMM library to do the computation with this specific format
155155
return recv_hidden_states, recv_expert_count, handle
156156

157+
def low_latency_dispatch_fp4(self, hidden_states: torch.Tensor,
158+
scales: torch.Tensor, topk_idx: torch.Tensor,
159+
num_max_dispatch_tokens_per_rank: int,
160+
num_experts: int):
161+
assert num_experts == self.num_experts
162+
163+
# Do MoE dispatch, compatible with CUDA graph (but you may restore some buffer status once you replay)
164+
recv_hidden_states, recv_scales, recv_expert_count, handle, event, hook = \
165+
self.buffer.low_latency_dispatch_fp4(hidden_states, scales, topk_idx, num_max_dispatch_tokens_per_rank, num_experts)
166+
assert event.event is None
167+
assert hook is None
168+
169+
# NOTES: the actual tensor will not be received only if you call `hook()`,
170+
# it is useful for double-batch overlapping, but **without any SM occupation**
171+
# If you don't want to overlap, please set `return_recv_hook=False`
172+
# Later, you can use our GEMM library to do the computation with this specific format
173+
return recv_hidden_states, recv_scales, recv_expert_count, handle
174+
157175
def low_latency_combine(self, hidden_states: torch.Tensor,
158176
topk_idx: torch.Tensor, topk_weights: torch.Tensor,
159177
handle: Tuple):

tensorrt_llm/_torch/modules/fused_moe/fused_moe_wide_ep.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -588,43 +588,26 @@ def forward_chunk(
588588
x_sf = swizzle_sf(x_sf, x.shape[0], x.shape[1] * 2,
589589
self.scaling_vector_size)
590590
elif self.alltoall_method_type == AlltoallMethodType.DeepEPLowLatency:
591-
assert x_sf is not None and self.has_nvfp4
592591
token_num = x_row
593592
hidden_size = x_col
593+
assert x_sf is not None and self.has_nvfp4
594594
assert hidden_size % 32 == 0
595-
x_sf_dtype = x_sf.dtype
596-
x_dtype = x.dtype
597-
assert x_sf_dtype == torch.uint8 and x_dtype == torch.uint8
598-
x_sf = x_sf.view(torch.bfloat16)
595+
assert x.dtype == torch.uint8 and x_sf.dtype == torch.uint8
599596
assert x_sf.shape[0] == token_num and x_sf.shape[
600-
1] == hidden_size // 16 // 2
601-
x = x.view(torch.bfloat16)
602-
assert x.shape[0] == token_num and x.shape[1] == hidden_size // 4
603-
# DeepEP LL dispatch only supports bf16 tensors with a hidden size of 2560, 4096, 5120, or 7168 as input. A hidden size of 2560 is sufficient to accommodate packed FP4 data.
604-
packed_hidden_size = 2560
605-
assert x.shape[1] + x_sf.shape[1] <= packed_hidden_size
606-
fp4_packed_tensor = torch.empty((token_num, packed_hidden_size),
607-
dtype=torch.bfloat16,
608-
device=x.device)
609-
fp4_packed_tensor[:, :x.shape[1]] = x
610-
fp4_packed_tensor[:,
611-
x.shape[1]:x.shape[1] + x_sf.shape[1]] = x_sf
597+
1] == hidden_size // 16
598+
assert x.shape[0] == token_num and x.shape[1] == hidden_size // 2
612599

613600
deep_ep_topk_idx = token_selected_slots
614601
deep_ep_topk_weights = token_final_scales
615602

616603
assert all_rank_max_num_tokens <= self.deep_ep_max_num_tokens
617-
fp4_packed_tensor, recv_expert_count, deep_ep_handle = \
618-
self.deep_ep_buffer.low_latency_dispatch(fp4_packed_tensor, deep_ep_topk_idx, all_rank_max_num_tokens, self.num_slots)
619-
deep_ep_handle = list(deep_ep_handle)
620-
deep_ep_handle[3] = hidden_size
621-
deep_ep_handle = tuple(deep_ep_handle)
622-
623-
assert fp4_packed_tensor.ndim == 3 and fp4_packed_tensor.shape[
624-
2] == packed_hidden_size
625-
x_sf = fp4_packed_tensor[:, :, x.shape[1]:x.shape[1] +
626-
x_sf.shape[1]].contiguous()
627-
x = fp4_packed_tensor[:, :, :x.shape[1]].contiguous()
604+
x, x_sf, recv_expert_count, deep_ep_handle = \
605+
self.deep_ep_buffer.low_latency_dispatch_fp4(x, x_sf, deep_ep_topk_idx, all_rank_max_num_tokens, self.num_slots)
606+
assert x.dtype == torch.uint8 and x_sf.dtype == torch.uint8
607+
assert x.dim() == 3 and x_sf.dim() == 3
608+
assert x.shape[2] == hidden_size // 2 and x_sf.shape[
609+
2] == hidden_size // 16
610+
628611
mask = torch.arange(
629612
x.shape[1], dtype=torch.int32, device=x.device).expand(
630613
x.shape[0], x.shape[1]) < recv_expert_count.unsqueeze(1)
@@ -634,9 +617,9 @@ def forward_chunk(
634617
x.shape[0] * (self.mapping.moe_ep_rank + 1),
635618
dtype=torch.int32,
636619
device=x.device).unsqueeze(1), self.num_slots)
637-
x = x.reshape(x.shape[0] * x.shape[1], x.shape[2]).view(x_dtype)
620+
x = x.reshape(x.shape[0] * x.shape[1], x.shape[2])
638621
x_sf = x_sf.reshape(x_sf.shape[0] * x_sf.shape[1],
639-
x_sf.shape[2]).view(x_sf_dtype)
622+
x_sf.shape[2])
640623
x_sf = swizzle_sf(x_sf, x.shape[0], x.shape[1] * 2,
641624
self.scaling_vector_size)
642625
token_selected_slots = token_selected_slots.view(x.shape[0], 1)

0 commit comments

Comments
 (0)