vllm.model_executor.layers.fused_moe.cutlass_moe ¶
CUTLASS based Fused MoE kernels.
CutlassBatchedExpertsFp8 ¶
Bases: CutlassExpertsFp8Base
Batched CUTLASS FP8 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
CutlassExpertsFp4 ¶
Bases: FusedMoEPermuteExpertsUnpermute
CUTLASS FP4 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 749 750 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 | |
CutlassExpertsFp8 ¶
Bases: CutlassExpertsFp8Base
CUTLASS FP8 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
cutlass_moe_w4a8_fp8 ¶
cutlass_moe_w4a8_fp8(
a: Tensor,
w1_q: Tensor,
w2_q: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
a_strides1: Tensor,
a_strides2: Tensor,
b_strides1: Tensor,
b_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
s_strides1: Tensor,
s_strides2: Tensor,
quant_config: FusedMoEQuantConfig,
moe_config: FusedMoEConfig,
activation: MoEActivation = SILU,
expert_map: Tensor | None = None,
apply_router_weight_on_input: bool = False,
global_num_experts: int = -1,
group_size: int = 128,
) -> Tensor
This function computes a w4a8-quantized Mixture of Experts (MoE) layer using two sets of quantized weights, w1_q and w2_q, and top-k gating mechanism. The matrix multiplications are implemented with CUTLASS mixed-dtype grouped gemm.
- a (torch.Tensor): The input tensor to the MoE layer. Shape: [M, K]
- w1_q (torch.Tensor): The first set of fp8-quantized expert weights. Shape: [num_experts, 2*N, K // packed_factor]
- w2_q (torch.Tensor): The second set of fp8-quantized expert weights. Shape: [num_experts, K, N // packed_factor]
- topk_weights (torch.Tensor): The weights of each token->expert mapping.
- topk_ids (torch.Tensor): The token->expert mappings.
- a_strides1 (torch.Tensor): The input strides for the first gemm. Shape: [num_experts]
- a_strides2 (torch.Tensor): The input strides for the second gemm. Shape: [num_experts]
- b_strides1 (torch.Tensor): The packed layout for the first gemm weights. Shape: [num_experts, 3] dtype: torch.int32
- b_strides2 (torch.Tensor): The packed layout for the second gemm weights. Shape: [num_experts, 3] dtype: torch.int32
- c_strides1 (torch.Tensor): The output strides for the first gemm. Shape: [num_experts]
- c_strides2 (torch.Tensor): The output strides for the second gemm. Shape: [num_experts]
- s_strides1 (torch.Tensor): strides for the group-wise scales for the first gemm. Shape: [num_experts, 2] dtype: torch.int64
- s_strides2 (torch.Tensor): strides for the group-wise scales for the second gemm. Shape: [num_experts, 2] dtype: torch.int64
- per_act_token (Optional[bool]): Whether the scale is per-token or per-tensor.
- activation (MoEActivation): The activation function to use.
- expert_map (Optional[torch.Tensor]): In the case of Expert parallel, every Rank is responsible for a subset of experts. expert_map is a mapping from global expert-id to local expert-id. When expert_map[i] is -1, it means that this Rank is not responsible for global expert-id i.
- 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.
- global_num_experts (int): The total number of experts.
- group_size (int): The number of weights per scale factor
Returns: - torch.Tensor: The bf16 output tensor after applying the MoE layer.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 | |
run_cutlass_moe_fp4 ¶
run_cutlass_moe_fp4(
output: Tensor,
a: Tensor,
a1_gscale: Tensor,
w1_fp4: Tensor,
w1_blockscale: Tensor,
w1_alphas: Tensor,
a2_gscale: Tensor,
w2_fp4: Tensor,
w2_blockscale: Tensor,
w2_alphas: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
activation: MoEActivation,
workspace13: Tensor,
workspace2: Tensor,
m: int,
n: int,
k: int,
e: int,
device: device,
apply_router_weight_on_input: bool = False,
) -> None
MoE implementation for FP4 Inputs
Gemm 1¶
a: Input tensor: [m, k] (half/bfloat16) a1_gscale: Activation scale per expert: [e] (float32) w1(gate up) (not an argument to cutlass_moe_fp4): [e, 2 * n, k] w1_fp4: [e, 2 * n, k // 2], dtype: torch.uint8 (stacked fp4: E2M1) (Note: n is the up projection output dim, k is the input dim in full precision) w1_blockscale: [e, 2 * n, k // block_size] (float8_e4m3) (Block size = 16 for NVFP4)
Gemm 2¶
a2_gscale: Activation scale per expert: [e] w2(down projection) (not an argument to cutlass_moe_fp4): [e, k, n] w2_fp4: [e, k, n // 2], dtype: torch.uint8 (stacked E2M1) w2_blockscale: [e, k, n // block_size], dtype: float8_e4m3
topk_weights: [m, topk] dtype: float8 topk_ids: [m, topk] dtype: float8
m, n, k: Unquantized weight shapes, dtype: int e: number of experts, dtype: int
assumes that topk < k < n to satisfy - up/down projection expectations.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 | |