forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_compiled_autograd.cpp
677 lines (602 loc) · 22.3 KB
/
python_compiled_autograd.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <torch/csrc/dynamo/python_compiled_autograd.h>
#include <torch/csrc/autograd/engine.h>
#include <torch/csrc/autograd/functions/accumulate_grad.h>
#include <torch/csrc/dynamo/compiled_autograd.h>
#include <torch/csrc/jit/python/pybind_utils.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/utils/pythoncapi_compat.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
/*
[Note: Compiled Autograd]
Compiled autograd replaces the standard autograd engine by converting
the autograd graph to an FX graph that can be torch.compiled. It caches
this conversion using a shadow graph. We compare the new graph to the
shadow graph by walking the two graphs simultaneously and computing a
CacheKey for each original node to find the next edge in the shadow graph.
Two different graphs might have a shared common prefix in the shadow
graph, but then diverge at the first difference. Tensors, SavedVariables,
and SymInt found stored on the nodes in the autograd graph are lifted to
become inputs to the graph. All other properties (ints, floats, types,
etc.) are specialized using the CacheKey and will result in landing on
a different cache node in the shadow graph if some property differs.
To interact with the (hundreds) of different autograd::Node types,
we use a visitor pattern that walks each Node structure recursively.
- The first pass, compiled_args/collect, extracts all the inputs to the
graph and builds a CacheKey for us to specialize on. On a cache hit,
we stop here and this is the only pass.
- On a cache miss, a second pass kicks in to extract the FX graph using
apply_with_saved, which uses another visitor pattern. The before()
visitor swaps out all the Tensors, SavedVariables, and SymInt for
fake/symbolic versions to allow tracing. We then run the standard apply()
method, and after() restores things to how we found them.
When we see tensor hooks, we record them directly in the output graph
without tracing into them. We do this to avoid executing unsafe code
at trace time.
Notes:
- We require hooks to not change shapes of tensors.
- We require non-hook autograd nodes to be tracable.
*/
namespace torch::dynamo::autograd {
using c10::SymInt;
static PyObject* wrap_int_list(const std::vector<int64_t>& inputs) {
PyObject* pyinput = PyTuple_New(static_cast<Py_ssize_t>(inputs.size()));
for (const auto i : c10::irange(inputs.size())) {
PyTuple_SET_ITEM(pyinput, i, PyLong_FromSsize_t(inputs[i]));
}
return pyinput;
}
static PyObject* convert_hook_list(std::vector<c10::SafePyObject>& inputs) {
// inplace, consumes the input hooks
PyObject* pyinput = PyTuple_New(static_cast<Py_ssize_t>(inputs.size()));
for (const auto i : c10::irange(inputs.size())) {
PyTuple_SET_ITEM(pyinput, i, inputs[i].release());
}
return pyinput;
}
static PyObject* check(PyObject* pyresult) {
if (C10_UNLIKELY(pyresult == nullptr)) {
// see https://github.com/pytorch/pytorch/pull/34845
python_error err;
err.persist();
// NOLINTNEXTLINE(misc-throw-by-value-catch-by-reference)
throw err;
}
return pyresult;
}
static void check(bool result) {
if (C10_UNLIKELY(!result))
check(nullptr);
}
// snapshot of python verbose logging toggle
static PyObject* python_verbose_logger = nullptr;
struct VerboseLogger {
static std::optional<VerboseLogger> maybe_create() {
if (python_verbose_logger == nullptr) {
return std::nullopt;
}
return VerboseLogger();
}
void verbose_log_fn(std::string_view msg) const {
TORCH_CHECK(python_verbose_logger != nullptr);
check(PyObject_CallFunction(python_verbose_logger, "s", msg.data()));
}
void log_node_check(
const Node& fn,
size_t size_inputs_num,
std::unordered_set<CacheKey> cached_keys,
const CacheKey& key,
size_t node_idx) {
std::string node_name =
fn.name() + " (NodeCall " + std::to_string(node_idx) + ")";
cumulative_sizes_per_node[size_inputs_num] = node_name;
if (!logged_node_miss && cached_keys.find(key) == cached_keys.end()) {
_log_node_miss(typeid(fn), cached_keys, key, node_name);
logged_node_miss = true;
}
}
void _log_node_miss(
const std::type_info& node_type,
std::unordered_set<CacheKey> cached_keys,
const CacheKey& key,
const std::string& node_name) const {
std::ostringstream oss;
oss << "Cache miss due to new autograd node: " << node_name
<< " with key size " << std::to_string(key.key_size)
<< ", previous key sizes=[";
for (auto it = cached_keys.begin(); it != cached_keys.end(); it++) {
if (it->node_type != node_type) {
continue;
}
oss << it->key_size;
if (std::next(it) != cached_keys.end()) {
oss << ",";
}
}
oss << "]";
verbose_log_fn(oss.str());
}
void log_dynamic_shapes_check(size_t size_idx) const {
if (cumulative_sizes_per_node.empty()) {
return;
}
auto it = cumulative_sizes_per_node.lower_bound(size_idx);
TORCH_CHECK(it != cumulative_sizes_per_node.end());
size_t start_idx =
it == cumulative_sizes_per_node.begin() ? 0 : std::prev(it)->first;
verbose_log_fn(
"Cache miss due to changed shapes: marking size idx " +
std::to_string(size_idx - start_idx) + " of " + it->second +
" as dynamic");
}
// track which size index belongs to which node
std::map<size_t, std::string> cumulative_sizes_per_node;
// only log cache miss due to node key once
bool logged_node_miss = false;
};
struct CacheNode {
// A node in the shadow graph, we follow next edges until we reach the end of
// the graph
static CacheNode* root() {
static CacheNode _root;
return &_root;
}
CacheNode* lookup(const CacheKey& key, bool create = true) {
auto it = next.find(key);
if (it == next.end()) {
if (!create)
return nullptr;
// caller's key is in temporary memory, must copy it
CacheKeyBuffer buffer(key.key, key.key_size);
CacheKey key_with_storage(key.node_type, buffer.get(), key.key_size);
it = next.emplace(key_with_storage, std::make_unique<CacheNode>()).first;
key_storage.emplace_back(std::move(buffer));
}
return it->second.get();
}
void clear() {
next.clear();
key_storage.clear();
expected_sizes.clear();
compiled_fn = nullptr;
}
bool is_empty() const {
return next.empty() && !compiled_fn;
}
CacheNode() : compiled_fn(nullptr) {}
~CacheNode() {
if (!Py_IsInitialized()) {
compiled_fn.release(); // leak on shutdown
}
}
CacheNode(CacheNode&&) = delete;
CacheNode(const CacheNode&) = delete;
CacheNode& operator=(const CacheNode&) = delete;
CacheNode& operator=(CacheNode&&) = delete;
bool check_dynamic_sizes(
AutogradCompilerCall& call,
const std::optional<VerboseLogger>& vlogger) {
/*
We start off by assuming everything is static, then we mark things
as dynamic when we see them change. This function:
1) Checks for a cache hit
2) Updates expected_sizes to track what is dynamic
3) Populates call.dyn_size_inputs by filtering call.all_size_inputs
*/
bool cache_hit = compiled_fn.get() != nullptr;
auto len = call.all_size_inputs.size();
const SizeInput* data = call.all_size_inputs.data();
if (expected_sizes.empty()) {
expected_sizes.reserve(len);
for (const auto i : c10::irange(len)) {
expected_sizes.emplace_back(data[i]);
}
}
TORCH_INTERNAL_ASSERT(expected_sizes.size() == call.all_size_inputs.size());
for (const auto i : c10::irange(len)) {
auto& expected = expected_sizes[i];
bool was_dynamic = expected.dyn_type == SizeInput::DYNAMIC;
bool changed_value = expected.value != data[i].value;
if (changed_value) {
if (!was_dynamic) {
cache_hit = false;
if (vlogger.has_value()) {
vlogger->log_dynamic_shapes_check(i);
}
}
expected = SizeInput(SizeInput::DYNAMIC, data[i].value);
}
if (changed_value || was_dynamic) {
if (call.dyn_size_inputs.empty()) {
call.dyn_size_inputs.reserve(len);
}
call.dyn_size_inputs.emplace_back(data[i].value);
}
}
if (!cache_hit) {
// we missed cache because static size inputs didn't match; force
// recompilation with the varying size input as dynamic
compiled_fn = nullptr;
}
return cache_hit;
}
PyObject* wrap_dynamic_inputs() const {
size_t dynamic_count = 0;
size_t idx = 0;
for (const auto& i : expected_sizes) {
if (i.dyn_type == SizeInput::DYNAMIC) {
++dynamic_count;
}
}
PyObject* pyinput = PyTuple_New(static_cast<Py_ssize_t>(dynamic_count));
for (const auto& i : expected_sizes) {
if (i.dyn_type == SizeInput::DYNAMIC) {
PyTuple_SET_ITEM(pyinput, idx++, PyLong_FromSsize_t(i.value));
}
}
TORCH_INTERNAL_ASSERT(idx == dynamic_count);
return pyinput;
}
std::vector<std::optional<SymInt>> unwrap_dynamic_inputs(
PyObject* pyresult) const {
TORCH_INTERNAL_ASSERT(PyList_CheckExact(pyresult));
size_t idx = 0;
size_t result_len = PyList_GET_SIZE(pyresult);
std::vector<std::optional<SymInt>> result;
result.reserve(expected_sizes.size());
for (const auto& i : expected_sizes) {
if (i.dyn_type == SizeInput::DYNAMIC) {
TORCH_INTERNAL_ASSERT(idx < result_len);
result.emplace_back(
py::cast<c10::SymInt>(PyList_GET_ITEM(pyresult, idx++)));
} else {
result.emplace_back();
}
}
TORCH_INTERNAL_ASSERT(
idx == result_len && result.size() == expected_sizes.size());
return result;
}
std::unordered_map<CacheKey, std::unique_ptr<CacheNode>> next;
std::vector<CacheKeyBuffer> key_storage;
std::vector<SizeInput> expected_sizes;
THPObjectPtr compiled_fn;
};
struct InputBuffers : public std::unordered_map<Node*, InputBuffer> {
InputBuffer& lookup(Node* function) {
auto it = emplace(function, InputBuffer(function->num_inputs())).first;
return it->second;
}
};
static PyObject* the_autograd_compiler = nullptr;
static PyObject* set_autograd_compiler(PyObject* dummy, PyObject* args);
static PyObject* clear_cache(PyObject* dummy, PyObject* args) {
HANDLE_TH_ERRORS;
CacheNode::root()->clear();
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS;
}
static PyObject* is_cache_empty(PyObject* dummy, PyObject* args) {
HANDLE_TH_ERRORS;
if (CacheNode::root()->is_empty()) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
END_HANDLE_TH_ERRORS;
}
static PyObject* set_verbose_logger(PyObject* dummy, PyObject* args) {
HANDLE_TH_ERRORS;
PyObject* logger = nullptr;
if (!PyArg_ParseTuple(args, "O", &logger)) {
Py_RETURN_FALSE;
}
if (logger == Py_None) {
python_verbose_logger = nullptr;
} else {
python_verbose_logger = logger;
}
Py_RETURN_TRUE;
END_HANDLE_TH_ERRORS;
}
// NOLINTNEXTLINE(*array*)
static PyMethodDef _methods[] = {
{"set_autograd_compiler", set_autograd_compiler, METH_VARARGS, nullptr},
{"clear_cache", clear_cache, METH_NOARGS, nullptr},
{"is_cache_empty", is_cache_empty, METH_NOARGS, nullptr},
{"set_verbose_logger", set_verbose_logger, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}};
static struct PyModuleDef _module = {
PyModuleDef_HEAD_INIT,
"torch._C._dynamo.autograd_compiler",
"Hooks for compiling autograd",
-1,
_methods};
static TraceState call_begin_capture(
PyObject* self,
CacheNode& cache,
AutogradCompilerCall& compiler_call,
size_t num_outputs) {
static PyObject* method_name = PyUnicode_InternFromString("begin_capture");
THPObjectPtr pyinput(THPVariable_WrapList(compiler_call.tensor_args.inputs));
THPObjectPtr pysizeinput(cache.wrap_dynamic_inputs());
THPObjectPtr pyresult(check(PyObject_CallMethodObjArgs(
self, method_name, pyinput.get(), pysizeinput.get(), nullptr)));
PyObject *fake_inputs{nullptr}, *fake_sizes{nullptr};
check(PyArg_ParseTuple(pyresult.get(), "OO", &fake_inputs, &fake_sizes));
variable_list proxy_inputs = THPVariable_UnpackList(fake_inputs);
TORCH_INTERNAL_ASSERT(
proxy_inputs.size() == compiler_call.tensor_args.inputs.size());
for (const auto i : c10::irange(proxy_inputs.size())) {
TensorArg& arg =
compiler_call.tensor_args.lookup(compiler_call.tensor_args.inputs[i]);
arg.proxy_tensor = proxy_inputs[i];
}
return TraceState(cache.unwrap_dynamic_inputs(fake_sizes), num_outputs);
}
static PyObject* call_end_capture(PyObject* self, const variable_list& inputs) {
static PyObject* method_name = PyUnicode_InternFromString("end_capture");
THPObjectPtr pyinput(THPVariable_WrapList(inputs));
return check(PyObject_CallMethodOneArg(self, method_name, pyinput.get()));
}
struct ClosingTHPObjectPtr : public THPObjectPtr {
ClosingTHPObjectPtr(PyObject* o) : THPObjectPtr(o) {}
~ClosingTHPObjectPtr() {
if (PyErr_Occurred()) {
// do nothing, do not attempt to close
return;
}
static PyObject* method_name = PyUnicode_InternFromString("close");
if (PyObject_CallMethodNoArgs(get(), method_name) == nullptr) {
PyErr_WriteUnraisable(get());
PyErr_Clear();
}
}
};
// Only call this function while holding GIL
CacheNode* _compiled_autograd_impl(
const std::shared_ptr<Node>& graph_root,
GraphTask& graph_task,
bool accumulate_grad,
const edge_list& output_edges,
THPObjectPtr* graph_arg_inputs,
THPObjectPtr* graph_arg_sizes,
THPObjectPtr* graph_arg_hooks) {
std::unordered_map<Node*, int>& dependencies = graph_task.dependencies_;
std::vector<std::shared_ptr<Node>> worklist{graph_root};
AutogradCompilerCall compiler_call;
for (const auto i : c10::irange(output_edges.size())) {
compiler_call.node_calls
.lookup(output_edges[i].function)
// NOLINTNEXTLINE(*-narrowing-conversions)
.mark_output(output_edges[i].input_nr, i);
}
const bool check_exec_info = !graph_task.exec_info_.empty();
CacheNode* cache = CacheNode::root();
std::vector<NodeCall*> calls;
calls.reserve(
check_exec_info ? graph_task.exec_info_.size() : dependencies.size() + 1);
int i = 0;
std::optional<VerboseLogger> vlogger = VerboseLogger::maybe_create();
while (!worklist.empty()) {
std::shared_ptr<Node> fn = std::move(worklist.back());
worklist.pop_back();
NodeCall& call = compiler_call.node_calls.lookup(fn);
calls.emplace_back(&call);
{ // update cache and gather args into `compiler_call`
CompiledNodeArgs node_args(compiler_call, call);
node_args.collect(call);
if (node_args.cond(call.needed)) {
fn->compiled_args(node_args);
node_args.collect(call.node->next_edges());
}
CacheKey key = node_args.key();
if (vlogger.has_value()) {
std::unordered_set<CacheKey> cached_keys;
for (const auto& [k, _] : cache->next) {
cached_keys.emplace(k);
}
vlogger->log_node_check(
*fn,
compiler_call.all_size_inputs.size(),
std::move(cached_keys),
key,
i);
}
cache = cache->lookup(key);
}
for (const auto& edge : fn->next_edges()) {
if (!edge.is_valid()) {
continue;
}
if (check_exec_info) {
auto it = graph_task.exec_info_.find(edge.function.get());
if (it == graph_task.exec_info_.end() || !it->second.should_execute()) {
continue;
}
if (!it->second.needed_) {
compiler_call.node_calls.lookup(edge.function).needed = false;
}
}
auto it = dependencies.find(edge.function.get());
TORCH_INTERNAL_ASSERT(it != dependencies.end());
if (--it->second == 0) {
dependencies.erase(it);
worklist.emplace_back(edge.function);
}
}
i++;
}
// TODO(jansel): some dynamic sizes seem to be ints not symints
if (!cache->check_dynamic_sizes(compiler_call, vlogger)) {
// cache miss, need to capture FX graph
ClosingTHPObjectPtr py_compiler(
check(PyObject_CallNoArgs((the_autograd_compiler))));
TraceState state = call_begin_capture(
py_compiler, *cache, compiler_call, output_edges.size());
InputBuffers input_buffers;
for (size_t i = 0; i < calls.size(); i++) {
NodeCall& call = *calls[i];
// TODO(jansel): consider adding some of this stuff:
// guard(local_graph_task); NodeGuard ndguard(task.fn_); const auto
// opt_parent_stream = (*func).stream(c10::DeviceType::CUDA);
// c10::OptionalStreamGuard parent_stream_guard{opt_parent_stream};
// CheckpointValidGuard cpvguard(graph_task);
// at::getStepCallbacksUnlessEmpty(at::RecordScope::BACKWARD_FUNCTION);
// if (C10_UNLIKELY(step_callbacks.has_value())) { ... }
variable_list inputs =
std::move(input_buffers.lookup(call.node.get()).buffer);
input_buffers.erase(call.node.get());
if (!call.tensor_pre_hooks.empty()) {
THPObjectPtr pyinputs(THPVariable_WrapList(inputs));
for (const auto& hook : call.tensor_pre_hooks) {
pyinputs = check(PyObject_CallMethod(
py_compiler,
"tensor_pre_hook",
"Oii",
pyinputs.get(),
hook.first,
hook.second));
}
inputs = THPVariable_UnpackList(pyinputs);
}
for (const auto& graph_output : call.graph_output) {
int input_nr = graph_output.first;
int output_index = graph_output.second;
TORCH_INTERNAL_ASSERT(
output_index < static_cast<int>(state.outputs.size()));
TORCH_INTERNAL_ASSERT(!state.outputs[output_index].defined());
state.outputs[output_index] = inputs[input_nr];
}
if (!call.needed) {
continue;
}
if (!call.pre_hooks.empty()) {
THPObjectPtr pyinputs(THPVariable_WrapList(inputs));
for (const auto hook : call.pre_hooks) {
pyinputs = check(PyObject_CallMethod(
py_compiler.get(), "pre_hook", "Oi", pyinputs.get(), hook));
}
inputs = THPVariable_UnpackList(pyinputs);
}
if (python_verbose_logger != nullptr) {
std::string _node_name = call.node->name();
THPObjectPtr node_name(PyUnicode_FromString(_node_name.data()));
TORCH_INTERNAL_ASSERT(node_name != nullptr);
THPObjectPtr set_node_origin(
PyObject_GetAttrString(py_compiler.get(), "set_node_origin"));
check(PyObject_CallFunction(
set_node_origin, "OI", node_name.get(), i, nullptr));
}
SwapSavedVariables saved(compiler_call, state, py_compiler.get(), call);
variable_list outputs = call.node->apply_with_saved(inputs, saved);
saved.debug_asserts();
saved.before(call.node->next_edges());
validate_outputs(
call.node->next_edges(), outputs, [&](const std::string& msg) {
std::ostringstream ss;
ss << "[Compiled Autograd Tracing: " << call.node->name() << "] "
<< msg;
return ss.str();
});
saved.after(call.node->next_edges());
saved.debug_asserts();
if (!call.post_hooks.empty()) {
THPObjectPtr pyinputs(THPVariable_WrapList(inputs));
THPObjectPtr pyoutputs(THPVariable_WrapList(outputs));
for (const auto hook : call.post_hooks) {
pyoutputs = check(PyObject_CallMethod(
py_compiler.get(),
"post_hook",
"OOi",
pyoutputs.get(),
pyinputs.get(),
hook));
}
outputs = THPVariable_UnpackList(pyoutputs);
}
for (const auto i : c10::irange(outputs.size())) {
auto& output = outputs[i];
const auto& next = call.node->next_edge(i);
if (next.is_valid() && output.defined()) {
input_buffers.lookup(next.function.get())
.add(
next.input_nr, std::move(output), c10::nullopt, c10::nullopt);
}
}
}
cache->compiled_fn = check(call_end_capture(py_compiler, state.outputs));
state.debug_asserts();
} // End cache miss region
// TODO(jansel): we should release all the variables and then use a
// boxed calling convention so activation memory can be freed
// TODO(jansel): clear grads we will overwrite below
if (!graph_task.keep_graph_) {
for (auto& call : calls) {
call->node->release_variables();
}
}
*graph_arg_inputs = THPVariable_WrapList(compiler_call.tensor_args.inputs);
*graph_arg_sizes = wrap_int_list(compiler_call.dyn_size_inputs);
*graph_arg_hooks = convert_hook_list(compiler_call.hooks);
return cache;
}
variable_list compiled_autograd(
const std::shared_ptr<Node>& graph_root,
GraphTask& graph_task,
bool accumulate_grad,
const edge_list& output_edges) {
TORCH_CHECK(
output_edges.empty() || !accumulate_grad,
"specifying inputs= with .backward() not yet implemented for compiled autograd")
TORCH_CHECK(
c10::impl::TorchDispatchModeTLS::stack_len() == 0,
"TorchDispatchMode not yet implemented for compiled autograd")
static std::mutex lock;
std::lock_guard<std::mutex> lock_guard(lock);
pybind11::gil_scoped_acquire gil;
at::ThreadLocalStateGuard tls_guard(graph_task.thread_locals_);
THPObjectPtr inputs;
THPObjectPtr sizes;
THPObjectPtr hooks;
CacheNode* cache = _compiled_autograd_impl(
graph_root,
graph_task,
accumulate_grad,
output_edges,
&inputs,
&sizes,
&hooks);
THPObjectPtr pyresult(check(PyObject_CallFunctionObjArgs(
cache->compiled_fn.get(), inputs.get(), sizes.get(), hooks.get(), NULL)));
variable_list outputs = THPVariable_UnpackList(pyresult);
TORCH_INTERNAL_ASSERT(outputs.size() == output_edges.size());
return outputs;
}
static PyObject* set_autograd_compiler(PyObject* dummy, PyObject* args) {
HANDLE_TH_ERRORS;
PyObject* obj = nullptr;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return nullptr;
}
PyObject* prior = the_autograd_compiler;
if (obj == Py_None) { // disable
the_autograd_compiler = nullptr; // decref not needed due to `prior`
Engine::set_compiled_autograd(nullptr);
} else { // enable
Py_INCREF(obj);
the_autograd_compiler = obj;
Engine::set_compiled_autograd(&compiled_autograd);
}
if (prior == nullptr) {
Py_RETURN_NONE;
} else {
return prior;
}
END_HANDLE_TH_ERRORS;
}
PyObject* torch_c_dynamo_compiled_autograd_init() {
return PyModule_Create(&_module);
}
} // namespace torch::dynamo::autograd