Use torch top-k routing fallback
Browse files
zonos2/layers/moe/fused_moe/topk.py
CHANGED
|
@@ -1,14 +1,7 @@
|
|
| 1 |
-
from typing import
|
| 2 |
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
try:
|
| 6 |
-
from sgl_kernel import topk_softmax
|
| 7 |
-
except ImportError:
|
| 8 |
-
|
| 9 |
-
def topk_softmax(*args: Any, **kwargs: Any) -> Any:
|
| 10 |
-
raise NotImplementedError("sgl_kernel not found.")
|
| 11 |
-
|
| 12 |
|
| 13 |
def fused_topk(
|
| 14 |
hidden_states: torch.Tensor,
|
|
@@ -21,15 +14,9 @@ def fused_topk(
|
|
| 21 |
|
| 22 |
M, _ = hidden_states.shape
|
| 23 |
|
| 24 |
-
|
| 25 |
-
topk_ids = torch.
|
| 26 |
-
|
| 27 |
-
topk_softmax(
|
| 28 |
-
topk_weights,
|
| 29 |
-
topk_ids,
|
| 30 |
-
gating_output.float(),
|
| 31 |
-
renormalize,
|
| 32 |
-
)
|
| 33 |
|
| 34 |
return _fused_topk_postprocess(
|
| 35 |
topk_weights=topk_weights,
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def fused_topk(
|
| 7 |
hidden_states: torch.Tensor,
|
|
|
|
| 14 |
|
| 15 |
M, _ = hidden_states.shape
|
| 16 |
|
| 17 |
+
scores = torch.softmax(gating_output.float(), dim=-1)
|
| 18 |
+
topk_weights, topk_ids = torch.topk(scores, k=topk, dim=-1)
|
| 19 |
+
topk_ids = topk_ids.to(dtype=torch.int32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
return _fused_topk_postprocess(
|
| 22 |
topk_weights=topk_weights,
|