Fix LBFGS issue (#16167)
authorMiro Furtado <furtadomiro@gmail.com>
Sat, 19 Jan 2019 22:58:36 +0000 (14:58 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 19 Jan 2019 23:01:06 +0000 (15:01 -0800)
Summary:
Resolves #15923 where LBFGS threw "Error: a leaf Variable that requires grad has been used in an in-place operation."
Pull Request resolved: https://github.com/pytorch/pytorch/pull/16167

Differential Revision: D13745822

Pulled By: soumith

fbshipit-source-id: 7d1d0511d06838c0c6f4c8a6b53cf15193283059

torch/csrc/api/src/optim/lbfgs.cpp

index f1f80d9..ab4c2e1 100644 (file)
@@ -3,6 +3,7 @@
 #include <torch/csrc/autograd/generated/variable_factories.h>
 #include <torch/csrc/autograd/variable.h>
 #include <torch/serialize/archive.h>
+#include <torch/utils.h>
 
 #include <ATen/ATen.h>
 
@@ -25,6 +26,7 @@ Tensor LBFGS::gather_flat_grad() {
 }
 
 void LBFGS::add_grad(const torch::Tensor& step_size, const Tensor& update) {
+  NoGradGuard guard;
   int64_t offset = 0;
   for (auto& parameter : parameters_) {
     int64_t numel = parameter.numel();