bridge.models.ernie.modeling_ernie55.norm#

ERNIE 5.5 RMSNorms: the downcast of fp32 streams, and the reference implementation’s arithmetic.

With hyper-connections, ERNIE 5.5 carries the decoder’s residual streams in fp32: the embedding is cast to fp32 at the stack entry, and every mix of the streams runs in fp32. The layers themselves compute in bf16. The reference model’s RMSNorm therefore casts its input to the weight dtype first, then computes fp32 statistics and returns the weight dtype. A norm built here does the same: it is the configured norm module (Transformer Engine’s TENorm in the stack spec), unchanged and under its own parameter names, plus a forward pre-hook that casts the input to the norm weight’s dtype.

That cast is the only rounding between an fp32 stream and a layer, so a downcasting norm sits in every place that can receive a stream: each layer’s input_layernorm and pre_mlp_layernorm, the decoder’s final norm, and the MTP hnorm. Inputs that already have the weight dtype pass through unchanged, so the norms behave like the plain ones in stacks without hyper-connections. Autograd casts the gradient back to the input dtype.

Reference arithmetic (ernie_reference_rmsnorm). Transformer Engine’s RMSNorm and the reference implementation’s ErnieRMSNorm compute the same function, bf16(x * rsqrt(mean(x^2) + eps) * w) with fp32 statistics, but they sum the squares in different orders: TE’s kernel adds each thread’s squares in sequence and then reduces across a warp, PyTorch’s reduction kernel uses its own tree. The mean square then differs by an fp32 ulp on about a third of the rows, and when x * rsqrt lands on a bf16 rounding tie, the output differs by a bf16 ulp. TE also switches kernels when the row count is a multiple of 4, so a token’s norm output depends on how many tokens its micro-batch holds.

class:

Ernie55RMSNorm is the reference’s eager expression, statement for statement, and reproduces its outputs and gradients bitwise. It is slower than TE’s kernel (it keeps fp32 copies for the backward), so it runs only when the config sets ernie_reference_rmsnorm; the norm builders here (:func:ernie55_rmsnorm, :func:ernie55_rmsnorm_spec and :func:downcasting_norm) pick it or the configured norm per module. The reference builds the norms of every layer, the attention latents, the H compressor and the decoder’s final norm as ErnieRMSNorm, but its MTP block (enorm, hnorm and the MTP final norm) comes from Megatron-Core’s Transformer Engine MTP spec, so those stay TENorm.

Module Contents#

Classes#

Ernie55RMSNorm

RMSNorm with the reference implementation’s eager arithmetic (ErnieRMSNorm).

Ernie55RMSNormSelector

Norm “class” whose construction returns :class:Ernie55RMSNorm or norm, like TENorm itself.

Functions#

uses_reference_rmsnorm

Whether config selects the reference implementation’s RMSNorm arithmetic (ernie_reference_rmsnorm).

_norm_device

Device for the norm weight, chosen like Megatron-Core’s TE modules (CPU when CUDA is missing).

_config_argument

_build_ernie55_rmsnorm

ernie55_rmsnorm

Return a norm builder that builds :class:Ernie55RMSNorm under ernie_reference_rmsnorm, else norm.

ernie55_rmsnorm_spec

func:

ernie55_rmsnorm as a ModuleSpec, for slots Megatron-Core builds with build_module.

param norm:

The norm builder used without the switch, for example TENorm.

returns:

ModuleSpec(module=Ernie55RMSNormSelector, params={"norm": norm}).

_cast_input_to_weight_dtype

downcast_norm_input

Make module cast its input to its weight dtype before normalizing (in place, idempotent).

is_downcasting_norm

Whether :func:downcast_norm_input was applied to module.

_build_downcasting_norm

_build_fixed_downcasting_norm

downcasting_norm

Return a norm builder that builds norm and applies :func:downcast_norm_input.

Data#

API#

bridge.models.ernie.modeling_ernie55.norm.__all__#

