bridge.models.ernie.modeling_ernie55.output_layer#

ERNIE 5.5 multimax output head.

ERNIE 5.5 passes every logit x of the language-model head through a learned piecewise correction with ranges r and scales t (four of each):

multimax(x) = x + t0 relu(r0 - x) + t1 relu(x - r1) + t2 relu(r2 - x)^2 + t3 relu(x - r3)^2

computed in fp32 and returned in the logits dtype. With r = t = 0 it is the identity. The same parameters apply to the main logits and to the logits of every MTP depth, because both come out of the one output layer.

Contract:

  • multimax_ranges and multimax_ts are [4] parameters of the output layer in params_dtype (not fp32-kept), zero-initialized (Hugging Face model.lm_head.multimax_*).

  • Forward over [rows, vocab] chunks of :data:MULTIMAX_CHUNK_ROWS rows; only the logits and the parameters are saved for backward.

  • Backward recomputes each chunk with r and t as fp32 leaves; the logit gradient comes back in the logits dtype, and the r and t gradients are summed over chunks in fp32 and cast to the parameter dtype once.

  • With ernie_fused_ops (the default) and CUDA logits, the reference implementation’s Triton kernels replace the chunked forward and backward (:mod:~megatron.bridge.models.ernie.modeling_ernie55.fused_dispatch): one fp32 pass each, still saving only the logits; the r and t gradients are summed per 4096-logit block and then over the blocks in fp32. CPU logits and ernie_fused_ops=False keep the chunked path.

  • The parameters form their own gradient-norm group, :data:MULTIMAX_GRAD_NORM_GROUP: Megatron-Core leaves them out of the main (logged) gradient norm and clips them by their own norm with the same clip_grad. The group is registered with Megatron-Core lazily (:func:register_multimax_grad_norm_group), so importing this module changes nothing. Every rank must register it, including pipeline stages without an output layer: the optimizer issues one flag all-reduce per registered group over a process group that spans the pipeline stages, so ranks with different group lists would issue mismatched collectives.

    class:

    ~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel registers it on every stage; :func:install_ernie55_multimax registers it as well.

  • The logits must exist: fused linear cross-entropy (cross_entropy_fusion_impl="linear") never materializes them and is rejected.

Module Contents#

Classes#

_Ernie55MultimaxFunction

Multimax over [rows, vocab] chunks; backward recomputes each chunk under autograd.

_Ernie55MultimaxOutputLayer

Output-layer mixin (see :func:install_ernie55_multimax): logits pass through multimax.

Functions#

_multimax_fp32

The multimax correction in fp32: x + sum_k t_k f_k(x, r_k), for fp32 x, ranges and scales.

ernie55_multimax

Apply the ERNIE 5.5 multimax correction to logits (fp32 compute, output in the logits dtype).

register_multimax_grad_norm_group

Register :data:MULTIMAX_GRAD_NORM_GROUP in Megatron-Core’s SEPARATE_GRAD_NORM_GROUPS (idempotent).

validate_ernie55_multimax_config

Raise ValueError if config cannot run the multimax head.

install_ernie55_multimax

Give output_layer the multimax parameters and correction (in place, idempotent).

Data#

__all__

MULTIMAX_GRAD_NORM_GROUP

Megatron-Core gradient-norm group of the multimax parameters (param.grad_norm_group).

MULTIMAX_PARAMETER_NAMES

Output-layer parameter names; the Hugging Face keys are model.lm_head.<name>.

MULTIMAX_CHUNK_ROWS

Logit rows per multimax chunk in forward and backward (the reference implementation’s chunk).

_NUM_TERMS

API#

bridge.models.ernie.modeling_ernie55.output_layer.__all__#

[‘MULTIMAX_CHUNK_ROWS’, ‘MULTIMAX_GRAD_NORM_GROUP’, ‘MULTIMAX_PARAMETER_NAMES’, ‘ernie55_multimax’, …

bridge.models.ernie.modeling_ernie55.output_layer.MULTIMAX_GRAD_NORM_GROUP#

‘multimax’

Megatron-Core gradient-norm group of the multimax parameters (param.grad_norm_group).

bridge.models.ernie.modeling_ernie55.output_layer.MULTIMAX_PARAMETER_NAMES#

(‘multimax_ranges’, ‘multimax_ts’)

Output-layer parameter names; the Hugging Face keys are model.lm_head.<name>.

bridge.models.ernie.modeling_ernie55.output_layer.MULTIMAX_CHUNK_ROWS#

512

Logit rows per multimax chunk in forward and backward (the reference implementation’s chunk).

bridge.models.ernie.modeling_ernie55.output_layer._NUM_TERMS#

4

bridge.models.ernie.modeling_ernie55.output_layer._multimax_fp32(
x: torch.Tensor,
ranges: torch.Tensor,
scales: torch.Tensor,
) → torch.Tensor#

The multimax correction in fp32: x + sum_k t_k f_k(x, r_k), for fp32 x, ranges and scales.

class bridge.models.ernie.modeling_ernie55.output_layer._Ernie55MultimaxFunction#

Bases: torch.autograd.Function

Multimax over [rows, vocab] chunks; backward recomputes each chunk under autograd.

Outputs are freshly allocated tensors, not views: the cross-entropy that consumes the logits may modify an fp32 input in place.

static forward(
ctx,
logits: torch.Tensor,
ranges: torch.Tensor,
scales: torch.Tensor,
) → torch.Tensor#
static backward(
ctx,
grad_output: torch.Tensor,
) → tuple[torch.Tensor, torch.Tensor, torch.Tensor]#
bridge.models.ernie.modeling_ernie55.output_layer.ernie55_multimax(
logits: torch.Tensor,
ranges: torch.Tensor,
scales: torch.Tensor,
*,
fused: bool = False,
) → torch.Tensor#

Apply the ERNIE 5.5 multimax correction to logits (fp32 compute, output in the logits dtype).

Parameters:
  • logits – [..., vocab] logits.

  • ranges – [4] ranges r.

  • scales – [4] scales t.

  • fused – Run the Triton forward and backward when the logits are on a CUDA device in a float dtype the kernel takes and Triton imports (ernie_fused_ops); otherwise, and always on CPU, the chunked fp32 path runs.

Returns:

Corrected logits, same shape and dtype as logits, in a new tensor.

class bridge.models.ernie.modeling_ernie55.output_layer._Ernie55MultimaxOutputLayer#

Bases: torch.nn.Module

Output-layer mixin (see :func:install_ernie55_multimax): logits pass through multimax.

forward(
input_: torch.Tensor,
*args: Any,
**kwargs: Any,
) → tuple[torch.Tensor, torch.Tensor | None]#
bridge.models.ernie.modeling_ernie55.output_layer.register_multimax_grad_norm_group() → None#

Register :data:MULTIMAX_GRAD_NORM_GROUP in Megatron-Core’s SEPARATE_GRAD_NORM_GROUPS (idempotent).

Megatron-Core reads that module-level tuple each time it computes or clips gradient norms, so registering once per process before the optimizer is built is enough. It is process-wide, and it must happen on every rank that shares an optimizer’s gradient-statistics group, whether or not the rank owns multimax parameters: for each registered group the optimizer all-reduces a flag over that group (once; Megatron-Core caches the answer), so an unused group costs one reduction.

bridge.models.ernie.modeling_ernie55.output_layer.validate_ernie55_multimax_config(config: object) → None#

Raise ValueError if config cannot run the multimax head.

The logits must be materialized (no fused linear cross-entropy), the head unsharded (tensor_model_parallel_size=1; the multimax gradient would otherwise be a per-shard partial sum), and not scaled after the output layer (no muP).

Parameters:

config – Transformer config.

Raises:

ValueError – On the first violated requirement.

bridge.models.ernie.modeling_ernie55.output_layer.install_ernie55_multimax(
output_layer: torch.nn.Module,
config: object,
) → None#

Give output_layer the multimax parameters and correction (in place, idempotent).

Registers zero-initialized multimax_ranges and multimax_ts ([4], params_dtype, tagged with :data:MULTIMAX_GRAD_NORM_GROUP), registers that group with Megatron-Core, and makes the layer’s forward return multimax-corrected logits. Call it before the model is wrapped for training (DDP, optimizer). Every call, the first included, sets the layer’s ernie55_fused_multimax attribute from config.ernie_fused_ops (False for configs without the field), which selects the Triton kernels for CUDA logits (:func:ernie55_multimax).

Parameters:
  • output_layer – The model’s output layer (Megatron-Core’s LinearCrossEntropyModule, a ColumnParallelLinear subclass).

  • config – Transformer config.

Raises:

ValueError – If config fails :func:validate_ernie55_multimax_config.