Skip to content
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

[PIR]Remove refresh_stopgradient in backward #59579

Merged
merged 2 commits into from
Dec 1, 2023
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
1 change: 0 additions & 1 deletion python/paddle/autograd/ir_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ def remove_op(block, op, state):

def calc_gradient_helper(outputs, inputs, grad_outputs, no_grad_set):
block = outputs[0].get_defining_op().get_parent_block()
block.refresh_stopgradient()
state = State(block)

# check all inputs and outputs in the same block
Expand Down
24 changes: 0 additions & 24 deletions test/ir/pir/test_ir_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,5 @@ def test_basic_network(self):
input_grad = grad(res, x)


class TestBackward_refrash_stopgradients(unittest.TestCase):
def test_refreash_stopgradients(self):
import numpy as np

program = paddle.pir.core.default_main_program()
with paddle.pir_utils.IrGuard(), paddle.pir.core.program_guard(program):
data1 = paddle.static.data('data1', [3, 4, 5], np.float32)
data2 = paddle.static.data('data2', [3, 4, 5], np.float32)
out = paddle.add_n([data1, data2])
data1_arr = np.random.uniform(-1, 1, data1.shape).astype(np.float32)
data2_arr = np.random.uniform(-1, 1, data2.shape).astype(np.float32)
self.assertEqual(
program.global_block().ops[3].result(0).stop_gradient, True
)

data1.stop_gradient = False
data2.stop_gradient = False

dout = grad(out, [data1, data2])
self.assertEqual(
program.global_block().ops[3].result(0).stop_gradient, False
)


if __name__ == "__main__":
unittest.main()
4 changes: 4 additions & 0 deletions test/legacy_test/test_concat_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,10 @@ def func(self, place):

data1 = paddle.static.data('data1', [2, 3], dtype)
data1.persistable = True
data1.stop_gradient = False
data2 = paddle.static.data('data2', [2, 3], dtype)
data2.persistable = True
data2.stop_gradient = False
out = paddle.concat([data1, data2])
data1_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
data2_arr = np.random.uniform(-1, 1, data2.shape).astype(dtype)
Expand Down Expand Up @@ -841,8 +843,10 @@ def func(self, place):

data1 = paddle.static.data('data1', [2, 3, 4], dtype)
data1.persistable = True
data1.stop_gradient = False
data2 = paddle.static.data('data2', [2, 3, 4], dtype)
data2.persistable = True
data2.stop_gradient = False
out = paddle.concat([data1, data2], 1)
data1_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
data2_arr = np.random.uniform(-1, 1, data2.shape).astype(dtype)
Expand Down
4 changes: 4 additions & 0 deletions test/legacy_test/test_elementwise_nn_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def func(self, place):
x = paddle.static.data('x', shape, dtype)
y = paddle.static.data('y', shape[:-1], dtype)
x.persistable = True
x.stop_gradient = False
y.persistable = True
y.stop_gradient = False
out = paddle.tensor.math._multiply_with_axis(x, y, axis=0)
x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
y_arr = np.random.uniform(-1, 1, shape[:-1]).astype(dtype)
Expand Down Expand Up @@ -268,7 +270,9 @@ def func(self, place):
x = paddle.static.data('x', shape, dtype)
y = paddle.static.data('y', shape[1:-1], dtype)
x.persistable = True
x.stop_gradient = False
y.persistable = True
y.stop_gradient = False
out = paddle.tensor.math._divide_with_axis(x, y, axis=1)
x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
y_arr = np.random.uniform(-1, 1, shape[1:-1]).astype(dtype)
Expand Down
2 changes: 2 additions & 0 deletions test/legacy_test/test_nn_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ def func(self, place):
x1 = paddle.static.data('x', x_shape, dtype)
x2 = paddle.static.data('x', x_shape, dtype)
x1.persistable = True
x1.stop_gradient = False
x2.persistable = True
x2.stop_gradient = False
out = paddle.concat([x1, x2], axis=0)
x2_arr = np.random.uniform(-1, 1, x_shape).astype(dtype)
x1_arr = np.random.uniform(-1, 1, x_shape).astype(dtype)
Expand Down
4 changes: 4 additions & 0 deletions test/legacy_test/test_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,10 @@ def func(self, place):

data1 = paddle.static.data('data1', [3, 4, 5], dtype)
data1.persistable = True
data1.stop_gradient = False
data2 = paddle.static.data('data2', [3, 4, 5], dtype)
data2.persistable = True
data2.stop_gradient = False
out = paddle.add_n([data1, data2])
data1_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
data2_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
Expand Down Expand Up @@ -703,8 +705,10 @@ def func(self, place):

data1 = paddle.static.data('data1', [3, 4, 5], dtype)
data1.persistable = True
data1.stop_gradient = False
data2 = paddle.static.data('data2', [3, 4, 5], dtype)
data2.persistable = True
data2.stop_gradient = False
out = paddle.add_n([data1, data2])
data1_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
data2_arr = np.random.uniform(-1, 1, data1.shape).astype(dtype)
Expand Down