Skip to navigation

nemo_automodel.components.models.ernie5_5.mtp_training

View as MarkdownOpen in Claude

One-depth shared-last-layer ERNIE 5.5 multi-token prediction training.

Semantics reference (independent implementation): NVIDIA-NeMo/Megatron-Bridge 60f7921aa13bff30d801c6b37c063a29786e3626, ERNIE modeling_ernie55/mtp.py, Apache-2.0. Reuses AutoModel’s actual decoder block, norms and MultiMax. The generic MTPModule consumes post-norm 3-D hidden states; this envelope instead needs the final 4-D streams and a second call to a single registered decoder body.

Module Contents

Classes

NameDescription
Ernie5_5MTPEnvelopeOwn exactly eight trainable MTP tensors and one persistent router buffer.
Ernie5_5MTPForCausalLMTrain one shared-last-layer MTP depth with full parameters at CP1 and PP1.
Ernie5_5MTPModelParallelizerRequire the MTP envelope to belong to the outer FSDP root.
Ernie5_5MTPOutputStandard main logits plus one depth’s post-MultiMax auxiliary logits.

API

class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPEnvelope(
)

Bases: Module

Own exactly eight trainable MTP tensors and one persistent router buffer.

No decoder, embedding or vocabulary module is registered or cached here. Resolve the shared body at its decoder path after loading or FSDP wrapping.

e_proj
enorm
final_norm
h_proj
hnorm
hyper_head
router_state
= Ernie5_5MTPRouterState(config.n_routed_experts)
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPEnvelope.forward(
hidden_streams: torch.Tensor,
next_embeddings: torch.Tensor,
) -> torch.Tensor

Fuse the next token with each stream and call the shared body normally.

Parameters:

hidden_streams
torch.Tensor

Final decoder tensor [batch, sequence, streams, hidden].

next_embeddings
torch.Tensor

Shifted embeddings [batch, sequence, hidden]; no wrap.

shared_body
Ernie5_5Block

Registered last decoder block, passed afresh after wrapping.

inputs
Ernie5_5AttentionInputs

Same position/mask layout as the decoder’s original query slots.

Returns: torch.Tensor

MTP head input [batch, sequence, hidden] in the projection dtype. The

nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPEnvelope.init_weights(
std: float
) -> None

Initialize all owned state, including after meta materialization.

class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM(
mtp_loss_scaling_factor: float = 0.1,
logits_dtype: torch.dtype | None = None
)

Bases: Ernie5_5_MoeForCausalLM

Train one shared-last-layer MTP depth with full parameters at CP1 and PP1.

Each batch contains one unpadded document. Main logits use the decoder’s existing computation and MultiMax. The SFT recipe divides both main and auxiliary CE sums by the same global main-token count, preserving the configured auxiliary coefficient per token. Packed inputs, CP, PP, LoRA and KV caching are unsupported. Training is rejected if an MTP-owned parameter is frozen (including the default LoRA configuration) or the model carries LoRA adapter tensors.

_keep_in_fp32_modules_strict
mtp
= Ernie5_5MTPEnvelope(self.config)
mtp_config
parallelizer
= Ernie5_5MTPModelParallelizer()
state_dict_adapter
= Ernie5_5MTPCheckpointAdapter(self.config)
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM._forward_mtp(
streams: torch.Tensor,
input_ids: torch.Tensor
) -> torch.Tensor

Run MTP inside the active inner FSDP forward, without a cached body.

Parameters:

streams
torch.Tensor

Final decoder streams [batch, sequence, streams, hidden].

inputs
Ernie5_5AttentionInputs

Decoder query-slot positions and causal masks.

input_ids
torch.Tensor

Original integer tokens [batch, sequence].

Returns: torch.Tensor

Post-MultiMax logits [batch, sequence, vocab]. The rolled token-0

nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM._require_trainable_mtp() -> None

Reject training while an MTP-owned parameter is frozen or the model carries LoRA adapters.

Training is rejected if an MTP-owned parameter is frozen (including the default LoRA configuration, which freezes every base parameter), and if any parameter name of the model contains lora_, the test the framework’s trainability policy uses for adapter tensors. The second condition also covers LoRA combined with a freeze configuration that unfreezes the MTP tensors. Frozen decoder parameters without adapters are not detected. Only requires_grad and parameter names are read; no tensor value is touched.

Raises:

  • ValueError: If a parameter registered under mtp does not require gradients, or a parameter name of the model contains lora_.
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM.forward(
input_ids: torch.Tensor,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
use_cache: bool = False

Run the decoder and optionally the one-depth shared-body MTP path.

Parameters:

input_ids
torch.Tensor

Integer tensor [1, sequence] containing one unpadded document.

attention_mask
torch.Tensor | NoneDefaults to None

Optional all-true tensor [1, sequence].

padding_mask
torch.Tensor | NoneDefaults to None

Optional all-false tensor [1, sequence].

position_ids
torch.Tensor | NoneDefaults to None

Optional integer tensor [1, sequence] equal to 0..sequence-1.

use_cache
boolDefaults to False

Must be False; there is no KV cache.

Returns: Ernie5_5MTPOutput

Main and optional MTP logits [1, sequence, vocab]; neither includes loss.

Raises:

  • ValueError: If KV caching, CP, padded or multi-document inputs are requested, or the model is in training mode and a DSA indexer parameter requires gradients, an MTP-owned parameter does not, or the model carries LoRA adapter tensors.
  • TypeError: If packing metadata such as seq_lens or packed_seq_ids is supplied.
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype | None = None
) -> None

Initialize the production decoder and every MTP-owned tensor.

class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPModelParallelizer()

Bases: Ernie5_5ModelParallelizer

Require the MTP envelope to belong to the outer FSDP root.

The inherited flow gives the FP32 hyper-head its own unit. The remaining five owned tensors live outside the decoder root and require outer wrapping.

nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPModelParallelizer._apply_moe(
model: torch.nn.Module,
) -> torch.nn.Module

Check that every trainable MTP parameter was claimed by an FSDP unit.

Raises:

  • RuntimeError: If the outer model is not an FSDP unit or a trainable MTP parameter remains unsharded.
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPModelParallelizer._prepare_model(
model: torch.nn.Module,
) -> None

Reject CP and uncovered MTP ownership before the executor mutates state.

Raises:

  • ValueError: If CP is enabled, outer wrapping is disabled, or the base parallelizer rejects the topology.
class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPOutput(
mtp_per_depth_logits: list[torch.Tensor] | None = None,
mtp_loss_scaling_factor: float | None = None
)
Dataclass

Bases: CausalLMOutputWithPast

Standard main logits plus one depth’s post-MultiMax auxiliary logits.

logits

Main tensor of shape [batch, sequence, vocab].

mtp_loss_scaling_factor
float | None = None

Auxiliary-loss coefficient; no loss is computed here.

mtp_per_depth_logits
list[Tensor] | None = None

One tensor of shape [batch, sequence, vocab], or None for decoder-only evaluation. The standard SFT recipe applies the same global main-token denominator to main and auxiliary losses.