OSError: [Errno 12] Cannot allocate memory. But memory usage is actually normal

fanl: <p>Hi,everyone<br>
I run my code on a node with 8 GPUs. I have several observations below.</p>
<ol>
<li>num_worker = 32 for both training loader and testing loader. In first epoch, it’s fine during training,  but the error occurs during testing after the epoch.</li>
<li>num_worker = 64 for both training loader and testing loader. Got the same results as (1).</li>
<li>64 and 8 for training loader and testing loader respectively. First epoch is fine, but the error occurs in the training of next epoch.</li>
<li>32 and 8 for training loader and testing loader respectively. Got the same results as (3).</li>
<li>num_worker = 16 for both training loader and testing loader. Everything is fine.</li>
<li>cat /proc/sys/kernel/pid_max: 32768;</li>
<li>memory usage is shown below with  num_worker = 32 for both training loader and testing loader.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/f/fcb9261d9a00d4e4442b503322d4107f39c29bfc.png" data-download-href="https://discuss.pytorch.org/uploads/default/fcb9261d9a00d4e4442b503322d4107f39c29bfc" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/f/fcb9261d9a00d4e4442b503322d4107f39c29bfc.png" alt="image" data-base62-sha1="A3H08wNjo2zMRebK8MO6mI1OsNK" width="690" height="78" data-dominant-color="080808"><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">image</span><span class="informations">996×113 5.72 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>
<p>This is my traceback:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train.py", line 225, in &lt;module&gt;
    trainer.train()
  File "train.py", line 164, in train
    acc = self.evaluate(epoch)
  File "train.py", line 198, in evaluate
    for idx, (img, label) in enumerate(self.loader_test):
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 819, in __iter__
    return _DataLoaderIter(self)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 560, in __init__
    w.start()
  File "/opt/conda/lib/python3.7/multiprocessing/process.py", line 112, in start
    self._popen = self._Popen(self)
  File "/opt/conda/lib/python3.7/multiprocessing/context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/opt/conda/lib/python3.7/multiprocessing/context.py", line 277, in _Popen
    return Popen(process_obj)
  File "/opt/conda/lib/python3.7/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
  File "/opt/conda/lib/python3.7/multiprocessing/popen_fork.py", line 70, in _launch
    self.pid = os.fork()
OSError: [Errno 12] Cannot allocate memory
</code></pre>
<p>What’s the possible causes?</p>

Shisho_Sama: <p>Set <code>num_worker</code> to 0 for both loaders (test and train") and see how that goes and if you see the error again.</p>

fanl: <p>The error doesn’t occur when num_worker = 0, but the time consuming of IO is extremely high. it’s not an option.</p>

Shisho_Sama: <p>Whats your batchsize and input size?</p>

fanl: <p>batchsize:256   input size:224*224</p>

Shisho_Sama: <p>knowing that smaller batches run just fine it shows that you just run out of memory!<br>
what is your model? how many parameters does it have?<br>
you can calculate the amount of memory used by your model and just multiply that by the threads .<br>
for example for the input you have :</p>
<pre><code class="lang-auto">256x(224x224x3) x 4 / 1024^2 = 147MB
</code></pre>
<p>assuming you are using <code>float32</code>. So 32 threads each reading 256 images, will take 4,704 MB. and this is only the input batch, you need to calculate how much memory your model takes and take that into account as well.<br>
if you want you can use <a href="https://github.com/Swall0w/torchstat" rel="nofollow noopener">torchstat</a> and get such info and then choose the best course of action.<br>
also recall that in training phase the memory usage is 2~3 times more than what you calculate for your number of parameters since you need to take into account the gradients and optimizes states as well.</p>

ptrblck: <p>How large is your shared memory?</p>

fanl: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/5/5b03e596452b4c9717880888b2c4c608848ef430.png" alt="image" data-base62-sha1="cZ9NwgUk7aQAPw4yNLoscdGs4RG" width="658" height="142"><br>
Thank you for your reply,<br>
The share memory limits are extremely large. I feel like it’s very strange. I run my code in an AI platform, and the outputs of <code>ipcs -lm</code> before and after logging to a node are the same.  I’m not familiar with the use of the platform.<br>
Is this information about shared memory accurate?<br>
Can you see anything meaningful in it?</p>

fanl: <p>Thank your for your analysis,<br>
From my perspective, given the error occurs in Dataloader, it’s not related to the size of model. It’s a matter of CPU memory instead of GPU memory. And the information of memory usage shows that memory is adequate.<br>
Have I got it wrong?</p>

Shisho_Sama: <p>If I recall correctly, the data is first cached in your system RAM, and then when your model signals for new data, it is transferred to the GPU memory and there you go.<br>
I guess either this is the case, or you have memory leak. if its memory leak, you should be able to tell by monitoring the memory usage during a time frame in your training.<br>
I once faced memory leak, during long training session, memory was exhausted and ultimately I’d get a out of memory error!</p>

zarat: <p>i think Starting by checking the vmsize of the process that failed to fork, at the time of the fork attempt, and then compare to the amount of free memory (physical and swap) as it relates to the overcommit policy (plug the numbers in.)</p>