Skip to content

Commit a8ab43b

Browse files
authored
del split func for fused_experts (#4501)
1 parent 95c3c8a commit a8ab43b

2 files changed

Lines changed: 7 additions & 61 deletions

File tree

paddleformers/transformers/glm_moe_dsa/modeling.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,12 @@ def _qkv_sep(matrix_2d, ortho_fn, kv_head_num=None, num_key_value_groups=None):
122122
)
123123

124124
def _ffn_gate_up(matrix, ortho_fn, intermediate_size=None):
125+
"""Slice FFN gate_up, orthogonalise gate and up independently."""
125126
import paddle
126127

127-
if matrix.ndim == 2:
128-
gate, up = paddle.split(matrix, [intermediate_size, intermediate_size], axis=1)
129-
return paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=1)
130-
elif matrix.ndim == 3:
131-
expert_updates = []
132-
for ei in range(matrix.shape[0]):
133-
gate, up = paddle.split(matrix[ei], [intermediate_size, intermediate_size], axis=1)
134-
expert_updates.append(paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=1))
135-
return paddle.stack(expert_updates, axis=0)
136-
else:
137-
raise ValueError(f"FFN gate_up split expects 2D or 3D tensor, got shape {matrix.shape}")
128+
assert matrix.ndim == 2 or matrix.ndim == 3, "FFN gate_up split expects 2D or 3D tensor"
129+
gate, up = paddle.split(matrix, [intermediate_size, intermediate_size], axis=-1)
130+
return paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=-1)
138131

139132
def _mla_per_head(matrix_2d_global, ortho_fn, head_num=None, axis=None, head_split_sizes=None):
140133
import paddle
@@ -144,17 +137,6 @@ def _mla_per_head(matrix_2d_global, ortho_fn, head_num=None, axis=None, head_spl
144137
processed_groups = [ortho_fn(group) for group in groups]
145138
return paddle.concat(processed_groups, axis=axis)
146139

147-
def _moe_experts(matrix_3d_global, ortho_fn):
148-
import paddle
149-
150-
if matrix_3d_global.ndim != 3:
151-
raise ValueError(f"MoE expert split expects 3D tensor, got shape {matrix_3d_global.shape}")
152-
n_experts = matrix_3d_global.shape[0]
153-
return paddle.stack(
154-
[ortho_fn(matrix_3d_global[ei]) for ei in range(n_experts)],
155-
axis=0,
156-
)
157-
158140
slice_config = {}
159141

160142
muon_configs = config.muon_configs
@@ -164,7 +146,6 @@ def _moe_experts(matrix_3d_global, ortho_fn):
164146
num_key_value_heads = config.num_key_value_heads
165147
num_key_value_groups = num_attention_head // num_key_value_heads
166148
use_mla = getattr(config, "q_lora_rank", None) and config.q_lora_rank > 0
167-
moe_expert_fusion = getattr(config, "moe_expert_fusion", False)
168149
use_gated_attn = getattr(config, "use_gated_attn", False)
169150

170151
muon_qkv_update_mode = muon_configs.get("muon_qkv_update_mode", "split_head")
@@ -181,8 +162,6 @@ def _moe_experts(matrix_3d_global, ortho_fn):
181162

182163
ffn_slice_fn = _ffn_gate_up if muon_ffn_split else None
183164

184-
fused_moe_fn = _moe_experts if moe_expert_fusion else None
185-
186165
mla_slice_fn = None
187166
if use_mla and muon_qkv_update_mode == "split_head":
188167
mla_slice_fn = _mla_per_head
@@ -216,10 +195,6 @@ def _add_layer_slice_config(prefix):
216195
{"intermediate_size": moe_intermediate_size},
217196
)
218197

