Skip to main content

Overview

The data processor module provides classes and functions for handling vision-language training data, including dataset loading, preprocessing, packing, and batching for Qwen-VL models.

Constants

int
default:"-100"
Label value used to ignore tokens during loss calculation.
int
default:"151655"
Token ID representing image placeholders in the vocabulary.
int
default:"151656"
Token ID representing video placeholders in the vocabulary.
str
default:"<image>"
Text placeholder for images in conversation data.
str
default:"<video>"
Text placeholder for videos in conversation data.

LazySupervisedDataset

Dataset class for lazy loading of vision-language training data.

Constructor

ProcessorMixin
required
Qwen-VL processor containing tokenizer, image processor, and video processor.
DataArguments
required
Data configuration including dataset names, pixel limits, and video parameters.

Properties

list[int]
Approximate token length for each sample (text tokens + 128 per image).
list[int]
Token lengths with modality information. Positive for multimodal samples, negative for text-only.
np.ndarray
Pre-calculated token lengths if available in dataset metadata, otherwise returns array of ones.

Methods

__getitem__(i)

Retrieves a single training sample with retry logic.
int
required
Sample index.
Returns: Dictionary containing:
  • input_ids - Tokenized input sequence
  • labels - Target labels with IGNORE_INDEX for non-answer tokens
  • position_ids - 3D position IDs for RoPE
  • attention_mask - Sequence length for Flash Attention
  • pixel_values - Image pixel tensors (if images present)
  • image_grid_thw - Image grid dimensions (time, height, width)
  • pixel_values_videos - Video pixel tensors (if videos present)
  • video_grid_thw - Video grid dimensions
Retry Logic:
  • 3 attempts on the current sample
  • 3 attempts on the next sample if current fails
  • Raises exception if all retries fail

Utility Functions

update_processor_pixels()

Updates processor image and video resolution parameters.
ProcessorMixin
required
Processor to update.
DataArguments
required
Data arguments containing pixel and frame limits.
Updates:
  • Image processor: min_pixels, max_pixels
  • Video processor: min_pixels, max_pixels, min_frames, max_frames, fps
Returns: Updated processor instance.

preprocess_qwen_visual()

Preprocesses vision-language data into model inputs.
list[dict]
required
List containing a single data sample with:
  • conversations - List of conversation turns
  • image - Image path(s) or list of paths
  • video - Video path(s) or list of paths
  • data_path - Base directory path
ProcessorMixin
required
Qwen-VL processor for tokenization and vision processing.
Returns: Dictionary with:
  • input_ids - Tokenized sequences
  • labels - Labels with assistant responses marked
  • pixel_values, image_grid_thw - Image data
  • pixel_values_videos, video_grid_thw - Video data

read_jsonl()

Reads JSONL format annotation files.
str
required
Path to JSONL file.
Returns: List of parsed JSON objects.

Data Collators

DataCollatorForSupervisedDataset

Collates batches for standard training.
PreTrainedTokenizer
required
Tokenizer for padding configuration.
Collation Process:
  1. Pads input_ids and labels to batch max length
  2. Truncates sequences to model_max_length
  3. Concatenates all images and videos across batch
  4. Concatenates grid dimension tensors
  5. Creates attention mask from padding
Returns: Batch dictionary with:
  • input_ids - Padded input sequences
  • labels - Padded labels
  • attention_mask - Padding mask
  • position_ids - 3D position IDs
  • pixel_values - Concatenated images
  • image_grid_thw - Image grid dimensions
  • pixel_values_videos - Concatenated videos
  • video_grid_thw - Video grid dimensions

FlattenedDataCollatorForSupervisedDataset

Collates batches for packed/flattened training.
Differences from standard collator:
  • Concatenates sequences without padding between samples
  • Uses cumulative sequence length tensor for attention mask
  • Optimized for Flash Attention variable-length inputs
  • Maximizes GPU utilization by eliminating padding
Returns: Batch with:
  • attention_mask - Cumulative sequence length indices (for Flash Attention)
  • Other fields same as standard collator

Dataset Factory

make_supervised_data_module()

Creates dataset and collator for training.
ProcessorMixin
required
Qwen-VL processor instance.
DataArguments
required
Data configuration arguments.
Returns: Dictionary with:
  • train_dataset - LazySupervisedDataset instance
  • eval_dataset - None (evaluation not implemented)
  • data_collator - FlattenedDataCollatorForSupervisedDataset if packing/flatten enabled, otherwise DataCollatorForSupervisedDataset

Data Format

Conversation Format

Dataset annotations should follow this structure:

Multi-Image Example

Video Example

Usage Example

Advanced Features

Data Packing

When data_packing=True, multiple training examples are packed into single sequences:
Benefits:
  • Reduces padding overhead
  • Improves GPU utilization
  • Increases effective batch size
  • Requires Flash Attention for variable-length support

Custom Dataset Registration

Register custom datasets in data_list.py:

Position IDs for RoPE

The dataset generates 3D rotary position embeddings using model-specific functions:
  • get_rope_index_2() - Qwen2-VL models
  • get_rope_index_25() - Qwen2.5-VL models
  • get_rope_index_3() - Qwen3-VL models
These handle the complex position encoding required for vision-language inputs with variable-resolution images and videos.