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:
Ernie55RMSNormis 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 setsernie_reference_rmsnorm; the norm builders here (:func:ernie55_rmsnorm, :func:ernie55_rmsnorm_specand :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 asErnieRMSNorm, but its MTP block (enorm,hnormand the MTP final norm) comes from Megatron-Core’s Transformer Engine MTP spec, so those stayTENorm.
Module Contents#
Classes#
RMSNorm with the reference implementation’s eager arithmetic ( |
|
Norm “class” whose construction returns :class: |
Functions#
Whether |
|
Device for the norm weight, chosen like Megatron-Core’s TE modules (CPU when CUDA is missing). |
|
Return a norm builder that builds :class: |
|
|
|
Make |
|
Whether :func: |
|
Return a norm builder that builds |
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
configselects 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.ModuleRMSNorm with the reference implementation’s eager arithmetic (
ErnieRMSNorm).forwardcomputesx = x.to(w.dtype), then(xf * rsqrt(mean(xf^2) + eps) * w.float())withxf = 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 isweight(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;
normalizationmust be"RMSNorm", withoutlayernorm_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,
- class bridge.models.ernie.modeling_ernie55.norm.Ernie55RMSNormSelector#
Norm “class” whose construction returns :class:
Ernie55RMSNormornorm, likeTENormitself.Ernie55RMSNormSelector(config=..., hidden_size=..., eps=..., norm=TENorm)builds- Class:
Ernie55RMSNormwhenconfigsetsernie_reference_rmsnormandnorm(...)otherwise. It is a class so that Megatron-Core’sbuild_moduleaccepts it inside aModuleSpec(:func:ernie55_rmsnorm_spec); slots that Megatron-Core calls directly take :func:ernie55_rmsnorm.
- __new__(
- *args,
- norm: Callable[..., torch.nn.Module],
- **kwargs,
- bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm(
- norm: Callable[..., torch.nn.Module],
Return a norm builder that builds :class:
Ernie55RMSNormunderernie_reference_rmsnorm, elsenorm.The result goes into layer-spec norm slots that Megatron-Core calls directly with
config,hidden_sizeandeps; the config of that call decides.normstays visible as thenormkeyword, so spec tools can replace it. Slots built withbuild_moduletake- Func:
ernie55_rmsnorm_spec.- Parameters:
norm – The norm builder used without the switch, for example
TENorm.- Returns:
functools.partialof :class:Ernie55RMSNormSelectorwithnormbound.
- bridge.models.ernie.modeling_ernie55.norm.ernie55_rmsnorm_spec(
- norm: Callable[..., torch.nn.Module],
- Func:
ernie55_rmsnormas aModuleSpec, for slots Megatron-Core builds withbuild_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,
- bridge.models.ernie.modeling_ernie55.norm.downcast_norm_input(module: torch.nn.Module) torch.nn.Module#
Make
modulecast its input to its weight dtype before normalizing (in place, idempotent).- Parameters:
module – A norm module with a
weightparameter.- Returns:
module.- Raises:
TypeError – If
modulehas noweightparameter.
- bridge.models.ernie.modeling_ernie55.norm.is_downcasting_norm(module: torch.nn.Module) bool#
Whether :func:
downcast_norm_inputwas applied tomodule.
- bridge.models.ernie.modeling_ernie55.norm._build_downcasting_norm(
- *args,
- norm: Callable[..., torch.nn.Module],
- **kwargs,
- bridge.models.ernie.modeling_ernie55.norm._build_fixed_downcasting_norm(
- *args,
- norm: Callable[..., torch.nn.Module],
- **kwargs,
- bridge.models.ernie.modeling_ernie55.norm.downcasting_norm(
- norm: Callable[..., torch.nn.Module],
- *,
- reference_switch: bool = True,
Return a norm builder that builds
normand applies :func:downcast_norm_input.The result goes into layer-spec norm slots (
input_layernorm,pre_mlp_layernorm, the MTPhnorm), which Megatron-Core calls withconfig,hidden_sizeandeps. Underernie_reference_rmsnormit builds :class:Ernie55RMSNorminstead ofnorm(see- Func:
ernie55_rmsnorm), unlessreference_switchis False: the reference implementation builds its MTP block norms with Transformer Engine, so the MTPhnormalways buildsnorm. The wrapped builder stays visible as thenormkeyword, so spec tools can replace it.- Parameters:
norm – The norm builder, for example
TENorm.reference_switch – Whether
ernie_reference_rmsnormselects :class:Ernie55RMSNorm.
- Returns:
functools.partialof the internal builder withnormbound.