Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/mcore_bridge/bridge/gpt_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ def _set_moe_state(
hf_prefix: str,
layer_idx: int,
to_mcore: bool,
is_mtp: bool = False,
):
if to_mcore:
hf_state_dict = self._remove_prefix(hf_state_dict, hf_prefix)
Expand Down Expand Up @@ -689,15 +690,18 @@ def _set_moe_state(
layer_idx,
to_mcore,
ep_rank=ep_rank,
is_mtp=is_mtp,
))
if to_mcore:
hf_state_dict = {}
else:
hf_state_dict = self._add_prefix(hf_state_dict, hf_prefix)
return hf_state_dict

def _get_hf_experts_attr(self):
def _get_hf_experts_attr(self, is_mtp: bool = False):
# return hf_grouped, is_gate_up
if self.model_type == 'qwen3_5_moe' and not is_mtp:
return True, True
if self.model_type in {'glm4v_moe', 'kimi_vl', 'qwen3_omni_moe'} or self.llm_model_type in {
'qwen2_moe', 'qwen3_moe', 'deepseek_v2', 'deepseek_v3', 'kimi_k2', 'dots1', 'ernie4_5_moe', 'glm4_moe',
'glm4_moe_lite', 'minimax_m2', 'olmoe', 'qwen3_next', 'qwen3_5_moe', 'glm_moe_dsa', 'deepseek_v32'
Expand All @@ -723,6 +727,7 @@ def _set_mlp_state(
layer_idx: int,
to_mcore: bool,
ep_rank: Optional[int] = None,
is_mtp: bool = False,
):
if to_mcore:
hf_state_dict = self._remove_prefix(hf_state_dict, hf_prefix)
Expand All @@ -738,7 +743,7 @@ def _set_mlp_state(
is_gate_up = any('gate_up_proj' in k for k in hf_state_dict.keys())
# transformers 5.0 compatibility
if not to_mcore and is_expert:
hf_grouped, is_gate_up = self._get_hf_experts_attr()
hf_grouped, is_gate_up = self._get_hf_experts_attr(is_mtp)
need_transpose = False
if hf_grouped:
need_transpose = self._get_need_transpose()
Expand Down Expand Up @@ -1401,7 +1406,7 @@ def _set_layer_attn(self, mg_layer, hf_state_dict, layer_idx: int, to_mcore: boo
'input_layernorm.weight', to_mcore)
return hf_state_dict

def _set_layer_mlp(self, mg_layer, hf_state_dict, layer_idx: int, to_mcore: bool):
def _set_layer_mlp(self, mg_layer, hf_state_dict, layer_idx: int, to_mcore: bool, is_mtp: bool = False):
mg_mlp = None if mg_layer is None else mg_layer.mlp
is_moe = True if hasattr(mg_mlp, 'experts') else False
if not to_mcore:
Expand All @@ -1410,7 +1415,8 @@ def _set_layer_mlp(self, mg_layer, hf_state_dict, layer_idx: int, to_mcore: bool
dist.all_reduce(is_moe, group=self.pp_group)
if is_moe:
hf_state_dict.update(
self._set_moe_state(mg_mlp, hf_state_dict, f'{self.hf_mlp_prefix}.', layer_idx, to_mcore))
self._set_moe_state(
mg_mlp, hf_state_dict, f'{self.hf_mlp_prefix}.', layer_idx, to_mcore, is_mtp=is_mtp))
self._set_state_dict(mg_layer, 'pre_mlp_layernorm.weight', hf_state_dict, 'post_attention_layernorm.weight',
to_mcore)
else:
Expand Down Expand Up @@ -1608,7 +1614,7 @@ def _convert_mtp_layer(self, lm_model, hf_state_dict, hf_prefix: str, layer_idx:
self._set_state_dict(lm_model, 'output_layer.weight', hf_state_dict, 'shared_head.head.weight',
to_mcore)
hf_state_dict.update(self._set_layer_attn(transformer_layer, hf_state_dict, -1, to_mcore))
hf_state_dict.update(self._set_layer_mlp(transformer_layer, hf_state_dict, -1, to_mcore))
hf_state_dict.update(self._set_layer_mlp(transformer_layer, hf_state_dict, -1, to_mcore, is_mtp=True))
if to_mcore:
hf_state_dict = {}
else:
Expand Down
2 changes: 1 addition & 1 deletion src/mcore_bridge/model/gpt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def _postprocess(
if self.config.is_multimodal and self.config.context_parallel_size > 1:
input_ids = split_cp_inputs(input_ids, getattr(packed_seq_params, 'cu_seqlens_q', None), 1)

if self.mtp_process:
if self.mtp_process and labels is not None:
if self.config.is_multimodal:
embedding_ = (self.embedding, decoder_input)
else:
Expand Down
Loading