bridge.models.ernie.modeling_ernie55.model#

ERNIE 5.5 model: Megatron-Core’s HybridModel plus the ERNIE post-construction contract.

Module Contents#

Classes#

Ernie55HybridModel

HybridModel that applies the ERNIE 5.5 contract right after Megatron-Core builds it.

_AliasedTensors

Identity and memory of the shared parameters, to find sharded entries that hold them.

Functions#

_drop_mtp_aliases_from_state_dict

state_dict post-hook: drop the aliased MTP entries, which repeat their decoder entries.

_fill_mtp_aliases_from_decoder

load_state_dict pre-hook: an aliased MTP entry takes its decoder entry; a different value is an error.

_check_mtp_aliases_after_load

load_state_dict post-hook: the load must have kept every alias shared.

_drop_aliased_entries

Drop aliased entries below container[key]; return whether container[key] itself should go.

Data#

API#

bridge.models.ernie.modeling_ernie55.model.__all__#

[‘Ernie55HybridModel’]

class bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel(
config: megatron.core.transformer.transformer_config.TransformerConfig,
*args: Any,
**kwargs: Any,
)#

Bases: megatron.core.models.hybrid.hybrid_model.HybridModel

HybridModel that applies the ERNIE 5.5 contract right after Megatron-Core builds it.

Build it with :func:~megatron.bridge.models.ernie.modeling_ernie55.ernie55_hybrid_stack_spec and Megatron-Core’s HybridModel arguments (passed through unchanged). After construction:

  • every hyper-connection module, the MTP stack’s included, uses the ERNIE 5.5 eager fp32 arithmetic (:mod:~megatron.bridge.models.ernie.modeling_ernie55.hyper_connection);

  • the multimax gradient-norm group is registered with Megatron-Core, on every pipeline stage (the optimizer’s per-group collectives need the same group list on every rank);

  • the output layer, when this stage has one, applies multimax to the main and the MTP logits and owns the multimax parameters (:mod:~megatron.bridge.models.ernie.modeling_ernie55.output_layer);

  • with ernie_mtp_shared_last_layer, every MTP depth’s inner layers share the Parameters of the decoder’s last layers (:mod:~megatron.bridge.models.ernie.modeling_ernie55.mtp);

  • every attention module is checked to be an ERNIE H or D layer;

  • ernie_fused_ops takes effect: the Triton kernels’ dispatchers are installed in Megatron-Core, and the SiTU-GLU kernel is turned on or off for this model’s clamp scales (:mod:~megatron.bridge.models.ernie.modeling_ernie55.fused_dispatch).

With ernie_per_row_loss the forward normalizes every MTP depth per row, with the row’s supervised tokens counted over the context-parallel group, instead of Megatron-Core’s per-rank token mean (:mod:~megatron.bridge.models.ernie.modeling_ernie55.mtp_loss).

Shared parameters are written to checkpoints once, under their decoder names: state_dict() and sharded_state_dict() leave out the aliased MTP entries, and loading a state dict fills them from the decoder entries, so strict loading works with checkpoints saved either way. load_state_dict(assign=True), which would give the MTP body its own copies, is rejected, and every load ends with :func:~megatron.bridge.models.ernie.modeling_ernie55.mtp.check_ernie55_mtp_aliases.

All of this happens before DDP, the optimizer and any checkpoint load see the model.

Initialization

forward(*args: Any, **kwargs: Any) → torch.Tensor#

Megatron-Core’s HybridModel.forward, with the per-row MTP loss under ernie_per_row_loss.

Arguments and return value are HybridModel.forward’s. With ernie_per_row_loss on a stage that runs MTP, Megatron-Core’s process_mtp_loss is replaced by

Func:

~megatron.bridge.models.ernie.modeling_ernie55.mtp_loss.ernie55_process_mtp_loss for this call (Megatron-Core builds without the grouped MTP roll keep their own loss, which is the same at CP = 1; the config validation rejects per-row MTP at CP > 1 on them).

peft_excluded_module_paths() → frozenset[str]#

Modules PEFT leaves untouched: the SD-MTP body and the decoder layers it aliases.

With ernie_mtp_shared_last_layer the MTP body holds the decoder’s last-layer nn.Parameter objects in its own modules (:func:~megatron.bridge.models.ernie.modeling_ernie55.mtp.share_mtp_body_with_last_layer). A PEFT method that wraps modules one by one (LoRALinear and friends) would give every shared weight two adapters, one per path, and move the parameters behind wrapper attributes, which

Func:

~megatron.bridge.models.ernie.modeling_ernie55.mtp.check_ernie55_mtp_aliases then rejects.

Class:

~megatron.bridge.peft.base.PEFT skips the paths returned here, so the shared body stays frozen and adapters go on every other targeted module. Stages without the MTP return an empty set.

Returns:

Dotted module paths, relative to this model, of both sides of every SD-MTP alias.

sharded_state_dict(
prefix: str = '',
sharded_offsets: tuple = (),
metadata: dict | None = None,
) → megatron.core.dist_checkpointing.mapping.ShardedStateDict#

Megatron-Core’s sharded state dict without the MTP entries that alias decoder parameters.

Parameters:
  • prefix – Module name prefix.

  • sharded_offsets – Pipeline offsets (empty at the model level).

  • metadata – Sharded-state-dict metadata.

Returns:

The sharded state dict.

bridge.models.ernie.modeling_ernie55.model._drop_mtp_aliases_from_state_dict(
module: bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel,
state_dict: dict[str, Any],
prefix: str,
local_metadata: dict[str, Any],
) → None#

state_dict post-hook: drop the aliased MTP entries, which repeat their decoder entries.

bridge.models.ernie.modeling_ernie55.model._fill_mtp_aliases_from_decoder(
module: bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel,
state_dict: dict[str, Any],
prefix: str,
local_metadata: dict[str, Any],
strict: bool,
missing_keys: list[str],
unexpected_keys: list[str],
error_msgs: list[str],
) → None#

load_state_dict pre-hook: an aliased MTP entry takes its decoder entry; a different value is an error.

assign=True is rejected before anything is loaded: it would wrap the decoder entry and the MTP entry in two separate Parameters.

bridge.models.ernie.modeling_ernie55.model._check_mtp_aliases_after_load(
module: bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel,
incompatible_keys: object,
) → None#

load_state_dict post-hook: the load must have kept every alias shared.

class bridge.models.ernie.modeling_ernie55.model._AliasedTensors(parameters: list)#

Identity and memory of the shared parameters, to find sharded entries that hold them.

Entries normally hold the parameter itself under its own name. Some modules split one parameter into several entries (views), so entries are also matched by memory, which is only meaningful for allocated (non-meta) tensors.

Initialization

holds(entry) → bool#
bridge.models.ernie.modeling_ernie55.model._drop_aliased_entries(
container: dict,
key: str,
aliased: bridge.models.ernie.modeling_ernie55.model._AliasedTensors,
) → bool#

Drop aliased entries below container[key]; return whether container[key] itself should go.