2222from lightllm .utils .device_utils import is_sm100_gpu
2323
2424logger = init_logger (__name__ )
25- _MEGA_MOE_STATES : Dict [Tuple [int , int , int , int ], Dict [ str , Any ] ] = {}
25+ _MEGA_MOE_STATS : Dict [Tuple [int , int , int , int ], torch . Tensor ] = {}
2626SUPPORTED_EP_EXPERT_DTYPES = ("deepgemm-fp8w8a8-b128" , "deepgemm-fp4fp8-b32" )
2727
2828try :
@@ -232,17 +232,21 @@ def masked_group_gemm(
232232 return gemm_out_b
233233
234234
235- def _get_mega_moe_cache_state (w13 : Any , w2 : Any ):
235+ def _get_mega_moe_cumulative_stats (w13 : Any , w2 : Any ):
236236 state_key = (
237237 w13 .weight .data_ptr (),
238238 w13 .weight_scale .data_ptr (),
239239 w2 .weight .data_ptr (),
240240 w2 .weight_scale .data_ptr (),
241241 )
242- return _MEGA_MOE_STATES .setdefault (state_key , {})
242+ stats = _MEGA_MOE_STATS .get (state_key )
243+ if stats is None :
244+ stats = torch .zeros ((w13 .weight .shape [0 ],), device = w13 .weight .device , dtype = torch .int32 )
245+ _MEGA_MOE_STATS [state_key ] = stats
246+ return stats
243247
244248
245- def _transform_mega_moe_weights_in_place (w13 : Any , w2 : Any ):
249+ def transform_mega_moe_weights_in_place (w13 : Any , w2 : Any ):
246250 """Convert to Mega MoE layout without retaining a second weight copy."""
247251 transformed_l1 , transformed_l2 = deep_gemm .transform_weights_for_mega_moe (
248252 (w13 .weight , w13 .weight_scale ),
@@ -251,21 +255,6 @@ def _transform_mega_moe_weights_in_place(w13: Any, w2: Any):
251255 w13 .weight .copy_ (transformed_l1 [0 ])
252256 w13 .weight_scale .copy_ (transformed_l1 [1 ])
253257 w2 .weight_scale .copy_ (transformed_l2 [1 ])
254- return (w13 .weight , w13 .weight_scale ), (w2 .weight , w2 .weight_scale )
255-
256-
257- def _get_mega_moe_weights (w13 : Any , w2 : Any , state : Dict [str , Any ]):
258- if "weight_cache" not in state :
259- state ["weight_cache" ] = _transform_mega_moe_weights_in_place (w13 , w2 )
260- return state ["weight_cache" ]
261-
262-
263- def _get_mega_moe_cumulative_stats (num_local_experts : int , device : torch .device , state : Dict [str , Any ]):
264- stats = state .get ("stats" )
265- if stats is None or stats .numel () != num_local_experts or stats .device != device :
266- stats = torch .zeros ((num_local_experts ,), device = device , dtype = torch .int32 )
267- state ["stats" ] = stats
268- return stats
269258
270259
271260def mega_moe_impl (
@@ -289,9 +278,9 @@ def mega_moe_impl(
289278 f"Mega MoE got { num_tokens } tokens, exceeding num_max_tokens_per_rank={ buffer .num_max_tokens_per_rank } "
290279 )
291280
292- state = _get_mega_moe_cache_state (w13 , w2 )
293- l1_weights , l2_weights = _get_mega_moe_weights ( w13 , w2 , state )
294- stats = _get_mega_moe_cumulative_stats (w13 . weight . shape [ 0 ], hidden_states . device , state )
281+ l1_weights = (w13 . weight , w13 . weight_scale )
282+ l2_weights = ( w2 . weight , w2 . weight_scale )
283+ stats = _get_mega_moe_cumulative_stats (w13 , w2 )
295284 _prepare_mega_moe_buffer (hidden_states , topk_ids , topk_weights , buffer , quant_method .block_size )
296285
297286 output = torch .empty_like (hidden_states )
0 commit comments