From af8a9a489e76d1b1bf30e5c9fa6df0e5392d35a5 Mon Sep 17 00:00:00 2001 From: hjyp <2594297576@qq.com> Date: Sun, 1 Jan 2023 20:07:24 +0800 Subject: [PATCH 1/3] Add ignore_module API --- python/paddle/jit/__init__.py | 1 + python/paddle/jit/api.py | 29 +++++++++++++++++++ .../paddle/jit/dy2static/convert_call_func.py | 11 +++++++ 3 files changed, 41 insertions(+) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index fd5ca115c2e2e7..aabd6b777456f3 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -27,6 +27,7 @@ 'save', 'load', 'to_static', + 'ignore_module', 'ProgramTranslator', 'TranslatedLayer', 'set_code_level', diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 199667d3cb1923..35cc1c35153f22 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -41,6 +41,7 @@ from .dy2static.convert_call_func import ( ConversionOptions, CONVERSION_OPTIONS, + add_ignore_module, ) from .dy2static.program_translator import ( ProgramTranslator, @@ -190,6 +191,34 @@ def copy_decorator_attrs(original_func, decorated_obj): return decorated_obj +def ignore_module(modules: List): + """ + Adds modules that ignore transcription. + Builtin modules that have been ignored are collections, pdb, copy, inspect, re, numpy, logging, six + + Args: + modules (list[]): Ignored modules that you want to add + + Examples: + .. code-block:: python + + import scipy + import pandas + + import paddle + from paddle.jit import ignore_module + + modules = [ + scipy, + pandas + ] + + ignore_module(modules) + + """ + add_ignore_module(modules) + + def to_static( function=None, input_spec=None, build_strategy=None, property=False ): diff --git a/python/paddle/jit/dy2static/convert_call_func.py b/python/paddle/jit/dy2static/convert_call_func.py index 26264840f26768..2e4a6be7bf865b 100644 --- a/python/paddle/jit/dy2static/convert_call_func.py +++ b/python/paddle/jit/dy2static/convert_call_func.py @@ -21,6 +21,7 @@ import pdb import re import types +from typing import List import numpy @@ -105,6 +106,16 @@ def builtin_modules(): BUILTIN_LIKELY_MODULES = builtin_modules() +def add_ignore_module(modules: List): + """ + Adds modules that ignore transcription + """ + global BUILTIN_LIKELY_MODULES + for module in modules: + if module not in BUILTIN_LIKELY_MODULES: + BUILTIN_LIKELY_MODULES.append(module) + + def is_unsupported(func): """ Checks whether the func is supported by dygraph to static graph. From a53c766a827e812084328ef489757685242006b0 Mon Sep 17 00:00:00 2001 From: hjyp <2594297576@qq.com> Date: Mon, 9 Jan 2023 12:57:54 +0800 Subject: [PATCH 2/3] fix type of parameter --- python/paddle/jit/__init__.py | 1 + python/paddle/jit/api.py | 10 +++++----- python/paddle/jit/dy2static/convert_call_func.py | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index aabd6b777456f3..00f5f609430db8 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -17,6 +17,7 @@ from .api import load from .api import to_static from .api import not_to_static +from .api import ignore_module from .dy2static.logging_utils import set_code_level, set_verbosity from . import dy2static diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 818aa5ac676741..88988016401c11 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -24,7 +24,7 @@ from collections import OrderedDict import inspect import threading -from typing import Any +from typing import Any, List import paddle from paddle.fluid import core, dygraph @@ -193,26 +193,26 @@ def copy_decorator_attrs(original_func, decorated_obj): return decorated_obj -def ignore_module(modules: List): +def ignore_module(modules: List[Any]): """ Adds modules that ignore transcription. Builtin modules that have been ignored are collections, pdb, copy, inspect, re, numpy, logging, six Args: - modules (list[]): Ignored modules that you want to add + modules (List[Any]): Ignored modules that you want to add Examples: .. code-block:: python import scipy - import pandas + import astor import paddle from paddle.jit import ignore_module modules = [ scipy, - pandas + astor ] ignore_module(modules) diff --git a/python/paddle/jit/dy2static/convert_call_func.py b/python/paddle/jit/dy2static/convert_call_func.py index a99a217de7b7cf..d4bb0513a6ba8e 100644 --- a/python/paddle/jit/dy2static/convert_call_func.py +++ b/python/paddle/jit/dy2static/convert_call_func.py @@ -21,7 +21,7 @@ import pdb import re import types -from typing import List +from typing import Any, List import numpy @@ -101,7 +101,7 @@ def builtin_modules(): BUILTIN_LIKELY_MODULES = builtin_modules() -def add_ignore_module(modules: List): +def add_ignore_module(modules: List[Any]): """ Adds modules that ignore transcription """ From a7415636d2253d361415d5887281efedebff85db Mon Sep 17 00:00:00 2001 From: hjyp <2594297576@qq.com> Date: Mon, 9 Jan 2023 20:29:57 +0800 Subject: [PATCH 3/3] Add test case of ignore-module --- .../dygraph_to_static/test_ignore_module.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/dygraph_to_static/test_ignore_module.py diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ignore_module.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ignore_module.py new file mode 100644 index 00000000000000..d1ef9e0be23f6c --- /dev/null +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ignore_module.py @@ -0,0 +1,36 @@ +# Copyright (c) 2021 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 astor +import scipy + +from paddle.jit import ignore_module +from paddle.jit.dy2static.convert_call_func import BUILTIN_LIKELY_MODULES + + +class TestIgnoreModule(unittest.TestCase): + def test_ignore_module(self): + modules = [scipy, astor] + ignore_module(modules) + self.assertEquals( + [scipy, astor], + BUILTIN_LIKELY_MODULES[-2:], + 'Failed to add modules that ignore transcription', + ) + + +if __name__ == '__main__': + unittest.main()