vllm.model_executor.layers.fused_moe.cutlass_moe
CUTLASS based Fused MoE kernels.
CutlassExpertsFp8
¶
Bases: FusedMoEPermuteExpertsUnpermute
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
15 16 17 18 19 20 21 22 23 24 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 | |
__init__
¶
__init__(
ab_strides1: Tensor,
c_strides1: Tensor,
ab_strides2: Tensor,
c_strides2: Tensor,
out_dtype: dtype,
)
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
apply
¶
apply(
hidden_states: Tensor,
w1: Tensor,
w2: 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_num_tokens: Optional[Tensor],
) -> Tensor
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 | |
cutlass_moe_fp4
¶
cutlass_moe_fp4(
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,
m: int,
n: int,
k: int,
e: int,
device: device,
)
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
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 | |
cutlass_moe_fp8
¶
cutlass_moe_fp8(
a: Tensor,
w1_q: Tensor,
w2_q: Tensor,
w1_scale: Tensor,
w2_scale: Tensor,
topk_weights: Tensor,
topk_ids: Tensor,
ab_strides1: Tensor,
c_strides1: Tensor,
ab_strides2: Tensor,
c_strides2: Tensor,
a1_scale: Optional[Tensor] = None,
a2_scale: Optional[Tensor] = None,
out_dtype: dtype = half,
expert_map: Optional[Tensor] = None,
apply_router_weight_on_input: bool = False,
) -> 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)
- 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]
- gating_output (torch.Tensor): The output of the gating operation (before softmax).
- topk_weights (torch.Tensor): The weights of each token->expert mapping.
- ab_strides1 (torch.Tensor): The input and weights strides of the first grouped gemm.
- c_strides1 (torch.Tensor): The output strides of the first grouped gemm.
- ab_strides2 (torch.Tensor): The input and weights strides of the second grouped gemm.
- c_strides2 (torch.Tensor): The output strides of the second grouped gemm.
- 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]
- out_dtype (torch.dtype): The output tensor type.
- 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.
Returns: - torch.Tensor: The fp16 output tensor after applying the MoE layer.
Source code in vllm/model_executor/layers/fused_moe/cutlass_moe.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |