forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_hook.cpp
352 lines (310 loc) · 10.7 KB
/
python_hook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <torch/csrc/autograd/python_hook.h>
#include <c10/util/irange.h>
#include <pybind11/pybind11.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/PyInterpreter.h>
#include <torch/csrc/THP.h>
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/dynamo/compiled_autograd.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/pybind.h>
#include <torch/csrc/utils/python_strings.h>
#include <iostream>
#include <sstream>
using torch::autograd::Variable;
using torch::autograd::variable_list;
static PyObject* wrap_variables(const variable_list& c_variables);
static variable_list unwrap_variables(PyObject* py_variables);
static std::string hook_name(PyObject* hook);
static void check_result(PyObject* original, PyObject* result, PyObject* hook);
static void check_single_result(
PyObject* original,
PyObject* result,
PyObject* hook);
namespace torch::autograd {
namespace {
// This function is called in 4 different cases:
// 1) TensorPreHook
// 2) PreHook
// 3) PostHook
// 4) TensorPostAccGradHook
//
// Depending on the case, args and res can hold different types of objects:
//
// args:
// TensorPreHook (Tensor,)
// PreHook ((Tensor, ...),) (grad_outputs,)
// PostHook ((Tensor, ...), (Tensor, ...)) (grad_inputs, grad_outputs)
// TensorPostAccGradHook ((Tensor), ()) (tensor,)
//
// res:
// TensorPreHook Tensor
// PreHook ((Tensor, ...),) (grad_outputs,)
// PostHook ((Tensor, ...),) (grad_inputs,)
// TensorPostAccGradHook None
//
// This function returns True if any hook returned non-None value, and False
// otherwise.
bool _call_hooks(PyObject* dict, PyObject* args) {
// Note: [Extend Hook Lifetime]
// Hold a reference to hooks till we iterate over them.
// This is to handle the case when hook calls `handle.remove` inside it
// and it's refcount goes to `0`, Python is free to GC it.
// We hold onto a stale pointer and subsequent call to
// `check_single_result`, which tries to fetch the `hook`'s name segfaults.
// So, we use `PyDict_Values` which returns a new reference to the values
// i.e. we hold the reference to the hooks till we have iterated over them.
// Reference: https://github.com/pytorch/pytorch/issues/58354
auto hooks = THPObjectPtr{PyDict_Values(dict)};
bool is_modified = false;
const auto len = PyList_Size(hooks);
for (Py_ssize_t idx = 0; idx < len; ++idx) {
const auto hook = PyList_GetItem(hooks, idx);
THPObjectPtr res(PyObject_CallObject(hook, args));
if (!res)
throw python_error();
if (res == Py_None)
continue;
PyObject* args0 = PyTuple_GetItem(args, 0);
if (res == args0)
continue;
if (PyTuple_CheckExact(args0)) {
check_result(args0, res, hook);
} else {
check_single_result(args0, res, hook);
}
PyTuple_SetItem(args, 0, res.release());
is_modified = true;
}
return is_modified;
}
} // namespace
PyFunctionTensorPreHook::PyFunctionTensorPreHook(
PyObject* dict,
size_t value_idx)
: dict(dict), value_idx(value_idx) {
Py_INCREF(dict);
}
// NOLINTNEXTLINE(bugprone-exception-escape)
PyFunctionTensorPreHook::~PyFunctionTensorPreHook() {
// If python is already dead, leak the wrapped python objects
if (Py_IsInitialized()) {
pybind11::gil_scoped_acquire gil;
Py_DECREF(dict);
}
}
auto PyFunctionTensorPreHook::operator()(const variable_list& values)
-> variable_list {
pybind11::gil_scoped_acquire gil;
THPObjectPtr value(THPVariable_Wrap(values.at(value_idx)));
if (!value)
throw python_error();
THPObjectPtr tup(PyTuple_New(1));
PyTuple_SET_ITEM(tup.get(), 0, value.release());
bool is_tup_modified = _call_hooks(dict, tup.get());
variable_list results(values);
if (is_tup_modified) {
results[value_idx] = THPVariable_Unpack(PyTuple_GetItem(tup.get(), 0));
}
return results;
}
PyFunctionPreHook::PyFunctionPreHook(PyObject* dict) : dict(dict) {
Py_INCREF(dict);
}
// NOLINTNEXTLINE(bugprone-exception-escape)
PyFunctionPreHook::~PyFunctionPreHook() {
// If python is already dead, leak the wrapped python objects
if (Py_IsInitialized()) {
pybind11::gil_scoped_acquire gil;
Py_DECREF(dict);
}
}
auto PyFunctionPreHook::operator()(const variable_list& grad_outputs_)
-> variable_list {
pybind11::gil_scoped_acquire gil;
THPObjectPtr grad_outputs(wrap_variables(grad_outputs_));
THPObjectPtr tup(PyTuple_New(1));
PyTuple_SET_ITEM(tup.get(), 0, grad_outputs.release());
_call_hooks(dict, tup.get());
return unwrap_variables(PyTuple_GetItem(tup.get(), 0));
}
PyFunctionPostHook::PyFunctionPostHook(PyObject* dict) : dict(dict) {
Py_INCREF(dict);
}
// NOLINTNEXTLINE(bugprone-exception-escape)
PyFunctionPostHook::~PyFunctionPostHook() {
// If python is already dead, leak the wrapped python objects
if (Py_IsInitialized()) {
pybind11::gil_scoped_acquire gil;
Py_DECREF(dict);
}
}
auto PyFunctionPostHook::operator()(
const variable_list& _outputs, /* grad_inputs */
const variable_list& _inputs /* grad_outputs */) -> variable_list {
pybind11::gil_scoped_acquire gil;
THPObjectPtr grad_inputs(wrap_variables(_outputs));
THPObjectPtr grad_outputs(wrap_variables(_inputs));
THPObjectPtr tup(PyTuple_New(2));
PyTuple_SET_ITEM(tup.get(), 0, grad_inputs.release());
PyTuple_SET_ITEM(tup.get(), 1, grad_outputs.release());
_call_hooks(dict, tup.get());
return unwrap_variables(PyTuple_GetItem(tup.get(), 0));
}
void PyFunctionTensorPreHook::compiled_args(CompiledNodeArgs& args) {
PyObject *key = nullptr, *value = nullptr;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
Py_INCREF(value);
args.add_tensor_pre_hook(
c10::SafePyObject(value, getPyInterpreter()),
static_cast<int>(value_idx));
}
}
void PyFunctionPreHook::compiled_args(CompiledNodeArgs& args) {
PyObject *key = nullptr, *value = nullptr;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
Py_INCREF(value);
args.add_pre_hook(c10::SafePyObject(value, getPyInterpreter()));
}
}
void PyFunctionPostHook::compiled_args(CompiledNodeArgs& args) {
PyObject *key = nullptr, *value = nullptr;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
Py_INCREF(value);
args.add_post_hook(c10::SafePyObject(value, getPyInterpreter()));
}
}
PyFunctionTensorPostAccGradHooks::PyFunctionTensorPostAccGradHooks(
PyObject* dict)
: dict(dict) {
Py_INCREF(dict);
}
// NOLINTNEXTLINE(bugprone-exception-escape)
PyFunctionTensorPostAccGradHooks::~PyFunctionTensorPostAccGradHooks() {
// If python is already dead, leak the wrapped python objects
if (Py_IsInitialized()) {
pybind11::gil_scoped_acquire gil;
Py_DECREF(dict);
}
}
auto PyFunctionTensorPostAccGradHooks::operator()(const Variable& tensor)
-> void {
pybind11::gil_scoped_acquire gil;
THPObjectPtr tup(PyTuple_New(1));
PyTuple_SET_ITEM(tup.get(), 0, THPVariable_Wrap(tensor));
bool returned_none = !_call_hooks(dict, tup.get());
TORCH_CHECK(
returned_none, "Tensor post accumulate grad hooks should return None.");
}
void PyFunctionTensorPostAccGradHooks::compiled_args(
torch::dynamo::autograd::CompiledNodeArgs& args) {
PyObject *key = nullptr, *value = nullptr;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
Py_INCREF(value);
c10::SafePyObject hook_obj(value, getPyInterpreter());
args.add_post_acc_grad_hook(std::move(hook_obj));
}
}
void PyFunctionTensorPostAccGradHooks::apply_with_saved(
Variable& tensor,
torch::dynamo::autograd::SwapSavedVariables& saved) {
for (const auto hook : saved.get_curr_node_call().post_acc_grad_hooks) {
THPObjectPtr py_var(THPVariable_Wrap(tensor));
PyObject_CallMethod(
saved.get_py_compiler(),
"post_acc_grad_hook",
"Oi",
py_var.get(),
hook);
}
}
} // namespace torch::autograd
static PyObject* wrap_variables(const variable_list& c_variables) {
size_t num_vars = c_variables.size();
THPObjectPtr tuple(PyTuple_New(static_cast<Py_ssize_t>(num_vars)));
if (!tuple)
throw python_error();
for (const auto i : c10::irange(num_vars)) {
THPObjectPtr var(THPVariable_Wrap(c_variables[i]));
if (!var)
throw python_error();
PyTuple_SET_ITEM(tuple.get(), i, var.release());
}
return tuple.release();
}
static variable_list unwrap_variables(PyObject* py_variables) {
variable_list results(PyTuple_GET_SIZE(py_variables));
for (const auto i : c10::irange(results.size())) {
PyObject* item = PyTuple_GET_ITEM(py_variables, i);
if (item == Py_None) {
continue;
} else if (THPVariable_Check(item)) {
results[i] = THPVariable_Unpack(item);
} else {
// this should never happen, but just in case...
std::stringstream ss;
ss << "expected variable but got " << Py_TYPE(item)->tp_name;
throw std::runtime_error(ss.str());
}
}
return results;
}
static void check_result(PyObject* prev, PyObject* result, PyObject* hook) {
if (!PyTuple_Check(result)) {
PyErr_Format(
PyExc_TypeError,
"expected tuple, but hook returned '%s'",
THPUtils_typename(result));
throw python_error();
}
auto prev_size = PyTuple_GET_SIZE(prev);
auto result_size = PyTuple_GET_SIZE(result);
if (prev_size != result_size) {
std::stringstream ss;
auto name = hook_name(hook);
ss << "hook '" << name << "' has returned an incorrect number ";
ss << "of values (got " << result_size << ", but expected ";
ss << prev_size << ")";
throw std::runtime_error(ss.str());
}
for (const auto i : c10::irange(prev_size)) {
check_single_result(
PyTuple_GET_ITEM(prev, i), PyTuple_GET_ITEM(result, i), hook);
}
}
static void check_single_result(
PyObject* _original,
PyObject* _result,
PyObject* hook) {
if (_result == Py_None)
return;
if (_original == Py_None) {
throw std::runtime_error(
"can't replace a None gradient with a non-None value");
}
if (!PyObject_IsInstance(_result, THPVariableClass)) {
PyErr_Format(
PyExc_TypeError,
"expected Variable, but hook returned '%s'",
THPUtils_typename(_result));
throw python_error();
}
const auto& original = THPVariable_Unpack(_original);
const auto& result = THPVariable_Unpack(_result);
torch::autograd::check_variable_result(original, result, hook_name(hook));
}
static std::string hook_name(PyObject* hook) {
if (PyObject_HasAttrString(hook, "__name__")) {
THPObjectPtr name(PyObject_GetAttrString(hook, "__name__"));
if (!name)
throw python_error();
if (name && THPUtils_checkString(name.get())) {
return THPUtils_unpackString(name.get());
}
}
return "<unknown>";
}