Skip to content

Commit bae3655

Browse files
author
niushengxiao
committed
feat: mega moe weight in place
1 parent 10e4e4a commit bae3655

4 files changed

Lines changed: 25 additions & 22 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: 11 additions & 22 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:
@@ -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

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

Comments
 (0)