vllm.model_executor.layers.fused_moe.modular_kernel ¶
ExpertTokensMetadata dataclass ¶
Metadata regarding expert-token routing.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEActivationFormat ¶
FusedMoEExperts ¶
Bases: ABC
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
expects_unquantized_inputs property ¶
expects_unquantized_inputs: bool
Whether or not the PrepareFinalize should defer input quantization in the prepare step. If True, then the Experts kernel will execute the input quantization itself.
Sample subclasses that override are AITER and FlashInfer CUTLASS.
__init__ ¶
__init__(
moe_config: FusedMoEConfig,
quant_config: FusedMoEQuantConfig,
max_num_tokens: int | None = None,
num_dispatchers: int | None = None,
)
moe_config: MoE layer configuration. quant_config: Quantization parameters for this experts instance.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_activation abstractmethod staticmethod ¶
_supports_activation(activation: MoEActivation) -> bool
Whether the kernel supports a particular act function.
_supports_current_device abstractmethod staticmethod ¶
_supports_current_device() -> bool
Whether the kernel supports the current device type (compute cability and current platform).
_supports_no_act_and_mul abstractmethod staticmethod ¶
_supports_no_act_and_mul() -> bool
Whether the kernel supports act_and_mul=False, i.e. non-gated MoE models like Nemotron-Nano.
_supports_parallel_config abstractmethod staticmethod ¶
_supports_parallel_config(
moe_parallel_config: FusedMoEParallelConfig,
) -> bool
Whether the kernel supports deployment in particular parallel config.
Can be overridden if a kernel does not support EP, SP or some other configuration.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_router_logits_dtype staticmethod ¶
_supports_router_logits_dtype(
router_logits_dtype: dtype | None,
routing_method: RoutingMethodType,
) -> bool
Whether a kernel supports a particular dtype for router logits input.
Can be overridden by monolithic kernels that execute the router in addition to the experts if certain dtypes are not supported.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_routing_method staticmethod ¶
_supports_routing_method(
routing_method: RoutingMethodType,
weight_key: QuantKey | None,
activation_key: QuantKey | None,
) -> bool
Whether the kernel supports a routing method (e.g. GroupedTopK).
Can be overridden by monolithic kernels that execute the router in addition to the experts if certain routers are not supported.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_shape staticmethod ¶
Whether a kernel supports a particular shape. Can be overridden if a kernel has specific shape requirements.
activation_format abstractmethod staticmethod ¶
activation_format() -> FusedMoEActivationFormat
A property which is a tuple of the input and output activation formats for the 'apply' method.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
supports_chunking abstractmethod ¶
supports_chunking() -> bool
A flag indicating whether or not this class supports activation chunking.
supports_expert_map abstractmethod ¶
supports_expert_map() -> bool
A flag indicating whether or not this class supports expert maps
FusedMoEExpertsModular ¶
Bases: FusedMoEExperts
An abstract base class for the [Permute-Experts-Unpermute] step described above.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 | |
adjust_N_for_activation staticmethod ¶
adjust_N_for_activation(
N: int, activation: MoEActivation
) -> int
Calculate the output dimension for the activation function.
For *_no_mul activations (e.g. relu2_no_mul), there's no gate/up split, so output size equals input size (N).
For regular gated activations (e.g., silu, gelu, swigluoai), output size is N // 2 due to gate × activation(up) multiplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N | int | The intermediate size (width of w1/w3 weights). | required |
activation | MoEActivation | The activation function enum. | required |
Returns:
| Type | Description |
|---|---|
int | The output dimension after activation. |
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
apply abstractmethod ¶
apply(
output: Tensor,
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
activation: MoEActivation,
global_num_experts: int,
expert_map: Tensor | None,
a1q_scale: Tensor | None,
a2_scale: Tensor | None,
workspace13: Tensor,
workspace2: Tensor,
expert_tokens_meta: ExpertTokensMetadata | None,
apply_router_weight_on_input: bool,
) -> None
This function computes the intermediate result of a Mixture of Experts (MoE) layer using two sets of weights, w1 and w2.
Parameters: - output: (torch.Tensor): The unweighted, unreduced output tensor. - hidden_states: (torch.Tensor): The (quantized) input tensor to the MoE layer. - w1 (torch.Tensor): The first set of expert weights. - w2 (torch.Tensor): The second set of expert weights. - topk_weights: A map of row to expert weights. Some implementations choose to do weight application. - topk_ids (torch.Tensor): A map of row to expert id. - activation (str): The activation function to apply after the first MoE layer. - global_num_experts (int): The total number of experts in the global expert space. - expert_map (Optional[torch.Tensor]): A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard. - a1q_scale (Optional[torch.Tensor]): Optional quantized scale to be used for a1. Result of quantization from prepare/finalize and not from the FusedMoEQuantConfig. - workspace13 (torch.Tensor): A scratch tensor used for gemm outputs must be large enough to hold output of either MoE gemm. - workspace2 (torch.Tensor): A scratch tensor used for the activation function. - expert_tokens_meta (Optional[ExpertTokensMetadata]) - An optional ExpertTokensMetadata object containing gpu/cpu tensors as big as the number of local experts with the information about the number of tokens assigned to each local expert. - apply_router_weight_on_input: True if router weights are already applied on the input. This is relevant if the implementation chooses to do weight application.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
moe_problem_size ¶
moe_problem_size(
a1: Tensor, w1: Tensor, w2: Tensor, topk_ids: Tensor
) -> tuple[int, int, int, int, int]
Extract the MoE problem size from the given tensor arguments: - a: The hidden states, input to the MoE layer. - w1: The first set of expert weights. - w2: The second set of expert weights. - topk_ids: The topk ids.
Note: extracting the problem shape from the weight and activation tensors is not obvious. It needs to be done this way specifically due to subtle issues with particular kernels, e.g. the int4 kernels divide the trailing dimension by two, so it's not "correct" to extract N or K from the trailing dimension of w1 or w2. Similarly, some kernels transpose the weights, so this needs to be kept in mind.
Note: This implementation covers most cases. However, if experts require a specialized implementation, like MarlinExperts, they are free to override this function.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
workspace_dtype ¶
workspace_shapes abstractmethod ¶
workspace_shapes(
M: int,
N: int,
K: int,
topk: int,
global_num_experts: int,
local_num_experts: int,
expert_tokens_meta: ExpertTokensMetadata | None,
activation: MoEActivation,
) -> tuple[
tuple[int, ...], tuple[int, ...], tuple[int, ...]
]
Compute the shapes for the temporary and final outputs of the two gemms and activation in the fused expert function. Since the gemms are independent, the workspace for the first gemm can be shared with the workspace for the last gemm.
Inputs: - M: number of tokens. - N: Row (or column) dimension of expert weights. - K: hidden dimension - topk: The number of top-k experts to select. - global_num_experts: global number of experts. - local_num_experts: local number of experts due to DP/EP. - expert_tokens_meta: number of tokens per expert metadata for batched format.
Returns a tuple of: - workspace13 shape tuple: must be large enough to hold the result of either expert gemm. - workspace2 shape tuple: must be large enough to hold the result of the activation function. - output shape tuple: must be exact size of the final gemm output. - Note: workspace shapes can be 0 if the workspace is not needed. But in order for activation chunking to work, the first dimension of each tuple must be the number of tokens when the shape is not 0.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEExpertsMonolithic ¶
Bases: FusedMoEExperts
An abstract base class for the [Permute-Experts-Unpermute] step described above, but with the monolithic interface (accepts router logits rather than topk ids and weights).
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_router_logits_dtype staticmethod ¶
_supports_router_logits_dtype(
router_logits_dtype: dtype | None,
routing_method: RoutingMethodType,
) -> bool
Whether the kernel supports a dtype for router logits.
Modular kernels should opt-in to support.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_supports_routing_method staticmethod ¶
_supports_routing_method(
routing_method: RoutingMethodType,
weight_key: QuantKey | None,
activation_key: QuantKey | None,
) -> bool
Whether the kernel supports a routing method (e.g. GroupedTopK).
Monolithic kernels should explicitly opt-in to support.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
apply ¶
apply(
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
router_logits: Tensor,
activation: MoEActivation,
global_num_experts: int,
expert_map: Tensor | None,
a1q_scale: Tensor | None,
apply_router_weight_on_input: bool,
num_expert_group: int | None = None,
e_score_correction_bias: Tensor | None = None,
routed_scaling_factor: float | None = None,
topk_group: int | None = None,
) -> Tensor
Same as apply(), except uses router_logits as opposed to the topk_ids and topk_weights. This is useful for kernels with fused router and fused_experts (e.g. FLASHINFER_TRTLLM).
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEKernel ¶
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 | |
_post_init_setup ¶
Resolve any leftover setup dependencies between self.prepare_finalize and self.fused_experts here.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
output_is_reduced ¶
output_is_reduced() -> bool
Indicates whether or not the output of fused MoE kernel is reduced across all ranks.
FusedMoEKernelModularImpl ¶
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 | |
_allocate_buffers ¶
_allocate_buffers(
out_dtype: dtype,
device: device,
M_chunk: int,
M_full: int,
N: int,
K: int,
top_k: int,
global_num_experts: int,
local_num_experts: int,
expert_tokens_meta: ExpertTokensMetadata | None,
activation: MoEActivation,
) -> tuple[Tensor, Tensor, Tensor]
Allocate temporary and output buffers for the fused experts op. Inputs: - out_dtype: output type of workspace and output tensors. - device: the device of the workspace and output tensors. See workspace_shapes for a description of the remainder of arguments. Returns a tuple of (workspace13, workspace2, output) tensors.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | |
_chunk_info ¶
Compute number of chunks and chunk size for given M. If chunking is not supported, set the CHUNK_SIZE to M so we get num_chunks == 1. Take max(M, 1) to avoid divide by zero. If there are no tokens to process, the number of chunks will be zero.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
_finalize ¶
_finalize(
output: Tensor,
fused_out: Tensor,
hidden_states: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
apply_router_weight_on_input: bool,
shared_experts_input: Tensor | None,
) -> Tensor | tuple[Tensor, Tensor]
The _finalize method is a wrapper around self.prepare_finalize.finalize that handles DBO, async and shared expert overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shared_experts_input | Tensor | None | Optional separate input for shared experts. When latent MoE is used, hidden_states is the latent-projected tensor (smaller dimension) used by routed experts, while shared_experts_input is the original hidden_states (full dimension) needed by the shared expert MLP. | required |
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
_prepare ¶
_prepare(
hidden_states: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
global_num_experts: int,
expert_map: Tensor | None,
apply_router_weight_on_input: bool,
) -> tuple[
Tensor,
Tensor | None,
ExpertTokensMetadata | None,
Tensor,
Tensor,
]
The _prepare method is a wrapper around self.prepare_finalize.prepare that handles DBO and async.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 | |
apply ¶
apply(
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
topk_ids: Tensor,
topk_weights: Tensor,
activation: MoEActivation = SILU,
global_num_experts: int = -1,
expert_map: Tensor | None = None,
apply_router_weight_on_input: bool = False,
shared_experts_input: Tensor | None = None,
) -> Tensor | tuple[Tensor, Tensor]
This function computes a Mixture of Experts (MoE) layer using two sets of weights, w1 and w2, and top-k gating mechanism.
Parameters: - hidden_states: (torch.Tensor): The input tensor to the MoE layer. - w1 (torch.Tensor): The first set of expert weights. - w2 (torch.Tensor): The second set of expert weights. - topk_weights (torch.Tensor): The topk weights applied at the end of the layer. - topk_ids (torch.Tensor): A map of row to expert id. - activation (MoEActivation): The activation function to apply after the first MoE layer. - global_num_experts (int): The total number of experts in the global expert space. - expert_map (Optional[torch.Tensor]): A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard. - apply_router_weight_on_input (bool): When true, the topk weights are applied directly on the inputs. This is only applicable when topk is 1. - shared_experts_input (Optional[torch.Tensor]): Optional separate input for shared experts. For latent MoE, this is the original hidden_states before latent projection.
Returns: - torch.Tensor: The output tensor after applying the MoE layer.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 | |
FusedMoEKernelMonolithicImpl ¶
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
apply ¶
apply(
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
router_logits: Tensor,
activation: MoEActivation,
global_num_experts: int,
expert_map: Tensor | None,
apply_router_weight_on_input: bool,
num_expert_group: int | None = None,
e_score_correction_bias: Tensor | None = None,
routed_scaling_factor: float | None = None,
topk_group: int | None = None,
) -> Tensor
Same as forward(), except uses router_logits as opposed to the topk_ids and topk_weights. This is used for kernels that have fused router + experts (e.g. FLASHINFER_TRTLLM).
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEPrepareAndFinalize ¶
Bases: ABC
An abstract base class for the [Quantize-Prepare] and [Finalize] steps described above.
There are two variants of this class: * FusedMoEPrepareAndFinalizeModular - this operates on topk ids and weights * FusedMoEPrepareAndFinalizeMonolithic - the operates on router_logits
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
activation_format abstractmethod property ¶
activation_format: FusedMoEActivationFormat
A property indicating the output format of the activations for the 'prepare' method.
max_num_tokens_per_rank abstractmethod ¶
max_num_tokens_per_rank() -> int | None
Some PrepareFinalize All2All implementations are batched. Meaning, they can process only as set of tokens at a time. This function returns the batch size i.e the maximum number of tokens the implementation can process at a time. Return None if there are no such restrictions.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
output_is_reduced abstractmethod ¶
output_is_reduced() -> bool
Indicates whether or not the output of finalize is reduced across all ranks.
post_init_setup ¶
post_init_setup(fused_experts: FusedMoEExperts)
Initialize FusedMoEPrepareAndFinalizeModular settings that depend on FusedMoEExpertsModular experts object. The FusedMoEPrepareAndFinalizeModular implementations that have such dependencies may choose to override this function.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
topk_indices_dtype abstractmethod ¶
topk_indices_dtype() -> dtype | None
The PrepareFinalize All2All implementations generally constrain the dtype of the topk_ids they support. This function returns the required topk indices dtype so it can be respected. Return None if there are no such restrictions.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEPrepareAndFinalizeModular ¶
Bases: FusedMoEPrepareAndFinalize
An abstract base class for the [Quantize-Prepare] and [Finalize] steps described above for the Modular case.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
finalize abstractmethod ¶
finalize(
output: Tensor,
fused_expert_output: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
apply_router_weight_on_input: bool,
weight_and_reduce_impl: TopKWeightAndReduce,
) -> None
Perform any combine plus apply weights and perform a reduction on the fused experts output. - output: The output tensor, written in place. Must be (M, K) shape. - fused_expert_output: The unweighted, unreduced output of the fused experts, it will have (M, topk, K) shape. - topk_weights: The weights to be applied to the fused_experts_output. - topk_ids: The topk_ids. - apply_router_weight_on_input: When False, apply the weights to fused_expert_output. - weight_and_reduce_impl: An optional TopKWeightAndReduce implementation.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
finalize_async ¶
finalize_async(
output: Tensor,
fused_expert_output: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
apply_router_weight_on_input: bool,
weight_and_reduce_impl: TopKWeightAndReduce,
) -> tuple[Callable, Callable] | Callable
Perform any combine plus apply weights and perform a reduction on the fused experts output but do not wait for results from other workers. - output: The output tensor, written in place. Must be (M, K) shape. - fused_expert_output: The unweighted, unreduced output of the fused experts, it will have (M, topk, K) shape. - topk_weights: The weights to be applied to the fused_experts_output. - topk_ids: The topk_ids. - apply_router_weight_on_input: When False, apply the weights to fused_expert_output. - weight_and_reduce_impl: An optional TopKWeightAndReduce implementation.
Returns a callback or a hook callback pair that when invoked waits for results from other workers and has the same return signature as finalize, if a hook is returned this is more lightweight check that the recv is complete without doing extra work (used by DBO, will be refactored in the very near future)
ret = obj.finalize_async(output, ...) ... output not valid yet ... if isinstance(ret, tuple): hook, receiver = ret hook() receiver() ... output valid here ...
is equivalent to:
obj.finalize(output, ...)
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
prepare abstractmethod ¶
prepare(
a1: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
num_experts: int,
expert_map: Tensor | None,
apply_router_weight_on_input: bool,
quant_config: FusedMoEQuantConfig,
defer_input_quant: bool,
) -> PrepareResultType
Perform any quantization (and/or) dispatching needed for this kernel. - a1: The (unquantized) input to the MoE layer. - topk_ids: The topk ids. - topk_weights: The topk weights. - num_experts: The total number of experts in the global expert space. - expert_map: A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard. - apply_router_weight_on_input: When True, apply the weights to the activations, before quantization + dispatching. - quant_config: Quantization info provided by the fused experts. - defer_input_quant: Runtime parameter indicating whether or not to defer input quantization to the FusedMoEExpertsModular in cases where the compute kernel expects unquantized inputs
Returns a tuple of: - quantized + dispatched a. - Optional quantized + dispatched a1_scales. - Optional ExpertTokensMetadata containing gpu/cpu tensors as big as the number of local experts with the information about the number of tokens assigned to each local expert. - Optional dispatched expert topk IDs - Optional dispatched expert topk weight
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
prepare_async ¶
prepare_async(
a1: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
num_experts: int,
expert_map: Tensor | None,
apply_router_weight_on_input: bool,
quant_config: FusedMoEQuantConfig,
defer_input_quant: bool,
) -> tuple[Callable, ReceiverType] | ReceiverType
Perform any quantization (and/or) dispatching needed for this kernel but do not wait for results from other workers. - a1: The (unquantized) input to the MoE layer. - a1_scale: Optional scales for a1 - a2_scale: Optional scales for the second MoE gemm. Required to make sure the quantization is consistent for both gemms. - topk_ids: The topk ids. - topk_weights: The topk weights. - num_experts: The total number of experts in the global expert space. - expert_map: A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard. - apply_router_weight_on_input: When True, apply the weights to the activations, before quantization + dispatching. - defer_input_quant: Runtime parameter indicating whether or not to defer input quantization to the FusedMoEExpertsModular in cases where the compute kernel expects unquantized inputs
Returns a callback or a hook callback pair that when invoked waits for results from other workers and has the same return signature as prepare, if a hook is returned this is more lightweight check that the recv is complete without doing extra work (used by DBO, will be refactored in the very near future)
e.g.
ret = obj.prepare_async(...)
if isinstance(ret, tuple): hook, receiver = ret hook()
if hook is not None: a, a_scales, expert_meta, topk_ids, topk_weights = receiver()
is equivalent to:
a, a_scales, expert_meta, topk_ids, topk_weights = obj.prepare(...)
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
FusedMoEPrepareAndFinalizeMonolithic ¶
Bases: FusedMoEPrepareAndFinalize
An abstract base class for the [Quantize-Prepare] and [Finalize] steps described above for the monolithic case.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
finalize abstractmethod ¶
Optional method for subclasses compatible with monolithic FusedMoEExpertsModular kernels.
Perform any combine plus apply weights and perform a reduction on the fused experts output. - fused_expert_output: The unweighted, unreduced output of the fused experts, it will have (M, topk, K) shape.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
prepare abstractmethod ¶
prepare(
a1: Tensor,
router_logits: Tensor,
quant_config: FusedMoEQuantConfig,
defer_input_quant: bool = False,
) -> PrepareMonolithicResultType
Optional method for subclasses compatible with monolithic FusedMoEExpertsModular kernels.
Perform any quantization (and/or) dispatching needed for this kernel. - a1: The (unquantized) input to the MoE layer. - quant_config: Quantization info provided by the fused experts. - defer_input_quant: Runtime parameter indicating whether or not to defer input quantization to the FusedMoEExpertsModular
Returns a tuple of: - quantized + dispatched a. - Optional quantized + dispatched a1_scales.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
TopKWeightAndReduce ¶
Bases: ABC
An abstract base class for weight application and reduction implementations.
Source code in vllm/model_executor/layers/fused_moe/modular_kernel.py
apply abstractmethod ¶
apply(
output: Tensor | None,
fused_expert_output: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
apply_router_weight_on_input: bool,
) -> Tensor
Apply topk_weights to the fused_experts_outputs and/or reduce. If an output tensor is not passed, it will be created in the function.