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:
The data iterator yields a dict with
tokensandlabels([b, T];labels[t]is the target of positiont,-100for none), optionallyloss_mask(0also drops a target),cu_seqlens(the document boundaries of ab = 1row; without it every row is one document) andpad_token_id.- class:
~megatron.bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfigproduces it, and so can any provider of Bridge’s GPT batches (one document per row).
- func:
~megatron.bridge.models.ernie.ernie55_context_parallel.build_ernie55_thd_cp_batchpads every document to a multiple of2 * 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 onecu_seqlenstensor in all four packed fields, prebuilds the context-parallel partition routes and slices this rank’s rows of the model’scp_partition_mode.
The model gets the labels with
-100replaced by a valid id and the loss masklabels != -100(Megatron-Core masks the LM and MTP losses withloss_mask).The loss function follows the model config (:func:
ernie55_loss_function):ernie_per_row_lossselects the reference’s per-row loss (:func:~megatron.bridge.models.ernie.ernie55_context_parallel.ernie55_per_row_lm_loss) and requirescalculate_per_token_loss=False; otherwise Bridge’smasked_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#
The documents of one micro-batch as |
|
The LM loss of the ERNIE 5.5 training step, selected from the model config. |
|
Read one micro-batch and build this rank’s packed THD batch. |
|
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],
The documents of one micro-batch as
(input_ids, labels)pairs on the host.- Parameters:
batch –
tokensandlabels[b, T](labels aligned with the inputs,-100for no target), optionallyloss_mask[b, T](where it is 0 the label becomes-100) andcu_seqlens, then + 1document boundaries of a single row (b = 1). Rows after the last boundary are padding and are dropped. Withoutcu_seqlensevery 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_seqlenscomes withb > 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,
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); requirescalculate_per_token_loss=False.Otherwise Bridge’s
masked_next_token_loss. At context_parallel_size > 1 it then needscalculate_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,
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 intpad_token_id(needed only when a document must be padded to a multiple of2 * CP).config – Model (transformer) config (
cp_partition_mode, andcsa_compress_ratioswithcsa_window_sizefor 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,
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.