|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +""" |
| 12 | +MoE Op abstraction for supporting different MoE computation implementations. |
| 13 | +This module provides a unified interface for different MoE ops (Cutlass, DeepGemm, etc.) |
| 14 | +""" |
| 15 | + |
| 16 | +from abc import ABC, abstractmethod |
| 17 | +from typing import TYPE_CHECKING, List, Optional |
| 18 | + |
| 19 | +import torch |
| 20 | + |
| 21 | +from tensorrt_llm._utils import get_sm_version |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from ..interface import MoE |
| 25 | + |
| 26 | + |
| 27 | +class MoEOp(ABC): |
| 28 | + """Abstract base class for MoE computation ops. |
| 29 | +
|
| 30 | + This class provides a strategy pattern for different MoE computation implementations. |
| 31 | + It is used by MoE modules (like WideEPMoE) to delegate the actual computation. |
| 32 | +
|
| 33 | + Note: MoEOp is NOT a MoE module itself, but a computation strategy. |
| 34 | + The actual MoE module (e.g., WideEPMoE) inherits from MoE and uses MoEOp |
| 35 | + for the computation implementation. |
| 36 | + """ |
| 37 | + |
| 38 | + # Op-specific abstract methods |
| 39 | + @abstractmethod |
| 40 | + def finalize_tactic( |
| 41 | + self, |
| 42 | + module: 'MoE', |
| 43 | + tuner_input: torch.Tensor, |
| 44 | + output_dtype: torch.dtype, |
| 45 | + min_latency_mode: bool = False, |
| 46 | + use_fused_finalize: bool = True, |
| 47 | + tuner_top_k: Optional[int] = None, |
| 48 | + ) -> None: |
| 49 | + """ |
| 50 | + Finalize tactics for the MoE computation. |
| 51 | + For Cutlass op, this includes profiling and tactic selection. |
| 52 | + For DeepGemm op, this can be a no-op. |
| 53 | +
|
| 54 | + Args: |
| 55 | + module: The MoE module containing MoE configurations |
| 56 | + tuner_input: Real input used for tuning (same shape/layout as non-alltoall) |
| 57 | + output_dtype: Output dtype for tuner run |
| 58 | + min_latency_mode: Whether to profile for min-latency path |
| 59 | + use_fused_finalize: Whether to use fused finalize |
| 60 | + tuner_top_k: Top-k value for tuning (Cutlass specific) |
| 61 | + """ |
| 62 | + |
| 63 | + @abstractmethod |
| 64 | + def compute_moe( |
| 65 | + self, |
| 66 | + module: 'MoE', |
| 67 | + # Input tensors |
| 68 | + x: torch.Tensor, |
| 69 | + token_selected_slots: torch.Tensor, |
| 70 | + token_final_scales: Optional[torch.Tensor], |
| 71 | + # Weight tensors |
| 72 | + w3_w1_weight: torch.Tensor, |
| 73 | + w3_w1_bias: Optional[torch.Tensor], |
| 74 | + w2_weight: torch.Tensor, |
| 75 | + w2_bias: Optional[torch.Tensor], |
| 76 | + # Output configuration |
| 77 | + output_dtype: torch.dtype, |
| 78 | + # Quantization parameters |
| 79 | + quant_scales: List[torch.Tensor], |
| 80 | + input_sf: Optional[torch.Tensor] = None, |
| 81 | + swizzled_input_sf: bool = True, |
| 82 | + # Performance tuning (only runtime-variable parameters) |
| 83 | + min_latency_mode: bool = False, |
| 84 | + use_fused_finalize: bool = True, |
| 85 | + tuner_num_tokens: Optional[int] = None, |
| 86 | + tuner_top_k: Optional[int] = None, |
| 87 | + **kwargs) -> torch.Tensor: |
| 88 | + """ |
| 89 | + Perform the actual MoE computation. |
| 90 | +
|
| 91 | + Configuration parameters (tp_size, ep_size, swiglu params, etc.) are |
| 92 | + automatically extracted from the module parameter. |
| 93 | +
|
| 94 | + Args: |
| 95 | + module: MoE module containing configuration and parameters. |
| 96 | + x: Input tensor |
| 97 | + token_selected_slots: Selected expert slots |
| 98 | + token_final_scales: Scaling factors |
| 99 | + w3_w1_weight: Fused gate and up projection weights |
| 100 | + w3_w1_bias: Optional bias |
| 101 | + w2_weight: Down projection weights |
| 102 | + w2_bias: Optional bias |
| 103 | + output_dtype: Output data type |
| 104 | + quant_scales: Quantization scales |
| 105 | + input_sf: Input scaling factor |
| 106 | + swizzled_input_sf: Whether input_sf is swizzled |
| 107 | + min_latency_mode: Use minimum latency optimizations |
| 108 | + use_fused_finalize: Use fused finalization |
| 109 | + tuner_num_tokens: Number of tokens for tuning |
| 110 | + tuner_top_k: Top-k value for tuning |
| 111 | +
|
| 112 | + Returns: |
| 113 | + Computed MoE output tensor |
| 114 | + """ |
| 115 | + |
| 116 | + def run_moe( |
| 117 | + self, |
| 118 | + module: 'MoE', |
| 119 | + # Input tensors |
| 120 | + input: torch.Tensor, |
| 121 | + token_selected_slots: torch.Tensor, |
| 122 | + token_final_scales: torch.Tensor, |
| 123 | + w3_w1_weight: torch.Tensor, |
| 124 | + w3_w1_bias: Optional[torch.Tensor], |
| 125 | + w2_weight: torch.Tensor, |
| 126 | + w2_bias: Optional[torch.Tensor], |
| 127 | + output_dtype: torch.dtype, |
| 128 | + # Quantization parameters |
| 129 | + quant_scales: List[torch.Tensor], |
| 130 | + input_sf: Optional[torch.Tensor] = None, |
| 131 | + swizzled_input_sf: bool = True, |
| 132 | + # Performance tuning (only runtime-variable parameters) |
| 133 | + min_latency_mode: bool = False, |
| 134 | + use_fused_finalize: bool = True, |
| 135 | + tuner_num_tokens: Optional[int] = None, |
| 136 | + tuner_top_k: Optional[int] = None, |
| 137 | + **kwargs) -> torch.Tensor: |
| 138 | + """ |
| 139 | + Run the complete MoE computation pipeline. |
| 140 | +
|
| 141 | + Configuration parameters are automatically extracted from the module. |
| 142 | +
|
| 143 | + Args: |
| 144 | + module: MoE module containing configuration |
| 145 | + input: Input tensor to the MoE layer |
| 146 | + token_selected_slots: Selected expert slots for each token |
| 147 | + token_final_scales: Final scaling factors for each token |
| 148 | + w3_w1_weight: Concatenated weights for w3 and w1 projections |
| 149 | + w3_w1_bias: Optional bias for w3/w1 projections |
| 150 | + w2_weight: Weight for w2 projection |
| 151 | + w2_bias: Optional bias for w2 projection |
| 152 | + output_dtype: Desired output data type |
| 153 | + quant_scales: Quantization scales for weights |
| 154 | + input_sf: Optional input scale factors for quantization |
| 155 | + swizzled_input_sf: Whether input scale factors are swizzled |
| 156 | + min_latency_mode: Use minimum latency optimizations |
| 157 | + use_fused_finalize: Use fused finalization |
| 158 | + tuner_num_tokens: Number of tokens for tuner input |
| 159 | + tuner_top_k: Top-k value for tuning |
| 160 | +
|
| 161 | + Returns: |
| 162 | + Computed MoE output tensor |
| 163 | + """ |
| 164 | + self.finalize_tactic(module, input, output_dtype, min_latency_mode, |
| 165 | + use_fused_finalize, tuner_top_k) |
| 166 | + |
| 167 | + # Call compute_moe with module |
| 168 | + return self.compute_moe(module=module, |
| 169 | + x=input, |
| 170 | + token_selected_slots=token_selected_slots, |
| 171 | + token_final_scales=token_final_scales, |
| 172 | + w3_w1_weight=w3_w1_weight, |
| 173 | + w3_w1_bias=w3_w1_bias, |
| 174 | + w2_weight=w2_weight, |
| 175 | + w2_bias=w2_bias, |
| 176 | + output_dtype=output_dtype, |
| 177 | + quant_scales=quant_scales, |
| 178 | + input_sf=input_sf, |
| 179 | + swizzled_input_sf=swizzled_input_sf, |
| 180 | + min_latency_mode=min_latency_mode, |
| 181 | + use_fused_finalize=use_fused_finalize, |
| 182 | + tuner_num_tokens=tuner_num_tokens, |
| 183 | + tuner_top_k=tuner_top_k, |
| 184 | + **kwargs) |
| 185 | + |
| 186 | + |
| 187 | +class MoEOpSelector: |
| 188 | + """ |
| 189 | + Utility class for selecting the appropriate MoE op based on |
| 190 | + hardware capabilities and quantization configuration. |
| 191 | +
|
| 192 | + This class implements the strategy pattern for op selection, |
| 193 | + choosing between Cutlass and DeepGemm implementations based on: |
| 194 | + - Hardware capabilities (SM version) |
| 195 | + - Quantization configuration (block FP8 support) |
| 196 | + """ |
| 197 | + |
| 198 | + @staticmethod |
| 199 | + def select_op(module: 'MoE') -> MoEOp: |
| 200 | + """ |
| 201 | + Select the appropriate MoE op based on module configuration. |
| 202 | +
|
| 203 | + Selection criteria: |
| 204 | + - Blackwell (SM100) with block FP8 quantization -> DeepGemm op |
| 205 | + - All other configurations -> Cutlass op |
| 206 | +
|
| 207 | + Args: |
| 208 | + module: The MoE module containing configuration information |
| 209 | +
|
| 210 | + Returns: |
| 211 | + MoEOp: Selected op instance (CutlassMoEOp or DeepGemmMoEOp) |
| 212 | +
|
| 213 | + Example: |
| 214 | + >>> op = MoEOpSelector.select_op(moe_module) |
| 215 | + >>> output = op.run_moe(input, ...) |
| 216 | + """ |
| 217 | + from .moe_op_cutlass import CutlassMoEOp |
| 218 | + from .moe_op_deepgemm import DeepGemmMoEOp |
| 219 | + |
| 220 | + # Check if we should use DeepGemm op |
| 221 | + # Blackwell has SM version 100 |
| 222 | + is_blackwell = get_sm_version() == 100 |
| 223 | + has_block_fp8 = module.has_deepseek_fp8_block_scales |
| 224 | + |
| 225 | + if is_blackwell and has_block_fp8: |
| 226 | + # Use DeepGemm op for Blackwell with block FP8 |
| 227 | + return DeepGemmMoEOp() |
| 228 | + else: |
| 229 | + # Use Cutlass op for all other cases |
| 230 | + return CutlassMoEOp() |
0 commit comments