pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
83.3k stars 22.47k forks source link

Crash on cpp application exit with "corrupted size vs. prev_size while consolidating" followed by Segmentation fault #114506

Closed gorilux closed 11 months ago

gorilux commented 11 months ago

🐛 Describe the bug

TLDR; Crash on cpp application exit with "corrupted size vs. prev_size while consolidating" followed by Segmentation fault

Details: I've written a multithreaded c++ application that loads a collection pytorch modules for object detection. All the models are created in the main thread and inference is launched from a different thread into the cuda GPU. The snippet below is just an example that resembles the actual implementation of loading the models.

struct pytorch_model {
  torch::jit::script::Module module;
  torch::Device  device     = torch::kCPU;
};
std::vector<pytorch_model> model_collection {};
for(auto& cfg: configurations)
{
  auto& pm = model_collection.emplace_back( pytorch_model{});
   if (cfg.use_cuda && torch::cuda::is_available()) {
      pm.device = torch::kCUDA;
    } else {
      pm.device = torch::kCPU;
    }

  pm.module =  torch::jit::load(cfg.model_path);
  pm.module.to(pm.device);
}

Everything works perfectly until the application ends and there is a crash on the destructor cpp torch::jit::script::Module::~Module()

Here is a screen capture of the stack trace: image

Versions

Collecting environment information... PyTorch version: 2.1.0 Is debug build: False CUDA used to build PyTorch: 12.3 ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64) GCC version: (GCC) 13.2.1 20230801 Clang version: 16.0.6 CMake version: version 3.27.8 Libc version: glibc-2.38

Python version: 3.11.5 (main, Sep 2 2023, 14:16:33) [GCC 13.2.1 20230801] (64-bit runtime) Python platform: Linux-6.6.1-arch1-1-x86_64-with-glibc2.38 Is CUDA available: True CUDA runtime version: 12.3.52 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 Nvidia driver version: 545.29.02 cuDNN version: Probably one of the following: /usr/lib/libcudnn.so.8.9.6 /usr/lib/libcudnn_adv_infer.so.8.9.6 /usr/lib/libcudnn_adv_train.so.8.9.6 /usr/lib/libcudnn_cnn_infer.so.8.9.6 /usr/lib/libcudnn_cnn_train.so.8.9.6 /usr/lib/libcudnn_ops_infer.so.8.9.6 /usr/lib/libcudnn_ops_train.so.8.9.6 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: 43 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 24 On-line CPU(s) list: 0-23 Vendor ID: AuthenticAMD Model name: AMD Ryzen 9 3900XT 12-Core Processor CPU family: 23 Model: 113 Thread(s) per core: 2 Core(s) per socket: 12 Socket(s): 1 Stepping: 0 Frequency boost: disabled CPU(s) scaling MHz: 80% CPU max MHz: 5278.7100 CPU min MHz: 2200.0000 BogoMIPS: 8403.36 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 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 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 hw_pstate ssbd mba ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sev sev_es Virtualization: AMD-V L1d cache: 384 KiB (12 instances) L1i cache: 384 KiB (12 instances) L2 cache: 6 MiB (12 instances) L3 cache: 64 MiB (4 instances) NUMA node(s): 1 NUMA node0 CPU(s): 0-23 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: Mitigation; untrained return thunk; SMT enabled with STIBP protection Vulnerability Spec rstack overflow: Mitigation; safe RET 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; Retpolines, IBPB conditional, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] flake8==6.1.0 [pip3] mypy==1.3.0 [pip3] mypy-extensions==1.0.0 [pip3] numpy==1.26.1 [pip3] pytorch-lightning==2.0.7 [pip3] torch==2.1.0 [pip3] torchmetrics==1.1.0 [pip3] torchvision==0.15.2a0 [conda] Could not collect

cc @jbschlosser @ptrblck

malfet commented 11 months ago

Not yet adding oncall: jit as I want to debug it a bit further. @gorilux alas, this snippet is entirely sufficient to debug the problem. Would you object sharing the entire code? (Privately if you prefer)

gorilux commented 11 months ago

Not yet adding oncall: jit as I want to debug it a bit further. @gorilux alas, this snippet is entirely sufficient to debug the problem. Would you object sharing the entire code? (Privately if you prefer)

The project is currently private here on github. I can share it with you right now until I make it all public.

gorilux commented 11 months ago

@malfet There is no issue, the problem is within my code. Closing it.

malfet commented 11 months ago

@gorilux thank you, I was about to mention that it looks like that. Please do not hesitate to hesitate to remove me from the project.

SebastianBruijns commented 8 months ago

I have the same issue now, what was the problem in your case?

gorilux commented 8 months ago

I have the same issue now, what was the problem in your case?

In my case, I solved it by clearing the vector with the pytorch models before the application exited. I suspect it was a concurrency problem.