bridge.models.ernie.ernie55_step#

ERNIE 5.5 training step: packed THD micro-batches, context parallelism and the per-row loss.

Select it with --step_func ernie55_step (scripts/training/run_recipe.py) or pass :func:forward_step to finetune/pretrain. Every micro-batch becomes one packed THD row, as in the reference implementation:

  1. The data iterator yields a dict with tokens and labels ([b, T]; labels[t] is the target of position t, -100 for none), optionally loss_mask (0 also drops a target), cu_seqlens (the document boundaries of a b = 1 row; without it every row is one document) and pad_token_id.

    class:

    ~megatron.bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfig produces it, and so can any provider of Bridge’s GPT batches (one document per row).

  2. func:

    ~megatron.bridge.models.ernie.ernie55_context_parallel.build_ernie55_thd_cp_batch pads every document to a multiple of 2 * CP (pad tokens with label -100; a pack too short for the H layers’ CP boundary exchange gets more pads on its last document), puts one cu_seqlens tensor in all four packed fields, prebuilds the context-parallel partition routes and slices this rank’s rows of the model’s cp_partition_mode.

  3. The model gets the labels with -100 replaced by a valid id and the loss mask labels != -100 (Megatron-Core masks the LM and MTP losses with loss_mask).

  4. The loss function follows the model config (:func:ernie55_loss_function): ernie_per_row_loss selects the reference’s per-row loss (:func:~megatron.bridge.models.ernie.ernie55_context_parallel.ernie55_per_row_lm_loss) and requires calculate_per_token_loss=False; otherwise Bridge’s masked_next_token_loss, which at context_parallel_size > 1 requires per-token loss. The MTP depths follow the same flag inside the model.

Module Contents#

Functions#

ernie55_documents_from_batch

The documents of one micro-batch as (input_ids, labels) pairs on the host.

ernie55_loss_function

The LM loss of the ERNIE 5.5 training step, selected from the model config.

get_batch

Read one micro-batch and build this rank’s packed THD batch.

forward_step

Forward training step of ERNIE 5.5.

Data#

API#

bridge.models.ernie.ernie55_step.__all__#

[‘ernie55_documents_from_batch’, ‘ernie55_loss_function’, ‘forward_step’, ‘get_batch’]

bridge.models.ernie.ernie55_step.ernie55_documents_from_batch(
batch: collections.abc.Mapping[str, Any],
) → list[tuple[torch.Tensor, torch.Tensor]]#

The documents of one micro-batch as (input_ids, labels) pairs on the host.

Parameters:

batch – tokens and labels [b, T] (labels aligned with the inputs, -100 for no target), optionally loss_mask [b, T] (where it is 0 the label becomes -100) and cu_seqlens, the n + 1 document boundaries of a single row (b = 1). Rows after the last boundary are padding and are dropped. Without cu_seqlens every row is one document.

Returns:

One (input_ids, labels) pair of equal-length 1-D int64 CPU tensors per document.

Raises:

ValueError – If the shapes disagree, cu_seqlens comes with b > 1, or the boundaries are not increasing from 0 within the row.

bridge.models.ernie.ernie55_step.ernie55_loss_function(
loss_mask: torch.Tensor,
*,
config: Any,
cp_group: torch.distributed.ProcessGroup | None,
check_for_nan_in_loss: bool,
check_for_spiky_loss: bool,
) → functools.partial#

The LM loss of the ERNIE 5.5 training step, selected from the model config.

  • ernie_per_row_loss: :func:~megatron.bridge.models.ernie.ernie55_context_parallel.ernie55_per_row_lm_loss (the micro-batch’s token mean over all of its CP ranks’ tokens); requires calculate_per_token_loss=False.

  • Otherwise Bridge’s masked_next_token_loss. At context_parallel_size > 1 it then needs calculate_per_token_loss=True: a per-rank token mean would weight the ranks of a row equally whatever their supervised tokens.

Parameters:
  • loss_mask – This rank’s loss mask of the micro-batch.

  • config – Model (transformer) config.

  • cp_group – Context-parallel group, or None.

  • check_for_nan_in_loss – Run the rerun state machine’s NaN/Inf check.

  • check_for_spiky_loss – Run the rerun state machine’s spiky-loss check.

Returns:

loss_function(output_tensor) for Megatron-Core’s schedule.

Raises:

ValueError – If the loss settings are inconsistent.

bridge.models.ernie.ernie55_step.get_batch(
data_iterator: collections.abc.Iterable,
config: Any,
*,
cp_group: torch.distributed.ProcessGroup | None,
device: torch.device | str,
) → megatron.bridge.models.ernie.ernie55_context_parallel.Ernie55ThdBatch#

Read one micro-batch and build this rank’s packed THD batch.

Parameters:
  • data_iterator – Yields batches in the contract of :func:ernie55_documents_from_batch, optionally with an int pad_token_id (needed only when a document must be padded to a multiple of 2 * CP).

  • config – Model (transformer) config (cp_partition_mode, and csa_compress_ratios with csa_window_size for the fewest rows a CP rank may hold).

  • cp_group – Context-parallel group, or None.

  • device – Device of the returned tensors.

Returns:

This rank’s rows with the metadata of the whole pack.

Raises:

ValueError – If a document needs padding and the batch has no pad_token_id.

bridge.models.ernie.ernie55_step.forward_step(
state: megatron.bridge.training.state.GlobalState,
data_iterator: collections.abc.Iterable,
model: torch.nn.Module,
return_schedule_plan: bool = False,
) → tuple[torch.Tensor, functools.partial]#

Forward training step of ERNIE 5.5.

Parameters:
  • state – Global training state.

  • data_iterator – The micro-batch iterator (see :func:get_batch).

  • model – The (wrapped) ERNIE 5.5 model chunk.

  • return_schedule_plan – Not supported (no EP communication overlap schedule for ERNIE 5.5).

Returns:

The per-token losses and the loss function (:func:ernie55_loss_function).

Raises:

NotImplementedError – If a schedule plan is requested.