nemo_automodel.components.datasets.llm.neat_packing
nemo_automodel.components.datasets.llm.neat_packing
Neat packing (greedy knapsack) for LLM datasets.
This module provides an alternative packing strategy that uses a greedy
knapsack algorithm (min-heap) for better bin-packing utilization compared
to the sequential first-fit approach in packed_sequence.py.
The packed output uses an indexed attention mask (1, 2, 3, … per sub-sequence, 0 for padding) and reset position IDs so that it works with eager / SDPA attention backends — no Transformer Engine dependency.
Module Contents
Functions
Data
API
Lay out consecutive packs: concatenate each pack’s samples and pad it to pack_size.
Parameters:
Token ids of every sample, one list per sample.
Labels of every sample, one list per sample, aligned with input_ids.
Optional per-token mask of every sample; labels where it is 0 become -100.
int64 array of shape [samples], the token count of every sample.
int64 array of shape [chunk_samples], the samples of these packs in pack order,
as indices into input_ids.
int64 array of shape [chunk_packs], how many consecutive entries of order
each pack holds.
Length of every packed row.
input_ids value of the padding after a pack’s last document.
Column types of the packed rows.
Returns: pa.RecordBatch
One row per pack. input_ids, labels, attention_mask and position_ids
Return the column types of a NEAT-packed dataset.
Dataset.from_dict narrows input_ids to int32 and attention_mask to int8 when
every value fits (datasets.arrow_writer.OptimizedTypedSequence), and stores the other
columns as int64. Packed datasets keep exactly those types.
Parameters:
Smallest value stored in input_ids, padding included.
Largest value stored in input_ids, padding included.
Largest number of documents in one pack.
Returns: pa.Schema
A schema with list columns input_ids, labels, attention_mask and position_ids.
Bin-pack sample indices using a greedy knapsack (min-heap) algorithm.
Samples are sorted by length in descending order. Each sample is assigned to the bin with the smallest current total that can still accommodate it. If no bin fits, a new bin is created.
Parameters:
Length of each sample.
Maximum capacity of each bin.
Returns: list[list[int]]
A list of bins, where each bin is a list of sample indices.
Pack tokenized samples into fixed-length rows with a greedy knapsack.
The samples are first materialized into one Arrow table (tokenize_dataset_parallel,
which tokenizes a lazy dataset). Sample lengths then come from the list offsets, and the
packs are assembled with NumPy, _PACKS_PER_CHUNK packs at a time.
Parameters:
Samples with list fields input_ids and labels of equal length and an
optional loss_mask of the same length, which sets labels to -100 where it is 0.
A HuggingFace Dataset, a DatasetDict holding split, or a dataset that
tokenizes each sample on access (e.g. ChatDataset).
Dataset split key (e.g. "train").
Target packed sequence length.
Optional cap on number of packs to create.
Token ID for padding.
If True, silently drop samples longer than
pack_size; otherwise raise ValueError.
Worker processes that tokenize the samples; values above 1 need an
indexable dataset (see tokenize_dataset_parallel).
Returns: Dataset
A HuggingFace Dataset with one row per pack and list columns input_ids,
Raises:
ValueError: If a sample is longer thanpack_sizeanddrop_long_samplesisFalse, if a sample’slabelsorloss_masklength differs from itsinput_idslength, or if no sample remains.