8000 Memory leak in torch.save · Issue #149846 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Memory leak in torch.save #149846

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cdzhan opened this issue Mar 24, 2025 · 2 comments
Open

Memory leak in torch.save #149846

cdzhan opened this issue Mar 24, 2025 · 2 comments
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects needs reproduction Someone else needs to try reproducing the issue given the instructions. No action needed from user triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@cdzhan
Copy link
Contributor
cdzhan commented Mar 24, 2025

🐛 Describe the bug

It seems that the storage of tensor was not released immediately after this #136034.

>>> import torch
>>> import gc
>>> def test():
...  a = torch.randn(3)
...  torch.save(a, 'test.pt')
... 
>>> gc.set_debug(gc.DEBUG_SAVEALL)
>>> test()
>>> gc.collect()
35
>>> a = gc.garbage
>>> a
[{'mmap': <class 'bool'>, 'endianness': typing.Optional[ForwardRef('_LoadEndianess')], 'mmap_flags': typing.Optional[int], 'calculate_storage_offsets': <class 'bool'>}, (<class 'object'>,), {'__module__': 'torch.utils.serialization.config', '__annotations__': {'mmap': <class 'bool'>, 'endianness': typing.Optional[ForwardRef('_LoadEndianess')], 'mmap_flags': typing.Optional[int], 'calculate_storage_offsets': <class 'bool'>}, 'mmap': False, 'endianness': None, 'mmap_flags': 2, 'calculate_storage_offsets': False, '__dict__': <attribute '__dict__' of 'load' objects>, '__weakref__': <attribute '__weakref__' of 'load' objects>, '__doc__': None}, <class 'torch.utils.serialization.config.load'>, (<class 'torch.utils.serialization.config.load'>, <class 'object'>), <attribute '__dict__' of 'load' objects>, <attribute '__weakref__' of 'load' objects>, (<class 'object'>,), {'__module__': 'torch.utils.serialization.config', '__annotations__': {'compute_crc32': <class 'bool'>, 'use_pinned_memory_for_d2h': <class 'bool'>, 'storage_alignment': <class 'int'>}, 'compute_crc32': True, 'use_pinned_memory_for_d2h': False, 'storage_alignment': 64, '__dict__': <attribute '__dict__' of 'save' objects>, '__weakref__': <attribute '__weakref__' of 'save' objects>, '__doc__': None}, <class 'torch.utils.serialization.config.save'>, (<class 'torch.utils.serialization.config.save'>, <class 'object'>), <attribute '__dict__' of 'save' objects>, <attribute '__weakref__' of 'save' objects>, <cell at 0x7f4b8e29e230: dict object at 0x7f4c479fb000>, <cell at 0x7f4b8e29e140: ConfigModuleInstance object at 0x7f4c479e2890>, <cell at 0x7f4b8e29e110: function object at 0x7f4b8e272dd0>, ('source', typing.Union[module, type], 'dest', typing.Union[module, torch.utils._config_module.SubConfigProxy], 'prefix', <class 'str'>, 'return', None), (<cell at 0x7f4b8e29e230: dict object at 0x7f4c479fb000>, <cell at 0x7f4b8e29e140: ConfigModuleInstance object at 0x7f4c479e2890>, <cell at 0x7f4b8e29e110: function object at 0x7f4b8e272dd0>), <function install_config_module.<locals>.visit at 0x7f4b8e272dd0>, <cell at 0x7f4c479f7b20: dict object at 0x7f4b8e115f80>, <
8000
cell at 0x7f4c479f7550: function object at 0x7f4c479ffa30>, <cell at 0x7f4c479f67d0: dict object at 0x7f4c48342140>, <cell at 0x7f4c479f7d00: dict object at 0x7f4c479fb100>, (<cell at 0x7f4c479f7b20: dict object at 0x7f4b8e115f80>, <cell at 0x7f4c479f67d0: dict object at 0x7f4c48342140>, <cell at 0x7f4c479f7d00: dict object at 0x7f4c479fb100>), <function _save.<locals>.persistent_id at 0x7f4c479ffa30>, (<class '_pickle.Pickler'>,), (<cell at 0x7f4c479f7550: function object at 0x7f4c479ffa30>,), <function _save.<locals>.PyTorchPickler.persistent_id at 0x7f4b8e273010>, {'__module__': 'torch.serialization', 'persistent_id': <function _save.<locals>.PyTorchPickler.persistent_id at 0x7f4b8e273010>, '__dict__': <attribute '__dict__' of 'PyTorchPickler' objects>, '__weakref__': <attribute '__weakref__' of 'PyTorchPickler' objects>, '__doc__': None}, <class 'torch.serialization._save.<locals>.PyTorchPickler'>, (<class 'torch.serialization._save.<locals>.PyTorchPickler'>, <class '_pickle.Pickler'>, <class 'object'>), <attribute '__dict__' of 'PyTorchPickler' objects>, <attribute '__weakref__' of 'PyTorchPickler' objects>,  17
 148
 142
 191
 140
 242
 157
 63
 157
 4
 175
 62
[torch.storage.UntypedStorage(device=cpu) of size 12], {'0':  17
 148
 142
 191
 140
 242
 157
 63
 157
 4
 175
 62
[torch.storage.UntypedStorage(device=cpu) of size 12]}]

Versions

Collecting environment information...
PyTorch version: 2.8.0.dev20250319+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.12 (main, Feb 4 2025, 14:57:36) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.2.0-26-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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, 57 bits virtual
Byte Order: Little Endian
CPU(s): 112
On-line CPU(s) list: 0-111
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Gold 6330 CPU @ 2.00GHz
CPU family: 6
Model: 106
Thread(s) per core: 2
Core(s) per socket: 28
Socket(s): 2
Stepping: 6
CPU max MHz: 3100.0000
CPU min MHz: 800.0000
BogoMIPS: 4000.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 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 ss
e4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_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 rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx51
2cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 2.6 MiB (56 instances)
L1i cache: 1.8 MiB (56 instances)
L2 cache: 70 MiB (56 instances)
L3 cache: 84 MiB (2 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-27,56-83
NUMA node1 CPU(s): 28-55,84-111
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] mypy==1.11.2
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] onnx==1.17.0
[pip3] onnxscript==0.1.0.dev20240817
[pip3] optree==0.13.0
[pip3] torch==2.8.0.dev20250319+cpu
[pip3] torch-tb-profiler==0.4.3
[pip3] torchaudio==2.5.0.dev20241201+cpu
[pip3] torchvision==0.20.0.dev20241201+cpu
[conda] Could not collect

cc @mruberry @mikaylagawarecki

@malfet malfet added needs reproduction Someone else needs to try reproducing the issue given the instructions. No action needed from user module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Mar 24, 2025
@andrewilyas
Copy link

Also an issue when saving GPU tensors!

@drawfish
73D8
Copy link

Same problem when building a webdataset with torch.tensor. It seems to be a long-standing problem, because my pytorch version is 2.3.1.

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: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects needs reproduction Someone else needs to try reproducing the issue given the instructions. No action needed from user triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants
0