Skip to content

Commit 23e19e2

Browse files
committed
Fix bug
1 parent 3b04f00 commit 23e19e2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

python/paddle/fluid/backward.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ def append_backward(loss, parameter_list=None, no_grad_set=None,
449449
"""
450450
assert isinstance(loss, framework.Variable)
451451

452+
if loss.op is None:
453+
# the loss is from a cloned program. Find loss op manually.
454+
for op in reversed(loss.block.ops):
455+
assert isinstance(op, framework.Operator)
456+
if len(op.output_arg_names) == 1 and op.output_arg_names[
457+
0] == loss.name:
458+
loss.op = op
459+
break
460+
if loss.op is None:
461+
raise ValueError("loss.op is None. Should not happend")
462+
452463
loss.op.set_attr(core.op_proto_and_checker_maker.kOpRoleAttrName(),
453464
int(core.op_proto_and_checker_maker.OpRole.Forward) |
454465
int(core.op_proto_and_checker_maker.OpRole.Loss))

0 commit comments

Comments
 (0)