Repository: pytorch
Issue: Segmentation fault about torch::nn::Conv2d in `libtorch2.0.1+cuda11.8`
Labels: needs reproduction,module: crash,module: cpp,triaged

### 🐛 Describe the bug

I got a segmentation fault when executing `Conv2d` using `Libtorch2.0.1+cuda11.8` version, but the version `Libtorch2.0.0+cuda11.8` is work. So, I think this may be a bug in `Libtorch2.0.1+cuda11.8`, or may be I have some errors when using Libtorch. The follow is the `C++` test code and `CMakeList.txt` file.

- C++ code:

```cpp
#include<torch/torch.h>
#include<iostream>
#include <opencv2/opencv.hpp>

struct TestCNN : torch::nn::Module {
    TestCNN() {
        conv1a = register_module("conv1a", torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 64, 3).stride(1).padding(1)));
        conv1b = register_module("conv1b", torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 128, 3).stride(1).padding(1)));
        out = register_module("out", torch::nn::Conv2d(torch::nn::Conv2dOptions(128, 1, 3).stride(1).padding(1)));
    }

    torch::Tensor forward(torch::Tensor x) {
        x = torch::relu(conv1a->forward(x));
        x = torch::relu(conv1b->forward(x));
        x = out->forward(x);
        return x;
    }

    torch::nn::Conv2d conv1a{nullptr};
    torch::nn::Conv2d conv1b{nullptr};
    torch::nn::Conv2d out{nullptr};
};

int main(){
    auto device_type = torch::kCUDA;
    auto device = torch::Device(device_type);

    std::string img_path  = "/path/to/image/";
    cv::Mat img = cv::imread(img_path, cv::IMREAD_UNCHANGED);
    cv::Mat gray;
    cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);

    torch::Tensor inp = torch::from_blob(gray.data, {gray.rows, gray.cols}, torch::kByte);
    inp = inp.unsqueeze(0).unsqueeze(1);
    inp = inp.to(torch::kFloat32);
    inp = inp.div(255.0);

    std::shared_ptr<TestCNN> model = std::make_shared<TestCNN>();
    model->eval();
    model->to(device);

    auto out = model->forward(inp.to(device));

    return 0;
}
```

- CMakeLists.txt:

```cmake
cmake_minimum_required(VERSION 3.10)
project(demo)

set(CMAKE_BUILD_TYPE Debug)

set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/path/to/libtorch2.0.1+cuda11.8/")
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(demo demo.cpp)
target_link_libraries(demo ${TORCH_LIBRARIES} ${OpenCV_LIBS} -pthread)
set_property(TARGET demo PROPERTY CXX_STANDARD 14)
```

When executing `x = torch::relu(conv1a->forward(x));`, I got the following output without any other prompt information.
```shell
Signal: SIGSEGV (Segmentation fault)
```

When I change the `/path/to/libtorch2.0.1+cuda11.8/` into `libtorch2.0.0+cuda11.8` in CMakeLists.txt, it can work well. And more interesting, if I generate a random Tensor instead of reading a image, the same codes also can work well.

### Versions

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.27.7
Libc version: glibc-2.31

Python version: 3.10.11 (main, May 16 2023, 00:28:57) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-73-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4090
Nvidia driver version: 525.116.04
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/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
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              1
Core(s) per socket:              16
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           183
Model name:                      13th Gen Intel(R) Core(TM) i7-13700KF
Stepping:                        1
CPU MHz:                         3400.000
CPU max MHz:                     5400.0000
CPU min MHz:                     800.0000
BogoMIPS:                        6835.20
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       256 KiB
L2 cache:                        16 MiB
NUMA node0 CPU(s):               0-23
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 and seccomp
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
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 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 epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi 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 umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342  
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.6           py310h1128e8f_1  
[conda] mkl_random                1.2.2           py310h1128e8f_1  
[conda] numpy                     1.24.3          py310h5f9d8c6_1  
[conda] numpy-base                1.24.3          py310hb5e798b_1  
[conda] pytorch                   2.0.1           py3.10_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.0.2               py310_cu118    pytorch
[conda] torchtriton               2.0.0                     py310    pytorch
[conda] torchvision               0.15.2              py310_cu118    pytorch

cc @jbschlosser