Abstract neural network visualization with data flow connections

Optimizing PyTorch Data Loading for Large-Scale Multimodal LLM Pretraining

Addressing frequent Out-of-Memory errors and slow online computation in extreme-scale training environments

10B+ Samples 10K+ GPUs Multimodal Data
40x
Memory replication factor in naive DataLoader setups
OOM
Frequent worker process crashes during distributed training

Executive Overview

Large-scale multimodal LLM pretraining presents unprecedented data loading challenges. This comprehensive analysis addresses frequent Out-of-Memory (OOM) errors and slow online computation for image/video data in PyTorch environments. By implementing specialized data loading libraries like FFCV or WebDataset, leveraging GPU-accelerated preprocessing with NVIDIA DALI, and optimizing memory management, organizations can achieve substantial performance improvements while maintaining training stability at extreme scales.

Memory Optimization

Implement forkserver_preload and efficient worker memory management to reduce OOM errors by up to 80%

GPU Acceleration

Offload image and video decoding to GPU with DALI for 10-50x faster preprocessing throughput

Cloud Optimization

Stream from S3 with specialized connectors for 3.5x faster data loading from cloud storage

1. Understanding the Core Challenges

1.1 Scale of Data and Compute

The user's pretraining task involves massive scale across two dimensions: data volume and computational resources. The dataset comprises tens of billions of samples (tens of trillions of tokens) encompassing text, images, and videos stored on Amazon S3 services. Training utilizes tens of thousands of GPUs through the Megatron-LM framework.

Critical Bottleneck

This combination of vast data quantities, diverse data types, and distributed cloud storage creates unprecedented challenges for data loading pipelines. The extensive GPU resources risk being underutilized due to data supply bottlenecks.

1.2 Frequent Out-of-Memory Errors

The primary pain point is frequent OOM errors during data loading, not during GPU computation. This indicates inefficiencies within DataLoader worker processes. At scale, even small memory overheads per sample accumulate rapidly across thousands of concurrent workers.

Real-World Example: LLaVA-NeXT Issue #196

A concrete example from LLaVA-NeXT GitHub issue #196 demonstrates this challenge:

"DataLoader workers were killed due to signal Killed (OOM indicator) during distributed training of a multimodal model, even with --dataloader_num_workers 4 configuration."

The issue was linked to memory leakage in video processing libraries like decord, highlighting how third-party dependencies can compound memory pressure.

1.3 Slow Online Computation

The second major bottleneck is "slow online computation for image and video data", explicitly identified as a computation issue rather than I/O limitation. This involves CPU-intensive tasks:

  • Decoding compressed image files (JPEG, PNG) and video files (MP4, H.264)
  • Resizing, cropping, and normalizing high-resolution frames
  • Applying data augmentations and feature extraction
  • Converting processed data into PyTorch tensors

As noted in the PyTorch torchcodec blog, "decoding videos for ML training has different performance requirements than decoding videos for playback," emphasizing the need for efficient seeking and batch processing of frames from numerous videos.

2. Diagnosing DataLoader Bottlenecks

2.1 Standard PyTorch DataLoader Inefficiencies

The standard PyTorch DataLoader exhibits significant inefficiencies at extreme scale. Yuxin Wu's analysis reveals that each DataLoader worker incurs substantial memory overhead:

Memory Replication Problem

Each worker imports torch and loads dataset parts, creating memory duplication:

40x Replication

Example: 8 GPUs × 4 workers = 32× baseline memory usage

Per-Worker Overhead

  • ~160MB per worker for torch import
  • Dataset index and metadata copies
  • Library initialization overhead

2.2 High Memory Usage Patterns

Memory consumption within DataLoader workers extends beyond baseline overhead. The OVERLORD paper explains that "each worker process maintains independent file access states... imposing linear memory overhead growth with respect to the number of data sources."

Key Memory Pressure Sources

Processing Logic Issues
  • • Large intermediate tensors from decoding
  • • Memory leaks in C extensions (OpenCV, codecs)
  • • Unbounded cache growth in workers
System-Level Factors
  • • Circular references preventing garbage collection
  • • Heavy data sources requiring larger worker pools
  • • Combinatorial explosion with heterogeneous sources

2.3 Impact of On-the-Fly Decoding

The computational cost of on-the-fly image and video processing is a primary bottleneck. A Medium article on video preparation notes that on-the-fly frame extraction "significantly slows down training because for each epoch, frames must be re-extracted."

Video Decoding Process Flow

graph TD A["Compressed Video File"] --> B["File Seek & Read"] B --> C["Container Demuxing"] C --> D["Frame Decoding"] D --> E["Color Space Conversion"] E --> F["Resizing & Cropping"] F --> G["Normalization"] G --> H["Tensor Conversion"] H --> I["Augmentation"] I --> J["Batch Assembly"] style A fill:#e1f5fe,stroke:#01579b,stroke-width:2px,color:#000 style D fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#000 style F fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#000 style J fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px,color:#000

3. In-Memory Data Management Strategies

Worker Memory Optimization

forkserver_preload

Share torch import memory across workers using copy-on-write semantics

Selective Frame Decoding

Seek and decode only required frames using libraries like decord

Efficient Data Formats

FFCV Binary Format

Custom .beton format for direct memory mapping and fast random access

Memory-Mapped Files

numpy.memmap for handling datasets larger than available RAM

Pinned Memory & Asynchronous Transfer

