From: Alban Desmaison Date: Fri, 10 Sep 2021 20:07:37 +0000 (-0700) Subject: Move THPVariable_NewWithVar around (#64550) X-Git-Tag: accepted/tizen/8.0/unified/20231005.095509~298 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=89f94fc15f672725f5ce2cdc109c8b3310e58de7;p=platform%2Fupstream%2Fpytorch.git Move THPVariable_NewWithVar around (#64550) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/64550 Just moves a function around to make the next PR easier to read. Test Plan: Imported from OSS Reviewed By: ngimel Differential Revision: D30867570 Pulled By: albanD fbshipit-source-id: 99ae925568ed29ca7fdea059762c21d430d4a204 --- diff --git a/torch/csrc/autograd/python_variable.cpp b/torch/csrc/autograd/python_variable.cpp index 5f115ee..769f9e0 100644 --- a/torch/csrc/autograd/python_variable.cpp +++ b/torch/csrc/autograd/python_variable.cpp @@ -117,6 +117,11 @@ PyObject *THPVariableClass = nullptr; PyObject *ParameterClass = nullptr; +static PyObject* THPVariable_NewWithVar( + PyTypeObject* type, + Variable _var, + c10::impl::PyInterpreterStatus status); + // clang-tidy gets confused by static const static const char* VOLATILE_WARNING = "volatile was removed and now has no effect. Use " @@ -131,32 +136,6 @@ static bool check_has_torch_dispatch(PyObject *obj) { ); } -// Creates a new Python object for a Variable. The status parameter -// specifies what the interpreter tag status on the object is; for -// example, if you ran check_pyobj, the return optional of this object -// tells you if the tensor was already tagged or not so you can pass -// TAGGED_BY_US or MAYBE_UNINITIALIZED; in other cases, you know where -// var came from and can directly assert that it's DEFINITELY_UNINITIALIZED. -// It's ALWAYS safe (albeit slower) to call this with MAYBE_UNINITIALIZED. -static PyObject* THPVariable_NewWithVar( - PyTypeObject* type, - Variable _var, - c10::impl::PyInterpreterStatus status) { - PyObject* obj = type->tp_alloc(type, 0); - if (obj) { - auto v = (THPVariable*) obj; - // TODO: named constructor to avoid default initialization - new (&v->cdata) MaybeOwned(); - v->cdata = MaybeOwned::owned(std::move(_var)); - const auto& var = THPVariable_Unpack(v); - var.unsafeGetTensorImpl()->init_pyobj(self_interpreter.get(), obj, status); - if (check_has_torch_dispatch(obj)) { - var.unsafeGetTensorImpl()->set_python_dispatch(true); - } - } - return obj; -} - // TODO: Make this take Variable by const reference PyObject * THPVariable_Wrap(at::TensorBase var) { @@ -1262,6 +1241,32 @@ void THPVariable_subclass_dealloc(PyObject* self) { Py_DECREF(type); } +// Creates a new Python object for a Variable. The status parameter +// specifies what the interpreter tag status on the object is; for +// example, if you ran check_pyobj, the return optional of this object +// tells you if the tensor was already tagged or not so you can pass +// TAGGED_BY_US or MAYBE_UNINITIALIZED; in other cases, you know where +// var came from and can directly assert that it's DEFINITELY_UNINITIALIZED. +// It's ALWAYS safe (albeit slower) to call this with MAYBE_UNINITIALIZED. +static PyObject* THPVariable_NewWithVar( + PyTypeObject* type, + Variable _var, + c10::impl::PyInterpreterStatus status) { + PyObject* obj = type->tp_alloc(type, 0); + if (obj) { + auto v = (THPVariable*) obj; + // TODO: named constructor to avoid default initialization + new (&v->cdata) MaybeOwned(); + v->cdata = MaybeOwned::owned(std::move(_var)); + const auto& var = THPVariable_Unpack(v); + var.unsafeGetTensorImpl()->init_pyobj(self_interpreter.get(), obj, status); + if (check_has_torch_dispatch(obj)) { + var.unsafeGetTensorImpl()->set_python_dispatch(true); + } + } + return obj; +} + /// NOTE [ PyObject Traversal ] /// /// PyObjects that are wrapping c++ objects can lead to non-trivial traverse logic