Repository: pytorch
Issue: SEGFAULT on "import torch"
Labels: needs reproduction,module: crash,triaged

### 🐛 Describe the bug

Unable to import torch in python. I've tried version 1.8.2 and 1.10.1 using python 3.8.6, 3.9.7 and 3.9.9.

```bash
$ python -c "import torch"
Segmentation fault (core dumped)
```

Ubuntu 20.04, cuda 11.3, driver 465.19.01

With GDB I get:

```
$ gdb python
(gdb) r -c "import torch"
Starting program: /home/username/.local/bin/python -c "import torch"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
type_qualname (type=0x555558790100, context=0x0) at Objects/typeobject.c:479
479     Objects/typeobject.c: No such file or directory.

(gdb) bt
#0  type_qualname (type=0x555558790100, context=0x0) at Objects/typeobject.c:479
#1  0x000055555562bae2 in type_getattro (type=0x555558790100, name=0x7ffff6ff02b0) at Objects/typeobject.c:3401
#2  0x000055555560477b in PyObject_GetAttrString (name=<optimized out>, v=0x555558790100) at Objects/object.c:795
#3  PyObject_GetAttrString (v=0x555558790100, name=<optimized out>) at Objects/object.c:786
#4  0x00007ffff518e39f in pybind11::cpp_function::initialize_generic(std::unique_ptr<pybind11::detail::function_record, pybind11::cpp_function::InitializingFunctionRecordDeleter>&&, char const*, std::type_info const* const*, unsigned long)
    () from /home/username/.local/share/libtorch/lib/libtorch_python.so
#5  0x00007ffff5c402ef in pybind11::enum_<onnx_torch::TensorProto_DataType>::enum_<>(pybind11::handle const&, char const*) () from /home/username/.local/share/libtorch/lib/libtorch_python.so
#6  0x00007ffff5c3db75 in torch::onnx::initONNXBindings(_object*) ()
   from /home/username/.local/share/libtorch/lib/libtorch_python.so
#7  0x00007ffff5844b94 in initModule () from /home/username/.local/share/libtorch/lib/libtorch_python.so
#8  0x00005555556a1239 in _PyImport_LoadDynamicModuleWithSpec (spec=spec@entry=0x7ffff74272e0, fp=fp@entry=0x0)
    at ./Python/importdl.c:167
#9  0x000055555569ca5d in _imp_create_dynamic_impl (module=<optimized out>, file=<optimized out>, spec=0x7ffff74272e0)
    at Python/import.c:2302
#10 _imp_create_dynamic (module=<optimized out>, args=<optimized out>, nargs=<optimized out>)
    at Python/clinic/import.c.h:330
#11 0x000055555577c351 in cfunction_vectorcall_FASTCALL (func=0x7ffff78d2b80, args=0x7ffff7427ce8,
    nargsf=<optimized out>, kwnames=<optimized out>) at Objects/methodobject.c:430
#12 0x00005555555c887c in PyVectorcall_Call (callable=0x7ffff78d2b80, tuple=<optimized out>, kwargs=<optimized out>)
    at Objects/call.c:231
#13 0x00005555555b2e02 in do_call_core (kwdict=0x7ffff70314c0, callargs=0x7ffff7427cd0, func=0x7ffff78d2b80,
    tstate=<optimized out>) at Python/ceval.c:5123
#14 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>)
    at Python/ceval.c:3580
#15 0x000055555567b7ba in _PyEval_EvalFrame (throwflag=0, f=0x7ffff7303ac0, tstate=0x555555928d10)
    at ./Include/internal/pycore_ceval.h:40
#16 _PyEval_EvalCode (tstate=tstate@entry=0x555555928d10, _co=<optimized out>, globals=<optimized out>,
--Type <RET> for more, q to quit, c to continue without paging--
    try=0x0, args=<optimized out>, argcount=2, kwnames=0x0, kwargs=0x7ffff77533a0, kwcount=0, kwstep=1, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x7ffff78bfa80, qualname=0x7ffff78bfa80) at Python/ceval.c:4327
#17 0x00005555555c8e37 in _PyFunction_Vectorcall (func=<optimized out>, stack=<optimized out>, nargsf=<optimized out>, kwnames=<optimized out>) at Objects/call.c:396
#18 0x00005555555b7f5e in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7753390, callable=0x7ffff78d63a0, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#19 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7753390, callable=0x7ffff78d63a0) at ./Include/cpython/abstract.h:127
#20 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#21 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3487
#22 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x7ffff7753200, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#23 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#24 0x00005555555b8041 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x555555acbd90, callable=0x7ffff7898670, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#25 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x555555acbd90, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#26 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#27 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3504
#28 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x555555acbc10, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#29 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=1, globals=<optimized out>) at Objects/call.c:330
#30 0x00005555555b6e07 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x555555a9bae0, callable=0x7ffff78d6d30, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#31 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x555555a9bae0, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#32 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#33 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3518
#34 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x555555a9b960, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#35 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=1, globals=<optimized out>) at Objects/call.c:330
#36 0x00005555555b6e07 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x555555b06ea8, callable=0x7ffff78d6f70, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#37 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x555555b06ea8, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#38 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#39 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3518
#40 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x555555b06cf0, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#41 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#42 0x00005555555b6e07 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7253d18, callable=0x7ffff78da1f0, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#43 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7253d18, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#44 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#45 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3518
#46 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x7ffff7253b80, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#47 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#48 0x00005555555c839c in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=2, args=0x7fffffffcd70, callable=0x7ffff78da280, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#49 object_vacall (tstate=tstate@entry=0x555555928d10, base=base@entry=0x0, callable=0x7ffff78da280, vargs=vargs@entry=0x7fffffffce00) at Objects/call.c:792
#50 0x00005555555ca9c8 in _PyObject_CallMethodIdObjArgs (obj=0x0, name=name@entry=0x5555558e0140 <PyId__find_and_load.17719>) at Objects/call.c:883
#51 0x00005555556a00aa in import_find_and_load (abs_name=0x7ffff779cc70, tstate=0x555555928d10) at Python/import.c:1776
#52 PyImport_ImportModuleLevelObject (name=name@entry=0x7ffff779cc70, globals=<optimized out>, locals=<optimized out>, fromlist=fromlist@entry=0x7ffff7790dc0, level=0) at Python/import.c:1877
#53 0x00005555555b75a5 in import_name (level=0x7ffff78f0910, fromlist=0x7ffff7790dc0, name=0x7ffff779cc70, f=0x555555979db0, tstate=<optimized out>) at Python/ceval.c:5196
#54 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3097
#55 0x000055555567b7ba in _PyEval_EvalFrame (throwflag=0, f=0x555555979db0, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#56 _PyEval_EvalCode (tstate=0x555555928d10, _co=_co@entry=0x7ffff7798be0, globals=globals@entry=0x7ffff77f3e80, locals=locals@entry=0x7ffff77f3e80, args=args@entry=0x0, argcount=argcount@entry=0, kwnames=0x0, kwargs=0x0, kwcount=0, kwstep=2, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:4327
#57 0x000055555567baea in _PyEval_EvalCodeWithName (qualname=0x0, name=0x0, closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwstep=2, kwcount=0, kwargs=0x0, kwnames=0x0, argcount=0, args=0x0, locals=0x7ffff77f3e80, globals=0x7ffff77f3e80, _co=0x7ffff7798be0) at Python/ceval.c:4359
#58 PyEval_EvalCodeEx (closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwcount=0, kws=0x0, argcount=0, args=0x0, locals=0x7ffff77f3e80, globals=0x7ffff77f3e80, _co=0x7ffff7798be0) at Python/ceval.c:4375
#59 PyEval_EvalCode (co=co@entry=0x7ffff7798be0, globals=globals@entry=0x7ffff77f3e80, locals=locals@entry=0x7ffff77f3e80) at Python/ceval.c:826
#60 0x00005555557adf81 in builtin_exec_impl (module=<optimized out>, locals=0x7ffff77f3e80, globals=0x7ffff77f3e80, source=0x7ffff7798be0) at Python/bltinmodule.c:1026
#61 builtin_exec (module=<optimized out>, args=0x7ffff779e318, nargs=<optimized out>) at Python/clinic/bltinmodule.c.h:396
#62 0x000055555577c351 in cfunction_vectorcall_FASTCALL (func=0x7ffff78bad60, args=0x7ffff779e318, nargsf=<optimized out>, kwnames=<optimized out>) at Objects/methodobject.c:430
#63 0x00005555555c887c in PyVectorcall_Call (callable=0x7ffff78bad60, tuple=<optimized out>, kwargs=<optimized out>) at Objects/call.c:231
#64 0x00005555555b2e02 in do_call_core (kwdict=0x7ffff778c700, callargs=0x7ffff779e300, func=0x7ffff78bad60, tstate=<optimized out>) at Python/ceval.c:5123
#65 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3580
#66 0x000055555567b7ba in _PyEval_EvalFrame (throwflag=0, f=0x7ffff7788900, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#67 _PyEval_EvalCode (tstate=tstate@entry=0x555555928d10, _co=<optimized out>, globals=<optimized out>, locals=locals@entry=0x0, args=<optimized out>, argcount=3, kwnames=0x0, kwargs=0x7ffff78588e8, kwcount=0, kwstep=1, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x7ffff78bfa80, qualname=0x7ffff78bfa80) at Python/ceval.c:4327
#68 0x00005555555c8e37 in _PyFunction_Vectorcall (func=<optimized out>, stack=<optimized out>, nargsf=<optimized out>, kwnames=<optimized out>) at Objects/call.c:396
#69 0x00005555555b7f5e in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff78588d0, callable=0x7ffff78d63a0, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#70 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff78588d0, callable=0x7ffff78d63a0) at ./Include/cpython/abstract.h:127
#71 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#72 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3487
#73 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x7ffff7858740, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#74 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#75 0x00005555555b8041 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff785a790, callable=0x7ffff78964c0, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#76 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff785a790, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#77 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#78 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3504
--Type <RET> for more, q to quit, c to continue without paging--
#79 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x7ffff785a610, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#80 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=1, globals=<optimized out>) at Objects/call.c:330
#81 0x00005555555b6e07 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x55555598c1d8, callable=0x7ffff78d6f70, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#82 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x55555598c1d8, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#83 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#84 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3518
#85 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x55555598c020, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#86 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#87 0x00005555555b6e07 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff778f958, callable=0x7ffff78da1f0, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#88 PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff778f958, callable=<optimized out>) at ./Include/cpython/abstract.h:127
#89 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=<optimized out>) at Python/ceval.c:5075
#90 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3518
#91 0x00005555555afdab in _PyEval_EvalFrame (throwflag=0, f=0x7ffff778f7c0, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#92 function_code_fastcall (tstate=0x555555928d10, co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at Objects/call.c:330
#93 0x00005555555c839c in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=2, args=0x7fffffffdb30, callable=0x7ffff78da280, tstate=0x555555928d10) at ./Include/cpython/abstract.h:118
#94 object_vacall (tstate=tstate@entry=0x555555928d10, base=base@entry=0x0, callable=0x7ffff78da280, vargs=vargs@entry=0x7fffffffdbc0) at Objects/call.c:792
#95 0x00005555555ca9c8 in _PyObject_CallMethodIdObjArgs (obj=0x0, name=name@entry=0x5555558e0140 <PyId__find_and_load.17719>) at Objects/call.c:883
#96 0x00005555556a00aa in import_find_and_load (abs_name=0x7ffff7860470, tstate=0x555555928d10) at Python/import.c:1776
#97 PyImport_ImportModuleLevelObject (name=name@entry=0x7ffff7860470, globals=<optimized out>, locals=<optimized out>, fromlist=fromlist@entry=0x5555558d8200 <_Py_NoneStruct>, level=0) at Python/import.c:1877
#98 0x00005555555b75a5 in import_name (level=0x7ffff78f0910, fromlist=0x5555558d8200 <_Py_NoneStruct>, name=0x7ffff7860470, f=0x7ffff782d5e0, tstate=<optimized out>) at Python/ceval.c:5196
#99 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:3097
#100 0x000055555567b7ba in _PyEval_EvalFrame (throwflag=0, f=0x7ffff782d5e0, tstate=0x555555928d10) at ./Include/internal/pycore_ceval.h:40
#101 _PyEval_EvalCode (tstate=0x555555928d10, _co=_co@entry=0x7ffff7864920, globals=globals@entry=0x7ffff7860280, locals=locals@entry=0x7ffff7860280, args=args@entry=0x0, argcount=argcount@entry=0, kwnames=0x0, kwargs=0x0, kwcount=0, kwstep=2, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:4327
#102 0x000055555567baea in _PyEval_EvalCodeWithName (qualname=0x0, name=0x0, closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwstep=2, kwcount=0, kwargs=0x0, kwnames=0x0, argcount=0, args=0x0, locals=0x7ffff7860280, globals=0x7ffff7860280, _co=0x7ffff7864920) at Python/ceval.c:4359
#103 PyEval_EvalCodeEx (closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwcount=0, kws=0x0, argcount=0, args=0x0, locals=0x7ffff7860280, globals=0x7ffff7860280, _co=0x7ffff7864920) at Python/ceval.c:4375
#104 PyEval_EvalCode (co=co@entry=0x7ffff7864920, globals=globals@entry=0x7ffff7860280, locals=locals@entry=0x7ffff7860280) at Python/ceval.c:826
#105 0x00005555556bc8b7 in run_eval_code_obj (locals=0x7ffff7860280, globals=0x7ffff7860280, co=0x7ffff7864920, tstate=0x555555928d10) at Python/pythonrun.c:1221
#106 run_mod (mod=mod@entry=0x555555967858, filename=filename@entry=0x7ffff77ef8b0, globals=globals@entry=0x7ffff7860280, locals=locals@entry=0x7ffff7860280, flags=flags@entry=0x7fffffffe028, arena=arena@entry=0x7ffff78ab910) at Python/pythonrun.c:1242
#107 0x00005555556bd970 in PyRun_StringFlags (str=<optimized out>, start=257, globals=0x7ffff7860280, locals=0x7ffff7860280, flags=0x7fffffffe028) at Python/pythonrun.c:1108
#108 0x00005555556bd9ed in PyRun_SimpleStringFlags (command=0x7ffff78cec20 "import torch\n", flags=flags@entry=0x7fffffffe028) at Python/pythonrun.c:497
#109 0x00005555555bab10 in pymain_run_command (cf=0x7fffffffe028, command=<optimized out>) at Modules/main.c:246
#110 pymain_run_python (exitcode=exitcode@entry=0x7fffffffe160) at Modules/main.c:589
#111 0x00005555555bb6cf in Py_RunMain () at Modules/main.c:677
#112 pymain_main (args=0x7fffffffe120) at Modules/main.c:707
#113 Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:731
#114 0x00007ffff7c6b0b3 in __libc_start_main (main=0x5555555afd40 <main>, argc=3, argv=0x7fffffffe288, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe278) at ../csu/libc-start.c:308
#115 0x00005555555ba2de in _start () at ./Include/object.h:430
```

### Versions

Yields segfault