1313# limitations under the License.
1414
1515from functools import partial
16+ from typing import Any
1617
1718import torch
1819from megatron .core .models .gpt .gpt_layer_specs import get_gpt_decoder_block_spec
1920from megatron .core .models .gpt .gpt_model import GPTModel
20- from transformers import Glm4MoeForCausalLM
21+ from transformers import Glm4MoeForCausalLM , PretrainedConfig
2122
2223from megatron .bridge .models .conversion .mapping_registry import MegatronMappingRegistry
2324from megatron .bridge .models .conversion .model_bridge import MegatronModelBridge
2829 GatedMLPMapping ,
2930 QKVMapping ,
3031)
31- from megatron .bridge .models .gpt_provider import GPTModelProvider
32- from megatron .bridge .models .hf_pretrained .causal_lm import PreTrainedCausalLM
3332
3433
3534try :
@@ -51,47 +50,58 @@ class GLM45Bridge(MegatronModelBridge):
5150 Example:
5251 >>> from megatron.bridge import AutoBridge
5352 >>> bridge = AutoBridge.from_hf_pretrained("zai-org/GLM-4.5")
54- >>> provider = bridge.to_megatron_provider ()
53+ >>> model_config = bridge.get_model_config ()
5554 """
5655
57- def provider_bridge (self , hf_pretrained : PreTrainedCausalLM ) -> GPTModelProvider :
58- """Convert HuggingFace config to GPTModelProvider."""
59- provider = super ().provider_bridge (hf_pretrained )
60- hf_config = hf_pretrained .config
56+ USE_MODEL_CONFIG_FOR_CONVERSION = True
6157
62- # Use decoder block spec to properly handle moe_layer_freq (mixed dense/MoE layers)
63- provider .transformer_layer_spec = partial (get_gpt_decoder_block_spec , use_transformer_engine = HAVE_TE )
64- provider .normalization = "RMSNorm"
65- provider .gated_linear_unit = True
66- provider .add_bias_linear = False
67- provider .share_embeddings_and_output_weights = False
58+ def hf_config_to_model_config_kwargs (self , hf_config : PretrainedConfig ) -> dict [str , Any ]:
59+ """Convert a Hugging Face GLM-4.5 config to builder config kwargs.
6860
69- provider .moe_shared_expert_overlap = True
70- provider .moe_token_dispatcher_type = "alltoall"
71- provider .moe_router_load_balancing_type = "seq_aux_loss"
72- provider .moe_router_pre_softmax = False
73- provider .moe_grouped_gemm = True
74- provider .moe_router_score_function = "sigmoid"
75- provider .moe_permute_fusion = True
76- provider .moe_router_enable_expert_bias = True
77- provider .moe_router_dtype = "fp32"
78- provider .moe_router_bias_update_rate = 0
79- provider .moe_aux_loss_coeff = 0.001
80-
81- provider .persist_layer_norm = True
82- provider .bias_activation_fusion = True
83- provider .bias_dropout_fusion = True
84- provider .hidden_dropout = 0.0
85- provider .autocast_dtype = torch .bfloat16
86- provider .mtp_num_layers = getattr (hf_config , "num_nextn_predict_layers" , None )
87- provider .mtp_loss_scaling_factor = 0.3
88- provider .moe_shared_expert_intermediate_size = hf_config .moe_intermediate_size
89-
90- provider .moe_layer_freq = [0 ] * hf_config .first_k_dense_replace + [1 ] * (
91- hf_config .num_hidden_layers - hf_config .first_k_dense_replace
61+ The builder's default MoE layer spec already uses the GPT decoder block
62+ spec, which honors the mixed dense/MoE ``moe_layer_freq``.
63+ """
64+ config_kwargs = super ().hf_config_to_model_config_kwargs (hf_config )
65+ config_kwargs .update (
66+ normalization = "RMSNorm" ,
67+ gated_linear_unit = True ,
68+ add_bias_linear = False ,
69+ share_embeddings_and_output_weights = False ,
70+ moe_shared_expert_overlap = True ,
71+ moe_token_dispatcher_type = "alltoall" ,
72+ moe_router_load_balancing_type = "seq_aux_loss" ,
73+ moe_router_pre_softmax = False ,
74+ moe_grouped_gemm = True ,
75+ moe_router_score_function = "sigmoid" ,
76+ moe_permute_fusion = True ,
77+ moe_router_enable_expert_bias = True ,
78+ moe_router_dtype = "fp32" ,
79+ moe_router_bias_update_rate = 0 ,
80+ moe_aux_loss_coeff = 0.001 ,
81+ persist_layer_norm = True ,
82+ bias_activation_fusion = True ,
83+ bias_dropout_fusion = True ,
84+ hidden_dropout = 0.0 ,
85+ autocast_dtype = torch .bfloat16 ,
86+ masked_softmax_fusion = True ,
87+ rope_scaling = False ,
88+ rope_scaling_factor = 1.0 ,
89+ mtp_num_layers = getattr (hf_config , "num_nextn_predict_layers" , None ),
90+ mtp_loss_scaling_factor = 0.3 ,
91+ moe_shared_expert_intermediate_size = hf_config .moe_intermediate_size ,
92+ moe_layer_freq = [0 ] * hf_config .first_k_dense_replace
93+ + [1 ] * (hf_config .num_hidden_layers - hf_config .first_k_dense_replace ),
9294 )
95+ config_kwargs .setdefault ("position_embedding_type" , "rope" )
96+ return config_kwargs
9397
94- return provider
98+ def hf_config_to_provider_kwargs (self , hf_config : PretrainedConfig ) -> dict [str , Any ]:
99+ """Adapt the canonical builder mapping to the deprecated provider path."""
100+ provider_kwargs = self .hf_config_to_model_config_kwargs (hf_config )
101+ # The provider's default layer spec builds homogeneous layers; the decoder
102+ # block spec is needed for mixed dense/MoE layers (moe_layer_freq).
103+ provider_kwargs ["transformer_layer_spec" ] = partial (get_gpt_decoder_block_spec , use_transformer_engine = HAVE_TE )
104+ return provider_kwargs
95105
96106 def mapping_registry (self ) -> MegatronMappingRegistry :
97107 mapping_list = []
0 commit comments