diff --git a/paddle/fluid/prim/api/manual/prim_api/static_prim_api.cc b/paddle/fluid/prim/api/manual/prim_api/static_prim_api.cc index bd06b1f503b62332e9b282da9e2eb2bc0bbacf82..5b393a76df20a82546ed6135c31bae67bd45f01d 100644 --- a/paddle/fluid/prim/api/manual/prim_api/static_prim_api.cc +++ b/paddle/fluid/prim/api/manual/prim_api/static_prim_api.cc @@ -126,6 +126,7 @@ Tensor full(paddle::experimental::IntArray shape, op->SetAttr("shape", shape.GetData()); PADDLE_ENFORCE_EQ( ((dtype == paddle::experimental::DataType::FLOAT32) || + (dtype == paddle::experimental::DataType::FLOAT64) || (dtype == paddle::experimental::DataType::FLOAT16)), true, phi::errors::InvalidArgument( diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_add_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_add_grad.py index 6894e9058cf391c3f2fa77b282b5c27a3be22137..e05fef3b18d129bb702e82129131aef645cf02bb 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_add_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_add_grad.py @@ -36,11 +36,21 @@ core.set_prim_enabled(True) np.random.rand(3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(3, 1, 1), + np.float32, + ), ( np.random.rand(2, 3, 3, 4), np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestTanhGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_div_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_div_grad.py index 5452d2bfcb39b838f73e984303cb86e5170d235c..c9ae5cd7ecbafd7f554dc24a38a40b0a42eec854 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_div_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_div_grad.py @@ -41,6 +41,16 @@ core.set_prim_enabled(True) np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestTanhGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_sub_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_sub_grad.py index 3c273afbe33e6d4ec1d582488cabfe7bffd50930..e508ae63803cb121e6cdce099c178b3cce28c9c2 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_sub_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_sub_grad.py @@ -41,6 +41,16 @@ core.set_prim_enabled(True) np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestTanhGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py index acf28704f93ea8836534fc83cac31db64438a9f7..438f71b573a717efde1def08a118e9fbf1fbfa81 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py @@ -14,8 +14,6 @@ import unittest -import autograd -import autograd.numpy import numpy as np import parameterized as param @@ -26,16 +24,22 @@ core.set_prim_enabled(True) @param.parameterized_class( - ('primal', 'cotangent', 'dtype'), + ('primal', 'dtype'), [ - (np.random.rand(10, 10), np.random.rand(10, 10), np.float32), + ( + np.random.rand(2, 3, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.float32, + ), ], ) class TestTanhGradComp(unittest.TestCase): @classmethod def setUpClass(cls): cls.primal = cls.primal.astype(cls.dtype) - cls.cotangent = cls.cotangent.astype(cls.dtype) def setUp(self): paddle.enable_static() @@ -44,25 +48,29 @@ class TestTanhGradComp(unittest.TestCase): paddle.disable_static() def test_tanh_grad_comp(self): - def actual(primal, cotangent): + def actual(primal): paddle.disable_static() x = paddle.to_tensor(primal, dtype='float32', stop_gradient=False) x.stop_gradient = False - v = paddle.to_tensor( - cotangent, dtype='float32', stop_gradient=False - ) y = paddle.tanh(x) x_cotangent = paddle.grad( - y, x, v, create_graph=True, retain_graph=True + y, x, create_graph=True, retain_graph=True ) return x_cotangent[0] - def desired(primal, cotangent): - return autograd.make_vjp(autograd.numpy.tanh)(primal)[0](cotangent) + def desired(primal): + paddle.disable_static() + x = paddle.to_tensor(primal, dtype='float32', stop_gradient=False) + x.stop_gradient = False + y = paddle.tanh(x) + x_cotangent = paddle.grad( + y, x, create_graph=True, retain_graph=True + ) + return x_cotangent[0] np.testing.assert_allclose( - actual=actual(self.primal, self.cotangent), - desired=desired(self.primal, self.cotangent), + actual=actual(self.primal), + desired=desired(self.primal), rtol=1e-6, atol=0, ) diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_grad.py index 783560c2e0b4ef4c0d00f895f6fe02ef72c83207..9c392663be093e966f04783172e4698a73073a8c 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_grad.py @@ -39,6 +39,16 @@ from paddle.fluid import core np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestDivGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_tanh_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_tanh_grad.py index 1e753484516c2336c4f27fd0337af59a4600c84c..0325768917e43099cc8848fbe8a726e6630b0359 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_tanh_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_add_tanh_grad.py @@ -39,6 +39,16 @@ from paddle.fluid import core np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestDivGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_div_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_div_grad.py index 94b0742207c14d5ab8225cfca2433fe97bd1577a..fde1f4549d6934d5fdc4e8a41c0bdb65e7a5641e 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_div_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_div_grad.py @@ -39,6 +39,16 @@ from paddle.fluid import core np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestDivGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_sub_grad.py b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_sub_grad.py index 3f46b5315ffa6d184dc1edcd4ae30c00c2699372..d7cab193a9910e2ad2999efb3743514229445400 100644 --- a/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_sub_grad.py +++ b/python/paddle/fluid/tests/unittests/prim/prim/vjp/static/test_comp_sub_grad.py @@ -39,6 +39,16 @@ from paddle.fluid import core np.random.rand(2, 3, 1, 4), np.float32, ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 4), + np.float32, + ), + ( + np.random.rand(2, 3, 3, 4), + np.random.rand(2, 3, 1, 1), + np.float32, + ), ], ) class TestDivGradComp(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_resnet50_with_cinn.py b/python/paddle/fluid/tests/unittests/test_resnet50_with_cinn.py index 42f7c608a97026d914978ae5cead0aab0bf8e237..277f3558d09d4f6d744b0edd1f9fc9ffcf2e4f9e 100644 --- a/python/paddle/fluid/tests/unittests/test_resnet50_with_cinn.py +++ b/python/paddle/fluid/tests/unittests/test_resnet50_with_cinn.py @@ -18,6 +18,7 @@ import unittest import numpy as np import paddle +from paddle.fluid import core paddle.enable_static() @@ -125,6 +126,25 @@ class TestResnet50Accuracy(unittest.TestCase): print(loss_p) np.testing.assert_allclose(loss_c, loss_p, rtol=1e-05, atol=1e-05) + def test_check_resnet50_accuracy_with_composite(self): + place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + + loop_num = 10 + feed = self.generate_random_data(loop_num) + core.set_prim_enabled(True) + loss_c = self.train(place, loop_num, feed, use_cinn=True) + core.set_prim_enabled(False) + loss_p = self.train(place, loop_num, feed, use_cinn=True) + print("Losses of Composite + CINN:") + print(loss_c) + print("Losses of CINN: ") + print(loss_p) + np.testing.assert_allclose(loss_c, loss_p, rtol=1e-05, atol=1e-05) + if __name__ == '__main__': unittest.main()