Skip to content
Merged
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
23 changes: 23 additions & 0 deletions python/sglang/srt/layers/moe/topk.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@
if _is_cuda:
from sgl_kernel import kimi_k2_moe_fused_gate, moe_fused_gate

@torch.library.register_fake("sgl_kernel::kimi_k2_moe_fused_gate")
def _kimi_k2_moe_fused_gate(
input_tensor,
bias,
topk,
renormalize,
routed_scaling_factor,
apply_routed_scaling_factor_on_output,
):
num_rows = input_tensor.shape[0]
topk_weights = input_tensor.new_empty(
num_rows,
topk,
dtype=torch.float32,
)
topk_ids = input_tensor.new_empty(
num_rows,
topk,
dtype=torch.int32,
Comment on lines +87 to +95
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

[nitpick] The tensor creation pattern is inconsistent with the existing moe_fused_gate fake implementation (lines 1072-1077). The existing implementation uses torch.empty((num_rows, topk), ...) with an explicit device=input_tensor.device parameter, while this uses input_tensor.new_empty(num_rows, topk, ...). For consistency and clarity, consider using the same pattern: torch.empty((num_rows, topk), dtype=torch.float32, device=input_tensor.device).

Suggested change
topk_weights = input_tensor.new_empty(
num_rows,
topk,
dtype=torch.float32,
)
topk_ids = input_tensor.new_empty(
num_rows,
topk,
dtype=torch.int32,
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,

Copilot uses AI. Check for mistakes.
)
return topk_weights, topk_ids


if _is_cuda or _is_hip:
from sgl_kernel import topk_softmax
if _use_aiter:
Expand Down
Loading