219-
if moe_expert_fusion and fused_moe_fn is not None:
220-
slice_config[f"{prefix}.mlp.experts.down_proj.weight"] = (fused_moe_fn, {})
221-
slice_config[f"{prefix}.mlp.grouped_gemm_experts.weight2"] = (fused_moe_fn, {})
222-
223198
if use_mla and mla_slice_fn is not None:
224199
assert (
225200
hasattr(config, "qk_nope_head_dim")

paddleformers/transformers/minimax_m2/modeling.py

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,9 @@ def _ffn_gate_up(matrix, ortho_fn, intermediate_size=None):
9393
"""Slice FFN gate_up, orthogonalise gate and up independently."""
9494
import paddle
9595

96-
if matrix.ndim == 2:
97-
gate, up = paddle.split(matrix, [intermediate_size, intermediate_size], axis=1)
98-
return paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=1)
99-
elif matrix.ndim == 3:
100-
expert_updates = []
101-
for ei in range(matrix.shape[0]):
102-
gate, up = paddle.split(matrix[ei], [intermediate_size, intermediate_size], axis=1)
103-
expert_updates.append(paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=1))
104-
return paddle.stack(expert_updates, axis=0)
105-
else:
106-
raise ValueError(f"FFN gate_up split expects 2D or 3D tensor, got shape {matrix.shape}")
96+
assert matrix.ndim == 2 or matrix.ndim == 3, "FFN gate_up split expects 2D or 3D tensor"
97+
gate, up = paddle.split(matrix, [intermediate_size, intermediate_size], axis=-1)
98+
return paddle.concat([ortho_fn(gate), ortho_fn(up)], axis=-1)
10799

108100
def _mla_per_head(matrix_2d_global, ortho_fn, head_num=None, axis=None, head_split_sizes=None):
109101
"""Slice MLA weights by heads."""
@@ -114,18 +106,6 @@ def _mla_per_head(matrix_2d_global, ortho_fn, head_num=None, axis=None, head_spl
114106
processed_groups = [ortho_fn(group) for group in groups]
115107
return paddle.concat(processed_groups, axis=axis)
116108

117-
def _moe_experts(matrix_3d_global, ortho_fn):
118-
"""Slice MoE weights by experts."""
119-
import paddle
120-
121-
if matrix_3d_global.ndim != 3:
122-
raise ValueError(f"MoE expert split expects 3D tensor, got shape {matrix_3d_global.shape}")
123-
n_experts = matrix_3d_global.shape[0]
124-
return paddle.stack(
125-
[ortho_fn(matrix_3d_global[ei]) for ei in range(n_experts)],
126-
axis=0,
127-
)
128-
129109
slice_config = {}
130110

131111
muon_configs = config.muon_configs
@@ -135,7 +115,6 @@ def _moe_experts(matrix_3d_global, ortho_fn):
135115
num_key_value_heads = config.num_key_value_heads
136116
num_key_value_groups = num_attention_head // num_key_value_heads
137117
use_mla = getattr(config, "q_lora_rank", None) and config.q_lora_rank > 0
138-
moe_expert_fusion = getattr(config, "moe_expert_fusion", False)
139118
use_gated_attn = getattr(config, "use_gated_attn", False)
140119

141120
# Get Muon configuration from muon_configs
@@ -155,9 +134,6 @@ def _moe_experts(matrix_3d_global, ortho_fn):
155134
# Determine FFN slice strategy
156135
ffn_slice_fn = _ffn_gate_up if muon_ffn_split else None
157136

158-
# Determine Fused MoE slice strategy
159-
fused_moe_fn = _moe_experts if moe_expert_fusion else None
160-
161137
# Determine MLA slice strategy
162138
mla_slice_fn = None
163139
if use_mla and muon_qkv_update_mode == "split_head":
@@ -198,11 +174,6 @@ def _add_layer_slice_config(prefix):
198174
{"intermediate_size": moe_intermediate_size},
199175
)
200176

201-
# Fused MoE weights (grouped_gemm)
202-
if moe_expert_fusion and fused_moe_fn is not None:
203-
slice_config[f"{prefix}.mlp.experts.down_proj.weight"] = (fused_moe_fn, {})
204-
slice_config[f"{prefix}.mlp.grouped_gemm_experts.weight2"] = (fused_moe_fn, {})
205-
206177
# MLA weights
207178
if use_mla and mla_slice_fn is not None:
208179
assert (

0 commit comments

Comments
 (0)