forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiff.cpp
1192 lines (1071 loc) · 48.2 KB
/
autodiff.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <torch/csrc/jit/autodiff.h>
#include <ATen/core/functional.h>
#include <torch/csrc/jit/operator.h>
#include <torch/csrc/jit/passes/common_subexpression_elimination.h>
#include <torch/csrc/jit/passes/constant_pooling.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/lower_tuples.h>
#include <torch/csrc/jit/script/compiler.h>
#include <torch/csrc/jit/symbolic_script.h>
#include <torch/csrc/jit/symbolic_variable.h>
#include <c10/util/Exception.h>
#include <algorithm>
#include <memory>
namespace torch {
namespace jit {
using value_map = std::unordered_map<Value*, Value*>;
using value_set = std::unordered_set<Value*>;
void wrapDim(int64_t& dim, const std::vector<int64_t>& sizes) {
if (dim < 0) {
dim += sizes.size();
}
}
// need_trim_grad_ops contains functions that return multiple outputs in
// forward, but only the first one requires grad.
// Example:
// kthvalue returns (kthvalue, index of kthvalue), currently autodiff only
// supports at most one output that requires grad. Thus we need to remove
// the grad for index that doesn't require grad.
bool needTrimGrad(Node* n) {
static OperatorSet need_trim_grad_ops = {
"aten::kthvalue(Tensor self, int k, int dim, bool keepdim) -> (Tensor, Tensor)",
"aten::topk(Tensor self, int k, int dim, bool largest, bool sorted) -> (Tensor, Tensor)",
};
if (need_trim_grad_ops.find(n)) {
return true;
}
return false;
}
bool isDifferentiable(Node* n) {
// TODO: scalar-tensor ops should be canonicalized
static OperatorSet differentiable_ops = {
"aten::add(Tensor self, Tensor other, *, Scalar alpha) -> Tensor",
"aten::add(Tensor self, Scalar other, Scalar alpha) -> Tensor",
"aten::sub(Tensor self, Tensor other, *, Scalar alpha) -> Tensor",
"aten::sub(Tensor self, Scalar other, Scalar alpha) -> Tensor",
"aten::mul(Tensor self, Tensor other) -> Tensor",
"aten::mul(Tensor self, Scalar other) -> Tensor",
"aten::div(Tensor self, Tensor other) -> Tensor",
"aten::div(Tensor self, Scalar other) -> Tensor",
"aten::max(Tensor self, Tensor other) -> Tensor",
"aten::min(Tensor self, Tensor other) -> Tensor",
"aten::sigmoid(Tensor self) -> Tensor",
"aten::tanh(Tensor self) -> Tensor",
"aten::relu(Tensor self) -> Tensor",
"aten::threshold(Tensor self, Scalar threshold, Scalar value) -> Tensor",
"aten::erf(Tensor self) -> Tensor",
"aten::erfc(Tensor self) -> Tensor",
"aten::exp(Tensor self) -> Tensor",
"aten::t(Tensor self) -> Tensor",
"aten::neg(Tensor self) -> Tensor",
"aten::clamp(Tensor self, Scalar? min, Scalar? max) -> Tensor",
"aten::where(Tensor condition, Tensor self, Tensor other) -> Tensor",
"aten::type_as(Tensor self, Tensor other) -> Tensor",
"aten::unsqueeze(Tensor self, int dim) -> Tensor",
"aten::addmm(Tensor self, Tensor mat1, Tensor mat2, *, Scalar beta, Scalar alpha) -> Tensor",
"aten::mm(Tensor self, Tensor mat2) -> Tensor",
"aten::lt(Tensor self, Tensor other) -> Tensor",
"aten::le(Tensor self, Tensor other) -> Tensor",
"aten::gt(Tensor self, Tensor other) -> Tensor",
"aten::ge(Tensor self, Tensor other) -> Tensor",
"aten::eq(Tensor self, Tensor other) -> Tensor",
"aten::ne(Tensor self, Tensor other) -> Tensor",
"aten::lt(Tensor self, Scalar other) -> Tensor",
"aten::le(Tensor self, Scalar other) -> Tensor",
"aten::gt(Tensor self, Scalar other) -> Tensor",
"aten::ge(Tensor self, Scalar other) -> Tensor",
"aten::eq(Tensor self, Scalar other) -> Tensor",
"aten::ne(Tensor self, Scalar other) -> Tensor",
"aten::abs(Tensor self) -> Tensor",
"aten::acos(Tensor self) -> Tensor",
"aten::asin(Tensor self) -> Tensor",
"aten::atan(Tensor self) -> Tensor",
"aten::ceil(Tensor self) -> Tensor",
"aten::cos(Tensor self) -> Tensor",
"aten::cosh(Tensor self) -> Tensor",
"aten::exp(Tensor self) -> Tensor",
"aten::expm1(Tensor self) -> Tensor",
"aten::floor(Tensor self) -> Tensor",
"aten::fmod(Tensor self, Scalar other) -> Tensor",
"aten::frac(Tensor self) -> Tensor",
"aten::log(Tensor self) -> Tensor",
"aten::log10(Tensor self) -> Tensor",
"aten::log1p(Tensor self) -> Tensor",
"aten::log2(Tensor self) -> Tensor",
"aten::rand_like(Tensor self) -> Tensor",
"aten::reciprocal(Tensor self) -> Tensor",
"aten::remainder(Tensor self, Scalar other) -> Tensor",
"aten::round(Tensor self) -> Tensor",
"aten::rsqrt(Tensor self) -> Tensor",
"aten::sin(Tensor self) -> Tensor",
"aten::sinh(Tensor self) -> Tensor",
"aten::tan(Tensor self) -> Tensor",
"aten::trunc(Tensor self) -> Tensor",
"aten::_grad_sum_to_size(Tensor(a) self, int[] size) -> Tensor(a)",
"aten::log_softmax(Tensor self, int dim) -> Tensor",
"aten::avg_pool2d(Tensor self, int[] kernel_size, int[] stride, int[] padding, bool ceil_mode, bool count_include_pad) -> Tensor",
"aten::max_pool2d_with_indices(Tensor self, int[] kernel_size, int[] stride, int[] padding, int[] dilation, bool ceil_mode) -> (Tensor, Tensor)",
"aten::thnn_conv2d_forward(Tensor self, Tensor weight, int[] kernel_size, Tensor? bias, int[] stride, int[] padding) -> (Tensor, Tensor, Tensor)",
"aten::native_batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps) -> (Tensor, Tensor, Tensor)",
};
// TODO: add support for the following fusible operators.
// They're a little tricky to implement; max/min require mutability for best
// perf "aten::atan2(Tensor self) -> Tensor", "aten::max(Tensor self) ->
// Tensor", "aten::min(Tensor self) -> Tensor"
if (n->kind() == prim::Constant || n->kind() == prim::AutogradZero ||
n->kind() == prim::AutogradAdd || n->kind() == prim::ConstantChunk)
return true;
if (differentiable_ops.find(n))
return true;
if (n->matches(
"aten::dropout(Tensor input, float p, bool train) -> Tensor",
attr::train)) {
return n->get<bool>(attr::train).value();
}
auto schema = n->maybeSchema();
if (schema && hasGradientInfoForSchema(*schema)) {
return true;
}
if (n->matches(
"aten::expand(Tensor self, int[] size, *, bool implicit) -> Tensor")) {
return n->get<std::vector<int64_t>>(attr::size) &&
n->is_constant(attr::implicit) &&
n->namedInput(attr::self)->type()->cast<CompleteTensorType>();
}
if (n->matches("aten::view(Tensor self, int[] size) -> Tensor")) {
return n->get<std::vector<int64_t>>(attr::size) &&
n->namedInput(attr::self)->type()->cast<CompleteTensorType>();
}
if (n->matches(
"aten::nll_loss(Tensor self, Tensor target, Tensor? weight, int reduction, int ignore_index) -> Tensor")) {
// TODO(asuhan): support weight
return n->namedInput(attr::weight)->node()->mustBeNone();
}
// linear blocks may appear as inputs to graph executors, but they are removed
// before differentiation occurs
if (n->kind() == prim::GradOf) {
auto body = n->blocks().at(0);
return std::all_of(
body->nodes().begin(),
body->nodes().end(),
static_cast<bool (*)(Node*)>(isDifferentiable));
}
// formulas are only defined with floating point scalars,
// so we fallback to autograd for other cases.
for (const Value* input : n->inputs()) {
if (input->type() == NumberType::get()) {
return false;
}
}
return false;
}
bool isDifferentiable(Graph& g) {
return std::all_of(
g.nodes().begin(),
g.nodes().end(),
static_cast<bool (*)(Node*)>(isDifferentiable));
}
// NB: Write gradient using torchscript
// For example, node aten::mul() should be defined as follows
// def forward(x, y):
// return x*y, (x, y)
// def backward(ctx, grad_output):
// x, y = ctx
// return (y * grad_output).sum_to_size(x), (x * grad_output).sum_to_size(y)
//
// Here ctx is a tuple that carries all input/intermediate results needed in
// backward from forward pass.
//
// This python code is compiled into a GradientPair which includes a forward
// graph and a backward graph. Forward graph will be used to replace the node in
// grad_desc.f, and backward graph will be used to construct GradOf(node) in
// reverse_block. Grad_values(a.k.a gradOutputs) propagated through
// node->owningGraph() in **reversed** order, thus GradientPair.forward ahould
// be inserted **after** the node being replaced, so that we don't traverse the
// graph infinite times.
//
// The output of compiled forward graph is [real_outputs, ctx]
// The input of compiled backward graph is [ctx, grad_values]
// We run LowerSimpleTuples afterwards to elmininate all tuples generated in
// this process. The original node and TupleConstruct nodes in forward graph
// will be cleaned up later using EliminateDeadCode(block). TupleUnPack node in
// backward graph will be removed in eliminateDeadcode(ReverseDetails) defined
// in this file.
static c10::optional<std::vector<Value*>> build_script_grad(
Node* node,
const ArrayRef<Value*>& grads) {
auto graph = node->owningGraph();
auto compiled_graphs = gradientInfoForSchema(node->schema());
if (!compiled_graphs) {
return c10::nullopt;
}
// Use forward graph to replace node in grad_desc.f
value_list new_outputs;
{
WithInsertPoint guard(node->next());
auto fw_graph = compiled_graphs->forward;
new_outputs = inlineCallTo(
*graph, *fw_graph, node->inputs(), /*unpack_outputs=*/true);
auto outputs = node->outputs();
AT_ASSERT(new_outputs.size() == outputs.size() + 1);
for (size_t i = 0; i < outputs.size(); ++i) {
new_outputs.at(i)->setType(outputs[i]->type());
outputs[i]->replaceAllUsesWith(new_outputs.at(i));
}
}
// Use backward graph to construct reverse_block
auto bw_graph = compiled_graphs->backward;
auto grad_vec = grads.vec();
if (needTrimGrad(node)) {
grad_vec.erase(grad_vec.begin() + 1, grad_vec.end());
}
auto it = grad_vec.begin();
grad_vec.insert(it, new_outputs.back());
ArrayRef<Value*> grad(grad_vec);
auto grad_inputs =
inlineCallTo(*graph, *bw_graph, grad, /*unpack_outputs=*/true);
return grad_inputs;
};
namespace {
class GradientHelper {
public:
GradientHelper(Node* n) : node(n) {}
std::vector<Value*> gradient(ArrayRef<Value*> grad_values) {
if (!isDifferentiable(node)) {
throw std::runtime_error(
std::string("differentiation of ") + node->kind().toDisplayString() +
" is not supported, or it is missing necessary type information");
}
// If AD is defined using torchscript, use it instead of symbolic
auto script_grads = build_script_grad(node, grad_values);
if (script_grads)
return *script_grads;
// Definition not found in torchscript, look up in the buildSymbolicGradient
// TODO: migrate all to using torchscript
auto sym_grads = buildSymbolicGradient(fmap<SymbolicVariable>(grad_values));
return fmap(sym_grads, [](const SymbolicVariable& v) { return v.value(); });
}
private:
Node* node;
SymbolicVariable gradSumToSizeOf(SymbolicVariable v, Symbol input_name) {
Value* size;
{
WithInsertPoint insert_guard{node};
size = SymbolicVariable(node->namedInput(input_name)).size();
}
return v.gradSumToSize(size);
};
std::vector<SymbolicVariable> buildSymbolicGradient(
const std::vector<SymbolicVariable>& grads) {
static const OperatorSet comparison_ops = {
"aten::lt(Tensor self, Tensor other) -> Tensor",
"aten::le(Tensor self, Tensor other) -> Tensor",
"aten::gt(Tensor self, Tensor other) -> Tensor",
"aten::ge(Tensor self, Tensor other) -> Tensor",
"aten::eq(Tensor self, Tensor other) -> Tensor",
"aten::ne(Tensor self, Tensor other) -> Tensor",
"aten::lt(Tensor self, Scalar other) -> Tensor",
"aten::le(Tensor self, Scalar other) -> Tensor",
"aten::gt(Tensor self, Scalar other) -> Tensor",
"aten::ge(Tensor self, Scalar other) -> Tensor",
"aten::eq(Tensor self, Scalar other) -> Tensor",
"aten::ne(Tensor self, Scalar other) -> Tensor",
};
auto inputs = fmap<SymbolicVariable>(node->inputs());
auto outputs = fmap<SymbolicVariable>(node->outputs());
if (node->matches(
"aten::add(Tensor self, Tensor other, *, Scalar alpha) -> Tensor")) {
return {gradSumToSizeOf(grads.at(0), attr::self),
gradSumToSizeOf(
grads.at(0) * node->namedInput(attr::alpha), attr::other),
nullptr};
} else if (
node->matches(
"aten::add(Tensor self, Scalar other, Scalar alpha) -> Tensor")) {
return {grads.at(0), nullptr, nullptr};
} else if (node->kind() == prim::AutogradAdd) {
// NB: AutogradAdds don't broadcast
return {grads.at(0), grads.at(0)};
} else if (
node->matches(
"aten::sub(Tensor self, Tensor other, *, Scalar alpha) -> Tensor")) {
return {gradSumToSizeOf(grads.at(0), attr::self),
gradSumToSizeOf(
-grads.at(0) * node->namedInput(attr::alpha), attr::other),
nullptr};
} else if (
node->matches(
"aten::sub(Tensor self, Scalar other, Scalar alpha) -> Tensor")) {
return {grads.at(0), nullptr, nullptr};
} else if (node->matches(
"aten::mul(Tensor self, Tensor other) -> Tensor")) {
return {gradSumToSizeOf(grads.at(0) * inputs.at(1), attr::self),
gradSumToSizeOf(grads.at(0) * inputs.at(0), attr::other)};
} else if (node->matches(
"aten::mul(Tensor self, Scalar other) -> Tensor")) {
return {grads.at(0) * inputs.at(1), nullptr};
} else if (node->matches(
"aten::div(Tensor self, Tensor other) -> Tensor")) {
return {gradSumToSizeOf(grads.at(0) / inputs.at(1), attr::self),
gradSumToSizeOf(
-grads.at(0) * inputs.at(0) / (inputs.at(1) * inputs.at(1)),
attr::other)};
} else if (node->matches(
"aten::div(Tensor self, Scalar other) -> Tensor")) {
return {grads.at(0) / inputs.at(1), nullptr};
} else if (node->matches(
"aten::max(Tensor self, Tensor other) -> Tensor")) {
return {
gradSumToSizeOf(
grads.at(0) * (inputs.at(0) > inputs.at(1)).type_as(grads.at(0)),
attr::self),
gradSumToSizeOf(
grads.at(0) * (inputs.at(1) > inputs.at(0)).type_as(grads.at(0)),
attr::other)};
} else if (node->matches(
"aten::min(Tensor self, Tensor other) -> Tensor")) {
return {
gradSumToSizeOf(
grads.at(0) * (inputs.at(0) < inputs.at(1)).type_as(grads.at(0)),
attr::self),
gradSumToSizeOf(
grads.at(0) * (inputs.at(1) < inputs.at(0)).type_as(grads.at(0)),
attr::other)};
} else if (
node->matches(
"aten::where(Tensor condition, Tensor self, Tensor other) -> Tensor")) {
return {nullptr,
gradSumToSizeOf(
grads.at(0) * inputs.at(0).type_as(grads.at(0)), attr::self),
gradSumToSizeOf(
grads.at(0) * (1 - inputs.at(0)).type_as(grads.at(0)),
attr::other)};
} else if (node->matches("aten::sigmoid(Tensor self) -> Tensor")) {
// TODO: The order of operations matter in this case. This
// works for ppc64le and x86_64. Need to look at why the
// order matters.
return {(1 - outputs.at(0)) * outputs.at(0) * grads.at(0)};
} else if (node->matches("aten::tanh(Tensor self) -> Tensor")) {
return {grads.at(0) * (1 - outputs.at(0) * outputs.at(0))};
} else if (node->matches("aten::relu(Tensor self) -> Tensor")) {
return {grads.at(0) *
(outputs.at(0) > at::Scalar(0)).type_as(outputs.at(0))};
} else if (
node->matches(
"aten::clamp(Tensor self, Scalar? min, Scalar? max) -> Tensor")) {
// handle the case that min/max is None
Value* min = inputs.at(1);
bool min_must_be_none = min->mustBeNone();
Value* max = inputs.at(2);
bool max_must_be_none = max->mustBeNone();
// XXX - this formula is wrong when min or max are not stricly a constant
// None but may be None dynamically. In this case an internal compiler
// error will get thrown when trying to generate expressions involving the
// values of min/max
if (!min_must_be_none && !max_must_be_none) {
return {grads.at(0) *
(1 - (inputs.at(0) <= inputs.at(1)).type_as(inputs.at(0))) *
(1 - (inputs.at(0) >= inputs.at(2)).type_as(inputs.at(0))),
nullptr,
nullptr};
} else if (max_must_be_none) {
return {grads.at(0) *
(1 - (inputs.at(0) <= inputs.at(1)).type_as(inputs.at(0))),
nullptr,
nullptr};
} else if (min_must_be_none) {
return {grads.at(0) *
(1 - (inputs.at(0) >= inputs.at(2)).type_as(inputs.at(0))),
nullptr,
nullptr};
} else {
return {grads.at(0), nullptr, nullptr};
}
} else if (
node->matches(
"aten::threshold(Tensor self, Scalar threshold, Scalar value) -> Tensor")) {
auto threshold = node->get<at::Scalar>(attr::threshold).value();
return {grads.at(0) * (inputs.at(0) > threshold).type_as(outputs.at(0)),
nullptr,
nullptr};
} else if (node->matches("aten::erf(Tensor self) -> Tensor")) {
return {grads.at(0) * 1.12837916709551 *
(-inputs.at(0) * inputs.at(0)).exp()};
} else if (node->matches("aten::erfc(Tensor self) -> Tensor")) {
return {-grads.at(0) * 1.12837916709551 *
(-inputs.at(0) * inputs.at(0)).exp()};
} else if (node->matches("aten::exp(Tensor self) -> Tensor")) {
return {grads.at(0) * (outputs.at(0))};
} else if (node->matches("aten::t(Tensor self) -> Tensor")) {
return {grads.at(0).t()};
} else if (node->matches("aten::neg(Tensor self) -> Tensor")) {
return {-grads.at(0)};
} else if (node->matches("aten::abs(Tensor self) -> Tensor")) {
return {grads.at(0) * inputs.at(0).sign()};
} else if (node->matches("aten::acos(Tensor self) -> Tensor")) {
return {grads.at(0) *
-((-inputs.at(0) * inputs.at(0) + at::Scalar(1)).rsqrt())};
} else if (node->matches("aten::asin(Tensor self) -> Tensor")) {
return {grads.at(0) *
(-inputs.at(0) * inputs.at(0) + at::Scalar(1)).rsqrt()};
} else if (node->matches("aten::atan(Tensor self) -> Tensor")) {
return {grads.at(0) / (inputs.at(0) * inputs.at(0) + at::Scalar(1))};
} else if (
node->matches(
"aten::_grad_sum_to_size(Tensor(a) self, int[] size) -> Tensor(a)")) {
Value* self_size;
{
WithInsertPoint insert_guard{node};
self_size = inputs.at(0).size();
}
return {grads.at(0).expand(self_size), nullptr};
} else if (node->matches("aten::ceil(Tensor self) -> Tensor")) {
return {SymbolicVariable::zeros_like(grads.at(0))};
} else if (node->matches("aten::cos(Tensor self) -> Tensor")) {
return {grads.at(0) * -inputs.at(0).sin()};
} else if (node->matches("aten::cosh(Tensor self) -> Tensor")) {
return {grads.at(0) * inputs.at(0).sinh()};
} else if (node->matches("aten::exp(Tensor self) -> Tensor")) {
return {grads.at(0) * outputs.at(0)};
} else if (node->matches("aten::expm1(Tensor self) -> Tensor")) {
return {grads.at(0) * (outputs.at(0) + at::Scalar(1))};
} else if (node->matches("aten::floor(Tensor self) -> Tensor")) {
return {SymbolicVariable::zeros_like(grads.at(0))};
} else if (node->matches(
"aten::fmod(Tensor self, Scalar other) -> Tensor")) {
return {grads.at(0), nullptr};
} else if (node->matches("aten::frac(Tensor self) -> Tensor")) {
return {grads.at(0)};
} else if (node->matches("aten::log(Tensor self) -> Tensor")) {
return {grads.at(0) / inputs.at(0)};
} else if (node->matches("aten::log10(Tensor self) -> Tensor")) {
return {grads.at(0) / (inputs.at(0) * 2.3025850929940456)};
} else if (node->matches("aten::log1p(Tensor self) -> Tensor")) {
return {grads.at(0) / (inputs.at(0) + at::Scalar(1))};
} else if (node->matches("aten::log2(Tensor self) -> Tensor")) {
return {grads.at(0) / (inputs.at(0) * 0.6931471805599453)};
} else if (node->matches("aten::reciprocal(Tensor self) -> Tensor")) {
return {-grads.at(0) * outputs.at(0) * outputs.at(0)};
} else if (node->matches(
"aten::remainder(Tensor self, Scalar other) -> Tensor")) {
return {grads.at(0), nullptr};
} else if (node->matches("aten::round(Tensor self) -> Tensor")) {
return {SymbolicVariable::zeros_like(grads.at(0))};
} else if (node->matches("aten::rsqrt(Tensor self) -> Tensor")) {
return {grads.at(0) * outputs.at(0).pow(3.) * -0.5};
} else if (node->matches("aten::sin(Tensor self) -> Tensor")) {
return {grads.at(0) * inputs.at(0).cos()};
} else if (node->matches("aten::sinh(Tensor self) -> Tensor")) {
return {grads.at(0) * inputs.at(0).cosh()};
} else if (node->matches("aten::tan(Tensor self) -> Tensor")) {
return {grads.at(0) * (1. + outputs.at(0) * outputs.at(0))};
} else if (node->matches("aten::trunc(Tensor self) -> Tensor")) {
return {SymbolicVariable::zeros_like(grads.at(0))};
} else if (node->kind() == prim::ConstantChunk) {
return {SymbolicVariable::cat(grads, node->i(attr::dim))};
} else if (
node->matches("aten::view(Tensor self, int[] size) -> Tensor") ||
node->matches("aten::reshape(Tensor self, int[] shape) -> Tensor")) {
// TODO: if sizes are not available statically, add an operator that
// reutrns them as a tuple
auto sizes = node->namedInput(attr::self)
->type()
->expect<CompleteTensorType>()
->sizes();
return {grads.at(0).reshape(sizes), nullptr};
} else if (node->matches(
"aten::type_as(Tensor self, Tensor other) -> Tensor")) {
return {grads.at(0).type_as(inputs.at(0)), nullptr};
} else if (node->matches("aten::rand_like(Tensor self) -> Tensor")) {
return {nullptr};
} else if (node->matches(
"aten::unsqueeze(Tensor self, int dim) -> Tensor")) {
return {grads.at(0).squeeze(node->namedInput(attr::dim)), nullptr};
} else if (
node->matches(
"aten::addmm(Tensor self, Tensor mat1, Tensor mat2, *, Scalar beta, Scalar alpha) -> Tensor")) {
return {gradSumToSizeOf(
grads.at(0) * node->namedInput(attr::beta), attr::self),
grads.at(0).mm(inputs.at(2).t()) * node->namedInput(attr::alpha),
inputs.at(1).t().mm(grads.at(0)) * node->namedInput(attr::alpha),
nullptr,
nullptr};
} else if (node->matches("aten::mm(Tensor self, Tensor mat2) -> Tensor")) {
return {grads.at(0).mm(inputs.at(1).t()),
inputs.at(0).t().mm(grads.at(0))};
} else if (
node->matches(
"aten::expand(Tensor self, int[] size, *, bool implicit) -> Tensor")) {
const auto& input_sizes = inputs.at(0).sizes();
if (input_sizes.size() == 0)
return {grads.at(0).sum(), nullptr, nullptr};
auto grad_sizes = node->get<std::vector<int64_t>>(attr::size).value();
auto grad = grads.at(0);
while (grad_sizes.size() > input_sizes.size()) {
grad = grad.sum(0, false);
grad_sizes.erase(grad_sizes.begin());
}
for (size_t i = 0; i < input_sizes.size(); ++i) {
if (input_sizes[i] == 1 && grad_sizes[i] > 1) {
grad = grad.sum(i, true);
}
}
return {grad, nullptr, nullptr};
} else if (node->matches("aten::squeeze(Tensor self) -> Tensor")) {
const auto& sizes = inputs.at(0).sizes();
std::vector<size_t> squeezed_dims;
for (size_t i = 0; i < sizes.size(); ++i) {
if (sizes[i] != 1)
continue;
squeezed_dims.push_back(i);
}
SymbolicVariable returned_grad = grads.at(0);
for (const auto& dim : squeezed_dims) {
returned_grad = returned_grad.unsqueeze(dim);
}
return {returned_grad};
} else if (node->matches(
"aten::squeeze(Tensor self, int dim) -> Tensor",
/*const_inputs=*/attr::dim)) {
int64_t dim = *node->get<int64_t>(attr::dim);
const auto& sizes = inputs.at(0).sizes();
wrapDim(dim, sizes);
if (sizes.size() == 0) {
return {grads.at(0), nullptr};
}
return {sizes.at(dim) > 1 ? grads.at(0) : grads.at(0).unsqueeze(dim),
nullptr};
} else if (comparison_ops.find(node)) {
return {nullptr, nullptr};
} else if (
node->matches(
"aten::avg_pool2d(Tensor self, int[] kernel_size, int[] stride, int[] padding, bool ceil_mode, bool count_include_pad) -> Tensor")) {
AT_ASSERT(grads.size() == 1);
auto graph = node->owningGraph();
auto backward_value = graph->insert(
aten::avg_pool2d_backward,
{grads.at(0).value(),
node->namedInput(attr::self),
node->namedInput(attr::kernel_size),
node->namedInput(attr::stride),
node->namedInput(attr::padding),
node->namedInput(attr::ceil_mode),
node->namedInput(attr::count_include_pad)});
return {backward_value->node()->output(0),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr};
} else if (
node->matches(
"aten::max_pool2d_with_indices(Tensor self, int[] kernel_size, int[] stride, int[] padding, int[] dilation, bool ceil_mode) -> (Tensor, Tensor)")) {
AT_ASSERT(grads.size() == 2);
auto graph = node->owningGraph();
auto backward_value = graph->insert(
aten::max_pool2d_with_indices_backward,
{grads.at(0).value(),
node->namedInput(attr::self),
node->namedInput(attr::kernel_size),
node->namedInput(attr::stride),
node->namedInput(attr::padding),
node->namedInput(attr::dilation),
node->namedInput(attr::ceil_mode),
outputs.at(1).value()});
return {backward_value->node()->output(0),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr};
} else if (
node->matches(
"aten::thnn_conv2d_forward(Tensor self, Tensor weight, int[] kernel_size, Tensor? bias, int[] stride, int[] padding) -> (Tensor, Tensor, Tensor)")) {
auto graph = node->owningGraph();
auto backward_value = graph->insert(
aten::thnn_conv2d_backward,
{grads.at(0).value(),
inputs.at(0).value(),
inputs.at(1).value(),
node->namedInput(attr::kernel_size),
node->namedInput(attr::stride),
node->namedInput(attr::padding),
outputs.at(1).value(),
outputs.at(2).value(),
graph->insertConstant(std::vector<bool>{true, true, true})});
// graph->insert returns a tuple automatically if multiple outputs are
// returned. So unpack them again.
Node* tuple_unpack_node =
graph->insertNode(graph->createTupleUnpack(backward_value));
auto tuple_outputs = tuple_unpack_node->outputs();
AT_ASSERT(tuple_outputs.size() == size_t(3));
return {tuple_outputs[0],
tuple_outputs[1],
nullptr,
tuple_outputs[2],
nullptr,
nullptr};
} else if (
node->matches(
"aten::native_batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps) -> (Tensor, Tensor, Tensor)")) {
auto graph = node->owningGraph();
auto backward_value = graph->insert(
aten::native_batch_norm_backward,
{grads.at(0).value(),
inputs.at(0).value(),
inputs.at(1).value(),
inputs.at(3).value(),
inputs.at(4).value(),
outputs.at(1).value(),
outputs.at(2).value(),
inputs.at(5).value(),
inputs.at(7).value(),
graph->insertConstant(std::vector<bool>{true, true, true})});
// graph->insert returns a tuple automatically if multiple outputs are
// returned. So unpack them again.
Node* tuple_unpack_node =
graph->insertNode(graph->createTupleUnpack(backward_value));
auto tuple_outputs = tuple_unpack_node->outputs();
AT_ASSERT(tuple_outputs.size() == size_t(3));
return {tuple_outputs[0],
tuple_outputs[1],
tuple_outputs[2],
nullptr,
nullptr,
nullptr,
nullptr,
nullptr};
} else if (
node->matches(
"aten::nll_loss(Tensor self, Tensor target, Tensor? weight, int reduction, int ignore_index) -> Tensor")) {
auto graph = node->owningGraph();
auto total_weight = graph->insertNode(graph->createAutogradZero());
auto weight = graph->insertNode(graph->createNone(TensorType::get()));
auto backward_value = graph->insert(
aten::nll_loss_backward,
{grads.at(0).value(),
inputs.at(0).value(),
inputs.at(1).value(),
weight->output(),
inputs.at(3).value(),
inputs.at(4).value(),
total_weight->output()});
return {backward_value->node()->output(0),
nullptr,
nullptr,
nullptr,
nullptr};
} else if (node->matches(
"aten::log_softmax(Tensor self, int dim) -> Tensor")) {
AT_ASSERT(grads.size() == 1);
auto graph = node->owningGraph();
auto backward_value = graph->insert(
aten::_log_softmax_backward_data,
{grads.at(0).value(),
outputs.at(0).value(),
node->namedInput(attr::dim),
node->namedInput(attr::self)});
return {backward_value->node()->output(0), nullptr};
} else if (
node->kind() == prim::Constant || node->kind() == prim::AutogradZero) {
return {};
}
throw std::runtime_error(
std::string("failed to differentiate `") +
node->kind().toDisplayString() + "`");
}
};
} // namespace
// If we have a function y = f(x) with jacobian J, the backwards of f is dx =
// J^t dy. Note that because the backwards always implements this matrix
// multiply, we know that it maps an input vector of zeros to an output vector
// of zero regardless of what operations it choses to do inside to actually
// implement the matrix multiply (most use some optimized form and never
// generate J^t). More generally, we know that all of the backward computations
// are linear and can use this property to do more aggressive optimizations
// later. It is ok to replace any backward function with known-zero inputs with
// something that produces known-zero outputs. This function encloses each
// know-linear backward function in a 'GradOf' sub-block so that we can perform
// optimizations using this information. In particular, specializeUndef will
// observe if all the inputs to the linear block are Undef, which the autograd
// uses to represent zeros, and then propagate the undefs to the outputs of the
// block.
static std::vector<Value*> linearGradientForNode(
Node* node,
ArrayRef<Value*> grad_values) {
auto& graph = *node->owningGraph();
// FIXME: In case forward has multi outputs, we only support one requires grad
if (needTrimGrad(node)) {
grad_values = grad_values.at(0);
}
auto linear = graph.insertNode(graph.create(prim::GradOf, {grad_values}, 0));
// to make reading gradient graphs easier, remember the name of the forward op
linear->s_(attr::name, node->kind().toDisplayString());
auto block = linear->addBlock();
WithInsertPoint guard(block);
auto results = GradientHelper(node).gradient(grad_values);
return fmap(results, [block, linear](Value* grad) -> Value* {
if (!grad)
return nullptr;
block->registerOutput(grad);
return linear->addOutput()->copyMetadata(grad);
});
}
struct ReverseDetails {
ReverseDetails(value_map&& grad_map, Block* reverse_block)
: grad_map(std::move(grad_map)), reverse_block(reverse_block) {}
value_map grad_map;
Block* reverse_block;
};
// AutogradAdd is a special addition function that handles Undef
// AutogradAdd(a, b) == a + b if defined(a) and defined(b)
// AutogradAdd(Undef, b) == b
// AutogradAdd(a, Undef) == a
// AutogradAdd(Undef, Undef) == Undef
static Value* createAutogradAdd(Value* a, Value* b) {
auto graph = a->owningGraph();
return graph->insertNode(graph->create(prim::AutogradAdd, {a, b}))->output();
}
// Before:
// - grad_desc has field f initialized to the original 0-stage graph
// After:
// - the last node of f (f->nodes().reverse()[0]) is a gradient node
// whose block has vjp inputs for all outputs that require_grad
// and vjp outputs for all primal inputs that require_grad
// - grad_desc has df_input_vjps and df_output_vjps set
// (but df_input_vjps will be modified later as well)
static ReverseDetails addReverseInline(Gradient& grad_desc) {
auto& graph = *grad_desc.f;
// note: reverse_node is intentionally not inserted to avoid
// accidentally acting on it (e.g. in elminate dead code),
// std::cout << *reverse_node << to view its state.
auto reverse_node = graph.create(prim::Reverse, 0);
auto reverse_block = reverse_node->addBlock();
WithInsertPoint guard(reverse_block);
value_map grad_map; // x -> dx mapping
const auto get_grad = [&](Value* v) -> Value* {
auto it = grad_map.find(v);
if (it == grad_map.end()) {
auto undef = graph.insertNode(graph.createAutogradZero());
std::tie(it, std::ignore) = grad_map.emplace(v, undef->output());
}
return it->second;
};
const auto set_grad = [&](Value* x, Value* dx) {
if (Value* prev_grad = grad_map[x]) {
grad_map[x] = createAutogradAdd(prev_grad, dx);
} else {
grad_map[x] = dx;
}
};
auto outputs = graph.outputs();
for (size_t i = 0, num_outputs = outputs.size(); i < num_outputs; ++i) {
Value* output = outputs[i];
if (!output->requires_grad())
continue;
Value* output_grad = reverse_block->addInput()->setType(output->type());
set_grad(output, output_grad);
grad_desc.df_input_vjps.push_back(i);
}
for (auto it = graph.nodes().rbegin(), end = graph.nodes().rend(); it != end;
++it) {
Node* node = *it;
auto inputs = node->inputs();
auto outputs = node->outputs();
if (std::all_of(outputs.begin(), outputs.end(), [](Value* v) {
return !v->requires_grad();
})) {
continue;
}
value_list grad_inputs =
linearGradientForNode(node, fmap(node->outputs(), get_grad));
LowerSimpleTuples(reverse_block);
AT_ASSERT(grad_inputs.size() == node->inputs().size());
for (size_t i = 0, num_inputs = grad_inputs.size(); i < num_inputs; ++i) {
if (!inputs[i]->requires_grad())
continue;
// NB: Not returning a gradient w.r.t. a value that requires grad is
// normal if the input is non-differentiable. This happens e.g. in the
// aten::type_as case.
if (!grad_inputs[i])
continue;
set_grad(inputs[i], grad_inputs[i]);
}
}
auto inputs = graph.inputs();
for (size_t i = 0, num_inputs = inputs.size(); i < num_inputs; ++i) {
Value* input = inputs[i];
if (!input->requires_grad())
continue;
// NB: Not having a gradient defined w.r.t. an input to the graph which
// requires grad can happen and is not an error. It might have been used
// only in non-differentiable contexts (e.g. as second input to
// aten::type_as). In that case we simply ignore it as an output, because it
// won't ever produce any meaningful values.
if (grad_map.count(input) == 0)
continue;
reverse_block->registerOutput(get_grad(input));
grad_desc.df_output_vjps.push_back(i);
}
return ReverseDetails(std::move(grad_map), reverse_block);
}
// Returns a topologically-sorted list of values produced in f, and used in its
// reverse program.
static value_list getReverseCaptures(Gradient& grad_desc) {
auto& graph = *grad_desc.f;
auto primal_block = graph.block();
value_set reverse_captures_set;
value_list reverse_captures; // Invariant: topo sorted
auto check_uses = [&](Value* v) {
for (auto use : v->uses()) {
if (use.user->owningBlock() == primal_block)
continue;
if (/* bool unseen = */ reverse_captures_set.emplace(v).second) {
reverse_captures.push_back(v);
}
}
};
for (Value* input : graph.inputs()) {
check_uses(input);
}
for (Node* node : graph.nodes()) {
for (Value* output : node->outputs())
check_uses(output);
}
return reverse_captures;
}
// Any temporary value from the primal graphs needs to be captured for later use
// in the reverse graph, to avoid costly recomputations. However, a lot of the
// nodes we have in our graphs are simply constants, which are cheap to execute
// and replicate, and so it's better to just copy them into the reverse graph,
// without polluting the output lists unnecessarily.
static void liftConstants(Gradient& grad_desc, ReverseDetails& rev_info) {
static const auto err = [](Value*) -> Value* {
throw std::runtime_error("unexpected input");
};
auto& graph = *grad_desc.f;
Block* reverse_block = rev_info.reverse_block;
for (Node* top_node : reverse_block->nodes()) {
AT_ASSERT(
top_node->kind() == prim::GradOf ||
top_node->kind() == prim::AutogradAdd ||
top_node->kind() == prim::AutogradZero);
if (top_node->kind() != prim::GradOf)
continue;
Block* grad_body = top_node->blocks().at(0);
for (Node* node : grad_body->nodes()) {
for (Value* input : node->inputs()) {
if (input->node()->kind() != prim::Constant)
continue;
if (input->node()->owningBlock() == grad_body)
continue;
Node* lifted_constant = graph.createClone(input->node(), err);
reverse_block->prependNode(lifted_constant);
node->replaceInputWith(input, lifted_constant->output());
}
}
}
}
static void deduplicateSizeCaptures(
Gradient& grad_desc,
ReverseDetails& rev_info) {
Block* primal_block = grad_desc.f->block();
const auto usedOnlyInReverse = [primal_block](Value* v) {
const auto& uses = v->uses();
return std::all_of(uses.begin(), uses.end(), [primal_block](const Use& u) {
return u.user->owningBlock() != primal_block;
});
};
auto captures = getReverseCaptures(grad_desc);
value_set capture_set(captures.begin(), captures.end());
for (Value* capture : captures) {
Node* node = capture->node();
if (!node->matches("aten::size(Tensor self) -> int[]")) {
continue;
}
if (usedOnlyInReverse(capture) && capture_set.count(node->input())) {
WithInsertPoint insert_guard{*rev_info.reverse_block->nodes().begin()};
capture->replaceAllUsesWith(SymbolicVariable(node->input()).size());
node->destroy();
}
}
}
static void eliminateDeadCode(ReverseDetails& rev_info) {