Skip to content

Commit ae64707

Browse files
author
0x45f
committed
fix error by drop_kids in python
1 parent efa655f commit ae64707

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

paddle/fluid/pybind/pybind.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,17 @@ All parameter, weight, gradient are variables in Paddle.
12411241
return self.GetMutable<framework::ReaderHolder>();
12421242
},
12431243
py::return_value_policy::reference)
1244+
.def("get_scope",
1245+
[](Variable &self) -> Scope * {
1246+
auto scope_vec =
1247+
self.GetMutable<std::vector<framework::Scope *>>();
1248+
PADDLE_ENFORCE_GT(
1249+
scope_vec->size(), 0,
1250+
platform::errors::InvalidArgument(
1251+
"The size of scope_vec should greater than 0"));
1252+
return scope_vec->front();
1253+
},
1254+
py::return_value_policy::reference)
12441255
.def("set_scope", [](Variable &self, Scope &scope) {
12451256
auto scope_vec = self.GetMutable<std::vector<framework::Scope *>>();
12461257
scope_vec->emplace_back(&scope);

python/paddle/fluid/dygraph/dygraph_to_static/partial_program.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,26 @@ def __call__(self, inputs):
290290
self._valid_vars(self._params),
291291
self._valid_vars(out_vars), self._tmp_scope_vec, self._double_grads,
292292
*attrs)
293-
293+
self.drop_kids_in_nograd()
294294
restored_nest_out = self._restore_out(out_vars)
295295
return self._remove_no_value(restored_nest_out)
296296

297+
def drop_kids_in_nograd(self):
298+
tracer = framework._dygraph_tracer()
299+
if self.training and not tracer._has_grad:
300+
self._tmp_scope_vec.value().get_scope().drop_kids()
301+
297302
@property
298303
def program(self):
299-
tracer = framework._dygraph_tracer()
300-
if self.training and tracer._has_grad:
304+
if self.training:
301305
return self._train_amp_program if _in_amp_guard(
302306
) else self._train_program
303307
else:
304308
return self._infer_program
305309

306310
@property
307311
def program_id(self):
308-
tracer = framework._dygraph_tracer()
309-
if self.training and tracer._has_grad:
312+
if self.training:
310313
return self._train_amp_program_id if _in_amp_guard(
311314
) else self._train_program_id
312315
else:

python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,6 @@ def test_switch_eval_and_train(self):
152152
partial_layer._train_program)
153153

154154

155-
class TestWithNoGrad(unittest.TestCase):
156-
def test_with_no_grad(self):
157-
with fluid.dygraph.guard():
158-
linear_net = Linear()
159-
x_data = np.random.random((5, 10)).astype('float32')
160-
x = fluid.dygraph.to_variable(x_data)
161-
162-
linear_net.train()
163-
linear_net(x)
164-
165-
_, partial_layer = linear_net.forward.program_cache.last()[-1]
166-
self.assertEqual(partial_layer.program,
167-
partial_layer._train_program)
168-
169-
with paddle.no_grad():
170-
linear_net(x)
171-
self.assertEqual(partial_layer.program,
172-
partial_layer._infer_program)
173-
174-
175155
class GPT2LMHeadModel(fluid.dygraph.Layer):
176156
def __init__(self):
177157
super(GPT2LMHeadModel, self).__init__()

0 commit comments

Comments
 (0)