Skip to content

Commit fe797cd

Browse files
author
niushengxiao
committed
feat: mega moe weight in place
1 parent 40586f0 commit fe797cd

4 files changed

Lines changed: 34 additions & 24 deletions

File tree

lightllm/common/basemodel/layer_weights/base_layer_weight.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def verify_load(self):
3838
else:
3939
layer_num = None
4040
assert attr.verify_load(), f"Loading {attr_name} of layers {layer_num} fails."
41+
attr.finalize_load()
4142

4243
def _cuda(self, cpu_tensor):
4344
return cpu_tensor.contiguous().to(self.data_type_).cuda(get_current_device_id())

lightllm/common/basemodel/layer_weights/meta_weights/base_weight.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def _create_weight(self):
2121
def verify_load(self) -> bool:
2222
pass
2323

24+
def finalize_load(self) -> None:
25+
pass
26+
2427

2528
class BaseWeightTpl(BaseWeight):
2629
def __init__(self, tp_rank: int = None, tp_world_size: int = None, data_type: torch.dtype = None):

lightllm/common/basemodel/layer_weights/meta_weights/fused_moe/fused_moe_weight.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ def verify_load(self):
284284
)
285285
return weight_load_ok and per_expert_scale_load_ok and e_score_correction_bias_load_ok
286286

287+
def finalize_load(self):
288+
if self.enable_ep_moe:
289+
from lightllm.common.basemodel.triton_kernel.fused_moe.grouped_fused_moe_ep import (
290+
transform_mega_moe_weights_in_place,
291+
use_sm100_mega_moe,
292+
)
293+
294+
if use_sm100_mega_moe(self.quant_method):
295+
transform_mega_moe_weights_in_place(self.w13, self.w2)
296+
287297
def _create_weight(self):
288298
intermediate_size = self.split_inter_size
289299
self.e_score_correction_bias = None

lightllm/common/basemodel/triton_kernel/fused_moe/grouped_fused_moe_ep.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from lightllm.utils.device_utils import is_sm100_gpu
2323

2424
logger = 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] = {}
2626
SUPPORTED_EP_EXPERT_DTYPES = ("deepgemm-fp8w8a8-b128", "deepgemm-fp4fp8-b32")
2727

2828
try:
@@ -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

264260
def 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

Comments
 (0)