Pytorch DataLoader pipelining not working

dlsf: <p>I would expect DataLoader to load batches concurrently to the main process, refilling the buffer as soon as a batch is consumed from the buffer. However, when I track the utilization of GPU and order of loading vs execution I see some different behavior:</p>
<ol>
<li>loading of the whole buffer (expected)</li>
<li>consuming the whole buffer by execution of all the batches until the buffer is empty (not expected)</li>
<li>loading the whole buffer again without parallel execution (not expected)</li>
<li>goto 2.</li>
</ol>
<p>This obviously results in dips in GPU utilization when in step 3.</p>
<p>I set:</p>
<p>num_workers &gt;= 1</p>
<p>pin_memory = True/False (doesn’t influence the described behavior)</p>
<p>Did anyone experience the same? What could be the issue?</p>

ptrblck: <p>Your experience would mean that e.g. if you are using 10 workers, the GPU would starve after 10 batches, as each worker would have to load the next batch?<br>
I haven’t seen this behavior yet and only see a latency in the first epoch of the <code>DataLoader</code>, where each worker is loading the batch. Afterwards the data loading time should be hidden in the background, if your actual training workload is large enough.</p>

dlsf: <p>Yes, that’s more or less what happens.<br>
After some isolated tests of just loading batches from the dataloader without doing any GPU processing, I think the problem could stem from the size of the epoch and how the dataloader iterates over epochs. The current dataset only has 50 samples (long videos) from which smaller samples are taken. The dataloader iterates over epochs of 50. Is it possible that the dataloader only prefetches until the end of the epoch is reached and after the last batch is prefetched, it waits until completion of all batches on the GPU? Afterwards it somehow “restarts”? This could explain the behavior I’ve seen lately: Near 100% GPU utilization with regular intermediate dips to 0%</p>
<p>(I’m using a map-style dataset)</p>
<p>EDIT:<br>
Probably that’s the issue right here (<a href="https://pytorch.org/docs/stable/data.html" rel="nofollow noopener">https://pytorch.org/docs/stable/data.html</a>):</p>
<h3>Multi-process data loading</h3>
<p>Setting the argument  <code>num_workers</code>  as a positive integer will turn on multi-process data loading with the specified number of loader worker processes.</p>
<p>In this mode, <strong>each time</strong> an iterator of a <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader" rel="nofollow noopener"> <code>DataLoader</code> </a> is created (e.g., when you call  <code>enumerate(dataloader)</code> ),  <code>num_workers</code>  worker processes are created. At this point, the  <code>dataset</code> ,  <code>collate_fn</code> , and  <code>worker_init_fn</code>  are passed to each worker, where they are used to initialize, and fetch data. This means that dataset access together with its internal IO, transforms (including  <code>collate_fn</code> ) runs in the worker process.</p>
<p>Shouldn’t the implementation of torch.utils.data.DataLoader take care of seamless prefetching over epochs?</p>

ptrblck: <p>In a lot of use cases training a complete epoch takes a lot of more time than reinitializing the worker processes, so that the epoch start could be ignored.<br>
It’s probably an edge case, but you could also manipulate some arguments of the <code>DataLoader</code> itself or the underlying <code>Dataset</code> between epochs.</p>
<p>However, if this behavior is the bottleneck in your code, you might try to e.g. increase the length of your <code>Dataset</code> artificially (<code>__len__</code> could return multiple epochs and in <code>__getitem__</code> you could use the <code>%</code> operator) or warm-up the next iterator via <code>loader_iter2 = iter(train_loader)</code> during one of the last iterations of your training.</p>

dlsf: <p>I could replicate the issue with two extreme cases (artificial):</p>
<ol>
<li>epoch of 10000 samples</li>
<li>epoch of 1 sample</li>
</ol>
<p>-&gt; 1. has 4 times higher bitrate when loading my data, which confirms the suspected issue.</p>
<p>That’s exactly what I came up with aswell <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> (first case)</p>
<p>I agree with your statement, that this is probably en edge case. However, I would expect torch.utils.data.DataLoader to be an infinite iterator. I therefore think seamless batch loading should be implemented natively because: in any case, if you learn over more than 1 epoch, there’s an inherent unnecessary downtime (which is probably not expected by the users). If you learn over rather small epochs, this downtime can lead to significant lower GPU utilization, wasting precious processing time. Do you think this could be an easy fix in torch.utils.data.DataLoader?</p>

ptrblck: <p>There might be a “fix”, but it would be interesting to see the performance improvements for some typical use cases first.<br>
If the speedup is marginal (besides extreme cases, where an epochs use 1 sample), I’m not sure it’s worth the effort.<br>
How big is the slowdown in each epoch for your current use case?</p>