File: | build/../torch/csrc/autograd/python_hook.cpp |
Warning: | line 105, column 22 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | #include <torch/csrc/autograd/python_hook.h> | |||
2 | ||||
3 | #include <c10/util/irange.h> | |||
4 | #include <pybind11/pybind11.h> | |||
5 | #include <torch/csrc/THP.h> | |||
6 | #include <torch/csrc/autograd/python_variable.h> | |||
7 | #include <torch/csrc/utils/object_ptr.h> | |||
8 | #include <torch/csrc/utils/python_strings.h> | |||
9 | #include <torch/csrc/Exceptions.h> | |||
10 | ||||
11 | #include <sstream> | |||
12 | ||||
13 | using torch::autograd::variable_list; | |||
14 | using torch::autograd::Variable; | |||
15 | ||||
16 | static PyObject* wrap_variables(const variable_list& c_variables); | |||
17 | static variable_list unwrap_variables(PyObject* py_variables); | |||
18 | static std::string hook_name(PyObject* hook); | |||
19 | static void check_result(PyObject* original, PyObject* result, PyObject* hook); | |||
20 | static void check_single_result(PyObject* original, PyObject* result, PyObject* hook); | |||
21 | ||||
22 | ||||
23 | namespace torch { namespace autograd { | |||
24 | ||||
25 | PyFunctionPreHook::PyFunctionPreHook(PyObject* dict, int value_idx) | |||
26 | : dict(dict) | |||
27 | , value_idx(value_idx) | |||
28 | { | |||
29 | Py_INCREF(dict)_Py_INCREF(((PyObject*)(dict))); | |||
30 | } | |||
31 | ||||
32 | PyFunctionPreHook::~PyFunctionPreHook() { | |||
33 | // If python is already dead, leak the wrapped python objects | |||
34 | if (Py_IsInitialized()) { | |||
35 | pybind11::gil_scoped_acquire gil; | |||
36 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
37 | } | |||
38 | } | |||
39 | ||||
40 | auto PyFunctionPreHook::operator()(const variable_list& values) -> variable_list | |||
41 | { | |||
42 | pybind11::gil_scoped_acquire gil; | |||
43 | ||||
44 | THPObjectPtr value(THPVariable_Wrap(values.at(value_idx))); | |||
45 | if (!value) throw python_error(); | |||
46 | ||||
47 | // NOLINTNEXTLINE(cppcoreguidelines-init-variables) | |||
48 | PyObject *key, *hook; | |||
49 | Py_ssize_t pos = 0; | |||
50 | while (PyDict_Next(dict, &pos, &key, &hook)) { | |||
51 | THPObjectPtr res(PyObject_CallFunctionObjArgs(hook, value.get(), nullptr)); | |||
52 | if (!res) throw python_error(); | |||
53 | if (res == Py_None(&_Py_NoneStruct)) continue; | |||
54 | check_single_result(value.get(), res.get(), hook); | |||
55 | value = std::move(res); | |||
56 | } | |||
57 | ||||
58 | variable_list results(values); | |||
59 | if (value != Py_None(&_Py_NoneStruct)) results[value_idx] = THPVariable_Unpack(value.get()); | |||
60 | return results; | |||
61 | } | |||
62 | ||||
63 | PyFunctionPostHook::PyFunctionPostHook(PyObject* dict) : dict(dict) { | |||
64 | Py_INCREF(dict)_Py_INCREF(((PyObject*)(dict))); | |||
65 | } | |||
66 | ||||
67 | PyFunctionPostHook::~PyFunctionPostHook() { | |||
68 | // If python is already dead, leak the wrapped python objects | |||
69 | if (Py_IsInitialized()) { | |||
70 | pybind11::gil_scoped_acquire gil; | |||
71 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
72 | } | |||
73 | } | |||
74 | ||||
75 | auto PyFunctionPostHook::operator()( | |||
76 | const variable_list& _outputs, /* grad_inputs */ | |||
77 | const variable_list& _inputs /* grad_outputs */) -> variable_list | |||
78 | { | |||
79 | pybind11::gil_scoped_acquire gil; | |||
80 | ||||
81 | THPObjectPtr outputs(wrap_variables(_outputs)); | |||
| ||||
82 | THPObjectPtr inputs(wrap_variables(_inputs)); | |||
83 | ||||
84 | // NOLINTNEXTLINE(cppcoreguidelines-init-variables) | |||
85 | PyObject *key, *hook; | |||
86 | Py_ssize_t pos = 0; | |||
87 | while (PyDict_Next(dict, &pos, &key, &hook)) { | |||
88 | THPObjectPtr res(PyObject_CallFunctionObjArgs( | |||
89 | hook, outputs.get(), inputs.get(), nullptr)); | |||
90 | if (!res) throw python_error(); | |||
91 | if (res == Py_None(&_Py_NoneStruct)) continue; | |||
92 | check_result(outputs, res, hook); | |||
93 | outputs = std::move(res); | |||
94 | } | |||
95 | ||||
96 | return unwrap_variables(outputs.get()); | |||
97 | } | |||
98 | ||||
99 | }} // namespace torch::autograd | |||
100 | ||||
101 | ||||
102 | static PyObject *wrap_variables(const variable_list& c_variables) | |||
103 | { | |||
104 | size_t num_vars = c_variables.size(); | |||
105 | THPObjectPtr tuple(PyTuple_New(num_vars)); | |||
| ||||
106 | if (!tuple) throw python_error(); | |||
107 | for (const auto i : c10::irange(num_vars)) { | |||
108 | THPObjectPtr var(THPVariable_Wrap(c_variables[i])); | |||
109 | if (!var) throw python_error(); | |||
110 | PyTuple_SET_ITEM(tuple.get(), i, var.release())PyTuple_SetItem(tuple.get(), i, var.release()); | |||
111 | } | |||
112 | return tuple.release(); | |||
113 | } | |||
114 | ||||
115 | static variable_list unwrap_variables(PyObject* py_variables) { | |||
116 | variable_list results(PyTuple_GET_SIZE(py_variables)(((PyVarObject*)(((PyTupleObject *)(py_variables))))->ob_size )); | |||
117 | for(const auto i : c10::irange(results.size())) { | |||
118 | PyObject* item = PyTuple_GET_ITEM(py_variables, i)(((PyTupleObject *)(py_variables))->ob_item[i]); | |||
119 | if (item == Py_None(&_Py_NoneStruct)) { | |||
120 | continue; | |||
121 | } else if (THPVariable_Check(item)) { | |||
122 | results[i] = THPVariable_Unpack(item); | |||
123 | } else { | |||
124 | // this should never happen, but just in case... | |||
125 | std::stringstream ss; | |||
126 | ss << "expected variable but got " << Py_TYPE(item)(((PyObject*)(item))->ob_type)->tp_name; | |||
127 | throw std::runtime_error(ss.str()); | |||
128 | } | |||
129 | } | |||
130 | return results; | |||
131 | } | |||
132 | ||||
133 | static void check_result(PyObject* prev, PyObject* result, PyObject* hook) { | |||
134 | if (!PyTuple_Check(result)((((((PyObject*)(result))->ob_type))->tp_flags & (( 1UL << 26))) != 0)) { | |||
135 | PyErr_Format(PyExc_TypeError, "expected tuple, but hook returned '%s'", | |||
136 | THPUtils_typename(result)((((PyObject*)(result))->ob_type)->tp_name)); | |||
137 | throw python_error(); | |||
138 | } | |||
139 | ||||
140 | auto prev_size = PyTuple_GET_SIZE(prev)(((PyVarObject*)(((PyTupleObject *)(prev))))->ob_size); | |||
141 | auto result_size = PyTuple_GET_SIZE(result)(((PyVarObject*)(((PyTupleObject *)(result))))->ob_size); | |||
142 | if (prev_size != result_size) { | |||
143 | std::stringstream ss; | |||
144 | auto name = hook_name(hook); | |||
145 | ss << "hook '" << name << "' has returned an incorrect number "; | |||
146 | ss << "of values (got " << result_size << ", but expected "; | |||
147 | ss << prev_size << ")"; | |||
148 | throw std::runtime_error(ss.str()); | |||
149 | } | |||
150 | ||||
151 | for (const auto i : c10::irange(prev_size)) { | |||
152 | check_single_result(PyTuple_GET_ITEM(prev, i)(((PyTupleObject *)(prev))->ob_item[i]), PyTuple_GET_ITEM(result, i)(((PyTupleObject *)(result))->ob_item[i]), hook); | |||
153 | } | |||
154 | } | |||
155 | ||||
156 | static void check_single_result(PyObject* _original, PyObject* _result, PyObject* hook) { | |||
157 | if (_result == Py_None(&_Py_NoneStruct)) return; | |||
158 | ||||
159 | if (_original == Py_None(&_Py_NoneStruct)) { | |||
160 | throw std::runtime_error("can't replace a None gradient with a non-None value"); | |||
161 | } | |||
162 | ||||
163 | if (!PyObject_IsInstance(_result, THPVariableClass)) { | |||
164 | PyErr_Format(PyExc_TypeError, "expected Variable, but hook returned '%s'", | |||
165 | THPUtils_typename(_result)((((PyObject*)(_result))->ob_type)->tp_name)); | |||
166 | throw python_error(); | |||
167 | } | |||
168 | ||||
169 | const auto& original = THPVariable_Unpack(_original); | |||
170 | const auto& result = THPVariable_Unpack(_result); | |||
171 | ||||
172 | torch::autograd::check_variable_result(original, result, hook_name(hook)); | |||
173 | } | |||
174 | ||||
175 | static std::string hook_name(PyObject* hook) { | |||
176 | if (PyObject_HasAttrString(hook, "__name__")) { | |||
177 | THPObjectPtr name(PyObject_GetAttrString(hook, "__name__")); | |||
178 | if (!name) throw python_error(); | |||
179 | ||||
180 | if (name && THPUtils_checkString(name.get())) { | |||
181 | return THPUtils_unpackString(name.get()); | |||
182 | } | |||
183 | } | |||
184 | return "<unknown>"; | |||
185 | } |
1 | #ifndef PyTuple_New |
2 | struct _object; |
3 | typedef struct _object PyObject; |
4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
5 | PyObject* PyTuple_New(Py_ssize_t len) { |
6 | return clang_analyzer_PyObject_New_Reference(); |
7 | } |
8 | #else |
9 | #warning "API PyTuple_New is defined as a macro." |
10 | #endif |