-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
88 changed files
with
338 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Torch-native implementation for FusedMoE. This is used for torch.compile. | ||
It is based on https://github.com/pytorch-labs/gpt-fast/blob/32971d3129541c5bfb4f715abc33d1c5f408d204/mixtral-moe/model.py#L204 | ||
""" | ||
|
||
from typing import Callable, Optional | ||
|
||
import torch | ||
from torch.nn import functional as F | ||
|
||
from sglang.srt.layers.moe.topk import select_experts | ||
|
||
|
||
def fused_moe_forward_native( | ||
layer: torch.nn.Module, | ||
x: torch.Tensor, | ||
use_grouped_topk: bool, | ||
top_k: int, | ||
router_logits: torch.Tensor, | ||
renormalize: bool, | ||
topk_group: Optional[int] = None, | ||
num_expert_group: Optional[int] = None, | ||
custom_routing_function: Optional[Callable] = None, | ||
correction_bias: Optional[torch.Tensor] = None, | ||
) -> torch.Tensor: | ||
topk_weights, topk_ids = select_experts( | ||
hidden_states=x, | ||
router_logits=router_logits, | ||
use_grouped_topk=use_grouped_topk, | ||
top_k=top_k, | ||
renormalize=renormalize, | ||
topk_group=topk_group, | ||
num_expert_group=num_expert_group, | ||
custom_routing_function=custom_routing_function, | ||
correction_bias=correction_bias, | ||
torch_native=True, | ||
) | ||
|
||
w13_weights = layer.w13_weight[topk_ids] | ||
w1_weights, w3_weights = torch.chunk(w13_weights, 2, dim=2) | ||
w2_weights = layer.w2_weight[topk_ids] | ||
x1 = torch.einsum("ti,taoi -> tao", x, w1_weights) | ||
x1 = F.silu(x1) | ||
x3 = torch.einsum("ti, taoi -> tao", x, w3_weights) | ||
expert_outs = torch.einsum("tao, taio -> tai", (x1 * x3), w2_weights) | ||
return torch.einsum("tai,ta -> ti", expert_outs, topk_weights.to(expert_outs.dtype)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.