Skip to navigation

nemo_automodel.components.speculative.dflash.cached_data

View as MarkdownOpen in Claude

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

Module Contents

Classes

NameDescription
CachedDFlashBatchA right-padded batch of cache rows, with each row treated as one document.
CachedDFlashDataConfigSettings for a cache-backed DFlash dataloader.
CachedDFlashDatasetRead one SpecForge .ckpt per item, in lexicographic filename order.
CachedDFlashRowOne unpadded record with token ids, assistant mask and optional scores of shape

Functions

NameDescription
collate_cached_dflashBatch right-padded rows, keeping their original sequence boundaries.

Data

VISUAL_SCORE_SCALE

_FORMAT_VERSION

_HIDDEN_DTYPES

_TEXT_ONLY_SCORE

API

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

Bases: 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
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
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

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
boolDefaults to False

Shard samples across the default process group.

Returns: DataLoader[CachedDFlashRow]

Dataloader yielding right-padded CPU batches as defined by

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
nemo_automodel.components.speculative.dflash.cached_data.CachedDFlashDataset.__getitem__(
index: int

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

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

Return the number of cached rows.

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]
nemo_automodel.components.speculative.dflash.cached_data.collate_cached_dflash(
pad_token_id: int

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,

nemo_automodel.components.speculative.dflash.cached_data.VISUAL_SCORE_SCALE = 10000
nemo_automodel.components.speculative.dflash.cached_data._FORMAT_VERSION = 1
nemo_automodel.components.speculative.dflash.cached_data._HIDDEN_DTYPES = {'bfloat16': torch.bfloat16, 'float16': torch.float16, 'float32': torch.float32}
nemo_automodel.components.speculative.dflash.cached_data._TEXT_ONLY_SCORE = -1