[‘Ernie55RMSNorm’, ‘Ernie55RMSNormSelector’, ‘downcast_norm_input’, ‘downcasting_norm’, ‘ernie55_rms…

bridge.models.ernie.modeling_ernie55.norm._DOWNCAST_ATTR#

‘_ernie55_downcasts_input’

bridge.models.ernie.modeling_ernie55.norm.uses_reference_rmsnorm(config: object) → bool#

Whether config selects the reference implementation’s RMSNorm arithmetic (ernie_reference_rmsnorm).

bridge.models.ernie.modeling_ernie55.norm._norm_device(config: object) → torch.device | str#

Device for the norm weight, chosen like Megatron-Core’s TE modules (CPU when CUDA is missing).

class bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNorm(
config: Any,
hidden_size: int,
eps: float = 1e-05,
has_residual: bool = False,
)#

Bases: torch.nn.Module

RMSNorm with the reference implementation’s eager arithmetic (ErnieRMSNorm).

forward computes x = x.to(w.dtype), then (xf * rsqrt(mean(xf^2) + eps) * w.float()) with xf = x.float(), rounded once to the weight dtype: the reference’s expression in the reference’s order, so its outputs and gradients are bitwise the reference’s. The parameter is weight (params_dtype, initialized to ones), the name and layout of Transformer Engine’s RMSNorm, so checkpoints and weight mappings are unchanged.

Parameters:
  • config – Transformer config; normalization must be "RMSNorm", without layernorm_zero_centered_gamma.

  • hidden_size – Normalized width.

  • eps – Epsilon added to the mean square.

  • has_residual – Must be False (no fused residual).

Raises:

ValueError – If the config asks for another normalization, a zero-centered gamma or a fused residual.

Initialization

forward(x: torch.Tensor) → torch.Tensor#

Normalize the last dimension of x; returns the weight dtype.

extra_repr() → str#

Width and epsilon.

bridge.models.ernie.modeling_ernie55.norm._config_argument(args: tuple, kwargs: dict) → object#
bridge.models.ernie.modeling_ernie55.norm._build_ernie55_rmsnorm(
*args,
norm: Callable[..., torch.nn.Module],
**kwargs,
) → torch.nn.Module#
class bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNormSelector#

Norm “class” whose construction returns :class:Ernie55RMSNorm or norm, like TENorm itself.

Ernie55RMSNormSelector(config=..., hidden_size=..., eps=..., norm=TENorm) builds

Class:

Ernie55RMSNorm when config sets ernie_reference_rmsnorm and norm(...) otherwise. It is a class so that Megatron-Core’s build_module accepts it inside a ModuleSpec (:func:ernie55_rmsnorm_spec); slots that Megatron-Core calls directly take :func:ernie55_rmsnorm.

__new__(
*args,
norm: Callable[..., torch.nn.Module],
**kwargs,
) → torch.nn.Module#
bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm(
norm: Callable[..., torch.nn.Module],
) → functools.partial#

Return a norm builder that builds :class:Ernie55RMSNorm under ernie_reference_rmsnorm, else norm.

The result goes into layer-spec norm slots that Megatron-Core calls directly with config, hidden_size and eps; the config of that call decides. norm stays visible as the norm keyword, so spec tools can replace it. Slots built with build_module take

Func:

ernie55_rmsnorm_spec.

Parameters:

norm – The norm builder used without the switch, for example TENorm.

Returns:

functools.partial of :class:Ernie55RMSNormSelector with norm bound.

bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm_spec(
norm: Callable[..., torch.nn.Module],
) → megatron.core.transformer.spec_utils.ModuleSpec#
Func:

ernie55_rmsnorm as a ModuleSpec, for slots Megatron-Core builds with build_module.

Parameters:

norm – The norm builder used without the switch, for example TENorm.

Returns:

ModuleSpec(module=Ernie55RMSNormSelector, params={"norm": norm}).

bridge.models.ernie.modeling_ernie55.norm._cast_input_to_weight_dtype(
module: torch.nn.Module,
args: tuple,
) → tuple#
bridge.models.ernie.modeling_ernie55.norm.downcast_norm_input(module: torch.nn.Module) → torch.nn.Module#

Make module cast its input to its weight dtype before normalizing (in place, idempotent).

Parameters:

module – A norm module with a weight parameter.

Returns:

module.

Raises:

TypeError – If module has no weight parameter.

bridge.models.ernie.modeling_ernie55.norm.is_downcasting_norm(module: torch.nn.Module) → bool#

Whether :func:downcast_norm_input was applied to module.

bridge.models.ernie.modeling_ernie55.norm._build_downcasting_norm(
*args,
norm: Callable[..., torch.nn.Module],
**kwargs,
) → torch.nn.Module#
bridge.models.ernie.modeling_ernie55.norm._build_fixed_downcasting_norm(
*args,
norm: Callable[..., torch.nn.Module],
**kwargs,
) → torch.nn.Module#
bridge.models.ernie.modeling_ernie55.norm.downcasting_norm(
norm: Callable[..., torch.nn.Module],
*,
reference_switch: bool = True,
) → functools.partial#

Return a norm builder that builds norm and applies :func:downcast_norm_input.

The result goes into layer-spec norm slots (input_layernorm, pre_mlp_layernorm, the MTP hnorm), which Megatron-Core calls with config, hidden_size and eps. Under ernie_reference_rmsnorm it builds :class:Ernie55RMSNorm instead of norm (see

Func:

ernie55_rmsnorm), unless reference_switch is False: the reference implementation builds its MTP block norms with Transformer Engine, so the MTP hnorm always builds norm. The wrapped builder stays visible as the norm keyword, so spec tools can replace it.

Parameters:
  • norm – The norm builder, for example TENorm.

  • reference_switch – Whether ernie_reference_rmsnorm selects :class:Ernie55RMSNorm.

Returns:

functools.partial of the internal builder with norm bound.