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

[Prim][PIR] Support dynamic shape for relu_grad #65482

Merged
merged 2 commits into from
Jun 27, 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
9 changes: 3 additions & 6 deletions paddle/fluid/primitive/rule/vjp/details.h
Original file line number Diff line number Diff line change
Expand Up @@ -1287,12 +1287,9 @@ void masked_select_grad(const Tensor& x,
template <typename T>
void relu_grad(const Tensor& out, const Tensor& out_grad, Tensor* x_grad) {
if (x_grad) {
auto condition = greater_than<T>(
out, full<T>(common::vectorize(out.dims()), 0.0, out.dtype()));
auto res =
where<T>(condition,
out_grad,
full<T>(common::vectorize(out.dims()), 0.0, out.dtype()));
Tensor zeros = full_scalar<T>(0.0, out.dtype());
auto mask = greater_than<T>(out, zeros);
auto res = cast<T>(mask, out.dtype()) * out_grad;
set_output<T>(res, x_grad);
}
}
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/autograd/backward_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"pd_op.concat",
"pd_op.split",
"pd_op.multiply",
"pd_op.relu",
"pd_op.sigmoid",
]


Expand Down
32 changes: 32 additions & 0 deletions test/prim/pir_prim/test_prim_sub_graph_backward_dynamic_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def multiply_net(x, y):
return x * y


def relu_net(x):
return paddle.nn.functional.relu(x)


def sigmoid_net(x):
return paddle.nn.functional.sigmoid(x)


def apply_to_static(net, use_cinn, input_spec=None):
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = use_cinn
Expand Down Expand Up @@ -782,5 +790,29 @@ def setUp(self):
self.tol = 1e-5


class TestPrimReluWithGrad(TestPrimBaseWithGrad):
def setUp(self):
np.random.seed(2023)
self.dtype = "float32"
self.x_shape = [30, 200, 40]
self.init_x_shape = [None, None, None]
self.x = np.random.random(self.x_shape).astype(self.dtype)
self.net = relu_net
self.enable_cinn = False
self.tol = 1e-6


class TestPrimSigmoidWithGrad(TestPrimBaseWithGrad):
def setUp(self):
np.random.seed(2023)
self.dtype = "float32"
self.x_shape = [30, 200, 40]
self.init_x_shape = [None, None, None]
self.x = np.random.random(self.x_shape).astype(self.dtype)
self.net = sigmoid_net
self.enable_cinn = False
self.tol = 1e-6


if __name__ == "__main__":
unittest.main()