Repository: pytorch
Issue: Segfault in ~PyFunctionPreHook
Labels: needs reproduction,module: crash,module: autograd,triaged

### 🐛 Describe the bug

Encountered a segfault when using pytorch on ubuntu 22.04, with python 3.10. It does not seem to happen on python3.9

GDB Backtrace
```
#0  0x00007f1a3a32ee2b in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00005572445cc9f0 in ?? ()
#2  0x0000557244748706 in PyEval_AcquireThread ()
#3  0x00007f19beecc26e in pybind11::gil_scoped_acquire::gil_scoped_acquire() () from /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_python.so
#4  0x00007f19bf1a40cb in torch::autograd::PyFunctionPreHook::~PyFunctionPreHook() () from /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_python.so
#5  0x00007f19aaf71f62 in torch::autograd::AutogradMeta::~AutogradMeta() () from /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_cpu.so
#6  0x00007f19bfb985c0 in c10::TensorImpl::release_resources() () from /usr/local/lib/python3.10/dist-packages/torch/lib/libc10.so
#7  0x00007f19beec8229 in c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::reset_() () from /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_python.so
#8  0x00007f19bf1b3078 in THPVariable_clear(THPVariable*) () from /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_python.so
#9  0x00005572445dd265 in ?? ()
#10 0x00005572446e2b70 in ?? ()
#11 0x00005572445dafaa in PyType_GenericAlloc ()
#12 0x00005572445df816 in ?? ()
#13 0x0000557244603f19 in ?? ()
#14 0x000055724461bc3b in PyObject_Call ()
#15 0x000055724461508c in _PyErr_NormalizeException ()
#16 0x00005572445fca14 in _PyEval_EvalFrameDefault ()
#17 0x000055724460d95c in _PyFunction_Vectorcall ()
```

Python backtraces
```
Fatal Python error: Segmentation fault
Stack (most recent call first):
  File "<frozen importlib._bootstrap>", line 188 in _get_module_lock
  File "<frozen importlib._bootstrap>", line 170 in __enter__
  File "<frozen importlib._bootstrap>", line 1024 in _find_and_load
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/yoso/modeling_yoso.py", line 23 in <module>
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 883 in exec_module
  File "<frozen importlib._bootstrap>", line 688 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1006 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1027 in _find_and_load
  File "<frozen importlib._bootstrap>", line 1050 in _gcd_import
  File "/usr/lib/python3.10/importlib/__init__.py", line 126 in import_module
  File "/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py", line 857 in _get_module
  File "/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py", line 847 in __getattr__
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/auto/auto_factory.py", line 534 in getattribute_from_module
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/auto/auto_factory.py", line 572 in _load_attr_from_module
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/auto/auto_factory.py", line 603 in <listcomp>
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/auto/auto_factory.py", line 600 in items
  File "/usr/local/lib/python3.10/dist-packages/transformers/pipelines/base.py", line 873 in check_model_type
  File "/usr/local/lib/python3.10/dist-packages/transformers/pipelines/text_classification.py", line 69 in __init__
  File "/usr/local/lib/python3.10/dist-packages/transformers/pipelines/__init__.py", line 664 in pipeline
```
```
Fatal Python error: Segmentation fault
Stack (most recent call first):
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/bert/tokenization_bert.py", line 199 in <listcomp>
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/bert/tokenization_bert.py", line 199 in __init__
  File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 1915 in _from_pretrained
  File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 1806 in _from_pretrained
  File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 1780 in from_pretrained
  File "/usr/local/lib/python3.10/dist-packages/transformers/models/auto/tokenization_auto.py", line 546 in from_pretrained
  File "/usr/local/lib/python3.10/dist-packages/transformers/pipelines/__init__.py", line 598 in pipeline
```

I tried reducing the cause to a simple example but was unable to get it to occur in a simple example

### Versions

PyTorch version: 1.11.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04 LTS (x86_64)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.10.4 (main, Apr  2 2022, 09:04:19) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.32-gentoo-r1-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect


cc @ezyang @gchanan @zou3519 @albanD @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7