Instructions to use replicate/moe-new-models with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/moe-new-models with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/moe-new-models", version=1) - Notebooks
- Google Colab
- Kaggle
Build
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- build/torch25-cxx11-cu118-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx11-cu118-x86_64-linux/moe/{_moe_2f2wzwk42r5t2.abi3.so → _moe_3c64348.abi3.so} +2 -2
- build/torch25-cxx11-cu118-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx11-cu118-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx11-cu118-x86_64-linux/moe/layers.py +51 -0
- build/torch25-cxx11-cu121-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx11-cu121-x86_64-linux/moe/{_moe_qx7m4hiw6tx7s.abi3.so → _moe_3c64348.abi3.so} +2 -2
- build/torch25-cxx11-cu121-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx11-cu121-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx11-cu121-x86_64-linux/moe/layers.py +51 -0
- build/torch25-cxx11-cu124-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx11-cu124-x86_64-linux/moe/{_moe_ctvji3e7dq64w.abi3.so → _moe_3c64348.abi3.so} +2 -2
- build/torch25-cxx11-cu124-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx11-cu124-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx11-cu124-x86_64-linux/moe/layers.py +51 -0
- build/torch25-cxx98-cu118-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx98-cu118-x86_64-linux/moe/{_moe_2dhx2xkm6c5wu.abi3.so → _moe_3c64348.abi3.so} +2 -2
- build/torch25-cxx98-cu118-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx98-cu118-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx98-cu118-x86_64-linux/moe/layers.py +51 -0
- build/torch25-cxx98-cu121-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx98-cu121-x86_64-linux/moe/_moe_3c64348.abi3.so +3 -0
- build/torch25-cxx98-cu121-x86_64-linux/moe/_moe_wfwoejktckaue.abi3.so +0 -3
- build/torch25-cxx98-cu121-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx98-cu121-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx98-cu121-x86_64-linux/moe/layers.py +51 -0
- build/torch25-cxx98-cu124-x86_64-linux/moe/__init__.py +2 -0
- build/torch25-cxx98-cu124-x86_64-linux/moe/_moe_3c64348.abi3.so +3 -0
- build/torch25-cxx98-cu124-x86_64-linux/moe/_moe_plhnk6yxrdq3c.abi3.so +0 -3
- build/torch25-cxx98-cu124-x86_64-linux/moe/_ops.py +3 -3
- build/torch25-cxx98-cu124-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch25-cxx98-cu124-x86_64-linux/moe/layers.py +51 -0
- build/torch26-cxx11-cu118-x86_64-linux/moe/__init__.py +2 -0
- build/torch26-cxx11-cu118-x86_64-linux/moe/_moe_3c64348.abi3.so +3 -0
- build/torch26-cxx11-cu118-x86_64-linux/moe/_moe_mrhezivmofzdg.abi3.so +0 -3
- build/torch26-cxx11-cu118-x86_64-linux/moe/_ops.py +3 -3
- build/torch26-cxx11-cu118-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch26-cxx11-cu118-x86_64-linux/moe/layers.py +51 -0
- build/torch26-cxx11-cu124-x86_64-linux/moe/__init__.py +2 -0
- build/torch26-cxx11-cu124-x86_64-linux/moe/_moe_3c64348.abi3.so +3 -0
- build/torch26-cxx11-cu124-x86_64-linux/moe/_moe_lvqy7x44edhqo.abi3.so +0 -3
- build/torch26-cxx11-cu124-x86_64-linux/moe/_ops.py +3 -3
- build/torch26-cxx11-cu124-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch26-cxx11-cu124-x86_64-linux/moe/layers.py +51 -0
- build/torch26-cxx11-cu126-x86_64-linux/moe/__init__.py +2 -0
- build/torch26-cxx11-cu126-x86_64-linux/moe/_moe_3c64348.abi3.so +3 -0
- build/torch26-cxx11-cu126-x86_64-linux/moe/_moe_gasoel7noy6kw.abi3.so +0 -3
- build/torch26-cxx11-cu126-x86_64-linux/moe/_ops.py +3 -3
- build/torch26-cxx11-cu126-x86_64-linux/moe/fused_moe.py +78 -63
- build/torch26-cxx11-cu126-x86_64-linux/moe/layers.py +51 -0
build/torch25-cxx11-cu118-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx11-cu118-x86_64-linux/moe/{_moe_2f2wzwk42r5t2.abi3.so → _moe_3c64348.abi3.so}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8c3f7e77fec716ceef6ecbc561aca6c77357f00fd24b17b4f285249fda0422e8
|
| 3 |
+
size 85827648
|
build/torch25-cxx11-cu118-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx11-cu118-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx11-cu118-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch25-cxx11-cu121-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx11-cu121-x86_64-linux/moe/{_moe_qx7m4hiw6tx7s.abi3.so → _moe_3c64348.abi3.so}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6a8d550f099a536818532aa332a83a9d9eb106cb7431ed29f19bb29b9c2d269e
|
| 3 |
+
size 86034480
|
build/torch25-cxx11-cu121-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx11-cu121-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx11-cu121-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch25-cxx11-cu124-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx11-cu124-x86_64-linux/moe/{_moe_ctvji3e7dq64w.abi3.so → _moe_3c64348.abi3.so}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37f4cdf7ef050df0b438f7af05046fa52512ef38f5dcde712d60c22d41d6ea41
|
| 3 |
+
size 85732952
|
build/torch25-cxx11-cu124-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx11-cu124-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx11-cu124-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch25-cxx98-cu118-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx98-cu118-x86_64-linux/moe/{_moe_2dhx2xkm6c5wu.abi3.so → _moe_3c64348.abi3.so}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d9d6b4f6ae4102cc11e5c9bdb467967f8ca4a62ab700383d2e76d0457871e991
|
| 3 |
+
size 85819488
|
build/torch25-cxx98-cu118-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx98-cu118-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx98-cu118-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch25-cxx98-cu121-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx98-cu121-x86_64-linux/moe/_moe_3c64348.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3e6fd1a8e77c52f0172df63ed906939c4e42bb265086ca09bedd95387c48cabf
|
| 3 |
+
size 86022600
|
build/torch25-cxx98-cu121-x86_64-linux/moe/_moe_wfwoejktckaue.abi3.so
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:e52b7dea31f1c9ee0c6ff81e5eb66f794d8a6475eb145e41f80974a73d80a331
|
| 3 |
-
size 86026776
|
|
|
|
|
|
|
|
|
|
|
|
build/torch25-cxx98-cu121-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx98-cu121-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx98-cu121-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch25-cxx98-cu124-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch25-cxx98-cu124-x86_64-linux/moe/_moe_3c64348.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fe62c9a85c408d659de5405bb1663ccac5e23a36ec541fcb8d38580449f03a2c
|
| 3 |
+
size 85733368
|
build/torch25-cxx98-cu124-x86_64-linux/moe/_moe_plhnk6yxrdq3c.abi3.so
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:2ea126bf0453d6f6fe7320362d147687ef554aacc37884ec5951ebc19eafe0c6
|
| 3 |
-
size 85729320
|
|
|
|
|
|
|
|
|
|
|
|
build/torch25-cxx98-cu124-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch25-cxx98-cu124-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch25-cxx98-cu124-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch26-cxx11-cu118-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch26-cxx11-cu118-x86_64-linux/moe/_moe_3c64348.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5295d92c2a0607ce6f21057ec3381ade69c7997003aa881dd9e1fa76545b05b9
|
| 3 |
+
size 85827824
|
build/torch26-cxx11-cu118-x86_64-linux/moe/_moe_mrhezivmofzdg.abi3.so
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:0ce59ec9d04ecd293feb8e7d4d2d637c441ecbdcec6127e2ab7fba9c868d1ba5
|
| 3 |
-
size 85827872
|
|
|
|
|
|
|
|
|
|
|
|
build/torch26-cxx11-cu118-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch26-cxx11-cu118-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch26-cxx11-cu118-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch26-cxx11-cu124-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch26-cxx11-cu124-x86_64-linux/moe/_moe_3c64348.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8cc5a6e9baefba09d38e9ae13d21a3fd2106f0a8e18514edd7e8331f46efe4e
|
| 3 |
+
size 85737352
|
build/torch26-cxx11-cu124-x86_64-linux/moe/_moe_lvqy7x44edhqo.abi3.so
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:cc1e383aa51ecc99476634640f580e7b5d89aadc33222c7f428c40a86149b3b5
|
| 3 |
-
size 85733304
|
|
|
|
|
|
|
|
|
|
|
|
build/torch26-cxx11-cu124-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch26-cxx11-cu124-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch26-cxx11-cu124-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|
build/torch26-cxx11-cu126-x86_64-linux/moe/__init__.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
|
|
|
|
| 3 |
from ._ops import ops
|
| 4 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 5 |
from .fused_marlin_moe import fused_marlin_moe
|
|
@@ -87,6 +88,7 @@ __all__ = [
|
|
| 87 |
"fused_topk",
|
| 88 |
"gptq_marlin_moe_repack",
|
| 89 |
"grouped_topk",
|
|
|
|
| 90 |
"moe_align_block_size",
|
| 91 |
"moe_sum",
|
| 92 |
"per_token_group_quant_fp8",
|
|
|
|
| 1 |
import torch
|
| 2 |
|
| 3 |
+
from . import layers
|
| 4 |
from ._ops import ops
|
| 5 |
from .fp8_utils import per_token_group_quant_fp8, w8a8_block_fp8_matmul
|
| 6 |
from .fused_marlin_moe import fused_marlin_moe
|
|
|
|
| 88 |
"fused_topk",
|
| 89 |
"gptq_marlin_moe_repack",
|
| 90 |
"grouped_topk",
|
| 91 |
+
"layers",
|
| 92 |
"moe_align_block_size",
|
| 93 |
"moe_sum",
|
| 94 |
"per_token_group_quant_fp8",
|
build/torch26-cxx11-cu126-x86_64-linux/moe/_moe_3c64348.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:93a582eef89b93f65dd25a6e17d1c9cf5fec111eb40ade61159d873081df45c3
|
| 3 |
+
size 86192272
|
build/torch26-cxx11-cu126-x86_64-linux/moe/_moe_gasoel7noy6kw.abi3.so
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:71784baa3bf781a0d915f0086fee151817a463d1d96879a7dc2e832b21fb1d8c
|
| 3 |
-
size 86192352
|
|
|
|
|
|
|
|
|
|
|
|
build/torch26-cxx11-cu126-x86_64-linux/moe/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _moe_3c64348
|
| 3 |
+
ops = torch.ops._moe_3c64348
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_moe_3c64348::{op_name}"
|
build/torch26-cxx11-cu126-x86_64-linux/moe/fused_moe.py
CHANGED
|
@@ -1022,6 +1022,7 @@ def inplace_fused_experts(
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
|
|
|
| 1025 |
use_fp8_w8a8: bool = False,
|
| 1026 |
use_int8_w8a16: bool = False,
|
| 1027 |
use_int4_w4a16: bool = False,
|
|
@@ -1034,22 +1035,23 @@ def inplace_fused_experts(
|
|
| 1034 |
block_shape: Optional[List[int]] = None,
|
| 1035 |
) -> None:
|
| 1036 |
fused_experts_impl(
|
| 1037 |
-
hidden_states,
|
| 1038 |
-
w1,
|
| 1039 |
-
w2,
|
| 1040 |
-
topk_weights,
|
| 1041 |
-
topk_ids,
|
| 1042 |
-
True,
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
|
|
@@ -1059,6 +1061,7 @@ def outplace_fused_experts(
|
|
| 1059 |
w2: torch.Tensor,
|
| 1060 |
topk_weights: torch.Tensor,
|
| 1061 |
topk_ids: torch.Tensor,
|
|
|
|
| 1062 |
use_fp8_w8a8: bool = False,
|
| 1063 |
use_int8_w8a16: bool = False,
|
| 1064 |
use_int4_w4a16: bool = False,
|
|
@@ -1071,22 +1074,23 @@ def outplace_fused_experts(
|
|
| 1071 |
block_shape: Optional[List[int]] = None,
|
| 1072 |
) -> torch.Tensor:
|
| 1073 |
return fused_experts_impl(
|
| 1074 |
-
hidden_states,
|
| 1075 |
-
w1,
|
| 1076 |
-
w2,
|
| 1077 |
-
topk_weights,
|
| 1078 |
-
topk_ids,
|
| 1079 |
-
False,
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
|
|
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
|
|
@@ -1097,6 +1101,7 @@ def fused_experts(
|
|
| 1097 |
topk_weights: torch.Tensor,
|
| 1098 |
topk_ids: torch.Tensor,
|
| 1099 |
inplace: bool = False,
|
|
|
|
| 1100 |
use_fp8_w8a8: bool = False,
|
| 1101 |
use_int8_w8a16: bool = False,
|
| 1102 |
use_int4_w4a16: bool = False,
|
|
@@ -1110,40 +1115,42 @@ def fused_experts(
|
|
| 1110 |
):
|
| 1111 |
if inplace:
|
| 1112 |
inplace_fused_experts(
|
| 1113 |
-
hidden_states,
|
| 1114 |
-
w1,
|
| 1115 |
-
w2,
|
| 1116 |
-
topk_weights,
|
| 1117 |
-
topk_ids,
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
|
|
|
| 1128 |
)
|
| 1129 |
return hidden_states
|
| 1130 |
else:
|
| 1131 |
return outplace_fused_experts(
|
| 1132 |
-
hidden_states,
|
| 1133 |
-
w1,
|
| 1134 |
-
w2,
|
| 1135 |
-
topk_weights,
|
| 1136 |
-
topk_ids,
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
| 1147 |
)
|
| 1148 |
|
| 1149 |
|
|
@@ -1154,6 +1161,7 @@ def fused_experts_impl(
|
|
| 1154 |
topk_weights: torch.Tensor,
|
| 1155 |
topk_ids: torch.Tensor,
|
| 1156 |
inplace: bool = False,
|
|
|
|
| 1157 |
use_fp8_w8a8: bool = False,
|
| 1158 |
use_int8_w8a16: bool = False,
|
| 1159 |
use_int4_w4a16: bool = False,
|
|
@@ -1281,7 +1289,12 @@ def fused_experts_impl(
|
|
| 1281 |
block_shape=block_shape,
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1285 |
|
| 1286 |
invoke_fused_moe_kernel(
|
| 1287 |
intermediate_cache2,
|
|
@@ -1320,6 +1333,7 @@ def fused_moe(
|
|
| 1320 |
topk: int,
|
| 1321 |
renormalize: bool,
|
| 1322 |
inplace: bool = False,
|
|
|
|
| 1323 |
use_grouped_topk: bool = False,
|
| 1324 |
num_expert_group: Optional[int] = None,
|
| 1325 |
topk_group: Optional[int] = None,
|
|
@@ -1404,6 +1418,7 @@ def fused_moe(
|
|
| 1404 |
topk_weights,
|
| 1405 |
topk_ids,
|
| 1406 |
inplace=inplace,
|
|
|
|
| 1407 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1408 |
use_int8_w8a16=use_int8_w8a16,
|
| 1409 |
use_int4_w4a16=use_int4_w4a16,
|
|
|
|
| 1022 |
w2: torch.Tensor,
|
| 1023 |
topk_weights: torch.Tensor,
|
| 1024 |
topk_ids: torch.Tensor,
|
| 1025 |
+
activation: str = "silu",
|
| 1026 |
use_fp8_w8a8: bool = False,
|
| 1027 |
use_int8_w8a16: bool = False,
|
| 1028 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1035 |
block_shape: Optional[List[int]] = None,
|
| 1036 |
) -> None:
|
| 1037 |
fused_experts_impl(
|
| 1038 |
+
hidden_states=hidden_states,
|
| 1039 |
+
w1=w1,
|
| 1040 |
+
w2=w2,
|
| 1041 |
+
topk_weights=topk_weights,
|
| 1042 |
+
topk_ids=topk_ids,
|
| 1043 |
+
inplace=True,
|
| 1044 |
+
activation=activation,
|
| 1045 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1046 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1047 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1048 |
+
w1_scale=w1_scale,
|
| 1049 |
+
w2_scale=w2_scale,
|
| 1050 |
+
w1_zp=w1_zp,
|
| 1051 |
+
w2_zp=w2_zp,
|
| 1052 |
+
a1_scale=a1_scale,
|
| 1053 |
+
a2_scale=a2_scale,
|
| 1054 |
+
block_shape=block_shape,
|
| 1055 |
)
|
| 1056 |
|
| 1057 |
|
|
|
|
| 1061 |
w2: torch.Tensor,
|
| 1062 |
topk_weights: torch.Tensor,
|
| 1063 |
topk_ids: torch.Tensor,
|
| 1064 |
+
activation: str = "silu",
|
| 1065 |
use_fp8_w8a8: bool = False,
|
| 1066 |
use_int8_w8a16: bool = False,
|
| 1067 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1074 |
block_shape: Optional[List[int]] = None,
|
| 1075 |
) -> torch.Tensor:
|
| 1076 |
return fused_experts_impl(
|
| 1077 |
+
hidden_states=hidden_states,
|
| 1078 |
+
w1=w1,
|
| 1079 |
+
w2=w2,
|
| 1080 |
+
topk_weights=topk_weights,
|
| 1081 |
+
topk_ids=topk_ids,
|
| 1082 |
+
inplace=False,
|
| 1083 |
+
activation=activation,
|
| 1084 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1085 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1086 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1087 |
+
w1_scale=w1_scale,
|
| 1088 |
+
w2_scale=w2_scale,
|
| 1089 |
+
w1_zp=w1_zp,
|
| 1090 |
+
w2_zp=w2_zp,
|
| 1091 |
+
a1_scale=a1_scale,
|
| 1092 |
+
a2_scale=a2_scale,
|
| 1093 |
+
block_shape=block_shape,
|
| 1094 |
)
|
| 1095 |
|
| 1096 |
|
|
|
|
| 1101 |
topk_weights: torch.Tensor,
|
| 1102 |
topk_ids: torch.Tensor,
|
| 1103 |
inplace: bool = False,
|
| 1104 |
+
activation: str = "silu",
|
| 1105 |
use_fp8_w8a8: bool = False,
|
| 1106 |
use_int8_w8a16: bool = False,
|
| 1107 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1115 |
):
|
| 1116 |
if inplace:
|
| 1117 |
inplace_fused_experts(
|
| 1118 |
+
hidden_states=hidden_states,
|
| 1119 |
+
w1=w1,
|
| 1120 |
+
w2=w2,
|
| 1121 |
+
topk_weights=topk_weights,
|
| 1122 |
+
topk_ids=topk_ids,
|
| 1123 |
+
activation=activation,
|
| 1124 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1125 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1126 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1127 |
+
w1_scale=w1_scale,
|
| 1128 |
+
w2_scale=w2_scale,
|
| 1129 |
+
w1_zp=w1_zp,
|
| 1130 |
+
w2_zp=w2_zp,
|
| 1131 |
+
a1_scale=a1_scale,
|
| 1132 |
+
a2_scale=a2_scale,
|
| 1133 |
+
block_shape=block_shape,
|
| 1134 |
)
|
| 1135 |
return hidden_states
|
| 1136 |
else:
|
| 1137 |
return outplace_fused_experts(
|
| 1138 |
+
hidden_states=hidden_states,
|
| 1139 |
+
w1=w1,
|
| 1140 |
+
w2=w2,
|
| 1141 |
+
topk_weights=topk_weights,
|
| 1142 |
+
topk_ids=topk_ids,
|
| 1143 |
+
activation=activation,
|
| 1144 |
+
use_fp8_w8a8=use_fp8_w8a8,
|
| 1145 |
+
use_int8_w8a16=use_int8_w8a16,
|
| 1146 |
+
use_int4_w4a16=use_int4_w4a16,
|
| 1147 |
+
w1_scale=w1_scale,
|
| 1148 |
+
w2_scale=w2_scale,
|
| 1149 |
+
w1_zp=w1_zp,
|
| 1150 |
+
w2_zp=w2_zp,
|
| 1151 |
+
a1_scale=a1_scale,
|
| 1152 |
+
a2_scale=a2_scale,
|
| 1153 |
+
block_shape=block_shape,
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
|
|
|
|
| 1161 |
topk_weights: torch.Tensor,
|
| 1162 |
topk_ids: torch.Tensor,
|
| 1163 |
inplace: bool = False,
|
| 1164 |
+
activation: str = "silu",
|
| 1165 |
use_fp8_w8a8: bool = False,
|
| 1166 |
use_int8_w8a16: bool = False,
|
| 1167 |
use_int4_w4a16: bool = False,
|
|
|
|
| 1289 |
block_shape=block_shape,
|
| 1290 |
)
|
| 1291 |
|
| 1292 |
+
if activation == "silu":
|
| 1293 |
+
ops.silu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1294 |
+
elif activation == "gelu":
|
| 1295 |
+
ops.gelu_and_mul(intermediate_cache2, intermediate_cache1.view(-1, N))
|
| 1296 |
+
else:
|
| 1297 |
+
raise ValueError(f"Unsupported FusedMoe activation: {activation}")
|
| 1298 |
|
| 1299 |
invoke_fused_moe_kernel(
|
| 1300 |
intermediate_cache2,
|
|
|
|
| 1333 |
topk: int,
|
| 1334 |
renormalize: bool,
|
| 1335 |
inplace: bool = False,
|
| 1336 |
+
activation: str = "silu",
|
| 1337 |
use_grouped_topk: bool = False,
|
| 1338 |
num_expert_group: Optional[int] = None,
|
| 1339 |
topk_group: Optional[int] = None,
|
|
|
|
| 1418 |
topk_weights,
|
| 1419 |
topk_ids,
|
| 1420 |
inplace=inplace,
|
| 1421 |
+
activation=activation,
|
| 1422 |
use_fp8_w8a8=use_fp8_w8a8,
|
| 1423 |
use_int8_w8a16=use_int8_w8a16,
|
| 1424 |
use_int4_w4a16=use_int4_w4a16,
|
build/torch26-cxx11-cu126-x86_64-linux/moe/layers.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .fused_moe import fused_moe
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _llama4_topk(
|
| 8 |
+
hidden_states: torch.Tensor,
|
| 9 |
+
gating_output: torch.Tensor,
|
| 10 |
+
topk: int,
|
| 11 |
+
renormalize: bool,
|
| 12 |
+
):
|
| 13 |
+
assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch"
|
| 14 |
+
topk_weights, topk_ids = torch.topk(gating_output, topk, dim=1)
|
| 15 |
+
topk_weights = torch.sigmoid(topk_weights.float()).to(hidden_states.dtype)
|
| 16 |
+
return topk_weights, topk_ids
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _fix_llama4_experts(hidden_states: torch.Tensor, experts: nn.Module):
|
| 20 |
+
if experts.gate_up_proj.shape[-1] != hidden_states.shape[-1]:
|
| 21 |
+
experts.gate_up_proj = nn.Parameter(
|
| 22 |
+
experts.gate_up_proj.transpose(1, 2).contiguous()
|
| 23 |
+
)
|
| 24 |
+
experts.down_proj = nn.Parameter(experts.down_proj.transpose(1, 2).contiguous())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Llama4TextMoe(nn.Module):
|
| 28 |
+
experts: nn.Module
|
| 29 |
+
router: nn.Linear
|
| 30 |
+
shared_expert: nn.Module
|
| 31 |
+
top_k: int
|
| 32 |
+
|
| 33 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
hidden_states = hidden_states.reshape(-1, hidden_states.shape[-1])
|
| 35 |
+
|
| 36 |
+
_fix_llama4_experts(hidden_states, self.experts)
|
| 37 |
+
|
| 38 |
+
router_logits = self.router(hidden_states)
|
| 39 |
+
out = fused_moe(
|
| 40 |
+
hidden_states,
|
| 41 |
+
w1=self.experts.gate_up_proj,
|
| 42 |
+
w2=self.experts.down_proj,
|
| 43 |
+
gating_output=router_logits,
|
| 44 |
+
topk=self.top_k,
|
| 45 |
+
renormalize=False,
|
| 46 |
+
custom_routing_function=_llama4_topk,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
out += self.shared_expert(hidden_states)
|
| 50 |
+
|
| 51 |
+
return out, router_logits.t()
|