From f555abc801ed0c42560e587e0d48bac7d2228b94 Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Mon, 5 Jun 2023 15:01:42 +0800 Subject: [PATCH 1/6] [CINN] add paddle.device.is_run_with_cinn() api for running check --- paddle/fluid/pybind/pybind.cc | 10 ++++++++++ python/paddle/device/__init__.py | 2 ++ python/paddle/fluid/framework.py | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 0f1d9fc6782a45..2b410cc13ba009 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -312,6 +312,15 @@ bool IsCompiledWithCINN() { #endif } +bool IsRunWithCINN() { +#ifndef PADDLE_WITH_CINN + return false; +#else + return framework::paddle2cinn::CinnCompiler::GetInstance() + ->real_compiled_num() > 0; +#endif +} + bool IsCompiledWithHETERPS() { #ifndef PADDLE_WITH_HETERPS return false; @@ -1909,6 +1918,7 @@ All parameter, weight, gradient are variables in Paddle. m.def("is_compiled_with_mpi", IsCompiledWithMPI); m.def("is_compiled_with_mpi_aware", IsCompiledWithMPIAWARE); m.def("is_compiled_with_cinn", IsCompiledWithCINN); + m.def("is_run_with_cinn", IsRunWithCINN); m.def("_is_compiled_with_heterps", IsCompiledWithHETERPS); m.def("supports_bfloat16", SupportsBfloat16); m.def("supports_bfloat16_fast_performance", SupportsBfloat16FastPerformance); diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index c19e6e08b4d2d0..ff8570a50470b7 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -22,6 +22,7 @@ from paddle.fluid.framework import is_compiled_with_cinn # noqa: F401 from paddle.fluid.framework import is_compiled_with_cuda # noqa: F401 from paddle.fluid.framework import is_compiled_with_rocm # noqa: F401 +from paddle.fluid.framework import is_run_with_cinn # noqa: F401 from . import cuda from . import xpu @@ -47,6 +48,7 @@ 'set_stream', 'stream_guard', 'synchronize', + 'is_run_with_cinn', ] _cudnn_version = None diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 38b62736e58bba..f73326ca790fd8 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -62,6 +62,7 @@ 'device_guard', 'set_flags', 'get_flags', + 'is_run_with_cinn', ] EMPTY_VAR_NAME = core.kEmptyVarName() @@ -695,6 +696,26 @@ def is_compiled_with_cinn(): return core.is_compiled_with_cinn() +def is_run_with_cinn(): + """ + Whether the model is running with CINN. + + Make sure this whl package can be used to run the model on CINN, you can check it by calling `paddle.device.is_compiled_with_cinn()`. + + Make sure the model has run at least once, before the model run, the API will always return False. + + Returns: + Bool: `True` if CINN is currently available, otherwise `False`. + + Examples: + .. code-block:: python + + import paddle + run_cinn = paddle.device.is_run_with_cinn() + """ + return core.is_run_with_cinn() + + def is_compiled_with_cuda(): """ Whether this whl package can be used to run the model on GPU. From 321118a3e5a1852e86247cae4f3cb571c12bde5b Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Mon, 5 Jun 2023 20:42:37 +0800 Subject: [PATCH 2/6] for pass ci converge --- test/dygraph_to_static/test_cinn.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index 0ef5186dab2d0b..3c6ea1b70b8890 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -64,6 +64,13 @@ def train(self, use_cinn): sgd.clear_grad() res.append(out.numpy()) + + if use_cinn: + self.assertTrue( + paddle.device.is_run_with_cinn(), + msg="The test was not running with CINN! Please check.", + ) + return res def test_cinn(self): From 9f4747496f354365e98c85fbf98bbba677fa212f Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Tue, 6 Jun 2023 10:27:46 +0800 Subject: [PATCH 3/6] add check whl should compiled with cinn --- test/dygraph_to_static/test_cinn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index 3c6ea1b70b8890..1a01c088feb797 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -65,7 +65,7 @@ def train(self, use_cinn): res.append(out.numpy()) - if use_cinn: + if use_cinn and paddle.device.is_compiled_with_cinn(): self.assertTrue( paddle.device.is_run_with_cinn(), msg="The test was not running with CINN! Please check.", From b0c55429cef9577f9981ccaec5e1385b8bbde93e Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Tue, 6 Jun 2023 16:58:04 +0800 Subject: [PATCH 4/6] fix ci coverage bug --- test/dygraph_to_static/test_cinn.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index 1a01c088feb797..44dd9f7ab9ac12 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -70,6 +70,11 @@ def train(self, use_cinn): paddle.device.is_run_with_cinn(), msg="The test was not running with CINN! Please check.", ) + else: + self.assertFalse( + paddle.device.is_run_with_cinn(), + msg="The test should not running with CINN when the whl package was not compiled with CINN! Please check.", + ) return res From 0cd5693c248bacbf9c9de0adc93f715261476438 Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Wed, 7 Jun 2023 15:18:32 +0800 Subject: [PATCH 5/6] remove exposed API --- python/paddle/device/__init__.py | 2 -- python/paddle/fluid/framework.py | 21 --------------------- test/dygraph_to_static/test_cinn.py | 12 ------------ 3 files changed, 35 deletions(-) diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index ff8570a50470b7..c19e6e08b4d2d0 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -22,7 +22,6 @@ from paddle.fluid.framework import is_compiled_with_cinn # noqa: F401 from paddle.fluid.framework import is_compiled_with_cuda # noqa: F401 from paddle.fluid.framework import is_compiled_with_rocm # noqa: F401 -from paddle.fluid.framework import is_run_with_cinn # noqa: F401 from . import cuda from . import xpu @@ -48,7 +47,6 @@ 'set_stream', 'stream_guard', 'synchronize', - 'is_run_with_cinn', ] _cudnn_version = None diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index f73326ca790fd8..38b62736e58bba 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -62,7 +62,6 @@ 'device_guard', 'set_flags', 'get_flags', - 'is_run_with_cinn', ] EMPTY_VAR_NAME = core.kEmptyVarName() @@ -696,26 +695,6 @@ def is_compiled_with_cinn(): return core.is_compiled_with_cinn() -def is_run_with_cinn(): - """ - Whether the model is running with CINN. - - Make sure this whl package can be used to run the model on CINN, you can check it by calling `paddle.device.is_compiled_with_cinn()`. - - Make sure the model has run at least once, before the model run, the API will always return False. - - Returns: - Bool: `True` if CINN is currently available, otherwise `False`. - - Examples: - .. code-block:: python - - import paddle - run_cinn = paddle.device.is_run_with_cinn() - """ - return core.is_run_with_cinn() - - def is_compiled_with_cuda(): """ Whether this whl package can be used to run the model on GPU. diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index 44dd9f7ab9ac12..0ef5186dab2d0b 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -64,18 +64,6 @@ def train(self, use_cinn): sgd.clear_grad() res.append(out.numpy()) - - if use_cinn and paddle.device.is_compiled_with_cinn(): - self.assertTrue( - paddle.device.is_run_with_cinn(), - msg="The test was not running with CINN! Please check.", - ) - else: - self.assertFalse( - paddle.device.is_run_with_cinn(), - msg="The test should not running with CINN when the whl package was not compiled with CINN! Please check.", - ) - return res def test_cinn(self): From a03e878f9c287b369554c099c0b93d30478e8671 Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Thu, 8 Jun 2023 15:02:17 +0800 Subject: [PATCH 6/6] import is_run_with_cinn explicitly --- python/paddle/fluid/core.py | 4 ++++ test/dygraph_to_static/test_cinn.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/python/paddle/fluid/core.py b/python/paddle/fluid/core.py index bcfd845d7fcb0d..2f3c36bb3468e8 100644 --- a/python/paddle/fluid/core.py +++ b/python/paddle/fluid/core.py @@ -338,6 +338,10 @@ def to_list(s): from .libpaddle import _cleanup_mmap_fds from .libpaddle import _remove_tensor_list_mmap_fds from .libpaddle import _set_max_memory_map_allocation_pool_size + + # CINN + from .libpaddle import is_run_with_cinn + except Exception as e: if has_paddle_dy_lib: sys.stderr.write( diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index 0ef5186dab2d0b..d52364462791d4 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -64,6 +64,18 @@ def train(self, use_cinn): sgd.clear_grad() res.append(out.numpy()) + + if use_cinn and paddle.device.is_compiled_with_cinn(): + self.assertTrue( + paddle.framework.core.is_run_with_cinn(), + msg="The test was not running with CINN! Please check.", + ) + else: + self.assertFalse( + paddle.framework.core.is_run_with_cinn(), + msg="The test should not running with CINN when the whl package was not compiled with CINN! Please check.", + ) + return res def test_cinn(self):