pin_memory=True

Enable DMA transfers for faster CPU-to-GPU data movement

prefetch_factor

Overlap computation and data transfer with batched prefetching

non_blocking=True

Asynchronous tensor operations to prevent GPU starvation

Caution: Pinned memory is limited resource. Excessive allocation can cause host OOM errors even with available GPU memory.

Distributed Data Loading & Sharding

For training on tens of thousands of GPUs, effective sharding is essential:

S3 Storage Optimization

  • Shard across buckets and folders to avoid throttling
  • Optimal file size: 50-200MB per object
  • Use C++ S3 handler for better CPU utilization
  • Implement Exponential Backoff for error handling

WebDataset Integration

  • Sharded tar archives for efficient streaming
  • Sequential access patterns to maximize throughput
  • Built-in shuffling at shard level
  • AIStore compatibility for ETL operations

4. Efficient On-the-Fly Preprocessing

GPU-Accelerated Preprocessing

NVIDIA DALI Benefits

  • 10-50x faster image/video decoding
  • • Offload CPU-intensive operations to GPU
  • • Integrated augmentation pipelines
  • • Seamless PyTorch integration via DALIGenericIterator

Performance Impact

Image Decoding ~40x faster
Video Processing ~15x faster
CPU Load Reduction ~80%

Decord

Specialized video loader for deep learning with GPU support and selective frame reading

Best for: CPU-based video loading with efficient seeking

PyAV

Pythonic FFmpeg binding with fine-grained control over decoding process

Best for: Advanced video processing with FFmpeg flexibility

torchcodec

New PyTorch-native video decoding library with CUDA support

Best for: Integrated PyTorch workflows with GPU acceleration

Balancing Offline vs Online Processing

Strategy Speed Flexibility Storage Cost Best Use Case
Pure Online Slow High Low Rapid experimentation
Pure Offline Fast Low High Production training
Hybrid Approach Moderate Moderate Moderate General purpose

Recommendation: Use hybrid approach - preprocess heavy, stable operations offline (decoding, basic normalization) and perform dynamic augmentations online.

5. Third-Party Solutions & Advanced Techniques

FFCV

Fast Forward Computer Vision - specialized binary format for lightning-fast data loading

Benefits

  • Extremely fast I/O via memory mapping
  • • GPU-accelerated augmentation pipelines
  • • Efficient random access with precomputed indices
  • • Substantial training time reduction

Trade-offs

  • • Dataset conversion required upfront
  • • New API and concepts to learn
  • • Less flexible for rapid experimentation

WebDataset

Streaming solution for large datasets, especially from cloud storage like S3

Benefits

  • Efficient S3 streaming via sharded tar files
  • • Maximizes I/O throughput with sequential access
  • • Built-in shuffling and ETL capabilities
  • • AIStore integration for storage-side operations

Trade-offs

  • • Sequential access within shards
  • • Requires data organization into tar files
  • • Less optimal for small-scale experimentation

Cloud Storage Optimization

Amazon S3 Connector

Throughput Improvement 3.5x faster
Memory Efficiency C++ SDK
File Format Agnostic

MosaicML Streaming

  • • Compressed .mds file format
  • • Fast random access to samples
  • • Deterministic shuffling
  • • Efficient checkpoint resumption

Advanced Distributed Architectures

For extreme-scale training, advanced architectures like OVERLORD propose disaggregated data loading approaches that address fundamental limitations of colocated designs.

OVERLORD Architecture Benefits

Memory Management

Dedicated loader instances reduce memory pressure on training nodes by moving heavy preprocessing to specialized hardware

Global Orchestration

Intelligent scheduling across heterogeneous data sources prevents stragglers and ensures balanced resource utilization

6. Recommendations & Best Practices

Optimization Strategy Summary

Strategy Benefits Trade-offs Priority
GPU-Accelerated Preprocessing (DALI) Drastic CPU load reduction, faster transforms Learning curve, GPU resource usage HIGH
Memory Management (Workers) Reduced OOM errors, better resource utilization Requires careful tuning and profiling HIGH
Specialized Formats (FFCV) Very fast I/O, memory mapping Dataset conversion overhead MEDIUM
Streaming (WebDataset) Efficient S3 access, good for large datasets Requires data organization into shards HIGH
Advanced Architectures (OVERLORD) Global orchestration, load balancing Significant development effort FUTURE
1

Immediate Actions

  • • Profile current pipeline with PyTorch Profiler
  • • Implement forkserver_preload optimization
  • • Enable pin_memory and async transfer
  • • Tune num_workers based on memory profiling
2

Medium-term

  • • Integrate GPU-accelerated preprocessing (DALI)
  • • Evaluate FFCV for image datasets
  • • Implement WebDataset for S3 streaming
  • • Test optimized video libraries (torchcodec)
3

Long-term

  • • Consider advanced architectures like OVERLORD
  • • Implement distributed caching (Alluxio)
  • • Develop custom binary formats for video
  • • Build hybrid offline/online pipelines

Critical Success Factors

Multimodal Optimization

Implement modality-specific optimizations: lightweight processing for text, GPU acceleration for images, and specialized handling for video data.

Megatron-LM Integration

Ensure seamless integration with Megatron-LM's distributed training framework, paying special attention to checkpointing and sharding compatibility.

Performance Target: Achieve sustained GPU utilization above 90% while maintaining training stability across tens of thousands of GPUs.