> This page is for version 0.7.0 · 26.10.
> 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.dflash.cached_data

Read the unshifted tensor rows produced by SpecForge's DFlash visual scoring capture.

## Module Contents

### Classes

| Name                                                                                                         | Description                                                                     |
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| [`CachedDFlashBatch`](#nemo_automodel-components-speculative-dflash-cached_data-CachedDFlashBatch)           | A right-padded batch of cache rows, with each row treated as one document.      |
| [`CachedDFlashDataConfig`](#nemo_automodel-components-speculative-dflash-cached_data-CachedDFlashDataConfig) | Settings for a cache-backed DFlash dataloader.                                  |
| [`CachedDFlashDataset`](#nemo_automodel-components-speculative-dflash-cached_data-CachedDFlashDataset)       | Read one SpecForge `.ckpt` per item, in lexicographic filename order.           |
| [`CachedDFlashRow`](#nemo_automodel-components-speculative-dflash-cached_data-CachedDFlashRow)               | One unpadded record with token ids, assistant mask and optional scores of shape |

### Functions

| Name                                                                                                       | Description                                                          |
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| [`collate_cached_dflash`](#nemo_automodel-components-speculative-dflash-cached_data-collate_cached_dflash) | Batch right-padded rows, keeping their original sequence boundaries. |

### Data

[`VISUAL_SCORE_SCALE`](#nemo_automodel-components-speculative-dflash-cached_data-VISUAL_SCORE_SCALE)

[`_FORMAT_VERSION`](#nemo_automodel-components-speculative-dflash-cached_data-_FORMAT_VERSION)

[`_HIDDEN_DTYPES`](#nemo_automodel-components-speculative-dflash-cached_data-_HIDDEN_DTYPES)

[`_TEXT_ONLY_SCORE`](#nemo_automodel-components-speculative-dflash-cached_data-_TEXT_ONLY_SCORE)

### API

```python
class nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashBatch()
```

**Bases:** [CachedDFlashRow](#nemo_automodel-components-speculative-dflash-cached_data-CachedDFlashRow)

A right-padded batch of cache rows, with each row treated as one document.

**`attention_mask`** `Tensor`

---

**`doc_remaining`** `Tensor`

---

**`position_ids`** `Tensor`

---

**`seq_lens`** `Tensor`

---

```python
class nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataConfig(
    cache_dir: str | pathlib.Path,
    max_seq_len: int,
    batch_size: int,
    require_visual_score: bool = False,
    num_workers: int = 0,
    shuffle: bool = True,
    shuffle_seed: int = 42
)
```

Dataclass

Settings for a cache-backed DFlash dataloader.

**`batch_size`** `int`

---

**`cache_dir`** `str | Path`

---

**`max_seq_len`** `int`

---

**`num_workers`** `int = 0`

---

**`require_visual_score`** `bool = False`

---

**`shuffle`** `bool = True`

---

**`shuffle_seed`** `int = 42`

---

```python
nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataConfig.build(
    target_model_name_or_path: str,
    target_layer_ids: collections.abc.Sequence[int],
    target_hidden_size: int,
    target_vocab_size: int,
    block_size: int,
    mask_token_id: int,
    pad_token_id: int,
    distributed: bool = False
) -> torch.utils.data.DataLoader[nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashRow]
```

Build a cache reader and its CPU dataloader.

**Parameters:**

**`target_model_name_or_path`** `str`

Exact teacher model ID or path recorded at capture.

---

**`target_layer_ids`** `Sequence[int]`

Decoder layer indices in hidden-state concatenation order.

---

**`target_hidden_size`** `int`

Width of one teacher layer.

---

**`target_vocab_size`** `int`

Size of the teacher's output vocabulary.

---

**`block_size`** `int`

DFlash block size, including the unmasked anchor token.

---

**`mask_token_id`** `int`

Token used for the masked draft positions.

---

**`pad_token_id`** `int`

In-vocabulary token used to pad uneven batches.

---

**`distributed`** `bool` — default: False

Shard samples across the default process group.

---

**Returns:** `DataLoader[CachedDFlashRow]`

Dataloader yielding right-padded CPU batches as defined by

```python
class nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataset(
    cache_dir: str | pathlib.Path,
    target_model_name_or_path: str,
    target_layer_ids: collections.abc.Sequence[int],
    target_hidden_size: int,
    target_vocab_size: int,
    block_size: int,
    mask_token_id: int,
    max_seq_len: int,
    require_visual_score: bool = False
)
```

**Bases:** `Dataset[CachedDFlashRow]`

Read one SpecForge `.ckpt` per item, in lexicographic filename order.

A `manifest.json` identifies the capture and its settings. Setup reads the
manifest and enumerates the files. Tensors are read and validated on demand.
The trainer determines full-block anchor eligibility after batching.

**`_feature_width`** `= len(layer_ids) * target_hidden_size`

---

**`_fields`** `= {'input_ids', 'loss_mask', 'hidden_states'}`

---

**`_hidden_dtype`** `= _HIDDEN_DTYPES[hidden_dtype]`

---

**`_paths`**

---

```python
nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataset.__getitem__(
    index: int
) -> nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashRow
```

Load an unshifted record on CPU.

**Parameters:**

**`index`** `int`

Index into the sorted checkpoint files.

---

**Returns:** `CachedDFlashRow`

Int64 `input_ids` and binary `loss_mask` of shape \[sequence].

```python
nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataset.__len__() -> int
```

Return the number of cached rows.

```python
class nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashRow
```

**Bases:** `typing.TypedDict`

One unpadded record with token ids, assistant mask and optional scores of shape
\[sequence], and target hidden states of shape \[sequence, layers \* hidden].

**`hidden_states`** `Tensor`

---

**`input_ids`** `Tensor`

---

**`loss_mask`** `Tensor`

---

**`visual_score`** `NotRequired[Tensor]`

---

```python
nemo_automodel.components.speculative.dflash.cached_data.collate_cached_dflash(
    examples: list[nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashRow],
    pad_token_id: int
) -> nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashBatch
```

Batch right-padded rows, keeping their original sequence boundaries.

**Parameters:**

**`examples`** `list[CachedDFlashRow]`

Cache records with `input_ids`, `loss_mask`, and optional
`visual_score` of shape \[sequence] and `hidden_states` of shape
\[sequence, layers \* hidden]. Sequences can have different lengths.

---

**`pad_token_id`** `int`

Valid vocabulary ID used for padding input ids.

---

**Returns:** `CachedDFlashBatch`

`input_ids`, `loss_mask`, `attention_mask`, `position_ids`,

```python
nemo_automodel.components.speculative.dflash.cached_data.VISUAL_SCORE_SCALE = 10000
```

```python
nemo_automodel.components.speculative.dflash.cached_data._FORMAT_VERSION = 1
```

```python
nemo_automodel.components.speculative.dflash.cached_data._HIDDEN_DTYPES = {'bfloat16': torch.bfloat16, 'float16': torch.float16, 'float32': torch.float32}
```

```python
nemo_automodel.components.speculative.dflash.cached_data._TEXT_ONLY_SCORE = -1
```