-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Description
🐛 Describe the bug
In some models user deallocates intermediates or function inputs using del
.
In case of graph breaks dynamo ignores del
and "deleted" variable will be alive by the end of frame (python function block).
import torch
import time
@torch.library.custom_op("_test::_clone", mutates_args={})
def f(x: torch.Tensor) -> torch.Tensor:
return x.clone()
def f_meta(x):
return torch.empty_like(x)
f.register_fake(f_meta)
@torch.compile
def fn(x):
x2 = torch.ops._test._clone(x)
x_int = x2 + 1
del x
torch._dynamo.graph_break()
del x_int
torch._dynamo.graph_break()
x3 = torch.ops._test._clone(x2)
return x3
def inp():
return torch.randn((100 * 1024 * 1024,), device="cuda")
time_s = time.strftime("%H-%M-%S")
fn(inp())
path = f"/tmp/del_snap_{time_s}.pickle"
torch.cuda.empty_cache()
torch.cuda.memory._record_memory_history(max_entries=100000)
fn(inp())
torch.cuda.memory._dump_snapshot(path)
print(f"SNAPSHOT_PATH:{path}")
eager snapshot on the left, compile on the right.
x_int in compile is held by the end of the function.
One of the workarounds is to wrap variables to delete into list, that only list object will be on the stack and calling list.clear()
instead of del
E.g.:
import torch
import time
@torch.library.custom_op("_test::_clone", mutates_args={})
def f(x: torch.Tensor) -> torch.Tensor:
return x.clone()
def f_meta(x):
return torch.empty_like(x)
f.register_fake(f_meta)
@torch.compile
def fn(x):
x2 = torch.ops._test._clone(x[0])
x_int = [x2 + 1]
x.clear()
del x
torch._dynamo.graph_break()
x_int.clear()
del x_int
torch._dynamo.graph_break()
x3 = torch.ops._test._clone(x2)
return x3
def inp():
return torch.randn((100 * 1024 * 1024,), device="cuda")
time_s = time.strftime("%H-%M-%S")
fn([inp()])
path = f"/tmp/del_snap_{time_s}.pickle"
torch.cuda.empty_cache()
torch.cuda.memory._record_memory_history(max_entries=100000)
fn([inp()])
torch.cuda.memory._dump_snapshot(path)
print(f"SNAPSHOT_PATH:{path}")
eager snapshot on the left, compile on the right

