Training fails due to memory exhaustion when running in a python multiprocessing.Process

username: mbbyn
cooked: <p>When deploying our training code, we schedule training jobs  in a <code>multiprocessing.Process</code> with <code>forkserver</code> context in order to supervise the work, isolate errors, etc.</p>
<p>Sadly, this crashes for training on sufficiently large dataset due to exhaustion of shared memory (<code>/dev/shm</code>). If increasing <code>/dev/shm</code> size was the answer, I wouldn’t be here. The problem is that it consistently fails as soon as the shared memory allocation reaches <code>250 MB</code> <strong>exactly, every time</strong>.</p>
<p>We have no idea who, what, where this seemingly arbitrary limit comes from. Any help or pointers would be appreciated. I acknowledge this issue might not be directly related to pytorch, but I’m not sure where else to ask it.</p>
<p>Some additional environment information:</p>
<ul>
<li>Using python 3.11.9</li>
<li>Tested on pytorch 2.1.2, 2.3.0</li>
<li><code>/dev/shm</code> size is &gt;10 GB</li>
<li>Reproducible with <code>spawn</code>, <code>forkserver</code>, <strong>but not</strong> <code>fork</code></li>
<li>Reproducible within docker (on RHEL 8) and WSL 2 (Ubuntu 20.04)</li>
<li><code>num_workers</code> must be &gt; 0. Even num_workers = 1, batch_size = 1 fails.</li>
<li>The error seems to be within <code>DataLoader</code> specifically when it is creating the iterator.</li>
<li>Training works fine running it outside <code>multiprocessing.Process</code></li>
</ul>
<p>Partial stacktrace</p>
<pre><code class="lang-auto">  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 434, in __iter__
    self._iterator = self._get_iterator()
                     ^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 387, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 1040, in __init__
    w.start()
  File "/usr/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/usr/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/multiprocessing/reductions.py", line 557, in reduce_storage
    metadata = storage._share_filename_cpu_()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/storage.py", line 304, in wrapper
    return fn(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.cache/pypoetry/virtualenvs/.../lib/python3.11/site-packages/torch/storage.py", line 378, in _share_filename_cpu_
    return super()._share_filename_cpu_(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: unable to mmap 320 bytes from file &lt;/torch_8549_3875816624_63978&gt;: Cannot allocate memory (12)
</code></pre>

username: mbbyn
cooked: <p>One wrong assumption threw us way off track… The multiprocessing context used by pytorch is dependent on the context of the parent process, we assumed it wasn’t.</p>
<p>Basically, by spinning off a python process using spawn or forkserver, the pytorch workers also switched to that context, leading to forkserver being the context used within pytorch. Once we added <code>set_start_method("fork", force=True)</code>, the problem went away. Turns out, regardless of how the parent process creates the sub process, whether through fork, forkserver, or spawn, if the pytorch context is anything other than fork, this leads to extensive usage of named files/file descriptors (based on your selection of sharing strategy), and that hits the system limits in different ways.</p>
<p>If using file descriptors, you hit “too many open files” or “too many fds”, since the number created (in our scenario) exceeds 15k fds. If the strategry is file_system, the error seems to be due to limits on “max_map_count”, found under <code>/proc/sys/vm/max_map_count</code>, and that limit, multiplied by 4 KB seems to give the magical 250 MB number.</p>
<p>We simply set the torch mp_context to fork and called it a day.</p>