bridge.models.ernie.ernie55_bridge#
Bridge between Hugging Face ERNIE 5.5 (Ernie5_5_MoeForCausalLM) and Megatron-Core HybridModel.
ERNIE 5.5 has no Hugging Face modeling code; :mod:~megatron.bridge.models.ernie.configuration_ernie55
registers its config.json schema with AutoConfig. The bridge maps that config to an
- class:
~megatron.bridge.models.ernie.ernie55_config.Ernie55ModelConfig, whose builder constructs- class:
~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel:
layout: every ERNIE layer becomes two hybrid layers,
H(compress_ratios128) orD(-2) followed by-(the firstfirst_k_dense_replacelayers) orE; the trailingcompress_ratiosentries are the MTP depths ("H-DEHEDE/DE"for the 4-layer proxy);H layers: the stack geometry (
num_attention_heads,q_lora_rank,v_head_dim,qk_rope_head_dim,o_groupsxo_lora_rank), thesliding_windowwindow and RoPE atcompress_rope_theta(plain RoPE or YaRN, perhca_rope_type);D layers:
hybrid_mla_*into theernie_d_*fields with RoPE atrope_theta, and the DSA indexer (index_n_headsxindex_head_dim, top-index_topk); the checkpointās RoPE channel layout (mqa_latent_rope_adjacent_pairing) is kept for weight conversion and export;MoE: the split-feature sigmoid router with the noaux_tc expert bias, the learned per-expert scale (initialized to
routed_scaling_factor),n_shared_expertsshared experts, optional latent MoE and SiTU-GLU;hyper-connections (
hc_multstreams,hc_sinkhorn_itersSinkhorn iterations), SD-MTP (mtp_shared_last_layer), the untied output layer and the unpadded vocabulary.
Settings the Hugging Face config does not carry and the architecture fixes: the VHA rank (16 unless the config
sets vha_rank), the fixed indexer settings, position_embedding_type="none" (every attention owns its
RoPE) and a frozen expert bias (moe_router_bias_update_rate=0; training recipes set the update rate).
seq_length is the recipeās, not max_position_embeddings. Generic CONFIG_MAPPING entries that would
misread ERNIE keys are dropped: num_key_value_heads (one shared latent head, not GQA groups), head_dim
(not the MCore kv_channels) and max_position_embeddings.
Configs outside the supported envelope fail at mapping time: H-layer RoPE other than plain RoPE or YaRN
(hca_rope_type other than "rope" or "yarn"), D layers other than latent MQA with DSA (hybrid_mla_attention other than "mqa_dsa"), missing
q/kv norms (use_qk_norm), tied embeddings, biases, dropout, a hyper-connection epsilon other than 1e-6,
MTP depths with different layers and MoE keys that the reference implementation would read under another name
with another value (:func:~megatron.bridge.models.ernie.configuration_ernie55.check_ernie55_reference_key_spellings).
Weight conversion: :class:~megatron.bridge.models.ernie.ernie55_mappings.Ernie55WeightConversionMixin gives
the bridge its mapping registry and checks on import that a checkpoint holds exactly the tensors of its config.
Module Contents#
Classes#
Bridge for ERNIE 5.5: Hugging Face config to :class: |
Functions#
H-layer RoPE fields of an ERNIE 5.5 Hugging Face config. |
|
Flat Megatron config fields of an ERNIE 5.5 Hugging Face config. |
Data#
VHA rank of ERNIE 5.5 when the Hugging Face config has no |
|
Hyper-connection epsilon of the ERNIE 5.5 arithmetic (Sinkhorn normalizers, mapping norm and gates). |
|
API#
- bridge.models.ernie.ernie55_bridge.__all__#
[āERNIE55_DEFAULT_VHA_RANKā, āERNIE55_HC_EPSā, āErnie55Bridgeā]
- bridge.models.ernie.ernie55_bridge.ERNIE55_DEFAULT_VHA_RANK#
16
VHA rank of ERNIE 5.5 when the Hugging Face config has no
vha_rankkey (the real configs have none).
- bridge.models.ernie.ernie55_bridge.ERNIE55_HC_EPS#
1e-06
Hyper-connection epsilon of the ERNIE 5.5 arithmetic (Sinkhorn normalizers, mapping norm and gates).
- bridge.models.ernie.ernie55_bridge._DROPPED_GENERIC_FIELDS#
(ānum_query_groupsā, ākv_channelsā, āseq_lengthā, ā_mla_rope_paramsā)
- bridge.models.ernie.ernie55_bridge._TRANSFORMERS5_FAST_TOKENIZER_CLASS#
āTokenizersBackendā
- bridge.models.ernie.ernie55_bridge._FAST_TOKENIZER_CLASS#
āPreTrainedTokenizerFastā
- bridge.models.ernie.ernie55_bridge._required(
- hf_config: transformers.PretrainedConfig,
- name: str,
- bridge.models.ernie.ernie55_bridge._ernie55_hca_rope_settings(
- hf_config: transformers.PretrainedConfig,
H-layer RoPE fields of an ERNIE 5.5 Hugging Face config.
hca_rope_type"rope"(the default) is plain RoPE;"yarn"is Megatron-Coreās YaRN atcompress_rope_theta, as in the reference implementation. Its keys are read from the top level of the config, then fromrope_scaling, then take the reference defaults (beta_fast32,beta_slow1). The YaRN concentration factor is not applied: DSv4 attention rotates withmscale=1.0, as the reference does.- Raises:
ValueError ā If
hca_rope_typeis neither"rope"nor"yarn".
- bridge.models.ernie.ernie55_bridge._ernie55_settings(
- hf_config: transformers.PretrainedConfig,
Flat Megatron config fields of an ERNIE 5.5 Hugging Face config.
- Raises:
ValueError ā If a required key is missing or the config is outside the supported envelope.
- class bridge.models.ernie.ernie55_bridge.Ernie55Bridge#
Bases:
megatron.bridge.models.ernie.ernie55_mappings.Ernie55WeightConversionMixin,megatron.bridge.models.conversion.model_bridge.MegatronModelBridgeBridge for ERNIE 5.5: Hugging Face config to :class:
Ernie55ModelConfig(builder-backed only)... rubric:: Example
from megatron.bridge import AutoBridge model_config = AutoBridge.from_hf_pretrained(ā/path/to/ernie55ā).get_model_config()
There is no model provider for ERNIE 5.5: :meth:
provider_bridgeraises. Use the builder-backed- Meth:
~megatron.bridge.models.conversion.auto_bridge.AutoBridge.get_model_config.
- MODEL_CONFIG_CLASS#
None
- USE_MODEL_CONFIG_FOR_CONVERSION#
True
- classmethod hf_to_megatron_activation(hidden_act: str)#
SwiGLU (
silu) and SiTU-GLU (situ) both useF.silu; SiTU adds Megatron-Coreās clamp scales.
- hf_config_to_model_config_kwargs(
- hf_config: transformers.PretrainedConfig,
Flat :class:
Ernie55ModelConfigkwargs of an ERNIE 5.5 Hugging Face config.- Parameters:
hf_config ā The
ernie5_5_moeconfig.- Returns:
Model and transformer fields;
seq_lengthis left to the recipe.- Raises:
ValueError ā If the config is outside the supported envelope (see the module docstring).
- abstractmethod hf_config_to_provider_kwargs(
- hf_config: transformers.PretrainedConfig,
ERNIE 5.5 has no provider path.
- Raises:
NotImplementedError ā Always.
- provider_bridge(hf_pretrained: Any) Any#
ERNIE 5.5 has no provider path.
- Raises:
NotImplementedError ā Always.
- postprocess_hf_export_artifacts(path: pathlib.Path) None#
Name the exported tokenizerās class so that transformers 4 can load it.
transformers 5 writes
"tokenizer_class": "TokenizersBackend"intotokenizer_config.json; the reference implementation runs transformers 4.57, whoseAutoTokenizerrejects that name. The released checkpointās tokenizer namesPreTrainedTokenizerFast, which transformers 5 keeps as an alias of the same class, so the export restores it. Tokens and every other key stay as saved.- Parameters:
path ā Directory containing the exported Hugging Face artifacts.
- classmethod megatron_to_hf_config(
- provider: megatron.bridge.models.ernie.ernie55_config.Ernie55ModelConfig,
The ERNIE 5.5
config.jsonof an :class:Ernie55ModelConfig.- Parameters:
provider ā The model config.
- Returns:
The Hugging Face config dict (
architectures,model_typeand the ERNIE keys).- Raises:
ValueError ā If the dense layers are not a prefix of the decoder (
first_k_dense_replace).