bridge.models.ernie.modeling_ernie55.mtp#
ERNIE 5.5 shared-decoder MTP (SD-MTP): the MTP body reuses the last decoder layers’ parameters.
With Hugging Face mtp_shared_last_layer (ernie_mtp_shared_last_layer), every MTP depth runs
its own D + MoE layer pair, but that pair’s Parameters are the decoder’s last D + MoE pair: the
attention (indexer, sink, VHA and gate included), both hyper-connection modules, the router
weights and scales, the routed experts and the shared expert. In a hybrid model each ERNIE layer is
two symbols, so the MTP stack "DE" of every depth is aliased onto the last two decoder layers.
Owned by the MTP depth and not shared: enorm, hnorm, e_proj, h_proj, its
hc_head_* contraction and final norm, and all buffers (each MTP router keeps its own
expert_bias and token counts, and updates them from its own routing).
Each shared tensor is one nn.Parameter used twice in the forward, so the gradients of the LM
loss and the MTP loss add up in it. named_parameters() and parameters() return it once,
under the decoder name, so DDP buckets, the optimizer and the gradient norm see it once, and
- class:
~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModelwrites it once to checkpoints.
The aliasing must happen before the model is wrapped for training and before any checkpoint load, and the decoder’s last layers must be on the MTP’s pipeline stage (Megatron-Core runs mHC MTP on the last stage).
Sharing is plain nn.Parameter sharing, so anything that later re-creates parameters module by
module splits each shared tensor into two silently diverging copies: materializing a meta-device
model (init_model_with_meta_device), load_state_dict(assign=True), and the per-module
sharding of Megatron-FSDP or torch FSDP2. :func:share_mtp_body_with_last_layer rejects meta-device
models, :class:~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel rejects
assign=True loads, :func:validate_ernie55_mtp_sharing rejects the FSDP and meta-device
settings for the code that knows them (the training setup), and :func:check_ernie55_mtp_aliases
verifies the sharing after anything that may have replaced parameters.
Module Contents#
Functions#
Alias every MTP depth’s inner layers’ Parameters to the decoder’s last layers (in place). |
|
Verify that every SD-MTP alias is still the decoder’s own |
|
Raise |
Data#
API#
- bridge.models.ernie.modeling_ernie55.mtp.__all__#
[‘check_ernie55_mtp_aliases’, ‘share_mtp_body_with_last_layer’, ‘validate_ernie55_mtp_sharing’]
- bridge.models.ernie.modeling_ernie55.mtp._PARAMETER_ATTRIBUTES#
(‘allreduce’, ‘tensor_model_parallel’, ‘partition_dim’, ‘partition_stride’, ‘sequence_parallel’, ‘ke…
- bridge.models.ernie.modeling_ernie55.mtp._symbols(
- stack: megatron.core.models.hybrid.hybrid_block.HybridStack,
- bridge.models.ernie.modeling_ernie55.mtp._set_parameter(
- root: torch.nn.Module,
- name: str,
- parameter: torch.nn.Parameter,
- model: torch.nn.Module,
Alias every MTP depth’s inner layers’ Parameters to the decoder’s last layers (in place).
- Parameters:
model – A Megatron-Core
HybridModelstage that builds MTP.- Returns:
{mtp parameter name: decoder parameter name}, relative tomodel, one entry per aliased MTP parameter.- Raises:
RuntimeError – If the model has no MTP, if the decoder’s last layers are not on this stage, if a depth’s layer symbols, parameter names, shapes, dtypes or attributes differ from the decoder layers they alias, if a configuration feature contradicts sharing (
mtp_detach_heads,freeze_base_model_for_mtp), or if the model is on the meta device or built for meta-device initialization (materializing it re-creates the parameters module by module, which splits every shared tensor).
- bridge.models.ernie.modeling_ernie55.mtp.check_ernie55_mtp_aliases(
- models: torch.nn.Module | collections.abc.Iterable[torch.nn.Module],
Verify that every SD-MTP alias is still the decoder’s own
nn.Parameter.Cheap (one lookup per alias). Call it after anything that may replace parameters module by module: pre-wrap hooks, wrapping, and checkpoint loads. It finds every module under
modelsthat records SD-MTP aliases (ernie55_mtp_aliases, set by- Class:
~megatron.bridge.models.ernie.modeling_ernie55.model.Ernie55HybridModel), so wrapped models and lists of model chunks work too.- Parameters:
models – A model, a wrapped model or an iterable of model chunks.
- Returns:
The number of aliases checked (0 for models without SD-MTP).
- Raises:
RuntimeError – If an MTP parameter is no longer the decoder parameter it aliases, or if a wrapper or adapter replaced a module behind an alias so the parameter cannot be resolved by name.
- bridge.models.ernie.modeling_ernie55.mtp.validate_ernie55_mtp_sharing(
- config: object,
- *,
- use_megatron_fsdp: bool,
- use_torch_fsdp2: bool,
Raise
ValueErrorif the training setup cannot keep the SD-MTP parameters shared.Megatron-FSDP and torch FSDP2 shard and re-create parameters module by module, and meta-device initialization re-creates them when it materializes the model; each splits the shared tensors. The model config does not carry the FSDP switches, so the code that builds the training setup calls this with them. Configs without
ernie_mtp_shared_last_layerpass.- Parameters:
config – Transformer config.
use_megatron_fsdp – Whether the model will be wrapped with Megatron-FSDP.
use_torch_fsdp2 – Whether the model will be wrapped with torch FSDP2.
- Raises:
ValueError – If
ernie_mtp_shared_last_layeris set together with one of them or withinit_model_with_meta_device.