nemo_automodel.components.datasets.vlm.datasets
nemo_automodel.components.datasets.vlm.datasets
Module Contents
Classes
Functions
Data
API
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.
Bases: list
list subclass that carries pre-computed dataset statistics.
Attached by :func:make_meta_dataset so downstream code (e.g.
_log_global_dataset_stats) can read aggregated stats without
re-scanning all examples.
Count images, videos, text-only samples and estimate token counts.
Token estimation mirrors the logic in LengthGroupedSampler._estimate_tokens:
- Text tokens: uses pre-computed
_text_tokenswhen present (written byscripts/precompute_tokens.py), otherwise falls back tochars // 3. - Media tokens: uses
mm_inputs_metaimage/video dimensions when present (populated by the precompute script), otherwise500per media item.
Returns:
dict with keys n_images, n_videos, n_text_only, n_text_tokens,
Convert a single sharegpt-format example to Automodel conversation format.
Parameters:
A single data example in sharegpt format.
Column name mapping with keys ‘messages’, ‘images’, ‘videos’.
Tag mapping with keys ‘role_tag’, ‘content_tag’, ‘user_tag’, ‘assistant_tag’.
Directory prefix for resolving relative media paths.
Returns:
Example in Automodel conversation format.
Load data from a JSON or JSONL file.
Parameters:
Path to the JSON or JSONL file.
Returns:
list[dict]: List of data examples.
Load only the JSONL lines needed for this rank, avoiding full json.loads on skipped lines.
Handles sample_ratio and sharding so that each rank only parses and stores its own subset. The semantics match the original load-all-then-slice approach:
- Apply
sample_ratio(deterministicRandom(42).sample) on the full index range. - Shard the resulting list with
[rank::world_size].
Returns:
tuple[list[dict], int]: (parsed examples for this rank, total line count).
Print a visual summary of per-dataset loading times and data statistics.
Return a mismatch description if media grids survived without tokens.
Resolve a model-specific media token id from processor/config/tokenizer.
Load and preprocess the CORD-V2 dataset for image-to-text fine-tuning.
Load and preprocess the LLaVA-Instruct-150K dataset for LLaVA-OneVision-1.5.
This function loads conversation-format data with images and returns it in the standard NeMo VLM format expected by the collate function.
Parameters:
Path to the dataset on HuggingFace Hub or local path.
Dataset split to load (e.g., “train”, “train[:1000]”).
Additional arguments passed to load_dataset.
Returns:
List of dicts with “conversation” and “image” keys.
Load and preprocess the MedPix dataset for image-to-text fine-tuning.
Formatting is deferred to __getitem__ via with_transform so the
dataset stays Arrow-backed (no Python-side copy of every image). Images are
loaded undecoded (Image(decode=False)) and wrapped as lazy PIL
handles (Image.open reads only the header); the actual pixel decode then
happens on demand in the DataLoader workers, rather than eagerly decoding the
whole split up front.
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).
- 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 allenai/tulu-3-sft-mixture directly from the HF Hub as text-only conversations.
This avoids the meta-JSON + JSONL data-prep step required by
:func:make_meta_dataset: point the recipe’s dataset._target_ at this
function and the Tulu-3 split is pulled straight from the Hub. Each row’s
messages field is converted with the same helper the meta-JSON path uses
(:func:_convert_sharegpt_to_conversation), so the resulting data composition is
identical to dumping the split to JSONL and loading it via
:func:make_meta_dataset: no turn cap, system turns dropped, every row kept
in the original split order. Conversations are text-only (no image entries),
so batches carry no pixel_values / vision tensors.
The returned dataset stays Arrow-backed (map) so the full ~939k-row split is
not copied into a Python list.
Parameters:
HF Hub id (or local path) of the Tulu-3 SFT mixture.
HF split expression (e.g. "train" or "train[:50000]").
Ignored. Accepted so recipe-level dataset keys (e.g. truncate)
that are forwarded to the dataset target do not raise.
Returns:
datasets.Dataset: Rows with a single conversation column, each a list of
Build a text-only 80/20 mix of Tulu-3-SFT-mixture and Magicoder-OSS-Instruct-75K.
Both datasets are converted into the NeMo VLM {"conversation": [...]} shape
consumed by :func:nemo_automodel.components.datasets.vlm.collate_fns.default_collate_fn.
Because default_collate_fn is image-aware only when a conversation turn
contains an {"type": "image", ...} entry, returning text-only conversations
here yields batches with no pixel_values / vision tensors — which is what
the Gemma 4 base+drafter composite expects for text-only training.
Mixing uses datasets.interleave_datasets with probabilities [0.8, 0.2]
and stopping_strategy="all_exhausted" so both datasets are sampled until
every example has been drawn at least once.
Parameters:
HF split expression for the Tulu-3 source (e.g. "train"
or "train[:50000]").
HF split expression for the Magicoder source.
Seed forwarded to interleave_datasets for reproducibility.
Drop Tulu-3 conversations with more than this many turns to keep memory bounded. Magicoder samples are always 2 turns.
If set, cap the merged dataset to this many rows.
Additional arguments forwarded to load_dataset for both
sources.
Returns: list
List of {"conversation": [...]} dicts. Each conversation is a list of
Load and preprocess the UniMM-Chat dataset for image-to-text fine-tuning.