mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Inserted check for the return of PyObject_IsInstance to capture the case in which it raises an exception and return -1. When this happen THPVariable_Check now throws a python_error to signal the exception. Fixes https://github.com/pytorch/pytorch/issues/65084 Pull Request resolved: https://github.com/pytorch/pytorch/pull/67588 Reviewed By: mruberry Differential Revision: D32064776 Pulled By: albanD fbshipit-source-id: 895c7682e0991ca257e27f9638a7462d83707320
65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <torch/csrc/python_headers.h>
|
|
#include <memory>
|
|
#include <ATen/ATen.h>
|
|
|
|
#include <torch/csrc/autograd/variable.h>
|
|
#include <torch/csrc/THP_export.h>
|
|
#include <torch/csrc/Exceptions.h>
|
|
|
|
// Python object that backs torch.autograd.Variable
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
|
struct THPVariable {
|
|
PyObject_HEAD;
|
|
// Payload
|
|
c10::MaybeOwned<at::Tensor> cdata;
|
|
// Hooks to be run on backwards pass (corresponds to Python attr
|
|
// '_backwards_hooks', set by 'register_hook')
|
|
PyObject* backward_hooks = nullptr;
|
|
};
|
|
|
|
TORCH_API void registerPythonTensorClass(const std::string& device, PyObject* python_tensor_class);
|
|
|
|
THP_API PyObject *THPVariableClass;
|
|
THP_API PyObject *ParameterClass;
|
|
|
|
bool THPVariable_initModule(PyObject *module);
|
|
THP_API PyObject * THPVariable_Wrap(at::TensorBase var);
|
|
|
|
static inline bool THPVariable_CheckTypeExact(PyTypeObject* tp) {
|
|
// Check that a python object is a `Tensor`, but not a `Tensor` subclass.
|
|
// (A subclass could have different semantics.) The one exception is
|
|
// Parameter, which is used for Python bookkeeping but is equivalent to
|
|
// Tensor as far as C++ is concerned.
|
|
return (
|
|
tp == (PyTypeObject*)THPVariableClass ||
|
|
tp == (PyTypeObject*)ParameterClass
|
|
);
|
|
}
|
|
|
|
static inline bool THPVariable_CheckExact(PyObject *obj) {
|
|
return THPVariable_CheckTypeExact(Py_TYPE(obj));
|
|
}
|
|
|
|
inline bool THPVariable_Check(PyObject *obj)
|
|
{
|
|
if (!THPVariableClass)
|
|
return false;
|
|
|
|
const auto result = PyObject_IsInstance(obj, THPVariableClass);
|
|
if (result == -1)
|
|
throw python_error();
|
|
return result;
|
|
}
|
|
|
|
inline const at::Tensor& THPVariable_Unpack(THPVariable* var) {
|
|
return *var->cdata;
|
|
}
|
|
|
|
inline const at::Tensor& THPVariable_Unpack(PyObject* obj) {
|
|
return THPVariable_Unpack(reinterpret_cast<THPVariable*>(obj));
|
|
}
|
|
|
|
THP_API c10::impl::PyInterpreter* getPyInterpreter();
|