Repository: pytorch
Issue: Libtorch crashes docker when included in header file
Labels: needs reproduction,module: abi,triaged

### 🐛 Describe the bug

I’ve been working on integrating libtorch C++ into my project for a few weeks and while it works fine when I include the
```
#include <torch/script.h>
#include <torch/torch.h>
```
headers in a cpp source file it works fine. But when I include them in a cpp header file it results in my docker environment crashing and the following error log. Please note, while the version section below only shows data for version 2.2.2 I have tried with every version since **v2.0.0**.

Also, I have put the issue on the PyTorch Discussion Board and some people have replied there as well. https://discuss.pytorch.org/t/libtorch-crashes-docker-when-included-in-header-file/201026/6

**Log Screenshot**
![image](https://github.com/pytorch/pytorch/assets/43173458/c11c0d63-e351-4712-a199-075e537afb56)

**Log File**
```
-- The CXX compiler identification is GNU 10.5.0
-- The CUDA compiler identification is NVIDIA 12.2.140
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
Sim Mode: Disabled
-- The C compiler identification is GNU 10.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
RoveComm Build Mode: 1
RoveComm_CPP -- LIBRARY MODE
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Found CUDA: /usr/local/cuda (found suitable exact version "12.2") 
-- Found OpenCV: /usr/local (found version "4.9.0") 
-- Reading /usr/local/lib/cmake/GeographicLib/geographiclib-config.cmake
-- GeographicLib configuration, version 2.3
--   ${GeographicLib_LIBRARIES} set to shared library
-- Looking for sgemm_
-- Looking for sgemm_ - not found
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Found BLAS: /usr/lib/x86_64-linux-gnu/libopenblas.so  
-- Found CUDA: /usr/local/cuda (found suitable version "12.2", minimum required is "12") 
-- Found CUDAToolkit: /usr/local/cuda/include (found suitable version "12.2.140", minimum required is "12") 
-- Found CUDA: /usr/local/cuda (found version "12.2") 
-- Found CUDAToolkit: /usr/local/cuda/include (found version "12.2.140") 
-- Caffe2: CUDA detected: 12.2
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 12.2
-- /usr/local/cuda-12.2/targets/x86_64-linux/lib/libnvrtc.so shorthash is 000ca627
-- Found CUDNN: /usr/local/cuda/lib64/libcudnn.so  
-- USE_CUSPARSELT is set to 0. Compiling without cuSPARSELt support
-- Autodetected CUDA architecture(s):  8.6
-- Added CUDA NVCC flags for: -gencode;arch=compute_86,code=sm_86
-- Found Torch: /usr/lib/libtorch.so  
-- Found TENSORFLOWLITE: /usr/local/lib/libtensorflowlite.so  
-- Found LIBEDGETPU: /usr/local/lib/libedgetpu.so.1.0  
-- Configuring done (1.8s)
-- Generating done (0.0s)
-- Build files have been written to: /workspaces/Autonomy_Software/build
[  2%] Building CXX object external/rovecomm/CMakeFiles/RoveComm_CPP.dir/src/RoveComm/RoveCommGlobals.cpp.o
[  5%] Building CXX object external/rovecomm/CMakeFiles/RoveComm_CPP.dir/src/RoveComm/RoveCommPacket.cpp.o
[  7%] Building CXX object external/rovecomm/CMakeFiles/RoveComm_CPP.dir/src/RoveComm/RoveCommTCP.cpp.o
[ 10%] Building CXX object external/rovecomm/CMakeFiles/RoveComm_CPP.dir/src/RoveComm/RoveCommUDP.cpp.o
[ 13%] Linking CXX static library libRoveComm_CPP.a
[ 13%] Built target RoveComm_CPP
[ 15%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/AutonomyGlobals.cpp.o
[ 18%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/AutonomyLogging.cpp.o
[ 21%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/AutonomyNetworking.cpp.o
[ 23%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/algorithms/controllers/PIDController.cpp.o
[ 26%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/IdentitySoftware.cpp.o
[ 31%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/algorithms/planners/AStar.cpp.o
[ 31%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/algorithms/controllers/StanleyController.cpp.o
[ 34%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/drivers/MultimediaBoard.cpp.o
[ 36%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/drivers/DriveBoard.cpp.o
[ 39%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/drivers/NavigationBoard.cpp.o
[ 42%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/CameraHandler.cpp.o
[ 44%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/RecordingHandler.cpp.o
[ 47%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/ObjectDetectionHandler.cpp.o
[ 50%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/TagDetectionHandler.cpp.o
[ 52%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/StateMachineHandler.cpp.o
[ 55%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/handlers/WaypointHandler.cpp.o
[ 57%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/main.cpp.o
[ 60%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/ApproachingObjectState.cpp.o
[ 63%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/ApproachingMarkerState.cpp.o
[ 65%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/AvoidanceState.cpp.o
[ 68%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/IdleState.cpp.o
[ 71%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/NavigatingState.cpp.o
[ 73%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/ReversingState.cpp.o
[ 76%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/SearchPatternState.cpp.o
[ 78%] Building CXX object CMakeFiles/Autonomy_Software.dir/src/states/StuckState.cpp.o
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
make[2]: *** [CMakeFiles/Autonomy_Software.dir/build.make:272: CMakeFiles/Autonomy_Software.dir/src/handlers/TagDetectionHandler.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
make[2]: *** [CMakeFiles/Autonomy_Software.dir/build.make:314: CMakeFiles/Autonomy_Software.dir/src/states/ApproachingMarkerState.cpp.o] Error 1
```

### Versions

Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 10.5.0-1ubuntu1~22.04) 10.5.0
Clang version: Could not collect
CMake version: version 3.27.9
Libc version: glibc-2.35

Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-27-generic-x86_64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: 12.2.140
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060
Nvidia driver version: 550.54.15
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8
/usr/lib/libcudnn_adv_infer.so.8
/usr/lib/libcudnn_adv_train.so.8
/usr/lib/libcudnn_cnn_infer.so.8
/usr/lib/libcudnn_cnn_train.so.8
/usr/lib/libcudnn_ops_infer.so.8
/usr/lib/libcudnn_ops_train.so.8
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.6
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.7
/usr/local/cuda-12.2/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.7
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

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):                             20
On-line CPU(s) list:                0-19
Vendor ID:                          GenuineIntel
Model name:                         12th Gen Intel(R) Core(TM) i7-12700K
CPU family:                         6
Model:                              151
Thread(s) per core:                 2
Core(s) per socket:                 12
Socket(s):                          1
Stepping:                           2
CPU max MHz:                        5000.0000
CPU min MHz:                        800.0000
BogoMIPS:                           7219.20
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 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 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:                          512 KiB (12 instances)
L1i cache:                          512 KiB (12 instances)
L2 cache:                           12 MiB (9 instances)
L3 cache:                           25 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-19
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 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
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[conda] Could not collect