未验证 提交 797b2dfd 编写于 作者: W WeiXin 提交者: GitHub

clear 'BasicEngine' when an exception occurs in the backward. (#32546)

* clear 'BasicEngine' when an exception occurs in the backward.

* deal with conflict.

* deal with conflict.
上级 f285f4c1
...@@ -470,12 +470,20 @@ void BasicEngine::Execute() { ...@@ -470,12 +470,20 @@ void BasicEngine::Execute() {
{ {
VLOG(3) << "Start to execute grad op " << cur_op.Type(); VLOG(3) << "Start to execute grad op " << cur_op.Type();
if (tmp_ins_ptr == nullptr) { try {
OpBase::Run(cur_op.InnerOp(), bwd_ins, tmp_outs, cur_op.Attrs(), if (tmp_ins_ptr == nullptr) {
cur_op.place()); OpBase::Run(cur_op.InnerOp(), bwd_ins, tmp_outs, cur_op.Attrs(),
} else { cur_op.place());
OpBase::Run(cur_op.InnerOp(), *tmp_ins_ptr, tmp_outs, cur_op.Attrs(), } else {
cur_op.place()); OpBase::Run(cur_op.InnerOp(), *tmp_ins_ptr, tmp_outs,
cur_op.Attrs(), cur_op.place());
}
} catch (platform::EnforceNotMet& exception) {
Clear();
throw std::move(exception);
} catch (std::exception& ex) {
Clear();
PADDLE_THROW(platform::errors::External("%s", ex.what()));
} }
} }
......
...@@ -234,8 +234,7 @@ class TestPyLayer(unittest.TestCase): ...@@ -234,8 +234,7 @@ class TestPyLayer(unittest.TestCase):
z = Layer_bk_none1.apply(input2) z = Layer_bk_none1.apply(input2)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.sum().backward()
z.sum().backward()
class Layer_bk_none2(PyLayer): class Layer_bk_none2(PyLayer):
@staticmethod @staticmethod
...@@ -249,9 +248,9 @@ class TestPyLayer(unittest.TestCase): ...@@ -249,9 +248,9 @@ class TestPyLayer(unittest.TestCase):
input1 = paddle.randn([2, 3]).astype("float64") input1 = paddle.randn([2, 3]).astype("float64")
input1.stop_gradient = False input1.stop_gradient = False
z = Layer_bk_none2.apply(input1, input1) z = Layer_bk_none2.apply(input1, input1)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.mean().backward()
z.mean().backward()
class Layer_bk_one1(PyLayer): class Layer_bk_one1(PyLayer):
@staticmethod @staticmethod
...@@ -265,9 +264,9 @@ class TestPyLayer(unittest.TestCase): ...@@ -265,9 +264,9 @@ class TestPyLayer(unittest.TestCase):
input1 = paddle.randn([2, 3]).astype("float64") input1 = paddle.randn([2, 3]).astype("float64")
input1.stop_gradient = False input1.stop_gradient = False
z = Layer_bk_one1.apply(input1) z = Layer_bk_one1.apply(input1)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.mean().backward()
z.mean().backward()
class Layer_bk_one2(PyLayer): class Layer_bk_one2(PyLayer):
@staticmethod @staticmethod
...@@ -280,11 +279,11 @@ class TestPyLayer(unittest.TestCase): ...@@ -280,11 +279,11 @@ class TestPyLayer(unittest.TestCase):
input1 = paddle.randn([2, 3]).astype("float64") input1 = paddle.randn([2, 3]).astype("float64")
input1.stop_gradient = False input1.stop_gradient = False
y = Layer_bk_one2.apply(input1, input1) y = Layer_bk_one2.apply(input1, input1)
z = y[0] + y[1] z = y[0] + y[1]
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.mean().backward()
z.mean().backward()
class Layer_no_bk(PyLayer): class Layer_no_bk(PyLayer):
@staticmethod @staticmethod
...@@ -295,10 +294,9 @@ class TestPyLayer(unittest.TestCase): ...@@ -295,10 +294,9 @@ class TestPyLayer(unittest.TestCase):
input1.stop_gradient = False input1.stop_gradient = False
z = Layer_no_bk.apply(input1) z = Layer_no_bk.apply(input1)
with self.assertRaises(NotImplementedError): with self.assertRaises(OSError):
with paddle.fluid.dygraph.guard(): z = z[0] + z[1]
z = z[0] + z[1] z.mean().backward()
z.mean().backward()
class Layer_bk_match(PyLayer): class Layer_bk_match(PyLayer):
@staticmethod @staticmethod
...@@ -313,9 +311,8 @@ class TestPyLayer(unittest.TestCase): ...@@ -313,9 +311,8 @@ class TestPyLayer(unittest.TestCase):
input1.stop_gradient = False input1.stop_gradient = False
z = Layer_bk_match.apply(input1) z = Layer_bk_match.apply(input1)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z = z[0] + z[1]
z = z[0] + z[1] z.mean().backward()
z.mean().backward()
def test_pylayer_bk_return_none(self): def test_pylayer_bk_return_none(self):
class Layer_bk_none1(PyLayer): class Layer_bk_none1(PyLayer):
...@@ -334,8 +331,7 @@ class TestPyLayer(unittest.TestCase): ...@@ -334,8 +331,7 @@ class TestPyLayer(unittest.TestCase):
z = Layer_bk_none1.apply(input1, input2) z = Layer_bk_none1.apply(input1, input2)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.mean().backward()
z.mean().backward()
class Layer_bk_none2(PyLayer): class Layer_bk_none2(PyLayer):
@staticmethod @staticmethod
...@@ -353,8 +349,7 @@ class TestPyLayer(unittest.TestCase): ...@@ -353,8 +349,7 @@ class TestPyLayer(unittest.TestCase):
z = Layer_bk_none2.apply(input1, input2) z = Layer_bk_none2.apply(input1, input2)
z = z[0] + z[1] z = z[0] + z[1]
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with paddle.fluid.dygraph.guard(): z.mean().backward()
z.mean().backward()
def test_pylayer_inplace(self): def test_pylayer_inplace(self):
class cus_tanh(PyLayer): class cus_tanh(PyLayer):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册