It helps to free intermediate x_int
in compile, but does not work for input wrapped in the list, that stays allocated by the end of the frame.
What could be the workaround for the inputs?
Could we respect del
in dynamo?
The case when model forward is "gigantic" function, user can use del for beter control of memory allocations. Ignoring del could result in higher peak memory for compile than in eager when user manually optimized it with del.
Refactoring into several functions and doing list/dict/set clear() instead of del could partially solve this, while compile will be silently holding some unexpected memory by user.
Error logs
No response
Versions
PyTorch version: 2.8.0a0+gitdaca611
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A
OS: CentOS Stream 9 (x86_64)
GCC version: (conda-forge gcc 13.3.0-2) 13.3.0
Clang version: 14.0.6
CMake version: version 3.31.2
Libc version: glibc-2.34
Python version: 3.10.16 (main, Dec 11 2024, 16:24:50) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-6.4.3-0_fbk20_zion_2830_g3e5ab162667d-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: 12.6.85
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA H100
GPU 1: NVIDIA H100
GPU 2: NVIDIA H100
GPU 3: NVIDIA H100
GPU 4: NVIDIA H100
GPU 5: NVIDIA H100
GPU 6: NVIDIA H100
GPU 7: NVIDIA H100
Nvidia driver version: 535.154.05
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.2
/usr/lib64/libcudnn.so.9.4.0
/usr/lib64/libcudnn_adv.so.9.4.0
/usr/lib64/libcudnn_adv_infer.so.8.9.2
/usr/lib64/libcudnn_adv_train.so.8.9.2
/usr/lib64/libcudnn_cnn.so.9.4.0
/usr/lib64/libcudnn_cnn_infer.so.8.9.2
/usr/lib64/libcudnn_cnn_train.so.8.9.2
/usr/lib64/libcudnn_engines_precompiled.so.9.4.0
/usr/lib64/libcudnn_engines_runtime_compiled.so.9.4.0
/usr/lib64/libcudnn_graph.so.9.4.0
/usr/lib64/libcudnn_heuristic.so.9.4.0
/usr/lib64/libcudnn_ops.so.9.4.0
/usr/lib64/libcudnn_ops_infer.so.8.9.2
/usr/lib64/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: 52 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 384
On-line CPU(s) list: 0-383
Vendor ID: AuthenticAMD
Model name: AMD EPYC 9654 96-Core Processor
CPU family: 25
Model: 17
Thread(s) per core: 2
Core(s) per socket: 96
Socket(s): 2
Stepping: 1
Frequency boost: enabled
CPU(s) scaling MHz: 81%
CPU max MHz: 3707.8120
CPU min MHz: 1500.0000
BogoMIPS: 4792.63
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid overflow_recov succor smca fsrm flush_l1d
Virtualization: AMD-V
L1d cache: 6 MiB (192 instances)
L1i cache: 6 MiB (192 instances)
L2 cache: 192 MiB (192 instances)
L3 cache: 768 MiB (24 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-95,192-287
NUMA node1 CPU(s): 96-191,288-383
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
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: Vulnerable: eIBRS with unprivileged eBPF
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected
Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.15.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.14.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-cusparselt-cu12==0.6.2
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] optree==0.13.0
[pip3] pytorch-triton==3.3.0+git96316ce5
[pip3] torch==2.8.0a0+gitdaca611
[pip3] torchao==0.11.0+gitb195c571
[pip3] torchdata==0.11.0
[pip3] torchtune==0.0.0
[pip3] torchvision==0.22.0a0+5f03dc5
[pip3] triton==3.2.0
[conda] blas 1.0 mkl defaults
[conda] cuda-cudart 12.4.127 h99ab3db_0 defaults
[conda] cuda-cudart_linux-64 12.4.127 hd681fbe_0 defaults
[conda] magma-cuda116 2.6.1 1 pytorch
[conda] mkl 2023.1.0 h213fc3f_46344 defaults
[conda] mkl-include 2025.0.0 hc79277c_941 defaults
[conda] mkl-service 2.4.0 py310h5eee18b_2 defaults
[conda] mkl_fft 1.3.11 py310h5eee18b_0 defaults
[conda] mkl_random 1.2.8 py310h1128e8f_0 defaults
[conda] nccl [2.21.5.1](https://www.internalfb.com/phabricator/paste/view/2.21.5.1) ha515578_0 defaults
[conda] numpy 1.26.4 pypi_0 pypi
[conda] nvidia-cublas-cu12 [12.4.5.8](https://www.internalfb.com/phabricator/paste/view/12.4.5.8) pypi_0 pypi
[conda] nvidia-cuda-cupti-cu12 12.4.127 pypi_0 pypi
[conda] nvidia-cuda-nvrtc-cu12 12.4.127 pypi_0 pypi
[conda] nvidia-cuda-runtime-cu12 12.4.127 pypi_0 pypi
[conda] nvidia-cudnn-cu12 [9.1.0.70](https://www.internalfb.com/phabricator/paste/view/9.1.0.70) pypi_0 pypi
[conda] nvidia-cufft-cu12 [11.2.1.3](https://www.internalfb.com/phabricator/paste/view/11.2.1.3) pypi_0 pypi
[conda] nvidia-curand-cu12 [10.3.5.147](https://www.internalfb.com/phabricator/paste/view/10.3.5.147) pypi_0 pypi
[conda] nvidia-cusolver-cu12 [11.6.1.9](https://www.internalfb.com/phabricator/paste/view/11.6.1.9) pypi_0 pypi
[conda] nvidia-cusparse-cu12 [12.3.1.170](https://www.internalfb.com/phabricator/paste/view/12.3.1.170) pypi_0 pypi
[conda] nvidia-cusparselt-cu12 0.6.2 pypi_0 pypi
[conda] nvidia-nccl-cu12 2.21.5 pypi_0 pypi
[conda] nvidia-nvjitlink-cu12 12.4.127 pypi_0 pypi
[conda] nvidia-nvtx-cu12 12.4.127 pypi_0 pypi
[conda] optree 0.13.0 pypi_0 pypi
[conda] pytorch-triton 3.3.0+git96316ce5 pypi_0 pypi
[conda] torch 2.8.0a0+gitdaca611 dev_0 <develop>
[conda] torchao 0.11.0+gitb195c571 dev_0 <develop>
[conda] torchdata 0.11.0 pypi_0 pypi
[conda] torchfix 0.4.0 pypi_0 pypi
[conda] torchtune 0.0.0 pypi_0 pypi
[conda] torchvision 0.22.0a0+5f03dc5 dev_0 <develop>
[conda] triton 3.2.0 pypi_0 pypi
cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @cheny 68D6 ang78 @kadeng @amjames