Skip to content
Merged
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
9 changes: 7 additions & 2 deletions python/sglang/srt/layers/moe/fused_moe_triton/fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,11 @@ def moe_align_block_size(
by block_size for proper block matrix operations.
"""
max_num_tokens_padded = topk_ids.numel() + num_experts * (block_size - 1)
sorted_ids, cumsum_buffer = init_sorted_ids_and_cumsum_buffer(
max_num_tokens_padded, topk_ids.numel(), num_experts, topk_ids.device
sorted_ids = torch.empty(
(max_num_tokens_padded,), dtype=torch.int32, device=topk_ids.device
)
sorted_ids.fill_(topk_ids.numel())
Comment on lines +753 to +756
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This change directly initializes sorted_ids and fills it, replacing a previous function call. This improves efficiency by avoiding an unnecessary kernel launch for initialization and enhances clarity by making the initialization explicit at the point of use.


max_num_m_blocks = triton.cdiv(max_num_tokens_padded, block_size)
expert_ids = torch.empty(
(max_num_m_blocks,), dtype=torch.int32, device=topk_ids.device
Expand All @@ -768,6 +770,9 @@ def moe_align_block_size(
num_tokens_post_pad,
)
else:
cumsum_buffer = torch.empty(
(num_experts + 1,), dtype=torch.int32, device=topk_ids.device
)
Comment on lines +773 to +775
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Moving the cumsum_buffer initialization into this conditional block is a good optimization. It ensures that the buffer is only allocated when it's actually needed, reducing memory overhead and improving performance in scenarios where enable_moe_align_block_size_triton is true.

token_cnts_buffer = torch.empty(
(num_experts + 1) * num_experts,
dtype=torch.int32,
Expand Down
Loading