Skip to navigation

nemo_automodel.recipes.speculative_decoding.train_dfly2

View as MarkdownOpen in Claude

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

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

Functions

NameDescription
check_dfly2_checkpoint_settingsRefuse warm-start weights whose recorded DFly2 settings differ from this run’s.
mainEntrypoint for TrainDFly2Recipe.

Data

logger

API

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

Bases: TrainDSparkRecipe

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

default_confidence_head_alpha
= 0.0
nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_model_owned_draft(
) -> torch.nn.Module

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

nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_target_wrapper(
target_layer_ids: list[int],

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

nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._build_trainer_module(
confidence_head_alpha: float
) -> torch.nn.Module

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

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.

nemo_automodel.recipes.speculative_decoding.train_dfly2.TrainDFly2Recipe._forward_batch(
batch: dict[str, torch.Tensor]

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

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.

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.
nemo_automodel.recipes.speculative_decoding.train_dfly2.main(
config_path: str | None = None
) -> None

Entrypoint for TrainDFly2Recipe.

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