From 58b12ccb4629099b8730e37c444f8e6596d7b55d Mon Sep 17 00:00:00 2001 From: Ke Bao Date: Mon, 10 Nov 2025 23:18:03 +0800 Subject: [PATCH] Support piecewise cuda graph for deepseek v3 (#12996) --- python/sglang/srt/layers/moe/topk.py | 24 ++++++++++++++++++++++++ python/sglang/srt/server_args.py | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/python/sglang/srt/layers/moe/topk.py b/python/sglang/srt/layers/moe/topk.py index 28636de23..203cd5f41 100644 --- a/python/sglang/srt/layers/moe/topk.py +++ b/python/sglang/srt/layers/moe/topk.py @@ -922,3 +922,27 @@ def select_experts( get_global_expert_distribution_recorder().on_select_experts(topk_ids=topk_ids) return StandardTopKOutput(topk_weights, topk_ids, router_logits) + + +# Register fake implementations for torch.compile support +if _is_cuda: + + @torch.library.register_fake("sgl_kernel::moe_fused_gate") + def _( + input_tensor, + bias, + num_expert_group, + topk_group, + topk, + num_fused_shared_experts=0, + routed_scaling_factor=0, + apply_routed_scaling_factor_on_output=False, + ): + num_rows = input_tensor.shape[0] + topk_weights = torch.empty( + (num_rows, topk), dtype=torch.float32, device=input_tensor.device + ) + topk_ids = torch.empty( + (num_rows, topk), dtype=torch.int32, device=input_tensor.device + ) + return topk_weights, topk_ids diff --git a/python/sglang/srt/server_args.py b/python/sglang/srt/server_args.py index dd5bc924c..5b9a520b9 100644 --- a/python/sglang/srt/server_args.py +++ b/python/sglang/srt/server_args.py @@ -819,6 +819,10 @@ class ServerArgs: # eagle draft models and cuda graphs reserved_mem += 2 * 1024 + # For piecewise cuda graphs + if self.enable_piecewise_cuda_graph: + reserved_mem += self.piecewise_cuda_graph_max_tokens // 4 + self.mem_fraction_static = ( round((gpu_mem - reserved_mem) / gpu_mem, 3) if gpu_mem is not None @@ -900,6 +904,9 @@ class ServerArgs: hf_config = self.get_hf_config() model_arch = hf_config.architectures[0] if model_arch in ["DeepseekV3ForCausalLM"] and not is_deepseek_nsa(hf_config): + if self.enable_piecewise_cuda_graph: + logger.info("Piecewise CUDA graph is enabled, use MLA for prefill.") + if is_cuda() and is_sm100_supported(): if ( self.attention_backend is None