> This page is for version 0.5.0 · 26.06.
> 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.common

Target features and key lists shared by every DFly2 draft.

A DFly2 draft layer owns no context projection. It is paired with one sparse-attention
layer of the target and reads that layer's own keys (KV sharing) at the positions that
layer attends from the row before the anchor (index sharing). The target therefore hands
the draft, per paired layer, the key tensor it attended over and the key list of every
row; the draft keeps the row before each anchor and appends its own block.

## Module Contents

### Classes

| Name                                                                                             | Description                                        |
| ------------------------------------------------------------------------------------------------ | -------------------------------------------------- |
| [`DFly2TargetFeatures`](#nemo_automodel-components-speculative-dfly2-common-DFly2TargetFeatures) | Frozen target state one DFly2 training step reads. |

### Functions

| Name                                                                                                     | Description                                                                           |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| [`build_block_key_indices`](#nemo_automodel-components-speculative-dfly2-common-build_block_key_indices) | Build every draft query's key list: the target's picks for its block, then its block. |
| [`context_key_indices`](#nemo_automodel-components-speculative-dfly2-common-context_key_indices)         | Keep only the context slots of draft key lists, masking the block's own keys.         |

### Data

[`KEY_LIST_ALIGNMENT`](#nemo_automodel-components-speculative-dfly2-common-KEY_LIST_ALIGNMENT)

### API

```python
class nemo_automodel.components.speculative.dfly2.common.DFly2TargetFeatures(
    context_keys: tuple[torch.Tensor, ...],
    context_indices: tuple[torch.Tensor, ...],
    last_hidden_states: torch.Tensor
)
```

Dataclass

Frozen target state one DFly2 training step reads.

**`context_indices`** `tuple[Tensor, ...]`

One integer tensor per draft layer, of shape \[batch, sequence,
slots]. Row `r` lists the `context_keys` rows the paired target layer
attended from token `r`, with `-1` marking unused slots. Every listed key
lies at or before `r` in `r`'s document.

---

**`context_keys`** `tuple[Tensor, ...]`

One tensor per draft layer, of shape \[batch, context_keys, key_dim]:
the keys and values the paired target layer attended over, in that layer's
own row order (for example a sliding-window segment followed by a shared
global segment).

---

**`last_hidden_states`** `Tensor`

Tensor of shape \[batch, sequence, hidden], the target's final
normalized hidden state (the input of its LM head).

---

```python
nemo_automodel.components.speculative.dfly2.common.DFly2TargetFeatures.__post_init__() -> None
```

```python
nemo_automodel.components.speculative.dfly2.common.build_block_key_indices(
    context_indices: torch.Tensor,
    anchor_positions: torch.Tensor,
    block_keep_mask: torch.Tensor,
    num_context_keys: int,
    block_size: int,
    causal: bool
) -> torch.Tensor
```

Build every draft query's key list: the target's picks for its block, then its block.

Each block reads the key list of the target row just before its anchor, the last token
the target has verified when the block is drafted. Every query of a block shares that
list and then sees its own block's draft keys, which follow the `num_context_keys`
context rows in the draft attention's key tensor. A block never sees another block. An
anchor at the sequence start has no verified row before it and sees only its own block;
a dropped block (`block_keep_mask` false) sees no key at all, so its output is zero.

**Parameters:**

**`context_indices`** `torch.Tensor`

Integer tensor of shape \[batch, sequence, slots] with `-1`
unused, as in `DFly2TargetFeatures`.

---

**`anchor_positions`** `torch.Tensor`

Integer tensor of shape \[batch, num_anchors].

---

**`block_keep_mask`** `torch.Tensor`

Boolean tensor of shape \[batch, num_anchors].

---

**`num_context_keys`** `int`

Number of context rows preceding the draft keys.

---

**`block_size`** `int`

Draft slots per block, the anchor slot included.

---

**`causal`** `bool`

Whether a draft query sees only the earlier slots of its block.

---

**Returns:** `torch.Tensor`

Int32 tensor of shape \[batch, num_anchors \* block_size, padded_slots], the context

```python
nemo_automodel.components.speculative.dfly2.common.context_key_indices(
    key_indices: torch.Tensor,
    num_context_keys: int
) -> torch.Tensor
```

Keep only the context slots of draft key lists, masking the block's own keys.

**Parameters:**

**`key_indices`** `torch.Tensor`

Integer tensor of shape \[batch, queries, slots] from
`build_block_key_indices`.

---

**`num_context_keys`** `int`

Number of context rows preceding the draft keys.

---

**Returns:** `torch.Tensor`

Tensor with the shape and dtype of `key_indices` where every draft-key slot is `-1`.

```python
nemo_automodel.components.speculative.dfly2.common.KEY_LIST_ALIGNMENT = 64
```