ultralytics / ultralytics

Ultralytics YOLO11 πŸš€
https://docs.ultralytics.com
GNU Affero General Public License v3.0
36.58k stars 7.05k forks source link

Potential memory leak #19070

Open NicolasDrapier opened 1 week ago

NicolasDrapier commented 1 week ago

Search before asking

Ultralytics YOLO Component

No response

Bug

I am encountering a memory leak issue when using the Detect head with my custom backbone and the v8DetectionLoss function. This results in a CUDA out-of-memory (COOM) error during training. Switching to the CenterNet head resolves the issue, suggesting the problem is specific to the Detect head configuration.

Periodically empty PyTorch's cache using torch.cuda.empty_cache() every few steps temporarily mitigates the issue but does not resolve the root cause.

How can I help you to find the root cause of this problem?

Model:

Using auto half precision backend
***** Running training *****
  Num examples = 11,214
  Num Epochs = 30
  Instantaneous batch size per device = 16
  Total train batch size (w. parallel, distributed & accumulation) = 16
  Gradient Accumulation steps = 1
  Total optimization steps = 21,030
  Number of trainable parameters = 96,642,290

Environment

Collecting environment information...
PyTorch version: 2.4.0+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.1 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-52-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: 12.0.140
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4090
Nvidia driver version: 555.42.06
cuDNN version: Could not collect
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):                               32
On-line CPU(s) list:                  0-31
Vendor ID:                            GenuineIntel
Model name:                           13th Gen Intel(R) Core(TM) i9-13900K
CPU family:                           6
Model:                                183
Thread(s) per core:                   2
Core(s) per socket:                   24
Socket(s):                            1
Stepping:                             1
CPU(s) scaling MHz:                   99%
CPU max MHz:                          5800.0000
CPU min MHz:                          800.0000
BogoMIPS:                             5990.40
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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            896 KiB (24 instances)
L1i cache:                            1.3 MiB (24 instances)
L2 cache:                             32 MiB (12 instances)
L3 cache:                             36 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-31
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 Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   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 / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] ament-flake8==0.19.0
[pip3] flake8==7.0.0
[pip3] flake8-blind-except==0.2.1
[pip3] flake8-builtins==2.1.0
[pip3] flake8-class-newline==1.6.0
[pip3] flake8-comprehensions==3.14.0
[pip3] flake8-deprecated==2.2.1
[pip3] flake8-docstrings==1.6.0
[pip3] flake8-import-order==0.18.2
[pip3] flake8-quotes==3.4.0
[pip3] numpy==1.26.3
[pip3] nvidia-cublas-cu12==12.4.2.65
[pip3] nvidia-cuda-cupti-cu12==12.4.99
[pip3] nvidia-cuda-nvrtc-cu12==12.4.99
[pip3] nvidia-cuda-runtime-cu12==12.4.99
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.0.44
[pip3] nvidia-curand-cu12==10.3.5.119
[pip3] nvidia-cusolver-cu12==11.6.0.99
[pip3] nvidia-cusparse-cu12==12.3.0.142
[pip3] nvidia-ml-py==12.535.161
[pip3] nvidia-nccl-cu12==2.20.5
[pip3] nvidia-nvjitlink-cu12==12.4.99
[pip3] nvidia-nvtx-cu12==12.4.99
[pip3] onnx==1.17.0
[pip3] onnxruntime==1.20.1
[pip3] pyzmq==26.0.3
[pip3] torch==2.4.0+cu124
[pip3] torchaudio==2.4.0+cu124
[pip3] torchinfo==1.8.0
[pip3] torchsummary==1.5.1
[pip3] torchvision==0.19.0+cu124
[pip3] transformers==4.48.1
[pip3] triton==3.0.0
[pip3] visualtorch==0.2.3
[conda] Could not collect
ROCM Version: Could not collect
Neuron SDK Version: N/A
vLLM Version: N/A
vLLM Build Flags:
CUDA Archs: Not Set; ROCm: Disabled; Neuron: Disabled
GPU Topology:
GPU0    CPU Affinity    NUMA Affinity   GPU NUMA ID
GPU0     X  0-31    0       N/A

Legend:

  X    = Self
  SYS  = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI)
  NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node
  PHB  = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU)
  PXB  = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge)
  PIX  = Connection traversing at most a single PCIe bridge
  NV#  = Connection traversing a bonded set of # NVLinks

Minimal Reproducible Example

not yet

Additional

No response

Are you willing to submit a PR?

UltralyticsAssistant commented 1 week ago

πŸ‘‹ Hello @NicolasDrapier, thank you for bringing this to our attention! πŸš€ We understand how important it is to get your issue resolved. Here's how you can help us assist you more effectively:

To investigate this πŸ› potential bug thoroughly, we kindly ask that you provide a minimum reproducible example (MRE). This helps ensure that we can pinpoint the issue quickly and provide an accurate solution. The MRE should ideally include:

In the meantime, please ensure that you are using the latest version of Ultralytics. Upgrade with the following command and let us know if the issue persists afterward:

pip install -U ultralytics

We also recommend running your implementation in one of our verified environments to ensure compatibility. YOLO may be run in environments such as:

πŸ“– While we await your MRE, you may also find it useful to visit our Documentation and explore our Model Training Tips for potential insights.

If you'd like to connect with the broader community for additional support while we investigate, feel free to join us on Discord 🎧, Discourse, or our Subreddit.

An Ultralytics engineer will also review your issue soon and provide further assistance. Thank you for your patience and for helping improve Ultralytics! 😊

Y-T-G commented 1 week ago

If it's only with your custom backbone, then it could be related to the backbone since it doesn't occur with the default backbone.

NicolasDrapier commented 1 week ago

It does not occur with my backbone and the CenterNet head. It occurs with my backbone and this Detect head. My backbone is just the Swin Transformer from hugging face

glenn-jocher commented 1 week ago

@NicolasDrapier for memory leaks specific to custom backbones with Detect heads, we recommend:

  1. Profile memory usage with torch.cuda.memory_profiler to identify exact leak locations
  2. Compare forward/backward passes between CenterNet and Detect heads using PyTorch hooks
  3. Check for retained graph references in your detection head implementation, particularly in loss calculation

The v8DetectionLoss maintains gradient histories - ensure intermediate outputs are properly detached when using custom components. Would you mind sharing a minimal reproducible code snippet showing your head-backbone integration? This would help us investigate further.