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

# Train a DFly2 Drafter for Speculative Decoding

> Train a DFly2 block-parallel speculative-decoding drafter for DeepSeek-V4.1-Flash with NeMo AutoModel, reading the target's own sparse-attention keys.

This guide shows you how to train a DFly2 speculative-decoding drafter for
DeepSeek-V4.1-Flash with [NeMo AutoModel](https://github.com/NVIDIA-NeMo/Automodel).

## What Is DFly2?

[DFly2](https://arxiv.org/abs/2611.34776) is a block-parallel drafter for
sparse-attention targets. Each draft layer is paired with one of the target's
sparse-attention layers and owns no context projection of its own:

* **KV sharing**: The draft layer reads the keys its paired target layer
  attends over, so the served drafter keeps no KV cache of its own.
* **Index sharing**: It reads them at the positions that layer attends from the
  row before the block's anchor, so the drafter runs no indexer and its context
  read has a fixed size at any context length.

On DeepSeek-V4.1-Flash, the paired layers are the ratio-1 layers 28, 32, and 36.
Each one attends over its own 128-token sliding window plus the shared global
latents at its indexer's top-512 picks. Around the backbone, the target's final
hidden state is fused into the anchor slot, and a hidden correction (a SwiGLU
plus an attentive correction over the last layer's context and the block's
prefix) adjusts every slot before the frozen LM head. A block of `block_size`
slots holds the anchor and drafts `block_size - 1` tokens.

The draft shares and freezes the target's embedding and LM head. The recipe,
`TrainDFly2Recipe`, reuses the DSpark recipe's frozen EP and FSDP2 target, anchor
sampling, gradient accumulation, metrics, and checkpointing.

## Objective

Each drafted slot is trained toward the target's distribution at the token
before its label:

| Term                                            | Meaning                                                                                                                                                                        |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| LK distillation                                 | `lambda * KL + (1 - lambda) * TV` with `lambda = exp(-lk_eta * (1 - TV))`: distills while the draft is far from the target, then optimizes the acceptance probability `1 - TV` |
| D-PACE weights (`dpace_alpha`, default 0.5)     | detached per-slot weights from the draft's confidence on the corpus token, emphasizing the slots that decide the accepted length                                               |
| `L_conf` (`confidence_head_alpha`, default 0.0) | optional binary cross-entropy (BCE) training a confidence head, on the detached final-normalized states, against the slot's acceptance probability                             |

The logs report the corpus cross-entropy (`ce`), the TV term (`tv`), the
per-position acceptance `accept_rate@k`, and the expected accepted block length
`tau`.

As in the DSpark recipe, a block is supervised up to its first unsupervised label,
and the D-PACE weights ignore slots past the end of the sequence.

## Data

Use a chat dataset of OpenAI-format `messages` rows with the responses
regenerated by the target model, as for DSpark: training is teacher-forced, and
regenerating avoids a training and inference distribution mismatch. Point
`recipe_args.train_data_path` at the dataset and set `chat_template` to the
target's training template.

## Train

The example configuration follows the released
[AngelSlim/DS-V4.1-Flash-DFly2-B6](https://huggingface.co/AngelSlim/DS-V4.1-Flash-DFly2-B6)
layout. Launch the same command on every node, varying `--node-rank`:

```bash
torchrun --nnodes=16 --node-rank=0 --nproc_per_node=4 \
  --master-addr=<NODE0_IP> --master-port=29500 \
  -m nemo_automodel.recipes.speculative_decoding.train_dfly2 \
  -c examples/speculative/dfly2/deepseek_v41_flash_dfly2.yaml
```

The key settings in `recipe_args` are:

| Key                  | Value          | Meaning                                                                                                    |
| -------------------- | -------------- | ---------------------------------------------------------------------------------------------------------- |
| `target_layer_ids`   | `[28, 32, 36]` | paired ratio-1 target layers, one per draft layer                                                          |
| `block_size`         | `6`            | slots per block: the anchor plus five drafted tokens                                                       |
| `mask_token_id`      | `128799`       | token filling the non-anchor slots                                                                         |
| `draft_attn_backend` | `tilelang`     | the sparse kernel reads each key list directly, while `sdpa` builds a dense mask and suits only short rows |
| `draft_weights_path` | unset          | warm-start every draft weight from a released draft or an earlier run                                      |

Without `draft_weights_path`, each draft layer's attention, norms, mHC mixes, and
shared expert start from its paired target layer. The routed experts and the router
start fresh, where the paper draws 128 of the paired layer's experts with their router
rows (its "attention only" ablation reaches 3.51 instead of 3.57 accepted tokens). A
warm start checks the paired layers, block size, and mask token recorded in the
checkpoint's `config.json` against the run's.

The example trains with AdamW and FP32 master weights at a global batch of 256
sequences. The paper uses Muon. Its learning rate is for a fresh draft: when
warm-starting from the released draft, lower it (for example, to `5.0e-6`), or the
first steps move the converged draft away from its optimum.

> **Note**
>
> The DFly2 path trains against a colocated text target on unpacked rows. Sequence
> packing, image inputs, the precomputed and remote target backends, and TP, PP,
> and CP are not supported.

## Checkpoints

Consolidated checkpoints use the released layout (`model.layers.*`,
`model.mtp_fusion.*`, `model.hidden_correction.*`), so a trained draft and the
released one load the same way.