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_ratios 128) or D (-2) followed by - (the first first_k_dense_replace layers) or E; the trailing compress_ratios entries 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_groups x o_lora_rank), the sliding_window window and RoPE at compress_rope_theta (plain RoPE or YaRN, per hca_rope_type);

  • D layers: hybrid_mla_* into the ernie_d_* fields with RoPE at rope_theta, and the DSA indexer (index_n_heads x index_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_experts shared experts, optional latent MoE and SiTU-GLU;

  • hyper-connections (hc_mult streams, hc_sinkhorn_iters Sinkhorn 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#

Ernie55Bridge

Bridge for ERNIE 5.5: Hugging Face config to :class:Ernie55ModelConfig (builder-backed only).

Functions#

_required

_ernie55_hca_rope_settings

H-layer RoPE fields of an ERNIE 5.5 Hugging Face config.

_ernie55_settings

Flat Megatron config fields of an ERNIE 5.5 Hugging Face config.

Data#

__all__

ERNIE55_DEFAULT_VHA_RANK

VHA rank of ERNIE 5.5 when the Hugging Face config has no vha_rank key (the real configs have none).

ERNIE55_HC_EPS

Hyper-connection epsilon of the ERNIE 5.5 arithmetic (Sinkhorn normalizers, mapping norm and gates).

_DROPPED_GENERIC_FIELDS

_TRANSFORMERS5_FAST_TOKENIZER_CLASS

_FAST_TOKENIZER_CLASS

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_rank key (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,
) → Any#
bridge.models.ernie.ernie55_bridge._ernie55_hca_rope_settings(
hf_config: transformers.PretrainedConfig,
) → dict[str, Any]#

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 at compress_rope_theta, as in the reference implementation. Its keys are read from the top level of the config, then from rope_scaling, then take the reference defaults (beta_fast 32, beta_slow 1). The YaRN concentration factor is not applied: DSv4 attention rotates with mscale=1.0, as the reference does.

Raises:

ValueError – If hca_rope_type is neither "rope" nor "yarn".

bridge.models.ernie.ernie55_bridge._ernie55_settings(
hf_config: transformers.PretrainedConfig,
) → dict[str, Any]#

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.MegatronModelBridge

Bridge 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_bridge raises. 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 use F.silu; SiTU adds Megatron-Core’s clamp scales.

hf_config_to_model_config_kwargs(
hf_config: transformers.PretrainedConfig,
) → dict[str, Any]#

Flat :class:Ernie55ModelConfig kwargs of an ERNIE 5.5 Hugging Face config.

Parameters:

hf_config – The ernie5_5_moe config.

Returns:

Model and transformer fields; seq_length is 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,
) → dict[str, Any]#

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" into tokenizer_config.json; the reference implementation runs transformers 4.57, whose AutoTokenizer rejects that name. The released checkpoint’s tokenizer names PreTrainedTokenizerFast, 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,
) → dict[str, Any]#

The ERNIE 5.5 config.json of an :class:Ernie55ModelConfig.

Parameters:

provider – The model config.

Returns:

The Hugging Face config dict (architectures, model_type and the ERNIE keys).

Raises:

ValueError – If the dense layers are not a prefix of the decoder (first_k_dense_replace).