Data loading time is nearly proportional to batchsize

Sergius_Liu: <p>There is one weird phenomenon that after I doubled my batchsize in a training process, the data loading time is almost doubled ( about 1.8 times long as original).<br>
I measure the data loading time by measure the interval between the end of each iter and beginning of next iter. ( That is when the for loop to fetch next batch).<br>
I simplified the logic as follows:</p>
<pre><code class="lang-auto">import ... 

end = time.time() 
For (input , label) in loader:
data_load_time = time.time() - end
output = model(input)
loss = crit(output, label) 
...
end = time.time() 
</code></pre>
<p>I conducted experiments on a single gpu to compare bch32 and bch64 settings:</p>
<blockquote>
<p>gpu 1 batch 32<br>
Time 2.505s (2.551s)	Speed 12.8 samples/s	Data 1.808s (1.849s)<br>
gpu 1 bch 64  (cpu 5,  worker 24)<br>
Time 4.707s (4.897s)    Speed 13.6 samples/s  Data 3.453s (3.599s)</p>
</blockquote>
<p>It shows that with larger batchsize, the dataLoading time is nearly doubled. (around 1.8).<br>
With multiple-workers, the loading process should be conducted before hand and will be ready when it is needed.<br>
So with enough workers, larger batch should not delay much comparing to a small batch setting as the loading is handled parallelly with multiple cpu thread.</p>
<p>This causes troubles.  It means more gpu resource (either more gpus or larger G-mem) will not help to reduce the training time, as the data loading time is proportional to batchsize. Even you get enough GPU to handle more data at same time, the dataloading will delay everything up.</p>

ptrblck: <aside class="quote no-group" data-username="Sergius_Liu" data-post="1" data-topic="101140">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sergius_liu/48/13538_2.png" class="avatar"> Sergius_Liu:</div>
<blockquote>
<p>With multiple-workers, the loading process should be conducted before hand and will be ready when it is needed.</p>
</blockquote>
</aside>
<p>Multiple workers try to load the data in the background, while the model training is executed.<br>
If the workers are not fast enough or bottlenecked by e.g. a local (spinning) HDD, they won’t be able to preload the next batch, which seems to be the case for your training routine.<br>
Have a look at <a href="https://discuss.pytorch.org/t/how-to-prefetch-data-when-processing-with-gpu/548/19">this post</a> for more information.</p>

Sergius_Liu: <p>It makes sense that IOPS could limit the reading speed as the data are lots of images.</p>
<p>Here is what I think:<br>
Let’s assume the IOPS is a fix number N.<br>
If the data reading workload is under the drive’s capability (let’s assume the corresponding batch size is n), the data loading time cost should be similar when the batch size is lower than n.<br>
Or we can assume that the batch/time curve is linear(proportional) when batch&gt;n and is flattened when batch is really small  batch&lt;=n.</p>
<p>So I did a test with varying batch size numbers:</p>
<blockquote>
<p>gpu 1 batch 32<br>
Time 2.505s (2.551s)	Speed 12.8 samples/s	Data 1.808s (1.849s)<br>
gpu 1 bch 64  (cpu 5,  worker 24)<br>
Time 4.707s (4.897s)    Speed 13.6 samples/s  Data 3.453s (3.599s)<br>
bch 16<br>
Time 1.349s (1.351s)    Speed 11.9 samples/s    Data 0.900s (0.917s)<br>
proportional to batchsize??</p>
<p>bch 8<br>
Time 0.814s (0.832s)    Speed 9.8 samples/s     Data 0.471s (0.476s)</p>
<p>bch 4<br>
Time 0.542s (0.590s)    Speed 7.4 samples/s     Data 0.242s (0.259s)<br>
…</p>
</blockquote>
<p>It is surprising that even the batch size is as small as 4， the reading time is still almost follows the linear curve trend instead of flattened.</p>
<p>Does it mean the reading is very slow and even 4 batch size is already large enough to make the data loading the bottle neck.</p>
<p>BTW, I am working on a cluster. The shared file system could cause this issue.</p>

ptrblck: <p>Might be the case and you could try to profile the data reading from your storage separately to get an idea of the max. throughput.<br>
The shared file system sound interesting. Is it a network drive? If so, this would most likely limit the loading speed.</p>

Sergius_Liu: <p>I have profiled the data reading with a python loop for loop.<br>
For cleanness, only the loop section is presented.</p>
<pre><code class="lang-auto">	for i, db in enumerate(train_dataset.db):
		st = time.time()
		image_file = db['image']
		data_numpy = cv2.imread(
			image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
		)
		smplRd_tm.update(time.time()- st)
		if i % 100 == 0 :       # print out the time
			print('Time cost {tm.val:.6f}s ({tm.avg:.6f})'.format(tm=smplRd_tm))
</code></pre>
<p>The images are supposed to be read continuously without any processing. So it can roughly measure how fast each image is read in from the file system. What I get is:</p>
<blockquote>
<p>…<br>
Time cost 0.009754s (0.022841)<br>
Time cost 0.015513s (0.022823)<br>
Time cost 0.034945s (0.022812)<br>
Time cost 0.012610s (0.022782)<br>
Time cost 0.017248s (0.022775)<br>
Time cost 0.030351s (0.022780)<br>
Time cost 0.030921s (0.022800)<br>
…</p>
</blockquote>
<p>So each image reading costs roughly 0.02 s.<br>
I found that bottleneck comes from the debug image saving in the training loop.<br>
I reduced frequency and it is much better now.<br>
Thanks.</p>