diff --git a/python/paddle/autograd/backward_utils.py b/python/paddle/autograd/backward_utils.py index c82b7b9217883..54bc55745e8fa 100644 --- a/python/paddle/autograd/backward_utils.py +++ b/python/paddle/autograd/backward_utils.py @@ -30,45 +30,49 @@ # TODO(CZ): to be removed when we support dynamic shape by default. ALLOW_DYNAMIC_SHAPE_VJP_OPS = [ "pd_op.abs", + "pd_op.add", "pd_op.assign", - "pd_op.sin", - "pd_op.cos", - "pd_op.tanh", "pd_op.cast", - "pd_op.log", - "pd_op.exp", - "pd_op.sqrt", - "pd_op.rsqrt", - "pd_op.sigmoid", - "pd_op.silu", - "pd_op.sum", - "pd_op.mean", - "pd_op.add", - "pd_op.subtract", "pd_op.concat", - "pd_op.split", - "pd_op.multiply", - "pd_op.relu", + "pd_op.cos", + "pd_op.cumsum", "pd_op.divide", - "pd_op.pow", "pd_op.elementwise_pow", - "pd_op.softmax", - "pd_op.matmul", - "pd_op.cumsum", "pd_op.erf", + "pd_op.exp", + "pd_op.expand", "pd_op.floor", - "pd_op.reshape", + "pd_op.gelu", + "pd_op.hardswish", "pd_op.leaky_relu", - "pd_op.softsign", + "pd_op.log", + "pd_op.matmul", + "pd_op.max", "pd_op.maximum", + "pd_op.mean", "pd_op.minimum", - "pd_op.gelu", - "pd_op.hardswish", + "pd_op.multiply", + "pd_op.pad", + "pd_op.pow", "pd_op.reduce_as", - "pd_op.max", + "pd_op.relu", + "pd_op.reshape", + "pd_op.rsqrt", + "pd_op.scale", + "pd_op.sigmoid", + "pd_op.silu", + "pd_op.sin", + "pd_op.subtract", + "pd_op.sum", + "pd_op.softmax", + "pd_op.softsign", + "pd_op.split", + "pd_op.sqrt", + "pd_op.square", "pd_op.stack", - "pd_op.expand", - "pd_op.pad", + "pd_op.swiglu", + "pd_op.transpose", + "pd_op.tanh", ] diff --git a/test/prim/pir_prim/CMakeLists.txt b/test/prim/pir_prim/CMakeLists.txt index eafa8b5842bd4..380743f5bd88f 100644 --- a/test/prim/pir_prim/CMakeLists.txt +++ b/test/prim/pir_prim/CMakeLists.txt @@ -19,6 +19,10 @@ set(TEST_PRIM_PURE_PIR_CASES test_auto_recompute_dy2static test_prim_sub_graph_dynamic_shape test_prim_sub_graph_backward_dynamic_shape + test_prim_sub_graph_abcde_backward_dynamic_shape + test_prim_sub_graph_fghij_backward_dynamic_shape + test_prim_sub_graph_klmno_backward_dynamic_shape + test_prim_sub_graph_pqrst_backward_dynamic_shape test_decompose_control_flow test_decomp_whole_program test_dynamic_combine1 diff --git a/test/prim/pir_prim/test_prim_sub_graph_abcde_backward_dynamic_shape.py b/test/prim/pir_prim/test_prim_sub_graph_abcde_backward_dynamic_shape.py new file mode 100644 index 0000000000000..2827d255ded69 --- /dev/null +++ b/test/prim/pir_prim/test_prim_sub_graph_abcde_backward_dynamic_shape.py @@ -0,0 +1,736 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from test_prim_sub_graph_backward_dynamic_shape import ( + TestPrimBaseWithGrad, + TestPrimTwoWithGrad, + apply_to_static, +) + +import paddle +from paddle.framework import core +from paddle.static import InputSpec + + +def add_net(x, y): + return x + y + + +def concat_net1(x): + y = x + 1 + return paddle.concat([x, y], axis=-1) + + +def concat_net2(x): + y = x + 1 + return paddle.concat([x, y], axis=1) + + +def concat_net3(x): + return paddle.concat(x, axis=0) + + +def cumsum_net(x): + return paddle.cumsum(x, axis=1) + + +def divide_net(x, y): + return x / y + + +def elementwise_pow_net(x, y): + return paddle.pow(x, y) + + +def erf_net(x): + return paddle.erf(x) + + +def expand_net(x): + return paddle.expand(x, [30, 200, 40]) + + +class TestPrimAddWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 1, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 1, 40] + self.init_y_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 = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad7(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [40] + self.init_y_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad8(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [40] + self.init_x_shape = [None] + self.y_shape = [30, 200, 40] + self.init_y_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 = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad9(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.y_shape = [200, 40] + self.init_y_shape = self.y_shape + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimAddWithGrad10(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = self.x_shape + self.y_shape = [30, 200, 40] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = add_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimConcatWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = concat_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimConcatWithGrad2(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 = concat_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimConcatWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = concat_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimConcatWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = concat_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimConcatWithGrad5(unittest.TestCase): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + x = np.random.random(self.x_shape).astype(self.dtype) + self.x = [x + i for i in range(4)] + self.net = concat_net3 + self.enable_cinn = False + self.tol = 1e-6 + + def base_net(self, flag=None): + if flag == "prim": + core._set_prim_all_enabled(True) + x = [paddle.to_tensor(self.x[i], stop_gradient=False) for i in range(4)] + if flag == "prim": + fn = apply_to_static( + self.net, + use_cinn=self.enable_cinn, + input_spec=[ + [ + InputSpec(shape=self.x_shape, dtype='float32'), + InputSpec(shape=self.init_x_shape, dtype='float32'), + InputSpec(shape=self.init_x_shape, dtype='float32'), + InputSpec(shape=self.x_shape, dtype='float32'), + ] + ], + ) + fn.train() + else: + fn = self.net + res = fn(x) + res.backward() + x_grad1 = x[0].gradient() + x_grad2 = x[1].gradient() + x_grad3 = x[2].gradient() + x_grad4 = x[3].gradient() + if flag == "prim": + core._set_prim_all_enabled(False) + return res, [x_grad1, x_grad2, x_grad3, x_grad4] + + def test_prim_all_dynamic(self): + res_ref, grad_ref = self.base_net() + res, grad = self.base_net("prim") + + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose( + ref, actual, rtol=self.tol, atol=self.tol + ) + + for dr, d in zip(grad_ref, grad): + np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) + + +class TestPrimConcatWithGrad6(TestPrimConcatWithGrad5): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + x = np.random.random(self.x_shape).astype(self.dtype) + self.x = [x + i for i in range(4)] + self.net = concat_net3 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimCumsumWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = cumsum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimDivideWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 1, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 1, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad7(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad8(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [40] + self.init_y_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad9(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [40] + self.init_x_shape = [None] + self.y_shape = [30, 200, 40] + self.init_y_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 = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad10(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.y_shape = [200, 40] + self.init_y_shape = self.y_shape + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimDivideWithGrad11(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = self.x_shape + self.y_shape = [30, 200, 40] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = divide_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 1, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 1, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad7(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad8(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [40] + self.init_y_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad9(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [40] + self.init_x_shape = [None] + self.y_shape = [30, 200, 40] + self.init_y_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 = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad10(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.y_shape = [200, 40] + self.init_y_shape = self.y_shape + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimElementwisePowWithGrad11(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = self.x_shape + self.y_shape = [30, 200, 40] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = elementwise_pow_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimErfWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = erf_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimExpandWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = [None, 40] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = expand_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimExpandWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 1, 40] + self.init_x_shape = [None, None, 40] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = expand_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimExpandWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = expand_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimExpandWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 1, 1] + self.init_x_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = expand_net + self.enable_cinn = False + self.tol = 1e-6 + + +if __name__ == "__main__": + unittest.main() diff --git a/test/prim/pir_prim/test_prim_sub_graph_backward_dynamic_shape.py b/test/prim/pir_prim/test_prim_sub_graph_backward_dynamic_shape.py index 680466962040b..b975519e20b34 100644 --- a/test/prim/pir_prim/test_prim_sub_graph_backward_dynamic_shape.py +++ b/test/prim/pir_prim/test_prim_sub_graph_backward_dynamic_shape.py @@ -25,202 +25,14 @@ def sum_net1(x): return paddle.sum(x, axis=1, keepdim=False) -def sum_net2(x): - return paddle.sum(x) - - -def sum_net3(x): - return paddle.sum(x, keepdim=True) - - -def sum_net4(x): - return paddle.sum(x, axis=-1, keepdim=False) - - -def sum_net5(x): - return paddle.sum(x, axis=[0, 2], keepdim=False) - - -def mean_net1(x): - return paddle.mean(x, axis=1, keepdim=False) - - -def mean_net2(x): - return paddle.mean(x, axis=-1, keepdim=False) - - -def mean_net3(x): - return paddle.mean(x, axis=[0, 2], keepdim=False) - - def add_net(x, y): return x + y -def subtract_net(x, y): - return x - y - - -def concat_net1(x): - y = x + 1 - return paddle.concat([x, y], axis=-1) - - -def concat_net2(x): - y = x + 1 - return paddle.concat([x, y], axis=1) - - -def concat_net3(x): - return paddle.concat(x, axis=0) - - -def split_net1(x): - res = paddle.split(x, num_or_sections=10, axis=-1) - tmp_res = res[0] - for i in range(1, len(res)): - tmp_res = tmp_res + res[i] * i - return tmp_res / len(res) - - -def split_net2(x): - res = paddle.split(x, num_or_sections=10, axis=1) - tmp_res = res[0] - for i in range(1, len(res)): - tmp_res = tmp_res + res[i] * i - return tmp_res / len(res) - - -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 divide_net(x, y): - return x / y - - -def elementwise_pow_net(x, y): - return paddle.pow(x, y) - - -def pow_net(x): - return paddle.pow(x, 3.2) - - -def softmax_net(x): - return paddle.nn.functional.softmax(x, axis=-1) - - -def matmul_net(x, y): - return paddle.matmul(x, y) - - -def tanh_net(x): - return paddle.tanh(x) - - -def cumsum_net(x): - return paddle.cumsum(x, axis=1) - - -def erf_net(x): - return paddle.erf(x) - - -def floor_net(x): - return paddle.floor(x) - - -def reshape_net(x): - return paddle.reshape(x, [30, 200 * 40]) - - -def leaky_relu_net(x): - return paddle.nn.functional.leaky_relu(x) - - -def softsign_net(x): - return paddle.nn.functional.softsign(x) - - -def gelu_net1(x): - return paddle.nn.functional.gelu(x, approximate=True) - - -def gelu_net2(x): - return paddle.nn.functional.gelu(x, approximate=False) - - -def minimum_net(x, y): - return paddle.minimum(x, y) - - -def hardswish_net(x): - return paddle.nn.functional.hardswish(x) - - -def maximum_net(x, y): - return paddle.maximum(x, y) - - def reduce_as_net(x, y): return paddle.reduce_as(x, y) -def max_net1(x): - return paddle.max(x, keepdim=True) - - -def max_net2(x): - return paddle.max(x, keepdim=False) - - -def max_net3(x): - return paddle.max(x, axis=[0, 1], keepdim=False) - - -def max_net4(x): - return paddle.max(x, axis=[-1, -2], keepdim=False) - - -def max_net5(x): - return paddle.max(x, axis=[-1, 0], keepdim=False) - - -def max_net6(x): - return paddle.max(x) - - -def expand_net(x): - return paddle.expand(x, [30, 200, 40]) - - -def stack_net1(x): - y = x + 1 - return paddle.stack([x, y], axis=-1) - - -def stack_net2(x): - y = x + 1 - return paddle.stack([x, y], axis=1) - - -def stack_net3(x): - return paddle.stack(x, axis=0) - - -def pad_net(x): - return paddle.nn.functional.pad(x, [0, 1, 2]) - - def apply_to_static(net, use_cinn, input_spec=None): build_strategy = paddle.static.BuildStrategy() build_strategy.build_cinn_pass = use_cinn @@ -278,102 +90,6 @@ def test_prim_all_dynamic(self): np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) -class TestPrimSumWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1000] - self.init_x_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = sum_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSumWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = sum_net3 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSumWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = sum_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSumWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = sum_net4 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSumWithGrad5(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = sum_net5 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMeanWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = mean_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMeanWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = mean_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMeanWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = mean_net3 - self.enable_cinn = False - self.tol = 1e-6 - - class TestPrimTwoWithGrad(unittest.TestCase): def setUp(self): np.random.seed(2023) @@ -426,1809 +142,60 @@ def test_prim_all_dynamic(self): np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) -class TestPrimAddWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 1, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad4(TestPrimTwoWithGrad): +class TestPrimBaseOneGradTwoInputs(unittest.TestCase): def setUp(self): np.random.seed(2023) self.dtype = "float32" + self.y_shape = [200, 40] + self.init_y_shape = [None, 200] self.x_shape = [30, 200, 40] self.init_x_shape = [None, None, 40] - self.y_shape = [1, 1, 40] - self.init_y_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 = add_net + self.net = reduce_as_net self.enable_cinn = False - self.tol = 1e-6 + self.tol = 1e-5 + self.y_without_grad = True + def base_net(self, flag=None): + if flag == "prim": + core._set_prim_all_enabled(True) + x = paddle.to_tensor(self.x, stop_gradient=False) + y = paddle.to_tensor(self.y, stop_gradient=False) + if flag == "prim": + fn = apply_to_static( + self.net, + use_cinn=self.enable_cinn, + input_spec=[ + InputSpec(shape=self.init_x_shape, dtype='float32'), + InputSpec(shape=self.init_y_shape, dtype='float32'), + ], + ) + fn.train() + else: + fn = self.net + res = fn(x, y) + res.backward() + if self.y_without_grad: + grad = x.gradient() + else: + grad = y.gradient() + if flag == "prim": + core._set_prim_all_enabled(False) + return res, [grad] -class TestPrimAddWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = add_net - self.enable_cinn = False - self.tol = 1e-6 + def test_prim_all_dynamic(self): + res_ref, grad_ref = self.base_net() + res, grad = self.base_net("prim") - -class TestPrimAddWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad7(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [40] - self.init_y_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad8(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [40] - self.init_x_shape = [None] - self.y_shape = [30, 200, 40] - self.init_y_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 = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad9(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.y_shape = [200, 40] - self.init_y_shape = self.y_shape - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimAddWithGrad10(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = self.x_shape - self.y_shape = [30, 200, 40] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = add_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 1, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 1, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad7(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad8(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [40] - self.init_y_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad9(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [40] - self.init_x_shape = [None] - self.y_shape = [30, 200, 40] - self.init_y_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 = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad10(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.y_shape = [200, 40] - self.init_y_shape = self.y_shape - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSubtractWithGrad11(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = self.x_shape - self.y_shape = [30, 200, 40] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = subtract_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimConcatWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = concat_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimConcatWithGrad2(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 = concat_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimConcatWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = concat_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimConcatWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = concat_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimConcatWithGrad5(unittest.TestCase): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - x = np.random.random(self.x_shape).astype(self.dtype) - self.x = [x + i for i in range(4)] - self.net = concat_net3 - self.enable_cinn = False - self.tol = 1e-6 - - def base_net(self, flag=None): - if flag == "prim": - core._set_prim_all_enabled(True) - x = [paddle.to_tensor(self.x[i], stop_gradient=False) for i in range(4)] - if flag == "prim": - fn = apply_to_static( - self.net, - use_cinn=self.enable_cinn, - input_spec=[ - [ - InputSpec(shape=self.x_shape, dtype='float32'), - InputSpec(shape=self.init_x_shape, dtype='float32'), - InputSpec(shape=self.init_x_shape, dtype='float32'), - InputSpec(shape=self.x_shape, dtype='float32'), - ] - ], - ) - fn.train() - else: - fn = self.net - res = fn(x) - res.backward() - x_grad1 = x[0].gradient() - x_grad2 = x[1].gradient() - x_grad3 = x[2].gradient() - x_grad4 = x[3].gradient() - if flag == "prim": - core._set_prim_all_enabled(False) - return res, [x_grad1, x_grad2, x_grad3, x_grad4] - - def test_prim_all_dynamic(self): - res_ref, grad_ref = self.base_net() - res, grad = self.base_net("prim") - - for ref, actual in zip(res_ref, res): - np.testing.assert_allclose( - ref, actual, rtol=self.tol, atol=self.tol - ) - - for dr, d in zip(grad_ref, grad): - np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) - - -class TestPrimConcatWithGrad6(TestPrimConcatWithGrad5): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - x = np.random.random(self.x_shape).astype(self.dtype) - self.x = [x + i for i in range(4)] - self.net = concat_net3 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSplitWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = split_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSplitWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = split_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSplitWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = split_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSplitWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = split_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMultiplyWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 1, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 1, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad7(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad8(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [40] - self.init_y_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad9(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [40] - self.init_x_shape = [None] - self.y_shape = [30, 200, 40] - self.init_y_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 = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad10(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.y_shape = [200, 40] - self.init_y_shape = self.y_shape - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = multiply_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMultiplyWithGrad11(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = self.x_shape - self.y_shape = [30, 200, 40] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = multiply_net - self.enable_cinn = False - 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 - - -class TestPrimDivideWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 1, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 1, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad7(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad8(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [40] - self.init_y_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad9(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [40] - self.init_x_shape = [None] - self.y_shape = [30, 200, 40] - self.init_y_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 = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad10(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.y_shape = [200, 40] - self.init_y_shape = self.y_shape - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimDivideWithGrad11(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = self.x_shape - self.y_shape = [30, 200, 40] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = divide_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 1, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.y_shape = [30, 200, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 1, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [1, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad7(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [30, 200, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad8(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - self.y_shape = [40] - self.init_y_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad9(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [40] - self.init_x_shape = [None] - self.y_shape = [30, 200, 40] - self.init_y_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 = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad10(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.y_shape = [200, 40] - self.init_y_shape = self.y_shape - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimElementwisePowWithGrad11(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = self.x_shape - self.y_shape = [30, 200, 40] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = elementwise_pow_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimPowWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [100, 20, 30] - self.init_x_shape = [None, None, 30] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = pow_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSoftmaxWithGrad1(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 = softmax_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSoftmaxWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = softmax_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSoftmaxWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [30, 200, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = softmax_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMatmulWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 40, 200] - self.init_x_shape = [None, None, 200] - self.y_shape = [30, 200, 40] - self.init_y_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 = matmul_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMatmulWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 30, 40, 200] - self.init_x_shape = [None, None, None, 200] - self.y_shape = [30, 1, 200, 40] - self.init_y_shape = [None, 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 = matmul_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMatmulWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [1, 30, 40, 200] - self.init_x_shape = [1, None, None, 200] - self.y_shape = [30, 1, 200, 40] - self.init_y_shape = [None, 1, 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 = matmul_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMatmulWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 1, 40, 200] - self.init_x_shape = [None, None, None, 200] - self.y_shape = [1, 30, 200, 40] - self.init_y_shape = [None, 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 = matmul_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimMatmulWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 1, 40, 200] - self.init_x_shape = [None, 1, None, 200] - self.y_shape = [1, 30, 200, 40] - self.init_y_shape = [1, 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 = matmul_net - self.enable_cinn = False - self.tol = 1e-5 - - -class TestPrimTanhWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = tanh_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimCumsumWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = cumsum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimFloorWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = floor_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimReshapeWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = reshape_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimErfWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = erf_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimLeakyReluWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = leaky_relu_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimSoftsignWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = softsign_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [200, 40] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [200, 40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [1, 1] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaximumWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [1, 1] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = maximum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad1(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad2(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [200, 40] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad3(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [200, 40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad4(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [40] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad5(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [1, 1] - self.y_shape = [30, 200, 40] - self.init_x_shape = [None, None] - self.init_y_shape = [None, None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMinimumWithGrad6(TestPrimTwoWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.y_shape = [1, 1] - self.init_x_shape = [None, None, None] - self.init_y_shape = [None, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.y = np.random.random(self.y_shape).astype(self.dtype) - self.net = minimum_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimGeluWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = gelu_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimGeluWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = gelu_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimGeluWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float16" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.x = np.random.uniform(-1, 1, size=self.x_shape).astype(self.dtype) - self.net = gelu_net1 - self.enable_cinn = False - self.rtol = 1e-5 - self.atol = 0.0005 - - def test_prim_all_dynamic(self): - if not paddle.is_compiled_with_cuda(): - return - place = core.CUDAPlace(0) - if not core.is_float16_supported(place): - return - - res_ref, grad_ref = self.base_net() - res, grad = self.base_net("prim") - - for ref, actual in zip(res_ref, res): - np.testing.assert_allclose( - ref, actual, rtol=self.rtol, atol=self.atol - ) - - for dr, d in zip(grad_ref, grad): - np.testing.assert_allclose(dr, d, rtol=self.rtol, atol=self.atol) - - -class TestPrimGeluWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float16" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, None] - self.x = np.random.uniform(-1, 1, size=self.x_shape).astype(self.dtype) - self.net = gelu_net2 - self.enable_cinn = False - self.rtol = 1e-5 - self.atol = 0.0005 - - def test_prim_all_dynamic(self): - if not paddle.is_compiled_with_cuda(): - return - place = core.CUDAPlace(0) - if not core.is_float16_supported(place): - return - - res_ref, grad_ref = self.base_net() - res, grad = self.base_net("prim") - - for ref, actual in zip(res_ref, res): - np.testing.assert_allclose( - ref, actual, rtol=self.rtol, atol=self.atol - ) - - for dr, d in zip(grad_ref, grad): - np.testing.assert_allclose(dr, d, rtol=self.rtol, atol=self.atol) - - -class TestPrimHardswishWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = hardswish_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimBaseOneGradTwoInputs(unittest.TestCase): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.y_shape = [200, 40] - self.init_y_shape = [None, 200] - 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 = reduce_as_net - self.enable_cinn = False - self.tol = 1e-5 - self.y_without_grad = True - - def base_net(self, flag=None): - if flag == "prim": - core._set_prim_all_enabled(True) - x = paddle.to_tensor(self.x, stop_gradient=False) - y = paddle.to_tensor(self.y, stop_gradient=False) - if flag == "prim": - fn = apply_to_static( - self.net, - use_cinn=self.enable_cinn, - input_spec=[ - InputSpec(shape=self.init_x_shape, dtype='float32'), - InputSpec(shape=self.init_y_shape, dtype='float32'), - ], - ) - fn.train() - else: - fn = self.net - res = fn(x, y) - res.backward() - if self.y_without_grad: - grad = x.gradient() - else: - grad = y.gradient() - if flag == "prim": - core._set_prim_all_enabled(False) - return res, [grad] - - def test_prim_all_dynamic(self): - res_ref, grad_ref = self.base_net() - res, grad = self.base_net("prim") - - for ref, actual in zip(res_ref, res): - np.testing.assert_allclose( - ref, actual, rtol=self.tol, atol=self.tol - ) + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose( + ref, actual, rtol=self.tol, atol=self.tol + ) for dr, d in zip(grad_ref, grad): np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) -class TestPrimReduceAsWithGrad2(TestPrimBaseOneGradTwoInputs): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.y_shape = [30, 1, 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 = reduce_as_net - self.enable_cinn = False - self.tol = 1e-5 - self.y_without_grad = True - - -class TestPrimReduceAsWithGrad3(TestPrimBaseOneGradTwoInputs): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.y_shape = [30, 200, 1] - self.init_y_shape = [None, None, 1] - 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 = reduce_as_net - self.enable_cinn = False - self.tol = 1e-5 - self.y_without_grad = True - - -class TestPrimReduceAsWithGrad4(TestPrimBaseOneGradTwoInputs): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.y_shape = [30, 1, 1] - self.init_y_shape = [None, None, 1] - 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 = reduce_as_net - self.enable_cinn = False - self.tol = 1e-5 - self.y_without_grad = True - - -class TestPrimMaxWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - self.dtype = "float32" - self.x_shape = [30] - self.init_x_shape = [None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = max_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net3 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad5(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net4 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad6(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net5 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimMaxWithGrad7(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2024) - 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 = max_net6 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimExpandWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [200, 40] - self.init_x_shape = [None, 40] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = expand_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimExpandWithGrad2(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 1, 40] - self.init_x_shape = [None, None, 40] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = expand_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimExpandWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 1] - self.init_x_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = expand_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimExpandWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 1, 1] - self.init_x_shape = [None, None, 1] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = expand_net - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad1(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = stack_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad2(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 = stack_net1 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad3(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - 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.net = stack_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad4(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = stack_net2 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad5(TestPrimConcatWithGrad5): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, None, 40] - x = np.random.random(self.x_shape).astype(self.dtype) - self.x = [x + i for i in range(4)] - self.net = stack_net3 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimStackWithGrad6(TestPrimConcatWithGrad5): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [30, 200, 40] - self.init_x_shape = [None, 200, None] - x = np.random.random(self.x_shape).astype(self.dtype) - self.x = [x + i for i in range(4)] - self.net = stack_net3 - self.enable_cinn = False - self.tol = 1e-6 - - -class TestPrimPadWithGrad(TestPrimBaseWithGrad): - def setUp(self): - np.random.seed(2023) - self.dtype = "float32" - self.x_shape = [10, 20, 30, 40] - self.init_x_shape = [None, None, None, 40] - self.x = np.random.random(self.x_shape).astype(self.dtype) - self.net = pad_net - self.enable_cinn = False - self.tol = 1e-6 - - if __name__ == "__main__": unittest.main() diff --git a/test/prim/pir_prim/test_prim_sub_graph_fghij_backward_dynamic_shape.py b/test/prim/pir_prim/test_prim_sub_graph_fghij_backward_dynamic_shape.py new file mode 100644 index 0000000000000..55ff82b9a819c --- /dev/null +++ b/test/prim/pir_prim/test_prim_sub_graph_fghij_backward_dynamic_shape.py @@ -0,0 +1,151 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from test_prim_sub_graph_backward_dynamic_shape import TestPrimBaseWithGrad + +import paddle +from paddle.framework import core + + +def floor_net(x): + return paddle.floor(x) + + +def gelu_net1(x): + return paddle.nn.functional.gelu(x, approximate=True) + + +def gelu_net2(x): + return paddle.nn.functional.gelu(x, approximate=False) + + +def hardswish_net(x): + return paddle.nn.functional.hardswish(x) + + +class TestPrimFloorWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = floor_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimGeluWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = gelu_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimGeluWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = gelu_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimGeluWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float16" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.x = np.random.uniform(-1, 1, size=self.x_shape).astype(self.dtype) + self.net = gelu_net1 + self.enable_cinn = False + self.rtol = 1e-5 + self.atol = 0.0005 + + def test_prim_all_dynamic(self): + if not paddle.is_compiled_with_cuda(): + return + place = core.CUDAPlace(0) + if not core.is_float16_supported(place): + return + + res_ref, grad_ref = self.base_net() + res, grad = self.base_net("prim") + + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose( + ref, actual, rtol=self.rtol, atol=self.atol + ) + + for dr, d in zip(grad_ref, grad): + np.testing.assert_allclose(dr, d, rtol=self.rtol, atol=self.atol) + + +class TestPrimGeluWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float16" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.x = np.random.uniform(-1, 1, size=self.x_shape).astype(self.dtype) + self.net = gelu_net2 + self.enable_cinn = False + self.rtol = 1e-5 + self.atol = 0.0005 + + def test_prim_all_dynamic(self): + if not paddle.is_compiled_with_cuda(): + return + place = core.CUDAPlace(0) + if not core.is_float16_supported(place): + return + + res_ref, grad_ref = self.base_net() + res, grad = self.base_net("prim") + + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose( + ref, actual, rtol=self.rtol, atol=self.atol + ) + + for dr, d in zip(grad_ref, grad): + np.testing.assert_allclose(dr, d, rtol=self.rtol, atol=self.atol) + + +class TestPrimHardswishWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = hardswish_net + self.enable_cinn = False + self.tol = 1e-6 + + +if __name__ == "__main__": + unittest.main() diff --git a/test/prim/pir_prim/test_prim_sub_graph_klmno_backward_dynamic_shape.py b/test/prim/pir_prim/test_prim_sub_graph_klmno_backward_dynamic_shape.py new file mode 100644 index 0000000000000..00bc44498a3f0 --- /dev/null +++ b/test/prim/pir_prim/test_prim_sub_graph_klmno_backward_dynamic_shape.py @@ -0,0 +1,635 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from test_prim_sub_graph_backward_dynamic_shape import ( + TestPrimBaseWithGrad, + TestPrimTwoWithGrad, +) + +import paddle + + +def leaky_relu_net(x): + return paddle.nn.functional.leaky_relu(x) + + +def matmul_net(x, y): + return paddle.matmul(x, y) + + +def max_net1(x): + return paddle.max(x, keepdim=True) + + +def max_net2(x): + return paddle.max(x, keepdim=False) + + +def max_net3(x): + return paddle.max(x, axis=[0, 1], keepdim=False) + + +def max_net4(x): + return paddle.max(x, axis=[-1, -2], keepdim=False) + + +def max_net5(x): + return paddle.max(x, axis=[-1, 0], keepdim=False) + + +def max_net6(x): + return paddle.max(x) + + +def maximum_net(x, y): + return paddle.maximum(x, y) + + +def mean_net1(x): + return paddle.mean(x, axis=1, keepdim=False) + + +def mean_net2(x): + return paddle.mean(x, axis=-1, keepdim=False) + + +def mean_net3(x): + return paddle.mean(x, axis=[0, 2], keepdim=False) + + +def minimum_net(x, y): + return paddle.minimum(x, y) + + +def multiply_net(x, y): + return x * y + + +class TestPrimLeakyReluWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = leaky_relu_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMatmulWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 40, 200] + self.init_x_shape = [None, None, 200] + self.y_shape = [30, 200, 40] + self.init_y_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 = matmul_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMatmulWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 30, 40, 200] + self.init_x_shape = [None, None, None, 200] + self.y_shape = [30, 1, 200, 40] + self.init_y_shape = [None, 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 = matmul_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMatmulWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 30, 40, 200] + self.init_x_shape = [1, None, None, 200] + self.y_shape = [30, 1, 200, 40] + self.init_y_shape = [None, 1, 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 = matmul_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMatmulWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 1, 40, 200] + self.init_x_shape = [None, None, None, 200] + self.y_shape = [1, 30, 200, 40] + self.init_y_shape = [None, 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 = matmul_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMatmulWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 1, 40, 200] + self.init_x_shape = [None, 1, None, 200] + self.y_shape = [1, 30, 200, 40] + self.init_y_shape = [1, 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 = matmul_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMaxWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30] + self.init_x_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = max_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net3 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad5(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net4 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad6(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net5 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaxWithGrad7(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = max_net6 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [200, 40] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [200, 40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [1, 1] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMaximumWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [1, 1] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = maximum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMeanWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = mean_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMeanWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = mean_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMeanWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = mean_net3 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [200, 40] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [200, 40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [40] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [1, 1] + self.y_shape = [30, 200, 40] + self.init_x_shape = [None, None] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMinimumWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2024) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.y_shape = [1, 1] + self.init_x_shape = [None, None, None] + self.init_y_shape = [None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = minimum_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimMultiplyWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 1, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 1, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad7(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad8(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [40] + self.init_y_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad9(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [40] + self.init_x_shape = [None] + self.y_shape = [30, 200, 40] + self.init_y_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 = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad10(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.y_shape = [200, 40] + self.init_y_shape = self.y_shape + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +class TestPrimMultiplyWithGrad11(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = self.x_shape + self.y_shape = [30, 200, 40] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = multiply_net + self.enable_cinn = False + self.tol = 1e-5 + + +if __name__ == "__main__": + unittest.main() diff --git a/test/prim/pir_prim/test_prim_sub_graph_pqrst_backward_dynamic_shape.py b/test/prim/pir_prim/test_prim_sub_graph_pqrst_backward_dynamic_shape.py new file mode 100644 index 0000000000000..bc3a5ab5172be --- /dev/null +++ b/test/prim/pir_prim/test_prim_sub_graph_pqrst_backward_dynamic_shape.py @@ -0,0 +1,762 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from test_prim_sub_graph_backward_dynamic_shape import ( + TestPrimBaseOneGradTwoInputs, + TestPrimBaseWithGrad, + TestPrimTwoWithGrad, + apply_to_static, +) + +import paddle +from paddle.framework import core +from paddle.static import InputSpec + + +def pad_net(x): + return paddle.nn.functional.pad(x, [0, 1, 2]) + + +def pow_net(x): + return paddle.pow(x, 3.2) + + +def scale_net(x): + return paddle.scale(x, scale=-2.3) + + +def reduce_as_net(x, y): + return paddle.reduce_as(x, y) + + +def relu_net(x): + return paddle.nn.functional.relu(x) + + +def reshape_net(x): + return paddle.reshape(x, [30, 200 * 40]) + + +def sigmoid_net(x): + return paddle.nn.functional.sigmoid(x) + + +def softmax_net(x): + return paddle.nn.functional.softmax(x, axis=-1) + + +def softsign_net(x): + return paddle.nn.functional.softsign(x) + + +def split_net1(x): + res = paddle.split(x, num_or_sections=10, axis=-1) + tmp_res = res[0] + for i in range(1, len(res)): + tmp_res = tmp_res + res[i] * i + return tmp_res / len(res) + + +def split_net2(x): + res = paddle.split(x, num_or_sections=10, axis=1) + tmp_res = res[0] + for i in range(1, len(res)): + tmp_res = tmp_res + res[i] * i + return tmp_res / len(res) + + +def square_net(x): + return paddle.square(x) + + +def stack_net1(x): + y = x + 1 + return paddle.stack([x, y], axis=-1) + + +def stack_net2(x): + y = x + 1 + return paddle.stack([x, y], axis=1) + + +def stack_net3(x): + return paddle.stack(x, axis=0) + + +def subtract_net(x, y): + return x - y + + +def sum_net1(x): + return paddle.sum(x, axis=1, keepdim=False) + + +def sum_net2(x): + return paddle.sum(x) + + +def sum_net3(x): + return paddle.sum(x, keepdim=True) + + +def sum_net4(x): + return paddle.sum(x, axis=-1, keepdim=False) + + +def sum_net5(x): + return paddle.sum(x, axis=[0, 2], keepdim=False) + + +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 tanh_net(x): + return paddle.tanh(x) + + +def transpose_net(x): + return paddle.transpose(x, perm=[0, 3, 1, 2]) + + +class TestPrimPadWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [10, 20, 30, 40] + self.init_x_shape = [None, None, None, 40] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = pad_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimPowWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [100, 20, 30] + self.init_x_shape = [None, None, 30] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = pow_net + self.enable_cinn = False + 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 TestPrimReduceAsWithGrad2(TestPrimBaseOneGradTwoInputs): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.y_shape = [30, 1, 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 = reduce_as_net + self.enable_cinn = False + self.tol = 1e-5 + self.y_without_grad = True + + +class TestPrimReduceAsWithGrad3(TestPrimBaseOneGradTwoInputs): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + 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 = reduce_as_net + self.enable_cinn = False + self.tol = 1e-5 + self.y_without_grad = True + + +class TestPrimReduceAsWithGrad4(TestPrimBaseOneGradTwoInputs): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.y_shape = [30, 1, 1] + self.init_y_shape = [None, None, 1] + 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 = reduce_as_net + self.enable_cinn = False + self.tol = 1e-5 + self.y_without_grad = True + + +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 TestPrimReshapeWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = reshape_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 + + +class TestPrimSoftmaxWithGrad1(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 = softmax_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSoftmaxWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = softmax_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSoftmaxWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [30, 200, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = softmax_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSoftsignWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = softsign_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSplitWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = split_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSplitWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = split_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSplitWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = split_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSplitWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = split_net2 + 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 TestPrimStackWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = stack_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimStackWithGrad2(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 = stack_net1 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimStackWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = stack_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimStackWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = stack_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimStackWithGrad5(unittest.TestCase): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + x = np.random.random(self.x_shape).astype(self.dtype) + self.x = [x + i for i in range(4)] + self.net = stack_net3 + self.enable_cinn = False + self.tol = 1e-6 + + def base_net(self, flag=None): + if flag == "prim": + core._set_prim_all_enabled(True) + x = [paddle.to_tensor(self.x[i], stop_gradient=False) for i in range(4)] + if flag == "prim": + fn = apply_to_static( + self.net, + use_cinn=self.enable_cinn, + input_spec=[ + [ + InputSpec(shape=self.x_shape, dtype='float32'), + InputSpec(shape=self.init_x_shape, dtype='float32'), + InputSpec(shape=self.init_x_shape, dtype='float32'), + InputSpec(shape=self.x_shape, dtype='float32'), + ] + ], + ) + fn.train() + else: + fn = self.net + res = fn(x) + res.backward() + x_grad1 = x[0].gradient() + x_grad2 = x[1].gradient() + x_grad3 = x[2].gradient() + x_grad4 = x[3].gradient() + if flag == "prim": + core._set_prim_all_enabled(False) + return res, [x_grad1, x_grad2, x_grad3, x_grad4] + + def test_prim_all_dynamic(self): + res_ref, grad_ref = self.base_net() + res, grad = self.base_net("prim") + + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose( + ref, actual, rtol=self.tol, atol=self.tol + ) + + for dr, d in zip(grad_ref, grad): + np.testing.assert_allclose(dr, d, rtol=self.tol, atol=self.tol) + + +class TestPrimStackWithGrad6(TestPrimStackWithGrad5): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, 200, None] + x = np.random.random(self.x_shape).astype(self.dtype) + self.x = [x + i for i in range(4)] + self.net = stack_net3 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad1(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 1, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad2(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad3(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 1] + self.init_x_shape = [None, None, 1] + self.y_shape = [30, 200, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad4(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 1, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad5(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [1, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad6(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 1] + self.init_y_shape = [None, None, 1] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad7(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [30, 200, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad8(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, 40] + self.y_shape = [40] + self.init_y_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad9(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [40] + self.init_x_shape = [None] + self.y_shape = [30, 200, 40] + self.init_y_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 = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad10(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [30, 200, 40] + self.init_x_shape = [None, None, None] + self.y_shape = [200, 40] + self.init_y_shape = self.y_shape + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSubtractWithGrad11(TestPrimTwoWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [200, 40] + self.init_x_shape = self.x_shape + self.y_shape = [30, 200, 40] + self.init_y_shape = [None, None, None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.y = np.random.random(self.y_shape).astype(self.dtype) + self.net = subtract_net + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSumWithGrad1(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + self.x_shape = [1000] + self.init_x_shape = [None] + self.x = np.random.random(self.x_shape).astype(self.dtype) + self.net = sum_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSumWithGrad2(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = sum_net3 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSumWithGrad3(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = sum_net2 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSumWithGrad4(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = sum_net4 + self.enable_cinn = False + self.tol = 1e-6 + + +class TestPrimSumWithGrad5(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2023) + self.dtype = "float32" + 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.net = sum_net5 + 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 + + +class TestPrimTanhWithGrad(TestPrimBaseWithGrad): + def setUp(self): + np.random.seed(2024) + 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 = tanh_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 + + +if __name__ == "__main__": + unittest.main()