What is ~1.4 GB CPU memory jump when call torch.distributed.barrier?

username: pbelevich
cooked: <p>Hi, dear former colleagues!</p>
<p>What is ~1.4GB CPU memory jump when call torch.distributed.barrier?</p>
<p>Tested with 1 and 8 GPUs, PT 1.9.0 and 1.12.1:</p>
<pre><code class="lang-auto">import torch
from memory_profiler import profile

@profile
def test():
    print("before")
    torch.distributed.barrier()
    print("after")

torch.distributed.init_process_group(backend='nccl', rank=0, world_size=1, init_method='tcp://localhost:23456')

test()
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     4    227.2 MiB    227.2 MiB           1   @profile
     5                                         def test():
     6    227.2 MiB      0.0 MiB           1       print("before")
     7   1641.7 MiB   1414.5 MiB           1       torch.distributed.barrier()
     8   1641.7 MiB      0.0 MiB           1       print("after")
</code></pre>
<p>Different training on 8 GPUs:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/4/f4af698e55923dbe031b9c10d43e61ad3f2d96dd.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f4af698e55923dbe031b9c10d43e61ad3f2d96dd" title="Screen Shot 2023-08-01 at 4.22.49 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/4/f4af698e55923dbe031b9c10d43e61ad3f2d96dd_2_690x378.jpeg" alt="Screen Shot 2023-08-01 at 4.22.49 PM" data-base62-sha1="yUAjXEhYoTmaXjphaeN6r6b2gwZ" width="690" height="378" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/4/f4af698e55923dbe031b9c10d43e61ad3f2d96dd_2_690x378.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/4/f4af698e55923dbe031b9c10d43e61ad3f2d96dd_2_1035x567.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/4/f4af698e55923dbe031b9c10d43e61ad3f2d96dd_2_1380x756.jpeg 2x" data-dominant-color="F2F0F1"><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">Screen Shot 2023-08-01 at 4.22.49 PM</span><span class="informations">1676×920 171 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>

username: wconstab
cooked: <p>This looks like a bug.  Could you file a github issue and link it back here (<a href="https://github.com/pytorch/pytorch/issues/new/choose" class="inline-onebox" rel="noopener nofollow ugc">Sign in to GitHub · GitHub</a>)?  I will try to reproduce on the latest pytorch build.</p>

username: pbelevich
cooked: <aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/106511">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/106511" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/106511" target="_blank" rel="noopener nofollow ugc">1.4 GB CPU memory jump when call torch.distributed.barrier</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2023-08-03" data-time="02:16:07" data-timezone="UTC">02:16AM - 03 Aug 23 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/pbelevich" target="_blank" rel="noopener nofollow ugc">
          <img alt="pbelevich" src="https://avatars.githubusercontent.com/u/1160355?v=4" class="onebox-avatar-inline" width="20" height="20">
          pbelevich
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

```python
import torch
from memory_profiler import pro<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">file

@profile
def test():
    print("before")
    torch.distributed.barrier()
    print("after")

torch.distributed.init_process_group(backend='nccl', rank=0, world_size=1, init_method='tcp://localhost:23456')

test()
```
Output:
```
Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     4    378.0 MiB    378.0 MiB           1   @profile
     5                                         def test():
     6    378.0 MiB      0.0 MiB           1       print("before")
     7   1800.1 MiB   1422.1 MiB           1       torch.distributed.barrier()
     8   1800.1 MiB      0.0 MiB           1       print("after")
```

### Versions

```
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.10.2
Libc version: glibc-2.27

Python version: 3.9.17 (main, Jul  5 2023, 20:41:20)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.4.0-1103-aws-x86_64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: 11.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: Tesla T4
GPU 1: Tesla T4
GPU 2: Tesla T4
GPU 3: Tesla T4
GPU 4: Tesla T4
GPU 5: Tesla T4
GPU 6: Tesla T4
GPU 7: Tesla T4

Nvidia driver version: 530.30.02
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              96
On-line CPU(s) list: 0-95
Thread(s) per core:  2
Core(s) per socket:  24
Socket(s):           2
NUMA node(s):        2
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
Stepping:            7
CPU MHz:             1200.056
CPU max MHz:         3500.0000
CPU min MHz:         1200.0000
BogoMIPS:            5000.00
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            36608K
NUMA node0 CPU(s):   0-23,48-71
NUMA node1 CPU(s):   24-47,72-95
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.25.0
[pip3] torch==2.0.1
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342  
[conda] mkl-service               2.4.0            py39h5eee18b_1  
[conda] mkl_fft                   1.3.6            py39h417a72b_1  
[conda] mkl_random                1.2.2            py39h417a72b_1  
[conda] numpy                     1.25.0           py39h5f9d8c6_0  
[conda] numpy-base                1.25.0           py39hb5e798b_0  
[conda] pytorch                   2.0.1           py3.9_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchtriton               2.0.0                      py39    pytorch
[conda] torchvision               0.15.2               py39_cu118    pytorch
```</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>