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_rangesandmultimax_tsare[4]parameters of the output layer inparams_dtype(not fp32-kept), zero-initialized (Hugging Facemodel.lm_head.multimax_*).Forward over
[rows, vocab]chunks of :data:MULTIMAX_CHUNK_ROWSrows; only the logits and the parameters are saved for backward.Backward recomputes each chunk with
randtas fp32 leaves; the logit gradient comes back in the logits dtype, and therandtgradients 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; therandtgradients are summed per 4096-logit block and then over the blocks in fp32. CPU logits andernie_fused_ops=Falsekeep 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 sameclip_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.Ernie55HybridModelregisters it on every stage; :func:install_ernie55_multimaxregisters 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#
Multimax over |
|
Output-layer mixin (see :func: |
Functions#
The multimax correction in fp32: |
|
Apply the ERNIE 5.5 multimax correction to |
|
Register :data: |
|
Raise |
|
Give |
Data#
Megatron-Core gradient-norm group of the multimax parameters ( |
|
Output-layer parameter names; the Hugging Face keys are |
|
Logit rows per multimax chunk in forward and backward (the reference implementation’s chunk). |
|
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,
The multimax correction in fp32:
x + sum_k t_k f_k(x, r_k), for fp32x,rangesandscales.
- class bridge.models.ernie.modeling_ernie55.output_layer._Ernie55MultimaxFunction#
Bases:
torch.autograd.FunctionMultimax 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,
- static backward(
- ctx,
- grad_output: torch.Tensor,
- bridge.models.ernie.modeling_ernie55.output_layer.ernie55_multimax(
- logits: torch.Tensor,
- ranges: torch.Tensor,
- scales: torch.Tensor,
- *,
- fused: bool = False,
Apply the ERNIE 5.5 multimax correction to
logits(fp32 compute, output in the logits dtype).- Parameters:
logits –
[..., vocab]logits.ranges –
[4]rangesr.scales –
[4]scalest.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.ModuleOutput-layer mixin (see :func:
install_ernie55_multimax): logits pass through multimax.- forward(
- input_: torch.Tensor,
- *args: Any,
- **kwargs: Any,
- bridge.models.ernie.modeling_ernie55.output_layer.register_multimax_grad_norm_group() None#
Register :data:
MULTIMAX_GRAD_NORM_GROUPin Megatron-Core’sSEPARATE_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
ValueErrorifconfigcannot 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,
Give
output_layerthe multimax parameters and correction (in place, idempotent).Registers zero-initialized
multimax_rangesandmultimax_ts([4],params_dtype, tagged with :data:MULTIMAX_GRAD_NORM_GROUP), registers that group with Megatron-Core, and makes the layer’sforwardreturn multimax-corrected logits. Call it before the model is wrapped for training (DDP, optimizer). Every call, the first included, sets the layer’sernie55_fused_multimaxattribute fromconfig.ernie_fused_ops(Falsefor 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, aColumnParallelLinearsubclass).config – Transformer config.
- Raises:
ValueError – If
configfails :func:validate_ernie55_multimax_config.