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_spec_tree

Copy module specs, submodule dataclasses, functools.partial objects and containers.

_use_downcasting_norm

Replace the expected norm in submodules.<slot> by its downcasting version (in place).

_use_ernie55_rmsnorm

Replace the expected norm in submodules.<slot> by :func:ernie55_rmsnorm around it (in place).

_use_ernie55_compressor_norm

Give a private copy of Megatron-Core’s Compressor spec the :func:ernie55_rmsnorm norm (in place).

_ernie55_hca_layer_spec

Return a copy of a Megatron-Core DSv4 CSA layer spec turned into the ERNIE 5.5 H layer.

_use_unfused_dense_mlp

Give the dense - layer a standalone downcasting pre_mlp_layernorm and a plain fc1 (in place).

_use_ernie_moe

Swap :class:Ernie55MoELayer and :class:Ernie55SplitFeatureRouter into the E layer (in place).

ernie55_hybrid_stack_spec

Return a private copy of Megatron-Core’s hybrid_stack_spec with the ERNIE 5.5 layers.

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.partial objects and containers.

Every other leaf is shared: module classes, functions and similar immutable objects. copy.deepcopy would 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,
) → None#

Replace the expected norm in submodules.<slot> by its downcasting version (in place).

bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie55_rmsnorm(
submodules: object,
slot: str,
expected: type,
) → None#

Replace the expected norm in submodules.<slot> by :func:ernie55_rmsnorm around it (in place).

bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie55_compressor_norm(
compressor: object,
expected: type,
) → None#

Give a private copy of Megatron-Core’s Compressor spec the :func:ernie55_rmsnorm norm (in place).

bridge.models.ernie.modeling_ernie55.stack_spec._ernie55_hca_layer_spec(
mcore_layer: megatron.core.transformer.spec_utils.ModuleSpec,
) → 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, Compressor spec and bias-dropout-add. The attention becomes :class:Ernie55HCASelfAttention with a TELinear output gate, the core becomes :class:Ernie55HCACoreAttention without an indexer, and the q/kv latent norms are explicit TENorm modules (ERNIE 5.5 always normalizes the latents, whatever qk_layernorm says). 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 under ernie_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,
) → None#

Give the dense - layer a standalone downcasting pre_mlp_layernorm and a plain fc1 (in place).

bridge.models.ernie.modeling_ernie55.stack_spec._use_ernie_moe(
moe_layer: megatron.core.transformer.spec_utils.ModuleSpec,
) → None#

Swap :class:Ernie55MoELayer and :class:Ernie55SplitFeatureRouter into the E layer (in place).

bridge.models.ernie.modeling_ernie55.stack_spec.ernie55_hybrid_stack_spec(
config: object | None = None,
) → megatron.core.transformer.spec_utils.ModuleSpec#

Return a private copy of Megatron-Core’s hybrid_stack_spec with 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_norm around TENorm) in every slot that can receive an fp32 stream: the input_layernorm of D and H layers, the pre_mlp_layernorm of - and E layers, and the MTP hnorm (mtp_block_spec). They round the fp32 layer input to bf16 once, as the reference model’s norms do; with bf16 inputs they are plain TENorm modules.

  • The D and H q/kv latent norms and the H compressor’s norm hold

    func:

    ~megatron.bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm around TENorm. With ernie_reference_rmsnorm these 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 of TENorm; the decoder’s final norm follows the same switch in :class:~megatron.bridge.models.ernie.modeling_ernie55.hybrid_stack.Ernie55HybridStack. The MTP block’s enorm, hnorm and final layer_norm stay TENorm, as in the reference, which builds them from Megatron-Core’s Transformer Engine MTP spec.

  • dsa_layer (D):

    • self_attention.module becomes :class:Ernie55LatentMQASelfAttention;

    • core_attention becomes :class:Ernie55LatentMQACoreAttention, reusing the original DSAIndexer spec and adding a TELinear output gate;

    • q_layernorm and kv_layernorm become TENorm (through ernie55_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_layer and, on Megatron-Core dev, hca_layer (H): every slot an H layer can be built from gets its own copy of Megatron-Core’s csa_qk_layernorm_layer with :class:Ernie55HCASelfAttention, an

    class:

    Ernie55HCACoreAttention core without an indexer, a TELinear output gate and TENorm latent and compressor norms (through ernie55_rmsnorm). C and W layers 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_layers on the built model (:class:~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel does), in case a future Megatron-Core adds a slot this function does not set.

  • mlp_layer (dense -): a standalone pre_mlp_layernorm and a plain TEColumnParallelLinear fc1 instead of the fused TELayerNormColumnParallelLinear. 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 the E layer.

  • moe_layer (E): MoELayer becomes :class:Ernie55MoELayer and MoESubmodules.router becomes :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 TypeError if Megatron-Core’s spec ever provides separate MTP layer slots (mtp_stack_submodules). Megatron-Core builds the MTP stack itself as a plain HybridStack (its streams stay bf16);

Class:

~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel gives 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 Ernie55HybridStack module spec.

Raises:

TypeError – If Megatron-Core’s hybrid_stack_spec no longer has the layout this factory edits.