Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python memory leak in torch.onnx.export of HF GPT2 #106976

Closed
samodi-nv opened this issue Aug 10, 2023 · 1 comment
Closed

Python memory leak in torch.onnx.export of HF GPT2 #106976

samodi-nv opened this issue Aug 10, 2023 · 1 comment
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@samodi-nv
Copy link

samodi-nv commented Aug 10, 2023

馃悰 Describe the bug

Network memory cannot be reclaimed by python after torch.onnx.export of a HF transformers GPT2 model.

>>> import psutil, torch, transformers, gc
>>> model = transformers.AutoModelForCausalLM.from_pretrained("gpt2")
>>> del model
>>> gc.collect()
41
>>> psutil.virtual_memory()
svmem(total=270117285888, available=265515003904, percent=1.7, used=2239725568, free=221613559808, active=3426291712, inactive=41614942208, buffers=413712384, cached=45850288128, shared=13168640, slab=2276139008)
>>> model = transformers.AutoModelForCausalLM.from_pretrained("gpt2")
>>> psutil.virtual_memory()
svmem(total=270117285888, available=264621350912, percent=2.0, used=3133378560, free=220719882240, active=3426316288, inactive=42506604544, buffers=413736960, cached=45850288128, shared=13168640, slab=2276065280)
>>> del model
>>> psutil.virtual_memory()
svmem(total=270117285888, available=265540001792, percent=1.7, used=2214719488, free=221638533120, active=3426316288, inactive=41591566336, buffers=413745152, cached=45850288128, shared=13168640, slab=2276065280)
>>> gc.collect()
49
>>> psutil.virtual_memory()
svmem(total=270117285888, available=265540239360, percent=1.7, used=2214481920, free=221638762496, active=3426324480, inactive=41591336960, buffers=413753344, cached=45850288128, shared=13168640, slab=2276065280)
>>> dummy_input = torch.zeros(1,10).long()
>>> torch.onnx.export(model, dummy_input, "dummy.onnx")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'model' is not defined
>>> model = transformers.AutoModelForCausalLM.from_pretrained("gpt2")
>>> torch.onnx.export(model, dummy_input, "dummy.onnx")
============= Diagnostic Run torch.onnx.export version 2.0.1+cu118 =============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================

>>> psutil.virtual_memory()
svmem(total=270117285888, available=264406749184, percent=2.1, used=3347980288, free=219851485184, active=3426377728, inactive=43362242560, buffers=413798400, cached=46504022016, shared=13168640, slab=2278494208)
>>> del model
>>> psutil.virtual_memory()
svmem(total=270117285888, available=264406749184, percent=2.1, used=3347972096, free=219851485184, active=3426377728, inactive=43362242560, buffers=413806592, cached=46504022016, shared=13168640, slab=2278494208)
>>> gc.collect()
0
>>> psutil.virtual_memory()
svmem(total=270117285888, available=264407302144, percent=2.1, used=3347427328, free=219852021760, active=3426394112, inactive=43362308096, buffers=413814784, cached=46504022016, shared=13168640, slab=2278494208)
>>> exit()

$ python3
>>> import psutil, torch, transformers, gc
>>> psutil.virtual_memory()
svmem(total=270117285888, available=265545867264, percent=1.7, used=2208858112, free=220990570496, active=3426861056, inactive=42230235136, buffers=413831168, cached=46504026112, shared=13168640, slab=2278260736)

Expected behavior: del model and gc.collect() after torch.onnx.export should free up all resources tied to the model (namely its weights). This is reproducible with larger 6.7B models as well where torch.onnx.export leaks a more problematic 26GB.

Versions

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

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
Clang version: 14.0.0-1ubuntu1
CMake version: version 3.27.0
Libc version: glibc-2.35

Python version: 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.14.0-1059-oem-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.2.128
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3080 Ti
GPU 1: Quadro P400

Nvidia driver version: 535.86.10
cuDNN version: Probably one of the following:
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.2
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.2
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
Address sizes:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          36
On-line CPU(s) list:             0-35
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              18
Socket(s):                       1
Stepping:                        7
CPU max MHz:                     4800.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        6000.00
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 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 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 avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       576 KiB (18 instances)
L1i cache:                       576 KiB (18 instances)
L2 cache:                        18 MiB (18 instances)
L3 cache:                        24.8 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-35
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==1.24.4
[pip3] torch==2.0.1+cu118
[pip3] triton==2.0.0
[conda] Could not collect
@samodi-nv
Copy link
Author

Still occurs in today's nightly, 2.1.0.dev20230810+cu118

@jbschlosser jbschlosser added module: onnx Related to torch.onnx module: memory usage PyTorch is using more memory than it should, or it is leaking memory triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants