> This page is for version Nightly (default).
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.

# nemo_automodel.components.models.ernie5_5.mtp_training

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

| Name                                                                                                                   | Description                                                                |
| ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| [`Ernie5_5MTPEnvelope`](#nemo_automodel-components-models-ernie5_5-mtp_training-Ernie5_5MTPEnvelope)                   | Own exactly eight trainable MTP tensors and one persistent router buffer.  |
| [`Ernie5_5MTPForCausalLM`](#nemo_automodel-components-models-ernie5_5-mtp_training-Ernie5_5MTPForCausalLM)             | Train one shared-last-layer MTP depth with full parameters at CP1 and PP1. |
| [`Ernie5_5MTPModelParallelizer`](#nemo_automodel-components-models-ernie5_5-mtp_training-Ernie5_5MTPModelParallelizer) | Require the MTP envelope to belong to the outer FSDP root.                 |
| [`Ernie5_5MTPOutput`](#nemo_automodel-components-models-ernie5_5-mtp_training-Ernie5_5MTPOutput)                       | Standard main logits plus one depth's post-MultiMax auxiliary logits.      |

### API

```python
class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPEnvelope(
    config: nemo_automodel.components.models.ernie5_5.config.Ernie5_5Config
)
```

**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)`

---

```python
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPEnvelope.forward(
    hidden_streams: torch.Tensor,
    next_embeddings: torch.Tensor,
    shared_body: nemo_automodel.components.models.ernie5_5.model.Ernie5_5Block,
    inputs: nemo_automodel.components.models.ernie5_5.model.Ernie5_5AttentionInputs
) -> 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

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

Initialize all owned state, including after meta materialization.

```python
class nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM(
    config: nemo_automodel.components.models.ernie5_5.config.Ernie5_5Config,
    backend: nemo_automodel.components.models.common.BackendConfig | None = None,
    mtp_loss_scaling_factor: float = 0.1,
    logits_dtype: torch.dtype | None = None
)
```

**Bases:** [Ernie5_5_MoeForCausalLM](/nemo/automodel/nightly/nemo-automodel/nemo_automodel/components/models/ernie5_5/model#nemo_automodel-components-models-ernie5_5-model-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)`

---

```python
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPForCausalLM._forward_mtp(
    streams: torch.Tensor,
    inputs: nemo_automodel.components.models.ernie5_5.model.Ernie5_5AttentionInputs,
    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

```python
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_`.

```python
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
) -> nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPOutput
```

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 | None` — default: None

Optional all-true tensor \[1, sequence].

---

**`padding_mask`** `torch.Tensor | None` — default: None

Optional all-false tensor \[1, sequence].

---

**`position_ids`** `torch.Tensor | None` — default: None

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

---

**`use_cache`** `bool` — default: 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.

```python
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.

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

**Bases:** [Ernie5_5ModelParallelizer](/nemo/automodel/nightly/nemo-automodel/nemo_automodel/components/models/ernie5_5/parallelization#nemo_automodel-components-models-ernie5_5-parallelization-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.

```python
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPModelParallelizer._apply_moe(
    model: torch.nn.Module,
    mesh_context: nemo_automodel.components.distributed.MeshContext
) -> 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.

```python
nemo_automodel.components.models.ernie5_5.mtp_training.Ernie5_5MTPModelParallelizer._prepare_model(
    model: torch.nn.Module,
    mesh_context: nemo_automodel.components.distributed.MeshContext
) -> 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.

```python
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.

---