3434- EXAONE bridge: QK layernorm mapping pattern
3535"""
3636
37+ import logging
38+ from typing import Any
39+
3740import torch
3841import torch .nn .functional as F
3942from megatron .core .models .gpt .gpt_model import GPTModel
43+ from transformers import PretrainedConfig
4044
4145from megatron .bridge .models .conversion .mapping_registry import MegatronMappingRegistry
4246from megatron .bridge .models .conversion .model_bridge import MegatronModelBridge
4650 QKVMapping ,
4751)
4852from megatron .bridge .models .exaone .exaone4 .exaone4_provider import exaone4_layer_spec
53+ from megatron .bridge .models .gpt .model_config import BridgeGPTModelConfig
4954from megatron .bridge .models .gpt_provider import GPTModelProvider
50- from megatron .bridge .models .hf_pretrained .causal_lm import PreTrainedCausalLM
5155
5256
57+ logger = logging .getLogger (__name__ )
58+
5359# Register custom EXAONE modules for AutoMapping weight distribution
5460# TERowParallelLinearLayerNorm is a row-parallel linear with post-layernorm
5561# (same pattern as other Post-LN architectures)
@@ -82,51 +88,64 @@ class Exaone4Bridge(MegatronModelBridge):
8288 ... "LGAI-EXAONE/EXAONE-4.0-1.2B",
8389 ... trust_remote_code=True,
8490 ... )
85- >>> provider = bridge.to_megatron_provider ()
91+ >>> model_config = bridge.get_model_config ()
8692 """
8793
88- def provider_bridge ( self , hf_pretrained : PreTrainedCausalLM ) -> GPTModelProvider :
89- """Convert HuggingFace EXAONE 4.0 config to Megatron GPTModelProvider.
94+ MODEL_CONFIG_CLASS = BridgeGPTModelConfig
95+ USE_MODEL_CONFIG_FOR_CONVERSION = True
9096
91- Maps HF config fields to Megatron TransformerConfig parameters and sets
92- EXAONE-specific options including Post-LN, QK norm, and RoPE scaling.
97+ # Megatron Core applies llama3 RoPE scaling with these fixed values; only the factor is configurable.
98+ _MCORE_LLAMA3_ROPE_SCALING = {
99+ "low_freq_factor" : 1.0 ,
100+ "high_freq_factor" : 4.0 ,
101+ "original_max_position_embeddings" : 8192 ,
102+ }
93103
94- Args :
95- hf_pretrained: HuggingFace PreTrainedCausalLM containing the EXAONE config
104+ def hf_config_to_model_config_kwargs ( self , hf_config : PretrainedConfig ) -> dict [ str , Any ] :
105+ """Convert a Hugging Face EXAONE 4.0 config to builder config kwargs.
96106
97- Returns:
98- GPTModelProvider configured for EXAONE 4.0 architecture
107+ Sets EXAONE-specific options including Post-LN, QK norm, and RoPE scaling.
108+ Size-dependent fields are populated by the shared HF config mapping.
99109 """
100- hf_config = hf_pretrained . config
101-
102- provider = super (). provider_bridge ( hf_pretrained )
103-
104- # EXAONE-specific architecture settings. Size-dependent fields are
105- # populated by the shared HF config mapping in the base bridge.
106- provider . normalization = "RMSNorm"
107- provider . activation_func = F . silu
108- provider . gated_linear_unit = True
109- provider . position_embedding_type = "rope"
110- provider . add_bias_linear = False
111- provider . add_qkv_bias = False
112- provider . qk_layernorm = True
113- provider . hidden_dropout = 0.0
114- provider . attention_dropout = 0.0
115- provider . transformer_layer_spec = exaone4_layer_spec
116- provider . autocast_dtype = torch . bfloat16
110+ config_kwargs = super (). hf_config_to_model_config_kwargs ( hf_config )
111+ config_kwargs . update (
112+ normalization = "RMSNorm" ,
113+ activation_func = F . silu ,
114+ gated_linear_unit = True ,
115+ position_embedding_type = "rope" ,
116+ add_bias_linear = False ,
117+ add_qkv_bias = False ,
118+ qk_layernorm = True ,
119+ hidden_dropout = 0.0 ,
120+ attention_dropout = 0.0 ,
121+ transformer_layer_spec = exaone4_layer_spec ,
122+ autocast_dtype = torch . bfloat16 ,
123+ masked_softmax_fusion = True ,
124+ rope_scaling = False ,
125+ rope_scaling_factor = 1.0 ,
126+ )
117127
118128 # RoPE scaling for EXAONE 4.0 (llama3-style)
119129 hf_rope_scaling = getattr (hf_config , "rope_scaling" , None )
120130 if hf_rope_scaling is not None and hf_rope_scaling .get ("rope_type" ) == "llama3" :
121- provider .rope_scaling = True
122- provider .rope_scaling_factor = hf_rope_scaling .get ("factor" , 16.0 )
123- provider .rope_scaling_low_freq_factor = hf_rope_scaling .get ("low_freq_factor" , 1.0 )
124- provider .rope_scaling_high_freq_factor = hf_rope_scaling .get ("high_freq_factor" , 4.0 )
125- provider .rope_scaling_original_max_position_embeddings = hf_rope_scaling .get (
126- "original_max_position_embeddings" , 8192
127- )
131+ config_kwargs .update (rope_scaling = True , rope_scaling_factor = hf_rope_scaling .get ("factor" , 16.0 ))
132+ unsupported = {
133+ key : hf_rope_scaling [key ]
134+ for key , value in self ._MCORE_LLAMA3_ROPE_SCALING .items ()
135+ if key in hf_rope_scaling and hf_rope_scaling [key ] != value
136+ }
137+ if unsupported :
138+ logger .warning (
139+ "EXAONE 4.0 llama3 RoPE scaling values %s differ from the fixed values Megatron Core applies (%s)." ,
140+ unsupported ,
141+ self ._MCORE_LLAMA3_ROPE_SCALING ,
142+ )
143+
144+ return config_kwargs
128145
129- return provider
146+ def hf_config_to_provider_kwargs (self , hf_config : PretrainedConfig ) -> dict [str , Any ]:
147+ """Adapt the canonical builder mapping to the deprecated provider path."""
148+ return self .hf_config_to_model_config_kwargs (hf_config )
130149
131150 @classmethod
132151 def megatron_to_hf_config (cls , provider : GPTModelProvider ) -> dict :
@@ -149,9 +168,11 @@ def megatron_to_hf_config(cls, provider: GPTModelProvider) -> dict:
149168 hf_config ["rope_scaling" ] = {
150169 "rope_type" : "llama3" ,
151170 "factor" : provider .rope_scaling_factor ,
152- "low_freq_factor" : provider .rope_scaling_low_freq_factor ,
153- "high_freq_factor" : provider .rope_scaling_high_freq_factor ,
154- "original_max_position_embeddings" : provider .rope_scaling_original_max_position_embeddings ,
171+ # Megatron Core's fixed llama3 values, unless a caller set them explicitly.
172+ ** {
173+ key : getattr (provider , f"rope_scaling_{ key } " , value )
174+ for key , value in cls ._MCORE_LLAMA3_ROPE_SCALING .items ()
175+ },
155176 }
156177
157178 return hf_config
0 commit comments