1 #pragma once
2
3 #include <torch/csrc/Export.h>
4 #include <torch/csrc/autograd/InferenceMode.h>
5 #include <torch/csrc/autograd/autograd.h>
6 #include <torch/csrc/autograd/function.h>
7 #include <torch/csrc/autograd/variable.h>
8 #include <torch/csrc/utils/variadic.h>
9
10 #include <ATen/core/Tensor.h>
11
12 #include <functional>
13 #include <memory>
14 #include <vector>
15
16 namespace torch::autograd {
17
18 using function_constructor = std::function<std::shared_ptr<Node>(edge_list&&)>;
19
20 /**
21 * Wraps the tensor outputs in variables and creates the grad_fn and sets the
22 * grad_fn if necessary.
23 */
24 TORCH_API variable_list wrap_outputs(
25 const variable_list& inputs,
26 tensor_list&& outputs,
27 const function_constructor& ctr);
28
29 /// Checks that inputs contains exactly `args` items and that the first
30 /// `required_args`
31 /// items are not nullptr. If not specified, `required_args` defaults to `args`.
32 TORCH_API void check_input_variables(
33 const char* name,
34 const variable_list& inputs,
35 int args,
36 int required_args = -1,
37 bool allow_undefined = false);
38
39 struct ComputeRequiresGrad : IterArgs<ComputeRequiresGrad> {
40 bool out = false;
41 using IterArgs<ComputeRequiresGrad>::operator();
operatorComputeRequiresGrad42 void operator()(const at::Tensor& tensor) {
43 const auto& var = static_cast<const Variable&>(tensor);
44 if (var.defined() && var.requires_grad()) {
45 out = true;
46 }
47 }
operatorComputeRequiresGrad48 void operator()(const std::optional<at::Tensor>& tensor) {
49 if (tensor.has_value()) {
50 (*this)(*tensor);
51 }
52 }
short_circuitComputeRequiresGrad53 bool short_circuit() {
54 return out;
55 }
56 };
57
58 template <typename... Args>
compute_requires_grad(Args &&...args)59 inline bool compute_requires_grad(Args&&... args) {
60 if (!GradMode::is_enabled()) {
61 return false;
62 }
63 return ComputeRequiresGrad().apply(std::forward<Args>(args)...).out;
64 }
65
set_history(const at::Tensor & variable,const std::shared_ptr<Node> & grad_fn)66 inline void set_history(
67 const at::Tensor& variable,
68 const std::shared_ptr<Node>& grad_fn) {
69 TORCH_CHECK(grad_fn != nullptr);
70 if (variable.defined()) {
71 // If the codegen triggers this, you most likely want to add your newly
72 // added function to the DONT_REQUIRE_DERIVATIVE list in
73 // tools/autograd/gen_variable_type.py
74 TORCH_INTERNAL_ASSERT(isDifferentiableType(variable.scalar_type()));
75 auto output_nr = grad_fn->add_input_metadata(variable);
76 impl::set_gradient_edge(variable, {grad_fn, output_nr});
77 } else {
78 grad_fn->add_input_metadata(Node::undefined_input());
79 }
80 }
81
set_history(const std::vector<Variable> & variables,const std::shared_ptr<Node> & grad_fn)82 inline void set_history(
83 const std::vector<Variable>& variables,
84 const std::shared_ptr<Node>& grad_fn) {
85 for (auto& variable : variables) {
86 set_history(variable, grad_fn);
87 }
88 }
89
isFwGradDefined(const std::optional<at::Tensor> & t)90 inline bool isFwGradDefined(const std::optional<at::Tensor>& t) {
91 return t.has_value() && t->defined() && t->_fw_grad(/*level */ 0).defined();
92 }
93
isFwGradDefinedTensorList(const at::ITensorListRef & variables)94 inline bool isFwGradDefinedTensorList(const at::ITensorListRef& variables) {
95 bool ret = false;
96 for (auto& variable : variables) {
97 ret |= isFwGradDefined(variable);
98 }
99 return ret;
100 }
101
isFwGradDefinedTensorList(const c10::List<std::optional<at::Tensor>> & li)102 inline bool isFwGradDefinedTensorList(
103 const c10::List<std::optional<at::Tensor>>& li) {
104 bool ret = false;
105 for (auto i : c10::irange(li.size())) {
106 auto t = li.get(i);
107 ret |= isFwGradDefined(t);
108 }
109 return ret;
110 }
111
112 } // namespace torch::autograd
113