1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15-
15+ from enum import Enum
1616from typing import Any
1717
1818import torch
1919
2020import vllm .envs as envs
2121import vllm .model_executor .layers .fused_moe .modular_kernel as mk
22+ from vllm ._aiter_ops import rocm_aiter_ops
23+ from vllm .config .kernel import MoEBackend
2224from vllm .logger import init_logger
23- from vllm .model_executor .layers .fused_moe .activation import MoEActivation
2425from vllm .model_executor .layers .fused_moe .config import (
2526 FusedMoEConfig ,
2627)
27- from vllm .model_executor .layers .fused_moe .fused_moe import (
28- TritonExperts ,
29- try_get_optimal_moe_config ,
30- )
31- from vllm .model_executor .layers .fused_moe .oracle .unquantized import (
32- UnquantizedMoeBackend ,
33- backend_to_kernel_cls ,
34- map_unquantized_backend ,
35- )
36- from vllm .model_executor .layers .fused_moe .utils import (
37- _resize_cache ,
38- moe_kernel_quantize_input ,
39- )
28+ from vllm .platforms import current_platform
29+ from vllm .utils .flashinfer import has_flashinfer_cutlass_fused_moe
30+ from vllm .model_executor .layers .fused_moe .oracle .unquantized import UnquantizedMoeBackend , map_unquantized_backend , backend_to_kernel_cls
31+ from vllm .model_executor .layers .fused_moe .activation import MoEActivation
32+ from vllm .model_executor .layers .fused_moe .fused_moe import TritonExperts , try_get_optimal_moe_config
33+ from vllm .model_executor .layers .fused_moe .utils import _resize_cache , moe_kernel_quantize_input
4034from vllm .model_executor .layers .quantization .utils .flashinfer_utils import (
4135 FlashinferMoeBackend ,
4236 get_flashinfer_moe_backend ,
4337)
44- from vllm .platforms import current_platform
45- from vllm .triton_utils import tl
46-
38+ from vllm .triton_utils import tl , triton
4739from vllm_fl .dispatch import CachedOp
4840from vllm_fl .ops .fused_moe .activation import apply_moe_activation
4941from vllm_fl .utils import use_flaggems
5042# Kunlunxin: always use TritonExpertsFL regardless of flaggems setting
5143# (avoids _moe_C arg mismatch when flaggems is off)
5244from vllm_fl .dispatch .config .utils import get_platform_name
45+
5346_moe_align_block_size = CachedOp ("moe_align_block_size" )
5447_invoke_fused_moe_triton_kernel = CachedOp ("invoke_fused_moe_triton_kernel" )
5548_moe_sum = CachedOp ("moe_sum" )
@@ -102,10 +95,8 @@ def _move_to_back(
10295 ]
10396 return _AVAILABLE_BACKENDS
10497
105-
10698## Adopt from select_unquantized_moe_backend
107- def select_unquantized_moe_backend_oot (
108- moe_config : FusedMoEConfig ,
99+ def select_unquantized_moe_backend_oot (moe_config : FusedMoEConfig ,
109100) -> tuple [UnquantizedMoeBackend , type [mk .FusedMoEExperts ] | None ]:
110101 """
111102 Select the primary Unquantized MoE backend.
@@ -118,16 +109,14 @@ def select_unquantized_moe_backend_oot(
118109
119110 if current_platform .is_tpu ():
120111 return UnquantizedMoeBackend .TPU , None
121-
122- if current_platform .is_out_of_tree () and use_flaggems ():
112+ if current_platform .is_out_of_tree () and use_flaggems ():
123113 return UnquantizedMoeBackend .TRITON , TritonExpertsFL
124114 elif current_platform .is_out_of_tree ():
125115 # Kunlunxin uses TritonExpertsFL unconditionally because its
126116 # fused_experts_impl is patched to xtorch_ops, not flaggems.
127117 if get_platform_name () == "kunlunxin" :
128118 return UnquantizedMoeBackend .TRITON , TritonExpertsFL
129119 return UnquantizedMoeBackend .TRITON , TritonExperts
130-
131120 if moe_config .is_lora_enabled :
132121 return UnquantizedMoeBackend .TRITON , backend_to_kernel_cls (
133122 UnquantizedMoeBackend .TRITON
@@ -257,7 +246,6 @@ def _return_or_raise(
257246 "No Unquantized MoE backend supports the deployment configuration."
258247 )
259248
260-
261249def _prepare_expert_assignment (
262250 topk_ids : torch .Tensor ,
263251 config : dict [str , Any ],
@@ -306,7 +294,6 @@ def _prepare_expert_assignment(
306294 ignore_invalid_experts = ignore_invalid_experts ,
307295 )
308296
309-
310297class TritonExpertsFL (TritonExperts ):
311298 def apply (
312299 self ,
@@ -326,53 +313,11 @@ def apply(
326313 expert_tokens_meta : mk .ExpertTokensMetadata | None ,
327314 apply_router_weight_on_input : bool ,
328315 ):
329- # Dynamic W8A8 is handled by TritonW8A8Experts so vLLM owns
330- # activation quantization. Do not allow it to fall back into the
331- # FlagGems contract, which expects floating-point input here.
332- if self .quant_config .use_int8_w8a8 :
333- raise RuntimeError (
334- "W8A8 MoE must use TritonW8A8Experts, not TritonExpertsFL"
335- )
336-
337- # Fast path (no LoRA, NVIDIA only): let FlagGems own both expert GEMMs
338- # for unquantized and W8A16 inputs.
339- if self ._lora_context is None and current_platform .is_cuda ():
340- import flag_gems
341-
342- output .copy_ (
343- flag_gems .fused_experts_impl (
344- hidden_states ,
345- w1 ,
346- w2 ,
347- topk_weights ,
348- topk_ids ,
349- inplace = False ,
350- activation = activation .value ,
351- apply_router_weight_on_input = apply_router_weight_on_input ,
352- use_fp8_w8a8 = self .quant_config .use_fp8_w8a8 ,
353- use_int8_w8a8 = False ,
354- use_int8_w8a16 = self .quant_config .use_int8_w8a16 ,
355- use_int4_w4a16 = self .quant_config .use_int4_w4a16 ,
356- per_channel_quant = self .per_act_token_quant ,
357- global_num_experts = global_num_experts ,
358- expert_map = expert_map ,
359- w1_scale = self .w1_scale ,
360- w2_scale = self .w2_scale ,
361- a1_scale = a1q_scale ,
362- a2_scale = a2_scale ,
363- block_shape = self .block_shape ,
364- w1_bias = self .w1_bias ,
365- w2_bias = self .w2_bias ,
366- )
367- )
368316 # Fast path (no LoRA): single fused call via patched fused_experts_impl.
369317 if self ._lora_context is None :
370318 from vllm_fl .ops .fused_moe .fused_moe import fused_experts_impl as _fused_experts_impl
371- # Fast path (no LoRA, NVIDIA only): single fused FlagGems call.
372- if self ._lora_context is None and current_platform .is_cuda ():
373- import flag_gems
374319
375- output .copy_ (flag_gems . fused_experts_impl (
320+ output .copy_ (_fused_experts_impl (
376321 hidden_states ,
377322 w1 ,
378323 w2 ,
0 commit comments