From 70aff7db328b0ac604d8551c0fab50738b3f2c3f Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 18 Dec 2024 10:53:17 +0800 Subject: [PATCH 1/4] [SOT] Mark some APIs can be directly run in simulation mode --- .../executor/variables/callable.py | 17 ++++++++ python/paddle/jit/sot/utils/__init__.py | 1 + .../paddle/jit/sot/utils/paddle_api_config.py | 21 ++++++++++ test/sot/test_builtin_dispatch.py | 41 +++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/python/paddle/jit/sot/opcode_translator/executor/variables/callable.py b/python/paddle/jit/sot/opcode_translator/executor/variables/callable.py index 6a971cf074d0d..fa891d0bf524d 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/variables/callable.py +++ b/python/paddle/jit/sot/opcode_translator/executor/variables/callable.py @@ -36,6 +36,7 @@ is_break_graph_api, is_break_graph_tensor_methods, is_builtin_fn, + is_directly_run_api, is_not_supported_paddle_layer, is_paddle_api, magic_method_builtin_dispatch, @@ -699,6 +700,22 @@ def call_function(self, /, *args, **kwargs): ) return handler(*args, **kwargs) + # If API can be directly called in simulation mode (e.g. user defined native code + # without graph affect), we can directly call it. + if is_directly_run_api(self.value): + from ..function_graph import convert_to_py_value + + res = self.value( + *convert_to_py_value(args), + **convert_to_py_value(kwargs), + ) + + return VariableFactory.from_value( + res, + self.graph, + DummyTracker([self, *list(args), *list(kwargs.values())]), + ) + # Try to inline call the magic function magic_methods = magic_method_builtin_dispatch(self.value) for magic_method in magic_methods: diff --git a/python/paddle/jit/sot/utils/__init__.py b/python/paddle/jit/sot/utils/__init__.py index b491dca07a862..e35bc8af68947 100644 --- a/python/paddle/jit/sot/utils/__init__.py +++ b/python/paddle/jit/sot/utils/__init__.py @@ -49,6 +49,7 @@ from .paddle_api_config import ( # noqa: F401 get_tensor_methods, is_break_graph_tensor_methods, + is_directly_run_api, is_inplace_api, is_not_supported_paddle_layer, ) diff --git a/python/paddle/jit/sot/utils/paddle_api_config.py b/python/paddle/jit/sot/utils/paddle_api_config.py index cc0302a9bb844..5a858ed87bbeb 100644 --- a/python/paddle/jit/sot/utils/paddle_api_config.py +++ b/python/paddle/jit/sot/utils/paddle_api_config.py @@ -129,3 +129,24 @@ def is_break_graph_tensor_methods(method_name): def add_break_graph_apis(apis: list): break_graph_set.update(apis) + + +def is_directly_run_api(api): + NATIVE_CODE_PURE_FUNCTIONS = { + paddle.base.libpaddle.is_compiled_with_avx, + paddle.base.libpaddle.is_compiled_with_cuda, + paddle.base.libpaddle.is_compiled_with_cudnn_frontend, + paddle.base.libpaddle.is_compiled_with_rocm, + paddle.base.libpaddle.is_compiled_with_custom_device, + paddle.base.libpaddle.is_compiled_with_ipu, + paddle.base.libpaddle.is_compiled_with_xpu, + paddle.base.libpaddle.is_compiled_with_mkldnn, + paddle.base.libpaddle.is_compiled_with_nccl, + paddle.base.libpaddle.is_compiled_with_mpi, + paddle.base.libpaddle.is_compiled_with_mpi_aware, + paddle.base.libpaddle.is_compiled_with_cinn, + paddle.base.libpaddle.is_compiled_with_distribute, + paddle.base.libpaddle.is_compiled_with_brpc, + paddle.base.libpaddle.is_compiled_with_dist, + } + return api in NATIVE_CODE_PURE_FUNCTIONS diff --git a/test/sot/test_builtin_dispatch.py b/test/sot/test_builtin_dispatch.py index d710c12141032..c58e4e0c7d034 100644 --- a/test/sot/test_builtin_dispatch.py +++ b/test/sot/test_builtin_dispatch.py @@ -388,5 +388,46 @@ def test_builtin_type_conversion_breakgraph(self): ) +@check_no_breakgraph +def test_native_code_function(): + res1 = paddle.base.libpaddle.is_compiled_with_avx() + res2 = paddle.base.libpaddle.is_compiled_with_cuda() + res3 = paddle.base.libpaddle.is_compiled_with_cudnn_frontend() + res4 = paddle.base.libpaddle.is_compiled_with_rocm() + res5 = paddle.base.libpaddle.is_compiled_with_custom_device("npu") + res6 = paddle.base.libpaddle.is_compiled_with_ipu() + res7 = paddle.base.libpaddle.is_compiled_with_xpu() + res8 = paddle.base.libpaddle.is_compiled_with_mkldnn() + res9 = paddle.base.libpaddle.is_compiled_with_nccl() + res10 = paddle.base.libpaddle.is_compiled_with_mpi() + res11 = paddle.base.libpaddle.is_compiled_with_mpi_aware() + res12 = paddle.base.libpaddle.is_compiled_with_cinn() + res13 = paddle.base.libpaddle.is_compiled_with_distribute() + res14 = paddle.base.libpaddle.is_compiled_with_brpc() + res15 = paddle.base.libpaddle.is_compiled_with_dist() + return ( + res1, + res2, + res3, + res4, + res5, + res6, + res7, + res8, + res9, + res10, + res11, + res12, + res13, + res14, + res15, + ) + + +class TestNativeCodeFunction(TestCaseBase): + def test_native_code_function(self): + self.assert_results(test_native_code_function) + + if __name__ == "__main__": unittest.main() From 1b6133eb027220080407a346bf0e4240ff36b212 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 18 Dec 2024 11:28:38 +0800 Subject: [PATCH 2/4] add `__dict__` to `DYGRAPH_ONLY_TENSOR_ATTRS_ALLOW_LIST` --- test/dygraph_to_static/test_tensor_attr_consistency.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/dygraph_to_static/test_tensor_attr_consistency.py b/test/dygraph_to_static/test_tensor_attr_consistency.py index b59456c0fd174..a198f974628d8 100644 --- a/test/dygraph_to_static/test_tensor_attr_consistency.py +++ b/test/dygraph_to_static/test_tensor_attr_consistency.py @@ -26,6 +26,7 @@ '__len__', '__long__', '__nonzero__', + '__dict__', 'apply_', 'backward', 'clear_grad', From 3f99ca832176861bd4697e4d8189aa6c6d227780 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 18 Dec 2024 11:31:12 +0800 Subject: [PATCH 3/4] Revert "add `__dict__` to `DYGRAPH_ONLY_TENSOR_ATTRS_ALLOW_LIST`" This reverts commit 1b6133eb027220080407a346bf0e4240ff36b212. --- test/dygraph_to_static/test_tensor_attr_consistency.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/dygraph_to_static/test_tensor_attr_consistency.py b/test/dygraph_to_static/test_tensor_attr_consistency.py index a198f974628d8..b59456c0fd174 100644 --- a/test/dygraph_to_static/test_tensor_attr_consistency.py +++ b/test/dygraph_to_static/test_tensor_attr_consistency.py @@ -26,7 +26,6 @@ '__len__', '__long__', '__nonzero__', - '__dict__', 'apply_', 'backward', 'clear_grad', From d5cc8b83799f2eacbaa2b69f0d4ec3a09d2f4511 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 18 Dec 2024 14:02:38 +0800 Subject: [PATCH 4/4] skip unhashable apis --- python/paddle/jit/sot/utils/paddle_api_config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/paddle/jit/sot/utils/paddle_api_config.py b/python/paddle/jit/sot/utils/paddle_api_config.py index 5a858ed87bbeb..d973599007233 100644 --- a/python/paddle/jit/sot/utils/paddle_api_config.py +++ b/python/paddle/jit/sot/utils/paddle_api_config.py @@ -132,6 +132,10 @@ def add_break_graph_apis(apis: list): def is_directly_run_api(api): + from .utils import hashable + + if not hashable(api): + return False NATIVE_CODE_PURE_FUNCTIONS = { paddle.base.libpaddle.is_compiled_with_avx, paddle.base.libpaddle.is_compiled_with_cuda,