vllm.model_executor.layers.fused_moe.cutlass_moe
CUTLASS based Fused MoE kernels.
CutlassBatchedExpertsFp8 ¶
Bases: CutlassExpertsFp8Base
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
activation_formats property
¶
activation_formats: tuple[
FusedMoEActivationFormat, FusedMoEActivationFormat
]
__init__ ¶
__init__(
max_experts_per_worker: int,
num_dispatchers: int,
out_dtype: Optional[dtype],
per_act_token_quant: bool,
per_out_ch_quant: bool,
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
block_shape: Optional[list[int]] = None,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
workspace_shapes ¶
workspace_shapes(
a: Tensor,
aq: Tensor,
M: int,
N: int,
K: int,
topk: int,
global_num_experts: int,
local_num_experts: int,
expert_tokens_meta: Optional[ExpertTokensMetadata],
) -> tuple[
tuple[int, ...], tuple[int, ...], tuple[int, ...], dtype
]
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
CutlassExpertsFp4 ¶
Bases: FusedMoEPermuteExpertsUnpermute
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 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 |
|
activation_formats property
¶
activation_formats: tuple[
FusedMoEActivationFormat, FusedMoEActivationFormat
]
__init__ ¶
__init__(
g1_alphas: Tensor,
g2_alphas: Tensor,
a1_gscale: Tensor,
a2_gscale: Tensor,
max_experts_per_worker: int,
out_dtype: dtype,
per_act_token_quant: bool,
per_out_ch_quant: bool,
block_shape: Optional[list[int]] = None,
use_batched_format: bool = False,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
apply ¶
apply(
output: Tensor,
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
activation: str,
global_num_experts: int,
expert_map: Optional[Tensor],
w1_scale: Tensor,
w2_scale: Tensor,
w1_zp: Optional[Tensor],
w2_zp: Optional[Tensor],
a1q_scale: Optional[Tensor],
a2_scale: Tensor,
workspace13: Optional[Tensor],
workspace2: Optional[Tensor],
expert_tokens_meta: Optional[ExpertTokensMetadata],
apply_router_weight_on_input: bool,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
finalize_weight_and_reduce_impl ¶
finalize_weight_and_reduce_impl() -> TopKWeightAndReduce
workspace_shapes ¶
workspace_shapes(
a: Tensor,
aq: Tensor,
M: int,
N: int,
K: int,
topk: int,
global_num_experts: int,
local_num_experts: int,
expert_tokens_meta: Optional[ExpertTokensMetadata],
) -> tuple[
tuple[int, ...], tuple[int, ...], tuple[int, ...], dtype
]
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
CutlassExpertsFp8 ¶
Bases: CutlassExpertsFp8Base
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
activation_formats property
¶
activation_formats: tuple[
FusedMoEActivationFormat, FusedMoEActivationFormat
]
__init__ ¶
__init__(
out_dtype: Optional[dtype],
per_act_token_quant: bool,
per_out_ch_quant: bool,
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
block_shape: Optional[list[int]] = None,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
finalize_weight_and_reduce_impl ¶
finalize_weight_and_reduce_impl() -> TopKWeightAndReduce
workspace_shapes ¶
workspace_shapes(
a: Tensor,
aq: Tensor,
M: int,
N: int,
K: int,
topk: int,
global_num_experts: int,
local_num_experts: int,
expert_tokens_meta: Optional[ExpertTokensMetadata],
) -> tuple[
tuple[int, ...], tuple[int, ...], tuple[int, ...], dtype
]
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
CutlassExpertsFp8Base ¶
Bases: FusedMoEPermuteExpertsUnpermute
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
__init__ ¶
__init__(
out_dtype: Optional[dtype],
per_act_token_quant: bool,
per_out_ch_quant: bool,
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
block_shape: Optional[list[int]] = None,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
apply ¶
apply(
output: Tensor,
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
activation: str,
global_num_experts: int,
expert_map: Optional[Tensor],
w1_scale: Optional[Tensor],
w2_scale: Optional[Tensor],
w1_zp: Optional[Tensor],
w2_zp: Optional[Tensor],
a1q_scale: Optional[Tensor],
a2_scale: Optional[Tensor],
workspace13: Tensor,
workspace2: Tensor,
expert_tokens_meta: Optional[ExpertTokensMetadata],
apply_router_weight_on_input: bool,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
finalize_weight_and_reduce_impl ¶
finalize_weight_and_reduce_impl() -> TopKWeightAndReduce
_valid_cutlass_block_scaled_grouped_gemm ¶
_valid_cutlass_block_scaled_grouped_gemm(
w1: Tensor,
w2: Tensor,
inplace: bool,
activation: str,
apply_router_weight_on_input: bool,
expert_map: Optional[Tensor],
) -> bool
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
cutlass_moe_fp4 ¶
cutlass_moe_fp4(
a: Tensor,
w1_fp4: Tensor,
w2_fp4: Tensor,
w1_blockscale: Tensor,
w2_blockscale: Tensor,
g1_alphas: Tensor,
g2_alphas: Tensor,
a1_gscale: Tensor,
a2_gscale: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
m: int,
n: int,
k: int,
e: int,
expert_map: Optional[Tensor] = None,
apply_router_weight_on_input: bool = False,
) -> Tensor
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
cutlass_moe_fp8 ¶
cutlass_moe_fp8(
a: Tensor,
w1_q: Tensor,
w2_q: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
w1_scale: Tensor,
w2_scale: Tensor,
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
per_act_token: Optional[bool] = None,
activation: str = "silu",
a1_scale: Optional[Tensor] = None,
a2_scale: Optional[Tensor] = None,
expert_map: Optional[Tensor] = None,
apply_router_weight_on_input: bool = False,
global_num_experts: int = -1,
) -> Tensor
This function computes a a8w8-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 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, K, 2N] (the weights are passed transposed)
- w2_q (torch.Tensor): The second set of fp8-quantized expert weights. Shape: [num_experts, N, K] (the weights are passed transposed)
- topk_weights (torch.Tensor): The weights of each token->expert mapping.
- topk_ids (torch.Tensor): The token->expert mappings.
- w1_scale (torch.Tensor): The fp32 scale to dequantize w1_q. Shape: [num_experts] or [num_experts, 2N]
- w2_scale (torch.Tensor): The fp32 scale to dequantize w2_q. Shape: [num_experts] or [num_experts, K]
- ab_strides1 (torch.Tensor): The input/weight strides for the first gemm. Shape: [num_experts]
- ab_strides2 (torch.Tensor): The input/weight strides for the second gemm. Shape: [num_experts]
- 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]
- per_act_token (Optional[bool]): Whether the scale is per-token or per-tensor.
- activation (str): The activation function to use.
- a1_scale (Optional[torch.Tensor]): The optional fp32 scale to quantize a. Shape: scalar or [M]
- a2_scale (Optional[torch.Tensor]): The optional fp32 scale to quantize the intermediate result between the gemms. Shape: scalar or [M]
- 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.
Returns: - torch.Tensor: The fp16 output tensor after applying the MoE layer.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 |
|
run_cutlass_block_scaled_fused_experts ¶
run_cutlass_block_scaled_fused_experts(
a: Tensor,
w1: Tensor,
w2: Tensor,
w1_scale: Tensor,
w2_scale: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
) -> Tensor
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 |
|
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,
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
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 |
|
run_cutlass_moe_fp8 ¶
run_cutlass_moe_fp8(
output: Tensor,
hidden_states: Tensor,
w1: Tensor,
w2: Tensor,
topk_ids: Tensor,
activation_callable: Callable,
global_num_experts: int,
expert_map: Optional[Tensor],
w1_scale: Optional[Tensor],
w2_scale: Optional[Tensor],
a1q_scale: Optional[Tensor],
a2_scale: Optional[Tensor],
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
workspace13: Tensor,
workspace2: Tensor,
expert_num_tokens: Optional[Tensor],
out_dtype: dtype,
per_act_token: bool,
per_out_ch: bool,
use_batched_format: bool,
topk_weights: Optional[Tensor],
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|