Check tensors are defined before attempting to access their impl (#106787)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/106787
Approved by: https://github.com/albanD
This commit is contained in:
David Watson
2023-08-24 11:38:32 +00:00
committed by PyTorch MergeBot
parent c91d2f5bf6
commit 256fed02e9

View File

@ -340,14 +340,14 @@ bool isResurrectable(THPVariable* self) {
return false;
}
auto const& tensor = THPVariable_Unpack(self);
if (!tensor.defined() || tensor.use_count() <= 1) {
return false;
}
// Check if this is hermetic. If it is, no resurrection.
if (tensor.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
getPyInterpreter()) != c10::make_optional((PyObject*)self)) {
return false;
}
if (!tensor.defined() || tensor.use_count() <= 1) {
return false;
}
return true;
}