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 :
@@ -135,9 +135,7 @@ def _mega_moe_quant_topk_to_buffer_kernel(
135135 topk_idx .to (topk_idx_out_ptr .dtype .element_ty ),
136136 )
137137 tl .store (
138- topk_weights_out_ptr
139- + token_id * stride_topk_weights_out_m
140- + topk_offsets * stride_topk_weights_out_k ,
138+ topk_weights_out_ptr + token_id * stride_topk_weights_out_m + topk_offsets * stride_topk_weights_out_k ,
141139 topk_weights .to (topk_weights_out_ptr .dtype .element_ty ),
142140 )
143141
@@ -234,31 +232,29 @@ def masked_group_gemm(
234232 return gemm_out_b
235233
236234
237- def _get_mega_moe_cache_state (w13 : Any , w2 : Any ):
235+ def _get_mega_moe_cumulative_stats (w13 : Any , w2 : Any ):
238236 state_key = (
239237 w13 .weight .data_ptr (),
240238 w13 .weight_scale .data_ptr (),
241239 w2 .weight .data_ptr (),
242240 w2 .weight_scale .data_ptr (),
243241 )
244- return _MEGA_MOE_STATES .setdefault (state_key , {})
245-
246-
247- def _get_mega_moe_weights (w13 : Any , w2 : Any , state : Dict [str , Any ]):
248- if "weight_cache" not in state :
249- state ["weight_cache" ] = deep_gemm .transform_weights_for_mega_moe (
250- (w13 .weight , w13 .weight_scale ),
251- (w2 .weight , w2 .weight_scale ),
252- )
253- return state ["weight_cache" ]
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
254247
255248
256- def _get_mega_moe_cumulative_stats (num_local_experts : int , device : torch .device , state : Dict [str , Any ]):
257- stats = state .get ("stats" )
258- if stats is None or stats .numel () != num_local_experts or stats .device != device :
259- stats = torch .zeros ((num_local_experts ,), device = device , dtype = torch .int32 )
260- state ["stats" ] = stats
261- return stats
249+ def transform_mega_moe_weights_in_place (w13 : Any , w2 : Any ):
250+ """Convert to Mega MoE layout without retaining a second weight copy."""
251+ transformed_l1 , transformed_l2 = deep_gemm .transform_weights_for_mega_moe (
252+ (w13 .weight , w13 .weight_scale ),
253+ (w2 .weight , w2 .weight_scale ),
254+ )
255+ w13 .weight .copy_ (transformed_l1 [0 ])
256+ w13 .weight_scale .copy_ (transformed_l1 [1 ])
257+ w2 .weight_scale .copy_ (transformed_l2 [1 ])
262258
263259
264260def mega_moe_impl (
@@ -282,9 +278,9 @@ def mega_moe_impl(
282278 f"Mega MoE got { num_tokens } tokens, exceeding num_max_tokens_per_rank={ buffer .num_max_tokens_per_rank } "
283279 )
284280
285- state = _get_mega_moe_cache_state (w13 , w2 )
286- l1_weights , l2_weights = _get_mega_moe_weights ( w13 , w2 , state )
287- 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 )
288284 _prepare_mega_moe_buffer (hidden_states , topk_ids , topk_weights , buffer , quant_method .block_size )
289285
290286 output = torch .empty_like (hidden_states )
0 commit comments