@@ -303,7 +303,7 @@ def fused_experts(
303303 topk_ids .shape [0 ] == hidden_states .shape [0 ]
304304 ), f"topk_ids shape { topk_ids .shape } and topk_weights shape { topk_weights .shape } must be equal and match hidden_states shape[0] { hidden_states .shape [0 ]} "
305305
306- num_tokens , _ = hidden_states .shape
306+ num_tokens , hidden_dims = hidden_states .shape
307307
308308 E , _ , K = w1 .shape
309309 E , OutK , N = w2 .shape
@@ -312,20 +312,6 @@ def fused_experts(
312312 M = num_tokens
313313 TopK = topk_ids .shape [1 ]
314314
315- # import pdb; pdb.set_trace()
316- cache = torch .empty (
317- M * TopK * max (2 * N , OutK ),
318- device = hidden_states .device ,
319- dtype = hidden_states .dtype ,
320- )
321- intermediate_cache1 = cache [: M * TopK * 2 * N ].view ((M * TopK , 2 * N ))
322- intermediate_cache2 = torch .empty (
323- (M * TopK , N ),
324- device = hidden_states .device ,
325- dtype = hidden_states .dtype ,
326- )
327- intermediate_cache3 = cache [: M * TopK * OutK ].view ((M * TopK , OutK ))
328-
329315 if no_combine :
330316 assert not inplace
331317 out_hidden_states = torch .empty (
@@ -338,33 +324,55 @@ def fused_experts(
338324 else :
339325 out_hidden_states = torch .zeros_like (hidden_states )
340326
341- flat_topk = topk_ids .flatten ()
342- idxs = flat_topk .argsort ()
343- sorted_expert_ids = flat_topk [idxs ]
344-
345- counts = torch .bincount (sorted_expert_ids , minlength = E ) # [E]
346- token_idxs = idxs // TopK # [num_tokens * TopK]
347- input_A = torch .empty (
327+ topk_ids = topk_ids .int () if topk_ids .dtype == torch .long else topk_ids
328+ expert_offsets = torch .zeros ((E ), dtype = torch .int32 , device = hidden_states .device )
329+ problem_sizes1 = torch .empty ((E , 3 ), dtype = torch .int32 , device = hidden_states .device )
330+ problem_sizes2 = torch .empty ((E , 3 ), dtype = torch .int32 , device = hidden_states .device )
331+ a_map = torch .empty (
332+ (topk_ids .numel ()), dtype = torch .int32 , device = hidden_states .device
333+ )
334+ c_map = torch .empty (
335+ (topk_ids .numel ()), dtype = torch .int32 , device = hidden_states .device
336+ )
337+ torch .ops .sgl_kernel .prepare_moe_input .default (
338+ topk_ids ,
339+ expert_offsets ,
340+ None ,
341+ problem_sizes1 ,
342+ problem_sizes2 ,
343+ a_map ,
344+ c_map ,
345+ E ,
346+ hidden_dims ,
347+ TopK ,
348+ )
349+ input_A_shuffle = torch .empty (
348350 (num_tokens * TopK , K ), device = hidden_states .device , dtype = hidden_states .dtype
349351 )
350- input_A = hidden_states [token_idxs ].squeeze (1 )
351- offset = counts .to (torch .int32 )
352+ torch .ops .sgl_kernel .shuffle_rows .default (hidden_states , a_map , input_A_shuffle )
353+
354+ intermediate_cache1 = torch .empty (
355+ (M * TopK , 2 * N ), device = hidden_states .device , dtype = hidden_states .dtype
356+ )
357+ intermediate_cache2 = torch .empty (
358+ (M * TopK , N ), device = hidden_states .device , dtype = hidden_states .dtype
359+ )
360+ intermediate_cache3 = torch .empty (
361+ (M * TopK , OutK ), device = hidden_states .device , dtype = hidden_states .dtype
362+ )
352363
353- torch .ops .sgl_kernel .moe_grouped_mm_nt (intermediate_cache1 , input_A , w1 , offset , E )
364+ torch .ops .sgl_kernel .moe_grouped_mm_nt (
365+ intermediate_cache1 , input_A_shuffle , w1 , expert_offsets , E
366+ )
354367
355368 torch .ops .sgl_kernel .silu_and_mul (intermediate_cache2 , intermediate_cache1 )
356369
357370 torch .ops .sgl_kernel .moe_grouped_mm_nt (
358- intermediate_cache3 , intermediate_cache2 , w2 , offset , E
371+ intermediate_cache3 , intermediate_cache2 , w2 , expert_offsets , E
359372 )
360373
361- flat_weights = topk_weights .to (intermediate_cache3 .dtype ).flatten ()[idxs ] # [N]
362- intermediate_cache3 = intermediate_cache3 * flat_weights .unsqueeze (1 )
363- out_hidden_states .scatter_reduce_ (
364- 0 ,
365- token_idxs .view (- 1 , 1 ).expand (- 1 , OutK ),
366- intermediate_cache3 ,
367- reduce = "sum" ,
374+ torch .ops .sgl_kernel .apply_shuffle_mul_sum .default (
375+ intermediate_cache3 , out_hidden_states , c_map , topk_weights
368376 )
369377
370378 return out_hidden_states
0 commit comments