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],
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
quant_config: FusedMoEQuantConfig,
)
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
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 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 |
|
activation_formats property
¶
activation_formats: tuple[
FusedMoEActivationFormat, FusedMoEActivationFormat
]
__init__ ¶
__init__(
max_experts_per_worker: int,
out_dtype: dtype,
quant_config: FusedMoEQuantConfig,
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],
a1q_scale: Optional[Tensor],
a2_scale: Optional[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],
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
quant_config: FusedMoEQuantConfig,
)
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
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 |
|
__init__ ¶
__init__(
out_dtype: Optional[dtype],
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
quant_config: FusedMoEQuantConfig,
)
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],
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,
topk_weights: Tensor,
topk_ids: Tensor,
quant_config: FusedMoEQuantConfig,
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,
ab_strides1: Tensor,
ab_strides2: Tensor,
c_strides1: Tensor,
c_strides2: Tensor,
quant_config: FusedMoEQuantConfig,
activation: str = "silu",
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
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 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 |
|
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
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 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 |
|
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
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 |
|
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
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|