summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/autograd/gen_variable_type.py4
-rw-r--r--torch/csrc/autograd/functions/utils.h15
2 files changed, 10 insertions, 9 deletions
diff --git a/tools/autograd/gen_variable_type.py b/tools/autograd/gen_variable_type.py
index 87f1318759..f7a68d8bcc 100644
--- a/tools/autograd/gen_variable_type.py
+++ b/tools/autograd/gen_variable_type.py
@@ -196,7 +196,9 @@ DISPATCH_TO_NON_VAR_TYPE_WITHOUT_RETURN_VALUES = CodeTemplate("""\
""")
SET_HISTORY = CodeTemplate("""\
-${fn}_history(${differentiable_outputs}, grad_fn);
+if (grad_fn) {
+ ${fn}_history(${differentiable_outputs}, grad_fn);
+}
""")
CONDITIONAL = CodeTemplate("""\
diff --git a/torch/csrc/autograd/functions/utils.h b/torch/csrc/autograd/functions/utils.h
index c63252605e..b4ee46dcec 100644
--- a/torch/csrc/autograd/functions/utils.h
+++ b/torch/csrc/autograd/functions/utils.h
@@ -51,14 +51,13 @@ inline bool compute_requires_grad(Args&&... args) {
inline void set_history(
at::Tensor& variable,
const std::shared_ptr<Function>& grad_fn) {
- if (grad_fn) {
- if (variable.defined()) {
- auto output_nr =
- grad_fn->add_input_metadata(variable);
- as_variable_ref(variable).set_gradient_edge({grad_fn, output_nr});
- } else {
- grad_fn->add_input_metadata(Function::undefined_input());
- }
+ AT_ASSERT(grad_fn);
+ if (variable.defined()) {
+ auto output_nr =
+ grad_fn->add_input_metadata(variable);
+ as_variable_ref(variable).set_gradient_edge({grad_fn, output_nr});
+ } else {
+ grad_fn->add_input_metadata(Function::undefined_input());
}
}