Loading data slow and processes all in kernel mode

kidd_lee: <p>I am sorry that currently I can not create a minimum code sample to reproduce the problems I meet.</p>
<h3><a name="problems-1" class="anchor" href="#problems-1"></a>Problems:</h3>
<p>Clues I can provide:</p>
<ol>
<li>Htop<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/7/87be150195257a6f107f531b84853ff77a7ad319.png" data-download-href="https://discuss.pytorch.org/uploads/default/87be150195257a6f107f531b84853ff77a7ad319" title="Screenshot from 2021-03-20 21-26-56"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87be150195257a6f107f531b84853ff77a7ad319_2_690x57.png" alt="Screenshot from 2021-03-20 21-26-56" data-base62-sha1="jmPHRnPxmQ3MM8t0Yh6bkIhLYLL" width="690" height="57" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87be150195257a6f107f531b84853ff77a7ad319_2_690x57.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87be150195257a6f107f531b84853ff77a7ad319_2_1035x85.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87be150195257a6f107f531b84853ff77a7ad319_2_1380x114.png 2x" data-dominant-color="2F4D5B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-03-20 21-26-56</span><span class="informations">3462×290 287 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
As can be seen from the screen shot, the htop command shows that four cpu cores are dominated by the red color, which means they are in kernel mode (from F1 help of htop).</li>
</ol>
<p>Four workers are used in my dataloader so the four red cores are exactly the ones belongs to my task.</p>
<p>As long as this happens, my training code gets extremely slow. The gpu usage decrease to dramatically (most of the times to zero). In the below figure, I use device 0.</p>
<ol start="2">
<li>Nvidia-smi<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/4/84544d62c4be37748d296b3831347f8d18a87fae.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/84544d62c4be37748d296b3831347f8d18a87fae" title="Screenshot from 2021-03-20 21-33-21"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84544d62c4be37748d296b3831347f8d18a87fae_2_517x351.jpeg" alt="Screenshot from 2021-03-20 21-33-21" data-base62-sha1="iSDDRnBO8PDTF6ab63J6ztvZGR8" width="517" height="351" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84544d62c4be37748d296b3831347f8d18a87fae_2_517x351.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84544d62c4be37748d296b3831347f8d18a87fae_2_775x526.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84544d62c4be37748d296b3831347f8d18a87fae_2_1034x702.jpeg 2x" data-dominant-color="324C5D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-03-20 21-33-21</span><span class="informations">1046×712 285 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></li>
</ol>
<h4><a name="the-information-currently-know-2" class="anchor" href="#the-information-currently-know-2"></a>The information currently know:</h4>
<ol>
<li>If the process goes into <strong>kernel mode</strong>, it is likely that IO operation is called.  So,  the first guess would be my dataloader is waiting for the data from disk.</li>
</ol>
<p>a) If too much data waiting to be loaded from disk?<br>
I checked the <strong>io speed</strong> using a serious of tools such as IOTop, hdparm.</p>
<ul>
<li>(Disproof) The reported disk reading speed is around <strong>20MB-30MB</strong>, which is fine for even a hdd. Not to mention I use ssd to store my dataset.</li>
</ul>
<p>b) If my disk is broken?</p>
<ul>
<li>
<p>(Proof) If my task <strong>stop reading data from disk</strong> (replace cv2.imread/PIL.Image.open with random data by numpy) while all other codes remain the same, training task is always fine. This means the problems are related to IO.</p>
</li>
<li>
<p>(Disproof) As the training task runs, I tested the disk performance, the IO band-width is around 300MB. So I think the disk is all right. The training task of other people on the same server is all right. They never face my problems although we are using the same disks.</p>
</li>
<li>
<p>(Disproof) If I <strong>execute two training tasks in parallel</strong>, the problems almost always happens. But this also always happens if two tasks <strong>load data from different disks</strong> (both SSDs, one nvme + one Sata). In this cases, both task slows down dramatically although they are loading from different disks. As long as I stop one task, the other will resume to normal trainng speed with a high GPU utilization. I can not suspect that all three disks (hdd+sata ssd+nvme ssd) on my server are broken. In this case, it is likely the disks are not the ones to be blamed but other shared hardwares are. RAM? PCIE?.. I am do not know.</p>
</li>
<li>
<p>(Disproof) The problem do not always happen. If I restart my training task, there is a probability that it disappear and everything goes fine.<br>
If the problem shows at the beginning of the task, there is little possibility that problem will disappear later.<br>
If the problem do not showup at the beginning, it will almost never happens later.</p>
</li>
</ul>
<p>c) If something wrong with opencv?</p>
<ul>
<li>(Disproof) Tried both opencv and PIL.Image. The problems are the same. In the case of using opencv, the following codes are also tested, not working.</li>
</ul>
<pre><code class="lang-auto">cv.setNumThreads(0)
cv.ocl.setUseOpenCL(False)
</code></pre>
<p>d) If something wrong with certain version of torch?</p>
<ul>
<li>(Disproof) The problem occurs on versions of 1.7.0, 1.7.1, 1.8.0.</li>
</ul>
<p>e) If something wrong with other third-party libraries?</p>
<ul>
<li>I have not found a clue.</li>
</ul>
<p>f) Short of memory?</p>
<ul>
<li>There are 64GB RAM on server and I see neither OOM error nor utilization of swap.</li>
</ul>
<p>I searched for a long time and tried all the suggestions I found.<br>
What shall I do next to debug this problem.</p>

ThomasOerkild: <p>I’m having the exact same issue. Did you manage to find a solution?</p>

kidd_lee: <p>The issue arises from the Copy-On-Write (COW) behavior of the Fork mechanism, which is triggered by the increased reference count of Python objects when accessing them.</p>
<p>Here are some Links related to the issue:<br>
<a href="https://github.com/Lightning-AI/pytorch-lightning/issues/17257" rel="noopener nofollow ugc">https://github.com/Lightning-AI/pytorch-lightning/issues/17257</a><br>
<a href="https://stackoverflow.com/questions/71902144/htop-cpu-bar-red-100-kernel-time/72759615#72759615" rel="noopener nofollow ugc">https://stackoverflow.com/questions/71902144/htop-cpu-bar-red-100-kernel-time/72759615#72759615</a><br>
<a href="https://github.com/pytorch/pytorch/issues/13246" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/issues/13246</a></p>