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: FusedMoEExpertsModular
CUTLASS FP4 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
CutlassExpertsFp8 ¶
Bases: CutlassExpertsFp8Base
CUTLASS FP8 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
CutlassExpertsMxfp4 ¶
Bases: FusedMoEExpertsModular
CUTLASS MXFP4 x MXFP4 fused MoE expert implementation.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 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 | |
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
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 | |
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 (not an argument to cutlass_moe_fp4): [e, w1_n, k] w1_fp4: [e, w1_n, k // 2], dtype: torch.uint8 (stacked fp4: E2M1) where w1_n = 2*n for gated activations (gate+up), n for non-gated (up only). (Note: n is the up projection output dim, k is the input dim in full precision) w1_blockscale: [e, w1_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
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 | |
run_cutlass_moe_mxfp4 ¶
run_cutlass_moe_mxfp4(
output: Tensor,
a: Tensor,
w1_fp4: Tensor,
w1_blockscale: Tensor,
w2_fp4: Tensor,
w2_blockscale: 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
MXFP4 x MXFP4 MoE implementation using CUTLASS grouped GEMM.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 | |
swizzle_mxfp4_scales ¶
Swizzle flat [N, K//32] E8M0 scales to CUTLASS tiled layout.
CUTLASS expects MX scale factors in a tiled layout
[numMTiles, numKTiles, 32, 4, 4]
where numMTiles = ceil(N/128), numKTiles = ceil(K/128), and the inner dimensions correspond to the swizzle pattern: mTileIdx = mIdx / 128 outerMIdx = mIdx % 32 innerMIdx = (mIdx / 32) % 4 kTileIdx = kIdx / 4 innerKIdx = kIdx % 4 with kIdx = col_in_scale_space (i.e., index into K//32).