bridge.models.ernie.modeling_ernie55.stack_spec#
Hybrid stack spec for ERNIE 5.5: Megatron-Core’s hybrid_stack_spec with the ERNIE layers.
Module Contents#
Functions#
Copy module specs, submodule dataclasses, |
|
Replace the |
|
Replace the |
|
Give a private copy of Megatron-Core’s |
|
Return a copy of a Megatron-Core DSv4 CSA layer spec turned into the ERNIE 5.5 H layer. |
|
Give the dense |
|
Swap :class: |
|
Return a private copy of Megatron-Core’s |
Data#
API#
- bridge.models.ernie.modeling_ernie55.stack_spec.__all__#
[‘ernie55_hybrid_stack_spec’]
- bridge.models.ernie.modeling_ernie55.stack_spec._HCA_LAYER_SLOTS#
(‘csa_layer’, ‘csa_qk_layernorm_layer’, ‘hca_layer’)
- bridge.models.ernie.modeling_ernie55.stack_spec._copy_spec_tree(node: object, memo: dict[int, object]) object#
Copy module specs, submodule dataclasses,
functools.partialobjects and containers.Every other leaf is shared: module classes, functions and similar immutable objects.
copy.deepcopywould return classes unchanged too, but it fails on the placeholders Megatron-Core substitutes for Transformer Engine modules when TE is not installed; sharing them keeps the factory usable on machines without TE. Partials are rebuilt around the same function with copied arguments, because Megatron-Core binds submodule dataclasses into them. Objects referenced more than once stay shared within the copy.
- bridge.models.ernie.modeling_ernie55.stack_spec._use_downcasting_norm(
- submodules: object,
- slot: str,
- expected: type,
- *,
- reference_switch: bool = True,
Replace the
expectednorm insubmodules.<slot>by its downcasting version (in place).
- bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie55_rmsnorm(
- submodules: object,
- slot: str,
- expected: type,
Replace the
expectednorm insubmodules.<slot>by :func:ernie55_rmsnormaround it (in place).
- bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie55_compressor_norm(
- compressor: object,
- expected: type,
Give a private copy of Megatron-Core’s
Compressorspec the :func:ernie55_rmsnormnorm (in place).
- bridge.models.ernie.modeling_ernie55.stack_spec._ernie55_hca_layer_spec(
- mcore_layer: megatron.core.transformer.spec_utils.ModuleSpec,
Return a copy of a Megatron-Core DSv4 CSA layer spec turned into the ERNIE 5.5 H layer.
The layer keeps Megatron-Core’s
TransformerLayer, input norm, projections,Compressorspec and bias-dropout-add. The attention becomes :class:Ernie55HCASelfAttentionwith aTELinearoutput gate, the core becomes :class:Ernie55HCACoreAttentionwithout an indexer, and the q/kv latent norms are explicitTENormmodules (ERNIE 5.5 always normalizes the latents, whateverqk_layernormsays). Every RMSNorm (input, q/kv latents, compressor) is built through :func:~megatron.bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm, which picks the reference arithmetic underernie_reference_rmsnorm.
- bridge.models.ernie.modeling_ernie55.stack_spec._use_unfused_dense_mlp(
- mlp_layer: megatron.core.transformer.spec_utils.ModuleSpec,
- *,
- norm: type,
- linear_fc1: type,
Give the dense
-layer a standalone downcastingpre_mlp_layernormand a plain fc1 (in place).
- bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie_moe(
- moe_layer: megatron.core.transformer.spec_utils.ModuleSpec,
Swap :class:
Ernie55MoELayerand :class:Ernie55SplitFeatureRouterinto theElayer (in place).
- bridge.models.ernie.modeling_ernie55.stack_spec.ernie55_hybrid_stack_spec(
- config: object | None = None,
Return a private copy of Megatron-Core’s
hybrid_stack_specwith the ERNIE 5.5 layers.Megatron-Core’s shared spec is never mutated. Changes:
module: :class:~megatron.bridge.models.ernie.modeling_ernie55.hybrid_stack.Ernie55HybridStack, the decoder stack that carries fp32 residual streams with hyper-connections.Downcasting norms (:func:
~megatron.bridge.models.ernie.modeling_ernie55.norm.downcasting_normaroundTENorm) in every slot that can receive an fp32 stream: theinput_layernormofDandHlayers, thepre_mlp_layernormof-andElayers, and the MTPhnorm(mtp_block_spec). They round the fp32 layer input to bf16 once, as the reference model’s norms do; with bf16 inputs they are plainTENormmodules.The D and H q/kv latent norms and the H compressor’s norm hold
- func:
~megatron.bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnormaroundTENorm. Withernie_reference_rmsnormthese builders and the downcasting ones of the layers build- class:
~megatron.bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNorm, the reference implementation’s eager arithmetic, instead ofTENorm; the decoder’s final norm follows the same switch in :class:~megatron.bridge.models.ernie.modeling_ernie55.hybrid_stack.Ernie55HybridStack. The MTP block’senorm,hnormand finallayer_normstayTENorm, as in the reference, which builds them from Megatron-Core’s Transformer Engine MTP spec.
dsa_layer(D):self_attention.modulebecomes :class:Ernie55LatentMQASelfAttention;core_attentionbecomes :class:Ernie55LatentMQACoreAttention, reusing the originalDSAIndexerspec and adding aTELinearoutput gate;q_layernormandkv_layernormbecomeTENorm(throughernie55_rmsnorm_spec). Explicit latent norms keep the q/kv RMSNorms standalone (Megatron-Core’s QK-norm resolver would otherwise fuse them into the up-projections, which AbsorbedMLA bypasses), whatever the stack’s attention variant.
csa_layer,csa_qk_layernorm_layerand, on Megatron-Core dev,hca_layer(H): every slot anHlayer can be built from gets its own copy of Megatron-Core’scsa_qk_layernorm_layerwith :class:Ernie55HCASelfAttention, an- class:
Ernie55HCACoreAttentioncore without an indexer, aTELinearoutput gate andTENormlatent and compressor norms (throughernie55_rmsnorm).CandWlayers built from these slots raise, because ERNIE 5.5 has none. Builders should still call- func:
~megatron.bridge.models.ernie.modeling_ernie55.check_ernie55_attention_layerson the built model (:class:~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModeldoes), in case a future Megatron-Core adds a slot this function does not set.
mlp_layer(dense-): a standalonepre_mlp_layernormand a plainTEColumnParallelLinearfc1 instead of the fusedTELayerNormColumnParallelLinear. This matches the reference’s unfused dense layers and its parameter names (pre_mlp_layernorm.weight,mlp.linear_fc1.weight,mlp.linear_fc2.weight), and gives the norm its own slot like theElayer.moe_layer(E):MoELayerbecomes :class:Ernie55MoELayerandMoESubmodules.routerbecomes :class:Ernie55SplitFeatureRouter; the grouped experts and the shared expert stay Megatron-Core’s.
The MLP activation (SwiGLU or SiTU-GLU) comes from the config, not the spec. The nested MTP stack reuses these slots, so MTP D, H, dense and MoE layers get the same modules; the factory raises
TypeErrorif Megatron-Core’s spec ever provides separate MTP layer slots (mtp_stack_submodules). Megatron-Core builds the MTP stack itself as a plainHybridStack(its streams stay bf16);- Class:
~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModelgives it the ERNIE hyper-connection arithmetic.- Parameters:
config – Model config (flat or nested); accepted so builders can call the factory with their config. The spec does not depend on it.
- Returns:
A new
Ernie55HybridStackmodule spec.- Raises:
TypeError – If Megatron-Core’s
hybrid_stack_specno longer has the layout this factory edits.