> 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.recipes.speculative_decoding.train_dfly2

DFly2 draft-model training recipe (DeepSeek V4.1 targets).

DFly2 (arXiv:2611.34776) is a block-parallel drafter for sparse-attention targets. Each
draft layer is paired with one target sparse-attention layer and reads that layer's own
keys at the positions it attends from the row before the anchor, so the served drafter
shares the target's KV cache and indexer instead of keeping its own. See
`nemo_automodel.components.speculative.dfly2`.

This recipe reuses every piece of the DSpark recipe -- the frozen EP/FSDP target, anchor
sampling, gradient accumulation with a trailing-window flush, metrics, and checkpointing
\-- and swaps in the model-owned DFly2 draft, the target capture of the paired layers'
keys and key lists, and the D-PACE-weighted LK objective. `target_layer_ids` names the
paired target layers, one per draft layer.

## Module Contents

### Classes

| Name                                                                                            | Description                                                                   |
| ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| [`TrainDFly2Recipe`](#nemo_automodel-recipes-speculative_decoding-train_dfly2-TrainDFly2Recipe) | Recipe for DFly2 draft-model training on a colocated sparse-attention target. |

### Functions

| Name                                                                                                                          | Description                                                                     |
| ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| [`check_dfly2_checkpoint_settings`](#nemo_automodel-recipes-speculative_decoding-train_dfly2-check_dfly2_checkpoint_settings) | Refuse warm-start weights whose recorded DFly2 settings differ from this run's. |
| [`main`](#nemo_automodel-recipes-speculative_decoding-train_dfly2-main)                                                       | Entrypoint for `TrainDFly2Recipe`.                                              |

### Data

[`logger`](#nemo_automodel-recipes-speculative_decoding-train_dfly2-logger)

### API

```python
class nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe()
```

**Bases:** [TrainDSparkRecipe](/nemo/automodel/nightly/nemo-automodel/nemo_automodel/recipes/speculative_decoding/train_dspark#nemo_automodel-recipes-speculative_decoding-train_dspark-TrainDSparkRecipe)

Recipe for DFly2 draft-model training on a colocated sparse-attention target.

**`default_confidence_head_alpha`** `= 0.0`

---

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_model_owned_draft(
    target_config: nemo_automodel.components.speculative.dspark.registry.ModelOwnedDSparkProvider,
    margs: nemo_automodel.recipes.speculative_decoding.train_dspark._DraftArgs
) -> torch.nn.Module
```

Build the target package's DFly2 draft and start its layers from their paired target layers.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_target_wrapper(
    target_layer_ids: list[int],
    target_packed_forward: nemo_automodel.components.speculative.dspark.target._DSparkPackedForward | None
) -> nemo_automodel.components.speculative.dfly2.DFly2TargetModel
```

Capture the paired layers' keys and key lists instead of hidden states.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_trainer_module(
    recipe_cfg: nemo_automodel.components.config.loader.ConfigNode,
    confidence_head_alpha: float
) -> torch.nn.Module
```

Build the DFly2 trainer wrapper (D-PACE-weighted LK plus confidence).

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._draft_fsdp_units(
    draft_model: torch.nn.Module
) -> list[torch.nn.Module]
```

staticmethod

Shard the heads by dtype too: the corrections hold FP32 sinks and biases, the confidence head is FP32.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._forward_batch(
    batch: dict[str, torch.Tensor]
) -> nemo_automodel.components.speculative.dspark.core.DSparkStepMetrics
```

Run the frozen target once and train the draft on its paired layers' keys.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe.setup() -> None
```

Reject the DSpark options DFly2 does not support, then build everything via the DSpark recipe.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.check_dfly2_checkpoint_settings(
    path: str,
    target_layer_ids: list[int],
    block_size: int,
    mask_token_id: int
) -> None
```

Refuse warm-start weights whose recorded DFly2 settings differ from this run's.

The paired target layers, the block size and the mask token own no tensors, so a draft
trained with others loads cleanly and then reads the wrong keys. A released draft and an
export of this recipe record them in `config.json` under `dfly_config`.

**Parameters:**

**`path`** `str`

The configured `draft_weights_path`, a directory or a `.safetensors` file.

---

**`target_layer_ids`** `list[int]`

Paired target layers of this run, in draft-layer order.

---

**`block_size`** `int`

Draft slots per block of this run.

---

**`mask_token_id`** `int`

Mask token of this run.

---

**Raises:**

* `ValueError`: A recorded setting differs from this run's.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.main(
    config_path: str | None = None
) -> None
```

Entrypoint for `TrainDFly2Recipe`.

```python
nemo_automodel.recipes.speculative_decoding.train_dfly2.logger = logging.getLogger(__name__)
```