Skip to navigation

nemo_automodel.components.speculative.dfly2.target

View as MarkdownOpen in Claude

Model-owned contracts and the frozen-target wrapper for DFly2 training.

DFly2 reads the target’s own attention state, whose layout is architecture specific, so the target model produces it (DFly2TargetFeatureProvider) and the target config builds the matching draft (ModelOwnedDFly2Provider). This module only names those contracts and runs the frozen forward.

Module Contents

Classes

NameDescription
DFly2TargetBatchTarget features and the batch tensors the DFly2 trainer reads.
DFly2TargetFeatureProviderTarget model that exposes the attention state its paired DFly2 draft layers read.
DFly2TargetModelRun a frozen target that implements DFly2TargetFeatureProvider.
ModelOwnedDFly2ProviderTarget config that owns construction of its architecture-specific DFly2 draft.
_DFly2DraftOptionsRecipe options consumed by model-owned DFly2 draft constructors.

API

class nemo_automodel.components.speculative.dfly2.target.DFly2TargetBatch(
input_ids: torch.Tensor,
loss_mask: torch.Tensor
)
Dataclass

Target features and the batch tensors the DFly2 trainer reads.

features
DFly2TargetFeatures

Frozen target state, documented by DFly2TargetFeatures.

input_ids
Tensor

Integer tensor of shape [batch, sequence].

loss_mask
Tensor

Tensor of shape [batch, sequence] selecting supervised tokens.

class nemo_automodel.components.speculative.dfly2.target.DFly2TargetFeatureProvider()
Protocol

Target model that exposes the attention state its paired DFly2 draft layers read.

nemo_automodel.components.speculative.dfly2.target.DFly2TargetFeatureProvider.dfly2_target_features(
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
layer_ids: collections.abc.Sequence[int]

Run the frozen forward and return the paired layers’ keys and key lists.

Parameters:

input_ids
torch.Tensor

Integer tensor of shape [batch, sequence].

attention_mask
torch.Tensor

Binary right-padding tensor of shape [batch, sequence].

layer_ids
Sequence[int]

Target layers paired with the draft layers, in draft-layer order.

class nemo_automodel.components.speculative.dfly2.target.DFly2TargetModel(
model: torch.nn.Module,
target_layer_ids: collections.abc.Sequence[int]
)

Run a frozen target that implements DFly2TargetFeatureProvider.

model
= model.eval()
target_layer_ids
= [(int(layer_id)) for layer_id in target_layer_ids]
nemo_automodel.components.speculative.dfly2.target.DFly2TargetModel.generate_batch(
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
loss_mask: torch.Tensor

Run the target once and collect the DFly2 features.

Parameters:

input_ids
torch.Tensor

Integer tensor of shape [batch, sequence].

attention_mask
torch.Tensor

Binary right-padding tensor of shape [batch, sequence].

loss_mask
torch.Tensor

Tensor of shape [batch, sequence] selecting supervised tokens.

Returns: DFly2TargetBatch

The batch with the features of DFly2TargetFeatureProvider.dfly2_target_features.

nemo_automodel.components.speculative.dfly2.target.DFly2TargetModel.get_input_embeddings() -> torch.nn.Module

Return the target’s input embedding.

nemo_automodel.components.speculative.dfly2.target.DFly2TargetModel.get_output_embeddings() -> torch.nn.Module

Return the target’s LM head.

class nemo_automodel.components.speculative.dfly2.target.ModelOwnedDFly2Provider()
Protocol

Target config that owns construction of its architecture-specific DFly2 draft.

nemo_automodel.components.speculative.dfly2.target.ModelOwnedDFly2Provider.build_dfly2_draft(
) -> torch.nn.Module

Build a draft from the recipe’s declarative options.

class nemo_automodel.components.speculative.dfly2.target._DFly2DraftOptions()
Protocol

Recipe options consumed by model-owned DFly2 draft constructors.

attn_backend
str
block_size
int
confidence_head_stop_gradient
bool
enable_confidence_head
bool
mask_token_id
int
num_anchors
int
target_layer_ids
list[int]