bridge.models.ernie.modeling_ernie55.hybrid_stack#

ERNIE 5.5 decoder stack: Megatron-Core’s HybridStack carrying fp32 residual streams.

ERNIE 5.5 casts the embedding output to fp32 before it expands into the hyper-connection streams, so every decoder residual stream is fp32 while the layers compute in bf16 (their pre-norms downcast the fp32 layer input, see :mod:~megatron.bridge.models.ernie.modeling_ernie55.norm). Megatron-Core cannot express this with configuration: it rejects fp32_residual_connection together with hyper-connections, HybridModel has no hook between the embedding and the decoder, and HybridStack builds its final norm as a plain TENorm.

class:

Ernie55HybridStack adds exactly those pieces:

  • the fp32 cast of the embedding at the decoder entry (pre_process stacks with hyper-connections; MTP stacks, whose streams stay bf16, are Megatron-Core’s own class);

  • a downcasting final norm, since the learned stream contraction returns fp32; under ernie_reference_rmsnorm it is :class:~megatron.bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNorm, the reference implementation’s arithmetic, instead of Megatron-Core’s TENorm;

  • the ERNIE 5.5 hyper-connection arithmetic on its layers (:func:~megatron.bridge.models.ernie.modeling_ernie55.hyper_connection.use_ernie55_hyper_connections).

The MTP stack, which Megatron-Core builds directly as HybridStack, gets the same hyper-connection arithmetic from

class:

~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel.

Module Contents#

Classes#

Ernie55HybridStack

HybridStack with the ERNIE 5.5 fp32 residual-stream contract.

Data#

API#

bridge.models.ernie.modeling_ernie55.hybrid_stack.__all__#

[‘Ernie55HybridStack’]

class bridge.models.ernie.modeling_ernie55.hybrid_stack.Ernie55HybridStack(
config: megatron.core.transformer.transformer_config.TransformerConfig,
*args: Any,
**kwargs: Any,
)#

Bases: megatron.core.models.hybrid.hybrid_block.HybridStack

HybridStack with the ERNIE 5.5 fp32 residual-stream contract.

Constructor arguments are Megatron-Core’s (they differ between Megatron-Core versions and pass through unchanged). With hyper-connections the constructor validates the config with

Func:

~megatron.bridge.models.ernie.modeling_ernie55.hyper_connection.validate_ernie55_mhc_config (which also rejects pipeline parallelism with a pipeline dtype narrower than fp32) and checks that every layer norm the streams reach downcasts its input, so no layer can receive an fp32 activation. With ernie_reference_rmsnorm the final norm is replaced by

Class:

~megatron.bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNorm right after Megatron-Core builds it.

Raises:

TypeError –

If hyper-connections are on and a layer’s input or pre-MLP norm does not downcast (the stack was built from a spec other than

func:

~megatron.bridge.models.ernie.modeling_ernie55.ernie55_hybrid_stack_spec).

Initialization

property carries_fp32_streams: bool#

Whether this stack casts its input to fp32 before expanding it into streams.

forward(
hidden_states: torch.Tensor | megatron.core.utils.WrappedTensor,
*args: Any,
**kwargs: Any,
) → torch.Tensor#

Run Megatron-Core’s HybridStack.forward on the fp32 embedding when the stack carries fp32 streams.

Parameters:
  • hidden_states – The embedding output (pre_process) or ignored (later pipeline stages read input_tensor); may be a WrappedTensor in inference.

  • *args – Passed through.

  • **kwargs – Passed through.

Returns:

Whatever HybridStack.forward returns.