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] Add the dynamic shape test case for scale_grad, square_grad, transpose_grad and swiglu_grad #67590

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions python/paddle/autograd/backward_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
"pd_op.max",
"pd_op.stack",
"pd_op.expand",
"pd_op.scale",
"pd_op.square",
"pd_op.transpose",
"pd_op.swiglu",
]


Expand Down
84 changes: 84 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 @@ -217,6 +217,26 @@ def stack_net3(x):
return paddle.stack(x, axis=0)


def scale_net(x):
return paddle.scale(x, scale=-2.3)


def square_net(x):
return paddle.square(x)


def transpose_net(x):
return paddle.transpose(x, perm=[0, 3, 1, 2])


def swiglu_net1(x, y):
return paddle.incubate.nn.functional.swiglu(x, y)


def swiglu_net2(x):
return paddle.incubate.nn.functional.swiglu(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 @@ -2214,5 +2234,69 @@ def setUp(self):
self.tol = 1e-6


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


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


class TestPrimTransposeWithGrad(TestPrimBaseWithGrad):
def setUp(self):
np.random.seed(2023)
self.dtype = "float32"
self.x_shape = [20, 30, 50, 70]
self.init_x_shape = [None, None, None, 70]
self.x = np.random.random(self.x_shape).astype(self.dtype)
self.net = transpose_net
self.enable_cinn = False
self.tol = 1e-6


class TestPrimSwigluWithGrad1(TestPrimBaseOneGradTwoInputs):
def setUp(self):
np.random.seed(2023)
self.dtype = "float32"
self.y_shape = [30, 200, 40]
self.init_y_shape = [None, None, 40]
self.x_shape = [30, 200, 40]
self.init_x_shape = [None, None, 40]
self.x = np.random.random(self.x_shape).astype(self.dtype)
self.y = np.random.random(self.y_shape).astype(self.dtype)
self.net = swiglu_net1
self.enable_cinn = False
self.tol = 1e-5
self.y_without_grad = True


class TestPrimSwigluWithGrad2(TestPrimBaseWithGrad):
def setUp(self):
np.random.seed(2023)
self.dtype = "float32"
self.x_shape = [20, 30, 50, 70]
self.init_x_shape = [None, None, None, 70]
self.x = np.random.random(self.x_shape).astype(self.dtype)
self.net = swiglu_net2
self.enable_cinn = False
self.tol = 1e-6


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