bridge.models.ernie.modeling_ernie55.model#
ERNIE 5.5 model: Megatron-Core’s HybridModel plus the ERNIE post-construction contract.
Module Contents#
Classes#
|
|
Identity and memory of the shared parameters, to find sharded entries that hold them. |
Functions#
|
|
|
|
|
|
Drop aliased entries below |
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.HybridModelHybridModelthat 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_specand Megatron-Core’sHybridModelarguments (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
multimaxgradient-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
multimaxparameters (: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_opstakes 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_lossthe 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()andsharded_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 underernie_per_row_loss.Arguments and return value are
HybridModel.forward’s. Withernie_per_row_losson a stage that runs MTP, Megatron-Core’sprocess_mtp_lossis replaced by- Func:
~megatron.bridge.models.ernie.modeling_ernie55.mtp_loss.ernie55_process_mtp_lossfor 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_layerthe MTP body holds the decoder’s last-layernn.Parameterobjects 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 (LoRALinearand 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_aliasesthen rejects.- Class:
~megatron.bridge.peft.base.PEFTskips 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’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],
state_dictpost-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],
load_state_dictpre-hook: an aliased MTP entry takes its decoder entry; a different value is an error.assign=Trueis 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,
load_state_dictpost-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,
Drop aliased entries below
container[key]; return whethercontainer[key]itself should go.