> This page is for version 0.4.0 · 26.04.
> 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.speculative.dfly2.core

DFly2 online training wrapper.

Like DSpark's, the draft is self-contained (anchors, key lists, backbone, corrections), so
this wrapper only calls it with the frozen target features and computes the DFly2
objective. It reports `DSparkStepMetrics` so the DSpark recipe's metric window,
evaluation and logging apply unchanged.

## Module Contents

### Classes

| Name                                                                                         | Description                                                               |
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| [`DFly2TrainerModule`](#nemo_automodel-components-speculative-dfly2-core-DFly2TrainerModule) | DFly2 online training wrapper computing the D-PACE-weighted LK objective. |

### API

```python
class nemo_automodel.components.speculative.dfly2.core.DFly2TrainerModule(
    draft_model: torch.nn.Module,
    dpace_alpha: float = 0.5,
    lk_eta: float = 3.0,
    confidence_head_alpha: float = 0.0
)
```

**Bases:** `Module`

DFly2 online training wrapper computing the D-PACE-weighted LK objective.

**`confidence_head_alpha`** `= float(confidence_head_alpha)`

---

**`dpace_alpha`** `= float(dpace_alpha)`

---

**`lk_eta`** `= float(lk_eta)`

---

```python
nemo_automodel.components.speculative.dfly2.core.DFly2TrainerModule.forward(
    input_ids: torch.Tensor,
    loss_mask: torch.Tensor,
    context_keys: collections.abc.Sequence[torch.Tensor],
    context_indices: collections.abc.Sequence[torch.Tensor],
    target_last_hidden_states: torch.Tensor
) -> nemo_automodel.components.speculative.dspark.core.DSparkStepMetrics
```

Run the draft on the target features and compute the DFly2 loss.

**Parameters:**

**`input_ids`** `torch.Tensor`

Target-token tensor of shape \[batch, sequence].

---

**`loss_mask`** `torch.Tensor`

Supervision tensor of shape \[batch, sequence].

---

**`context_keys`** `Sequence[torch.Tensor]`

One tensor per draft layer of shape \[batch, context_keys,
key_dim], as in `DFly2TargetFeatures`.

---

**`context_indices`** `Sequence[torch.Tensor]`

One integer tensor per draft layer of shape \[batch,
sequence, slots], as in `DFly2TargetFeatures`.

---

**`target_last_hidden_states`** `torch.Tensor`

Tensor of shape \[batch, sequence, hidden].

---

**Returns:** `DSparkStepMetrics`

Step metrics. The per-position acceptance sums have `block_size` entries,