Dist.barrier got out of memory

ruotianluo: <p>I got this error randomly on the cluster. I am clueless and don’t know what the problem could be. One possibility is because multiple jobs may share the same machine, and it may cause some problem. But it’s unlikely that the gpus are occupied because I printed the nvidia-smi in the log and they are fine before running the code.</p>
<pre><code class="lang-auto"> File "/home/code/utils/distributed.py", line 128, in synchronize
   dist.barrier()
 File "/opt/conda/lib/python3.7/site-packages/torch/distributed/distributed_c10d.py", line 1710, in barrier
   work = _default_pg.barrier()
RuntimeError: CUDA error: out of memory
</code></pre>
<p>If someone has seen this error and found the solution, please let me know.<br>
If someone has a hint why this happens let me know.<br>
One more thing, this is running others’ code, I don’t know why they put this barrier before making the model distributeddataparallel and after dataloader initialization. Is barrier suggested to be used?</p>

ptrblck: <p>I don’t think the barrier causes the OOM, but might catch the error if it’s synchronizing the code.<br>
To avoid it, you would have to lower the batch size or slim down the model or inputs.</p>
<p>Also, make sure that the GPU memory is not increasing in each iteration, as this might happen if you are storing the complete computation graph via storing tensors which are still attached to the computation graph.</p>

ruotianluo: <p>It actually happens before the first iteration. Very weird. Currently I just resubmit and hope this time it will work. My guess it may be the issue of some particular machine, but I don’t know where to start to diagnose it.</p>

ptrblck: <p>Are your GPUs empty before you start the training?<br>
If that’s the case, you can check the memory after creating the model, data etc. via <code>print(torch.cuda.memory_allocated())</code>.</p>

amsword: <p>Also seen similar issues. After init_process_group, i run dist.barrier before creating data loader, creating model, … It crashes with OOM at dist.barrier, which is quite strange. Normally i resubmit the job, and the new job might be allocated to some new nodes, and then it can run smoothly sometimes. But this problem happens almost every day. GPU memory is 0 before it hits init_process_group(). pytorch 1.6.</p>

eric-haibin-lin: <p>I am hitting the same error. I got OOM before the model is created, with 30+ GB GPU memory available.<br>
<a class="mention" href="/u/amsword">@amsword</a>  Is your issue resolved?</p>

amsword: <p>it is hardware issue on my side. Try to use different nodes or check if the node has hardware issue.</p>

sbelharbi: <p>any updates on this?<br>
i am hitting the same issue.<br>
gpu of 32gb is <code>CUDA error: out of memory</code> while i am still parsing the args… it is so weird.<br>
the code runs fine on a gpu with 16gb and uses about 11gb on a local machine.</p>
<p>it happened at barrier. i am using ddp but using only one gpu.</p>
<pre><code class="lang-auto">dist.barrier()
import torch.distributed as dist
 call dist.barrier()
  File "python3.7/site-packages/torch/distributed/distributed_c10d.py", line 2524, in barrier
    work = default_pg.barrier(opts=opts)
RuntimeError: CUDA error: out of memory
</code></pre>
<p>any idea? this happened on a cluster with multi-nodes. i am using only one node with single gpu.<br>
pytorch 1.9.0<br>
cuda 11.1.1<br>
gpu type: nvidia v100 volta</p>
<p>thanks</p>

ptrblck: <p>Could you update to the latest stable release (or the nightly) and check, if you are hitting the same issue? If so, could you post a minimal executable code snippet to reproduce the issue, please?</p>

sbelharbi: <p>i have to ask admins to upgrade to torch 1.10.0 and if possible to nightly.<br>
they rebuild pytorch to optimize it on the hardware of the cluster.<br>
i’ll let you know.<br>
i already asked to upgrade to 1.10.0 (+torchvision) couple days ago. it takes time…<br>
i dont want to install from pypi as it may not reproduce the same error.<br>
and it is recommended, by admins, to install prebuilt (on cluster) packages.</p>
<p>thanks</p>

sbelharbi: <p>i upgraded to torch 1.10.0 and torchvision 0.11.1 that have been built on a server using cuda 11.4.<br>
now, i am getting a different error at the first call (probably during loading torch libs). the error does not point to a line in my code. but it seems related to loading distributed …<br>
any idea what could be the cause of this error?</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  Socket Timeout
Exception raised from recvBytes at /tmp-folder/pytorch_build_2021-11-09_14-57-01/moduleX/python3.7/pytorch/torch/csrc/distributed/c10d/Utils.hpp:619 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) + 0x55 (0x2b10898cb905 in VENV-NAME/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 0xd6 (0x2b10898ad2a9 in VENV-NAME/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #2: c10d::TCPStore::doWait(c10::ArrayRef&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::chrono::duration&lt;long, std::ratio&lt;1l, 1000l&gt; &gt;) + 0xe8 (0x2b10673a9058 in VENV-NAME/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)

</code></pre>
<p>the same versions installed using conda work fine on a local server.</p>
<p>thanks</p>

Yuhongze_Zhou: <p>Hello Soufiane Belharbi,<br>
Do you solve this issue? I encountered exactly the same problem. Thank you!</p>

sbelharbi: <p>hi Yuhongze,<br>
short answer: no.</p>
<p>both errors i mentioned here raised using pytorch that was built on server A(with multi-nodes).<br>
on a different server B (single machine, multi-gpus), pytorch works fine (installed using conda).</p>
<p>to make the code work on server A, i had to install the upstream version of pytorch/torchvision instead of the built version. this seems to have solved all issues. i think the built version has issues with ddp.</p>
<p>pip install torch==1.10.0 -f <a href="https://download.pytorch.org/whl/cu111/torch-1.10.0%2Bcu111-cp37-cp37m-linux_x86_64.whl" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu111/torch-1.10.0%2Bcu111-cp37-cp37m-linux_x86_64.whl</a></p>
<p>pip install torchvision==0.11.1 -f <a href="https://download.pytorch.org/whl/cu111/torchvision-0.11.1%2Bcu111-cp37-cp37m-linux_x86_64.whl" rel="noopener nofollow ugc">https://download.pytorch.org/whl/cu111/torchvision-0.11.1%2Bcu111-cp37-cp37m-linux_x86_64.whl</a></p>
<p>torch: 1.10.0<br>
torchvision: 0.11.1</p>
<p>also, this may depend on servers, the distributed backend could also be the cause. in the second error above, the error happened when calling tcp.<br>
on one server, mpi works better (== no error) than gloo. others, nccl.<br>
so, try to change the backend. also, see the recommended backend for ddp on your server.<br>
i didnt test this, but harovod could be another option for backend: <a href="https://horovod.readthedocs.io/en/latest/summary_include.html" class="inline-onebox" rel="noopener nofollow ugc">Overview — Horovod documentation</a> . you may need to change slightly the code for this backend: <a href="https://horovod.readthedocs.io/en/latest/pytorch.html" class="inline-onebox" rel="noopener nofollow ugc">Horovod with PyTorch — Horovod documentation</a> .</p>
<p>also, you can downgrade to torch 1.9.x.</p>
<p>let me know how it goes.</p>
<p>thanks</p>

imkzh: <p>Reset all gpus resolved the problem.</p>
<pre><code class="lang-auto">sudo nvidia-smi -r
</code></pre>
<p>you can check active process using gpu by (you should exit all of them before reset)</p>
<pre><code class="lang-auto">sudo lsof /dev/nvidia*
</code></pre>