Repository: pytorch
Issue: Segfault with fake tensor
Labels: triaged,module: fakeTensor

### 🐛 Describe the bug

This torchdynamo branch https://github.com/ezyang/torchdynamo/tree/fake-segfault triggers a segfault in mutation analysis with `python tests/test_optimizations.py -k test_has_mutation`. I'm pretty sure it's fake tensor related. I have a workaround so fixing the segfault isn't urgent, but it should be looked into.

gdb backtrace: 

```
#0  0x00007fffb0d5d2b4 in c10::SafePyObject::ptr(c10::impl::PyInterpreter const*) const ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libc10.so
#1  0x00007fffc714afc9 in torch::handle_torch_function_no_python_arg_parser(c10::ArrayRef<pybind11::handle>, _object*, _object*, char const*, _object*, char const*, torch::TorchFunctionName) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_python.so
#2  0x00007fffc6e0c763 in (anonymous namespace)::concrete_dispatch_fn(c10::impl::PyInterpreter const*, c10::OperatorHandle const&, std::vector<c10::IValue, std::allocator<c10::IValue> >*) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_python.so
#3  0x00007fffbd01ef6e in void c10::KernelFunction::make_boxed_function<&(anonymous namespace)::pythonFallback>(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#4  0x00007fffbd94696d in c10::impl::BoxedKernelWrapper<at::Tensor (at::Tensor const&), void>::call(void (*)(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*), c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#5  0x00007fffbdc49d0c in at::_ops::t::redispatch(c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#6  0x00007fffbf8ee5a8 in torch::ADInplaceOrView::(anonymous namespace)::t(c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#7  0x00007fffbf8ee7a8 in c10::impl::wrap_kernel_functor_unboxed_<c10::impl::detail::WrapFunctionIntoFunctor_<c10::CompileTimeFunctionPointer<at::Tensor (c10::DispatchKeySet, at::Tensor const&), &torch::ADInplaceOrView::(anonymous namespace)::t>, at::Tensor, c10::guts::typelist::typelist<c10::DispatchKeySet, at::Tensor const&> >, at::Tensor (c10::DispatchKeySet, at::Tensor const&)>::call(c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#8  0x00007fffbdc49c61 in at::_ops::t::redispatch(c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#9  0x00007fffbf3f1b99 in torch::autograd::VariableType::(anonymous namespace)::t(c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#10 0x00007fffbf3f2108 in c10::impl::make_boxed_from_unboxed_functor<c10::impl::detail::WrapFunctionIntoFunctor_<c10::CompileTimeFunctionPointer<at::Tensor (c10::DispatchKeySet, at::Tensor const&), &torch::autograd::VariableType::(anonymous namespace)::t>, at::Tensor, c10::guts::typelist::typelist<c10::DispatchKeySet, at::Tensor const&> >, false>::call(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#11 0x00007fffbd01edfe in void c10::KernelFunction::make_boxed_function<&(anonymous namespace)::pythonTLSSnapshotFallback>(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#12 0x00007fffbd94696d in c10::impl::BoxedKernelWrapper<at::Tensor (at::Tensor const&), void>::call(void (*)(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*), c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#13 0x00007fffbdc8755f in at::_ops::t::call(at::Tensor const&) () from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#14 0x00007fffbd25b780 in at::native::linear(at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#15 0x00007fffbe182943 in c10::impl::make_boxed_from_unboxed_functor<c10::impl::detail::WrapFunctionIntoFunctor_<c10::CompileTimeFunctionPointer<at::Tensor (at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&), &at::(anonymous namespace)::(anonymous namespace)::wrapper__linear>, at::Tensor, c10::guts::typelist::typelist<at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&> >, false>::call(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*) () from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#16 0x00007fffbd01edfe in void c10::KernelFunction::make_boxed_function<&(anonymous namespace)::pythonTLSSnapshotFallback>(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*) ()
   from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#17 0x00007fffbd94f25f in c10::impl::BoxedKernelWrapper<at::Tensor (at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&), void>::call(void (*)(c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, std::vector<c10::IValue, std::allocator<c10::IValue> >*), c10::OperatorKernel*, c10::OperatorHandle const&, c10::DispatchKeySet, at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&) () from /raid/ezyang/pytorch-scratch2/torch/lib/libtorch_cpu.so
#18 0x00007fffbd8f37ae in at::_ops::linear::call(at::Tensor const&, at::Tensor const&, c10::optional<at::Tensor> const&) ()
```

### Versions

master