pytorch-lightning icon indicating copy to clipboard operation
pytorch-lightning copied to clipboard

Profiler misses dataloader worker initialization (and outdated docs)

Open yuxin00j opened this issue 5 months ago • 1 comments

Description & Motivation

The PyTorch Lightning profiler currently fails to capture the most expensive part of data loading: dataloader worker initialization and prefetching.

While the time spent fetching subsequent batches during the epoch is correctly captured under the [TrainingEpochLoop].train_dataloader_next action, the massive latency spike at the very start of training goes completely unrecorded. Because of this, users experiencing data-loading bottlenecks during startup are forced to monkey-patch private PyTorch Lightning internals (like FitLoop.setup_data and _PrefetchDataFetcher.__iter__) just to measure their initialization latency.

Here is why this happens:

  1. In FitLoop.setup_data(), Lightning calls iter(self._data_fetcher). This triggers PyTorch's DataLoader to spawn its multiprocessing workers. If it's an IterableDataset, it also triggers _PrefetchDataFetcher.__iter__ to eagerly prefetch the first batches.
  2. But the profiler isn't attached yet! The profiler hooks (data_fetcher._start_profiler = self._on_before_fetch) are only attached later, inside TrainingEpochLoop.
  3. As a result, the initialization phase goes completely unprofiled. (Additionally, the Profiler documentation still explicitly advertises get_train_batch as the profiled action, which has been outdated since the 2021 loops refactor).

Pitch

We would like to fix this by adding a profiling hook specifically for dataloader initialization, ensuring the startup latency appears in the standard Fit Profiler Report.

1. Profile dataloader initialization and worker spawning In src/lightning/pytorch/loops/fit_loop.py inside setup_data():

        self._data_fetcher = _select_data_fetcher(trainer, RunningStage.TRAINING)
        self._data_fetcher.setup(combined_loader)
        
        with trainer.profiler.profile("setup_train_dataloader"):
            iter(self._data_fetcher)  # Creates the iterator, spawns workers, and prefetches

2. Update Documentation Update the profiler docs to remove the outdated mentions of get_train_batch and replace them with the actual hooks: [TrainingEpochLoop].train_dataloader_next and setup_train_dataloader.

We can include a unit test using BoringModel to assert that "setup_train_dataloader" is successfully recorded in profiler.profiled_actions. We are happy to submit a PR for this if the Core team agrees with the approach!

Alternatives

The only alternative currently available to users is to manually monkey-patch private Lightning classes (e.g., overriding _FitLoop.setup_data and _PrefetchDataFetcher.__iter__), which is fragile and breaks between minor versions.

Additional context

No response

cc @lantiga @tchaton

yuxin00j avatar Jun 22 '26 12:06 yuxin00j

Seeing this gap in profiling matches what I've encountered running large-scale training pipelines with heavy data augmentation and multiprocessed loaders. The dataloader worker init can easily add several seconds upfront, especially with complex IterableDatasets and prefetching enabled.

A practical approach is to manually instrument this phase by wrapping FitLoop.setup_data with a custom timer or context manager before the profiler is attached. Another option is to hook into PyTorch DataLoader’s worker init function (worker_init_fn) to add timers there. For longer term, the Lightning team might consider moving profiler hook attachments earlier—right before iter(self._data_fetcher) is called—to capture that init latency natively.

Also, updating the docs to reflect the current TrainingEpochLoop action names would clear confusion. We’ve used python’s time.perf_counter() or torch.profiler at those early data loading points to get precise timing without monkey-patching internal Lightning classes. That approach gives visibility on the hidden startup cost without waiting on native profiler support.

smqd19 avatar Jun 22 '26 13:06 smqd19