nemo_automodel.components.datasets.vlm
nemo_automodel.components.datasets.vlm
Submodules
nemo_automodel.components.datasets.vlm.collate_fnsnemo_automodel.components.datasets.vlm.datasetsnemo_automodel.components.datasets.vlm.dspark_collatenemo_automodel.components.datasets.vlm.fake_imagenemo_automodel.components.datasets.vlm.loadernemo_automodel.components.datasets.vlm.mocknemo_automodel.components.datasets.vlm.neat_packing_vlmnemo_automodel.components.datasets.vlm.pp_medianemo_automodel.components.datasets.vlm.samplersnemo_automodel.components.datasets.vlm.utils
Package Contents
Classes
Functions
API
Bases: Sampler
Sampler that groups samples by total token count for balanced distributed training.
With shard_data=True each rank owns a different subset of data.
This sampler sorts every rank’s indices by total tokens
(text_tokens + media_tokens, descending). All ranks share the
same seed + epoch so position N on every rank corresponds to a
sample of similar length, keeping cross-rank padding minimal.
Per-epoch randomness is achieved by rotating the sorted order by a deterministic random offset (same on every rank).
Parameters:
The dataset to sample from.
Base random seed (same value on every rank).
Optional HuggingFace processor (e.g. Qwen2VLProcessor).
Used to read image_processor / video_processor attributes
for accurate media token estimation via smart_resize.
Compute token lengths with direct list access for speed.
Estimate token count for one image from its [height, width] metadata.
Return (text_tokens, media_tokens) for one example.
Uses pre-computed _text_tokens / _media_tokens when available
(written by scripts/precompute_tokens.py). Otherwise falls back
to heuristic estimation.
Estimate token count for one video from its
[total_frames, height, width, fps, duration] metadata.
Unwrap dataset wrappers to get the underlying list for direct access.
Set the epoch for deterministic shuffling (standard PyTorch pattern).
Bases: Dataset
Dataset wrapper that tokenizes samples in __getitem__.
Instead of deferring apply_chat_template to the collate function, this
wrapper performs tokenization per-sample so that:
- The collate function only needs to pad and stack.
- Overlong samples are detected after precise tokenization (including media-token expansion) and replaced with a different random sample.
- Tokenization work is distributed across DataLoader workers.
Each __getitem__ call returns a dict with at least::
{ “input_ids”: (seq_len,), “attention_mask”: (seq_len,), “labels”: (seq_len,), }
Plus optional media tensors (pixel_values, image_grid_thw,
pixel_values_videos, video_grid_thw).
Wrap collate_fn so that on failure the entire batch is re-sampled.
Bases: Dataset
Wrapper that catches __getitem__ and collate errors, substituting random replacement samples.
This handles failures such as corrupted files, missing media, bad data, or processor errors (e.g. multimodal token mismatch from truncation) without crashing the entire training run.
Wrap a collate_fn so that on failure the entire batch is re-sampled and retried.
Build a mock VLM dataset in Automodel conversation format.
Each sample is a dict with a "conversation" key whose value is a list
of user/assistant message dicts. User messages contain one or more
{"type": "image", "image": <PIL.Image>} items followed by a text prompt.
Assistant messages contain a single text response.
This is the same format produced by make_rdr_dataset,
make_unimm_chat_dataset, and make_meta_dataset, so the returned
list can be fed directly to PreTokenizedDatasetWrapper.
When max_length is set and responses is None, each sample’s
assistant response is generated with max_length words — guaranteed
to exceed max_length tokens so that PreTokenizedDatasetWrapper
with truncate=True produces exactly max_length tokens per sample.
Parameters:
Number of conversation examples to generate.
Number of random images per user turn.
(width, height) of each generated image.
Text prompt appended after the image(s) in the user turn.
Optional list of assistant responses. Cycled over samples.
Target sequence length. When set (and responses is
None), generates a response of max_length words per sample
so the tokenized sequence always exceeds max_length tokens.
Random seed for reproducibility.
Returns: list
A list of dicts, each with a single "conversation" key.
Load and preprocess the CORD-V2 dataset for image-to-text fine-tuning.
Load datasets defined in a meta JSON file and convert to Automodel conversation format.
The meta JSON file maps dataset names to their configurations. Each configuration can have:
- file_name (str): Path to the data file (JSON/JSONL). Relative paths are resolved against the meta file’s directory.
- columns (dict): Column name mapping (messages, images, videos).
- tags (dict): Tag mapping (role_tag, content_tag, user_tag, assistant_tag, system_tag).
- media_dir (str): Directory prefix for media files.
- sample_ratio (float): Sampling ratio (0.0 to 1.0, default 1.0).
When shard_data=True, each rank loads only its 1/world_size slice of
every dataset file (interleaved: raw_data[rank::world_size]). This
reduces per-rank memory and I/O. The caller should use a local sampler
(e.g. RandomSampler) instead of DistributedSampler since data is
already partitioned.
Video frame sampling (fps, min_frames, max_frames) should be configured on the processor rather than here. For example in YAML::
processor: target: transformers.AutoProcessor.from_pretrained pretrained_model_name_or_path: … fps: 1 min_frames: 4 max_frames: 128
Example meta JSON::
{ “my_dataset”: { “file_name”: “data/train.jsonl”, “columns”: {“messages”: “conversations”}, “media_dir”: “/data/media” } }
Parameters:
Path to the meta JSON file.
Which datasets to load. None means all.
Unused, kept for API consistency.
If True, each rank loads only its 1/world_size slice.
Data-parallel rank. Inferred from torch.distributed if None.
Data-parallel world size. Inferred from torch.distributed if None.
Additional arguments (unused).
Returns:
list[dict]: Combined list of examples in Automodel conversation format.
Load and preprocess the RDR dataset for image-to-text fine-tuning.
Parameters:
Path or identifier for the RDR dataset.
Dataset split to load.
Additional arguments.
Returns:
The processed dataset.
Load and preprocess the UniMM-Chat dataset for image-to-text fine-tuning.