Skip to content

[Dy2St] Add ignore_module API #49485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions python/paddle/jit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,7 @@
'save',
'load',
'to_static',
'ignore_module',
'ProgramTranslator',
'TranslatedLayer',
'set_code_level',
Expand Down
31 changes: 30 additions & 1 deletion python/paddle/jit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +43,7 @@
from .dy2static.convert_call_func import (
ConversionOptions,
CONVERSION_OPTIONS,
add_ignore_module,
)
from .dy2static.program_translator import (
ProgramTranslator,
Expand Down Expand Up @@ -192,6 +193,34 @@ def copy_decorator_attrs(original_func, decorated_obj):
return decorated_obj


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[Any]): Ignored modules that you want to add

Examples:
.. code-block:: python

import scipy
import astor

import paddle
from paddle.jit import ignore_module

modules = [
scipy,
astor
]

ignore_module(modules)

"""
add_ignore_module(modules)


def to_static(
function=None, input_spec=None, build_strategy=None, property=False
):
Expand Down
11 changes: 11 additions & 0 deletions python/paddle/jit/dy2static/convert_call_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pdb
import re
import types
from typing import Any, List

import numpy

Expand Down Expand Up @@ -100,6 +101,16 @@ def builtin_modules():
BUILTIN_LIKELY_MODULES = builtin_modules()


def add_ignore_module(modules: List[Any]):
"""
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.
Expand Down