bridge.models.ernie.modeling_ernie55.mtp_loss#
ERNIE 5.5 per-row MTP loss: every MTP depth normalized by the row’s supervised tokens over the CP group.
With per-row loss (ernie_per_row_loss) the reference implementation normalizes each MTP depth like the LM
loss (:func:~megatron.bridge.models.ernie.ernie55_context_parallel.ernie55_per_row_lm_loss): the row is the
micro-batch, S is the depth’s summed token loss on this context-parallel rank and N the depth’s supervised
tokens summed over the CP group (clamped at 1), after the per-document label and mask roll. The depth injects
mtp_loss_scaling_factor / mtp_num_layers * CP / N times its token losses into the backward
(Megatron-Core’s MTPLossAutoScaler, whose scale the schedule sets to 1 / num_microbatches), and DDP
averages the gradients over DP x CP, so the gradient is that of the mean over micro-batches of each depth’s token
mean over all of its CP ranks’ tokens. The tracker records CP * S / N per micro-batch and averages it over
DP x CP, which logs the mean row MTP loss.
Megatron-Core’s process_mtp_loss divides by this rank’s own tokens instead (S / n_local): the same value at
CP = 1, but wrong at CP > 1, where a row’s supervised tokens are spread unevenly over the ranks. Megatron-Core has
no hook for the normalization, so :class:~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel
runs :func:ernie55_process_mtp_loss in its place for the duration of its forward
(:func:per_row_mtp_loss_in). It reuses Megatron-Core’s pieces: the document-aware (and CP-aware) label and mask
roll with the micro-batch’s prefetched roll context, the output layer (the multimax head), the language-model
loss, the MTP loss tracker, the acceptance counts and MTPLossAutoScaler.
Only the plain cross-entropy MTP objective is supported: labels given (SFT), no MTP input mask, no fused linear
cross-entropy (multimax needs the logits), no logit scaling and no early MTP backward. It needs Megatron-Core dev’s
grouped roll_tensor (:data:PER_ROW_MTP_LOSS_SUPPORTED); on other builds the model keeps Megatron-Core’s own
loss, which is the same at CP = 1, and the ERNIE 5.5 config validation rejects per-row MTP at CP > 1
(:func:~megatron.bridge.models.ernie.modeling_ernie55.context_parallel.validate_ernie55_context_parallel_config).
Module Contents#
Functions#
Per-row MTP loss of one micro-batch; Megatron-Core |
|
Make |
Data#
Whether this Megatron-Core build has the grouped |
API#
- bridge.models.ernie.modeling_ernie55.mtp_loss.__all__#
[‘PER_ROW_MTP_LOSS_SUPPORTED’, ‘ernie55_process_mtp_loss’, ‘per_row_mtp_loss_in’]
- bridge.models.ernie.modeling_ernie55.mtp_loss.PER_ROW_MTP_LOSS_SUPPORTED#
None
Whether this Megatron-Core build has the grouped
roll_tensorand token-aware MTP tracker (dev) the per-row MTP loss calls.
- bridge.models.ernie.modeling_ernie55.mtp_loss.ernie55_process_mtp_loss(
- hidden_states: torch.Tensor,
- labels: torch.Tensor | None,
- loss_mask: torch.Tensor | None,
- output_layer: collections.abc.Callable,
- output_weight: torch.Tensor | None,
- is_training: bool,
- compute_language_model_loss: collections.abc.Callable,
- config: Any,
- cp_group: torch.distributed.ProcessGroup | None = None,
- tp_group: torch.distributed.ProcessGroup | None = None,
- packed_seq_params: Any = None,
- scale_logits_fn: collections.abc.Callable[[torch.Tensor], torch.Tensor] | None = None,
- input_ids: torch.Tensor | None = None,
- sequence_roll_context: Any = None,
- mtp_input_mask: torch.Tensor | None = None,
- metric_avg_group: torch.distributed.ProcessGroup | None = None,
- main_hidden_states: torch.Tensor | None = None,
- dp_cp_group: torch.distributed.ProcessGroup | None = None,
- runtime_gather_output: bool | None = None,
Per-row MTP loss of one micro-batch; Megatron-Core
process_mtp_loss’s arguments and return value.- Parameters:
hidden_states – The decoder output and every MTP depth’s output, concatenated on the sequence axis.
labels –
[b, s]targets (already aligned with the inputs).loss_mask –
[b, s]1 for supervised tokens;Nonesupervises every token.output_layer – The model’s output layer.
output_weight – Shared output weight, or
None(the output layer’s own).is_training – Whether to record the loss and acceptance metrics.
compute_language_model_loss – Per-token cross entropy
(labels, logits) -> [b, s].config – Transformer config (
mtp_num_layers,mtp_loss_scaling_factor,attention_cp_layout).cp_group – Context-parallel group of the MTP layout.
tp_group – Tensor-parallel group (acceptance counts).
packed_seq_params – Packed metadata of the MTP layout (document-aware rolls).
scale_logits_fn – Must be
None.input_ids – Unused (labels are given).
sequence_roll_context – The micro-batch’s prefetched roll context.
mtp_input_mask – Must be
None.metric_avg_group – Group the logged loss is averaged over (DP x CP).
main_hidden_states – The decoder output to return (
None: the first chunk ofhidden_states).dp_cp_group – Fallback for
metric_avg_group.runtime_gather_output – Unused; MTP logits stay vocab-sharded.
- Returns:
The decoder output, carrying every depth’s loss for the backward.
- Raises:
ValueError – Without labels, or with an MTP input mask, logit scaling, fused linear cross-entropy, early MTP backward or an MTP loss type other than cross entropy.
- bridge.models.ernie.modeling_ernie55.mtp_loss.per_row_mtp_loss_in(
- module: Any,
Make
module.process_mtp_lossthe ERNIE 5.5 per-row MTP loss inside thewithblock.moduleis the Megatron-Core module whose forward callsprocess_mtp_lossby its global name (megatron.core.models.hybrid.hybrid_model). The previous binding is restored on exit, also on error.- Parameters:
module – The module to patch.