22# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33"""Fused MoE utilities for GPTQ."""
44
5+ from collections .abc import Callable
6+
57import torch
68from typing_extensions import override
79
2224 maybe_warn_marlin_atomic_add ,
2325)
2426from vllm .scalar_type import ScalarType , scalar_types
25- from vllm .utils import direct_register_custom_op
27+
28+
29+ def default_activation_func (
30+ activation : str | None , output : torch .Tensor , input : torch .Tensor
31+ ) -> None :
32+ if activation == "silu" :
33+ torch .ops ._C .silu_and_mul (output , input )
34+ elif activation == "swigluoai" :
35+ # alpha = 1.702, limit = 7.0
36+ torch .ops ._C .swigluoai_and_mul (output , input )
37+ else :
38+ raise ValueError (
39+ f"Unsupported activation: { activation } . "
40+ "Only silu and swigluoai activations are supported."
41+ )
2642
2743
2844def fused_marlin_moe (
@@ -40,8 +56,10 @@ def fused_marlin_moe(
4056 apply_router_weight_on_input : bool = False ,
4157 global_num_experts : int = - 1 ,
4258 activation : str | None = "silu" ,
43- activation_func : str | None = None , # FIXME: type Callable
44- moe_sum : str | None = None , # FIXME: type Callable
59+ activation_func : Callable [
60+ [str | None , torch .Tensor , torch .Tensor ], None
61+ ] = default_activation_func ,
62+ moe_sum : Callable [[torch .Tensor , torch .Tensor ], None ] | None = None ,
4563 expert_map : torch .Tensor | None = None ,
4664 global_scale1 : torch .Tensor | None = None ,
4765 global_scale2 : torch .Tensor | None = None ,
@@ -190,22 +208,6 @@ def fused_marlin_moe(
190208 is_zp_float = False ,
191209 )
192210
193- if activation_func is None :
194-
195- def activation_func (
196- activation : str , output : torch .Tensor , input : torch .Tensor
197- ) -> None :
198- if activation == "silu" :
199- torch .ops ._C .silu_and_mul (output , input )
200- elif activation == "swigluoai" :
201- # alpha = 1.702, limit = 7.0
202- torch .ops ._C .swigluoai_and_mul (output , input )
203- else :
204- raise ValueError (
205- f"Unsupported activation: { activation } . "
206- "Only silu and swigluoai activations are supported."
207- )
208-
209211 activation_func (
210212 activation , intermediate_cache2 , intermediate_cache1 .view (- 1 , 2 * N )
211213 )
@@ -254,44 +256,6 @@ def activation_func(
254256 return moe_sum (intermediate_cache3 , output )
255257
256258
257- def fused_marlin_moe_fake (
258- hidden_states : torch .Tensor ,
259- w1 : torch .Tensor ,
260- w2 : torch .Tensor ,
261- w1_scale : torch .Tensor ,
262- w2_scale : torch .Tensor ,
263- gating_output : torch .Tensor | None ,
264- topk_weights : torch .Tensor ,
265- topk_ids : torch .Tensor ,
266- quant_type_id : int ,
267- apply_router_weight_on_input : bool = False ,
268- global_num_experts : int = - 1 ,
269- global_scale1 : torch .Tensor | None = None ,
270- global_scale2 : torch .Tensor | None = None ,
271- expert_map : torch .Tensor | None = None ,
272- g_idx1 : torch .Tensor | None = None ,
273- g_idx2 : torch .Tensor | None = None ,
274- sort_indices1 : torch .Tensor | None = None ,
275- sort_indices2 : torch .Tensor | None = None ,
276- w1_zeros : torch .Tensor | None = None ,
277- w2_zeros : torch .Tensor | None = None ,
278- workspace : torch .Tensor | None = None ,
279- intermediate_cache13 : torch .Tensor | None = None ,
280- intermediate_cache2 : torch .Tensor | None = None ,
281- is_k_full : bool = True ,
282- output : torch .Tensor | None = None ,
283- inplace : bool = False ,
284- ) -> torch .Tensor :
285- return torch .empty_like (hidden_states )
286-
287-
288- direct_register_custom_op (
289- op_name = "fused_marlin_moe" ,
290- op_func = fused_marlin_moe ,
291- fake_impl = fused_marlin_moe_fake ,
292- )
293-
294-
295259class MarlinExperts (mk .FusedMoEPermuteExpertsUnpermute ):
296260 def __init__ (self , quant_config : FusedMoEQuantConfig ):
297261 # TODO (varun) : Enable activation quantization
0 commit comments