Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions python/sglang/srt/layers/moe/moe_runner/flashinfer_trtllm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
_TRTLLM_MOE_PDL_MAX_TOKENS = envs.SGLANG_TRTLLM_MOE_PDL_MAX_TOKENS.get()


def trtllm_moe_enable_pdl(num_tokens: int) -> bool:
from sglang.kernels.jit.utils import is_arch_support_pdl

return is_arch_support_pdl() and num_tokens <= _TRTLLM_MOE_PDL_MAX_TOKENS


@dataclass
class FlashInferTrtllmDeferredFinalizeOutput:
gemm2_out: torch.Tensor
Expand All @@ -74,7 +80,6 @@ def finalize_flashinfer_trtllm_deferred_output(
deferred_output: FlashInferTrtllmDeferredFinalizeOutput,
shared_output: torch.Tensor,
) -> torch.Tensor:
from sglang.kernels.jit.utils import is_arch_support_pdl
from sglang.kernels.ops.moe.moe_finalize_fuse_shared import moe_finalize_fuse_shared

return moe_finalize_fuse_shared(
Expand All @@ -83,7 +88,7 @@ def finalize_flashinfer_trtllm_deferred_output(
deferred_output.expert_weights,
shared_output,
deferred_output.top_k,
enable_pdl=is_arch_support_pdl(),
enable_pdl=trtllm_moe_enable_pdl(deferred_output.expert_weights.shape[0]),
)


Expand Down Expand Up @@ -1087,7 +1092,7 @@ def fused_experts_none_to_flashinfer_trtllm_fp4(
activation_type=activation_type,
tune_max_num_tokens=next_power_of_2(hs_fp4.shape[0]),
output=symm_output,
enable_pdl=hs_fp4.shape[0] <= _TRTLLM_MOE_PDL_MAX_TOKENS,
enable_pdl=trtllm_moe_enable_pdl(hs_fp4.shape[0]),
)[0]
else:
assert TopKOutputChecker.format_is_bypassed(topk_output)
Expand Down Expand Up @@ -1131,7 +1136,7 @@ def fused_experts_none_to_flashinfer_trtllm_fp4(
do_finalize=not defer_finalize,
activation_type=activation_type,
tune_max_num_tokens=next_power_of_2(hs_fp4.shape[0]),
enable_pdl=hs_fp4.shape[0] <= _TRTLLM_MOE_PDL_MAX_TOKENS,
enable_pdl=trtllm_moe_enable_pdl(hs_fp4.shape[0]),
)
if not defer_finalize:
moe_kwargs["output"] = symm_output
Expand Down
7 changes: 7 additions & 0 deletions python/sglang/srt/layers/quantization/mxfp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,10 @@ def apply(
if self._fi_kernel == "cutlass_sm120":
return self._apply_sm120_cutlass(layer, dispatch_output)
if self.use_flashinfer:
from sglang.srt.layers.moe.moe_runner.flashinfer_trtllm import (
trtllm_moe_enable_pdl,
)

# When bf16 mode is enabled, we don't need to quantize the input,
# TRT-LLM automatically handles quantization in the kernel implementation and pipelines it with GEMM operations,
# which can theoretically improve performance
Expand Down Expand Up @@ -1591,6 +1595,7 @@ def apply(
tune_max_num_tokens=next_power_of_2(x_quant.shape[0]),
output=symm_output,
do_finalize=not defer_finalize,
enable_pdl=trtllm_moe_enable_pdl(x_quant.shape[0]),
)
if defer_finalize:
from sglang.srt.layers.moe.moe_runner.flashinfer_trtllm import (
Expand Down Expand Up @@ -1650,6 +1655,7 @@ def apply(
local_num_experts=layer.num_local_experts,
tune_max_num_tokens=next_power_of_2(x_quant.shape[0]),
output=symm_output,
enable_pdl=trtllm_moe_enable_pdl(x_quant.shape[0]),
)
return StandardCombineInput(hidden_states=symm_output)

Expand Down Expand Up @@ -1682,6 +1688,7 @@ def apply(
True, # do finalize
tune_max_num_tokens=next_power_of_2(x_quant.shape[0]),
output=symm_output,
enable_pdl=trtllm_moe_enable_pdl(x_quant.shape[0]),
)[0]
return StandardCombineInput(hidden_states=trtllm_gen_output)
if _use_aiter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ def apply(
else:
raise NotImplementedError(f"Unsupported mxfp4 moe precision: {precision}")

from sglang.srt.layers.moe.moe_runner.flashinfer_trtllm import (
trtllm_moe_enable_pdl,
)

with use_symmetric_memory(
get_tp_group(), disabled=not is_allocation_symmetric()
):
Expand Down Expand Up @@ -361,6 +365,7 @@ def apply(
do_finalize=True,
tune_max_num_tokens=next_power_of_2(x_quant.shape[0]),
output=symm_output,
enable_pdl=trtllm_moe_enable_pdl(num_tokens),
)[0]

return StandardCombineInput(hidden_states=output)
Expand Down
Loading