support xverse_moe on npu

Co-authored-by: sglang-npu-bot <sglangnpu@163.com>
This commit is contained in:
fy
2026-02-24 14:11:20 +08:00
committed by GitHub
co-authored by sglang-npu-bot
parent 5e80027ac7
commit 4f25a48d7a
2 changed files with 85 additions and 2 deletions
@@ -139,6 +139,85 @@ def npu_fused_moe_without_routing_weights_bf16(
return hidden_states
def fused_moe_npu(
x,
w1,
w2,
topk_output,
moe_runner_config,
):
# TODO: reuse the codes of UnquantizedFusedMoEMethod-forward_npu
topk_weights, topk_ids, _ = topk_output
original_dtype = x.dtype
num_tokens = x.shape[0]
topk_weights = topk_weights.to(x.dtype)
topk_ids = topk_ids.to(torch.int32)
num_experts = w1.shape[0]
top_k = topk_weights.shape[-1]
row_idx_len = num_tokens * top_k
row_idx = (
torch.arange(0, row_idx_len, dtype=torch.int32, device=topk_weights.device)
.view(top_k, -1)
.permute(1, 0)
.contiguous()
)
hidden_states, expanded_row_idx, expanded_expert_idx = (
torch.ops.npu.npu_moe_init_routing(
x, row_idx=row_idx, expert_idx=topk_ids, active_num=num_tokens
)
)
expert_tokens = torch.ops.npu.npu_moe_compute_expert_tokens(
expanded_expert_idx, num_experts
)
expert_tokens = expert_tokens.to(torch.int64)
# gmm1: gate_up_proj
hidden_states = torch.ops.npu.npu_grouped_matmul(
x=[hidden_states],
weight=[w1.permute(0, 2, 1)],
bias=None,
split_item=2,
group_list_type=0,
group_type=0,
group_list=expert_tokens,
output_dtype=original_dtype,
)[0]
# act_fn:
if moe_runner_config.activation == "silu":
hidden_states = torch.ops.npu.npu_swiglu(hidden_states)
else:
from sglang.srt.layers.activation import GeluAndMul
hidden_states = GeluAndMul()(hidden_states)
# gmm2: down_proj
hidden_states = torch.ops.npu.npu_grouped_matmul(
x=[hidden_states],
weight=[w2.permute(0, 2, 1)],
bias=None,
split_item=2,
group_list_type=0,
group_type=0,
group_list=expert_tokens,
output_dtype=original_dtype,
)[0]
final_hidden_states = torch.ops.npu.npu_moe_finalize_routing(
hidden_states,
skip1=None,
skip2=None,
bias=None,
scales=topk_weights,
expanded_src_to_dst_row=expanded_row_idx,
export_for_source_row=topk_ids,
)
return final_hidden_states
class _NPUFusedMoEMethodBase(FusedMoEMethodBase):
def __init__(