GIL contention between pin_memory & main trainer threads?

cyme: <p>We’re observing what looks like recurring patterns of significant competition for the GIL between the pin_memory and the main trainer threads. See in the trace below: the trainer thread seems to be stalled by the pin_memory thread repeatedly holding the GIL during some long operations.</p>
<p>In this case, it’s PyTorch code that’s holding the GIL in THPStorage_newSharedFd. But I’ve observed other instances of this pattern, e.g. in the Python built-in _io module. And there was <a href="https://github.com/pytorch/pytorch/issues/77139" rel="noopener nofollow ugc">another report</a> of a similar issue (now mitigated) with the pin_memory thread.</p>
<p>So it appears we are looking at a pattern of long-running operations holding the GIL, rather than an isolated problem. To make things worse, the performance hit is greatly amplified in a distributed training environment, as the probability of any rank hitting this GIL contention pattern increases.</p>
<p>I can’t think of a straightforward solution. I understand from <a href="https://discuss.pytorch.org/t/why-not-multiprocess-pin-memory-in-data-loader/197345/2">this discussion</a> that memory pinning cannot be moved to a different process. Do you see a mitigation, short of rewriting the multiprocessing queue and memory pinning logic in C++?</p>
<p>cc: <a class="mention" href="/u/alband">@albanD</a> , <a class="mention" href="/u/ezyang">@ezyang</a></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/2/72090720b46f79bf36a48358f36d28ce52d8e229.png" data-download-href="https://discuss.pytorch.org/uploads/default/72090720b46f79bf36a48358f36d28ce52d8e229" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/2/72090720b46f79bf36a48358f36d28ce52d8e229_2_652x500.png" alt="image" data-base62-sha1="ggNLY5dAO6rLehizx8zZpu81u53" width="652" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/2/72090720b46f79bf36a48358f36d28ce52d8e229_2_652x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/2/72090720b46f79bf36a48358f36d28ce52d8e229_2_978x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/2/72090720b46f79bf36a48358f36d28ce52d8e229_2_1304x1000.png 2x" data-dominant-color="B3B3B7"><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">1405×1076 219 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></p>

albanD: <p>Hey!<br>
This is a very good question!<br>
I think the long term solution here is going to switch to no-gil python. If you have this flexibility, 3.13 should have this as an opt-in feature and so is not that far away!</p>
<p>If this is not an option, then I guess yes moving part of this in C++ could help. But it’s not super clear to me because the only parts that you can really move to c++ are the ones that don’t use python, and these ones we should be releasing the GIL today (do open issues / send PR to fix that if we don’t!).<br>
So short of re-writing the whole queuing system, I’m not sure this is going to help indeed.<br>
The thing is the multiprocessing module supports a lot of features and will be quite daunting to rewrite. If you’re only sending Tensors over the wire though, it might be “relatively” simple to have such an extension. Integrating it within PyTorch dataloader might be a bit challenging though.</p>
<p>So a good middle ground (between moving to the latest beta CPython version and rewriting a whole multiprocessing comm protocal) here might be to systematically hunt down any missing gil release?</p>

cyme: <p>Thanks for the thorough answer, <a class="mention" href="/u/alband">@albanD</a> ! For a long term solution, I agree the C++ rewrite option is not a clear case given the level of effort and unknowns. The case becomes even more difficult to make if the PyTorch team is considering adopting the no-gil Python developments. In the meantime, hunting down the worst offenders seems like the most practical option. We’ll look in that direction.</p>
<p>FWIW to facilitate this, I have added GIL monitoring capability to Python (new GIL release / wait / acquire event notifications sent to the <a href="https://docs.python.org/3/library/sys.html#sys.setprofile" rel="noopener nofollow ugc">built-in Python profiler hook</a>) and I also patched the PyTorch profiler to surface these events in traces. See highlights in trace below. I’m planning to release these patches as PRs for anybody else interested in analyzing GIL contention issues.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/e/be4fb34387ac0e1b9ccb920f9881dde514658b5b.png" data-download-href="https://discuss.pytorch.org/uploads/default/be4fb34387ac0e1b9ccb920f9881dde514658b5b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/e/be4fb34387ac0e1b9ccb920f9881dde514658b5b_2_690x365.png" alt="image" data-base62-sha1="r9ztxd5puiqsfvVueyDfIUJN7XJ" width="690" height="365" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/e/be4fb34387ac0e1b9ccb920f9881dde514658b5b_2_690x365.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/e/be4fb34387ac0e1b9ccb920f9881dde514658b5b_2_1035x547.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/e/be4fb34387ac0e1b9ccb920f9881dde514658b5b_2_1380x730.png 2x" data-dominant-color="C2BFC0"><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">1475×782 142 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></p>

divyanshk: <p>Pretty cool, interested in trying out the GIL monitoring in PyTorch profiler. Do you have the PRs for it (not sure if it is already out there)? Thanks.</p>