-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
gen_variable_type.py
1318 lines (1132 loc) · 58.8 KB
/
gen_variable_type.py
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
# Generates VariableType.h/cpp
#
# VariableType is a subclass of at::Type that provides the binding code
# necessary to provide a differentiable version of ATen operators. There are a
# number of different things we could mean:
#
# - Given a non-differentiable forward implementation, we might
# directly associate it with a backward implementation to make
# it differentiable. This is the common case.
#
# - Some functions don't need a backwards implementation, because
# backpropagation will never propagate beyond them. There are a
# number of different reasons why this may be the case:
#
# - The function has no differentiable inputs
# - The function's output is not differentiable
# - The function has no data dependency on its input
#
# - Some function don't need a backwards implementation because they
# are implemented as a composition of other (differentiable) ATen
# functions. These are dispatched directly to the Type superclass,
# which will in turn dispatch back to VariableType for its
# differentiable subcomponents.
#
from .utils import CodeTemplate, nested_dict, write, uninplace_api_name
from .gen_autograd import VIEW_FUNCTIONS, VIEW_FUNCTIONS_WITH_METADATA_CHANGE, \
MULTI_OUTPUT_SAFE_FUNCTIONS, RETURNS_VIEWS_OF_INPUT
from .gen_autograd_functions import uses_single_grad
# These functions we don't want to record for tracing, because we always want
# to trace their constituent parts. This is a temporary hack in lieue
# of proper scopes, where subsequent compilation passes can ask for the unfolding
# on demand. Only concrete ATen methods can be disabled this way; it will have
# NO EFFECT otherwise.
DONT_RECORD_TRACE = {
'convolution', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d',
'conv_transpose2d', 'conv_transpose3d', 'lstm_cell', 'gru_cell',
'rnn_tanh_cell', 'rnn_relu_cell', 'linear',
# FIXME: figure out a better way when we support sparse tensors in jit
'_coalesced_',
}
# These functions have their names recorded under trace renamed,
RENAME_TRACE = {
'zero': 'zeros_like', # replacing aten::zero_ with aten::zeros_like
'fill': 'full_like', # replacing aten::fill_ with aten::full_like
}
# `torch.jit.trace` have undocumented keyword argument `_force_outplace`,
# which force jit to replace functions with outplace variants (for
# example `aten::add_` becomes `aten::add`).
#
# This replacement implemented in-place with minimum modifications of
# arguments stack (as it assumes that outplace call has the same arguments
# as inplace version).
#
# However there are no such substitutions available for `aten::fill_`
# and `aten::zero_` operators, as we never implemented `aten::fill`
# and `aten::zero`. So jit tracing hack replacing `aten::zero_` with
# `aten::zeros_like` and replacing `aten::fill_` with `aten::full_like`.
#
# But as they potentially can have different arguments, we also have
# to hack into the stack and add missing ones.
#
# A possible alternative would be:
#
# - Add `aten::fill` and `aten::zero`
#
# - Or keep `aten::zeros_like` arguments aligned with `aten::zero_`
# arguments (inside of the `native_functions.yaml`)
RENAME_TRACE_ADD_ARGS = {
'fill': '''\
jit::tracer::addInputs(node, "options", c10::optional<ScalarType>());
jit::tracer::addInputs(node, "options", layout_or_default(c10::nullopt));
jit::tracer::addInputs(node, "options", device_or_default(c10::nullopt));
jit::tracer::addInputs(node, "options", pinned_memory_or_default(c10::nullopt));
c10::optional<MemoryFormat> memory_format = c10::MemoryFormat::Preserve;
jit::tracer::addInputs(node, "memory_format", memory_format);
''',
'zero': '''\
jit::tracer::addInputs(node, "options", c10::optional<ScalarType>());
jit::tracer::addInputs(node, "options", layout_or_default(c10::nullopt));
jit::tracer::addInputs(node, "options", device_or_default(c10::nullopt));
jit::tracer::addInputs(node, "options", pinned_memory_or_default(c10::nullopt));
c10::optional<MemoryFormat> memory_format = c10::MemoryFormat::Preserve;
jit::tracer::addInputs(node, "memory_format", memory_format);
''',
}
# (declaration name, argument name) -> attribute name
RENAME_ATTRIBUTES = {
('fill_', 'value'): 'fill_value'
}
# These functions are not worth profiling because they are very cheap and may
# be called very often.
DONT_PROFILE = {
'data_ptr', 'get_device', 'is_contiguous', 'is_cuda', 'is_distributed',
'is_same_size', 'is_set_to', 'is_signed', 'is_sparse', 'numel',
'size', 'storage_offset', 'stride',
}
# Note [Manual Backend kernels]
# For these ops, we want to manually register to dispatch key Backend and
# skip codegen-ed registeration to all keys before Backend.
# For codegen this means:
# - op set below must match ops with manual_kernel_registration=True in native_functions.yaml
# where we skip codegen backend kernels
# - all ops below are part of MANUAL_AUTOGRAD to skip codegen Autograd kernel registration
# - all ops below are part of MANUAL_TRACER to skip codegen Tracer kernel registration
# Note: we still register to dispatch key Profiler for these ops, keeping it untouched for now.
# You can find the manual registration in torch/csrc/autograd/VariableTypeManual.cpp
MANUAL_BACKEND = set([
'options', 'data', 'set_data', 'is_leaf', 'output_nr', '_version', 'retain_grad',
'backward', 'requires_grad_',
])
# For these ops we want to skip the codegen-ed registration to both Autograd and Tracer keys.
# You can find the manual registration in torch/csrc/autograd/VariableTypeManual.cpp
MANUAL_AUTOGRAD_AND_TRACER = set([
'resize_', 'resize_as_', 'detach', 'detach_', 'copy_',
])
# Currently MANUAL_AUTOGRAD and MANUAL_TRACER share the same set of ops:
# union(MANUAL_BACKEND, MANUAL_AUTOGRAD_AND_TRACER)
# You can find the manual registration in torch/csrc/autograd/VariableTypeManual.cpp
MANUAL_AUTOGRAD = MANUAL_TRACER = MANUAL_BACKEND | MANUAL_AUTOGRAD_AND_TRACER
# We don't set or modify grad_fn on these methods. Generally, they return
# tensors that have requires_grad=False. In-place functions listed here will
# not examine or modify requires_grad or grad_fn.
DONT_REQUIRE_DERIVATIVE = {
# These only depend on the input Tensor's shape and device, not the data
'ones_like', 'zeros_like', 'rand_like', 'randn_like',
# These are only implemented on integral types
'__and__', '__iand__', '__ilshift__', '__ior__', '__irshift__', '__ixor__',
'__lshift__', '__or__', '__rshift__', '__xor__',
# These work on integral data types, and hence don't require derivative
'_sobol_engine_draw', '_sobol_engine_ff', '_sobol_engine_scramble_',
'_sobol_engine_initialize_state_',
# This is an unsafe method that is meant to be out of reach of autograd.
'_coalesced_',
# Quantize functions should not record gradients
'quantize_per_tensor', 'quantize_per_channel',
# Functions that return integers should not have output that require gradients
'argmax', 'argmin', 'argsort', 'searchsorted',
'bucketize',
# Functions that return booleans are not differentiable
'isnan', 'isposinf', 'isneginf', 'isinf'
# Functions return none are not differentiable
'record_stream',
}
# The C -> R functions at the time of adding this are still being audited and tested
# but will not error out.
# C -> C, R -> C functions for which backward is correctly implemented and tested
GRADIENT_IMPLEMENTED_FOR_COMPLEX = {
't', 'view', 'reshape', 'reshape_as', 'view_as', 'roll', 'clone',
'repeat', 'expand', 'flip', 'fliplr', 'flipud', 'rot90', 'transpose',
'permute', 'squeeze', 'unsqueeze', 'resize', 'resize_as', 'tril', 'triu',
'chunk', 'split', 'split_with_sizes', 'repeat', 'expand', 'zero_', 'eq_',
'ne_', 'add', '__radd__', 'sum', '_conj', 'sin', 'cos', 'mul', 'sinh',
'cosh', '__rmul__', 'sgn', 'asin', 'acos', 'sub', 'div', 'cat', 'view_as_complex',
'neg', 'complex', 'select', '_s_where', 'as_strided', 'slice', 'constant_pad_nd',
'unbind', 'split', 'split_with_sizes', 'unsafe_split', 'split_with_sizes_backward',
'dot', 'vdot', 'cholesky', 'triangular_solve', 'mm', '_unsafe_view', 'mv', 'ger',
'bmm', 'diagonal', 'cholesky', 'atan', 'log', 'log10', 'log1p', 'log2', 'reciprocal',
'tan', 'pow', 'rsqrt', 'tanh', 'tanh_backward', 'asinh', 'acosh', 'take', 'fill_',
'exp'
}
# Some operators invalidate the grad_accumulator. Let's reset it.
RESET_GRAD_ACCUMULATOR = {
'set', 'resize'
}
# NOTE [ Invariant: TensorImpl and Storage Pointer Equality ]
#
# When a function modifies its input tensors (via inplace or out-variants),
# it should never change the the input tensors' underlying c10::TensorImpl pointers
# or c10::Storage pointers.
#
# The following code templates implement the checks for this invariant:
SAVE_TENSOR_STORAGE = CodeTemplate("""\
c10::optional<Storage> ${tensor_name}_storage_saved =
${tensor_name}.has_storage() ? c10::optional<Storage>(${tensor_name}.storage()) : c10::nullopt;
""")
ENFORCE_SAME_TENSOR_STORAGE = CodeTemplate("""\
if (${tensor_name}_storage_saved.has_value())
AT_ASSERT(${tensor_name}_storage_saved.value().is_alias_of(${tensor_name}.storage()));
""")
SAVE_TENSORLIST_STORAGE = CodeTemplate("""\
std::vector<c10::optional<Storage>> ${tensorlist_name}_storage_saved(${tensorlist_name}.size());
for (const Tensor& tensor : ${tensorlist_name})
${tensorlist_name}_storage_saved.push_back(
tensor.has_storage() ? c10::optional<Storage>(tensor.storage()) : c10::nullopt);
""")
ENFORCE_SAME_TENSORLIST_STORAGE = CodeTemplate("""\
for (size_t i=0; i<${tensorlist_name}.size(); i++) {
if (${tensorlist_name}_storage_saved[i].has_value())
AT_ASSERT(${tensorlist_name}_storage_saved[i].value().is_alias_of(${tensorlist_name}[i].storage()));
}
""")
SAVE_TENSOR_IMPL = CodeTemplate("""\
c10::intrusive_ptr<TensorImpl> ${tensor_name}_impl_saved;
if (${tensor_name}.defined()) ${tensor_name}_impl_saved = ${tensor_name}.getIntrusivePtr();
""")
ENFORCE_SAME_TENSOR_IMPL = CodeTemplate("""\
if (${tensor_name}_impl_saved) AT_ASSERT(${tensor_name}_impl_saved == ${tensor_name}.getIntrusivePtr());
""")
SAVE_TENSORLIST_IMPL = CodeTemplate("""\
std::vector<c10::intrusive_ptr<TensorImpl>> ${tensorlist_name}_impl_saved(${tensorlist_name}.size());
for (size_t i=0; i<${tensorlist_name}.size(); i++)
if (${tensorlist_name}[i].defined()) ${tensorlist_name}_impl_saved[i] = ${tensorlist_name}[i].getIntrusivePtr();
""")
ENFORCE_SAME_TENSORLIST_IMPL = CodeTemplate("""\
for (size_t i=0; i<${tensorlist_name}.size(); i++) {
if (${tensorlist_name}_impl_saved[i])
AT_ASSERT(${tensorlist_name}_impl_saved[i] == ${tensorlist_name}[i].getIntrusivePtr());
}
""")
# The following list contains functions that we don't enforce the invariant on.
DONT_ENFORCE_SAME_TENSOR_IMPL_OR_STORAGE = {
# These functions are expected to change impl or storage of input tensors
'set_', '_cudnn_rnn_flatten_weight',
}
# END CHECKS FOR [ Invariant: TensorImpl and Storage Pointer Equality ]
METHOD_DECLARATION = CodeTemplate("""\
${return_type} ${type_wrapper_name}(${formals}) ;
""")
METHOD_DEFINITION = CodeTemplate("""\
${return_type} ${type_wrapper_name}(${formals}) {
${type_definition_body}
}
""")
# NOTE[UnboxedOnly] Many of our codegen templates currently exist twice, once
# in an _UNBOXEDONLY_ variant and once without _UNBOXEDONLY_. This is because
# ops that are `use_c10_dispatcher: full` need different c++ code than ops
# that aren't `use_c10_dispatcher: full` yet. The _UNBOXEDONLY_ variants
# are for ops that aren't `use_c10_dispatcher: full` yet and those code templates
# can be deleted once all ops are `use_c10_dispatcher: full`.
# If you update one of the templates, you likely also have to update the other.
# See NOTE[UnboxedOnly]
UNBOXEDONLY_WRAPPER_REGISTRATION = CodeTemplate("""\
m.impl_UNBOXED("${unqual_operator_name_with_overload}", &${class_type}::${type_wrapper_name});
""")
WRAPPER_REGISTRATION = CodeTemplate("""\
m.impl("${unqual_operator_name_with_overload}",
TORCH_FN(${class_type}::${type_wrapper_name})
);
""")
UNPACK_TENSOR = CodeTemplate("""\
auto${ref} ${arg_name}_ = unpack${suffix}(${arg_name}, "${arg_name}", ${arg_pos});""")
LEGACY_WRAP_OPTIONS = CodeTemplate("""\
auto ${arg_name}_ = TensorOptions(${arg_name});""")
DECLARE_GRAD_FN = CodeTemplate("""\
std::shared_ptr<${op}> grad_fn;
""")
SETUP_DERIVATIVE = CodeTemplate("""\
if (compute_requires_grad( ${args_with_derivatives} )) {
${setup}
}
""")
ASSIGN_GRAD_FN = CodeTemplate("""\
grad_fn = std::shared_ptr<${op}>(new ${op}(${op_ctor}), deleteNode);
grad_fn->set_next_edges(collect_next_edges( ${args_with_derivatives} ));
""")
CALL_DISPATCH_VIA_NAMESPACE = CodeTemplate("""\
at::${api_name}(${unpacked_args})""")
CALL_DISPATCH_VIA_METHOD = CodeTemplate("""\
${var}.${api_name}(${unpacked_method_args})""")
# If the non-variable operation has return values, we use the `tmp` variable to hold the
# values temporarily and pass the values to the return variables outside of the
# `at::AutoNonVariableTypeMode` guard block.
DISPATCH_TO_NON_VAR_TYPE_WITH_TMP_RETURN_VALUES = CodeTemplate("""\
auto tmp = ([&]() {
at::AutoNonVariableTypeMode non_var_type_mode(true);
return ${base_type_call};
})();
""")
ASSIGN_RETURN_VALUE = CodeTemplate("""\
${return_values} = ${rhs_value};
""")
ARRAYREF_TO_VEC = CodeTemplate("""\
auto ${vec} = ${arg}.vec();
""")
OPTIONAL_TO_VAL = CodeTemplate("""\
auto ${val} = ${arg}.value_or(${default});
""")
SETUP_REPLAY_VIEW_IF_NOT_SUPPORT_AS_STRIDED_OR_VIEW_WITH_METADATA_CHANGE = CodeTemplate("""\
c10::optional<std::function<at::Tensor(const at::Tensor&)>> func=c10::nullopt;
if (${is_view_with_metadata_change} || !self.unsafeGetTensorImpl()->support_as_strided()) {
${replay_view_func}
}
""")
REPLAY_VIEW_LAMBDA_FUNC = CodeTemplate("""\
func = [=](const at::Tensor& ${input_base}) {
return ${replay_view_call};
};
""")
DISPATCH_TO_NON_VAR_TYPE_WITHOUT_RETURN_VALUES = CodeTemplate("""\
{
at::AutoNonVariableTypeMode non_var_type_mode(true);
${base_type_call};
}
""")
SET_HISTORY = CodeTemplate("""\
if (grad_fn) {
${fn}_history(${differentiable_outputs}, grad_fn);
}
""")
CONDITIONAL = CodeTemplate("""\
if (${cond}) {
${statements}
}
""")
SELECT = CodeTemplate("""\
if (${cond}) {
${true}
} else {
${false}
}
""")
OP_NAME = CodeTemplate("""\
op_name = jit::Symbol::fromQualString("aten::${trace_name}");
""")
PRE_RECORD_TRACE = CodeTemplate("""\
torch::jit::Node* node = nullptr;
std::shared_ptr<jit::tracer::TracingState> tracer_state;
if (jit::tracer::isTracing()) {
tracer_state = jit::tracer::getTracingState();
at::Symbol op_name;
${set_op_name}
node = tracer_state->graph->create(op_name, /*num_outputs=*/0);
jit::tracer::recordSourceLocation(node);
${add_trace_inputs}
tracer_state->graph->insertNode(node);
${inplace_guard}
jit::tracer::setTracingState(nullptr);
}
""")
INPLACE_GUARD = CodeTemplate("""\
jit::tracer::ensureUniqueIfOutOfPlaced("${name}", ${mutable_input});
""")
ADD_TRACE_INPUT = CodeTemplate("""jit::tracer::addInputs(node, "${name}", ${input});""")
POST_RECORD_TRACE = CodeTemplate("""\
if (tracer_state) {
jit::tracer::setTracingState(std::move(tracer_state));
${add_trace_outputs}
}
""")
RUN_ONLY_IN_DEBUG_MODE = CodeTemplate("""\
#ifndef NDEBUG
${statements}
#endif
""")
# TraceType templates
# TODO: change `redispatch` to `NoTracerDispatchMode` + regular `call`.
# See NOTE[UnboxedOnly]
UNBOXED_TRACE_DISPATCH = CodeTemplate("""\
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("aten::${operator_name}", "${overload_name}")
.typed<${return_type} (${arg_types})>();
${assign_return_values}c10::Dispatcher::singleton().redispatch<${ret_and_arg_types}>(${trace_dispatch_args});
""")
TRACE_DISPATCH = CodeTemplate("""\
static auto op = c10::Dispatcher::singleton()
.findSchemaOrThrow("aten::${operator_name}", "${overload_name}")
.typed<${return_type} (${schema_order_arg_types})>();
${assign_return_values}c10::Dispatcher::singleton()
.redispatch<${schema_order_ret_and_arg_types}>(${schema_order_trace_dispatch_args});
""")
FACTORY_FUNCTION_NAMES = None
# TODO The maybe_unwrap_optional_tensors is only needed because our at::native::xxx functions
# still take "Tensor" instead of "optional<Tensor>", so we need CPUType, TypeDefault, ...
# to do the same. Once at::native::xxx are converted, we can remove use_optional_tensor
# and use the use_optional_tensor=True behavior always.
def maybe_unwrap_optional_tensors(option, formals, args):
assert len(formals) == len(args), \
"Assert we didn't screw up with method_args removing self but forgetting to remove it from formals"
if option['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
def maybe_unwrap_optional_tensor(formal, arg):
if formal['dynamic_type'] == 'Tensor' and formal['is_nullable']:
return "{}.has_value() ? *{} : at::Tensor()".format(arg, arg)
else:
return arg
return [maybe_unwrap_optional_tensor(formal, arg) for (formal, arg) in zip(formals, args)]
else:
assert option['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
return args
def find_factory_functions(declarations):
global FACTORY_FUNCTION_NAMES
FACTORY_FUNCTION_NAMES = set()
for declaration in declarations:
if declaration['is_factory_method']:
FACTORY_FUNCTION_NAMES.add(declaration['api_name'])
def should_trace(declaration):
# Operations involving Storage or Type are not traceable at the moment
if any(arg['simple_type'] in {'Storage', 'Type', 'ConstQuantizerPtr'} for arg in declaration['arguments']):
return False
# We can't trace functions which don't have any Tensor or TensorList returns
if 'Tensor' not in declaration['return_type']:
return False
name = declaration['name']
base_name = name[:-1] if declaration['inplace'] else name[:-4] if name.endswith('_out') else name
if base_name in DONT_RECORD_TRACE or name in DONT_RECORD_TRACE:
return False
return True
def is_out_overload(declaration):
return declaration['api_name'].endswith('_out')
def format_postrecord_trace(declaration):
# For outplacing ops, *_out overloads require special handling to move the
# output *argument* to a return value
if is_out_overload(declaration):
output_names_outplace = [arg['name'] for arg in declaration['arguments'] if arg.get('output', False)]
output_names_inplace = [r['name'] for r in declaration['returns']]
# Code size optimization: the common case is that the return value is
# the same for both variants
if output_names_outplace == output_names_inplace:
outputs = ['jit::tracer::addOutput(node, {});'.format(n) for n in output_names_outplace]
return POST_RECORD_TRACE.substitute(add_trace_outputs=outputs)
local = {}
local['cond'] = 'force_outplace'
local['true'] = ['jit::tracer::addOutput(node, {});'.format(n) for n in output_names_outplace]
local['false'] = ['jit::tracer::addOutput(node, {});'.format(n) for n in output_names_inplace]
selection = SELECT.substitute(local)
return POST_RECORD_TRACE.substitute(add_trace_outputs=selection)
output_names = [r['name'] for r in declaration['returns']]
outputs = ['jit::tracer::addOutput(node, {});'.format(n) for n in output_names]
return POST_RECORD_TRACE.substitute(add_trace_outputs=outputs)
def format_trace_op_name(declaration):
is_inplace = declaration['api_name'] != uninplace_api_name(declaration['api_name'])
if not is_inplace or is_out_overload(declaration):
# special case for *_out functions: the in-place and out-of-place ops
# are overloaded with the same name in the JIT
trace_name = uninplace_api_name(declaration['api_name'])
trace_name = RENAME_TRACE.get(trace_name, trace_name)
return OP_NAME.substitute(trace_name=trace_name)
# otherwise, this is an in-place op and we need to emit both in- and
# out-of-place versions
outplace_trace_name = uninplace_api_name(declaration['api_name'])
inplace_trace_name = declaration['api_name']
outplace_trace_name = RENAME_TRACE.get(outplace_trace_name, outplace_trace_name)
inplace_trace_name = RENAME_TRACE.get(inplace_trace_name, inplace_trace_name)
select_params = {}
select_params['cond'] = 'tracer_state->force_outplace'
select_params['true'] = OP_NAME.substitute(trace_name=outplace_trace_name)
select_params['false'] = OP_NAME.substitute(trace_name=inplace_trace_name)
return SELECT.substitute(select_params)
def format_trace_inputs(declaration):
gather_tensor_options = "TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory)"
def dispatch_trace_input(arg_spec):
name, value, simple_type, nullable = arg_spec
# XXX: For arg that have type of Tensor?[], tracer will pass allow_undefined to addInputs
if simple_type == 'TensorList' and nullable:
return '''jit::tracer::addInputs(node, "{}", {}, {});'''.format(name, value, "true")
else:
if value == "options":
result = ""
result += ADD_TRACE_INPUT.substitute(name=name, input="optTypeMetaToScalarType(options.dtype_opt())") + "\n"
result += ADD_TRACE_INPUT.substitute(name=name, input="options.layout()") + "\n"
result += ADD_TRACE_INPUT.substitute(name=name, input="options.device()") + "\n"
result += ADD_TRACE_INPUT.substitute(name=name, input="options.pinned_memory()")
return result
else:
return ADD_TRACE_INPUT.substitute(name=name, input=value)
if declaration['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
trace_inputs = declaration['schema_order_arguments']
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
trace_inputs = declaration['arguments']
if is_out_overload(declaration):
# *_out functions take the result as a first argument, but they are the
# last argument in the JIT schema.
out_input = trace_inputs[0]
trace_inputs = trace_inputs[1:]
if declaration['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
trace_input_spec = [(i['name'], i['name'], i['type'], i.get('is_nullable')) for i in trace_inputs]
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
trace_input_spec = [(i['name'], i['name'], i['simple_type'], i.get('is_nullable')) for i in trace_inputs]
trace_inputs = \
'\n'.join(dispatch_trace_input(arg_spec) for arg_spec in trace_input_spec)
if is_out_overload(declaration):
# for *_out functions, handle the result argument differently for inplace/outplace.
# For inplace: just add the input to the end to confirm with the JIT schema
value = out_input['name']
inplace = ADD_TRACE_INPUT.substitute(name=out_input['name'], input=value)
# for outplace: do nothing, except if the declaration is a factory.
# Factories are a bit special because their out-of-place overloads
# take an extra TensorOptions argument, which is missing in the _out function
trace_name = uninplace_api_name(declaration['api_name'])
has_factory_name = trace_name in FACTORY_FUNCTION_NAMES
if has_factory_name:
outplace = ""
outplace += ADD_TRACE_INPUT.substitute(name='out', input='optTypeMetaToScalarType(out.options().dtype_opt())') + "\n"
outplace += ADD_TRACE_INPUT.substitute(name='out', input='out.options().layout()') + "\n"
outplace += ADD_TRACE_INPUT.substitute(name='out', input='out.options().device()') + "\n"
outplace += ADD_TRACE_INPUT.substitute(name='out', input='out.options().pinned_memory()')
else:
outplace = ''
trace_inputs += '\n'
trace_inputs += SELECT.substitute(
cond='tracer_state->force_outplace', true=outplace, false=inplace)
return trace_inputs
def format_prerecord_trace(declaration):
local = {}
is_inplace = declaration['api_name'] != uninplace_api_name(declaration['api_name'])
local['set_op_name'] = format_trace_op_name(declaration)
is_inplace = declaration['api_name'] != uninplace_api_name(declaration['api_name'])
add_args = ''
if is_inplace:
api_name = uninplace_api_name(declaration['api_name'])
add_args = RENAME_TRACE_ADD_ARGS.get(api_name, '')
if add_args:
select_params = {}
select_params['cond'] = 'tracer_state->force_outplace'
select_params['true'] = add_args
select_params['false'] = ''
additional_inputs = SELECT.substitute(select_params)
else:
additional_inputs = ''
local['add_trace_inputs'] = format_trace_inputs(declaration) + additional_inputs
local['inplace_guard'] = ''
if is_inplace:
local['inplace_guard'] = INPLACE_GUARD.substitute(
name=declaration['api_name'],
mutable_input=declaration['arguments'][0]['name'])
return PRE_RECORD_TRACE.substitute(local)
def format_trace(declaration):
if not should_trace(declaration):
return ('', '')
return (format_prerecord_trace(declaration), format_postrecord_trace(declaration))
# Methods shared by TraceType and VariableType to handle return variable declaration, tie and tuple.
def format_return_variables(declaration):
name = declaration['name']
arguments = declaration['arguments']
inplace = declaration['inplace']
is_out_fn = name.endswith('_out')
modifies_arguments = inplace or is_out_fn
def declare_returned_variables():
if modifies_arguments:
return ''
if len(declaration['returns']) == 1:
return ''
# TODO: this will be ugly
names = [ret['type'] + ' ' + ret['name'] + ';' for ret in declaration['returns']]
return '\n'.join(names)
def tie_return_values():
if len(declaration['returns']) == 1:
return 'auto {}'.format(declaration['returns'][0]['name'])
names = [ret['name'] for ret in declaration['returns']]
return 'std::tie({})'.format(', '.join(names))
def get_return_value():
if inplace:
return 'self'
if is_out_fn:
return_names = [arg['name'] for arg in arguments
if arg.get('output', False)]
if len(return_names) == 1:
return return_names[0]
return 'std::forward_as_tuple({})'.format(', '.join(return_names))
returns = declaration['returns']
if len(returns) == 1:
return returns[0]['name']
moved = ['std::move({})'.format(r['name']) for r in returns]
return 'std::make_tuple({})'.format(', '.join(moved))
return (declare_returned_variables(), tie_return_values(), get_return_value())
def gen_variable_type(out, aten_declarations, template_path):
"""VariableType.h and VariableType.cpp body
This is the at::Type subclass for differentiable tensors. The
implementation of each function dispatches to the base tensor type to
compute the output. The grad_fn is attached to differentiable functions.
"""
# WARNING: this function call modifies global mutable state
find_factory_functions(aten_declarations)
aten_declarations = list(sorted(aten_declarations, key=lambda decl: decl['name']))
gen_variable_type_shard(out, aten_declarations, template_path, None, True)
# NOTE: see Note [Sharded File] at the top of the VariableType.cpp
# template regarding sharding of the generated files.
num_shards = 5
shards = [[] for _ in range(num_shards)]
# functions are assigned arbitrarily but stably to a file based on hash
for decl in aten_declarations:
x = sum(ord(c) for c in decl['name']) % num_shards
shards[x].append(decl)
for i, shard in enumerate(shards):
gen_variable_type_shard(out, shard, template_path, '_%d' % i, False)
gen_variable_type_shard(out, aten_declarations, template_path, 'Everything', False)
def gen_variable_type_shard(out, aten_declarations, template_path, suffix, header):
VARIABLE_TYPE_H = CodeTemplate.from_file(template_path + '/VariableType.h')
VARIABLE_TYPE_CPP = CodeTemplate.from_file(template_path + '/VariableType.cpp')
TRACE_TYPE_CPP = CodeTemplate.from_file(template_path + '/TraceType.cpp')
type_declarations = []
type_definitions = []
wrapper_registrations = []
trace_method_definitions = []
trace_wrapper_registrations = []
for declaration in aten_declarations:
formal_types = [arg['type'] for arg in declaration['arguments']]
if declaration['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
formals = declaration['schema_order_formals']
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
formals = declaration['formals']
type_declarations.append(METHOD_DECLARATION.substitute(declaration, formals=formals))
strategy = dispatch_strategy(declaration)
if declaration['name'] not in MANUAL_AUTOGRAD and strategy == 'use_derived':
body = emit_body(declaration)
type_definitions.append(METHOD_DEFINITION.substitute(
declaration, type_definition_body=body, formals=formals))
if declaration['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
wrapper_registrations.append(WRAPPER_REGISTRATION.substitute(
declaration, class_type='VariableType'))
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
wrapper_registrations.append(UNBOXEDONLY_WRAPPER_REGISTRATION.substitute(
declaration, class_type='VariableType'))
# See Note [Manual Backend kernels]
assert (declaration['name'] in MANUAL_BACKEND) == declaration['manual_kernel_registration']
# If you want to register a kernel to Autograd, you must make the op abstract.
# In other words, this op must have dispatch section in native_functions.yaml.
if declaration['name'] in MANUAL_AUTOGRAD_AND_TRACER or declaration['derivative']:
msg = (f'There\'s a formula for {declaration["name"]}(or its functional variant) in derivatives.yaml. '
f'It\'s required to add a dispatch section for it with explicit supported backends e.g CPU/CUDA '
f'or DefaultBackend in native_functions.yaml. Please see '
f'https://github.com/pytorch/pytorch/tree/master/aten/src/ATen/native#choosing-the-right-dispatch-keyword '
f'for instructions to choose the right dispatch keyword.')
assert declaration['abstract'], msg
# Emit TraceType code
if declaration['name'] not in MANUAL_TRACER:
trace_body = emit_trace_body(declaration)
trace_method_definitions.append(METHOD_DEFINITION.substitute(
declaration, type_definition_body=trace_body, formals=formals))
if declaration['use_c10_dispatcher'] in ['full', 'hacky_wrapper_for_legacy_signatures']:
trace_wrapper_registrations.append(WRAPPER_REGISTRATION.substitute(
declaration, class_type='TraceType'))
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
trace_wrapper_registrations.append(UNBOXEDONLY_WRAPPER_REGISTRATION.substitute(
declaration, class_type='TraceType'))
env = {
'type_derived_method_declarations': type_declarations,
'type_derived_method_definitions': type_definitions,
'wrapper_registrations': wrapper_registrations,
'trace_method_definitions': trace_method_definitions,
'trace_wrapper_registrations': trace_wrapper_registrations,
}
if header:
write(out, 'VariableType.h', VARIABLE_TYPE_H, env)
else:
write(out, 'VariableType%s.cpp' % suffix, VARIABLE_TYPE_CPP, env)
write(out, 'TraceType%s.cpp' % suffix, TRACE_TYPE_CPP, env)
def emit_trace_body(declaration):
returns = declaration['returns']
name = declaration['name']
inplace = declaration['inplace']
is_out_fn = name.endswith('_out')
modifies_arguments = inplace or is_out_fn
returns_void = len(returns) == 0
trace_body = []
pre_record_trace, post_record_trace = format_trace(declaration)
declare_returned_variables, tie_return_values, get_return_value = format_return_variables(declaration)
trace_body.append(pre_record_trace)
trace_body.append(declare_returned_variables)
arg_types = ', '.join([a['type'] for a in declaration['arguments']])
ret_and_arg_types = ', '.join([declaration['return_type']] + [a['type'] for a in declaration['arguments']])
schema_order_arg_types = ', '.join([a['type'] for a in declaration['schema_order_arguments']])
schema_order_ret_and_arg_types = ', '.join(
[declaration['return_type']] + [a['type'] for a in declaration['schema_order_arguments']])
trace_dispatch_args = ['op', 'c10::DispatchKey::Tracer'] + declaration['args']
schema_order_trace_dispatch_args = ['op', 'c10::DispatchKey::Tracer'] + declaration['schema_order_args']
assign_return_values = '{} = '.format(tie_return_values) if not modifies_arguments and not returns_void else ''
if declaration['use_c10_dispatcher'] in ['hacky_wrapper_for_legacy_signatures', 'full']:
call = TRACE_DISPATCH.substitute(
declaration,
schema_order_arg_types=schema_order_arg_types,
assign_return_values=assign_return_values,
schema_order_ret_and_arg_types=schema_order_ret_and_arg_types,
schema_order_trace_dispatch_args=schema_order_trace_dispatch_args,
)
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
call = UNBOXED_TRACE_DISPATCH.substitute(
declaration,
arg_types=arg_types,
ret_and_arg_types=ret_and_arg_types,
trace_dispatch_args=trace_dispatch_args,
assign_return_values=assign_return_values,
)
trace_body.append(call)
trace_body.append(post_record_trace)
if not returns_void:
trace_body.append('return {};'.format(get_return_value))
return trace_body
def emit_body(declaration):
assert dispatch_strategy(declaration) == 'use_derived'
arguments = declaration['arguments']
returns = declaration['returns']
func = declaration['derivative']
name = declaration['name']
inplace = declaration['inplace']
is_out_fn = name.endswith('_out')
modifies_arguments = inplace or is_out_fn
returns_void = len(returns) == 0
base_name = name[:-1] if inplace else name[:-4] if is_out_fn else name
view_info = VIEW_FUNCTIONS.get(base_name, None)
if view_info is None and base_name in RETURNS_VIEWS_OF_INPUT:
view_info = "self"
def is_differentiable(arg):
if 'TensorOptions' in arg['type']:
return False
if 'Tensor' not in arg['type']:
return False
if arg['name'] in declaration.get('non_differentiable_arg_names', []):
return False
return True
def find_args_with_derivatives(differentiable_inputs):
"""Find arguments that have derivative definitions"""
if func is None:
return differentiable_inputs
names = set(name for d in func['derivatives'] for name in d['var_names'])
differentiable = [arg for arg in differentiable_inputs if arg['name'] in names]
if len(differentiable) != len(names):
missing = names - set(arg['name'] for arg in differentiable)
raise RuntimeError('Missing arguments for derivatives: {} in {}'.format(missing, func['name']))
return differentiable
inputs = [arg for arg in arguments if not arg.get('output', False)]
differentiable_inputs = list(filter(is_differentiable, inputs))
args_with_derivatives = find_args_with_derivatives(differentiable_inputs)
non_differentiable_arg_names = declaration.get('non_differentiable_arg_names', [])
candidate_differentiable_outputs = list(filter(is_differentiable, returns))
if declaration['output_differentiability'] is not None:
differentiable_outputs = []
output_differentiability = declaration['output_differentiability']
if False in output_differentiability and inplace:
raise RuntimeError("output_differentiability=False for inplace operation (version_counter won't get updated)")
for differentiable, output in zip(output_differentiability, returns):
if differentiable:
differentiable_outputs.append(output)
elif uses_single_grad(func):
differentiable_outputs = candidate_differentiable_outputs[:1]
else:
differentiable_outputs = candidate_differentiable_outputs
requires_derivative = (
base_name not in DONT_REQUIRE_DERIVATIVE and name not in DONT_REQUIRE_DERIVATIVE and
len(differentiable_inputs) > 0 and len(differentiable_outputs) > 0)
if func is not None and not requires_derivative:
raise RuntimeError('ERROR: derivative ignored for {} -- specified an autograd function without derivative'
.format(name))
def emit_save_inputs():
setup = []
if func is None:
return setup
has_tensorlist_arg = any(arg['type'] == 'TensorList' for arg in func['args_with_derivatives'])
# We don't want to save tensors if we know that they will never be used
# when computing the derivative, so we add guards to those statements
def guard_for(arg):
# It's hard to determine the edge offset if we have TensorLists
if has_tensorlist_arg:
return None
# Empirical evaluation of the cases where we insert those guards in
# backward show that they are somewhat useless. E.g. there's no need
# to guard on some values captured from forward, because they had to
# require_grad if the backward function even gets executed. I don't
# have any good ideas for detecting those cases, so I simply disabled the
# checks.
if 'backward' in func['name']:
return None
# If there's a single derivative we could compute, we already have
# a requires_grad check that is sufficient
if len(func['args_with_derivatives']) <= 1:
return None
# We really only care about trimming down the amount of tensors we save
if arg['type'] != 'Tensor':
return None
# We want to emit simple guards, so we only allow that if checking one
# input is enough to determine whether we need that value
used_in = [d for d in func['derivatives'] if arg in d['saved_inputs']]
assert len(used_in) > 0
if len(used_in) != 1:
return None
derivative = used_in[0]
if len(derivative['var_names']) != 1:
return None
derivative_var_name = derivative['var_names'][0]
# Figure out the offset of the edge that uses this variable
for edge_off, arg in enumerate(func['args_with_derivatives']):
if arg['name'] == derivative_var_name:
break
else:
raise AssertionError()
return 'grad_fn->should_compute_output({})'.format(edge_off)
setup.extend(save_variables(func['saved_inputs'], False, guard_for))
for arg in func['args_with_derivatives']:
if arg['type'] == 'TensorList':
setup.append("grad_fn->{}_size_ = {}.size();".format(arg['name'], arg['name']))
return setup
def setup_derivative(differentiable_inputs):
env = {}
env['args_with_derivatives'] = [arg['name'] for arg in args_with_derivatives]
env['op'] = func['op'] if func is not None else 'NotImplemented'
env['op_ctor'] = '' if func is not None else '"{}"'.format(declaration['api_name'])
if is_out_fn:
setup = ['throw_error_out_requires_grad("{}");'.format(base_name)]
body = []
body.append(DECLARE_GRAD_FN.substitute(op='Node'))
body.append(SETUP_DERIVATIVE.substitute(
setup=setup,
args_with_derivatives=[arg['name'] for arg in differentiable_inputs]))
body.append(SETUP_DERIVATIVE.substitute(
setup=setup,
args_with_derivatives=[arg['name'] for arg in differentiable_outputs]))
return body
setup = []
setup.extend(ASSIGN_GRAD_FN.substitute(env).split('\n'))
setup.extend(emit_save_inputs())
body = []
body.extend(emit_check_no_requires_grad(differentiable_inputs, args_with_derivatives))
body.append(DECLARE_GRAD_FN.substitute(env))
body.append(SETUP_DERIVATIVE.substitute(env, setup=setup))
return body
def emit_check_if_in_complex_autograd_allowlist():
body = []
if base_name in GRADIENT_IMPLEMENTED_FOR_COMPLEX:
return body
for arg in differentiable_outputs:
name = arg['name']
if arg['type'] == 'Tensor' or arg['type'] == 'TensorList':
body.append('throw_error_for_complex_autograd({}, "{}");'.format(name, base_name))
return body
def emit_check_no_requires_grad(tensor_args, args_with_derivatives):
"""Checks that arguments without derivatives don't require grad"""
body = []
for arg in tensor_args:
if arg in args_with_derivatives:
continue
name = arg['name']
if name in non_differentiable_arg_names:
continue
if name == 'output':
# Double-backwards definitions sometimes take in 'input' and
# 'output', but only define the derivative for input.
continue
if arg['dynamic_type'] in {'IndexTensor', 'ByteTensor', 'BoolTensor'}:
continue
body.append('check_no_requires_grad({}, "{}");'.format(name, name))
return body
def save_variables(saved_variables, is_output, guard_for=lambda name: None):
# assign the saved variables to the generated grad_fn
stmts = []
for arg in saved_variables:
name = arg['name']
expr = arg.get('expr', arg['name'])
if arg['type'] == 'Tensor' or arg['type'] == 'c10::optional<Tensor>' or \
arg['type'] == 'c10::optional<Tensor>&' or (is_output and arg['type'] == 'Scalar'):
name += '_'
var = arg['name']
if var == 'self' and inplace:
var = 'self.clone()'
assert not is_output