vlm_dataset_utils#

Utility functions for getting samples and dataloader for different VLM calibration datasets.

This module supports both: - Small non-streaming VLM datasets (e.g., ScienceQA) - Large streaming VLM datasets (e.g., Nemotron-VLM-Dataset-v2) where we want to avoid downloading everything.

Functions

create_vlm_calibration_forward

Return forward(batch), which runs one multimodal calibration batch through the VLM.

create_vlm_calibration_loop

Create a calibration loop for VLM models that handles multimodal inputs.

get_supported_vlm_datasets

Retrieves a list of vlm datasets supported.

get_vlm_dataset_dataloader

Get a dataloader with the dataset name and processor of the target model.

create_vlm_calibration_forward(full_model)#

Return forward(batch), which runs one multimodal calibration batch through the VLM.

It filters the batch kwargs to the parameters the model’s forward accepts, then calls the appropriate forward method. create_vlm_calibration_loop() runs it over a dataloader; an expert-parallel run hands it to distributed_forward_loop instead.

Parameters:

full_model – The full VLM model

Returns:

A function that runs one batch through full_model

create_vlm_calibration_loop(full_model, calib_dataloader)#

Create a calibration loop for VLM models that handles multimodal inputs.

It runs every batch through create_vlm_calibration_forward(). An expert-parallel model calibrates through distributed_forward_loop instead, handed that forward and the experts’ ep_mesh, as hf_ptq.py does: on transformers 5.16 and later, quantizer insertion drops the experts’ all-to-all forwards, which that loop re-installs before the first batch and this one does not.

Parameters:
  • full_model – The full VLM model

  • calib_dataloader – DataLoader yielding multimodal batches

Returns:

A calibration function that can be passed to mtq.quantize()

get_supported_vlm_datasets()#

Retrieves a list of vlm datasets supported.

Returns:

A list of strings, where each string is the name of a supported dataset.

Return type:

list[str]

Example usage:

from modelopt.torch.utils import get_supported_vlm_datasets

print("Supported datasets:", get_supported_vlm_datasets())
get_vlm_dataset_dataloader(dataset_name='scienceqa', processor=None, batch_size=1, num_samples=512, device=None, require_image=True, subsets=None, shuffle_buffer_size=10000, seed=42, image_root=None, use_media_shards=True, max_shards=None, dp_size=1, dp_rank=0)#

Get a dataloader with the dataset name and processor of the target model.

Parameters:
  • dataset_name (str) – Name of the dataset to load.

  • processor (Any) – Processor used for encoding images and text data.

  • batch_size (int) – Batch size of the returned dataloader.

  • num_samples (int) – Number of samples from the dataset.

  • device (str | device | None) – Device to move returned tensors to. If None, keep on CPU.

  • require_image (bool) – If True, keep only samples that have an image field.

  • dp_size (int) – Data-parallel world size; if > 1, the dataset is sharded across DP ranks.

  • dp_rank (int) – This process’s rank within the data-parallel group.

  • subsets (list[str] | None)

  • shuffle_buffer_size (int)

  • seed (int)

  • image_root (str | Path | None)

  • use_media_shards (bool)

  • max_shards (int | None)

Returns:

An instance of dataloader.

Return type:

DataLoader