@@ -208,7 +208,9 @@ def load_megatron_model(args):
208208
209209
210210@torch .inference_mode ()
211- def convert_checkpoint_from_megatron_to_transformers (mgmodel , hfmodel , args ):
211+ def convert_checkpoint_from_megatron_to_transformers (
212+ mgmodel , hfmodel , args , hf_pretrained_model = None
213+ ):
212214 if args .fp16 :
213215 mgmodel = mgmodel .half ()
214216 hfmodel = hfmodel .half ()
@@ -230,7 +232,14 @@ def convert_checkpoint_from_megatron_to_transformers(mgmodel, hfmodel, args):
230232 vision_head_dim = vision_hidden_size // mgvision .config .num_attention_heads
231233 copied_numel = 0
232234 safe_copy (mgvision .rotary_pos_emb .inv_freq , hfvision .rotary_pos_emb .inv_freq )
233- copied_numel += safe_copy (mgvision .patch_embed .proj .weight , hfvision .patch_embed .proj .weight )
235+ # patch_embed: mcore may use Linear (enable_linear) while hf uses Conv3d
236+ mg_patch_weight = mgvision .patch_embed .proj .weight
237+ hf_patch_weight = hfvision .patch_embed .proj .weight
238+ if mg_patch_weight .dim () == 2 and hf_patch_weight .dim () == 5 :
239+ # mcore Linear [embed_dim, flat_dim] -> hf Conv3d [embed_dim, C, T, H, W]
240+ copied_numel += safe_copy (mg_patch_weight .view (hf_patch_weight .shape ), hf_patch_weight )
241+ else :
242+ copied_numel += safe_copy (mg_patch_weight , hf_patch_weight )
234243 copied_numel += safe_copy (mgvision .patch_embed .proj .bias , hfvision .patch_embed .proj .bias )
235244 copied_numel += safe_copy (mgvision .pos_embed .weight , hfvision .pos_embed .weight )
236245
@@ -367,7 +376,7 @@ def convert_checkpoint_from_megatron_to_transformers(mgmodel, hfmodel, args):
367376
368377 copied_numel += safe_copy (mgllm .decoder .final_layernorm .weight , hfllm .norm .weight )
369378 if args .untie_embeddings_and_output_weights :
370- safe_copy (mgllm .output_layer .weight , hfmodel .lm_head .weight )
379+ safe_copy (mgllm .output_layer .weight , hf_pretrained_model .lm_head .weight )
371380
372381 n_params = sum (
373382 [
@@ -382,7 +391,9 @@ def convert_checkpoint_from_megatron_to_transformers(mgmodel, hfmodel, args):
382391
383392
384393@torch .inference_mode ()
385- def convert_checkpoint_from_transformers_to_megatron (hfmodel , mgmodel , args ):
394+ def convert_checkpoint_from_transformers_to_megatron (
395+ hfmodel , mgmodel , args , hf_pretrained_model = None
396+ ):
386397 if args .fp16 :
387398 mgmodel = mgmodel .half ()
388399 hfmodel = hfmodel .half ()
@@ -410,7 +421,14 @@ def convert_checkpoint_from_transformers_to_megatron(hfmodel, mgmodel, args):
410421 copied_numel = 0
411422 # rotary_pos_emb.inv_freq is buffer not parameter
412423 safe_copy (hfvision .rotary_pos_emb .inv_freq , mgvision .rotary_pos_emb .inv_freq )
413- copied_numel += safe_copy (hfvision .patch_embed .proj .weight , mgvision .patch_embed .proj .weight )
424+ # patch_embed: hf uses Conv3d while mcore may use Linear (enable_linear)
425+ hf_patch_weight = hfvision .patch_embed .proj .weight
426+ mg_patch_weight = mgvision .patch_embed .proj .weight
427+ if hf_patch_weight .dim () == 5 and mg_patch_weight .dim () == 2 :
428+ # hf Conv3d [embed_dim, C, T, H, W] -> mcore Linear [embed_dim, flat_dim]
429+ copied_numel += safe_copy (hf_patch_weight .reshape (mg_patch_weight .shape ), mg_patch_weight )
430+ else :
431+ copied_numel += safe_copy (hf_patch_weight , mg_patch_weight )
414432 copied_numel += safe_copy (hfvision .patch_embed .proj .bias , mgvision .patch_embed .proj .bias )
415433 copied_numel += safe_copy (hfvision .pos_embed .weight , mgvision .pos_embed .weight )
416434
@@ -550,7 +568,7 @@ def convert_checkpoint_from_transformers_to_megatron(hfmodel, mgmodel, args):
550568 copied_numel += safe_copy (hfllm .norm .weight , mgllm .decoder .final_layernorm .weight )
551569 if args .untie_embeddings_and_output_weights :
552570 # lm_head not in hfllm
553- safe_copy (hfmodel .lm_head .weight , mgllm .output_layer .weight )
571+ safe_copy (hf_pretrained_model .lm_head .weight , mgllm .output_layer .weight )
554572
555573 n_params = sum ([t .numel () for t in hfllm .state_dict ().values ()])
556574 assert n_params == copied_numel , (
@@ -873,15 +891,19 @@ def main():
873891 args .hf_ckpt_path , torch_dtype = config .torch_dtype
874892 )
875893 mg_model = load_megatron_model (args )
876- convert_checkpoint_from_megatron_to_transformers (mg_model , hf_model , args )
894+ convert_checkpoint_from_megatron_to_transformers (
895+ mg_model , hf_model .model , args , hf_pretrained_model = hf_model
896+ )
877897 save_hfmodel (args , hf_model )
878898 else :
879899 config = AutoConfig .from_pretrained (args .load )
880900 hf_model = Qwen3VLForConditionalGeneration .from_pretrained (
881901 args .load , torch_dtype = config .torch_dtype
882902 )
883903 mg_model = model_provider ()
884- convert_checkpoint_from_transformers_to_megatron (hf_model , mg_model , args )
904+ convert_checkpoint_from_transformers_to_megatron (
905+ hf_model .model , mg_model , args , hf_pretrained_model = hf_model
906+ )
885907 save_mgmodel (mg_model , args )
886908
887909
0 commit comments