Skip to content

[PIR]Fix pruning for saveload and backward #68300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions python/paddle/autograd/backward_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,19 @@ def get_real_op_inputs(op):
return op.operands_source()


def get_real_op_outputs(op):
outputs = op.results()
if op.name() == "pd_op.array_write_":
for x in op.operands():
outputs.append(x.source())
if op.name() == "pd_op.while":
for internal_op in op.as_while_op().body().ops:
if internal_op.name() == "pd_op.array_write_":
for x in internal_op.operands():
outputs.append(x.source())
return outputs


def inverse_sort_op(old_ops):
'''
if topo graph is op1 -> op2 -> op3
Expand Down
7 changes: 4 additions & 3 deletions python/paddle/autograd/ir_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
dynamic_shape_prim_vjp_guard,
get_grad_semantic_info,
get_real_op_inputs,
get_real_op_outputs,
get_split_op,
inverse_sort_op,
is_builtin_op,
Expand Down Expand Up @@ -260,21 +261,21 @@ def prune_ops(total_ops, inputs_set, outputs_set, no_grad_set):
# from input to output
if inputs_set:
for i, op in enumerate(total_ops):
if some_in_set(op.results(), inputs_set):
if some_in_set(get_real_op_outputs(op), inputs_set):
union_op_flags[i] = True
continue

if some_in_set(get_real_op_inputs(op), inputs_set):
union_op_flags[i] = True
for value in op.results():
for value in get_real_op_outputs(op):
if value not in no_grad_set:
inputs_set.add(value)
else:
intersection_op_flags[i] = False

# from output to input
for i, op in reversed(list(enumerate(total_ops))):
if some_in_set(op.results(), outputs_set):
if some_in_set(get_real_op_outputs(op), outputs_set):
union_op_flags[i] = True
for operand in get_real_op_inputs(op):
if operand not in no_grad_set:
Expand Down
5 changes: 3 additions & 2 deletions python/paddle/static/pir_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from paddle.autograd.backward_utils import (
ValueSet,
get_real_op_inputs,
get_real_op_outputs,
some_in_set,
)
from paddle.base import (
Expand Down Expand Up @@ -175,15 +176,15 @@ def pir_prune_with_input(program, feed_vars, target_vars):
# from output to input
target_vars_ = ValueSet(target_vars)
for i, op in reversed(list(enumerate(total_ops))):
if some_in_set(op.results(), target_vars_):
if some_in_set(get_real_op_outputs(op), target_vars_):
for operand in get_real_op_inputs(op):
target_vars_.add(operand)
else:
intersection_op_flags[i] = False

for i, op in reversed(list(enumerate(total_ops))):
if not intersection_op_flags[i]:
if some_in_set(op.results(), ValueSet(feed_vars)):
if some_in_set(get_real_op_outputs(op), ValueSet(feed_vars)):
raise ValueError(
f"The feed_var create by: '{op.name()}' is not involved in the target_vars calculation"
f"Please remove it from feed_vars ."
Expand Down